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