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