1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
34 (eval-when-compile (require 'cl))
37 (nnoo-declare nnfolder)
39 (defvoo nnfolder-directory (expand-file-name message-directory)
40 "The name of the nnfolder directory.")
42 (defvoo nnfolder-active-file
43 (nnheader-concat nnfolder-directory "active")
44 "The name of the active file.")
46 ;; I renamed this variable to something more in keeping with the general GNU
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.")
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.")
62 (defvoo nnfolder-newsgroups-file
63 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
64 "Mail newsgroups description file.")
66 (defvoo nnfolder-get-new-mail t
67 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
69 (defvoo nnfolder-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
72 (defvoo nnfolder-save-buffer-hook nil
73 "Hook run before saving the nnfolder mbox buffer.")
75 (defvoo nnfolder-inhibit-expiry nil
76 "If non-nil, inhibit expiry.")
80 (defconst nnfolder-version "nnfolder 1.0"
83 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
84 "String used to demarcate what the article number for a message is.")
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 (defvoo nnfolder-file-coding-system nnmail-file-coding-system-1)
97 ;;; Interface functions
99 (nnoo-define-basics nnfolder)
101 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
103 (set-buffer nntp-server-buffer)
105 (let (article start stop)
106 (nnfolder-possibly-change-group group server)
107 (when nnfolder-current-buffer
108 (set-buffer nnfolder-current-buffer)
109 (goto-char (point-min))
110 (if (stringp (car articles))
112 (while (setq article (pop articles))
113 (set-buffer nnfolder-current-buffer)
114 (when (nnfolder-goto-article article)
116 (search-forward "\n\n" nil t)
117 (setq stop (1- (point)))
118 (set-buffer nntp-server-buffer)
119 (insert (format "221 %d Article retrieved.\n" article))
120 (insert-buffer-substring nnfolder-current-buffer start stop)
121 (goto-char (point-max))
124 (set-buffer nntp-server-buffer)
125 (nnheader-fold-continuation-lines)
128 (deffoo nnfolder-open-server (server &optional defs)
129 (nnoo-change-server 'nnfolder server defs)
130 (nnmail-activate 'nnfolder t)
131 (gnus-make-directory nnfolder-directory)
133 ((not (file-exists-p nnfolder-directory))
134 (nnfolder-close-server)
135 (nnheader-report 'nnfolder "Couldn't create directory: %s"
137 ((not (file-directory-p (file-truename nnfolder-directory)))
138 (nnfolder-close-server)
139 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
141 (nnmail-activate 'nnfolder)
142 (nnheader-report 'nnfolder "Opened server %s using directory %s"
143 server nnfolder-directory)
146 (deffoo nnfolder-request-close ()
147 (let ((alist nnfolder-buffer-alist))
149 (nnfolder-close-group (caar alist) nil t)
150 (setq alist (cdr alist))))
151 (nnoo-close-server 'nnfolder)
152 (setq nnfolder-buffer-alist nil
153 nnfolder-group-alist nil))
155 (deffoo nnfolder-request-article (article &optional group server buffer)
156 (nnfolder-possibly-change-group group server)
158 (set-buffer nnfolder-current-buffer)
159 (goto-char (point-min))
160 (when (nnfolder-goto-article article)
164 (unless (and (nnmail-search-unix-mail-delim)
166 (goto-char (point-max)))
168 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
169 (set-buffer nntp-server-buffer)
171 (insert-buffer-substring nnfolder-current-buffer start stop)
172 (goto-char (point-min))
173 (while (looking-at "From ")
175 (insert "X-From-Line: ")
177 (if (numberp article)
178 (cons nnfolder-current-group article)
179 (goto-char (point-min))
180 (search-forward (concat "\n" nnfolder-article-marker))
181 (cons nnfolder-current-group
184 (point) (progn (end-of-line) (point)))))))))))
186 (deffoo nnfolder-request-group (group &optional server dont-check)
187 (nnfolder-possibly-change-group group server t)
189 (if (not (assoc group nnfolder-group-alist))
190 (nnheader-report 'nnfolder "No such group: %s" group)
193 (nnheader-report 'nnfolder "Selected group %s" group)
195 (let* ((active (assoc group nnfolder-group-alist))
197 (range (cadr active)))
200 (nnheader-report 'nnfolder "No such group: %s" group))
201 ((null nnfolder-current-group)
202 (nnheader-report 'nnfolder "Empty group: %s" group))
204 (nnheader-report 'nnfolder "Selected group %s" group)
205 (nnheader-insert "211 %d %d %d %s\n"
206 (1+ (- (cdr range) (car range)))
207 (car range) (cdr range) group))))))))
209 (deffoo nnfolder-request-scan (&optional group server)
210 (nnfolder-possibly-change-group nil server)
211 (when nnfolder-get-new-mail
212 (nnfolder-possibly-change-group group server)
216 (let ((bufs nnfolder-buffer-alist))
219 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
220 (setq nnfolder-buffer-alist
221 (delq (car bufs) nnfolder-buffer-alist))
222 (set-buffer (nth 1 (car bufs)))
223 (nnfolder-save-buffer)
224 (kill-buffer (current-buffer)))
225 (setq bufs (cdr bufs))))))
229 ;; Don't close the buffer if we're not shutting down the server. This way,
230 ;; we can keep the buffer in the group buffer cache, and not have to grovel
231 ;; over the buffer again unless we add new mail to it or modify it in some
234 (deffoo nnfolder-close-group (group &optional server force)
235 ;; Make sure we _had_ the group open.
236 (when (or (assoc group nnfolder-buffer-alist)
237 (equal group nnfolder-current-group))
238 (let ((inf (assoc group nnfolder-buffer-alist)))
240 (when (and nnfolder-current-group
241 nnfolder-current-buffer)
242 (push (list nnfolder-current-group nnfolder-current-buffer)
243 nnfolder-buffer-alist))
244 (setq nnfolder-buffer-alist
245 (delq inf nnfolder-buffer-alist))
246 (setq nnfolder-current-buffer (cadr inf)
247 nnfolder-current-group (car inf))))
248 (when (and nnfolder-current-buffer
249 (buffer-name nnfolder-current-buffer))
251 (set-buffer nnfolder-current-buffer)
252 ;; If the buffer was modified, write the file out now.
253 (nnfolder-save-buffer)
254 ;; If we're shutting the server down, we need to kill the
255 ;; buffer and remove it from the open buffer list. Or, of
256 ;; course, if we're trying to minimize our space impact.
257 (kill-buffer (current-buffer))
258 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
259 nnfolder-buffer-alist)))))
260 (setq nnfolder-current-group nil
261 nnfolder-current-buffer nil)
264 (deffoo nnfolder-request-create-group (group &optional server args)
265 (nnfolder-possibly-change-group nil server)
266 (nnmail-activate 'nnfolder)
268 (unless (assoc group nnfolder-group-alist)
269 (push (list group (cons 1 0)) nnfolder-group-alist)
270 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
271 (nnfolder-read-folder group)))
274 (deffoo nnfolder-request-list (&optional server)
275 (nnfolder-possibly-change-group nil server)
277 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
278 (pathname-coding-system 'binary))
279 (nnmail-find-file nnfolder-active-file)
280 (setq nnfolder-group-alist (nnmail-get-active)))
283 (deffoo nnfolder-request-newgroups (date &optional server)
284 (nnfolder-possibly-change-group nil server)
285 (nnfolder-request-list server))
287 (deffoo nnfolder-request-list-newsgroups (&optional server)
288 (nnfolder-possibly-change-group nil server)
290 (nnmail-find-file nnfolder-newsgroups-file)))
292 (deffoo nnfolder-request-expire-articles
293 (articles newsgroup &optional server force)
294 (nnfolder-possibly-change-group newsgroup server)
297 (nnmail-activate 'nnfolder)
300 (set-buffer nnfolder-current-buffer)
301 (while (and articles is-old)
302 (goto-char (point-min))
303 (when (and (nnfolder-goto-article (car articles))
304 (search-forward (concat "\n" nnfolder-article-marker)
308 (nnmail-expired-article-p
311 (point) (progn (end-of-line) (point)))
312 force nnfolder-inhibit-expiry))
314 (nnheader-message 5 "Deleting article %d..."
315 (car articles) newsgroup)
316 (nnfolder-delete-mail))
317 (push (car articles) rest)))
318 (setq articles (cdr articles)))
319 (unless nnfolder-inhibit-expiry
320 (nnheader-message 5 "Deleting articles...done"))
321 (nnfolder-save-buffer)
322 (nnfolder-adjust-min-active newsgroup)
323 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
324 (nconc rest articles))))
326 (deffoo nnfolder-request-move-article (article group server
327 accept-form &optional last)
329 (let ((buf (get-buffer-create " *nnfolder move*"))
332 (nnfolder-request-article article group server)
336 (insert-buffer-substring nntp-server-buffer)
337 (goto-char (point-min))
338 (while (re-search-forward
339 (concat "^" nnfolder-article-marker)
340 (save-excursion (search-forward "\n\n" nil t) (point)) t)
341 (delete-region (progn (beginning-of-line) (point))
342 (progn (forward-line 1) (point))))
343 (setq result (eval accept-form))
347 (nnfolder-possibly-change-group group server)
348 (set-buffer nnfolder-current-buffer)
349 (goto-char (point-min))
350 (when (nnfolder-goto-article article)
351 (nnfolder-delete-mail))
353 (nnfolder-save-buffer)
354 (nnfolder-adjust-min-active group)
355 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))))
358 (deffoo nnfolder-request-accept-article (group &optional server last)
360 (nnfolder-possibly-change-group group server)
361 (nnmail-check-syntax)
362 (let ((buf (current-buffer))
364 (goto-char (point-min))
365 (when (looking-at "X-From-Line: ")
366 (replace-match "From "))
368 (nnfolder-request-list)
371 (goto-char (point-min))
372 (search-forward "\n\n" nil t)
374 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
375 (delete-region (point) (progn (forward-line 1) (point))))
376 (when nnmail-cache-accepted-message-ids
377 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
378 (setq result (if (stringp group)
379 (list (cons group (nnfolder-active-number group)))
381 (nnmail-article-group 'nnfolder-active-number))))
382 (if (and (null result)
383 (yes-or-no-p "Moved to `junk' group; delete article? "))
386 (car (nnfolder-save-mail result)))))
389 (nnfolder-possibly-change-folder (or (caar art-group) group))
390 (nnfolder-save-buffer)
391 (when nnmail-cache-accepted-message-ids
392 (nnmail-cache-close)))))
393 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
395 (nnheader-report 'nnfolder "Couldn't store article"))
398 (deffoo nnfolder-request-replace-article (article group buffer)
399 (nnfolder-possibly-change-group group)
402 (goto-char (point-min))
404 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
405 (setq xfrom (match-string 1))
407 (goto-char (point-min))
409 (insert "From " xfrom "\n")
410 (unless (looking-at message-unix-mail-delimiter)
411 (insert "From nobody " (current-time-string) "\n"))))
412 (nnfolder-normalize-buffer)
413 (set-buffer nnfolder-current-buffer)
414 (goto-char (point-min))
415 (if (not (nnfolder-goto-article article))
417 (nnfolder-delete-mail)
418 (insert-buffer-substring buffer)
419 (nnfolder-save-buffer)
422 (deffoo nnfolder-request-delete-group (group &optional force server)
423 (nnfolder-close-group group server t)
424 ;; Delete all articles in GROUP.
426 () ; Don't delete the articles.
427 ;; Delete the file that holds the group.
429 (delete-file (nnfolder-group-pathname group))))
430 ;; Remove the group from all structures.
431 (setq nnfolder-group-alist
432 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
433 nnfolder-current-group nil
434 nnfolder-current-buffer nil)
435 ;; Save the active file.
436 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
439 (deffoo nnfolder-request-rename-group (group new-name &optional server)
440 (nnfolder-possibly-change-group group server)
442 (set-buffer nnfolder-current-buffer)
443 (and (file-writable-p buffer-file-name)
447 (nnfolder-group-pathname new-name))
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))
460 (defun nnfolder-request-regenerate (server)
461 (nnfolder-possibly-change-group nil server)
462 (nnfolder-generate-active-file)
466 ;;; Internal functions.
468 (defun nnfolder-adjust-min-active (group)
469 ;; Find the lowest active article in this group.
470 (let* ((active (cadr (assoc group nnfolder-group-alist)))
471 (marker (concat "\n" nnfolder-article-marker))
473 (activemin (cdr active)))
475 (set-buffer nnfolder-current-buffer)
476 (goto-char (point-min))
477 (while (and (search-forward marker nil t)
478 (re-search-forward number nil t))
479 (let ((newnum (string-to-number (match-string 0))))
480 (if (nnmail-within-headers-p)
481 (setq activemin (min activemin newnum)))))
482 (setcar active activemin))))
484 (defun nnfolder-article-string (article)
485 (if (numberp article)
486 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
487 (concat "\nMessage-ID: " article)))
489 (defun nnfolder-goto-article (article)
490 "Place point at the start of the headers of ARTICLE.
491 ARTICLE can be an article number or a Message-ID.
492 Returns t if successful, nil otherwise."
493 (let ((art-string (nnfolder-article-string article))
495 ;; It is likely that we are at or before the delimiter line.
496 ;; We therefore go to the end of the previous line, and start
497 ;; searching from there.
502 ;; First search forward.
503 (while (and (setq found (search-forward art-string nil t))
504 (not (nnmail-within-headers-p))))
505 ;; If unsuccessful, search backward from where we started,
508 (while (and (setq found (search-backward art-string nil t))
509 (not (nnmail-within-headers-p)))))
511 (nnmail-search-unix-mail-delim-backward))))
513 (defun nnfolder-delete-mail (&optional leave-delim)
514 "Delete the message that point is in.
515 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
516 deleted. Point is left where the deleted region was."
520 ;; In case point is at the beginning of the message already.
522 (nnmail-search-unix-mail-delim-backward)
523 (if leave-delim (progn (forward-line 1) (point))
527 (if (nnmail-search-unix-mail-delim)
530 (run-hooks 'nnfolder-delete-mail-hook)
531 (delete-region (point-min) (point-max))))
533 (defun nnfolder-possibly-change-group (group &optional server dont-check)
536 (not (nnfolder-server-opened server)))
537 (nnfolder-open-server server))
538 (unless (gnus-buffer-live-p nnfolder-current-buffer)
539 (setq nnfolder-current-buffer nil
540 nnfolder-current-group nil))
543 (not (equal group nnfolder-current-group)))
544 (let ((pathname-coding-system 'binary))
545 (nnmail-activate 'nnfolder)
546 (when (and (not (assoc group nnfolder-group-alist))
548 (nnfolder-group-pathname group))))
549 ;; The group doesn't exist, so we create a new entry for it.
550 (push (list group (cons 1 0)) nnfolder-group-alist)
551 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
554 (setq nnfolder-current-group group
555 nnfolder-current-buffer nil)
557 ;; If we have to change groups, see if we don't already have the
558 ;; folder in memory. If we do, verify the modtime and destroy
559 ;; the folder if needed so we can rescan it.
560 (setq nnfolder-current-buffer
561 (nth 1 (assoc group nnfolder-buffer-alist)))
563 ;; If the buffer is not live, make sure it isn't in the alist. If it
564 ;; is live, verify that nobody else has touched the file since last
566 (when (and nnfolder-current-buffer
567 (not (gnus-buffer-live-p nnfolder-current-buffer)))
568 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
569 nnfolder-current-buffer nil))
571 (setq nnfolder-current-group group)
573 (when (or (not nnfolder-current-buffer)
574 (not (verify-visited-file-modtime
575 nnfolder-current-buffer)))
577 (setq file (nnfolder-group-pathname group))
578 ;; See whether we need to create the new file.
579 (unless (file-exists-p file)
580 (gnus-make-directory (file-name-directory file))
581 (nnmail-write-region 1 1 file t 'nomesg))
582 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
583 (set-buffer nnfolder-current-buffer)
584 (push (list group nnfolder-current-buffer)
585 nnfolder-buffer-alist)))))))))
587 (defun nnfolder-save-mail (group-art-list)
588 "Called narrowed to an article."
589 (let* (save-list group-art)
590 (goto-char (point-min))
591 ;; The From line may have been quoted by movemail.
592 (when (looking-at (concat ">" message-unix-mail-delimiter))
594 ;; This might come from somewhere else.
595 (unless (looking-at message-unix-mail-delimiter)
596 (insert "From nobody " (current-time-string) "\n")
597 (goto-char (point-min)))
598 ;; Quote all "From " lines in the article.
600 (let (case-fold-search)
601 (while (re-search-forward "^From " nil t)
604 (setq save-list group-art-list)
605 (nnmail-insert-lines)
606 (nnmail-insert-xref group-art-list)
607 (run-hooks 'nnmail-prepare-save-mail-hook)
608 (run-hooks 'nnfolder-prepare-save-mail-hook)
610 ;; Insert the mail into each of the destination groups.
611 (while (setq group-art (pop group-art-list))
612 ;; Kill any previous newsgroup markers.
613 (goto-char (point-min))
614 (search-forward "\n\n" nil t)
616 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
617 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
619 ;; Insert the new newsgroup marker.
620 (nnfolder-insert-newsgroup-line group-art)
623 (let ((beg (point-min))
625 (obuf (current-buffer)))
626 (nnfolder-possibly-change-folder (car group-art))
627 (let ((buffer-read-only nil))
628 (nnfolder-normalize-buffer)
629 (insert-buffer-substring obuf beg end)))))
631 ;; Did we save it anywhere?
634 (defun nnfolder-normalize-buffer ()
635 "Make sure there are two newlines at the end of the buffer."
636 (goto-char (point-max))
637 (skip-chars-backward "\n")
638 (delete-region (point) (point-max))
641 (defun nnfolder-insert-newsgroup-line (group-art)
643 (goto-char (point-min))
644 (when (search-forward "\n\n" nil t)
646 (insert (format (concat nnfolder-article-marker "%d %s\n")
647 (cdr group-art) (current-time-string))))))
649 (defun nnfolder-active-number (group)
650 ;; Find the next article number in GROUP.
651 (let ((active (cadr (assoc group nnfolder-group-alist))))
653 (setcdr active (1+ (cdr active)))
654 ;; This group is new, so we create a new entry for it.
655 ;; This might be a bit naughty... creating groups on the drop of
656 ;; a hat, but I don't know...
657 (push (list group (setq active (cons 1 1)))
658 nnfolder-group-alist))
661 (defun nnfolder-possibly-change-folder (group)
662 (let ((inf (assoc group nnfolder-buffer-alist)))
664 (gnus-buffer-live-p (cadr inf)))
665 (set-buffer (cadr inf))
667 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
668 (when nnfolder-group-alist
669 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
670 (push (list group (nnfolder-read-folder group))
671 nnfolder-buffer-alist))))
673 ;; This method has a problem if you've accidentally let the active list get
674 ;; out of sync with the files. This could happen, say, if you've
675 ;; accidentally gotten new mail with something other than Gnus (but why
676 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
677 ;; processing the file, ready to add new X-Gnus article number markers, and
678 ;; we'll run across a message with no ID yet - the active list _may_not_ be
681 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
682 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
683 ;; rest of the message looking for any more, possibly higher IDs. We'll
684 ;; assume the maximum that we find is the highest active. Note that this
685 ;; shouldn't cost us much extra time at all, but will be a lot less
686 ;; vulnerable to glitches between the mbox and the active file.
688 (defun nnfolder-read-folder (group)
689 (let* ((file (nnfolder-group-pathname group))
691 (let ((nnmail-file-coding-system
692 nnfolder-file-coding-system))
693 (nnheader-find-file-noselect file)))))
694 (if (equal (cadr (assoc group nnfolder-scantime-alist))
695 (nth 5 (file-attributes file)))
696 ;; This looks up-to-date, so we don't do any scanning.
697 (if (file-exists-p file)
699 (push (list group buffer) nnfolder-buffer-alist)
700 (set-buffer-modified-p t)
702 ;; Parse the damn thing.
704 (nnmail-activate 'nnfolder)
706 (let ((delim (concat "^" message-unix-mail-delimiter))
707 (marker (concat "\n" nnfolder-article-marker))
709 (active (or (cadr (assoc group nnfolder-group-alist))
711 (scantime (assoc group nnfolder-scantime-alist))
713 maxid start end newscantime
715 (buffer-disable-undo)
716 (setq maxid (cdr active))
717 (goto-char (point-min))
719 ;; Anytime the active number is 1 or 0, it is suspect. In that
720 ;; case, search the file manually to find the active number. Or,
721 ;; of course, if we're being paranoid. (This would also be the
722 ;; place to build other lists from the header markers, such as
723 ;; expunge lists, etc., if we ever desired to abandon the active
724 ;; file entirely for mboxes.)
725 (when (or nnfolder-ignore-active-file
727 (while (and (search-forward marker nil t)
728 (re-search-forward number nil t))
729 (let ((newnum (string-to-number (match-string 0))))
730 (if (nnmail-within-headers-p)
731 (setq maxid (max maxid newnum)
732 minid (min minid newnum)))))
733 (setcar active (max 1 (min minid maxid)))
734 (setcdr active (max maxid (cdr active)))
735 (goto-char (point-min)))
737 ;; As long as we trust that the user will only insert unmarked mail
738 ;; at the end, go to the end and search backwards for the last
739 ;; marker. Find the start of that message, and begin to search for
740 ;; unmarked messages from there.
741 (when (not (or nnfolder-distrust-mbox
743 (goto-char (point-max))
744 (unless (re-search-backward marker nil t)
745 (goto-char (point-min)))
746 (when (nnmail-search-unix-mail-delim)
747 (goto-char (point-min))))
749 ;; Keep track of the active number on our own, and insert it back
750 ;; into the active list when we're done. Also, prime the pump to
751 ;; cut down on the number of searches we do.
752 (unless (nnmail-search-unix-mail-delim)
753 (goto-char (point-max)))
754 (setq end (point-marker))
755 (while (not (= end (point-max)))
756 (setq start (marker-position end))
758 ;; There may be more than one "From " line, so we skip past
760 (while (looking-at delim)
762 (set-marker end (if (nnmail-search-unix-mail-delim)
766 (when (not (search-forward marker end t))
767 (narrow-to-region start end)
768 (nnmail-insert-lines)
769 (nnfolder-insert-newsgroup-line
770 (cons nil (nnfolder-active-number nnfolder-current-group)))
774 ;; Make absolutely sure that the active list reflects reality!
775 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
776 ;; Set the scantime for this group.
777 (setq newscantime (visited-file-modtime))
779 (setcdr scantime (list newscantime))
780 (push (list nnfolder-current-group newscantime)
781 nnfolder-scantime-alist))
782 (current-buffer))))))
785 (defun nnfolder-generate-active-file ()
786 "Look for mbox folders in the nnfolder directory and make them into groups."
788 (nnmail-activate 'nnfolder)
789 (let ((files (directory-files nnfolder-directory))
791 (while (setq file (pop files))
792 (when (and (not (backup-file-name-p file))
793 (message-mail-file-mbox-p
794 (nnheader-concat nnfolder-directory file)))
795 (let ((oldgroup (assoc file nnfolder-group-alist)))
797 (nnheader-message 5 "Refreshing group %s..." file)
798 (nnheader-message 5 "Adding group %s..." file))
800 (setq nnfolder-group-alist
801 (delq oldgroup (copy-sequence nnfolder-group-alist))))
802 (push (list file (cons 1 0)) nnfolder-group-alist)
803 (nnfolder-possibly-change-folder file)
804 (nnfolder-possibly-change-group file)
805 (nnfolder-close-group file))))
806 (nnheader-message 5 "")))
808 (defun nnfolder-group-pathname (group)
809 "Make pathname for GROUP."
811 (mm-encode-coding-string group nnmail-pathname-coding-system))
812 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
813 ;; If this file exists, we use it directly.
814 (if (or nnmail-use-long-file-names
815 (file-exists-p (concat dir group)))
817 ;; If not, we translate dots into slashes.
818 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
820 (defun nnfolder-save-buffer ()
822 (when (buffer-modified-p)
823 (run-hooks 'nnfolder-save-buffer-hook)
824 (gnus-make-directory (file-name-directory (buffer-file-name)))
829 ;;; nnfolder.el ends here