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: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;; Scott Byer <byer@mv.us.adobe.com>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
37 (eval-when-compile (require 'cl))
43 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
44 (autoload 'gnus-intersection "gnus-range"))
46 (nnoo-declare nnfolder)
48 (defvoo nnfolder-directory (expand-file-name message-directory)
49 "The name of the nnfolder directory.
51 This variable is a virtual server slot. See the Gnus manual for details.")
53 (defvoo nnfolder-nov-directory nil
54 "The name of the nnfolder NOV directory.
55 If nil, `nnfolder-directory' is used.")
57 (defvoo nnfolder-marks-directory nil
58 "The name of the nnfolder MARKS directory.
59 If nil, `nnfolder-directory' is used.")
61 (defvoo nnfolder-active-file
62 (nnheader-concat nnfolder-directory "active")
63 "The name of the active file.
65 This variable is a virtual server slot. See the Gnus manual for details.")
67 ;; I renamed this variable to something more in keeping with the general GNU
70 (defvoo nnfolder-ignore-active-file nil
71 "If non-nil, the active file is ignored.
72 This causes nnfolder to do some extra work in order to determine the
73 true active ranges of an mbox file. Note that the active file is
74 still saved, but its values are not used. This costs some extra time
75 when scanning an mbox when opening it.")
77 (defvoo nnfolder-distrust-mbox nil
78 "If non-nil, the folder will be distrusted.
79 This means that nnfolder will not trust the user with respect to
80 inserting unaccounted for mail in the middle of an mbox file. This
81 can greatly slow down scans, which now must scan the entire file for
82 unmarked messages. When nil, scans occur forward from the last marked
83 message, a huge time saver for large mailboxes.")
85 (defvoo nnfolder-newsgroups-file
86 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
87 "Mail newsgroups description file.
89 This variable is a virtual server slot. See the Gnus manual for details.")
91 (defvoo nnfolder-get-new-mail t
92 "If non-nil, nnfolder will check the incoming mail file and split the mail.
94 This variable is a virtual server slot. See the Gnus manual for details.")
96 (defvoo nnfolder-prepare-save-mail-hook nil
97 "Hook run narrowed to an article before saving.")
99 (defvoo nnfolder-save-buffer-hook nil
100 "Hook run before saving the nnfolder mbox buffer.
102 This variable is a virtual server slot. See the Gnus manual for details.")
105 (defvoo nnfolder-inhibit-expiry nil
106 "If non-nil, inhibit expiry.
108 This variable is a virtual server slot. See the Gnus manual for details.")
112 (defconst nnfolder-version "nnfolder 2.0"
115 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
116 "String used to demarcate what the article number for a message is.")
118 (defvoo nnfolder-current-group nil)
119 (defvoo nnfolder-current-buffer nil)
120 (defvoo nnfolder-status-string "")
121 (defvoo nnfolder-group-alist nil)
122 (defvoo nnfolder-buffer-alist nil)
123 (defvoo nnfolder-scantime-alist nil)
124 (defvoo nnfolder-active-timestamp nil)
125 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
126 (defvoo nnfolder-active-file-coding-system-for-write
127 nnmail-active-file-coding-system)
128 (defvoo nnfolder-file-coding-system mm-text-coding-system)
129 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
130 "Coding system for save nnfolder file.
131 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
133 (defvoo nnfolder-nov-is-evil nil
134 "If non-nil, Gnus will never generate and use nov databases for mail groups.
135 Using nov databases will speed up header fetching considerably.
136 This variable shouldn't be flipped much. If you have, for some reason,
137 set this to t, and want to set it to nil again, you should always run
138 the `nnfolder-generate-active-file' command. The function will go
139 through all nnfolder directories and generate nov databases for them
140 all. This may very well take some time.
142 This variable is a virtual server slot. See the Gnus manual for details.")
144 (defvoo nnfolder-nov-file-suffix ".nov")
146 (defvoo nnfolder-nov-buffer-alist nil)
148 (defvar nnfolder-nov-buffer-file-name nil)
150 (defvoo nnfolder-marks-is-evil nil
151 "If non-nil, Gnus will never generate and use marks file for mail groups.
152 Using marks files makes it possible to backup and restore mail groups
153 separately from `.newsrc.eld'. If you have, for some reason, set
154 this to t, and want to set it to nil again, you should always remove
155 the corresponding marks file (usually base nnfolder file name
156 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
157 the group. Then the marks file will be regenerated properly by Gnus.
159 This variable is a virtual server slot. See the Gnus manual for details.")
161 (defvoo nnfolder-marks nil)
163 (defvoo nnfolder-marks-file-suffix ".mrk")
165 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
169 ;;; Interface functions
171 (nnoo-define-basics nnfolder)
173 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
175 (set-buffer nntp-server-buffer)
177 (let (article start stop)
178 (nnfolder-possibly-change-group group server)
179 (when nnfolder-current-buffer
180 (set-buffer nnfolder-current-buffer)
181 (goto-char (point-min))
182 (if (stringp (car articles))
184 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
186 (setq articles (gnus-sorted-intersection
187 ;; Is ARTICLES sorted?
189 (nnfolder-existing-articles)))
190 (while (setq article (pop articles))
191 (set-buffer nnfolder-current-buffer)
192 (when (nnfolder-goto-article article)
194 (setq stop (if (search-forward "\n\n" nil t)
197 (set-buffer nntp-server-buffer)
198 (insert (format "221 %d Article retrieved.\n" article))
199 (insert-buffer-substring nnfolder-current-buffer start stop)
200 (goto-char (point-max))
202 (set-buffer nntp-server-buffer)
203 (nnheader-fold-continuation-lines)
206 (deffoo nnfolder-open-server (server &optional defs)
207 (nnoo-change-server 'nnfolder server defs)
208 (nnmail-activate 'nnfolder t)
209 (gnus-make-directory nnfolder-directory)
210 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
211 (and nnfolder-nov-directory
212 (gnus-make-directory nnfolder-nov-directory)))
213 (unless nnfolder-marks-is-evil
214 (and nnfolder-marks-directory
215 (gnus-make-directory nnfolder-marks-directory)))
217 ((not (file-exists-p nnfolder-directory))
218 (nnfolder-close-server)
219 (nnheader-report 'nnfolder "Couldn't create directory: %s"
221 ((not (file-directory-p (file-truename nnfolder-directory)))
222 (nnfolder-close-server)
223 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
225 (nnmail-activate 'nnfolder)
226 (nnheader-report 'nnfolder "Opened server %s using directory %s"
227 server nnfolder-directory)
230 (deffoo nnfolder-request-close ()
231 (let ((alist nnfolder-buffer-alist))
233 (nnfolder-close-group (caar alist) nil t)
234 (setq alist (cdr alist))))
235 (nnoo-close-server 'nnfolder)
236 (setq nnfolder-buffer-alist nil
237 nnfolder-group-alist nil))
239 (deffoo nnfolder-request-article (article &optional group server buffer)
240 (nnfolder-possibly-change-group group server)
242 (set-buffer nnfolder-current-buffer)
243 (goto-char (point-min))
244 (when (nnfolder-goto-article article)
248 (unless (and (nnmail-search-unix-mail-delim)
250 (goto-char (point-max)))
252 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
253 (set-buffer nntp-server-buffer)
255 (insert-buffer-substring nnfolder-current-buffer start stop)
256 (goto-char (point-min))
257 (while (looking-at "From ")
259 (insert "X-From-Line: ")
261 (if (numberp article)
262 (cons nnfolder-current-group article)
263 (goto-char (point-min))
264 (cons nnfolder-current-group
265 (if (search-forward (concat "\n" nnfolder-article-marker)
269 (point) (progn (end-of-line) (point))))
272 (deffoo nnfolder-request-group (group &optional server dont-check)
273 (nnfolder-possibly-change-group group server t)
275 (if (not (assoc group nnfolder-group-alist))
276 (nnheader-report 'nnfolder "No such group: %s" group)
279 (nnheader-report 'nnfolder "Selected group %s" group)
281 (let* ((active (assoc group nnfolder-group-alist))
283 (range (cadr active)))
286 (nnheader-report 'nnfolder "No such group: %s" group))
287 ((null nnfolder-current-group)
288 (nnheader-report 'nnfolder "Empty group: %s" group))
290 (nnheader-report 'nnfolder "Selected group %s" group)
291 (nnheader-insert "211 %d %d %d %s\n"
292 (1+ (- (cdr range) (car range)))
293 (car range) (cdr range) group))))))))
295 (deffoo nnfolder-request-scan (&optional group server)
296 (nnfolder-possibly-change-group nil server)
297 (when nnfolder-get-new-mail
298 (nnfolder-possibly-change-group group server)
302 (let ((bufs nnfolder-buffer-alist))
305 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
306 (setq nnfolder-buffer-alist
307 (delq (car bufs) nnfolder-buffer-alist))
308 (set-buffer (nth 1 (car bufs)))
309 (nnfolder-save-buffer)
310 (kill-buffer (current-buffer)))
311 (setq bufs (cdr bufs))))))
315 ;; Don't close the buffer if we're not shutting down the server. This way,
316 ;; we can keep the buffer in the group buffer cache, and not have to grovel
317 ;; over the buffer again unless we add new mail to it or modify it in some
320 (deffoo nnfolder-close-group (group &optional server force)
321 ;; Make sure we _had_ the group open.
322 (when (or (assoc group nnfolder-buffer-alist)
323 (equal group nnfolder-current-group))
324 (let ((inf (assoc group nnfolder-buffer-alist)))
326 (when (and nnfolder-current-group
327 nnfolder-current-buffer)
328 (push (list nnfolder-current-group nnfolder-current-buffer)
329 nnfolder-buffer-alist))
330 (setq nnfolder-buffer-alist
331 (delq inf nnfolder-buffer-alist))
332 (setq nnfolder-current-buffer (cadr inf)
333 nnfolder-current-group (car inf))))
334 (when (and nnfolder-current-buffer
335 (buffer-name nnfolder-current-buffer))
337 (set-buffer nnfolder-current-buffer)
338 ;; If the buffer was modified, write the file out now.
339 (nnfolder-save-buffer)
340 ;; If we're shutting the server down, we need to kill the
341 ;; buffer and remove it from the open buffer list. Or, of
342 ;; course, if we're trying to minimize our space impact.
343 (kill-buffer (current-buffer))
344 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
345 nnfolder-buffer-alist)))))
346 (setq nnfolder-current-group nil
347 nnfolder-current-buffer nil)
350 (deffoo nnfolder-request-create-group (group &optional server args)
351 (nnfolder-possibly-change-group nil server)
352 (nnmail-activate 'nnfolder)
354 (unless (assoc group nnfolder-group-alist)
355 (push (list group (cons 1 0)) nnfolder-group-alist)
356 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
357 (nnfolder-read-folder group)))
360 (deffoo nnfolder-request-list (&optional server)
361 (nnfolder-possibly-change-group nil server)
363 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
364 (nnmail-find-file nnfolder-active-file)
365 (setq nnfolder-group-alist (nnmail-get-active)))
368 (deffoo nnfolder-request-newgroups (date &optional server)
369 (nnfolder-possibly-change-group nil server)
370 (nnfolder-request-list server))
372 (deffoo nnfolder-request-list-newsgroups (&optional server)
373 (nnfolder-possibly-change-group nil server)
375 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
376 (nnmail-find-file nnfolder-newsgroups-file))))
378 ;; Return a list consisting of all article numbers existing in the
381 (defun nnfolder-existing-articles ()
383 (when nnfolder-current-buffer
384 (set-buffer nnfolder-current-buffer)
385 (goto-char (point-min))
386 (let ((marker (concat "\n" nnfolder-article-marker))
389 (while (and (search-forward marker nil t)
390 (re-search-forward number nil t))
391 (let ((newnum (string-to-number (match-string 0))))
392 (if (nnmail-within-headers-p)
393 (push newnum numbers))))
394 ;; The article numbers are increasing, so this result is sorted.
395 (nreverse numbers)))))
397 (deffoo nnfolder-request-expire-articles
398 (articles newsgroup &optional server force)
399 (nnfolder-possibly-change-group newsgroup server)
401 ;; The articles we have deleted so far.
402 (deleted-articles nil)
403 ;; The articles that really exist and will
404 ;; be expired if they are old enough.
406 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
407 (nnmail-activate 'nnfolder)
410 (set-buffer nnfolder-current-buffer)
411 ;; Since messages are sorted in arrival order and expired in the
412 ;; same order, we can stop as soon as we find a message that is
414 (while (and maybe-expirable is-old)
415 (goto-char (point-min))
416 (when (and (nnfolder-goto-article (car maybe-expirable))
417 (search-forward (concat "\n" nnfolder-article-marker)
421 (nnmail-expired-article-p
424 (point) (progn (end-of-line) (point)))
425 force nnfolder-inhibit-expiry))
426 (unless (eq nnmail-expiry-target 'delete)
428 (nnfolder-request-article (car maybe-expirable)
429 newsgroup server (current-buffer))
430 (let ((nnfolder-current-directory nil))
431 (nnmail-expiry-target-group
432 nnmail-expiry-target newsgroup)))
433 (nnfolder-possibly-change-group newsgroup server))
434 (nnheader-message 5 "Deleting article %d in %s..."
435 (car maybe-expirable) newsgroup)
436 (nnfolder-delete-mail)
437 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
438 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
439 ;; Must remember which articles were actually deleted
440 (push (car maybe-expirable) deleted-articles)))
441 (setq maybe-expirable (cdr maybe-expirable)))
442 (unless nnfolder-inhibit-expiry
443 (nnheader-message 5 "Deleting articles...done"))
444 (nnfolder-save-buffer)
445 (nnfolder-adjust-min-active newsgroup)
446 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
447 (gnus-sorted-complement articles (nreverse deleted-articles)))))
449 (deffoo nnfolder-request-move-article (article group server
450 accept-form &optional last)
452 (let ((buf (get-buffer-create " *nnfolder move*"))
455 (nnfolder-request-article article group server)
459 (insert-buffer-substring nntp-server-buffer)
460 (goto-char (point-min))
461 (while (re-search-forward
462 (concat "^" nnfolder-article-marker)
463 (save-excursion (and (search-forward "\n\n" nil t) (point)))
465 (delete-region (progn (beginning-of-line) (point))
466 (progn (forward-line 1) (point))))
467 (setq result (eval accept-form))
471 (nnfolder-possibly-change-group group server)
472 (set-buffer nnfolder-current-buffer)
473 (goto-char (point-min))
474 (when (nnfolder-goto-article article)
475 (nnfolder-delete-mail))
476 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
477 (nnfolder-nov-delete-article group article))
479 (nnfolder-save-buffer)
480 (nnfolder-adjust-min-active group)
481 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
484 (deffoo nnfolder-request-accept-article (group &optional server last)
486 (nnfolder-possibly-change-group group server)
487 (nnmail-check-syntax)
488 (let ((buf (current-buffer))
490 (goto-char (point-min))
491 (when (looking-at "X-From-Line: ")
492 (replace-match "From "))
494 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
495 (nntp-server-buffer (current-buffer)))
496 (nnmail-find-file nnfolder-active-file)
497 (setq nnfolder-group-alist (nnmail-parse-active))))
499 (goto-char (point-min))
500 (if (search-forward "\n\n" nil t)
502 (goto-char (point-max)))
503 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
504 (delete-region (point) (progn (forward-line 1) (point))))
505 (when nnmail-cache-accepted-message-ids
506 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
507 (setq result (if (stringp group)
508 (list (cons group (nnfolder-active-number group)))
510 (nnmail-article-group 'nnfolder-active-number))))
511 (if (and (null result)
512 (yes-or-no-p "Moved to `junk' group; delete article? "))
515 (car (nnfolder-save-mail result)))))
518 (nnfolder-possibly-change-folder (or (caar art-group) group))
519 (nnfolder-save-buffer)
520 (when nnmail-cache-accepted-message-ids
521 (nnmail-cache-close))))
522 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
524 (nnheader-report 'nnfolder "Couldn't store article"))
527 (deffoo nnfolder-request-replace-article (article group buffer)
528 (nnfolder-possibly-change-group group)
531 (goto-char (point-min))
533 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
534 (setq xfrom (match-string 1))
536 (goto-char (point-min))
538 (insert "From " xfrom "\n")
539 (unless (looking-at "From ")
540 (insert "From nobody " (current-time-string) "\n"))))
541 (nnfolder-normalize-buffer)
542 (set-buffer nnfolder-current-buffer)
543 (goto-char (point-min))
544 (if (not (nnfolder-goto-article article))
546 (nnfolder-delete-mail)
547 (insert-buffer-substring buffer)
548 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
551 (let ((headers (nnfolder-parse-head article
552 (point-min) (point-max))))
553 (with-current-buffer (nnfolder-open-nov group)
554 (if (nnheader-find-nov-line article)
555 (delete-region (point) (progn (forward-line 1) (point))))
556 (nnheader-insert-nov headers)))))
557 (nnfolder-save-buffer)
560 (deffoo nnfolder-request-delete-group (group &optional force server)
561 (nnfolder-close-group group server t)
562 ;; Delete all articles in GROUP.
564 () ; Don't delete the articles.
565 ;; Delete the file that holds the group.
567 (delete-file (nnfolder-group-pathname group))
568 (when (file-exists-p (nnfolder-group-nov-pathname group))
569 (delete-file (nnfolder-group-nov-pathname group)))
570 (when (file-exists-p (nnfolder-group-marks-pathname group))
571 (delete-file (nnfolder-group-marks-pathname group)))))
572 ;; Remove the group from all structures.
573 (setq nnfolder-group-alist
574 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
575 nnfolder-current-group nil
576 nnfolder-current-buffer nil)
577 ;; Save the active file.
578 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
581 (deffoo nnfolder-request-rename-group (group new-name &optional server)
582 (nnfolder-possibly-change-group group server)
584 (set-buffer nnfolder-current-buffer)
585 (and (file-writable-p buffer-file-name)
587 (let ((new-file (nnfolder-group-pathname new-name)))
588 (gnus-make-directory (file-name-directory new-file))
589 (rename-file buffer-file-name new-file)
590 (when (file-exists-p (nnfolder-group-nov-pathname group))
591 (setq new-file (nnfolder-group-nov-pathname new-name))
592 (gnus-make-directory (file-name-directory new-file))
593 (rename-file (nnfolder-group-nov-pathname group) new-file))
594 (when (file-exists-p (nnfolder-group-marks-pathname group))
595 (setq new-file (nnfolder-group-marks-pathname new-name))
596 (gnus-make-directory (file-name-directory new-file))
597 (rename-file (nnfolder-group-marks-pathname group) new-file)))
599 ;; That went ok, so we change the internal structures.
600 (let ((entry (assoc group nnfolder-group-alist)))
601 (and entry (setcar entry new-name))
602 (setq nnfolder-current-buffer nil
603 nnfolder-current-group nil)
604 ;; Save the new group alist.
605 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
606 ;; We kill the buffer instead of renaming it and stuff.
607 (kill-buffer (current-buffer))
610 (deffoo nnfolder-request-regenerate (server)
611 (nnfolder-possibly-change-group nil server)
612 (nnfolder-generate-active-file)
616 ;;; Internal functions.
618 (defun nnfolder-adjust-min-active (group)
619 ;; Find the lowest active article in this group.
620 (let* ((active (cadr (assoc group nnfolder-group-alist)))
621 (marker (concat "\n" nnfolder-article-marker))
623 (activemin (cdr active)))
625 (set-buffer nnfolder-current-buffer)
626 (goto-char (point-min))
627 (while (and (search-forward marker nil t)
628 (re-search-forward number nil t))
629 (let ((newnum (string-to-number (match-string 0))))
630 (if (nnmail-within-headers-p)
631 (setq activemin (min activemin newnum)))))
632 (setcar active activemin))))
634 (defun nnfolder-article-string (article)
635 (if (numberp article)
636 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
637 (concat "\nMessage-ID: " article)))
639 (defun nnfolder-goto-article (article)
640 "Place point at the start of the headers of ARTICLE.
641 ARTICLE can be an article number or a Message-ID.
642 Returns t if successful, nil otherwise."
643 (let ((art-string (nnfolder-article-string article))
645 ;; It is likely that we are at or before the delimiter line.
646 ;; We therefore go to the end of the previous line, and start
647 ;; searching from there.
652 ;; First search forward.
653 (while (and (setq found (search-forward art-string nil t))
654 (not (nnmail-within-headers-p))))
655 ;; If unsuccessful, search backward from where we started,
658 (while (and (setq found (search-backward art-string nil t))
659 (not (nnmail-within-headers-p)))))
661 (nnmail-search-unix-mail-delim-backward))))
663 (defun nnfolder-delete-mail (&optional leave-delim)
664 "Delete the message that point is in.
665 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
666 deleted. Point is left where the deleted region was."
670 ;; In case point is at the beginning of the message already.
672 (nnmail-search-unix-mail-delim-backward)
673 (if leave-delim (progn (forward-line 1) (point))
677 (if (nnmail-search-unix-mail-delim)
680 (run-hooks 'nnfolder-delete-mail-hook)
681 (delete-region (point-min) (point-max))))
683 (defun nnfolder-possibly-change-group (group &optional server dont-check)
686 (not (nnfolder-server-opened server)))
687 (nnfolder-open-server server))
688 (unless (gnus-buffer-live-p nnfolder-current-buffer)
689 (setq nnfolder-current-buffer nil
690 nnfolder-current-group nil))
692 (let ((file-name-coding-system nnmail-pathname-coding-system))
694 (not (equal group nnfolder-current-group))
696 (nnmail-activate 'nnfolder)
697 (and (assoc group nnfolder-group-alist)
698 (file-exists-p (nnfolder-group-pathname group)))))
700 (setq nnfolder-current-group group
701 nnfolder-current-buffer nil)
703 ;; If we have to change groups, see if we don't already have
704 ;; the folder in memory. If we do, verify the modtime and
705 ;; destroy the folder if needed so we can rescan it.
706 (setq nnfolder-current-buffer
707 (nth 1 (assoc group nnfolder-buffer-alist)))
709 ;; If the buffer is not live, make sure it isn't in the
710 ;; alist. If it is live, verify that nobody else has
711 ;; touched the file since last time.
712 (when (and nnfolder-current-buffer
713 (not (gnus-buffer-live-p nnfolder-current-buffer)))
714 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
715 nnfolder-current-buffer nil))
717 (setq nnfolder-current-group group)
719 (when (or (not nnfolder-current-buffer)
720 (not (verify-visited-file-modtime
721 nnfolder-current-buffer)))
723 (setq file (nnfolder-group-pathname group))
724 ;; See whether we need to create the new file.
725 (unless (file-exists-p file)
726 (gnus-make-directory (file-name-directory file))
727 (let ((nnmail-file-coding-system
728 (or nnfolder-file-coding-system-for-write
729 nnfolder-file-coding-system-for-write)))
730 (nnmail-write-region 1 1 file t 'nomesg)))
731 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
732 (set-buffer nnfolder-current-buffer)
733 (push (list group nnfolder-current-buffer)
734 nnfolder-buffer-alist)))))))))
736 (defun nnfolder-save-mail (group-art-list)
737 "Called narrowed to an article."
738 (let* (save-list group-art)
739 (goto-char (point-min))
740 ;; The From line may have been quoted by movemail.
741 (when (looking-at ">From")
743 ;; This might come from somewhere else.
744 (unless (looking-at "From ")
745 (insert "From nobody " (current-time-string) "\n")
746 (goto-char (point-min)))
747 ;; Quote all "From " lines in the article.
749 (let (case-fold-search)
750 (while (re-search-forward "^From " nil t)
753 (setq save-list group-art-list)
754 (nnmail-insert-lines)
755 (nnmail-insert-xref group-art-list)
756 (run-hooks 'nnmail-prepare-save-mail-hook)
757 (run-hooks 'nnfolder-prepare-save-mail-hook)
759 ;; Insert the mail into each of the destination groups.
760 (while (setq group-art (pop group-art-list))
761 ;; Kill any previous newsgroup markers.
762 (goto-char (point-min))
763 (if (search-forward "\n\n" nil t)
765 (goto-char (point-max)))
766 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
767 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
769 ;; Insert the new newsgroup marker.
770 (nnfolder-insert-newsgroup-line group-art)
773 (let ((beg (point-min))
775 (obuf (current-buffer)))
776 (nnfolder-possibly-change-folder (car group-art))
777 (let ((buffer-read-only nil))
778 (nnfolder-normalize-buffer)
779 (insert-buffer-substring obuf beg end))
780 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
782 (nnfolder-add-nov (car group-art) (cdr group-art)
783 (nnfolder-parse-head nil beg end))))))
785 ;; Did we save it anywhere?
788 (defun nnfolder-normalize-buffer ()
789 "Make sure there are two newlines at the end of the buffer."
790 (goto-char (point-max))
791 (skip-chars-backward "\n")
792 (delete-region (point) (point-max))
796 (defun nnfolder-insert-newsgroup-line (group-art)
798 (goto-char (point-min))
799 (unless (search-forward "\n\n" nil t)
800 (goto-char (point-max))
803 (insert (format (concat nnfolder-article-marker "%d %s\n")
804 (cdr group-art) (current-time-string)))))
806 (defun nnfolder-active-number (group)
807 ;; Find the next article number in GROUP.
808 (let ((active (cadr (assoc group nnfolder-group-alist))))
810 (setcdr active (1+ (cdr active)))
811 ;; This group is new, so we create a new entry for it.
812 ;; This might be a bit naughty... creating groups on the drop of
813 ;; a hat, but I don't know...
814 (push (list group (setq active (cons 1 1)))
815 nnfolder-group-alist))
818 (defun nnfolder-possibly-change-folder (group)
819 (let ((inf (assoc group nnfolder-buffer-alist)))
821 (gnus-buffer-live-p (cadr inf)))
822 (set-buffer (cadr inf))
824 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
825 (when nnfolder-group-alist
826 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
827 (push (list group (nnfolder-read-folder group))
828 nnfolder-buffer-alist))))
830 ;; This method has a problem if you've accidentally let the active
831 ;; list get out of sync with the files. This could happen, say, if
832 ;; you've accidentally gotten new mail with something other than Gnus
833 ;; (but why would _that_ ever happen? :-). In that case, we will be
834 ;; in the middle of processing the file, ready to add new X-Gnus
835 ;; article number markers, and we'll run across a message with no ID
836 ;; yet - the active list _may_not_ be ready for us yet.
838 ;; To handle this, I'm modifying this routine to maintain the maximum
839 ;; ID seen so far, and when we hit a message with no ID, we will
840 ;; _manually_ scan the rest of the message looking for any more,
841 ;; possibly higher IDs. We'll assume the maximum that we find is the
842 ;; highest active. Note that this shouldn't cost us much extra time
843 ;; at all, but will be a lot less vulnerable to glitches between the
844 ;; mbox and the active file.
846 (defun nnfolder-read-folder (group)
847 (let* ((file (nnfolder-group-pathname group))
848 (nov (nnfolder-group-nov-pathname group))
850 (let ((nnheader-file-coding-system
851 nnfolder-file-coding-system))
852 (nnheader-find-file-noselect file)))))
853 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
854 (if (equal (cadr (assoc group nnfolder-scantime-alist))
855 (nth 5 (file-attributes file)))
856 ;; This looks up-to-date, so we don't do any scanning.
857 (if (file-exists-p file)
859 (push (list group buffer) nnfolder-buffer-alist)
860 (set-buffer-modified-p t)
861 (nnfolder-save-buffer))
862 ;; Parse the damn thing.
864 (goto-char (point-min))
865 ;; Remove any blank lines at the start.
866 (while (eq (following-char) ?\n)
868 (nnmail-activate 'nnfolder)
870 (let ((delim "^From ")
871 (marker (concat "\n" nnfolder-article-marker))
873 (active (or (cadr (assoc group nnfolder-group-alist))
875 (scantime (assoc group nnfolder-scantime-alist))
877 maxid start end newscantime
878 novbuf articles newnum
880 (buffer-disable-undo)
881 (setq maxid (cdr active))
883 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
884 (and (file-exists-p nov)
885 (file-newer-than-file-p nov file)))
886 (unless (file-exists-p nov)
887 (gnus-make-directory (file-name-directory nov)))
889 (setq novbuf (nnfolder-open-nov group))
890 (goto-char (point-min))
892 (push (read novbuf) articles)
894 (setq articles (nreverse articles))))
895 (goto-char (point-min))
897 ;; Anytime the active number is 1 or 0, it is suspect. In
898 ;; that case, search the file manually to find the active
899 ;; number. Or, of course, if we're being paranoid. (This
900 ;; would also be the place to build other lists from the
901 ;; header markers, such as expunge lists, etc., if we ever
902 ;; desired to abandon the active file entirely for mboxes.)
903 (when (or nnfolder-ignore-active-file
906 (while (and (search-forward marker nil t)
908 (setq newnum (string-to-number (match-string 0)))
909 (when (nnmail-within-headers-p)
910 (setq maxid (max maxid newnum)
911 minid (min minid newnum))
913 (if (memq newnum articles)
914 (setq articles (delq newnum articles))
915 (let ((headers (nnfolder-parse-head newnum)))
916 (with-current-buffer novbuf
917 (nnheader-find-nov-line newnum)
918 (nnheader-insert-nov headers)))))))
919 (when (and novbuf articles)
920 (with-current-buffer novbuf
921 (dolist (article articles)
922 (when (nnheader-find-nov-line article)
923 (delete-region (point)
924 (progn (forward-line 1) (point)))))))
925 (setcar active (max 1 (min minid maxid)))
926 (setcdr active (max maxid (cdr active)))
927 (goto-char (point-min)))
929 ;; As long as we trust that the user will only insert
930 ;; unmarked mail at the end, go to the end and search
931 ;; backwards for the last marker. Find the start of that
932 ;; message, and begin to search for unmarked messages from
934 (when (not (or nnfolder-distrust-mbox
936 (goto-char (point-max))
937 (unless (re-search-backward marker nil t)
938 (goto-char (point-min)))
939 ;;(when (nnmail-search-unix-mail-delim)
940 ;; (goto-char (point-min)))
943 ;; Keep track of the active number on our own, and insert it
944 ;; back into the active list when we're done. Also, prime
945 ;; the pump to cut down on the number of searches we do.
946 (unless (nnmail-search-unix-mail-delim)
947 (goto-char (point-max)))
948 (setq end (point-marker))
949 (while (not (= end (point-max)))
950 (setq start (marker-position end))
952 ;; There may be more than one "From " line, so we skip past
954 (while (looking-at delim)
956 (set-marker end (if (nnmail-search-unix-mail-delim)
960 (when (not (search-forward marker end t))
961 (narrow-to-region start end)
962 (nnmail-insert-lines)
963 (nnfolder-insert-newsgroup-line
966 (nnfolder-active-number nnfolder-current-group))))
968 (let ((headers (nnfolder-parse-head newnum (point-min)
970 (with-current-buffer novbuf
971 (goto-char (point-max))
972 (nnheader-insert-nov headers))))
976 ;; Make absolutely sure that the active list reflects
978 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
980 ;; Set the scantime for this group.
981 (setq newscantime (visited-file-modtime))
983 (setcdr scantime (list newscantime))
984 (push (list nnfolder-current-group newscantime)
985 nnfolder-scantime-alist))
989 (current-buffer))))))
992 (defun nnfolder-generate-active-file ()
993 "Look for mbox folders in the nnfolder directory and make them into groups.
994 This command does not work if you use short group names."
996 (nnmail-activate 'nnfolder)
997 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
998 (dolist (file (directory-files (or nnfolder-nov-directory
1002 (regexp-quote nnfolder-nov-file-suffix)
1004 (when (not (message-mail-file-mbox-p file))
1006 (delete-file file)))))
1007 (let ((files (directory-files nnfolder-directory))
1009 (while (setq file (pop files))
1010 (when (and (not (backup-file-name-p file))
1011 (message-mail-file-mbox-p
1012 (nnheader-concat nnfolder-directory file)))
1013 (let ((oldgroup (assoc file nnfolder-group-alist)))
1015 (nnheader-message 5 "Refreshing group %s..." file)
1016 (nnheader-message 5 "Adding group %s..." file))
1018 (setq nnfolder-group-alist
1019 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1020 (push (list file (cons 1 0)) nnfolder-group-alist)
1021 (nnfolder-possibly-change-folder file)
1022 (nnfolder-possibly-change-group file)
1023 (nnfolder-close-group file))))
1024 (nnheader-message 5 "")))
1026 (defun nnfolder-group-pathname (group)
1027 "Make pathname for GROUP."
1029 (mm-encode-coding-string group nnmail-pathname-coding-system))
1030 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1031 ;; If this file exists, we use it directly.
1032 (if (or nnmail-use-long-file-names
1033 (file-exists-p (concat dir group)))
1035 ;; If not, we translate dots into slashes.
1036 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1038 (defun nnfolder-group-nov-pathname (group)
1039 "Make pathname for GROUP NOV."
1040 (let ((nnfolder-directory
1041 (or nnfolder-nov-directory nnfolder-directory)))
1042 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1044 (defun nnfolder-save-buffer ()
1046 (when (buffer-modified-p)
1047 (run-hooks 'nnfolder-save-buffer-hook)
1048 (gnus-make-directory (file-name-directory (buffer-file-name)))
1049 (let ((coding-system-for-write
1050 (or nnfolder-file-coding-system-for-write
1051 nnfolder-file-coding-system)))
1053 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1054 (nnfolder-save-nov)))
1056 (defun nnfolder-save-active (group-alist active-file)
1057 (let ((nnmail-active-file-coding-system
1058 (or nnfolder-active-file-coding-system-for-write
1059 nnfolder-active-file-coding-system)))
1060 (nnmail-save-active group-alist active-file)))
1062 (defun nnfolder-open-nov (group)
1063 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1064 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1067 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1068 (nnfolder-group-nov-pathname group))
1070 (when (file-exists-p nnfolder-nov-buffer-file-name)
1071 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1072 (push (cons group buffer) nnfolder-nov-buffer-alist)
1075 (defun nnfolder-save-nov ()
1077 (while nnfolder-nov-buffer-alist
1078 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1079 (set-buffer (cdar nnfolder-nov-buffer-alist))
1080 (when (buffer-modified-p)
1081 (gnus-make-directory (file-name-directory
1082 nnfolder-nov-buffer-file-name))
1083 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1085 (set-buffer-modified-p nil)
1086 (kill-buffer (current-buffer)))
1087 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1089 (defun nnfolder-nov-delete-article (group article)
1091 (set-buffer (nnfolder-open-nov group))
1092 (when (nnheader-find-nov-line article)
1093 (delete-region (point) (progn (forward-line 1) (point))))
1096 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1097 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1099 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1100 (when (file-exists-p nov)
1102 (set-buffer nntp-server-buffer)
1104 (nnheader-insert-file-contents nov)
1106 (not (numberp fetch-old)))
1107 t ; Don't remove anything.
1108 (nnheader-nov-delete-outside-range
1109 (if fetch-old (max 1 (- (car articles) fetch-old))
1111 (car (last articles)))
1114 (defun nnfolder-parse-head (&optional number b e)
1115 "Parse the head of the current buffer."
1116 (let ((buf (current-buffer))
1120 (setq b (if (nnmail-search-unix-mail-delim-backward)
1121 (point) (point-min)))
1123 (setq e (if (nnmail-search-unix-mail-delim)
1124 (point) (point-max))))
1125 (setq chars (- e b))
1126 (unless (zerop chars)
1128 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1130 (insert-buffer-substring buf b e)
1131 ;; Fold continuation lines.
1132 (goto-char (point-min))
1133 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1134 (replace-match " " t t))
1135 ;; Remove any tabs; they are too confusing.
1136 (subst-char-in-region (point-min) (point-max) ?\t ? )
1137 (let ((headers (nnheader-parse-head t)))
1138 (mail-header-set-chars headers chars)
1139 (mail-header-set-number headers number)
1142 (defun nnfolder-add-nov (group article headers)
1143 "Add a nov line for the GROUP base."
1145 (set-buffer (nnfolder-open-nov group))
1146 (goto-char (point-max))
1147 (mail-header-set-number headers article)
1148 (nnheader-insert-nov headers)))
1150 (deffoo nnfolder-request-set-mark (group actions &optional server)
1152 (not (nnfolder-server-opened server)))
1153 (nnfolder-open-server server))
1154 (unless nnfolder-marks-is-evil
1155 (nnfolder-open-marks group server)
1156 (dolist (action actions)
1157 (let ((range (nth 0 action))
1158 (what (nth 1 action))
1159 (marks (nth 2 action)))
1160 (assert (or (eq what 'add) (eq what 'del)) t
1161 "Unknown request-set-mark action: %s" what)
1162 (dolist (mark marks)
1163 (setq nnfolder-marks (gnus-update-alist-soft
1165 (funcall (if (eq what 'add) 'gnus-range-add
1166 'gnus-remove-from-range)
1167 (cdr (assoc mark nnfolder-marks)) range)
1169 (nnfolder-save-marks group server))
1172 (deffoo nnfolder-request-update-info (group info &optional server)
1175 (not (nnfolder-server-opened server)))
1176 (nnfolder-open-server server))
1177 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1178 (nnheader-message 8 "Updating marks for %s..." group)
1179 (nnfolder-open-marks group server)
1180 ;; Update info using `nnfolder-marks'.
1181 (mapcar (lambda (pred)
1182 (gnus-info-set-marks
1184 (gnus-update-alist-soft
1186 (cdr (assq (cdr pred) nnfolder-marks))
1187 (gnus-info-marks info))
1189 gnus-article-mark-lists)
1190 (let ((seen (cdr (assq 'read nnfolder-marks))))
1191 (gnus-info-set-read info
1192 (if (and (integerp (car seen))
1194 (list (cons (car seen) (car seen)))
1196 (nnheader-message 8 "Updating marks for %s...done" group))
1199 (defun nnfolder-group-marks-pathname (group)
1200 "Make pathname for GROUP NOV."
1201 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1202 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1204 (defun nnfolder-marks-changed-p (group)
1205 (let ((file (nnfolder-group-marks-pathname group)))
1206 (if (null (gnus-gethash file nnfolder-marks-modtime))
1207 t ;; never looked at marks file, assume it has changed
1208 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1209 (nth 5 (file-attributes file)))))))
1211 (defun nnfolder-save-marks (group server)
1212 (let ((file-name-coding-system nnmail-pathname-coding-system)
1213 (file (nnfolder-group-marks-pathname group)))
1216 (with-temp-file file
1218 (princ nnfolder-marks (current-buffer))
1221 (nth 5 (file-attributes file))
1222 nnfolder-marks-modtime))
1223 (error (or (gnus-yes-or-no-p
1224 (format "Could not write to %s (%s). Continue? " file err))
1225 (error "Cannot write to %s (%s)" err))))))
1227 (defun nnfolder-open-marks (group server)
1228 (let ((file (nnfolder-group-marks-pathname group)))
1229 (if (file-exists-p file)
1232 (gnus-sethash file (nth 5 (file-attributes file))
1233 nnfolder-marks-modtime)
1234 (nnheader-insert-file-contents file)
1235 (setq nnfolder-marks (read (current-buffer)))
1236 (dolist (el gnus-article-unpropagated-mark-lists)
1237 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1238 (error (or (gnus-yes-or-no-p
1239 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1240 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1241 ;; User didn't have a .marks file. Probably first time
1242 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1243 (let ((info (gnus-get-info
1244 (gnus-group-prefixed-name
1246 (gnus-server-to-method (format "nnfolder:%s" server))))))
1247 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1248 (setq nnfolder-marks (gnus-info-marks info))
1249 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1250 (dolist (el gnus-article-unpropagated-mark-lists)
1251 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1252 (nnfolder-save-marks group server)))))
1256 ;;; nnfolder.el ends here