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