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