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