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