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