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