(nnml-request-post): New function. Can be used for
[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   (let ((success t))
374     (dolist (mbx (message-unquote-tokens
375                   (message-tokenize-header
376                    (message-fetch-field "Newsgroups") ", ")) success)
377       (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
378         (or (gnus-active to-newsgroup)
379             (gnus-activate-group to-newsgroup)
380             (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
381                                        to-newsgroup))
382                 (or (and (gnus-request-create-group
383                           to-newsgroup gnus-command-method)
384                          (gnus-activate-group to-newsgroup nil nil
385                                               gnus-command-method))
386                     (error "Couldn't create group %s" to-newsgroup)))
387             (error "No such group: %s" to-newsgroup))
388         (unless (nnml-request-accept-article mbx (nth 1 gnus-command-method))
389           (setq success nil))))))
390
391 (deffoo nnml-request-replace-article (article group buffer)
392   (nnml-possibly-change-directory group)
393   (save-excursion
394     (set-buffer buffer)
395     (nnml-possibly-create-directory group)
396     (let ((chars (nnmail-insert-lines))
397           (art (concat (int-to-string article) "\t"))
398           headers)
399       (when (ignore-errors
400               (nnmail-write-region
401                (point-min) (point-max)
402                (or (nnml-article-to-file article)
403                    (expand-file-name (int-to-string article)
404                                      nnml-current-directory))
405                nil (if (nnheader-be-verbose 5) nil 'nomesg))
406               t)
407         (setq headers (nnml-parse-head chars article))
408         ;; Replace the NOV line in the NOV file.
409         (save-excursion
410           (set-buffer (nnml-open-nov group))
411           (goto-char (point-min))
412           (if (or (looking-at art)
413                   (search-forward (concat "\n" art) nil t))
414               ;; Delete the old NOV line.
415               (delete-region (progn (beginning-of-line) (point))
416                              (progn (forward-line 1) (point)))
417             ;; The line isn't here, so we have to find out where
418             ;; we should insert it.  (This situation should never
419             ;; occur, but one likes to make sure...)
420             (while (and (looking-at "[0-9]+\t")
421                         (< (string-to-int
422                             (buffer-substring
423                              (match-beginning 0) (match-end 0)))
424                            article)
425                         (zerop (forward-line 1)))))
426           (beginning-of-line)
427           (nnheader-insert-nov headers)
428           (nnml-save-nov)
429           t)))))
430
431 (deffoo nnml-request-delete-group (group &optional force server)
432   (nnml-possibly-change-directory group server)
433   (when force
434     ;; Delete all articles in GROUP.
435     (let ((articles
436            (directory-files
437             nnml-current-directory t
438             (concat nnheader-numerical-short-files
439                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
440           article)
441       (while articles
442         (setq article (pop articles))
443         (when (file-writable-p article)
444           (nnheader-message 5 "Deleting article %s in %s..." article group)
445           (funcall nnmail-delete-file-function article))))
446     ;; Try to delete the directory itself.
447     (ignore-errors (delete-directory nnml-current-directory)))
448   ;; Remove the group from all structures.
449   (setq nnml-group-alist
450         (delq (assoc group nnml-group-alist) nnml-group-alist)
451         nnml-current-group nil
452         nnml-current-directory nil)
453   ;; Save the active file.
454   (nnmail-save-active nnml-group-alist nnml-active-file)
455   t)
456
457 (deffoo nnml-request-rename-group (group new-name &optional server)
458   (nnml-possibly-change-directory group server)
459   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
460         (old-dir (nnmail-group-pathname group nnml-directory)))
461     (when (ignore-errors
462             (make-directory new-dir t)
463             t)
464       ;; We move the articles file by file instead of renaming
465       ;; the directory -- there may be subgroups in this group.
466       ;; One might be more clever, I guess.
467       (let ((files (nnheader-article-to-file-alist old-dir)))
468         (while files
469           (rename-file
470            (concat old-dir (cdar files))
471            (concat new-dir (cdar files)))
472           (pop files)))
473       ;; Move .overview file.
474       (let ((overview (concat old-dir nnml-nov-file-name)))
475         (when (file-exists-p overview)
476           (rename-file overview (concat new-dir nnml-nov-file-name))))
477       (when (<= (length (directory-files old-dir)) 2)
478         (ignore-errors (delete-directory old-dir)))
479       ;; That went ok, so we change the internal structures.
480       (let ((entry (assoc group nnml-group-alist)))
481         (when entry
482           (setcar entry new-name))
483         (setq nnml-current-directory nil
484               nnml-current-group nil)
485         ;; Save the new group alist.
486         (nnmail-save-active nnml-group-alist nnml-active-file)
487         t))))
488
489 (deffoo nnml-set-status (article name value &optional group server)
490   (nnml-possibly-change-directory group server)
491   (let ((file (nnml-article-to-file article)))
492     (cond
493      ((not (file-exists-p file))
494       (nnheader-report 'nnml "File %s does not exist" file))
495      (t
496       (with-temp-file file
497         (nnheader-insert-file-contents file)
498         (nnmail-replace-status name value))
499       t))))
500
501 \f
502 ;;; Internal functions.
503
504 (defun nnml-article-to-file (article)
505   (nnml-update-file-alist)
506   (let (file)
507     (if (setq file (cdr (assq article nnml-article-file-alist)))
508         (expand-file-name file nnml-current-directory)
509       ;; Just to make sure nothing went wrong when reading over NFS --
510       ;; check once more.
511       (when (file-exists-p
512              (setq file (expand-file-name (number-to-string article)
513                                           nnml-current-directory)))
514         (nnml-update-file-alist t)
515         file))))
516
517 (defun nnml-deletable-article-p (group article)
518   "Say whether ARTICLE in GROUP can be deleted."
519   (let (path)
520     (when (setq path (nnml-article-to-file article))
521       (when (file-writable-p path)
522         (or (not nnmail-keep-last-article)
523             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
524                      article)))))))
525
526 ;; Find an article number in the current group given the Message-ID.
527 (defun nnml-find-group-number (id)
528   (save-excursion
529     (set-buffer (get-buffer-create " *nnml id*"))
530     (let ((alist nnml-group-alist)
531           number)
532       ;; We want to look through all .overview files, but we want to
533       ;; start with the one in the current directory.  It seems most
534       ;; likely that the article we are looking for is in that group.
535       (if (setq number (nnml-find-id nnml-current-group id))
536           (cons nnml-current-group number)
537         ;; It wasn't there, so we look through the other groups as well.
538         (while (and (not number)
539                     alist)
540           (or (string= (caar alist) nnml-current-group)
541               (setq number (nnml-find-id (caar alist) id)))
542           (or number
543               (setq alist (cdr alist))))
544         (and number
545              (cons (caar alist) number))))))
546
547 (defun nnml-find-id (group id)
548   (erase-buffer)
549   (let ((nov (expand-file-name nnml-nov-file-name
550                                (nnmail-group-pathname group nnml-directory)))
551         number found)
552     (when (file-exists-p nov)
553       (nnheader-insert-file-contents nov)
554       (while (and (not found)
555                   (search-forward id nil t)) ; We find the ID.
556         ;; And the id is in the fourth field.
557         (if (not (and (search-backward "\t" nil t 4)
558                       (not (search-backward"\t" (gnus-point-at-bol) t))))
559             (forward-line 1)
560           (beginning-of-line)
561           (setq found t)
562           ;; We return the article number.
563           (setq number
564                 (ignore-errors (read (current-buffer))))))
565       number)))
566
567 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
568   (if (or gnus-nov-is-evil nnml-nov-is-evil)
569       nil
570     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
571       (when (file-exists-p nov)
572         (save-excursion
573           (set-buffer nntp-server-buffer)
574           (erase-buffer)
575           (nnheader-insert-file-contents nov)
576           (if (and fetch-old
577                    (not (numberp fetch-old)))
578               t                         ; Don't remove anything.
579             (nnheader-nov-delete-outside-range
580              (if fetch-old (max 1 (- (car articles) fetch-old))
581                (car articles))
582              (car (last articles)))
583             t))))))
584
585 (defun nnml-possibly-change-directory (group &optional server)
586   (when (and server
587              (not (nnml-server-opened server)))
588     (nnml-open-server server))
589   (if (not group)
590       t
591     (let ((pathname (nnmail-group-pathname group nnml-directory))
592           (file-name-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