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