*** 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         (nnfolder-possibly-change-group group)
296         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
297   t)
298
299 (defun nnfolder-request-list (&optional server)
300   (save-excursion
301     (nnmail-find-file nnfolder-active-file)
302     (setq nnfolder-group-alist (nnmail-get-active))))
303
304 (defun nnfolder-request-newgroups (date &optional server)
305   (nnfolder-request-list server))
306
307 (defun nnfolder-request-list-newsgroups (&optional server)
308   (save-excursion
309     (nnmail-find-file nnfolder-newsgroups-file)))
310
311 (defun nnfolder-request-post (&optional server)
312   (mail-send-and-exit nil))
313
314 (defun nnfolder-request-expire-articles 
315   (articles newsgroup &optional server force)
316   (nnfolder-possibly-change-group newsgroup)
317   (let* ((days (or (and nnmail-expiry-wait-function
318                         (funcall nnmail-expiry-wait-function newsgroup))
319                    nnmail-expiry-wait))
320          (is-old t)
321          rest)
322     (nnmail-activate 'nnfolder)
323
324     (save-excursion 
325       (set-buffer nnfolder-current-buffer)
326       (while (and articles is-old)
327         (goto-char (point-min))
328         (if (search-forward (nnfolder-article-string (car articles)) nil t)
329             (if (or force
330                     (setq is-old
331                           (> (nnmail-days-between 
332                               (current-time-string)
333                               (buffer-substring 
334                                (point) (progn (end-of-line) (point))))
335                              days)))
336                 (progn
337                   (and gnus-verbose-backends
338                        (message "Deleting article %d..." 
339                                 (car articles) newsgroup))
340                   (nnfolder-delete-mail))
341               (setq rest (cons (car articles) rest))))
342         (setq articles (cdr articles)))
343       (and (buffer-modified-p) (save-buffer))
344       ;; Find the lowest active article in this group.
345       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
346              (marker (concat "\n" nnfolder-article-marker))
347              (number "[0-9]+")
348              (activemin (cdr active)))
349         (goto-char (point-min))
350         (while (and (search-forward marker nil t)
351                     (re-search-forward number nil t))
352           (setq activemin (min activemin
353                                (string-to-number (buffer-substring
354                                                   (match-beginning 0)
355                                                   (match-end 0))))))
356         (setcar active activemin))
357       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
358       (nconc rest articles))))
359
360 (defun nnfolder-request-move-article
361   (article group server accept-form &optional last)
362   (nnfolder-possibly-change-group group)
363   (let ((buf (get-buffer-create " *nnfolder move*"))
364         result)
365     (and 
366      (nnfolder-request-article article group server)
367      (save-excursion
368        (set-buffer buf)
369        (buffer-disable-undo (current-buffer))
370        (erase-buffer)
371        (insert-buffer-substring nntp-server-buffer)
372        (goto-char (point-min))
373        (while (re-search-forward 
374                (concat "^" nnfolder-article-marker)
375                (save-excursion (search-forward "\n\n" nil t) (point)) t)
376          (delete-region (progn (beginning-of-line) (point))
377                         (progn (forward-line 1) (point))))
378        (setq result (eval accept-form))
379        (kill-buffer buf)
380        result)
381      (save-excursion
382        (nnfolder-possibly-change-group group)
383        (set-buffer nnfolder-current-buffer)
384        (goto-char (point-min))
385        (if (search-forward (nnfolder-article-string article) nil t)
386            (nnfolder-delete-mail))
387        (and last 
388             (buffer-modified-p)
389             (save-buffer))))
390     result))
391
392 (defun nnfolder-request-accept-article (group &optional last)
393   (and (stringp group) (nnfolder-possibly-change-group group))
394   (let ((buf (current-buffer))
395         result)
396     (goto-char (point-min))
397     (cond ((looking-at "X-From-Line: ")
398            (replace-match "From "))
399           ((not (looking-at "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 (concat "^" nnfolder-article-marker) 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 (defun nnfolder-request-delete-group (group &optional force server)
430   (nnfolder-possibly-change-group group)
431   ;; Delete all articles in GROUP.
432   (if (not force)
433       ()                                ; Don't delete the articles.
434     ;; Delete the file that holds the group and kill the buffer as
435     ;; well.  
436     (save-excursion
437       (and (set-buffer nnfolder-current-buffer)
438            (progn
439              (and (file-writable-p buffer-file-name)
440                   (delete-file buffer-file-name))
441              (kill-buffer (current-buffer))))))
442   ;; Remove the group from all structures.
443   (setq nnfolder-group-alist 
444         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
445         nnfolder-current-group nil
446         nnfolder-current-buffer nil)
447   ;; Save the active file.
448   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
449   t)
450
451 (defun nnfolder-request-rename-group (group new-name &optional server)
452   (nnfolder-possibly-change-group group)
453   (save-excursion
454     (set-buffer nnfolder-current-buffer)
455     (and (file-writable-p buffer-file-name)
456          (condition-case ()
457              (progn
458                (rename-file buffer-file-name
459                             (concat (file-name-as-directory nnfolder-directory)
460                                     new-name))
461                t)
462            (error nil))
463          ;; That went ok, so we change the internal structures.
464          (let ((entry (assoc group nnfolder-group-alist)))
465            (and entry (setcar entry new-name))
466            (setq nnfolder-current-buffer nil
467                  nnfolder-current-group nil)
468            ;; Save the new group alist.
469            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
470            ;; We kill the buffer instead of renaming it and stuff.
471            (kill-buffer (current-buffer))
472            t))))
473
474 \f
475 ;;; Internal functions.
476
477 (defun nnfolder-article-string (article)
478   (if (numberp article)
479       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
480     (concat "\nMessage-ID: " article)))
481
482 (defun nnfolder-delete-mail (&optional force leave-delim)
483   ;; Beginning of the article.
484   (save-excursion
485     (save-restriction
486       (narrow-to-region
487        (save-excursion
488          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
489          (if leave-delim (progn (forward-line 1) (point))
490            (match-beginning 0)))
491        (progn
492          (forward-line 1)
493          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
494                                      nil t)
495                   (if (and (not (bobp)) leave-delim)
496                       (progn (forward-line -2) (point))
497                     (match-beginning 0)))
498              (point-max))))
499       (delete-region (point-min) (point-max)))))
500
501 (defun nnfolder-possibly-change-group (group)
502   (or (file-exists-p nnfolder-directory)
503       (make-directory (directory-file-name nnfolder-directory)))
504   (nnfolder-possibly-activate-groups nil)
505   (or (assoc group nnfolder-group-alist)
506       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
507                                   group)))
508       (progn
509         (setq nnfolder-group-alist 
510               (cons (list group (cons 1 0)) nnfolder-group-alist))
511         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
512   (let (inf file)
513     (if (and (equal group nnfolder-current-group)
514              nnfolder-current-buffer
515              (buffer-name nnfolder-current-buffer))
516         ()
517       (setq nnfolder-current-group group)
518
519       ;; If we have to change groups, see if we don't already have the mbox
520       ;; in memory.  If we do, verify the modtime and destroy the mbox if
521       ;; needed so we can rescan it.
522       (if (setq inf (assoc group nnfolder-buffer-alist))
523           (setq nnfolder-current-buffer (nth 1 inf)))
524
525       ;; If the buffer is not live, make sure it isn't in the alist.  If it
526       ;; is live, verify that nobody else has touched the file since last
527       ;; time.
528       (if (or (not (and nnfolder-current-buffer
529                         (buffer-name nnfolder-current-buffer)))
530               (not (and (bufferp nnfolder-current-buffer)
531                         (verify-visited-file-modtime 
532                          nnfolder-current-buffer))))
533           (progn
534             (if (and nnfolder-current-buffer
535                      (buffer-name nnfolder-current-buffer)
536                      (bufferp nnfolder-current-buffer))
537                 (kill-buffer nnfolder-current-buffer))
538             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
539             (setq inf nil)))
540       
541       (if inf
542           ()
543         (save-excursion
544           (setq file (concat (file-name-as-directory nnfolder-directory)
545                              group))
546           (if (file-directory-p (file-truename file))
547               ()
548             (if (not (file-exists-p file))
549                 (write-region 1 1 file t 'nomesg))
550             (setq nnfolder-current-buffer 
551                   (set-buffer (nnfolder-read-folder file)))
552             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
553                                               nnfolder-buffer-alist)))))))
554   (setq nnfolder-current-group group))
555
556 (defun nnfolder-save-mail (&optional group)
557   "Called narrowed to an article."
558   (let* ((nnmail-split-methods 
559           (if group (list (list group "")) nnmail-split-methods))
560          (group-art-list
561           (nreverse (nnmail-article-group 'nnfolder-active-number)))
562          save-list group-art)
563     (setq save-list group-art-list)
564     (nnmail-insert-lines)
565     (nnmail-insert-xref group-art-list)
566     (run-hooks 'nnfolder-prepare-save-mail-hook)
567
568     ;; Insert the mail into each of the destination groups.
569     (while group-art-list
570       (setq group-art (car group-art-list)
571             group-art-list (cdr group-art-list))
572
573       ;; Kill the previous newsgroup markers.
574       (goto-char (point-min))
575       (search-forward "\n\n" nil t)
576       (forward-line -1)
577       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
578         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
579
580       ;; Insert the new newsgroup marker.
581       (nnfolder-possibly-change-group (car group-art))
582       (nnfolder-insert-newsgroup-line group-art)
583       (let ((beg (point-min))
584             (end (point-max))
585             (obuf (current-buffer)))
586         (set-buffer nnfolder-current-buffer)
587         (goto-char (point-max))
588         (insert-buffer-substring obuf beg end)
589         (set-buffer obuf)))
590
591     ;; Did we save it anywhere?
592     save-list))
593
594 (defun nnfolder-insert-newsgroup-line (group-art)
595   (save-excursion
596     (goto-char (point-min))
597     (if (search-forward "\n\n" nil t)
598         (progn
599           (forward-char -1)
600           (insert (format (concat nnfolder-article-marker "%d   %s\n")
601                           (cdr group-art) (current-time-string)))))))
602
603 (defun nnfolder-possibly-activate-groups (&optional group)
604   (save-excursion
605     ;; If we're looking for the activation of a specific group, find out
606     ;; its real name and switch to it.
607     (if group (nnfolder-possibly-change-group group))
608     ;; If the group alist isn't active, activate it now.
609     (nnmail-activate 'nnfolder)))
610
611 (defun nnfolder-active-number (group)
612   (save-excursion 
613     ;; Find the next article number in GROUP.
614     (prog1
615         (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
616           (if active
617               (setcdr active (1+ (cdr active)))
618             ;; This group is new, so we create a new entry for it.
619             ;; This might be a bit naughty... creating groups on the drop of
620             ;; a hat, but I don't know...
621             (setq nnfolder-group-alist 
622                   (cons (list group (setq active (cons 1 1)))
623                         nnfolder-group-alist)))
624           (cdr active))
625       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
626       (nnfolder-possibly-activate-groups group)
627       )))
628
629
630 ;; This method has a problem if you've accidentally let the active list get
631 ;; out of sync with the files.  This could happen, say, if you've
632 ;; accidentally gotten new mail with something other than Gnus (but why
633 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
634 ;; processing the file, ready to add new X-Gnus article number markers, and
635 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
636 ;; ready for us yet.
637
638 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
639 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
640 ;; rest of the message looking for any more, possibly higher IDs.  We'll
641 ;; assume the maximum that we find is the highest active.  Note that this
642 ;; shouldn't cost us much extra time at all, but will be a lot less
643 ;; vulnerable to glitches between the mbox and the active file.
644
645 (defun nnfolder-read-folder (file)
646   (save-excursion
647     (nnfolder-possibly-activate-groups nil)
648     ;; We should be paranoid here and make sure the group is in the alist,
649     ;; and add it if it isn't.
650     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
651     (set-buffer (setq nnfolder-current-buffer 
652                       (nnheader-find-file-noselect file nil 'raw)))
653     (buffer-disable-undo (current-buffer))
654     (let ((delim (concat "^" rmail-unix-mail-delimiter))
655           (marker (concat "\n" nnfolder-article-marker))
656           (number "[0-9]+")
657           (active (car (cdr (assoc nnfolder-current-group 
658                                    nnfolder-group-alist))))
659           activenumber activemin start end)
660       (goto-char (point-min))
661       ;;
662       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
663       ;; search the file manually to find the active number.  Or, of course,
664       ;; if we're being paranoid.  (This would also be the place to build
665       ;; other lists from the header markers, such as expunge lists, etc., if
666       ;; we ever desired to abandon the active file entirely for mboxes.)
667       (setq activenumber (cdr active))
668       (if (or nnfolder-ignore-active-file
669               (< activenumber 2))
670           (progn
671             (setq activemin (max (1- (lsh 1 23)) 
672                                  (1- (lsh 1 24)) 
673                                  (1- (lsh 1 25))))
674             (while (and (search-forward marker nil t)
675                         (re-search-forward number nil t))
676               (let ((newnum (string-to-number (buffer-substring
677                                                (match-beginning 0)
678                                                (match-end 0)))))
679                 (setq activenumber (max activenumber newnum))
680                 (setq activemin (min activemin newnum))))
681             (setcar active (min activemin activenumber))
682             (setcdr active (max activenumber (cdr active)))
683             (goto-char (point-min))))
684
685       ;; Keep track of the active number on our own, and insert it back into
686       ;; the active list when we're done. Also, prime the pump to cut down on
687       ;; the number of searches we do.
688       (setq end (point-marker))
689       (set-marker end (or (and (re-search-forward delim nil t)
690                                (match-beginning 0))
691                           (point-max)))
692       (while (not (= end (point-max)))
693         (setq start (marker-position end))
694         (goto-char end)
695         ;; There may be more than one "From " line, so we skip past
696         ;; them.  
697         (while (looking-at delim) 
698           (forward-line 1))
699         (set-marker end (or (and (re-search-forward delim nil t)
700                                  (match-beginning 0))
701                             (point-max)))
702         (goto-char start)
703         (if (not (search-forward marker end t))
704             (progn
705               (narrow-to-region start end)
706               (nnmail-insert-lines)
707               (nnfolder-insert-newsgroup-line
708                (cons nil (nnfolder-active-number nnfolder-current-group)))
709               (widen))))
710
711       ;; Make absolutely sure that the active list reflects reality!
712       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
713       (current-buffer))))
714
715 (provide 'nnfolder)
716
717 ;;; nnfolder.el ends here