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