*** 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 (defun nnml-request-regenerate (server)
161   (nnml-possibly-change-directory nil server)
162   (nnml-generate-nov-databases))
163
164 (deffoo nnml-request-article (id &optional newsgroup server buffer)
165   (nnml-possibly-change-directory newsgroup server)
166   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
167          path gpath group-num)
168     (if (stringp id)
169         (when (and (setq group-num (nnml-find-group-number id))
170                    (cdr
171                     (assq (cdr group-num)
172                           (nnheader-article-to-file-alist
173                            (setq gpath
174                                  (nnmail-group-pathname
175                                   (car group-num)
176                                   nnml-directory))))))
177           (setq path (concat gpath (int-to-string (cdr group-num)))))
178       (setq path (nnml-article-to-file id)))
179     (cond 
180      ((not path)
181       (nnheader-report 'nnml "No such article: %s" id))
182      ((not (file-exists-p path))
183       (nnheader-report 'nnml "No such file: %s" path))
184      ((file-directory-p path)
185       (nnheader-report 'nnml "File is a directory: %s" path))
186      ((not (save-excursion (nnmail-find-file path)))
187       (nnheader-report 'nnml "Couldn't read file: %s" path))
188      (t
189       (nnheader-report 'nnml "Article %s retrieved" id)
190       ;; We return the article number.
191       (cons newsgroup (string-to-int (file-name-nondirectory path)))))))
192
193 (deffoo nnml-request-group (group &optional server dont-check)
194   (cond 
195    ((not (nnml-possibly-change-directory group server))
196     (nnheader-report 'nnml "Invalid group (no such directory)"))
197    ((not (file-exists-p nnml-current-directory))
198     (nnheader-report 'nnml "Directory %s does not exist"
199                      nnml-current-directory))
200    ((not (file-directory-p nnml-current-directory))
201     (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
202    (dont-check 
203     (nnheader-report 'nnml "Group %s selected" group)
204     t)
205    (t
206     (nnheader-re-read-dir nnml-current-directory)
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   (nnml-possibly-change-directory group server)
219   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
220
221 (deffoo nnml-close-group (group &optional server)
222   (setq nnml-article-file-alist nil)
223   t)
224
225 (deffoo nnml-request-create-group (group &optional server args)
226   (nnmail-activate 'nnml)
227   (unless (assoc group nnml-group-alist)
228     (let (active)
229       (push (list group (setq active (cons 1 0)))
230             nnml-group-alist)
231       (nnml-possibly-create-directory group)
232       (nnml-possibly-change-directory group server)
233       (let ((articles (nnheader-directory-articles nnml-current-directory)))
234         (when articles
235           (setcar active (apply 'min articles))
236           (setcdr active (apply 'max articles))))
237       (nnmail-save-active nnml-group-alist nnml-active-file)))
238   t)
239
240 (deffoo nnml-request-list (&optional server)
241   (save-excursion
242     (nnmail-find-file nnml-active-file)
243     (setq nnml-group-alist (nnmail-get-active))
244     t))
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          (setq result (car (nnml-save-mail 
330                             (list (cons group (nnml-active-number group))))))
331          (progn
332            (nnmail-save-active nnml-group-alist nnml-active-file)
333            (and last (nnml-save-nov))))
334       (and
335        (nnmail-activate 'nnml)
336        (setq result (car (nnml-save-mail
337                           (nnmail-article-group 'nnml-active-number))))
338        (progn
339          (nnmail-save-active nnml-group-alist nnml-active-file)
340          (and last (nnml-save-nov)))))
341     result))
342
343 (deffoo nnml-request-replace-article (article group buffer)
344   (nnml-possibly-change-directory group)
345   (save-excursion
346     (set-buffer buffer)
347     (nnml-possibly-create-directory group)
348     (let ((chars (nnmail-insert-lines))
349           (art (concat (int-to-string article) "\t"))
350           headers)
351       (when (condition-case ()
352                 (progn
353                   (nnmail-write-region 
354                    (point-min) (point-max)
355                    (concat nnml-current-directory
356                            (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         (when entry
441           (setcar entry new-name))
442         (setq nnml-current-directory nil
443               nnml-current-group nil)
444         ;; Save the new group alist.
445         (nnmail-save-active nnml-group-alist nnml-active-file)
446         t))))
447
448 (deffoo nnml-set-status (article name value &optional group server)
449   (nnml-possibly-change-directory group server)
450   (let ((file (nnml-article-to-file article)))
451     (cond
452      ((not (file-exists-p file))
453       (nnheader-report 'nnml "File %s does not exist" file))
454      (t
455       (nnheader-temp-write file
456         (nnheader-insert-file-contents file)
457         (nnmail-replace-status name value))
458       t))))
459
460 \f
461 ;;; Internal functions.
462
463 (defun nnml-article-to-file (article)
464   (unless nnml-article-file-alist
465     (setq nnml-article-file-alist
466           (nnheader-article-to-file-alist nnml-current-directory)))
467   (let (file)
468     (when (setq file (cdr (assq article nnml-article-file-alist)))
469       (concat nnml-current-directory file))))
470
471 (defun nnml-deletable-article-p (group article)
472   "Say whether ARTICLE in GROUP can be deleted."
473   (let (file path)
474     (when (setq file (cdr (assq article nnml-article-file-alist)))
475       (setq path (concat nnml-current-directory file))
476       (when (file-writable-p path)
477         (or (not nnmail-keep-last-article)
478             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
479                      article)))))))
480
481 ;; Find an article number in the current group given the Message-ID. 
482 (defun nnml-find-group-number (id)
483   (save-excursion
484     (set-buffer (get-buffer-create " *nnml id*"))
485     (buffer-disable-undo (current-buffer))
486     (let ((alist nnml-group-alist)
487           number)
488       ;; We want to look through all .overview files, but we want to
489       ;; start with the one in the current directory.  It seems most
490       ;; likely that the article we are looking for is in that group. 
491       (if (setq number (nnml-find-id nnml-current-group id))
492           (cons nnml-current-group number)
493         ;; It wasn't there, so we look through the other groups as well.
494         (while (and (not number)
495                     alist)
496           (or (string= (caar alist) nnml-current-group)
497               (setq number (nnml-find-id (caar alist) id)))
498           (or number
499               (setq alist (cdr alist))))
500         (and number
501              (cons (caar alist) number))))))
502
503 (defun nnml-find-id (group id)
504   (erase-buffer)
505   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
506                      nnml-nov-file-name))
507         number found)
508     (when (file-exists-p nov)
509       (nnheader-insert-file-contents nov)
510       (while (and (not found)
511                   (search-forward id nil t)) ; We find the ID.
512         ;; And the id is in the fourth field.
513         (if (not (and (search-backward "\t" nil t 4)
514                       (not (search-backward"\t" (gnus-point-at-bol) t))))
515             (forward-line 1)
516           (beginning-of-line)
517           (setq found t)
518           ;; We return the article number.
519           (setq number
520                 (condition-case ()
521                     (read (current-buffer))
522                   (error nil)))))
523       number)))
524
525 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
526   (if (or gnus-nov-is-evil nnml-nov-is-evil)
527       nil
528     (let ((first (car articles))
529           (last (progn (while (cdr articles) (setq articles (cdr articles)))
530                        (car articles)))
531           (nov (concat nnml-current-directory nnml-nov-file-name)))
532       (when (file-exists-p nov)
533         (save-excursion
534           (set-buffer nntp-server-buffer)
535           (erase-buffer)
536           (nnheader-insert-file-contents nov)
537           (if (and fetch-old
538                    (not (numberp fetch-old)))
539               t                         ; Don't remove anything.
540             (when fetch-old
541               (setq first (max 1 (- first fetch-old))))
542             (goto-char (point-min))
543             (while (and (not (eobp)) (> first (read (current-buffer))))
544               (forward-line 1))
545             (beginning-of-line)
546             (when (not (eobp))
547               (delete-region 1 (point)))
548             (while (and (not (eobp)) (>= last (read (current-buffer))))
549               (forward-line 1))
550             (beginning-of-line)
551             (when (not (eobp))
552               (delete-region (point) (point-max)))
553             t))))))
554
555 (defun nnml-possibly-change-directory (group &optional server)
556   (when (and server
557              (not (nnml-server-opened server)))
558     (nnml-open-server server))
559   (when group
560     (let ((pathname (nnmail-group-pathname group nnml-directory)))
561       (when (not (equal pathname nnml-current-directory))
562         (setq nnml-current-directory pathname
563               nnml-current-group group
564               nnml-article-file-alist nil))))
565   t)
566
567 (defun nnml-possibly-create-directory (group)
568   (let (dir dirs)
569     (setq dir (nnmail-group-pathname group nnml-directory))
570     (while (not (file-directory-p dir))
571       (push dir dirs)
572       (setq dir (file-name-directory (directory-file-name dir))))
573     (while dirs
574       (make-directory (directory-file-name (car dirs)))
575       (nnheader-message 5 "Creating mail directory %s" (car dirs))
576       (setq dirs (cdr dirs)))))
577              
578 (defun nnml-save-mail (group-art)
579   "Called narrowed to an article."
580   (let (chars headers)
581     (setq chars (nnmail-insert-lines))
582     (nnmail-insert-xref group-art)
583     (run-hooks 'nnmail-prepare-save-mail-hook)
584     (run-hooks 'nnml-prepare-save-mail-hook)
585     (goto-char (point-min))
586     (while (looking-at "From ")
587       (replace-match "X-From-Line: ")
588       (forward-line 1))
589     ;; We save the article in all the newsgroups it belongs in.
590     (let ((ga group-art)
591           first)
592       (while ga
593         (nnml-possibly-create-directory (caar ga))
594         (let ((file (concat (nnmail-group-pathname 
595                              (caar ga) nnml-directory)
596                             (int-to-string (cdar ga)))))
597           (if first
598               ;; It was already saved, so we just make a hard link.
599               (funcall nnmail-crosspost-link-function first file t)
600             ;; Save the article.
601             (nnmail-write-region (point-min) (point-max) file nil 
602                                  (if (nnheader-be-verbose 5) nil 'nomesg))
603             (setq first file)))
604         (setq ga (cdr ga))))
605     ;; Generate a nov line for this article.  We generate the nov
606     ;; line after saving, because nov generation destroys the
607     ;; header. 
608     (setq headers (nnml-parse-head chars))
609     ;; Output the nov line to all nov databases that should have it.
610     (let ((ga group-art))
611       (while ga
612         (nnml-add-nov (caar ga) (cdar ga) headers)
613         (setq ga (cdr ga))))
614     group-art))
615
616 (defun nnml-active-number (group)
617   "Compute the next article number in GROUP."
618   (let ((active (cadr (assoc group nnml-group-alist))))
619     ;; The group wasn't known to nnml, so we just create an active
620     ;; entry for it.   
621     (unless active
622       ;; Perhaps the active file was corrupt?  See whether
623       ;; there are any articles in this group.
624       (nnml-possibly-create-directory group)
625       (nnml-possibly-change-directory group)
626       (unless nnml-article-file-alist
627         (setq nnml-article-file-alist
628               (sort
629                (nnheader-article-to-file-alist nnml-current-directory)
630                (lambda (a1 a2) (< (car a1) (car a2))))))
631       (setq active
632             (if nnml-article-file-alist
633                 (cons (caar nnml-article-file-alist)
634                       (caar (last nnml-article-file-alist)))
635               (cons 1 0)))
636       (push (list group active) nnml-group-alist))
637     (setcdr active (1+ (cdr active)))
638     (while (file-exists-p
639             (concat (nnmail-group-pathname group nnml-directory)
640                     (int-to-string (cdr active))))
641       (setcdr active (1+ (cdr active))))
642     (cdr active)))
643
644 (defun nnml-add-nov (group article headers)
645   "Add a nov line for the GROUP base."
646   (save-excursion 
647     (set-buffer (nnml-open-nov group))
648     (goto-char (point-max))
649     (mail-header-set-number headers article)
650     (nnheader-insert-nov headers)))
651
652 (defsubst nnml-header-value ()
653   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
654
655 (defun nnml-parse-head (chars &optional number)
656   "Parse the head of the current buffer."
657   (save-excursion
658     (save-restriction
659       (goto-char (point-min))
660       (narrow-to-region 
661        (point)
662        (1- (or (search-forward "\n\n" nil t) (point-max))))
663       ;; Fold continuation lines.
664       (goto-char (point-min))
665       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
666         (replace-match " " t t))
667       ;; Remove any tabs; they are too confusing.
668       (subst-char-in-region (point-min) (point-max) ?\t ? )
669       (let ((headers (nnheader-parse-head t)))
670         (mail-header-set-chars headers chars)
671         (mail-header-set-number headers number)
672         headers))))
673
674 (defun nnml-open-nov (group)
675   (or (cdr (assoc group nnml-nov-buffer-alist))
676       (let ((buffer (nnheader-find-file-noselect 
677                      (concat (nnmail-group-pathname group nnml-directory)
678                              nnml-nov-file-name))))
679         (save-excursion
680           (set-buffer buffer)
681           (buffer-disable-undo (current-buffer)))
682         (push (cons group buffer) nnml-nov-buffer-alist)
683         buffer)))
684
685 (defun nnml-save-nov ()
686   (save-excursion
687     (while nnml-nov-buffer-alist
688       (when (buffer-name (cdar nnml-nov-buffer-alist))
689         (set-buffer (cdar nnml-nov-buffer-alist))
690         (when (buffer-modified-p)
691           (nnmail-write-region 1 (point-max) (buffer-file-name) nil 'nomesg))
692         (set-buffer-modified-p nil)
693         (kill-buffer (current-buffer)))
694       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
695
696 ;;;###autoload
697 (defun nnml-generate-nov-databases ()
698   "Generate nov databases in all nnml directories."
699   (interactive)
700   ;; Read the active file to make sure we don't re-use articles 
701   ;; numbers in empty groups.
702   (nnmail-activate 'nnml)
703   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
704   (setq nnml-directory (expand-file-name nnml-directory))
705   ;; Recurse down the directories.
706   (nnml-generate-nov-databases-1 nnml-directory)
707   ;; Save the active file.
708   (nnmail-save-active nnml-group-alist nnml-active-file))
709
710 (defun nnml-generate-nov-databases-1 (dir &optional seen)
711   (setq dir (file-name-as-directory dir))
712   ;; Only scan this sub-tree if we haven't been here yet.
713   (unless (member (file-truename dir) seen)
714     (push (file-truename dir) seen)
715     ;; We descend recursively
716     (let ((dirs (directory-files dir t nil t))
717           dir)
718       (while (setq dir (pop dirs))
719         (when (and (not (member (file-name-nondirectory dir) '("." "..")))
720                    (file-directory-p dir))
721           (nnml-generate-nov-databases-1 dir seen))))
722     ;; Do this directory.
723     (let ((files (sort
724                   (mapcar
725                    (lambda (name) (string-to-int name))
726                    (directory-files dir nil "^[0-9]+$" t))
727                   '<)))
728       (when files
729         (funcall nnml-generate-active-function dir)
730         ;; Generate the nov file.
731         (nnml-generate-nov-file dir files)))))
732
733 (defvar files)
734 (defun nnml-generate-active-info (dir)
735   ;; Update the active info for this group.
736   (let ((group (nnheader-file-to-group 
737                 (directory-file-name dir) nnml-directory)))
738     (setq nnml-group-alist
739           (delq (assoc group nnml-group-alist) nnml-group-alist))
740     (push (list group
741                 (cons (car files)
742                       (let ((f files))
743                         (while (cdr f) (setq f (cdr f)))
744                         (car f))))
745           nnml-group-alist)))
746
747 (defun nnml-generate-nov-file (dir files)
748   (let* ((dir (file-name-as-directory dir))
749          (nov (concat dir nnml-nov-file-name))
750          (nov-buffer (get-buffer-create " *nov*"))
751          chars file headers)
752     (save-excursion
753       ;; Init the nov buffer.
754       (set-buffer nov-buffer)
755       (buffer-disable-undo (current-buffer))
756       (erase-buffer)
757       (set-buffer nntp-server-buffer)
758       ;; Delete the old NOV file.
759       (when (file-exists-p nov)
760         (funcall nnmail-delete-file-function nov))
761       (while files
762         (unless (file-directory-p 
763                  (setq file (concat dir (int-to-string (car files)))))
764           (erase-buffer)
765           (nnheader-insert-file-contents file)
766           (narrow-to-region 
767            (goto-char (point-min))
768            (progn
769              (search-forward "\n\n" nil t)
770              (setq chars (- (point-max) (point)))
771              (max 1 (1- (point)))))
772           (when (and (not (= 0 chars))  ; none of them empty files...
773                      (not (= (point-min) (point-max))))
774             (goto-char (point-min))
775             (setq headers (nnml-parse-head chars (car files)))
776             (save-excursion
777               (set-buffer nov-buffer)
778               (goto-char (point-max))
779               (nnheader-insert-nov headers)))
780           (widen))
781         (setq files (cdr files)))
782       (save-excursion
783         (set-buffer nov-buffer)
784         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
785         (kill-buffer (current-buffer))))))
786
787 (defun nnml-nov-delete-article (group article)
788   (save-excursion
789     (set-buffer (nnml-open-nov group))
790     (goto-char (point-min))
791     (when (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
792       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
793     t))
794
795 (provide 'nnml)
796
797 ;;; nnml.el ends here