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