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