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