*** 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: news, 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 'rmail)
37 (require 'nnmail)
38 (eval-when-compile (require 'cl))
39
40 (defvar nnfolder-directory (expand-file-name "~/Mail/")
41   "The name of the nnfolder directory.")
42
43 (defvar nnfolder-active-file 
44   (concat (file-name-as-directory nnfolder-directory) "active")
45   "The name of the active file.")
46
47 ;; I renamed this variable to something more in keeping with the general GNU
48 ;; style. -SLB
49
50 (defvar nnfolder-ignore-active-file nil
51   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
52 Note that the active file is still saved, but it's values are not
53 used.  This costs some extra time when scanning an mbox when opening
54 it.")
55
56 (defvar nnfolder-newsgroups-file 
57   (concat (file-name-as-directory nnfolder-directory) "newsgroups")
58   "Mail newsgroups description file.")
59
60 (defvar nnfolder-get-new-mail t
61   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
62
63 (defvar nnfolder-prepare-save-mail-hook nil
64   "Hook run narrowed to an article before saving.")
65
66 (defvar nnfolder-inhibit-expiry nil
67   "If non-nil, inhibit expiry.")
68
69 \f
70
71 (defconst nnfolder-version "nnfolder 1.0"
72   "nnfolder version.")
73
74 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
75   "String used to demarcate what the article number for a message is.")
76
77 (defvar nnfolder-current-group nil)
78 (defvar nnfolder-current-buffer nil)
79 (defvar nnfolder-status-string "")
80 (defvar nnfolder-group-alist nil)
81 (defvar nnfolder-buffer-alist nil)
82 (defvar nnfolder-active-timestamp nil)
83
84 \f
85
86 (defvar nnfolder-current-server nil)
87 (defvar nnfolder-server-alist nil)
88 (defvar nnfolder-server-variables 
89   `((nnfolder-directory ,nnfolder-directory)
90     (nnfolder-active-file ,nnfolder-active-file)
91     (nnfolder-newsgroups-file ,nnfolder-newsgroups-file)
92     (nnfolder-get-new-mail ,nnfolder-get-new-mail)
93     (nnfolder-inhibit-expiry ,nnfolder-inhibit-expiry) 
94     (nnfolder-current-group nil)
95     (nnfolder-prepare-save-mail-hook nil)
96     (nnfolder-ignore-active-file ,nnfolder-ignore-active-file)
97     (nnfolder-current-buffer nil)
98     (nnfolder-status-string "")
99     (nnfolder-group-alist nil)
100     (nnfolder-buffer-alist nil)
101     (nnfolder-active-timestamp nil)))
102
103 \f
104
105 ;;; Interface functions
106
107 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
108   (save-excursion
109     (set-buffer nntp-server-buffer)
110     (erase-buffer)
111     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
112           article art-string start stop)
113       (nnfolder-possibly-change-group newsgroup server)
114       (set-buffer nnfolder-current-buffer)
115       (goto-char (point-min))
116       (if (stringp (car sequence))
117           'headers
118         (while sequence
119           (setq article (car sequence))
120           (setq art-string (nnfolder-article-string article))
121           (set-buffer nnfolder-current-buffer)
122           (if (or (search-forward art-string nil t)
123                   ;; Don't search the whole file twice!  Also, articles
124                   ;; probably have some locality by number, so searching
125                   ;; backwards will be faster.  Especially if we're at the
126                   ;; beginning of the buffer :-). -SLB
127                   (search-backward art-string nil t))
128               (progn
129                 (setq start (or (re-search-backward delim-string nil t)
130                                 (point)))
131                 (search-forward "\n\n" nil t)
132                 (setq stop (1- (point)))
133                 (set-buffer nntp-server-buffer)
134                 (insert (format "221 %d Article retrieved.\n" article))
135                 (insert-buffer-substring nnfolder-current-buffer start stop)
136                 (goto-char (point-max))
137                 (insert ".\n")))
138           (setq sequence (cdr sequence)))
139
140         (set-buffer nntp-server-buffer)
141         (nnheader-fold-continuation-lines)
142         'headers))))
143
144 (defun nnfolder-open-server (server &optional defs)
145   (nnheader-change-server 'nnfolder server defs)
146   (when (not (file-exists-p nnfolder-directory))
147     (condition-case ()
148         (make-directory nnfolder-directory t)
149       (error t)))
150   (cond 
151    ((not (file-exists-p nnfolder-directory))
152     (nnfolder-close-server)
153     (nnheader-report 'nnfolder "Couldn't create directory: %s"
154                      nnfolder-directory))
155    ((not (file-directory-p (file-truename nnfolder-directory)))
156     (nnfolder-close-server)
157     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
158    (t
159     (nnheader-report 'nnfolder "Opened server %s using directory %s"
160                      server nnfolder-directory)
161     t)))
162
163 (defun nnfolder-close-server (&optional server)
164   (setq nnfolder-current-server nil
165         nnfolder-group-alist nil)
166   t)
167
168 (defun nnfolder-server-opened (&optional server)
169   (and (equal server nnfolder-current-server)
170        nntp-server-buffer
171        (buffer-name nntp-server-buffer)))
172
173 (defun nnfolder-request-close ()
174   (let ((alist nnfolder-buffer-alist))
175     (while alist
176       (nnfolder-close-group (caar alist) nil t)
177       (setq alist (cdr alist))))
178   (setq nnfolder-buffer-alist nil
179         nnfolder-current-server nil
180         nnfolder-group-alist nil))
181
182 (defun nnfolder-status-message (&optional server)
183   nnfolder-status-string)
184
185 (defun nnfolder-request-article (article &optional newsgroup server buffer)
186   (nnfolder-possibly-change-group newsgroup server)
187   (save-excursion
188     (set-buffer nnfolder-current-buffer)
189     (goto-char (point-min))
190     (if (search-forward (nnfolder-article-string article) nil t)
191         (let (start stop)
192           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
193           (setq start (point))
194           (forward-line 1)
195           (or (and (re-search-forward 
196                     (concat "^" rmail-unix-mail-delimiter) nil t)
197                    (forward-line -1))
198               (goto-char (point-max)))
199           (setq stop (point))
200           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
201             (set-buffer nntp-server-buffer)
202             (erase-buffer)
203             (insert-buffer-substring nnfolder-current-buffer start stop)
204             (goto-char (point-min))
205             (while (looking-at "From ")
206               (delete-char 5)
207               (insert "X-From-Line: ")
208               (forward-line 1))
209             (if (numberp article) 
210                 (cons nnfolder-current-group article)
211               (goto-char (point-min))
212               (search-forward (concat "\n" nnfolder-article-marker))
213               (cons nnfolder-current-group
214                     (string-to-int 
215                      (buffer-substring 
216                       (point) (progn (end-of-line) (point)))))))))))
217
218 (defun nnfolder-request-group (group &optional server dont-check)
219   (save-excursion
220     (nnmail-activate 'nnfolder)
221     (if (not (assoc group nnfolder-group-alist))
222         (nnheader-report 'nnfolder "No such group: %s" group)
223       (nnfolder-possibly-change-group group server)
224       (if dont-check
225           (progn 
226             (nnheader-report 'nnfolder "Selected group %s" group)
227             t)
228         (let* ((active (assoc group nnfolder-group-alist))
229                (group (car active))
230                (range (cadr active))
231                (minactive (car range))
232                (maxactive (cdr range)))
233           (cond 
234            ((null active)
235             (nnheader-report 'nnfolder "No such group: %s" group))
236            (t
237             (nnheader-report 'nnfolder "Selected group %s" group)
238             (nnheader-insert "211 %d %d %d %s\n" 
239                              (1+ (- maxactive minactive))
240                              minactive maxactive group))))))))
241
242 (defun nnfolder-request-scan (&optional group server)
243   (nnmail-get-new-mail
244    'nnfolder 
245    (lambda ()
246      (let ((bufs nnfolder-buffer-alist))
247        (save-excursion
248          (while bufs
249            (if (not (buffer-name (nth 1 (car bufs))))
250                (setq nnfolder-buffer-alist 
251                      (delq (car bufs) nnfolder-buffer-alist))
252              (set-buffer (nth 1 (car bufs)))
253              (and (buffer-modified-p) (save-buffer))
254              (kill-buffer (current-buffer)))
255            (setq bufs (cdr bufs))))))
256    nnfolder-directory
257    group))
258
259 ;; Don't close the buffer if we're not shutting down the server.  This way,
260 ;; we can keep the buffer in the group buffer cache, and not have to grovel
261 ;; over the buffer again unless we add new mail to it or modify it in some
262 ;; way.
263
264 (defun nnfolder-close-group (group &optional server force)
265   ;; Make sure we _had_ the group open.
266   (when (or (assoc group nnfolder-buffer-alist)
267             (equal group nnfolder-current-group))
268     (nnfolder-possibly-change-group group server)
269     (save-excursion
270       (set-buffer nnfolder-current-buffer)
271       ;; If the buffer was modified, write the file out now.
272       (and (buffer-modified-p) (save-buffer))
273       ;; If we're shutting the server down, we need to kill the
274       ;; buffer and remove it from the open buffer list.  Or, of
275       ;; course, if we're trying to minimize our space impact.
276       (kill-buffer (current-buffer))
277       (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
278                                         nnfolder-buffer-alist))))
279   (setq nnfolder-current-group nil
280         nnfolder-current-buffer nil)
281   t)
282
283 (defun nnfolder-request-create-group (group &optional server) 
284   (nnmail-activate 'nnfolder)
285   (unless (assoc group nnfolder-group-alist)
286     (push (list group (cons 1 0)) nnfolder-group-alist)
287     (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
288   t)
289
290 (defun nnfolder-request-list (&optional server)
291   (save-excursion
292     (nnmail-find-file nnfolder-active-file)
293     (setq nnfolder-group-alist (nnmail-get-active))))
294
295 (defun nnfolder-request-newgroups (date &optional server)
296   (nnfolder-request-list server))
297
298 (defun nnfolder-request-list-newsgroups (&optional server)
299   (save-excursion
300     (nnmail-find-file nnfolder-newsgroups-file)))
301
302 (defun nnfolder-request-expire-articles 
303   (articles newsgroup &optional server force)
304   (nnfolder-possibly-change-group newsgroup server)
305   (let* ((is-old t)
306          rest)
307     (nnmail-activate 'nnfolder)
308
309     (save-excursion 
310       (set-buffer nnfolder-current-buffer)
311       (while (and articles is-old)
312         (goto-char (point-min))
313         (if (search-forward (nnfolder-article-string (car articles)) nil t)
314             (if (setq is-old
315                       (nnmail-expired-article-p 
316                        newsgroup
317                        (buffer-substring 
318                         (point) (progn (end-of-line) (point))) 
319                        force nnfolder-inhibit-expiry))
320                 (progn
321                   (nnheader-message 5 "Deleting article %d..." 
322                                     (car articles) newsgroup)
323                   (nnfolder-delete-mail))
324               (setq rest (cons (car articles) rest))))
325         (setq articles (cdr articles)))
326       (and (buffer-modified-p) (save-buffer))
327       ;; Find the lowest active article in this group.
328       (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
329              (marker (concat "\n" nnfolder-article-marker))
330              (number "[0-9]+")
331              (activemin (cdr active)))
332         (goto-char (point-min))
333         (while (and (search-forward marker nil t)
334                     (re-search-forward number nil t))
335           (setq activemin (min activemin
336                                (string-to-number (buffer-substring
337                                                   (match-beginning 0)
338                                                   (match-end 0))))))
339         (setcar active activemin))
340       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
341       (nconc rest articles))))
342
343 (defun nnfolder-request-move-article
344   (article group server accept-form &optional last)
345   (nnfolder-possibly-change-group group server)
346   (let ((buf (get-buffer-create " *nnfolder move*"))
347         result)
348     (and 
349      (nnfolder-request-article article group server)
350      (save-excursion
351        (set-buffer buf)
352        (buffer-disable-undo (current-buffer))
353        (erase-buffer)
354        (insert-buffer-substring nntp-server-buffer)
355        (goto-char (point-min))
356        (while (re-search-forward 
357                (concat "^" nnfolder-article-marker)
358                (save-excursion (search-forward "\n\n" nil t) (point)) t)
359          (delete-region (progn (beginning-of-line) (point))
360                         (progn (forward-line 1) (point))))
361        (setq result (eval accept-form))
362        (kill-buffer buf)
363        result)
364      (save-excursion
365        (nnfolder-possibly-change-group group server)
366        (set-buffer nnfolder-current-buffer)
367        (goto-char (point-min))
368        (if (search-forward (nnfolder-article-string article) nil t)
369            (nnfolder-delete-mail))
370        (and last 
371             (buffer-modified-p)
372             (save-buffer))))
373     result))
374
375 (defun nnfolder-request-accept-article (group &optional last)
376   (and (stringp group) (nnfolder-possibly-change-group group))
377   (let ((buf (current-buffer))
378         result)
379     (goto-char (point-min))
380     (when (looking-at "X-From-Line: ")
381       (replace-match "From "))
382     (and 
383      (nnfolder-request-list)
384      (save-excursion
385        (set-buffer buf)
386        (goto-char (point-min))
387        (search-forward "\n\n" nil t)
388        (forward-line -1)
389        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
390          (delete-region (point) (progn (forward-line 1) (point))))
391        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
392      (save-excursion
393        (set-buffer nnfolder-current-buffer)
394        (and last (buffer-modified-p) (save-buffer))))
395     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
396     result))
397
398 (defun nnfolder-request-replace-article (article group buffer)
399   (nnfolder-possibly-change-group group)
400   (save-excursion
401     (set-buffer nnfolder-current-buffer)
402     (goto-char (point-min))
403     (if (not (search-forward (nnfolder-article-string article) nil t))
404         nil
405       (nnfolder-delete-mail t t)
406       (insert-buffer-substring buffer)
407       (and (buffer-modified-p) (save-buffer))
408       t)))
409
410 (defun nnfolder-request-delete-group (group &optional force server)
411   (nnfolder-close-group group server t)
412   ;; Delete all articles in GROUP.
413   (if (not force)
414       ()                                ; Don't delete the articles.
415     ;; Delete the file that holds the group.
416     (condition-case nil
417         (delete-file (concat (file-name-as-directory nnfolder-directory)
418                              group))
419       (error nil)))
420   ;; Remove the group from all structures.
421   (setq nnfolder-group-alist 
422         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
423         nnfolder-current-group nil
424         nnfolder-current-buffer nil)
425   ;; Save the active file.
426   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
427   t)
428
429 (defun nnfolder-request-rename-group (group new-name &optional server)
430   (nnfolder-possibly-change-group group server)
431   (save-excursion
432     (set-buffer nnfolder-current-buffer)
433     (and (file-writable-p buffer-file-name)
434          (condition-case ()
435              (progn
436                (rename-file buffer-file-name
437                             (concat (file-name-as-directory nnfolder-directory)
438                                     new-name))
439                t)
440            (error nil))
441          ;; That went ok, so we change the internal structures.
442          (let ((entry (assoc group nnfolder-group-alist)))
443            (and entry (setcar entry new-name))
444            (setq nnfolder-current-buffer nil
445                  nnfolder-current-group nil)
446            ;; Save the new group alist.
447            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
448            ;; We kill the buffer instead of renaming it and stuff.
449            (kill-buffer (current-buffer))
450            t))))
451
452 \f
453 ;;; Internal functions.
454
455 (defun nnfolder-article-string (article)
456   (if (numberp article)
457       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
458     (concat "\nMessage-ID: " article)))
459
460 (defun nnfolder-delete-mail (&optional force leave-delim)
461   ;; Beginning of the article.
462   (save-excursion
463     (save-restriction
464       (narrow-to-region
465        (save-excursion
466          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
467          (if leave-delim (progn (forward-line 1) (point))
468            (match-beginning 0)))
469        (progn
470          (forward-line 1)
471          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
472                                      nil t)
473                   (if (and (not (bobp)) leave-delim)
474                       (progn (forward-line -2) (point))
475                     (match-beginning 0)))
476              (point-max))))
477       (delete-region (point-min) (point-max)))))
478
479 (defun nnfolder-possibly-change-group (group &optional server)
480   (when (and server
481              (not (nnfolder-server-opened server)))
482     (nnfolder-open-server server))
483   (unless (file-exists-p nnfolder-directory)
484     (make-directory (directory-file-name nnfolder-directory) t))
485   (nnfolder-possibly-activate-groups nil)
486   (or (assoc group nnfolder-group-alist)
487       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
488                                   group)))
489       (progn
490         (setq nnfolder-group-alist 
491               (cons (list group (cons 1 0)) nnfolder-group-alist))
492         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
493   (let (inf file)
494     (if (and (equal group nnfolder-current-group)
495              nnfolder-current-buffer
496              (buffer-name nnfolder-current-buffer))
497         ()
498       (setq nnfolder-current-group group)
499
500       ;; If we have to change groups, see if we don't already have the mbox
501       ;; in memory.  If we do, verify the modtime and destroy the mbox if
502       ;; needed so we can rescan it.
503       (if (setq inf (assoc group nnfolder-buffer-alist))
504           (setq nnfolder-current-buffer (nth 1 inf)))
505
506       ;; If the buffer is not live, make sure it isn't in the alist.  If it
507       ;; is live, verify that nobody else has touched the file since last
508       ;; time.
509       (if (or (not (and nnfolder-current-buffer
510                         (buffer-name nnfolder-current-buffer)))
511               (not (and (bufferp nnfolder-current-buffer)
512                         (verify-visited-file-modtime 
513                          nnfolder-current-buffer))))
514           (progn
515             (if (and nnfolder-current-buffer
516                      (buffer-name nnfolder-current-buffer)
517                      (bufferp nnfolder-current-buffer))
518                 (kill-buffer nnfolder-current-buffer))
519             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
520             (setq inf nil)))
521       
522       (if inf
523           ()
524         (save-excursion
525           (setq file (concat (file-name-as-directory nnfolder-directory)
526                              group))
527           (if (file-directory-p (file-truename file))
528               ()
529             (if (not (file-exists-p file))
530                 (write-region 1 1 file t 'nomesg))
531             (setq nnfolder-current-buffer 
532                   (set-buffer (nnfolder-read-folder file)))
533             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
534                                               nnfolder-buffer-alist)))))))
535   (setq nnfolder-current-group group))
536
537 (defun nnfolder-save-mail (&optional group)
538   "Called narrowed to an article."
539   (let* ((nnmail-split-methods 
540           (if group (list (list group "")) nnmail-split-methods))
541          (group-art-list
542           (nreverse (nnmail-article-group 'nnfolder-active-number)))
543          (delim (concat "^" rmail-unix-mail-delimiter))
544          save-list group-art)
545     (goto-char (point-min))
546     ;; This might come from somewhere else.
547     (unless (looking-at delim)
548       (insert "From nobody " (current-time-string) "\n")
549       (goto-char (point-min)))
550     ;; Quote all "From " lines in the article.
551     (forward-line 1)
552     (while (re-search-forward delim nil t)
553       (beginning-of-line)
554       (insert "> "))
555     (setq save-list group-art-list)
556     (nnmail-insert-lines)
557     (nnmail-insert-xref group-art-list)
558     (run-hooks 'nnfolder-prepare-save-mail-hook)
559
560     ;; Insert the mail into each of the destination groups.
561     (while group-art-list
562       (setq group-art (car group-art-list)
563             group-art-list (cdr group-art-list))
564
565       ;; Kill the previous newsgroup markers.
566       (goto-char (point-min))
567       (search-forward "\n\n" nil t)
568       (forward-line -1)
569       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
570         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
571
572       ;; Insert the new newsgroup marker.
573       (nnfolder-possibly-change-group (car group-art))
574       (nnfolder-insert-newsgroup-line group-art)
575       (let ((beg (point-min))
576             (end (point-max))
577             (obuf (current-buffer)))
578         (set-buffer nnfolder-current-buffer)
579         (goto-char (point-max))
580         (insert-buffer-substring obuf beg end)
581         (set-buffer obuf)))
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     (if (search-forward "\n\n" nil t)
590         (progn
591           (forward-char -1)
592           (insert (format (concat nnfolder-article-marker "%d   %s\n")
593                           (cdr group-art) (current-time-string)))))))
594
595 (defun nnfolder-possibly-activate-groups (&optional group)
596   (save-excursion
597     ;; If we're looking for the activation of a specific group, find out
598     ;; its real name and switch to it.
599     (if group (nnfolder-possibly-change-group group))
600     ;; If the group alist isn't active, activate it now.
601     (nnmail-activate 'nnfolder)))
602
603 (defun nnfolder-active-number (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 "^" rmail-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 (provide 'nnfolder)
718
719 ;;; nnfolder.el ends here