*** 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   (insert-file-contents 
493    (concat (nnmail-group-pathname group nnml-directory)
494            nnml-nov-file-name))
495   (let (number found)
496     (while (and (not found) 
497                 (search-forward id nil t)) ; We find the ID.
498       ;; And the id is in the fourth field.
499       (if (search-backward 
500            "\t" (save-excursion (beginning-of-line) (point)) t 4)
501           (progn
502             (beginning-of-line)
503             (setq found t)
504             ;; We return the article number.
505             (setq number
506                   (condition-case ()
507                       (read (current-buffer))
508                     (error nil))))))
509     number))
510
511 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
512   (if (or gnus-nov-is-evil nnml-nov-is-evil)
513       nil
514     (let ((first (car articles))
515           (last (progn (while (cdr articles) (setq articles (cdr articles)))
516                        (car articles)))
517           (nov (concat nnml-current-directory nnml-nov-file-name)))
518       (if (file-exists-p nov)
519           (save-excursion
520             (set-buffer nntp-server-buffer)
521             (erase-buffer)
522             (insert-file-contents nov)
523             (if (and fetch-old
524                      (not (numberp fetch-old)))
525                 t                       ; Don't remove anything.
526               (if fetch-old
527                   (setq first (max 1 (- first fetch-old))))
528               (goto-char (point-min))
529               (while (and (not (eobp)) (< first (read (current-buffer))))
530                 (forward-line 1))
531               (beginning-of-line)
532               (if (not (eobp)) (delete-region 1 (point)))
533               (while (and (not (eobp)) (>= last (read (current-buffer))))
534                 (forward-line 1))
535               (beginning-of-line)
536               (if (not (eobp)) (delete-region (point) (point-max)))
537               t))))))
538
539 (defun nnml-possibly-change-directory (group &optional force)
540   (when group
541     (let ((pathname (nnmail-group-pathname group nnml-directory)))
542       (when (or force
543                 (not (equal pathname nnml-current-directory)))
544         (setq nnml-current-directory pathname
545               nnml-current-group group
546               nnml-article-file-alist nil))))
547   t)
548
549 (defun nnml-possibly-create-directory (group)
550   (let (dir dirs)
551     (setq dir (nnmail-group-pathname group nnml-directory))
552     (while (not (file-directory-p dir))
553       (setq dirs (cons dir dirs))
554       (setq dir (file-name-directory (directory-file-name dir))))
555     (while dirs
556       (make-directory (directory-file-name (car dirs)))
557       (nnheader-message 5 "Creating mail directory %s" (car dirs))
558       (setq dirs (cdr dirs)))))
559              
560 (defun nnml-save-mail ()
561   "Called narrowed to an article."
562   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
563         chars nov-line)
564     (setq chars (nnmail-insert-lines))
565     (nnmail-insert-xref group-art)
566     (run-hooks 'nnml-prepare-save-mail-hook)
567     (goto-char (point-min))
568     (while (looking-at "From ")
569       (replace-match "X-From-Line: ")
570       (forward-line 1))
571     ;; We save the article in all the newsgroups it belongs in.
572     (let ((ga group-art)
573           first)
574       (while ga
575         (nnml-possibly-create-directory (car (car ga)))
576         (let ((file (concat (nnmail-group-pathname 
577                              (car (car ga)) nnml-directory)
578                             (int-to-string (cdr (car ga))))))
579           (if first
580               ;; It was already saved, so we just make a hard link.
581               (funcall nnmail-crosspost-link-function first file t)
582             ;; Save the article.
583             (write-region (point-min) (point-max) file nil 
584                           (if (nnheader-be-verbose 5) nil 'nomesg))
585             (setq first file)))
586         (setq ga (cdr ga))))
587     ;; Generate a nov line for this article. We generate the nov
588     ;; line after saving, because nov generation destroys the
589     ;; header. 
590     (setq nov-line (nnml-make-nov-line chars))
591     ;; Output the nov line to all nov databases that should have it.
592     (let ((ga group-art))
593       (while ga
594         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
595         (setq ga (cdr ga))))
596     group-art))
597
598 (defun nnml-active-number (group)
599   "Compute the next article number in GROUP."
600   (let ((active (car (cdr (assoc group nnml-group-alist)))))
601     ;; The group wasn't known to nnml, so we just create an active
602     ;; entry for it.   
603     (or active
604         (progn
605           (setq active (cons 1 0))
606           (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
607     (setcdr active (1+ (cdr active)))
608     (while (file-exists-p
609             (concat (nnmail-group-pathname group nnml-directory)
610                     (int-to-string (cdr active))))
611       (setcdr active (1+ (cdr active))))
612     (cdr active)))
613
614 (defun nnml-add-nov (group article line)
615   "Add a nov line for the GROUP base."
616   (save-excursion 
617     (set-buffer (nnml-open-nov group))
618     (goto-char (point-max))
619     (insert (int-to-string article) line)))
620
621 (defsubst nnml-header-value ()
622   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
623
624 (defun nnml-make-nov-line (chars)
625   "Create a nov from the current headers."
626   (let ((case-fold-search t)
627         subject from date id references lines xref in-reply-to char)
628     (save-excursion
629       (save-restriction
630         (goto-char (point-min))
631         (narrow-to-region 
632          (point)
633          (1- (or (search-forward "\n\n" nil t) (point-max))))
634         ;; Fold continuation lines.
635         (goto-char (point-min))
636         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
637           (replace-match " " t t))
638         (subst-char-in-region (point-min) (point-max) ?\t ? )
639         ;; [number subject from date id references chars lines xref]
640         (save-excursion
641           (goto-char (point-min))
642           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
643                                     nil t)
644             (beginning-of-line)
645             (setq char (downcase (following-char))) 
646             (cond
647              ((eq char ?s)
648               (setq subject (nnml-header-value)))
649              ((eq char ?f)
650               (setq from (nnml-header-value)))
651              ((eq char ?x)
652               (setq xref (buffer-substring (match-beginning 0) 
653                                            (progn (end-of-line) (point)))))
654              ((eq char ?l)
655               (setq lines (nnml-header-value)))
656              ((eq char ?d)
657               (setq date (nnml-header-value)))
658              ((eq char ?m)
659               (setq id (setq id (nnml-header-value))))
660              ((eq char ?r)
661               (setq references (nnml-header-value)))
662              ((eq char ?i)
663               (setq in-reply-to (nnml-header-value))))
664             (forward-line 1))
665       
666           (and (not references)
667                in-reply-to
668                (string-match "<[^>]+>" in-reply-to)
669                (setq references
670                      (substring in-reply-to (match-beginning 0)
671                                 (match-end 0)))))
672         ;; [number subject from date id references chars lines xref]
673         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
674                 (or subject "(none)") (or from "(nobody)") (or date "")
675                 (or id (nnmail-message-id))
676                 (or references "") (or chars 0) (or lines "0") 
677                 (or xref ""))))))
678
679 (defun nnml-open-nov (group)
680   (or (cdr (assoc group nnml-nov-buffer-alist))
681       (let ((buffer (find-file-noselect 
682                      (concat (nnmail-group-pathname group nnml-directory)
683                              nnml-nov-file-name))))
684         (save-excursion
685           (set-buffer buffer)
686           (buffer-disable-undo (current-buffer)))
687         (setq nnml-nov-buffer-alist 
688               (cons (cons group buffer) nnml-nov-buffer-alist))
689         buffer)))
690
691 (defun nnml-save-nov ()
692   (save-excursion
693     (while nnml-nov-buffer-alist
694       (when (buffer-name (cdr (car nnml-nov-buffer-alist)))
695         (set-buffer (cdr (car nnml-nov-buffer-alist)))
696         (and (buffer-modified-p)
697              (write-region 
698               1 (point-max) (buffer-file-name) nil 'nomesg))
699         (set-buffer-modified-p nil)
700         (kill-buffer (current-buffer)))
701       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
702
703 ;;;###autoload
704 (defun nnml-generate-nov-databases ()
705   "Generate nov databases in all nnml directories."
706   (interactive)
707   ;; Read the active file to make sure we don't re-use articles 
708   ;; numbers in empty groups.
709   (nnmail-activate 'nnml)
710   (nnml-open-server (or nnml-current-server ""))
711   (setq nnml-directory (expand-file-name nnml-directory))
712   ;; Recurse down the directories.
713   (nnml-generate-nov-databases-1 nnml-directory)
714   ;; Save the active file.
715   (nnmail-save-active nnml-group-alist nnml-active-file))
716
717 (defun nnml-generate-nov-databases-1 (dir)
718   (setq dir (file-name-as-directory dir))
719   ;; We descend recursively 
720   (let ((dirs (directory-files dir t nil t))
721         dir)
722     (while dirs 
723       (setq dir (pop dirs))
724       (when (and (not (string-match "/\\.\\.?$" dir))
725                  (file-directory-p dir))
726         (nnml-generate-nov-databases-1 dir))))
727   ;; Do this directory.
728   (let ((files (sort
729                 (mapcar
730                  (lambda (name) (string-to-int name))
731                  (directory-files dir nil "^[0-9]+$" t))
732                 '<)))
733     (when files
734       (funcall nnml-generate-active-function dir)
735       ;; Generate the nov file.
736       (nnml-generate-nov-file dir files))))
737
738 (defun nnml-generate-active-info (dir)
739   ;; Update the active info for this group.
740   (let ((group (nnheader-file-to-group 
741                 (directory-file-name dir) nnml-directory)))
742     (setq nnml-group-alist
743           (delq (assoc group nnml-group-alist) nnml-group-alist))
744     (push (list group
745                 (cons (car files)
746                       (let ((f files))
747                         (while (cdr f) (setq f (cdr f)))
748                         (car f))))
749           nnml-group-alist)))
750
751 (defun nnml-generate-nov-file (dir files)
752   (let* ((dir (file-name-as-directory dir))
753          (nov (concat dir nnml-nov-file-name))
754          (nov-buffer (get-buffer-create " *nov*"))
755          nov-line chars file)
756     (save-excursion
757       ;; Init the nov buffer.
758       (set-buffer nov-buffer)
759       (buffer-disable-undo (current-buffer))
760       (erase-buffer)
761       (set-buffer nntp-server-buffer)
762       ;; Delete the old NOV file.
763       (when (file-exists-p nov)
764         (funcall nnmail-delete-file-function nov))
765       (while files
766         (unless (file-directory-p 
767                  (setq file (concat dir (int-to-string (car files)))))
768           (erase-buffer)
769           (insert-file-contents file)
770           (narrow-to-region 
771            (goto-char (point-min))
772            (progn
773              (search-forward "\n\n" nil t)
774              (setq chars (- (point-max) (point)))
775              (max 1 (1- (point)))))
776           (when (and (not (= 0 chars))  ; none of them empty files...
777                      (not (= (point-min) (point-max))))
778             (goto-char (point-min))
779             (setq nov-line (nnml-make-nov-line chars))
780             (save-excursion
781               (set-buffer nov-buffer)
782               (goto-char (point-max))
783               (insert (int-to-string (car files)) nov-line)))
784           (widen)
785           (setq files (cdr files))))
786       (save-excursion
787         (set-buffer nov-buffer)
788         (write-region 1 (point-max) (expand-file-name nov) nil
789                       'nomesg)
790         (kill-buffer (current-buffer))))))
791
792 (defun nnml-nov-delete-article (group article)
793   (save-excursion
794     (set-buffer (nnml-open-nov group))
795     (goto-char (point-min))
796     (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
797         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
798     t))
799
800 (provide 'nnml)
801
802 ;;; nnml.el ends here