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