*** 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'nnmail)
34 (eval-when-compile (require 'cl))
35
36 (defvar nnml-directory "~/Mail/"
37   "Mail spool directory.")
38
39 (defvar nnml-active-file (concat nnml-directory "active")
40   "Mail active file.")
41
42 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
43   "Mail newsgroups description file.")
44
45 (defvar nnml-get-new-mail t
46   "If non-nil, nnml will check the incoming mail file and split the mail.")
47
48 (defvar nnml-nov-is-evil nil
49   "If non-nil, Gnus will never generate and use nov databases for mail groups.
50 Using nov databases will speed up header fetching considerably.
51 This variable shouldn't be flipped much. If you have, for some reason,
52 set this to t, and want to set it to nil again, you should always run
53 the `nnml-generate-nov-databases' command. The function will go
54 through all nnml directories and generate nov databases for them
55 all. This may very well take some time.")
56
57 (defvar nnml-prepare-save-mail-hook nil
58   "Hook run narrowed to an article before saving.")
59
60 \f
61
62 (defconst nnml-version "nnml 1.0"
63   "nnml version.")
64
65 (defvar nnml-nov-file-name ".overview")
66
67 (defvar nnml-current-directory nil)
68 (defvar nnml-current-group nil)
69 (defvar nnml-status-string "")
70 (defvar nnml-nov-buffer-alist nil)
71 (defvar nnml-group-alist nil)
72 (defvar nnml-active-timestamp nil)
73 (defvar nnml-article-file-alist nil)
74
75 (defvar nnml-generate-active-function 'nnml-generate-active-info)
76
77 \f
78
79 ;; Server variables.
80
81 (defvar nnml-current-server nil)
82 (defvar nnml-server-alist nil)
83 (defvar nnml-server-variables 
84   (list 
85    (list 'nnml-directory nnml-directory)
86    (list 'nnml-active-file nnml-active-file)
87    (list 'nnml-newsgroups-file nnml-newsgroups-file)
88    (list 'nnml-get-new-mail nnml-get-new-mail)
89    (list 'nnml-nov-is-evil nnml-nov-is-evil)
90    (list '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-init-server-buffer)
154   (if (equal server nnml-current-server)
155       t
156     (if nnml-current-server
157         (setq nnml-server-alist 
158               (cons (list nnml-current-server
159                           (nnheader-save-variables nnml-server-variables))
160                     nnml-server-alist)))
161     (let ((state (assoc server nnml-server-alist)))
162       (if state 
163           (progn
164             (nnheader-restore-variables (nth 1 state))
165             (setq nnml-server-alist (delq state nnml-server-alist)))
166         (nnheader-set-init-variables nnml-server-variables defs)))
167     (setq nnml-current-server server)))
168
169 (defun nnml-close-server (&optional server)
170   (setq nnml-current-server nil)
171   t)
172
173 (defun nnml-server-opened (&optional server)
174   (and (equal server nnml-current-server)
175        nntp-server-buffer
176        (buffer-name nntp-server-buffer)))
177
178 (defun nnml-status-message (&optional server)
179   nnml-status-string)
180
181 (defun nnml-request-article (id &optional newsgroup server buffer)
182   (nnml-possibly-change-directory newsgroup)
183   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
184          file path gpath group-num)
185     (if (stringp id)
186         (when (and (setq group-num (nnml-find-group-number id))
187                    (setq file (cdr
188                                (assq (cdr group-num) 
189                                      (nnheader-article-to-file-alist
190                                       (setq gpath
191                                             (nnmail-group-pathname
192                                              (car group-num) 
193                                              nnml-directory)))))))
194           (setq path (concat gpath (int-to-string (cdr group-num)))))
195       (unless nnml-article-file-alist
196         (setq nnml-article-file-alist
197               (nnheader-article-to-file-alist nnml-current-directory)))
198       (when (setq file (cdr (assq id nnml-article-file-alist)))
199         (setq path (concat nnml-current-directory file))))
200     (cond 
201      ((not path)
202       (nnheader-report 'nnml "No such article: %s" id))
203      ((not (file-exists-p path))
204       (nnheader-report 'nnml "No such file: %s" path))
205      ((file-directory-p path)
206       (nnheader-report 'nnml "File is a directory: %s" path))
207      ((not (save-excursion (nnmail-find-file path)))
208       (nnheader-report 'nnml "Couldn't read file: %s" path))
209      (t
210       (nnheader-report 'nnml "Article %s retrieved" id)
211       ;; We return the article number.
212       (cons newsgroup (string-to-int (file-name-nondirectory path)))))))
213
214 (defun nnml-request-group (group &optional server dont-check)
215   (cond 
216    ((not (nnml-possibly-change-directory group))
217     (nnheader-report 'nnml "Invalid group (no such directory)"))
218    (dont-check 
219     (nnheader-report 'nnml "Group %s selected" group)
220     t)
221    (t
222     (nnmail-activate 'nnml)
223     (let ((active (nth 1 (assoc group nnml-group-alist))))
224       (save-excursion
225         (set-buffer nntp-server-buffer)
226         (erase-buffer)
227         (if (not active)
228             (nnheader-report 'nnml "No such group: %s" group)
229           (insert (format "211 %d %d %d %s\n" 
230                           (max (1+ (- (cdr active) (car active))) 0)
231                           (car active) (cdr active) group))
232           (nnheader-report 'nnml "Group %s selected" group)
233           t))))))
234
235 (defun nnml-request-scan (&optional group server)
236   (setq nnml-article-file-alist nil)
237   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
238
239 (defun nnml-close-group (group &optional server)
240   t)
241
242 (defun nnml-request-close ()
243   (setq nnml-current-server nil)
244   (setq nnml-server-alist nil)
245   t)
246
247 (defun nnml-request-create-group (group &optional server) 
248   (nnmail-activate 'nnml)
249   (or (assoc group nnml-group-alist)
250       (let (active)
251         (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
252                                      nnml-group-alist))
253         (nnml-possibly-create-directory group)
254         (nnml-possibly-change-directory group)
255         (let ((articles 
256                (nnheader-directory-articles nnml-current-directory )))
257           (and articles
258                (progn
259                  (setcar active (apply 'min articles))
260                  (setcdr active (apply 'max articles)))))
261         (nnmail-save-active nnml-group-alist nnml-active-file)))
262   t)
263
264 (defun nnml-request-list (&optional server)
265   (save-excursion
266     (nnmail-find-file nnml-active-file)
267     (setq nnml-group-alist (nnmail-get-active))))
268
269 (defun nnml-request-newgroups (date &optional server)
270   (nnml-request-list server))
271
272 (defun nnml-request-list-newsgroups (&optional server)
273   (save-excursion
274     (nnmail-find-file nnml-newsgroups-file)))
275
276 (defun nnml-request-post (&optional server)
277   (mail-send-and-exit nil))
278
279 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
280   (nnml-possibly-change-directory newsgroup)
281   (let* ((active-articles 
282           (nnheader-directory-articles nnml-current-directory))
283          (max-article (and active-articles (apply 'max active-articles)))
284          (is-old t)
285          article rest mod-time number)
286     (nnmail-activate 'nnml)
287
288     (unless nnml-article-file-alist
289       (setq nnml-article-file-alist
290             (nnheader-article-to-file-alist nnml-current-directory)))
291
292     (while (and articles is-old)
293       (setq article (concat nnml-current-directory 
294                             (int-to-string 
295                              (setq number (pop articles)))))
296       (when (setq mod-time (nth 5 (file-attributes article)))
297         (if (and (nnml-deletable-article-p newsgroup number)
298                  (setq is-old 
299                        (nnmail-expired-article-p newsgroup mod-time force)))
300             (progn
301               (nnheader-message 5 "Deleting article %s in %s..."
302                                 article newsgroup)
303               (condition-case ()
304                   (funcall nnmail-delete-file-function article)
305                 (file-error
306                  (push number rest)))
307               (setq active-articles (delq number active-articles))
308               (nnml-nov-delete-article newsgroup number))
309           (push number rest))))
310     (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
311       (and active
312            (setcar active (or (and active-articles
313                                    (apply 'min active-articles))
314                               0)))
315       (nnmail-save-active nnml-group-alist nnml-active-file))
316     (nnml-save-nov)
317     (message "")
318     (nconc rest articles)))
319
320 (defun nnml-request-move-article 
321   (article group server accept-form &optional last)
322   (let ((buf (get-buffer-create " *nnml move*"))
323         result)
324     (and 
325      (nnml-deletable-article-p group article)
326      (nnml-request-article article group server)
327      (save-excursion
328        (set-buffer buf)
329        (insert-buffer-substring nntp-server-buffer)
330        (setq result (eval accept-form))
331        (kill-buffer (current-buffer))
332        result)
333      (progn
334        (condition-case ()
335            (funcall nnmail-delete-file-function
336                     (concat nnml-current-directory 
337                             (int-to-string article)))
338          (file-error nil))
339        (nnml-nov-delete-article group article)
340        (and last (nnml-save-nov))))
341     result))
342
343 (defun nnml-request-accept-article (group &optional last)
344   (let (result)
345     (if (stringp group)
346         (and 
347          (nnmail-activate 'nnml)
348          ;; We trick the choosing function into believing that only one
349          ;; group is available.  
350          (let ((nnmail-split-methods (list (list group ""))))
351            (setq result (car (nnml-save-mail))))
352          (progn
353            (nnmail-save-active nnml-group-alist nnml-active-file)
354            (and last (nnml-save-nov))))
355       (and
356        (nnmail-activate 'nnml)
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     result))
362
363 (defun nnml-request-replace-article (article group buffer)
364   (nnml-possibly-change-directory group)
365   (save-excursion
366     (set-buffer buffer)
367     (nnml-possibly-create-directory group)
368     (if (not (condition-case ()
369                  (progn
370                    (write-region (point-min) (point-max)
371                                  (concat nnml-current-directory 
372                                          (int-to-string article))
373                                  nil (if (nnheader-be-verbose 5) nil 'nomesg))
374                    t)
375                (error nil)))
376         ()
377       (let ((chars (nnmail-insert-lines))
378             (art (concat (int-to-string article) "\t"))
379             nov-line)
380         (setq nov-line (nnml-make-nov-line chars))
381         ;; Replace the NOV line in the NOV file.
382         (save-excursion 
383           (set-buffer (nnml-open-nov group))
384           (goto-char (point-min))
385           (if (or (looking-at art)
386                   (search-forward (concat "\n" art) nil t))
387               ;; Delete the old NOV line.
388               (delete-region (progn (beginning-of-line) (point))
389                              (progn (forward-line 1) (point)))
390             ;; The line isn't here, so we have to find out where
391             ;; we should insert it. (This situation should never
392             ;; occur, but one likes to make sure...)
393             (while (and (looking-at "[0-9]+\t")
394                         (< (string-to-int 
395                             (buffer-substring 
396                              (match-beginning 0) (match-end 0)))
397                            article)
398                         (zerop (forward-line 1)))))
399           (beginning-of-line)
400           (insert (int-to-string article) nov-line)
401           (nnml-save-nov)
402           t)))))
403
404 (defun nnml-request-delete-group (group &optional force server)
405   (nnml-possibly-change-directory group)
406   (when force
407     ;; Delete all articles in GROUP.
408     (let ((articles 
409            (directory-files 
410             nnml-current-directory t
411             (concat nnheader-numerical-short-files
412                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
413           article)
414       (while articles 
415         (setq article (pop articles))
416         (when (file-writable-p article)
417           (nnheader-message 5 "Deleting article %s in %s..." article group)
418           (funcall nnmail-delete-file-function article))))
419     ;; Try to delete the directory itself.
420     (condition-case ()
421         (delete-directory nnml-current-directory)
422       (error nil)))
423   ;; Remove the group from all structures.
424   (setq nnml-group-alist 
425         (delq (assoc group nnml-group-alist) nnml-group-alist)
426         nnml-current-group nil
427         nnml-current-directory nil)
428   ;; Save the active file.
429   (nnmail-save-active nnml-group-alist nnml-active-file)
430   t)
431
432 (defun nnml-request-rename-group (group new-name &optional server)
433   (nnml-possibly-change-directory group)
434   ;; Rename directory.
435   (and (file-writable-p nnml-current-directory)
436        (condition-case ()
437            (progn
438              (rename-file 
439               (directory-file-name nnml-current-directory)
440               (directory-file-name 
441                (nnmail-group-pathname new-name nnml-directory)))
442              t)
443          (error nil))
444        ;; That went ok, so we change the internal structures.
445        (let ((entry (assoc group nnml-group-alist)))
446          (and entry (setcar entry new-name))
447          (setq nnml-current-directory nil
448                nnml-current-group nil)
449          ;; Save the new group alist.
450          (nnmail-save-active nnml-group-alist nnml-active-file)
451          t)))
452
453 \f
454 ;;; Internal functions.
455
456 (defun nnml-deletable-article-p (group article)
457   "Say whether ARTICLE in GROUP can be deleted."
458   (let (file path)
459     (when (setq file (cdr (assq article nnml-article-file-alist)))
460       (setq path (concat nnml-current-directory file))
461       (and (file-writable-p path)
462            (or (not nnmail-keep-last-article)
463                (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) 
464                         article)))))))
465
466 ;; Find an article number in the current group given the Message-ID. 
467 (defun nnml-find-group-number (id)
468   (save-excursion
469     (set-buffer (get-buffer-create " *nnml id*"))
470     (buffer-disable-undo (current-buffer))
471     (let ((alist nnml-group-alist)
472           number)
473       ;; We want to look through all .overview files, but we want to
474       ;; start with the one in the current directory.  It seems most
475       ;; likely that the article we are looking for is in that group. 
476       (if (setq number (nnml-find-id nnml-current-group id))
477           (cons nnml-current-group number)
478         ;; It wasn't there, so we look through the other groups as well.
479         (while (and (not number)
480                     alist)
481           (or (string= (car (car alist)) nnml-current-group)
482               (setq number (nnml-find-id (car (car alist)) id)))
483           (or number
484               (setq alist (cdr alist))))
485         (and number
486              (cons (car (car alist)) number))))))
487
488 (defun nnml-find-id (group id)
489   (erase-buffer)
490   (insert-file-contents 
491    (concat (nnmail-group-pathname group nnml-directory)
492            nnml-nov-file-name))
493   (let (number found)
494     (while (and (not found) 
495                 (search-forward id nil t)) ; We find the ID.
496       ;; And the id is in the fourth field.
497       (if (search-backward 
498            "\t" (save-excursion (beginning-of-line) (point)) t 4)
499           (progn
500             (beginning-of-line)
501             (setq found t)
502             ;; We return the article number.
503             (setq number
504                   (condition-case ()
505                       (read (current-buffer))
506                     (error nil))))))
507     number))
508
509 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
510   (if (or gnus-nov-is-evil nnml-nov-is-evil)
511       nil
512     (let ((first (car articles))
513           (last (progn (while (cdr articles) (setq articles (cdr articles)))
514                        (car articles)))
515           (nov (concat nnml-current-directory nnml-nov-file-name)))
516       (if (file-exists-p nov)
517           (save-excursion
518             (set-buffer nntp-server-buffer)
519             (erase-buffer)
520             (insert-file-contents nov)
521             (if (and fetch-old
522                      (not (numberp fetch-old)))
523                 t                       ; Don't remove anything.
524               (if fetch-old
525                   (setq first (max 1 (- first fetch-old))))
526               (goto-char (point-min))
527               (while (and (not (eobp)) (< first (read (current-buffer))))
528                 (forward-line 1))
529               (beginning-of-line)
530               (if (not (eobp)) (delete-region 1 (point)))
531               (while (and (not (eobp)) (>= last (read (current-buffer))))
532                 (forward-line 1))
533               (beginning-of-line)
534               (if (not (eobp)) (delete-region (point) (point-max)))
535               t))))))
536
537 (defun nnml-possibly-change-directory (group &optional force)
538   (when group
539     (let ((pathname (nnmail-group-pathname group nnml-directory)))
540       (when (or force
541                 (not (equal pathname nnml-current-directory)))
542         (setq nnml-current-directory pathname
543               nnml-current-group group
544               nnml-article-file-alist nil))))
545   t)
546
547 (defun nnml-possibly-create-directory (group)
548   (let (dir dirs)
549     (setq dir (nnmail-group-pathname group nnml-directory))
550     (while (not (file-directory-p dir))
551       (setq dirs (cons dir dirs))
552       (setq dir (file-name-directory (directory-file-name dir))))
553     (while dirs
554       (make-directory (directory-file-name (car dirs)))
555       (nnheader-message 5 "Creating mail directory %s" (car dirs))
556       (setq dirs (cdr dirs)))))
557              
558 (defun nnml-save-mail ()
559   "Called narrowed to an article."
560   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
561         chars nov-line)
562     (setq chars (nnmail-insert-lines))
563     (nnmail-insert-xref group-art)
564     (run-hooks 'nnml-prepare-save-mail-hook)
565     (goto-char (point-min))
566     (while (looking-at "From ")
567       (replace-match "X-From-Line: ")
568       (forward-line 1))
569     ;; We save the article in all the newsgroups it belongs in.
570     (let ((ga group-art)
571           first)
572       (while ga
573         (nnml-possibly-create-directory (car (car ga)))
574         (let ((file (concat (nnmail-group-pathname 
575                              (car (car ga)) nnml-directory)
576                             (int-to-string (cdr (car ga))))))
577           (if first
578               ;; It was already saved, so we just make a hard link.
579               (funcall nnmail-crosspost-link-function first file t)
580             ;; Save the article.
581             (write-region (point-min) (point-max) file nil 
582                           (if (nnheader-be-verbose 5) nil 'nomesg))
583             (setq first file)))
584         (setq ga (cdr ga))))
585     ;; Generate a nov line for this article. We generate the nov
586     ;; line after saving, because nov generation destroys the
587     ;; header. 
588     (setq nov-line (nnml-make-nov-line chars))
589     ;; Output the nov line to all nov databases that should have it.
590     (let ((ga group-art))
591       (while ga
592         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
593         (setq ga (cdr ga))))
594     group-art))
595
596 (defun nnml-active-number (group)
597   "Compute the next article number in GROUP."
598   (let ((active (car (cdr (assoc group nnml-group-alist)))))
599     ;; The group wasn't known to nnml, so we just create an active
600     ;; entry for it.   
601     (or active
602         (progn
603           (setq active (cons 1 0))
604           (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
605     (setcdr active (1+ (cdr active)))
606     (while (file-exists-p
607             (concat (nnmail-group-pathname group nnml-directory)
608                     (int-to-string (cdr active))))
609       (setcdr active (1+ (cdr active))))
610     (cdr active)))
611
612 (defun nnml-add-nov (group article line)
613   "Add a nov line for the GROUP base."
614   (save-excursion 
615     (set-buffer (nnml-open-nov group))
616     (goto-char (point-max))
617     (insert (int-to-string article) line)))
618
619 (defsubst nnml-header-value ()
620   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
621
622 (defun nnml-make-nov-line (chars)
623   "Create a nov from the current headers."
624   (let ((case-fold-search t)
625         subject from date id references lines xref in-reply-to char)
626     (save-excursion
627       (save-restriction
628         (goto-char (point-min))
629         (narrow-to-region 
630          (point)
631          (1- (or (search-forward "\n\n" nil t) (point-max))))
632         ;; Fold continuation lines.
633         (goto-char (point-min))
634         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
635           (replace-match " " t t))
636         (subst-char-in-region (point-min) (point-max) ?\t ? )
637         ;; [number subject from date id references chars lines xref]
638         (save-excursion
639           (goto-char (point-min))
640           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
641                                     nil t)
642             (beginning-of-line)
643             (setq char (downcase (following-char))) 
644             (cond
645              ((eq char ?s)
646               (setq subject (nnml-header-value)))
647              ((eq char ?f)
648               (setq from (nnml-header-value)))
649              ((eq char ?x)
650               (setq xref (buffer-substring (match-beginning 0) 
651                                            (progn (end-of-line) (point)))))
652              ((eq char ?l)
653               (setq lines (nnml-header-value)))
654              ((eq char ?d)
655               (setq date (nnml-header-value)))
656              ((eq char ?m)
657               (setq id (setq id (nnml-header-value))))
658              ((eq char ?r)
659               (setq references (nnml-header-value)))
660              ((eq char ?i)
661               (setq in-reply-to (nnml-header-value))))
662             (forward-line 1))
663       
664           (and (not references)
665                in-reply-to
666                (string-match "<[^>]+>" in-reply-to)
667                (setq references
668                      (substring in-reply-to (match-beginning 0)
669                                 (match-end 0)))))
670         ;; [number subject from date id references chars lines xref]
671         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
672                 (or subject "(none)") (or from "(nobody)") (or date "")
673                 (or id (nnmail-message-id))
674                 (or references "") (or chars 0) (or lines "0") 
675                 (or xref ""))))))
676
677 (defun nnml-open-nov (group)
678   (or (cdr (assoc group nnml-nov-buffer-alist))
679       (let ((buffer (find-file-noselect 
680                      (concat (nnmail-group-pathname group nnml-directory)
681                              nnml-nov-file-name))))
682         (save-excursion
683           (set-buffer buffer)
684           (buffer-disable-undo (current-buffer)))
685         (setq nnml-nov-buffer-alist 
686               (cons (cons group buffer) nnml-nov-buffer-alist))
687         buffer)))
688
689 (defun nnml-save-nov ()
690   (save-excursion
691     (while nnml-nov-buffer-alist
692       (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
693           (progn
694             (set-buffer (cdr (car nnml-nov-buffer-alist)))
695             (and (buffer-modified-p)
696                  (write-region 
697                   1 (point-max) (buffer-file-name) nil 'nomesg))
698             (set-buffer-modified-p nil)
699             (kill-buffer (current-buffer))))
700       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
701
702 ;;;###autoload
703 (defun nnml-generate-nov-databases ()
704   "Generate nov databases in all nnml directories."
705   (interactive)
706   ;; Read the active file to make sure we don't re-use articles 
707   ;; numbers in empty groups.
708   (nnmail-activate 'nnml)
709   (nnml-open-server (or nnml-current-server ""))
710   (setq nnml-directory (expand-file-name nnml-directory))
711   ;; Recurse down the directories.
712   (nnml-generate-nov-databases-1 nnml-directory)
713   ;; Save the active file.
714   (nnmail-save-active nnml-group-alist nnml-active-file))
715
716 (defun nnml-generate-nov-databases-1 (dir)
717   (setq dir (file-name-as-directory dir))
718   ;; We descend recursively 
719   (let ((dirs (directory-files dir t nil t))
720         dir)
721     (while dirs 
722       (setq dir (pop dirs))
723       (when (and (not (string-match "/\\.\\.?$" dir))
724                  (file-directory-p dir))
725         (nnml-generate-nov-databases-1 dir))))
726   ;; Do this directory.
727   (let ((files (sort
728                 (mapcar
729                  (lambda (name) (string-to-int name))
730                  (directory-files dir nil "^[0-9]+$" t))
731                 '<)))
732     (when files
733       (funcall nnml-generate-active-function dir)
734       ;; Generate the nov file.
735       (nnml-generate-nov-file dir files))))
736
737 (defun nnml-generate-active-info (dir)
738   ;; Update the active info for this group.
739   (let ((group (nnheader-file-to-group 
740                 (directory-file-name dir) nnml-directory)))
741     (setq nnml-group-alist
742           (delq (assoc group nnml-group-alist) nnml-group-alist))
743     (push (list group
744                 (cons (car files)
745                       (let ((f files))
746                         (while (cdr f) (setq f (cdr f)))
747                         (car f))))
748           nnml-group-alist)))
749
750 (defun nnml-generate-nov-file (dir files)
751   (let* ((dir (file-name-as-directory dir))
752          (nov (concat dir nnml-nov-file-name))
753          (nov-buffer (get-buffer-create " *nov*"))
754          nov-line chars)
755     (save-excursion
756       ;; Init the nov buffer.
757       (set-buffer nov-buffer)
758       (buffer-disable-undo (current-buffer))
759       (erase-buffer)
760       (set-buffer nntp-server-buffer)
761       ;; Delete the old NOV file.
762       (when (file-exists-p nov)
763         (funcall nnmail-delete-file-function nov))
764       (while files
765         (erase-buffer)
766         (insert-file-contents (concat dir (int-to-string (car files))))
767         (narrow-to-region 
768          (goto-char (point-min))
769          (progn
770            (search-forward "\n\n" nil t)
771            (setq chars (- (point-max) (point)))
772            (max 1 (1- (point)))))
773         (when (and (not (= 0 chars))    ; none of them empty files...
774                    (not (= (point-min) (point-max))))
775           (goto-char (point-min))
776           (setq nov-line (nnml-make-nov-line chars))
777           (save-excursion
778             (set-buffer nov-buffer)
779             (goto-char (point-max))
780             (insert (int-to-string (car files)) nov-line)))
781         (widen)
782         (setq files (cdr files)))
783       (save-excursion
784         (set-buffer nov-buffer)
785         (write-region 1 (point-max) (expand-file-name nov) nil
786                       'nomesg)
787         (kill-buffer (current-buffer))))))
788
789 (defun nnml-nov-delete-article (group article)
790   (save-excursion
791     (set-buffer (nnml-open-nov group))
792     (goto-char (point-min))
793     (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
794         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
795     t))
796
797 (provide 'nnml)
798
799 ;;; nnml.el ends here