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