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