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