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