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