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