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