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