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