]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnml.el
(close-rectangle): New command.
[gnu-emacs] / lisp / gnus / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41 "Spool directory for the nnml mail backend.")
42
43 (defvoo nnml-active-file
44 (concat (file-name-as-directory nnml-directory) "active")
45 "Mail active file.")
46
47 (defvoo nnml-newsgroups-file
48 (concat (file-name-as-directory nnml-directory) "newsgroups")
49 "Mail newsgroups description file.")
50
51 (defvoo nnml-get-new-mail t
52 "If non-nil, nnml will check the incoming mail file and split the mail.")
53
54 (defvoo nnml-nov-is-evil nil
55 "If non-nil, Gnus will never generate and use nov databases for mail groups.
56 Using nov databases will speed up header fetching considerably.
57 This variable shouldn't be flipped much. If you have, for some reason,
58 set this to t, and want to set it to nil again, you should always run
59 the `nnml-generate-nov-databases' command. The function will go
60 through all nnml directories and generate nov databases for them
61 all. This may very well take some time.")
62
63 (defvoo nnml-prepare-save-mail-hook nil
64 "Hook run narrowed to an article before saving.")
65
66 (defvoo nnml-inhibit-expiry nil
67 "If non-nil, inhibit expiry.")
68
69
70 \f
71
72 (defconst nnml-version "nnml 1.0"
73 "nnml version.")
74
75 (defvoo nnml-nov-file-name ".overview")
76
77 (defvoo nnml-current-directory nil)
78 (defvoo nnml-current-group nil)
79 (defvoo nnml-status-string "")
80 (defvoo nnml-nov-buffer-alist nil)
81 (defvoo nnml-group-alist nil)
82 (defvoo nnml-active-timestamp nil)
83 (defvoo nnml-article-file-alist nil)
84
85 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
86
87 \f
88
89 ;;; Interface functions.
90
91 (nnoo-define-basics nnml)
92
93 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
94 (when (nnml-possibly-change-directory group server)
95 (save-excursion
96 (set-buffer nntp-server-buffer)
97 (erase-buffer)
98 (let ((file nil)
99 (number (length sequence))
100 (count 0)
101 ;; 1997/8/12 by MORIOKA Tomohiko
102 (file-name-coding-system 'binary) ; for Emacs 20
103 (pathname-coding-system 'binary) ; for XEmacs/mule
104 beg article)
105 (if (stringp (car sequence))
106 'headers
107 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
108 'nov
109 (while sequence
110 (setq article (car sequence))
111 (setq file (nnml-article-to-file article))
112 (when (and file
113 (file-exists-p file)
114 (not (file-directory-p file)))
115 (insert (format "221 %d Article retrieved.\n" article))
116 (setq beg (point))
117 (nnheader-insert-head file)
118 (goto-char beg)
119 (if (search-forward "\n\n" nil t)
120 (forward-char -1)
121 (goto-char (point-max))
122 (insert "\n\n"))
123 (insert ".\n")
124 (delete-region (point) (point-max)))
125 (setq sequence (cdr sequence))
126 (setq count (1+ count))
127 (and (numberp nnmail-large-newsgroup)
128 (> number nnmail-large-newsgroup)
129 (zerop (% count 20))
130 (nnheader-message 6 "nnml: Receiving headers... %d%%"
131 (/ (* count 100) number))))
132
133 (and (numberp nnmail-large-newsgroup)
134 (> number nnmail-large-newsgroup)
135 (nnheader-message 6 "nnml: Receiving headers...done"))
136
137 (nnheader-fold-continuation-lines)
138 'headers))))))
139
140 (deffoo nnml-open-server (server &optional defs)
141 (nnoo-change-server 'nnml server defs)
142 (when (not (file-exists-p nnml-directory))
143 (condition-case ()
144 (make-directory nnml-directory t)
145 (error)))
146 (cond
147 ((not (file-exists-p nnml-directory))
148 (nnml-close-server)
149 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
150 ((not (file-directory-p (file-truename nnml-directory)))
151 (nnml-close-server)
152 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
153 (t
154 (nnheader-report 'nnml "Opened server %s using directory %s"
155 server nnml-directory)
156 t)))
157
158 (defun nnml-request-regenerate (server)
159 (nnml-possibly-change-directory nil server)
160 (nnml-generate-nov-databases)
161 t)
162
163 (deffoo nnml-request-article (id &optional group server buffer)
164 (nnml-possibly-change-directory group server)
165 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
166 ;; 1997/8/12 by MORIOKA Tomohiko
167 (file-name-coding-system 'binary) ; for Emacs 20
168 (pathname-coding-system 'binary) ; for XEmacs/mule
169 path gpath group-num)
170 (if (stringp id)
171 (when (and (setq group-num (nnml-find-group-number id))
172 (cdr
173 (assq (cdr group-num)
174 (nnheader-article-to-file-alist
175 (setq gpath
176 (nnmail-group-pathname
177 (car group-num)
178 nnml-directory))))))
179 (setq path (concat gpath (int-to-string (cdr group-num)))))
180 (setq path (nnml-article-to-file id)))
181 (cond
182 ((not path)
183 (nnheader-report 'nnml "No such article: %s" id))
184 ((not (file-exists-p path))
185 (nnheader-report 'nnml "No such file: %s" path))
186 ((file-directory-p path)
187 (nnheader-report 'nnml "File is a directory: %s" path))
188 ((not (save-excursion (nnmail-find-file path)))
189 (nnheader-report 'nnml "Couldn't read file: %s" path))
190 (t
191 (nnheader-report 'nnml "Article %s retrieved" id)
192 ;; We return the article number.
193 (cons (if group-num (car group-num) group)
194 (string-to-int (file-name-nondirectory path)))))))
195
196 (deffoo nnml-request-group (group &optional server dont-check)
197 ;; 1997/8/12 by MORIOKA Tomohiko
198 (let ((file-name-coding-system 'binary) ; for Emacs 20
199 (pathname-coding-system 'binary)) ; for XEmacs/mule
200 (cond
201 ((not (nnml-possibly-change-directory group server))
202 (nnheader-report 'nnml "Invalid group (no such directory)"))
203 ((not (file-exists-p nnml-current-directory))
204 (nnheader-report 'nnml "Directory %s does not exist"
205 nnml-current-directory))
206 ((not (file-directory-p nnml-current-directory))
207 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
208 (dont-check
209 (nnheader-report 'nnml "Group %s selected" group)
210 t)
211 (t
212 (nnheader-re-read-dir nnml-current-directory)
213 (nnmail-activate 'nnml)
214 (let ((active (nth 1 (assoc group nnml-group-alist))))
215 (if (not active)
216 (nnheader-report 'nnml "No such group: %s" group)
217 (nnheader-report 'nnml "Selected group %s" group)
218 (nnheader-insert "211 %d %d %d %s\n"
219 (max (1+ (- (cdr active) (car active))) 0)
220 (car active) (cdr active) group)))))))
221
222 (deffoo nnml-request-scan (&optional group server)
223 (setq nnml-article-file-alist nil)
224 (nnml-possibly-change-directory group server)
225 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
226
227 (deffoo nnml-close-group (group &optional server)
228 (setq nnml-article-file-alist nil)
229 t)
230
231 (deffoo nnml-request-create-group (group &optional server args)
232 (nnmail-activate 'nnml)
233 (unless (assoc group nnml-group-alist)
234 (let (active)
235 (push (list group (setq active (cons 1 0)))
236 nnml-group-alist)
237 (nnml-possibly-create-directory group)
238 (nnml-possibly-change-directory group server)
239 (let ((articles (nnheader-directory-articles nnml-current-directory)))
240 (when articles
241 (setcar active (apply 'min articles))
242 (setcdr active (apply 'max articles))))
243 (nnmail-save-active nnml-group-alist nnml-active-file)))
244 t)
245
246 (deffoo nnml-request-list (&optional server)
247 (save-excursion
248 ;; 1997/8/12 by MORIOKA Tomohiko
249 ;; for XEmacs/mule.
250 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
251 (file-name-coding-system 'binary) ; for Emacs 20
252 (pathname-coding-system 'binary)) ; for XEmacs/mule
253 (nnmail-find-file nnml-active-file)
254 )
255 (setq nnml-group-alist (nnmail-get-active))
256 t))
257
258 (deffoo nnml-request-newgroups (date &optional server)
259 (nnml-request-list server))
260
261 (deffoo nnml-request-list-newsgroups (&optional server)
262 (save-excursion
263 (nnmail-find-file nnml-newsgroups-file)))
264
265 (deffoo nnml-request-expire-articles (articles group
266 &optional server force)
267 (nnml-possibly-change-directory group server)
268 (let* ((active-articles
269 (nnheader-directory-articles nnml-current-directory))
270 (is-old t)
271 article rest mod-time number)
272 (nnmail-activate 'nnml)
273
274 (while (and articles is-old)
275 (when (setq article (nnml-article-to-file (setq number (pop articles))))
276 (when (setq mod-time (nth 5 (file-attributes article)))
277 (if (and (nnml-deletable-article-p group number)
278 (setq is-old
279 (nnmail-expired-article-p group mod-time force
280 nnml-inhibit-expiry)))
281 (progn
282 (nnheader-message 5 "Deleting article %s in %s"
283 article group)
284 (condition-case ()
285 (funcall nnmail-delete-file-function article)
286 (file-error
287 (push number rest)))
288 (setq active-articles (delq number active-articles))
289 (nnml-nov-delete-article group number))
290 (push number rest)))))
291 (let ((active (nth 1 (assoc group nnml-group-alist))))
292 (when active
293 (setcar active (or (and active-articles
294 (apply 'min active-articles))
295 (1+ (cdr active)))))
296 (nnmail-save-active nnml-group-alist nnml-active-file))
297 (nnml-save-nov)
298 (nconc rest articles)))
299
300 (deffoo nnml-request-move-article
301 (article group server accept-form &optional last)
302 (let ((buf (get-buffer-create " *nnml move*"))
303 result)
304 (nnml-possibly-change-directory group server)
305 (nnml-update-file-alist)
306 (and
307 (nnml-deletable-article-p group article)
308 (nnml-request-article article group server)
309 (save-excursion
310 (set-buffer buf)
311 (insert-buffer-substring nntp-server-buffer)
312 (setq result (eval accept-form))
313 (kill-buffer (current-buffer))
314 result)
315 (progn
316 (nnml-possibly-change-directory group server)
317 (condition-case ()
318 (funcall nnmail-delete-file-function
319 (nnml-article-to-file article))
320 (file-error nil))
321 (nnml-nov-delete-article group article)
322 (when last
323 (nnml-save-nov)
324 (nnmail-save-active nnml-group-alist nnml-active-file))))
325 result))
326
327 (deffoo nnml-request-accept-article (group &optional server last)
328 (nnml-possibly-change-directory group server)
329 (nnmail-check-syntax)
330 (let (result)
331 (when nnmail-cache-accepted-message-ids
332 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
333 (if (stringp group)
334 (and
335 (nnmail-activate 'nnml)
336 (setq result (car (nnml-save-mail
337 (list (cons group (nnml-active-number group))))))
338 (progn
339 (nnmail-save-active nnml-group-alist nnml-active-file)
340 (and last (nnml-save-nov))))
341 (and
342 (nnmail-activate 'nnml)
343 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
344 (yes-or-no-p "Moved to `junk' group; delete article? "))
345 (setq result 'junk)
346 (setq result (car (nnml-save-mail result))))
347 (when last
348 (nnmail-save-active nnml-group-alist nnml-active-file)
349 (when nnmail-cache-accepted-message-ids
350 (nnmail-cache-close))
351 (nnml-save-nov))))
352 result))
353
354 (deffoo nnml-request-replace-article (article group buffer)
355 (nnml-possibly-change-directory group)
356 (save-excursion
357 (set-buffer buffer)
358 (nnml-possibly-create-directory group)
359 (let ((chars (nnmail-insert-lines))
360 (art (concat (int-to-string article) "\t"))
361 headers)
362 (when (condition-case ()
363 (progn
364 (nnmail-write-region
365 (point-min) (point-max)
366 (or (nnml-article-to-file article)
367 (concat nnml-current-directory
368 (int-to-string article)))
369 nil (if (nnheader-be-verbose 5) nil 'nomesg))
370 t)
371 (error nil))
372 (setq headers (nnml-parse-head chars article))
373 ;; Replace the NOV line in the NOV file.
374 (save-excursion
375 (set-buffer (nnml-open-nov group))
376 (goto-char (point-min))
377 (if (or (looking-at art)
378 (search-forward (concat "\n" art) nil t))
379 ;; Delete the old NOV line.
380 (delete-region (progn (beginning-of-line) (point))
381 (progn (forward-line 1) (point)))
382 ;; The line isn't here, so we have to find out where
383 ;; we should insert it. (This situation should never
384 ;; occur, but one likes to make sure...)
385 (while (and (looking-at "[0-9]+\t")
386 (< (string-to-int
387 (buffer-substring
388 (match-beginning 0) (match-end 0)))
389 article)
390 (zerop (forward-line 1)))))
391 (beginning-of-line)
392 (nnheader-insert-nov headers)
393 (nnml-save-nov)
394 t)))))
395
396 (deffoo nnml-request-delete-group (group &optional force server)
397 (nnml-possibly-change-directory group server)
398 (when force
399 ;; Delete all articles in GROUP.
400 (let ((articles
401 (directory-files
402 nnml-current-directory t
403 (concat nnheader-numerical-short-files
404 "\\|" (regexp-quote nnml-nov-file-name) "$")))
405 article)
406 (while articles
407 (setq article (pop articles))
408 (when (file-writable-p article)
409 (nnheader-message 5 "Deleting article %s in %s..." article group)
410 (funcall nnmail-delete-file-function article))))
411 ;; Try to delete the directory itself.
412 (condition-case ()
413 (delete-directory nnml-current-directory)
414 (error nil)))
415 ;; Remove the group from all structures.
416 (setq nnml-group-alist
417 (delq (assoc group nnml-group-alist) nnml-group-alist)
418 nnml-current-group nil
419 nnml-current-directory nil)
420 ;; Save the active file.
421 (nnmail-save-active nnml-group-alist nnml-active-file)
422 t)
423
424 (deffoo nnml-request-rename-group (group new-name &optional server)
425 (nnml-possibly-change-directory group server)
426 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
427 (old-dir (nnmail-group-pathname group nnml-directory)))
428 (when (condition-case ()
429 (progn
430 (make-directory new-dir t)
431 t)
432 (error nil))
433 ;; We move the articles file by file instead of renaming
434 ;; the directory -- there may be subgroups in this group.
435 ;; One might be more clever, I guess.
436 (let ((files (nnheader-article-to-file-alist old-dir)))
437 (while files
438 (rename-file
439 (concat old-dir (cdar files))
440 (concat new-dir (cdar files)))
441 (pop files)))
442 ;; Move .overview file.
443 (let ((overview (concat old-dir nnml-nov-file-name)))
444 (when (file-exists-p overview)
445 (rename-file overview (concat new-dir nnml-nov-file-name))))
446 (when (<= (length (directory-files old-dir)) 2)
447 (condition-case ()
448 (delete-directory old-dir)
449 (error nil)))
450 ;; That went ok, so we change the internal structures.
451 (let ((entry (assoc group nnml-group-alist)))
452 (when entry
453 (setcar entry new-name))
454 (setq nnml-current-directory nil
455 nnml-current-group nil)
456 ;; Save the new group alist.
457 (nnmail-save-active nnml-group-alist nnml-active-file)
458 t))))
459
460 (deffoo nnml-set-status (article name value &optional group server)
461 (nnml-possibly-change-directory group server)
462 (let ((file (nnml-article-to-file article)))
463 (cond
464 ((not (file-exists-p file))
465 (nnheader-report 'nnml "File %s does not exist" file))
466 (t
467 (nnheader-temp-write file
468 (nnheader-insert-file-contents file)
469 (nnmail-replace-status name value))
470 t))))
471
472 \f
473 ;;; Internal functions.
474
475 (defun nnml-article-to-file (article)
476 (nnml-update-file-alist)
477 (let (file)
478 (if (setq file (cdr (assq article nnml-article-file-alist)))
479 (concat nnml-current-directory file)
480 ;; Just to make sure nothing went wrong when reading over NFS --
481 ;; check once more.
482 (when (file-exists-p
483 (setq file (concat nnml-current-directory "/"
484 (number-to-string article))))
485 (nnml-update-file-alist t)
486 file))))
487
488 (defun nnml-deletable-article-p (group article)
489 "Say whether ARTICLE in GROUP can be deleted."
490 (let (path)
491 (when (setq path (nnml-article-to-file article))
492 (when (file-writable-p path)
493 (or (not nnmail-keep-last-article)
494 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
495 article)))))))
496
497 ;; Find an article number in the current group given the Message-ID.
498 (defun nnml-find-group-number (id)
499 (save-excursion
500 (set-buffer (get-buffer-create " *nnml id*"))
501 (buffer-disable-undo (current-buffer))
502 (let ((alist nnml-group-alist)
503 number)
504 ;; We want to look through all .overview files, but we want to
505 ;; start with the one in the current directory. It seems most
506 ;; likely that the article we are looking for is in that group.
507 (if (setq number (nnml-find-id nnml-current-group id))
508 (cons nnml-current-group number)
509 ;; It wasn't there, so we look through the other groups as well.
510 (while (and (not number)
511 alist)
512 (or (string= (caar alist) nnml-current-group)
513 (setq number (nnml-find-id (caar alist) id)))
514 (or number
515 (setq alist (cdr alist))))
516 (and number
517 (cons (caar alist) number))))))
518
519 (defun nnml-find-id (group id)
520 (erase-buffer)
521 (let ((nov (concat (nnmail-group-pathname group nnml-directory)
522 nnml-nov-file-name))
523 number found)
524 (when (file-exists-p nov)
525 (nnheader-insert-file-contents nov)
526 (while (and (not found)
527 (search-forward id nil t)) ; We find the ID.
528 ;; And the id is in the fourth field.
529 (if (not (and (search-backward "\t" nil t 4)
530 (not (search-backward"\t" (gnus-point-at-bol) t))))
531 (forward-line 1)
532 (beginning-of-line)
533 (setq found t)
534 ;; We return the article number.
535 (setq number
536 (condition-case ()
537 (read (current-buffer))
538 (error nil)))))
539 number)))
540
541 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
542 (if (or gnus-nov-is-evil nnml-nov-is-evil)
543 nil
544 (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
545 (when (file-exists-p nov)
546 (save-excursion
547 (set-buffer nntp-server-buffer)
548 (erase-buffer)
549 (nnheader-insert-file-contents nov)
550 (if (and fetch-old
551 (not (numberp fetch-old)))
552 t ; Don't remove anything.
553 (nnheader-nov-delete-outside-range
554 (if fetch-old (max 1 (- (car articles) fetch-old))
555 (car articles))
556 (car (last articles)))
557 t))))))
558
559 (defun nnml-possibly-change-directory (group &optional server)
560 (when (and server
561 (not (nnml-server-opened server)))
562 (nnml-open-server server))
563 (if (not group)
564 t
565 (let ((pathname (nnmail-group-pathname group nnml-directory))
566 ;; 1997/8/14 by MORIOKA Tomohiko
567 (file-name-coding-system 'binary) ; for Emacs 20
568 (pathname-coding-system 'binary)) ; for XEmacs/mule
569 (when (not (equal pathname nnml-current-directory))
570 (setq nnml-current-directory pathname
571 nnml-current-group group
572 nnml-article-file-alist nil))
573 (file-exists-p nnml-current-directory))))
574
575 (defun nnml-possibly-create-directory (group)
576 (let (dir dirs)
577 (setq dir (nnmail-group-pathname group nnml-directory))
578 (while (not (file-directory-p dir))
579 (push dir dirs)
580 (setq dir (file-name-directory (directory-file-name dir))))
581 (while dirs
582 (make-directory (directory-file-name (car dirs)))
583 (nnheader-message 5 "Creating mail directory %s" (car dirs))
584 (setq dirs (cdr dirs)))))
585
586 (defun nnml-save-mail (group-art)
587 "Called narrowed to an article."
588 (let (chars headers)
589 (setq chars (nnmail-insert-lines))
590 (nnmail-insert-xref group-art)
591 (run-hooks 'nnmail-prepare-save-mail-hook)
592 (run-hooks 'nnml-prepare-save-mail-hook)
593 (goto-char (point-min))
594 (while (looking-at "From ")
595 (replace-match "X-From-Line: ")
596 (forward-line 1))
597 ;; We save the article in all the groups it belongs in.
598 (let ((ga group-art)
599 first)
600 (while ga
601 (nnml-possibly-create-directory (caar ga))
602 (let ((file (concat (nnmail-group-pathname
603 (caar ga) nnml-directory)
604 (int-to-string (cdar ga)))))
605 (if first
606 ;; It was already saved, so we just make a hard link.
607 (funcall nnmail-crosspost-link-function first file t)
608 ;; Save the article.
609 (nnmail-write-region (point-min) (point-max) file nil
610 (if (nnheader-be-verbose 5) nil 'nomesg))
611 (setq first file)))
612 (setq ga (cdr ga))))
613 ;; Generate a nov line for this article. We generate the nov
614 ;; line after saving, because nov generation destroys the
615 ;; header.
616 (setq headers (nnml-parse-head chars))
617 ;; Output the nov line to all nov databases that should have it.
618 (let ((ga group-art))
619 (while ga
620 (nnml-add-nov (caar ga) (cdar ga) headers)
621 (setq ga (cdr ga))))
622 group-art))
623
624 (defun nnml-active-number (group)
625 "Compute the next article number in GROUP."
626 (let ((active (cadr (assoc group nnml-group-alist))))
627 ;; The group wasn't known to nnml, so we just create an active
628 ;; entry for it.
629 (unless active
630 ;; Perhaps the active file was corrupt? See whether
631 ;; there are any articles in this group.
632 (nnml-possibly-create-directory group)
633 (nnml-possibly-change-directory group)
634 (unless nnml-article-file-alist
635 (setq nnml-article-file-alist
636 (sort
637 (nnheader-article-to-file-alist nnml-current-directory)
638 (lambda (a1 a2) (< (car a1) (car a2))))))
639 (setq active
640 (if nnml-article-file-alist
641 (cons (caar nnml-article-file-alist)
642 (caar (last nnml-article-file-alist)))
643 (cons 1 0)))
644 (push (list group active) nnml-group-alist))
645 (setcdr active (1+ (cdr active)))
646 (while (file-exists-p
647 (concat (nnmail-group-pathname group nnml-directory)
648 (int-to-string (cdr active))))
649 (setcdr active (1+ (cdr active))))
650 (cdr active)))
651
652 (defun nnml-add-nov (group article headers)
653 "Add a nov line for the GROUP base."
654 (save-excursion
655 (set-buffer (nnml-open-nov group))
656 (goto-char (point-max))
657 (mail-header-set-number headers article)
658 (nnheader-insert-nov headers)))
659
660 (defsubst nnml-header-value ()
661 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
662
663 (defun nnml-parse-head (chars &optional number)
664 "Parse the head of the current buffer."
665 (save-excursion
666 (save-restriction
667 (goto-char (point-min))
668 (narrow-to-region
669 (point)
670 (1- (or (search-forward "\n\n" nil t) (point-max))))
671 ;; Fold continuation lines.
672 (goto-char (point-min))
673 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
674 (replace-match " " t t))
675 ;; Remove any tabs; they are too confusing.
676 (subst-char-in-region (point-min) (point-max) ?\t ? )
677 (let ((headers (nnheader-parse-head t)))
678 (mail-header-set-chars headers chars)
679 (mail-header-set-number headers number)
680 headers))))
681
682 (defun nnml-open-nov (group)
683 (or (cdr (assoc group nnml-nov-buffer-alist))
684 (let ((buffer (nnheader-find-file-noselect
685 (concat (nnmail-group-pathname group nnml-directory)
686 nnml-nov-file-name))))
687 (save-excursion
688 (set-buffer buffer)
689 (buffer-disable-undo (current-buffer)))
690 (push (cons group buffer) nnml-nov-buffer-alist)
691 buffer)))
692
693 (defun nnml-save-nov ()
694 (save-excursion
695 (while nnml-nov-buffer-alist
696 (when (buffer-name (cdar nnml-nov-buffer-alist))
697 (set-buffer (cdar nnml-nov-buffer-alist))
698 (when (buffer-modified-p)
699 (nnmail-write-region 1 (point-max) (buffer-file-name) nil 'nomesg))
700 (set-buffer-modified-p nil)
701 (kill-buffer (current-buffer)))
702 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
703
704 ;;;###autoload
705 (defun nnml-generate-nov-databases ()
706 "Generate NOV databases in all nnml directories."
707 (interactive)
708 ;; Read the active file to make sure we don't re-use articles
709 ;; numbers in empty groups.
710 (nnmail-activate 'nnml)
711 (nnml-open-server (or (nnoo-current-server 'nnml) ""))
712 (setq nnml-directory (expand-file-name nnml-directory))
713 ;; Recurse down the directories.
714 (nnml-generate-nov-databases-1 nnml-directory nil t)
715 ;; Save the active file.
716 (nnmail-save-active nnml-group-alist nnml-active-file))
717
718 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
719 "Regenerate the NOV database in DIR."
720 (interactive "DRegenerate NOV in: ")
721 (setq dir (file-name-as-directory dir))
722 ;; Only scan this sub-tree if we haven't been here yet.
723 (unless (member (file-truename dir) seen)
724 (push (file-truename dir) seen)
725 ;; We descend recursively
726 (let ((dirs (directory-files dir t nil t))
727 dir)
728 (while (setq dir (pop dirs))
729 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
730 (file-directory-p dir))
731 (nnml-generate-nov-databases-1 dir seen))))
732 ;; Do this directory.
733 (let ((files (sort (nnheader-article-to-file-alist dir)
734 (lambda (a b) (< (car a) (car b))))))
735 (when files
736 (funcall nnml-generate-active-function dir)
737 ;; Generate the nov file.
738 (nnml-generate-nov-file dir files)
739 (unless no-active
740 (nnmail-save-active nnml-group-alist nnml-active-file))))))
741
742 (defvar files)
743 (defun nnml-generate-active-info (dir)
744 ;; Update the active info for this group.
745 (let ((group (nnheader-file-to-group
746 (directory-file-name dir) nnml-directory)))
747 (setq nnml-group-alist
748 (delq (assoc group nnml-group-alist) nnml-group-alist))
749 (push (list group
750 (cons (caar files)
751 (let ((f files))
752 (while (cdr f) (setq f (cdr f)))
753 (caar f))))
754 nnml-group-alist)))
755
756 (defun nnml-generate-nov-file (dir files)
757 (let* ((dir (file-name-as-directory dir))
758 (nov (concat dir nnml-nov-file-name))
759 (nov-buffer (get-buffer-create " *nov*"))
760 chars file headers)
761 (save-excursion
762 ;; Init the nov buffer.
763 (set-buffer nov-buffer)
764 (buffer-disable-undo (current-buffer))
765 (erase-buffer)
766 (set-buffer nntp-server-buffer)
767 ;; Delete the old NOV file.
768 (when (file-exists-p nov)
769 (funcall nnmail-delete-file-function nov))
770 (while files
771 (unless (file-directory-p (setq file (concat dir (cdar files))))
772 (erase-buffer)
773 (nnheader-insert-file-contents file)
774 (narrow-to-region
775 (goto-char (point-min))
776 (progn
777 (search-forward "\n\n" nil t)
778 (setq chars (- (point-max) (point)))
779 (max 1 (1- (point)))))
780 (unless (zerop (buffer-size))
781 (goto-char (point-min))
782 (setq headers (nnml-parse-head chars (caar files)))
783 (save-excursion
784 (set-buffer nov-buffer)
785 (goto-char (point-max))
786 (nnheader-insert-nov headers)))
787 (widen))
788 (setq files (cdr files)))
789 (save-excursion
790 (set-buffer nov-buffer)
791 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
792 (kill-buffer (current-buffer))))))
793
794 (defun nnml-nov-delete-article (group article)
795 (save-excursion
796 (set-buffer (nnml-open-nov group))
797 (when (nnheader-find-nov-line article)
798 (delete-region (point) (progn (forward-line 1) (point)))
799 (when (bobp)
800 (let ((active (cadr (assoc group nnml-group-alist)))
801 num)
802 (when active
803 (if (eobp)
804 (setf (car active) (1+ (cdr active)))
805 (when (and (setq num (ignore-errors (read (current-buffer))))
806 (numberp num))
807 (setf (car active) num)))))))
808 t))
809
810 (defun nnml-update-file-alist (&optional force)
811 (when (or (not nnml-article-file-alist)
812 force)
813 (setq nnml-article-file-alist
814 (nnheader-article-to-file-alist nnml-current-directory))))
815
816 (provide 'nnml)
817
818 ;;; nnml.el ends here