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