This commit was generated by cvs2svn to compensate for changes in r3350,
[elisp/gnus.git-] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 message-directory
41   "Spool directory for the nnml mail backend.")
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 (defvar nnml-nov-buffer-file-name nil)
88
89 \f
90
91 ;;; Interface functions.
92
93 (nnoo-define-basics nnml)
94
95 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
96   (when (nnml-possibly-change-directory group server)
97     (save-excursion
98       (set-buffer nntp-server-buffer)
99       (erase-buffer)
100       (let ((file nil)
101             (number (length sequence))
102             (count 0)
103             (pathname-coding-system 'binary)
104             beg article)
105         (if (stringp (car sequence))
106             'headers
107           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
108               'nov
109             (while sequence
110               (setq article (car sequence))
111               (setq file (nnml-article-to-file article))
112               (when (and file
113                          (file-exists-p file)
114                          (not (file-directory-p file)))
115                 (insert (format "221 %d Article retrieved.\n" article))
116                 (setq beg (point))
117                 (nnheader-insert-head file)
118                 (goto-char beg)
119                 (if (search-forward "\n\n" nil t)
120                     (forward-char -1)
121                   (goto-char (point-max))
122                   (insert "\n\n"))
123                 (insert ".\n")
124                 (delete-region (point) (point-max)))
125               (setq sequence (cdr sequence))
126               (setq count (1+ count))
127               (and (numberp nnmail-large-newsgroup)
128                    (> number nnmail-large-newsgroup)
129                    (zerop (% count 20))
130                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
131                                      (/ (* count 100) number))))
132
133             (and (numberp nnmail-large-newsgroup)
134                  (> number nnmail-large-newsgroup)
135                  (nnheader-message 6 "nnml: Receiving headers...done"))
136
137             (nnheader-fold-continuation-lines)
138             'headers))))))
139
140 (deffoo nnml-open-server (server &optional defs)
141   (nnoo-change-server 'nnml server defs)
142   (when (not (file-exists-p nnml-directory))
143     (condition-case ()
144         (make-directory nnml-directory t)
145       (error)))
146   (cond
147    ((not (file-exists-p nnml-directory))
148     (nnml-close-server)
149     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
150    ((not (file-directory-p (file-truename nnml-directory)))
151     (nnml-close-server)
152     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
153    (t
154     (nnheader-report 'nnml "Opened server %s using directory %s"
155                      server nnml-directory)
156     t)))
157
158 (defun nnml-request-regenerate (server)
159   (nnml-possibly-change-directory nil server)
160   (nnml-generate-nov-databases)
161   t)
162
163 (deffoo nnml-request-article (id &optional group server buffer)
164   (nnml-possibly-change-directory group server)
165   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
166          (pathname-coding-system 'binary)
167          path gpath group-num)
168     (if (stringp id)
169         (when (and (setq group-num (nnml-find-group-number id))
170                    (cdr
171                     (assq (cdr group-num)
172                           (nnheader-article-to-file-alist
173                            (setq gpath
174                                  (nnmail-group-pathname
175                                   (car group-num)
176                                   nnml-directory))))))
177           (setq path (concat gpath (int-to-string (cdr group-num)))))
178       (setq path (nnml-article-to-file id)))
179     (cond
180      ((not path)
181       (nnheader-report 'nnml "No such article: %s" id))
182      ((not (file-exists-p path))
183       (nnheader-report 'nnml "No such file: %s" path))
184      ((file-directory-p path)
185       (nnheader-report 'nnml "File is a directory: %s" path))
186      ((not (save-excursion (nnmail-find-file path)))
187       (nnheader-report 'nnml "Couldn't read file: %s" path))
188      (t
189       (nnheader-report 'nnml "Article %s retrieved" id)
190       ;; We return the article number.
191       (cons (if group-num (car group-num) group)
192             (string-to-int (file-name-nondirectory path)))))))
193
194 (deffoo nnml-request-group (group &optional server dont-check)
195   (let ((pathname-coding-system 'binary))
196     (cond
197      ((not (nnml-possibly-change-directory group server))
198       (nnheader-report 'nnml "Invalid group (no such directory)"))
199      ((not (file-exists-p nnml-current-directory))
200       (nnheader-report 'nnml "Directory %s does not exist"
201                        nnml-current-directory))
202      ((not (file-directory-p nnml-current-directory))
203       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
204      (dont-check
205       (nnheader-report 'nnml "Group %s selected" group)
206       t)
207      (t
208       (nnheader-re-read-dir nnml-current-directory)
209       (nnmail-activate 'nnml)
210       (let ((active (nth 1 (assoc group nnml-group-alist))))
211         (if (not active)
212             (nnheader-report 'nnml "No such group: %s" group)
213           (nnheader-report 'nnml "Selected group %s" group)
214           (nnheader-insert "211 %d %d %d %s\n"
215                            (max (1+ (- (cdr active) (car active))) 0)
216                            (car active) (cdr active) group)))))))
217
218 (deffoo nnml-request-scan (&optional group server)
219   (setq nnml-article-file-alist nil)
220   (nnml-possibly-change-directory group server)
221   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
222
223 (deffoo nnml-close-group (group &optional server)
224   (setq nnml-article-file-alist nil)
225   t)
226
227 (deffoo nnml-request-create-group (group &optional server args)
228   (nnmail-activate 'nnml)
229   (cond
230    ((assoc group nnml-group-alist)
231     t)
232    ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
233          (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
234     (nnheader-report 'nnml "%s is a file"
235                      (nnmail-group-pathname group nnml-directory)))
236    (t
237     (let (active)
238       (push (list group (setq active (cons 1 0)))
239             nnml-group-alist)
240       (nnml-possibly-create-directory group)
241       (nnml-possibly-change-directory group server)
242       (let ((articles (nnheader-directory-articles nnml-current-directory)))
243         (when articles
244           (setcar active (apply 'min articles))
245           (setcdr active (apply 'max articles))))
246       (nnmail-save-active nnml-group-alist nnml-active-file)
247       t))))
248
249 (deffoo nnml-request-list (&optional server)
250   (save-excursion
251     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
252           (pathname-coding-system 'binary)) ; for XEmacs/mule
253       (nnmail-find-file nnml-active-file)
254       )
255     (setq nnml-group-alist (nnmail-get-active))
256     t))
257
258 (deffoo nnml-request-newgroups (date &optional server)
259   (nnml-request-list server))
260
261 (deffoo nnml-request-list-newsgroups (&optional server)
262   (save-excursion
263     (nnmail-find-file nnml-newsgroups-file)))
264
265 (deffoo nnml-request-expire-articles (articles group
266                                                &optional server force)
267   (nnml-possibly-change-directory group server)
268   (let ((active-articles
269          (nnheader-directory-articles nnml-current-directory))
270         (is-old t)
271         article rest mod-time number)
272     (nnmail-activate 'nnml)
273
274     (setq active-articles (sort active-articles '<))
275     ;; Articles not listed in active-articles are already gone,
276     ;; so don't try to expire them.
277     (setq articles (gnus-sorted-intersection articles active-articles))
278
279     (while (and articles is-old)
280       (when (setq article (nnml-article-to-file (setq number (pop articles))))
281         (when (setq mod-time (nth 5 (file-attributes article)))
282           (if (and (nnml-deletable-article-p group number)
283                    (setq is-old
284                          (nnmail-expired-article-p group mod-time force
285                                                    nnml-inhibit-expiry)))
286               (progn
287                 (nnheader-message 5 "Deleting article %s in %s"
288                                   article group)
289                 (condition-case ()
290                     (funcall nnmail-delete-file-function article)
291                   (file-error
292                    (push number rest)))
293                 (setq active-articles (delq number active-articles))
294                 (nnml-nov-delete-article group number))
295             (push number rest)))))
296     (let ((active (nth 1 (assoc group nnml-group-alist))))
297       (when active
298         (setcar active (or (and active-articles
299                                 (apply 'min active-articles))
300                            (1+ (cdr active)))))
301       (nnmail-save-active nnml-group-alist nnml-active-file))
302     (nnml-save-nov)
303     (nconc rest articles)))
304
305 (deffoo nnml-request-move-article
306   (article group server accept-form &optional last)
307   (let ((buf (get-buffer-create " *nnml move*"))
308         result)
309     (nnml-possibly-change-directory group server)
310     (nnml-update-file-alist)
311     (and
312      (nnml-deletable-article-p group article)
313      (nnml-request-article article group server)
314      (save-excursion
315        (set-buffer buf)
316        (insert-buffer-substring nntp-server-buffer)
317        (setq result (eval accept-form))
318        (kill-buffer (current-buffer))
319        result)
320      (progn
321        (nnml-possibly-change-directory group server)
322        (condition-case ()
323            (funcall nnmail-delete-file-function
324                     (nnml-article-to-file  article))
325          (file-error nil))
326        (nnml-nov-delete-article group article)
327        (when last
328          (nnml-save-nov)
329          (nnmail-save-active nnml-group-alist nnml-active-file))))
330     result))
331
332 (deffoo nnml-request-accept-article (group &optional server last)
333   (nnml-possibly-change-directory group server)
334   (nnmail-check-syntax)
335   (let (result)
336     (when nnmail-cache-accepted-message-ids
337       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
338     (if (stringp group)
339         (and
340          (nnmail-activate 'nnml)
341          (setq result (car (nnml-save-mail
342                             (list (cons group (nnml-active-number group))))))
343          (progn
344            (nnmail-save-active nnml-group-alist nnml-active-file)
345            (and last (nnml-save-nov))))
346       (and
347        (nnmail-activate 'nnml)
348        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
349                 (yes-or-no-p "Moved to `junk' group; delete article? "))
350            (setq result 'junk)
351          (setq result (car (nnml-save-mail result))))
352        (when last
353          (nnmail-save-active nnml-group-alist nnml-active-file)
354          (when nnmail-cache-accepted-message-ids
355            (nnmail-cache-close))
356          (nnml-save-nov))))
357     result))
358
359 (deffoo 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     (let ((chars (nnmail-insert-lines))
365           (art (concat (int-to-string article) "\t"))
366           headers)
367       (when (condition-case ()
368                 (progn
369                   (nnmail-write-region
370                    (point-min) (point-max)
371                    (or (nnml-article-to-file article)
372                        (concat nnml-current-directory
373                                (int-to-string article)))
374                    nil (if (nnheader-be-verbose 5) nil 'nomesg))
375                   t)
376               (error nil))
377         (setq headers (nnml-parse-head chars article))
378         ;; Replace the NOV line in the NOV file.
379         (save-excursion
380           (set-buffer (nnml-open-nov group))
381           (goto-char (point-min))
382           (if (or (looking-at art)
383                   (search-forward (concat "\n" art) nil t))
384               ;; Delete the old NOV line.
385               (delete-region (progn (beginning-of-line) (point))
386                              (progn (forward-line 1) (point)))
387             ;; The line isn't here, so we have to find out where
388             ;; we should insert it.  (This situation should never
389             ;; occur, but one likes to make sure...)
390             (while (and (looking-at "[0-9]+\t")
391                         (< (string-to-int
392                             (buffer-substring
393                              (match-beginning 0) (match-end 0)))
394                            article)
395                         (zerop (forward-line 1)))))
396           (beginning-of-line)
397           (nnheader-insert-nov headers)
398           (nnml-save-nov)
399           t)))))
400
401 (deffoo nnml-request-delete-group (group &optional force server)
402   (nnml-possibly-change-directory group server)
403   (when force
404     ;; Delete all articles in GROUP.
405     (let ((articles
406            (directory-files
407             nnml-current-directory t
408             (concat nnheader-numerical-short-files
409                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
410           article)
411       (while articles
412         (setq article (pop articles))
413         (when (file-writable-p article)
414           (nnheader-message 5 "Deleting article %s in %s..." article group)
415           (funcall nnmail-delete-file-function article))))
416     ;; Try to delete the directory itself.
417     (condition-case ()
418         (delete-directory nnml-current-directory)
419       (error nil)))
420   ;; Remove the group from all structures.
421   (setq nnml-group-alist
422         (delq (assoc group nnml-group-alist) nnml-group-alist)
423         nnml-current-group nil
424         nnml-current-directory nil)
425   ;; Save the active file.
426   (nnmail-save-active nnml-group-alist nnml-active-file)
427   t)
428
429 (deffoo nnml-request-rename-group (group new-name &optional server)
430   (nnml-possibly-change-directory group server)
431   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
432         (old-dir (nnmail-group-pathname group nnml-directory)))
433     (when (condition-case ()
434               (progn
435                 (make-directory new-dir t)
436                 t)
437             (error nil))
438       ;; We move the articles file by file instead of renaming
439       ;; the directory -- there may be subgroups in this group.
440       ;; One might be more clever, I guess.
441       (let ((files (nnheader-article-to-file-alist old-dir)))
442         (while files
443           (rename-file
444            (concat old-dir (cdar files))
445            (concat new-dir (cdar files)))
446           (pop files)))
447       ;; Move .overview file.
448       (let ((overview (concat old-dir nnml-nov-file-name)))
449         (when (file-exists-p overview)
450           (rename-file overview (concat new-dir nnml-nov-file-name))))
451       (when (<= (length (directory-files old-dir)) 2)
452         (condition-case ()
453             (delete-directory old-dir)
454           (error nil)))
455       ;; That went ok, so we change the internal structures.
456       (let ((entry (assoc group nnml-group-alist)))
457         (when entry
458           (setcar entry new-name))
459         (setq nnml-current-directory nil
460               nnml-current-group nil)
461         ;; Save the new group alist.
462         (nnmail-save-active nnml-group-alist nnml-active-file)
463         t))))
464
465 (deffoo nnml-set-status (article name value &optional group server)
466   (nnml-possibly-change-directory group server)
467   (let ((file (nnml-article-to-file article)))
468     (cond
469      ((not (file-exists-p file))
470       (nnheader-report 'nnml "File %s does not exist" file))
471      (t
472       (nnheader-temp-write file
473         (nnheader-insert-file-contents file)
474         (nnmail-replace-status name value))
475       t))))
476
477 \f
478 ;;; Internal functions.
479
480 (defun nnml-article-to-file (article)
481   (nnml-update-file-alist)
482   (let (file)
483     (if (setq file (cdr (assq article nnml-article-file-alist)))
484         (concat nnml-current-directory file)
485       ;; Just to make sure nothing went wrong when reading over NFS --
486       ;; check once more.
487       (when (file-exists-p
488              (setq file (expand-file-name (number-to-string article)
489                                           nnml-current-directory)))
490         (nnml-update-file-alist t)
491         file))))
492
493 (defun nnml-deletable-article-p (group article)
494   "Say whether ARTICLE in GROUP can be deleted."
495   (let (path)
496     (when (setq path (nnml-article-to-file article))
497       (when (file-writable-p path)
498         (or (not nnmail-keep-last-article)
499             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
500                      article)))))))
501
502 ;; Find an article number in the current group given the Message-ID.
503 (defun nnml-find-group-number (id)
504   (save-excursion
505     (set-buffer (get-buffer-create " *nnml id*"))
506     (buffer-disable-undo (current-buffer))
507     (let ((alist nnml-group-alist)
508           number)
509       ;; We want to look through all .overview files, but we want to
510       ;; start with the one in the current directory.  It seems most
511       ;; likely that the article we are looking for is in that group.
512       (if (setq number (nnml-find-id nnml-current-group id))
513           (cons nnml-current-group number)
514         ;; It wasn't there, so we look through the other groups as well.
515         (while (and (not number)
516                     alist)
517           (or (string= (caar alist) nnml-current-group)
518               (setq number (nnml-find-id (caar alist) id)))
519           (or number
520               (setq alist (cdr alist))))
521         (and number
522              (cons (caar alist) number))))))
523
524 (defun nnml-find-id (group id)
525   (erase-buffer)
526   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
527                      nnml-nov-file-name))
528         number found)
529     (when (file-exists-p nov)
530       (nnheader-insert-file-contents nov)
531       (while (and (not found)
532                   (search-forward id nil t)) ; We find the ID.
533         ;; And the id is in the fourth field.
534         (if (not (and (search-backward "\t" nil t 4)
535                       (not (search-backward"\t" (gnus-point-at-bol) t))))
536             (forward-line 1)
537           (beginning-of-line)
538           (setq found t)
539           ;; We return the article number.
540           (setq number
541                 (condition-case ()
542                     (read (current-buffer))
543                   (error nil)))))
544       number)))
545
546 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
547   (if (or gnus-nov-is-evil nnml-nov-is-evil)
548       nil
549     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
550       (when (file-exists-p nov)
551         (save-excursion
552           (set-buffer nntp-server-buffer)
553           (erase-buffer)
554           (nnheader-insert-file-contents nov)
555           (if (and fetch-old
556                    (not (numberp fetch-old)))
557               t                         ; Don't remove anything.
558             (nnheader-nov-delete-outside-range
559              (if fetch-old (max 1 (- (car articles) fetch-old))
560                (car articles))
561              (car (last articles)))
562             t))))))
563
564 (defun nnml-possibly-change-directory (group &optional server)
565   (when (and server
566              (not (nnml-server-opened server)))
567     (nnml-open-server server))
568   (if (not group)
569       t
570     (let ((pathname (nnmail-group-pathname group nnml-directory))
571           (pathname-coding-system 'binary))
572       (when (not (equal pathname nnml-current-directory))
573         (setq nnml-current-directory pathname
574               nnml-current-group group
575               nnml-article-file-alist nil))
576       (file-exists-p nnml-current-directory))))
577
578 (defun nnml-possibly-create-directory (group)
579   (let (dir dirs)
580     (setq dir (nnmail-group-pathname group nnml-directory))
581     (while (not (file-directory-p dir))
582       (push dir dirs)
583       (setq dir (file-name-directory (directory-file-name dir))))
584     (while dirs
585       (make-directory (directory-file-name (car dirs)))
586       (nnheader-message 5 "Creating mail directory %s" (car dirs))
587       (setq dirs (cdr dirs)))))
588
589 (defun nnml-save-mail (group-art)
590   "Called narrowed to an article."
591   (let (chars headers)
592     (setq chars (nnmail-insert-lines))
593     (nnmail-insert-xref group-art)
594     (run-hooks 'nnmail-prepare-save-mail-hook)
595     (run-hooks 'nnml-prepare-save-mail-hook)
596     (goto-char (point-min))
597     (while (looking-at "From ")
598       (replace-match "X-From-Line: ")
599       (forward-line 1))
600     ;; We save the article in all the groups it belongs in.
601     (let ((ga group-art)
602           first)
603       (while ga
604         (nnml-possibly-create-directory (caar ga))
605         (let ((file (concat (nnmail-group-pathname
606                              (caar ga) nnml-directory)
607                             (int-to-string (cdar ga)))))
608           (if first
609               ;; It was already saved, so we just make a hard link.
610               (funcall nnmail-crosspost-link-function first file t)
611             ;; Save the article.
612             (nnmail-write-region (point-min) (point-max) file nil
613                                  (if (nnheader-be-verbose 5) nil 'nomesg))
614             (setq first file)))
615         (setq ga (cdr ga))))
616     ;; Generate a nov line for this article.  We generate the nov
617     ;; line after saving, because nov generation destroys the
618     ;; header.
619     (setq headers (nnml-parse-head chars))
620     ;; Output the nov line to all nov databases that should have it.
621     (let ((ga group-art))
622       (while ga
623         (nnml-add-nov (caar ga) (cdar ga) headers)
624         (setq ga (cdr ga))))
625     group-art))
626
627 (defun nnml-active-number (group)
628   "Compute the next article number in GROUP."
629   (let ((active (cadr (assoc group nnml-group-alist))))
630     ;; The group wasn't known to nnml, so we just create an active
631     ;; entry for it.
632     (unless active
633       ;; Perhaps the active file was corrupt?  See whether
634       ;; there are any articles in this group.
635       (nnml-possibly-create-directory group)
636       (nnml-possibly-change-directory group)
637       (unless nnml-article-file-alist
638         (setq nnml-article-file-alist
639               (sort
640                (nnheader-article-to-file-alist nnml-current-directory)
641                'car-less-than-car)))
642       (setq active
643             (if nnml-article-file-alist
644                 (cons (caar nnml-article-file-alist)
645                       (caar (last nnml-article-file-alist)))
646               (cons 1 0)))
647       (push (list group active) nnml-group-alist))
648     (setcdr active (1+ (cdr active)))
649     (while (file-exists-p
650             (concat (nnmail-group-pathname group nnml-directory)
651                     (int-to-string (cdr active))))
652       (setcdr active (1+ (cdr active))))
653     (cdr active)))
654
655 (defun nnml-add-nov (group article headers)
656   "Add a nov line for the GROUP base."
657   (save-excursion
658     (set-buffer (nnml-open-nov group))
659     (goto-char (point-max))
660     (mail-header-set-number headers article)
661     (nnheader-insert-nov headers)))
662
663 (defsubst nnml-header-value ()
664   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
665
666 (defun nnml-parse-head (chars &optional number)
667   "Parse the head of the current buffer."
668   (save-excursion
669     (save-restriction
670       (unless (zerop (buffer-size))
671         (narrow-to-region
672          (goto-char (point-min))
673          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
674       ;; Fold continuation lines.
675       (goto-char (point-min))
676       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
677         (replace-match " " t t))
678       ;; Remove any tabs; they are too confusing.
679       (subst-char-in-region (point-min) (point-max) ?\t ? )
680       (let ((headers (nnheader-parse-head t)))
681         (mail-header-set-chars headers chars)
682         (mail-header-set-number headers number)
683         headers))))
684
685 (defun nnml-open-nov (group)
686   (or (cdr (assoc group nnml-nov-buffer-alist))
687       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
688         (save-excursion
689           (set-buffer buffer)
690           (set (make-local-variable 'nnml-nov-buffer-file-name)
691                (concat (nnmail-group-pathname group nnml-directory)
692                        nnml-nov-file-name))
693           (erase-buffer)
694           (when (file-exists-p nnml-nov-buffer-file-name)
695             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
696         (push (cons group buffer) nnml-nov-buffer-alist)
697         buffer)))
698
699 (defun nnml-save-nov ()
700   (save-excursion
701     (while nnml-nov-buffer-alist
702       (when (buffer-name (cdar nnml-nov-buffer-alist))
703         (set-buffer (cdar nnml-nov-buffer-alist))
704         (when (buffer-modified-p)
705           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
706                                nil 'nomesg))
707         (set-buffer-modified-p nil)
708         (kill-buffer (current-buffer)))
709       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
710
711 ;;;###autoload
712 (defun nnml-generate-nov-databases ()
713   "Generate NOV databases in all nnml directories."
714   (interactive)
715   ;; Read the active file to make sure we don't re-use articles
716   ;; numbers in empty groups.
717   (nnmail-activate 'nnml)
718   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
719   (setq nnml-directory (expand-file-name nnml-directory))
720   ;; Recurse down the directories.
721   (nnml-generate-nov-databases-1 nnml-directory nil t)
722   ;; Save the active file.
723   (nnmail-save-active nnml-group-alist nnml-active-file))
724
725 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
726   "Regenerate the NOV database in DIR."
727   (interactive "DRegenerate NOV in: ")
728   (setq dir (file-name-as-directory dir))
729   ;; Only scan this sub-tree if we haven't been here yet.
730   (unless (member (file-truename dir) seen)
731     (push (file-truename dir) seen)
732     ;; We descend recursively
733     (let ((dirs (directory-files dir t nil t))
734           dir)
735       (while (setq dir (pop dirs))
736         (when (and (not (member (file-name-nondirectory dir) '("." "..")))
737                    (file-directory-p dir))
738           (nnml-generate-nov-databases-1 dir seen))))
739     ;; Do this directory.
740     (let ((files (sort (nnheader-article-to-file-alist dir)
741                        'car-less-than-car)))
742       (if (not files)
743           (let* ((group (nnheader-file-to-group
744                          (directory-file-name dir) nnml-directory))
745                  (info (cadr (assoc group nnml-group-alist))))
746             (when info
747               (setcar info (1+ (cdr info)))))
748         (funcall nnml-generate-active-function dir)
749         ;; Generate the nov file.
750         (nnml-generate-nov-file dir files)
751         (unless no-active
752           (nnmail-save-active nnml-group-alist nnml-active-file))))))
753
754 (defvar files)
755 (defun nnml-generate-active-info (dir)
756   ;; Update the active info for this group.
757   (let ((group (nnheader-file-to-group
758                 (directory-file-name dir) nnml-directory)))
759     (setq nnml-group-alist
760           (delq (assoc group nnml-group-alist) nnml-group-alist))
761     (push (list group
762                 (cons (caar files)
763                       (let ((f files))
764                         (while (cdr f) (setq f (cdr f)))
765                         (caar f))))
766           nnml-group-alist)))
767
768 (defun nnml-generate-nov-file (dir files)
769   (let* ((dir (file-name-as-directory dir))
770          (nov (concat dir nnml-nov-file-name))
771          (nov-buffer (get-buffer-create " *nov*"))
772          chars file headers)
773     (save-excursion
774       ;; Init the nov buffer.
775       (set-buffer nov-buffer)
776       (buffer-disable-undo (current-buffer))
777       (erase-buffer)
778       (set-buffer nntp-server-buffer)
779       ;; Delete the old NOV file.
780       (when (file-exists-p nov)
781         (funcall nnmail-delete-file-function nov))
782       (while files
783         (unless (file-directory-p (setq file (concat dir (cdar files))))
784           (erase-buffer)
785           (nnheader-insert-file-contents file)
786           (narrow-to-region
787            (goto-char (point-min))
788            (progn
789              (search-forward "\n\n" nil t)
790              (setq chars (- (point-max) (point)))
791              (max 1 (1- (point)))))
792           (unless (zerop (buffer-size))
793             (goto-char (point-min))
794             (setq headers (nnml-parse-head chars (caar files)))
795             (save-excursion
796               (set-buffer nov-buffer)
797               (goto-char (point-max))
798               (nnheader-insert-nov headers)))
799           (widen))
800         (setq files (cdr files)))
801       (save-excursion
802         (set-buffer nov-buffer)
803         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
804         (kill-buffer (current-buffer))))))
805
806 (defun nnml-nov-delete-article (group article)
807   (save-excursion
808     (set-buffer (nnml-open-nov group))
809     (when (nnheader-find-nov-line article)
810       (delete-region (point) (progn (forward-line 1) (point)))
811       (when (bobp)
812         (let ((active (cadr (assoc group nnml-group-alist)))
813               num)
814           (when active
815             (if (eobp)
816                 (setf (car active) (1+ (cdr active)))
817               (when (and (setq num (ignore-errors (read (current-buffer))))
818                          (numberp num))
819                 (setf (car active) num)))))))
820     t))
821
822 (defun nnml-update-file-alist (&optional force)
823   (when (or (not nnml-article-file-alist)
824             force)
825     (setq nnml-article-file-alist
826           (nnheader-article-to-file-alist nnml-current-directory))))
827
828 (provide 'nnml)
829
830 ;;; nnml.el ends here