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