e3b3305aa3c3c6081c96393bd5e4a14a24c4145e
[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             (if (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                 (progn
126                   (setq start (or (re-search-backward delim-string nil t)
127                                   (point)))
128                   (search-forward "\n\n" nil t)
129                   (setq stop (1- (point)))
130                   (set-buffer nntp-server-buffer)
131                   (insert (format "221 %d Article retrieved.\n" article))
132                   (insert-buffer-substring nnfolder-current-buffer start stop)
133                   (goto-char (point-max))
134                   (insert ".\n")))
135             (setq articles (cdr articles)))
136
137           (set-buffer nntp-server-buffer)
138           (nnheader-fold-continuation-lines)
139           'headers)))))
140
141 (deffoo nnfolder-open-server (server &optional defs)
142   (nnoo-change-server 'nnfolder server defs)
143   (when (not (file-exists-p nnfolder-directory))
144     (condition-case ()
145         (gnus-make-directory nnfolder-directory)
146       (error t)))
147   (cond 
148    ((not (file-exists-p nnfolder-directory))
149     (nnfolder-close-server)
150     (nnheader-report 'nnfolder "Couldn't create directory: %s"
151                      nnfolder-directory))
152    ((not (file-directory-p (file-truename nnfolder-directory)))
153     (nnfolder-close-server)
154     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
155    (t
156     (nnheader-report 'nnfolder "Opened server %s using directory %s"
157                      server nnfolder-directory)
158     t)))
159
160 (deffoo nnfolder-request-close ()
161   (let ((alist nnfolder-buffer-alist))
162     (while alist
163       (nnfolder-close-group (caar alist) nil t)
164       (setq alist (cdr alist))))
165   (nnoo-close-server 'nnfolder)
166   (setq nnfolder-buffer-alist nil
167         nnfolder-group-alist nil))
168
169 (deffoo nnfolder-request-article (article &optional group server buffer)
170   (nnfolder-possibly-change-group group server)
171   (save-excursion
172     (set-buffer nnfolder-current-buffer)
173     (goto-char (point-min))
174     (if (search-forward (nnfolder-article-string article) nil t)
175         (let (start stop)
176           (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
177           (setq start (point))
178           (forward-line 1)
179           (or (and (re-search-forward 
180                     (concat "^" message-unix-mail-delimiter) nil t)
181                    (forward-line -1))
182               (goto-char (point-max)))
183           (setq stop (point))
184           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
185             (set-buffer nntp-server-buffer)
186             (erase-buffer)
187             (insert-buffer-substring nnfolder-current-buffer start stop)
188             (goto-char (point-min))
189             (while (looking-at "From ")
190               (delete-char 5)
191               (insert "X-From-Line: ")
192               (forward-line 1))
193             (if (numberp article) 
194                 (cons nnfolder-current-group article)
195               (goto-char (point-min))
196               (search-forward (concat "\n" nnfolder-article-marker))
197               (cons nnfolder-current-group
198                     (string-to-int 
199                      (buffer-substring 
200                       (point) (progn (end-of-line) (point)))))))))))
201
202 (deffoo nnfolder-request-group (group &optional server dont-check)
203   (save-excursion
204     (nnmail-activate 'nnfolder)
205     (if (not (assoc group nnfolder-group-alist))
206         (nnheader-report 'nnfolder "No such group: %s" group)
207       (nnfolder-possibly-change-group group server)
208       (if dont-check
209           (progn 
210             (nnheader-report 'nnfolder "Selected group %s" group)
211             t)
212         (let* ((active (assoc group nnfolder-group-alist))
213                (group (car active))
214                (range (cadr active)))
215           (cond 
216            ((null active)
217             (nnheader-report 'nnfolder "No such group: %s" group))
218            ((null nnfolder-current-group)
219             (nnheader-report 'nnfolder "Empty group: %s" group))
220            (t
221             (nnheader-report 'nnfolder "Selected group %s" group)
222             (nnheader-insert "211 %d %d %d %s\n" 
223                              (1+ (- (cdr range) (car range)))
224                              (car range) (cdr range) group))))))))
225
226 (deffoo nnfolder-request-scan (&optional group server)
227   (nnfolder-possibly-change-group group server t)
228   (nnmail-get-new-mail
229    'nnfolder 
230    (lambda ()
231      (let ((bufs nnfolder-buffer-alist))
232        (save-excursion
233          (while bufs
234            (if (not (buffer-name (nth 1 (car bufs))))
235                (setq nnfolder-buffer-alist 
236                      (delq (car bufs) nnfolder-buffer-alist))
237              (set-buffer (nth 1 (car bufs)))
238              (nnfolder-save-buffer)
239              (kill-buffer (current-buffer)))
240            (setq bufs (cdr bufs))))))
241    nnfolder-directory
242    group))
243
244 ;; Don't close the buffer if we're not shutting down the server.  This way,
245 ;; we can keep the buffer in the group buffer cache, and not have to grovel
246 ;; over the buffer again unless we add new mail to it or modify it in some
247 ;; way.
248
249 (deffoo nnfolder-close-group (group &optional server force)
250   ;; Make sure we _had_ the group open.
251   (when (or (assoc group nnfolder-buffer-alist)
252             (equal group nnfolder-current-group))
253     (let ((inf (assoc group nnfolder-buffer-alist)))
254       (when inf
255         (when nnfolder-current-group
256           (push (list nnfolder-current-group nnfolder-current-buffer)
257                 nnfolder-buffer-alist))
258         (setq nnfolder-buffer-alist
259               (delq inf nnfolder-buffer-alist))
260         (setq nnfolder-current-buffer (cadr inf)
261               nnfolder-current-group (car inf))))
262     (when (and nnfolder-current-buffer
263                (buffer-name nnfolder-current-buffer))
264       (save-excursion
265         (set-buffer nnfolder-current-buffer)
266         ;; If the buffer was modified, write the file out now.
267         (nnfolder-save-buffer)
268         ;; If we're shutting the server down, we need to kill the
269         ;; buffer and remove it from the open buffer list.  Or, of
270         ;; course, if we're trying to minimize our space impact.
271         (kill-buffer (current-buffer))
272         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
273                                           nnfolder-buffer-alist)))))
274   (setq nnfolder-current-group nil
275         nnfolder-current-buffer nil)
276   t)
277
278 (deffoo nnfolder-request-create-group (group &optional server args) 
279   (nnfolder-possibly-change-group nil server)
280   (nnmail-activate 'nnfolder)
281   (when group 
282     (unless (assoc group nnfolder-group-alist)
283       (push (list group (cons 1 0)) nnfolder-group-alist)
284       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
285   t)
286
287 (deffoo nnfolder-request-list (&optional server)
288   (nnfolder-possibly-change-group nil server)
289   (save-excursion
290     (nnmail-find-file nnfolder-active-file)
291     (setq nnfolder-group-alist (nnmail-get-active))))
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         (if (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               (setq rest (cons (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        (if (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   (and (stringp group) (nnfolder-possibly-change-group group))
376   (let ((buf (current-buffer))
377         result)
378     (goto-char (point-min))
379     (when (looking-at "X-From-Line: ")
380       (replace-match "From "))
381     (and 
382      (nnfolder-request-list)
383      (save-excursion
384        (set-buffer buf)
385        (goto-char (point-min))
386        (search-forward "\n\n" nil t)
387        (forward-line -1)
388        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
389          (delete-region (point) (progn (forward-line 1) (point))))
390        (setq result
391              (car (nnfolder-save-mail
392                    (if (stringp group)
393                        (list (cons group (nnfolder-active-number group)))
394                      (nnmail-article-group 'nnfolder-active-number))))))
395      (save-excursion
396        (set-buffer nnfolder-current-buffer)
397        (and last (nnfolder-save-buffer))))
398     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
399     (unless result
400       (nnheader-report 'nnfolder "Couldn't store article"))
401     result))
402
403 (deffoo nnfolder-request-replace-article (article group buffer)
404   (nnfolder-possibly-change-group group)
405   (save-excursion
406     (set-buffer nnfolder-current-buffer)
407     (goto-char (point-min))
408     (if (not (search-forward (nnfolder-article-string article) nil t))
409         nil
410       (nnfolder-delete-mail t t)
411       (insert-buffer-substring buffer)
412       (nnfolder-save-buffer)
413       t)))
414
415 (deffoo nnfolder-request-delete-group (group &optional force server)
416   (nnfolder-close-group group server t)
417   ;; Delete all articles in GROUP.
418   (if (not force)
419       ()                                ; Don't delete the articles.
420     ;; Delete the file that holds the group.
421     (condition-case nil
422         (delete-file (nnfolder-group-pathname group))
423       (error nil)))
424   ;; Remove the group from all structures.
425   (setq nnfolder-group-alist 
426         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
427         nnfolder-current-group nil
428         nnfolder-current-buffer nil)
429   ;; Save the active file.
430   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
431   t)
432
433 (deffoo nnfolder-request-rename-group (group new-name &optional server)
434   (nnfolder-possibly-change-group group server)
435   (save-excursion
436     (set-buffer nnfolder-current-buffer)
437     (and (file-writable-p buffer-file-name)
438          (condition-case ()
439              (progn
440                (rename-file 
441                 buffer-file-name
442                 (nnfolder-group-pathname new-name))
443                t)
444            (error nil))
445          ;; That went ok, so we change the internal structures.
446          (let ((entry (assoc group nnfolder-group-alist)))
447            (and entry (setcar entry new-name))
448            (setq nnfolder-current-buffer nil
449                  nnfolder-current-group nil)
450            ;; Save the new group alist.
451            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
452            ;; We kill the buffer instead of renaming it and stuff.
453            (kill-buffer (current-buffer))
454            t))))
455
456 \f
457 ;;; Internal functions.
458
459 (defun nnfolder-article-string (article)
460   (if (numberp article)
461       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
462     (concat "\nMessage-ID: " article)))
463
464 (defun nnfolder-delete-mail (&optional force leave-delim)
465   "Delete the message that point is in."
466   (save-excursion
467     (delete-region
468      (save-excursion
469        (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
470        (if leave-delim (progn (forward-line 1) (point))
471          (match-beginning 0)))
472      (progn
473        (forward-line 1)
474        (if (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
475            (if (and (not (bobp)) leave-delim)
476                (progn (forward-line -2) (point))
477              (match-beginning 0))
478          (point-max))))))
479
480 ;; When scanning, we're not looking t immediately switch into the group - if
481 ;; we know our information is up to date, don't even bother reading the file.
482 (defun nnfolder-possibly-change-group (group &optional server scanning)
483   (when (and server
484              (not (nnfolder-server-opened server)))
485     (nnfolder-open-server server))
486   (when (and group (or nnfolder-current-buffer
487                        (not (equal group nnfolder-current-group))))
488     (gnus-make-directory (directory-file-name nnfolder-directory))
489     (nnfolder-possibly-activate-groups nil)
490     (or (assoc group nnfolder-group-alist)
491         (not (file-exists-p
492               (nnfolder-group-pathname group)))
493         (progn
494           (setq nnfolder-group-alist 
495                 (cons (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         (if (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         (if (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             (progn
519               (if (and nnfolder-current-buffer
520                        (buffer-name nnfolder-current-buffer)
521                        (bufferp nnfolder-current-buffer))
522                   (kill-buffer nnfolder-current-buffer))
523               (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
524               (setq inf nil)))
525       
526         (unless inf
527           (save-excursion
528             (setq file (nnfolder-group-pathname group))
529             (unless (file-directory-p (file-truename file))
530               (unless (file-exists-p file)
531                 (gnus-make-directory (file-name-directory file))
532                 (nnmail-write-region 1 1 file t 'nomesg))
533               (setq nnfolder-current-buffer
534                     (nnfolder-read-folder file scanning))
535               (when nnfolder-current-buffer 
536                 (set-buffer nnfolder-current-buffer)
537                 (setq nnfolder-buffer-alist 
538                       (cons (list group nnfolder-current-buffer)
539                             nnfolder-buffer-alist))))))))
540     (setq nnfolder-current-group group)))
541
542 (defun nnfolder-save-mail (group-art-list)
543   "Called narrowed to an article."
544   (let* ((delim (concat "^" message-unix-mail-delimiter))
545          save-list group-art)
546     (goto-char (point-min))
547     ;; The From line may have been quoted by movemail.
548     (when (looking-at (concat ">" message-unix-mail-delimiter))
549       (delete-char 1))
550     ;; This might come from somewhere else.    
551     (unless (looking-at delim)
552       (insert "From nobody " (current-time-string) "\n")
553       (goto-char (point-min)))
554     ;; Quote all "From " lines in the article.
555     (forward-line 1)
556     (while (re-search-forward delim nil t)
557       (beginning-of-line)
558       (insert "> "))
559     (setq save-list group-art-list)
560     (nnmail-insert-lines)
561     (nnmail-insert-xref group-art-list)
562     (run-hooks 'nnmail-prepare-save-mail-hook)
563     (run-hooks 'nnfolder-prepare-save-mail-hook)
564
565     ;; Insert the mail into each of the destination groups.
566     (while group-art-list
567       (setq group-art (car group-art-list)
568             group-art-list (cdr group-art-list))
569
570       ;; Kill the previous newsgroup markers.
571       (goto-char (point-min))
572       (search-forward "\n\n" nil t)
573       (forward-line -1)
574       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
575         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
576
577       (nnfolder-possibly-change-group (car group-art))
578       ;; Insert the new newsgroup marker.
579       (nnfolder-insert-newsgroup-line group-art)
580       (unless nnfolder-current-buffer
581         (nnfolder-close-group (car group-art))
582         (nnfolder-request-create-group (car group-art))
583         (nnfolder-possibly-change-group (car group-art)))
584       (let ((beg (point-min))
585             (end (point-max))
586             (obuf (current-buffer)))
587         (set-buffer nnfolder-current-buffer)
588         (goto-char (point-max))
589         (unless (eolp)
590           (insert "\n"))
591         (insert "\n")
592         (insert-buffer-substring obuf beg end)
593         (set-buffer obuf)))
594
595     ;; Did we save it anywhere?
596     save-list))
597
598 (defun nnfolder-insert-newsgroup-line (group-art)
599   (save-excursion
600     (goto-char (point-min))
601     (if (search-forward "\n\n" nil t)
602         (progn
603           (forward-char -1)
604           (insert (format (concat nnfolder-article-marker "%d   %s\n")
605                           (cdr group-art) (current-time-string)))))))
606
607 (defun nnfolder-possibly-activate-groups (&optional group)
608   (save-excursion
609     ;; If we're looking for the activation of a specific group, find out
610     ;; its real name and switch to it.
611     (if group (nnfolder-possibly-change-group group))
612     ;; If the group alist isn't active, activate it now.
613     (nnmail-activate 'nnfolder)))
614
615 (defun nnfolder-active-number (group)
616   (when group
617     (save-excursion 
618       ;; Find the next article number in GROUP.
619       (prog1
620           (let ((active (cadr (assoc group nnfolder-group-alist))))
621             (if active
622                 (setcdr active (1+ (cdr active)))
623               ;; This group is new, so we create a new entry for it.
624               ;; This might be a bit naughty... creating groups on the drop of
625               ;; a hat, but I don't know...
626               (setq nnfolder-group-alist 
627                     (cons (list group (setq active (cons 1 1)))
628                           nnfolder-group-alist)))
629             (cdr active))
630         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
631         (nnfolder-possibly-activate-groups group)))))
632
633
634 ;; This method has a problem if you've accidentally let the active list get
635 ;; out of sync with the files.  This could happen, say, if you've
636 ;; accidentally gotten new mail with something other than Gnus (but why
637 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
638 ;; processing the file, ready to add new X-Gnus article number markers, and
639 ;; we'll run across a message with no ID yet - the active list _may_not_ be
640 ;; ready for us yet.
641
642 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
643 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
644 ;; rest of the message looking for any more, possibly higher IDs.  We'll
645 ;; assume the maximum that we find is the highest active.  Note that this
646 ;; shouldn't cost us much extra time at all, but will be a lot less
647 ;; vulnerable to glitches between the mbox and the active file.
648
649 (defun nnfolder-read-folder (file &optional scanning)
650   ;; This is an attempt at a serious shortcut - don't even read in the file
651   ;; if we know we've seen it since the last time it was touched.
652   (let ((scantime (cadr (assoc nnfolder-current-group 
653                                nnfolder-scantime-alist)))
654         (modtime (nth 5 (or (file-attributes file) '(nil nil nil nil nil)))))
655     (if (and scanning scantime
656              (eq (car scantime) (car modtime))
657              (eq (cdr scantime) (cadr modtime)))
658         nil
659       (save-excursion
660         (nnfolder-possibly-activate-groups nil)
661         ;; Read in the file.
662         (set-buffer (setq nnfolder-current-buffer 
663                           (nnheader-find-file-noselect file nil 'raw)))
664         (buffer-disable-undo (current-buffer))
665         ;; If the file hasn't been touched since the last time we scanned it,
666         ;; don't bother doing anything with it.
667         (let ((delim (concat "^" message-unix-mail-delimiter))
668               (marker (concat "\n" nnfolder-article-marker))
669               (number "[0-9]+")
670               (active (or (cadr (assoc nnfolder-current-group 
671                                        nnfolder-group-alist))
672                           (cons 1 0)))
673               (scantime (assoc nnfolder-current-group nnfolder-scantime-alist))
674               (minid (lsh -1 -1))
675               maxid start end newscantime)
676
677           (setq maxid (or (cdr active) 0))
678           (goto-char (point-min))
679
680           ;; Anytime the active number is 1 or 0, it is suspect.  In that
681           ;; case, search the file manually to find the active number.  Or,
682           ;; of course, if we're being paranoid.  (This would also be the
683           ;; place to build other lists from the header markers, such as
684           ;; expunge lists, etc., if we ever desired to abandon the active
685           ;; file entirely for mboxes.)
686           (when (or nnfolder-ignore-active-file
687                     (< maxid 2))
688             (while (and (search-forward marker nil t)
689                         (re-search-forward number nil t))
690               (let ((newnum (string-to-number (match-string 0))))
691                 (setq maxid (max maxid newnum))
692                 (setq minid (min minid newnum))))
693             (setcar active (max 1 (min minid maxid)))
694             (setcdr active (max maxid (cdr active)))
695             (goto-char (point-min)))
696
697           ;; As long as we trust that the user will only insert unmarked mail
698           ;; at the end, go to the end and search backwards for the last
699           ;; marker.  Find the start of that message, and begin to search for
700           ;; unmarked messages from there.
701           (if (not (or nnfolder-distrust-mbox
702                        (< maxid 2)))
703               (progn
704                 (goto-char (point-max))
705                 (if (not (re-search-backward marker nil t))
706                     (goto-char (point-min))
707                   (if (not (re-search-backward delim nil t))
708                       (goto-char (point-min))))))
709
710           ;; Keep track of the active number on our own, and insert it back
711           ;; into the active list when we're done.  Also, prime the pump to
712           ;; cut down on the number of searches we do.
713           (setq end (point-marker))
714           (set-marker end (or (and (re-search-forward delim nil t)
715                                    (match-beginning 0))
716                               (point-max)))
717           (while (not (= end (point-max)))
718             (setq start (marker-position end))
719             (goto-char end)
720             ;; There may be more than one "From " line, so we skip past
721             ;; them.  
722             (while (looking-at delim) 
723               (forward-line 1))
724             (set-marker end (or (and (re-search-forward delim nil t)
725                                      (match-beginning 0))
726                                 (point-max)))
727             (goto-char start)
728             (if (not (search-forward marker end t))
729                 (progn
730                   (narrow-to-region start end)
731                   (nnmail-insert-lines)
732                   (nnfolder-insert-newsgroup-line
733                    (cons nil (nnfolder-active-number nnfolder-current-group)))
734                   (widen))))
735
736           ;; Make absolutely sure that the active list reflects reality!
737           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
738           ;; Set the scantime for this group.
739           (setq newscantime (visited-file-modtime))
740           (if scantime
741               (setcdr scantime (list newscantime))
742             (push (list nnfolder-current-group newscantime) 
743                   nnfolder-scantime-alist))
744           (current-buffer))))))
745
746 ;;;###autoload
747 (defun nnfolder-generate-active-file ()
748   "Look for mbox folders in the nnfolder directory and make them into groups."
749   (interactive)
750   (nnmail-activate 'nnfolder)
751   (let ((files (directory-files nnfolder-directory))
752         file)
753     (while (setq file (pop files))
754       (when (and (not (backup-file-name-p file))
755                  (nnheader-mail-file-mbox-p
756                   (concat nnfolder-directory file)))
757         (nnheader-message 5 "Adding group %s..." file)
758         (push (list file (cons 1 0)) nnfolder-group-alist)
759         (nnfolder-possibly-change-group file)
760         (nnfolder-close-group file))
761       (message ""))))
762
763 (defun nnfolder-group-pathname (group)
764   "Make pathname for GROUP."
765   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
766     ;; If this file exists, we use it directly.
767     (if (or nnmail-use-long-file-names 
768             (file-exists-p (concat dir group)))
769         (concat dir group)
770       ;; If not, we translate dots into slashes.
771       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
772
773 (defun nnfolder-save-buffer ()
774   "Save the buffer."
775   (when (buffer-modified-p)
776     (run-hooks 'nnfolder-save-buffer-hook)
777     (save-buffer)))
778
779 (provide 'nnfolder)
780
781 ;;; nnfolder.el ends here