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