(dgnushack-install-package): Don't install dgnuspath.el nor dgnuspath.elc.
[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 (deffoo nnfolder-request-expire-articles
301   (articles newsgroup &optional server force)
302   (nnfolder-possibly-change-group newsgroup server)
303   (let* ((is-old t)
304          rest)
305     (nnmail-activate 'nnfolder)
306
307     (save-excursion
308       (set-buffer nnfolder-current-buffer)
309       (while (and articles is-old)
310         (goto-char (point-min))
311         (when (and (nnfolder-goto-article (car articles))
312                    (search-forward (concat "\n" nnfolder-article-marker)
313                                    nil t))
314           (forward-sexp)
315           (if (setq is-old
316                     (nnmail-expired-article-p
317                      newsgroup
318                      (buffer-substring
319                       (point) (progn (end-of-line) (point)))
320                      force nnfolder-inhibit-expiry))
321               (progn
322                 (nnheader-message 5 "Deleting article %d..."
323                                   (car articles) newsgroup)
324                 (nnfolder-delete-mail))
325             (push (car articles) rest)))
326         (setq articles (cdr articles)))
327       (unless nnfolder-inhibit-expiry
328         (nnheader-message 5 "Deleting articles...done"))
329       (nnfolder-save-buffer)
330       (nnfolder-adjust-min-active newsgroup)
331       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
332       (nconc rest articles))))
333
334 (deffoo nnfolder-request-move-article (article group server
335                                                accept-form &optional last)
336   (save-excursion
337     (let ((buf (get-buffer-create " *nnfolder move*"))
338           result)
339       (and
340        (nnfolder-request-article article group server)
341        (save-excursion
342          (set-buffer buf)
343          (erase-buffer)
344          (insert-buffer-substring nntp-server-buffer)
345          (goto-char (point-min))
346          (while (re-search-forward
347                  (concat "^" nnfolder-article-marker)
348                  (save-excursion (search-forward "\n\n" nil t) (point)) t)
349            (delete-region (progn (beginning-of-line) (point))
350                           (progn (forward-line 1) (point))))
351          (setq result (eval accept-form))
352          (kill-buffer buf)
353          result)
354        (save-excursion
355          (nnfolder-possibly-change-group group server)
356          (set-buffer nnfolder-current-buffer)
357          (goto-char (point-min))
358          (when (nnfolder-goto-article article)
359            (nnfolder-delete-mail))
360          (when last
361            (nnfolder-save-buffer)
362            (nnfolder-adjust-min-active group)
363            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
364       result)))
365
366 (deffoo nnfolder-request-accept-article (group &optional server last)
367   (save-excursion
368     (nnfolder-possibly-change-group group server)
369     (nnmail-check-syntax)
370     (let ((buf (current-buffer))
371           result art-group)
372       (goto-char (point-min))
373       (when (looking-at "X-From-Line: ")
374         (replace-match "From "))
375       (and
376        (nnfolder-request-list)
377        (save-excursion
378          (set-buffer buf)
379          (goto-char (point-min))
380          (search-forward "\n\n" nil t)
381          (forward-line -1)
382          (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
383            (delete-region (point) (progn (forward-line 1) (point))))
384          (when nnmail-cache-accepted-message-ids
385            (nnmail-cache-insert (nnmail-fetch-field "message-id")))
386          (setq result (if (stringp group)
387                           (list (cons group (nnfolder-active-number group)))
388                         (setq art-group
389                               (nnmail-article-group 'nnfolder-active-number))))
390          (if (and (null result)
391                   (yes-or-no-p "Moved to `junk' group; delete article? "))
392              (setq result 'junk)
393            (setq result
394                  (car (nnfolder-save-mail result)))))
395        (when last
396          (save-excursion
397            (nnfolder-possibly-change-folder (or (caar art-group) group))
398            (nnfolder-save-buffer)
399            (when nnmail-cache-accepted-message-ids
400              (nnmail-cache-close)))))
401       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
402       (unless result
403         (nnheader-report 'nnfolder "Couldn't store article"))
404       result)))
405
406 (deffoo nnfolder-request-replace-article (article group buffer)
407   (nnfolder-possibly-change-group group)
408   (save-excursion
409     (set-buffer buffer)
410     (goto-char (point-min))
411     (let (xfrom)
412       (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
413         (setq xfrom (match-string 1))
414         (gnus-delete-line))
415       (goto-char (point-min))
416       (if xfrom
417           (insert "From " xfrom "\n")
418         (unless (looking-at "From ")
419           (insert "From nobody " (current-time-string) "\n"))))
420     (nnfolder-normalize-buffer)
421     (set-buffer nnfolder-current-buffer)
422     (goto-char (point-min))
423     (if (not (nnfolder-goto-article article))
424         nil
425       (nnfolder-delete-mail)
426       (insert-buffer-substring buffer)
427       (nnfolder-save-buffer)
428       t)))
429
430 (deffoo nnfolder-request-delete-group (group &optional force server)
431   (nnfolder-close-group group server t)
432   ;; Delete all articles in GROUP.
433   (if (not force)
434       ()                                ; Don't delete the articles.
435     ;; Delete the file that holds the group.
436     (ignore-errors
437       (delete-file (nnfolder-group-pathname group))))
438   ;; Remove the group from all structures.
439   (setq nnfolder-group-alist
440         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
441         nnfolder-current-group nil
442         nnfolder-current-buffer nil)
443   ;; Save the active file.
444   (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
445   t)
446
447 (deffoo nnfolder-request-rename-group (group new-name &optional server)
448   (nnfolder-possibly-change-group group server)
449   (save-excursion
450     (set-buffer nnfolder-current-buffer)
451     (and (file-writable-p buffer-file-name)
452          (ignore-errors
453            (rename-file
454             buffer-file-name
455             (let ((new-file (nnfolder-group-pathname new-name)))
456               (gnus-make-directory (file-name-directory new-file))
457               new-file))
458            t)
459          ;; That went ok, so we change the internal structures.
460          (let ((entry (assoc group nnfolder-group-alist)))
461            (and entry (setcar entry new-name))
462            (setq nnfolder-current-buffer nil
463                  nnfolder-current-group nil)
464            ;; Save the new group alist.
465            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
466            ;; We kill the buffer instead of renaming it and stuff.
467            (kill-buffer (current-buffer))
468            t))))
469
470 (defun nnfolder-request-regenerate (server)
471   (nnfolder-possibly-change-group nil server)
472   (nnfolder-generate-active-file)
473   t)
474
475 \f
476 ;;; Internal functions.
477
478 (defun nnfolder-adjust-min-active (group)
479   ;; Find the lowest active article in this group.
480   (let* ((active (cadr (assoc group nnfolder-group-alist)))
481          (marker (concat "\n" nnfolder-article-marker))
482          (number "[0-9]+")
483          (activemin (cdr active)))
484     (save-excursion
485       (set-buffer nnfolder-current-buffer)
486       (goto-char (point-min))
487       (while (and (search-forward marker nil t)
488                   (re-search-forward number nil t))
489         (let ((newnum (string-to-number (match-string 0))))
490           (if (nnmail-within-headers-p)
491               (setq activemin (min activemin newnum)))))
492       (setcar active activemin))))
493
494 (defun nnfolder-article-string (article)
495   (if (numberp article)
496       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
497     (concat "\nMessage-ID: " article)))
498
499 (defun nnfolder-goto-article (article)
500   "Place point at the start of the headers of ARTICLE.
501 ARTICLE can be an article number or a Message-ID.
502 Returns t if successful, nil otherwise."
503   (let ((art-string (nnfolder-article-string article))
504         start found)
505     ;; It is likely that we are at or before the delimiter line.
506     ;; We therefore go to the end of the previous line, and start
507     ;; searching from there.
508     (beginning-of-line)
509     (unless (bobp)
510       (forward-char -1))
511     (setq start (point))
512     ;; First search forward.
513     (while (and (setq found (search-forward art-string nil t))
514                 (not (nnmail-within-headers-p))))
515     ;; If unsuccessful, search backward from where we started,
516     (unless found
517       (goto-char start)
518       (while (and (setq found (search-backward art-string nil t))
519                   (not (nnmail-within-headers-p)))))
520     (when found
521       (nnmail-search-unix-mail-delim-backward))))
522
523 (defun nnfolder-delete-mail (&optional leave-delim)
524   "Delete the message that point is in.
525 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
526 deleted.  Point is left where the deleted region was."
527   (save-restriction
528     (narrow-to-region
529      (save-excursion
530        ;; In case point is at the beginning of the message already.
531        (forward-line 1)
532        (nnmail-search-unix-mail-delim-backward)
533        (if leave-delim (progn (forward-line 1) (point))
534          (point)))
535      (progn
536        (forward-line 1)
537        (if (nnmail-search-unix-mail-delim)
538            (point)
539          (point-max))))
540     (run-hooks 'nnfolder-delete-mail-hook)
541     (delete-region (point-min) (point-max))))
542
543 (defun nnfolder-possibly-change-group (group &optional server dont-check)
544   ;; Change servers.
545   (when (and server
546              (not (nnfolder-server-opened server)))
547     (nnfolder-open-server server))
548   (unless (gnus-buffer-live-p nnfolder-current-buffer)
549     (setq nnfolder-current-buffer nil
550           nnfolder-current-group nil))
551   ;; Change group.
552   (when (and group
553              (not (equal group nnfolder-current-group)))
554     (let ((pathname-coding-system nnmail-pathname-coding-system))
555       (nnmail-activate 'nnfolder)
556       (when (and (not (assoc group nnfolder-group-alist))
557                  (not (file-exists-p
558                        (nnfolder-group-pathname group))))
559         ;; The group doesn't exist, so we create a new entry for it.
560         (push (list group (cons 1 0)) nnfolder-group-alist)
561         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
562
563       (if dont-check
564           (setq nnfolder-current-group group
565                 nnfolder-current-buffer nil)
566         (let (inf file)
567           ;; If we have to change groups, see if we don't already have the
568           ;; folder in memory.  If we do, verify the modtime and destroy
569           ;; the folder if needed so we can rescan it.
570           (setq nnfolder-current-buffer
571                 (nth 1 (assoc group nnfolder-buffer-alist)))
572
573           ;; If the buffer is not live, make sure it isn't in the alist.  If it
574           ;; is live, verify that nobody else has touched the file since last
575           ;; time.
576           (when (and nnfolder-current-buffer
577                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
578             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
579                   nnfolder-current-buffer nil))
580
581           (setq nnfolder-current-group group)
582
583           (when (or (not nnfolder-current-buffer)
584                     (not (verify-visited-file-modtime
585                           nnfolder-current-buffer)))
586             (save-excursion
587               (setq file (nnfolder-group-pathname group))
588               ;; See whether we need to create the new file.
589               (unless (file-exists-p file)
590                 (gnus-make-directory (file-name-directory file))
591                 (let ((nnmail-file-coding-system 
592                        (or nnfolder-file-coding-system-for-write
593                            nnfolder-file-coding-system)))
594                   (nnmail-write-region 1 1 file t 'nomesg)))
595               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
596                 (set-buffer nnfolder-current-buffer)
597                 (push (list group nnfolder-current-buffer)
598                       nnfolder-buffer-alist)))))))))
599
600 (defun nnfolder-save-mail (group-art-list)
601   "Called narrowed to an article."
602   (let* (save-list group-art)
603     (goto-char (point-min))
604     ;; The From line may have been quoted by movemail.
605     (when (looking-at ">From")
606       (delete-char 1))
607     ;; This might come from somewhere else.
608     (unless (looking-at "From ")
609       (insert "From nobody " (current-time-string) "\n")
610       (goto-char (point-min)))
611     ;; Quote all "From " lines in the article.
612     (forward-line 1)
613     (let (case-fold-search)
614       (while (re-search-forward "^From " nil t)
615         (beginning-of-line)
616         (insert "> ")))
617     (setq save-list group-art-list)
618     (nnmail-insert-lines)
619     (nnmail-insert-xref group-art-list)
620     (run-hooks 'nnmail-prepare-save-mail-hook)
621     (run-hooks 'nnfolder-prepare-save-mail-hook)
622
623     ;; Insert the mail into each of the destination groups.
624     (while (setq group-art (pop group-art-list))
625       ;; Kill any previous newsgroup markers.
626       (goto-char (point-min))
627       (search-forward "\n\n" nil t)
628       (forward-line -1)
629       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
630         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
631
632       ;; Insert the new newsgroup marker.
633       (nnfolder-insert-newsgroup-line group-art)
634
635       (save-excursion
636         (let ((beg (point-min))
637               (end (point-max))
638               (obuf (current-buffer)))
639           (nnfolder-possibly-change-folder (car group-art))
640           (let ((buffer-read-only nil))
641             (nnfolder-normalize-buffer)
642             (insert-buffer-substring obuf beg end)))))
643
644     ;; Did we save it anywhere?
645     save-list))
646
647 (defun nnfolder-normalize-buffer ()
648   "Make sure there are two newlines at the end of the buffer."
649   (goto-char (point-max))
650   (skip-chars-backward "\n")
651   (delete-region (point) (point-max))
652   (insert "\n\n"))
653
654 (defun nnfolder-insert-newsgroup-line (group-art)
655   (save-excursion
656     (goto-char (point-min))
657     (when (search-forward "\n\n" nil t)
658       (forward-char -1)
659       (insert (format (concat nnfolder-article-marker "%d   %s\n")
660                       (cdr group-art) (current-time-string))))))
661
662 (defun nnfolder-active-number (group)
663   ;; Find the next article number in GROUP.
664   (let ((active (cadr (assoc group nnfolder-group-alist))))
665     (if active
666         (setcdr active (1+ (cdr active)))
667       ;; This group is new, so we create a new entry for it.
668       ;; This might be a bit naughty... creating groups on the drop of
669       ;; a hat, but I don't know...
670       (push (list group (setq active (cons 1 1)))
671             nnfolder-group-alist))
672     (cdr active)))
673
674 (defun nnfolder-possibly-change-folder (group)
675   (let ((inf (assoc group nnfolder-buffer-alist)))
676     (if (and inf
677              (gnus-buffer-live-p (cadr inf)))
678         (set-buffer (cadr inf))
679       (when inf
680         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
681       (when nnfolder-group-alist
682         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
683       (push (list group (nnfolder-read-folder group))
684             nnfolder-buffer-alist))))
685
686 ;; This method has a problem if you've accidentally let the active list get
687 ;; out of sync with the files.  This could happen, say, if you've
688 ;; accidentally gotten new mail with something other than Gnus (but why
689 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
690 ;; processing the file, ready to add new X-Gnus article number markers, and
691 ;; we'll run across a message with no ID yet - the active list _may_not_ be
692 ;; ready for us yet.
693
694 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
695 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
696 ;; rest of the message looking for any more, possibly higher IDs.  We'll
697 ;; assume the maximum that we find is the highest active.  Note that this
698 ;; shouldn't cost us much extra time at all, but will be a lot less
699 ;; vulnerable to glitches between the mbox and the active file.
700
701 (defun nnfolder-read-folder (group)
702   (let* ((file (nnfolder-group-pathname group))
703          (buffer (set-buffer
704                   (let ((nnheader-file-coding-system 
705                          nnfolder-file-coding-system))
706                     (nnheader-find-file-noselect file)))))
707     (if (equal (cadr (assoc group nnfolder-scantime-alist))
708                (nth 5 (file-attributes file)))
709         ;; This looks up-to-date, so we don't do any scanning.
710         (if (file-exists-p file)
711             buffer
712           (push (list group buffer) nnfolder-buffer-alist)
713           (set-buffer-modified-p t)
714           (save-buffer))
715       ;; Parse the damn thing.
716       (save-excursion
717         (goto-char (point-min))
718         ;; Remove any blank lines at the start.
719         (while (eq (following-char) ?\n)
720           (delete-char 1))
721         (nnmail-activate 'nnfolder)
722         ;; Read in the file.
723         (let ((delim "^From ")
724               (marker (concat "\n" nnfolder-article-marker))
725               (number "[0-9]+")
726               (active (or (cadr (assoc group nnfolder-group-alist))
727                           (cons 1 0)))
728               (scantime (assoc group nnfolder-scantime-alist))
729               (minid (lsh -1 -1))
730               maxid start end newscantime
731               buffer-read-only)
732           (buffer-disable-undo)
733           (setq maxid (cdr active))
734           (goto-char (point-min))
735
736           ;; Anytime the active number is 1 or 0, it is suspect.  In that
737           ;; case, search the file manually to find the active number.  Or,
738           ;; of course, if we're being paranoid.  (This would also be the
739           ;; place to build other lists from the header markers, such as
740           ;; expunge lists, etc., if we ever desired to abandon the active
741           ;; file entirely for mboxes.)
742           (when (or nnfolder-ignore-active-file
743                     (< maxid 2))
744             (while (and (search-forward marker nil t)
745                         (re-search-forward number nil t))
746               (let ((newnum (string-to-number (match-string 0))))
747                 (if (nnmail-within-headers-p)
748                     (setq maxid (max maxid newnum)
749                           minid (min minid newnum)))))
750             (setcar active (max 1 (min minid maxid)))
751             (setcdr active (max maxid (cdr active)))
752             (goto-char (point-min)))
753
754           ;; As long as we trust that the user will only insert unmarked mail
755           ;; at the end, go to the end and search backwards for the last
756           ;; marker.  Find the start of that message, and begin to search for
757           ;; unmarked messages from there.
758           (when (not (or nnfolder-distrust-mbox
759                          (< maxid 2)))
760             (goto-char (point-max))
761             (unless (re-search-backward marker nil t)
762               (goto-char (point-min)))
763             (when (nnmail-search-unix-mail-delim)
764               (goto-char (point-min))))
765
766           ;; Keep track of the active number on our own, and insert it back
767           ;; into the active list when we're done.  Also, prime the pump to
768           ;; cut down on the number of searches we do.
769           (unless (nnmail-search-unix-mail-delim)
770             (goto-char (point-max)))
771           (setq end (point-marker))
772           (while (not (= end (point-max)))
773             (setq start (marker-position end))
774             (goto-char end)
775             ;; There may be more than one "From " line, so we skip past
776             ;; them.
777             (while (looking-at delim)
778               (forward-line 1))
779             (set-marker end (if (nnmail-search-unix-mail-delim)
780                                 (point)
781                               (point-max)))
782             (goto-char start)
783             (when (not (search-forward marker end t))
784               (narrow-to-region start end)
785               (nnmail-insert-lines)
786               (nnfolder-insert-newsgroup-line
787                (cons nil (nnfolder-active-number nnfolder-current-group)))
788               (widen)))
789
790           (set-marker end nil)
791           ;; Make absolutely sure that the active list reflects reality!
792           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
793           ;; Set the scantime for this group.
794           (setq newscantime (visited-file-modtime))
795           (if scantime
796               (setcdr scantime (list newscantime))
797             (push (list nnfolder-current-group newscantime)
798                   nnfolder-scantime-alist))
799           (current-buffer))))))
800
801 ;;;###autoload
802 (defun nnfolder-generate-active-file ()
803   "Look for mbox folders in the nnfolder directory and make them into groups.
804 This command does not work if you use short group names."
805   (interactive)
806   (nnmail-activate 'nnfolder)
807   (let ((files (directory-files nnfolder-directory))
808         file)
809     (while (setq file (pop files))
810       (when (and (not (backup-file-name-p file))
811                  (message-mail-file-mbox-p
812                   (nnheader-concat nnfolder-directory file)))
813         (let ((oldgroup (assoc file nnfolder-group-alist)))
814           (if oldgroup
815               (nnheader-message 5 "Refreshing group %s..." file)
816             (nnheader-message 5 "Adding group %s..." file))
817           (if oldgroup
818               (setq nnfolder-group-alist
819                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
820           (push (list file (cons 1 0)) nnfolder-group-alist)
821           (nnfolder-possibly-change-folder file)
822           (nnfolder-possibly-change-group file)
823           (nnfolder-close-group file))))
824     (nnheader-message 5 "")))
825
826 (defun nnfolder-group-pathname (group)
827   "Make pathname for GROUP."
828   (setq group
829         (encode-coding-string group nnmail-pathname-coding-system))
830   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
831     ;; If this file exists, we use it directly.
832     (if (or nnmail-use-long-file-names
833             (file-exists-p (concat dir group)))
834         (concat dir group)
835       ;; If not, we translate dots into slashes.
836       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
837
838 (defun nnfolder-save-buffer ()
839   "Save the buffer."
840   (when (buffer-modified-p)
841     (run-hooks 'nnfolder-save-buffer-hook)
842     (gnus-make-directory (file-name-directory (buffer-file-name)))
843     (let ((coding-system-for-write 
844            (or nnfolder-file-coding-system-for-write
845                nnfolder-file-coding-system)))
846       (save-buffer))))
847
848 (defun nnfolder-save-active (group-alist active-file)
849   (let ((nnmail-active-file-coding-system
850          (or nnfolder-active-file-coding-system-for-write
851              nnfolder-active-file-coding-system)))
852     (nnmail-save-active group-alist active-file)))
853
854 (provide 'nnfolder)
855
856 ;;; nnfolder.el ends here