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