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