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