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