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