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