*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41   "Spool directory for the nnml mail backend.")
42
43 (defvoo nnml-active-file
44   (concat (file-name-as-directory nnml-directory) "active")
45   "Mail active file.")
46
47 (defvoo nnml-newsgroups-file
48   (concat (file-name-as-directory nnml-directory) "newsgroups")
49   "Mail newsgroups description file.")
50
51 (defvoo nnml-get-new-mail t
52   "If non-nil, nnml will check the incoming mail file and split the mail.")
53
54 (defvoo nnml-nov-is-evil nil
55   "If non-nil, Gnus will never generate and use nov databases for mail groups.
56 Using nov databases will speed up header fetching considerably.
57 This variable shouldn't be flipped much.  If you have, for some reason,
58 set this to t, and want to set it to nil again, you should always run
59 the `nnml-generate-nov-databases' command.  The function will go
60 through all nnml directories and generate nov databases for them
61 all.  This may very well take some time.")
62
63 (defvoo nnml-prepare-save-mail-hook nil
64   "Hook run narrowed to an article before saving.")
65
66 (defvoo nnml-inhibit-expiry nil
67   "If non-nil, inhibit expiry.")
68
69
70 \f
71
72 (defconst nnml-version "nnml 1.0"
73   "nnml version.")
74
75 (defvoo nnml-nov-file-name ".overview")
76
77 (defvoo nnml-current-directory nil)
78 (defvoo nnml-current-group nil)
79 (defvoo nnml-status-string "")
80 (defvoo nnml-nov-buffer-alist nil)
81 (defvoo nnml-group-alist nil)
82 (defvoo nnml-active-timestamp nil)
83 (defvoo nnml-article-file-alist nil)
84
85 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
86
87 (defvar nnml-nov-buffer-file-name nil)
88
89 (defvoo nnml-file-coding-system nnmail-file-coding-system-1)
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     (condition-case ()
146         (make-directory nnml-directory t)
147       (error)))
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
270                                                &optional server force)
271   (nnml-possibly-change-directory group server)
272   (let ((active-articles
273          (nnheader-directory-articles nnml-current-directory))
274         (is-old t)
275         article rest mod-time number)
276     (nnmail-activate 'nnml)
277
278     (setq active-articles (sort active-articles '<))
279     ;; Articles not listed in active-articles are already gone,
280     ;; so don't try to expire them.
281     (setq articles (gnus-sorted-intersection articles active-articles))
282
283     (while (and articles is-old)
284       (when (setq article (nnml-article-to-file (setq number (pop articles))))
285         (when (setq mod-time (nth 5 (file-attributes article)))
286           (if (and (nnml-deletable-article-p group number)
287                    (setq is-old
288                          (nnmail-expired-article-p group mod-time force
289                                                    nnml-inhibit-expiry)))
290               (progn
291                 (nnheader-message 5 "Deleting article %s in %s"
292                                   article group)
293                 (condition-case ()
294                     (funcall nnmail-delete-file-function article)
295                   (file-error
296                    (push number rest)))
297                 (setq active-articles (delq number active-articles))
298                 (nnml-nov-delete-article group number))
299             (push number rest)))))
300     (let ((active (nth 1 (assoc group nnml-group-alist))))
301       (when active
302         (setcar active (or (and active-articles
303                                 (apply 'min active-articles))
304                            (1+ (cdr active)))))
305       (nnmail-save-active nnml-group-alist nnml-active-file))
306     (nnml-save-nov)
307     (nconc rest articles)))
308
309 (deffoo nnml-request-move-article
310   (article group server accept-form &optional last)
311   (let ((buf (get-buffer-create " *nnml move*"))
312         result)
313     (nnml-possibly-change-directory group server)
314     (nnml-update-file-alist)
315     (and
316      (nnml-deletable-article-p group article)
317      (nnml-request-article article group server)
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 (condition-case ()
372                 (progn
373                   (nnmail-write-region
374                    (point-min) (point-max)
375                    (or (nnml-article-to-file article)
376                        (concat nnml-current-directory
377                                (int-to-string article)))
378                    nil (if (nnheader-be-verbose 5) nil 'nomesg))
379                   t)
380               (error nil))
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     (condition-case ()
422         (delete-directory nnml-current-directory)
423       (error nil)))
424   ;; Remove the group from all structures.
425   (setq nnml-group-alist
426         (delq (assoc group nnml-group-alist) nnml-group-alist)
427         nnml-current-group nil
428         nnml-current-directory nil)
429   ;; Save the active file.
430   (nnmail-save-active nnml-group-alist nnml-active-file)
431   t)
432
433 (deffoo nnml-request-rename-group (group new-name &optional server)
434   (nnml-possibly-change-directory group server)
435   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
436         (old-dir (nnmail-group-pathname group nnml-directory)))
437     (when (condition-case ()
438               (progn
439                 (make-directory new-dir t)
440                 t)
441             (error nil))
442       ;; We move the articles file by file instead of renaming
443       ;; the directory -- there may be subgroups in this group.
444       ;; One might be more clever, I guess.
445       (let ((files (nnheader-article-to-file-alist old-dir)))
446         (while files
447           (rename-file
448            (concat old-dir (cdar files))
449            (concat new-dir (cdar files)))
450           (pop files)))
451       ;; Move .overview file.
452       (let ((overview (concat old-dir nnml-nov-file-name)))
453         (when (file-exists-p overview)
454           (rename-file overview (concat new-dir nnml-nov-file-name))))
455       (when (<= (length (directory-files old-dir)) 2)
456         (condition-case ()
457             (delete-directory old-dir)
458           (error nil)))
459       ;; That went ok, so we change the internal structures.
460       (let ((entry (assoc group nnml-group-alist)))
461         (when entry
462           (setcar entry new-name))
463         (setq nnml-current-directory nil
464               nnml-current-group nil)
465         ;; Save the new group alist.
466         (nnmail-save-active nnml-group-alist nnml-active-file)
467         t))))
468
469 (deffoo nnml-set-status (article name value &optional group server)
470   (nnml-possibly-change-directory group server)
471   (let ((file (nnml-article-to-file article)))
472     (cond
473      ((not (file-exists-p file))
474       (nnheader-report 'nnml "File %s does not exist" file))
475      (t
476       (with-temp-file file
477         (nnheader-insert-file-contents file)
478         (nnmail-replace-status name value))
479       t))))
480
481 \f
482 ;;; Internal functions.
483
484 (defun nnml-article-to-file (article)
485   (nnml-update-file-alist)
486   (let (file)
487     (if (setq file (cdr (assq article nnml-article-file-alist)))
488         (concat nnml-current-directory file)
489       ;; Just to make sure nothing went wrong when reading over NFS --
490       ;; check once more.
491       (when (file-exists-p
492              (setq file (expand-file-name (number-to-string article)
493                                           nnml-current-directory)))
494         (nnml-update-file-alist t)
495         file))))
496
497 (defun nnml-deletable-article-p (group article)
498   "Say whether ARTICLE in GROUP can be deleted."
499   (let (path)
500     (when (setq path (nnml-article-to-file article))
501       (when (file-writable-p path)
502         (or (not nnmail-keep-last-article)
503             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
504                      article)))))))
505
506 ;; Find an article number in the current group given the Message-ID.
507 (defun nnml-find-group-number (id)
508   (save-excursion
509     (set-buffer (get-buffer-create " *nnml id*"))
510     (let ((alist nnml-group-alist)
511           number)
512       ;; We want to look through all .overview files, but we want to
513       ;; start with the one in the current directory.  It seems most
514       ;; likely that the article we are looking for is in that group.
515       (if (setq number (nnml-find-id nnml-current-group id))
516           (cons nnml-current-group number)
517         ;; It wasn't there, so we look through the other groups as well.
518         (while (and (not number)
519                     alist)
520           (or (string= (caar alist) nnml-current-group)
521               (setq number (nnml-find-id (caar alist) id)))
522           (or number
523               (setq alist (cdr alist))))
524         (and number
525              (cons (caar alist) number))))))
526
527 (defun nnml-find-id (group id)
528   (erase-buffer)
529   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
530                      nnml-nov-file-name))
531         number found)
532     (when (file-exists-p nov)
533       (nnheader-insert-file-contents nov)
534       (while (and (not found)
535                   (search-forward id nil t)) ; We find the ID.
536         ;; And the id is in the fourth field.
537         (if (not (and (search-backward "\t" nil t 4)
538                       (not (search-backward"\t" (gnus-point-at-bol) t))))
539             (forward-line 1)
540           (beginning-of-line)
541           (setq found t)
542           ;; We return the article number.
543           (setq number
544                 (condition-case ()
545                     (read (current-buffer))
546                   (error nil)))))
547       number)))
548
549 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
550   (if (or gnus-nov-is-evil nnml-nov-is-evil)
551       nil
552     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
553       (when (file-exists-p nov)
554         (save-excursion
555           (set-buffer nntp-server-buffer)
556           (erase-buffer)
557           (nnheader-insert-file-contents nov)
558           (if (and fetch-old
559                    (not (numberp fetch-old)))
560               t                         ; Don't remove anything.
561             (nnheader-nov-delete-outside-range
562              (if fetch-old (max 1 (- (car articles) fetch-old))
563                (car articles))
564              (car (last articles)))
565             t))))))
566
567 (defun nnml-possibly-change-directory (group &optional server)
568   (when (and server
569              (not (nnml-server-opened server)))
570     (nnml-open-server server))
571   (if (not group)
572       t
573     (let ((pathname (nnmail-group-pathname group nnml-directory))
574           (pathname-coding-system 'binary))
575       (when (not (equal pathname nnml-current-directory))
576         (setq nnml-current-directory pathname
577               nnml-current-group group
578               nnml-article-file-alist nil))
579       (file-exists-p nnml-current-directory))))
580
581 (defun nnml-possibly-create-directory (group)
582   (let ((dir (nnmail-group-pathname group nnml-directory)))
583     (unless (file-exists-p dir)
584       (make-directory (directory-file-name dir) t)
585       (nnheader-message 5 "Creating mail directory %s" dir))))
586
587 (defun nnml-save-mail (group-art)
588   "Called narrowed to an article."
589   (let (chars headers)
590     (setq chars (nnmail-insert-lines))
591     (nnmail-insert-xref group-art)
592     (run-hooks 'nnmail-prepare-save-mail-hook)
593     (run-hooks 'nnml-prepare-save-mail-hook)
594     (goto-char (point-min))
595     (while (looking-at "From ")
596       (replace-match "X-From-Line: ")
597       (forward-line 1))
598     ;; We save the article in all the groups it belongs in.
599     (let ((ga group-art)
600           first)
601       (while ga
602         (nnml-possibly-create-directory (caar ga))
603         (let ((file (concat (nnmail-group-pathname
604                              (caar ga) nnml-directory)
605                             (int-to-string (cdar ga)))))
606           (if first
607               ;; It was already saved, so we just make a hard link.
608               (funcall nnmail-crosspost-link-function first file t)
609             ;; Save the article.
610             (nnmail-write-region (point-min) (point-max) file nil
611                                  (if (nnheader-be-verbose 5) nil 'nomesg))
612             (setq first file)))
613         (setq ga (cdr ga))))
614     ;; Generate a nov line for this article.  We generate the nov
615     ;; line after saving, because nov generation destroys the
616     ;; header.
617     (setq headers (nnml-parse-head chars))
618     ;; Output the nov line to all nov databases that should have it.
619     (let ((ga group-art))
620       (while ga
621         (nnml-add-nov (caar ga) (cdar ga) headers)
622         (setq ga (cdr ga))))
623     group-art))
624
625 (defun nnml-active-number (group)
626   "Compute the next article number in GROUP."
627   (let ((active (cadr (assoc group nnml-group-alist))))
628     ;; The group wasn't known to nnml, so we just create an active
629     ;; entry for it.
630     (unless active
631       ;; Perhaps the active file was corrupt?  See whether
632       ;; there are any articles in this group.
633       (nnml-possibly-create-directory group)
634       (nnml-possibly-change-directory group)
635       (unless nnml-article-file-alist
636         (setq nnml-article-file-alist
637               (sort
638                (nnheader-article-to-file-alist nnml-current-directory)
639                'car-less-than-car)))
640       (setq active
641             (if nnml-article-file-alist
642                 (cons (caar nnml-article-file-alist)
643                       (caar (last nnml-article-file-alist)))
644               (cons 1 0)))
645       (push (list group active) nnml-group-alist))
646     (setcdr active (1+ (cdr active)))
647     (while (file-exists-p
648             (concat (nnmail-group-pathname group nnml-directory)
649                     (int-to-string (cdr active))))
650       (setcdr active (1+ (cdr active))))
651     (cdr active)))
652
653 (defun nnml-add-nov (group article headers)
654   "Add a nov line for the GROUP base."
655   (save-excursion
656     (set-buffer (nnml-open-nov group))
657     (goto-char (point-max))
658     (mail-header-set-number headers article)
659     (nnheader-insert-nov headers)))
660
661 (defsubst nnml-header-value ()
662   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
663
664 (defun nnml-parse-head (chars &optional number)
665   "Parse the head of the current buffer."
666   (save-excursion
667     (save-restriction
668       (unless (zerop (buffer-size))
669         (narrow-to-region
670          (goto-char (point-min))
671          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
672       ;; Fold continuation lines.
673       (goto-char (point-min))
674       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
675         (replace-match " " t t))
676       ;; Remove any tabs; they are too confusing.
677       (subst-char-in-region (point-min) (point-max) ?\t ? )
678       (let ((headers (nnheader-parse-head t)))
679         (mail-header-set-chars headers chars)
680         (mail-header-set-number headers number)
681         headers))))
682
683 (defun nnml-open-nov (group)
684   (or (cdr (assoc group nnml-nov-buffer-alist))
685       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
686         (save-excursion
687           (set-buffer buffer)
688           (set (make-local-variable 'nnml-nov-buffer-file-name)
689                (concat (nnmail-group-pathname group nnml-directory)
690                        nnml-nov-file-name))
691           (erase-buffer)
692           (when (file-exists-p nnml-nov-buffer-file-name)
693             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
694         (push (cons group buffer) nnml-nov-buffer-alist)
695         buffer)))
696
697 (defun nnml-save-nov ()
698   (save-excursion
699     (while nnml-nov-buffer-alist
700       (when (buffer-name (cdar nnml-nov-buffer-alist))
701         (set-buffer (cdar nnml-nov-buffer-alist))
702         (when (buffer-modified-p)
703           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
704                                nil 'nomesg))
705         (set-buffer-modified-p nil)
706         (kill-buffer (current-buffer)))
707       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
708
709 ;;;###autoload
710 (defun nnml-generate-nov-databases ()
711   "Generate NOV databases in all nnml directories."
712   (interactive)
713   ;; Read the active file to make sure we don't re-use articles
714   ;; numbers in empty groups.
715   (nnmail-activate 'nnml)
716   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
717   (setq nnml-directory (expand-file-name nnml-directory))
718   ;; Recurse down the directories.
719   (nnml-generate-nov-databases-1 nnml-directory nil t)
720   ;; Save the active file.
721   (nnmail-save-active nnml-group-alist nnml-active-file))
722
723 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
724   "Regenerate the NOV database in DIR."
725   (interactive "DRegenerate NOV in: ")
726   (setq dir (file-name-as-directory dir))
727   ;; Only scan this sub-tree if we haven't been here yet.
728   (unless (member (file-truename dir) seen)
729     (push (file-truename dir) seen)
730     ;; We descend recursively
731     (let ((dirs (directory-files dir t nil t))
732           dir)
733       (while (setq dir (pop dirs))
734         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
735                    (file-directory-p dir))
736           (nnml-generate-nov-databases-1 dir seen))))
737     ;; Do this directory.
738     (let ((files (sort (nnheader-article-to-file-alist dir)
739                        'car-less-than-car)))
740       (if (not files)
741           (let* ((group (nnheader-file-to-group
742                          (directory-file-name dir) nnml-directory))
743                  (info (cadr (assoc group nnml-group-alist))))
744             (when info
745               (setcar info (1+ (cdr info)))))
746         (funcall nnml-generate-active-function dir)
747         ;; Generate the nov file.
748         (nnml-generate-nov-file dir files)
749         (unless no-active
750           (nnmail-save-active nnml-group-alist nnml-active-file))))))
751
752 (defvar files)
753 (defun nnml-generate-active-info (dir)
754   ;; Update the active info for this group.
755   (let ((group (nnheader-file-to-group
756                 (directory-file-name dir) nnml-directory)))
757     (setq nnml-group-alist
758           (delq (assoc group nnml-group-alist) nnml-group-alist))
759     (push (list group
760                 (cons (caar files)
761                       (let ((f files))
762                         (while (cdr f) (setq f (cdr f)))
763                         (caar f))))
764           nnml-group-alist)))
765
766 (defun nnml-generate-nov-file (dir files)
767   (let* ((dir (file-name-as-directory dir))
768          (nov (concat dir nnml-nov-file-name))
769          (nov-buffer (get-buffer-create " *nov*"))
770          chars file headers)
771     (save-excursion
772       ;; Init the nov buffer.
773       (set-buffer nov-buffer)
774       (buffer-disable-undo)
775       (erase-buffer)
776       (set-buffer nntp-server-buffer)
777       ;; Delete the old NOV file.
778       (when (file-exists-p nov)
779         (funcall nnmail-delete-file-function nov))
780       (while files
781         (unless (file-directory-p (setq file (concat dir (cdar files))))
782           (erase-buffer)
783           (nnheader-insert-file-contents file)
784           (narrow-to-region
785            (goto-char (point-min))
786            (progn
787              (search-forward "\n\n" nil t)
788              (setq chars (- (point-max) (point)))
789              (max 1 (1- (point)))))
790           (unless (zerop (buffer-size))
791             (goto-char (point-min))
792             (setq headers (nnml-parse-head chars (caar files)))
793             (save-excursion
794               (set-buffer nov-buffer)
795               (goto-char (point-max))
796               (nnheader-insert-nov headers)))
797           (widen))
798         (setq files (cdr files)))
799       (save-excursion
800         (set-buffer nov-buffer)
801         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
802         (kill-buffer (current-buffer))))))
803
804 (defun nnml-nov-delete-article (group article)
805   (save-excursion
806     (set-buffer (nnml-open-nov group))
807     (when (nnheader-find-nov-line article)
808       (delete-region (point) (progn (forward-line 1) (point)))
809       (when (bobp)
810         (let ((active (cadr (assoc group nnml-group-alist)))
811               num)
812           (when active
813             (if (eobp)
814                 (setf (car active) (1+ (cdr active)))
815               (when (and (setq num (ignore-errors (read (current-buffer))))
816                          (numberp num))
817                 (setf (car active) num)))))))
818     t))
819
820 (defun nnml-update-file-alist (&optional force)
821   (when (or (not nnml-article-file-alist)
822             force)
823     (setq nnml-article-file-alist
824           (nnheader-article-to-file-alist nnml-current-directory))))
825
826 (provide 'nnml)
827
828 ;;; nnml.el ends here