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