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