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