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