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