*** 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, nnml 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 0.2"
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
88 (defmacro nnfolder-article-string (article)
89   (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
90
91 \f
92
93 (defvar nnfolder-current-server nil)
94 (defvar nnfolder-server-alist nil)
95 (defvar nnfolder-server-variables 
96   (list 
97    (list 'nnfolder-directory nnfolder-directory)
98    (list 'nnfolder-active-file nnfolder-active-file)
99    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
100    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
101    '(nnfolder-current-group nil)
102    '(nnfolder-current-buffer nil)
103    '(nnfolder-status-string "")
104    '(nnfolder-group-alist nil)
105    '(nnfolder-buffer-alist nil)))
106
107 \f
108
109 ;;; Interface functions
110
111 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
112   (save-excursion
113     (set-buffer nntp-server-buffer)
114     (erase-buffer)
115     (let ((file nil)
116           (number (length sequence))
117           (delim-string (concat "^" rmail-unix-mail-delimiter))
118           beg 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                 (setq beg (point))
142                 (insert-buffer-substring nnfolder-current-buffer start stop)
143                 (goto-char (point-max))
144                 (insert ".\n")))
145           (setq sequence (cdr sequence)))
146
147         ;; Fold continuation lines.
148         (set-buffer nntp-server-buffer)
149         (goto-char (point-min))
150         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
151           (replace-match " " t t))
152         'headers))))
153
154 (defun nnfolder-open-server (server &optional defs)
155   (nnheader-init-server-buffer)
156   (if (equal server nnfolder-current-server)
157       t
158     (if nnfolder-current-server
159         (setq nnfolder-server-alist 
160               (cons (list nnfolder-current-server
161                           (nnheader-save-variables nnfolder-server-variables))
162                     nnfolder-server-alist)))
163     (let ((state (assoc server nnfolder-server-alist)))
164       (if state 
165           (progn
166             (nnheader-restore-variables (nth 1 state))
167             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
168         (nnheader-set-init-variables nnfolder-server-variables defs)))
169     (setq nnfolder-current-server server)))
170
171 (defun nnfolder-close-server (&optional server)
172   t)
173
174 (defun nnfolder-server-opened (&optional server)
175   (and (equal server nnfolder-current-server)
176        nntp-server-buffer
177        (buffer-name nntp-server-buffer)))
178
179 (defun nnfolder-request-close ()
180   (let ((alist nnfolder-buffer-alist))
181     (while alist
182       (nnfolder-close-group (car (car alist)) nil t)
183       (setq alist (cdr alist))))
184   (setq nnfolder-buffer-alist nil
185         nnfolder-group-alist nil))
186
187 (defun nnfolder-status-message (&optional server)
188   nnfolder-status-string)
189
190 (defun nnfolder-request-article (article &optional newsgroup server buffer)
191   (nnfolder-possibly-change-group newsgroup)
192   (if (stringp article)
193       nil
194     (save-excursion
195       (set-buffer nnfolder-current-buffer)
196       (goto-char (point-min))
197       (if (search-forward (nnfolder-article-string article) nil t)
198           (let (start stop)
199             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
200             (setq start (point))
201             (forward-line 1)
202             (or (and (re-search-forward 
203                       (concat "^" rmail-unix-mail-delimiter) nil t)
204                      (forward-line -1))
205                 (goto-char (point-max)))
206             (setq stop (point))
207             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
208               (set-buffer nntp-server-buffer)
209               (erase-buffer)
210               (insert-buffer-substring nnfolder-current-buffer start stop)
211               (goto-char (point-min))
212               (while (looking-at "From ")
213                 (delete-char 5)
214                 (insert "X-From-Line: ")
215                 (forward-line 1))
216               t))))))
217
218 (defun nnfolder-request-group (group &optional server dont-check)
219   (save-excursion
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              (insert (format "211 %d %d %d %s\n" 
236                              (1+ (- maxactive minactive))
237                              minactive maxactive group))
238              t)))))
239
240 ;; Don't close the buffer if we're not shutting down the server.  This way,
241 ;; we can keep the buffer in the group buffer cache, and not have to grovel
242 ;; over the buffer again unless we add new mail to it or modify it in some
243 ;; way.
244
245 (defun nnfolder-close-group (group &optional server force)
246   ;; Make sure we _had_ the group open.
247   (if (or (assoc group nnfolder-buffer-alist)
248           (equal group nnfolder-current-group))
249       (progn
250         (nnfolder-possibly-change-group group)
251         (save-excursion
252           (set-buffer nnfolder-current-buffer)
253           ;; If the buffer was modified, write the file out now.
254           (and (buffer-modified-p) (save-buffer))
255           (if (or force
256                   nnfolder-always-close)
257               ;; If we're shutting the server down, we need to kill the
258               ;; buffer and remove it from the open buffer list.  Or, of
259               ;; course, if we're trying to minimize our space impact.
260               (progn
261                 (kill-buffer (current-buffer))
262                 (setq nnfolder-buffer-alist (delq (assoc group 
263                                                          nnfolder-buffer-alist)
264                                                   nnfolder-buffer-alist)))))))
265   (setq nnfolder-current-group nil
266         nnfolder-current-buffer nil)
267   t)
268
269 (defun nnfolder-request-create-group (group &optional server) 
270   (nnfolder-request-list)
271   (setq nnfolder-group-alist (nnmail-get-active))
272   (or (assoc group nnfolder-group-alist)
273       (let (active)
274         (setq nnfolder-group-alist 
275               (cons (list group (setq active (cons 0 0)))
276                     nnfolder-group-alist))
277         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
278   t)
279
280 (defun nnfolder-request-list (&optional server)
281   (if server (nnfolder-get-new-mail))
282   (save-excursion
283     (or nnfolder-group-alist
284         (nnmail-find-file nnfolder-active-file)
285         (progn
286           (setq nnfolder-group-alist (nnmail-get-active))
287           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
288           (nnmail-find-file nnfolder-active-file)))))
289
290 (defun nnfolder-request-newgroups (date &optional server)
291   (nnfolder-request-list server))
292
293 (defun nnfolder-request-list-newsgroups (&optional server)
294   (save-excursion
295     (nnmail-find-file nnfolder-newsgroups-file)))
296
297 (defun nnfolder-request-post (&optional server)
298   (mail-send-and-exit nil))
299
300 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
301
302 (defun nnfolder-request-expire-articles (articles newsgroup &optional server force)
303   (nnfolder-possibly-change-group newsgroup)
304   (let* ((days (or (and nnmail-expiry-wait-function
305                         (funcall nnmail-expiry-wait-function newsgroup))
306                    nnmail-expiry-wait))
307          article rest)
308     (save-excursion 
309       (set-buffer nnfolder-current-buffer)
310       (while articles
311         (goto-char (point-min))
312         (if (search-forward (nnfolder-article-string (car articles)) nil t)
313             (if (or force
314                     (> (nnmail-days-between 
315                         (current-time-string)
316                         (buffer-substring 
317                          (point) (progn (end-of-line) (point))))
318                        days))
319                 (progn
320                   (and gnus-verbose-backends
321                        (message "Deleting: %s" (car articles)))
322                   (nnfolder-delete-mail))
323               (setq rest (cons (car articles) rest))))
324         (setq articles (cdr articles)))
325       (and (buffer-modified-p) (save-buffer))
326       ;; Find the lowest active article in this group.
327       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
328              (marker (concat "\n" nnfolder-article-marker))
329              (number "[0-9]+")
330              (activemin (cdr active)))
331         (goto-char (point-min))
332         (while (and (search-forward marker nil t)
333                     (re-search-forward number nil t))
334           (setq activemin (min activemin
335                                (string-to-number (buffer-substring
336                                                   (match-beginning 0)
337                                                   (match-end 0))))))
338         (setcar active activemin))
339       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
340       rest)))
341
342 (defun nnfolder-request-move-article
343   (article group server accept-form &optional last)
344   (nnfolder-possibly-change-group group)
345   (let ((buf (get-buffer-create " *nnfolder move*"))
346         result)
347     (and 
348      (nnfolder-request-article article group server)
349      (save-excursion
350        (set-buffer buf)
351        (buffer-disable-undo (current-buffer))
352        (erase-buffer)
353        (insert-buffer-substring nntp-server-buffer)
354        (goto-char (point-min))
355        (while (re-search-forward 
356                "^X-Gnus-Newsgroup:" 
357                (save-excursion (search-forward "\n\n" nil t) (point)) t)
358          (delete-region (progn (beginning-of-line) (point))
359                         (progn (forward-line 1) (point))))
360        (setq result (eval accept-form))
361        (kill-buffer buf)
362        result)
363      (save-excursion
364        (nnfolder-possibly-change-group group)
365        (set-buffer nnfolder-current-buffer)
366        (goto-char (point-min))
367        (if (search-forward (nnfolder-article-string article) nil t)
368            (nnfolder-delete-mail))
369        (and last 
370             (buffer-modified-p)
371             (save-buffer))))
372     result))
373
374 (defun nnfolder-request-accept-article (group &optional last)
375   (nnfolder-possibly-change-group group)
376   (let ((buf (current-buffer))
377         result beg)
378     (goto-char (point-min))
379     (if (looking-at "X-From-Line: ")
380         (replace-match "From ")
381       (insert "From nobody " (current-time-string) "\n"))
382     (and 
383      (nnfolder-request-list)
384      (save-excursion
385        (set-buffer buf)
386        (goto-char (point-min))
387        (search-forward "\n\n" nil t)
388        (forward-line -1)
389        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
390          (delete-region (point) (progn (forward-line 1) (point))))
391        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
392      (save-excursion
393        (set-buffer nnfolder-current-buffer)
394        (and last (buffer-modified-p) (save-buffer))))
395     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
396     result))
397
398 (defun nnfolder-request-replace-article (article group buffer)
399   (nnfolder-possibly-change-group group)
400   (save-excursion
401     (set-buffer nnfolder-current-buffer)
402     (goto-char (point-min))
403     (if (not (search-forward (nnfolder-article-string article) nil t))
404         nil
405       (nnfolder-delete-mail t t)
406       (insert-buffer-substring buffer)
407       (and (buffer-modified-p) (save-buffer))
408       t)))
409
410 \f
411 ;;; Internal functions.
412
413 (defun nnfolder-delete-mail (&optional force leave-delim)
414   ;; Beginning of the article.
415   (save-excursion
416     (save-restriction
417       (narrow-to-region
418        (save-excursion
419          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
420          (if leave-delim (progn (forward-line 1) (point))
421            (match-beginning 0)))
422        (progn
423          (forward-line 1)
424          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
425                                      nil t)
426                   (if (and (not (bobp)) leave-delim)
427                       (progn (forward-line -2) (point))
428                     (match-beginning 0)))
429              (point-max))))
430       (delete-region (point-min) (point-max)))))
431
432 (defun nnfolder-possibly-change-group (group)
433   (or (file-exists-p nnfolder-directory)
434       (make-directory (directory-file-name nnfolder-directory)))
435   (nnfolder-possibly-activate-groups nil)
436   (or (assoc group nnfolder-group-alist)
437       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
438                                   group)))
439       (progn
440         (setq nnfolder-group-alist 
441               (cons (list group (cons 1 0)) nnfolder-group-alist))
442         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
443   (let (inf file)
444     (if (and (equal group nnfolder-current-group)
445              nnfolder-current-buffer
446              (buffer-name nnfolder-current-buffer))
447         ()
448       (setq nnfolder-current-group group)
449
450       ;; If we have to change groups, see if we don't already have the mbox
451       ;; in memory.  If we do, verify the modtime and destroy the mbox if
452       ;; needed so we can rescan it.
453       (if (setq inf (assoc group nnfolder-buffer-alist))
454           (setq nnfolder-current-buffer (nth 1 inf)))
455
456       ;; If the buffer is not live, make sure it isn't in the alist.  If it
457       ;; is live, verify that nobody else has touched the file since last
458       ;; time.
459       (if (or (not (and nnfolder-current-buffer
460                         (buffer-name nnfolder-current-buffer)))
461               (not (and (bufferp nnfolder-current-buffer)
462                         (verify-visited-file-modtime 
463                          nnfolder-current-buffer))))
464           (progn
465             (if (and nnfolder-current-buffer
466                      (buffer-name nnfolder-current-buffer)
467                      (bufferp nnfolder-current-buffer))
468                 (kill-buffer nnfolder-current-buffer))
469             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
470             (setq inf nil)))
471       
472       (if inf
473           ()
474         (save-excursion
475           (setq file (concat (file-name-as-directory nnfolder-directory)
476                              group))
477           (if (file-directory-p (file-truename file))
478               ()
479             (if (not (file-exists-p file))
480                 (write-region 1 1 file t 'nomesg))
481             (setq nnfolder-current-buffer 
482                   (set-buffer (nnfolder-read-folder file)))
483             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
484                                               nnfolder-buffer-alist)))))))
485   (setq nnfolder-current-group group))
486
487 (defun nnfolder-save-mail (&optional group)
488   "Called narrowed to an article."
489   (let* ((nnmail-split-methods 
490           (if group (list (list group "")) nnmail-split-methods))
491          (group-art-list
492           (nreverse (nnmail-article-group 'nnfolder-active-number)))
493          save-list group-art)
494     (setq save-list group-art-list)
495     (nnmail-insert-lines)
496     (nnmail-insert-xref group-art-list)
497     (run-hooks 'nnfolder-prepare-save-mail-hook)
498
499     ;; Insert the mail into each of the destination groups.
500     (while group-art-list
501       (setq group-art (car group-art-list)
502             group-art-list (cdr group-art-list))
503
504       ;; Kill the previous newsgroup markers.
505       (goto-char (point-min))
506       (search-forward "\n\n" nil t)
507       (forward-line -1)
508       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
509         (delete-region (point) (progn (forward-line 1) (point))))
510
511       ;; Insert the new newsgroup marker.
512       (nnfolder-possibly-change-group (car group-art))
513       (nnfolder-insert-newsgroup-line group-art)
514       (let ((beg (point-min))
515             (end (point-max))
516             (obuf (current-buffer)))
517         (set-buffer nnfolder-current-buffer)
518         (goto-char (point-max))
519         (insert-buffer-substring obuf beg end)
520         (set-buffer obuf)))
521
522     ;; Did we save it anywhere?
523     save-list))
524
525 (defun nnfolder-insert-newsgroup-line (group-art)
526   (save-excursion
527     (goto-char (point-min))
528     (if (search-forward "\n\n" nil t)
529         (progn
530           (forward-char -1)
531           (insert (format (concat nnfolder-article-marker "%d   %s\n")
532                           (cdr group-art) (current-time-string)))))))
533
534 (defun nnfolder-possibly-activate-groups (&optional group)
535   (save-excursion
536     ;; If we're looking for the activation of a specific group, find out
537     ;; it's real name and switch to it.
538     (if group (nnfolder-possibly-change-group group))
539     ;; If the group alist isn't active, activate it now.
540     (if (not nnfolder-group-alist)
541         (progn
542           (nnfolder-request-list)
543           (setq nnfolder-group-alist (nnmail-get-active))))))
544
545 (defun nnfolder-active-number (group)
546   (save-excursion 
547     (nnfolder-possibly-activate-groups group)
548     (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
549       (setcdr active (1+ (cdr active)))
550       (cdr active))))
551
552 ;; This method has a problem if you've accidentally let the active list get
553 ;; out of sync with the files.  This could happen, say, if you've
554 ;; accidentally gotten new mail with something other than (ding) (but why
555 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
556 ;; processing the file, ready to add new X-Gnus article number markers, and
557 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
558 ;; ready for us yet.
559
560 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
561 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
562 ;; rest of the message looking for any more, possibly higher IDs.  We'll
563 ;; assume the maximum that we find is the highest active.  Note that this
564 ;; shouldn't cost us much extra time at all, but will be a lot less
565 ;; vulnerable to glitches between the mbox and the active file.
566
567 (defun nnfolder-read-folder (file)
568   (save-excursion
569     (nnfolder-possibly-activate-groups nil)
570     ;; We should be paranoid here and make sure the group is in the alist,
571     ;; and add it if it isn't.
572     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
573     (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
574     (buffer-disable-undo (current-buffer))
575     (let ((delim (concat "^" rmail-unix-mail-delimiter))
576           (marker (concat "\n" nnfolder-article-marker))
577           (number "[0-9]+")
578           (active (car (cdr (assoc nnfolder-current-group 
579                                    nnfolder-group-alist))))
580           activenumber activemin start end)
581       (goto-char (point-min))
582       ;;
583       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
584       ;; search the file manually to find the active number.  Or, of course,
585       ;; if we're being paranoid.  (This would also be the place to build
586       ;; other lists from the header markers, such as expunge lists, etc., if
587       ;; we ever desired to abandon the active file entirely for mboxes.)
588       (setq activenumber (cdr active))
589       (if (or nnfolder-ignore-active-file
590               (< activenumber 2))
591           (progn
592             (setq activemin (max (1- (lsh 1 23)) 
593                                  (1- (lsh 1 24)) 
594                                  (1- (lsh 1 25))))
595             (while (and (search-forward marker nil t)
596                         (re-search-forward number nil t))
597               (let ((newnum (string-to-number (buffer-substring
598                                               (match-beginning 0)
599                                               (match-end 0)))))
600                 (setq activenumber (max activenumber newnum))
601                 (setq activemin (min activemin newnum))))
602             (setcar active (min activemin activenumber))
603             (setcdr active activenumber)
604             (goto-char (point-min))))
605
606       ;; Keep track of the active number on our own, and insert it back into
607       ;; the active list when we're done. Also, prime the pump to cut down on
608       ;; the number of searches we do.
609       (setq end (point-marker))
610       (set-marker end (or (and (re-search-forward delim nil t)
611                                (match-beginning 0))
612                           (point-max)))
613       (while (not (= end (point-max)))
614         (setq start (marker-position end))
615         (goto-char end)
616         (end-of-line)
617         (set-marker end (or (and (re-search-forward delim nil t)
618                                  (match-beginning 0))
619                             (point-max)))
620         (goto-char start)
621         (if (not (search-forward marker end t))
622             (progn
623               (narrow-to-region start end)
624               (nnmail-insert-lines)
625               (setq activenumber (1+ activenumber))
626               (nnfolder-insert-newsgroup-line (cons nil activenumber))
627               (widen))))
628
629       ;; Make absolutely sure that the active list reflects reality!
630       (setcdr active activenumber)
631       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
632       (current-buffer))))
633
634 (defun nnfolder-get-new-mail (&optional group)
635   "Read new incoming mail."
636   (let* ((spools (nnmail-get-spool-files group))
637          (all-spools spools)
638          (group-in group)
639          incomings incoming)
640     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
641         ()
642       ;; We first activate all the groups.
643       (nnfolder-possibly-activate-groups nil)
644       ;; The we go through all the existing spool files and split the
645       ;; mail from each.
646       (while spools
647         (and
648          (file-exists-p (car spools))
649          (> (nth 7 (file-attributes (car spools))) 0)
650          (progn
651            (and gnus-verbose-backends 
652                 (message "nnfolder: Reading incoming mail..."))
653            (setq incoming 
654                  (nnmail-move-inbox 
655                   (car spools) 
656                   (concat (file-name-as-directory nnfolder-directory)
657                           "Incoming")))
658            (setq incomings (cons incoming incomings))
659            (setq group (nnmail-get-split-group (car spools) group-in))
660            (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
661         (setq spools (cdr spools)))
662       ;; If we did indeed read any incoming spools, we save all info. 
663       (if incoming 
664           (progn
665             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
666             (run-hooks 'nnmail-read-incoming-hook)
667             (and gnus-verbose-backends
668                  (message "nnfolder: Reading incoming mail...done"))))
669       (let ((bufs nnfolder-buffer-alist))
670         (save-excursion
671           (while bufs
672             (if (not (buffer-name (nth 1 (car bufs))))
673                 (setq nnfolder-buffer-alist 
674                       (delq (car bufs) nnfolder-buffer-alist))
675               (set-buffer (nth 1 (car bufs)))
676               (and (buffer-modified-p) (save-buffer)))
677             (setq bufs (cdr bufs)))))
678       (while incomings
679         (and 
680          nnmail-delete-incoming
681          (file-writable-p incoming)
682          (delete-file incoming))
683         (setq incomings (cdr incomings))))))
684
685 (provide 'nnfolder)
686
687 ;;; nnfolder.el ends here