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