*** 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 (concat nnfolder-directory "active")
42   "The name of the active file.")
43
44 ;; I renamed this variable to somehting more in keeping with the general GNU
45 ;; style. -SLB
46
47 (defvar nnfolder-ignore-active-file nil
48   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
49 Note that the active file is still saved, but it's values are not
50 used.  This costs some extra time when scanning an mbox when opening
51 it.")
52
53 ;; Note that this variable may not be completely implemented yet. -SLB
54
55 (defvar nnfolder-always-close nil
56   "If non-nil, nnfolder attempts to only ever have one mbox open at a time.  
57 This is a straight space/performance trade off, as the mboxes will have to 
58 be scanned every time they are read in.  If nil (default), nnfolder will
59 attempt to keep the buffers around (saving the nnfolder's buffer upon group 
60 close, but not killing it), speeding some things up tremendously, especially
61 such things as moving mail.  All buffers always get killed upon server close.")
62
63 (defvar nnfolder-newsgroups-file (concat nnfolder-directory "newsgroups")
64   "Mail newsgroups description file.")
65
66 (defvar nnfolder-get-new-mail t
67   "If non-nil, nnml will check the incoming mail file and split the mail.")
68
69 (defvar nnfolder-prepare-save-mail-hook nil
70   "Hook run narrowed to an article before saving.")
71
72 \f
73
74 (defconst nnfolder-version "nnfolder 0.2"
75   "nnfolder version.")
76
77 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
78   "String used to demarcate what the article number for a message is.")
79
80 (defvar nnfolder-current-group nil)
81 (defvar nnfolder-current-buffer nil)
82 (defvar nnfolder-status-string "")
83 (defvar nnfolder-group-alist nil)
84 (defvar nnfolder-buffer-alist nil)
85
86 (defmacro nnfolder-article-string (article)
87   (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
88
89 \f
90
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables 
94   (list 
95    (list 'nnfolder-directory nnfolder-directory)
96    (list 'nnfolder-active-file nnfolder-active-file)
97    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99    '(nnfolder-current-group nil)
100    '(nnfolder-current-buffer nil)
101    '(nnfolder-status-string "")
102    '(nnfolder-group-alist nil)
103    '(nnfolder-buffer-alist nil)))
104
105 \f
106
107 ;;; Interface functions
108
109 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
110   (save-excursion
111     (set-buffer nntp-server-buffer)
112     (erase-buffer)
113     (let ((file nil)
114           (number (length sequence))
115           (delim-string (concat "^" rmail-unix-mail-delimiter))
116           beg 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                 (setq beg (point))
140                 (insert-buffer-substring nnfolder-current-buffer start stop)
141                 (goto-char (point-max))
142                 (insert ".\n")))
143           (setq sequence (cdr sequence)))
144
145         ;; Fold continuation lines.
146         (set-buffer nntp-server-buffer)
147         (goto-char (point-min))
148         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
149           (replace-match " " t t))
150         'headers))))
151
152 (defun nnfolder-open-server (server &optional defs)
153   (nnheader-init-server-buffer)
154   (if (equal server nnfolder-current-server)
155       t
156     (if nnfolder-current-server
157         (setq nnfolder-server-alist 
158               (cons (list nnfolder-current-server
159                           (nnheader-save-variables nnfolder-server-variables))
160                     nnfolder-server-alist)))
161     (let ((state (assoc server nnfolder-server-alist)))
162       (if state 
163           (progn
164             (nnheader-restore-variables (nth 1 state))
165             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
166         (nnheader-set-init-variables nnfolder-server-variables defs)))
167     (setq nnfolder-current-server server)))
168
169 (defun nnfolder-close-server (&optional server)
170   t)
171
172 (defun nnfolder-server-opened (&optional server)
173   (and (equal server nnfolder-current-server)
174        nntp-server-buffer
175        (buffer-name nntp-server-buffer)))
176
177 (defun nnfolder-request-close ()
178   (let ((alist nnfolder-buffer-alist))
179     (while alist
180       (nnfolder-close-group (car (car alist)) nil t)
181       (setq alist (cdr alist))))
182   (setq nnfolder-buffer-alist nil
183         nnfolder-group-alist nil))
184
185 (defun nnfolder-status-message (&optional server)
186   nnfolder-status-string)
187
188 (defun nnfolder-request-article (article &optional newsgroup server buffer)
189   (nnfolder-possibly-change-group newsgroup)
190   (if (stringp article)
191       nil
192     (save-excursion
193       (set-buffer nnfolder-current-buffer)
194       (goto-char (point-min))
195       (if (search-forward (nnfolder-article-string article) nil t)
196           (let (start stop)
197             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
198             (setq start (point))
199             (forward-line 1)
200             (or (and (re-search-forward 
201                       (concat "^" rmail-unix-mail-delimiter) nil t)
202                      (forward-line -1))
203                 (goto-char (point-max)))
204             (setq stop (point))
205             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
206               (set-buffer nntp-server-buffer)
207               (erase-buffer)
208               (insert-buffer-substring nnfolder-current-buffer start stop)
209               (goto-char (point-min))
210               (while (looking-at "From ")
211                 (delete-char 5)
212                 (insert "X-From-Line: ")
213                 (forward-line 1))
214               t))))))
215
216 (defun nnfolder-request-group (group &optional server dont-check)
217   (save-excursion
218     (nnfolder-possibly-change-group group)
219     (and (assoc group nnfolder-group-alist)
220          (progn
221            (if dont-check
222                t
223              (nnfolder-get-new-mail group))
224            (let* ((active (assoc group nnfolder-group-alist))
225                   (group (car active))
226                   (range (car (cdr active)))
227                   (minactive (car range))
228                   (maxactive (cdr range)))
229              ;; I've been getting stray 211 lines in my nnfolder active
230              ;; file.  So, let's make sure that doesn't happen. -SLB
231              (set-buffer nntp-server-buffer)
232              (erase-buffer)
233              (insert (format "211 %d %d %d %s\n" 
234                              (1+ (- maxactive minactive))
235                              minactive maxactive group))
236              t)))))
237
238 ;; Don't close the buffer if we're not shutting down the server.  This way,
239 ;; we can keep the buffer in the group buffer cache, and not have to grovel
240 ;; over the buffer again unless we add new mail to it or modify it in some
241 ;; way.
242
243 (defun nnfolder-close-group (group &optional server force)
244   ;; Make sure we _had_ the group open.
245   (if (or (assoc group nnfolder-buffer-alist)
246           (equal group nnfolder-current-group))
247       (progn
248         (nnfolder-possibly-change-group group)
249         (save-excursion
250           (set-buffer nnfolder-current-buffer)
251           ;; If the buffer was modified, write the file out now.
252           (and (buffer-modified-p) (save-buffer))
253           (if (or force
254                   nnfolder-always-close)
255               ;; If we're shutting the server down, we need to kill the
256               ;; buffer and remove it from the open buffer list.  Or, of
257               ;; course, if we're trying to minimize our space impact.
258               (progn
259                 (kill-buffer (current-buffer))
260                 (setq nnfolder-buffer-alist (delq (assoc group 
261                                                          nnfolder-buffer-alist)
262                                                   nnfolder-buffer-alist)))))))
263   (setq nnfolder-current-group nil
264         nnfolder-current-buffer nil)
265   t)
266
267 (defun nnfolder-request-create-group (group &optional server) 
268   (nnfolder-request-list)
269   (setq nnfolder-group-alist (nnmail-get-active))
270   (or (assoc group nnfolder-group-alist)
271       (let (active)
272         (setq nnfolder-group-alist 
273               (cons (list group (setq active (cons 0 0)))
274                     nnfolder-group-alist))
275         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
276   t)
277
278 (defun nnfolder-request-list (&optional server)
279   (if server (nnfolder-get-new-mail))
280   (save-excursion
281     (or nnfolder-group-alist
282         (nnmail-find-file nnfolder-active-file)
283         (progn
284           (setq nnfolder-group-alist (nnmail-get-active))
285           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
286           (nnmail-find-file nnfolder-active-file)))))
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 (articles newsgroup &optional server force)
301   (nnfolder-possibly-change-group newsgroup)
302   (let* ((days (or (and nnmail-expiry-wait-function
303                         (funcall nnmail-expiry-wait-function newsgroup))
304                    nnmail-expiry-wait))
305          article 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                     (> (nnmail-days-between 
313                         (current-time-string)
314                         (buffer-substring 
315                          (point) (progn (end-of-line) (point))))
316                        days))
317                 (progn
318                   (and gnus-verbose-backends
319                        (message "Deleting: %s" (car articles)))
320                   (nnfolder-delete-mail))
321               (setq rest (cons (car articles) rest))))
322         (setq articles (cdr articles)))
323       (and (buffer-modified-p) (save-buffer))
324       ;; Find the lowest active article in this group.
325       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
326              (marker (concat "\n" nnfolder-article-marker))
327              (number "[0-9]+")
328              (activemin (cdr active)))
329         (goto-char (point-min))
330         (while (and (search-forward marker nil t)
331                     (re-search-forward number nil t))
332           (setq activemin (min activemin
333                                (string-to-number (buffer-substring
334                                                   (match-beginning 0)
335                                                   (match-end 0))))))
336         (setcar active activemin))
337       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
338       rest)))
339
340 (defun nnfolder-request-move-article
341   (article group server accept-form &optional last)
342   (nnfolder-possibly-change-group group)
343   (let ((buf (get-buffer-create " *nnfolder move*"))
344         result)
345     (and 
346      (nnfolder-request-article article group server)
347      (save-excursion
348        (set-buffer buf)
349        (buffer-disable-undo (current-buffer))
350        (erase-buffer)
351        (insert-buffer-substring nntp-server-buffer)
352        (goto-char (point-min))
353        (while (re-search-forward 
354                "^X-Gnus-Newsgroup:" 
355                (save-excursion (search-forward "\n\n" nil t) (point)) t)
356          (delete-region (progn (beginning-of-line) (point))
357                         (progn (forward-line 1) (point))))
358        (setq result (eval accept-form))
359        (kill-buffer buf)
360        result)
361      (save-excursion
362        (nnfolder-possibly-change-group group)
363        (set-buffer nnfolder-current-buffer)
364        (goto-char (point-min))
365        (if (search-forward (nnfolder-article-string article) nil t)
366            (nnfolder-delete-mail))
367        (and last 
368             (buffer-modified-p)
369             (save-buffer))))
370     result))
371
372 (defun nnfolder-request-accept-article (group &optional last)
373   (nnfolder-possibly-change-group group)
374   (let ((buf (current-buffer))
375         result beg)
376     (goto-char (point-min))
377     (if (looking-at "X-From-Line: ")
378         (replace-match "From ")
379       (insert "From nobody " (current-time-string) "\n"))
380     (and 
381      (nnfolder-request-list)
382      (save-excursion
383        (set-buffer buf)
384        (goto-char (point-min))
385        (search-forward "\n\n" nil t)
386        (forward-line -1)
387        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
388          (delete-region (point) (progn (forward-line 1) (point))))
389        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
390      (save-excursion
391        (set-buffer nnfolder-current-buffer)
392        (and last (buffer-modified-p) (save-buffer))))
393     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
394     result))
395
396 (defun nnfolder-request-replace-article (article group buffer)
397   (nnfolder-possibly-change-group group)
398   (save-excursion
399     (set-buffer nnfolder-current-buffer)
400     (goto-char (point-min))
401     (if (not (search-forward (nnfolder-article-string article) nil t))
402         nil
403       (nnfolder-delete-mail t t)
404       (insert-buffer-substring buffer)
405       (and (buffer-modified-p) (save-buffer))
406       t)))
407
408 \f
409 ;;; Internal functions.
410
411 (defun nnfolder-delete-mail (&optional force leave-delim)
412   ;; Beginning of the article.
413   (save-excursion
414     (save-restriction
415       (narrow-to-region
416        (save-excursion
417          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
418          (if leave-delim (progn (forward-line 1) (point))
419            (match-beginning 0)))
420        (progn
421          (forward-line 1)
422          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
423                                      nil t)
424                   (if (and (not (bobp)) leave-delim)
425                       (progn (forward-line -2) (point))
426                     (match-beginning 0)))
427              (point-max))))
428       (delete-region (point-min) (point-max)))))
429
430 (defun nnfolder-possibly-change-group (group)
431   (or (file-exists-p nnfolder-directory)
432       (make-directory (directory-file-name nnfolder-directory)))
433   (nnfolder-possibly-activate-groups nil)
434   (or (assoc group nnfolder-group-alist)
435       (not (file-exists-p (concat nnfolder-directory group)))
436       (progn
437         (setq nnfolder-group-alist 
438               (cons (list group (cons 1 0)) nnfolder-group-alist))
439         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
440   (let (inf file)
441     (if (and (equal group nnfolder-current-group)
442              nnfolder-current-buffer
443              (buffer-name nnfolder-current-buffer))
444         ()
445       (setq nnfolder-current-group group)
446
447       ;; If we have to change groups, see if we don't already have the mbox
448       ;; in memory.  If we do, verify the modtime and destroy the mbox if
449       ;; needed so we can rescan it.
450       (if (setq inf (assoc group nnfolder-buffer-alist))
451           (setq nnfolder-current-buffer (nth 1 inf)))
452
453       ;; If the buffer is not live, make sure it isn't in the alist.  If it
454       ;; is live, verify that nobody else has touched the file since last
455       ;; time.
456       (if (or (not (and nnfolder-current-buffer
457                         (buffer-name nnfolder-current-buffer)))
458               (not (and (bufferp nnfolder-current-buffer)
459                         (verify-visited-file-modtime 
460                          nnfolder-current-buffer))))
461           (progn
462             (if (and nnfolder-current-buffer
463                      (buffer-name nnfolder-current-buffer)
464                      (bufferp nnfolder-current-buffer))
465                 (kill-buffer nnfolder-current-buffer))
466             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
467             (setq inf nil)))
468       
469       (if inf
470           ()
471         (save-excursion
472           (setq file (concat nnfolder-directory 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          (all-spools spools)
634          (group-in group)
635          incomings incoming)
636     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
637         ()
638       ;; We first activate all the groups.
639       (nnfolder-possibly-activate-groups nil)
640       ;; The we go through all the existing spool files and split the
641       ;; mail from each.
642       (while spools
643         (and
644          (file-exists-p (car spools))
645          (> (nth 7 (file-attributes (car spools))) 0)
646          (progn
647            (and gnus-verbose-backends 
648                 (message "nnfolder: Reading incoming mail..."))
649            (setq incoming 
650                  (nnmail-move-inbox 
651                   (car spools) (concat nnfolder-directory "Incoming")))
652            (setq incomings (cons incoming incomings))
653            (setq group (nnmail-get-split-group (car spools) group-in))
654            (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
655         (setq spools (cdr spools)))
656       ;; If we did indeed read any incoming spools, we save all info. 
657       (if incoming 
658           (progn
659             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
660             (run-hooks 'nnmail-read-incoming-hook)
661             (and gnus-verbose-backends
662                  (message "nnfolder: Reading incoming mail...done"))))
663       (let ((bufs nnfolder-buffer-alist))
664         (save-excursion
665           (while bufs
666             (if (not (buffer-name (nth 1 (car bufs))))
667                 (setq nnfolder-buffer-alist 
668                       (delq (car bufs) nnfolder-buffer-alist))
669               (set-buffer (nth 1 (car bufs)))
670               (and (buffer-modified-p) (save-buffer)))
671             (setq bufs (cdr bufs)))))
672       (while incomings
673         (and 
674          nnmail-delete-incoming
675          (file-writable-p incoming)
676          (delete-file incoming))
677         (setq incomings (cdr incomings))))))
678
679 (provide 'nnfolder)
680
681 ;;; nnfolder.el ends here