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