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