*** 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 
449                          nnfolder-current-buffer))))
450           (progn
451             (if (and (buffer-name nnfolder-current-buffer)
452                      (bufferp nnfolder-current-buffer))
453                 (kill-buffer nnfolder-current-buffer))
454             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
455             (setq inf nil)))
456       
457       (if inf
458           ()
459         (save-excursion
460           (setq file (concat nnfolder-directory group))
461           (if (or (file-directory-p file)
462                   (file-symlink-p file))
463               ()
464             (if (not (file-exists-p file))
465                 (write-region 1 1 file t 'nomesg))
466             (set-buffer (nnfolder-read-folder file))
467             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
468                                               nnfolder-buffer-alist)))))))
469   (setq nnfolder-current-group group))
470
471 (defun nnfolder-save-mail (&optional group)
472   "Called narrowed to an article."
473   (let* ((nnmail-split-methods 
474           (if group (list (list group "")) nnmail-split-methods))
475          (group-art-list
476           (nreverse (nnmail-article-group 'nnfolder-active-number)))
477          save-list group-art)
478     (setq save-list group-art-list)
479     (nnmail-insert-lines)
480     (nnmail-insert-xref group-art-list)
481     (run-hooks 'nnfolder-prepare-save-mail-hook)
482
483     ;; Insert the mail into each of the destination groups.
484     (while group-art-list
485       (setq group-art (car group-art-list)
486             group-art-list (cdr group-art-list))
487
488       ;; Kill the previous newsgroup markers.
489       (goto-char (point-min))
490       (search-forward "\n\n" nil t)
491       (forward-line -1)
492       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
493         (delete-region (point) (progn (forward-line 1) (point))))
494
495       ;; Insert the new newsgroup marker.
496       (nnfolder-possibly-change-group (car group-art))
497       (nnfolder-insert-newsgroup-line group-art)
498       (let ((beg (point-min))
499             (end (point-max))
500             (obuf (current-buffer)))
501         (set-buffer nnfolder-current-buffer)
502         (goto-char (point-max))
503         (insert-buffer-substring obuf beg end)
504         (set-buffer obuf)))
505
506     ;; Did we save it anywhere?
507     save-list))
508
509 (defun nnfolder-insert-newsgroup-line (group-art)
510   (save-excursion
511     (goto-char (point-min))
512     (if (search-forward "\n\n" nil t)
513         (progn
514           (forward-char -1)
515           (insert (format (concat nnfolder-article-marker "%d   %s\n")
516                           (cdr group-art) (current-time-string)))))))
517
518 (defun nnfolder-possibly-activate-groups (&optional group)
519   (save-excursion
520     ;; If we're looking for the activation of a specific group, find out
521     ;; it's real name and switch to it.
522     (if group (nnfolder-possibly-change-group group))
523     ;; If the group alist isn't active, activate it now.
524     (if (not nnfolder-group-alist)
525         (progn
526           (nnfolder-request-list)
527           (setq nnfolder-group-alist (nnmail-get-active))))))
528
529 (defun nnfolder-active-number (group)
530   (save-excursion 
531     (nnfolder-possibly-activate-groups group)
532     (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
533       (setcdr active (1+ (cdr active)))
534       (cdr active))))
535
536 ;; This method has a problem if you've accidentally let the active list get
537 ;; out of sync with the files.  This could happen, say, if you've
538 ;; accidentally gotten new mail with something other than (ding) (but why
539 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
540 ;; processing the file, ready to add new X-Gnus article number markers, and
541 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
542 ;; ready for us yet.
543
544 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
545 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
546 ;; rest of the message looking for any more, possibly higher IDs.  We'll
547 ;; assume the maximum that we find is the highest active.  Note that this
548 ;; shouldn't cost us much extra time at all, but will be a lot less
549 ;; vulnerable to glitches between the mbox and the active file.
550
551 (defun nnfolder-read-folder (file)
552   (save-excursion
553     (nnfolder-possibly-activate-groups nil)
554     ;; We should be paranoid here and make sure the group is in the alist,
555     ;; and add it if it isn't.
556     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
557     (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
558     (buffer-disable-undo (current-buffer))
559     (let ((delim (concat "^" rmail-unix-mail-delimiter))
560           (marker (concat "\n" nnfolder-article-marker))
561           (number "[0-9]+")
562           (active (car (cdr (assoc nnfolder-current-group 
563                                    nnfolder-group-alist))))
564           activenumber activemin start end)
565       (goto-char (point-min))
566       ;;
567       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
568       ;; search the file manually to find the active number.  Or, of course,
569       ;; if we're being paranoid.  (This would also be the place to build
570       ;; other lists from the header markers, such as expunge lists, etc., if
571       ;; we ever desired to abandon the active file entirely for mboxes.)
572       (setq activenumber (cdr active))
573       (if (or nnfolder-ignore-active-file
574               (< activenumber 2))
575           (progn
576             (setq activemin (max (1- (lsh 1 23)) 
577                                  (1- (lsh 1 24)) 
578                                  (1- (lsh 1 25))))
579             (while (and (search-forward marker nil t)
580                         (re-search-forward number nil t))
581               (let ((newnum (string-to-number (buffer-substring
582                                               (match-beginning 0)
583                                               (match-end 0)))))
584                 (setq activenumber (max activenumber newnum))
585                 (setq activemin (min activemin newnum))))
586             (setcar active (min activemin activenumber))
587             (setcdr active activenumber)
588             (goto-char (point-min))))
589
590       ;; Keep track of the active number on our own, and insert it back into
591       ;; the active list when we're done. Also, prime the pump to cut down on
592       ;; the number of searches we do.
593       (setq end (point-marker))
594       (set-marker end (or (and (re-search-forward delim nil t)
595                                (match-beginning 0))
596                           (point-max)))
597       (while (not (= end (point-max)))
598         (setq start (marker-position end))
599         (goto-char end)
600         (end-of-line)
601         (set-marker end (or (and (re-search-forward delim nil t)
602                                  (match-beginning 0))
603                             (point-max)))
604         (goto-char start)
605         (if (not (search-forward marker end t))
606             (progn
607               (narrow-to-region start end)
608               (nnmail-insert-lines)
609               (setq activenumber (1+ activenumber))
610               (nnfolder-insert-newsgroup-line (cons nil activenumber))
611               (widen))))
612
613       ;; Make absolutely sure that the active list reflects reality!
614       (setcdr active activenumber)
615       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
616       (current-buffer))))
617
618 (defun nnfolder-get-new-mail (&optional group)
619   "Read new incoming mail."
620   (let* ((spools (nnmail-get-spool-files group))
621          (all-spools spools)
622          incomings incoming)
623     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
624         ()
625       ;; We first activate all the groups.
626       (nnfolder-possibly-activate-groups nil)
627       ;; The we go through all the existing spool files and split the
628       ;; mail from each.
629       (while spools
630         (and
631          (file-exists-p (car spools))
632          (> (nth 7 (file-attributes (car spools))) 0)
633          (progn
634            (and gnus-verbose-backends 
635                 (message "nnfolder: Reading incoming mail..."))
636            (setq incoming 
637                  (nnmail-move-inbox 
638                   (car spools) (concat nnfolder-directory "Incoming")))
639            (setq incomings (cons incoming incomings))
640            (setq group (nnmail-get-split-group (car spools) group))
641            (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
642         (setq spools (cdr spools)))
643       ;; If we did indeed read any incoming spools, we save all info. 
644       (if incoming 
645           (progn
646             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
647             (run-hooks 'nnmail-read-incoming-hook)
648             (and gnus-verbose-backends
649                  (message "nnfolder: Reading incoming mail...done"))))
650       (let ((bufs nnfolder-buffer-alist))
651         (save-excursion
652           (while bufs
653             (if (not (buffer-name (nth 1 (car bufs))))
654                 (setq nnfolder-buffer-alist 
655                       (delq (car bufs) nnfolder-buffer-alist))
656               (set-buffer (nth 1 (car bufs)))
657               (and (buffer-modified-p) (save-buffer)))
658             (setq bufs (cdr bufs)))))
659       (while incomings
660         (and 
661          nnmail-delete-incoming
662          (file-writable-p incoming)
663          (delete-file incoming))
664         (setq incomings (cdr incomings))))))
665
666 (provide 'nnfolder)
667
668 ;;; nnfolder.el ends here