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