*** empty log message ***
[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        (nnmail-cache-insert (nnmail-fetch-field "message-id"))
386        (setq result
387              (car (nnfolder-save-mail
388                    (if (stringp group)
389                        (list (cons group (nnfolder-active-number group)))
390                      (setq art-group
391                            (nnmail-article-group 'nnfolder-active-number)))))))
392      (when last
393        (save-excursion
394          (nnfolder-possibly-change-folder (or (caar art-group) group))
395          (nnfolder-save-buffer)
396          (nnmail-cache-close))))
397     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
398     (unless result
399       (nnheader-report 'nnfolder "Couldn't store article"))
400     result))
401
402 (deffoo nnfolder-request-replace-article (article group buffer)
403   (nnfolder-possibly-change-group group)
404   (save-excursion
405     (set-buffer nnfolder-current-buffer)
406     (goto-char (point-min))
407     (if (not (search-forward (nnfolder-article-string article) nil t))
408         nil
409       (nnfolder-delete-mail t t)
410       (insert-buffer-substring buffer)
411       (nnfolder-save-buffer)
412       t)))
413
414 (deffoo nnfolder-request-delete-group (group &optional force server)
415   (nnfolder-close-group group server t)
416   ;; Delete all articles in GROUP.
417   (if (not force)
418       ()                                ; Don't delete the articles.
419     ;; Delete the file that holds the group.
420     (ignore-errors
421       (delete-file (nnfolder-group-pathname group))))
422   ;; Remove the group from all structures.
423   (setq nnfolder-group-alist
424         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
425         nnfolder-current-group nil
426         nnfolder-current-buffer nil)
427   ;; Save the active file.
428   (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
429   t)
430
431 (deffoo nnfolder-request-rename-group (group new-name &optional server)
432   (nnfolder-possibly-change-group group server)
433   (save-excursion
434     (set-buffer nnfolder-current-buffer)
435     (and (file-writable-p buffer-file-name)
436          (ignore-errors
437            (rename-file
438             buffer-file-name
439             (nnfolder-group-pathname new-name))
440            t)
441          ;; That went ok, so we change the internal structures.
442          (let ((entry (assoc group nnfolder-group-alist)))
443            (and entry (setcar entry new-name))
444            (setq nnfolder-current-buffer nil
445                  nnfolder-current-group nil)
446            ;; Save the new group alist.
447            (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
448            ;; We kill the buffer instead of renaming it and stuff.
449            (kill-buffer (current-buffer))
450            t))))
451
452 \f
453 ;;; Internal functions.
454
455 (defun nnfolder-article-string (article)
456   (if (numberp article)
457       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
458     (concat "\nMessage-ID: " article)))
459
460 (defun nnfolder-delete-mail (&optional force leave-delim)
461   "Delete the message that point is in."
462   (save-excursion
463     (delete-region
464      (save-excursion
465        (nnmail-search-unix-mail-delim-backward)
466        (if leave-delim (progn (forward-line 1) (point))
467          (point)))
468      (progn
469        (forward-line 1)
470        (if (nnmail-search-unix-mail-delim)
471            (if (and (not (bobp)) leave-delim)
472                (progn (forward-line -2) (point))
473              (point))
474          (point-max))))))
475
476 (defun nnfolder-possibly-change-group (group &optional server scanning)
477   ;; Change servers.
478   (when (and server
479              (not (nnfolder-server-opened server)))
480     (nnfolder-open-server server))
481   ;; Change group.
482   (when (and group
483              (not (equal group nnfolder-current-group)))
484     (nnmail-activate 'nnfolder)
485     (when (and (not (assoc group nnfolder-group-alist))
486                (not (file-exists-p
487                      (nnfolder-group-pathname group))))
488       ;; The group doesn't exist, so we create a new entry for it.
489       (push (list group (cons 1 0)) nnfolder-group-alist)
490       (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
491
492     (let (inf file)
493       ;; If we have to change groups, see if we don't already have the
494       ;; folder in memory.  If we do, verify the modtime and destroy
495       ;; the folder if needed so we can rescan it.
496       (when (setq inf (assoc group nnfolder-buffer-alist))
497         (setq nnfolder-current-buffer (nth 1 inf)))
498
499       ;; If the buffer is not live, make sure it isn't in the alist.  If it
500       ;; is live, verify that nobody else has touched the file since last
501       ;; time.
502       (when (and nnfolder-current-buffer
503                  (not (gnus-buffer-live-p nnfolder-current-buffer)))
504         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
505               nnfolder-current-buffer nil))
506
507       (setq nnfolder-current-group group)
508
509       (when (or (not nnfolder-current-buffer)
510                 (not (verify-visited-file-modtime nnfolder-current-buffer)))
511         (save-excursion
512           (setq file (nnfolder-group-pathname group))
513           ;; See whether we need to create the new file.
514           (unless (file-exists-p file)
515             (gnus-make-directory (file-name-directory file))
516             (nnmail-write-region 1 1 file t 'nomesg))
517           (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
518             (set-buffer nnfolder-current-buffer)
519             (push (list group nnfolder-current-buffer)
520                   nnfolder-buffer-alist)))))))
521
522 (defun nnfolder-save-mail (group-art-list)
523   "Called narrowed to an article."
524   (let* (save-list group-art)
525     (goto-char (point-min))
526     ;; The From line may have been quoted by movemail.
527     (when (looking-at (concat ">" message-unix-mail-delimiter))
528       (delete-char 1))
529     ;; This might come from somewhere else.
530     (unless (looking-at message-unix-mail-delimiter)
531       (insert "From nobody " (current-time-string) "\n")
532       (goto-char (point-min)))
533     ;; Quote all "From " lines in the article.
534     (forward-line 1)
535     (while (re-search-forward "^From " nil t)
536       (beginning-of-line)
537       (insert "> "))
538     (setq save-list group-art-list)
539     (nnmail-insert-lines)
540     (nnmail-insert-xref group-art-list)
541     (run-hooks 'nnmail-prepare-save-mail-hook)
542     (run-hooks 'nnfolder-prepare-save-mail-hook)
543
544     ;; Insert the mail into each of the destination groups.
545     (while (setq group-art (pop group-art-list))
546       ;; Kill any previous newsgroup markers.
547       (goto-char (point-min))
548       (search-forward "\n\n" nil t)
549       (forward-line -1)
550       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
551         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
552
553       ;; Insert the new newsgroup marker.
554       (nnfolder-insert-newsgroup-line group-art)
555
556       (save-excursion
557         (let ((beg (point-min))
558               (end (point-max))
559               (obuf (current-buffer)))
560           (nnfolder-possibly-change-folder (car group-art))
561           (goto-char (point-max))
562           (unless (eolp)
563             (insert "\n"))
564           (unless (bobp)
565             (insert "\n"))
566           (insert-buffer-substring obuf beg end))))
567
568     ;; Did we save it anywhere?
569     save-list))
570
571 (defun nnfolder-insert-newsgroup-line (group-art)
572   (save-excursion
573     (goto-char (point-min))
574     (when (search-forward "\n\n" nil t)
575       (forward-char -1)
576       (insert (format (concat nnfolder-article-marker "%d   %s\n")
577                       (cdr group-art) (current-time-string))))))
578
579 (defun nnfolder-active-number (group)
580   ;; Find the next article number in GROUP.
581   (let ((active (cadr (assoc group nnfolder-group-alist))))
582     (if active
583         (setcdr active (1+ (cdr active)))
584       ;; This group is new, so we create a new entry for it.
585       ;; This might be a bit naughty... creating groups on the drop of
586       ;; a hat, but I don't know...
587       (push (list group (setq active (cons 1 1)))
588             nnfolder-group-alist))
589     (cdr active)))
590
591 (defun nnfolder-possibly-change-folder (group)
592   (let ((inf (assoc group nnfolder-buffer-alist)))
593     (if (and inf
594              (gnus-buffer-live-p (cadr inf)))
595         (set-buffer (cadr inf))
596       (when inf
597         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
598       (when nnfolder-group-alist
599         (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
600       (push (list group (nnfolder-read-folder group))
601             nnfolder-buffer-alist))))
602
603 ;; This method has a problem if you've accidentally let the active list get
604 ;; out of sync with the files.  This could happen, say, if you've
605 ;; accidentally gotten new mail with something other than Gnus (but why
606 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
607 ;; processing the file, ready to add new X-Gnus article number markers, and
608 ;; we'll run across a message with no ID yet - the active list _may_not_ be
609 ;; ready for us yet.
610
611 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
612 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
613 ;; rest of the message looking for any more, possibly higher IDs.  We'll
614 ;; assume the maximum that we find is the highest active.  Note that this
615 ;; shouldn't cost us much extra time at all, but will be a lot less
616 ;; vulnerable to glitches between the mbox and the active file.
617
618 (defun nnfolder-read-folder (group)
619   (let* ((file (nnfolder-group-pathname group))
620          (buffer (set-buffer (nnheader-find-file-noselect file))))
621     (if (equal (cadr (assoc group nnfolder-scantime-alist))
622                (nth 5 (file-attributes file)))
623         ;; This looks up-to-date, so we don't do any scanning.
624         buffer
625       ;; Parse the damn thing.
626       (save-excursion
627         (nnmail-activate 'nnfolder)
628         ;; Read in the file.
629         (let ((delim (concat "^" message-unix-mail-delimiter))
630               (marker (concat "\n" nnfolder-article-marker))
631               (number "[0-9]+")
632               (active (cadr (assoc group nnfolder-group-alist)))
633               (scantime (assoc group nnfolder-scantime-alist))
634               (minid (lsh -1 -1))
635               maxid start end newscantime
636               buffer-read-only)
637           (buffer-disable-undo (current-buffer))
638           (setq maxid (cdr active))
639           (goto-char (point-min))
640
641           ;; Anytime the active number is 1 or 0, it is suspect.  In that
642           ;; case, search the file manually to find the active number.  Or,
643           ;; of course, if we're being paranoid.  (This would also be the
644           ;; place to build other lists from the header markers, such as
645           ;; expunge lists, etc., if we ever desired to abandon the active
646           ;; file entirely for mboxes.)
647           (when (or nnfolder-ignore-active-file
648                     (< maxid 2))
649             (while (and (search-forward marker nil t)
650                         (re-search-forward number nil t))
651               (let ((newnum (string-to-number (match-string 0))))
652                 (setq maxid (max maxid newnum))
653                 (setq minid (min minid newnum))))
654             (setcar active (max 1 (min minid maxid)))
655             (setcdr active (max maxid (cdr active)))
656             (goto-char (point-min)))
657
658           ;; As long as we trust that the user will only insert unmarked mail
659           ;; at the end, go to the end and search backwards for the last
660           ;; marker.  Find the start of that message, and begin to search for
661           ;; unmarked messages from there.
662           (when (not (or nnfolder-distrust-mbox
663                          (< maxid 2)))
664             (goto-char (point-max))
665             (if (not (re-search-backward marker nil t))
666                 (goto-char (point-min))
667               (when (not (nnmail-search-unix-mail-delim))
668                 (goto-char (point-min)))))
669
670           ;; Keep track of the active number on our own, and insert it back
671           ;; into the active list when we're done.  Also, prime the pump to
672           ;; cut down on the number of searches we do.
673           (unless (nnmail-search-unix-mail-delim)
674             (goto-char (point-max)))
675           (setq end (point-marker))
676           (while (not (= end (point-max)))
677             (setq start (marker-position end))
678             (goto-char end)
679             ;; There may be more than one "From " line, so we skip past
680             ;; them.
681             (while (looking-at delim)
682               (forward-line 1))
683             (set-marker end (if (nnmail-search-unix-mail-delim)
684                                 (point)
685                               (point-max)))
686             (goto-char start)
687             (when (not (search-forward marker end t))
688               (narrow-to-region start end)
689               (nnmail-insert-lines)
690               (nnfolder-insert-newsgroup-line
691                (cons nil (nnfolder-active-number nnfolder-current-group)))
692               (widen)))
693
694           (set-marker end nil)
695           ;; Make absolutely sure that the active list reflects reality!
696           (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
697           ;; Set the scantime for this group.
698           (setq newscantime (visited-file-modtime))
699           (if scantime
700               (setcdr scantime (list newscantime))
701             (push (list nnfolder-current-group newscantime)
702                   nnfolder-scantime-alist))
703           (current-buffer))))))
704
705 ;;;###autoload
706 (defun nnfolder-generate-active-file ()
707   "Look for mbox folders in the nnfolder directory and make them into groups."
708   (interactive)
709   (nnmail-activate 'nnfolder)
710   (let ((files (directory-files nnfolder-directory))
711         file)
712     (while (setq file (pop files))
713       (when (and (not (backup-file-name-p file))
714                  (message-mail-file-mbox-p
715                   (concat nnfolder-directory file)))
716         (nnheader-message 5 "Adding group %s..." file)
717         (push (list file (cons 1 0)) nnfolder-group-alist)
718         (nnfolder-possibly-change-group file)
719         (nnfolder-close-group file))
720       (message ""))))
721
722 (defun nnfolder-group-pathname (group)
723   "Make pathname for GROUP."
724   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
725     ;; If this file exists, we use it directly.
726     (if (or nnmail-use-long-file-names
727             (file-exists-p (concat dir group)))
728         (concat dir group)
729       ;; If not, we translate dots into slashes.
730       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
731
732 (defun nnfolder-save-buffer ()
733   "Save the buffer."
734   (when (buffer-modified-p)
735     (run-hooks 'nnfolder-save-buffer-hook)
736     (save-buffer)))
737
738 (provide 'nnfolder)
739
740 ;;; nnfolder.el ends here