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