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