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