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