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