*** 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 (articles newsgroup &optional server force)
299   (nnfolder-possibly-change-group newsgroup)
300   (let* ((days (or (and nnmail-expiry-wait-function
301                         (funcall nnmail-expiry-wait-function newsgroup))
302                    nnmail-expiry-wait))
303          rest)
304     (save-excursion 
305       (set-buffer nnfolder-current-buffer)
306       (while articles
307         (goto-char (point-min))
308         (if (search-forward (nnfolder-article-string (car articles)) nil t)
309             (if (or force
310                     (> (nnmail-days-between 
311                         (current-time-string)
312                         (buffer-substring 
313                          (point) (progn (end-of-line) (point))))
314                        days))
315                 (progn
316                   (and gnus-verbose-backends
317                        (message "Deleting: %s" (car articles)))
318                   (nnfolder-delete-mail))
319               (setq rest (cons (car articles) rest))))
320         (setq articles (cdr articles)))
321       (and (buffer-modified-p) (save-buffer))
322       ;; Find the lowest active article in this group.
323       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
324              (marker (concat "\n" nnfolder-article-marker))
325              (number "[0-9]+")
326              (activemin (cdr active)))
327         (goto-char (point-min))
328         (while (and (search-forward marker nil t)
329                     (re-search-forward number nil t))
330           (setq activemin (min activemin
331                                (string-to-number (buffer-substring
332                                                   (match-beginning 0)
333                                                   (match-end 0))))))
334         (setcar active activemin))
335       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
336       rest)))
337
338 (defun nnfolder-request-move-article
339   (article group server accept-form &optional last)
340   (nnfolder-possibly-change-group group)
341   (let ((buf (get-buffer-create " *nnfolder move*"))
342         result)
343     (and 
344      (nnfolder-request-article article group server)
345      (save-excursion
346        (set-buffer buf)
347        (buffer-disable-undo (current-buffer))
348        (erase-buffer)
349        (insert-buffer-substring nntp-server-buffer)
350        (goto-char (point-min))
351        (while (re-search-forward 
352                "^X-Gnus-Newsgroup:" 
353                (save-excursion (search-forward "\n\n" nil t) (point)) t)
354          (delete-region (progn (beginning-of-line) (point))
355                         (progn (forward-line 1) (point))))
356        (setq result (eval accept-form))
357        (kill-buffer buf)
358        result)
359      (save-excursion
360        (nnfolder-possibly-change-group group)
361        (set-buffer nnfolder-current-buffer)
362        (goto-char (point-min))
363        (if (search-forward (nnfolder-article-string article) nil t)
364            (nnfolder-delete-mail))
365        (and last 
366             (buffer-modified-p)
367             (save-buffer))))
368     result))
369
370 (defun nnfolder-request-accept-article (group &optional last)
371   (nnfolder-possibly-change-group group)
372   (let ((buf (current-buffer))
373         result)
374     (goto-char (point-min))
375     (if (looking-at "X-From-Line: ")
376         (replace-match "From ")
377       (insert "From nobody " (current-time-string) "\n"))
378     (and 
379      (nnfolder-request-list)
380      (save-excursion
381        (set-buffer buf)
382        (goto-char (point-min))
383        (search-forward "\n\n" nil t)
384        (forward-line -1)
385        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
386          (delete-region (point) (progn (forward-line 1) (point))))
387        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
388      (save-excursion
389        (set-buffer nnfolder-current-buffer)
390        (and last (buffer-modified-p) (save-buffer))))
391     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
392     result))
393
394 (defun nnfolder-request-replace-article (article group buffer)
395   (nnfolder-possibly-change-group group)
396   (save-excursion
397     (set-buffer nnfolder-current-buffer)
398     (goto-char (point-min))
399     (if (not (search-forward (nnfolder-article-string article) nil t))
400         nil
401       (nnfolder-delete-mail t t)
402       (insert-buffer-substring buffer)
403       (and (buffer-modified-p) (save-buffer))
404       t)))
405
406 \f
407 ;;; Internal functions.
408
409 (defun nnfolder-delete-mail (&optional force leave-delim)
410   ;; Beginning of the article.
411   (save-excursion
412     (save-restriction
413       (narrow-to-region
414        (save-excursion
415          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
416          (if leave-delim (progn (forward-line 1) (point))
417            (match-beginning 0)))
418        (progn
419          (forward-line 1)
420          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
421                                      nil t)
422                   (if (and (not (bobp)) leave-delim)
423                       (progn (forward-line -2) (point))
424                     (match-beginning 0)))
425              (point-max))))
426       (delete-region (point-min) (point-max)))))
427
428 (defun nnfolder-possibly-change-group (group)
429   (or (file-exists-p nnfolder-directory)
430       (make-directory (directory-file-name nnfolder-directory)))
431   (nnfolder-possibly-activate-groups nil)
432   (or (assoc group nnfolder-group-alist)
433       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
434                                   group)))
435       (progn
436         (setq nnfolder-group-alist 
437               (cons (list group (cons 1 0)) nnfolder-group-alist))
438         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
439   (let (inf file)
440     (if (and (equal group nnfolder-current-group)
441              nnfolder-current-buffer
442              (buffer-name nnfolder-current-buffer))
443         ()
444       (setq nnfolder-current-group group)
445
446       ;; If we have to change groups, see if we don't already have the mbox
447       ;; in memory.  If we do, verify the modtime and destroy the mbox if
448       ;; needed so we can rescan it.
449       (if (setq inf (assoc group nnfolder-buffer-alist))
450           (setq nnfolder-current-buffer (nth 1 inf)))
451
452       ;; If the buffer is not live, make sure it isn't in the alist.  If it
453       ;; is live, verify that nobody else has touched the file since last
454       ;; time.
455       (if (or (not (and nnfolder-current-buffer
456                         (buffer-name nnfolder-current-buffer)))
457               (not (and (bufferp nnfolder-current-buffer)
458                         (verify-visited-file-modtime 
459                          nnfolder-current-buffer))))
460           (progn
461             (if (and nnfolder-current-buffer
462                      (buffer-name nnfolder-current-buffer)
463                      (bufferp nnfolder-current-buffer))
464                 (kill-buffer nnfolder-current-buffer))
465             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
466             (setq inf nil)))
467       
468       (if inf
469           ()
470         (save-excursion
471           (setq file (concat (file-name-as-directory nnfolder-directory)
472                              group))
473           (if (file-directory-p (file-truename file))
474               ()
475             (if (not (file-exists-p file))
476                 (write-region 1 1 file t 'nomesg))
477             (setq nnfolder-current-buffer 
478                   (set-buffer (nnfolder-read-folder file)))
479             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
480                                               nnfolder-buffer-alist)))))))
481   (setq nnfolder-current-group group))
482
483 (defun nnfolder-save-mail (&optional group)
484   "Called narrowed to an article."
485   (let* ((nnmail-split-methods 
486           (if group (list (list group "")) nnmail-split-methods))
487          (group-art-list
488           (nreverse (nnmail-article-group 'nnfolder-active-number)))
489          save-list group-art)
490     (setq save-list group-art-list)
491     (nnmail-insert-lines)
492     (nnmail-insert-xref group-art-list)
493     (run-hooks 'nnfolder-prepare-save-mail-hook)
494
495     ;; Insert the mail into each of the destination groups.
496     (while group-art-list
497       (setq group-art (car group-art-list)
498             group-art-list (cdr group-art-list))
499
500       ;; Kill the previous newsgroup markers.
501       (goto-char (point-min))
502       (search-forward "\n\n" nil t)
503       (forward-line -1)
504       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
505         (delete-region (point) (progn (forward-line 1) (point))))
506
507       ;; Insert the new newsgroup marker.
508       (nnfolder-possibly-change-group (car group-art))
509       (nnfolder-insert-newsgroup-line group-art)
510       (let ((beg (point-min))
511             (end (point-max))
512             (obuf (current-buffer)))
513         (set-buffer nnfolder-current-buffer)
514         (goto-char (point-max))
515         (insert-buffer-substring obuf beg end)
516         (set-buffer obuf)))
517
518     ;; Did we save it anywhere?
519     save-list))
520
521 (defun nnfolder-insert-newsgroup-line (group-art)
522   (save-excursion
523     (goto-char (point-min))
524     (if (search-forward "\n\n" nil t)
525         (progn
526           (forward-char -1)
527           (insert (format (concat nnfolder-article-marker "%d   %s\n")
528                           (cdr group-art) (current-time-string)))))))
529
530 (defun nnfolder-possibly-activate-groups (&optional group)
531   (save-excursion
532     ;; If we're looking for the activation of a specific group, find out
533     ;; it's real name and switch to it.
534     (if group (nnfolder-possibly-change-group group))
535     ;; If the group alist isn't active, activate it now.
536     (if (not nnfolder-group-alist)
537         (progn
538           (nnfolder-request-list)
539           (setq nnfolder-group-alist (nnmail-get-active))))))
540
541 (defun nnfolder-active-number (group)
542   (save-excursion 
543     (nnfolder-possibly-activate-groups group)
544     (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
545       (setcdr active (1+ (cdr active)))
546       (cdr active))))
547
548 ;; This method has a problem if you've accidentally let the active list get
549 ;; out of sync with the files.  This could happen, say, if you've
550 ;; accidentally gotten new mail with something other than (ding) (but why
551 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
552 ;; processing the file, ready to add new X-Gnus article number markers, and
553 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
554 ;; ready for us yet.
555
556 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
557 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
558 ;; rest of the message looking for any more, possibly higher IDs.  We'll
559 ;; assume the maximum that we find is the highest active.  Note that this
560 ;; shouldn't cost us much extra time at all, but will be a lot less
561 ;; vulnerable to glitches between the mbox and the active file.
562
563 (defun nnfolder-read-folder (file)
564   (save-excursion
565     (nnfolder-possibly-activate-groups nil)
566     ;; We should be paranoid here and make sure the group is in the alist,
567     ;; and add it if it isn't.
568     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
569     (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
570     (buffer-disable-undo (current-buffer))
571     (let ((delim (concat "^" rmail-unix-mail-delimiter))
572           (marker (concat "\n" nnfolder-article-marker))
573           (number "[0-9]+")
574           (active (car (cdr (assoc nnfolder-current-group 
575                                    nnfolder-group-alist))))
576           activenumber activemin start end)
577       (goto-char (point-min))
578       ;;
579       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
580       ;; search the file manually to find the active number.  Or, of course,
581       ;; if we're being paranoid.  (This would also be the place to build
582       ;; other lists from the header markers, such as expunge lists, etc., if
583       ;; we ever desired to abandon the active file entirely for mboxes.)
584       (setq activenumber (cdr active))
585       (if (or nnfolder-ignore-active-file
586               (< activenumber 2))
587           (progn
588             (setq activemin (max (1- (lsh 1 23)) 
589                                  (1- (lsh 1 24)) 
590                                  (1- (lsh 1 25))))
591             (while (and (search-forward marker nil t)
592                         (re-search-forward number nil t))
593               (let ((newnum (string-to-number (buffer-substring
594                                               (match-beginning 0)
595                                               (match-end 0)))))
596                 (setq activenumber (max activenumber newnum))
597                 (setq activemin (min activemin newnum))))
598             (setcar active (min activemin activenumber))
599             (setcdr active activenumber)
600             (goto-char (point-min))))
601
602       ;; Keep track of the active number on our own, and insert it back into
603       ;; the active list when we're done. Also, prime the pump to cut down on
604       ;; the number of searches we do.
605       (setq end (point-marker))
606       (set-marker end (or (and (re-search-forward delim nil t)
607                                (match-beginning 0))
608                           (point-max)))
609       (while (not (= end (point-max)))
610         (setq start (marker-position end))
611         (goto-char end)
612         (end-of-line)
613         (set-marker end (or (and (re-search-forward delim nil t)
614                                  (match-beginning 0))
615                             (point-max)))
616         (goto-char start)
617         (if (not (search-forward marker end t))
618             (progn
619               (narrow-to-region start end)
620               (nnmail-insert-lines)
621               (setq activenumber (1+ activenumber))
622               (nnfolder-insert-newsgroup-line (cons nil activenumber))
623               (widen))))
624
625       ;; Make absolutely sure that the active list reflects reality!
626       (setcdr active activenumber)
627       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
628       (current-buffer))))
629
630 (defun nnfolder-get-new-mail (&optional group)
631   "Read new incoming mail."
632   (let* ((spools (nnmail-get-spool-files group))
633          (group-in group)
634          incomings incoming)
635     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
636         ()
637       ;; We first activate all the groups.
638       (nnfolder-possibly-activate-groups nil)
639       ;; The we go through all the existing spool files and split the
640       ;; mail from each.
641       (while spools
642         (and
643          (file-exists-p (car spools))
644          (> (nth 7 (file-attributes (car spools))) 0)
645          (progn
646            (and gnus-verbose-backends 
647                 (message "nnfolder: Reading incoming mail..."))
648            (setq incoming 
649                  (nnmail-move-inbox 
650                   (car spools) 
651                   (concat (file-name-as-directory nnfolder-directory)
652                           "Incoming")))
653            (setq incomings (cons incoming incomings))
654            (setq group (nnmail-get-split-group (car spools) group-in))
655            (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
656         (setq spools (cdr spools)))
657       ;; If we did indeed read any incoming spools, we save all info. 
658       (if incoming 
659           (progn
660             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
661             (run-hooks 'nnmail-read-incoming-hook)
662             (and gnus-verbose-backends
663                  (message "nnfolder: Reading incoming mail...done"))))
664       (let ((bufs nnfolder-buffer-alist))
665         (save-excursion
666           (while bufs
667             (if (not (buffer-name (nth 1 (car bufs))))
668                 (setq nnfolder-buffer-alist 
669                       (delq (car bufs) nnfolder-buffer-alist))
670               (set-buffer (nth 1 (car bufs)))
671               (and (buffer-modified-p) (save-buffer)))
672             (setq bufs (cdr bufs)))))
673       (while incomings
674         (and 
675          nnmail-delete-incoming
676          (file-writable-p incoming)
677          (delete-file incoming))
678         (setq incomings (cdr incomings))))))
679
680 (provide 'nnfolder)
681
682 ;;; nnfolder.el ends here