cdc5ef5429adda8bac643d534d138f057dd36192
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96 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 (require 'cl)
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41   "Mail spool directory.")
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 newsgroup server fetch-old)
94   (save-excursion
95     (set-buffer nntp-server-buffer)
96     (erase-buffer)
97     (let ((file nil)
98           (number (length sequence))
99           (count 0)
100           beg article)
101       (if (stringp (car sequence))
102           'headers
103         (nnml-possibly-change-directory newsgroup server)
104         (unless nnml-article-file-alist
105           (setq nnml-article-file-alist
106                 (nnheader-article-to-file-alist nnml-current-directory)))
107         (if (nnml-retrieve-headers-with-nov sequence fetch-old)
108             'nov
109           (while sequence
110             (setq article (car sequence))
111             (setq file 
112                   (concat nnml-current-directory 
113                           (or (cdr (assq article nnml-article-file-alist))
114                               "")))
115             (if (and (file-exists-p file)
116                      (not (file-directory-p file)))
117                 (progn
118                   (insert (format "221 %d Article retrieved.\n" article))
119                   (setq beg (point))
120                   (nnheader-insert-head file)
121                   (goto-char beg)
122                   (if (search-forward "\n\n" nil t)
123                       (forward-char -1)
124                     (goto-char (point-max))
125                     (insert "\n\n"))
126                   (insert ".\n")
127                   (delete-region (point) (point-max))))
128             (setq sequence (cdr sequence))
129             (setq count (1+ count))
130             (and (numberp nnmail-large-newsgroup)
131                  (> number nnmail-large-newsgroup)
132                  (zerop (% count 20))
133                  (nnheader-message 6 "nnml: Receiving headers... %d%%"
134                                    (/ (* count 100) number))))
135
136           (and (numberp nnmail-large-newsgroup)
137                (> number nnmail-large-newsgroup)
138                (nnheader-message 6 "nnml: Receiving headers...done"))
139
140           (nnheader-fold-continuation-lines)
141           'headers)))))
142
143 (deffoo nnml-open-server (server &optional defs)
144   (nnoo-change-server 'nnml server defs)
145   (when (not (file-exists-p nnml-directory))
146     (condition-case ()
147         (make-directory nnml-directory t)
148       (error t)))
149   (cond 
150    ((not (file-exists-p nnml-directory))
151     (nnml-close-server)
152     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
153    ((not (file-directory-p (file-truename nnml-directory)))
154     (nnml-close-server)
155     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
156    (t
157     (nnheader-report 'nnml "Opened server %s using directory %s"
158                      server nnml-directory)
159     t)))
160
161 (deffoo nnml-request-article (id &optional newsgroup server buffer)
162   (nnml-possibly-change-directory newsgroup server)
163   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
164          file path gpath group-num)
165     (if (stringp id)
166         (when (and (setq group-num (nnml-find-group-number id))
167                    (setq file (cdr
168                                (assq (cdr group-num) 
169                                      (nnheader-article-to-file-alist
170                                       (setq gpath
171                                             (nnmail-group-pathname
172                                              (car group-num) 
173                                              nnml-directory)))))))
174           (setq path (concat gpath (int-to-string (cdr group-num)))))
175       (unless nnml-article-file-alist
176         (setq nnml-article-file-alist
177               (nnheader-article-to-file-alist nnml-current-directory)))
178       (when (setq file (cdr (assq id nnml-article-file-alist)))
179         (setq path (concat nnml-current-directory file))))
180     (cond 
181      ((not path)
182       (nnheader-report 'nnml "No such article: %s" id))
183      ((not (file-exists-p path))
184       (nnheader-report 'nnml "No such file: %s" path))
185      ((file-directory-p path)
186       (nnheader-report 'nnml "File is a directory: %s" path))
187      ((not (save-excursion (nnmail-find-file path)))
188       (nnheader-report 'nnml "Couldn't read file: %s" path))
189      (t
190       (nnheader-report 'nnml "Article %s retrieved" id)
191       ;; We return the article number.
192       (cons newsgroup (string-to-int (file-name-nondirectory path)))))))
193
194 (deffoo nnml-request-group (group &optional server dont-check)
195   (cond 
196    ((not (nnml-possibly-change-directory group server))
197     (nnheader-report 'nnml "Invalid group (no such directory)"))
198    ((not (file-directory-p nnml-current-directory))
199     (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
200    (dont-check 
201     (nnheader-report 'nnml "Group %s selected" group)
202     t)
203    (t
204     (nnmail-activate 'nnml)
205     (let ((active (nth 1 (assoc group nnml-group-alist))))
206       (if (not active)
207           (nnheader-report 'nnml "No such group: %s" group)
208         (nnheader-report 'nnml "Selected group %s" group)
209         (nnheader-insert "211 %d %d %d %s\n" 
210                          (max (1+ (- (cdr active) (car active))) 0)
211                          (car active) (cdr active) group))))))
212
213 (deffoo nnml-request-scan (&optional group server)
214   (setq nnml-article-file-alist nil)
215   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
216
217 (deffoo nnml-close-group (group &optional server)
218   (setq nnml-article-file-alist nil)
219   t)
220
221 (deffoo nnml-request-create-group (group &optional server) 
222   (nnmail-activate 'nnml)
223   (or (assoc group nnml-group-alist)
224       (let (active)
225         (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
226                                      nnml-group-alist))
227         (nnml-possibly-create-directory group)
228         (nnml-possibly-change-directory group server)
229         (let ((articles 
230                (nnheader-directory-articles nnml-current-directory )))
231           (and articles
232                (progn
233                  (setcar active (apply 'min articles))
234                  (setcdr active (apply 'max articles)))))
235         (nnmail-save-active nnml-group-alist nnml-active-file)))
236   t)
237
238 (deffoo nnml-request-list (&optional server)
239   (save-excursion
240     (nnmail-find-file nnml-active-file)
241     (setq nnml-group-alist (nnmail-get-active))))
242
243 (deffoo nnml-request-newgroups (date &optional server)
244   (nnml-request-list server))
245
246 (deffoo nnml-request-list-newsgroups (&optional server)
247   (save-excursion
248     (nnmail-find-file nnml-newsgroups-file)))
249
250 (deffoo nnml-request-expire-articles (articles newsgroup &optional server force)
251   (nnml-possibly-change-directory newsgroup server)
252   (let* ((active-articles 
253           (nnheader-directory-articles nnml-current-directory))
254          (is-old t)
255          article rest mod-time number)
256     (nnmail-activate 'nnml)
257
258     (unless nnml-article-file-alist
259       (setq nnml-article-file-alist
260             (nnheader-article-to-file-alist nnml-current-directory)))
261
262     (while (and articles is-old)
263       (setq article (concat nnml-current-directory 
264                             (int-to-string 
265                              (setq number (pop articles)))))
266       (when (setq mod-time (nth 5 (file-attributes article)))
267         (if (and (nnml-deletable-article-p newsgroup number)
268                  (setq is-old 
269                        (nnmail-expired-article-p newsgroup mod-time force
270                                                  nnml-inhibit-expiry)))
271             (progn
272               (nnheader-message 5 "Deleting article %s in %s..."
273                                 article newsgroup)
274               (condition-case ()
275                   (funcall nnmail-delete-file-function article)
276                 (file-error
277                  (push number rest)))
278               (setq active-articles (delq number active-articles))
279               (nnml-nov-delete-article newsgroup number))
280           (push number rest))))
281     (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
282       (when active
283         (setcar active (or (and active-articles
284                                 (apply 'min active-articles))
285                            (1+ (cdr active)))))
286       (nnmail-save-active nnml-group-alist nnml-active-file))
287     (nnml-save-nov)
288     (message "")
289     (nconc rest articles)))
290
291 (deffoo nnml-request-move-article 
292   (article group server accept-form &optional last)
293   (let ((buf (get-buffer-create " *nnml move*"))
294         result)
295     (nnml-possibly-change-directory group server)
296     (unless nnml-article-file-alist
297       (setq nnml-article-file-alist
298             (nnheader-article-to-file-alist nnml-current-directory)))
299     (and 
300      (nnml-deletable-article-p group article)
301      (nnml-request-article article group server)
302      (save-excursion
303        (set-buffer buf)
304        (insert-buffer-substring nntp-server-buffer)
305        (setq result (eval accept-form))
306        (kill-buffer (current-buffer))
307        result)
308      (progn
309        (nnml-possibly-change-directory group server)
310        (condition-case ()
311            (funcall nnmail-delete-file-function
312                     (concat nnml-current-directory 
313                             (int-to-string article)))
314          (file-error nil))
315        (nnml-nov-delete-article group article)
316        (and last (nnml-save-nov))))
317     result))
318
319 (deffoo nnml-request-accept-article (group &optional server last)
320   (nnml-possibly-change-directory group server)
321   (let (result)
322     (if (stringp group)
323         (and 
324          (nnmail-activate 'nnml)
325          ;; We trick the choosing function into believing that only one
326          ;; group is available.  
327          (let ((nnmail-split-methods (list (list group ""))))
328            (setq result (car (nnml-save-mail))))
329          (progn
330            (nnmail-save-active nnml-group-alist nnml-active-file)
331            (and last (nnml-save-nov))))
332       (and
333        (nnmail-activate 'nnml)
334        (setq result (car (nnml-save-mail)))
335        (progn
336          (nnmail-save-active nnml-group-alist nnml-active-file)
337          (and last (nnml-save-nov)))))
338     result))
339
340 (deffoo nnml-request-replace-article (article group buffer)
341   (nnml-possibly-change-directory group)
342   (save-excursion
343     (set-buffer buffer)
344     (nnml-possibly-create-directory group)
345     (when (condition-case ()
346                  (progn
347                    (write-region 
348                     (point-min) (point-max)
349                     (concat nnml-current-directory (int-to-string article))
350                     nil (if (nnheader-be-verbose 5) nil 'nomesg))
351                    t)
352                (error nil))
353       (let ((chars (nnmail-insert-lines))
354             (art (concat (int-to-string article) "\t"))
355             headers)
356         (setq headers (nnml-parse-head chars article))
357         ;; Replace the NOV line in the NOV file.
358         (save-excursion 
359           (set-buffer (nnml-open-nov group))
360           (goto-char (point-min))
361           (if (or (looking-at art)
362                   (search-forward (concat "\n" art) nil t))
363               ;; Delete the old NOV line.
364               (delete-region (progn (beginning-of-line) (point))
365                              (progn (forward-line 1) (point)))
366             ;; The line isn't here, so we have to find out where
367             ;; we should insert it. (This situation should never
368             ;; occur, but one likes to make sure...)
369             (while (and (looking-at "[0-9]+\t")
370                         (< (string-to-int 
371                             (buffer-substring 
372                              (match-beginning 0) (match-end 0)))
373                            article)
374                         (zerop (forward-line 1)))))
375           (beginning-of-line)
376           (nnheader-insert-nov headers)
377           (nnml-save-nov)
378           t)))))
379
380 (deffoo nnml-request-delete-group (group &optional force server)
381   (nnml-possibly-change-directory group server)
382   (when force
383     ;; Delete all articles in GROUP.
384     (let ((articles 
385            (directory-files 
386             nnml-current-directory t
387             (concat nnheader-numerical-short-files
388                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
389           article)
390       (while articles 
391         (setq article (pop articles))
392         (when (file-writable-p article)
393           (nnheader-message 5 "Deleting article %s in %s..." article group)
394           (funcall nnmail-delete-file-function article))))
395     ;; Try to delete the directory itself.
396     (condition-case ()
397         (delete-directory nnml-current-directory)
398       (error nil)))
399   ;; Remove the group from all structures.
400   (setq nnml-group-alist 
401         (delq (assoc group nnml-group-alist) nnml-group-alist)
402         nnml-current-group nil
403         nnml-current-directory nil)
404   ;; Save the active file.
405   (nnmail-save-active nnml-group-alist nnml-active-file)
406   t)
407
408 (deffoo nnml-request-rename-group (group new-name &optional server)
409   (nnml-possibly-change-directory group server)
410   ;; Rename directory.
411   (and (file-writable-p nnml-current-directory)
412        (condition-case ()
413            (let ((parent 
414                   (file-name-directory
415                    (directory-file-name 
416                     (nnmail-group-pathname new-name nnml-directory)))))
417              (unless (file-exists-p parent)
418                (make-directory parent t))
419              (rename-file 
420               (directory-file-name nnml-current-directory)
421               (directory-file-name 
422                (nnmail-group-pathname new-name nnml-directory)))
423              t)
424          (error nil))
425        ;; That went ok, so we change the internal structures.
426        (let ((entry (assoc group nnml-group-alist)))
427          (and entry (setcar entry new-name))
428          (setq nnml-current-directory nil
429                nnml-current-group nil)
430          ;; Save the new group alist.
431          (nnmail-save-active nnml-group-alist nnml-active-file)
432          t)))
433
434 \f
435 ;;; Internal functions.
436
437 (defun nnml-deletable-article-p (group article)
438   "Say whether ARTICLE in GROUP can be deleted."
439   (let (file path)
440     (when (setq file (cdr (assq article nnml-article-file-alist)))
441       (setq path (concat nnml-current-directory file))
442       (and (file-writable-p path)
443            (or (not nnmail-keep-last-article)
444                (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) 
445                         article)))))))
446
447 ;; Find an article number in the current group given the Message-ID. 
448 (defun nnml-find-group-number (id)
449   (save-excursion
450     (set-buffer (get-buffer-create " *nnml id*"))
451     (buffer-disable-undo (current-buffer))
452     (let ((alist nnml-group-alist)
453           number)
454       ;; We want to look through all .overview files, but we want to
455       ;; start with the one in the current directory.  It seems most
456       ;; likely that the article we are looking for is in that group. 
457       (if (setq number (nnml-find-id nnml-current-group id))
458           (cons nnml-current-group number)
459         ;; It wasn't there, so we look through the other groups as well.
460         (while (and (not number)
461                     alist)
462           (or (string= (caar alist) nnml-current-group)
463               (setq number (nnml-find-id (caar alist) id)))
464           (or number
465               (setq alist (cdr alist))))
466         (and number
467              (cons (caar alist) number))))))
468
469 (defun nnml-find-id (group id)
470   (erase-buffer)
471   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
472                      nnml-nov-file-name))
473         number found)
474     (when (file-exists-p nov)
475       (insert-file-contents nov)
476       (while (and (not found) 
477                   (search-forward id nil t)) ; We find the ID.
478         ;; And the id is in the fourth field.
479         (if (search-backward 
480              "\t" (save-excursion (beginning-of-line) (point)) t 4)
481             (progn
482               (beginning-of-line)
483               (setq found t)
484               ;; We return the article number.
485               (setq number
486                     (condition-case ()
487                         (read (current-buffer))
488                       (error nil))))))
489       number)))
490
491 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
492   (if (or gnus-nov-is-evil nnml-nov-is-evil)
493       nil
494     (let ((first (car articles))
495           (last (progn (while (cdr articles) (setq articles (cdr articles)))
496                        (car articles)))
497           (nov (concat nnml-current-directory nnml-nov-file-name)))
498       (when (file-exists-p nov)
499         (save-excursion
500           (set-buffer nntp-server-buffer)
501           (erase-buffer)
502           (insert-file-contents nov)
503           (if (and fetch-old
504                    (not (numberp fetch-old)))
505               t                         ; Don't remove anything.
506             (if fetch-old
507                 (setq first (max 1 (- first fetch-old))))
508             (goto-char (point-min))
509             (while (and (not (eobp)) (> first (read (current-buffer))))
510               (forward-line 1))
511             (beginning-of-line)
512             (if (not (eobp)) (delete-region 1 (point)))
513             (while (and (not (eobp)) (>= last (read (current-buffer))))
514               (forward-line 1))
515             (beginning-of-line)
516             (if (not (eobp)) (delete-region (point) (point-max)))
517             t))))))
518
519 (defun nnml-possibly-change-directory (group &optional server)
520   (when (and server
521              (not (nnml-server-opened server)))
522     (nnml-open-server server))
523   (when group
524     (let ((pathname (nnmail-group-pathname group nnml-directory)))
525       (when (not (equal pathname nnml-current-directory))
526         (setq nnml-current-directory pathname
527               nnml-current-group group
528               nnml-article-file-alist nil))))
529   t)
530
531 (defun nnml-possibly-create-directory (group)
532   (let (dir dirs)
533     (setq dir (nnmail-group-pathname group nnml-directory))
534     (while (not (file-directory-p dir))
535       (setq dirs (cons dir dirs))
536       (setq dir (file-name-directory (directory-file-name dir))))
537     (while dirs
538       (make-directory (directory-file-name (car dirs)))
539       (nnheader-message 5 "Creating mail directory %s" (car dirs))
540       (setq dirs (cdr dirs)))))
541              
542 (defun nnml-save-mail ()
543   "Called narrowed to an article."
544   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
545         chars headers)
546     (setq chars (nnmail-insert-lines))
547     (nnmail-insert-xref group-art)
548     (run-hooks 'nnmail-prepare-save-mail-hook)
549     (run-hooks 'nnml-prepare-save-mail-hook)
550     (goto-char (point-min))
551     (while (looking-at "From ")
552       (replace-match "X-From-Line: ")
553       (forward-line 1))
554     ;; We save the article in all the newsgroups it belongs in.
555     (let ((ga group-art)
556           first)
557       (while ga
558         (nnml-possibly-create-directory (caar ga))
559         (let ((file (concat (nnmail-group-pathname 
560                              (caar ga) nnml-directory)
561                             (int-to-string (cdar ga)))))
562           (if first
563               ;; It was already saved, so we just make a hard link.
564               (funcall nnmail-crosspost-link-function first file t)
565             ;; Save the article.
566             (write-region (point-min) (point-max) file nil 
567                           (if (nnheader-be-verbose 5) nil 'nomesg))
568             (setq first file)))
569         (setq ga (cdr ga))))
570     ;; Generate a nov line for this article. We generate the nov
571     ;; line after saving, because nov generation destroys the
572     ;; header. 
573     (setq headers (nnml-parse-head chars))
574     ;; Output the nov line to all nov databases that should have it.
575     (let ((ga group-art))
576       (while ga
577         (nnml-add-nov (caar ga) (cdar ga) headers)
578         (setq ga (cdr ga))))
579     group-art))
580
581 (defun nnml-active-number (group)
582   "Compute the next article number in GROUP."
583   (let ((active (cadr (assoc group nnml-group-alist))))
584     ;; The group wasn't known to nnml, so we just create an active
585     ;; entry for it.   
586     (unless active
587       ;; Perhaps the active file was corrupt?  See whether
588       ;; there are any articles in this group.
589       (nnml-possibly-create-directory group)
590       (nnml-possibly-change-directory group)
591       (unless nnml-article-file-alist
592         (setq nnml-article-file-alist
593               (sort
594                (nnheader-article-to-file-alist nnml-current-directory)
595                (lambda (a1 a2) (< (car a1) (car a2))))))
596       (setq active
597             (if nnml-article-file-alist
598                 (cons (caar nnml-article-file-alist)
599                       (caar (last nnml-article-file-alist)))
600               (cons 1 0)))
601       (setq nnml-group-alist (cons (list group active) nnml-group-alist)))
602     (setcdr active (1+ (cdr active)))
603     (while (file-exists-p
604             (concat (nnmail-group-pathname group nnml-directory)
605                     (int-to-string (cdr active))))
606       (setcdr active (1+ (cdr active))))
607     (cdr active)))
608
609 (defun nnml-add-nov (group article headers)
610   "Add a nov line for the GROUP base."
611   (save-excursion 
612     (set-buffer (nnml-open-nov group))
613     (goto-char (point-max))
614     (mail-header-set-number headers article)
615     (nnheader-insert-nov headers)))
616
617 (defsubst nnml-header-value ()
618   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
619
620 (defun nnml-parse-head (chars &optional number)
621   "Parse the head of the current buffer."
622   (save-excursion
623     (save-restriction
624       (goto-char (point-min))
625       (narrow-to-region 
626        (point)
627        (1- (or (search-forward "\n\n" nil t) (point-max))))
628       ;; Fold continuation lines.
629       (goto-char (point-min))
630       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
631         (replace-match " " t t))
632       ;; Remove any tabs; they are too confusing.
633       (subst-char-in-region (point-min) (point-max) ?\t ? )
634       (let ((headers (nnheader-parse-head t)))
635         (mail-header-set-chars headers chars)
636         (mail-header-set-number headers number)
637         headers))))
638
639 (defun nnml-open-nov (group)
640   (or (cdr (assoc group nnml-nov-buffer-alist))
641       (let ((buffer (find-file-noselect 
642                      (concat (nnmail-group-pathname group nnml-directory)
643                              nnml-nov-file-name))))
644         (save-excursion
645           (set-buffer buffer)
646           (buffer-disable-undo (current-buffer)))
647         (setq nnml-nov-buffer-alist 
648               (cons (cons group buffer) nnml-nov-buffer-alist))
649         buffer)))
650
651 (defun nnml-save-nov ()
652   (save-excursion
653     (while nnml-nov-buffer-alist
654       (when (buffer-name (cdar nnml-nov-buffer-alist))
655         (set-buffer (cdar nnml-nov-buffer-alist))
656         (and (buffer-modified-p)
657              (write-region 
658               1 (point-max) (buffer-file-name) nil 'nomesg))
659         (set-buffer-modified-p nil)
660         (kill-buffer (current-buffer)))
661       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
662
663 ;;;###autoload
664 (defun nnml-generate-nov-databases ()
665   "Generate nov databases in all nnml directories."
666   (interactive)
667   ;; Read the active file to make sure we don't re-use articles 
668   ;; numbers in empty groups.
669   (nnmail-activate 'nnml)
670   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
671   (setq nnml-directory (expand-file-name nnml-directory))
672   ;; Recurse down the directories.
673   (nnml-generate-nov-databases-1 nnml-directory)
674   ;; Save the active file.
675   (nnmail-save-active nnml-group-alist nnml-active-file))
676
677 (defun nnml-generate-nov-databases-1 (dir)
678   (setq dir (file-name-as-directory dir))
679   ;; We descend recursively 
680   (let ((dirs (directory-files dir t nil t))
681         dir)
682     (while dirs 
683       (setq dir (pop dirs))
684       (when (and (not (member (file-name-nondirectory dir) '("." "..")))
685                  (file-directory-p dir))
686         (nnml-generate-nov-databases-1 dir))))
687   ;; Do this directory.
688   (let ((files (sort
689                 (mapcar
690                  (lambda (name) (string-to-int name))
691                  (directory-files dir nil "^[0-9]+$" t))
692                 '<)))
693     (when files
694       (funcall nnml-generate-active-function dir)
695       ;; Generate the nov file.
696       (nnml-generate-nov-file dir files))))
697
698 (defvar files)
699 (defun nnml-generate-active-info (dir)
700   ;; Update the active info for this group.
701   (let ((group (nnheader-file-to-group 
702                 (directory-file-name dir) nnml-directory)))
703     (setq nnml-group-alist
704           (delq (assoc group nnml-group-alist) nnml-group-alist))
705     (push (list group
706                 (cons (car files)
707                       (let ((f files))
708                         (while (cdr f) (setq f (cdr f)))
709                         (car f))))
710           nnml-group-alist)))
711
712 (defun nnml-generate-nov-file (dir files)
713   (let* ((dir (file-name-as-directory dir))
714          (nov (concat dir nnml-nov-file-name))
715          (nov-buffer (get-buffer-create " *nov*"))
716          nov-line chars file headers)
717     (save-excursion
718       ;; Init the nov buffer.
719       (set-buffer nov-buffer)
720       (buffer-disable-undo (current-buffer))
721       (erase-buffer)
722       (set-buffer nntp-server-buffer)
723       ;; Delete the old NOV file.
724       (when (file-exists-p nov)
725         (funcall nnmail-delete-file-function nov))
726       (while files
727         (unless (file-directory-p 
728                  (setq file (concat dir (int-to-string (car files)))))
729           (erase-buffer)
730           (insert-file-contents file)
731           (narrow-to-region 
732            (goto-char (point-min))
733            (progn
734              (search-forward "\n\n" nil t)
735              (setq chars (- (point-max) (point)))
736              (max 1 (1- (point)))))
737           (when (and (not (= 0 chars))  ; none of them empty files...
738                      (not (= (point-min) (point-max))))
739             (goto-char (point-min))
740             (setq headers (nnml-parse-head chars (car files)))
741             (save-excursion
742               (set-buffer nov-buffer)
743               (goto-char (point-max))
744               (nnheader-insert-nov headers)))
745           (widen))
746         (setq files (cdr files)))
747       (save-excursion
748         (set-buffer nov-buffer)
749         (write-region 1 (point-max) (expand-file-name nov) nil
750                       'nomesg)
751         (kill-buffer (current-buffer))))))
752
753 (defun nnml-nov-delete-article (group article)
754   (save-excursion
755     (set-buffer (nnml-open-nov group))
756     (goto-char (point-min))
757     (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
758         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
759     t))
760
761 (provide 'nnml)
762
763 ;;; nnml.el ends here