*** empty log message ***
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96,97 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 (defvoo nnfolder-active-timestamp nil)
93
94 \f
95
96 ;;; Interface functions
97
98 (nnoo-define-basics nnfolder)
99
100 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
101   (save-excursion
102     (set-buffer nntp-server-buffer)
103     (erase-buffer)
104     (let (article art-string start stop)
105       (nnfolder-possibly-change-group group server)
106       (when nnfolder-current-buffer
107         (set-buffer nnfolder-current-buffer)
108         (goto-char (point-min))
109         (if (stringp (car articles))
110             'headers
111           (while articles
112             (setq article (car articles))
113             (setq art-string (nnfolder-article-string article))
114             (set-buffer nnfolder-current-buffer)
115             (when (or (search-forward art-string nil t)
116                       ;; Don't search the whole file twice!  Also, articles
117                       ;; probably have some locality by number, so searching
118                       ;; backwards will be faster.  Especially if we're at the
119                       ;; beginning of the buffer :-). -SLB
120                       (search-backward art-string nil t))
121               (nnmail-search-unix-mail-delim-backward)
122               (setq start (point))
123               (search-forward "\n\n" nil t)
124               (setq stop (1- (point)))
125               (set-buffer nntp-server-buffer)
126               (insert (format "221 %d Article retrieved.\n" article))
127               (insert-buffer-substring nnfolder-current-buffer start stop)
128               (goto-char (point-max))
129               (insert ".\n"))
130             (setq articles (cdr articles)))
131
132           (set-buffer nntp-server-buffer)
133           (nnheader-fold-continuation-lines)
134           'headers)))))
135
136 (deffoo nnfolder-open-server (server &optional defs)
137   (nnoo-change-server 'nnfolder server defs)
138   (nnmail-activate 'nnfolder t)
139   (gnus-make-directory nnfolder-directory)
140   (cond
141    ((not (file-exists-p nnfolder-directory))
142     (nnfolder-close-server)
143     (nnheader-report 'nnfolder "Couldn't create directory: %s"
144                      nnfolder-directory))
145    ((not (file-directory-p (file-truename nnfolder-directory)))
146     (nnfolder-close-server)
147     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
148    (t
149     (nnmail-activate 'nnfolder)
150     (nnheader-report 'nnfolder "Opened server %s using directory %s"
151                      server nnfolder-directory)
152     t)))
153
154 (deffoo nnfolder-request-close ()
155   (let ((alist nnfolder-buffer-alist))
156     (while alist
157       (nnfolder-close-group (caar alist) nil t)
158       (setq alist (cdr alist))))
159   (nnoo-close-server 'nnfolder)
160   (setq nnfolder-buffer-alist nil
161         nnfolder-group-alist nil))
162
163 (deffoo nnfolder-request-article (article &optional group server buffer)
164   (nnfolder-possibly-change-group group server)
165   (save-excursion
166     (set-buffer nnfolder-current-buffer)
167     (goto-char (point-min))
168     (when (search-forward (nnfolder-article-string article) nil t)
169       (let (start stop)
170         (nnmail-search-unix-mail-delim-backward)
171         (setq start (point))
172         (forward-line 1)
173         (unless (and (nnmail-search-unix-mail-delim)
174                      (forward-line -1))
175           (goto-char (point-max)))
176         (setq stop (point))
177         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
178           (set-buffer nntp-server-buffer)
179           (erase-buffer)
180           (insert-buffer-substring nnfolder-current-buffer start stop)
181           (goto-char (point-min))
182           (while (looking-at "From ")
183             (delete-char 5)
184             (insert "X-From-Line: ")
185             (forward-line 1))
186           (if (numberp article)
187               (cons nnfolder-current-group article)
188             (goto-char (point-min))
189             (search-forward (concat "\n" nnfolder-article-marker))
190             (cons nnfolder-current-group
191                   (string-to-int
192                    (buffer-substring
193                     (point) (progn (end-of-line) (point)))))))))))
194
195 (deffoo nnfolder-request-group (group &optional server dont-check)
196   (nnfolder-possibly-change-group group server t)
197   (save-excursion
198     (if (not (assoc group nnfolder-group-alist))
199         (nnheader-report 'nnfolder "No such group: %s" group)
200       (if dont-check
201           (progn
202             (nnheader-report 'nnfolder "Selected group %s" group)
203             t)
204         (let* ((active (assoc group nnfolder-group-alist))
205                (group (car active))
206                (range (cadr active)))
207           (cond
208            ((null active)
209             (nnheader-report 'nnfolder "No such group: %s" group))
210            ((null nnfolder-current-group)
211             (nnheader-report 'nnfolder "Empty group: %s" group))
212            (t
213             (nnheader-report 'nnfolder "Selected group %s" group)
214             (nnheader-insert "211 %d %d %d %s\n"
215                              (1+ (- (cdr range) (car range)))
216                              (car range) (cdr range) group))))))))
217
218 (deffoo nnfolder-request-scan (&optional group server)
219   (nnfolder-possibly-change-group nil server)
220   (when nnfolder-get-new-mail
221     (nnfolder-possibly-change-group group server)
222     (nnmail-get-new-mail
223      'nnfolder
224      (lambda ()
225        (let ((bufs nnfolder-buffer-alist))
226          (save-excursion
227            (while bufs
228              (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
229                  (setq nnfolder-buffer-alist
230                        (delq (car bufs) nnfolder-buffer-alist))
231                (set-buffer (nth 1 (car bufs)))
232                (nnfolder-save-buffer)
233                (kill-buffer (current-buffer)))
234              (setq bufs (cdr bufs))))))
235      nnfolder-directory
236      group)))
237
238 ;; Don't close the buffer if we're not shutting down the server.  This way,
239 ;; we can keep the buffer in the group buffer cache, and not have to grovel
240 ;; over the buffer again unless we add new mail to it or modify it in some
241 ;; way.
242
243 (deffoo nnfolder-close-group (group &optional server force)
244   ;; Make sure we _had_ the group open.
245   (when (or (assoc group nnfolder-buffer-alist)
246             (equal group nnfolder-current-group))
247     (let ((inf (assoc group nnfolder-buffer-alist)))
248       (when inf
249         (when (and nnfolder-current-group
250                    nnfolder-current-buffer)
251           (push (list nnfolder-current-group nnfolder-current-buffer)
252                 nnfolder-buffer-alist))
253         (setq nnfolder-buffer-alist
254               (delq inf nnfolder-buffer-alist))
255         (setq nnfolder-current-buffer (cadr inf)
256               nnfolder-current-group (car inf))))
257     (when (and nnfolder-current-buffer
258                (buffer-name nnfolder-current-buffer))
259       (save-excursion
260         (set-buffer nnfolder-current-buffer)
261         ;; If the buffer was modified, write the file out now.
262         (nnfolder-save-buffer)
263         ;; If we're shutting the server down, we need to kill the
264         ;; buffer and remove it from the open buffer list.  Or, of
265         ;; course, if we're trying to minimize our space impact.
266         (kill-buffer (current-buffer))
267         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
268                                           nnfolder-buffer-alist)))))
269   (setq nnfolder-current-group nil
270         nnfolder-current-buffer nil)
271   t)
272
273 (deffoo nnfolder-request-create-group (group &optional server args)
274   (nnfolder-possibly-change-group nil server)
275   (nnmail-activate 'nnfolder)
276   (when group
277     (unless (assoc group nnfolder-group-alist)
278       (push (list group (cons 1 0)) nnfolder-group-alist)
279       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
280       (nnfolder-read-folder group)))
281   t)
282
283 (deffoo nnfolder-request-list (&optional server)
284   (nnfolder-possibly-change-group nil server)
285   (save-excursion
286     (nnmail-find-file nnfolder-active-file)
287     (setq nnfolder-group-alist (nnmail-get-active))
288     t))
289
290 (deffoo nnfolder-request-newgroups (date &optional server)
291   (nnfolder-possibly-change-group nil server)
292   (nnfolder-request-list server))
293
294 (deffoo nnfolder-request-list-newsgroups (&optional server)
295   (nnfolder-possibly-change-group nil server)
296   (save-excursion
297     (nnmail-find-file nnfolder-newsgroups-file)))
298
299 (deffoo nnfolder-request-expire-articles
300   (articles newsgroup &optional server force)
301   (nnfolder-possibly-change-group newsgroup server)
302   (let* ((is-old t)
303          rest)
304     (nnmail-activate 'nnfolder)
305
306     (save-excursion
307       (set-buffer nnfolder-current-buffer)
308       (while (and articles is-old)
309         (goto-char (point-min))
310         (when (search-forward (nnfolder-article-string (car articles)) nil t)
311           (if (setq is-old
312                     (nnmail-expired-article-p
313                      newsgroup
314                      (buffer-substring
315                       (point) (progn (end-of-line) (point)))
316                      force nnfolder-inhibit-expiry))
317               (progn
318                 (nnheader-message 5 "Deleting article %d..."
319                                   (car articles) newsgroup)
320                 (nnfolder-delete-mail))
321             (push (car articles) rest)))
322         (setq articles (cdr articles)))
323       (unless nnfolder-inhibit-expiry
324         (nnheader-message 5 "Deleting articles...done"))
325       (nnfolder-save-buffer)
326       (nnfolder-adjust-min-active newsgroup)
327       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
328       (nconc rest articles))))
329
330 (deffoo nnfolder-request-move-article
331   (article group server accept-form &optional last)
332   (let ((buf (get-buffer-create " *nnfolder move*"))
333         result)
334     (and
335      (nnfolder-request-article article group server)
336      (save-excursion
337        (set-buffer buf)
338        (buffer-disable-undo (current-buffer))
339        (erase-buffer)
340        (insert-buffer-substring nntp-server-buffer)
341        (goto-char (point-min))
342        (while (re-search-forward
343                (concat "^" nnfolder-article-marker)
344                (save-excursion (search-forward "\n\n" nil t) (point)) t)
345          (delete-region (progn (beginning-of-line) (point))
346                         (progn (forward-line 1) (point))))
347        (setq result (eval accept-form))
348        (kill-buffer buf)
349        result)
350      (save-excursion
351        (nnfolder-possibly-change-group group server)
352        (set-buffer nnfolder-current-buffer)
353        (goto-char (point-min))
354        (when (search-forward (nnfolder-article-string article) nil t)
355          (nnfolder-delete-mail))
356        (when last
357          (nnfolder-save-buffer)
358          (nnfolder-adjust-min-active group)
359          (nnmail-save-active nnfolder-group-alist nnfolder-active-file))))
360     result))
361
362 (deffoo nnfolder-request-accept-article (group &optional server last)
363   (nnfolder-possibly-change-group group server)
364   (nnmail-check-syntax)
365   (let ((buf (current-buffer))
366         result art-group)
367     (goto-char (point-min))
368     (when (looking-at "X-From-Line: ")
369       (replace-match "From "))
370     (and
371      (nnfolder-request-list)
372      (save-excursion
373        (set-buffer buf)
374        (goto-char (point-min))
375        (search-forward "\n\n" nil t)
376        (forward-line -1)
377        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
378          (delete-region (point) (progn (forward-line 1) (point))))
379        (when nnmail-cache-accepted-message-ids
380          (nnmail-cache-insert (nnmail-fetch-field "message-id")))
381        (setq result (if (stringp group)
382                         (list (cons group (nnfolder-active-number group)))
383                       (setq art-group
384                             (nnmail-article-group 'nnfolder-active-number))))
385        (if (and (null result)
386                 (yes-or-no-p "Moved to `junk' group; delete article? "))
387            (setq result 'junk)
388          (setq result
389                (car (nnfolder-save-mail result)))))
390      (when last
391        (save-excursion
392          (nnfolder-possibly-change-folder (or (caar art-group) group))
393          (nnfolder-save-buffer)
394          (when nnmail-cache-accepted-message-ids
395            (nnmail-cache-close)))))
396     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
397     (unless result
398       (nnheader-report 'nnfolder "Couldn't store article"))
399     result))
400
401 (deffoo nnfolder-request-replace-article (article group buffer)
402   (nnfolder-possibly-change-group group)
403   (save-excursion
404     (set-buffer nnfolder-current-buffer)
405     (goto-char (point-min))
406     (if (not (search-forward (nnfolder-article-string article) nil t))
407         nil
408       (nnfolder-delete-mail t t)
409       (insert-buffer-substring buffer)
410       (nnfolder-save-buffer)
411       t)))
412
413 (deffoo nnfolder-request-delete-group (group &optional force server)
414   (nnfolder-close-group group server t)
415   ;; Delete all articles in GROUP.
416   (if (not force)
417       ()                                ; Don't delete the articles.
418     ;; Delete the file that holds the group.
419     (ignore-errors
420       (delete-file (nnfolder-group-pathname group))))
421   ;; Remove the group from all structures.
422   (setq nnfolder-group-alist
423         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
424         nnfolder-current-group nil
425         nnfolder-current-buffer nil)
426   ;; Save the active file.
427   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
428   t)
429
430 (deffoo nnfolder-request-rename-group (group new-name &optional server)
431   (nnfolder-possibly-change-group group server)
432   (save-excursion
433     (set-buffer nnfolder-current-buffer)
434     (and (file-writable-p buffer-file-name)
435          (ignore-errors
436            (rename-file
437             buffer-file-name
438             (nnfolder-group-pathname new-name))
439            t)
440          ;; That went ok, so we change the internal structures.
441          (let ((entry (assoc group nnfolder-group-alist)))
442            (and entry (setcar entry new-name))
443            (setq nnfolder-current-buffer nil
444                  nnfolder-current-group nil)
445            ;; Save the new group alist.
446            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
447            ;; We kill the buffer instead of renaming it and stuff.
448            (kill-buffer (current-buffer))
449            t))))
450
451 \f
452 ;;; Internal functions.
453
454 (defun nnfolder-adjust-min-active (group)
455   ;; Find the lowest active article in this group.
456   (let* ((active (cadr (assoc group nnfolder-group-alist)))
457          (marker (concat "\n" nnfolder-article-marker))
458          (number "[0-9]+")
459          (activemin (cdr active)))
460     (save-excursion
461       (set-buffer nnfolder-current-buffer)
462       (goto-char (point-min))
463       (while (and (search-forward marker nil t)
464                   (re-search-forward number nil t))
465         (setq activemin (min activemin
466                              (string-to-number (buffer-substring
467                                                 (match-beginning 0)
468                                                 (match-end 0))))))
469       (setcar active activemin))))
470
471 (defun nnfolder-article-string (article)
472   (if (numberp article)
473       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
474     (concat "\nMessage-ID: " article)))
475
476 (defun nnfolder-delete-mail (&optional force leave-delim)
477   "Delete the message that point is in."
478   (save-excursion
479     (delete-region
480      (save-excursion
481        (nnmail-search-unix-mail-delim-backward)
482        (if leave-delim (progn (forward-line 1) (point))
483          (point)))
484      (progn
485        (forward-line 1)
486        (if (nnmail-search-unix-mail-delim)
487            (if (and (not (bobp)) leave-delim)
488                (progn (forward-line -2) (point))
489              (point))
490          (point-max))))))
491
492 (defun nnfolder-possibly-change-group (group &optional server dont-check)
493   ;; Change servers.
494   (when (and server
495              (not (nnfolder-server-opened server)))
496     (nnfolder-open-server server))
497   (unless (gnus-buffer-live-p nnfolder-current-buffer)
498     (setq nnfolder-current-buffer nil
499           nnfolder-current-group nil))
500   ;; Change group.
501   (when (and group
502              (not (equal group nnfolder-current-group)))
503     (nnmail-activate 'nnfolder)
504     (when (and (not (assoc group nnfolder-group-alist))
505                (not (file-exists-p
506                      (nnfolder-group-pathname group))))
507       ;; The group doesn't exist, so we create a new entry for it.
508       (push (list group (cons 1 0)) nnfolder-group-alist)
509       (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
510
511     (if dont-check
512         (setq nnfolder-current-group group
513               nnfolder-current-buffer nil)
514       (let (inf file)
515         ;; If we have to change groups, see if we don't already have the
516         ;; folder in memory.  If we do, verify the modtime and destroy
517         ;; the folder if needed so we can rescan it.
518         (setq nnfolder-current-buffer
519               (nth 1 (assoc group nnfolder-buffer-alist)))
520
521         ;; If the buffer is not live, make sure it isn't in the alist.  If it
522         ;; is live, verify that nobody else has touched the file since last
523         ;; time.
524         (when (and nnfolder-current-buffer
525                    (not (gnus-buffer-live-p nnfolder-current-buffer)))
526           (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
527                 nnfolder-current-buffer nil))
528
529         (setq nnfolder-current-group group)
530
531         (when (or (not nnfolder-current-buffer)
532                   (not (verify-visited-file-modtime nnfolder-current-buffer)))
533           (save-excursion
534             (setq file (nnfolder-group-pathname group))
535             ;; See whether we need to create the new file.
536             (unless (file-exists-p file)
537               (gnus-make-directory (file-name-directory file))
538               (nnmail-write-region 1 1 file t 'nomesg))
539             (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
540               (set-buffer nnfolder-current-buffer)
541               (push (list group nnfolder-current-buffer)
542                     nnfolder-buffer-alist))))))))
543
544 (defun nnfolder-save-mail (group-art-list)
545   "Called narrowed to an article."
546   (let* (save-list group-art)
547     (goto-char (point-min))
548     ;; The From line may have been quoted by movemail.
549     (when (looking-at (concat ">" message-unix-mail-delimiter))
550       (delete-char 1))
551     ;; This might come from somewhere else.
552     (unless (looking-at message-unix-mail-delimiter)
553       (insert "From nobody " (current-time-string) "\n")
554       (goto-char (point-min)))
555     ;; Quote all "From " lines in the article.
556     (forward-line 1)
557     (let (case-fold-search)
558       (while (re-search-forward "^From " nil t)
559         (beginning-of-line)
560         (insert "> ")))
561     (setq save-list group-art-list)
562     (nnmail-insert-lines)
563     (nnmail-insert-xref group-art-list)
564     (run-hooks 'nnmail-prepare-save-mail-hook)
565     (run-hooks 'nnfolder-prepare-save-mail-hook)
566
567     ;; Insert the mail into each of the destination groups.
568     (while (setq group-art (pop group-art-list))
569       ;; Kill any previous newsgroup markers.
570       (goto-char (point-min))
571       (search-forward "\n\n" nil t)
572       (forward-line -1)
573       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
574         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
575
576       ;; Insert the new newsgroup marker.
577       (nnfolder-insert-newsgroup-line group-art)
578
579       (save-excursion
580         (let ((beg (point-min))
581               (end (point-max))
582               (obuf (current-buffer)))
583           (nnfolder-possibly-change-folder (car group-art))
584           (let ((buffer-read-only nil))
585             (goto-char (point-max))
586             (unless (eolp)
587               (insert "\n"))
588             (unless (bobp)
589               (insert "\n"))
590             (insert-buffer-substring obuf beg end)))))
591
592     ;; Did we save it anywhere?
593     save-list))
594
595 (defun nnfolder-insert-newsgroup-line (group-art)
596   (save-excursion
597     (goto-char (point-min))
598     (when (search-forward "\n\n" nil t)
599       (forward-char -1)
600       (insert (format (concat nnfolder-article-marker "%d   %s\n")
601                       (cdr group-art) (current-time-string))))))
602
603 (defun nnfolder-active-number (group)
604   ;; Find the next article number in GROUP.
605   (let ((active (cadr (assoc group nnfolder-group-alist))))
606     (if active
607         (setcdr active (1+ (cdr active)))
608       ;; This group is new, so we create a new entry for it.
609       ;; This might be a bit naughty... creating groups on the drop of
610       ;; a hat, but I don't know...
611       (push (list group (setq active (cons 1 1)))
612             nnfolder-group-alist))
613     (cdr active)))
614
615 (defun nnfolder-possibly-change-folder (group)
616   (let ((inf (assoc group nnfolder-buffer-alist)))
617     (if (and inf
618              (gnus-buffer-live-p (cadr inf)))
619         (set-buffer (cadr inf))
620       (when inf
621         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
622       (when nnfolder-group-alist
623         (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
624       (push (list group (nnfolder-read-folder group))
625             nnfolder-buffer-alist))))
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 (group)
643   (let* ((file (nnfolder-group-pathname group))
644          (buffer (set-buffer (nnheader-find-file-noselect file))))
645     (if (equal (cadr (assoc group nnfolder-scantime-alist))
646                (nth 5 (file-attributes file)))
647         ;; This looks up-to-date, so we don't do any scanning.
648         buffer
649       ;; Parse the damn thing.
650       (save-excursion
651         (nnmail-activate 'nnfolder)
652         ;; Read in the file.
653         (let ((delim (concat "^" message-unix-mail-delimiter))
654               (marker (concat "\n" nnfolder-article-marker))
655               (number "[0-9]+")
656               (active (or (cadr (assoc group nnfolder-group-alist))
657                           (cons 1 0)))
658               (scantime (assoc group nnfolder-scantime-alist))
659               (minid (lsh -1 -1))
660               maxid start end newscantime
661               buffer-read-only)
662           (buffer-disable-undo (current-buffer))
663           (setq maxid (cdr active))
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             (unless (re-search-backward marker nil t)
691               (goto-char (point-min)))
692             (when (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           (unless (nnmail-search-unix-mail-delim)
699             (goto-char (point-max)))
700           (setq end (point-marker))
701           (while (not (= end (point-max)))
702             (setq start (marker-position end))
703             (goto-char end)
704             ;; There may be more than one "From " line, so we skip past
705             ;; them.
706             (while (looking-at delim)
707               (forward-line 1))
708             (set-marker end (if (nnmail-search-unix-mail-delim)
709                                 (point)
710                               (point-max)))
711             (goto-char start)
712             (when (not (search-forward marker end t))
713               (narrow-to-region start end)
714               (nnmail-insert-lines)
715               (nnfolder-insert-newsgroup-line
716                (cons nil (nnfolder-active-number nnfolder-current-group)))
717               (widen)))
718
719           (set-marker end nil)
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                  (message-mail-file-mbox-p
740                   (nnheader-concat nnfolder-directory file)))
741         (let ((oldgroup (assoc file nnfolder-group-alist)))
742           (if oldgroup
743               (nnheader-message 5 "Refreshing group %s..." file)
744             (nnheader-message 5 "Adding group %s..." file))
745           (setq nnfolder-group-alist (remove oldgroup nnfolder-group-alist))
746           (push (list file (cons 1 0)) nnfolder-group-alist)
747           (nnfolder-possibly-change-folder file)
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