*** 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   (when (stringp group)
374     (nnfolder-possibly-change-group group))
375   (let ((buf (current-buffer))
376         result)
377     (goto-char (point-min))
378     (when (looking-at "X-From-Line: ")
379       (replace-match "From "))
380     (and 
381      (nnfolder-request-list)
382      (save-excursion
383        (set-buffer buf)
384        (goto-char (point-min))
385        (search-forward "\n\n" nil t)
386        (forward-line -1)
387        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
388          (delete-region (point) (progn (forward-line 1) (point))))
389        (setq result
390              (car (nnfolder-save-mail
391                    (if (stringp group)
392                        (list (cons group (nnfolder-active-number group)))
393                      (nnmail-article-group 'nnfolder-active-number))))))
394      (save-excursion
395        (set-buffer nnfolder-current-buffer)
396        (and last (nnfolder-save-buffer))))
397     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
398     (unless result
399       (nnheader-report 'nnfolder "Couldn't store article"))
400     result))
401
402 (deffoo nnfolder-request-replace-article (article group buffer)
403   (nnfolder-possibly-change-group group)
404   (save-excursion
405     (set-buffer nnfolder-current-buffer)
406     (goto-char (point-min))
407     (if (not (search-forward (nnfolder-article-string article) nil t))
408         nil
409       (nnfolder-delete-mail t t)
410       (insert-buffer-substring buffer)
411       (nnfolder-save-buffer)
412       t)))
413
414 (deffoo nnfolder-request-delete-group (group &optional force server)
415   (nnfolder-close-group group server t)
416   ;; Delete all articles in GROUP.
417   (if (not force)
418       ()                                ; Don't delete the articles.
419     ;; Delete the file that holds the group.
420     (ignore-errors
421       (delete-file (nnfolder-group-pathname group))))
422   ;; Remove the group from all structures.
423   (setq nnfolder-group-alist 
424         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
425         nnfolder-current-group nil
426         nnfolder-current-buffer nil)
427   ;; Save the active file.
428   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
429   t)
430
431 (deffoo nnfolder-request-rename-group (group new-name &optional server)
432   (nnfolder-possibly-change-group group server)
433   (save-excursion
434     (set-buffer nnfolder-current-buffer)
435     (and (file-writable-p buffer-file-name)
436          (ignore-errors
437            (rename-file 
438             buffer-file-name
439             (nnfolder-group-pathname new-name))
440            t)
441          ;; That went ok, so we change the internal structures.
442          (let ((entry (assoc group nnfolder-group-alist)))
443            (and entry (setcar entry new-name))
444            (setq nnfolder-current-buffer nil
445                  nnfolder-current-group nil)
446            ;; Save the new group alist.
447            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
448            ;; We kill the buffer instead of renaming it and stuff.
449            (kill-buffer (current-buffer))
450            t))))
451
452 \f
453 ;;; Internal functions.
454
455 (defun nnfolder-article-string (article)
456   (if (numberp article)
457       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
458     (concat "\nMessage-ID: " article)))
459
460 (defun nnfolder-delete-mail (&optional force leave-delim)
461   "Delete the message that point is in."
462   (save-excursion
463     (delete-region
464      (save-excursion
465        (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
466        (if leave-delim (progn (forward-line 1) (point))
467          (match-beginning 0)))
468      (progn
469        (forward-line 1)
470        (if (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
471            (if (and (not (bobp)) leave-delim)
472                (progn (forward-line -2) (point))
473              (match-beginning 0))
474          (point-max))))))
475
476 ;; When scanning, we're not looking t immediately switch into the group - if
477 ;; we know our information is up to date, don't even bother reading the file.
478 (defun nnfolder-possibly-change-group (group &optional server scanning)
479   (when (and server
480              (not (nnfolder-server-opened server)))
481     (nnfolder-open-server server))
482   (when (and group (or nnfolder-current-buffer
483                        (not (equal group nnfolder-current-group))))
484     (gnus-make-directory (directory-file-name nnfolder-directory))
485     (nnfolder-possibly-activate-groups nil)
486     (or (assoc group nnfolder-group-alist)
487         (not (file-exists-p
488               (nnfolder-group-pathname group)))
489         (progn
490           (push (list group (cons 1 0)) nnfolder-group-alist)
491           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
492     (let (inf file)
493       (if (and (equal group nnfolder-current-group)
494                nnfolder-current-buffer
495                (buffer-name nnfolder-current-buffer))
496           ()
497         (setq nnfolder-current-group group)
498
499         ;; If we have to change groups, see if we don't already have the mbox
500         ;; in memory.  If we do, verify the modtime and destroy the mbox if
501         ;; needed so we can rescan it.
502         (when (setq inf (assoc group nnfolder-buffer-alist))
503           (setq nnfolder-current-buffer (nth 1 inf)))
504
505         ;; If the buffer is not live, make sure it isn't in the alist.  If it
506         ;; is live, verify that nobody else has touched the file since last
507         ;; time.
508         (when (or (not (and nnfolder-current-buffer
509                             (buffer-name nnfolder-current-buffer)))
510                   (not (and (bufferp nnfolder-current-buffer)
511                             (verify-visited-file-modtime 
512                              nnfolder-current-buffer))))
513           (when (and nnfolder-current-buffer
514                      (buffer-name nnfolder-current-buffer)
515                      (bufferp nnfolder-current-buffer))
516             (kill-buffer nnfolder-current-buffer))
517           (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
518           (setq inf nil))
519       
520         (unless inf
521           (save-excursion
522             (setq file (nnfolder-group-pathname group))
523             (unless (file-directory-p (file-truename file))
524               (unless (file-exists-p file)
525                 (gnus-make-directory (file-name-directory file))
526                 (nnmail-write-region 1 1 file t 'nomesg))
527               (setq nnfolder-current-buffer
528                     (nnfolder-read-folder file scanning))
529               (when nnfolder-current-buffer 
530                 (set-buffer nnfolder-current-buffer)
531                 (push (list group nnfolder-current-buffer)
532                       nnfolder-buffer-alist)))))))
533     (setq nnfolder-current-group group)))
534
535 (defun nnfolder-save-mail (group-art-list)
536   "Called narrowed to an article."
537   (let* ((delim (concat "^" message-unix-mail-delimiter))
538          save-list group-art)
539     (goto-char (point-min))
540     ;; The From line may have been quoted by movemail.
541     (when (looking-at (concat ">" message-unix-mail-delimiter))
542       (delete-char 1))
543     ;; This might come from somewhere else.    
544     (unless (looking-at delim)
545       (insert "From nobody " (current-time-string) "\n")
546       (goto-char (point-min)))
547     ;; Quote all "From " lines in the article.
548     (forward-line 1)
549     (while (re-search-forward delim nil t)
550       (beginning-of-line)
551       (insert "> "))
552     (setq save-list group-art-list)
553     (nnmail-insert-lines)
554     (nnmail-insert-xref group-art-list)
555     (run-hooks 'nnmail-prepare-save-mail-hook)
556     (run-hooks 'nnfolder-prepare-save-mail-hook)
557
558     ;; Insert the mail into each of the destination groups.
559     (while group-art-list
560       (setq group-art (car group-art-list)
561             group-art-list (cdr group-art-list))
562
563       ;; Kill the previous newsgroup markers.
564       (goto-char (point-min))
565       (search-forward "\n\n" nil t)
566       (forward-line -1)
567       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
568         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
569
570       (nnfolder-possibly-change-group (car group-art))
571       ;; Insert the new newsgroup marker.
572       (nnfolder-insert-newsgroup-line group-art)
573       (unless nnfolder-current-buffer
574         (nnfolder-close-group (car group-art))
575         (nnfolder-request-create-group (car group-art))
576         (nnfolder-possibly-change-group (car group-art)))
577       (let ((beg (point-min))
578             (end (point-max))
579             (obuf (current-buffer)))
580         (set-buffer nnfolder-current-buffer)
581         (goto-char (point-max))
582         (unless (eolp)
583           (insert "\n"))
584         (unless (bobp)
585           (insert "\n"))
586         (insert-buffer-substring obuf beg end)
587         (set-buffer obuf)))
588
589     ;; Did we save it anywhere?
590     save-list))
591
592 (defun nnfolder-insert-newsgroup-line (group-art)
593   (save-excursion
594     (goto-char (point-min))
595     (when (search-forward "\n\n" nil t)
596       (forward-char -1)
597       (insert (format (concat nnfolder-article-marker "%d   %s\n")
598                       (cdr group-art) (current-time-string))))))
599
600 (defun nnfolder-possibly-activate-groups (&optional group)
601   (save-excursion
602     ;; If we're looking for the activation of a specific group, find out
603     ;; its real name and switch to it.
604     (when group
605       (nnfolder-possibly-change-group group))
606     ;; If the group alist isn't active, activate it now.
607     (nnmail-activate 'nnfolder)))
608
609 (defun nnfolder-active-number (group)
610   (when group
611     (save-excursion 
612       ;; Find the next article number in GROUP.
613       (prog1
614           (let ((active (cadr (assoc group nnfolder-group-alist))))
615             (if active
616                 (setcdr active (1+ (cdr active)))
617               ;; This group is new, so we create a new entry for it.
618               ;; This might be a bit naughty... creating groups on the drop of
619               ;; a hat, but I don't know...
620               (push (list group (setq active (cons 1 1)))
621                     nnfolder-group-alist))
622             (cdr active))
623         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
624         (nnfolder-possibly-activate-groups group)))))
625
626
627 ;; This method has a problem if you've accidentally let the active list get
628 ;; out of sync with the files.  This could happen, say, if you've
629 ;; accidentally gotten new mail with something other than Gnus (but why
630 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
631 ;; processing the file, ready to add new X-Gnus article number markers, and
632 ;; we'll run across a message with no ID yet - the active list _may_not_ be
633 ;; ready for us yet.
634
635 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
636 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
637 ;; rest of the message looking for any more, possibly higher IDs.  We'll
638 ;; assume the maximum that we find is the highest active.  Note that this
639 ;; shouldn't cost us much extra time at all, but will be a lot less
640 ;; vulnerable to glitches between the mbox and the active file.
641
642 (defun nnfolder-read-folder (file &optional scanning)
643   ;; This is an attempt at a serious shortcut - don't even read in the file
644   ;; if we know we've seen it since the last time it was touched.
645   (let ((scantime (cadr (assoc nnfolder-current-group 
646                                nnfolder-scantime-alist)))
647         (modtime (nth 5 (or (file-attributes file) '(nil nil nil nil nil)))))
648     (if (and scanning scantime
649              (eq (car scantime) (car modtime))
650              (eq (cdr scantime) (cadr modtime)))
651         nil
652       (save-excursion
653         (nnfolder-possibly-activate-groups nil)
654         ;; Read in the file.
655         (set-buffer (setq nnfolder-current-buffer 
656                           (nnheader-find-file-noselect file nil 'raw)))
657         (buffer-disable-undo (current-buffer))
658         ;; If the file hasn't been touched since the last time we scanned it,
659         ;; don't bother doing anything with it.
660         (let ((delim (concat "^" message-unix-mail-delimiter))
661               (marker (concat "\n" nnfolder-article-marker))
662               (number "[0-9]+")
663               (active (or (cadr (assoc nnfolder-current-group 
664                                        nnfolder-group-alist))
665                           (cons 1 0)))
666               (scantime (assoc nnfolder-current-group nnfolder-scantime-alist))
667               (minid (lsh -1 -1))
668               maxid start end newscantime)
669
670           (setq maxid (or (cdr active) 0))
671           (goto-char (point-min))
672
673           ;; Anytime the active number is 1 or 0, it is suspect.  In that
674           ;; case, search the file manually to find the active number.  Or,
675           ;; of course, if we're being paranoid.  (This would also be the
676           ;; place to build other lists from the header markers, such as
677           ;; expunge lists, etc., if we ever desired to abandon the active
678           ;; file entirely for mboxes.)
679           (when (or nnfolder-ignore-active-file
680                     (< maxid 2))
681             (while (and (search-forward marker nil t)
682                         (re-search-forward number nil t))
683               (let ((newnum (string-to-number (match-string 0))))
684                 (setq maxid (max maxid newnum))
685                 (setq minid (min minid newnum))))
686             (setcar active (max 1 (min minid maxid)))
687             (setcdr active (max maxid (cdr active)))
688             (goto-char (point-min)))
689
690           ;; As long as we trust that the user will only insert unmarked mail
691           ;; at the end, go to the end and search backwards for the last
692           ;; marker.  Find the start of that message, and begin to search for
693           ;; unmarked messages from there.
694           (when (not (or nnfolder-distrust-mbox
695                          (< maxid 2)))
696             (goto-char (point-max))
697             (if (not (re-search-backward marker nil t))
698                 (goto-char (point-min))
699               (when (not (re-search-backward delim nil t))
700                 (goto-char (point-min)))))
701
702           ;; Keep track of the active number on our own, and insert it back
703           ;; into the active list when we're done.  Also, prime the pump to
704           ;; cut down on the number of searches we do.
705           (setq end (point-marker))
706           (set-marker end (or (and (re-search-forward delim nil t)
707                                    (match-beginning 0))
708                               (point-max)))
709           (while (not (= end (point-max)))
710             (setq start (marker-position end))
711             (goto-char end)
712             ;; There may be more than one "From " line, so we skip past
713             ;; them.  
714             (while (looking-at delim)
715               (forward-line 1))
716             (set-marker end (or (and (re-search-forward delim nil t)
717                                      (match-beginning 0))
718                                 (point-max)))
719             (goto-char start)
720             (when (not (search-forward marker end t))
721               (narrow-to-region start end)
722               (nnmail-insert-lines)
723               (nnfolder-insert-newsgroup-line
724                (cons nil (nnfolder-active-number nnfolder-current-group)))
725               (widen)))
726
727           ;; Make absolutely sure that the active list reflects reality!
728           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
729           ;; Set the scantime for this group.
730           (setq newscantime (visited-file-modtime))
731           (if scantime
732               (setcdr scantime (list newscantime))
733             (push (list nnfolder-current-group newscantime)
734                   nnfolder-scantime-alist))
735           (current-buffer))))))
736
737 ;;;###autoload
738 (defun nnfolder-generate-active-file ()
739   "Look for mbox folders in the nnfolder directory and make them into groups."
740   (interactive)
741   (nnmail-activate 'nnfolder)
742   (let ((files (directory-files nnfolder-directory))
743         file)
744     (while (setq file (pop files))
745       (when (and (not (backup-file-name-p file))
746                  (nnheader-mail-file-mbox-p
747                   (concat nnfolder-directory file)))
748         (nnheader-message 5 "Adding group %s..." file)
749         (push (list file (cons 1 0)) nnfolder-group-alist)
750         (nnfolder-possibly-change-group file)
751         (nnfolder-close-group file))
752       (message ""))))
753
754 (defun nnfolder-group-pathname (group)
755   "Make pathname for GROUP."
756   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
757     ;; If this file exists, we use it directly.
758     (if (or nnmail-use-long-file-names 
759             (file-exists-p (concat dir group)))
760         (concat dir group)
761       ;; If not, we translate dots into slashes.
762       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
763
764 (defun nnfolder-save-buffer ()
765   "Save the buffer."
766   (when (buffer-modified-p)
767     (run-hooks 'nnfolder-save-buffer-hook)
768     (save-buffer)))
769
770 (provide 'nnfolder)
771
772 ;;; nnfolder.el ends here