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