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-active-file-coding-system
94 (if (memq system-type '(windows-nt ms-dos ms-windows))
95 'raw-text-dos 'raw-text))
96 (defvoo nnfolder-active-file-coding-system-for-write
97 nnmail-active-file-coding-system)
98 (defvoo nnfolder-file-coding-system nnfolder-active-file-coding-system)
99 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
100 "Coding system for save nnfolder file.
101 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
105 ;;; Interface functions
107 (nnoo-define-basics nnfolder)
109 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
111 (set-buffer nntp-server-buffer)
113 (let (article start stop)
114 (nnfolder-possibly-change-group group server)
115 (when nnfolder-current-buffer
116 (set-buffer nnfolder-current-buffer)
117 (goto-char (point-min))
118 (if (stringp (car articles))
120 (while (setq article (pop articles))
121 (set-buffer nnfolder-current-buffer)
122 (when (nnfolder-goto-article article)
124 (search-forward "\n\n" nil t)
125 (setq stop (1- (point)))
126 (set-buffer nntp-server-buffer)
127 (insert (format "221 %d Article retrieved.\n" article))
128 (insert-buffer-substring nnfolder-current-buffer start stop)
129 (goto-char (point-max))
132 (set-buffer nntp-server-buffer)
133 (nnheader-fold-continuation-lines)
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)
141 ((not (file-exists-p nnfolder-directory))
142 (nnfolder-close-server)
143 (nnheader-report 'nnfolder "Couldn't create directory: %s"
145 ((not (file-directory-p (file-truename nnfolder-directory)))
146 (nnfolder-close-server)
147 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
149 (nnmail-activate 'nnfolder)
150 (nnheader-report 'nnfolder "Opened server %s using directory %s"
151 server nnfolder-directory)
154 (deffoo nnfolder-request-close ()
155 (let ((alist nnfolder-buffer-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))
163 (deffoo nnfolder-request-article (article &optional group server buffer)
164 (nnfolder-possibly-change-group group server)
166 (set-buffer nnfolder-current-buffer)
167 (goto-char (point-min))
168 (when (nnfolder-goto-article article)
172 (unless (and (nnmail-search-unix-mail-delim)
174 (goto-char (point-max)))
176 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
177 (set-buffer nntp-server-buffer)
179 (insert-buffer-substring nnfolder-current-buffer start stop)
180 (goto-char (point-min))
181 (while (looking-at "From ")
183 (insert "X-From-Line: ")
185 (if (numberp article)
186 (cons nnfolder-current-group article)
187 (goto-char (point-min))
188 (search-forward (concat "\n" nnfolder-article-marker))
189 (cons nnfolder-current-group
192 (point) (progn (end-of-line) (point)))))))))))
194 (deffoo nnfolder-request-group (group &optional server dont-check)
195 (nnfolder-possibly-change-group group server t)
197 (if (not (assoc group nnfolder-group-alist))
198 (nnheader-report 'nnfolder "No such group: %s" group)
201 (nnheader-report 'nnfolder "Selected group %s" group)
203 (let* ((active (assoc group nnfolder-group-alist))
205 (range (cadr active)))
208 (nnheader-report 'nnfolder "No such group: %s" group))
209 ((null nnfolder-current-group)
210 (nnheader-report 'nnfolder "Empty group: %s" group))
212 (nnheader-report 'nnfolder "Selected group %s" group)
213 (nnheader-insert "211 %d %d %d %s\n"
214 (1+ (- (cdr range) (car range)))
215 (car range) (cdr range) group))))))))
217 (deffoo nnfolder-request-scan (&optional group server)
218 (nnfolder-possibly-change-group nil server)
219 (when nnfolder-get-new-mail
220 (nnfolder-possibly-change-group group server)
224 (let ((bufs nnfolder-buffer-alist))
227 (if (not (gnus-buffer-live-p (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))))))
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
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)))
248 (when (and nnfolder-current-group
249 nnfolder-current-buffer)
250 (push (list nnfolder-current-group nnfolder-current-buffer)
251 nnfolder-buffer-alist))
252 (setq nnfolder-buffer-alist
253 (delq inf nnfolder-buffer-alist))
254 (setq nnfolder-current-buffer (cadr inf)
255 nnfolder-current-group (car inf))))
256 (when (and nnfolder-current-buffer
257 (buffer-name nnfolder-current-buffer))
259 (set-buffer nnfolder-current-buffer)
260 ;; If the buffer was modified, write the file out now.
261 (nnfolder-save-buffer)
262 ;; If we're shutting the server down, we need to kill the
263 ;; buffer and remove it from the open buffer list. Or, of
264 ;; course, if we're trying to minimize our space impact.
265 (kill-buffer (current-buffer))
266 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
267 nnfolder-buffer-alist)))))
268 (setq nnfolder-current-group nil
269 nnfolder-current-buffer nil)
272 (deffoo nnfolder-request-create-group (group &optional server args)
273 (nnfolder-possibly-change-group nil server)
274 (nnmail-activate 'nnfolder)
276 (unless (assoc group nnfolder-group-alist)
277 (push (list group (cons 1 0)) nnfolder-group-alist)
278 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
279 (nnfolder-read-folder group)))
282 (deffoo nnfolder-request-list (&optional server)
283 (nnfolder-possibly-change-group nil server)
285 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
286 (nnmail-find-file nnfolder-active-file)
287 (setq nnfolder-group-alist (nnmail-get-active)))
290 (deffoo nnfolder-request-newgroups (date &optional server)
291 (nnfolder-possibly-change-group nil server)
292 (nnfolder-request-list server))
294 (deffoo nnfolder-request-list-newsgroups (&optional server)
295 (nnfolder-possibly-change-group nil server)
297 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
298 (nnmail-find-file nnfolder-newsgroups-file))))
300 ;; Return a list consisting of all article numbers existing in the
303 (defun nnfolder-existing-articles ()
305 (when nnfolder-current-buffer
306 (set-buffer nnfolder-current-buffer)
307 (goto-char (point-min))
308 (let ((marker (concat "\n" nnfolder-article-marker))
312 (while (and (search-forward marker nil t)
313 (re-search-forward number nil t))
314 (let ((newnum (string-to-number (match-string 0))))
315 (if (nnmail-within-headers-p)
316 (push newnum numbers))))
319 (deffoo nnfolder-request-expire-articles
320 (articles newsgroup &optional server force)
321 (nnfolder-possibly-change-group newsgroup server)
323 ;; The articles we have deleted so far.
324 (deleted-articles nil)
325 ;; The articles that really exist and will be expired if they are old enough.
326 (maybe-expirable (gnus-intersection articles (nnfolder-existing-articles))))
327 (nnmail-activate 'nnfolder)
330 (set-buffer nnfolder-current-buffer)
331 ;; Since messages are sorted in arrival order and expired in the
332 ;; same order, we can stop as soon as we find a message that is
334 (while (and maybe-expirable is-old)
335 (goto-char (point-min))
336 (when (and (nnfolder-goto-article (car maybe-expirable))
337 (search-forward (concat "\n" nnfolder-article-marker)
341 (nnmail-expired-article-p
344 (point) (progn (end-of-line) (point)))
345 force nnfolder-inhibit-expiry))
346 (nnheader-message 5 "Deleting article %d..."
347 (car maybe-expirable) newsgroup)
348 (nnfolder-delete-mail)
349 ;; Must remember which articles were actually deleted
350 (push (car maybe-expirable) deleted-articles)))
351 (setq maybe-expirable (cdr maybe-expirable)))
352 (unless nnfolder-inhibit-expiry
353 (nnheader-message 5 "Deleting articles...done"))
354 (nnfolder-save-buffer)
355 (nnfolder-adjust-min-active newsgroup)
356 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
357 (gnus-sorted-complement articles (nreverse deleted-articles)))))
359 (deffoo nnfolder-request-move-article (article group server
360 accept-form &optional last)
362 (let ((buf (get-buffer-create " *nnfolder move*"))
365 (nnfolder-request-article article group server)
369 (insert-buffer-substring nntp-server-buffer)
370 (goto-char (point-min))
371 (while (re-search-forward
372 (concat "^" nnfolder-article-marker)
373 (save-excursion (search-forward "\n\n" nil t) (point)) t)
374 (delete-region (progn (beginning-of-line) (point))
375 (progn (forward-line 1) (point))))
376 (setq result (eval accept-form))
380 (nnfolder-possibly-change-group group server)
381 (set-buffer nnfolder-current-buffer)
382 (goto-char (point-min))
383 (when (nnfolder-goto-article article)
384 (nnfolder-delete-mail))
386 (nnfolder-save-buffer)
387 (nnfolder-adjust-min-active group)
388 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
391 (deffoo nnfolder-request-accept-article (group &optional server last)
393 (nnfolder-possibly-change-group group server)
394 (nnmail-check-syntax)
395 (let ((buf (current-buffer))
397 (goto-char (point-min))
398 (when (looking-at "X-From-Line: ")
399 (replace-match "From "))
401 (nnfolder-request-list)
404 (goto-char (point-min))
405 (search-forward "\n\n" nil t)
407 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
408 (delete-region (point) (progn (forward-line 1) (point))))
409 (when nnmail-cache-accepted-message-ids
410 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
411 (setq result (if (stringp group)
412 (list (cons group (nnfolder-active-number group)))
414 (nnmail-article-group 'nnfolder-active-number))))
415 (if (and (null result)
416 (yes-or-no-p "Moved to `junk' group; delete article? "))
419 (car (nnfolder-save-mail result)))))
422 (nnfolder-possibly-change-folder (or (caar art-group) group))
423 (nnfolder-save-buffer)
424 (when nnmail-cache-accepted-message-ids
425 (nnmail-cache-close)))))
426 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
428 (nnheader-report 'nnfolder "Couldn't store article"))
431 (deffoo nnfolder-request-replace-article (article group buffer)
432 (nnfolder-possibly-change-group group)
435 (goto-char (point-min))
437 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
438 (setq xfrom (match-string 1))
440 (goto-char (point-min))
442 (insert "From " xfrom "\n")
443 (unless (looking-at "From ")
444 (insert "From nobody " (current-time-string) "\n"))))
445 (nnfolder-normalize-buffer)
446 (set-buffer nnfolder-current-buffer)
447 (goto-char (point-min))
448 (if (not (nnfolder-goto-article article))
450 (nnfolder-delete-mail)
451 (insert-buffer-substring buffer)
452 (nnfolder-save-buffer)
455 (deffoo nnfolder-request-delete-group (group &optional force server)
456 (nnfolder-close-group group server t)
457 ;; Delete all articles in GROUP.
459 () ; Don't delete the articles.
460 ;; Delete the file that holds the group.
462 (delete-file (nnfolder-group-pathname group))))
463 ;; Remove the group from all structures.
464 (setq nnfolder-group-alist
465 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
466 nnfolder-current-group nil
467 nnfolder-current-buffer nil)
468 ;; Save the active file.
469 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
472 (deffoo nnfolder-request-rename-group (group new-name &optional server)
473 (nnfolder-possibly-change-group group server)
475 (set-buffer nnfolder-current-buffer)
476 (and (file-writable-p buffer-file-name)
480 (let ((new-file (nnfolder-group-pathname new-name)))
481 (gnus-make-directory (file-name-directory new-file))
484 ;; That went ok, so we change the internal structures.
485 (let ((entry (assoc group nnfolder-group-alist)))
486 (and entry (setcar entry new-name))
487 (setq nnfolder-current-buffer nil
488 nnfolder-current-group nil)
489 ;; Save the new group alist.
490 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
491 ;; We kill the buffer instead of renaming it and stuff.
492 (kill-buffer (current-buffer))
495 (defun nnfolder-request-regenerate (server)
496 (nnfolder-possibly-change-group nil server)
497 (nnfolder-generate-active-file)
501 ;;; Internal functions.
503 (defun nnfolder-adjust-min-active (group)
504 ;; Find the lowest active article in this group.
505 (let* ((active (cadr (assoc group nnfolder-group-alist)))
506 (marker (concat "\n" nnfolder-article-marker))
508 (activemin (cdr active)))
510 (set-buffer nnfolder-current-buffer)
511 (goto-char (point-min))
512 (while (and (search-forward marker nil t)
513 (re-search-forward number nil t))
514 (let ((newnum (string-to-number (match-string 0))))
515 (if (nnmail-within-headers-p)
516 (setq activemin (min activemin newnum)))))
517 (setcar active activemin))))
519 (defun nnfolder-article-string (article)
520 (if (numberp article)
521 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
522 (concat "\nMessage-ID: " article)))
524 (defun nnfolder-goto-article (article)
525 "Place point at the start of the headers of ARTICLE.
526 ARTICLE can be an article number or a Message-ID.
527 Returns t if successful, nil otherwise."
528 (let ((art-string (nnfolder-article-string article))
530 ;; It is likely that we are at or before the delimiter line.
531 ;; We therefore go to the end of the previous line, and start
532 ;; searching from there.
537 ;; First search forward.
538 (while (and (setq found (search-forward art-string nil t))
539 (not (nnmail-within-headers-p))))
540 ;; If unsuccessful, search backward from where we started,
543 (while (and (setq found (search-backward art-string nil t))
544 (not (nnmail-within-headers-p)))))
546 (nnmail-search-unix-mail-delim-backward))))
548 (defun nnfolder-delete-mail (&optional leave-delim)
549 "Delete the message that point is in.
550 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
551 deleted. Point is left where the deleted region was."
555 ;; In case point is at the beginning of the message already.
557 (nnmail-search-unix-mail-delim-backward)
558 (if leave-delim (progn (forward-line 1) (point))
562 (if (nnmail-search-unix-mail-delim)
565 (run-hooks 'nnfolder-delete-mail-hook)
566 (delete-region (point-min) (point-max))))
568 (defun nnfolder-possibly-change-group (group &optional server dont-check)
571 (not (nnfolder-server-opened server)))
572 (nnfolder-open-server server))
573 (unless (gnus-buffer-live-p nnfolder-current-buffer)
574 (setq nnfolder-current-buffer nil
575 nnfolder-current-group nil))
578 (not (equal group nnfolder-current-group)))
579 (nnmail-activate 'nnfolder)
581 (setq nnfolder-current-group group
582 nnfolder-current-buffer nil)
583 ;; If we have to change groups, see if we don't already have the
584 ;; folder in memory. If we do, verify the modtime and destroy
585 ;; the folder if needed so we can rescan it.
586 (setq nnfolder-current-buffer
587 (nth 1 (assoc group nnfolder-buffer-alist)))
589 ;; If the buffer is not live, make sure it isn't in the alist. If it
590 ;; is live, verify that nobody else has touched the file since last
592 (when (and nnfolder-current-buffer
593 (not (gnus-buffer-live-p nnfolder-current-buffer)))
594 (setq nnfolder-current-buffer nil))
596 (setq nnfolder-current-group group)
598 (when (or (not nnfolder-current-buffer)
599 (not (verify-visited-file-modtime
600 nnfolder-current-buffer)))
602 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
603 (set-buffer nnfolder-current-buffer)
604 (push (list group nnfolder-current-buffer)
605 nnfolder-buffer-alist)))))))
607 (defun nnfolder-save-mail (group-art-list)
608 "Called narrowed to an article."
609 (let* (save-list group-art)
610 (goto-char (point-min))
611 ;; The From line may have been quoted by movemail.
612 (when (looking-at ">From")
614 ;; This might come from somewhere else.
615 (unless (looking-at "From ")
616 (insert "From nobody " (current-time-string) "\n")
617 (goto-char (point-min)))
618 ;; Quote all "From " lines in the article.
620 (let (case-fold-search)
621 (while (re-search-forward "^From " nil t)
624 (setq save-list group-art-list)
625 (nnmail-insert-lines)
626 (nnmail-insert-xref group-art-list)
627 (run-hooks 'nnmail-prepare-save-mail-hook)
628 (run-hooks 'nnfolder-prepare-save-mail-hook)
630 ;; Insert the mail into each of the destination groups.
631 (while (setq group-art (pop group-art-list))
632 ;; Kill any previous newsgroup markers.
633 (goto-char (point-min))
634 (search-forward "\n\n" nil t)
636 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
637 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
639 ;; Insert the new newsgroup marker.
640 (nnfolder-insert-newsgroup-line group-art)
643 (let ((beg (point-min))
645 (obuf (current-buffer)))
646 (nnfolder-possibly-change-folder (car group-art))
647 (let ((buffer-read-only nil))
648 (nnfolder-normalize-buffer)
649 (insert-buffer-substring obuf beg end)))))
651 ;; Did we save it anywhere?
654 (defun nnfolder-normalize-buffer ()
655 "Make sure there are two newlines at the end of the buffer."
656 (goto-char (point-max))
657 (skip-chars-backward "\n")
658 (delete-region (point) (point-max))
661 (defun nnfolder-insert-newsgroup-line (group-art)
663 (goto-char (point-min))
664 (when (search-forward "\n\n" nil t)
666 (insert (format (concat nnfolder-article-marker "%d %s\n")
667 (cdr group-art) (current-time-string))))))
669 (defun nnfolder-active-number (group)
670 ;; Find the next article number in GROUP.
671 (let ((active (cadr (assoc group nnfolder-group-alist))))
673 (setcdr active (1+ (cdr active)))
674 ;; This group is new, so we create a new entry for it.
675 ;; This might be a bit naughty... creating groups on the drop of
676 ;; a hat, but I don't know...
677 (push (list group (setq active (cons 1 1)))
678 nnfolder-group-alist))
681 (defun nnfolder-possibly-change-folder (group)
682 (let ((inf (assoc group nnfolder-buffer-alist)))
684 (gnus-buffer-live-p (cadr inf)))
685 (set-buffer (cadr inf))
687 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
688 (when nnfolder-group-alist
689 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
690 (push (list group (nnfolder-read-folder group))
691 nnfolder-buffer-alist))))
693 ;; This method has a problem if you've accidentally let the active list get
694 ;; out of sync with the files. This could happen, say, if you've
695 ;; accidentally gotten new mail with something other than Gnus (but why
696 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
697 ;; processing the file, ready to add new X-Gnus article number markers, and
698 ;; we'll run across a message with no ID yet - the active list _may_not_ be
701 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
702 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
703 ;; rest of the message looking for any more, possibly higher IDs. We'll
704 ;; assume the maximum that we find is the highest active. Note that this
705 ;; shouldn't cost us much extra time at all, but will be a lot less
706 ;; vulnerable to glitches between the mbox and the active file.
708 (defun nnfolder-read-folder (group)
709 (let* ((file (nnfolder-group-pathname group))
711 (let ((nnheader-file-coding-system
712 nnfolder-file-coding-system))
713 (nnheader-find-file-noselect file)))))
714 (if (equal (cadr (assoc group nnfolder-scantime-alist))
715 (nth 5 (file-attributes file)))
716 ;; This looks up-to-date, so we don't do any scanning.
717 (if (file-exists-p file)
719 (push (list group buffer) nnfolder-buffer-alist)
720 (set-buffer-modified-p t)
722 ;; Parse the damn thing.
724 (goto-char (point-min))
725 ;; Remove any blank lines at the start.
726 (while (eq (following-char) ?\n)
728 (nnmail-activate 'nnfolder)
730 (let ((delim "^From ")
731 (marker (concat "\n" nnfolder-article-marker))
733 (active (or (cadr (assoc group nnfolder-group-alist))
735 (scantime (assoc group nnfolder-scantime-alist))
737 maxid start end newscantime
739 (buffer-disable-undo)
740 (setq maxid (cdr active))
741 (goto-char (point-min))
743 ;; Anytime the active number is 1 or 0, it is suspect. In that
744 ;; case, search the file manually to find the active number. Or,
745 ;; of course, if we're being paranoid. (This would also be the
746 ;; place to build other lists from the header markers, such as
747 ;; expunge lists, etc., if we ever desired to abandon the active
748 ;; file entirely for mboxes.)
749 (when (or nnfolder-ignore-active-file
751 (while (and (search-forward marker nil t)
752 (re-search-forward number nil t))
753 (let ((newnum (string-to-number (match-string 0))))
754 (if (nnmail-within-headers-p)
755 (setq maxid (max maxid newnum)
756 minid (min minid newnum)))))
757 (setcar active (max 1 (min minid maxid)))
758 (setcdr active (max maxid (cdr active)))
759 (goto-char (point-min)))
761 ;; As long as we trust that the user will only insert unmarked mail
762 ;; at the end, go to the end and search backwards for the last
763 ;; marker. Find the start of that message, and begin to search for
764 ;; unmarked messages from there.
765 (when (not (or nnfolder-distrust-mbox
767 (goto-char (point-max))
768 (unless (re-search-backward marker nil t)
769 (goto-char (point-min)))
770 (when (nnmail-search-unix-mail-delim)
771 (goto-char (point-min))))
773 ;; Keep track of the active number on our own, and insert it back
774 ;; into the active list when we're done. Also, prime the pump to
775 ;; cut down on the number of searches we do.
776 (unless (nnmail-search-unix-mail-delim)
777 (goto-char (point-max)))
778 (setq end (point-marker))
779 (while (not (= end (point-max)))
780 (setq start (marker-position end))
782 ;; There may be more than one "From " line, so we skip past
784 (while (looking-at delim)
786 (set-marker end (if (nnmail-search-unix-mail-delim)
790 (when (not (search-forward marker end t))
791 (narrow-to-region start end)
792 (nnmail-insert-lines)
793 (nnfolder-insert-newsgroup-line
794 (cons nil (nnfolder-active-number nnfolder-current-group)))
798 ;; Make absolutely sure that the active list reflects reality!
799 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
800 ;; Set the scantime for this group.
801 (setq newscantime (visited-file-modtime))
803 (setcdr scantime (list newscantime))
804 (push (list nnfolder-current-group newscantime)
805 nnfolder-scantime-alist))
806 (current-buffer))))))
809 (defun nnfolder-generate-active-file ()
810 "Look for mbox folders in the nnfolder directory and make them into groups.
811 This command does not work if you use short group names."
813 (nnmail-activate 'nnfolder)
814 (let ((files (directory-files nnfolder-directory))
816 (while (setq file (pop files))
817 (when (and (not (backup-file-name-p file))
818 (message-mail-file-mbox-p
819 (nnheader-concat nnfolder-directory file)))
820 (let ((oldgroup (assoc file nnfolder-group-alist)))
822 (nnheader-message 5 "Refreshing group %s..." file)
823 (nnheader-message 5 "Adding group %s..." file))
825 (setq nnfolder-group-alist
826 (delq oldgroup (copy-sequence nnfolder-group-alist))))
827 (push (list file (cons 1 0)) nnfolder-group-alist)
828 (nnfolder-possibly-change-folder file)
829 (nnfolder-possibly-change-group file)
830 (nnfolder-close-group file))))
831 (nnheader-message 5 "")))
833 (defun nnfolder-group-pathname (group)
834 "Make pathname for GROUP."
836 (encode-coding-string group nnmail-pathname-coding-system))
837 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
838 ;; If this file exists, we use it directly.
839 (if (or nnmail-use-long-file-names
840 (file-exists-p (concat dir group)))
842 ;; If not, we translate dots into slashes.
843 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
845 (defun nnfolder-save-buffer ()
847 (when (buffer-modified-p)
848 (run-hooks 'nnfolder-save-buffer-hook)
849 (gnus-make-directory (file-name-directory (buffer-file-name)))
850 (let* ((coding-system-for-write
851 (or nnfolder-file-coding-system-for-write
852 nnfolder-file-coding-system))
853 (output-coding-system coding-system-for-write))
856 (defun nnfolder-save-active (group-alist active-file)
857 (let ((nnmail-active-file-coding-system
858 (or nnfolder-active-file-coding-system-for-write
859 nnfolder-active-file-coding-system)))
860 (nnmail-save-active group-alist active-file)))
864 ;;; nnfolder.el ends here