*** 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 (eval-when-compile (require 'cl))
38
39 (defvar nnfolder-directory (expand-file-name "~/Mail/")
40   "The name of the mail box file in the users home directory.")
41
42 (defvar nnfolder-active-file 
43   (concat (file-name-as-directory nnfolder-directory) "active")
44   "The name of the active file.")
45
46 ;; I renamed this variable to somehting more in keeping with the general GNU
47 ;; style. -SLB
48
49 (defvar nnfolder-ignore-active-file nil
50   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
51 Note that the active file is still saved, but it's values are not
52 used.  This costs some extra time when scanning an mbox when opening
53 it.")
54
55 ;; Note that this variable may not be completely implemented yet. -SLB
56
57 (defvar nnfolder-always-close nil
58   "If non-nil, nnfolder attempts to only ever have one mbox open at a time.  
59 This is a straight space/performance trade off, as the mboxes will have to 
60 be scanned every time they are read in.  If nil (default), nnfolder will
61 attempt to keep the buffers around (saving the nnfolder's buffer upon group 
62 close, but not killing it), speeding some things up tremendously, especially
63 such things as moving mail.  All buffers always get killed upon server close.")
64
65 (defvar nnfolder-newsgroups-file 
66   (concat (file-name-as-directory  nnfolder-directory) "newsgroups")
67   "Mail newsgroups description file.")
68
69 (defvar nnfolder-get-new-mail t
70   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
71
72 (defvar nnfolder-prepare-save-mail-hook nil
73   "Hook run narrowed to an article before saving.")
74
75 \f
76
77 (defconst nnfolder-version "nnfolder 1.0"
78   "nnfolder version.")
79
80 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
81   "String used to demarcate what the article number for a message is.")
82
83 (defvar nnfolder-current-group nil)
84 (defvar nnfolder-current-buffer nil)
85 (defvar nnfolder-status-string "")
86 (defvar nnfolder-group-alist nil)
87 (defvar nnfolder-buffer-alist nil)
88 (defvar nnfolder-active-timestamp nil)
89
90 \f
91
92 (defvar nnfolder-current-server nil)
93 (defvar nnfolder-server-alist nil)
94 (defvar nnfolder-server-variables 
95   (list 
96    (list 'nnfolder-directory nnfolder-directory)
97    (list 'nnfolder-active-file nnfolder-active-file)
98    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
99    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
100    '(nnfolder-current-group nil)
101    '(nnfolder-current-buffer nil)
102    '(nnfolder-status-string "")
103    '(nnfolder-group-alist nil)
104    '(nnfolder-buffer-alist nil)
105    '(nnfolder-active-timestamp nil)))
106
107 \f
108
109 ;;; Interface functions
110
111 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
112   (save-excursion
113     (set-buffer nntp-server-buffer)
114     (erase-buffer)
115     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
116           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                 (insert-buffer-substring nnfolder-current-buffer start stop)
140                 (goto-char (point-max))
141                 (insert ".\n")))
142           (setq sequence (cdr sequence)))
143
144         ;; Fold continuation lines.
145         (set-buffer nntp-server-buffer)
146         (goto-char (point-min))
147         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
148           (replace-match " " t t))
149         'headers))))
150
151 (defun nnfolder-open-server (server &optional defs)
152   (nnheader-init-server-buffer)
153   (if (equal server nnfolder-current-server)
154       t
155     (if nnfolder-current-server
156         (setq nnfolder-server-alist 
157               (cons (list nnfolder-current-server
158                           (nnheader-save-variables nnfolder-server-variables))
159                     nnfolder-server-alist)))
160     (let ((state (assoc server nnfolder-server-alist)))
161       (if state 
162           (progn
163             (nnheader-restore-variables (nth 1 state))
164             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
165         (nnheader-set-init-variables nnfolder-server-variables defs)))
166     (setq nnfolder-current-server server)))
167
168 (defun nnfolder-close-server (&optional server)
169   (setq nnfolder-current-server nil)
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   (save-excursion
191     (set-buffer nnfolder-current-buffer)
192     (goto-char (point-min))
193     (if (search-forward (nnfolder-article-string article) nil t)
194         (let (start stop)
195           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
196           (setq start (point))
197           (forward-line 1)
198           (or (and (re-search-forward 
199                     (concat "^" rmail-unix-mail-delimiter) nil t)
200                    (forward-line -1))
201               (goto-char (point-max)))
202           (setq stop (point))
203           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
204             (set-buffer nntp-server-buffer)
205             (erase-buffer)
206             (insert-buffer-substring nnfolder-current-buffer start stop)
207             (goto-char (point-min))
208             (while (looking-at "From ")
209               (delete-char 5)
210               (insert "X-From-Line: ")
211               (forward-line 1))
212             (if (numberp article) 
213                 (cons nnfolder-current-group article)
214               (goto-char (point-min))
215               (search-forward (concat "\n" nnfolder-article-marker))
216               (cons nnfolder-current-group
217                     (string-to-int 
218                      (buffer-substring 
219                       (point) (progn (end-of-line) (point)))))))))))
220
221 (defun nnfolder-request-group (group &optional server dont-check)
222   (save-excursion
223     (nnmail-activate 'nnfolder)
224     (nnfolder-possibly-change-group group)
225     (and (assoc group nnfolder-group-alist)
226          (progn
227            (if dont-check
228                t
229              (let* ((active (assoc group nnfolder-group-alist))
230                     (group (car active))
231                     (range (car (cdr active)))
232                     (minactive (car range))
233                     (maxactive (cdr range)))
234                ;; I've been getting stray 211 lines in my nnfolder active
235                ;; file.  So, let's make sure that doesn't happen. -SLB
236                (set-buffer nntp-server-buffer)
237                (erase-buffer)
238                (if (not active)
239                    ()
240                  (insert (format "211 %d %d %d %s\n" 
241                                  (1+ (- maxactive minactive))
242                                  minactive maxactive group))
243                  t)))))))
244
245 (defun nnfolder-request-scan (&optional group server)
246   (nnmail-get-new-mail
247    'nnfolder 
248    (lambda ()
249      (let ((bufs nnfolder-buffer-alist))
250        (save-excursion
251          (while bufs
252            (if (not (buffer-name (nth 1 (car bufs))))
253                (setq nnfolder-buffer-alist 
254                      (delq (car bufs) nnfolder-buffer-alist))
255              (set-buffer (nth 1 (car bufs)))
256              (and (buffer-modified-p) (save-buffer)))
257            (setq bufs (cdr bufs))))))
258    nnfolder-directory
259    group))
260
261 ;; Don't close the buffer if we're not shutting down the server.  This way,
262 ;; we can keep the buffer in the group buffer cache, and not have to grovel
263 ;; over the buffer again unless we add new mail to it or modify it in some
264 ;; way.
265
266 (defun nnfolder-close-group (group &optional server force)
267   ;; Make sure we _had_ the group open.
268   (if (or (assoc group nnfolder-buffer-alist)
269           (equal group nnfolder-current-group))
270       (progn
271         (nnfolder-possibly-change-group group)
272         (save-excursion
273           (set-buffer nnfolder-current-buffer)
274           ;; If the buffer was modified, write the file out now.
275           (and (buffer-modified-p) (save-buffer))
276           (if (or force
277                   nnfolder-always-close)
278               ;; If we're shutting the server down, we need to kill the
279               ;; buffer and remove it from the open buffer list.  Or, of
280               ;; course, if we're trying to minimize our space impact.
281               (progn
282                 (kill-buffer (current-buffer))
283                 (setq nnfolder-buffer-alist (delq (assoc group 
284                                                          nnfolder-buffer-alist)
285                                                   nnfolder-buffer-alist)))))))
286   (setq nnfolder-current-group nil
287         nnfolder-current-buffer nil)
288   t)
289
290 (defun nnfolder-request-create-group (group &optional server) 
291   (nnmail-activate 'nnfolder)
292   (or (assoc group nnfolder-group-alist)
293       (let (active)
294         (setq nnfolder-group-alist 
295               (cons (list group (setq active (cons 1 0)))
296                     nnfolder-group-alist))
297         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
298   t)
299
300 (defun nnfolder-request-list (&optional server)
301   (save-excursion
302     (nnmail-find-file nnfolder-active-file)
303     (setq nnfolder-group-alist (nnmail-get-active))))
304
305 (defun nnfolder-request-newgroups (date &optional server)
306   (nnfolder-request-list server))
307
308 (defun nnfolder-request-list-newsgroups (&optional server)
309   (save-excursion
310     (nnmail-find-file nnfolder-newsgroups-file)))
311
312 (defun nnfolder-request-post (&optional server)
313   (mail-send-and-exit nil))
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     (nnmail-activate 'nnfolder)
324
325     (save-excursion 
326       (set-buffer nnfolder-current-buffer)
327       (while (and articles is-old)
328         (goto-char (point-min))
329         (if (search-forward (nnfolder-article-string (car articles)) nil t)
330             (if (or force
331                     (setq is-old
332                           (> (nnmail-days-between 
333                               (current-time-string)
334                               (buffer-substring 
335                                (point) (progn (end-of-line) (point))))
336                              days)))
337                 (progn
338                   (and gnus-verbose-backends
339                        (message "Deleting article %d..." 
340                                 (car articles) newsgroup))
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                (concat "^" nnfolder-article-marker)
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     (when (looking-at "X-From-Line: ")
399       (replace-match "From "))
400     (and 
401      (nnfolder-request-list)
402      (save-excursion
403        (set-buffer buf)
404        (goto-char (point-min))
405        (search-forward "\n\n" nil t)
406        (forward-line -1)
407        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
408          (delete-region (point) (progn (forward-line 1) (point))))
409        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
410      (save-excursion
411        (set-buffer nnfolder-current-buffer)
412        (and last (buffer-modified-p) (save-buffer))))
413     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
414     result))
415
416 (defun nnfolder-request-replace-article (article group buffer)
417   (nnfolder-possibly-change-group group)
418   (save-excursion
419     (set-buffer nnfolder-current-buffer)
420     (goto-char (point-min))
421     (if (not (search-forward (nnfolder-article-string article) nil t))
422         nil
423       (nnfolder-delete-mail t t)
424       (insert-buffer-substring buffer)
425       (and (buffer-modified-p) (save-buffer))
426       t)))
427
428 (defun nnfolder-request-delete-group (group &optional force server)
429   (nnfolder-close-group group server t)
430   ;; Delete all articles in GROUP.
431   (if (not force)
432       ()                                ; Don't delete the articles.
433     ;; Delete the file that holds the group.
434     (condition-case nil
435         (delete-file (concat (file-name-as-directory nnfolder-directory)
436                              group))
437       (error nil)))
438   ;; Remove the group from all structures.
439   (setq nnfolder-group-alist 
440         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
441         nnfolder-current-group nil
442         nnfolder-current-buffer nil)
443   ;; Save the active file.
444   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
445   t)
446
447 (defun nnfolder-request-rename-group (group new-name &optional server)
448   (nnfolder-possibly-change-group group)
449   (save-excursion
450     (set-buffer nnfolder-current-buffer)
451     (and (file-writable-p buffer-file-name)
452          (condition-case ()
453              (progn
454                (rename-file buffer-file-name
455                             (concat (file-name-as-directory nnfolder-directory)
456                                     new-name))
457                t)
458            (error nil))
459          ;; That went ok, so we change the internal structures.
460          (let ((entry (assoc group nnfolder-group-alist)))
461            (and entry (setcar entry new-name))
462            (setq nnfolder-current-buffer nil
463                  nnfolder-current-group nil)
464            ;; Save the new group alist.
465            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
466            ;; We kill the buffer instead of renaming it and stuff.
467            (kill-buffer (current-buffer))
468            t))))
469
470 \f
471 ;;; Internal functions.
472
473 (defun nnfolder-article-string (article)
474   (if (numberp article)
475       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
476     (concat "\nMessage-ID: " article)))
477
478 (defun nnfolder-delete-mail (&optional force leave-delim)
479   ;; Beginning of the article.
480   (save-excursion
481     (save-restriction
482       (narrow-to-region
483        (save-excursion
484          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
485          (if leave-delim (progn (forward-line 1) (point))
486            (match-beginning 0)))
487        (progn
488          (forward-line 1)
489          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
490                                      nil t)
491                   (if (and (not (bobp)) leave-delim)
492                       (progn (forward-line -2) (point))
493                     (match-beginning 0)))
494              (point-max))))
495       (delete-region (point-min) (point-max)))))
496
497 (defun nnfolder-possibly-change-group (group)
498   (or (file-exists-p nnfolder-directory)
499       (make-directory (directory-file-name nnfolder-directory)))
500   (nnfolder-possibly-activate-groups nil)
501   (or (assoc group nnfolder-group-alist)
502       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
503                                   group)))
504       (progn
505         (setq nnfolder-group-alist 
506               (cons (list group (cons 1 0)) nnfolder-group-alist))
507         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
508   (let (inf file)
509     (if (and (equal group nnfolder-current-group)
510              nnfolder-current-buffer
511              (buffer-name nnfolder-current-buffer))
512         ()
513       (setq nnfolder-current-group group)
514
515       ;; If we have to change groups, see if we don't already have the mbox
516       ;; in memory.  If we do, verify the modtime and destroy the mbox if
517       ;; needed so we can rescan it.
518       (if (setq inf (assoc group nnfolder-buffer-alist))
519           (setq nnfolder-current-buffer (nth 1 inf)))
520
521       ;; If the buffer is not live, make sure it isn't in the alist.  If it
522       ;; is live, verify that nobody else has touched the file since last
523       ;; time.
524       (if (or (not (and nnfolder-current-buffer
525                         (buffer-name nnfolder-current-buffer)))
526               (not (and (bufferp nnfolder-current-buffer)
527                         (verify-visited-file-modtime 
528                          nnfolder-current-buffer))))
529           (progn
530             (if (and nnfolder-current-buffer
531                      (buffer-name nnfolder-current-buffer)
532                      (bufferp nnfolder-current-buffer))
533                 (kill-buffer nnfolder-current-buffer))
534             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
535             (setq inf nil)))
536       
537       (if inf
538           ()
539         (save-excursion
540           (setq file (concat (file-name-as-directory nnfolder-directory)
541                              group))
542           (if (file-directory-p (file-truename file))
543               ()
544             (if (not (file-exists-p file))
545                 (write-region 1 1 file t 'nomesg))
546             (setq nnfolder-current-buffer 
547                   (set-buffer (nnfolder-read-folder file)))
548             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
549                                               nnfolder-buffer-alist)))))))
550   (setq nnfolder-current-group group))
551
552 (defun nnfolder-save-mail (&optional group)
553   "Called narrowed to an article."
554   (let* ((nnmail-split-methods 
555           (if group (list (list group "")) nnmail-split-methods))
556          (group-art-list
557           (nreverse (nnmail-article-group 'nnfolder-active-number)))
558          (delim (concat "^" rmail-unix-mail-delimiter))
559          save-list group-art)
560     (goto-char (point-min))
561     ;; This might come from somewhere else.
562     (unless (looking-at delim)
563       (insert "From nobody " (current-time-string) "\n")
564       (goto-char (point-min)))
565     ;; Quote all "From " lines in the article.
566     (forward-line 1)
567     (while (re-search-forward delim nil t)
568       (beginning-of-line)
569       (insert "> "))
570     (setq save-list group-art-list)
571     (nnmail-insert-lines)
572     (nnmail-insert-xref group-art-list)
573     (run-hooks 'nnfolder-prepare-save-mail-hook)
574
575     ;; Insert the mail into each of the destination groups.
576     (while group-art-list
577       (setq group-art (car group-art-list)
578             group-art-list (cdr group-art-list))
579
580       ;; Kill the previous newsgroup markers.
581       (goto-char (point-min))
582       (search-forward "\n\n" nil t)
583       (forward-line -1)
584       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
585         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
586
587       ;; Insert the new newsgroup marker.
588       (nnfolder-possibly-change-group (car group-art))
589       (nnfolder-insert-newsgroup-line group-art)
590       (let ((beg (point-min))
591             (end (point-max))
592             (obuf (current-buffer)))
593         (set-buffer nnfolder-current-buffer)
594         (goto-char (point-max))
595         (insert-buffer-substring obuf beg end)
596         (set-buffer obuf)))
597
598     ;; Did we save it anywhere?
599     save-list))
600
601 (defun nnfolder-insert-newsgroup-line (group-art)
602   (save-excursion
603     (goto-char (point-min))
604     (if (search-forward "\n\n" nil t)
605         (progn
606           (forward-char -1)
607           (insert (format (concat nnfolder-article-marker "%d   %s\n")
608                           (cdr group-art) (current-time-string)))))))
609
610 (defun nnfolder-possibly-activate-groups (&optional group)
611   (save-excursion
612     ;; If we're looking for the activation of a specific group, find out
613     ;; its real name and switch to it.
614     (if group (nnfolder-possibly-change-group group))
615     ;; If the group alist isn't active, activate it now.
616     (nnmail-activate 'nnfolder)))
617
618 (defun nnfolder-active-number (group)
619   (save-excursion 
620     ;; Find the next article number in GROUP.
621     (prog1
622         (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
623           (if active
624               (setcdr active (1+ (cdr active)))
625             ;; This group is new, so we create a new entry for it.
626             ;; This might be a bit naughty... creating groups on the drop of
627             ;; a hat, but I don't know...
628             (setq nnfolder-group-alist 
629                   (cons (list group (setq active (cons 1 1)))
630                         nnfolder-group-alist)))
631           (cdr active))
632       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
633       (nnfolder-possibly-activate-groups group))))
634
635
636 ;; This method has a problem if you've accidentally let the active list get
637 ;; out of sync with the files.  This could happen, say, if you've
638 ;; accidentally gotten new mail with something other than Gnus (but why
639 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
640 ;; processing the file, ready to add new X-Gnus article number markers, and
641 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
642 ;; ready for us yet.
643
644 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
645 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
646 ;; rest of the message looking for any more, possibly higher IDs.  We'll
647 ;; assume the maximum that we find is the highest active.  Note that this
648 ;; shouldn't cost us much extra time at all, but will be a lot less
649 ;; vulnerable to glitches between the mbox and the active file.
650
651 (defun nnfolder-read-folder (file)
652   (save-excursion
653     (nnfolder-possibly-activate-groups nil)
654     ;; We should be paranoid here and make sure the group is in the alist,
655     ;; and add it if it isn't.
656     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
657     (set-buffer (setq nnfolder-current-buffer 
658                       (nnheader-find-file-noselect file nil 'raw)))
659     (buffer-disable-undo (current-buffer))
660     (let ((delim (concat "^" rmail-unix-mail-delimiter))
661           (marker (concat "\n" nnfolder-article-marker))
662           (number "[0-9]+")
663           (active (car (cdr (assoc nnfolder-current-group 
664                                    nnfolder-group-alist))))
665           activenumber activemin start end)
666       (goto-char (point-min))
667       ;;
668       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
669       ;; search the file manually to find the active number.  Or, of course,
670       ;; if we're being paranoid.  (This would also be the place to build
671       ;; other lists from the header markers, such as expunge lists, etc., if
672       ;; we ever desired to abandon the active file entirely for mboxes.)
673       (setq activenumber (cdr active))
674       (if (or nnfolder-ignore-active-file
675               (< activenumber 2))
676           (progn
677             (setq activemin (max (1- (lsh 1 23)) 
678                                  (1- (lsh 1 24)) 
679                                  (1- (lsh 1 25))))
680             (while (and (search-forward marker nil t)
681                         (re-search-forward number nil t))
682               (let ((newnum (string-to-number (buffer-substring
683                                                (match-beginning 0)
684                                                (match-end 0)))))
685                 (setq activenumber (max activenumber newnum))
686                 (setq activemin (min activemin newnum))))
687             (setcar active (max 1 (min activemin activenumber)))
688             (setcdr active (max activenumber (cdr active)))
689             (goto-char (point-min))))
690
691       ;; Keep track of the active number on our own, and insert it back into
692       ;; the active list when we're done. Also, prime the pump to cut down on
693       ;; the number of searches we do.
694       (setq end (point-marker))
695       (set-marker end (or (and (re-search-forward delim nil t)
696                                (match-beginning 0))
697                           (point-max)))
698       (while (not (= end (point-max)))
699         (setq start (marker-position end))
700         (goto-char end)
701         ;; There may be more than one "From " line, so we skip past
702         ;; them.  
703         (while (looking-at delim) 
704           (forward-line 1))
705         (set-marker end (or (and (re-search-forward delim nil t)
706                                  (match-beginning 0))
707                             (point-max)))
708         (goto-char start)
709         (if (not (search-forward marker end t))
710             (progn
711               (narrow-to-region start end)
712               (nnmail-insert-lines)
713               (nnfolder-insert-newsgroup-line
714                (cons nil (nnfolder-active-number nnfolder-current-group)))
715               (widen))))
716
717       ;; Make absolutely sure that the active list reflects reality!
718       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
719       (current-buffer))))
720
721 (provide 'nnfolder)
722
723 ;;; nnfolder.el ends here