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