2001-08-19 Simon Josefsson <jas@extundo.com>
[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                     "\\|" (regexp-quote nnml-marks-file-name) "$")))
447           article)
448       (while articles
449         (setq article (pop articles))
450         (when (file-writable-p article)
451           (nnheader-message 5 "Deleting article %s in %s..." article group)
452           (funcall nnmail-delete-file-function article))))
453     ;; Try to delete the directory itself.
454     (ignore-errors (delete-directory nnml-current-directory)))
455   ;; Remove the group from all structures.
456   (setq nnml-group-alist
457         (delq (assoc group nnml-group-alist) nnml-group-alist)
458         nnml-current-group nil
459         nnml-current-directory nil)
460   ;; Save the active file.
461   (nnmail-save-active nnml-group-alist nnml-active-file)
462   t)
463
464 (deffoo nnml-request-rename-group (group new-name &optional server)
465   (nnml-possibly-change-directory group server)
466   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
467         (old-dir (nnmail-group-pathname group nnml-directory)))
468     (when (ignore-errors
469             (make-directory new-dir t)
470             t)
471       ;; We move the articles file by file instead of renaming
472       ;; the directory -- there may be subgroups in this group.
473       ;; One might be more clever, I guess.
474       (let ((files (nnheader-article-to-file-alist old-dir)))
475         (while files
476           (rename-file
477            (concat old-dir (cdar files))
478            (concat new-dir (cdar files)))
479           (pop files)))
480       ;; Move .overview file.
481       (let ((overview (concat old-dir nnml-nov-file-name)))
482         (when (file-exists-p overview)
483           (rename-file overview (concat new-dir nnml-nov-file-name))))
484       ;; Move .marks file.
485       (let ((marks (concat old-dir nnml-marks-file-name)))
486         (when (file-exists-p marks)
487           (rename-file marks (concat new-dir nnml-marks-file-name))))
488       (when (<= (length (directory-files old-dir)) 2)
489         (ignore-errors (delete-directory old-dir)))
490       ;; That went ok, so we change the internal structures.
491       (let ((entry (assoc group nnml-group-alist)))
492         (when entry
493           (setcar entry new-name))
494         (setq nnml-current-directory nil
495               nnml-current-group nil)
496         ;; Save the new group alist.
497         (nnmail-save-active nnml-group-alist nnml-active-file)
498         t))))
499
500 (deffoo nnml-set-status (article name value &optional group server)
501   (nnml-possibly-change-directory group server)
502   (let ((file (nnml-article-to-file article)))
503     (cond
504      ((not (file-exists-p file))
505       (nnheader-report 'nnml "File %s does not exist" file))
506      (t
507       (with-temp-file file
508         (nnheader-insert-file-contents file)
509         (nnmail-replace-status name value))
510       t))))
511
512 \f
513 ;;; Internal functions.
514
515 (defun nnml-article-to-file (article)
516   (nnml-update-file-alist)
517   (let (file)
518     (if (setq file (cdr (assq article nnml-article-file-alist)))
519         (expand-file-name file nnml-current-directory)
520       (if nnml-check-directory-twice
521       ;; Just to make sure nothing went wrong when reading over NFS --
522           ;; check once more.
523           (when (file-exists-p
524                  (setq file (expand-file-name (number-to-string article)
525                                               nnml-current-directory)))
526             (nnml-update-file-alist t)
527             file)))))
528
529 (defun nnml-deletable-article-p (group article)
530   "Say whether ARTICLE in GROUP can be deleted."
531   (let (path)
532     (when (setq path (nnml-article-to-file article))
533       (when (file-writable-p path)
534         (or (not nnmail-keep-last-article)
535             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
536                      article)))))))
537
538 ;; Find an article number in the current group given the Message-ID.
539 (defun nnml-find-group-number (id)
540   (save-excursion
541     (set-buffer (get-buffer-create " *nnml id*"))
542     (let ((alist nnml-group-alist)
543           number)
544       ;; We want to look through all .overview files, but we want to
545       ;; start with the one in the current directory.  It seems most
546       ;; likely that the article we are looking for is in that group.
547       (if (setq number (nnml-find-id nnml-current-group id))
548           (cons nnml-current-group number)
549       ;; It wasn't there, so we look through the other groups as well.
550         (while (and (not number)
551                     alist)
552           (or (string= (caar alist) nnml-current-group)
553               (setq number (nnml-find-id (caar alist) id)))
554           (or number
555               (setq alist (cdr alist))))
556         (and number
557              (cons (caar alist) number))))))
558
559 (defun nnml-find-id (group id)
560   (erase-buffer)
561   (let ((nov (expand-file-name nnml-nov-file-name
562                                (nnmail-group-pathname group nnml-directory)))
563         number found)
564     (when (file-exists-p nov)
565       (nnheader-insert-file-contents nov)
566       (while (and (not found)
567                   (search-forward id nil t)) ; We find the ID.
568         ;; And the id is in the fourth field.
569         (if (not (and (search-backward "\t" nil t 4)
570                       (not (search-backward"\t" (gnus-point-at-bol) t))))
571             (forward-line 1)
572           (beginning-of-line)
573           (setq found t)
574           ;; We return the article number.
575           (setq number
576                 (ignore-errors (read (current-buffer))))))
577       number)))
578
579 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
580   (if (or gnus-nov-is-evil nnml-nov-is-evil)
581       nil
582     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
583       (when (file-exists-p nov)
584         (save-excursion
585           (set-buffer nntp-server-buffer)
586           (erase-buffer)
587           (nnheader-insert-file-contents nov)
588           (if (and fetch-old
589                    (not (numberp fetch-old)))
590               t                         ; Don't remove anything.
591             (nnheader-nov-delete-outside-range
592              (if fetch-old (max 1 (- (car articles) fetch-old))
593                (car articles))
594              (car (last articles)))
595             t))))))
596
597 (defun nnml-possibly-change-directory (group &optional server)
598   (when (and server
599              (not (nnml-server-opened server)))
600     (nnml-open-server server))
601   (if (not group)
602       t
603     (let ((pathname (nnmail-group-pathname group nnml-directory))
604           (file-name-coding-system nnmail-pathname-coding-system))
605       (when (not (equal pathname nnml-current-directory))
606         (setq nnml-current-directory pathname
607               nnml-current-group group
608               nnml-article-file-alist nil))
609       (file-exists-p nnml-current-directory))))
610
611 (defun nnml-possibly-create-directory (group)
612   (let ((dir (nnmail-group-pathname group nnml-directory)))
613     (unless (file-exists-p dir)
614       (make-directory (directory-file-name dir) t)
615       (nnheader-message 5 "Creating mail directory %s" dir))))
616
617 (defun nnml-save-mail (group-art)
618   "Called narrowed to an article."
619   (let (chars headers)
620     (setq chars (nnmail-insert-lines))
621     (nnmail-insert-xref group-art)
622     (run-hooks 'nnmail-prepare-save-mail-hook)
623     (run-hooks 'nnml-prepare-save-mail-hook)
624     (goto-char (point-min))
625     (while (looking-at "From ")
626       (replace-match "X-From-Line: ")
627       (forward-line 1))
628     ;; We save the article in all the groups it belongs in.
629     (let ((ga group-art)
630           first)
631       (while ga
632         (nnml-possibly-create-directory (caar ga))
633         (let ((file (concat (nnmail-group-pathname
634                              (caar ga) nnml-directory)
635                             (int-to-string (cdar ga)))))
636           (if first
637               ;; It was already saved, so we just make a hard link.
638               (funcall nnmail-crosspost-link-function first file t)
639             ;; Save the article.
640             (nnmail-write-region (point-min) (point-max) file nil
641                                  (if (nnheader-be-verbose 5) nil 'nomesg))
642             (setq first file)))
643         (setq ga (cdr ga))))
644     ;; Generate a nov line for this article.  We generate the nov
645     ;; line after saving, because nov generation destroys the
646     ;; header.
647     (setq headers (nnml-parse-head chars))
648     ;; Output the nov line to all nov databases that should have it.
649     (let ((ga group-art))
650       (while ga
651         (nnml-add-nov (caar ga) (cdar ga) headers)
652         (setq ga (cdr ga))))
653     group-art))
654
655 (defun nnml-active-number (group)
656   "Compute the next article number in GROUP."
657   (let ((active (cadr (assoc group nnml-group-alist))))
658     ;; The group wasn't known to nnml, so we just create an active
659     ;; entry for it.
660     (unless active
661       ;; Perhaps the active file was corrupt?  See whether
662       ;; there are any articles in this group.
663       (nnml-possibly-create-directory group)
664       (nnml-possibly-change-directory group)
665       (unless nnml-article-file-alist
666         (setq nnml-article-file-alist
667               (sort
668                (nnheader-article-to-file-alist nnml-current-directory)
669                'car-less-than-car)))
670       (setq active
671             (if nnml-article-file-alist
672                 (cons (caar nnml-article-file-alist)
673                       (caar (last nnml-article-file-alist)))
674               (cons 1 0)))
675       (push (list group active) nnml-group-alist))
676     (setcdr active (1+ (cdr active)))
677     (while (file-exists-p
678             (expand-file-name (int-to-string (cdr active))
679                               (nnmail-group-pathname group nnml-directory)))
680       (setcdr active (1+ (cdr active))))
681     (cdr active)))
682
683 (defun nnml-add-nov (group article headers)
684   "Add a nov line for the GROUP base."
685   (save-excursion
686     (set-buffer (nnml-open-nov group))
687     (goto-char (point-max))
688     (mail-header-set-number headers article)
689     (nnheader-insert-nov headers)))
690
691 (defsubst nnml-header-value ()
692   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
693
694 (defun nnml-parse-head (chars &optional number)
695   "Parse the head of the current buffer."
696   (save-excursion
697     (save-restriction
698       (unless (zerop (buffer-size))
699         (narrow-to-region
700          (goto-char (point-min))
701          (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
702       ;; Fold continuation lines.
703       (goto-char (point-min))
704       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
705         (replace-match " " t t))
706       ;; Remove any tabs; they are too confusing.
707       (subst-char-in-region (point-min) (point-max) ?\t ? )
708       (let ((headers (nnheader-parse-head t)))
709         (mail-header-set-chars headers chars)
710         (mail-header-set-number headers number)
711         headers))))
712
713 (defun nnml-open-nov (group)
714   (or (cdr (assoc group nnml-nov-buffer-alist))
715       (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
716         (save-excursion
717           (set-buffer buffer)
718           (set (make-local-variable 'nnml-nov-buffer-file-name)
719                (expand-file-name
720                 nnml-nov-file-name
721                 (nnmail-group-pathname group nnml-directory)))
722           (erase-buffer)
723           (when (file-exists-p nnml-nov-buffer-file-name)
724             (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
725         (push (cons group buffer) nnml-nov-buffer-alist)
726         buffer)))
727
728 (defun nnml-save-nov ()
729   (save-excursion
730     (while nnml-nov-buffer-alist
731       (when (buffer-name (cdar nnml-nov-buffer-alist))
732         (set-buffer (cdar nnml-nov-buffer-alist))
733         (when (buffer-modified-p)
734           (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
735                                nil 'nomesg))
736         (set-buffer-modified-p nil)
737         (kill-buffer (current-buffer)))
738       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
739
740 ;;;###autoload
741 (defun nnml-generate-nov-databases (&optional server)
742   "Generate NOV databases in all nnml directories."
743   (interactive (list (or (nnoo-current-server 'nnml) "")))
744   ;; Read the active file to make sure we don't re-use articles
745   ;; numbers in empty groups.
746   (nnmail-activate 'nnml)
747   (unless (nnml-server-opened server)
748     (nnml-open-server server))
749   (setq nnml-directory (expand-file-name nnml-directory))
750   ;; Recurse down the directories.
751   (nnml-generate-nov-databases-1 nnml-directory nil t)
752   ;; Save the active file.
753   (nnmail-save-active nnml-group-alist nnml-active-file))
754
755 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
756   "Regenerate the NOV database in DIR."
757   (interactive "DRegenerate NOV in: ")
758   (setq dir (file-name-as-directory dir))
759   ;; Only scan this sub-tree if we haven't been here yet.
760   (unless (member (file-truename dir) seen)
761     (push (file-truename dir) seen)
762     ;; We descend recursively
763     (let ((dirs (directory-files dir t nil t))
764           dir)
765       (while (setq dir (pop dirs))
766         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
767                    (file-directory-p dir))
768           (nnml-generate-nov-databases-1 dir seen))))
769     ;; Do this directory.
770     (let ((files (sort (nnheader-article-to-file-alist dir)
771                        'car-less-than-car)))
772       (if (not files)
773           (let* ((group (nnheader-file-to-group
774                          (directory-file-name dir) nnml-directory))
775                  (info (cadr (assoc group nnml-group-alist))))
776             (when info
777               (setcar info (1+ (cdr info)))))
778         (funcall nnml-generate-active-function dir)
779         ;; Generate the nov file.
780         (nnml-generate-nov-file dir files)
781         (unless no-active
782           (nnmail-save-active nnml-group-alist nnml-active-file))))))
783
784 (eval-when-compile (defvar files))
785 (defun nnml-generate-active-info (dir)
786   ;; Update the active info for this group.
787   (let* ((group (nnheader-file-to-group
788                  (directory-file-name dir) nnml-directory))
789          (entry (assoc group nnml-group-alist))
790          (last (or (caadr entry) 0)))
791     (setq nnml-group-alist (delq entry nnml-group-alist))
792     (push (list group
793                 (cons (or (caar files) (1+ last))
794                       (max last
795                            (or (let ((f files))
796                                  (while (cdr f) (setq f (cdr f)))
797                                  (caar f))
798                                0))))
799           nnml-group-alist)))
800
801 (defun nnml-generate-nov-file (dir files)
802   (let* ((dir (file-name-as-directory dir))
803          (nov (concat dir nnml-nov-file-name))
804          (nov-buffer (get-buffer-create " *nov*"))
805          chars file headers)
806     (save-excursion
807       ;; Init the nov buffer.
808       (set-buffer nov-buffer)
809       (buffer-disable-undo)
810       (erase-buffer)
811       (set-buffer nntp-server-buffer)
812       ;; Delete the old NOV file.
813       (when (file-exists-p nov)
814         (funcall nnmail-delete-file-function nov))
815       (while files
816         (unless (file-directory-p (setq file (concat dir (cdar files))))
817           (erase-buffer)
818           (nnheader-insert-file-contents file)
819           (narrow-to-region
820            (goto-char (point-min))
821            (progn
822              (search-forward "\n\n" nil t)
823              (setq chars (- (point-max) (point)))
824              (max 1 (1- (point)))))
825           (unless (zerop (buffer-size))
826             (goto-char (point-min))
827             (setq headers (nnml-parse-head chars (caar files)))
828             (save-excursion
829               (set-buffer nov-buffer)
830               (goto-char (point-max))
831               (nnheader-insert-nov headers)))
832           (widen))
833         (setq files (cdr files)))
834       (save-excursion
835         (set-buffer nov-buffer)
836         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
837         (kill-buffer (current-buffer))))))
838
839 (defun nnml-nov-delete-article (group article)
840   (save-excursion
841     (set-buffer (nnml-open-nov group))
842     (when (nnheader-find-nov-line article)
843       (delete-region (point) (progn (forward-line 1) (point)))
844       (when (bobp)
845         (let ((active (cadr (assoc group nnml-group-alist)))
846               num)
847           (when active
848             (if (eobp)
849                 (setf (car active) (1+ (cdr active)))
850               (when (and (setq num (ignore-errors (read (current-buffer))))
851                          (numberp num))
852                 (setf (car active) num)))))))
853     t))
854
855 (defun nnml-update-file-alist (&optional force)
856   (when (or (not nnml-article-file-alist)
857             force)
858     (setq nnml-article-file-alist
859           (nnheader-article-to-file-alist nnml-current-directory))))
860
861 (defvoo nnml-marks-file-name ".marks")
862 (defvoo nnml-marks-is-evil nil)
863 (defvoo nnml-marks nil)
864
865 (deffoo nnml-request-set-mark (group actions &optional server)
866   (nnml-possibly-change-directory group server)
867   (unless nnml-marks-is-evil
868     (nnml-open-marks group server)
869     (dolist (action actions)
870       (let ((range (nth 0 action))
871             (what  (nth 1 action))
872             (marks (nth 2 action)))
873         (assert (or (eq what 'add) (eq what 'del)) t
874                 "Unknown request-set-mark action: %s" what)
875         (dolist (mark marks)
876           (setq nnml-marks (gnus-update-alist-soft
877                             mark
878                             (funcall (if (eq what 'add) 'gnus-range-add
879                                        'gnus-remove-from-range)
880                                      (cdr (assoc mark nnml-marks)) range)
881                             nnml-marks)))))
882     (nnml-save-marks group server))
883   nil)
884
885 (deffoo nnml-request-update-info (group info &optional server)
886   (nnml-possibly-change-directory group server)
887   (unless nnml-marks-is-evil
888     (nnml-open-marks group server)
889     ;; Update info using `nnml-marks'.
890     (mapcar (lambda (pred)
891               (gnus-info-set-marks
892                info
893                (gnus-update-alist-soft
894                 (cdr pred)
895                 (cdr (assq (cdr pred) nnml-marks))
896                 (gnus-info-marks info))
897                t))
898             gnus-article-mark-lists)
899     (let ((seen (cdr (assq 'read nnml-marks))))
900       (gnus-info-set-read info
901                           (if (and (integerp (car seen))
902                                    (null (cdr seen)))
903                               (list (cons (car seen) (car seen)))
904                             seen))))
905   info)
906
907 (defun nnml-save-marks (group server)
908   (let ((file-name-coding-system nnmail-pathname-coding-system)
909         (file (expand-file-name nnml-marks-file-name
910                                 (nnmail-group-pathname group nnml-directory))))
911     (nnml-possibly-create-directory group)
912     (with-temp-file file
913       (erase-buffer)
914       (princ nnml-marks (current-buffer))
915       (insert "\n"))))
916
917 (defun nnml-open-marks (group server)
918   (let ((file (expand-file-name 
919                nnml-marks-file-name 
920                (nnmail-group-pathname group nnml-directory))))
921     (if (file-exists-p file)
922         (setq nnml-marks (condition-case err
923                              (with-temp-buffer
924                                (nnheader-insert-file-contents file)
925                                (read (current-buffer)))
926                            (error (or (gnus-yes-or-no-p
927                                        (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
928                                       (error "Cannot read nnml marks file %s (%s)" file err)))))
929       ;; User didn't have a .marks file.  Probably first time
930       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
931       (let ((info (gnus-get-info
932                    (gnus-group-prefixed-name
933                     group
934                     (gnus-server-to-method (format "nnml:%s" server))))))
935         (nnheader-message 6 "Boostrapping nnml marks...")
936         (setq nnml-marks (gnus-info-marks info))
937         (push (cons 'read (gnus-info-read info)) nnml-marks)
938         (nnml-save-marks group server)))))
939
940 (provide 'nnml)
941
942 ;;; nnml.el ends here