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