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