*** 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-1)
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
268                                                &optional server force)
269   (nnml-possibly-change-directory group server)
270   (let ((active-articles
271          (nnheader-directory-articles nnml-current-directory))
272         (is-old t)
273         article rest mod-time number)
274     (nnmail-activate 'nnml)
275
276     (setq active-articles (sort active-articles '<))
277     ;; Articles not listed in active-articles are already gone,
278     ;; so don't try to expire them.
279     (setq articles (gnus-sorted-intersection articles active-articles))
280
281     (while (and articles is-old)
282       (when (setq article (nnml-article-to-file (setq number (pop articles))))
283         (when (setq mod-time (nth 5 (file-attributes article)))
284           (if (and (nnml-deletable-article-p group number)
285                    (setq is-old
286                          (nnmail-expired-article-p group mod-time force
287                                                    nnml-inhibit-expiry)))
288               (progn
289                 (nnheader-message 5 "Deleting article %s in %s"
290                                   article group)
291                 (condition-case ()
292                     (funcall nnmail-delete-file-function article)
293                   (file-error
294                    (push number rest)))
295                 (setq active-articles (delq number active-articles))
296                 (nnml-nov-delete-article group number))
297             (push number rest)))))
298     (let ((active (nth 1 (assoc group nnml-group-alist))))
299       (when active
300         (setcar active (or (and active-articles
301                                 (apply 'min active-articles))
302                            (1+ (cdr active)))))
303       (nnmail-save-active nnml-group-alist nnml-active-file))
304     (nnml-save-nov)
305     (nconc rest articles)))
306
307 (deffoo nnml-request-move-article
308   (article group server accept-form &optional last)
309   (let ((buf (get-buffer-create " *nnml move*"))
310         result)
311     (nnml-possibly-change-directory group server)
312     (nnml-update-file-alist)
313     (and
314      (nnml-deletable-article-p group article)
315      (nnml-request-article article group server)
316      (save-excursion
317        (set-buffer buf)
318        (insert-buffer-substring nntp-server-buffer)
319        (setq result (eval accept-form))
320        (kill-buffer (current-buffer))
321        result)
322      (progn
323        (nnml-possibly-change-directory group server)
324        (condition-case ()
325            (funcall nnmail-delete-file-function
326                     (nnml-article-to-file  article))
327          (file-error nil))
328        (nnml-nov-delete-article group article)
329        (when last
330          (nnml-save-nov)
331          (nnmail-save-active nnml-group-alist nnml-active-file))))
332     result))
333
334 (deffoo nnml-request-accept-article (group &optional server last)
335   (nnml-possibly-change-directory group server)
336   (nnmail-check-syntax)
337   (let (result)
338     (when nnmail-cache-accepted-message-ids
339       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
340     (if (stringp group)
341         (and
342          (nnmail-activate 'nnml)
343          (setq result (car (nnml-save-mail
344                             (list (cons group (nnml-active-number group))))))
345          (progn
346            (nnmail-save-active nnml-group-alist nnml-active-file)
347            (and last (nnml-save-nov))))
348       (and
349        (nnmail-activate 'nnml)
350        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
351                 (yes-or-no-p "Moved to `junk' group; delete article? "))
352            (setq result 'junk)
353          (setq result (car (nnml-save-mail result))))
354        (when last
355          (nnmail-save-active nnml-group-alist nnml-active-file)
356          (when nnmail-cache-accepted-message-ids
357            (nnmail-cache-close))
358          (nnml-save-nov))))
359     result))
360
361 (deffoo nnml-request-replace-article (article group buffer)
362   (nnml-possibly-change-directory group)
363   (save-excursion
364     (set-buffer buffer)
365     (nnml-possibly-create-directory group)
366     (let ((chars (nnmail-insert-lines))
367           (art (concat (int-to-string article) "\t"))
368           headers)
369       (when (ignore-errors
370               (nnmail-write-region
371                (point-min) (point-max)
372                (or (nnml-article-to-file article)
373                    (concat nnml-current-directory
374                            (int-to-string article)))
375                nil (if (nnheader-be-verbose 5) nil 'nomesg))
376               t)
377         (setq headers (nnml-parse-head chars article))
378         ;; Replace the NOV line in the NOV file.
379         (save-excursion
380           (set-buffer (nnml-open-nov group))
381           (goto-char (point-min))
382           (if (or (looking-at art)
383                   (search-forward (concat "\n" art) nil t))
384               ;; Delete the old NOV line.
385               (delete-region (progn (beginning-of-line) (point))
386                              (progn (forward-line 1) (point)))
387             ;; The line isn't here, so we have to find out where
388             ;; we should insert it.  (This situation should never
389             ;; occur, but one likes to make sure...)
390             (while (and (looking-at "[0-9]+\t")
391                         (< (string-to-int
392                             (buffer-substring
393                              (match-beginning 0) (match-end 0)))
394                            article)
395                         (zerop (forward-line 1)))))
396           (beginning-of-line)
397           (nnheader-insert-nov headers)
398           (nnml-save-nov)
399           t)))))
400
401 (deffoo nnml-request-delete-group (group &optional force server)
402   (nnml-possibly-change-directory group server)
403   (when force
404     ;; Delete all articles in GROUP.
405     (let ((articles
406            (directory-files
407             nnml-current-directory t
408             (concat nnheader-numerical-short-files
409                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
410           article)
411       (while articles
412         (setq article (pop articles))
413         (when (file-writable-p article)
414           (nnheader-message 5 "Deleting article %s in %s..." article group)
415           (funcall nnmail-delete-file-function article))))
416     ;; Try to delete the directory itself.
417     (ignore-errors (delete-directory nnml-current-directory)))
418   ;; Remove the group from all structures.
419   (setq nnml-group-alist
420         (delq (assoc group nnml-group-alist) nnml-group-alist)
421         nnml-current-group nil
422         nnml-current-directory nil)
423   ;; Save the active file.
424   (nnmail-save-active nnml-group-alist nnml-active-file)
425   t)
426
427 (deffoo nnml-request-rename-group (group new-name &optional server)
428   (nnml-possibly-change-directory group server)
429   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
430         (old-dir (nnmail-group-pathname group nnml-directory)))
431     (when (ignore-errors
432             (make-directory new-dir t)
433             t)
434       ;; We move the articles file by file instead of renaming
435       ;; the directory -- there may be subgroups in this group.
436       ;; One might be more clever, I guess.
437       (let ((files (nnheader-article-to-file-alist old-dir)))
438         (while files
439           (rename-file
440            (concat old-dir (cdar files))
441            (concat new-dir (cdar files)))
442           (pop files)))
443       ;; Move .overview file.
444       (let ((overview (concat old-dir nnml-nov-file-name)))
445         (when (file-exists-p overview)
446           (rename-file overview (concat new-dir nnml-nov-file-name))))
447       (when (<= (length (directory-files old-dir)) 2)
448         (ignore-errors (delete-directory old-dir)))
449       ;; That went ok, so we change the internal structures.
450       (let ((entry (assoc group nnml-group-alist)))
451         (when entry
452           (setcar entry new-name))
453         (setq nnml-current-directory nil
454               nnml-current-group nil)
455         ;; Save the new group alist.
456         (nnmail-save-active nnml-group-alist nnml-active-file)
457         t))))
458
459 (deffoo nnml-set-status (article name value &optional group server)
460   (nnml-possibly-change-directory group server)
461   (let ((file (nnml-article-to-file article)))
462     (cond
463      ((not (file-exists-p file))
464       (nnheader-report 'nnml "File %s does not exist" file))
465      (t
466       (with-temp-file file
467         (nnheader-insert-file-contents file)
468         (nnmail-replace-status name value))
469       t))))
470
471 \f
472 ;;; Internal functions.
473
474 (defun nnml-article-to-file (article)
475   (nnml-update-file-alist)
476   (let (file)
477     (if (setq file (cdr (assq article nnml-article-file-alist)))
478         (concat nnml-current-directory file)
479       ;; Just to make sure nothing went wrong when reading over NFS --
480       ;; check once more.
481       (when (file-exists-p
482              (setq file (expand-file-name (number-to-string article)
483                                           nnml-current-directory)))
484         (nnml-update-file-alist t)
485         file))))
486
487 (defun nnml-deletable-article-p (group article)
488   "Say whether ARTICLE in GROUP can be deleted."
489   (let (path)
490     (when (setq path (nnml-article-to-file article))
491       (when (file-writable-p path)
492         (or (not nnmail-keep-last-article)
493             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
494                      article)))))))
495
496 ;; Find an article number in the current group given the Message-ID.
497 (defun nnml-find-group-number (id)
498   (save-excursion
499     (set-buffer (get-buffer-create " *nnml id*"))
500     (let ((alist nnml-group-alist)
501           number)
502       ;; We want to look through all .overview files, but we want to
503       ;; start with the one in the current directory.  It seems most
504       ;; likely that the article we are looking for is in that group.
505       (if (setq number (nnml-find-id nnml-current-group id))
506           (cons nnml-current-group number)
507         ;; It wasn't there, so we look through the other groups as well.
508         (while (and (not number)
509                     alist)
510           (or (string= (caar alist) nnml-current-group)
511               (setq number (nnml-find-id (caar alist) id)))
512           (or number
513               (setq alist (cdr alist))))
514         (and number
515              (cons (caar alist) number))))))
516
517 (defun nnml-find-id (group id)
518   (erase-buffer)
519   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
520                      nnml-nov-file-name))
521         number found)
522     (when (file-exists-p nov)
523       (nnheader-insert-file-contents nov)
524       (while (and (not found)
525                   (search-forward id nil t)) ; We find the ID.
526         ;; And the id is in the fourth field.
527         (if (not (and (search-backward "\t" nil t 4)
528                       (not (search-backward"\t" (gnus-point-at-bol) t))))
529             (forward-line 1)
530           (beginning-of-line)
531           (setq found t)
532           ;; We return the article number.
533           (setq number
534                 (ignore-errors (read (current-buffer))))))
535       number)))
536
537 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
538   (if (or gnus-nov-is-evil nnml-nov-is-evil)
539       nil
540     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
541       (when (file-exists-p nov)
542         (save-excursion
543           (set-buffer nntp-server-buffer)
544           (erase-buffer)
545           (nnheader-insert-file-contents nov)
546           (if (and fetch-old
547                    (not (numberp fetch-old)))
548               t                         ; Don't remove anything.
549             (nnheader-nov-delete-outside-range
550              (if fetch-old (max 1 (- (car articles) fetch-old))
551                (car articles))
552              (car (last articles)))
553             t))))))
554
555 (defun nnml-possibly-change-directory (group &optional server)
556   (when (and server
557              (not (nnml-server-opened server)))
558     (nnml-open-server server))
559   (if (not group)
560       t
561     (let ((pathname (nnmail-group-pathname group nnml-directory))
562           (pathname-coding-system 'binary))
563       (when (not (equal pathname nnml-current-directory))
564         (setq nnml-current-directory pathname
565               nnml-current-group group
566               nnml-article-file-alist nil))
567       (file-exists-p nnml-current-directory))))
568
569 (defun nnml-possibly-create-directory (group)
570   (let ((dir (nnmail-group-pathname group nnml-directory)))
571     (unless (file-exists-p dir)
572       (make-directory (directory-file-name dir) t)
573       (nnheader-message 5 "Creating mail directory %s" dir))))
574
575 (defun nnml-save-mail (group-art)
576   "Called narrowed to an article."
577   (let (chars headers)
578     (setq chars (nnmail-insert-lines))
579     (nnmail-insert-xref group-art)
580     (run-hooks 'nnmail-prepare-save-mail-hook)
581     (run-hooks 'nnml-prepare-save-mail-hook)
582     (goto-char (point-min))
583     (while (looking-at "From ")
584       (replace-match "X-From-Line: ")
585       (forward-line 1))
586     ;; We save the article in all the groups it belongs in.
587     (let ((ga group-art)
588           first)
589       (while ga
590         (nnml-possibly-create-directory (caar ga))
591         (let ((file (concat (nnmail-group-pathname
592                              (caar ga) nnml-directory)
593                             (int-to-string (cdar ga)))))
594           (if first
595               ;; It was already saved, so we just make a hard link.
596               (funcall nnmail-crosspost-link-function first file t)
597             ;; Save the article.
598             (nnmail-write-region (point-min) (point-max) file nil
599                                  (if (nnheader-be-verbose 5) nil 'nomesg))
600             (setq first file)))
601         (setq ga (cdr ga))))
602     ;; Generate a nov line for this article.  We generate the nov
603     ;; line after saving, because nov generation destroys the
604     ;; header.
605     (setq headers (nnml-parse-head chars))
606     ;; Output the nov line to all nov databases that should have it.
607     (let ((ga group-art))
608       (while ga
609         (nnml-add-nov (caar ga) (cdar ga) headers)
610         (setq ga (cdr ga))))
611     group-art))
612
613 (defun nnml-active-number (group)
614   "Compute the next article number in GROUP."
615   (let ((active (cadr (assoc group nnml-group-alist))))
616     ;; The group wasn't known to nnml, so we just create an active
617     ;; entry for it.
618     (unless active
619       ;; Perhaps the active file was corrupt?  See whether
620       ;; there are any articles in this group.
621       (nnml-possibly-create-directory group)
622       (nnml-possibly-change-directory group)
623       (unless nnml-article-file-alist
624         (setq nnml-article-file-alist
625               (sort
626                (nnheader-article-to-file-alist nnml-current-directory)
627                'car-less-than-car)))
628       (setq active
629             (if nnml-article-file-alist
630                 (cons (caar nnml-article-file-alist)
631                       (caar (last nnml-article-file-alist)))
632               (cons 1 0)))
633       (push (list group active) nnml-group-alist))
634     (setcdr active (1+ (cdr active)))
635     (while (file-exists-p
636             (concat (nnmail-group-pathname group nnml-directory)
637                     (int-to-string (cdr active))))
638       (setcdr active (1+ (cdr active))))
639     (cdr active)))
640
641 (defun nnml-add-nov (group article headers)
642   "Add a nov line for the GROUP base."
643   (save-excursion
644     (set-buffer (nnml-open-nov group))
645     (goto-char (point-max))
646     (mail-header-set-number headers article)
647     (nnheader-insert-nov headers)))
648
649 (defsubst nnml-header-value ()
650   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
651
652 (defun nnml-parse-head (chars &optional number)
653   "Parse the head of the current buffer."
654   (save-excursion
655     (save-restriction
656       (unless (zerop (buffer-size))
657         (narrow-to-region
658          (goto-char (point-min))
659          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
660       ;; Fold continuation lines.
661       (goto-char (point-min))
662       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
663         (replace-match " " t t))
664       ;; Remove any tabs; they are too confusing.
665       (subst-char-in-region (point-min) (point-max) ?\t ? )
666       (let ((headers (nnheader-parse-head t)))
667         (mail-header-set-chars headers chars)
668         (mail-header-set-number headers number)
669         headers))))
670
671 (defun nnml-open-nov (group)
672   (or (cdr (assoc group nnml-nov-buffer-alist))
673       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
674         (save-excursion
675           (set-buffer buffer)
676           (set (make-local-variable 'nnml-nov-buffer-file-name)
677                (concat (nnmail-group-pathname group nnml-directory)
678                        nnml-nov-file-name))
679           (erase-buffer)
680           (when (file-exists-p nnml-nov-buffer-file-name)
681             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
682         (push (cons group buffer) nnml-nov-buffer-alist)
683         buffer)))
684
685 (defun nnml-save-nov ()
686   (save-excursion
687     (while nnml-nov-buffer-alist
688       (when (buffer-name (cdar nnml-nov-buffer-alist))
689         (set-buffer (cdar nnml-nov-buffer-alist))
690         (when (buffer-modified-p)
691           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
692                                nil 'nomesg))
693         (set-buffer-modified-p nil)
694         (kill-buffer (current-buffer)))
695       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
696
697 ;;;###autoload
698 (defun nnml-generate-nov-databases ()
699   "Generate NOV databases in all nnml directories."
700   (interactive)
701   ;; Read the active file to make sure we don't re-use articles
702   ;; numbers in empty groups.
703   (nnmail-activate 'nnml)
704   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
705   (setq nnml-directory (expand-file-name nnml-directory))
706   ;; Recurse down the directories.
707   (nnml-generate-nov-databases-1 nnml-directory nil t)
708   ;; Save the active file.
709   (nnmail-save-active nnml-group-alist nnml-active-file))
710
711 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
712   "Regenerate the NOV database in DIR."
713   (interactive "DRegenerate NOV in: ")
714   (setq dir (file-name-as-directory dir))
715   ;; Only scan this sub-tree if we haven't been here yet.
716   (unless (member (file-truename dir) seen)
717     (push (file-truename dir) seen)
718     ;; We descend recursively
719     (let ((dirs (directory-files dir t nil t))
720           dir)
721       (while (setq dir (pop dirs))
722         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
723                    (file-directory-p dir))
724           (nnml-generate-nov-databases-1 dir seen))))
725     ;; Do this directory.
726     (let ((files (sort (nnheader-article-to-file-alist dir)
727                        'car-less-than-car)))
728       (if (not files)
729           (let* ((group (nnheader-file-to-group
730                          (directory-file-name dir) nnml-directory))
731                  (info (cadr (assoc group nnml-group-alist))))
732             (when info
733               (setcar info (1+ (cdr info)))))
734         (funcall nnml-generate-active-function dir)
735         ;; Generate the nov file.
736         (nnml-generate-nov-file dir files)
737         (unless no-active
738           (nnmail-save-active nnml-group-alist nnml-active-file))))))
739
740 (defvar files)
741 (defun nnml-generate-active-info (dir)
742   ;; Update the active info for this group.
743   (let ((group (nnheader-file-to-group
744                 (directory-file-name dir) nnml-directory)))
745     (setq nnml-group-alist
746           (delq (assoc group nnml-group-alist) nnml-group-alist))
747     (push (list group
748                 (cons (caar files)
749                       (let ((f files))
750                         (while (cdr f) (setq f (cdr f)))
751                         (caar f))))
752           nnml-group-alist)))
753
754 (defun nnml-generate-nov-file (dir files)
755   (let* ((dir (file-name-as-directory dir))
756          (nov (concat dir nnml-nov-file-name))
757          (nov-buffer (get-buffer-create " *nov*"))
758          chars file headers)
759     (save-excursion
760       ;; Init the nov buffer.
761       (set-buffer nov-buffer)
762       (buffer-disable-undo)
763       (erase-buffer)
764       (set-buffer nntp-server-buffer)
765       ;; Delete the old NOV file.
766       (when (file-exists-p nov)
767         (funcall nnmail-delete-file-function nov))
768       (while files
769         (unless (file-directory-p (setq file (concat dir (cdar files))))
770           (erase-buffer)
771           (nnheader-insert-file-contents file)
772           (narrow-to-region
773            (goto-char (point-min))
774            (progn
775              (search-forward "\n\n" nil t)
776              (setq chars (- (point-max) (point)))
777              (max 1 (1- (point)))))
778           (unless (zerop (buffer-size))
779             (goto-char (point-min))
780             (setq headers (nnml-parse-head chars (caar files)))
781             (save-excursion
782               (set-buffer nov-buffer)
783               (goto-char (point-max))
784               (nnheader-insert-nov headers)))
785           (widen))
786         (setq files (cdr files)))
787       (save-excursion
788         (set-buffer nov-buffer)
789         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
790         (kill-buffer (current-buffer))))))
791
792 (defun nnml-nov-delete-article (group article)
793   (save-excursion
794     (set-buffer (nnml-open-nov group))
795     (when (nnheader-find-nov-line article)
796       (delete-region (point) (progn (forward-line 1) (point)))
797       (when (bobp)
798         (let ((active (cadr (assoc group nnml-group-alist)))
799               num)
800           (when active
801             (if (eobp)
802                 (setf (car active) (1+ (cdr active)))
803               (when (and (setq num (ignore-errors (read (current-buffer))))
804                          (numberp num))
805                 (setf (car active) num)))))))
806     t))
807
808 (defun nnml-update-file-alist (&optional force)
809   (when (or (not nnml-article-file-alist)
810             force)
811     (setq nnml-article-file-alist
812           (nnheader-article-to-file-alist nnml-current-directory))))
813
814 (provide 'nnml)
815
816 ;;; nnml.el ends here