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