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