*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41   "Spool directory for the nnml mail backend.")
42
43 (defvoo nnml-active-file
44   (concat (file-name-as-directory nnml-directory) "active")
45   "Mail active file.")
46
47 (defvoo nnml-newsgroups-file
48   (concat (file-name-as-directory nnml-directory) "newsgroups")
49   "Mail newsgroups description file.")
50
51 (defvoo nnml-get-new-mail t
52   "If non-nil, nnml will check the incoming mail file and split the mail.")
53
54 (defvoo nnml-nov-is-evil nil
55   "If non-nil, Gnus will never generate and use nov databases for mail groups.
56 Using nov databases will speed up header fetching considerably.
57 This variable shouldn't be flipped much.  If you have, for some reason,
58 set this to t, and want to set it to nil again, you should always run
59 the `nnml-generate-nov-databases' command.  The function will go
60 through all nnml directories and generate nov databases for them
61 all.  This may very well take some time.")
62
63 (defvoo nnml-prepare-save-mail-hook nil
64   "Hook run narrowed to an article before saving.")
65
66 (defvoo nnml-inhibit-expiry nil
67   "If non-nil, inhibit expiry.")
68
69
70 \f
71
72 (defconst nnml-version "nnml 1.0"
73   "nnml version.")
74
75 (defvoo nnml-nov-file-name ".overview")
76
77 (defvoo nnml-current-directory nil)
78 (defvoo nnml-current-group nil)
79 (defvoo nnml-status-string "")
80 (defvoo nnml-nov-buffer-alist nil)
81 (defvoo nnml-group-alist nil)
82 (defvoo nnml-active-timestamp nil)
83 (defvoo nnml-article-file-alist nil)
84
85 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
86
87 (defvar nnml-nov-buffer-file-name nil)
88
89 \f
90
91 ;;; Interface functions.
92
93 (nnoo-define-basics nnml)
94
95 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
96   (when (nnml-possibly-change-directory group server)
97     (save-excursion
98       (set-buffer nntp-server-buffer)
99       (erase-buffer)
100       (let ((file nil)
101             (number (length sequence))
102             (count 0)
103             (pathname-coding-system 'binary)
104             beg article)
105         (if (stringp (car sequence))
106             'headers
107           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
108               'nov
109             (while sequence
110               (setq article (car sequence))
111               (setq file (nnml-article-to-file article))
112               (when (and file
113                          (file-exists-p file)
114                          (not (file-directory-p file)))
115                 (insert (format "221 %d Article retrieved.\n" article))
116                 (setq beg (point))
117                 (nnheader-insert-head file)
118                 (goto-char beg)
119                 (if (search-forward "\n\n" nil t)
120                     (forward-char -1)
121                   (goto-char (point-max))
122                   (insert "\n\n"))
123                 (insert ".\n")
124                 (delete-region (point) (point-max)))
125               (setq sequence (cdr sequence))
126               (setq count (1+ count))
127               (and (numberp nnmail-large-newsgroup)
128                    (> number nnmail-large-newsgroup)
129                    (zerop (% count 20))
130                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
131                                      (/ (* count 100) number))))
132
133             (and (numberp nnmail-large-newsgroup)
134                  (> number nnmail-large-newsgroup)
135                  (nnheader-message 6 "nnml: Receiving headers...done"))
136
137             (nnheader-fold-continuation-lines)
138             'headers))))))
139
140 (deffoo nnml-open-server (server &optional defs)
141   (nnoo-change-server 'nnml server defs)
142   (when (not (file-exists-p nnml-directory))
143     (condition-case ()
144         (make-directory nnml-directory t)
145       (error)))
146   (cond
147    ((not (file-exists-p nnml-directory))
148     (nnml-close-server)
149     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
150    ((not (file-directory-p (file-truename nnml-directory)))
151     (nnml-close-server)
152     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
153    (t
154     (nnheader-report 'nnml "Opened server %s using directory %s"
155                      server nnml-directory)
156     t)))
157
158 (defun nnml-request-regenerate (server)
159   (nnml-possibly-change-directory nil server)
160   (nnml-generate-nov-databases)
161   t)
162
163 (deffoo nnml-request-article (id &optional group server buffer)
164   (nnml-possibly-change-directory group server)
165   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
166          (pathname-coding-system 'binary)
167          path gpath group-num)
168     (if (stringp id)
169         (when (and (setq group-num (nnml-find-group-number id))
170                    (cdr
171                     (assq (cdr group-num)
172                           (nnheader-article-to-file-alist
173                            (setq gpath
174                                  (nnmail-group-pathname
175                                   (car group-num)
176                                   nnml-directory))))))
177           (setq path (concat gpath (int-to-string (cdr group-num)))))
178       (setq path (nnml-article-to-file id)))
179     (cond
180      ((not path)
181       (nnheader-report 'nnml "No such article: %s" id))
182      ((not (file-exists-p path))
183       (nnheader-report 'nnml "No such file: %s" path))
184      ((file-directory-p path)
185       (nnheader-report 'nnml "File is a directory: %s" path))
186      ((not (save-excursion (nnmail-find-file path)))
187       (nnheader-report 'nnml "Couldn't read file: %s" path))
188      (t
189       (nnheader-report 'nnml "Article %s retrieved" id)
190       ;; We return the article number.
191       (cons (if group-num (car group-num) group)
192             (string-to-int (file-name-nondirectory path)))))))
193
194 (deffoo nnml-request-group (group &optional server dont-check)
195   (let ((pathname-coding-system 'binary))
196     (cond
197      ((not (nnml-possibly-change-directory group server))
198       (nnheader-report 'nnml "Invalid group (no such directory)"))
199      ((not (file-exists-p nnml-current-directory))
200       (nnheader-report 'nnml "Directory %s does not exist"
201                        nnml-current-directory))
202      ((not (file-directory-p nnml-current-directory))
203       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
204      (dont-check
205       (nnheader-report 'nnml "Group %s selected" group)
206       t)
207      (t
208       (nnheader-re-read-dir nnml-current-directory)
209       (nnmail-activate 'nnml)
210       (let ((active (nth 1 (assoc group nnml-group-alist))))
211         (if (not active)
212             (nnheader-report 'nnml "No such group: %s" group)
213           (nnheader-report 'nnml "Selected group %s" group)
214           (nnheader-insert "211 %d %d %d %s\n"
215                            (max (1+ (- (cdr active) (car active))) 0)
216                            (car active) (cdr active) group)))))))
217
218 (deffoo nnml-request-scan (&optional group server)
219   (setq nnml-article-file-alist nil)
220   (nnml-possibly-change-directory group server)
221   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
222
223 (deffoo nnml-close-group (group &optional server)
224   (setq nnml-article-file-alist nil)
225   t)
226
227 (deffoo nnml-request-create-group (group &optional server args)
228   (nnmail-activate 'nnml)
229   (cond
230    ((assoc group nnml-group-alist)
231     t)
232    ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
233          (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
234     (nnheader-report 'nnml "%s is a file"
235                      (nnmail-group-pathname group nnml-directory)))
236    (t
237     (let (active)
238       (push (list group (setq active (cons 1 0)))
239             nnml-group-alist)
240       (nnml-possibly-create-directory group)
241       (nnml-possibly-change-directory group server)
242       (let ((articles (nnheader-directory-articles nnml-current-directory)))
243         (when articles
244           (setcar active (apply 'min articles))
245           (setcdr active (apply 'max articles))))
246       (nnmail-save-active nnml-group-alist nnml-active-file)
247       t))))
248
249 (deffoo nnml-request-list (&optional server)
250   (save-excursion
251     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
252           (pathname-coding-system 'binary)) 
253       (nnmail-find-file nnml-active-file))
254     (setq nnml-group-alist (nnmail-get-active))
255     t))
256
257 (deffoo nnml-request-newgroups (date &optional server)
258   (nnml-request-list server))
259
260 (deffoo nnml-request-list-newsgroups (&optional server)
261   (save-excursion
262     (nnmail-find-file nnml-newsgroups-file)))
263
264 (deffoo nnml-request-expire-articles (articles group
265                                                &optional server force)
266   (nnml-possibly-change-directory group server)
267   (let ((active-articles
268          (nnheader-directory-articles nnml-current-directory))
269         (is-old t)
270         article rest mod-time number)
271     (nnmail-activate 'nnml)
272
273     (setq active-articles (sort active-articles '<))
274     ;; Articles not listed in active-articles are already gone,
275     ;; so don't try to expire them.
276     (setq articles (gnus-sorted-intersection articles active-articles))
277
278     (while (and articles is-old)
279       (when (setq article (nnml-article-to-file (setq number (pop articles))))
280         (when (setq mod-time (nth 5 (file-attributes article)))
281           (if (and (nnml-deletable-article-p group number)
282                    (setq is-old
283                          (nnmail-expired-article-p group mod-time force
284                                                    nnml-inhibit-expiry)))
285               (progn
286                 (nnheader-message 5 "Deleting article %s in %s"
287                                   article group)
288                 (condition-case ()
289                     (funcall nnmail-delete-file-function article)
290                   (file-error
291                    (push number rest)))
292                 (setq active-articles (delq number active-articles))
293                 (nnml-nov-delete-article group number))
294             (push number rest)))))
295     (let ((active (nth 1 (assoc group nnml-group-alist))))
296       (when active
297         (setcar active (or (and active-articles
298                                 (apply 'min active-articles))
299                            (1+ (cdr active)))))
300       (nnmail-save-active nnml-group-alist nnml-active-file))
301     (nnml-save-nov)
302     (nconc rest articles)))
303
304 (deffoo nnml-request-move-article
305   (article group server accept-form &optional last)
306   (let ((buf (get-buffer-create " *nnml move*"))
307         result)
308     (nnml-possibly-change-directory group server)
309     (nnml-update-file-alist)
310     (and
311      (nnml-deletable-article-p group article)
312      (nnml-request-article article group server)
313      (save-excursion
314        (set-buffer buf)
315        (insert-buffer-substring nntp-server-buffer)
316        (setq result (eval accept-form))
317        (kill-buffer (current-buffer))
318        result)
319      (progn
320        (nnml-possibly-change-directory group server)
321        (condition-case ()
322            (funcall nnmail-delete-file-function
323                     (nnml-article-to-file  article))
324          (file-error nil))
325        (nnml-nov-delete-article group article)
326        (when last
327          (nnml-save-nov)
328          (nnmail-save-active nnml-group-alist nnml-active-file))))
329     result))
330
331 (deffoo nnml-request-accept-article (group &optional server last)
332   (nnml-possibly-change-directory group server)
333   (nnmail-check-syntax)
334   (let (result)
335     (when nnmail-cache-accepted-message-ids
336       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
337     (if (stringp group)
338         (and
339          (nnmail-activate 'nnml)
340          (setq result (car (nnml-save-mail
341                             (list (cons group (nnml-active-number group))))))
342          (progn
343            (nnmail-save-active nnml-group-alist nnml-active-file)
344            (and last (nnml-save-nov))))
345       (and
346        (nnmail-activate 'nnml)
347        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
348                 (yes-or-no-p "Moved to `junk' group; delete article? "))
349            (setq result 'junk)
350          (setq result (car (nnml-save-mail result))))
351        (when last
352          (nnmail-save-active nnml-group-alist nnml-active-file)
353          (when nnmail-cache-accepted-message-ids
354            (nnmail-cache-close))
355          (nnml-save-nov))))
356     result))
357
358 (deffoo nnml-request-replace-article (article group buffer)
359   (nnml-possibly-change-directory group)
360   (save-excursion
361     (set-buffer buffer)
362     (nnml-possibly-create-directory group)
363     (let ((chars (nnmail-insert-lines))
364           (art (concat (int-to-string article) "\t"))
365           headers)
366       (when (condition-case ()
367                 (progn
368                   (nnmail-write-region
369                    (point-min) (point-max)
370                    (or (nnml-article-to-file article)
371                        (concat nnml-current-directory
372                                (int-to-string article)))
373                    nil (if (nnheader-be-verbose 5) nil 'nomesg))
374                   t)
375               (error nil))
376         (setq headers (nnml-parse-head chars article))
377         ;; Replace the NOV line in the NOV file.
378         (save-excursion
379           (set-buffer (nnml-open-nov group))
380           (goto-char (point-min))
381           (if (or (looking-at art)
382                   (search-forward (concat "\n" art) nil t))
383               ;; Delete the old NOV line.
384               (delete-region (progn (beginning-of-line) (point))
385                              (progn (forward-line 1) (point)))
386             ;; The line isn't here, so we have to find out where
387             ;; we should insert it.  (This situation should never
388             ;; occur, but one likes to make sure...)
389             (while (and (looking-at "[0-9]+\t")
390                         (< (string-to-int
391                             (buffer-substring
392                              (match-beginning 0) (match-end 0)))
393                            article)
394                         (zerop (forward-line 1)))))
395           (beginning-of-line)
396           (nnheader-insert-nov headers)
397           (nnml-save-nov)
398           t)))))
399
400 (deffoo nnml-request-delete-group (group &optional force server)
401   (nnml-possibly-change-directory group server)
402   (when force
403     ;; Delete all articles in GROUP.
404     (let ((articles
405            (directory-files
406             nnml-current-directory t
407             (concat nnheader-numerical-short-files
408                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
409           article)
410       (while articles
411         (setq article (pop articles))
412         (when (file-writable-p article)
413           (nnheader-message 5 "Deleting article %s in %s..." article group)
414           (funcall nnmail-delete-file-function article))))
415     ;; Try to delete the directory itself.
416     (condition-case ()
417         (delete-directory nnml-current-directory)
418       (error nil)))
419   ;; Remove the group from all structures.
420   (setq nnml-group-alist
421         (delq (assoc group nnml-group-alist) nnml-group-alist)
422         nnml-current-group nil
423         nnml-current-directory nil)
424   ;; Save the active file.
425   (nnmail-save-active nnml-group-alist nnml-active-file)
426   t)
427
428 (deffoo nnml-request-rename-group (group new-name &optional server)
429   (nnml-possibly-change-directory group server)
430   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
431         (old-dir (nnmail-group-pathname group nnml-directory)))
432     (when (condition-case ()
433               (progn
434                 (make-directory new-dir t)
435                 t)
436             (error nil))
437       ;; We move the articles file by file instead of renaming
438       ;; the directory -- there may be subgroups in this group.
439       ;; One might be more clever, I guess.
440       (let ((files (nnheader-article-to-file-alist old-dir)))
441         (while files
442           (rename-file
443            (concat old-dir (cdar files))
444            (concat new-dir (cdar files)))
445           (pop files)))
446       ;; Move .overview file.
447       (let ((overview (concat old-dir nnml-nov-file-name)))
448         (when (file-exists-p overview)
449           (rename-file overview (concat new-dir nnml-nov-file-name))))
450       (when (<= (length (directory-files old-dir)) 2)
451         (condition-case ()
452             (delete-directory old-dir)
453           (error nil)))
454       ;; That went ok, so we change the internal structures.
455       (let ((entry (assoc group nnml-group-alist)))
456         (when entry
457           (setcar entry new-name))
458         (setq nnml-current-directory nil
459               nnml-current-group nil)
460         ;; Save the new group alist.
461         (nnmail-save-active nnml-group-alist nnml-active-file)
462         t))))
463
464 (deffoo nnml-set-status (article name value &optional group server)
465   (nnml-possibly-change-directory group server)
466   (let ((file (nnml-article-to-file article)))
467     (cond
468      ((not (file-exists-p file))
469       (nnheader-report 'nnml "File %s does not exist" file))
470      (t
471       (with-temp-file file
472         (nnheader-insert-file-contents file)
473         (nnmail-replace-status name value))
474       t))))
475
476 \f
477 ;;; Internal functions.
478
479 (defun nnml-article-to-file (article)
480   (nnml-update-file-alist)
481   (let (file)
482     (if (setq file (cdr (assq article nnml-article-file-alist)))
483         (concat nnml-current-directory file)
484       ;; Just to make sure nothing went wrong when reading over NFS --
485       ;; check once more.
486       (when (file-exists-p
487              (setq file (expand-file-name (number-to-string article)
488                                           nnml-current-directory)))
489         (nnml-update-file-alist t)
490         file))))
491
492 (defun nnml-deletable-article-p (group article)
493   "Say whether ARTICLE in GROUP can be deleted."
494   (let (path)
495     (when (setq path (nnml-article-to-file article))
496       (when (file-writable-p path)
497         (or (not nnmail-keep-last-article)
498             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
499                      article)))))))
500
501 ;; Find an article number in the current group given the Message-ID.
502 (defun nnml-find-group-number (id)
503   (save-excursion
504     (set-buffer (get-buffer-create " *nnml id*"))
505     (buffer-disable-undo (current-buffer))
506     (let ((alist nnml-group-alist)
507           number)
508       ;; We want to look through all .overview files, but we want to
509       ;; start with the one in the current directory.  It seems most
510       ;; likely that the article we are looking for is in that group.
511       (if (setq number (nnml-find-id nnml-current-group id))
512           (cons nnml-current-group number)
513         ;; It wasn't there, so we look through the other groups as well.
514         (while (and (not number)
515                     alist)
516           (or (string= (caar alist) nnml-current-group)
517               (setq number (nnml-find-id (caar alist) id)))
518           (or number
519               (setq alist (cdr alist))))
520         (and number
521              (cons (caar alist) number))))))
522
523 (defun nnml-find-id (group id)
524   (erase-buffer)
525   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
526                      nnml-nov-file-name))
527         number found)
528     (when (file-exists-p nov)
529       (nnheader-insert-file-contents nov)
530       (while (and (not found)
531                   (search-forward id nil t)) ; We find the ID.
532         ;; And the id is in the fourth field.
533         (if (not (and (search-backward "\t" nil t 4)
534                       (not (search-backward"\t" (gnus-point-at-bol) t))))
535             (forward-line 1)
536           (beginning-of-line)
537           (setq found t)
538           ;; We return the article number.
539           (setq number
540                 (condition-case ()
541                     (read (current-buffer))
542                   (error nil)))))
543       number)))
544
545 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
546   (if (or gnus-nov-is-evil nnml-nov-is-evil)
547       nil
548     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
549       (when (file-exists-p nov)
550         (save-excursion
551           (set-buffer nntp-server-buffer)
552           (erase-buffer)
553           (nnheader-insert-file-contents nov)
554           (if (and fetch-old
555                    (not (numberp fetch-old)))
556               t                         ; Don't remove anything.
557             (nnheader-nov-delete-outside-range
558              (if fetch-old (max 1 (- (car articles) fetch-old))
559                (car articles))
560              (car (last articles)))
561             t))))))
562
563 (defun nnml-possibly-change-directory (group &optional server)
564   (when (and server
565              (not (nnml-server-opened server)))
566     (nnml-open-server server))
567   (if (not group)
568       t
569     (let ((pathname (nnmail-group-pathname group nnml-directory))
570           (pathname-coding-system 'binary))
571       (when (not (equal pathname nnml-current-directory))
572         (setq nnml-current-directory pathname
573               nnml-current-group group
574               nnml-article-file-alist nil))
575       (file-exists-p nnml-current-directory))))
576
577 (defun nnml-possibly-create-directory (group)
578   (let (dir dirs)
579     (setq dir (nnmail-group-pathname group nnml-directory))
580     (while (not (file-directory-p dir))
581       (push dir dirs)
582       (setq dir (file-name-directory (directory-file-name dir))))
583     (while dirs
584       (make-directory (directory-file-name (car dirs)))
585       (nnheader-message 5 "Creating mail directory %s" (car dirs))
586       (setq dirs (cdr dirs)))))
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             (concat (nnmail-group-pathname group nnml-directory)
650                     (int-to-string (cdr active))))
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                (concat (nnmail-group-pathname group nnml-directory)
691                        nnml-nov-file-name))
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 (member (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 (current-buffer))
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