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