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