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