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