1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
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
))
39 (nnoo-declare nnfolder
)
41 (defvoo nnfolder-directory
(expand-file-name message-directory
)
42 "The name of the nnfolder directory.")
44 (defvoo nnfolder-active-file
45 (nnheader-concat nnfolder-directory
"active")
46 "The name of the active file.")
48 ;; I renamed this variable to something more in keeping with the general GNU
51 (defvoo nnfolder-ignore-active-file nil
52 "If non-nil, the active file is ignored.
53 This causes nnfolder to do some extra work in order to determine the
54 true active ranges of an mbox file. Note that the active file is
55 still saved, but its values are not used. This costs some extra time
56 when scanning an mbox when opening it.")
58 (defvoo nnfolder-distrust-mbox nil
59 "If non-nil, the folder will be distrusted.
60 This means that nnfolder will not trust the user with respect to
61 inserting unaccounted for mail in the middle of an mbox file. This
62 can greatly slow down scans, which now must scan the entire file for
63 unmarked messages. When nil, scans occur forward from the last marked
64 message, a huge time saver for large mailboxes.")
66 (defvoo nnfolder-newsgroups-file
67 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
68 "Mail newsgroups description file.")
70 (defvoo nnfolder-get-new-mail t
71 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
73 (defvoo nnfolder-prepare-save-mail-hook nil
74 "Hook run narrowed to an article before saving.")
76 (defvoo nnfolder-save-buffer-hook nil
77 "Hook run before saving the nnfolder mbox buffer.")
79 (defvoo nnfolder-inhibit-expiry nil
80 "If non-nil, inhibit expiry.")
84 (defconst nnfolder-version
"nnfolder 1.0"
87 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
88 "String used to demarcate what the article number for a message is.")
90 (defvoo nnfolder-current-group nil
)
91 (defvoo nnfolder-current-buffer nil
)
92 (defvoo nnfolder-status-string
"")
93 (defvoo nnfolder-group-alist nil
)
94 (defvoo nnfolder-buffer-alist nil
)
95 (defvoo nnfolder-scantime-alist nil
)
96 (defvoo nnfolder-active-timestamp nil
)
97 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
98 (defvoo nnfolder-active-file-coding-system-for-write
99 nnmail-active-file-coding-system
)
100 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
101 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
102 "Coding system for save nnfolder file.
103 If nil, `nnfolder-file-coding-system' is used.")
107 ;;; Interface functions
109 (nnoo-define-basics nnfolder
)
111 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
113 (set-buffer nntp-server-buffer
)
115 (let (article start stop
)
116 (nnfolder-possibly-change-group group server
)
117 (when nnfolder-current-buffer
118 (set-buffer nnfolder-current-buffer
)
119 (goto-char (point-min))
120 (if (stringp (car articles
))
122 (while (setq article
(pop articles
))
123 (set-buffer nnfolder-current-buffer
)
124 (when (nnfolder-goto-article article
)
126 (setq stop
(if (search-forward "\n\n" nil t
)
129 (set-buffer nntp-server-buffer
)
130 (insert (format "221 %d Article retrieved.\n" article
))
131 (insert-buffer-substring nnfolder-current-buffer start stop
)
132 (goto-char (point-max))
135 (set-buffer nntp-server-buffer
)
136 (nnheader-fold-continuation-lines)
139 (deffoo nnfolder-open-server
(server &optional defs
)
140 (nnoo-change-server 'nnfolder server defs
)
141 (nnmail-activate 'nnfolder t
)
142 (gnus-make-directory nnfolder-directory
)
144 ((not (file-exists-p nnfolder-directory
))
145 (nnfolder-close-server)
146 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
148 ((not (file-directory-p (file-truename nnfolder-directory
)))
149 (nnfolder-close-server)
150 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
152 (nnmail-activate 'nnfolder
)
153 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
154 server nnfolder-directory
)
157 (deffoo nnfolder-request-close
()
158 (let ((alist nnfolder-buffer-alist
))
160 (nnfolder-close-group (caar alist
) nil t
)
161 (setq alist
(cdr alist
))))
162 (nnoo-close-server 'nnfolder
)
163 (setq nnfolder-buffer-alist nil
164 nnfolder-group-alist nil
))
166 (deffoo nnfolder-request-article
(article &optional group server buffer
)
167 (nnfolder-possibly-change-group group server
)
169 (set-buffer nnfolder-current-buffer
)
170 (goto-char (point-min))
171 (when (nnfolder-goto-article article
)
175 (unless (and (nnmail-search-unix-mail-delim)
177 (goto-char (point-max)))
179 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
180 (set-buffer nntp-server-buffer
)
182 (insert-buffer-substring nnfolder-current-buffer start stop
)
183 (goto-char (point-min))
184 (while (looking-at "From ")
186 (insert "X-From-Line: ")
188 (if (numberp article
)
189 (cons nnfolder-current-group article
)
190 (goto-char (point-min))
191 (cons nnfolder-current-group
192 (if (search-forward (concat "\n" nnfolder-article-marker
)
196 (point) (progn (end-of-line) (point))))
199 (deffoo nnfolder-request-group
(group &optional server dont-check
)
200 (nnfolder-possibly-change-group group server t
)
202 (if (not (assoc group nnfolder-group-alist
))
203 (nnheader-report 'nnfolder
"No such group: %s" group
)
206 (nnheader-report 'nnfolder
"Selected group %s" group
)
208 (let* ((active (assoc group nnfolder-group-alist
))
210 (range (cadr active
)))
213 (nnheader-report 'nnfolder
"No such group: %s" group
))
214 ((null nnfolder-current-group
)
215 (nnheader-report 'nnfolder
"Empty group: %s" group
))
217 (nnheader-report 'nnfolder
"Selected group %s" group
)
218 (nnheader-insert "211 %d %d %d %s\n"
219 (1+ (- (cdr range
) (car range
)))
220 (car range
) (cdr range
) group
))))))))
222 (deffoo nnfolder-request-scan
(&optional group server
)
223 (nnfolder-possibly-change-group nil server
)
224 (when nnfolder-get-new-mail
225 (nnfolder-possibly-change-group group server
)
229 (let ((bufs nnfolder-buffer-alist
))
232 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
233 (setq nnfolder-buffer-alist
234 (delq (car bufs
) nnfolder-buffer-alist
))
235 (set-buffer (nth 1 (car bufs
)))
236 (nnfolder-save-buffer)
237 (kill-buffer (current-buffer)))
238 (setq bufs
(cdr bufs
))))))
242 ;; Don't close the buffer if we're not shutting down the server. This way,
243 ;; we can keep the buffer in the group buffer cache, and not have to grovel
244 ;; over the buffer again unless we add new mail to it or modify it in some
247 (deffoo nnfolder-close-group
(group &optional server force
)
248 ;; Make sure we _had_ the group open.
249 (when (or (assoc group nnfolder-buffer-alist
)
250 (equal group nnfolder-current-group
))
251 (let ((inf (assoc group nnfolder-buffer-alist
)))
253 (when (and nnfolder-current-group
254 nnfolder-current-buffer
)
255 (push (list nnfolder-current-group nnfolder-current-buffer
)
256 nnfolder-buffer-alist
))
257 (setq nnfolder-buffer-alist
258 (delq inf nnfolder-buffer-alist
))
259 (setq nnfolder-current-buffer
(cadr inf
)
260 nnfolder-current-group
(car inf
))))
261 (when (and nnfolder-current-buffer
262 (buffer-name nnfolder-current-buffer
))
264 (set-buffer nnfolder-current-buffer
)
265 ;; If the buffer was modified, write the file out now.
266 (nnfolder-save-buffer)
267 ;; If we're shutting the server down, we need to kill the
268 ;; buffer and remove it from the open buffer list. Or, of
269 ;; course, if we're trying to minimize our space impact.
270 (kill-buffer (current-buffer))
271 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
272 nnfolder-buffer-alist
)))))
273 (setq nnfolder-current-group nil
274 nnfolder-current-buffer nil
)
277 (deffoo nnfolder-request-create-group
(group &optional server args
)
278 (nnfolder-possibly-change-group nil server
)
279 (nnmail-activate 'nnfolder
)
281 (unless (assoc group nnfolder-group-alist
)
282 (push (list group
(cons 1 0)) nnfolder-group-alist
)
283 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
284 (nnfolder-read-folder group
)))
287 (deffoo nnfolder-request-list
(&optional server
)
288 (nnfolder-possibly-change-group nil server
)
290 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
291 (nnmail-find-file nnfolder-active-file
)
292 (setq nnfolder-group-alist
(nnmail-get-active)))
295 (deffoo nnfolder-request-newgroups
(date &optional server
)
296 (nnfolder-possibly-change-group nil server
)
297 (nnfolder-request-list server
))
299 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
300 (nnfolder-possibly-change-group nil server
)
302 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
303 (nnmail-find-file nnfolder-newsgroups-file
))))
305 ;; Return a list consisting of all article numbers existing in the
308 (defun nnfolder-existing-articles ()
310 (when nnfolder-current-buffer
311 (set-buffer nnfolder-current-buffer
)
312 (goto-char (point-min))
313 (let ((marker (concat "\n" nnfolder-article-marker
))
317 (while (and (search-forward marker nil t
)
318 (re-search-forward number nil t
))
319 (let ((newnum (string-to-number (match-string 0))))
320 (if (nnmail-within-headers-p)
321 (push newnum numbers
))))
324 (deffoo nnfolder-request-expire-articles
325 (articles newsgroup
&optional server force
)
326 (nnfolder-possibly-change-group newsgroup server
)
328 ;; The articles we have deleted so far.
329 (deleted-articles nil
)
330 ;; The articles that really exist and will
331 ;; be expired if they are old enough.
333 (gnus-intersection articles
(nnfolder-existing-articles))))
334 (nnmail-activate 'nnfolder
)
337 (set-buffer nnfolder-current-buffer
)
338 ;; Since messages are sorted in arrival order and expired in the
339 ;; same order, we can stop as soon as we find a message that is
341 (while (and maybe-expirable is-old
)
342 (goto-char (point-min))
343 (when (and (nnfolder-goto-article (car maybe-expirable
))
344 (search-forward (concat "\n" nnfolder-article-marker
)
348 (nnmail-expired-article-p
351 (point) (progn (end-of-line) (point)))
352 force nnfolder-inhibit-expiry
))
353 (unless (eq nnmail-expiry-target
'delete
)
355 (nnfolder-request-article (car maybe-expirable
)
356 newsgroup server
(current-buffer))
357 (let ((nnml-current-directory nil
))
358 (nnmail-expiry-target-group
359 nnmail-expiry-target newsgroup
))))
360 (nnheader-message 5 "Deleting article %d in %s..."
361 (car maybe-expirable
) newsgroup
)
362 (nnfolder-delete-mail)
363 ;; Must remember which articles were actually deleted
364 (push (car maybe-expirable
) deleted-articles
)))
365 (setq maybe-expirable
(cdr maybe-expirable
)))
366 (unless nnfolder-inhibit-expiry
367 (nnheader-message 5 "Deleting articles...done"))
368 (nnfolder-save-buffer)
369 (nnfolder-adjust-min-active newsgroup
)
370 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
371 (gnus-sorted-complement articles
(nreverse deleted-articles
)))))
373 (deffoo nnfolder-request-move-article
(article group server
374 accept-form
&optional last
)
376 (let ((buf (get-buffer-create " *nnfolder move*"))
379 (nnfolder-request-article article group server
)
383 (insert-buffer-substring nntp-server-buffer
)
384 (goto-char (point-min))
385 (while (re-search-forward
386 (concat "^" nnfolder-article-marker
)
387 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
389 (delete-region (progn (beginning-of-line) (point))
390 (progn (forward-line 1) (point))))
391 (setq result
(eval accept-form
))
395 (nnfolder-possibly-change-group group server
)
396 (set-buffer nnfolder-current-buffer
)
397 (goto-char (point-min))
398 (when (nnfolder-goto-article article
)
399 (nnfolder-delete-mail))
401 (nnfolder-save-buffer)
402 (nnfolder-adjust-min-active group
)
403 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
406 (deffoo nnfolder-request-accept-article
(group &optional server last
)
408 (nnfolder-possibly-change-group group server
)
409 (nnmail-check-syntax)
410 (let ((buf (current-buffer))
412 (goto-char (point-min))
413 (when (looking-at "X-From-Line: ")
414 (replace-match "From "))
416 (nnfolder-request-list)
419 (goto-char (point-min))
420 (if (search-forward "\n\n" nil t
)
422 (goto-char (point-max)))
423 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
424 (delete-region (point) (progn (forward-line 1) (point))))
425 (when nnmail-cache-accepted-message-ids
426 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
427 (setq result
(if (stringp group
)
428 (list (cons group
(nnfolder-active-number group
)))
430 (nnmail-article-group 'nnfolder-active-number
))))
431 (if (and (null result
)
432 (yes-or-no-p "Moved to `junk' group; delete article? "))
435 (car (nnfolder-save-mail result
)))))
438 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
439 (nnfolder-save-buffer)
440 (when nnmail-cache-accepted-message-ids
441 (nnmail-cache-close)))))
442 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
444 (nnheader-report 'nnfolder
"Couldn't store article"))
447 (deffoo nnfolder-request-replace-article
(article group buffer
)
448 (nnfolder-possibly-change-group group
)
451 (goto-char (point-min))
453 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t
)
454 (setq xfrom
(match-string 1))
456 (goto-char (point-min))
458 (insert "From " xfrom
"\n")
459 (unless (looking-at "From ")
460 (insert "From nobody " (current-time-string) "\n"))))
461 (nnfolder-normalize-buffer)
462 (set-buffer nnfolder-current-buffer
)
463 (goto-char (point-min))
464 (if (not (nnfolder-goto-article article
))
466 (nnfolder-delete-mail)
467 (insert-buffer-substring buffer
)
468 (nnfolder-save-buffer)
471 (deffoo nnfolder-request-delete-group
(group &optional force server
)
472 (nnfolder-close-group group server t
)
473 ;; Delete all articles in GROUP.
475 () ; Don't delete the articles.
476 ;; Delete the file that holds the group.
478 (delete-file (nnfolder-group-pathname group
))))
479 ;; Remove the group from all structures.
480 (setq nnfolder-group-alist
481 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
482 nnfolder-current-group nil
483 nnfolder-current-buffer nil
)
484 ;; Save the active file.
485 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
488 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
489 (nnfolder-possibly-change-group group server
)
491 (set-buffer nnfolder-current-buffer
)
492 (and (file-writable-p buffer-file-name
)
496 (let ((new-file (nnfolder-group-pathname new-name
)))
497 (gnus-make-directory (file-name-directory new-file
))
500 ;; That went ok, so we change the internal structures.
501 (let ((entry (assoc group nnfolder-group-alist
)))
502 (and entry
(setcar entry new-name
))
503 (setq nnfolder-current-buffer nil
504 nnfolder-current-group nil
)
505 ;; Save the new group alist.
506 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
507 ;; We kill the buffer instead of renaming it and stuff.
508 (kill-buffer (current-buffer))
511 (defun nnfolder-request-regenerate (server)
512 (nnfolder-possibly-change-group nil server
)
513 (nnfolder-generate-active-file)
517 ;;; Internal functions.
519 (defun nnfolder-adjust-min-active (group)
520 ;; Find the lowest active article in this group.
521 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
522 (marker (concat "\n" nnfolder-article-marker
))
524 (activemin (cdr active
)))
526 (set-buffer nnfolder-current-buffer
)
527 (goto-char (point-min))
528 (while (and (search-forward marker nil t
)
529 (re-search-forward number nil t
))
530 (let ((newnum (string-to-number (match-string 0))))
531 (if (nnmail-within-headers-p)
532 (setq activemin
(min activemin newnum
)))))
533 (setcar active activemin
))))
535 (defun nnfolder-article-string (article)
536 (if (numberp article
)
537 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
538 (concat "\nMessage-ID: " article
)))
540 (defun nnfolder-goto-article (article)
541 "Place point at the start of the headers of ARTICLE.
542 ARTICLE can be an article number or a Message-ID.
543 Returns t if successful, nil otherwise."
544 (let ((art-string (nnfolder-article-string article
))
546 ;; It is likely that we are at or before the delimiter line.
547 ;; We therefore go to the end of the previous line, and start
548 ;; searching from there.
553 ;; First search forward.
554 (while (and (setq found
(search-forward art-string nil t
))
555 (not (nnmail-within-headers-p))))
556 ;; If unsuccessful, search backward from where we started,
559 (while (and (setq found
(search-backward art-string nil t
))
560 (not (nnmail-within-headers-p)))))
562 (nnmail-search-unix-mail-delim-backward))))
564 (defun nnfolder-delete-mail (&optional leave-delim
)
565 "Delete the message that point is in.
566 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
567 deleted. Point is left where the deleted region was."
571 ;; In case point is at the beginning of the message already.
573 (nnmail-search-unix-mail-delim-backward)
574 (if leave-delim
(progn (forward-line 1) (point))
578 (if (nnmail-search-unix-mail-delim)
581 (run-hooks 'nnfolder-delete-mail-hook
)
582 (delete-region (point-min) (point-max))))
584 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
587 (not (nnfolder-server-opened server
)))
588 (nnfolder-open-server server
))
589 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
590 (setq nnfolder-current-buffer nil
591 nnfolder-current-group nil
))
594 (not (equal group nnfolder-current-group
)))
595 (let ((file-name-coding-system nnmail-pathname-coding-system
))
596 (nnmail-activate 'nnfolder
)
597 (when (and (not (assoc group nnfolder-group-alist
))
599 (nnfolder-group-pathname group
))))
600 ;; The group doesn't exist, so we create a new entry for it.
601 (push (list group
(cons 1 0)) nnfolder-group-alist
)
602 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
605 (setq nnfolder-current-group group
606 nnfolder-current-buffer nil
)
608 ;; If we have to change groups, see if we don't already have the
609 ;; folder in memory. If we do, verify the modtime and destroy
610 ;; the folder if needed so we can rescan it.
611 (setq nnfolder-current-buffer
612 (nth 1 (assoc group nnfolder-buffer-alist
)))
614 ;; If the buffer is not live, make sure it isn't in the alist. If it
615 ;; is live, verify that nobody else has touched the file since last
617 (when (and nnfolder-current-buffer
618 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
619 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
620 nnfolder-current-buffer nil
))
622 (setq nnfolder-current-group group
)
624 (when (or (not nnfolder-current-buffer
)
625 (not (verify-visited-file-modtime
626 nnfolder-current-buffer
)))
628 (setq file
(nnfolder-group-pathname group
))
629 ;; See whether we need to create the new file.
630 (unless (file-exists-p file
)
631 (gnus-make-directory (file-name-directory file
))
632 (let ((nnmail-file-coding-system
633 (or nnfolder-file-coding-system-for-write
634 nnfolder-file-coding-system-for-write
)))
635 (nnmail-write-region (point-min) (point-min)
637 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
638 (set-buffer nnfolder-current-buffer
)
639 (push (list group nnfolder-current-buffer
)
640 nnfolder-buffer-alist
)))))))))
642 (defun nnfolder-save-mail (group-art-list)
643 "Called narrowed to an article."
644 (let* (save-list group-art
)
645 (goto-char (point-min))
646 ;; The From line may have been quoted by movemail.
647 (when (looking-at ">From")
649 ;; This might come from somewhere else.
650 (unless (looking-at "From ")
651 (insert "From nobody " (current-time-string) "\n")
652 (goto-char (point-min)))
653 ;; Quote all "From " lines in the article.
655 (let (case-fold-search)
656 (while (re-search-forward "^From " nil t
)
659 (setq save-list group-art-list
)
660 (nnmail-insert-lines)
661 (nnmail-insert-xref group-art-list
)
662 (run-hooks 'nnmail-prepare-save-mail-hook
)
663 (run-hooks 'nnfolder-prepare-save-mail-hook
)
665 ;; Insert the mail into each of the destination groups.
666 (while (setq group-art
(pop group-art-list
))
667 ;; Kill any previous newsgroup markers.
668 (goto-char (point-min))
669 (if (search-forward "\n\n" nil t
)
671 (goto-char (point-max)))
672 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
673 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
675 ;; Insert the new newsgroup marker.
676 (nnfolder-insert-newsgroup-line group-art
)
679 (let ((beg (point-min))
681 (obuf (current-buffer)))
682 (nnfolder-possibly-change-folder (car group-art
))
683 (let ((buffer-read-only nil
))
684 (nnfolder-normalize-buffer)
685 (insert-buffer-substring obuf beg end
)))))
687 ;; Did we save it anywhere?
690 (defun nnfolder-normalize-buffer ()
691 "Make sure there are two newlines at the end of the buffer."
692 (goto-char (point-max))
693 (skip-chars-backward "\n")
694 (delete-region (point) (point-max))
697 (defun nnfolder-insert-newsgroup-line (group-art)
699 (goto-char (point-min))
700 (unless (search-forward "\n\n" nil t
)
701 (goto-char (point-max))
704 (insert (format (concat nnfolder-article-marker
"%d %s\n")
705 (cdr group-art
) (current-time-string)))))
707 (defun nnfolder-active-number (group)
708 ;; Find the next article number in GROUP.
709 (let ((active (cadr (assoc group nnfolder-group-alist
))))
711 (setcdr active
(1+ (cdr active
)))
712 ;; This group is new, so we create a new entry for it.
713 ;; This might be a bit naughty... creating groups on the drop of
714 ;; a hat, but I don't know...
715 (push (list group
(setq active
(cons 1 1)))
716 nnfolder-group-alist
))
719 (defun nnfolder-possibly-change-folder (group)
720 (let ((inf (assoc group nnfolder-buffer-alist
)))
722 (gnus-buffer-live-p (cadr inf
)))
723 (set-buffer (cadr inf
))
725 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
726 (when nnfolder-group-alist
727 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
728 (push (list group
(nnfolder-read-folder group
))
729 nnfolder-buffer-alist
))))
731 ;; This method has a problem if you've accidentally let the active list get
732 ;; out of sync with the files. This could happen, say, if you've
733 ;; accidentally gotten new mail with something other than Gnus (but why
734 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
735 ;; processing the file, ready to add new X-Gnus article number markers, and
736 ;; we'll run across a message with no ID yet - the active list _may_not_ be
739 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
740 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
741 ;; rest of the message looking for any more, possibly higher IDs. We'll
742 ;; assume the maximum that we find is the highest active. Note that this
743 ;; shouldn't cost us much extra time at all, but will be a lot less
744 ;; vulnerable to glitches between the mbox and the active file.
746 (defun nnfolder-read-folder (group)
747 (let* ((file (nnfolder-group-pathname group
))
749 (let ((nnheader-file-coding-system
750 nnfolder-file-coding-system
))
751 (nnheader-find-file-noselect file
)))))
752 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
753 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
754 (nth 5 (file-attributes file
)))
755 ;; This looks up-to-date, so we don't do any scanning.
756 (if (file-exists-p file
)
758 (push (list group buffer
) nnfolder-buffer-alist
)
759 (set-buffer-modified-p t
)
760 (nnfolder-save-buffer))
761 ;; Parse the damn thing.
763 (goto-char (point-min))
764 ;; Remove any blank lines at the start.
765 (while (eq (following-char) ?
\n)
767 (nnmail-activate 'nnfolder
)
769 (let ((delim "^From ")
770 (marker (concat "\n" nnfolder-article-marker
))
772 (active (or (cadr (assoc group nnfolder-group-alist
))
774 (scantime (assoc group nnfolder-scantime-alist
))
776 maxid start end newscantime
778 (buffer-disable-undo)
779 (setq maxid
(cdr active
))
780 (goto-char (point-min))
782 ;; Anytime the active number is 1 or 0, it is suspect. In that
783 ;; case, search the file manually to find the active number. Or,
784 ;; of course, if we're being paranoid. (This would also be the
785 ;; place to build other lists from the header markers, such as
786 ;; expunge lists, etc., if we ever desired to abandon the active
787 ;; file entirely for mboxes.)
788 (when (or nnfolder-ignore-active-file
790 (while (and (search-forward marker nil t
)
791 (re-search-forward number nil t
))
792 (let ((newnum (string-to-number (match-string 0))))
793 (if (nnmail-within-headers-p)
794 (setq maxid
(max maxid newnum
)
795 minid
(min minid newnum
)))))
796 (setcar active
(max 1 (min minid maxid
)))
797 (setcdr active
(max maxid
(cdr active
)))
798 (goto-char (point-min)))
800 ;; As long as we trust that the user will only insert unmarked mail
801 ;; at the end, go to the end and search backwards for the last
802 ;; marker. Find the start of that message, and begin to search for
803 ;; unmarked messages from there.
804 (when (not (or nnfolder-distrust-mbox
806 (goto-char (point-max))
807 (unless (re-search-backward marker nil t
)
808 (goto-char (point-min)))
809 (when (nnmail-search-unix-mail-delim)
810 (goto-char (point-min))))
812 ;; Keep track of the active number on our own, and insert it back
813 ;; into the active list when we're done. Also, prime the pump to
814 ;; cut down on the number of searches we do.
815 (unless (nnmail-search-unix-mail-delim)
816 (goto-char (point-max)))
817 (setq end
(point-marker))
818 (while (not (= end
(point-max)))
819 (setq start
(marker-position end
))
821 ;; There may be more than one "From " line, so we skip past
823 (while (looking-at delim
)
825 (set-marker end
(if (nnmail-search-unix-mail-delim)
829 (when (not (search-forward marker end t
))
830 (narrow-to-region start end
)
831 (nnmail-insert-lines)
832 (nnfolder-insert-newsgroup-line
833 (cons nil
(nnfolder-active-number nnfolder-current-group
)))
837 ;; Make absolutely sure that the active list reflects reality!
838 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
839 ;; Set the scantime for this group.
840 (setq newscantime
(visited-file-modtime))
842 (setcdr scantime
(list newscantime
))
843 (push (list nnfolder-current-group newscantime
)
844 nnfolder-scantime-alist
))
845 (current-buffer))))))
848 (defun nnfolder-generate-active-file ()
849 "Look for mbox folders in the nnfolder directory and make them into groups.
850 This command does not work if you use short group names."
852 (nnmail-activate 'nnfolder
)
853 (let ((files (directory-files nnfolder-directory
))
855 (while (setq file
(pop files
))
856 (when (and (not (backup-file-name-p file
))
857 (message-mail-file-mbox-p
858 (nnheader-concat nnfolder-directory file
)))
859 (let ((oldgroup (assoc file nnfolder-group-alist
)))
861 (nnheader-message 5 "Refreshing group %s..." file
)
862 (nnheader-message 5 "Adding group %s..." file
))
864 (setq nnfolder-group-alist
865 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
866 (push (list file
(cons 1 0)) nnfolder-group-alist
)
867 (nnfolder-possibly-change-folder file
)
868 (nnfolder-possibly-change-group file
)
869 (nnfolder-close-group file
))))
870 (nnheader-message 5 "")))
872 (defun nnfolder-group-pathname (group)
873 "Make pathname for GROUP."
875 (mm-encode-coding-string group nnmail-pathname-coding-system
))
876 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
877 ;; If this file exists, we use it directly.
878 (if (or nnmail-use-long-file-names
879 (file-exists-p (concat dir group
)))
881 ;; If not, we translate dots into slashes.
882 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
884 (defun nnfolder-save-buffer ()
886 (when (buffer-modified-p)
887 (run-hooks 'nnfolder-save-buffer-hook
)
888 (gnus-make-directory (file-name-directory (buffer-file-name)))
889 (let ((coding-system-for-write
890 (or nnfolder-file-coding-system-for-write
891 nnfolder-file-coding-system
)))
894 (defun nnfolder-save-active (group-alist active-file
)
895 (let ((nnmail-active-file-coding-system
896 (or nnfolder-active-file-coding-system-for-write
897 nnfolder-active-file-coding-system
)))
898 (nnmail-save-active group-alist active-file
)))
902 ;;; nnfolder.el ends here