*** 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       (when nnfolder-current-buffer
114         (nnfolder-possibly-change-group newsgroup server)
115         (set-buffer nnfolder-current-buffer)
116         (goto-char (point-min))
117         (if (stringp (car sequence))
118             'headers
119           (while sequence
120             (setq article (car sequence))
121             (setq art-string (nnfolder-article-string article))
122             (set-buffer nnfolder-current-buffer)
123             (if (or (search-forward art-string nil t)
124                     ;; Don't search the whole file twice!  Also, articles
125                     ;; probably have some locality by number, so searching
126                     ;; backwards will be faster.  Especially if we're at the
127                     ;; beginning of the buffer :-). -SLB
128                     (search-backward art-string nil t))
129                 (progn
130                   (setq start (or (re-search-backward delim-string nil t)
131                                   (point)))
132                   (search-forward "\n\n" nil t)
133                   (setq stop (1- (point)))
134                   (set-buffer nntp-server-buffer)
135                   (insert (format "221 %d Article retrieved.\n" article))
136                   (insert-buffer-substring nnfolder-current-buffer start stop)
137                   (goto-char (point-max))
138                   (insert ".\n")))
139             (setq sequence (cdr sequence)))
140
141           (set-buffer nntp-server-buffer)
142           (nnheader-fold-continuation-lines)
143           'headers)))))
144
145 (defun nnfolder-open-server (server &optional defs)
146   (nnheader-change-server 'nnfolder server defs)
147   (when (not (file-exists-p nnfolder-directory))
148     (condition-case ()
149         (make-directory nnfolder-directory t)
150       (error t)))
151   (cond 
152    ((not (file-exists-p nnfolder-directory))
153     (nnfolder-close-server)
154     (nnheader-report 'nnfolder "Couldn't create directory: %s"
155                      nnfolder-directory))
156    ((not (file-directory-p (file-truename nnfolder-directory)))
157     (nnfolder-close-server)
158     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
159    (t
160     (nnheader-report 'nnfolder "Opened server %s using directory %s"
161                      server nnfolder-directory)
162     t)))
163
164 (defun nnfolder-close-server (&optional server)
165   (setq nnfolder-current-server nil
166         nnfolder-group-alist nil)
167   t)
168
169 (defun nnfolder-server-opened (&optional server)
170   (and (equal server nnfolder-current-server)
171        nntp-server-buffer
172        (buffer-name nntp-server-buffer)))
173
174 (defun nnfolder-request-close ()
175   (let ((alist nnfolder-buffer-alist))
176     (while alist
177       (nnfolder-close-group (caar alist) nil t)
178       (setq alist (cdr alist))))
179   (setq nnfolder-buffer-alist nil
180         nnfolder-server-alist nil
181         nnfolder-current-server nil
182         nnfolder-group-alist nil))
183
184 (defun nnfolder-status-message (&optional server)
185   (nnfolder-possibly-change-group nil server)
186   nnfolder-status-string)
187
188 (defun nnfolder-request-article (article &optional newsgroup server buffer)
189   (nnfolder-possibly-change-group newsgroup server)
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     (if (not (assoc group nnfolder-group-alist))
225         (nnheader-report 'nnfolder "No such group: %s" group)
226       (nnfolder-possibly-change-group group server)
227       (if dont-check
228           (progn 
229             (nnheader-report 'nnfolder "Selected group %s" group)
230             t)
231         (let* ((active (assoc group nnfolder-group-alist))
232                (group (car active))
233                (range (cadr active))
234                (minactive (car range))
235                (maxactive (cdr range)))
236           (cond 
237            ((null active)
238             (nnheader-report 'nnfolder "No such group: %s" group))
239            (t
240             (nnheader-report 'nnfolder "Selected group %s" group)
241             (nnheader-insert "211 %d %d %d %s\n" 
242                              (1+ (- maxactive minactive))
243                              minactive maxactive group))))))))
244
245 (defun nnfolder-request-scan (&optional group server)
246   (nnfolder-possibly-change-group group server)
247   (nnmail-get-new-mail
248    'nnfolder 
249    (lambda ()
250      (let ((bufs nnfolder-buffer-alist))
251        (save-excursion
252          (while bufs
253            (if (not (buffer-name (nth 1 (car bufs))))
254                (setq nnfolder-buffer-alist 
255                      (delq (car bufs) nnfolder-buffer-alist))
256              (set-buffer (nth 1 (car bufs)))
257              (and (buffer-modified-p) (save-buffer))
258              (kill-buffer (current-buffer)))
259            (setq bufs (cdr bufs))))))
260    nnfolder-directory
261    group))
262
263 ;; Don't close the buffer if we're not shutting down the server.  This way,
264 ;; we can keep the buffer in the group buffer cache, and not have to grovel
265 ;; over the buffer again unless we add new mail to it or modify it in some
266 ;; way.
267
268 (defun nnfolder-close-group (group &optional server force)
269   ;; Make sure we _had_ the group open.
270   (when (or (assoc group nnfolder-buffer-alist)
271             (equal group nnfolder-current-group))
272     (nnfolder-possibly-change-group group server)
273     (save-excursion
274       (set-buffer nnfolder-current-buffer)
275       ;; If the buffer was modified, write the file out now.
276       (and (buffer-modified-p) (save-buffer))
277       ;; If we're shutting the server down, we need to kill the
278       ;; buffer and remove it from the open buffer list.  Or, of
279       ;; course, if we're trying to minimize our space impact.
280       (kill-buffer (current-buffer))
281       (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
282                                         nnfolder-buffer-alist))))
283   (setq nnfolder-current-group nil
284         nnfolder-current-buffer nil)
285   t)
286
287 (defun nnfolder-request-create-group (group &optional server) 
288   (nnfolder-possibly-change-group nil server)
289   (nnmail-activate 'nnfolder)
290   (when group 
291     (unless (assoc group nnfolder-group-alist)
292       (push (list group (cons 1 0)) nnfolder-group-alist)
293       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
294   t)
295
296 (defun nnfolder-request-list (&optional server)
297   (nnfolder-possibly-change-group nil server)
298   (save-excursion
299     (nnmail-find-file nnfolder-active-file)
300     (setq nnfolder-group-alist (nnmail-get-active))))
301
302 (defun nnfolder-request-newgroups (date &optional server)
303   (nnfolder-possibly-change-group nil server)
304   (nnfolder-request-list server))
305
306 (defun nnfolder-request-list-newsgroups (&optional server)
307   (nnfolder-possibly-change-group nil server)
308   (save-excursion
309     (nnmail-find-file nnfolder-newsgroups-file)))
310
311 (defun nnfolder-request-expire-articles 
312   (articles newsgroup &optional server force)
313   (nnfolder-possibly-change-group newsgroup server)
314   (let* ((is-old t)
315          rest)
316     (nnmail-activate 'nnfolder)
317
318     (save-excursion 
319       (set-buffer nnfolder-current-buffer)
320       (while (and articles is-old)
321         (goto-char (point-min))
322         (if (search-forward (nnfolder-article-string (car articles)) nil t)
323             (if (setq is-old
324                       (nnmail-expired-article-p 
325                        newsgroup
326                        (buffer-substring 
327                         (point) (progn (end-of-line) (point))) 
328                        force nnfolder-inhibit-expiry))
329                 (progn
330                   (nnheader-message 5 "Deleting article %d..." 
331                                     (car articles) newsgroup)
332                   (nnfolder-delete-mail))
333               (setq rest (cons (car articles) rest))))
334         (setq articles (cdr articles)))
335       (and (buffer-modified-p) (save-buffer))
336       ;; Find the lowest active article in this group.
337       (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
338              (marker (concat "\n" nnfolder-article-marker))
339              (number "[0-9]+")
340              (activemin (cdr active)))
341         (goto-char (point-min))
342         (while (and (search-forward marker nil t)
343                     (re-search-forward number nil t))
344           (setq activemin (min activemin
345                                (string-to-number (buffer-substring
346                                                   (match-beginning 0)
347                                                   (match-end 0))))))
348         (setcar active activemin))
349       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
350       (nconc rest articles))))
351
352 (defun nnfolder-request-move-article
353   (article group server accept-form &optional last)
354   (nnfolder-possibly-change-group group server)
355   (let ((buf (get-buffer-create " *nnfolder move*"))
356         result)
357     (and 
358      (nnfolder-request-article article group server)
359      (save-excursion
360        (set-buffer buf)
361        (buffer-disable-undo (current-buffer))
362        (erase-buffer)
363        (insert-buffer-substring nntp-server-buffer)
364        (goto-char (point-min))
365        (while (re-search-forward 
366                (concat "^" nnfolder-article-marker)
367                (save-excursion (search-forward "\n\n" nil t) (point)) t)
368          (delete-region (progn (beginning-of-line) (point))
369                         (progn (forward-line 1) (point))))
370        (setq result (eval accept-form))
371        (kill-buffer buf)
372        result)
373      (save-excursion
374        (nnfolder-possibly-change-group group server)
375        (set-buffer nnfolder-current-buffer)
376        (goto-char (point-min))
377        (if (search-forward (nnfolder-article-string article) nil t)
378            (nnfolder-delete-mail))
379        (and last 
380             (buffer-modified-p)
381             (save-buffer))))
382     result))
383
384 (defun nnfolder-request-accept-article (group &optional last)
385   (and (stringp group) (nnfolder-possibly-change-group group))
386   (let ((buf (current-buffer))
387         result)
388     (goto-char (point-min))
389     (when (looking-at "X-From-Line: ")
390       (replace-match "From "))
391     (and 
392      (nnfolder-request-list)
393      (save-excursion
394        (set-buffer buf)
395        (goto-char (point-min))
396        (search-forward "\n\n" nil t)
397        (forward-line -1)
398        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
399          (delete-region (point) (progn (forward-line 1) (point))))
400        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
401      (save-excursion
402        (set-buffer nnfolder-current-buffer)
403        (and last (buffer-modified-p) (save-buffer))))
404     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
405     (unless result
406       (nnheader-report 'nnfolder "Couldn't store article"))
407     result))
408
409 (defun nnfolder-request-replace-article (article group buffer)
410   (nnfolder-possibly-change-group group)
411   (save-excursion
412     (set-buffer nnfolder-current-buffer)
413     (goto-char (point-min))
414     (if (not (search-forward (nnfolder-article-string article) nil t))
415         nil
416       (nnfolder-delete-mail t t)
417       (insert-buffer-substring buffer)
418       (and (buffer-modified-p) (save-buffer))
419       t)))
420
421 (defun nnfolder-request-delete-group (group &optional force server)
422   (nnfolder-close-group group server t)
423   ;; Delete all articles in GROUP.
424   (if (not force)
425       ()                                ; Don't delete the articles.
426     ;; Delete the file that holds the group.
427     (condition-case nil
428         (delete-file (directory-file-name 
429                       (nnmail-group-pathname group nnfolder-directory)))
430       (error nil)))
431   ;; Remove the group from all structures.
432   (setq nnfolder-group-alist 
433         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
434         nnfolder-current-group nil
435         nnfolder-current-buffer nil)
436   ;; Save the active file.
437   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
438   t)
439
440 (defun nnfolder-request-rename-group (group new-name &optional server)
441   (nnfolder-possibly-change-group group server)
442   (save-excursion
443     (set-buffer nnfolder-current-buffer)
444     (and (file-writable-p buffer-file-name)
445          (condition-case ()
446              (progn
447                (rename-file 
448                 buffer-file-name
449                 (directory-file-name
450                  (nnmail-group-pathname new-name nnfolder-directory)))
451                t)
452            (error nil))
453          ;; That went ok, so we change the internal structures.
454          (let ((entry (assoc group nnfolder-group-alist)))
455            (and entry (setcar entry new-name))
456            (setq nnfolder-current-buffer nil
457                  nnfolder-current-group nil)
458            ;; Save the new group alist.
459            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
460            ;; We kill the buffer instead of renaming it and stuff.
461            (kill-buffer (current-buffer))
462            t))))
463
464 \f
465 ;;; Internal functions.
466
467 (defun nnfolder-article-string (article)
468   (if (numberp article)
469       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
470     (concat "\nMessage-ID: " article)))
471
472 (defun nnfolder-delete-mail (&optional force leave-delim)
473   ;; Beginning of the article.
474   (save-excursion
475     (save-restriction
476       (narrow-to-region
477        (save-excursion
478          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
479          (if leave-delim (progn (forward-line 1) (point))
480            (match-beginning 0)))
481        (progn
482          (forward-line 1)
483          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
484                                      nil t)
485                   (if (and (not (bobp)) leave-delim)
486                       (progn (forward-line -2) (point))
487                     (match-beginning 0)))
488              (point-max))))
489       (delete-region (point-min) (point-max)))))
490
491 (defun nnfolder-possibly-change-group (group &optional server)
492   (when (and server
493              (not (nnfolder-server-opened server)))
494     (nnfolder-open-server server))
495   (when group
496     (unless (file-exists-p nnfolder-directory)
497       (make-directory (directory-file-name nnfolder-directory) t))
498     (nnfolder-possibly-activate-groups nil)
499     (or (assoc group nnfolder-group-alist)
500         (not (file-exists-p
501               (directory-file-name
502                (nnmail-group-pathname group nnfolder-directory))))
503         (progn
504           (setq nnfolder-group-alist 
505                 (cons (list group (cons 1 0)) nnfolder-group-alist))
506           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
507     (let (inf file)
508       (if (and (equal group nnfolder-current-group)
509                nnfolder-current-buffer
510                (buffer-name nnfolder-current-buffer))
511           ()
512         (setq nnfolder-current-group group)
513
514         ;; If we have to change groups, see if we don't already have the mbox
515         ;; in memory.  If we do, verify the modtime and destroy the mbox if
516         ;; needed so we can rescan it.
517         (if (setq inf (assoc group nnfolder-buffer-alist))
518             (setq nnfolder-current-buffer (nth 1 inf)))
519
520         ;; If the buffer is not live, make sure it isn't in the alist.  If it
521         ;; is live, verify that nobody else has touched the file since last
522         ;; time.
523         (if (or (not (and nnfolder-current-buffer
524                           (buffer-name nnfolder-current-buffer)))
525                 (not (and (bufferp nnfolder-current-buffer)
526                           (verify-visited-file-modtime 
527                            nnfolder-current-buffer))))
528             (progn
529               (if (and nnfolder-current-buffer
530                        (buffer-name nnfolder-current-buffer)
531                        (bufferp nnfolder-current-buffer))
532                   (kill-buffer nnfolder-current-buffer))
533               (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
534               (setq inf nil)))
535       
536         (if inf
537             ()
538           (save-excursion
539             (setq file (directory-file-name
540                         (nnmail-group-pathname group nnfolder-directory)))
541             (if (file-directory-p (file-truename file))
542                 ()
543               (unless (file-exists-p file)
544                 (unless (file-exists-p (file-name-directory file))
545                   (make-directory (file-name-directory file) t))
546                 (write-region 1 1 file t 'nomesg))
547               (setq nnfolder-current-buffer 
548                     (set-buffer (nnfolder-read-folder file)))
549               (setq nnfolder-buffer-alist (cons (list group (current-buffer))
550                                                 nnfolder-buffer-alist)))))))
551     (setq nnfolder-current-group group)))
552
553 (defun nnfolder-save-mail (&optional group)
554   "Called narrowed to an article."
555   (let* ((nnmail-split-methods 
556           (if group (list (list group "")) nnmail-split-methods))
557          (group-art-list
558           (nreverse (nnmail-article-group 'nnfolder-active-number)))
559          (delim (concat "^" rmail-unix-mail-delimiter))
560          save-list group-art)
561     (goto-char (point-min))
562     ;; This might come from somewhere else.
563     (unless (looking-at delim)
564       (insert "From nobody " (current-time-string) "\n")
565       (goto-char (point-min)))
566     ;; Quote all "From " lines in the article.
567     (forward-line 1)
568     (while (re-search-forward delim nil t)
569       (beginning-of-line)
570       (insert "> "))
571     (setq save-list group-art-list)
572     (nnmail-insert-lines)
573     (nnmail-insert-xref group-art-list)
574     (run-hooks 'nnfolder-prepare-save-mail-hook)
575
576     ;; Insert the mail into each of the destination groups.
577     (while group-art-list
578       (setq group-art (car group-art-list)
579             group-art-list (cdr group-art-list))
580
581       ;; Kill the previous newsgroup markers.
582       (goto-char (point-min))
583       (search-forward "\n\n" nil t)
584       (forward-line -1)
585       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
586         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
587
588       (nnfolder-possibly-change-group (car group-art))
589       ;; Insert the new newsgroup marker.
590       (nnfolder-insert-newsgroup-line group-art)
591       (unless nnfolder-current-buffer
592         (nnfolder-request-create-group (car group-art))
593         (nnfolder-possibly-change-group (car group-art)))
594       (let ((beg (point-min))
595             (end (point-max))
596             (obuf (current-buffer)))
597         (set-buffer nnfolder-current-buffer)
598         (goto-char (point-max))
599         (insert-buffer-substring obuf beg end)
600         (set-buffer obuf)))
601
602     ;; Did we save it anywhere?
603     save-list))
604
605 (defun nnfolder-insert-newsgroup-line (group-art)
606   (save-excursion
607     (goto-char (point-min))
608     (if (search-forward "\n\n" nil t)
609         (progn
610           (forward-char -1)
611           (insert (format (concat nnfolder-article-marker "%d   %s\n")
612                           (cdr group-art) (current-time-string)))))))
613
614 (defun nnfolder-possibly-activate-groups (&optional group)
615   (save-excursion
616     ;; If we're looking for the activation of a specific group, find out
617     ;; its real name and switch to it.
618     (if group (nnfolder-possibly-change-group group))
619     ;; If the group alist isn't active, activate it now.
620     (nnmail-activate 'nnfolder)))
621
622 (defun nnfolder-active-number (group)
623   (save-excursion 
624     ;; Find the next article number in GROUP.
625     (prog1
626         (let ((active (cadr (assoc group nnfolder-group-alist))))
627           (if active
628               (setcdr active (1+ (cdr active)))
629             ;; This group is new, so we create a new entry for it.
630             ;; This might be a bit naughty... creating groups on the drop of
631             ;; a hat, but I don't know...
632             (setq nnfolder-group-alist 
633                   (cons (list group (setq active (cons 1 1)))
634                         nnfolder-group-alist)))
635           (cdr active))
636       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
637       (nnfolder-possibly-activate-groups group))))
638
639
640 ;; This method has a problem if you've accidentally let the active list get
641 ;; out of sync with the files.  This could happen, say, if you've
642 ;; accidentally gotten new mail with something other than Gnus (but why
643 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
644 ;; processing the file, ready to add new X-Gnus article number markers, and
645 ;; we'll run across a message with no ID yet - the active list _may_not_ be
646 ;; ready for us yet.
647
648 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
649 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
650 ;; rest of the message looking for any more, possibly higher IDs.  We'll
651 ;; assume the maximum that we find is the highest active.  Note that this
652 ;; shouldn't cost us much extra time at all, but will be a lot less
653 ;; vulnerable to glitches between the mbox and the active file.
654
655 (defun nnfolder-read-folder (file)
656   (save-excursion
657     (nnfolder-possibly-activate-groups nil)
658     ;; We should be paranoid here and make sure the group is in the alist,
659     ;; and add it if it isn't.
660     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
661     (set-buffer (setq nnfolder-current-buffer 
662                       (nnheader-find-file-noselect file nil 'raw)))
663     (buffer-disable-undo (current-buffer))
664     (let* ((delim (concat "^" rmail-unix-mail-delimiter))
665            (marker (concat "\n" nnfolder-article-marker))
666            (number "[0-9]+")
667            (active (cadr (assoc nnfolder-current-group 
668                                 nnfolder-group-alist)))
669            ;; Set min to Big Number.
670            (min (max (1- (lsh 1 23)) (1- (lsh 1 24)) (1- (lsh 1 25)))) 
671            (max (cdr active))
672            start end)
673       (goto-char (point-min))
674
675       ;; Anytime the active number is 1 or 0, it is suspect.  In that case,
676       ;; search the file manually to find the active number.  Or, of course,
677       ;; if we're being paranoid.  (This would also be the place to build
678       ;; other lists from the header markers, such as expunge lists, etc., if
679       ;; we ever desired to abandon the active file entirely for mboxes.)
680       (when (or nnfolder-ignore-active-file
681                 (< max 2))
682         (while (and (search-forward marker nil t)
683                     (re-search-forward number nil t))
684           (let ((newnum (string-to-number (match-string 0))))
685             (setq max (max max newnum))
686             (setq min (min min newnum))))
687         (setcar active (max 1 (min min max)))
688         (setcdr active (max max (cdr active)))
689         (goto-char (point-min)))
690
691       ;; Keep track of the active number on our own, and insert it back into
692       ;; the active list when we're done. Also, prime the pump to cut down on
693       ;; the number of searches we do.
694       (setq end (point-marker))
695       (set-marker end (or (and (re-search-forward delim nil t)
696                                (match-beginning 0))
697                           (point-max)))
698       (while (not (= end (point-max)))
699         (setq start (marker-position end))
700         (goto-char end)
701         ;; There may be more than one "From " line, so we skip past
702         ;; them.  
703         (while (looking-at delim) 
704           (forward-line 1))
705         (set-marker end (or (and (re-search-forward delim nil t)
706                                  (match-beginning 0))
707                             (point-max)))
708         (goto-char start)
709         (if (not (search-forward marker end t))
710             (progn
711               (narrow-to-region start end)
712               (nnmail-insert-lines)
713               (nnfolder-insert-newsgroup-line
714                (cons nil (nnfolder-active-number nnfolder-current-group)))
715               (widen))))
716
717       ;; Make absolutely sure that the active list reflects reality!
718       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
719       (current-buffer))))
720
721 ;;;###autoload
722 (defun nnfolder-generate-active-file ()
723   "Look for mbox folders in the nnfolder directory and make them into groups."
724   (interactive)
725   (nnmail-activate 'nnfolder)
726   (let ((files (directory-files nnfolder-directory))
727         file)
728     (while (setq file (pop files))
729       (when (nnheader-mail-file-mbox-p file)
730         (nnheader-message 5 "Adding group %s..." file)
731         (push (list file (cons 1 0)) nnfolder-group-alist)
732         (nnfolder-read-folder file)
733         (nnfolder-close-group file))
734       (message ""))))
735
736 (provide 'nnfolder)
737
738 ;;; nnfolder.el ends here