2001-09-15 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-filenames-are-evil t
88   "If non-nil, Gnus will not assume that the articles file name 
89 is the same as the article number listed in the nov database.  This 
90 variable should be set if any of the files are compressed.
91
92 This variable is a virtual server slot.  See the Gnus manual for details.")
93
94 (defvoo nnml-prepare-save-mail-hook nil
95   "Hook run narrowed to an article before saving.
96
97 This variable is a virtual server slot.  See the Gnus manual for details.")
98
99 (defvoo nnml-inhibit-expiry nil
100   "If non-nil, inhibit expiry.
101
102 This variable is a virtual server slot.  See the Gnus manual for details.")
103
104
105 \f
106
107 (defconst nnml-version "nnml 1.0"
108   "nnml version.")
109
110 (defvoo nnml-nov-file-name ".overview")
111 (defvoo nnml-marks-file-name ".marks")
112
113 (defvoo nnml-current-directory nil)
114 (defvoo nnml-current-group nil)
115 (defvoo nnml-status-string "")
116 (defvoo nnml-nov-buffer-alist nil)
117 (defvoo nnml-group-alist nil)
118 (defvoo nnml-active-timestamp nil)
119 (defvoo nnml-article-file-alist nil)
120
121 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
122
123 (defvar nnml-nov-buffer-file-name nil)
124
125 (defvoo nnml-file-coding-system nnmail-file-coding-system)
126
127 (defvoo nnml-marks nil)
128
129 (defvar nnml-marks-modtime (gnus-make-hashtable))
130
131 \f
132 ;;; Interface functions.
133
134 (nnoo-define-basics nnml)
135
136 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
137   (when (nnml-possibly-change-directory group server)
138     (save-excursion
139       (set-buffer nntp-server-buffer)
140       (erase-buffer)
141       (let* ((file nil)
142              (number (length sequence))
143              (count 0)
144              (file-name-coding-system nnmail-pathname-coding-system)
145              beg article)
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 (re-search-forward "\n\r?\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 (nnml-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          (nnml-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       (if (and (setq article (nnml-article-to-file (setq number (pop articles))))
321                (setq mod-time (nth 5 (file-attributes article)))
322                (nnml-deletable-article-p group number)
323                (setq is-old (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 (current-buffer))
330                 (let (nnml-current-directory
331                       nnml-current-group
332                       nnml-article-file-alist)
333                   (nnmail-expiry-target-group 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 (not nnheader-directory-files-is-safe)
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                (nnml-current-group-article-to-file-alist)
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 (re-search-forward "\n\r?\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       ;; Remove any ^M's; they are too confusing.
724       (subst-char-in-region (point-min) (point-max) ?\r ? )
725       (let ((headers (nnheader-parse-head t)))
726         (mail-header-set-chars headers chars)
727         (mail-header-set-number headers number)
728         headers))))
729
730 (defun nnml-get-nov-buffer (group)
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     buffer))
742
743 (defun nnml-open-nov (group)
744   (or (cdr (assoc group nnml-nov-buffer-alist))
745       (let ((buffer (nnml-get-nov-buffer group)))
746         (push (cons group buffer) nnml-nov-buffer-alist)
747         buffer)))
748
749 (defun nnml-save-nov ()
750   (save-excursion
751     (while nnml-nov-buffer-alist
752       (when (buffer-name (cdar nnml-nov-buffer-alist))
753         (set-buffer (cdar nnml-nov-buffer-alist))
754         (when (buffer-modified-p)
755           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
756                                nil 'nomesg))
757         (set-buffer-modified-p nil)
758         (kill-buffer (current-buffer)))
759       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
760
761 ;;;###autoload
762 (defun nnml-generate-nov-databases (&optional server)
763   "Generate NOV databases in all nnml directories."
764   (interactive (list (or (nnoo-current-server 'nnml) "")))
765   ;; Read the active file to make sure we don't re-use articles
766   ;; numbers in empty groups.
767   (nnmail-activate 'nnml)
768   (unless (nnml-server-opened server)
769     (nnml-open-server server))
770   (setq nnml-directory (expand-file-name nnml-directory))
771   ;; Recurse down the directories.
772   (nnml-generate-nov-databases-1 nnml-directory nil t)
773   ;; Save the active file.
774   (nnmail-save-active nnml-group-alist nnml-active-file))
775
776 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
777   "Regenerate the NOV database in DIR."
778   (interactive "DRegenerate NOV in: ")
779   (setq dir (file-name-as-directory dir))
780   ;; Only scan this sub-tree if we haven't been here yet.
781   (unless (member (file-truename dir) seen)
782     (push (file-truename dir) seen)
783     ;; We descend recursively
784     (let ((dirs (directory-files dir t nil t))
785           dir)
786       (while (setq dir (pop dirs))
787         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
788                    (file-directory-p dir))
789           (nnml-generate-nov-databases-1 dir seen))))
790     ;; Do this directory.
791     (let ((files (sort (nnheader-article-to-file-alist dir)
792                        'car-less-than-car)))
793       (if (not files)
794           (let* ((group (nnheader-file-to-group
795                          (directory-file-name dir) nnml-directory))
796                  (info (cadr (assoc group nnml-group-alist))))
797             (when info
798               (setcar info (1+ (cdr info)))))
799         (funcall nnml-generate-active-function dir)
800         ;; Generate the nov file.
801         (nnml-generate-nov-file dir files)
802         (unless no-active
803           (nnmail-save-active nnml-group-alist nnml-active-file))))))
804
805 (eval-when-compile (defvar files))
806 (defun nnml-generate-active-info (dir)
807   ;; Update the active info for this group.
808   (let* ((group (nnheader-file-to-group
809                  (directory-file-name dir) nnml-directory))
810          (entry (assoc group nnml-group-alist))
811          (last (or (caadr entry) 0)))
812     (setq nnml-group-alist (delq entry nnml-group-alist))
813     (push (list group
814                 (cons (or (caar files) (1+ last))
815                       (max last
816                            (or (let ((f files))
817                                  (while (cdr f) (setq f (cdr f)))
818                                  (caar f))
819                                0))))
820           nnml-group-alist)))
821
822 (defun nnml-generate-nov-file (dir files)
823   (let* ((dir (file-name-as-directory dir))
824          (nov (concat dir nnml-nov-file-name))
825          (nov-buffer (get-buffer-create " *nov*"))
826          chars file headers)
827     (save-excursion
828       ;; Init the nov buffer.
829       (set-buffer nov-buffer)
830       (buffer-disable-undo)
831       (erase-buffer)
832       (set-buffer nntp-server-buffer)
833       ;; Delete the old NOV file.
834       (when (file-exists-p nov)
835         (funcall nnmail-delete-file-function nov))
836       (while files
837         (unless (file-directory-p (setq file (concat dir (cdar files))))
838           (erase-buffer)
839           (nnheader-insert-file-contents file)
840           (narrow-to-region
841            (goto-char (point-min))
842            (progn
843              (re-search-forward "\n\r?\n" nil t)
844              (setq chars (- (point-max) (point)))
845              (max 1 (1- (point)))))
846           (unless (zerop (buffer-size))
847             (goto-char (point-min))
848             (setq headers (nnml-parse-head chars (caar files)))
849             (save-excursion
850               (set-buffer nov-buffer)
851               (goto-char (point-max))
852               (nnheader-insert-nov headers)))
853           (widen))
854         (setq files (cdr files)))
855       (save-excursion
856         (set-buffer nov-buffer)
857         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
858         (kill-buffer (current-buffer))))))
859
860 (defun nnml-nov-delete-article (group article)
861   (save-excursion
862     (set-buffer (nnml-open-nov group))
863     (when (nnheader-find-nov-line article)
864       (delete-region (point) (progn (forward-line 1) (point)))
865       (when (bobp)
866         (let ((active (cadr (assoc group nnml-group-alist)))
867               num)
868           (when active
869             (if (eobp)
870                 (setf (car active) (1+ (cdr active)))
871               (when (and (setq num (ignore-errors (read (current-buffer))))
872                          (numberp num))
873                 (setf (car active) num)))))))
874     t))
875
876 (defun nnml-update-file-alist (&optional force)
877   (when (or (not nnml-article-file-alist)
878             force)
879     (setq nnml-article-file-alist
880           (nnml-current-group-article-to-file-alist))))
881
882 (defun nnml-directory-articles (dir)
883   "Return a list of all article files in a directory.
884 Use the nov database for that directory if available."
885   (if (or gnus-nov-is-evil nnml-nov-is-evil
886           (not (file-exists-p
887                 (expand-file-name nnml-nov-file-name dir))))
888       (nnheader-directory-articles dir)
889     ;; build list from .overview if available
890     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
891     ;; defvoo'd, and we might get called when it hasn't been swapped in.
892     (save-excursion
893       (let ((list nil)
894             art
895             (buffer (nnml-get-nov-buffer nnml-current-group)))
896         (set-buffer buffer)
897         (goto-char (point-min))
898         (while (not (eobp))
899           (setq art (read (current-buffer)))
900           (push art list)
901           (forward-line 1))
902         list))))
903
904 (defun nnml-current-group-article-to-file-alist ()
905   "Return an alist of article/file pairs in the current group.
906 Use the nov database for the current group if available."
907   (if (or gnus-nov-is-evil 
908           nnml-nov-is-evil
909           nnml-filenames-are-evil
910           (not (file-exists-p
911                 (expand-file-name nnml-nov-file-name
912                                   nnml-current-directory))))
913       (nnheader-article-to-file-alist nnml-current-directory)
914     ;; build list from .overview if available
915     (save-excursion
916       (let ((alist nil)
917             art
918             (buffer (nnml-get-nov-buffer nnml-current-group)))
919         (set-buffer buffer)
920         (goto-char (point-min))
921         (while (not (eobp))
922           (setq art (read (current-buffer)))
923           ;; assume file name is unadorned (ie. not compressed etc)
924           (push (cons art (int-to-string art)) alist)
925           (forward-line 1))
926         alist))))
927
928 (deffoo nnml-request-set-mark (group actions &optional server)
929   (nnml-possibly-change-directory group server)
930   (unless nnml-marks-is-evil
931     (nnml-open-marks group server)
932     (dolist (action actions)
933       (let ((range (nth 0 action))
934             (what  (nth 1 action))
935             (marks (nth 2 action)))
936         (assert (or (eq what 'add) (eq what 'del)) t
937                 "Unknown request-set-mark action: %s" what)
938         (dolist (mark marks)
939           (setq nnml-marks (gnus-update-alist-soft
940                             mark
941                             (funcall (if (eq what 'add) 'gnus-range-add
942                                        'gnus-remove-from-range)
943                                      (cdr (assoc mark nnml-marks)) range)
944                             nnml-marks)))))
945     (nnml-save-marks group server))
946   nil)
947
948 (deffoo nnml-request-update-info (group info &optional server)
949   (nnml-possibly-change-directory group server)
950   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
951     (nnheader-message 8 "Updating marks for %s..." group)
952     (nnml-open-marks group server)
953     ;; Update info using `nnml-marks'.
954     (mapcar (lambda (pred)
955               (gnus-info-set-marks
956                info
957                (gnus-update-alist-soft
958                 (cdr pred)
959                 (cdr (assq (cdr pred) nnml-marks))
960                 (gnus-info-marks info))
961                t))
962             gnus-article-mark-lists)
963     (let ((seen (cdr (assq 'read nnml-marks))))
964       (gnus-info-set-read info
965                           (if (and (integerp (car seen))
966                                    (null (cdr seen)))
967                               (list (cons (car seen) (car seen)))
968                             seen)))
969     (nnheader-message 8 "Updating marks for %s...done" group))
970   info)
971
972 (defun nnml-marks-changed-p (group)
973   (let ((file (expand-file-name nnml-marks-file-name
974                                 (nnmail-group-pathname group nnml-directory))))
975     (if (null (gnus-gethash file nnml-marks-modtime))
976         t ;; never looked at marks file, assume it has changed
977       (not (eq (gnus-gethash file nnml-marks-modtime)
978                (nth 5 (file-attributes file)))))))
979
980 (defun nnml-save-marks (group server)
981   (let ((file-name-coding-system nnmail-pathname-coding-system)
982         (file (expand-file-name nnml-marks-file-name
983                                 (nnmail-group-pathname group nnml-directory))))
984     (condition-case err
985         (progn
986           (nnml-possibly-create-directory group)
987           (with-temp-file file
988             (erase-buffer)
989             (princ nnml-marks (current-buffer))
990             (insert "\n"))
991           (gnus-sethash file
992                         (nth 5 (file-attributes file))
993                         nnml-marks-modtime))
994       (error (or (gnus-yes-or-no-p
995                   (format "Could not write to %s (%s).  Continue? " file err))
996                  (error "Cannot write to %s (%s)" err))))))
997
998 (defun nnml-open-marks (group server)
999   (let ((file (expand-file-name 
1000                nnml-marks-file-name 
1001                (nnmail-group-pathname group nnml-directory))))
1002     (if (file-exists-p file)
1003         (setq nnml-marks (condition-case err
1004                              (with-temp-buffer
1005                                (gnus-sethash file
1006                                              (nth 5 (file-attributes file))
1007                                              nnml-marks-modtime)
1008                                (nnheader-insert-file-contents file)
1009                                (read (current-buffer)))
1010                            (error (or (gnus-yes-or-no-p
1011                                        (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1012                                       (error "Cannot read nnml marks file %s (%s)" file err)))))
1013       ;; User didn't have a .marks file.  Probably first time
1014       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1015       (let ((info (gnus-get-info
1016                    (gnus-group-prefixed-name
1017                     group
1018                     (gnus-server-to-method (format "nnml:%s" server))))))
1019         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1020         (setq nnml-marks (gnus-info-marks info))
1021         (push (cons 'read (gnus-info-read info)) nnml-marks)
1022         (nnml-save-marks group server)))))
1023
1024 (provide 'nnml)
1025
1026 ;;; nnml.el ends here