*** empty log message ***
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
31
32 ;;; Code:
33
34 (require 'nnheader)
35 (require 'rmail)
36 (require 'nnmail)
37
38 (defvar nnfolder-directory (expand-file-name "~/Mail/")
39   "The name of the mail box file in the users home directory.")
40
41 (defvar nnfolder-active-file (concat nnfolder-directory "active")
42   "The name of the active file.")
43
44 ;; I renamed this variable to somehting more in keeping with the general GNU
45 ;; style. -SLB
46
47 (defvar nnfolder-ignore-active-file nil
48   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
49 Note that the active file is still saved, but it's values are not
50 used.  This costs some extra time when scanning an mbox when opening
51 it.")
52
53 ;; Note that this variable may not be completely implemented yet. -SLB
54
55 (defvar nnfolder-always-close nil
56   "If non-nil, nnfolder attempts to only ever have one mbox open at a time.  
57 This is a straight space/performance trade off, as the mboxes will have to 
58 be scanned every time they are read in.  If nil (default), nnfolder will
59 attempt to keep the buffers around (saving the nnfolder's buffer upon group 
60 close, but not killing it), speeding some things up tremendously, especially
61 such things as moving mail.  All buffers always get killed upon server close.")
62
63 (defvar nnfolder-newsgroups-file (concat nnfolder-directory "newsgroups")
64   "Mail newsgroups description file.")
65
66 (defvar nnfolder-get-new-mail t
67   "If non-nil, nnml will check the incoming mail file and split the mail.")
68
69 (defvar nnfolder-prepare-save-mail-hook nil
70   "Hook run narrowed to an article before saving.")
71
72 \f
73
74 (defconst nnfolder-version "nnfolder 0.2"
75   "nnfolder version.")
76
77 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
78   "String used to demarcate what the article number for a message is.")
79
80 (defvar nnfolder-current-group nil)
81 (defvar nnfolder-current-buffer nil)
82 (defvar nnfolder-status-string "")
83 (defvar nnfolder-group-alist nil)
84 (defvar nnfolder-buffer-alist nil)
85
86 (defmacro nnfolder-article-string (article)
87   (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
88
89 \f
90
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables 
94   (list 
95    (list 'nnfolder-directory nnfolder-directory)
96    (list 'nnfolder-active-file nnfolder-active-file)
97    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99    '(nnfolder-current-group nil)
100    '(nnfolder-current-buffer nil)
101    '(nnfolder-status-string "")
102    '(nnfolder-group-alist nil)
103    '(nnfolder-buffer-alist nil)))
104
105 \f
106
107 ;;; Interface functions
108
109 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
110   (save-excursion
111     (set-buffer nntp-server-buffer)
112     (erase-buffer)
113     (let ((file nil)
114           (number (length sequence))
115           (delim-string (concat "^" rmail-unix-mail-delimiter))
116           beg article art-string start stop)
117       (nnfolder-possibly-change-group newsgroup)
118       (set-buffer nnfolder-current-buffer)
119       (goto-char (point-min))
120       (if (stringp (car sequence))
121           'headers
122         (while sequence
123           (setq article (car sequence))
124           (setq art-string (nnfolder-article-string article))
125           (set-buffer nnfolder-current-buffer)
126           (if (or (search-forward art-string nil t)
127                   ;; Don't search the whole file twice!  Also, articles
128                   ;; probably have some locality by number, so searching
129                   ;; backwards will be faster.  Especially if we're at the
130                   ;; beginning of the buffer :-). -SLB
131                   (search-backward art-string nil t))
132               (progn
133                 (setq start (or (re-search-backward delim-string nil t)
134                                 (point)))
135                 (search-forward "\n\n" nil t)
136                 (setq stop (1- (point)))
137                 (set-buffer nntp-server-buffer)
138                 (insert (format "221 %d Article retrieved.\n" article))
139                 (setq beg (point))
140                 (insert-buffer-substring nnfolder-current-buffer start stop)
141                 (goto-char (point-max))
142                 (insert ".\n")))
143           (setq sequence (cdr sequence)))
144
145         ;; Fold continuation lines.
146         (set-buffer nntp-server-buffer)
147         (goto-char (point-min))
148         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
149           (replace-match " " t t))
150         'headers))))
151
152 (defun nnfolder-open-server (server &optional defs)
153   (nnheader-init-server-buffer)
154   (if (equal server nnfolder-current-server)
155       t
156     (if nnfolder-current-server
157         (setq nnfolder-server-alist 
158               (cons (list nnfolder-current-server
159                           (nnheader-save-variables nnfolder-server-variables))
160                     nnfolder-server-alist)))
161     (let ((state (assoc server nnfolder-server-alist)))
162       (if state 
163           (progn
164             (nnheader-restore-variables (nth 1 state))
165             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
166         (nnheader-set-init-variables nnfolder-server-variables defs)))
167     (setq nnfolder-current-server server)))
168
169 (defun nnfolder-close-server (&optional server)
170   t)
171
172 (defun nnfolder-server-opened (&optional server)
173   (and (equal server nnfolder-current-server)
174        nntp-server-buffer
175        (buffer-name nntp-server-buffer)))
176
177 (defun nnfolder-request-close ()
178   (let ((alist nnfolder-buffer-alist))
179     (while alist
180       (nnfolder-close-group (car (car alist)) nil t)
181       (setq alist (cdr alist))))
182   (setq nnfolder-buffer-alist nil
183         nnfolder-group-alist nil))
184
185 (defun nnfolder-status-message (&optional server)
186   nnfolder-status-string)
187
188 (defun nnfolder-request-article (article &optional newsgroup server buffer)
189   (nnfolder-possibly-change-group newsgroup)
190   (if (stringp article)
191       nil
192     (save-excursion
193       (set-buffer nnfolder-current-buffer)
194       (goto-char (point-min))
195       (if (search-forward (nnfolder-article-string article) nil t)
196           (let (start stop)
197             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
198             (setq start (point))
199             (forward-line 1)
200             (or (and (re-search-forward 
201                       (concat "^" rmail-unix-mail-delimiter) nil t)
202                      (forward-line -1))
203                 (goto-char (point-max)))
204             (setq stop (point))
205             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
206               (set-buffer nntp-server-buffer)
207               (erase-buffer)
208               (insert-buffer-substring nnfolder-current-buffer start stop)
209               (goto-char (point-min))
210               (while (looking-at "From ")
211                 (delete-char 5)
212                 (insert "X-From-Line: ")
213                 (forward-line 1))
214               t))))))
215
216 (defun nnfolder-request-group (group &optional server dont-check)
217   (save-excursion
218     (nnfolder-possibly-change-group group)
219     (and (assoc group nnfolder-group-alist)
220          (progn
221            (if dont-check
222                t
223              (nnfolder-get-new-mail group))
224            (let* ((active (assoc group nnfolder-group-alist))
225                   (group (car active))
226                   (range (car (cdr active)))
227                   (minactive (car range))
228                   (maxactive (cdr range)))
229              ;; I've been getting stray 211 lines in my nnfolder active
230              ;; file.  So, let's make sure that doesn't happen. -SLB
231              (set-buffer nntp-server-buffer)
232              (erase-buffer)
233              (insert (format "211 %d %d %d %s\n" 
234                              (1+ (- maxactive minactive))
235                              minactive maxactive group))
236              t)))))
237
238 ;; Don't close the buffer if we're not shutting down the server.  This way,
239 ;; we can keep the buffer in the group buffer cache, and not have to grovel
240 ;; over the buffer again unless we add new mail to it or modify it in some
241 ;; way.
242
243 (defun nnfolder-close-group (group &optional server force)
244   ;; Make sure we _had_ the group open.
245   (if (or (assoc group nnfolder-buffer-alist)
246           (equal group nnfolder-current-group))
247       (progn
248         (nnfolder-possibly-change-group group)
249         (save-excursion
250           (set-buffer nnfolder-current-buffer)
251           ;; If the buffer was modified, write the file out now.
252           (and (buffer-modified-p) (save-buffer))
253           (if (or force
254                   nnfolder-always-close)
255               ;; If we're shutting the server down, we need to kill the
256               ;; buffer and remove it from the open buffer list.  Or, of
257               ;; course, if we're trying to minimize our space impact.
258               (progn
259                 (kill-buffer (current-buffer))
260                 (setq nnfolder-buffer-alist (delq (assoc group 
261                                                          nnfolder-buffer-alist)
262                                                   nnfolder-buffer-alist)))))))
263   (setq nnfolder-current-group nil
264         nnfolder-current-buffer nil)
265   t)
266
267 (defun nnfolder-request-create-group (group &optional server) 
268   (nnfolder-request-list)
269   (setq nnfolder-group-alist (nnmail-get-active))
270   (or (assoc group nnfolder-group-alist)
271       (let (active)
272         (setq nnfolder-group-alist 
273               (cons (list group (setq active (cons 0 0)))
274                     nnfolder-group-alist))
275         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
276   t)
277
278 (defun nnfolder-request-list (&optional server)
279   (if server (nnfolder-get-new-mail))
280   (save-excursion
281     (or nnfolder-group-alist
282         (nnmail-find-file nnfolder-active-file)
283         (progn
284           (setq nnfolder-group-alist (nnmail-get-active))
285           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
286           (nnmail-find-file nnfolder-active-file)))))
287
288 (defun nnfolder-request-newgroups (date &optional server)
289   (nnfolder-request-list server))
290
291 (defun nnfolder-request-list-newsgroups (&optional server)
292   (save-excursion
293     (nnmail-find-file nnfolder-newsgroups-file)))
294
295 (defun nnfolder-request-post (&optional server)
296   (mail-send-and-exit nil))
297
298 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
299
300 (defun nnfolder-request-expire-articles (articles newsgroup &optional server force)
301   (nnfolder-possibly-change-group newsgroup)
302   (let* ((days (or (and nnmail-expiry-wait-function
303                         (funcall nnmail-expiry-wait-function newsgroup))
304                    nnmail-expiry-wait))
305          article rest)
306     (save-excursion 
307       (set-buffer nnfolder-current-buffer)
308       (while articles
309         (goto-char (point-min))
310         (if (search-forward (nnfolder-article-string (car articles)) nil t)
311             (if (or force
312                     (> (nnmail-days-between 
313                         (current-time-string)
314                         (buffer-substring 
315                          (point) (progn (end-of-line) (point))))
316                        days))
317                 (progn
318                   (and gnus-verbose-backends
319                        (message "Deleting: %s" (car articles)))
320                   (nnfolder-delete-mail))
321               (setq rest (cons (car articles) rest))))
322         (setq articles (cdr articles)))
323       (and (buffer-modified-p) (save-buffer))
324       ;; Find the lowest active article in this group.
325       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
326              (marker (concat "\n" nnfolder-article-marker))
327              (number "[0-9]+")
328              (activemin (cdr active)))
329         (goto-char (point-min))
330         (while (and (search-forward marker nil t)
331                     (re-search-forward number nil t))
332           (setq activemin (min activemin
333                                (string-to-number (buffer-substring
334                                                   (match-beginning 0)
335                                                   (match-end 0))))))
336         (setcar active activemin))
337       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
338       rest)))
339
340 (defun nnfolder-request-move-article
341   (article group server accept-form &optional last)
342   (nnfolder-possibly-change-group group)
343   (let ((buf (get-buffer-create " *nnfolder move*"))
344         result)
345     (and 
346      (nnfolder-request-article article group server)
347      (save-excursion
348        (set-buffer buf)
349        (buffer-disable-undo (current-buffer))
350        (erase-buffer)
351        (insert-buffer-substring nntp-server-buffer)
352        (goto-char (point-min))
353        (while (re-search-forward 
354                "^X-Gnus-Newsgroup:" 
355                (save-excursion (search-forward "\n\n" nil t) (point)) t)
356          (delete-region (progn (beginning-of-line) (point))
357                         (progn (forward-line 1) (point))))
358        (setq result (eval accept-form))
359        (kill-buffer buf)
360        result)
361      (save-excursion
362        (nnfolder-possibly-change-group group)
363        (set-buffer nnfolder-current-buffer)
364        (goto-char (point-min))
365        (if (search-forward (nnfolder-article-string article) nil t)
366            (nnfolder-delete-mail))
367        (and last 
368             (buffer-modified-p)
369             (save-buffer))))
370     result))
371
372 (defun nnfolder-request-accept-article (group &optional last)
373   (nnfolder-possibly-change-group group)
374   (let ((buf (current-buffer))
375         result beg)
376     (goto-char (point-min))
377     (if (looking-at "X-From-Line: ")
378         (replace-match "From ")
379       (insert "From nobody " (current-time-string) "\n"))
380     (and 
381      (nnfolder-request-list)
382      (save-excursion
383        (set-buffer buf)
384        (goto-char (point-min))
385        (search-forward "\n\n" nil t)
386        (forward-line -1)
387        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
388          (delete-region (point) (progn (forward-line 1) (point))))
389        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
390      (save-excursion
391        (set-buffer nnfolder-current-buffer)
392        (and last (buffer-modified-p) (save-buffer))))
393     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
394     result))
395
396 (defun nnfolder-request-replace-article (article group buffer)
397   (nnfolder-possibly-change-group group)
398   (save-excursion
399     (set-buffer nnfolder-current-buffer)
400     (goto-char (point-min))
401     (if (not (search-forward (nnfolder-article-string article) nil t))
402         nil
403       (nnfolder-delete-mail t t)
404       (insert-buffer-substring buffer)
405       (and (buffer-modified-p) (save-buffer))
406       t)))
407
408 \f
409 ;;; Internal functions.
410
411 (defun nnfolder-delete-mail (&optional force leave-delim)
412   ;; Beginning of the article.
413   (save-excursion
414     (save-restriction
415       (narrow-to-region
416        (save-excursion
417          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
418          (if leave-delim (progn (forward-line 1) (point))
419            (match-beginning 0)))
420        (progn
421          (forward-line 1)
422          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
423                                      nil t)
424                   (if (and (not (bobp)) leave-delim)
425                       (progn (forward-line -2) (point))
426                     (match-beginning 0)))
427              (point-max))))
428       (delete-region (point-min) (point-max)))))
429
430 (defun nnfolder-possibly-change-group (group)
431   (or (file-exists-p nnfolder-directory)
432       (make-directory (directory-file-name nnfolder-directory)))
433   (nnfolder-possibly-activate-groups nil)
434   (or (assoc group nnfolder-group-alist)
435       (not (file-exists-p (concat nnfolder-directory group)))
436       (progn
437         (setq nnfolder-group-alist 
438               (cons (list group (cons 1 0)) nnfolder-group-alist))
439         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
440   (let (inf file)
441     (if (and (equal group nnfolder-current-group)
442              (buffer-name nnfolder-current-buffer))
443         ()
444       (setq nnfolder-current-group group)
445
446       ;; If we have to change groups, see if we don't already have the mbox
447       ;; in memory.  If we do, verify the modtime and destroy the mbox if
448       ;; needed so we can rescan it.
449       (if (setq inf (assoc group nnfolder-buffer-alist))
450           (setq nnfolder-current-buffer (nth 1 inf)))
451
452       ;; If the buffer is not live, make sure it isn't in the alist.  If it
453       ;; is live, verify that nobody else has touched the file since last
454       ;; time.
455       (if (or (not (buffer-name nnfolder-current-buffer))
456               (not (and (bufferp nnfolder-current-buffer)
457                         (verify-visited-file-modtime 
458                          nnfolder-current-buffer))))
459           (progn
460             (if (and (buffer-name nnfolder-current-buffer)
461                      (bufferp nnfolder-current-buffer))
462                 (kill-buffer nnfolder-current-buffer))
463             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
464             (setq inf nil)))
465       
466       (if inf
467           ()
468         (save-excursion
469           (setq file (concat nnfolder-directory group))
470           (if (or (file-directory-p file)
471                   (file-symlink-p file))
472               ()
473             (if (not (file-exists-p file))
474                 (write-region 1 1 file t 'nomesg))
475             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
476                                               nnfolder-buffer-alist))
477             (set-buffer (nnfolder-read-folder file)))))))
478   (setq nnfolder-current-group group))
479
480 (defun nnfolder-save-mail (&optional group)
481   "Called narrowed to an article."
482   (let* ((nnmail-split-methods 
483           (if group (list (list group "")) nnmail-split-methods))
484          (group-art-list
485           (nreverse (nnmail-article-group 'nnfolder-active-number)))
486          save-list group-art)
487     (setq save-list group-art-list)
488     (nnmail-insert-lines)
489     (nnmail-insert-xref group-art-list)
490     (run-hooks 'nnfolder-prepare-save-mail-hook)
491
492     ;; Insert the mail into each of the destination groups.
493     (while group-art-list
494       (setq group-art (car group-art-list)
495             group-art-list (cdr group-art-list))
496
497       ;; Kill the previous newsgroup markers.
498       (goto-char (point-min))
499       (search-forward "\n\n" nil t)
500       (forward-line -1)
501       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
502         (delete-region (point) (progn (forward-line 1) (point))))
503
504       ;; Insert the new newsgroup marker.
505       (nnfolder-possibly-change-group (car group-art))
506       (nnfolder-insert-newsgroup-line group-art)
507       (let ((beg (point-min))
508             (end (point-max))
509             (obuf (current-buffer)))
510         (set-buffer nnfolder-current-buffer)
511         (goto-char (point-max))
512         (insert-buffer-substring obuf beg end)
513         (set-buffer obuf)))
514
515     ;; Did we save it anywhere?
516     save-list))
517
518 (defun nnfolder-insert-newsgroup-line (group-art)
519   (save-excursion
520     (goto-char (point-min))
521     (if (search-forward "\n\n" nil t)
522         (progn
523           (forward-char -1)
524           (insert (format (concat nnfolder-article-marker "%d   %s\n")
525                           (cdr group-art) (current-time-string)))))))
526
527 (defun nnfolder-possibly-activate-groups (&optional group)
528   (save-excursion
529     ;; If we're looking for the activation of a specific group, find out
530     ;; it's real name and switch to it.
531     (if group (nnfolder-possibly-change-group group))
532     ;; If the group alist isn't active, activate it now.
533     (if (not nnfolder-group-alist)
534         (progn
535           (nnfolder-request-list)
536           (setq nnfolder-group-alist (nnmail-get-active))))))
537
538 (defun nnfolder-active-number (group)
539   (save-excursion 
540     (nnfolder-possibly-activate-groups group)
541     (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
542       (setcdr active (1+ (cdr active)))
543       (cdr active))))
544
545 ;; This method has a problem if you've accidentally let the active list get
546 ;; out of sync with the files.  This could happen, say, if you've
547 ;; accidentally gotten new mail with something other than (ding) (but why
548 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
549 ;; processing the file, ready to add new X-Gnus article number markers, and
550 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
551 ;; ready for us yet.
552
553 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
554 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
555 ;; rest of the message looking for any more, possibly higher IDs.  We'll
556 ;; assume the maximum that we find is the highest active.  Note that this
557 ;; shouldn't cost us much extra time at all, but will be a lot less
558 ;; vulnerable to glitches between the mbox and the active file.
559
560 (defun nnfolder-read-folder (file)
561   (save-excursion
562     (nnfolder-possibly-activate-groups nil)
563     ;; We should be paranoid here and make sure the group is in the alist,
564     ;; and add it if it isn't.
565     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
566     (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
567     (buffer-disable-undo (current-buffer))
568     (let ((delim (concat "^" rmail-unix-mail-delimiter))
569           (marker (concat "\n" nnfolder-article-marker))
570           (number "[0-9]+")
571           (active (car (cdr (assoc nnfolder-current-group 
572                                    nnfolder-group-alist))))
573           activenumber activemin start end)
574       (goto-char (point-min))
575       ;;
576       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
577       ;; search the file manually to find the active number.  Or, of course,
578       ;; if we're being paranoid.  (This would also be the place to build
579       ;; other lists from the header markers, such as expunge lists, etc., if
580       ;; we ever desired to abandon the active file entirely for mboxes.)
581       (setq activenumber (cdr active))
582       (if (or nnfolder-ignore-active-file
583               (< activenumber 2))
584           (progn
585             (setq activemin (max (1- (lsh 1 23)) 
586                                  (1- (lsh 1 24)) 
587                                  (1- (lsh 1 25))))
588             (while (and (search-forward marker nil t)
589                         (re-search-forward number nil t))
590               (let ((newnum (string-to-number (buffer-substring
591                                               (match-beginning 0)
592                                               (match-end 0)))))
593                 (setq activenumber (max activenumber newnum))
594                 (setq activemin (min activemin newnum))))
595             (setcar active (min activemin activenumber))
596             (setcdr active activenumber)
597             (goto-char (point-min))))
598
599       ;; Keep track of the active number on our own, and insert it back into
600       ;; the active list when we're done. Also, prime the pump to cut down on
601       ;; the number of searches we do.
602       (setq end (point-marker))
603       (set-marker end (or (and (re-search-forward delim nil t)
604                                (match-beginning 0))
605                           (point-max)))
606       (while (not (= end (point-max)))
607         (setq start (marker-position end))
608         (goto-char end)
609         (end-of-line)
610         (set-marker end (or (and (re-search-forward delim nil t)
611                                  (match-beginning 0))
612                             (point-max)))
613         (goto-char start)
614         (if (not (search-forward marker end t))
615             (progn
616               (narrow-to-region start end)
617               (nnmail-insert-lines)
618               (setq activenumber (1+ activenumber))
619               (nnfolder-insert-newsgroup-line (cons nil activenumber))
620               (widen))))
621
622       ;; Make absolutely sure that the active list reflects reality!
623       (setcdr active activenumber)
624       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
625       (current-buffer))))
626
627 (defun nnfolder-get-new-mail (&optional group)
628   "Read new incoming mail."
629   (let* ((spools (nnmail-get-spool-files group))
630          (all-spools spools)
631          (group-in group)
632          incomings incoming)
633     (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
634         ()
635       ;; We first activate all the groups.
636       (nnfolder-possibly-activate-groups nil)
637       ;; The we go through all the existing spool files and split the
638       ;; mail from each.
639       (while spools
640         (and
641          (file-exists-p (car spools))
642          (> (nth 7 (file-attributes (car spools))) 0)
643          (progn
644            (and gnus-verbose-backends 
645                 (message "nnfolder: Reading incoming mail..."))
646            (setq incoming 
647                  (nnmail-move-inbox 
648                   (car spools) (concat nnfolder-directory "Incoming")))
649            (setq incomings (cons incoming incomings))
650            (setq group (nnmail-get-split-group (car spools) group-in))
651            (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
652         (setq spools (cdr spools)))
653       ;; If we did indeed read any incoming spools, we save all info. 
654       (if incoming 
655           (progn
656             (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
657             (run-hooks 'nnmail-read-incoming-hook)
658             (and gnus-verbose-backends
659                  (message "nnfolder: Reading incoming mail...done"))))
660       (let ((bufs nnfolder-buffer-alist))
661         (save-excursion
662           (while bufs
663             (if (not (buffer-name (nth 1 (car bufs))))
664                 (setq nnfolder-buffer-alist 
665                       (delq (car bufs) nnfolder-buffer-alist))
666               (set-buffer (nth 1 (car bufs)))
667               (and (buffer-modified-p) (save-buffer)))
668             (setq bufs (cdr bufs)))))
669       (while incomings
670         (and 
671          nnmail-delete-incoming
672          (file-writable-p incoming)
673          (delete-file incoming))
674         (setq incomings (cdr incomings))))))
675
676 (provide 'nnfolder)
677
678 ;;; nnfolder.el ends here