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