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