*** empty log message ***
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
31
32 ;;; Code:
33
34 (require 'nnheader)
35 (require 'rmail)
36 (require 'nnmail)
37
38 (defvar nnfolder-directory (expand-file-name "~/Mail/")
39   "The name of the mail box file in the users home directory.")
40
41 (defvar nnfolder-active-file 
42   (concat (file-name-as-directory nnfolder-directory) "active")
43   "The name of the active file.")
44
45 ;; I renamed this variable to somehting more in keeping with the general GNU
46 ;; style. -SLB
47
48 (defvar nnfolder-ignore-active-file nil
49   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
50 Note that the active file is still saved, but it's values are not
51 used.  This costs some extra time when scanning an mbox when opening
52 it.")
53
54 ;; Note that this variable may not be completely implemented yet. -SLB
55
56 (defvar nnfolder-always-close nil
57   "If non-nil, nnfolder attempts to only ever have one mbox open at a time.  
58 This is a straight space/performance trade off, as the mboxes will have to 
59 be scanned every time they are read in.  If nil (default), nnfolder will
60 attempt to keep the buffers around (saving the nnfolder's buffer upon group 
61 close, but not killing it), speeding some things up tremendously, especially
62 such things as moving mail.  All buffers always get killed upon server close.")
63
64 (defvar nnfolder-newsgroups-file 
65   (concat (file-name-as-directory  nnfolder-directory) "newsgroups")
66   "Mail newsgroups description file.")
67
68 (defvar nnfolder-get-new-mail t
69   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
70
71 (defvar nnfolder-prepare-save-mail-hook nil
72   "Hook run narrowed to an article before saving.")
73
74 \f
75
76 (defconst nnfolder-version "nnfolder 1.0"
77   "nnfolder version.")
78
79 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
80   "String used to demarcate what the article number for a message is.")
81
82 (defvar nnfolder-current-group nil)
83 (defvar nnfolder-current-buffer nil)
84 (defvar nnfolder-status-string "")
85 (defvar nnfolder-group-alist nil)
86 (defvar nnfolder-buffer-alist nil)
87 (defvar nnfolder-active-timestamp nil)
88
89 (defmacro nnfolder-article-string (article)
90   (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
91
92 \f
93
94 (defvar nnfolder-current-server nil)
95 (defvar nnfolder-server-alist nil)
96 (defvar nnfolder-server-variables 
97   (list 
98    (list 'nnfolder-directory nnfolder-directory)
99    (list 'nnfolder-active-file nnfolder-active-file)
100    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
101    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
102    '(nnfolder-current-group nil)
103    '(nnfolder-current-buffer nil)
104    '(nnfolder-status-string "")
105    '(nnfolder-group-alist nil)
106    '(nnfolder-buffer-alist nil)
107    '(nnfolder-active-timestamp nil)))
108
109 \f
110
111 ;;; Interface functions
112
113 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
114   (save-excursion
115     (set-buffer nntp-server-buffer)
116     (erase-buffer)
117     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
118           article art-string start stop)
119       (nnfolder-possibly-change-group newsgroup)
120       (set-buffer nnfolder-current-buffer)
121       (goto-char (point-min))
122       (if (stringp (car sequence))
123           'headers
124         (while sequence
125           (setq article (car sequence))
126           (setq art-string (nnfolder-article-string article))
127           (set-buffer nnfolder-current-buffer)
128           (if (or (search-forward art-string nil t)
129                   ;; Don't search the whole file twice!  Also, articles
130                   ;; probably have some locality by number, so searching
131                   ;; backwards will be faster.  Especially if we're at the
132                   ;; beginning of the buffer :-). -SLB
133                   (search-backward art-string nil t))
134               (progn
135                 (setq start (or (re-search-backward delim-string nil t)
136                                 (point)))
137                 (search-forward "\n\n" nil t)
138                 (setq stop (1- (point)))
139                 (set-buffer nntp-server-buffer)
140                 (insert (format "221 %d Article retrieved.\n" article))
141                 (insert-buffer-substring nnfolder-current-buffer start stop)
142                 (goto-char (point-max))
143                 (insert ".\n")))
144           (setq sequence (cdr sequence)))
145
146         ;; Fold continuation lines.
147         (set-buffer nntp-server-buffer)
148         (goto-char (point-min))
149         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
150           (replace-match " " t t))
151         'headers))))
152
153 (defun nnfolder-open-server (server &optional defs)
154   (nnheader-init-server-buffer)
155   (if (equal server nnfolder-current-server)
156       t
157     (if nnfolder-current-server
158         (setq nnfolder-server-alist 
159               (cons (list nnfolder-current-server
160                           (nnheader-save-variables nnfolder-server-variables))
161                     nnfolder-server-alist)))
162     (let ((state (assoc server nnfolder-server-alist)))
163       (if state 
164           (progn
165             (nnheader-restore-variables (nth 1 state))
166             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
167         (nnheader-set-init-variables nnfolder-server-variables defs)))
168     (setq nnfolder-current-server server)))
169
170 (defun nnfolder-close-server (&optional server)
171   t)
172
173 (defun nnfolder-server-opened (&optional server)
174   (and (equal server nnfolder-current-server)
175        nntp-server-buffer
176        (buffer-name nntp-server-buffer)))
177
178 (defun nnfolder-request-close ()
179   (let ((alist nnfolder-buffer-alist))
180     (while alist
181       (nnfolder-close-group (car (car alist)) nil t)
182       (setq alist (cdr alist))))
183   (setq nnfolder-buffer-alist nil
184         nnfolder-group-alist nil))
185
186 (defun nnfolder-status-message (&optional server)
187   nnfolder-status-string)
188
189 (defun nnfolder-request-article (article &optional newsgroup server buffer)
190   (nnfolder-possibly-change-group newsgroup)
191   (if (stringp article)
192       nil
193     (save-excursion
194       (set-buffer nnfolder-current-buffer)
195       (goto-char (point-min))
196       (if (search-forward (nnfolder-article-string article) nil t)
197           (let (start stop)
198             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
199             (setq start (point))
200             (forward-line 1)
201             (or (and (re-search-forward 
202                       (concat "^" rmail-unix-mail-delimiter) nil t)
203                      (forward-line -1))
204                 (goto-char (point-max)))
205             (setq stop (point))
206             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
207               (set-buffer nntp-server-buffer)
208               (erase-buffer)
209               (insert-buffer-substring nnfolder-current-buffer start stop)
210               (goto-char (point-min))
211               (while (looking-at "From ")
212                 (delete-char 5)
213                 (insert "X-From-Line: ")
214                 (forward-line 1))
215               t))))))
216
217 (defun nnfolder-request-group (group &optional server dont-check)
218   (save-excursion
219     (nnmail-activate 'nnfolder)
220     (nnfolder-possibly-change-group group)
221     (and (assoc group nnfolder-group-alist)
222          (progn
223            (if dont-check
224                t
225              (nnfolder-get-new-mail group)
226              (let* ((active (assoc group nnfolder-group-alist))
227                     (group (car active))
228                     (range (car (cdr active)))
229                     (minactive (car range))
230                     (maxactive (cdr range)))
231                ;; I've been getting stray 211 lines in my nnfolder active
232                ;; file.  So, let's make sure that doesn't happen. -SLB
233                (set-buffer nntp-server-buffer)
234                (erase-buffer)
235                (if (not active)
236                    ()
237                  (insert (format "211 %d %d %d %s\n" 
238                                  (1+ (- maxactive minactive))
239                                  minactive maxactive group))
240                  t)))))))
241
242 ;; Don't close the buffer if we're not shutting down the server.  This way,
243 ;; we can keep the buffer in the group buffer cache, and not have to grovel
244 ;; over the buffer again unless we add new mail to it or modify it in some
245 ;; way.
246
247 (defun nnfolder-close-group (group &optional server force)
248   ;; Make sure we _had_ the group open.
249   (if (or (assoc group nnfolder-buffer-alist)
250           (equal group nnfolder-current-group))
251       (progn
252         (nnfolder-possibly-change-group group)
253         (save-excursion
254           (set-buffer nnfolder-current-buffer)
255           ;; If the buffer was modified, write the file out now.
256           (and (buffer-modified-p) (save-buffer))
257           (if (or force
258                   nnfolder-always-close)
259               ;; If we're shutting the server down, we need to kill the
260               ;; buffer and remove it from the open buffer list.  Or, of
261               ;; course, if we're trying to minimize our space impact.
262               (progn
263                 (kill-buffer (current-buffer))
264                 (setq nnfolder-buffer-alist (delq (assoc group 
265                                                          nnfolder-buffer-alist)
266                                                   nnfolder-buffer-alist)))))))
267   (setq nnfolder-current-group nil
268         nnfolder-current-buffer nil)
269   t)
270
271 (defun nnfolder-request-create-group (group &optional server) 
272   (nnmail-activate 'nnfolder)
273   (or (assoc group nnfolder-group-alist)
274       (let (active)
275         (setq nnfolder-group-alist 
276               (cons (list group (setq active (cons 1 0)))
277                     nnfolder-group-alist))
278         (nnfolder-possibly-change-group group)
279         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
280   t)
281
282 (defun nnfolder-request-list (&optional server)
283   (if server (nnfolder-get-new-mail))
284   (save-excursion
285     (nnmail-find-file nnfolder-active-file)
286     (setq nnfolder-group-alist (nnmail-get-active))))
287
288 (defun nnfolder-request-newgroups (date &optional server)
289   (nnfolder-request-list server))
290
291 (defun nnfolder-request-list-newsgroups (&optional server)
292   (save-excursion
293     (nnmail-find-file nnfolder-newsgroups-file)))
294
295 (defun nnfolder-request-post (&optional server)
296   (mail-send-and-exit nil))
297
298 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
299
300 (defun nnfolder-request-expire-articles 
301   (articles newsgroup &optional server force)
302   (nnfolder-possibly-change-group newsgroup)
303   (let* ((days (or (and nnmail-expiry-wait-function
304                         (funcall nnmail-expiry-wait-function newsgroup))
305                    nnmail-expiry-wait))
306          (is-old t)
307          rest)
308     (nnmail-activate 'nnfolder)
309
310     (save-excursion 
311       (set-buffer nnfolder-current-buffer)
312       (while (and articles is-old)
313         (goto-char (point-min))
314         (if (search-forward (nnfolder-article-string (car articles)) nil t)
315             (if (or force
316                     (setq is-old
317                           (> (nnmail-days-between 
318                               (current-time-string)
319                               (buffer-substring 
320                                (point) (progn (end-of-line) (point))))
321                              days)))
322                 (progn
323                   (and gnus-verbose-backends
324                        (message "Deleting article %s..." (car articles)))
325                   (nnfolder-delete-mail))
326               (setq rest (cons (car articles) rest))))
327         (setq articles (cdr articles)))
328       (and (buffer-modified-p) (save-buffer))
329       ;; Find the lowest active article in this group.
330       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
331              (marker (concat "\n" nnfolder-article-marker))
332              (number "[0-9]+")
333              (activemin (cdr active)))
334         (goto-char (point-min))
335         (while (and (search-forward marker nil t)
336                     (re-search-forward number nil t))
337           (setq activemin (min activemin
338                                (string-to-number (buffer-substring
339                                                   (match-beginning 0)
340                                                   (match-end 0))))))
341         (setcar active activemin))
342       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
343       (nconc rest articles))))
344
345 (defun nnfolder-request-move-article
346   (article group server accept-form &optional last)
347   (nnfolder-possibly-change-group group)
348   (let ((buf (get-buffer-create " *nnfolder move*"))
349         result)
350     (and 
351      (nnfolder-request-article article group server)
352      (save-excursion
353        (set-buffer buf)
354        (buffer-disable-undo (current-buffer))
355        (erase-buffer)
356        (insert-buffer-substring nntp-server-buffer)
357        (goto-char (point-min))
358        (while (re-search-forward 
359                (concat "^" nnfolder-article-marker)
360                (save-excursion (search-forward "\n\n" nil t) (point)) t)
361          (delete-region (progn (beginning-of-line) (point))
362                         (progn (forward-line 1) (point))))
363        (setq result (eval accept-form))
364        (kill-buffer buf)
365        result)
366      (save-excursion
367        (nnfolder-possibly-change-group group)
368        (set-buffer nnfolder-current-buffer)
369        (goto-char (point-min))
370        (if (search-forward (nnfolder-article-string article) nil t)
371            (nnfolder-delete-mail))
372        (and last 
373             (buffer-modified-p)
374             (save-buffer))))
375     result))
376
377 (defun nnfolder-request-accept-article (group &optional last)
378   (and (stringp group) (nnfolder-possibly-change-group group))
379   (let ((buf (current-buffer))
380         result)
381     (goto-char (point-min))
382     (cond ((looking-at "X-From-Line: ")
383            (replace-match "From "))
384           ((not (looking-at "From "))
385            (insert "From nobody " (current-time-string) "\n")))
386     (and 
387      (nnfolder-request-list)
388      (save-excursion
389        (set-buffer buf)
390        (goto-char (point-min))
391        (search-forward "\n\n" nil t)
392        (forward-line -1)
393        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
394          (delete-region (point) (progn (forward-line 1) (point))))
395        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
396      (save-excursion
397        (set-buffer nnfolder-current-buffer)
398        (and last (buffer-modified-p) (save-buffer))))
399     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
400     result))
401
402 (defun nnfolder-request-replace-article (article group buffer)
403   (nnfolder-possibly-change-group group)
404   (save-excursion
405     (set-buffer nnfolder-current-buffer)
406     (goto-char (point-min))
407     (if (not (search-forward (nnfolder-article-string article) nil t))
408         nil
409       (nnfolder-delete-mail t t)
410       (insert-buffer-substring buffer)
411       (and (buffer-modified-p) (save-buffer))
412       t)))
413
414 \f
415 ;;; Internal functions.
416
417 (defun nnfolder-delete-mail (&optional force leave-delim)
418   ;; Beginning of the article.
419   (save-excursion
420     (save-restriction
421       (narrow-to-region
422        (save-excursion
423          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
424          (if leave-delim (progn (forward-line 1) (point))
425            (match-beginning 0)))
426        (progn
427          (forward-line 1)
428          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
429                                      nil t)
430                   (if (and (not (bobp)) leave-delim)
431                       (progn (forward-line -2) (point))
432                     (match-beginning 0)))
433              (point-max))))
434       (delete-region (point-min) (point-max)))))
435
436 (defun nnfolder-possibly-change-group (group)
437   (or (file-exists-p nnfolder-directory)
438       (make-directory (directory-file-name nnfolder-directory)))
439   (nnfolder-possibly-activate-groups nil)
440   (or (assoc group nnfolder-group-alist)
441       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
442                                   group)))
443       (progn
444         (setq nnfolder-group-alist 
445               (cons (list group (cons 1 0)) nnfolder-group-alist))
446         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
447   (let (inf file)
448     (if (and (equal group nnfolder-current-group)
449              nnfolder-current-buffer
450              (buffer-name nnfolder-current-buffer))
451         ()
452       (setq nnfolder-current-group group)
453
454       ;; If we have to change groups, see if we don't already have the mbox
455       ;; in memory.  If we do, verify the modtime and destroy the mbox if
456       ;; needed so we can rescan it.
457       (if (setq inf (assoc group nnfolder-buffer-alist))
458           (setq nnfolder-current-buffer (nth 1 inf)))
459
460       ;; If the buffer is not live, make sure it isn't in the alist.  If it
461       ;; is live, verify that nobody else has touched the file since last
462       ;; time.
463       (if (or (not (and nnfolder-current-buffer
464                         (buffer-name nnfolder-current-buffer)))
465               (not (and (bufferp nnfolder-current-buffer)
466                         (verify-visited-file-modtime 
467                          nnfolder-current-buffer))))
468           (progn
469             (if (and nnfolder-current-buffer
470                      (buffer-name nnfolder-current-buffer)
471                      (bufferp nnfolder-current-buffer))
472                 (kill-buffer nnfolder-current-buffer))
473             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
474             (setq inf nil)))
475       
476       (if inf
477           ()
478         (save-excursion
479           (setq file (concat (file-name-as-directory nnfolder-directory)
480                              group))
481           (if (file-directory-p (file-truename file))
482               ()
483             (if (not (file-exists-p file))
484                 (write-region 1 1 file t 'nomesg))
485             (setq nnfolder-current-buffer 
486                   (set-buffer (nnfolder-read-folder file)))
487             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
488                                               nnfolder-buffer-alist)))))))
489   (setq nnfolder-current-group group))
490
491 (defun nnfolder-save-mail (&optional group)
492   "Called narrowed to an article."
493   (let* ((nnmail-split-methods 
494           (if group (list (list group "")) nnmail-split-methods))
495          (group-art-list
496           (nreverse (nnmail-article-group 'nnfolder-active-number)))
497          save-list group-art)
498     (setq save-list group-art-list)
499     (nnmail-insert-lines)
500     (nnmail-insert-xref group-art-list)
501     (run-hooks 'nnfolder-prepare-save-mail-hook)
502
503     ;; Insert the mail into each of the destination groups.
504     (while group-art-list
505       (setq group-art (car group-art-list)
506             group-art-list (cdr group-art-list))
507
508       ;; Kill the previous newsgroup markers.
509       (goto-char (point-min))
510       (search-forward "\n\n" nil t)
511       (forward-line -1)
512       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
513         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
514
515       ;; Insert the new newsgroup marker.
516       (nnfolder-possibly-change-group (car group-art))
517       (nnfolder-insert-newsgroup-line group-art)
518       (let ((beg (point-min))
519             (end (point-max))
520             (obuf (current-buffer)))
521         (set-buffer nnfolder-current-buffer)
522         (goto-char (point-max))
523         (insert-buffer-substring obuf beg end)
524         (set-buffer obuf)))
525
526     ;; Did we save it anywhere?
527     save-list))
528
529 (defun nnfolder-insert-newsgroup-line (group-art)
530   (save-excursion
531     (goto-char (point-min))
532     (if (search-forward "\n\n" nil t)
533         (progn
534           (forward-char -1)
535           (insert (format (concat nnfolder-article-marker "%d   %s\n")
536                           (cdr group-art) (current-time-string)))))))
537
538 (defun nnfolder-possibly-activate-groups (&optional group)
539   (save-excursion
540     ;; If we're looking for the activation of a specific group, find out
541     ;; its real name and switch to it.
542     (if group (nnfolder-possibly-change-group group))
543     ;; If the group alist isn't active, activate it now.
544     (nnmail-activate 'nnfolder)))
545
546 (defun nnfolder-active-number (group)
547   (save-excursion 
548     ;; Find the next article number in GROUP.
549     (prog1
550         (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
551           (if active
552               (setcdr active (1+ (cdr active)))
553             ;; This group is new, so we create a new entry for it.
554             ;; This might be a bit naughty... creating groups on the drop of
555             ;; a hat, but I don't know...
556             (setq nnfolder-group-alist 
557                   (cons (list group (setq active (cons 1 1)))
558                         nnfolder-group-alist)))
559           (cdr active))
560       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
561       (nnfolder-possibly-activate-groups group)
562       )))
563
564
565 ;; This method has a problem if you've accidentally let the active list get
566 ;; out of sync with the files.  This could happen, say, if you've
567 ;; accidentally gotten new mail with something other than Gnus (but why
568 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
569 ;; processing the file, ready to add new X-Gnus article number markers, and
570 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
571 ;; ready for us yet.
572
573 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
574 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
575 ;; rest of the message looking for any more, possibly higher IDs.  We'll
576 ;; assume the maximum that we find is the highest active.  Note that this
577 ;; shouldn't cost us much extra time at all, but will be a lot less
578 ;; vulnerable to glitches between the mbox and the active file.
579
580 (defun nnfolder-read-folder (file)
581   (save-excursion
582     (nnfolder-possibly-activate-groups nil)
583     ;; We should be paranoid here and make sure the group is in the alist,
584     ;; and add it if it isn't.
585     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
586     (set-buffer (setq nnfolder-current-buffer 
587                       (nnheader-find-file-noselect file nil 'raw)))
588     (buffer-disable-undo (current-buffer))
589     (let ((delim (concat "^" rmail-unix-mail-delimiter))
590           (marker (concat "\n" nnfolder-article-marker))
591           (number "[0-9]+")
592           (active (car (cdr (assoc nnfolder-current-group 
593                                    nnfolder-group-alist))))
594           activenumber activemin start end)
595       (goto-char (point-min))
596       ;;
597       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
598       ;; search the file manually to find the active number.  Or, of course,
599       ;; if we're being paranoid.  (This would also be the place to build
600       ;; other lists from the header markers, such as expunge lists, etc., if
601       ;; we ever desired to abandon the active file entirely for mboxes.)
602       (setq activenumber (cdr active))
603       (if (or nnfolder-ignore-active-file
604               (< activenumber 2))
605           (progn
606             (setq activemin (max (1- (lsh 1 23)) 
607                                  (1- (lsh 1 24)) 
608                                  (1- (lsh 1 25))))
609             (while (and (search-forward marker nil t)
610                         (re-search-forward number nil t))
611               (let ((newnum (string-to-number (buffer-substring
612                                                (match-beginning 0)
613                                                (match-end 0)))))
614                 (setq activenumber (max activenumber newnum))
615                 (setq activemin (min activemin newnum))))
616             (setcar active (min activemin activenumber))
617             (setcdr active (max activenumber (cdr active)))
618             (goto-char (point-min))))
619
620       ;; Keep track of the active number on our own, and insert it back into
621       ;; the active list when we're done. Also, prime the pump to cut down on
622       ;; the number of searches we do.
623       (setq end (point-marker))
624       (set-marker end (or (and (re-search-forward delim nil t)
625                                (match-beginning 0))
626                           (point-max)))
627       (while (not (= end (point-max)))
628         (setq start (marker-position end))
629         (goto-char end)
630         ;; There may be more than one "From " line, so we skip past
631         ;; them.  
632         (while (looking-at delim) 
633           (forward-line 1))
634         (set-marker end (or (and (re-search-forward delim nil t)
635                                  (match-beginning 0))
636                             (point-max)))
637         (goto-char start)
638         (if (not (search-forward marker end t))
639             (progn
640               (narrow-to-region start end)
641               (nnmail-insert-lines)
642               (nnfolder-insert-newsgroup-line
643                (cons nil (nnfolder-active-number nnfolder-current-group)))
644               (widen))))
645
646       ;; Make absolutely sure that the active list reflects reality!
647       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
648       (current-buffer))))
649
650 (defun nnfolder-get-new-mail (&optional group)
651   "Read new incoming mail."
652   (let* ((spools (nnmail-get-spool-files group))
653          (group-in group)
654          incomings incoming)
655     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
656         ()
657       ;; We first activate all the groups.
658       (nnfolder-possibly-activate-groups nil)
659       ;; The we go through all the existing spool files and split the
660       ;; mail from each.
661       (while spools
662         (and
663          (file-exists-p (car spools))
664          (> (nth 7 (file-attributes (car spools))) 0)
665          (progn
666            (and gnus-verbose-backends 
667                 (message "nnfolder: Reading incoming mail..."))
668            (if (not (setq incoming 
669                           (nnmail-move-inbox 
670                            (car spools) 
671                            (concat (file-name-as-directory nnfolder-directory)
672                                    "Incoming"))))
673                ()
674              (setq incomings (cons incoming incomings))
675              (setq group (nnmail-get-split-group (car spools) group-in))
676              (nnmail-split-incoming incoming 'nnfolder-save-mail nil group))))
677         (setq spools (cdr spools)))
678       ;; If we did indeed read any incoming spools, we save all info. 
679       (if incoming 
680           (progn
681             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
682             (run-hooks 'nnmail-read-incoming-hook)
683             (and gnus-verbose-backends
684                  (message "nnfolder: Reading incoming mail...done"))))
685       (let ((bufs nnfolder-buffer-alist))
686         (save-excursion
687           (while bufs
688             (if (not (buffer-name (nth 1 (car bufs))))
689                 (setq nnfolder-buffer-alist 
690                       (delq (car bufs) nnfolder-buffer-alist))
691               (set-buffer (nth 1 (car bufs)))
692               (and (buffer-modified-p) (save-buffer)))
693             (setq bufs (cdr bufs)))))
694       (while incomings
695         (setq incoming (car incomings))
696         (and 
697          nnmail-delete-incoming
698          (file-writable-p incoming)
699          (file-exists-p incoming)
700          (delete-file incoming))
701         (setq incomings (cdr incomings))))))
702
703 (provide 'nnfolder)
704
705 ;;; nnfolder.el ends here