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