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