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