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