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