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