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