*** 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 (articles &optional group 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 group server)
114       (set-buffer nnfolder-current-buffer)
115       (goto-char (point-min))
116       (if (stringp (car articles))
117           'headers
118         (while articles
119           (setq article (car articles))
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 articles (cdr articles)))
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-server-alist nil
180         nnfolder-current-server nil
181         nnfolder-group-alist nil))
182
183 (defun nnfolder-status-message (&optional server)
184   (nnfolder-possibly-change-group nil server)
185   nnfolder-status-string)
186
187 (defun nnfolder-request-article (article &optional group server buffer)
188   (nnfolder-possibly-change-group group server)
189   (save-excursion
190     (set-buffer nnfolder-current-buffer)
191     (goto-char (point-min))
192     (if (search-forward (nnfolder-article-string article) nil t)
193         (let (start stop)
194           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
195           (setq start (point))
196           (forward-line 1)
197           (or (and (re-search-forward 
198                     (concat "^" rmail-unix-mail-delimiter) nil t)
199                    (forward-line -1))
200               (goto-char (point-max)))
201           (setq stop (point))
202           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
203             (set-buffer nntp-server-buffer)
204             (erase-buffer)
205             (insert-buffer-substring nnfolder-current-buffer start stop)
206             (goto-char (point-min))
207             (while (looking-at "From ")
208               (delete-char 5)
209               (insert "X-From-Line: ")
210               (forward-line 1))
211             (if (numberp article) 
212                 (cons nnfolder-current-group article)
213               (goto-char (point-min))
214               (search-forward (concat "\n" nnfolder-article-marker))
215               (cons nnfolder-current-group
216                     (string-to-int 
217                      (buffer-substring 
218                       (point) (progn (end-of-line) (point)))))))))))
219
220 (defun nnfolder-request-group (group &optional server dont-check)
221   (save-excursion
222     (nnmail-activate 'nnfolder)
223     (if (not (assoc group nnfolder-group-alist))
224         (nnheader-report 'nnfolder "No such group: %s" group)
225       (nnfolder-possibly-change-group group server)
226       (if dont-check
227           (progn 
228             (nnheader-report 'nnfolder "Selected group %s" group)
229             t)
230         (let* ((active (assoc group nnfolder-group-alist))
231                (group (car active))
232                (range (cadr active)))
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+ (- (cdr range) (car range)))
240                              (car range) (cdr range) 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     (when nnfolder-current-buffer
271       (save-excursion
272         (set-buffer nnfolder-current-buffer)
273         ;; If the buffer was modified, write the file out now.
274         (and (buffer-modified-p) (save-buffer))
275         ;; If we're shutting the server down, we need to kill the
276         ;; buffer and remove it from the open buffer list.  Or, of
277         ;; course, if we're trying to minimize our space impact.
278         (kill-buffer (current-buffer))
279         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
280                                           nnfolder-buffer-alist)))))
281   (setq nnfolder-current-group nil
282         nnfolder-current-buffer nil)
283   t)
284
285 (defun nnfolder-request-create-group (group &optional server) 
286   (nnfolder-possibly-change-group nil server)
287   (nnmail-activate 'nnfolder)
288   (when group 
289     (unless (assoc group nnfolder-group-alist)
290       (push (list group (cons 1 0)) nnfolder-group-alist)
291       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
292   t)
293
294 (defun nnfolder-request-list (&optional server)
295   (nnfolder-possibly-change-group nil server)
296   (save-excursion
297     (nnmail-find-file nnfolder-active-file)
298     (setq nnfolder-group-alist (nnmail-get-active))))
299
300 (defun nnfolder-request-newgroups (date &optional server)
301   (nnfolder-possibly-change-group nil server)
302   (nnfolder-request-list server))
303
304 (defun nnfolder-request-list-newsgroups (&optional server)
305   (nnfolder-possibly-change-group nil server)
306   (save-excursion
307     (nnmail-find-file nnfolder-newsgroups-file)))
308
309 (defun nnfolder-request-expire-articles 
310   (articles newsgroup &optional server force)
311   (nnfolder-possibly-change-group newsgroup server)
312   (let* ((is-old t)
313          rest)
314     (nnmail-activate 'nnfolder)
315
316     (save-excursion 
317       (set-buffer nnfolder-current-buffer)
318       (while (and articles is-old)
319         (goto-char (point-min))
320         (if (search-forward (nnfolder-article-string (car articles)) nil t)
321             (if (setq is-old
322                       (nnmail-expired-article-p 
323                        newsgroup
324                        (buffer-substring 
325                         (point) (progn (end-of-line) (point))) 
326                        force nnfolder-inhibit-expiry))
327                 (progn
328                   (nnheader-message 5 "Deleting article %d..." 
329                                     (car articles) newsgroup)
330                   (nnfolder-delete-mail))
331               (setq rest (cons (car articles) rest))))
332         (setq articles (cdr articles)))
333       (and (buffer-modified-p) (save-buffer))
334       ;; Find the lowest active article in this group.
335       (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
336              (marker (concat "\n" nnfolder-article-marker))
337              (number "[0-9]+")
338              (activemin (cdr active)))
339         (goto-char (point-min))
340         (while (and (search-forward marker nil t)
341                     (re-search-forward number nil t))
342           (setq activemin (min activemin
343                                (string-to-number (buffer-substring
344                                                   (match-beginning 0)
345                                                   (match-end 0))))))
346         (setcar active activemin))
347       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
348       (nconc rest articles))))
349
350 (defun nnfolder-request-move-article
351   (article group server accept-form &optional last)
352   (nnfolder-possibly-change-group group server)
353   (let ((buf (get-buffer-create " *nnfolder move*"))
354         result)
355     (and 
356      (nnfolder-request-article article group server)
357      (save-excursion
358        (set-buffer buf)
359        (buffer-disable-undo (current-buffer))
360        (erase-buffer)
361        (insert-buffer-substring nntp-server-buffer)
362        (goto-char (point-min))
363        (while (re-search-forward 
364                (concat "^" nnfolder-article-marker)
365                (save-excursion (search-forward "\n\n" nil t) (point)) t)
366          (delete-region (progn (beginning-of-line) (point))
367                         (progn (forward-line 1) (point))))
368        (setq result (eval accept-form))
369        (kill-buffer buf)
370        result)
371      (save-excursion
372        (nnfolder-possibly-change-group group server)
373        (set-buffer nnfolder-current-buffer)
374        (goto-char (point-min))
375        (if (search-forward (nnfolder-article-string article) nil t)
376            (nnfolder-delete-mail))
377        (and last 
378             (buffer-modified-p)
379             (save-buffer))))
380     result))
381
382 (defun nnfolder-request-accept-article (group &optional server last)
383   (nnfolder-possibly-change-group group server)
384   (and (stringp group) (nnfolder-possibly-change-group group))
385   (let ((buf (current-buffer))
386         result)
387     (goto-char (point-min))
388     (when (looking-at "X-From-Line: ")
389       (replace-match "From "))
390     (and 
391      (nnfolder-request-list)
392      (save-excursion
393        (set-buffer buf)
394        (goto-char (point-min))
395        (search-forward "\n\n" nil t)
396        (forward-line -1)
397        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
398          (delete-region (point) (progn (forward-line 1) (point))))
399        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
400      (save-excursion
401        (set-buffer nnfolder-current-buffer)
402        (and last (buffer-modified-p) (save-buffer))))
403     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
404     (unless result
405       (nnheader-report 'nnfolder "Couldn't store article"))
406     result))
407
408 (defun nnfolder-request-replace-article (article group buffer)
409   (nnfolder-possibly-change-group group)
410   (save-excursion
411     (set-buffer nnfolder-current-buffer)
412     (goto-char (point-min))
413     (if (not (search-forward (nnfolder-article-string article) nil t))
414         nil
415       (nnfolder-delete-mail t t)
416       (insert-buffer-substring buffer)
417       (and (buffer-modified-p) (save-buffer))
418       t)))
419
420 (defun nnfolder-request-delete-group (group &optional force server)
421   (nnfolder-close-group group server t)
422   ;; Delete all articles in GROUP.
423   (if (not force)
424       ()                                ; Don't delete the articles.
425     ;; Delete the file that holds the group.
426     (condition-case nil
427         (delete-file (nnfolder-group-pathname group))
428       (error nil)))
429   ;; Remove the group from all structures.
430   (setq nnfolder-group-alist 
431         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
432         nnfolder-current-group nil
433         nnfolder-current-buffer nil)
434   ;; Save the active file.
435   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
436   t)
437
438 (defun nnfolder-request-rename-group (group new-name &optional server)
439   (nnfolder-possibly-change-group group server)
440   (save-excursion
441     (set-buffer nnfolder-current-buffer)
442     (and (file-writable-p buffer-file-name)
443          (condition-case ()
444              (progn
445                (rename-file 
446                 buffer-file-name
447                 (nnfolder-group-pathname new-name))
448                t)
449            (error nil))
450          ;; That went ok, so we change the internal structures.
451          (let ((entry (assoc group nnfolder-group-alist)))
452            (and entry (setcar entry new-name))
453            (setq nnfolder-current-buffer nil
454                  nnfolder-current-group nil)
455            ;; Save the new group alist.
456            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
457            ;; We kill the buffer instead of renaming it and stuff.
458            (kill-buffer (current-buffer))
459            t))))
460
461 \f
462 ;;; Internal functions.
463
464 (defun nnfolder-article-string (article)
465   (if (numberp article)
466       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
467     (concat "\nMessage-ID: " article)))
468
469 (defun nnfolder-delete-mail (&optional force leave-delim)
470   ;; Beginning of the article.
471   (save-excursion
472     (save-restriction
473       (narrow-to-region
474        (save-excursion
475          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
476          (if leave-delim (progn (forward-line 1) (point))
477            (match-beginning 0)))
478        (progn
479          (forward-line 1)
480          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
481                                      nil t)
482                   (if (and (not (bobp)) leave-delim)
483                       (progn (forward-line -2) (point))
484                     (match-beginning 0)))
485              (point-max))))
486       (delete-region (point-min) (point-max)))))
487
488 (defun nnfolder-possibly-change-group (group &optional server)
489   (when (and server
490              (not (nnfolder-server-opened server)))
491     (nnfolder-open-server server))
492   (when group
493     (unless (file-exists-p nnfolder-directory)
494       (make-directory (directory-file-name nnfolder-directory) t))
495     (nnfolder-possibly-activate-groups nil)
496     (or (assoc group nnfolder-group-alist)
497         (not (file-exists-p
498               (nnfolder-group-pathname 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 (nnfolder-group-pathname group))
536             (if (file-directory-p (file-truename file))
537                 ()
538               (unless (file-exists-p file)
539                 (unless (file-exists-p (file-name-directory file))
540                   (make-directory (file-name-directory file) t))
541                 (write-region 1 1 file t 'nomesg))
542               (setq nnfolder-current-buffer 
543                     (set-buffer (nnfolder-read-folder file)))
544               (setq nnfolder-buffer-alist (cons (list group (current-buffer))
545                                                 nnfolder-buffer-alist)))))))
546     (setq nnfolder-current-group group)))
547
548 (defun nnfolder-save-mail (&optional group)
549   "Called narrowed to an article."
550   (let* ((nnmail-split-methods 
551           (if group (list (list group "")) nnmail-split-methods))
552          (group-art-list
553           (nreverse (nnmail-article-group 'nnfolder-active-number)))
554          (delim (concat "^" rmail-unix-mail-delimiter))
555          save-list group-art)
556     (goto-char (point-min))
557     ;; This might come from somewhere else.
558     (unless (looking-at delim)
559       (insert "From nobody " (current-time-string) "\n")
560       (goto-char (point-min)))
561     ;; Quote all "From " lines in the article.
562     (forward-line 1)
563     (while (re-search-forward delim nil t)
564       (beginning-of-line)
565       (insert "> "))
566     (setq save-list group-art-list)
567     (nnmail-insert-lines)
568     (nnmail-insert-xref group-art-list)
569     (run-hooks 'nnfolder-prepare-save-mail-hook)
570
571     ;; Insert the mail into each of the destination groups.
572     (while group-art-list
573       (setq group-art (car group-art-list)
574             group-art-list (cdr group-art-list))
575
576       ;; Kill the previous newsgroup markers.
577       (goto-char (point-min))
578       (search-forward "\n\n" nil t)
579       (forward-line -1)
580       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
581         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
582
583       (nnfolder-possibly-change-group (car group-art))
584       ;; Insert the new newsgroup marker.
585       (nnfolder-insert-newsgroup-line group-art)
586       (unless nnfolder-current-buffer
587         (nnfolder-request-create-group (car group-art))
588         (nnfolder-possibly-change-group (car group-art)))
589       (let ((beg (point-min))
590             (end (point-max))
591             (obuf (current-buffer)))
592         (set-buffer nnfolder-current-buffer)
593         (goto-char (point-max))
594         (insert-buffer-substring obuf beg end)
595         (set-buffer obuf)))
596
597     ;; Did we save it anywhere?
598     save-list))
599
600 (defun nnfolder-insert-newsgroup-line (group-art)
601   (save-excursion
602     (goto-char (point-min))
603     (if (search-forward "\n\n" nil t)
604         (progn
605           (forward-char -1)
606           (insert (format (concat nnfolder-article-marker "%d   %s\n")
607                           (cdr group-art) (current-time-string)))))))
608
609 (defun nnfolder-possibly-activate-groups (&optional group)
610   (save-excursion
611     ;; If we're looking for the activation of a specific group, find out
612     ;; its real name and switch to it.
613     (if group (nnfolder-possibly-change-group group))
614     ;; If the group alist isn't active, activate it now.
615     (nnmail-activate 'nnfolder)))
616
617 (defun nnfolder-active-number (group)
618   (save-excursion 
619     ;; Find the next article number in GROUP.
620     (prog1
621         (let ((active (cadr (assoc group nnfolder-group-alist))))
622           (if active
623               (setcdr active (1+ (cdr active)))
624             ;; This group is new, so we create a new entry for it.
625             ;; This might be a bit naughty... creating groups on the drop of
626             ;; a hat, but I don't know...
627             (setq nnfolder-group-alist 
628                   (cons (list group (setq active (cons 1 1)))
629                         nnfolder-group-alist)))
630           (cdr active))
631       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
632       (nnfolder-possibly-activate-groups group))))
633
634
635 ;; This method has a problem if you've accidentally let the active list get
636 ;; out of sync with the files.  This could happen, say, if you've
637 ;; accidentally gotten new mail with something other than Gnus (but why
638 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
639 ;; processing the file, ready to add new X-Gnus article number markers, and
640 ;; we'll run across a message with no ID yet - the active list _may_not_ be
641 ;; ready for us yet.
642
643 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
644 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
645 ;; rest of the message looking for any more, possibly higher IDs.  We'll
646 ;; assume the maximum that we find is the highest active.  Note that this
647 ;; shouldn't cost us much extra time at all, but will be a lot less
648 ;; vulnerable to glitches between the mbox and the active file.
649
650 (defun nnfolder-read-folder (file)
651   (save-excursion
652     (nnfolder-possibly-activate-groups nil)
653     ;; We should be paranoid here and make sure the group is in the alist,
654     ;; and add it if it isn't.
655     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
656     (set-buffer (setq nnfolder-current-buffer 
657                       (nnheader-find-file-noselect file nil 'raw)))
658     (buffer-disable-undo (current-buffer))
659     (let* ((delim (concat "^" rmail-unix-mail-delimiter))
660            (marker (concat "\n" nnfolder-article-marker))
661            (number "[0-9]+")
662            (active (cadr (assoc nnfolder-current-group 
663                                 nnfolder-group-alist)))
664            ;; Set min to Big Number.
665            (min (max (1- (lsh 1 23)) (1- (lsh 1 24)) (1- (lsh 1 25)))) 
666            (max (cdr active))
667            start end)
668       (goto-char (point-min))
669
670       ;; Anytime the active number is 1 or 0, it is suspect.  In that case,
671       ;; search the file manually to find the active number.  Or, of course,
672       ;; if we're being paranoid.  (This would also be the place to build
673       ;; other lists from the header markers, such as expunge lists, etc., if
674       ;; we ever desired to abandon the active file entirely for mboxes.)
675       (when (or nnfolder-ignore-active-file
676                 (< max 2))
677         (while (and (search-forward marker nil t)
678                     (re-search-forward number nil t))
679           (let ((newnum (string-to-number (match-string 0))))
680             (setq max (max max newnum))
681             (setq min (min min newnum))))
682         (setcar active (max 1 (min min max)))
683         (setcdr active (max max (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 ;;;###autoload
717 (defun nnfolder-generate-active-file ()
718   "Look for mbox folders in the nnfolder directory and make them into groups."
719   (interactive)
720   (nnmail-activate 'nnfolder)
721   (let ((files (directory-files nnfolder-directory))
722         file)
723     (while (setq file (pop files))
724       (when (nnheader-mail-file-mbox-p file)
725         (nnheader-message 5 "Adding group %s..." file)
726         (push (list file (cons 1 0)) nnfolder-group-alist)
727         (nnfolder-read-folder file)
728         (nnfolder-close-group file))
729       (message ""))))
730
731 (defun nnfolder-group-pathname (group)
732   "Make pathname for GROUP."
733   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
734     ;; If this file exists, we use it directly.
735     (if (or nnmail-use-long-file-names 
736             (file-exists-p (concat dir group)))
737         (concat dir group)
738       ;; If not, we translate dots into slashes.
739       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
740
741 (provide 'nnfolder)
742
743 ;;; nnfolder.el ends here