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