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