1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Scott Byer <byer@mv.us.adobe.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl
))
38 (nnoo-declare nnfolder
)
40 (defvoo nnfolder-directory
(expand-file-name message-directory
)
41 "The name of the nnfolder directory.")
43 (defvoo nnfolder-active-file
44 (nnheader-concat nnfolder-directory
"active")
45 "The name of the active file.")
47 ;; I renamed this variable to something more in keeping with the general GNU
50 (defvoo nnfolder-ignore-active-file nil
51 "If non-nil, the active file is ignored.
52 This causes nnfolder to do some extra work in order to determine the
53 true active ranges of an mbox file. Note that the active file is
54 still saved, but its values are not used. This costs some extra time
55 when scanning an mbox when opening it.")
57 (defvoo nnfolder-distrust-mbox nil
58 "If non-nil, the folder will be distrusted.
59 This means that nnfolder will not trust the user with respect to
60 inserting unaccounted for mail in the middle of an mbox file. This
61 can greatly slow down scans, which now must scan the entire file for
62 unmarked messages. When nil, scans occur forward from the last marked
63 message, a huge time saver for large mailboxes.")
65 (defvoo nnfolder-newsgroups-file
66 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
67 "Mail newsgroups description file.")
69 (defvoo nnfolder-get-new-mail t
70 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
72 (defvoo nnfolder-prepare-save-mail-hook nil
73 "Hook run narrowed to an article before saving.")
75 (defvoo nnfolder-save-buffer-hook nil
76 "Hook run before saving the nnfolder mbox buffer.")
78 (defvoo nnfolder-inhibit-expiry nil
79 "If non-nil, inhibit expiry.")
83 (defconst nnfolder-version
"nnfolder 1.0"
86 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
87 "String used to demarcate what the article number for a message is.")
89 (defvoo nnfolder-current-group nil
)
90 (defvoo nnfolder-current-buffer nil
)
91 (defvoo nnfolder-status-string
"")
92 (defvoo nnfolder-group-alist nil
)
93 (defvoo nnfolder-buffer-alist nil
)
94 (defvoo nnfolder-scantime-alist nil
)
95 (defvoo nnfolder-active-timestamp nil
)
96 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
97 (defvoo nnfolder-active-file-coding-system-for-write
98 nnmail-active-file-coding-system
)
99 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
100 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
101 "Coding system for save nnfolder file.
102 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
106 ;;; Interface functions
108 (nnoo-define-basics nnfolder
)
110 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
112 (set-buffer nntp-server-buffer
)
114 (let (article start stop
)
115 (nnfolder-possibly-change-group group server
)
116 (when nnfolder-current-buffer
117 (set-buffer nnfolder-current-buffer
)
118 (goto-char (point-min))
119 (if (stringp (car articles
))
121 (while (setq article
(pop articles
))
122 (set-buffer nnfolder-current-buffer
)
123 (when (nnfolder-goto-article article
)
125 (setq stop
(if (search-forward "\n\n" nil t
)
128 (set-buffer nntp-server-buffer
)
129 (insert (format "221 %d Article retrieved.\n" article
))
130 (insert-buffer-substring nnfolder-current-buffer start stop
)
131 (goto-char (point-max))
134 (set-buffer nntp-server-buffer
)
135 (nnheader-fold-continuation-lines)
138 (deffoo nnfolder-open-server
(server &optional defs
)
139 (nnoo-change-server 'nnfolder server defs
)
140 (nnmail-activate 'nnfolder t
)
141 (gnus-make-directory nnfolder-directory
)
143 ((not (file-exists-p nnfolder-directory
))
144 (nnfolder-close-server)
145 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
147 ((not (file-directory-p (file-truename nnfolder-directory
)))
148 (nnfolder-close-server)
149 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
151 (nnmail-activate 'nnfolder
)
152 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
153 server nnfolder-directory
)
156 (deffoo nnfolder-request-close
()
157 (let ((alist nnfolder-buffer-alist
))
159 (nnfolder-close-group (caar alist
) nil t
)
160 (setq alist
(cdr alist
))))
161 (nnoo-close-server 'nnfolder
)
162 (setq nnfolder-buffer-alist nil
163 nnfolder-group-alist nil
))
165 (deffoo nnfolder-request-article
(article &optional group server buffer
)
166 (nnfolder-possibly-change-group group server
)
168 (set-buffer nnfolder-current-buffer
)
169 (goto-char (point-min))
170 (when (nnfolder-goto-article article
)
174 (unless (and (nnmail-search-unix-mail-delim)
176 (goto-char (point-max)))
178 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
179 (set-buffer nntp-server-buffer
)
181 (insert-buffer-substring nnfolder-current-buffer start stop
)
182 (goto-char (point-min))
183 (while (looking-at "From ")
185 (insert "X-From-Line: ")
187 (if (numberp article
)
188 (cons nnfolder-current-group article
)
189 (goto-char (point-min))
190 (cons nnfolder-current-group
191 (if (search-forward (concat "\n" nnfolder-article-marker
)
195 (point) (progn (end-of-line) (point))))
198 (deffoo nnfolder-request-group
(group &optional server dont-check
)
199 (nnfolder-possibly-change-group group server t
)
201 (if (not (assoc group nnfolder-group-alist
))
202 (nnheader-report 'nnfolder
"No such group: %s" group
)
205 (nnheader-report 'nnfolder
"Selected group %s" group
)
207 (let* ((active (assoc group nnfolder-group-alist
))
209 (range (cadr active
)))
212 (nnheader-report 'nnfolder
"No such group: %s" group
))
213 ((null nnfolder-current-group
)
214 (nnheader-report 'nnfolder
"Empty group: %s" group
))
216 (nnheader-report 'nnfolder
"Selected group %s" group
)
217 (nnheader-insert "211 %d %d %d %s\n"
218 (1+ (- (cdr range
) (car range
)))
219 (car range
) (cdr range
) group
))))))))
221 (deffoo nnfolder-request-scan
(&optional group server
)
222 (nnfolder-possibly-change-group nil server
)
223 (when nnfolder-get-new-mail
224 (nnfolder-possibly-change-group group server
)
228 (let ((bufs nnfolder-buffer-alist
))
231 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
232 (setq nnfolder-buffer-alist
233 (delq (car bufs
) nnfolder-buffer-alist
))
234 (set-buffer (nth 1 (car bufs
)))
235 (nnfolder-save-buffer)
236 (kill-buffer (current-buffer)))
237 (setq bufs
(cdr bufs
))))))
241 ;; Don't close the buffer if we're not shutting down the server. This way,
242 ;; we can keep the buffer in the group buffer cache, and not have to grovel
243 ;; over the buffer again unless we add new mail to it or modify it in some
246 (deffoo nnfolder-close-group
(group &optional server force
)
247 ;; Make sure we _had_ the group open.
248 (when (or (assoc group nnfolder-buffer-alist
)
249 (equal group nnfolder-current-group
))
250 (let ((inf (assoc group nnfolder-buffer-alist
)))
252 (when (and nnfolder-current-group
253 nnfolder-current-buffer
)
254 (push (list nnfolder-current-group nnfolder-current-buffer
)
255 nnfolder-buffer-alist
))
256 (setq nnfolder-buffer-alist
257 (delq inf nnfolder-buffer-alist
))
258 (setq nnfolder-current-buffer
(cadr inf
)
259 nnfolder-current-group
(car inf
))))
260 (when (and nnfolder-current-buffer
261 (buffer-name nnfolder-current-buffer
))
263 (set-buffer nnfolder-current-buffer
)
264 ;; If the buffer was modified, write the file out now.
265 (nnfolder-save-buffer)
266 ;; If we're shutting the server down, we need to kill the
267 ;; buffer and remove it from the open buffer list. Or, of
268 ;; course, if we're trying to minimize our space impact.
269 (kill-buffer (current-buffer))
270 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
271 nnfolder-buffer-alist
)))))
272 (setq nnfolder-current-group nil
273 nnfolder-current-buffer nil
)
276 (deffoo nnfolder-request-create-group
(group &optional server args
)
277 (nnfolder-possibly-change-group nil server
)
278 (nnmail-activate 'nnfolder
)
280 (unless (assoc group nnfolder-group-alist
)
281 (push (list group
(cons 1 0)) nnfolder-group-alist
)
282 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
283 (nnfolder-read-folder group
)))
286 (deffoo nnfolder-request-list
(&optional server
)
287 (nnfolder-possibly-change-group nil server
)
289 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
290 (nnmail-find-file nnfolder-active-file
)
291 (setq nnfolder-group-alist
(nnmail-get-active)))
294 (deffoo nnfolder-request-newgroups
(date &optional server
)
295 (nnfolder-possibly-change-group nil server
)
296 (nnfolder-request-list server
))
298 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
299 (nnfolder-possibly-change-group nil server
)
301 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
302 (nnmail-find-file nnfolder-newsgroups-file
))))
304 ;; Return a list consisting of all article numbers existing in the
307 (defun nnfolder-existing-articles ()
309 (when nnfolder-current-buffer
310 (set-buffer nnfolder-current-buffer
)
311 (goto-char (point-min))
312 (let ((marker (concat "\n" nnfolder-article-marker
))
316 (while (and (search-forward marker nil t
)
317 (re-search-forward number nil t
))
318 (let ((newnum (string-to-number (match-string 0))))
319 (if (nnmail-within-headers-p)
320 (push newnum numbers
))))
323 (deffoo nnfolder-request-expire-articles
324 (articles newsgroup
&optional server force
)
325 (nnfolder-possibly-change-group newsgroup server
)
327 ;; The articles we have deleted so far.
328 (deleted-articles nil
)
329 ;; The articles that really exist and will
330 ;; be expired if they are old enough.
332 (gnus-intersection articles
(nnfolder-existing-articles))))
333 (nnmail-activate 'nnfolder
)
336 (set-buffer nnfolder-current-buffer
)
337 ;; Since messages are sorted in arrival order and expired in the
338 ;; same order, we can stop as soon as we find a message that is
340 (while (and maybe-expirable is-old
)
341 (goto-char (point-min))
342 (when (and (nnfolder-goto-article (car maybe-expirable
))
343 (search-forward (concat "\n" nnfolder-article-marker
)
347 (nnmail-expired-article-p
350 (point) (progn (end-of-line) (point)))
351 force nnfolder-inhibit-expiry
))
352 (nnheader-message 5 "Deleting article %d..."
353 (car maybe-expirable
) newsgroup
)
354 (nnfolder-delete-mail)
355 ;; Must remember which articles were actually deleted
356 (push (car maybe-expirable
) deleted-articles
)))
357 (setq maybe-expirable
(cdr maybe-expirable
)))
358 (unless nnfolder-inhibit-expiry
359 (nnheader-message 5 "Deleting articles...done"))
360 (nnfolder-save-buffer)
361 (nnfolder-adjust-min-active newsgroup
)
362 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
363 (gnus-sorted-complement articles
(nreverse deleted-articles
)))))
365 (deffoo nnfolder-request-move-article
(article group server
366 accept-form
&optional last
)
368 (let ((buf (get-buffer-create " *nnfolder move*"))
371 (nnfolder-request-article article group server
)
375 (insert-buffer-substring nntp-server-buffer
)
376 (goto-char (point-min))
377 (while (re-search-forward
378 (concat "^" nnfolder-article-marker
)
379 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
381 (delete-region (progn (beginning-of-line) (point))
382 (progn (forward-line 1) (point))))
383 (setq result
(eval accept-form
))
387 (nnfolder-possibly-change-group group server
)
388 (set-buffer nnfolder-current-buffer
)
389 (goto-char (point-min))
390 (when (nnfolder-goto-article article
)
391 (nnfolder-delete-mail))
393 (nnfolder-save-buffer)
394 (nnfolder-adjust-min-active group
)
395 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
398 (deffoo nnfolder-request-accept-article
(group &optional server last
)
400 (nnfolder-possibly-change-group group server
)
401 (nnmail-check-syntax)
402 (let ((buf (current-buffer))
404 (goto-char (point-min))
405 (when (looking-at "X-From-Line: ")
406 (replace-match "From "))
408 (nnfolder-request-list)
411 (goto-char (point-min))
412 (if (search-forward "\n\n" nil t
)
414 (goto-char (point-max)))
415 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
416 (delete-region (point) (progn (forward-line 1) (point))))
417 (when nnmail-cache-accepted-message-ids
418 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
419 (setq result
(if (stringp group
)
420 (list (cons group
(nnfolder-active-number group
)))
422 (nnmail-article-group 'nnfolder-active-number
))))
423 (if (and (null result
)
424 (yes-or-no-p "Moved to `junk' group; delete article? "))
427 (car (nnfolder-save-mail result
)))))
430 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
431 (nnfolder-save-buffer)
432 (when nnmail-cache-accepted-message-ids
433 (nnmail-cache-close)))))
434 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
436 (nnheader-report 'nnfolder
"Couldn't store article"))
439 (deffoo nnfolder-request-replace-article
(article group buffer
)
440 (nnfolder-possibly-change-group group
)
443 (goto-char (point-min))
445 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t
)
446 (setq xfrom
(match-string 1))
448 (goto-char (point-min))
450 (insert "From " xfrom
"\n")
451 (unless (looking-at "From ")
452 (insert "From nobody " (current-time-string) "\n"))))
453 (nnfolder-normalize-buffer)
454 (set-buffer nnfolder-current-buffer
)
455 (goto-char (point-min))
456 (if (not (nnfolder-goto-article article
))
458 (nnfolder-delete-mail)
459 (insert-buffer-substring buffer
)
460 (nnfolder-save-buffer)
463 (deffoo nnfolder-request-delete-group
(group &optional force server
)
464 (nnfolder-close-group group server t
)
465 ;; Delete all articles in GROUP.
467 () ; Don't delete the articles.
468 ;; Delete the file that holds the group.
470 (delete-file (nnfolder-group-pathname group
))))
471 ;; Remove the group from all structures.
472 (setq nnfolder-group-alist
473 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
474 nnfolder-current-group nil
475 nnfolder-current-buffer nil
)
476 ;; Save the active file.
477 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
480 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
481 (nnfolder-possibly-change-group group server
)
483 (set-buffer nnfolder-current-buffer
)
484 (and (file-writable-p buffer-file-name
)
488 (let ((new-file (nnfolder-group-pathname new-name
)))
489 (gnus-make-directory (file-name-directory new-file
))
492 ;; That went ok, so we change the internal structures.
493 (let ((entry (assoc group nnfolder-group-alist
)))
494 (and entry
(setcar entry new-name
))
495 (setq nnfolder-current-buffer nil
496 nnfolder-current-group nil
)
497 ;; Save the new group alist.
498 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
499 ;; We kill the buffer instead of renaming it and stuff.
500 (kill-buffer (current-buffer))
503 (defun nnfolder-request-regenerate (server)
504 (nnfolder-possibly-change-group nil server
)
505 (nnfolder-generate-active-file)
509 ;;; Internal functions.
511 (defun nnfolder-adjust-min-active (group)
512 ;; Find the lowest active article in this group.
513 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
514 (marker (concat "\n" nnfolder-article-marker
))
516 (activemin (cdr active
)))
518 (set-buffer nnfolder-current-buffer
)
519 (goto-char (point-min))
520 (while (and (search-forward marker nil t
)
521 (re-search-forward number nil t
))
522 (let ((newnum (string-to-number (match-string 0))))
523 (if (nnmail-within-headers-p)
524 (setq activemin
(min activemin newnum
)))))
525 (setcar active activemin
))))
527 (defun nnfolder-article-string (article)
528 (if (numberp article
)
529 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
530 (concat "\nMessage-ID: " article
)))
532 (defun nnfolder-goto-article (article)
533 "Place point at the start of the headers of ARTICLE.
534 ARTICLE can be an article number or a Message-ID.
535 Returns t if successful, nil otherwise."
536 (let ((art-string (nnfolder-article-string article
))
538 ;; It is likely that we are at or before the delimiter line.
539 ;; We therefore go to the end of the previous line, and start
540 ;; searching from there.
545 ;; First search forward.
546 (while (and (setq found
(search-forward art-string nil t
))
547 (not (nnmail-within-headers-p))))
548 ;; If unsuccessful, search backward from where we started,
551 (while (and (setq found
(search-backward art-string nil t
))
552 (not (nnmail-within-headers-p)))))
554 (nnmail-search-unix-mail-delim-backward))))
556 (defun nnfolder-delete-mail (&optional leave-delim
)
557 "Delete the message that point is in.
558 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
559 deleted. Point is left where the deleted region was."
563 ;; In case point is at the beginning of the message already.
565 (nnmail-search-unix-mail-delim-backward)
566 (if leave-delim
(progn (forward-line 1) (point))
570 (if (nnmail-search-unix-mail-delim)
573 (run-hooks 'nnfolder-delete-mail-hook
)
574 (delete-region (point-min) (point-max))))
576 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
579 (not (nnfolder-server-opened server
)))
580 (nnfolder-open-server server
))
581 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
582 (setq nnfolder-current-buffer nil
583 nnfolder-current-group nil
))
586 (not (equal group nnfolder-current-group
)))
587 (let ((file-name-coding-system nnmail-pathname-coding-system
))
588 (nnmail-activate 'nnfolder
)
589 (when (and (not (assoc group nnfolder-group-alist
))
591 (nnfolder-group-pathname group
))))
592 ;; The group doesn't exist, so we create a new entry for it.
593 (push (list group
(cons 1 0)) nnfolder-group-alist
)
594 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
597 (setq nnfolder-current-group group
598 nnfolder-current-buffer nil
)
600 ;; If we have to change groups, see if we don't already have the
601 ;; folder in memory. If we do, verify the modtime and destroy
602 ;; the folder if needed so we can rescan it.
603 (setq nnfolder-current-buffer
604 (nth 1 (assoc group nnfolder-buffer-alist
)))
606 ;; If the buffer is not live, make sure it isn't in the alist. If it
607 ;; is live, verify that nobody else has touched the file since last
609 (when (and nnfolder-current-buffer
610 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
611 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
612 nnfolder-current-buffer nil
))
614 (setq nnfolder-current-group group
)
616 (when (or (not nnfolder-current-buffer
)
617 (not (verify-visited-file-modtime
618 nnfolder-current-buffer
)))
620 (setq file
(nnfolder-group-pathname group
))
621 ;; See whether we need to create the new file.
622 (unless (file-exists-p file
)
623 (gnus-make-directory (file-name-directory file
))
624 (let ((nnmail-file-coding-system
625 (or nnfolder-file-coding-system-for-write
626 nnfolder-file-coding-system-for-write
)))
627 (nnmail-write-region 1 1 file t
'nomesg
)))
628 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
629 (set-buffer nnfolder-current-buffer
)
630 (push (list group nnfolder-current-buffer
)
631 nnfolder-buffer-alist
)))))))))
633 (defun nnfolder-save-mail (group-art-list)
634 "Called narrowed to an article."
635 (let* (save-list group-art
)
636 (goto-char (point-min))
637 ;; The From line may have been quoted by movemail.
638 (when (looking-at ">From")
640 ;; This might come from somewhere else.
641 (unless (looking-at "From ")
642 (insert "From nobody " (current-time-string) "\n")
643 (goto-char (point-min)))
644 ;; Quote all "From " lines in the article.
646 (let (case-fold-search)
647 (while (re-search-forward "^From " nil t
)
650 (setq save-list group-art-list
)
651 (nnmail-insert-lines)
652 (nnmail-insert-xref group-art-list
)
653 (run-hooks 'nnmail-prepare-save-mail-hook
)
654 (run-hooks 'nnfolder-prepare-save-mail-hook
)
656 ;; Insert the mail into each of the destination groups.
657 (while (setq group-art
(pop group-art-list
))
658 ;; Kill any previous newsgroup markers.
659 (goto-char (point-min))
660 (if (search-forward "\n\n" nil t
)
662 (goto-char (point-max)))
663 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
664 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
666 ;; Insert the new newsgroup marker.
667 (nnfolder-insert-newsgroup-line group-art
)
670 (let ((beg (point-min))
672 (obuf (current-buffer)))
673 (nnfolder-possibly-change-folder (car group-art
))
674 (let ((buffer-read-only nil
))
675 (nnfolder-normalize-buffer)
676 (insert-buffer-substring obuf beg end
)))))
678 ;; Did we save it anywhere?
681 (defun nnfolder-normalize-buffer ()
682 "Make sure there are two newlines at the end of the buffer."
683 (goto-char (point-max))
684 (skip-chars-backward "\n")
685 (delete-region (point) (point-max))
688 (defun nnfolder-insert-newsgroup-line (group-art)
690 (goto-char (point-min))
691 (unless (search-forward "\n\n" nil t
)
692 (goto-char (point-max))
695 (insert (format (concat nnfolder-article-marker
"%d %s\n")
696 (cdr group-art
) (current-time-string)))))
698 (defun nnfolder-active-number (group)
699 ;; Find the next article number in GROUP.
700 (let ((active (cadr (assoc group nnfolder-group-alist
))))
702 (setcdr active
(1+ (cdr active
)))
703 ;; This group is new, so we create a new entry for it.
704 ;; This might be a bit naughty... creating groups on the drop of
705 ;; a hat, but I don't know...
706 (push (list group
(setq active
(cons 1 1)))
707 nnfolder-group-alist
))
710 (defun nnfolder-possibly-change-folder (group)
711 (let ((inf (assoc group nnfolder-buffer-alist
)))
713 (gnus-buffer-live-p (cadr inf
)))
714 (set-buffer (cadr inf
))
716 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
717 (when nnfolder-group-alist
718 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
719 (push (list group
(nnfolder-read-folder group
))
720 nnfolder-buffer-alist
))))
722 ;; This method has a problem if you've accidentally let the active list get
723 ;; out of sync with the files. This could happen, say, if you've
724 ;; accidentally gotten new mail with something other than Gnus (but why
725 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
726 ;; processing the file, ready to add new X-Gnus article number markers, and
727 ;; we'll run across a message with no ID yet - the active list _may_not_ be
730 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
731 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
732 ;; rest of the message looking for any more, possibly higher IDs. We'll
733 ;; assume the maximum that we find is the highest active. Note that this
734 ;; shouldn't cost us much extra time at all, but will be a lot less
735 ;; vulnerable to glitches between the mbox and the active file.
737 (defun nnfolder-read-folder (group)
738 (let* ((file (nnfolder-group-pathname group
))
740 (let ((nnheader-file-coding-system
741 nnfolder-file-coding-system
))
742 (nnheader-find-file-noselect file
)))))
743 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
744 (nth 5 (file-attributes file
)))
745 ;; This looks up-to-date, so we don't do any scanning.
746 (if (file-exists-p file
)
748 (push (list group buffer
) nnfolder-buffer-alist
)
749 (set-buffer-modified-p t
)
750 (nnfolder-save-buffer))
751 ;; Parse the damn thing.
753 (goto-char (point-min))
754 ;; Remove any blank lines at the start.
755 (while (eq (following-char) ?
\n)
757 (nnmail-activate 'nnfolder
)
759 (let ((delim "^From ")
760 (marker (concat "\n" nnfolder-article-marker
))
762 (active (or (cadr (assoc group nnfolder-group-alist
))
764 (scantime (assoc group nnfolder-scantime-alist
))
766 maxid start end newscantime
768 (buffer-disable-undo)
769 (setq maxid
(cdr active
))
770 (goto-char (point-min))
772 ;; Anytime the active number is 1 or 0, it is suspect. In that
773 ;; case, search the file manually to find the active number. Or,
774 ;; of course, if we're being paranoid. (This would also be the
775 ;; place to build other lists from the header markers, such as
776 ;; expunge lists, etc., if we ever desired to abandon the active
777 ;; file entirely for mboxes.)
778 (when (or nnfolder-ignore-active-file
780 (while (and (search-forward marker nil t
)
781 (re-search-forward number nil t
))
782 (let ((newnum (string-to-number (match-string 0))))
783 (if (nnmail-within-headers-p)
784 (setq maxid
(max maxid newnum
)
785 minid
(min minid newnum
)))))
786 (setcar active
(max 1 (min minid maxid
)))
787 (setcdr active
(max maxid
(cdr active
)))
788 (goto-char (point-min)))
790 ;; As long as we trust that the user will only insert unmarked mail
791 ;; at the end, go to the end and search backwards for the last
792 ;; marker. Find the start of that message, and begin to search for
793 ;; unmarked messages from there.
794 (when (not (or nnfolder-distrust-mbox
796 (goto-char (point-max))
797 (unless (re-search-backward marker nil t
)
798 (goto-char (point-min)))
799 (when (nnmail-search-unix-mail-delim)
800 (goto-char (point-min))))
802 ;; Keep track of the active number on our own, and insert it back
803 ;; into the active list when we're done. Also, prime the pump to
804 ;; cut down on the number of searches we do.
805 (unless (nnmail-search-unix-mail-delim)
806 (goto-char (point-max)))
807 (setq end
(point-marker))
808 (while (not (= end
(point-max)))
809 (setq start
(marker-position end
))
811 ;; There may be more than one "From " line, so we skip past
813 (while (looking-at delim
)
815 (set-marker end
(if (nnmail-search-unix-mail-delim)
819 (when (not (search-forward marker end t
))
820 (narrow-to-region start end
)
821 (nnmail-insert-lines)
822 (nnfolder-insert-newsgroup-line
823 (cons nil
(nnfolder-active-number nnfolder-current-group
)))
827 ;; Make absolutely sure that the active list reflects reality!
828 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
829 ;; Set the scantime for this group.
830 (setq newscantime
(visited-file-modtime))
832 (setcdr scantime
(list newscantime
))
833 (push (list nnfolder-current-group newscantime
)
834 nnfolder-scantime-alist
))
835 (current-buffer))))))
838 (defun nnfolder-generate-active-file ()
839 "Look for mbox folders in the nnfolder directory and make them into groups.
840 This command does not work if you use short group names."
842 (nnmail-activate 'nnfolder
)
843 (let ((files (directory-files nnfolder-directory
))
845 (while (setq file
(pop files
))
846 (when (and (not (backup-file-name-p file
))
847 (message-mail-file-mbox-p
848 (nnheader-concat nnfolder-directory file
)))
849 (let ((oldgroup (assoc file nnfolder-group-alist
)))
851 (nnheader-message 5 "Refreshing group %s..." file
)
852 (nnheader-message 5 "Adding group %s..." file
))
854 (setq nnfolder-group-alist
855 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
856 (push (list file
(cons 1 0)) nnfolder-group-alist
)
857 (nnfolder-possibly-change-folder file
)
858 (nnfolder-possibly-change-group file
)
859 (nnfolder-close-group file
))))
860 (nnheader-message 5 "")))
862 (defun nnfolder-group-pathname (group)
863 "Make pathname for GROUP."
865 (mm-encode-coding-string group nnmail-pathname-coding-system
))
866 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
867 ;; If this file exists, we use it directly.
868 (if (or nnmail-use-long-file-names
869 (file-exists-p (concat dir group
)))
871 ;; If not, we translate dots into slashes.
872 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
874 (defun nnfolder-save-buffer ()
876 (when (buffer-modified-p)
877 (run-hooks 'nnfolder-save-buffer-hook
)
878 (gnus-make-directory (file-name-directory (buffer-file-name)))
879 (let ((coding-system-for-write
880 (or nnfolder-file-coding-system-for-write
881 nnfolder-file-coding-system
)))
884 (defun nnfolder-save-active (group-alist active-file
)
885 (let ((nnmail-active-file-coding-system
886 (or nnfolder-active-file-coding-system-for-write
887 nnfolder-active-file-coding-system
)))
888 (nnmail-save-active group-alist active-file
)))
892 ;;; nnfolder.el ends here