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