*** 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 1.0"
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 \f
90
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables 
94   (list 
95    (list 'nnfolder-directory nnfolder-directory)
96    (list 'nnfolder-active-file nnfolder-active-file)
97    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99    '(nnfolder-current-group nil)
100    '(nnfolder-current-buffer nil)
101    '(nnfolder-status-string "")
102    '(nnfolder-group-alist nil)
103    '(nnfolder-buffer-alist nil)
104    '(nnfolder-active-timestamp nil)))
105
106 \f
107
108 ;;; Interface functions
109
110 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
111   (save-excursion
112     (set-buffer nntp-server-buffer)
113     (erase-buffer)
114     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
115           article art-string start stop)
116       (nnfolder-possibly-change-group newsgroup)
117       (set-buffer nnfolder-current-buffer)
118       (goto-char (point-min))
119       (if (stringp (car sequence))
120           'headers
121         (while sequence
122           (setq article (car sequence))
123           (setq art-string (nnfolder-article-string article))
124           (set-buffer nnfolder-current-buffer)
125           (if (or (search-forward art-string nil t)
126                   ;; Don't search the whole file twice!  Also, articles
127                   ;; probably have some locality by number, so searching
128                   ;; backwards will be faster.  Especially if we're at the
129                   ;; beginning of the buffer :-). -SLB
130                   (search-backward art-string nil t))
131               (progn
132                 (setq start (or (re-search-backward delim-string nil t)
133                                 (point)))
134                 (search-forward "\n\n" nil t)
135                 (setq stop (1- (point)))
136                 (set-buffer nntp-server-buffer)
137                 (insert (format "221 %d Article retrieved.\n" article))
138                 (insert-buffer-substring nnfolder-current-buffer start stop)
139                 (goto-char (point-max))
140                 (insert ".\n")))
141           (setq sequence (cdr sequence)))
142
143         ;; Fold continuation lines.
144         (set-buffer nntp-server-buffer)
145         (goto-char (point-min))
146         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147           (replace-match " " t t))
148         'headers))))
149
150 (defun nnfolder-open-server (server &optional defs)
151   (nnheader-init-server-buffer)
152   (if (equal server nnfolder-current-server)
153       t
154     (if nnfolder-current-server
155         (setq nnfolder-server-alist 
156               (cons (list nnfolder-current-server
157                           (nnheader-save-variables nnfolder-server-variables))
158                     nnfolder-server-alist)))
159     (let ((state (assoc server nnfolder-server-alist)))
160       (if state 
161           (progn
162             (nnheader-restore-variables (nth 1 state))
163             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
164         (nnheader-set-init-variables nnfolder-server-variables defs)))
165     (setq nnfolder-current-server server)))
166
167 (defun nnfolder-close-server (&optional server)
168   t)
169
170 (defun nnfolder-server-opened (&optional server)
171   (and (equal server nnfolder-current-server)
172        nntp-server-buffer
173        (buffer-name nntp-server-buffer)))
174
175 (defun nnfolder-request-close ()
176   (let ((alist nnfolder-buffer-alist))
177     (while alist
178       (nnfolder-close-group (car (car alist)) nil t)
179       (setq alist (cdr alist))))
180   (setq nnfolder-buffer-alist nil
181         nnfolder-group-alist nil))
182
183 (defun nnfolder-status-message (&optional server)
184   nnfolder-status-string)
185
186 (defun nnfolder-request-article (article &optional newsgroup server buffer)
187   (nnfolder-possibly-change-group newsgroup)
188   (save-excursion
189     (set-buffer nnfolder-current-buffer)
190     (goto-char (point-min))
191     (if (search-forward (nnfolder-article-string article) nil t)
192         (let (start stop)
193           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
194           (setq start (point))
195           (forward-line 1)
196           (or (and (re-search-forward 
197                     (concat "^" rmail-unix-mail-delimiter) nil t)
198                    (forward-line -1))
199               (goto-char (point-max)))
200           (setq stop (point))
201           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
202             (set-buffer nntp-server-buffer)
203             (erase-buffer)
204             (insert-buffer-substring nnfolder-current-buffer start stop)
205             (goto-char (point-min))
206             (while (looking-at "From ")
207               (delete-char 5)
208               (insert "X-From-Line: ")
209               (forward-line 1))
210             (if (numberp article) 
211                 (cons nnfolder-current-group article)
212               (goto-char (point-min))
213               (search-forward (concat "\n" nnfolder-article-marker))
214               (cons nnfolder-current-group
215                     (string-to-int 
216                      (buffer-substring 
217                       (point) (progn (end-of-line) (point)))))))))))
218
219 (defun nnfolder-request-group (group &optional server dont-check)
220   (save-excursion
221     (nnmail-activate 'nnfolder)
222     (nnfolder-possibly-change-group group)
223     (and (assoc group nnfolder-group-alist)
224          (progn
225            (if dont-check
226                t
227              (let* ((active (assoc group nnfolder-group-alist))
228                     (group (car active))
229                     (range (car (cdr active)))
230                     (minactive (car range))
231                     (maxactive (cdr range)))
232                ;; I've been getting stray 211 lines in my nnfolder active
233                ;; file.  So, let's make sure that doesn't happen. -SLB
234                (set-buffer nntp-server-buffer)
235                (erase-buffer)
236                (if (not active)
237                    ()
238                  (insert (format "211 %d %d %d %s\n" 
239                                  (1+ (- maxactive minactive))
240                                  minactive maxactive group))
241                  t)))))))
242
243 (defun nnfolder-request-scan (&optional group server)
244   (nnmail-get-new-mail
245    'nnfolder 
246    (lambda ()
247      (let ((bufs nnfolder-buffer-alist))
248        (save-excursion
249          (while bufs
250            (if (not (buffer-name (nth 1 (car bufs))))
251                (setq nnfolder-buffer-alist 
252                      (delq (car bufs) nnfolder-buffer-alist))
253              (set-buffer (nth 1 (car bufs)))
254              (and (buffer-modified-p) (save-buffer)))
255            (setq bufs (cdr bufs))))))
256    nnfolder-directory
257    group))
258
259 ;; Don't close the buffer if we're not shutting down the server.  This way,
260 ;; we can keep the buffer in the group buffer cache, and not have to grovel
261 ;; over the buffer again unless we add new mail to it or modify it in some
262 ;; way.
263
264 (defun nnfolder-close-group (group &optional server force)
265   ;; Make sure we _had_ the group open.
266   (if (or (assoc group nnfolder-buffer-alist)
267           (equal group nnfolder-current-group))
268       (progn
269         (nnfolder-possibly-change-group group)
270         (save-excursion
271           (set-buffer nnfolder-current-buffer)
272           ;; If the buffer was modified, write the file out now.
273           (and (buffer-modified-p) (save-buffer))
274           (if (or force
275                   nnfolder-always-close)
276               ;; If we're shutting the server down, we need to kill the
277               ;; buffer and remove it from the open buffer list.  Or, of
278               ;; course, if we're trying to minimize our space impact.
279               (progn
280                 (kill-buffer (current-buffer))
281                 (setq nnfolder-buffer-alist (delq (assoc group 
282                                                          nnfolder-buffer-alist)
283                                                   nnfolder-buffer-alist)))))))
284   (setq nnfolder-current-group nil
285         nnfolder-current-buffer nil)
286   t)
287
288 (defun nnfolder-request-create-group (group &optional server) 
289   (nnmail-activate 'nnfolder)
290   (or (assoc group nnfolder-group-alist)
291       (let (active)
292         (setq nnfolder-group-alist 
293               (cons (list group (setq active (cons 1 0)))
294                     nnfolder-group-alist))
295         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
296   t)
297
298 (defun nnfolder-request-list (&optional server)
299   (save-excursion
300     (nnmail-find-file nnfolder-active-file)
301     (setq nnfolder-group-alist (nnmail-get-active))))
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 (defun nnfolder-request-expire-articles 
314   (articles newsgroup &optional server force)
315   (nnfolder-possibly-change-group newsgroup)
316   (let* ((days (or (and nnmail-expiry-wait-function
317                         (funcall nnmail-expiry-wait-function newsgroup))
318                    nnmail-expiry-wait))
319          (is-old t)
320          rest)
321     (nnmail-activate 'nnfolder)
322
323     (save-excursion 
324       (set-buffer nnfolder-current-buffer)
325       (while (and articles is-old)
326         (goto-char (point-min))
327         (if (search-forward (nnfolder-article-string (car articles)) nil t)
328             (if (or force
329                     (setq is-old
330                           (> (nnmail-days-between 
331                               (current-time-string)
332                               (buffer-substring 
333                                (point) (progn (end-of-line) (point))))
334                              days)))
335                 (progn
336                   (and gnus-verbose-backends
337                        (message "Deleting article %d..." 
338                                 (car articles) newsgroup))
339                   (nnfolder-delete-mail))
340               (setq rest (cons (car articles) rest))))
341         (setq articles (cdr articles)))
342       (and (buffer-modified-p) (save-buffer))
343       ;; Find the lowest active article in this group.
344       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
345              (marker (concat "\n" nnfolder-article-marker))
346              (number "[0-9]+")
347              (activemin (cdr active)))
348         (goto-char (point-min))
349         (while (and (search-forward marker nil t)
350                     (re-search-forward number nil t))
351           (setq activemin (min activemin
352                                (string-to-number (buffer-substring
353                                                   (match-beginning 0)
354                                                   (match-end 0))))))
355         (setcar active activemin))
356       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
357       (nconc rest articles))))
358
359 (defun nnfolder-request-move-article
360   (article group server accept-form &optional last)
361   (nnfolder-possibly-change-group group)
362   (let ((buf (get-buffer-create " *nnfolder move*"))
363         result)
364     (and 
365      (nnfolder-request-article article group server)
366      (save-excursion
367        (set-buffer buf)
368        (buffer-disable-undo (current-buffer))
369        (erase-buffer)
370        (insert-buffer-substring nntp-server-buffer)
371        (goto-char (point-min))
372        (while (re-search-forward 
373                (concat "^" nnfolder-article-marker)
374                (save-excursion (search-forward "\n\n" nil t) (point)) t)
375          (delete-region (progn (beginning-of-line) (point))
376                         (progn (forward-line 1) (point))))
377        (setq result (eval accept-form))
378        (kill-buffer buf)
379        result)
380      (save-excursion
381        (nnfolder-possibly-change-group group)
382        (set-buffer nnfolder-current-buffer)
383        (goto-char (point-min))
384        (if (search-forward (nnfolder-article-string article) nil t)
385            (nnfolder-delete-mail))
386        (and last 
387             (buffer-modified-p)
388             (save-buffer))))
389     result))
390
391 (defun nnfolder-request-accept-article (group &optional last)
392   (and (stringp group) (nnfolder-possibly-change-group group))
393   (let ((buf (current-buffer))
394         result)
395     (goto-char (point-min))
396     (cond ((looking-at "X-From-Line: ")
397            (replace-match "From "))
398           ((not (looking-at "From "))
399            (insert "From nobody " (current-time-string) "\n")))
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          save-list group-art)
559     (setq save-list group-art-list)
560     (nnmail-insert-lines)
561     (nnmail-insert-xref group-art-list)
562     (run-hooks 'nnfolder-prepare-save-mail-hook)
563
564     ;; Insert the mail into each of the destination groups.
565     (while group-art-list
566       (setq group-art (car group-art-list)
567             group-art-list (cdr group-art-list))
568
569       ;; Kill the previous newsgroup markers.
570       (goto-char (point-min))
571       (search-forward "\n\n" nil t)
572       (forward-line -1)
573       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
574         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
575
576       ;; Insert the new newsgroup marker.
577       (nnfolder-possibly-change-group (car group-art))
578       (nnfolder-insert-newsgroup-line group-art)
579       (let ((beg (point-min))
580             (end (point-max))
581             (obuf (current-buffer)))
582         (set-buffer nnfolder-current-buffer)
583         (goto-char (point-max))
584         (insert-buffer-substring obuf beg end)
585         (set-buffer obuf)))
586
587     ;; Did we save it anywhere?
588     save-list))
589
590 (defun nnfolder-insert-newsgroup-line (group-art)
591   (save-excursion
592     (goto-char (point-min))
593     (if (search-forward "\n\n" nil t)
594         (progn
595           (forward-char -1)
596           (insert (format (concat nnfolder-article-marker "%d   %s\n")
597                           (cdr group-art) (current-time-string)))))))
598
599 (defun nnfolder-possibly-activate-groups (&optional group)
600   (save-excursion
601     ;; If we're looking for the activation of a specific group, find out
602     ;; its real name and switch to it.
603     (if group (nnfolder-possibly-change-group group))
604     ;; If the group alist isn't active, activate it now.
605     (nnmail-activate 'nnfolder)))
606
607 (defun nnfolder-active-number (group)
608   (save-excursion 
609     ;; Find the next article number in GROUP.
610     (prog1
611         (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
612           (if active
613               (setcdr active (1+ (cdr active)))
614             ;; This group is new, so we create a new entry for it.
615             ;; This might be a bit naughty... creating groups on the drop of
616             ;; a hat, but I don't know...
617             (setq nnfolder-group-alist 
618                   (cons (list group (setq active (cons 1 1)))
619                         nnfolder-group-alist)))
620           (cdr active))
621       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
622       (nnfolder-possibly-activate-groups group)
623       )))
624
625
626 ;; This method has a problem if you've accidentally let the active list get
627 ;; out of sync with the files.  This could happen, say, if you've
628 ;; accidentally gotten new mail with something other than Gnus (but why
629 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
630 ;; processing the file, ready to add new X-Gnus article number markers, and
631 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
632 ;; ready for us yet.
633
634 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
635 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
636 ;; rest of the message looking for any more, possibly higher IDs.  We'll
637 ;; assume the maximum that we find is the highest active.  Note that this
638 ;; shouldn't cost us much extra time at all, but will be a lot less
639 ;; vulnerable to glitches between the mbox and the active file.
640
641 (defun nnfolder-read-folder (file)
642   (save-excursion
643     (nnfolder-possibly-activate-groups nil)
644     ;; We should be paranoid here and make sure the group is in the alist,
645     ;; and add it if it isn't.
646     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
647     (set-buffer (setq nnfolder-current-buffer 
648                       (nnheader-find-file-noselect file nil 'raw)))
649     (buffer-disable-undo (current-buffer))
650     (let ((delim (concat "^" rmail-unix-mail-delimiter))
651           (marker (concat "\n" nnfolder-article-marker))
652           (number "[0-9]+")
653           (active (car (cdr (assoc nnfolder-current-group 
654                                    nnfolder-group-alist))))
655           activenumber activemin start end)
656       (goto-char (point-min))
657       ;;
658       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
659       ;; search the file manually to find the active number.  Or, of course,
660       ;; if we're being paranoid.  (This would also be the place to build
661       ;; other lists from the header markers, such as expunge lists, etc., if
662       ;; we ever desired to abandon the active file entirely for mboxes.)
663       (setq activenumber (cdr active))
664       (if (or nnfolder-ignore-active-file
665               (< activenumber 2))
666           (progn
667             (setq activemin (max (1- (lsh 1 23)) 
668                                  (1- (lsh 1 24)) 
669                                  (1- (lsh 1 25))))
670             (while (and (search-forward marker nil t)
671                         (re-search-forward number nil t))
672               (let ((newnum (string-to-number (buffer-substring
673                                                (match-beginning 0)
674                                                (match-end 0)))))
675                 (setq activenumber (max activenumber newnum))
676                 (setq activemin (min activemin newnum))))
677             (setcar active (max 1 (min activemin activenumber)))
678             (setcdr active (max activenumber (cdr active)))
679             (goto-char (point-min))))
680
681       ;; Keep track of the active number on our own, and insert it back into
682       ;; the active list when we're done. Also, prime the pump to cut down on
683       ;; the number of searches we do.
684       (setq end (point-marker))
685       (set-marker end (or (and (re-search-forward delim nil t)
686                                (match-beginning 0))
687                           (point-max)))
688       (while (not (= end (point-max)))
689         (setq start (marker-position end))
690         (goto-char end)
691         ;; There may be more than one "From " line, so we skip past
692         ;; them.  
693         (while (looking-at delim) 
694           (forward-line 1))
695         (set-marker end (or (and (re-search-forward delim nil t)
696                                  (match-beginning 0))
697                             (point-max)))
698         (goto-char start)
699         (if (not (search-forward marker end t))
700             (progn
701               (narrow-to-region start end)
702               (nnmail-insert-lines)
703               (nnfolder-insert-newsgroup-line
704                (cons nil (nnfolder-active-number nnfolder-current-group)))
705               (widen))))
706
707       ;; Make absolutely sure that the active list reflects reality!
708       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
709       (current-buffer))))
710
711 (provide 'nnfolder)
712
713 ;;; nnfolder.el ends here