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