*** 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-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          (is-old t)
282          article rest mod-time number)
283     (nnmail-activate 'nnml)
284
285     (unless nnml-article-file-alist
286       (setq nnml-article-file-alist
287             (nnheader-article-to-file-alist nnml-current-directory)))
288
289     (while (and articles is-old)
290       (setq article (concat nnml-current-directory 
291                             (int-to-string 
292                              (setq number (pop articles)))))
293       (when (setq mod-time (nth 5 (file-attributes article)))
294         (if (and (nnml-deletable-article-p newsgroup number)
295                  (setq is-old 
296                        (nnmail-expired-article-p newsgroup mod-time force)))
297             (progn
298               (nnheader-message 5 "Deleting article %s in %s..."
299                                 article newsgroup)
300               (condition-case ()
301                   (funcall nnmail-delete-file-function article)
302                 (file-error
303                  (push number rest)))
304               (setq active-articles (delq number active-articles))
305               (nnml-nov-delete-article newsgroup number))
306           (push number rest))))
307     (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
308       (when active
309         (setcar active (or (and active-articles
310                                 (apply 'min active-articles))
311                            (1+ (cdr active)))))
312       (nnmail-save-active nnml-group-alist nnml-active-file))
313     (nnml-save-nov)
314     (message "")
315     (nconc rest articles)))
316
317 (defun nnml-request-move-article 
318   (article group server accept-form &optional last)
319   (let ((buf (get-buffer-create " *nnml move*"))
320         result)
321     (nnml-possibly-change-directory group)
322     (unless nnml-article-file-alist
323       (setq nnml-article-file-alist
324             (nnheader-article-to-file-alist nnml-current-directory)))
325     (and 
326      (nnml-deletable-article-p group article)
327      (nnml-request-article article group server)
328      (save-excursion
329        (set-buffer buf)
330        (insert-buffer-substring nntp-server-buffer)
331        (setq result (eval accept-form))
332        (kill-buffer (current-buffer))
333        result)
334      (progn
335        (condition-case ()
336            (funcall nnmail-delete-file-function
337                     (concat nnml-current-directory 
338                             (int-to-string article)))
339          (file-error nil))
340        (nnml-nov-delete-article group article)
341        (and last (nnml-save-nov))))
342     result))
343
344 (defun nnml-request-accept-article (group &optional last)
345   (let (result)
346     (if (stringp group)
347         (and 
348          (nnmail-activate 'nnml)
349          ;; We trick the choosing function into believing that only one
350          ;; group is available.  
351          (let ((nnmail-split-methods (list (list group ""))))
352            (setq result (car (nnml-save-mail))))
353          (progn
354            (nnmail-save-active nnml-group-alist nnml-active-file)
355            (and last (nnml-save-nov))))
356       (and
357        (nnmail-activate 'nnml)
358        (setq result (car (nnml-save-mail)))
359        (progn
360          (nnmail-save-active nnml-group-alist nnml-active-file)
361          (and last (nnml-save-nov)))))
362     result))
363
364 (defun nnml-request-replace-article (article group buffer)
365   (nnml-possibly-change-directory group)
366   (save-excursion
367     (set-buffer buffer)
368     (nnml-possibly-create-directory group)
369     (if (not (condition-case ()
370                  (progn
371                    (write-region (point-min) (point-max)
372                                  (concat nnml-current-directory 
373                                          (int-to-string article))
374                                  nil (if (nnheader-be-verbose 5) nil 'nomesg))
375                    t)
376                (error nil)))
377         ()
378       (let ((chars (nnmail-insert-lines))
379             (art (concat (int-to-string article) "\t"))
380             nov-line)
381         (setq nov-line (nnml-make-nov-line chars))
382         ;; Replace the NOV line in the NOV file.
383         (save-excursion 
384           (set-buffer (nnml-open-nov group))
385           (goto-char (point-min))
386           (if (or (looking-at art)
387                   (search-forward (concat "\n" art) nil t))
388               ;; Delete the old NOV line.
389               (delete-region (progn (beginning-of-line) (point))
390                              (progn (forward-line 1) (point)))
391             ;; The line isn't here, so we have to find out where
392             ;; we should insert it. (This situation should never
393             ;; occur, but one likes to make sure...)
394             (while (and (looking-at "[0-9]+\t")
395                         (< (string-to-int 
396                             (buffer-substring 
397                              (match-beginning 0) (match-end 0)))
398                            article)
399                         (zerop (forward-line 1)))))
400           (beginning-of-line)
401           (insert (int-to-string article) nov-line)
402           (nnml-save-nov)
403           t)))))
404
405 (defun nnml-request-delete-group (group &optional force server)
406   (nnml-possibly-change-directory group)
407   (when force
408     ;; Delete all articles in GROUP.
409     (let ((articles 
410            (directory-files 
411             nnml-current-directory t
412             (concat nnheader-numerical-short-files
413                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
414           article)
415       (while articles 
416         (setq article (pop articles))
417         (when (file-writable-p article)
418           (nnheader-message 5 "Deleting article %s in %s..." article group)
419           (funcall nnmail-delete-file-function article))))
420     ;; Try to delete the directory itself.
421     (condition-case ()
422         (delete-directory nnml-current-directory)
423       (error nil)))
424   ;; Remove the group from all structures.
425   (setq nnml-group-alist 
426         (delq (assoc group nnml-group-alist) nnml-group-alist)
427         nnml-current-group nil
428         nnml-current-directory nil)
429   ;; Save the active file.
430   (nnmail-save-active nnml-group-alist nnml-active-file)
431   t)
432
433 (defun nnml-request-rename-group (group new-name &optional server)
434   (nnml-possibly-change-directory group)
435   ;; Rename directory.
436   (and (file-writable-p nnml-current-directory)
437        (condition-case ()
438            (let ((parent 
439                   (file-name-directory
440                    (directory-file-name 
441                     (nnmail-group-pathname new-name nnml-directory)))))
442              (unless (file-exists-p parent)
443                (make-directory parent t))
444              (rename-file 
445               (directory-file-name nnml-current-directory)
446               (directory-file-name 
447                (nnmail-group-pathname new-name nnml-directory)))
448              t)
449          (error nil))
450        ;; That went ok, so we change the internal structures.
451        (let ((entry (assoc group nnml-group-alist)))
452          (and entry (setcar entry new-name))
453          (setq nnml-current-directory nil
454                nnml-current-group nil)
455          ;; Save the new group alist.
456          (nnmail-save-active nnml-group-alist nnml-active-file)
457          t)))
458
459 \f
460 ;;; Internal functions.
461
462 (defun nnml-deletable-article-p (group article)
463   "Say whether ARTICLE in GROUP can be deleted."
464   (let (file path)
465     (when (setq file (cdr (assq article nnml-article-file-alist)))
466       (setq path (concat nnml-current-directory file))
467       (and (file-writable-p path)
468            (or (not nnmail-keep-last-article)
469                (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) 
470                         article)))))))
471
472 ;; Find an article number in the current group given the Message-ID. 
473 (defun nnml-find-group-number (id)
474   (save-excursion
475     (set-buffer (get-buffer-create " *nnml id*"))
476     (buffer-disable-undo (current-buffer))
477     (let ((alist nnml-group-alist)
478           number)
479       ;; We want to look through all .overview files, but we want to
480       ;; start with the one in the current directory.  It seems most
481       ;; likely that the article we are looking for is in that group. 
482       (if (setq number (nnml-find-id nnml-current-group id))
483           (cons nnml-current-group number)
484         ;; It wasn't there, so we look through the other groups as well.
485         (while (and (not number)
486                     alist)
487           (or (string= (caar alist) nnml-current-group)
488               (setq number (nnml-find-id (caar alist) id)))
489           (or number
490               (setq alist (cdr alist))))
491         (and number
492              (cons (caar alist) number))))))
493
494 (defun nnml-find-id (group id)
495   (erase-buffer)
496   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
497                      nnml-nov-file-name))
498         number found)
499     (when (file-exists-p nov)
500       (insert-file-contents nov)
501       (while (and (not found) 
502                   (search-forward id nil t)) ; We find the ID.
503         ;; And the id is in the fourth field.
504         (if (search-backward 
505              "\t" (save-excursion (beginning-of-line) (point)) t 4)
506             (progn
507               (beginning-of-line)
508               (setq found t)
509               ;; We return the article number.
510               (setq number
511                     (condition-case ()
512                         (read (current-buffer))
513                       (error nil))))))
514       number)))
515
516 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
517   (if (or gnus-nov-is-evil nnml-nov-is-evil)
518       nil
519     (let ((first (car articles))
520           (last (progn (while (cdr articles) (setq articles (cdr articles)))
521                        (car articles)))
522           (nov (concat nnml-current-directory nnml-nov-file-name)))
523       (if (file-exists-p nov)
524           (save-excursion
525             (set-buffer nntp-server-buffer)
526             (erase-buffer)
527             (insert-file-contents nov)
528             (if (and fetch-old
529                      (not (numberp fetch-old)))
530                 t                       ; Don't remove anything.
531               (if fetch-old
532                   (setq first (max 1 (- first fetch-old))))
533               (goto-char (point-min))
534               (while (and (not (eobp)) (< first (read (current-buffer))))
535                 (forward-line 1))
536               (beginning-of-line)
537               (if (not (eobp)) (delete-region 1 (point)))
538               (while (and (not (eobp)) (>= last (read (current-buffer))))
539                 (forward-line 1))
540               (beginning-of-line)
541               (if (not (eobp)) (delete-region (point) (point-max)))
542               t))))))
543
544 (defun nnml-possibly-change-directory (group &optional force)
545   (when group
546     (let ((pathname (nnmail-group-pathname group nnml-directory)))
547       (when (or force
548                 (not (equal pathname nnml-current-directory)))
549         (setq nnml-current-directory pathname
550               nnml-current-group group
551               nnml-article-file-alist nil))))
552   t)
553
554 (defun nnml-possibly-create-directory (group)
555   (let (dir dirs)
556     (setq dir (nnmail-group-pathname group nnml-directory))
557     (while (not (file-directory-p dir))
558       (setq dirs (cons dir dirs))
559       (setq dir (file-name-directory (directory-file-name dir))))
560     (while dirs
561       (make-directory (directory-file-name (car dirs)))
562       (nnheader-message 5 "Creating mail directory %s" (car dirs))
563       (setq dirs (cdr dirs)))))
564              
565 (defun nnml-save-mail ()
566   "Called narrowed to an article."
567   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
568         chars nov-line)
569     (setq chars (nnmail-insert-lines))
570     (nnmail-insert-xref group-art)
571     (run-hooks 'nnml-prepare-save-mail-hook)
572     (goto-char (point-min))
573     (while (looking-at "From ")
574       (replace-match "X-From-Line: ")
575       (forward-line 1))
576     ;; We save the article in all the newsgroups it belongs in.
577     (let ((ga group-art)
578           first)
579       (while ga
580         (nnml-possibly-create-directory (caar ga))
581         (let ((file (concat (nnmail-group-pathname 
582                              (caar ga) nnml-directory)
583                             (int-to-string (cdar ga)))))
584           (if first
585               ;; It was already saved, so we just make a hard link.
586               (funcall nnmail-crosspost-link-function first file t)
587             ;; Save the article.
588             (write-region (point-min) (point-max) file nil 
589                           (if (nnheader-be-verbose 5) nil 'nomesg))
590             (setq first file)))
591         (setq ga (cdr ga))))
592     ;; Generate a nov line for this article. We generate the nov
593     ;; line after saving, because nov generation destroys the
594     ;; header. 
595     (setq nov-line (nnml-make-nov-line chars))
596     ;; Output the nov line to all nov databases that should have it.
597     (let ((ga group-art))
598       (while ga
599         (nnml-add-nov (caar ga) (cdar ga) nov-line)
600         (setq ga (cdr ga))))
601     group-art))
602
603 (defun nnml-active-number (group)
604   "Compute the next article number in GROUP."
605   (let ((active (cadr (assoc group nnml-group-alist))))
606     ;; The group wasn't known to nnml, so we just create an active
607     ;; entry for it.   
608     (or active
609         (progn
610           (setq active (cons 1 0))
611           (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
612     (setcdr active (1+ (cdr active)))
613     (while (file-exists-p
614             (concat (nnmail-group-pathname group nnml-directory)
615                     (int-to-string (cdr active))))
616       (setcdr active (1+ (cdr active))))
617     (cdr active)))
618
619 (defun nnml-add-nov (group article line)
620   "Add a nov line for the GROUP base."
621   (save-excursion 
622     (set-buffer (nnml-open-nov group))
623     (goto-char (point-max))
624     (insert (int-to-string article) line)))
625
626 (defsubst nnml-header-value ()
627   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
628
629 (defun nnml-make-nov-line (chars)
630   "Create a nov from the current headers."
631   (let ((case-fold-search t)
632         subject from date id references lines xref in-reply-to char)
633     (save-excursion
634       (save-restriction
635         (goto-char (point-min))
636         (narrow-to-region 
637          (point)
638          (1- (or (search-forward "\n\n" nil t) (point-max))))
639         ;; Fold continuation lines.
640         (goto-char (point-min))
641         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
642           (replace-match " " t t))
643         (subst-char-in-region (point-min) (point-max) ?\t ? )
644         ;; [number subject from date id references chars lines xref]
645         (save-excursion
646           (goto-char (point-min))
647           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
648                                     nil t)
649             (beginning-of-line)
650             (setq char (downcase (following-char))) 
651             (cond
652              ((eq char ?s)
653               (setq subject (nnml-header-value)))
654              ((eq char ?f)
655               (setq from (nnml-header-value)))
656              ((eq char ?x)
657               (setq xref (buffer-substring (match-beginning 0) 
658                                            (progn (end-of-line) (point)))))
659              ((eq char ?l)
660               (setq lines (nnml-header-value)))
661              ((eq char ?d)
662               (setq date (nnml-header-value)))
663              ((eq char ?m)
664               (setq id (setq id (nnml-header-value))))
665              ((eq char ?r)
666               (setq references (nnml-header-value)))
667              ((eq char ?i)
668               (setq in-reply-to (nnml-header-value))))
669             (forward-line 1))
670       
671           (and (not references)
672                in-reply-to
673                (string-match "<[^>]+>" in-reply-to)
674                (setq references
675                      (substring in-reply-to (match-beginning 0)
676                                 (match-end 0)))))
677         ;; [number subject from date id references chars lines xref]
678         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
679                 (or subject "(none)") (or from "(nobody)") (or date "")
680                 (or id (nnmail-message-id))
681                 (or references "") (or chars 0) (or lines "0") 
682                 (or xref ""))))))
683
684 (defun nnml-open-nov (group)
685   (or (cdr (assoc group nnml-nov-buffer-alist))
686       (let ((buffer (find-file-noselect 
687                      (concat (nnmail-group-pathname group nnml-directory)
688                              nnml-nov-file-name))))
689         (save-excursion
690           (set-buffer buffer)
691           (buffer-disable-undo (current-buffer)))
692         (setq nnml-nov-buffer-alist 
693               (cons (cons group buffer) nnml-nov-buffer-alist))
694         buffer)))
695
696 (defun nnml-save-nov ()
697   (save-excursion
698     (while nnml-nov-buffer-alist
699       (when (buffer-name (cdar nnml-nov-buffer-alist))
700         (set-buffer (cdar nnml-nov-buffer-alist))
701         (and (buffer-modified-p)
702              (write-region 
703               1 (point-max) (buffer-file-name) nil 'nomesg))
704         (set-buffer-modified-p nil)
705         (kill-buffer (current-buffer)))
706       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
707
708 ;;;###autoload
709 (defun nnml-generate-nov-databases ()
710   "Generate nov databases in all nnml directories."
711   (interactive)
712   ;; Read the active file to make sure we don't re-use articles 
713   ;; numbers in empty groups.
714   (nnmail-activate 'nnml)
715   (nnml-open-server (or nnml-current-server ""))
716   (setq nnml-directory (expand-file-name nnml-directory))
717   ;; Recurse down the directories.
718   (nnml-generate-nov-databases-1 nnml-directory)
719   ;; Save the active file.
720   (nnmail-save-active nnml-group-alist nnml-active-file))
721
722 (defun nnml-generate-nov-databases-1 (dir)
723   (setq dir (file-name-as-directory dir))
724   ;; We descend recursively 
725   (let ((dirs (directory-files dir t nil t))
726         dir)
727     (while dirs 
728       (setq dir (pop dirs))
729       (when (and (not (string-match "/\\.\\.?$" dir))
730                  (file-directory-p dir))
731         (nnml-generate-nov-databases-1 dir))))
732   ;; Do this directory.
733   (let ((files (sort
734                 (mapcar
735                  (lambda (name) (string-to-int name))
736                  (directory-files dir nil "^[0-9]+$" t))
737                 '<)))
738     (when files
739       (funcall nnml-generate-active-function dir)
740       ;; Generate the nov file.
741       (nnml-generate-nov-file dir files))))
742
743 (defun nnml-generate-active-info (dir)
744   ;; Update the active info for this group.
745   (let ((group (nnheader-file-to-group 
746                 (directory-file-name dir) nnml-directory)))
747     (setq nnml-group-alist
748           (delq (assoc group nnml-group-alist) nnml-group-alist))
749     (push (list group
750                 (cons (car files)
751                       (let ((f files))
752                         (while (cdr f) (setq f (cdr f)))
753                         (car f))))
754           nnml-group-alist)))
755
756 (defun nnml-generate-nov-file (dir files)
757   (let* ((dir (file-name-as-directory dir))
758          (nov (concat dir nnml-nov-file-name))
759          (nov-buffer (get-buffer-create " *nov*"))
760          nov-line chars file)
761     (save-excursion
762       ;; Init the nov buffer.
763       (set-buffer nov-buffer)
764       (buffer-disable-undo (current-buffer))
765       (erase-buffer)
766       (set-buffer nntp-server-buffer)
767       ;; Delete the old NOV file.
768       (when (file-exists-p nov)
769         (funcall nnmail-delete-file-function nov))
770       (while files
771         (unless (file-directory-p 
772                  (setq file (concat dir (int-to-string (car files)))))
773           (erase-buffer)
774           (insert-file-contents file)
775           (narrow-to-region 
776            (goto-char (point-min))
777            (progn
778              (search-forward "\n\n" nil t)
779              (setq chars (- (point-max) (point)))
780              (max 1 (1- (point)))))
781           (when (and (not (= 0 chars))  ; none of them empty files...
782                      (not (= (point-min) (point-max))))
783             (goto-char (point-min))
784             (setq nov-line (nnml-make-nov-line chars))
785             (save-excursion
786               (set-buffer nov-buffer)
787               (goto-char (point-max))
788               (insert (int-to-string (car files)) nov-line)))
789           (widen))
790         (setq files (cdr files)))
791       (save-excursion
792         (set-buffer nov-buffer)
793         (write-region 1 (point-max) (expand-file-name nov) nil
794                       'nomesg)
795         (kill-buffer (current-buffer))))))
796
797 (defun nnml-nov-delete-article (group article)
798   (save-excursion
799     (set-buffer (nnml-open-nov group))
800     (goto-char (point-min))
801     (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
802         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
803     t))
804
805 (provide 'nnml)
806
807 ;;; nnml.el ends here