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