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