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