*** empty log message ***
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96 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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.  
30
31 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
32
33 ;;; Code:
34
35 (require 'nnheader)
36 (require 'rmail)
37 (require 'nnmail)
38 (eval-when-compile (require 'cl))
39
40 (defvar nnfolder-directory (expand-file-name "~/Mail/")
41   "The name of the nnfolder directory.")
42
43 (defvar nnfolder-active-file 
44   (concat (file-name-as-directory nnfolder-directory) "active")
45   "The name of the active file.")
46
47 ;; I renamed this variable to something more in keeping with the general GNU
48 ;; style. -SLB
49
50 (defvar nnfolder-ignore-active-file nil
51   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
52 Note that the active file is still saved, but it's values are not
53 used.  This costs some extra time when scanning an mbox when opening
54 it.")
55
56 (defvar nnfolder-newsgroups-file 
57   (concat (file-name-as-directory nnfolder-directory) "newsgroups")
58   "Mail newsgroups description file.")
59
60 (defvar nnfolder-get-new-mail t
61   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
62
63 (defvar nnfolder-prepare-save-mail-hook nil
64   "Hook run narrowed to an article before saving.")
65
66 (defvar nnfolder-inhibit-expiry nil
67   "If non-nil, inhibit expiry.")
68
69 \f
70
71 (defconst nnfolder-version "nnfolder 1.0"
72   "nnfolder version.")
73
74 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
75   "String used to demarcate what the article number for a message is.")
76
77 (defvar nnfolder-current-group nil)
78 (defvar nnfolder-current-buffer nil)
79 (defvar nnfolder-status-string "")
80 (defvar nnfolder-group-alist nil)
81 (defvar nnfolder-buffer-alist nil)
82 (defvar nnfolder-active-timestamp nil)
83
84 \f
85
86 (defvar nnfolder-current-server nil)
87 (defvar nnfolder-server-alist nil)
88 (defvar nnfolder-server-variables 
89   `((nnfolder-directory ,nnfolder-directory)
90     (nnfolder-active-file ,nnfolder-active-file)
91     (nnfolder-newsgroups-file ,nnfolder-newsgroups-file)
92     (nnfolder-get-new-mail ,nnfolder-get-new-mail)
93     (nnfolder-inhibit-expiry ,nnfolder-inhibit-expiry) 
94     (nnfolder-current-group nil)
95     (nnfolder-prepare-save-mail-hook nil)
96     (nnfolder-ignore-active-file ,nnfolder-ignore-active-file)
97     (nnfolder-current-buffer nil)
98     (nnfolder-status-string "")
99     (nnfolder-group-alist nil)
100     (nnfolder-buffer-alist nil)
101     (nnfolder-active-timestamp nil)))
102
103 \f
104
105 ;;; Interface functions
106
107 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
108   (save-excursion
109     (set-buffer nntp-server-buffer)
110     (erase-buffer)
111     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
112           article art-string start stop)
113       (nnfolder-possibly-change-group newsgroup server)
114       (set-buffer nnfolder-current-buffer)
115       (goto-char (point-min))
116       (if (stringp (car sequence))
117           'headers
118         (while sequence
119           (setq article (car sequence))
120           (setq art-string (nnfolder-article-string article))
121           (set-buffer nnfolder-current-buffer)
122           (if (or (search-forward art-string nil t)
123                   ;; Don't search the whole file twice!  Also, articles
124                   ;; probably have some locality by number, so searching
125                   ;; backwards will be faster.  Especially if we're at the
126                   ;; beginning of the buffer :-). -SLB
127                   (search-backward art-string nil t))
128               (progn
129                 (setq start (or (re-search-backward delim-string nil t)
130                                 (point)))
131                 (search-forward "\n\n" nil t)
132                 (setq stop (1- (point)))
133                 (set-buffer nntp-server-buffer)
134                 (insert (format "221 %d Article retrieved.\n" article))
135                 (insert-buffer-substring nnfolder-current-buffer start stop)
136                 (goto-char (point-max))
137                 (insert ".\n")))
138           (setq sequence (cdr sequence)))
139
140         (set-buffer nntp-server-buffer)
141         (nnheader-fold-continuation-lines)
142         'headers))))
143
144 (defun nnfolder-open-server (server &optional defs)
145   (nnheader-change-server 'nnfolder server defs)
146   (when (not (file-exists-p nnfolder-directory))
147     (condition-case ()
148         (make-directory nnfolder-directory t)
149       (error t)))
150   (cond 
151    ((not (file-exists-p nnfolder-directory))
152     (nnfolder-close-server)
153     (nnheader-report 'nnfolder "Couldn't create directory: %s"
154                      nnfolder-directory))
155    ((not (file-directory-p (file-truename nnfolder-directory)))
156     (nnfolder-close-server)
157     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
158    (t
159     (nnheader-report 'nnfolder "Opened server %s using directory %s"
160                      server nnfolder-directory)
161     t)))
162
163 (defun nnfolder-close-server (&optional server)
164   (setq nnfolder-current-server nil
165         nnfolder-group-alist nil)
166   t)
167
168 (defun nnfolder-server-opened (&optional server)
169   (and (equal server nnfolder-current-server)
170        nntp-server-buffer
171        (buffer-name nntp-server-buffer)))
172
173 (defun nnfolder-request-close ()
174   (let ((alist nnfolder-buffer-alist))
175     (while alist
176       (nnfolder-close-group (caar alist) nil t)
177       (setq alist (cdr alist))))
178   (setq nnfolder-buffer-alist nil
179         nnfolder-current-server nil
180         nnfolder-group-alist nil))
181
182 (defun nnfolder-status-message (&optional server)
183   nnfolder-status-string)
184
185 (defun nnfolder-request-article (article &optional newsgroup server buffer)
186   (nnfolder-possibly-change-group newsgroup server)
187   (save-excursion
188     (set-buffer nnfolder-current-buffer)
189     (goto-char (point-min))
190     (if (search-forward (nnfolder-article-string article) nil t)
191         (let (start stop)
192           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
193           (setq start (point))
194           (forward-line 1)
195           (or (and (re-search-forward 
196                     (concat "^" rmail-unix-mail-delimiter) nil t)
197                    (forward-line -1))
198               (goto-char (point-max)))
199           (setq stop (point))
200           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
201             (set-buffer nntp-server-buffer)
202             (erase-buffer)
203             (insert-buffer-substring nnfolder-current-buffer start stop)
204             (goto-char (point-min))
205             (while (looking-at "From ")
206               (delete-char 5)
207               (insert "X-From-Line: ")
208               (forward-line 1))
209             (if (numberp article) 
210                 (cons nnfolder-current-group article)
211               (goto-char (point-min))
212               (search-forward (concat "\n" nnfolder-article-marker))
213               (cons nnfolder-current-group
214                     (string-to-int 
215                      (buffer-substring 
216                       (point) (progn (end-of-line) (point)))))))))))
217
218 (defun nnfolder-request-group (group &optional server dont-check)
219   (save-excursion
220     (nnmail-activate 'nnfolder)
221     (if (not (assoc group nnfolder-group-alist))
222         (nnheader-report 'nnfolder "No such group: %s" group)
223       (nnfolder-possibly-change-group group server)
224       (if dont-check
225           (progn 
226             (nnheader-report 'nnfolder "Selected group %s" group)
227             t)
228         (let* ((active (assoc group nnfolder-group-alist))
229                (group (car active))
230                (range (cadr active))
231                (minactive (car range))
232                (maxactive (cdr range)))
233           (cond 
234            ((null active)
235             (nnheader-report 'nnfolder "No such group: %s" group))
236            (t
237             (nnheader-report 'nnfolder "Selected group %s" group)
238             (nnheader-insert "211 %d %d %d %s\n" 
239                              (1+ (- maxactive minactive))
240                              minactive maxactive group))))))))
241
242 (defun nnfolder-request-scan (&optional group server)
243   (nnfolder-possibly-change-group 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              (kill-buffer (current-buffer)))
256            (setq bufs (cdr bufs))))))
257    nnfolder-directory
258    group))
259
260 ;; Don't close the buffer if we're not shutting down the server.  This way,
261 ;; we can keep the buffer in the group buffer cache, and not have to grovel
262 ;; over the buffer again unless we add new mail to it or modify it in some
263 ;; way.
264
265 (defun nnfolder-close-group (group &optional server force)
266   ;; Make sure we _had_ the group open.
267   (when (or (assoc group nnfolder-buffer-alist)
268             (equal group nnfolder-current-group))
269     (nnfolder-possibly-change-group group server)
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 we're shutting the server down, we need to kill the
275       ;; buffer and remove it from the open buffer list.  Or, of
276       ;; course, if we're trying to minimize our space impact.
277       (kill-buffer (current-buffer))
278       (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
279                                         nnfolder-buffer-alist))))
280   (setq nnfolder-current-group nil
281         nnfolder-current-buffer nil)
282   t)
283
284 (defun nnfolder-request-create-group (group &optional server) 
285   (nnmail-activate 'nnfolder)
286   (when group 
287     (unless (assoc group nnfolder-group-alist)
288       (push (list group (cons 1 0)) nnfolder-group-alist)
289       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
290   t)
291
292 (defun nnfolder-request-list (&optional server)
293   (save-excursion
294     (nnmail-find-file nnfolder-active-file)
295     (setq nnfolder-group-alist (nnmail-get-active))))
296
297 (defun nnfolder-request-newgroups (date &optional server)
298   (nnfolder-request-list server))
299
300 (defun nnfolder-request-list-newsgroups (&optional server)
301   (save-excursion
302     (nnmail-find-file nnfolder-newsgroups-file)))
303
304 (defun nnfolder-request-expire-articles 
305   (articles newsgroup &optional server force)
306   (nnfolder-possibly-change-group newsgroup server)
307   (let* ((is-old t)
308          rest)
309     (nnmail-activate 'nnfolder)
310
311     (save-excursion 
312       (set-buffer nnfolder-current-buffer)
313       (while (and articles is-old)
314         (goto-char (point-min))
315         (if (search-forward (nnfolder-article-string (car articles)) nil t)
316             (if (setq is-old
317                       (nnmail-expired-article-p 
318                        newsgroup
319                        (buffer-substring 
320                         (point) (progn (end-of-line) (point))) 
321                        force nnfolder-inhibit-expiry))
322                 (progn
323                   (nnheader-message 5 "Deleting article %d..." 
324                                     (car articles) newsgroup)
325                   (nnfolder-delete-mail))
326               (setq rest (cons (car articles) rest))))
327         (setq articles (cdr articles)))
328       (and (buffer-modified-p) (save-buffer))
329       ;; Find the lowest active article in this group.
330       (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
331              (marker (concat "\n" nnfolder-article-marker))
332              (number "[0-9]+")
333              (activemin (cdr active)))
334         (goto-char (point-min))
335         (while (and (search-forward marker nil t)
336                     (re-search-forward number nil t))
337           (setq activemin (min activemin
338                                (string-to-number (buffer-substring
339                                                   (match-beginning 0)
340                                                   (match-end 0))))))
341         (setcar active activemin))
342       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
343       (nconc rest articles))))
344
345 (defun nnfolder-request-move-article
346   (article group server accept-form &optional last)
347   (nnfolder-possibly-change-group group server)
348   (let ((buf (get-buffer-create " *nnfolder move*"))
349         result)
350     (and 
351      (nnfolder-request-article article group server)
352      (save-excursion
353        (set-buffer buf)
354        (buffer-disable-undo (current-buffer))
355        (erase-buffer)
356        (insert-buffer-substring nntp-server-buffer)
357        (goto-char (point-min))
358        (while (re-search-forward 
359                (concat "^" nnfolder-article-marker)
360                (save-excursion (search-forward "\n\n" nil t) (point)) t)
361          (delete-region (progn (beginning-of-line) (point))
362                         (progn (forward-line 1) (point))))
363        (setq result (eval accept-form))
364        (kill-buffer buf)
365        result)
366      (save-excursion
367        (nnfolder-possibly-change-group group server)
368        (set-buffer nnfolder-current-buffer)
369        (goto-char (point-min))
370        (if (search-forward (nnfolder-article-string article) nil t)
371            (nnfolder-delete-mail))
372        (and last 
373             (buffer-modified-p)
374             (save-buffer))))
375     result))
376
377 (defun nnfolder-request-accept-article (group &optional last)
378   (and (stringp group) (nnfolder-possibly-change-group group))
379   (let ((buf (current-buffer))
380         result)
381     (goto-char (point-min))
382     (when (looking-at "X-From-Line: ")
383       (replace-match "From "))
384     (and 
385      (nnfolder-request-list)
386      (save-excursion
387        (set-buffer buf)
388        (goto-char (point-min))
389        (search-forward "\n\n" nil t)
390        (forward-line -1)
391        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
392          (delete-region (point) (progn (forward-line 1) (point))))
393        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
394      (save-excursion
395        (set-buffer nnfolder-current-buffer)
396        (and last (buffer-modified-p) (save-buffer))))
397     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
398     result))
399
400 (defun nnfolder-request-replace-article (article group buffer)
401   (nnfolder-possibly-change-group group)
402   (save-excursion
403     (set-buffer nnfolder-current-buffer)
404     (goto-char (point-min))
405     (if (not (search-forward (nnfolder-article-string article) nil t))
406         nil
407       (nnfolder-delete-mail t t)
408       (insert-buffer-substring buffer)
409       (and (buffer-modified-p) (save-buffer))
410       t)))
411
412 (defun nnfolder-request-delete-group (group &optional force server)
413   (nnfolder-close-group group server t)
414   ;; Delete all articles in GROUP.
415   (if (not force)
416       ()                                ; Don't delete the articles.
417     ;; Delete the file that holds the group.
418     (condition-case nil
419         (delete-file (concat (file-name-as-directory nnfolder-directory)
420                              group))
421       (error nil)))
422   ;; Remove the group from all structures.
423   (setq nnfolder-group-alist 
424         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
425         nnfolder-current-group nil
426         nnfolder-current-buffer nil)
427   ;; Save the active file.
428   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
429   t)
430
431 (defun nnfolder-request-rename-group (group new-name &optional server)
432   (nnfolder-possibly-change-group group server)
433   (save-excursion
434     (set-buffer nnfolder-current-buffer)
435     (and (file-writable-p buffer-file-name)
436          (condition-case ()
437              (progn
438                (rename-file buffer-file-name
439                             (concat (file-name-as-directory nnfolder-directory)
440                                     new-name))
441                t)
442            (error nil))
443          ;; That went ok, so we change the internal structures.
444          (let ((entry (assoc group nnfolder-group-alist)))
445            (and entry (setcar entry new-name))
446            (setq nnfolder-current-buffer nil
447                  nnfolder-current-group nil)
448            ;; Save the new group alist.
449            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
450            ;; We kill the buffer instead of renaming it and stuff.
451            (kill-buffer (current-buffer))
452            t))))
453
454 \f
455 ;;; Internal functions.
456
457 (defun nnfolder-article-string (article)
458   (if (numberp article)
459       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
460     (concat "\nMessage-ID: " article)))
461
462 (defun nnfolder-delete-mail (&optional force leave-delim)
463   ;; Beginning of the article.
464   (save-excursion
465     (save-restriction
466       (narrow-to-region
467        (save-excursion
468          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
469          (if leave-delim (progn (forward-line 1) (point))
470            (match-beginning 0)))
471        (progn
472          (forward-line 1)
473          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
474                                      nil t)
475                   (if (and (not (bobp)) leave-delim)
476                       (progn (forward-line -2) (point))
477                     (match-beginning 0)))
478              (point-max))))
479       (delete-region (point-min) (point-max)))))
480
481 (defun nnfolder-possibly-change-group (group &optional server)
482   (when (and server
483              (not (nnfolder-server-opened server)))
484     (nnfolder-open-server server))
485   (unless (file-exists-p nnfolder-directory)
486     (make-directory (directory-file-name nnfolder-directory) t))
487   (nnfolder-possibly-activate-groups nil)
488   (or (assoc group nnfolder-group-alist)
489       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
490                                   group)))
491       (progn
492         (setq nnfolder-group-alist 
493               (cons (list group (cons 1 0)) nnfolder-group-alist))
494         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
495   (let (inf file)
496     (if (and (equal group nnfolder-current-group)
497              nnfolder-current-buffer
498              (buffer-name nnfolder-current-buffer))
499         ()
500       (setq nnfolder-current-group group)
501
502       ;; If we have to change groups, see if we don't already have the mbox
503       ;; in memory.  If we do, verify the modtime and destroy the mbox if
504       ;; needed so we can rescan it.
505       (if (setq inf (assoc group nnfolder-buffer-alist))
506           (setq nnfolder-current-buffer (nth 1 inf)))
507
508       ;; If the buffer is not live, make sure it isn't in the alist.  If it
509       ;; is live, verify that nobody else has touched the file since last
510       ;; time.
511       (if (or (not (and nnfolder-current-buffer
512                         (buffer-name nnfolder-current-buffer)))
513               (not (and (bufferp nnfolder-current-buffer)
514                         (verify-visited-file-modtime 
515                          nnfolder-current-buffer))))
516           (progn
517             (if (and nnfolder-current-buffer
518                      (buffer-name nnfolder-current-buffer)
519                      (bufferp nnfolder-current-buffer))
520                 (kill-buffer nnfolder-current-buffer))
521             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
522             (setq inf nil)))
523       
524       (if inf
525           ()
526         (save-excursion
527           (setq file (concat (file-name-as-directory nnfolder-directory)
528                              group))
529           (if (file-directory-p (file-truename file))
530               ()
531             (if (not (file-exists-p file))
532                 (write-region 1 1 file t 'nomesg))
533             (setq nnfolder-current-buffer 
534                   (set-buffer (nnfolder-read-folder file)))
535             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
536                                               nnfolder-buffer-alist)))))))
537   (setq nnfolder-current-group group))
538
539 (defun nnfolder-save-mail (&optional group)
540   "Called narrowed to an article."
541   (let* ((nnmail-split-methods 
542           (if group (list (list group "")) nnmail-split-methods))
543          (group-art-list
544           (nreverse (nnmail-article-group 'nnfolder-active-number)))
545          (delim (concat "^" rmail-unix-mail-delimiter))
546          save-list group-art)
547     (goto-char (point-min))
548     ;; This might come from somewhere else.
549     (unless (looking-at delim)
550       (insert "From nobody " (current-time-string) "\n")
551       (goto-char (point-min)))
552     ;; Quote all "From " lines in the article.
553     (forward-line 1)
554     (while (re-search-forward delim nil t)
555       (beginning-of-line)
556       (insert "> "))
557     (setq save-list group-art-list)
558     (nnmail-insert-lines)
559     (nnmail-insert-xref group-art-list)
560     (run-hooks 'nnfolder-prepare-save-mail-hook)
561
562     ;; Insert the mail into each of the destination groups.
563     (while group-art-list
564       (setq group-art (car group-art-list)
565             group-art-list (cdr group-art-list))
566
567       ;; Kill the previous newsgroup markers.
568       (goto-char (point-min))
569       (search-forward "\n\n" nil t)
570       (forward-line -1)
571       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
572         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
573
574       (nnfolder-possibly-change-group (car group-art))
575       ;; Insert the new newsgroup marker.
576       (nnfolder-insert-newsgroup-line group-art)
577       (unless nnfolder-current-buffer
578         (nnfolder-request-create-group (car group-art))
579         (nnfolder-possibly-change-group (car group-art)))
580       (let ((beg (point-min))
581             (end (point-max))
582             (obuf (current-buffer)))
583         (set-buffer nnfolder-current-buffer)
584         (goto-char (point-max))
585         (insert-buffer-substring obuf beg end)
586         (set-buffer obuf)))
587
588     ;; Did we save it anywhere?
589     save-list))
590
591 (defun nnfolder-insert-newsgroup-line (group-art)
592   (save-excursion
593     (goto-char (point-min))
594     (if (search-forward "\n\n" nil t)
595         (progn
596           (forward-char -1)
597           (insert (format (concat nnfolder-article-marker "%d   %s\n")
598                           (cdr group-art) (current-time-string)))))))
599
600 (defun nnfolder-possibly-activate-groups (&optional group)
601   (save-excursion
602     ;; If we're looking for the activation of a specific group, find out
603     ;; its real name and switch to it.
604     (if group (nnfolder-possibly-change-group group))
605     ;; If the group alist isn't active, activate it now.
606     (nnmail-activate 'nnfolder)))
607
608 (defun nnfolder-active-number (group)
609   (save-excursion 
610     ;; Find the next article number in GROUP.
611     (prog1
612         (let ((active (cadr (assoc group nnfolder-group-alist))))
613           (if active
614               (setcdr active (1+ (cdr active)))
615             ;; This group is new, so we create a new entry for it.
616             ;; This might be a bit naughty... creating groups on the drop of
617             ;; a hat, but I don't know...
618             (setq nnfolder-group-alist 
619                   (cons (list group (setq active (cons 1 1)))
620                         nnfolder-group-alist)))
621           (cdr active))
622       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
623       (nnfolder-possibly-activate-groups group))))
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 across 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 (cadr (assoc nnfolder-current-group 
654                                 nnfolder-group-alist)))
655            ;; Set min to Big Number.
656            (min (max (1- (lsh 1 23)) (1- (lsh 1 24)) (1- (lsh 1 25)))) 
657            (max (cdr active))
658            start end)
659       (goto-char (point-min))
660
661       ;; Anytime the active number is 1 or 0, it is suspect.  In that case,
662       ;; search the file manually to find the active number.  Or, of course,
663       ;; if we're being paranoid.  (This would also be the place to build
664       ;; other lists from the header markers, such as expunge lists, etc., if
665       ;; we ever desired to abandon the active file entirely for mboxes.)
666       (when (or nnfolder-ignore-active-file
667                 (< max 2))
668         (while (and (search-forward marker nil t)
669                     (re-search-forward number nil t))
670           (let ((newnum (string-to-number (match-string 0))))
671             (setq max (max max newnum))
672             (setq min (min min newnum))))
673         (setcar active (max 1 (min min max)))
674         (setcdr active (max max (cdr active)))
675         (goto-char (point-min)))
676
677       ;; Keep track of the active number on our own, and insert it back into
678       ;; the active list when we're done. Also, prime the pump to cut down on
679       ;; the number of searches we do.
680       (setq end (point-marker))
681       (set-marker end (or (and (re-search-forward delim nil t)
682                                (match-beginning 0))
683                           (point-max)))
684       (while (not (= end (point-max)))
685         (setq start (marker-position end))
686         (goto-char end)
687         ;; There may be more than one "From " line, so we skip past
688         ;; them.  
689         (while (looking-at delim) 
690           (forward-line 1))
691         (set-marker end (or (and (re-search-forward delim nil t)
692                                  (match-beginning 0))
693                             (point-max)))
694         (goto-char start)
695         (if (not (search-forward marker end t))
696             (progn
697               (narrow-to-region start end)
698               (nnmail-insert-lines)
699               (nnfolder-insert-newsgroup-line
700                (cons nil (nnfolder-active-number nnfolder-current-group)))
701               (widen))))
702
703       ;; Make absolutely sure that the active list reflects reality!
704       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
705       (current-buffer))))
706
707 ;;;###autoload
708 (defun nnfolder-generate-active-file ()
709   "Look for mbox folders in the nnfolder directory and make them into groups."
710   (interactive)
711   (nnmail-activate 'nnfolder)
712   (let ((files (directory-files nnfolder-directory))
713         file)
714     (while (setq file (pop files))
715       (when (nnheader-mail-file-mbox-p file)
716         (nnheader-message 5 "Adding group %s..." file)
717         (push (list file (cons 1 0)) nnfolder-group-alist)
718         (nnfolder-read-folder file)
719         (nnfolder-close-group file))
720       (message ""))))
721
722 (provide 'nnfolder)
723
724 ;;; nnfolder.el ends here