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