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