Synch 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
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 (deffoo nnml-request-regenerate (server)
163   (nnml-possibly-change-directory nil server)
164   (nnml-generate-nov-databases server)
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-post (&optional server)
379   (nnmail-do-request-post 'nnml-request-accept-article server))
380
381 (deffoo nnml-request-replace-article (article group buffer)
382   (nnml-possibly-change-directory group)
383   (save-excursion
384     (set-buffer buffer)
385     (nnml-possibly-create-directory group)
386     (let ((chars (nnmail-insert-lines))
387           (art (concat (int-to-string article) "\t"))
388           headers)
389       (when (ignore-errors
390               (nnmail-write-region
391                (point-min) (point-max)
392                (or (nnml-article-to-file article)
393                    (expand-file-name (int-to-string article)
394                                      nnml-current-directory))
395                nil (if (nnheader-be-verbose 5) nil 'nomesg))
396               t)
397         (setq headers (nnml-parse-head chars article))
398         ;; Replace the NOV line in the NOV file.
399         (save-excursion
400           (set-buffer (nnml-open-nov group))
401           (goto-char (point-min))
402           (if (or (looking-at art)
403                   (search-forward (concat "\n" art) nil t))
404               ;; Delete the old NOV line.
405               (delete-region (progn (beginning-of-line) (point))
406                              (progn (forward-line 1) (point)))
407             ;; The line isn't here, so we have to find out where
408             ;; we should insert it.  (This situation should never
409             ;; occur, but one likes to make sure...)
410             (while (and (looking-at "[0-9]+\t")
411                         (< (string-to-int
412                             (buffer-substring
413                              (match-beginning 0) (match-end 0)))
414                            article)
415                         (zerop (forward-line 1)))))
416           (beginning-of-line)
417           (nnheader-insert-nov headers)
418           (nnml-save-nov)
419           t)))))
420
421 (deffoo nnml-request-delete-group (group &optional force server)
422   (nnml-possibly-change-directory group server)
423   (when force
424     ;; Delete all articles in GROUP.
425     (let ((articles
426            (directory-files
427             nnml-current-directory t
428             (concat nnheader-numerical-short-files
429                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
430           article)
431       (while articles
432         (setq article (pop articles))
433         (when (file-writable-p article)
434           (nnheader-message 5 "Deleting article %s in %s..." article group)
435           (funcall nnmail-delete-file-function article))))
436     ;; Try to delete the directory itself.
437     (ignore-errors (delete-directory nnml-current-directory)))
438   ;; Remove the group from all structures.
439   (setq nnml-group-alist
440         (delq (assoc group nnml-group-alist) nnml-group-alist)
441         nnml-current-group nil
442         nnml-current-directory nil)
443   ;; Save the active file.
444   (nnmail-save-active nnml-group-alist nnml-active-file)
445   t)
446
447 (deffoo nnml-request-rename-group (group new-name &optional server)
448   (nnml-possibly-change-directory group server)
449   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
450         (old-dir (nnmail-group-pathname group nnml-directory)))
451     (when (ignore-errors
452             (make-directory new-dir t)
453             t)
454       ;; We move the articles file by file instead of renaming
455       ;; the directory -- there may be subgroups in this group.
456       ;; One might be more clever, I guess.
457       (let ((files (nnheader-article-to-file-alist old-dir)))
458         (while files
459           (rename-file
460            (concat old-dir (cdar files))
461            (concat new-dir (cdar files)))
462           (pop files)))
463       ;; Move .overview file.
464       (let ((overview (concat old-dir nnml-nov-file-name)))
465         (when (file-exists-p overview)
466           (rename-file overview (concat new-dir nnml-nov-file-name))))
467       (when (<= (length (directory-files old-dir)) 2)
468         (ignore-errors (delete-directory old-dir)))
469       ;; That went ok, so we change the internal structures.
470       (let ((entry (assoc group nnml-group-alist)))
471         (when entry
472           (setcar entry new-name))
473         (setq nnml-current-directory nil
474               nnml-current-group nil)
475         ;; Save the new group alist.
476         (nnmail-save-active nnml-group-alist nnml-active-file)
477         t))))
478
479 (deffoo nnml-set-status (article name value &optional group server)
480   (nnml-possibly-change-directory group server)
481   (let ((file (nnml-article-to-file article)))
482     (cond
483      ((not (file-exists-p file))
484       (nnheader-report 'nnml "File %s does not exist" file))
485      (t
486       (with-temp-file file
487         (nnheader-insert-file-contents file)
488         (nnmail-replace-status name value))
489       t))))
490
491 \f
492 ;;; Internal functions.
493
494 (defun nnml-article-to-file (article)
495   (nnml-update-file-alist)
496   (let (file)
497     (if (setq file (cdr (assq article nnml-article-file-alist)))
498         (expand-file-name file nnml-current-directory)
499       ;; Just to make sure nothing went wrong when reading over NFS --
500       ;; check once more.
501       (when (file-exists-p
502              (setq file (expand-file-name (number-to-string article)
503                                           nnml-current-directory)))
504         (nnml-update-file-alist t)
505         file))))
506
507 (defun nnml-deletable-article-p (group article)
508   "Say whether ARTICLE in GROUP can be deleted."
509   (let (path)
510     (when (setq path (nnml-article-to-file article))
511       (when (file-writable-p path)
512         (or (not nnmail-keep-last-article)
513             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
514                      article)))))))
515
516 ;; Find an article number in the current group given the Message-ID.
517 (defun nnml-find-group-number (id)
518   (save-excursion
519     (set-buffer (get-buffer-create " *nnml id*"))
520     (let ((alist nnml-group-alist)
521           number)
522       ;; We want to look through all .overview files, but we want to
523       ;; start with the one in the current directory.  It seems most
524       ;; likely that the article we are looking for is in that group.
525       (if (setq number (nnml-find-id nnml-current-group id))
526           (cons nnml-current-group number)
527         ;; It wasn't there, so we look through the other groups as well.
528         (while (and (not number)
529                     alist)
530           (or (string= (caar alist) nnml-current-group)
531               (setq number (nnml-find-id (caar alist) id)))
532           (or number
533               (setq alist (cdr alist))))
534         (and number
535              (cons (caar alist) number))))))
536
537 (defun nnml-find-id (group id)
538   (erase-buffer)
539   (let ((nov (expand-file-name nnml-nov-file-name
540                                (nnmail-group-pathname group nnml-directory)))
541         number found)
542     (when (file-exists-p nov)
543       (nnheader-insert-file-contents nov)
544       (while (and (not found)
545                   (search-forward id nil t)) ; We find the ID.
546         ;; And the id is in the fourth field.
547         (if (not (and (search-backward "\t" nil t 4)
548                       (not (search-backward"\t" (gnus-point-at-bol) t))))
549             (forward-line 1)
550           (beginning-of-line)
551           (setq found t)
552           ;; We return the article number.
553           (setq number
554                 (ignore-errors (read (current-buffer))))))
555       number)))
556
557 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
558   (if (or gnus-nov-is-evil nnml-nov-is-evil)
559       nil
560     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
561       (when (file-exists-p nov)
562         (save-excursion
563           (set-buffer nntp-server-buffer)
564           (erase-buffer)
565           (nnheader-insert-file-contents nov)
566           (if (and fetch-old
567                    (not (numberp fetch-old)))
568               t                         ; Don't remove anything.
569             (nnheader-nov-delete-outside-range
570              (if fetch-old (max 1 (- (car articles) fetch-old))
571                (car articles))
572              (car (last articles)))
573             t))))))
574
575 (defun nnml-possibly-change-directory (group &optional server)
576   (when (and server
577              (not (nnml-server-opened server)))
578     (nnml-open-server server))
579   (if (not group)
580       t
581     (let ((pathname (nnmail-group-pathname group nnml-directory))
582           (file-name-coding-system nnmail-pathname-coding-system)
583           (pathname-coding-system nnmail-pathname-coding-system))
584       (when (not (equal pathname nnml-current-directory))
585         (setq nnml-current-directory pathname
586               nnml-current-group group
587               nnml-article-file-alist nil))
588       (file-exists-p nnml-current-directory))))
589
590 (defun nnml-possibly-create-directory (group)
591   (let ((dir (nnmail-group-pathname group nnml-directory)))
592     (unless (file-exists-p dir)
593       (make-directory (directory-file-name dir) t)
594       (nnheader-message 5 "Creating mail directory %s" dir))))
595
596 (defun nnml-save-mail (group-art)
597   "Called narrowed to an article."
598   (let (chars headers)
599     (setq chars (nnmail-insert-lines))
600     (nnmail-insert-xref group-art)
601     (run-hooks 'nnmail-prepare-save-mail-hook)
602     (run-hooks 'nnml-prepare-save-mail-hook)
603     (goto-char (point-min))
604     (while (looking-at "From ")
605       (replace-match "X-From-Line: ")
606       (forward-line 1))
607     ;; We save the article in all the groups it belongs in.
608     (let ((ga group-art)
609           first)
610       (while ga
611         (nnml-possibly-create-directory (caar ga))
612         (let ((file (concat (nnmail-group-pathname
613                              (caar ga) nnml-directory)
614                             (int-to-string (cdar ga)))))
615           (if first
616               ;; It was already saved, so we just make a hard link.
617               (funcall nnmail-crosspost-link-function first file t)
618             ;; Save the article.
619             (nnmail-write-region (point-min) (point-max) file nil
620                                  (if (nnheader-be-verbose 5) nil 'nomesg))
621             (setq first file)))
622         (setq ga (cdr ga))))
623     ;; Generate a nov line for this article.  We generate the nov
624     ;; line after saving, because nov generation destroys the
625     ;; header.
626     (setq headers (nnml-parse-head chars))
627     ;; Output the nov line to all nov databases that should have it.
628     (let ((ga group-art))
629       (while ga
630         (nnml-add-nov (caar ga) (cdar ga) headers)
631         (setq ga (cdr ga))))
632     group-art))
633
634 (defun nnml-active-number (group)
635   "Compute the next article number in GROUP."
636   (let ((active (cadr (assoc group nnml-group-alist))))
637     ;; The group wasn't known to nnml, so we just create an active
638     ;; entry for it.
639     (unless active
640       ;; Perhaps the active file was corrupt?  See whether
641       ;; there are any articles in this group.
642       (nnml-possibly-create-directory group)
643       (nnml-possibly-change-directory group)
644       (unless nnml-article-file-alist
645         (setq nnml-article-file-alist
646               (sort
647                (nnheader-article-to-file-alist nnml-current-directory)
648                'car-less-than-car)))
649       (setq active
650             (if nnml-article-file-alist
651                 (cons (caar nnml-article-file-alist)
652                       (caar (last nnml-article-file-alist)))
653               (cons 1 0)))
654       (push (list group active) nnml-group-alist))
655     (setcdr active (1+ (cdr active)))
656     (while (file-exists-p
657             (expand-file-name (int-to-string (cdr active))
658                               (nnmail-group-pathname group nnml-directory)))
659       (setcdr active (1+ (cdr active))))
660     (cdr active)))
661
662 (defun nnml-add-nov (group article headers)
663   "Add a nov line for the GROUP base."
664   (save-excursion
665     (set-buffer (nnml-open-nov group))
666     (goto-char (point-max))
667     (mail-header-set-number headers article)
668     (nnheader-insert-nov headers)))
669
670 (defsubst nnml-header-value ()
671   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
672
673 (defun nnml-parse-head (chars &optional number)
674   "Parse the head of the current buffer."
675   (save-excursion
676     (save-restriction
677       (unless (zerop (buffer-size))
678         (narrow-to-region
679          (goto-char (point-min))
680          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
681       ;; Fold continuation lines.
682       (goto-char (point-min))
683       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
684         (replace-match " " t t))
685       ;; Remove any tabs; they are too confusing.
686       (subst-char-in-region (point-min) (point-max) ?\t ? )
687       (let ((headers (nnheader-parse-head t)))
688         (mail-header-set-chars headers chars)
689         (mail-header-set-number headers number)
690         headers))))
691
692 (defun nnml-open-nov (group)
693   (or (cdr (assoc group nnml-nov-buffer-alist))
694       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
695         (save-excursion
696           (set-buffer buffer)
697           (set (make-local-variable 'nnml-nov-buffer-file-name)
698                (expand-file-name
699                 nnml-nov-file-name
700                 (nnmail-group-pathname group nnml-directory)))
701           (erase-buffer)
702           (when (file-exists-p nnml-nov-buffer-file-name)
703             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
704         (push (cons group buffer) nnml-nov-buffer-alist)
705         buffer)))
706
707 (defun nnml-save-nov ()
708   (save-excursion
709     (while nnml-nov-buffer-alist
710       (when (buffer-name (cdar nnml-nov-buffer-alist))
711         (set-buffer (cdar nnml-nov-buffer-alist))
712         (when (buffer-modified-p)
713           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
714                                nil 'nomesg))
715         (set-buffer-modified-p nil)
716         (kill-buffer (current-buffer)))
717       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
718
719 ;;;###autoload
720 (defun nnml-generate-nov-databases (&optional server)
721   "Generate NOV databases in all nnml directories."
722   (interactive (list (or (nnoo-current-server 'nnml) "")))
723   ;; Read the active file to make sure we don't re-use articles
724   ;; numbers in empty groups.
725   (nnmail-activate 'nnml)
726   (unless (nnml-server-opened server)
727     (nnml-open-server server))
728   (setq nnml-directory (expand-file-name nnml-directory))
729   ;; Recurse down the directories.
730   (nnml-generate-nov-databases-1 nnml-directory nil t)
731   ;; Save the active file.
732   (nnmail-save-active nnml-group-alist nnml-active-file))
733
734 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
735   "Regenerate the NOV database in DIR."
736   (interactive "DRegenerate NOV in: ")
737   (setq dir (file-name-as-directory dir))
738   ;; Only scan this sub-tree if we haven't been here yet.
739   (unless (member (file-truename dir) seen)
740     (push (file-truename dir) seen)
741     ;; We descend recursively
742     (let ((dirs (directory-files dir t nil t))
743           dir)
744       (while (setq dir (pop dirs))
745         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
746                    (file-directory-p dir))
747           (nnml-generate-nov-databases-1 dir seen))))
748     ;; Do this directory.
749     (let ((files (sort (nnheader-article-to-file-alist dir)
750                        'car-less-than-car)))
751       (if (not files)
752           (let* ((group (nnheader-file-to-group
753                          (directory-file-name dir) nnml-directory))
754                  (info (cadr (assoc group nnml-group-alist))))
755             (when info
756               (setcar info (1+ (cdr info)))))
757         (funcall nnml-generate-active-function dir)
758         ;; Generate the nov file.
759         (nnml-generate-nov-file dir files)
760         (unless no-active
761           (nnmail-save-active nnml-group-alist nnml-active-file))))))
762
763 (eval-when-compile (defvar files))
764 (defun nnml-generate-active-info (dir)
765   ;; Update the active info for this group.
766   (let* ((group (nnheader-file-to-group
767                  (directory-file-name dir) nnml-directory))
768          (entry (assoc group nnml-group-alist))
769          (last (or (caadr entry) 0)))
770     (setq nnml-group-alist (delq entry nnml-group-alist))
771     (push (list group
772                 (cons (or (caar files) (1+ last))
773                       (max last
774                            (or (let ((f files))
775                                  (while (cdr f) (setq f (cdr f)))
776                                  (caar f))
777                                0))))
778           nnml-group-alist)))
779
780 (defun nnml-generate-nov-file (dir files)
781   (let* ((dir (file-name-as-directory dir))
782          (nov (concat dir nnml-nov-file-name))
783          (nov-buffer (get-buffer-create " *nov*"))
784          chars file headers)
785     (save-excursion
786       ;; Init the nov buffer.
787       (set-buffer nov-buffer)
788       (buffer-disable-undo)
789       (erase-buffer)
790       (set-buffer nntp-server-buffer)
791       ;; Delete the old NOV file.
792       (when (file-exists-p nov)
793         (funcall nnmail-delete-file-function nov))
794       (while files
795         (unless (file-directory-p (setq file (concat dir (cdar files))))
796           (erase-buffer)
797           (nnheader-insert-file-contents file)
798           (narrow-to-region
799            (goto-char (point-min))
800            (progn
801              (search-forward "\n\n" nil t)
802              (setq chars (- (point-max) (point)))
803              (max 1 (1- (point)))))
804           (unless (zerop (buffer-size))
805             (goto-char (point-min))
806             (setq headers (nnml-parse-head chars (caar files)))
807             (save-excursion
808               (set-buffer nov-buffer)
809               (goto-char (point-max))
810               (nnheader-insert-nov headers)))
811           (widen))
812         (setq files (cdr files)))
813       (save-excursion
814         (set-buffer nov-buffer)
815         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
816         (kill-buffer (current-buffer))))))
817
818 (defun nnml-nov-delete-article (group article)
819   (save-excursion
820     (set-buffer (nnml-open-nov group))
821     (when (nnheader-find-nov-line article)
822       (delete-region (point) (progn (forward-line 1) (point)))
823       (when (bobp)
824         (let ((active (cadr (assoc group nnml-group-alist)))
825               num)
826           (when active
827             (if (eobp)
828                 (setf (car active) (1+ (cdr active)))
829               (when (and (setq num (ignore-errors (read (current-buffer))))
830                          (numberp num))
831                 (setf (car active) num)))))))
832     t))
833
834 (defun nnml-update-file-alist (&optional force)
835   (when (or (not nnml-article-file-alist)
836             force)
837     (setq nnml-article-file-alist
838           (nnheader-article-to-file-alist nnml-current-directory))))
839
840 (provide 'nnml)
841
842 ;;; nnml.el ends here