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