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