* texi/gnus-ja.texi (Article Date): Update Japanese translation.
[elisp/gnus.git-] / 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 (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-filenames-are-evil t
81   "If non-nil, Gnus will not assume that the articles file name
82 is the same as the article number listed in the nov database.  This
83 variable should be set if any of the files are compressed.")
84
85 (defvoo nnml-prepare-save-mail-hook nil
86   "Hook run narrowed to an article before saving.")
87
88 (defvoo nnml-inhibit-expiry nil
89   "If non-nil, inhibit expiry.")
90
91
92 \f
93
94 (defconst nnml-version "nnml 1.0"
95   "nnml version.")
96
97 (defvoo nnml-nov-file-name ".overview")
98 (defvoo nnml-marks-file-name ".marks")
99
100 (defvoo nnml-current-directory nil)
101 (defvoo nnml-current-group nil)
102 (defvoo nnml-status-string "")
103 (defvoo nnml-nov-buffer-alist nil)
104 (defvoo nnml-group-alist nil)
105 (defvoo nnml-active-timestamp nil)
106 (defvoo nnml-article-file-alist nil)
107
108 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
109
110 (defvar nnml-nov-buffer-file-name nil)
111
112 (defvoo nnml-file-coding-system nnmail-file-coding-system)
113
114 (defvoo nnml-marks nil)
115
116 (defvar nnml-marks-modtime (gnus-make-hashtable))
117
118 \f
119 ;;; Interface functions.
120
121 (nnoo-define-basics nnml)
122
123 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
124   (when (nnml-possibly-change-directory group server)
125     (save-excursion
126       (set-buffer nntp-server-buffer)
127       (erase-buffer)
128       (let* ((file nil)
129              (number (length sequence))
130              (count 0)
131              (file-name-coding-system nnmail-pathname-coding-system)
132              (pathname-coding-system nnmail-pathname-coding-system)
133              beg article)
134         (if (stringp (car sequence))
135             'headers
136           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
137               'nov
138             (while sequence
139               (setq article (car sequence))
140               (setq file (nnml-article-to-file article))
141               (when (and file
142                          (file-exists-p file)
143                          (not (file-directory-p file)))
144                 (insert (format "221 %d Article retrieved.\n" article))
145                 (setq beg (point))
146                 (nnheader-insert-head file)
147                 (goto-char beg)
148                 (if (re-search-forward "\n\r?\n" nil t)
149                     (forward-char -1)
150                   (goto-char (point-max))
151                   (insert "\n\n"))
152                 (insert ".\n")
153                 (delete-region (point) (point-max)))
154               (setq sequence (cdr sequence))
155               (setq count (1+ count))
156               (and (numberp nnmail-large-newsgroup)
157                    (> number nnmail-large-newsgroup)
158                    (zerop (% count 20))
159                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
160                                      (/ (* count 100) number))))
161
162             (and (numberp nnmail-large-newsgroup)
163                  (> number nnmail-large-newsgroup)
164                  (nnheader-message 6 "nnml: Receiving headers...done"))
165
166             (nnheader-fold-continuation-lines)
167             'headers))))))
168
169 (deffoo nnml-open-server (server &optional defs)
170   (nnoo-change-server 'nnml server defs)
171   (when (not (file-exists-p nnml-directory))
172     (ignore-errors (make-directory nnml-directory t)))
173   (cond
174    ((not (file-exists-p nnml-directory))
175     (nnml-close-server)
176     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
177    ((not (file-directory-p (file-truename nnml-directory)))
178     (nnml-close-server)
179     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
180    (t
181     (nnheader-report 'nnml "Opened server %s using directory %s"
182                      server nnml-directory)
183     t)))
184
185 (deffoo nnml-request-regenerate (server)
186   (nnml-possibly-change-directory nil server)
187   (nnml-generate-nov-databases server)
188   t)
189
190 (deffoo nnml-request-article (id &optional group server buffer)
191   (nnml-possibly-change-directory group server)
192   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
193          (file-name-coding-system nnmail-pathname-coding-system)
194          (pathname-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-int (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         (pathname-coding-system nnmail-pathname-coding-system))
227     (cond
228      ((not (nnml-possibly-change-directory group server))
229       (nnheader-report 'nnml "Invalid group (no such directory)"))
230      ((not (file-exists-p nnml-current-directory))
231       (nnheader-report 'nnml "Directory %s does not exist"
232                        nnml-current-directory))
233      ((not (file-directory-p nnml-current-directory))
234       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
235      (dont-check
236       (nnheader-report 'nnml "Group %s selected" group)
237       t)
238      (t
239       (nnheader-re-read-dir nnml-current-directory)
240       (nnmail-activate 'nnml)
241       (let ((active (nth 1 (assoc group nnml-group-alist))))
242         (if (not active)
243             (nnheader-report 'nnml "No such group: %s" group)
244           (nnheader-report 'nnml "Selected group %s" group)
245           (nnheader-insert "211 %d %d %d %s\n"
246                            (max (1+ (- (cdr active) (car active))) 0)
247                            (car active) (cdr active) group)))))))
248
249 (deffoo nnml-request-scan (&optional group server)
250   (setq nnml-article-file-alist nil)
251   (nnml-possibly-change-directory group server)
252   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
253
254 (deffoo nnml-close-group (group &optional server)
255   (setq nnml-article-file-alist nil)
256   t)
257
258 (deffoo nnml-request-create-group (group &optional server args)
259   (nnml-possibly-change-directory nil server)
260   (nnmail-activate 'nnml)
261   (cond
262    ((assoc group nnml-group-alist)
263     t)
264    ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
265          (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
266     (nnheader-report 'nnml "%s is a file"
267                      (nnmail-group-pathname group nnml-directory)))
268    (t
269     (let (active)
270       (push (list group (setq active (cons 1 0)))
271             nnml-group-alist)
272       (nnml-possibly-create-directory group)
273       (nnml-possibly-change-directory group server)
274       (let ((articles (nnml-directory-articles nnml-current-directory)))
275         (when articles
276           (setcar active (apply 'min articles))
277           (setcdr active (apply 'max articles))))
278       (nnmail-save-active nnml-group-alist nnml-active-file)
279       t))))
280
281 (deffoo nnml-request-list (&optional server)
282   (save-excursion
283     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
284           (file-name-coding-system nnmail-pathname-coding-system)
285           (pathname-coding-system nnmail-pathname-coding-system))
286       (nnmail-find-file nnml-active-file))
287     (setq nnml-group-alist (nnmail-get-active))
288     t))
289
290 (deffoo nnml-request-newgroups (date &optional server)
291   (nnml-request-list server))
292
293 (deffoo nnml-request-list-newsgroups (&optional server)
294   (save-excursion
295     (nnmail-find-file nnml-newsgroups-file)))
296
297 (deffoo nnml-request-expire-articles (articles group &optional server force)
298   (nnml-possibly-change-directory group server)
299   (let ((active-articles
300          (nnml-directory-articles nnml-current-directory))
301         (is-old t)
302         article rest mod-time number)
303     (nnmail-activate 'nnml)
304
305     (setq active-articles (sort active-articles '<))
306     ;; Articles not listed in active-articles are already gone,
307     ;; so don't try to expire them.
308     (setq articles (gnus-sorted-intersection articles active-articles))
309
310     (while (and articles is-old)
311       (if (and (setq article (nnml-article-to-file (setq number (pop articles))))
312                (setq mod-time (nth 5 (file-attributes article)))
313                (nnml-deletable-article-p group number)
314                (setq is-old (nnmail-expired-article-p group mod-time force
315                                                       nnml-inhibit-expiry)))
316           (progn
317             ;; Allow a special target group.
318             (unless (eq nnmail-expiry-target 'delete)
319               (with-temp-buffer
320                 (nnml-request-article number group server (current-buffer))
321                 (let (nnml-current-directory
322                       nnml-current-group
323                       nnml-article-file-alist)
324                   (nnmail-expiry-target-group nnmail-expiry-target group)))
325               ;; Maybe directory is changed during nnmail-expiry-target-group.
326               (nnml-possibly-change-directory group server))
327             (nnheader-message 5 "Deleting article %s in %s"
328                               number group)
329             (condition-case ()
330                 (funcall nnmail-delete-file-function article)
331               (file-error
332                (push number rest)))
333             (setq active-articles (delq number active-articles))
334             (nnml-nov-delete-article group number))
335         (push number rest)))
336     (let ((active (nth 1 (assoc group nnml-group-alist))))
337       (when active
338         (setcar active (or (and active-articles
339                                 (apply 'min active-articles))
340                            (1+ (cdr active)))))
341       (nnmail-save-active nnml-group-alist nnml-active-file))
342     (nnml-save-nov)
343     (nconc rest articles)))
344
345 (deffoo nnml-request-move-article
346     (article group server accept-form &optional last)
347   (let ((buf (get-buffer-create " *nnml move*"))
348         result)
349     (nnml-possibly-change-directory group server)
350     (nnml-update-file-alist)
351     (and
352      (nnml-deletable-article-p group article)
353      (nnml-request-article article group server)
354      (let (nnml-current-directory
355            nnml-current-group
356            nnml-article-file-alist)
357        (save-excursion
358          (set-buffer buf)
359          (insert-buffer-substring nntp-server-buffer)
360          (setq result (eval accept-form))
361          (kill-buffer (current-buffer))
362          result))
363      (progn
364        (nnml-possibly-change-directory group server)
365        (condition-case ()
366            (funcall nnmail-delete-file-function
367                     (nnml-article-to-file  article))
368          (file-error nil))
369        (nnml-nov-delete-article group article)
370        (when last
371          (nnml-save-nov)
372          (nnmail-save-active nnml-group-alist nnml-active-file))))
373     result))
374
375 (deffoo nnml-request-accept-article (group &optional server last)
376   (nnml-possibly-change-directory group server)
377   (nnmail-check-syntax)
378   (let (result)
379     (when nnmail-cache-accepted-message-ids
380       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
381     (if (stringp group)
382         (and
383          (nnmail-activate 'nnml)
384          (setq result (car (nnml-save-mail
385                             (list (cons group (nnml-active-number group))))))
386          (progn
387            (nnmail-save-active nnml-group-alist nnml-active-file)
388            (and last (nnml-save-nov))))
389       (and
390        (nnmail-activate 'nnml)
391        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
392                 (yes-or-no-p "Moved to `junk' group; delete article? "))
393            (setq result 'junk)
394          (setq result (car (nnml-save-mail result))))
395        (when last
396          (nnmail-save-active nnml-group-alist nnml-active-file)
397          (when nnmail-cache-accepted-message-ids
398            (nnmail-cache-close))
399          (nnml-save-nov))))
400     result))
401
402 (deffoo nnml-request-post (&optional server)
403   (nnmail-do-request-post 'nnml-request-accept-article server))
404
405 (deffoo nnml-request-replace-article (article group buffer)
406   (nnml-possibly-change-directory group)
407   (save-excursion
408     (set-buffer buffer)
409     (nnml-possibly-create-directory group)
410     (let ((chars (nnmail-insert-lines))
411           (art (concat (int-to-string article) "\t"))
412           headers)
413       (when (ignore-errors
414               (nnmail-write-region
415                (point-min) (point-max)
416                (or (nnml-article-to-file article)
417                    (expand-file-name (int-to-string article)
418                                      nnml-current-directory))
419                nil (if (nnheader-be-verbose 5) nil 'nomesg))
420               t)
421         (setq headers (nnml-parse-head chars article))
422         ;; Replace the NOV line in the NOV file.
423         (save-excursion
424           (set-buffer (nnml-open-nov group))
425           (goto-char (point-min))
426           (if (or (looking-at art)
427                   (search-forward (concat "\n" art) nil t))
428               ;; Delete the old NOV line.
429               (delete-region (progn (beginning-of-line) (point))
430                              (progn (forward-line 1) (point)))
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 (cdr (assq article nnml-article-file-alist)))
527         (expand-file-name file nnml-current-directory)
528       (if (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" (gnus-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           (pathname-coding-system nnmail-pathname-coding-system))
614       (when (not (equal pathname nnml-current-directory))
615         (setq nnml-current-directory pathname
616               nnml-current-group group
617               nnml-article-file-alist nil))
618       (file-exists-p nnml-current-directory))))
619
620 (defun nnml-possibly-create-directory (group)
621   (let ((dir (nnmail-group-pathname group nnml-directory)))
622     (unless (file-exists-p dir)
623       (make-directory (directory-file-name dir) t)
624       (nnheader-message 5 "Creating mail directory %s" dir))))
625
626 (defun nnml-save-mail (group-art)
627   "Called narrowed to an article."
628   (let (chars headers)
629     (setq chars (nnmail-insert-lines))
630     (nnmail-insert-xref group-art)
631     (run-hooks 'nnmail-prepare-save-mail-hook)
632     (run-hooks 'nnml-prepare-save-mail-hook)
633     (goto-char (point-min))
634     (while (looking-at "From ")
635       (replace-match "X-From-Line: ")
636       (forward-line 1))
637     ;; We save the article in all the groups it belongs in.
638     (let ((ga group-art)
639           first)
640       (while ga
641         (nnml-possibly-create-directory (caar ga))
642         (let ((file (concat (nnmail-group-pathname
643                              (caar ga) nnml-directory)
644                             (int-to-string (cdar ga)))))
645           (if first
646               ;; It was already saved, so we just make a hard link.
647               (funcall nnmail-crosspost-link-function first file t)
648             ;; Save the article.
649             (nnmail-write-region (point-min) (point-max) file nil
650                                  (if (nnheader-be-verbose 5) nil 'nomesg))
651             (setq first file)))
652         (setq ga (cdr ga))))
653     ;; Generate a nov line for this article.  We generate the nov
654     ;; line after saving, because nov generation destroys the
655     ;; header.
656     (setq headers (nnml-parse-head chars))
657     ;; Output the nov line to all nov databases that should have it.
658     (let ((ga group-art))
659       (while ga
660         (nnml-add-nov (caar ga) (cdar ga) headers)
661         (setq ga (cdr ga))))
662     group-art))
663
664 (defun nnml-active-number (group)
665   "Compute the next article number in GROUP."
666   (let ((active (cadr (assoc group nnml-group-alist))))
667     ;; The group wasn't known to nnml, so we just create an active
668     ;; entry for it.
669     (unless active
670       ;; Perhaps the active file was corrupt?  See whether
671       ;; there are any articles in this group.
672       (nnml-possibly-create-directory group)
673       (nnml-possibly-change-directory group)
674       (unless nnml-article-file-alist
675         (setq nnml-article-file-alist
676               (sort
677                (nnml-current-group-article-to-file-alist)
678                'car-less-than-car)))
679       (setq active
680             (if nnml-article-file-alist
681                 (cons (caar nnml-article-file-alist)
682                       (caar (last nnml-article-file-alist)))
683               (cons 1 0)))
684       (push (list group active) nnml-group-alist))
685     (setcdr active (1+ (cdr active)))
686     (while (file-exists-p
687             (expand-file-name (int-to-string (cdr active))
688                               (nnmail-group-pathname group nnml-directory)))
689       (setcdr active (1+ (cdr active))))
690     (cdr active)))
691
692 (defun nnml-add-nov (group article headers)
693   "Add a nov line for the GROUP base."
694   (save-excursion
695     (set-buffer (nnml-open-nov group))
696     (goto-char (point-max))
697     (mail-header-set-number headers article)
698     (nnheader-insert-nov headers)))
699
700 (defsubst nnml-header-value ()
701   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
702
703 (defun nnml-parse-head (chars &optional number)
704   "Parse the head of the current buffer."
705   (save-excursion
706     (save-restriction
707       (unless (zerop (buffer-size))
708         (narrow-to-region
709          (goto-char (point-min))
710          (if (re-search-forward "\n\r?\n" nil t) (1- (point)) (point-max))))
711       ;; Fold continuation lines.
712       (goto-char (point-min))
713       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
714         (replace-match " " t t))
715       ;; Remove any tabs; they are too confusing.
716       (subst-char-in-region (point-min) (point-max) ?\t ? )
717       ;; Remove any ^M's; they are too confusing.
718       (subst-char-in-region (point-min) (point-max) ?\r ? )
719       (let ((headers (nnheader-parse-head t)))
720         (mail-header-set-chars headers chars)
721         (mail-header-set-number headers number)
722         headers))))
723
724 (defun nnml-get-nov-buffer (group)
725   (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
726     (save-excursion
727       (set-buffer buffer)
728       (set (make-local-variable 'nnml-nov-buffer-file-name)
729            (expand-file-name
730             nnml-nov-file-name
731             (nnmail-group-pathname group nnml-directory)))
732       (erase-buffer)
733       (when (file-exists-p nnml-nov-buffer-file-name)
734         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
735     buffer))
736
737 (defun nnml-open-nov (group)
738   (or (cdr (assoc group nnml-nov-buffer-alist))
739       (let ((buffer (nnml-get-nov-buffer group)))
740         (push (cons group buffer) nnml-nov-buffer-alist)
741         buffer)))
742
743 (defun nnml-save-nov ()
744   (save-excursion
745     (while nnml-nov-buffer-alist
746       (when (buffer-name (cdar nnml-nov-buffer-alist))
747         (set-buffer (cdar nnml-nov-buffer-alist))
748         (when (buffer-modified-p)
749           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
750                                nil 'nomesg))
751         (set-buffer-modified-p nil)
752         (kill-buffer (current-buffer)))
753       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
754
755 ;;;###autoload
756 (defun nnml-generate-nov-databases (&optional server)
757   "Generate NOV databases in all nnml directories."
758   (interactive (list (or (nnoo-current-server 'nnml) "")))
759   ;; Read the active file to make sure we don't re-use articles
760   ;; numbers in empty groups.
761   (nnmail-activate 'nnml)
762   (unless (nnml-server-opened server)
763     (nnml-open-server server))
764   (setq nnml-directory (expand-file-name nnml-directory))
765   ;; Recurse down the directories.
766   (nnml-generate-nov-databases-1 nnml-directory nil t)
767   ;; Save the active file.
768   (nnmail-save-active nnml-group-alist nnml-active-file))
769
770 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
771   "Regenerate the NOV database in DIR."
772   (interactive "DRegenerate NOV in: ")
773   (setq dir (file-name-as-directory dir))
774   ;; Only scan this sub-tree if we haven't been here yet.
775   (unless (member (file-truename dir) seen)
776     (push (file-truename dir) seen)
777     ;; We descend recursively
778     (let ((dirs (directory-files dir t nil t))
779           dir)
780       (while (setq dir (pop dirs))
781         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
782                    (file-directory-p dir))
783           (nnml-generate-nov-databases-1 dir seen))))
784     ;; Do this directory.
785     (let ((files (sort (nnheader-article-to-file-alist dir)
786                        'car-less-than-car)))
787       (if (not files)
788           (let* ((group (nnheader-file-to-group
789                          (directory-file-name dir) nnml-directory))
790                  (info (cadr (assoc group nnml-group-alist))))
791             (when info
792               (setcar info (1+ (cdr info)))))
793         (funcall nnml-generate-active-function dir)
794         ;; Generate the nov file.
795         (nnml-generate-nov-file dir files)
796         (unless no-active
797           (nnmail-save-active nnml-group-alist nnml-active-file))))))
798
799 (eval-when-compile (defvar files))
800 (defun nnml-generate-active-info (dir)
801   ;; Update the active info for this group.
802   (let* ((group (nnheader-file-to-group
803                  (directory-file-name dir) nnml-directory))
804          (entry (assoc group nnml-group-alist))
805          (last (or (caadr entry) 0)))
806     (setq nnml-group-alist (delq entry nnml-group-alist))
807     (push (list group
808                 (cons (or (caar files) (1+ last))
809                       (max last
810                            (or (let ((f files))
811                                  (while (cdr f) (setq f (cdr f)))
812                                  (caar f))
813                                0))))
814           nnml-group-alist)))
815
816 (defun nnml-generate-nov-file (dir files)
817   (let* ((dir (file-name-as-directory dir))
818          (nov (concat dir nnml-nov-file-name))
819          (nov-buffer (get-buffer-create " *nov*"))
820          chars file headers)
821     (save-excursion
822       ;; Init the nov buffer.
823       (set-buffer nov-buffer)
824       (buffer-disable-undo)
825       (erase-buffer)
826       (set-buffer nntp-server-buffer)
827       ;; Delete the old NOV file.
828       (when (file-exists-p nov)
829         (funcall nnmail-delete-file-function nov))
830       (while files
831         (unless (file-directory-p (setq file (concat dir (cdar files))))
832           (erase-buffer)
833           (nnheader-insert-file-contents file)
834           (narrow-to-region
835            (goto-char (point-min))
836            (progn
837              (re-search-forward "\n\r?\n" nil t)
838              (setq chars (- (point-max) (point)))
839              (max 1 (1- (point)))))
840           (unless (zerop (buffer-size))
841             (goto-char (point-min))
842             (setq headers (nnml-parse-head chars (caar files)))
843             (save-excursion
844               (set-buffer nov-buffer)
845               (goto-char (point-max))
846               (nnheader-insert-nov headers)))
847           (widen))
848         (setq files (cdr files)))
849       (save-excursion
850         (set-buffer nov-buffer)
851         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
852         (kill-buffer (current-buffer))))))
853
854 (defun nnml-nov-delete-article (group article)
855   (save-excursion
856     (set-buffer (nnml-open-nov group))
857     (when (nnheader-find-nov-line article)
858       (delete-region (point) (progn (forward-line 1) (point)))
859       (when (bobp)
860         (let ((active (cadr (assoc group nnml-group-alist)))
861               num)
862           (when active
863             (if (eobp)
864                 (setf (car active) (1+ (cdr active)))
865               (when (and (setq num (ignore-errors (read (current-buffer))))
866                          (numberp num))
867                 (setf (car active) num)))))))
868     t))
869
870 (defun nnml-update-file-alist (&optional force)
871   (when (or (not nnml-article-file-alist)
872             force)
873     (setq nnml-article-file-alist
874           (nnml-current-group-article-to-file-alist))))
875
876 (defun nnml-directory-articles (dir)
877   "Return a list of all article files in a directory.
878 Use the nov database for that directory if available."
879   (if (or gnus-nov-is-evil nnml-nov-is-evil
880           (not (file-exists-p
881                 (expand-file-name nnml-nov-file-name dir))))
882       (nnheader-directory-articles dir)
883     ;; build list from .overview if available
884     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
885     ;; defvoo'd, and we might get called when it hasn't been swapped in.
886     (save-excursion
887       (let ((list nil)
888             art
889             (buffer (nnml-get-nov-buffer nnml-current-group)))
890         (set-buffer buffer)
891         (goto-char (point-min))
892         (while (not (eobp))
893           (setq art (read (current-buffer)))
894           (push art list)
895           (forward-line 1))
896         list))))
897
898 (defun nnml-current-group-article-to-file-alist ()
899   "Return an alist of article/file pairs in the current group.
900 Use the nov database for the current group if available."
901   (if (or gnus-nov-is-evil
902           nnml-nov-is-evil
903           nnml-filenames-are-evil
904           (not (file-exists-p
905                 (expand-file-name nnml-nov-file-name
906                                   nnml-current-directory))))
907       (nnheader-article-to-file-alist nnml-current-directory)
908     ;; build list from .overview if available
909     (save-excursion
910       (let ((alist nil)
911             art
912             (buffer (nnml-get-nov-buffer nnml-current-group)))
913         (set-buffer buffer)
914         (goto-char (point-min))
915         (while (not (eobp))
916           (setq art (read (current-buffer)))
917           ;; assume file name is unadorned (ie. not compressed etc)
918           (push (cons art (int-to-string art)) alist)
919           (forward-line 1))
920         alist))))
921
922 (deffoo nnml-request-set-mark (group actions &optional server)
923   (nnml-possibly-change-directory group server)
924   (unless nnml-marks-is-evil
925     (nnml-open-marks group server)
926     (dolist (action actions)
927       (let ((range (nth 0 action))
928             (what  (nth 1 action))
929             (marks (nth 2 action)))
930         (assert (or (eq what 'add) (eq what 'del)) t
931                 "Unknown request-set-mark action: %s" what)
932         (dolist (mark marks)
933           (setq nnml-marks (gnus-update-alist-soft
934                             mark
935                             (funcall (if (eq what 'add) 'gnus-range-add
936                                        'gnus-remove-from-range)
937                                      (cdr (assoc mark nnml-marks)) range)
938                             nnml-marks)))))
939     (nnml-save-marks group server))
940   nil)
941
942 (deffoo nnml-request-update-info (group info &optional server)
943   (nnml-possibly-change-directory group server)
944   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
945     (nnheader-message 8 "Updating marks for %s..." group)
946     (nnml-open-marks group server)
947     ;; Update info using `nnml-marks'.
948     (mapcar (lambda (pred)
949               (gnus-info-set-marks
950                info
951                (gnus-update-alist-soft
952                 (cdr pred)
953                 (cdr (assq (cdr pred) nnml-marks))
954                 (gnus-info-marks info))
955                t))
956             gnus-article-mark-lists)
957     (let ((seen (cdr (assq 'read nnml-marks))))
958       (gnus-info-set-read info
959                           (if (and (integerp (car seen))
960                                    (null (cdr seen)))
961                               (list (cons (car seen) (car seen)))
962                             seen)))
963     (nnheader-message 8 "Updating marks for %s...done" group))
964   info)
965
966 (defun nnml-marks-changed-p (group)
967   (let ((file (expand-file-name nnml-marks-file-name
968                                 (nnmail-group-pathname group nnml-directory))))
969     (if (null (gnus-gethash file nnml-marks-modtime))
970         t ;; never looked at marks file, assume it has changed
971       (not (equal (gnus-gethash file nnml-marks-modtime)
972                   (nth 5 (file-attributes file)))))))
973
974 (defun nnml-save-marks (group server)
975   (let ((file-name-coding-system nnmail-pathname-coding-system)
976         (file (expand-file-name nnml-marks-file-name
977                                 (nnmail-group-pathname group nnml-directory))))
978     (condition-case err
979         (progn
980           (nnml-possibly-create-directory group)
981           (with-temp-file file
982             (erase-buffer)
983             (gnus-prin1 nnml-marks)
984             (insert "\n"))
985           (gnus-sethash file
986                         (nth 5 (file-attributes file))
987                         nnml-marks-modtime))
988       (error (or (gnus-yes-or-no-p
989                   (format "Could not write to %s (%s).  Continue? " file err))
990                  (error "Cannot write to %s (%s)" err))))))
991
992 (defun nnml-open-marks (group server)
993   (let ((file (expand-file-name
994                nnml-marks-file-name
995                (nnmail-group-pathname group nnml-directory))))
996     (if (file-exists-p file)
997         (condition-case err
998             (with-temp-buffer
999               (gnus-sethash file (nth 5 (file-attributes file))
1000                             nnml-marks-modtime)
1001               (nnheader-insert-file-contents file)
1002               (setq nnml-marks (read (current-buffer)))
1003               (dolist (el gnus-article-unpropagated-mark-lists)
1004                 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1005           (error (or (gnus-yes-or-no-p
1006                       (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1007                      (error "Cannot read nnml marks file %s (%s)" file err))))
1008       ;; User didn't have a .marks file.  Probably first time
1009       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1010       (let ((info (gnus-get-info
1011                    (gnus-group-prefixed-name
1012                     group
1013                     (gnus-server-to-method (format "nnml:%s" server))))))
1014         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1015         (setq nnml-marks (gnus-info-marks info))
1016         (push (cons 'read (gnus-info-read info)) nnml-marks)
1017         (dolist (el gnus-article-unpropagated-mark-lists)
1018           (setq nnml-marks (gnus-remassoc el nnml-marks)))
1019         (nnml-save-marks group server)))))
1020
1021 (provide 'nnml)
1022
1023 ;;; nnml.el ends here