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