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