Sync with latest Gnus.
[elisp/gnus.git-] / 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 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
36
37 (require 'nnheader)
38 (require 'nnmail)
39 (require 'nnoo)
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             (pathname-coding-system 'binary)
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          (pathname-coding-system 'binary)
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 ((pathname-coding-system 'binary))
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           (pathname-coding-system 'binary))
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 article group server
295                                           (current-buffer))
296                     (nnmail-expiry-target-group
297                      nnmail-expiry-target group)))
298                 (nnheader-message 5 "Deleting article %s in %s"
299                                   article group)
300                 (condition-case ()
301                     (funcall nnmail-delete-file-function article)
302                   (file-error
303                    (push number rest)))
304                 (setq active-articles (delq number active-articles))
305                 (nnml-nov-delete-article group number))
306             (push number rest)))))
307     (let ((active (nth 1 (assoc group nnml-group-alist))))
308       (when active
309         (setcar active (or (and active-articles
310                                 (apply 'min active-articles))
311                            (1+ (cdr active)))))
312       (nnmail-save-active nnml-group-alist nnml-active-file))
313     (nnml-save-nov)
314     (nconc rest articles)))
315
316 (deffoo nnml-request-move-article
317     (article group server accept-form &optional last)
318   (let ((buf (get-buffer-create " *nnml move*"))
319         result)
320     (nnml-possibly-change-directory group server)
321     (nnml-update-file-alist)
322     (and
323      (nnml-deletable-article-p group article)
324      (nnml-request-article article group server)
325      (let (nnml-current-directory 
326            nnml-current-group 
327            nnml-article-file-alist)
328        (save-excursion
329          (set-buffer buf)
330          (insert-buffer-substring nntp-server-buffer)
331          (setq result (eval accept-form))
332          (kill-buffer (current-buffer))
333          result))
334      (progn
335        (nnml-possibly-change-directory group server)
336        (condition-case ()
337            (funcall nnmail-delete-file-function
338                     (nnml-article-to-file  article))
339          (file-error nil))
340        (nnml-nov-delete-article group article)
341        (when last
342          (nnml-save-nov)
343          (nnmail-save-active nnml-group-alist nnml-active-file))))
344     result))
345
346 (deffoo nnml-request-accept-article (group &optional server last)
347   (nnml-possibly-change-directory group server)
348   (nnmail-check-syntax)
349   (let (result)
350     (when nnmail-cache-accepted-message-ids
351       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
352     (if (stringp group)
353         (and
354          (nnmail-activate 'nnml)
355          (setq result (car (nnml-save-mail
356                             (list (cons group (nnml-active-number group))))))
357          (progn
358            (nnmail-save-active nnml-group-alist nnml-active-file)
359            (and last (nnml-save-nov))))
360       (and
361        (nnmail-activate 'nnml)
362        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
363                 (yes-or-no-p "Moved to `junk' group; delete article? "))
364            (setq result 'junk)
365          (setq result (car (nnml-save-mail result))))
366        (when last
367          (nnmail-save-active nnml-group-alist nnml-active-file)
368          (when nnmail-cache-accepted-message-ids
369            (nnmail-cache-close))
370          (nnml-save-nov))))
371     result))
372
373 (deffoo nnml-request-replace-article (article group buffer)
374   (nnml-possibly-change-directory group)
375   (save-excursion
376     (set-buffer buffer)
377     (nnml-possibly-create-directory group)
378     (let ((chars (nnmail-insert-lines))
379           (art (concat (int-to-string article) "\t"))
380           headers)
381       (when (ignore-errors
382               (nnmail-write-region
383                (point-min) (point-max)
384                (or (nnml-article-to-file article)
385                    (expand-file-name (int-to-string article)
386                                      nnml-current-directory))
387                nil (if (nnheader-be-verbose 5) nil 'nomesg))
388               t)
389         (setq headers (nnml-parse-head chars article))
390         ;; Replace the NOV line in the NOV file.
391         (save-excursion
392           (set-buffer (nnml-open-nov group))
393           (goto-char (point-min))
394           (if (or (looking-at art)
395                   (search-forward (concat "\n" art) nil t))
396               ;; Delete the old NOV line.
397               (delete-region (progn (beginning-of-line) (point))
398                              (progn (forward-line 1) (point)))
399             ;; The line isn't here, so we have to find out where
400             ;; we should insert it.  (This situation should never
401             ;; occur, but one likes to make sure...)
402             (while (and (looking-at "[0-9]+\t")
403                         (< (string-to-int
404                             (buffer-substring
405                              (match-beginning 0) (match-end 0)))
406                            article)
407                         (zerop (forward-line 1)))))
408           (beginning-of-line)
409           (nnheader-insert-nov headers)
410           (nnml-save-nov)
411           t)))))
412
413 (deffoo nnml-request-delete-group (group &optional force server)
414   (nnml-possibly-change-directory group server)
415   (when force
416     ;; Delete all articles in GROUP.
417     (let ((articles
418            (directory-files
419             nnml-current-directory t
420             (concat nnheader-numerical-short-files
421                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
422           article)
423       (while articles
424         (setq article (pop articles))
425         (when (file-writable-p article)
426           (nnheader-message 5 "Deleting article %s in %s..." article group)
427           (funcall nnmail-delete-file-function article))))
428     ;; Try to delete the directory itself.
429     (ignore-errors (delete-directory nnml-current-directory)))
430   ;; Remove the group from all structures.
431   (setq nnml-group-alist
432         (delq (assoc group nnml-group-alist) nnml-group-alist)
433         nnml-current-group nil
434         nnml-current-directory nil)
435   ;; Save the active file.
436   (nnmail-save-active nnml-group-alist nnml-active-file)
437   t)
438
439 (deffoo nnml-request-rename-group (group new-name &optional server)
440   (nnml-possibly-change-directory group server)
441   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
442         (old-dir (nnmail-group-pathname group nnml-directory)))
443     (when (ignore-errors
444             (make-directory new-dir t)
445             t)
446       ;; We move the articles file by file instead of renaming
447       ;; the directory -- there may be subgroups in this group.
448       ;; One might be more clever, I guess.
449       (let ((files (nnheader-article-to-file-alist old-dir)))
450         (while files
451           (rename-file
452            (concat old-dir (cdar files))
453            (concat new-dir (cdar files)))
454           (pop files)))
455       ;; Move .overview file.
456       (let ((overview (concat old-dir nnml-nov-file-name)))
457         (when (file-exists-p overview)
458           (rename-file overview (concat new-dir nnml-nov-file-name))))
459       (when (<= (length (directory-files old-dir)) 2)
460         (ignore-errors (delete-directory old-dir)))
461       ;; That went ok, so we change the internal structures.
462       (let ((entry (assoc group nnml-group-alist)))
463         (when entry
464           (setcar entry new-name))
465         (setq nnml-current-directory nil
466               nnml-current-group nil)
467         ;; Save the new group alist.
468         (nnmail-save-active nnml-group-alist nnml-active-file)
469         t))))
470
471 (deffoo nnml-set-status (article name value &optional group server)
472   (nnml-possibly-change-directory group server)
473   (let ((file (nnml-article-to-file article)))
474     (cond
475      ((not (file-exists-p file))
476       (nnheader-report 'nnml "File %s does not exist" file))
477      (t
478       (with-temp-file file
479         (nnheader-insert-file-contents file)
480         (nnmail-replace-status name value))
481       t))))
482
483 \f
484 ;;; Internal functions.
485
486 (defun nnml-article-to-file (article)
487   (nnml-update-file-alist)
488   (let (file)
489     (if (setq file (cdr (assq article nnml-article-file-alist)))
490         (expand-file-name file nnml-current-directory)
491       ;; Just to make sure nothing went wrong when reading over NFS --
492       ;; check once more.
493       (when (file-exists-p
494              (setq file (expand-file-name (number-to-string article)
495                                           nnml-current-directory)))
496         (nnml-update-file-alist t)
497         file))))
498
499 (defun nnml-deletable-article-p (group article)
500   "Say whether ARTICLE in GROUP can be deleted."
501   (let (path)
502     (when (setq path (nnml-article-to-file article))
503       (when (file-writable-p path)
504         (or (not nnmail-keep-last-article)
505             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
506                      article)))))))
507
508 ;; Find an article number in the current group given the Message-ID.
509 (defun nnml-find-group-number (id)
510   (save-excursion
511     (set-buffer (get-buffer-create " *nnml id*"))
512     (let ((alist nnml-group-alist)
513           number)
514       ;; We want to look through all .overview files, but we want to
515       ;; start with the one in the current directory.  It seems most
516       ;; likely that the article we are looking for is in that group.
517       (if (setq number (nnml-find-id nnml-current-group id))
518           (cons nnml-current-group number)
519         ;; It wasn't there, so we look through the other groups as well.
520         (while (and (not number)
521                     alist)
522           (or (string= (caar alist) nnml-current-group)
523               (setq number (nnml-find-id (caar alist) id)))
524           (or number
525               (setq alist (cdr alist))))
526         (and number
527              (cons (caar alist) number))))))
528
529 (defun nnml-find-id (group id)
530   (erase-buffer)
531   (let ((nov (expand-file-name nnml-nov-file-name
532                                (nnmail-group-pathname group nnml-directory)))
533         number found)
534     (when (file-exists-p nov)
535       (nnheader-insert-file-contents nov)
536       (while (and (not found)
537                   (search-forward id nil t)) ; We find the ID.
538         ;; And the id is in the fourth field.
539         (if (not (and (search-backward "\t" nil t 4)
540                       (not (search-backward"\t" (gnus-point-at-bol) t))))
541             (forward-line 1)
542           (beginning-of-line)
543           (setq found t)
544           ;; We return the article number.
545           (setq number
546                 (ignore-errors (read (current-buffer))))))
547       number)))
548
549 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
550   (if (or gnus-nov-is-evil nnml-nov-is-evil)
551       nil
552     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
553       (when (file-exists-p nov)
554         (save-excursion
555           (set-buffer nntp-server-buffer)
556           (erase-buffer)
557           (nnheader-insert-file-contents nov)
558           (if (and fetch-old
559                    (not (numberp fetch-old)))
560               t                         ; Don't remove anything.
561             (nnheader-nov-delete-outside-range
562              (if fetch-old (max 1 (- (car articles) fetch-old))
563                (car articles))
564              (car (last articles)))
565             t))))))
566
567 (defun nnml-possibly-change-directory (group &optional server)
568   (when (and server
569              (not (nnml-server-opened server)))
570     (nnml-open-server server))
571   (if (not group)
572       t
573     (let ((pathname (nnmail-group-pathname group nnml-directory))
574           (pathname-coding-system 'binary))
575       (when (not (equal pathname nnml-current-directory))
576         (setq nnml-current-directory pathname
577               nnml-current-group group
578               nnml-article-file-alist nil))
579       (file-exists-p nnml-current-directory))))
580
581 (defun nnml-possibly-create-directory (group)
582   (let ((dir (nnmail-group-pathname group nnml-directory)))
583     (unless (file-exists-p dir)
584       (make-directory (directory-file-name dir) t)
585       (nnheader-message 5 "Creating mail directory %s" dir))))
586
587 (defun nnml-save-mail (group-art)
588   "Called narrowed to an article."
589   (let (chars headers)
590     (setq chars (nnmail-insert-lines))
591     (nnmail-insert-xref group-art)
592     (run-hooks 'nnmail-prepare-save-mail-hook)
593     (run-hooks 'nnml-prepare-save-mail-hook)
594     (goto-char (point-min))
595     (while (looking-at "From ")
596       (replace-match "X-From-Line: ")
597       (forward-line 1))
598     ;; We save the article in all the groups it belongs in.
599     (let ((ga group-art)
600           first)
601       (while ga
602         (nnml-possibly-create-directory (caar ga))
603         (let ((file (concat (nnmail-group-pathname
604                              (caar ga) nnml-directory)
605                             (int-to-string (cdar ga)))))
606           (if first
607               ;; It was already saved, so we just make a hard link.
608               (funcall nnmail-crosspost-link-function first file t)
609             ;; Save the article.
610             (nnmail-write-region (point-min) (point-max) file nil
611                                  (if (nnheader-be-verbose 5) nil 'nomesg))
612             (setq first file)))
613         (setq ga (cdr ga))))
614     ;; Generate a nov line for this article.  We generate the nov
615     ;; line after saving, because nov generation destroys the
616     ;; header.
617     (setq headers (nnml-parse-head chars))
618     ;; Output the nov line to all nov databases that should have it.
619     (let ((ga group-art))
620       (while ga
621         (nnml-add-nov (caar ga) (cdar ga) headers)
622         (setq ga (cdr ga))))
623     group-art))
624
625 (defun nnml-active-number (group)
626   "Compute the next article number in GROUP."
627   (let ((active (cadr (assoc group nnml-group-alist))))
628     ;; The group wasn't known to nnml, so we just create an active
629     ;; entry for it.
630     (unless active
631       ;; Perhaps the active file was corrupt?  See whether
632       ;; there are any articles in this group.
633       (nnml-possibly-create-directory group)
634       (nnml-possibly-change-directory group)
635       (unless nnml-article-file-alist
636         (setq nnml-article-file-alist
637               (sort
638                (nnheader-article-to-file-alist nnml-current-directory)
639                'car-less-than-car)))
640       (setq active
641             (if nnml-article-file-alist
642                 (cons (caar nnml-article-file-alist)
643                       (caar (last nnml-article-file-alist)))
644               (cons 1 0)))
645       (push (list group active) nnml-group-alist))
646     (setcdr active (1+ (cdr active)))
647     (while (file-exists-p
648             (expand-file-name (int-to-string (cdr active))
649                               (nnmail-group-pathname group nnml-directory)))
650       (setcdr active (1+ (cdr active))))
651     (cdr active)))
652
653 (defun nnml-add-nov (group article headers)
654   "Add a nov line for the GROUP base."
655   (save-excursion
656     (set-buffer (nnml-open-nov group))
657     (goto-char (point-max))
658     (mail-header-set-number headers article)
659     (nnheader-insert-nov headers)))
660
661 (defsubst nnml-header-value ()
662   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
663
664 (defun nnml-parse-head (chars &optional number)
665   "Parse the head of the current buffer."
666   (save-excursion
667     (save-restriction
668       (unless (zerop (buffer-size))
669         (narrow-to-region
670          (goto-char (point-min))
671          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
672       ;; Fold continuation lines.
673       (goto-char (point-min))
674       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
675         (replace-match " " t t))
676       ;; Remove any tabs; they are too confusing.
677       (subst-char-in-region (point-min) (point-max) ?\t ? )
678       (let ((headers (nnheader-parse-head t)))
679         (mail-header-set-chars headers chars)
680         (mail-header-set-number headers number)
681         headers))))
682
683 (defun nnml-open-nov (group)
684   (or (cdr (assoc group nnml-nov-buffer-alist))
685       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
686         (save-excursion
687           (set-buffer buffer)
688           (set (make-local-variable 'nnml-nov-buffer-file-name)
689                (expand-file-name
690                 nnml-nov-file-name
691                 (nnmail-group-pathname group nnml-directory)))
692           (erase-buffer)
693           (when (file-exists-p nnml-nov-buffer-file-name)
694             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
695         (push (cons group buffer) nnml-nov-buffer-alist)
696         buffer)))
697
698 (defun nnml-save-nov ()
699   (save-excursion
700     (while nnml-nov-buffer-alist
701       (when (buffer-name (cdar nnml-nov-buffer-alist))
702         (set-buffer (cdar nnml-nov-buffer-alist))
703         (when (buffer-modified-p)
704           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
705                                nil 'nomesg))
706         (set-buffer-modified-p nil)
707         (kill-buffer (current-buffer)))
708       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
709
710 ;;;###autoload
711 (defun nnml-generate-nov-databases ()
712   "Generate NOV databases in all nnml directories."
713   (interactive)
714   ;; Read the active file to make sure we don't re-use articles
715   ;; numbers in empty groups.
716   (nnmail-activate 'nnml)
717   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
718   (setq nnml-directory (expand-file-name nnml-directory))
719   ;; Recurse down the directories.
720   (nnml-generate-nov-databases-1 nnml-directory nil t)
721   ;; Save the active file.
722   (nnmail-save-active nnml-group-alist nnml-active-file))
723
724 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
725   "Regenerate the NOV database in DIR."
726   (interactive "DRegenerate NOV in: ")
727   (setq dir (file-name-as-directory dir))
728   ;; Only scan this sub-tree if we haven't been here yet.
729   (unless (member (file-truename dir) seen)
730     (push (file-truename dir) seen)
731     ;; We descend recursively
732     (let ((dirs (directory-files dir t nil t))
733           dir)
734       (while (setq dir (pop dirs))
735         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
736                    (file-directory-p dir))
737           (nnml-generate-nov-databases-1 dir seen))))
738     ;; Do this directory.
739     (let ((files (sort (nnheader-article-to-file-alist dir)
740                        'car-less-than-car)))
741       (if (not files)
742           (let* ((group (nnheader-file-to-group
743                          (directory-file-name dir) nnml-directory))
744                  (info (cadr (assoc group nnml-group-alist))))
745             (when info
746               (setcar info (1+ (cdr info)))))
747         (funcall nnml-generate-active-function dir)
748         ;; Generate the nov file.
749         (nnml-generate-nov-file dir files)
750         (unless no-active
751           (nnmail-save-active nnml-group-alist nnml-active-file))))))
752
753 (defvar files)
754 (defun nnml-generate-active-info (dir)
755   ;; Update the active info for this group.
756   (let ((group (nnheader-file-to-group
757                 (directory-file-name dir) nnml-directory)))
758     (setq nnml-group-alist
759           (delq (assoc group nnml-group-alist) nnml-group-alist))
760     (push (list group
761                 (cons (caar files)
762                       (let ((f files))
763                         (while (cdr f) (setq f (cdr f)))
764                         (caar f))))
765           nnml-group-alist)))
766
767 (defun nnml-generate-nov-file (dir files)
768   (let* ((dir (file-name-as-directory dir))
769          (nov (concat dir nnml-nov-file-name))
770          (nov-buffer (get-buffer-create " *nov*"))
771          chars file headers)
772     (save-excursion
773       ;; Init the nov buffer.
774       (set-buffer nov-buffer)
775       (buffer-disable-undo)
776       (erase-buffer)
777       (set-buffer nntp-server-buffer)
778       ;; Delete the old NOV file.
779       (when (file-exists-p nov)
780         (funcall nnmail-delete-file-function nov))
781       (while files
782         (unless (file-directory-p (setq file (concat dir (cdar files))))
783           (erase-buffer)
784           (nnheader-insert-file-contents file)
785           (narrow-to-region
786            (goto-char (point-min))
787            (progn
788              (search-forward "\n\n" nil t)
789              (setq chars (- (point-max) (point)))
790              (max 1 (1- (point)))))
791           (unless (zerop (buffer-size))
792             (goto-char (point-min))
793             (setq headers (nnml-parse-head chars (caar files)))
794             (save-excursion
795               (set-buffer nov-buffer)
796               (goto-char (point-max))
797               (nnheader-insert-nov headers)))
798           (widen))
799         (setq files (cdr files)))
800       (save-excursion
801         (set-buffer nov-buffer)
802         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
803         (kill-buffer (current-buffer))))))
804
805 (defun nnml-nov-delete-article (group article)
806   (save-excursion
807     (set-buffer (nnml-open-nov group))
808     (when (nnheader-find-nov-line article)
809       (delete-region (point) (progn (forward-line 1) (point)))
810       (when (bobp)
811         (let ((active (cadr (assoc group nnml-group-alist)))
812               num)
813           (when active
814             (if (eobp)
815                 (setf (car active) (1+ (cdr active)))
816               (when (and (setq num (ignore-errors (read (current-buffer))))
817                          (numberp num))
818                 (setf (car active) num)))))))
819     t))
820
821 (defun nnml-update-file-alist (&optional force)
822   (when (or (not nnml-article-file-alist)
823             force)
824     (setq nnml-article-file-alist
825           (nnheader-article-to-file-alist nnml-current-directory))))
826
827 (provide 'nnml)
828
829 ;;; nnml.el ends here