* nnheader.el (nnheader-parse-naked-head): New function.
[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 (eval-and-compile
42   (autoload 'gnus-article-unpropagatable-p "gnus-sum"))
43
44 (nnoo-declare nnml)
45
46 (defvoo nnml-directory message-directory
47   "Spool directory for the nnml mail backend.")
48
49 (defvoo nnml-active-file
50     (expand-file-name "active" nnml-directory)
51   "Mail active file.")
52
53 (defvoo nnml-newsgroups-file
54     (expand-file-name "newsgroups" nnml-directory)
55   "Mail newsgroups description file.")
56
57 (defvoo nnml-get-new-mail t
58   "If non-nil, nnml will check the incoming mail file and split the mail.")
59
60 (defvoo nnml-nov-is-evil nil
61   "If non-nil, Gnus will never generate and use nov databases for mail spools.
62 Using nov databases will speed up header fetching considerably.
63 This variable shouldn't be flipped much.  If you have, for some reason,
64 set this to t, and want to set it to nil again, you should always run
65 the `nnml-generate-nov-databases' command.  The function will go
66 through all nnml directories and generate nov databases for them
67 all.  This may very well take some time.")
68
69 (defvoo nnml-marks-is-evil nil
70   "If non-nil, Gnus will never generate and use marks file for mail spools.
71 Using marks files makes it possible to backup and restore mail groups
72 separately from `.newsrc.eld'.  If you have, for some reason, set this
73 to t, and want to set it to nil again, you should always remove the
74 corresponding marks file (usually named `.marks' in the nnml group
75 directory, but see `nnml-marks-file-name') for the group.  Then the
76 marks file will be regenerated properly by Gnus.")
77
78 (defvoo nnml-prepare-save-mail-hook nil
79   "Hook run narrowed to an article before saving.")
80
81 (defvoo nnml-inhibit-expiry nil
82   "If non-nil, inhibit expiry.")
83
84 (defvoo nnml-use-compressed-files nil
85   "If non-nil, allow using compressed message files.")
86
87 \f
88
89 (defconst nnml-version "nnml 1.0"
90   "nnml version.")
91
92 (defvoo nnml-nov-file-name ".overview")
93 (defvoo nnml-marks-file-name ".marks")
94
95 (defvoo nnml-current-directory nil)
96 (defvoo nnml-current-group nil)
97 (defvoo nnml-status-string "")
98 (defvoo nnml-nov-buffer-alist nil)
99 (defvoo nnml-group-alist nil)
100 (defvoo nnml-active-timestamp nil)
101 (defvoo nnml-article-file-alist nil)
102
103 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
104
105 (defvar nnml-nov-buffer-file-name nil)
106
107 (defvoo nnml-file-coding-system nnmail-file-coding-system)
108
109 (defvoo nnml-marks nil)
110
111 (defvar nnml-marks-modtime (gnus-make-hashtable))
112
113 \f
114 ;;; Interface functions.
115
116 (nnoo-define-basics nnml)
117
118 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
119   (when (nnml-possibly-change-directory group server)
120     (save-excursion
121       (set-buffer nntp-server-buffer)
122       (erase-buffer)
123       (let* ((file nil)
124              (number (length sequence))
125              (count 0)
126              (file-name-coding-system nnmail-pathname-coding-system)
127              beg article)
128         (if (stringp (car sequence))
129             'headers
130           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
131               'nov
132             (while sequence
133               (setq article (car sequence))
134               (setq file (nnml-article-to-file article))
135               (when (and file
136                          (file-exists-p file)
137                          (not (file-directory-p file)))
138                 (insert (format "221 %d Article retrieved.\n" article))
139                 (setq beg (point))
140                 (nnheader-insert-head file)
141                 (goto-char beg)
142                 (if (re-search-forward "\n\r?\n" nil t)
143                     (forward-char -1)
144                   (goto-char (point-max))
145                   (insert "\n\n"))
146                 (insert ".\n")
147                 (delete-region (point) (point-max)))
148               (setq sequence (cdr sequence))
149               (setq count (1+ count))
150               (and (numberp nnmail-large-newsgroup)
151                    (> number nnmail-large-newsgroup)
152                    (zerop (% count 20))
153                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
154                                      (/ (* count 100) number))))
155
156             (and (numberp nnmail-large-newsgroup)
157                  (> number nnmail-large-newsgroup)
158                  (nnheader-message 6 "nnml: Receiving headers...done"))
159
160             (nnheader-fold-continuation-lines)
161             'headers))))))
162
163 (deffoo nnml-open-server (server &optional defs)
164   (nnoo-change-server 'nnml server defs)
165   (when (not (file-exists-p nnml-directory))
166     (ignore-errors (make-directory nnml-directory t)))
167   (cond
168    ((not (file-exists-p nnml-directory))
169     (nnml-close-server)
170     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
171    ((not (file-directory-p (file-truename nnml-directory)))
172     (nnml-close-server)
173     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
174    (t
175     (nnheader-report 'nnml "Opened server %s using directory %s"
176                      server nnml-directory)
177     t)))
178
179 (deffoo nnml-request-regenerate (server)
180   (nnml-possibly-change-directory nil server)
181   (nnml-generate-nov-databases server)
182   t)
183
184 (deffoo nnml-request-article (id &optional group server buffer)
185   (nnml-possibly-change-directory group server)
186   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
187          (file-name-coding-system nnmail-pathname-coding-system)
188          path gpath group-num)
189     (if (stringp id)
190         (when (and (setq group-num (nnml-find-group-number id))
191                    (cdr
192                     (assq (cdr group-num)
193                           (nnheader-article-to-file-alist
194                            (setq gpath
195                                  (nnmail-group-pathname
196                                   (car group-num)
197                                   nnml-directory))))))
198           (setq path (concat gpath (int-to-string (cdr group-num)))))
199       (setq path (nnml-article-to-file id)))
200     (cond
201      ((not path)
202       (nnheader-report 'nnml "No such article: %s" id))
203      ((not (file-exists-p path))
204       (nnheader-report 'nnml "No such file: %s" path))
205      ((file-directory-p path)
206       (nnheader-report 'nnml "File is a directory: %s" path))
207      ((not (save-excursion (let ((nnmail-file-coding-system
208                                   nnml-file-coding-system))
209                              (nnmail-find-file path))))
210       (nnheader-report 'nnml "Couldn't read file: %s" path))
211      (t
212       (nnheader-report 'nnml "Article %s retrieved" id)
213       ;; We return the article number.
214       (cons (if group-num (car group-num) group)
215             (string-to-int (file-name-nondirectory path)))))))
216
217 (deffoo nnml-request-group (group &optional server dont-check)
218   (let ((file-name-coding-system nnmail-pathname-coding-system))
219     (cond
220      ((not (nnml-possibly-change-directory group server))
221       (nnheader-report 'nnml "Invalid group (no such directory)"))
222      ((not (file-exists-p nnml-current-directory))
223       (nnheader-report 'nnml "Directory %s does not exist"
224                        nnml-current-directory))
225      ((not (file-directory-p nnml-current-directory))
226       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
227      (dont-check
228       (nnheader-report 'nnml "Group %s selected" group)
229       t)
230      (t
231       (nnheader-re-read-dir nnml-current-directory)
232       (nnmail-activate 'nnml)
233       (let ((active (nth 1 (assoc group nnml-group-alist))))
234         (if (not active)
235             (nnheader-report 'nnml "No such group: %s" group)
236           (nnheader-report 'nnml "Selected group %s" group)
237           (nnheader-insert "211 %d %d %d %s\n"
238                            (max (1+ (- (cdr active) (car active))) 0)
239                            (car active) (cdr active) group)))))))
240
241 (deffoo nnml-request-scan (&optional group server)
242   (setq nnml-article-file-alist nil)
243   (nnml-possibly-change-directory group server)
244   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
245
246 (deffoo nnml-close-group (group &optional server)
247   (setq nnml-article-file-alist nil)
248   t)
249
250 (deffoo nnml-request-create-group (group &optional server args)
251   (nnml-possibly-change-directory nil server)
252   (nnmail-activate 'nnml)
253   (cond
254    ((assoc group nnml-group-alist)
255     t)
256    ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
257          (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
258     (nnheader-report 'nnml "%s is a file"
259                      (nnmail-group-pathname group nnml-directory)))
260    (t
261     (let (active)
262       (push (list group (setq active (cons 1 0)))
263             nnml-group-alist)
264       (nnml-possibly-create-directory group)
265       (nnml-possibly-change-directory group server)
266       (let ((articles (nnml-directory-articles nnml-current-directory)))
267         (when articles
268           (setcar active (apply 'min articles))
269           (setcdr active (apply 'max articles))))
270       (nnmail-save-active nnml-group-alist nnml-active-file)
271       t))))
272
273 (deffoo nnml-request-list (&optional server)
274   (save-excursion
275     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
276           (file-name-coding-system nnmail-pathname-coding-system))
277       (nnmail-find-file nnml-active-file))
278     (setq nnml-group-alist (nnmail-get-active))
279     t))
280
281 (deffoo nnml-request-newgroups (date &optional server)
282   (nnml-request-list server))
283
284 (deffoo nnml-request-list-newsgroups (&optional server)
285   (save-excursion
286     (nnmail-find-file nnml-newsgroups-file)))
287
288 (deffoo nnml-request-expire-articles (articles group &optional server force)
289   (nnml-possibly-change-directory group server)
290   (let ((active-articles
291          (nnml-directory-articles nnml-current-directory))
292         (is-old t)
293         article rest mod-time number)
294     (nnmail-activate 'nnml)
295
296     (setq active-articles (sort active-articles '<))
297     ;; Articles not listed in active-articles are already gone,
298     ;; so don't try to expire them.
299     (setq articles (gnus-sorted-intersection articles active-articles))
300
301     (while (and articles is-old)
302       (if (and (setq article (nnml-article-to-file
303                               (setq number (pop articles))))
304                (setq mod-time (nth 5 (file-attributes article)))
305                (nnml-deletable-article-p group number)
306                (setq is-old (nnmail-expired-article-p group mod-time force
307                                                       nnml-inhibit-expiry)))
308           (progn
309             ;; Allow a special target group.
310             (unless (eq nnmail-expiry-target 'delete)
311               (with-temp-buffer
312                 (nnml-request-article number group server (current-buffer))
313                 (let (nnml-current-directory
314                       nnml-current-group
315                       nnml-article-file-alist)
316                   (nnmail-expiry-target-group nnmail-expiry-target group)))
317               ;; Maybe directory is changed during nnmail-expiry-target-group.
318               (nnml-possibly-change-directory group server))
319             (nnheader-message 5 "Deleting article %s in %s"
320                               number group)
321             (condition-case ()
322                 (funcall nnmail-delete-file-function article)
323               (file-error
324                (push number rest)))
325             (setq active-articles (delq number active-articles))
326             (nnml-nov-delete-article group number))
327         (push number rest)))
328     (let ((active (nth 1 (assoc group nnml-group-alist))))
329       (when active
330         (setcar active (or (and active-articles
331                                 (apply 'min active-articles))
332                            (1+ (cdr active)))))
333       (nnmail-save-active nnml-group-alist nnml-active-file))
334     (nnml-save-nov)
335     (nconc rest articles)))
336
337 (deffoo nnml-request-move-article
338     (article group server accept-form &optional last)
339   (let ((buf (get-buffer-create " *nnml move*"))
340         result)
341     (nnml-possibly-change-directory group server)
342     (nnml-update-file-alist)
343     (and
344      (nnml-deletable-article-p group article)
345      (nnml-request-article article group server)
346      (let (nnml-current-directory
347            nnml-current-group
348            nnml-article-file-alist)
349        (save-excursion
350          (set-buffer buf)
351          (insert-buffer-substring nntp-server-buffer)
352          (setq result (eval accept-form))
353          (kill-buffer (current-buffer))
354          result))
355      (progn
356        (nnml-possibly-change-directory group server)
357        (condition-case ()
358            (funcall nnmail-delete-file-function
359                     (nnml-article-to-file  article))
360          (file-error nil))
361        (nnml-nov-delete-article group article)
362        (when last
363          (nnml-save-nov)
364          (nnmail-save-active nnml-group-alist nnml-active-file))))
365     result))
366
367 (deffoo nnml-request-accept-article (group &optional server last)
368   (nnml-possibly-change-directory group server)
369   (nnmail-check-syntax)
370   (let (result)
371     (when nnmail-cache-accepted-message-ids
372       (nnmail-cache-insert (nnmail-fetch-field "message-id") group))
373     (if (stringp group)
374         (and
375          (nnmail-activate 'nnml)
376          (setq result (car (nnml-save-mail
377                             (list (cons group (nnml-active-number group))))))
378          (progn
379            (nnmail-save-active nnml-group-alist nnml-active-file)
380            (and last (nnml-save-nov))))
381       (and
382        (nnmail-activate 'nnml)
383        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
384                 (yes-or-no-p "Moved to `junk' group; delete article? "))
385            (setq result 'junk)
386          (setq result (car (nnml-save-mail result))))
387        (when last
388          (nnmail-save-active nnml-group-alist nnml-active-file)
389          (when nnmail-cache-accepted-message-ids
390            (nnmail-cache-close))
391          (nnml-save-nov))))
392     result))
393
394 (deffoo nnml-request-post (&optional server)
395   (nnmail-do-request-post 'nnml-request-accept-article server))
396
397 (deffoo nnml-request-replace-article (article group buffer)
398   (nnml-possibly-change-directory group)
399   (save-excursion
400     (set-buffer buffer)
401     (nnml-possibly-create-directory group)
402     (let ((chars (nnmail-insert-lines))
403           (art (concat (int-to-string article) "\t"))
404           headers)
405       (when (ignore-errors
406               (nnmail-write-region
407                (point-min) (point-max)
408                (or (nnml-article-to-file article)
409                    (expand-file-name (int-to-string article)
410                                      nnml-current-directory))
411                nil (if (nnheader-be-verbose 5) nil 'nomesg))
412               t)
413         (setq headers (nnml-parse-head chars article))
414         ;; Replace the NOV line in the NOV file.
415         (save-excursion
416           (set-buffer (nnml-open-nov group))
417           (goto-char (point-min))
418           (if (or (looking-at art)
419                   (search-forward (concat "\n" art) nil t))
420               ;; Delete the old NOV line.
421               (delete-region (progn (beginning-of-line) (point))
422                              (progn (forward-line 1) (point)))
423             ;; The line isn't here, so we have to find out where
424             ;; we should insert it.  (This situation should never
425             ;; occur, but one likes to make sure...)
426             (while (and (looking-at "[0-9]+\t")
427                         (< (string-to-int
428                             (buffer-substring
429                              (match-beginning 0) (match-end 0)))
430                            article)
431                         (zerop (forward-line 1)))))
432           (beginning-of-line)
433           (nnheader-insert-nov headers)
434           (nnml-save-nov)
435           t)))))
436
437 (deffoo nnml-request-delete-group (group &optional force server)
438   (nnml-possibly-change-directory group server)
439   (when force
440     ;; Delete all articles in GROUP.
441     (let ((articles
442            (directory-files
443             nnml-current-directory t
444             (concat nnheader-numerical-short-files
445                     "\\|" (regexp-quote nnml-nov-file-name) "$"
446                     "\\|" (regexp-quote nnml-marks-file-name) "$")))
447           article)
448       (while articles
449         (setq article (pop articles))
450         (when (file-writable-p article)
451           (nnheader-message 5 "Deleting article %s in %s..." article group)
452           (funcall nnmail-delete-file-function article))))
453     ;; Try to delete the directory itself.
454     (ignore-errors (delete-directory nnml-current-directory)))
455   ;; Remove the group from all structures.
456   (setq nnml-group-alist
457         (delq (assoc group nnml-group-alist) nnml-group-alist)
458         nnml-current-group nil
459         nnml-current-directory nil)
460   ;; Save the active file.
461   (nnmail-save-active nnml-group-alist nnml-active-file)
462   t)
463
464 (deffoo nnml-request-rename-group (group new-name &optional server)
465   (nnml-possibly-change-directory group server)
466   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
467         (old-dir (nnmail-group-pathname group nnml-directory)))
468     (when (ignore-errors
469             (make-directory new-dir t)
470             t)
471       ;; We move the articles file by file instead of renaming
472       ;; the directory -- there may be subgroups in this group.
473       ;; One might be more clever, I guess.
474       (let ((files (nnheader-article-to-file-alist old-dir)))
475         (while files
476           (rename-file
477            (concat old-dir (cdar files))
478            (concat new-dir (cdar files)))
479           (pop files)))
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" (gnus-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)
623     (setq chars (nnmail-insert-lines))
624     (nnmail-insert-xref group-art)
625     (run-hooks 'nnmail-prepare-save-mail-hook)
626     (run-hooks 'nnml-prepare-save-mail-hook)
627     (goto-char (point-min))
628     (while (looking-at "From ")
629       (replace-match "X-From-Line: ")
630       (forward-line 1))
631     ;; We save the article in all the groups it belongs in.
632     (let ((ga group-art)
633           first)
634       (while ga
635         (nnml-possibly-create-directory (caar ga))
636         (let ((file (concat (nnmail-group-pathname
637                              (caar ga) nnml-directory)
638                             (int-to-string (cdar ga)))))
639           (if first
640               ;; It was already saved, so we just make a hard link.
641               (funcall nnmail-crosspost-link-function first file t)
642             ;; Save the article.
643             (nnmail-write-region (point-min) (point-max) file nil
644                                  (if (nnheader-be-verbose 5) nil 'nomesg))
645             (setq first file)))
646         (setq ga (cdr ga))))
647     ;; Generate a nov line for this article.  We generate the nov
648     ;; line after saving, because nov generation destroys the
649     ;; header.
650     (setq headers (nnml-parse-head chars))
651     ;; Output the nov line to all nov databases that should have it.
652     (let ((ga group-art))
653       (while ga
654         (nnml-add-nov (caar ga) (cdar ga) headers)
655         (setq ga (cdr ga))))
656     group-art))
657
658 (defun nnml-active-number (group)
659   "Compute the next article number in GROUP."
660   (let ((active (cadr (assoc group nnml-group-alist))))
661     ;; The group wasn't known to nnml, so we just create an active
662     ;; entry for it.
663     (unless active
664       ;; Perhaps the active file was corrupt?  See whether
665       ;; there are any articles in this group.
666       (nnml-possibly-create-directory group)
667       (nnml-possibly-change-directory group)
668       (unless nnml-article-file-alist
669         (setq nnml-article-file-alist
670               (sort
671                (nnml-current-group-article-to-file-alist)
672                'car-less-than-car)))
673       (setq active
674             (if nnml-article-file-alist
675                 (cons (caar nnml-article-file-alist)
676                       (caar (last nnml-article-file-alist)))
677               (cons 1 0)))
678       (push (list group active) nnml-group-alist))
679     (setcdr active (1+ (cdr active)))
680     (while (file-exists-p
681             (expand-file-name (int-to-string (cdr active))
682                               (nnmail-group-pathname group nnml-directory)))
683       (setcdr active (1+ (cdr active))))
684     (cdr active)))
685
686 (defun nnml-add-nov (group article headers)
687   "Add a nov line for the GROUP base."
688   (save-excursion
689     (set-buffer (nnml-open-nov group))
690     (goto-char (point-max))
691     (mail-header-set-number headers article)
692     (nnheader-insert-nov headers)))
693
694 (defsubst nnml-header-value ()
695   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
696
697 (defun nnml-parse-head (chars &optional number)
698   "Parse the head of the current buffer."
699   (save-excursion
700     (save-restriction
701       (unless (zerop (buffer-size))
702         (narrow-to-region
703          (goto-char (point-min))
704          (if (search-forward "\n\n" nil t)
705              (1- (point))
706            (if (search-forward "\n\r\n" nil t)
707                (- (point) 2)
708              (point-max)))))
709       (let ((headers (nnheader-parse-naked-head)))
710         (mail-header-set-chars headers chars)
711         (mail-header-set-number headers number)
712         headers))))
713
714 (defun nnml-get-nov-buffer (group)
715   (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
716     (save-excursion
717       (set-buffer buffer)
718       (set (make-local-variable 'nnml-nov-buffer-file-name)
719            (expand-file-name
720             nnml-nov-file-name
721             (nnmail-group-pathname group nnml-directory)))
722       (erase-buffer)
723       (when (file-exists-p nnml-nov-buffer-file-name)
724         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
725     buffer))
726
727 (defun nnml-open-nov (group)
728   (or (cdr (assoc group nnml-nov-buffer-alist))
729       (let ((buffer (nnml-get-nov-buffer group)))
730         (push (cons group buffer) nnml-nov-buffer-alist)
731         buffer)))
732
733 (defun nnml-save-nov ()
734   (save-excursion
735     (while nnml-nov-buffer-alist
736       (when (buffer-name (cdar nnml-nov-buffer-alist))
737         (set-buffer (cdar nnml-nov-buffer-alist))
738         (when (buffer-modified-p)
739           (nnmail-write-region (point-min) (point-max)
740                                nnml-nov-buffer-file-name nil 'nomesg))
741         (set-buffer-modified-p nil)
742         (kill-buffer (current-buffer)))
743       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
744
745 ;;;###autoload
746 (defun nnml-generate-nov-databases (&optional server)
747   "Generate NOV databases in all nnml directories."
748   (interactive (list (or (nnoo-current-server 'nnml) "")))
749   ;; Read the active file to make sure we don't re-use articles
750   ;; numbers in empty groups.
751   (nnmail-activate 'nnml)
752   (unless (nnml-server-opened server)
753     (nnml-open-server server))
754   (setq nnml-directory (expand-file-name nnml-directory))
755   ;; Recurse down the directories.
756   (nnml-generate-nov-databases-1 nnml-directory nil t)
757   ;; Save the active file.
758   (nnmail-save-active nnml-group-alist nnml-active-file))
759
760 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
761   "Regenerate the NOV database in DIR."
762   (interactive "DRegenerate NOV in: ")
763   (setq dir (file-name-as-directory dir))
764   ;; Only scan this sub-tree if we haven't been here yet.
765   (unless (member (file-truename dir) seen)
766     (push (file-truename dir) seen)
767     ;; We descend recursively
768     (let ((dirs (directory-files dir t nil t))
769           dir)
770       (while (setq dir (pop dirs))
771         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
772                    (file-directory-p dir))
773           (nnml-generate-nov-databases-1 dir seen))))
774     ;; Do this directory.
775     (let ((files (sort (nnheader-article-to-file-alist dir)
776                        'car-less-than-car)))
777       (if (not files)
778           (let* ((group (nnheader-file-to-group
779                          (directory-file-name dir) nnml-directory))
780                  (info (cadr (assoc group nnml-group-alist))))
781             (when info
782               (setcar info (1+ (cdr info)))))
783         (funcall nnml-generate-active-function dir)
784         ;; Generate the nov file.
785         (nnml-generate-nov-file dir files)
786         (unless no-active
787           (nnmail-save-active nnml-group-alist nnml-active-file))))))
788
789 (eval-when-compile (defvar files))
790 (defun nnml-generate-active-info (dir)
791   ;; Update the active info for this group.
792   (let* ((group (nnheader-file-to-group
793                  (directory-file-name dir) nnml-directory))
794          (entry (assoc group nnml-group-alist))
795          (last (or (caadr entry) 0)))
796     (setq nnml-group-alist (delq entry nnml-group-alist))
797     (push (list group
798                 (cons (or (caar files) (1+ last))
799                       (max last
800                            (or (let ((f files))
801                                  (while (cdr f) (setq f (cdr f)))
802                                  (caar f))
803                                0))))
804           nnml-group-alist)))
805
806 (defun nnml-generate-nov-file (dir files)
807   (let* ((dir (file-name-as-directory dir))
808          (nov (concat dir nnml-nov-file-name))
809          (nov-buffer (get-buffer-create " *nov*"))
810          chars file headers)
811     (save-excursion
812       ;; Init the nov buffer.
813       (set-buffer nov-buffer)
814       (buffer-disable-undo)
815       (erase-buffer)
816       (set-buffer nntp-server-buffer)
817       ;; Delete the old NOV file.
818       (when (file-exists-p nov)
819         (funcall nnmail-delete-file-function nov))
820       (while files
821         (unless (file-directory-p (setq file (concat dir (cdar files))))
822           (erase-buffer)
823           (nnheader-insert-file-contents file)
824           (narrow-to-region
825            (goto-char (point-min))
826            (progn
827              (re-search-forward "\n\r?\n" nil t)
828              (setq chars (- (point-max) (point)))
829              (max (point-min) (1- (point)))))
830           (unless (zerop (buffer-size))
831             (goto-char (point-min))
832             (setq headers (nnml-parse-head chars (caar files)))
833             (save-excursion
834               (set-buffer nov-buffer)
835               (goto-char (point-max))
836               (nnheader-insert-nov headers)))
837           (widen))
838         (setq files (cdr files)))
839       (save-excursion
840         (set-buffer nov-buffer)
841         (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
842         (kill-buffer (current-buffer))))))
843
844 (defun nnml-nov-delete-article (group article)
845   (save-excursion
846     (set-buffer (nnml-open-nov group))
847     (when (nnheader-find-nov-line article)
848       (delete-region (point) (progn (forward-line 1) (point)))
849       (when (bobp)
850         (let ((active (cadr (assoc group nnml-group-alist)))
851               num)
852           (when active
853             (if (eobp)
854                 (setf (car active) (1+ (cdr active)))
855               (when (and (setq num (ignore-errors (read (current-buffer))))
856                          (numberp num))
857                 (setf (car active) num)))))))
858     t))
859
860 (defun nnml-update-file-alist (&optional force)
861   (when nnml-use-compressed-files
862     (when (or (not nnml-article-file-alist)
863               force)
864       (setq nnml-article-file-alist
865             (nnml-current-group-article-to-file-alist)))))
866
867 (defun nnml-directory-articles (dir)
868   "Return a list of all article files in a directory.
869 Use the nov database for that directory if available."
870   (if (or gnus-nov-is-evil nnml-nov-is-evil
871           (not (file-exists-p
872                 (expand-file-name nnml-nov-file-name dir))))
873       (nnheader-directory-articles dir)
874     ;; build list from .overview if available
875     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
876     ;; defvoo'd, and we might get called when it hasn't been swapped in.
877     (save-excursion
878       (let ((list nil)
879             art
880             (buffer (nnml-get-nov-buffer nnml-current-group)))
881         (set-buffer buffer)
882         (goto-char (point-min))
883         (while (not (eobp))
884           (setq art (read (current-buffer)))
885           (push art list)
886           (forward-line 1))
887         list))))
888
889 (defun nnml-current-group-article-to-file-alist ()
890   "Return an alist of article/file pairs in the current group.
891 Use the nov database for the current group if available."
892   (if (or 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)) t
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     (mapcar (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)" 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