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