Typo.
[elisp/gnus.git-] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
6 ;;      Scott Byer <byer@mv.us.adobe.com>
7 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; Keywords: mail
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'message)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37 (require 'gnus-util)
38
39 (nnoo-declare nnfolder)
40
41 (defvoo nnfolder-directory (expand-file-name message-directory)
42   "The name of the nnfolder directory.")
43
44 (defvoo nnfolder-nov-directory nil
45   "The name of the nnfolder NOV directory.
46 If nil, `nnfolder-directory' is used.")
47
48 (defvoo nnfolder-active-file
49     (nnheader-concat nnfolder-directory "active")
50   "The name of the active file.")
51
52 ;; I renamed this variable to something more in keeping with the general GNU
53 ;; style. -SLB
54
55 (defvoo nnfolder-ignore-active-file nil
56   "If non-nil, the active file is ignored.
57 This causes nnfolder to do some extra work in order to determine the
58 true active ranges of an mbox file.  Note that the active file is
59 still saved, but its values are not used.  This costs some extra time
60 when scanning an mbox when opening it.")
61
62 (defvoo nnfolder-distrust-mbox nil
63   "If non-nil, the folder will be distrusted.
64 This means that nnfolder will not trust the user with respect to
65 inserting unaccounted for mail in the middle of an mbox file.  This
66 can greatly slow down scans, which now must scan the entire file for
67 unmarked messages.  When nil, scans occur forward from the last marked
68 message, a huge time saver for large mailboxes.")
69
70 (defvoo nnfolder-newsgroups-file
71     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
72   "Mail newsgroups description file.")
73
74 (defvoo nnfolder-get-new-mail t
75   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
76
77 (defvoo nnfolder-prepare-save-mail-hook nil
78   "Hook run narrowed to an article before saving.")
79
80 (defvoo nnfolder-save-buffer-hook nil
81   "Hook run before saving the nnfolder mbox buffer.")
82
83 (defvoo nnfolder-inhibit-expiry nil
84   "If non-nil, inhibit expiry.")
85
86 \f
87
88 (defconst nnfolder-version "nnfolder 2.0"
89   "nnfolder version.")
90
91 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
92   "String used to demarcate what the article number for a message is.")
93
94 (defvoo nnfolder-current-group nil)
95 (defvoo nnfolder-current-buffer nil)
96 (defvoo nnfolder-status-string "")
97 (defvoo nnfolder-group-alist nil)
98 (defvoo nnfolder-buffer-alist nil)
99 (defvoo nnfolder-scantime-alist nil)
100 (defvoo nnfolder-active-timestamp nil)
101 (defvoo nnfolder-active-file-coding-system nnheader-text-coding-system)
102 (defvoo nnfolder-active-file-coding-system-for-write 
103     nnmail-active-file-coding-system)
104 (defvoo nnfolder-file-coding-system nnheader-text-coding-system)
105 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
106   "Coding system for save nnfolder file.
107 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
108
109 (defvoo nnfolder-nov-is-evil nil
110   "If non-nil, Gnus will never generate and use nov databases for mail groups.
111 Using nov databases will speed up header fetching considerably.
112 This variable shouldn't be flipped much.  If you have, for some reason,
113 set this to t, and want to set it to nil again, you should always run
114 the `nnfolder-generate-active-file' command.  The function will go
115 through all nnfolder directories and generate nov databases for them
116 all.  This may very well take some time.")
117
118 (defvoo nnfolder-nov-file-suffix ".nov")
119
120 (defvoo nnfolder-nov-buffer-alist nil)
121
122 (defvar nnfolder-nov-buffer-file-name nil)
123
124 \f
125
126 ;;; Interface functions
127
128 (nnoo-define-basics nnfolder)
129
130 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
131   (save-excursion
132     (set-buffer nntp-server-buffer)
133     (erase-buffer)
134     (let (article start stop)
135       (nnfolder-possibly-change-group group server)
136       (when nnfolder-current-buffer
137         (set-buffer nnfolder-current-buffer)
138         (goto-char (point-min))
139         (if (stringp (car articles))
140             'headers
141           (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
142               'nov
143             (while (setq article (pop articles))
144               (set-buffer nnfolder-current-buffer)
145               (when (nnfolder-goto-article article)
146                 (setq start (point))
147                 (setq stop (if (search-forward "\n\n" nil t)
148                                (1- (point))
149                              (point-max)))
150                 (set-buffer nntp-server-buffer)
151                 (insert (format "221 %d Article retrieved.\n" article))
152                 (insert-buffer-substring nnfolder-current-buffer start stop)
153                 (goto-char (point-max))
154                 (insert ".\n")))
155             (set-buffer nntp-server-buffer)
156             (nnheader-fold-continuation-lines)
157             'headers))))))
158
159 (deffoo nnfolder-open-server (server &optional defs)
160   (nnoo-change-server 'nnfolder server defs)
161   (nnmail-activate 'nnfolder t)
162   (gnus-make-directory nnfolder-directory)
163   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
164     (and nnfolder-nov-directory
165          (gnus-make-directory nnfolder-nov-directory)))
166   (cond
167    ((not (file-exists-p nnfolder-directory))
168     (nnfolder-close-server)
169     (nnheader-report 'nnfolder "Couldn't create directory: %s"
170                      nnfolder-directory))
171    ((not (file-directory-p (file-truename nnfolder-directory)))
172     (nnfolder-close-server)
173     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
174    (t
175     (nnmail-activate 'nnfolder)
176     (nnheader-report 'nnfolder "Opened server %s using directory %s"
177                      server nnfolder-directory)
178     t)))
179
180 (deffoo nnfolder-request-close ()
181   (let ((alist nnfolder-buffer-alist))
182     (while alist
183       (nnfolder-close-group (caar alist) nil t)
184       (setq alist (cdr alist))))
185   (nnoo-close-server 'nnfolder)
186   (setq nnfolder-buffer-alist nil
187         nnfolder-group-alist nil))
188
189 (deffoo nnfolder-request-article (article &optional group server buffer)
190   (nnfolder-possibly-change-group group server)
191   (save-excursion
192     (set-buffer nnfolder-current-buffer)
193     (goto-char (point-min))
194     (when (nnfolder-goto-article article)
195       (let (start stop)
196         (setq start (point))
197         (forward-line 1)
198         (unless (and (nnmail-search-unix-mail-delim)
199                      (forward-line -1))
200           (goto-char (point-max)))
201         (setq stop (point))
202         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
203           (set-buffer nntp-server-buffer)
204           (erase-buffer)
205           (insert-buffer-substring nnfolder-current-buffer start stop)
206           (goto-char (point-min))
207           (while (looking-at "From ")
208             (delete-char 5)
209             (insert "X-From-Line: ")
210             (forward-line 1))
211           (if (numberp article)
212               (cons nnfolder-current-group article)
213             (goto-char (point-min))
214             (cons nnfolder-current-group
215                   (if (search-forward (concat "\n" nnfolder-article-marker) 
216                                       nil t)
217                       (string-to-int
218                        (buffer-substring
219                         (point) (progn (end-of-line) (point))))
220                     -1))))))))
221
222 (deffoo nnfolder-request-group (group &optional server dont-check)
223   (nnfolder-possibly-change-group group server t)
224   (save-excursion
225     (if (not (assoc group nnfolder-group-alist))
226         (nnheader-report 'nnfolder "No such group: %s" group)
227       (if dont-check
228           (progn
229             (nnheader-report 'nnfolder "Selected group %s" group)
230             t)
231         (let* ((active (assoc group nnfolder-group-alist))
232                (group (car active))
233                (range (cadr active)))
234           (cond
235            ((null active)
236             (nnheader-report 'nnfolder "No such group: %s" group))
237            ((null nnfolder-current-group)
238             (nnheader-report 'nnfolder "Empty group: %s" group))
239            (t
240             (nnheader-report 'nnfolder "Selected group %s" group)
241             (nnheader-insert "211 %d %d %d %s\n"
242                              (1+ (- (cdr range) (car range)))
243                              (car range) (cdr range) group))))))))
244
245 (deffoo nnfolder-request-scan (&optional group server)
246   (nnfolder-possibly-change-group nil server)
247   (when nnfolder-get-new-mail
248     (nnfolder-possibly-change-group group server)
249     (nnmail-get-new-mail
250      'nnfolder
251      (lambda ()
252        (let ((bufs nnfolder-buffer-alist))
253          (save-excursion
254            (while bufs
255              (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
256                  (setq nnfolder-buffer-alist
257                        (delq (car bufs) nnfolder-buffer-alist))
258                (set-buffer (nth 1 (car bufs)))
259                (nnfolder-save-buffer)
260                (kill-buffer (current-buffer)))
261              (setq bufs (cdr bufs))))))
262      nnfolder-directory
263      group)))
264
265 ;; Don't close the buffer if we're not shutting down the server.  This way,
266 ;; we can keep the buffer in the group buffer cache, and not have to grovel
267 ;; over the buffer again unless we add new mail to it or modify it in some
268 ;; way.
269
270 (deffoo nnfolder-close-group (group &optional server force)
271   ;; Make sure we _had_ the group open.
272   (when (or (assoc group nnfolder-buffer-alist)
273             (equal group nnfolder-current-group))
274     (let ((inf (assoc group nnfolder-buffer-alist)))
275       (when inf
276         (when (and nnfolder-current-group
277                    nnfolder-current-buffer)
278           (push (list nnfolder-current-group nnfolder-current-buffer)
279                 nnfolder-buffer-alist))
280         (setq nnfolder-buffer-alist
281               (delq inf nnfolder-buffer-alist))
282         (setq nnfolder-current-buffer (cadr inf)
283               nnfolder-current-group (car inf))))
284     (when (and nnfolder-current-buffer
285                (buffer-name nnfolder-current-buffer))
286       (save-excursion
287         (set-buffer nnfolder-current-buffer)
288         ;; If the buffer was modified, write the file out now.
289         (nnfolder-save-buffer)
290         ;; If we're shutting the server down, we need to kill the
291         ;; buffer and remove it from the open buffer list.  Or, of
292         ;; course, if we're trying to minimize our space impact.
293         (kill-buffer (current-buffer))
294         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
295                                           nnfolder-buffer-alist)))))
296   (setq nnfolder-current-group nil
297         nnfolder-current-buffer nil)
298   t)
299
300 (deffoo nnfolder-request-create-group (group &optional server args)
301   (nnfolder-possibly-change-group nil server)
302   (nnmail-activate 'nnfolder)
303   (when group
304     (unless (assoc group nnfolder-group-alist)
305       (push (list group (cons 1 0)) nnfolder-group-alist)
306       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
307       (nnfolder-read-folder group)))
308   t)
309
310 (deffoo nnfolder-request-list (&optional server)
311   (nnfolder-possibly-change-group nil server)
312   (save-excursion
313     (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
314       (nnmail-find-file nnfolder-active-file)
315       (setq nnfolder-group-alist (nnmail-get-active)))
316     t))
317
318 (deffoo nnfolder-request-newgroups (date &optional server)
319   (nnfolder-possibly-change-group nil server)
320   (nnfolder-request-list server))
321
322 (deffoo nnfolder-request-list-newsgroups (&optional server)
323   (nnfolder-possibly-change-group nil server)
324   (save-excursion
325     (let ((nnmail-file-coding-system nnfolder-file-coding-system))
326       (nnmail-find-file nnfolder-newsgroups-file))))
327
328 ;; Return a list consisting of all article numbers existing in the
329 ;; current folder.
330
331 (defun nnfolder-existing-articles ()
332   (save-excursion
333     (when nnfolder-current-buffer
334       (set-buffer nnfolder-current-buffer)
335       (goto-char (point-min))
336       (let ((marker (concat "\n" nnfolder-article-marker))
337             (number "[0-9]+")
338             numbers)
339       
340         (while (and (search-forward marker nil t)
341                     (re-search-forward number nil t))
342           (let ((newnum (string-to-number (match-string 0))))
343             (if (nnmail-within-headers-p)
344                 (push newnum numbers))))
345         numbers))))
346
347 (deffoo nnfolder-request-expire-articles
348     (articles newsgroup &optional server force)
349   (nnfolder-possibly-change-group newsgroup server)
350   (let* ((is-old t)
351          ;; The articles we have deleted so far.
352          (deleted-articles nil)
353          ;; The articles that really exist and will
354          ;; be expired if they are old enough.
355          (maybe-expirable
356           (gnus-intersection articles (nnfolder-existing-articles))))
357     (nnmail-activate 'nnfolder)
358
359     (save-excursion
360       (set-buffer nnfolder-current-buffer)
361       ;; Since messages are sorted in arrival order and expired in the
362       ;; same order, we can stop as soon as we find a message that is
363       ;; too old.
364       (while (and maybe-expirable is-old)
365         (goto-char (point-min))
366         (when (and (nnfolder-goto-article (car maybe-expirable))
367                    (search-forward (concat "\n" nnfolder-article-marker)
368                                    nil t))
369           (forward-sexp)
370           (when (setq is-old
371                       (nnmail-expired-article-p
372                        newsgroup
373                        (buffer-substring
374                         (point) (progn (end-of-line) (point)))
375                        force nnfolder-inhibit-expiry))
376             (nnheader-message 5 "Deleting article %d..."
377                               (car maybe-expirable) newsgroup)
378             (nnfolder-delete-mail)
379             (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
380               (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
381             ;; Must remember which articles were actually deleted
382             (push (car maybe-expirable) deleted-articles)))
383         (setq maybe-expirable (cdr maybe-expirable)))
384       (unless nnfolder-inhibit-expiry
385         (nnheader-message 5 "Deleting articles...done"))
386       (nnfolder-save-buffer)
387       (nnfolder-adjust-min-active newsgroup)
388       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
389       (gnus-sorted-complement articles (nreverse deleted-articles)))))
390
391 (deffoo nnfolder-request-move-article (article group server
392                                                accept-form &optional last)
393   (save-excursion
394     (let ((buf (get-buffer-create " *nnfolder move*"))
395           result)
396       (and
397        (nnfolder-request-article article group server)
398        (save-excursion
399          (set-buffer buf)
400          (erase-buffer)
401          (insert-buffer-substring nntp-server-buffer)
402          (goto-char (point-min))
403          (while (re-search-forward
404                  (concat "^" nnfolder-article-marker)
405                  (save-excursion (and (search-forward "\n\n" nil t) (point))) 
406                  t)
407            (delete-region (progn (beginning-of-line) (point))
408                           (progn (forward-line 1) (point))))
409          (setq result (eval accept-form))
410          (kill-buffer buf)
411          result)
412        (save-excursion
413          (nnfolder-possibly-change-group group server)
414          (set-buffer nnfolder-current-buffer)
415          (goto-char (point-min))
416          (when (nnfolder-goto-article article)
417            (nnfolder-delete-mail))
418          (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
419            (nnfolder-nov-delete-article group article))
420          (when last
421            (nnfolder-save-buffer)
422            (nnfolder-adjust-min-active group)
423            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
424       result)))
425
426 (deffoo nnfolder-request-accept-article (group &optional server last)
427   (save-excursion
428     (nnfolder-possibly-change-group group server)
429     (nnmail-check-syntax)
430     (let ((buf (current-buffer))
431           result art-group)
432       (goto-char (point-min))
433       (when (looking-at "X-From-Line: ")
434         (replace-match "From "))
435       (and
436        (nnfolder-request-list)
437        (save-excursion
438          (set-buffer buf)
439          (goto-char (point-min))
440          (if (search-forward "\n\n" nil t)
441              (forward-line -1)
442            (goto-char (point-max)))
443          (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
444            (delete-region (point) (progn (forward-line 1) (point))))
445          (when nnmail-cache-accepted-message-ids
446            (nnmail-cache-insert (nnmail-fetch-field "message-id")))
447          (setq result (if (stringp group)
448                           (list (cons group (nnfolder-active-number group)))
449                         (setq art-group
450                               (nnmail-article-group 'nnfolder-active-number))))
451          (if (and (null result)
452                   (yes-or-no-p "Moved to `junk' group; delete article? "))
453              (setq result 'junk)
454            (setq result
455                  (car (nnfolder-save-mail result)))))
456        (when last
457          (save-excursion
458            (nnfolder-possibly-change-folder (or (caar art-group) group))
459            (nnfolder-save-buffer)
460            (when nnmail-cache-accepted-message-ids
461              (nnmail-cache-close)))))
462       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
463       (unless result
464         (nnheader-report 'nnfolder "Couldn't store article"))
465       result)))
466
467 (deffoo nnfolder-request-replace-article (article group buffer)
468   (nnfolder-possibly-change-group group)
469   (save-excursion
470     (set-buffer buffer)
471     (goto-char (point-min))
472     (let (xfrom)
473       (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
474         (setq xfrom (match-string 1))
475         (gnus-delete-line))
476       (goto-char (point-min))
477       (if xfrom
478           (insert "From " xfrom "\n")
479         (unless (looking-at "From ")
480           (insert "From nobody " (current-time-string) "\n"))))
481     (nnfolder-normalize-buffer)
482     (set-buffer nnfolder-current-buffer)
483     (goto-char (point-min))
484     (if (not (nnfolder-goto-article article))
485         nil
486       (nnfolder-delete-mail)
487       (insert-buffer-substring buffer)
488       (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
489         (save-excursion
490           (set-buffer buffer)
491           (let ((headers (nnfolder-parse-head article 
492                                               (point-min) (point-max))))
493             (with-current-buffer (nnfolder-open-nov group)
494               (if (nnheader-find-nov-line article)
495                   (delete-region (point) (progn (forward-line 1) (point))))
496               (nnheader-insert-nov headers)))))
497       (nnfolder-save-buffer)
498       t)))
499
500 (deffoo nnfolder-request-delete-group (group &optional force server)
501   (nnfolder-close-group group server t)
502   ;; Delete all articles in GROUP.
503   (if (not force)
504       ()                                ; Don't delete the articles.
505     ;; Delete the file that holds the group.
506     (ignore-errors
507       (delete-file (nnfolder-group-pathname group))
508       (delete-file (nnfolder-group-nov-pathname group))))
509   ;; Remove the group from all structures.
510   (setq nnfolder-group-alist
511         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
512         nnfolder-current-group nil
513         nnfolder-current-buffer nil)
514   ;; Save the active file.
515   (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
516   t)
517
518 (deffoo nnfolder-request-rename-group (group new-name &optional server)
519   (nnfolder-possibly-change-group group server)
520   (save-excursion
521     (set-buffer nnfolder-current-buffer)
522     (and (file-writable-p buffer-file-name)
523          (ignore-errors
524            (let ((new-file (nnfolder-group-pathname new-name)))
525              (gnus-make-directory (file-name-directory new-file))
526              (rename-file buffer-file-name new-file)
527              (setq new-file (nnfolder-group-nov-pathname new-name))
528              (rename-file (nnfolder-group-nov-pathname group)
529                           new-file))
530            t)
531          ;; That went ok, so we change the internal structures.
532          (let ((entry (assoc group nnfolder-group-alist)))
533            (and entry (setcar entry new-name))
534            (setq nnfolder-current-buffer nil
535                  nnfolder-current-group nil)
536            ;; Save the new group alist.
537            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
538            ;; We kill the buffer instead of renaming it and stuff.
539            (kill-buffer (current-buffer))
540            t))))
541
542 (defun nnfolder-request-regenerate (server)
543   (nnfolder-possibly-change-group nil server)
544   (nnfolder-generate-active-file)
545   t)
546
547 \f
548 ;;; Internal functions.
549
550 (defun nnfolder-adjust-min-active (group)
551   ;; Find the lowest active article in this group.
552   (let* ((active (cadr (assoc group nnfolder-group-alist)))
553          (marker (concat "\n" nnfolder-article-marker))
554          (number "[0-9]+")
555          (activemin (cdr active)))
556     (save-excursion
557       (set-buffer nnfolder-current-buffer)
558       (goto-char (point-min))
559       (while (and (search-forward marker nil t)
560                   (re-search-forward number nil t))
561         (let ((newnum (string-to-number (match-string 0))))
562           (if (nnmail-within-headers-p)
563               (setq activemin (min activemin newnum)))))
564       (setcar active activemin))))
565
566 (defun nnfolder-article-string (article)
567   (if (numberp article)
568       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
569     (concat "\nMessage-ID: " article)))
570
571 (defun nnfolder-goto-article (article)
572   "Place point at the start of the headers of ARTICLE.
573 ARTICLE can be an article number or a Message-ID.
574 Returns t if successful, nil otherwise."
575   (let ((art-string (nnfolder-article-string article))
576         start found)
577     ;; It is likely that we are at or before the delimiter line.
578     ;; We therefore go to the end of the previous line, and start
579     ;; searching from there.
580     (beginning-of-line)
581     (unless (bobp)
582       (forward-char -1))
583     (setq start (point))
584     ;; First search forward.
585     (while (and (setq found (search-forward art-string nil t))
586                 (not (nnmail-within-headers-p))))
587     ;; If unsuccessful, search backward from where we started,
588     (unless found
589       (goto-char start)
590       (while (and (setq found (search-backward art-string nil t))
591                   (not (nnmail-within-headers-p)))))
592     (when found
593       (nnmail-search-unix-mail-delim-backward))))
594
595 (defun nnfolder-delete-mail (&optional leave-delim)
596   "Delete the message that point is in.
597 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
598 deleted.  Point is left where the deleted region was."
599   (save-restriction
600     (narrow-to-region
601      (save-excursion
602        ;; In case point is at the beginning of the message already.
603        (forward-line 1)
604        (nnmail-search-unix-mail-delim-backward)
605        (if leave-delim (progn (forward-line 1) (point))
606          (point)))
607      (progn
608        (forward-line 1)
609        (if (nnmail-search-unix-mail-delim)
610            (point)
611          (point-max))))
612     (run-hooks 'nnfolder-delete-mail-hook)
613     (delete-region (point-min) (point-max))))
614
615 (defun nnfolder-possibly-change-group (group &optional server dont-check)
616   ;; Change servers.
617   (when (and server
618              (not (nnfolder-server-opened server)))
619     (nnfolder-open-server server))
620   (unless (gnus-buffer-live-p nnfolder-current-buffer)
621     (setq nnfolder-current-buffer nil
622           nnfolder-current-group nil))
623   ;; Change group.
624   (when (and group
625              (not (equal group nnfolder-current-group)))
626     (let ((file-name-coding-system nnmail-pathname-coding-system)
627           (pathname-coding-system nnmail-pathname-coding-system))
628       (nnmail-activate 'nnfolder)
629       (when (and (not (assoc group nnfolder-group-alist))
630                  (not (file-exists-p
631                        (nnfolder-group-pathname group))))
632         ;; The group doesn't exist, so we create a new entry for it.
633         (push (list group (cons 1 0)) nnfolder-group-alist)
634         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
635
636       (if dont-check
637           (setq nnfolder-current-group group
638                 nnfolder-current-buffer nil)
639         (let (inf file)
640           ;; If we have to change groups, see if we don't already have the
641           ;; folder in memory.  If we do, verify the modtime and destroy
642           ;; the folder if needed so we can rescan it.
643           (setq nnfolder-current-buffer
644                 (nth 1 (assoc group nnfolder-buffer-alist)))
645
646           ;; If the buffer is not live, make sure it isn't in the alist.  If it
647           ;; is live, verify that nobody else has touched the file since last
648           ;; time.
649           (when (and nnfolder-current-buffer
650                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
651             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
652                   nnfolder-current-buffer nil))
653
654           (setq nnfolder-current-group group)
655
656           (when (or (not nnfolder-current-buffer)
657                     (not (verify-visited-file-modtime
658                           nnfolder-current-buffer)))
659             (save-excursion
660               (setq file (nnfolder-group-pathname group))
661               ;; See whether we need to create the new file.
662               (unless (file-exists-p file)
663                 (gnus-make-directory (file-name-directory file))
664                 (let ((nnmail-file-coding-system 
665                        (or nnfolder-file-coding-system-for-write
666                            nnfolder-file-coding-system-for-write)))
667                   (nnmail-write-region 1 1 file t 'nomesg)))
668               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
669                 (set-buffer nnfolder-current-buffer)
670                 (push (list group nnfolder-current-buffer)
671                       nnfolder-buffer-alist)))))))))
672
673 (defun nnfolder-save-mail (group-art-list)
674   "Called narrowed to an article."
675   (let* (save-list group-art)
676     (goto-char (point-min))
677     ;; The From line may have been quoted by movemail.
678     (when (looking-at ">From")
679       (delete-char 1))
680     ;; This might come from somewhere else.
681     (unless (looking-at "From ")
682       (insert "From nobody " (current-time-string) "\n")
683       (goto-char (point-min)))
684     ;; Quote all "From " lines in the article.
685     (forward-line 1)
686     (let (case-fold-search)
687       (while (re-search-forward "^From " nil t)
688         (beginning-of-line)
689         (insert "> ")))
690     (setq save-list group-art-list)
691     (nnmail-insert-lines)
692     (nnmail-insert-xref group-art-list)
693     (run-hooks 'nnmail-prepare-save-mail-hook)
694     (run-hooks 'nnfolder-prepare-save-mail-hook)
695
696     ;; Insert the mail into each of the destination groups.
697     (while (setq group-art (pop group-art-list))
698       ;; Kill any previous newsgroup markers.
699       (goto-char (point-min))
700       (if (search-forward "\n\n" nil t)
701           (forward-line -1)
702         (goto-char (point-max)))
703       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
704         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
705
706       ;; Insert the new newsgroup marker.
707       (nnfolder-insert-newsgroup-line group-art)
708
709       (save-excursion
710         (let ((beg (point-min))
711               (end (point-max))
712               (obuf (current-buffer)))
713           (nnfolder-possibly-change-folder (car group-art))
714           (let ((buffer-read-only nil))
715             (nnfolder-normalize-buffer)
716             (insert-buffer-substring obuf beg end))
717           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
718             (set-buffer obuf)
719             (nnfolder-add-nov (car group-art) (cdr group-art)
720                               (nnfolder-parse-head nil beg end))))))
721
722     ;; Did we save it anywhere?
723     save-list))
724
725 (defun nnfolder-normalize-buffer ()
726   "Make sure there are two newlines at the end of the buffer."
727   (goto-char (point-max))
728   (skip-chars-backward "\n")
729   (delete-region (point) (point-max))
730   (insert "\n\n"))
731
732 (defun nnfolder-insert-newsgroup-line (group-art)
733   (save-excursion
734     (goto-char (point-min))
735     (unless (search-forward "\n\n" nil t)
736       (goto-char (point-max))
737       (insert "\n"))
738     (forward-char -1)
739     (insert (format (concat nnfolder-article-marker "%d   %s\n")
740                     (cdr group-art) (current-time-string)))))
741
742 (defun nnfolder-active-number (group)
743   ;; Find the next article number in GROUP.
744   (let ((active (cadr (assoc group nnfolder-group-alist))))
745     (if active
746         (setcdr active (1+ (cdr active)))
747       ;; This group is new, so we create a new entry for it.
748       ;; This might be a bit naughty... creating groups on the drop of
749       ;; a hat, but I don't know...
750       (push (list group (setq active (cons 1 1)))
751             nnfolder-group-alist))
752     (cdr active)))
753
754 (defun nnfolder-possibly-change-folder (group)
755   (let ((inf (assoc group nnfolder-buffer-alist)))
756     (if (and inf
757              (gnus-buffer-live-p (cadr inf)))
758         (set-buffer (cadr inf))
759       (when inf
760         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
761       (when nnfolder-group-alist
762         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
763       (push (list group (nnfolder-read-folder group))
764             nnfolder-buffer-alist))))
765
766 ;; This method has a problem if you've accidentally let the active list get
767 ;; out of sync with the files.  This could happen, say, if you've
768 ;; accidentally gotten new mail with something other than Gnus (but why
769 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
770 ;; processing the file, ready to add new X-Gnus article number markers, and
771 ;; we'll run across a message with no ID yet - the active list _may_not_ be
772 ;; ready for us yet.
773
774 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
775 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
776 ;; rest of the message looking for any more, possibly higher IDs.  We'll
777 ;; assume the maximum that we find is the highest active.  Note that this
778 ;; shouldn't cost us much extra time at all, but will be a lot less
779 ;; vulnerable to glitches between the mbox and the active file.
780
781 (defun nnfolder-read-folder (group)
782   (let* ((file (nnfolder-group-pathname group))
783          (nov  (nnfolder-group-nov-pathname group))
784          (buffer (set-buffer
785                   (let ((nnheader-file-coding-system 
786                          nnfolder-file-coding-system))
787                     (nnheader-find-file-noselect file)))))
788     (if (equal (cadr (assoc group nnfolder-scantime-alist))
789                (nth 5 (file-attributes file)))
790         ;; This looks up-to-date, so we don't do any scanning.
791         (if (file-exists-p file)
792             buffer
793           (push (list group buffer) nnfolder-buffer-alist)
794           (set-buffer-modified-p t)
795           (nnfolder-save-buffer))
796       ;; Parse the damn thing.
797       (save-excursion
798         (goto-char (point-min))
799         ;; Remove any blank lines at the start.
800         (while (eq (following-char) ?\n)
801           (delete-char 1))
802         (nnmail-activate 'nnfolder)
803         ;; Read in the file.
804         (let ((delim "^From ")
805               (marker (concat "\n" nnfolder-article-marker))
806               (number "[0-9]+")
807               (active (or (cadr (assoc group nnfolder-group-alist))
808                           (cons 1 0)))
809               (scantime (assoc group nnfolder-scantime-alist))
810               (minid (lsh -1 -1))
811               maxid start end newscantime
812               novbuf articles newnum
813               buffer-read-only)
814           (buffer-disable-undo)
815           (setq maxid (cdr active))
816
817           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
818                       (and (file-exists-p nov)
819                            (file-newer-than-file-p nov file)))
820             (unless (file-exists-p nov)
821               (gnus-make-directory (file-name-directory nov)))
822             (with-current-buffer
823                 (setq novbuf (nnfolder-open-nov group))
824               (goto-char (point-min))
825               (while (not (eobp))
826                 (push (read novbuf) articles)
827                 (forward-line 1))
828               (setq articles (nreverse articles))))
829           (goto-char (point-min))
830
831           ;; Anytime the active number is 1 or 0, it is suspect.  In that
832           ;; case, search the file manually to find the active number.  Or,
833           ;; of course, if we're being paranoid.  (This would also be the
834           ;; place to build other lists from the header markers, such as
835           ;; expunge lists, etc., if we ever desired to abandon the active
836           ;; file entirely for mboxes.)
837           (when (or nnfolder-ignore-active-file
838                     novbuf
839                     (< maxid 2))
840             (while (and (search-forward marker nil t)
841                         (looking-at number))
842               (setq newnum (string-to-number (match-string 0)))
843               (when (nnmail-within-headers-p)
844                 (setq maxid (max maxid newnum)
845                       minid (min minid newnum))
846                 (when novbuf
847                   (if (memq newnum articles)
848                       (setq articles (delq newnum articles))
849                     (let ((headers (nnfolder-parse-head newnum)))
850                       (with-current-buffer novbuf
851                         (nnheader-find-nov-line newnum)
852                         (nnheader-insert-nov headers)))))))
853             (when (and novbuf articles)
854               (with-current-buffer novbuf
855                 (dolist (article articles)
856                   (when (nnheader-find-nov-line article)
857                     (delete-region (point) 
858                                    (progn (forward-line 1) (point)))))))
859             (setcar active (max 1 (min minid maxid)))
860             (setcdr active (max maxid (cdr active)))
861             (goto-char (point-min)))
862
863           ;; As long as we trust that the user will only insert unmarked mail
864           ;; at the end, go to the end and search backwards for the last
865           ;; marker.  Find the start of that message, and begin to search for
866           ;; unmarked messages from there.
867           (when (not (or nnfolder-distrust-mbox
868                          (< maxid 2)))
869             (goto-char (point-max))
870             (unless (re-search-backward marker nil t)
871               (goto-char (point-min)))
872             ;;(when (nnmail-search-unix-mail-delim)
873             ;;  (goto-char (point-min)))
874             )
875
876           ;; Keep track of the active number on our own, and insert it back
877           ;; into the active list when we're done.  Also, prime the pump to
878           ;; cut down on the number of searches we do.
879           (unless (nnmail-search-unix-mail-delim)
880             (goto-char (point-max)))
881           (setq end (point-marker))
882           (while (not (= end (point-max)))
883             (setq start (marker-position end))
884             (goto-char end)
885             ;; There may be more than one "From " line, so we skip past
886             ;; them.
887             (while (looking-at delim)
888               (forward-line 1))
889             (set-marker end (if (nnmail-search-unix-mail-delim)
890                                 (point)
891                               (point-max)))
892             (goto-char start)
893             (when (not (search-forward marker end t))
894               (narrow-to-region start end)
895               (nnmail-insert-lines)
896               (nnfolder-insert-newsgroup-line
897                (cons nil 
898                      (setq newnum
899                            (nnfolder-active-number nnfolder-current-group))))
900               (when novbuf
901                 (let ((headers (nnfolder-parse-head newnum (point-min)
902                                                       (point-max))))
903                   (with-current-buffer novbuf
904                     (goto-char (point-max))
905                     (nnheader-insert-nov headers))))
906               (widen)))
907
908           (set-marker end nil)
909           ;; Make absolutely sure that the active list reflects reality!
910           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
911
912           ;; Set the scantime for this group.
913           (setq newscantime (visited-file-modtime))
914           (if scantime
915               (setcdr scantime (list newscantime))
916             (push (list nnfolder-current-group newscantime)
917                   nnfolder-scantime-alist))
918           ;; Save nov.
919           (when novbuf
920             (nnfolder-save-nov))
921           (current-buffer))))))
922
923 ;;;###autoload
924 (defun nnfolder-generate-active-file ()
925   "Look for mbox folders in the nnfolder directory and make them into groups.
926 This command does not work if you use short group names."
927   (interactive)
928   (nnmail-activate 'nnfolder)
929   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
930     (dolist (file (directory-files (or nnfolder-nov-directory 
931                                        nnfolder-directory)
932                                    t 
933                                    (concat 
934                                     (regexp-quote nnfolder-nov-file-suffix)
935                                     "$")))
936       (when (not (message-mail-file-mbox-p file))
937         (ignore-errors
938           (delete-file file)))))
939   (let ((files (directory-files nnfolder-directory))
940         file)
941     (while (setq file (pop files))
942       (when (and (not (backup-file-name-p file))
943                  (message-mail-file-mbox-p
944                   (nnheader-concat nnfolder-directory file)))
945         (let ((oldgroup (assoc file nnfolder-group-alist)))
946           (if oldgroup
947               (nnheader-message 5 "Refreshing group %s..." file)
948             (nnheader-message 5 "Adding group %s..." file))
949           (if oldgroup
950               (setq nnfolder-group-alist
951                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
952           (push (list file (cons 1 0)) nnfolder-group-alist)
953           (nnfolder-possibly-change-folder file)
954           (nnfolder-possibly-change-group file)
955           (nnfolder-close-group file))))
956     (nnheader-message 5 "")))
957
958 (defun nnfolder-group-pathname (group)
959   "Make pathname for GROUP."
960   (setq group
961         (encode-coding-string group nnmail-pathname-coding-system))
962   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
963     ;; If this file exists, we use it directly.
964     (if (or nnmail-use-long-file-names
965             (file-exists-p (concat dir group)))
966         (concat dir group)
967       ;; If not, we translate dots into slashes.
968       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
969
970 (defun nnfolder-group-nov-pathname (group)
971   "Make pathname for GROUP NOV."
972   (let ((nnfolder-directory
973          (or nnfolder-nov-directory nnfolder-directory)))
974     (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
975
976 (defun nnfolder-save-buffer ()
977   "Save the buffer."
978   (when (buffer-modified-p)
979     (run-hooks 'nnfolder-save-buffer-hook)
980     (gnus-make-directory (file-name-directory (buffer-file-name)))
981     (let* ((coding-system-for-write
982             (or nnfolder-file-coding-system-for-write
983                 nnfolder-file-coding-system))
984            (output-coding-system coding-system-for-write))
985       (save-buffer)))
986   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
987     (nnfolder-save-nov)))
988
989 (defun nnfolder-save-active (group-alist active-file)
990   (let ((nnmail-active-file-coding-system
991          (or nnfolder-active-file-coding-system-for-write
992              nnfolder-active-file-coding-system)))
993     (nnmail-save-active group-alist active-file)))
994
995 (defun nnfolder-open-nov (group)
996   (or (cdr (assoc group nnfolder-nov-buffer-alist))
997       (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
998         (save-excursion
999           (set-buffer buffer)
1000           (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1001                (nnfolder-group-nov-pathname group))
1002           (erase-buffer)
1003           (when (file-exists-p nnfolder-nov-buffer-file-name)
1004             (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1005         (push (cons group buffer) nnfolder-nov-buffer-alist)
1006         buffer)))
1007
1008 (defun nnfolder-save-nov ()
1009   (save-excursion
1010     (while nnfolder-nov-buffer-alist
1011       (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1012         (set-buffer (cdar nnfolder-nov-buffer-alist))
1013         (when (buffer-modified-p)
1014           (gnus-make-directory (file-name-directory 
1015                                 nnfolder-nov-buffer-file-name))
1016           (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1017                                nil 'nomesg))
1018         (set-buffer-modified-p nil)
1019         (kill-buffer (current-buffer)))
1020       (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1021
1022 (defun nnfolder-nov-delete-article (group article)
1023   (save-excursion
1024     (set-buffer (nnfolder-open-nov group))
1025     (when (nnheader-find-nov-line article)
1026       (delete-region (point) (progn (forward-line 1) (point))))
1027     t))
1028
1029 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1030   (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1031       nil
1032     (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1033       (when (file-exists-p nov)
1034         (save-excursion
1035           (set-buffer nntp-server-buffer)
1036           (erase-buffer)
1037           (nnheader-insert-file-contents nov)
1038           (if (and fetch-old
1039                    (not (numberp fetch-old)))
1040               t                         ; Don't remove anything.
1041             (nnheader-nov-delete-outside-range
1042              (if fetch-old (max 1 (- (car articles) fetch-old))
1043                (car articles))
1044              (car (last articles)))
1045             t))))))
1046
1047 (defun nnfolder-parse-head (&optional number b e)
1048   "Parse the head of the current buffer."
1049   (let ((buf (current-buffer))
1050         chars)
1051   (save-excursion
1052     (unless b
1053       (setq b (if (nnmail-search-unix-mail-delim-backward)
1054                   (point) (point-min)))
1055       (forward-line 1)
1056       (setq e (if (nnmail-search-unix-mail-delim)
1057                   (point) (point-max))))
1058     (setq chars (- e b))
1059     (unless (zerop chars)
1060       (goto-char b)
1061       (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1062     (with-temp-buffer
1063       (insert-buffer-substring buf b e)
1064       ;; Fold continuation lines.
1065       (goto-char (point-min))
1066       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1067         (replace-match " " t t))
1068       ;; Remove any tabs; they are too confusing.
1069       (subst-char-in-region (point-min) (point-max) ?\t ? )
1070       (let ((headers (nnheader-parse-head t)))
1071         (mail-header-set-chars headers chars)
1072         (mail-header-set-number headers number)
1073         headers)))))
1074
1075 (defun nnfolder-add-nov (group article headers)
1076   "Add a nov line for the GROUP base."
1077   (save-excursion
1078     (set-buffer (nnfolder-open-nov group))
1079     (goto-char (point-max))
1080     (mail-header-set-number headers article)
1081     (nnheader-insert-nov headers)))
1082
1083 (provide 'nnfolder)
1084
1085 ;;; nnfolder.el ends here