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