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