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