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