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