*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.  
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (eval-when-compile (require 'cl))
36
37 (defvar nnml-directory "~/Mail/"
38   "Mail spool directory.")
39
40 (defvar nnml-active-file (concat nnml-directory "active")
41   "Mail active file.")
42
43 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
44   "Mail newsgroups description file.")
45
46 (defvar nnml-get-new-mail t
47   "If non-nil, nnml will check the incoming mail file and split the mail.")
48
49 (defvar nnml-nov-is-evil nil
50   "If non-nil, Gnus will never generate and use nov databases for mail groups.
51 Using nov databases will speed up header fetching considerably.
52 This variable shouldn't be flipped much. If you have, for some reason,
53 set this to t, and want to set it to nil again, you should always run
54 the `nnml-generate-nov-databases' command. The function will go
55 through all nnml directories and generate nov databases for them
56 all. This may very well take some time.")
57
58 (defvar nnml-prepare-save-mail-hook nil
59   "Hook run narrowed to an article before saving.")
60
61 \f
62
63 (defconst nnml-version "nnml 1.0"
64   "nnml version.")
65
66 (defvar nnml-nov-file-name ".overview")
67
68 (defvar nnml-current-directory nil)
69 (defvar nnml-current-group nil)
70 (defvar nnml-status-string "")
71 (defvar nnml-nov-buffer-alist nil)
72 (defvar nnml-group-alist nil)
73 (defvar nnml-active-timestamp nil)
74 (defvar nnml-article-file-alist nil)
75
76 (defvar nnml-generate-active-function 'nnml-generate-active-info)
77
78 \f
79
80 ;; Server variables.
81
82 (defvar nnml-current-server nil)
83 (defvar nnml-server-alist nil)
84 (defvar nnml-server-variables 
85   `((nnml-directory ,nnml-directory)
86     (nnml-active-file ,nnml-active-file)
87     (nnml-newsgroups-file ,nnml-newsgroups-file)
88     (nnml-get-new-mail ,nnml-get-new-mail)
89     (nnml-nov-is-evil ,nnml-nov-is-evil)
90     (nnml-nov-file-name ,nnml-nov-file-name)
91     (nnml-current-directory nil)
92     (nnml-current-group nil)
93     (nnml-status-string "")
94     (nnml-nov-buffer-alist nil)
95     (nnml-group-alist nil)
96     (nnml-active-timestamp nil)))
97
98 \f
99
100 ;;; Interface functions.
101
102 (defun nnml-retrieve-headers (sequence &optional newsgroup server fetch-old)
103   (save-excursion
104     (set-buffer nntp-server-buffer)
105     (erase-buffer)
106     (let ((file nil)
107           (number (length sequence))
108           (count 0)
109           beg article)
110       (if (stringp (car sequence))
111           'headers
112         (nnml-possibly-change-directory newsgroup)
113         (unless nnml-article-file-alist
114           (setq nnml-article-file-alist
115                 (nnheader-article-to-file-alist nnml-current-directory)))
116         (if (nnml-retrieve-headers-with-nov sequence fetch-old)
117             'nov
118           (while sequence
119             (setq article (car sequence))
120             (setq file 
121                   (concat nnml-current-directory 
122                           (or (cdr (assq article nnml-article-file-alist))
123                               "")))
124             (if (and (file-exists-p file)
125                      (not (file-directory-p file)))
126                 (progn
127                   (insert (format "221 %d Article retrieved.\n" article))
128                   (setq beg (point))
129                   (nnheader-insert-head file)
130                   (goto-char beg)
131                   (if (search-forward "\n\n" nil t)
132                       (forward-char -1)
133                     (goto-char (point-max))
134                     (insert "\n\n"))
135                   (insert ".\n")
136                   (delete-region (point) (point-max))))
137             (setq sequence (cdr sequence))
138             (setq count (1+ count))
139             (and (numberp nnmail-large-newsgroup)
140                  (> number nnmail-large-newsgroup)
141                  (zerop (% count 20))
142                  (nnheader-message 6 "nnml: Receiving headers... %d%%"
143                                    (/ (* count 100) number))))
144
145           (and (numberp nnmail-large-newsgroup)
146                (> number nnmail-large-newsgroup)
147                (nnheader-message 6 "nnml: Receiving headers...done"))
148
149           (nnheader-fold-continuation-lines)
150           'headers)))))
151
152 (defun nnml-open-server (server &optional defs)
153   (nnheader-change-server 'nnml server defs)
154   (when (not (file-exists-p nnml-directory))
155     (condition-case ()
156         (make-directory nnml-directory t)
157       (error t)))
158   (cond 
159    ((not (file-exists-p nnml-directory))
160     (nnml-close-server)
161     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
162    ((not (file-directory-p (file-truename nnml-directory)))
163     (nnml-close-server)
164     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
165    (t
166     (nnheader-report 'nnml "Opened server %s using directory %s"
167                      server nnml-directory)
168     t)))
169
170 (defun nnml-close-server (&optional server)
171   (setq nnml-current-server nil
172         nnml-group-alist nil)
173   t)
174
175 (defun nnml-server-opened (&optional server)
176   (and (equal server nnml-current-server)
177        nntp-server-buffer
178        (buffer-name nntp-server-buffer)))
179
180 (defun nnml-status-message (&optional server)
181   nnml-status-string)
182
183 (defun nnml-request-article (id &optional newsgroup server buffer)
184   (nnml-possibly-change-directory newsgroup)
185   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
186          file path gpath group-num)
187     (if (stringp id)
188         (when (and (setq group-num (nnml-find-group-number id))
189                    (setq file (cdr
190                                (assq (cdr group-num) 
191                                      (nnheader-article-to-file-alist
192                                       (setq gpath
193                                             (nnmail-group-pathname
194                                              (car group-num) 
195                                              nnml-directory)))))))
196           (setq path (concat gpath (int-to-string (cdr group-num)))))
197       (unless nnml-article-file-alist
198         (setq nnml-article-file-alist
199               (nnheader-article-to-file-alist nnml-current-directory)))
200       (when (setq file (cdr (assq id nnml-article-file-alist)))
201         (setq path (concat nnml-current-directory file))))
202     (cond 
203      ((not path)
204       (nnheader-report 'nnml "No such article: %s" id))
205      ((not (file-exists-p path))
206       (nnheader-report 'nnml "No such file: %s" path))
207      ((file-directory-p path)
208       (nnheader-report 'nnml "File is a directory: %s" path))
209      ((not (save-excursion (nnmail-find-file path)))
210       (nnheader-report 'nnml "Couldn't read file: %s" path))
211      (t
212       (nnheader-report 'nnml "Article %s retrieved" id)
213       ;; We return the article number.
214       (cons newsgroup (string-to-int (file-name-nondirectory path)))))))
215
216 (defun nnml-request-group (group &optional server dont-check)
217   (cond 
218    ((not (nnml-possibly-change-directory group))
219     (nnheader-report 'nnml "Invalid group (no such directory)"))
220    (dont-check 
221     (nnheader-report 'nnml "Group %s selected" group)
222     t)
223    (t
224     (nnmail-activate 'nnml)
225     (let ((active (nth 1 (assoc group nnml-group-alist))))
226       (if (not active)
227           (nnheader-report 'nnml "No such group: %s" group)
228         (nnheader-report 'nnml "Selected group %s" group)
229         (nnheader-insert "211 %d %d %d %s\n" 
230                          (max (1+ (- (cdr active) (car active))) 0)
231                          (car active) (cdr active) group))))))
232
233 (defun nnml-request-scan (&optional group server)
234   (setq nnml-article-file-alist nil)
235   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
236
237 (defun nnml-close-group (group &optional server)
238   t)
239
240 (defun nnml-request-close ()
241   (setq nnml-current-server nil)
242   (setq nnml-server-alist nil)
243   t)
244
245 (defun nnml-request-create-group (group &optional server) 
246   (nnmail-activate 'nnml)
247   (or (assoc group nnml-group-alist)
248       (let (active)
249         (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
250                                      nnml-group-alist))
251         (nnml-possibly-create-directory group)
252         (nnml-possibly-change-directory group)
253         (let ((articles 
254                (nnheader-directory-articles nnml-current-directory )))
255           (and articles
256                (progn
257                  (setcar active (apply 'min articles))
258                  (setcdr active (apply 'max articles)))))
259         (nnmail-save-active nnml-group-alist nnml-active-file)))
260   t)
261
262 (defun nnml-request-list (&optional server)
263   (save-excursion
264     (nnmail-find-file nnml-active-file)
265     (setq nnml-group-alist (nnmail-get-active))))
266
267 (defun nnml-request-newgroups (date &optional server)
268   (nnml-request-list server))
269
270 (defun nnml-request-list-newsgroups (&optional server)
271   (save-excursion
272     (nnmail-find-file nnml-newsgroups-file)))
273
274 (defun nnml-request-post (&optional server)
275   (mail-send-and-exit nil))
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            (progn
440              (rename-file 
441               (directory-file-name nnml-current-directory)
442               (directory-file-name 
443                (nnmail-group-pathname new-name nnml-directory)))
444              t)
445          (error nil))
446        ;; That went ok, so we change the internal structures.
447        (let ((entry (assoc group nnml-group-alist)))
448          (and entry (setcar entry new-name))
449          (setq nnml-current-directory nil
450                nnml-current-group nil)
451          ;; Save the new group alist.
452          (nnmail-save-active nnml-group-alist nnml-active-file)
453          t)))
454
455 \f
456 ;;; Internal functions.
457
458 (defun nnml-deletable-article-p (group article)
459   "Say whether ARTICLE in GROUP can be deleted."
460   (let (file path)
461     (when (setq file (cdr (assq article nnml-article-file-alist)))
462       (setq path (concat nnml-current-directory file))
463       (and (file-writable-p path)
464            (or (not nnmail-keep-last-article)
465                (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) 
466                         article)))))))
467
468 ;; Find an article number in the current group given the Message-ID. 
469 (defun nnml-find-group-number (id)
470   (save-excursion
471     (set-buffer (get-buffer-create " *nnml id*"))
472     (buffer-disable-undo (current-buffer))
473     (let ((alist nnml-group-alist)
474           number)
475       ;; We want to look through all .overview files, but we want to
476       ;; start with the one in the current directory.  It seems most
477       ;; likely that the article we are looking for is in that group. 
478       (if (setq number (nnml-find-id nnml-current-group id))
479           (cons nnml-current-group number)
480         ;; It wasn't there, so we look through the other groups as well.
481         (while (and (not number)
482                     alist)
483           (or (string= (car (car alist)) nnml-current-group)
484               (setq number (nnml-find-id (car (car alist)) id)))
485           (or number
486               (setq alist (cdr alist))))
487         (and number
488              (cons (car (car alist)) number))))))
489
490 (defun nnml-find-id (group id)
491   (erase-buffer)
492   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
493                      nnml-nov-file-name))
494         number found)
495     (when (file-exists-p nov)
496       (insert-file-contents nov)
497       (while (and (not found) 
498                   (search-forward id nil t)) ; We find the ID.
499         ;; And the id is in the fourth field.
500         (if (search-backward 
501              "\t" (save-excursion (beginning-of-line) (point)) t 4)
502             (progn
503               (beginning-of-line)
504               (setq found t)
505               ;; We return the article number.
506               (setq number
507                     (condition-case ()
508                         (read (current-buffer))
509                       (error nil))))))
510       number)))
511
512 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
513   (if (or gnus-nov-is-evil nnml-nov-is-evil)
514       nil
515     (let ((first (car articles))
516           (last (progn (while (cdr articles) (setq articles (cdr articles)))
517                        (car articles)))
518           (nov (concat nnml-current-directory nnml-nov-file-name)))
519       (if (file-exists-p nov)
520           (save-excursion
521             (set-buffer nntp-server-buffer)
522             (erase-buffer)
523             (insert-file-contents nov)
524             (if (and fetch-old
525                      (not (numberp fetch-old)))
526                 t                       ; Don't remove anything.
527               (if fetch-old
528                   (setq first (max 1 (- first fetch-old))))
529               (goto-char (point-min))
530               (while (and (not (eobp)) (< first (read (current-buffer))))
531                 (forward-line 1))
532               (beginning-of-line)
533               (if (not (eobp)) (delete-region 1 (point)))
534               (while (and (not (eobp)) (>= last (read (current-buffer))))
535                 (forward-line 1))
536               (beginning-of-line)
537               (if (not (eobp)) (delete-region (point) (point-max)))
538               t))))))
539
540 (defun nnml-possibly-change-directory (group &optional force)
541   (when group
542     (let ((pathname (nnmail-group-pathname group nnml-directory)))
543       (when (or force
544                 (not (equal pathname nnml-current-directory)))
545         (setq nnml-current-directory pathname
546               nnml-current-group group
547               nnml-article-file-alist nil))))
548   t)
549
550 (defun nnml-possibly-create-directory (group)
551   (let (dir dirs)
552     (setq dir (nnmail-group-pathname group nnml-directory))
553     (while (not (file-directory-p dir))
554       (setq dirs (cons dir dirs))
555       (setq dir (file-name-directory (directory-file-name dir))))
556     (while dirs
557       (make-directory (directory-file-name (car dirs)))
558       (nnheader-message 5 "Creating mail directory %s" (car dirs))
559       (setq dirs (cdr dirs)))))
560              
561 (defun nnml-save-mail ()
562   "Called narrowed to an article."
563   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
564         chars nov-line)
565     (setq chars (nnmail-insert-lines))
566     (nnmail-insert-xref group-art)
567     (run-hooks 'nnml-prepare-save-mail-hook)
568     (goto-char (point-min))
569     (while (looking-at "From ")
570       (replace-match "X-From-Line: ")
571       (forward-line 1))
572     ;; We save the article in all the newsgroups it belongs in.
573     (let ((ga group-art)
574           first)
575       (while ga
576         (nnml-possibly-create-directory (car (car ga)))
577         (let ((file (concat (nnmail-group-pathname 
578                              (car (car ga)) nnml-directory)
579                             (int-to-string (cdr (car ga))))))
580           (if first
581               ;; It was already saved, so we just make a hard link.
582               (funcall nnmail-crosspost-link-function first file t)
583             ;; Save the article.
584             (write-region (point-min) (point-max) file nil 
585                           (if (nnheader-be-verbose 5) nil 'nomesg))
586             (setq first file)))
587         (setq ga (cdr ga))))
588     ;; Generate a nov line for this article. We generate the nov
589     ;; line after saving, because nov generation destroys the
590     ;; header. 
591     (setq nov-line (nnml-make-nov-line chars))
592     ;; Output the nov line to all nov databases that should have it.
593     (let ((ga group-art))
594       (while ga
595         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
596         (setq ga (cdr ga))))
597     group-art))
598
599 (defun nnml-active-number (group)
600   "Compute the next article number in GROUP."
601   (let ((active (car (cdr (assoc group nnml-group-alist)))))
602     ;; The group wasn't known to nnml, so we just create an active
603     ;; entry for it.   
604     (or active
605         (progn
606           (setq active (cons 1 0))
607           (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
608     (setcdr active (1+ (cdr active)))
609     (while (file-exists-p
610             (concat (nnmail-group-pathname group nnml-directory)
611                     (int-to-string (cdr active))))
612       (setcdr active (1+ (cdr active))))
613     (cdr active)))
614
615 (defun nnml-add-nov (group article line)
616   "Add a nov line for the GROUP base."
617   (save-excursion 
618     (set-buffer (nnml-open-nov group))
619     (goto-char (point-max))
620     (insert (int-to-string article) line)))
621
622 (defsubst nnml-header-value ()
623   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
624
625 (defun nnml-make-nov-line (chars)
626   "Create a nov from the current headers."
627   (let ((case-fold-search t)
628         subject from date id references lines xref in-reply-to char)
629     (save-excursion
630       (save-restriction
631         (goto-char (point-min))
632         (narrow-to-region 
633          (point)
634          (1- (or (search-forward "\n\n" nil t) (point-max))))
635         ;; Fold continuation lines.
636         (goto-char (point-min))
637         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
638           (replace-match " " t t))
639         (subst-char-in-region (point-min) (point-max) ?\t ? )
640         ;; [number subject from date id references chars lines xref]
641         (save-excursion
642           (goto-char (point-min))
643           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
644                                     nil t)
645             (beginning-of-line)
646             (setq char (downcase (following-char))) 
647             (cond
648              ((eq char ?s)
649               (setq subject (nnml-header-value)))
650              ((eq char ?f)
651               (setq from (nnml-header-value)))
652              ((eq char ?x)
653               (setq xref (buffer-substring (match-beginning 0) 
654                                            (progn (end-of-line) (point)))))
655              ((eq char ?l)
656               (setq lines (nnml-header-value)))
657              ((eq char ?d)
658               (setq date (nnml-header-value)))
659              ((eq char ?m)
660               (setq id (setq id (nnml-header-value))))
661              ((eq char ?r)
662               (setq references (nnml-header-value)))
663              ((eq char ?i)
664               (setq in-reply-to (nnml-header-value))))
665             (forward-line 1))
666       
667           (and (not references)
668                in-reply-to
669                (string-match "<[^>]+>" in-reply-to)
670                (setq references
671                      (substring in-reply-to (match-beginning 0)
672                                 (match-end 0)))))
673         ;; [number subject from date id references chars lines xref]
674         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
675                 (or subject "(none)") (or from "(nobody)") (or date "")
676                 (or id (nnmail-message-id))
677                 (or references "") (or chars 0) (or lines "0") 
678                 (or xref ""))))))
679
680 (defun nnml-open-nov (group)
681   (or (cdr (assoc group nnml-nov-buffer-alist))
682       (let ((buffer (find-file-noselect 
683                      (concat (nnmail-group-pathname group nnml-directory)
684                              nnml-nov-file-name))))
685         (save-excursion
686           (set-buffer buffer)
687           (buffer-disable-undo (current-buffer)))
688         (setq nnml-nov-buffer-alist 
689               (cons (cons group buffer) nnml-nov-buffer-alist))
690         buffer)))
691
692 (defun nnml-save-nov ()
693   (save-excursion
694     (while nnml-nov-buffer-alist
695       (when (buffer-name (cdr (car nnml-nov-buffer-alist)))
696         (set-buffer (cdr (car nnml-nov-buffer-alist)))
697         (and (buffer-modified-p)
698              (write-region 
699               1 (point-max) (buffer-file-name) nil 'nomesg))
700         (set-buffer-modified-p nil)
701         (kill-buffer (current-buffer)))
702       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
703
704 ;;;###autoload
705 (defun nnml-generate-nov-databases ()
706   "Generate nov databases in all nnml directories."
707   (interactive)
708   ;; Read the active file to make sure we don't re-use articles 
709   ;; numbers in empty groups.
710   (nnmail-activate 'nnml)
711   (nnml-open-server (or nnml-current-server ""))
712   (setq nnml-directory (expand-file-name nnml-directory))
713   ;; Recurse down the directories.
714   (nnml-generate-nov-databases-1 nnml-directory)
715   ;; Save the active file.
716   (nnmail-save-active nnml-group-alist nnml-active-file))
717
718 (defun nnml-generate-nov-databases-1 (dir)
719   (setq dir (file-name-as-directory dir))
720   ;; We descend recursively 
721   (let ((dirs (directory-files dir t nil t))
722         dir)
723     (while dirs 
724       (setq dir (pop dirs))
725       (when (and (not (string-match "/\\.\\.?$" dir))
726                  (file-directory-p dir))
727         (nnml-generate-nov-databases-1 dir))))
728   ;; Do this directory.
729   (let ((files (sort
730                 (mapcar
731                  (lambda (name) (string-to-int name))
732                  (directory-files dir nil "^[0-9]+$" t))
733                 '<)))
734     (when files
735       (funcall nnml-generate-active-function dir)
736       ;; Generate the nov file.
737       (nnml-generate-nov-file dir files))))
738
739 (defun nnml-generate-active-info (dir)
740   ;; Update the active info for this group.
741   (let ((group (nnheader-file-to-group 
742                 (directory-file-name dir) nnml-directory)))
743     (setq nnml-group-alist
744           (delq (assoc group nnml-group-alist) nnml-group-alist))
745     (push (list group
746                 (cons (car files)
747                       (let ((f files))
748                         (while (cdr f) (setq f (cdr f)))
749                         (car f))))
750           nnml-group-alist)))
751
752 (defun nnml-generate-nov-file (dir files)
753   (let* ((dir (file-name-as-directory dir))
754          (nov (concat dir nnml-nov-file-name))
755          (nov-buffer (get-buffer-create " *nov*"))
756          nov-line chars file)
757     (save-excursion
758       ;; Init the nov buffer.
759       (set-buffer nov-buffer)
760       (buffer-disable-undo (current-buffer))
761       (erase-buffer)
762       (set-buffer nntp-server-buffer)
763       ;; Delete the old NOV file.
764       (when (file-exists-p nov)
765         (funcall nnmail-delete-file-function nov))
766       (while files
767         (unless (file-directory-p 
768                  (setq file (concat dir (int-to-string (car files)))))
769           (erase-buffer)
770           (insert-file-contents file)
771           (narrow-to-region 
772            (goto-char (point-min))
773            (progn
774              (search-forward "\n\n" nil t)
775              (setq chars (- (point-max) (point)))
776              (max 1 (1- (point)))))
777           (when (and (not (= 0 chars))  ; none of them empty files...
778                      (not (= (point-min) (point-max))))
779             (goto-char (point-min))
780             (setq nov-line (nnml-make-nov-line chars))
781             (save-excursion
782               (set-buffer nov-buffer)
783               (goto-char (point-max))
784               (insert (int-to-string (car files)) nov-line)))
785           (widen)
786           (setq files (cdr files))))
787       (save-excursion
788         (set-buffer nov-buffer)
789         (write-region 1 (point-max) (expand-file-name nov) nil
790                       'nomesg)
791         (kill-buffer (current-buffer))))))
792
793 (defun nnml-nov-delete-article (group article)
794   (save-excursion
795     (set-buffer (nnml-open-nov group))
796     (goto-char (point-min))
797     (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
798         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
799     t))
800
801 (provide 'nnml)
802
803 ;;; nnml.el ends here