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-intersection "gnus-range"))
45 (nnoo-declare nnfolder)
47 (defvoo nnfolder-directory (expand-file-name message-directory)
48 "The name of the nnfolder directory.
50 This variable is a virtual server slot. See the Gnus manual for details.")
52 (defvoo nnfolder-nov-directory nil
53 "The name of the nnfolder NOV directory.
54 If nil, `nnfolder-directory' is used.")
56 (defvoo nnfolder-marks-directory nil
57 "The name of the nnfolder MARKS directory.
58 If nil, `nnfolder-directory' is used.")
60 (defvoo nnfolder-active-file
61 (nnheader-concat nnfolder-directory "active")
62 "The name of the active file.
64 This variable is a virtual server slot. See the Gnus manual for details.")
66 ;; I renamed this variable to something more in keeping with the general GNU
69 (defvoo nnfolder-ignore-active-file nil
70 "If non-nil, the active file is ignored.
71 This causes nnfolder to do some extra work in order to determine the
72 true active ranges of an mbox file. Note that the active file is
73 still saved, but its values are not used. This costs some extra time
74 when scanning an mbox when opening it.")
76 (defvoo nnfolder-distrust-mbox nil
77 "If non-nil, the folder will be distrusted.
78 This means that nnfolder will not trust the user with respect to
79 inserting unaccounted for mail in the middle of an mbox file. This
80 can greatly slow down scans, which now must scan the entire file for
81 unmarked messages. When nil, scans occur forward from the last marked
82 message, a huge time saver for large mailboxes.")
84 (defvoo nnfolder-newsgroups-file
85 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
86 "Mail newsgroups description file.
88 This variable is a virtual server slot. See the Gnus manual for details.")
90 (defvoo nnfolder-get-new-mail t
91 "If non-nil, nnfolder will check the incoming mail file and split the mail.
93 This variable is a virtual server slot. See the Gnus manual for details.")
95 (defvoo nnfolder-prepare-save-mail-hook nil
96 "Hook run narrowed to an article before saving.")
98 (defvoo nnfolder-save-buffer-hook nil
99 "Hook run before saving the nnfolder mbox buffer.
101 This variable is a virtual server slot. See the Gnus manual for details.")
104 (defvoo nnfolder-inhibit-expiry nil
105 "If non-nil, inhibit expiry.
107 This variable is a virtual server slot. See the Gnus manual for details.")
111 (defconst nnfolder-version "nnfolder 2.0"
114 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
115 "String used to demarcate what the article number for a message is.")
117 (defvoo nnfolder-current-group nil)
118 (defvoo nnfolder-current-buffer nil)
119 (defvoo nnfolder-status-string "")
120 (defvoo nnfolder-group-alist nil)
121 (defvoo nnfolder-buffer-alist nil)
122 (defvoo nnfolder-scantime-alist nil)
123 (defvoo nnfolder-active-timestamp nil)
124 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
125 (defvoo nnfolder-active-file-coding-system-for-write
126 nnmail-active-file-coding-system)
127 (defvoo nnfolder-file-coding-system mm-text-coding-system)
128 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
129 "Coding system for save nnfolder file.
130 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
132 (defvoo nnfolder-nov-is-evil nil
133 "If non-nil, Gnus will never generate and use nov databases for mail groups.
134 Using nov databases will speed up header fetching considerably.
135 This variable shouldn't be flipped much. If you have, for some reason,
136 set this to t, and want to set it to nil again, you should always run
137 the `nnfolder-generate-active-file' command. The function will go
138 through all nnfolder directories and generate nov databases for them
139 all. This may very well take some time.
141 This variable is a virtual server slot. See the Gnus manual for details.")
143 (defvoo nnfolder-nov-file-suffix ".nov")
145 (defvoo nnfolder-nov-buffer-alist nil)
147 (defvar nnfolder-nov-buffer-file-name nil)
149 (defvoo nnfolder-marks-is-evil nil
150 "If non-nil, Gnus will never generate and use marks file for mail groups.
151 Using marks files makes it possible to backup and restore mail groups
152 separately from `.newsrc.eld'. If you have, for some reason, set
153 this to t, and want to set it to nil again, you should always remove
154 the corresponding marks file (usually base nnfolder file name
155 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
156 the group. Then the marks file will be regenerated properly by Gnus.
158 This variable is a virtual server slot. See the Gnus manual for details.")
160 (defvoo nnfolder-marks nil)
162 (defvoo nnfolder-marks-file-suffix ".mrk")
166 ;;; Interface functions
168 (nnoo-define-basics nnfolder)
170 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
172 (set-buffer nntp-server-buffer)
174 (let (article start stop)
175 (nnfolder-possibly-change-group group server)
176 (when nnfolder-current-buffer
177 (set-buffer nnfolder-current-buffer)
178 (goto-char (point-min))
179 (if (stringp (car articles))
181 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
183 (setq articles (gnus-sorted-intersection
184 ;; Is ARTICLES sorted?
186 (nnfolder-existing-articles)))
187 (while (setq article (pop articles))
188 (set-buffer nnfolder-current-buffer)
189 (when (nnfolder-goto-article article)
191 (setq stop (if (search-forward "\n\n" nil t)
194 (set-buffer nntp-server-buffer)
195 (insert (format "221 %d Article retrieved.\n" article))
196 (insert-buffer-substring nnfolder-current-buffer start stop)
197 (goto-char (point-max))
199 (set-buffer nntp-server-buffer)
200 (nnheader-fold-continuation-lines)
203 (deffoo nnfolder-open-server (server &optional defs)
204 (nnoo-change-server 'nnfolder server defs)
205 (nnmail-activate 'nnfolder t)
206 (gnus-make-directory nnfolder-directory)
207 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
208 (and nnfolder-nov-directory
209 (gnus-make-directory nnfolder-nov-directory)))
210 (unless nnfolder-marks-is-evil
211 (and nnfolder-marks-directory
212 (gnus-make-directory nnfolder-marks-directory)))
214 ((not (file-exists-p nnfolder-directory))
215 (nnfolder-close-server)
216 (nnheader-report 'nnfolder "Couldn't create directory: %s"
218 ((not (file-directory-p (file-truename nnfolder-directory)))
219 (nnfolder-close-server)
220 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
222 (nnmail-activate 'nnfolder)
223 (nnheader-report 'nnfolder "Opened server %s using directory %s"
224 server nnfolder-directory)
227 (deffoo nnfolder-request-close ()
228 (let ((alist nnfolder-buffer-alist))
230 (nnfolder-close-group (caar alist) nil t)
231 (setq alist (cdr alist))))
232 (nnoo-close-server 'nnfolder)
233 (setq nnfolder-buffer-alist nil
234 nnfolder-group-alist nil))
236 (deffoo nnfolder-request-article (article &optional group server buffer)
237 (nnfolder-possibly-change-group group server)
239 (set-buffer nnfolder-current-buffer)
240 (goto-char (point-min))
241 (when (nnfolder-goto-article article)
245 (unless (and (nnmail-search-unix-mail-delim)
247 (goto-char (point-max)))
249 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
250 (set-buffer nntp-server-buffer)
252 (insert-buffer-substring nnfolder-current-buffer start stop)
253 (goto-char (point-min))
254 (while (looking-at "From ")
256 (insert "X-From-Line: ")
258 (if (numberp article)
259 (cons nnfolder-current-group article)
260 (goto-char (point-min))
261 (cons nnfolder-current-group
262 (if (search-forward (concat "\n" nnfolder-article-marker)
266 (point) (progn (end-of-line) (point))))
269 (deffoo nnfolder-request-group (group &optional server dont-check)
270 (nnfolder-possibly-change-group group server t)
272 (if (not (assoc group nnfolder-group-alist))
273 (nnheader-report 'nnfolder "No such group: %s" group)
276 (nnheader-report 'nnfolder "Selected group %s" group)
278 (let* ((active (assoc group nnfolder-group-alist))
280 (range (cadr active)))
283 (nnheader-report 'nnfolder "No such group: %s" group))
284 ((null nnfolder-current-group)
285 (nnheader-report 'nnfolder "Empty group: %s" group))
287 (nnheader-report 'nnfolder "Selected group %s" group)
288 (nnheader-insert "211 %d %d %d %s\n"
289 (1+ (- (cdr range) (car range)))
290 (car range) (cdr range) group))))))))
292 (deffoo nnfolder-request-scan (&optional group server)
293 (nnfolder-possibly-change-group nil server)
294 (when nnfolder-get-new-mail
295 (nnfolder-possibly-change-group group server)
299 (let ((bufs nnfolder-buffer-alist))
302 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
303 (setq nnfolder-buffer-alist
304 (delq (car bufs) nnfolder-buffer-alist))
305 (set-buffer (nth 1 (car bufs)))
306 (nnfolder-save-buffer)
307 (kill-buffer (current-buffer)))
308 (setq bufs (cdr bufs))))))
312 ;; Don't close the buffer if we're not shutting down the server. This way,
313 ;; we can keep the buffer in the group buffer cache, and not have to grovel
314 ;; over the buffer again unless we add new mail to it or modify it in some
317 (deffoo nnfolder-close-group (group &optional server force)
318 ;; Make sure we _had_ the group open.
319 (when (or (assoc group nnfolder-buffer-alist)
320 (equal group nnfolder-current-group))
321 (let ((inf (assoc group nnfolder-buffer-alist)))
323 (when (and nnfolder-current-group
324 nnfolder-current-buffer)
325 (push (list nnfolder-current-group nnfolder-current-buffer)
326 nnfolder-buffer-alist))
327 (setq nnfolder-buffer-alist
328 (delq inf nnfolder-buffer-alist))
329 (setq nnfolder-current-buffer (cadr inf)
330 nnfolder-current-group (car inf))))
331 (when (and nnfolder-current-buffer
332 (buffer-name nnfolder-current-buffer))
334 (set-buffer nnfolder-current-buffer)
335 ;; If the buffer was modified, write the file out now.
336 (nnfolder-save-buffer)
337 ;; If we're shutting the server down, we need to kill the
338 ;; buffer and remove it from the open buffer list. Or, of
339 ;; course, if we're trying to minimize our space impact.
340 (kill-buffer (current-buffer))
341 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
342 nnfolder-buffer-alist)))))
343 (setq nnfolder-current-group nil
344 nnfolder-current-buffer nil)
347 (deffoo nnfolder-request-create-group (group &optional server args)
348 (nnfolder-possibly-change-group nil server)
349 (nnmail-activate 'nnfolder)
351 (unless (assoc group nnfolder-group-alist)
352 (push (list group (cons 1 0)) nnfolder-group-alist)
353 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
354 (nnfolder-read-folder group)))
357 (deffoo nnfolder-request-list (&optional server)
358 (nnfolder-possibly-change-group nil server)
360 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
361 (nnmail-find-file nnfolder-active-file)
362 (setq nnfolder-group-alist (nnmail-get-active)))
365 (deffoo nnfolder-request-newgroups (date &optional server)
366 (nnfolder-possibly-change-group nil server)
367 (nnfolder-request-list server))
369 (deffoo nnfolder-request-list-newsgroups (&optional server)
370 (nnfolder-possibly-change-group nil server)
372 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
373 (nnmail-find-file nnfolder-newsgroups-file))))
375 ;; Return a list consisting of all article numbers existing in the
378 (defun nnfolder-existing-articles ()
380 (when nnfolder-current-buffer
381 (set-buffer nnfolder-current-buffer)
382 (goto-char (point-min))
383 (let ((marker (concat "\n" nnfolder-article-marker))
386 (while (and (search-forward marker nil t)
387 (re-search-forward number nil t))
388 (let ((newnum (string-to-number (match-string 0))))
389 (if (nnmail-within-headers-p)
390 (push newnum numbers))))
391 ;; The article numbers are increasing, so this result is sorted.
392 (nreverse numbers)))))
394 (deffoo nnfolder-request-expire-articles
395 (articles newsgroup &optional server force)
396 (nnfolder-possibly-change-group newsgroup server)
398 ;; The articles we have deleted so far.
399 (deleted-articles nil)
400 ;; The articles that really exist and will
401 ;; be expired if they are old enough.
403 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
404 (nnmail-activate 'nnfolder)
407 (set-buffer nnfolder-current-buffer)
408 ;; Since messages are sorted in arrival order and expired in the
409 ;; same order, we can stop as soon as we find a message that is
411 (while (and maybe-expirable is-old)
412 (goto-char (point-min))
413 (when (and (nnfolder-goto-article (car maybe-expirable))
414 (search-forward (concat "\n" nnfolder-article-marker)
418 (nnmail-expired-article-p
421 (point) (progn (end-of-line) (point)))
422 force nnfolder-inhibit-expiry))
423 (unless (eq nnmail-expiry-target 'delete)
425 (nnfolder-request-article (car maybe-expirable)
426 newsgroup server (current-buffer))
427 (let ((nnml-current-directory nil))
428 (nnmail-expiry-target-group
429 nnmail-expiry-target newsgroup))))
430 (nnheader-message 5 "Deleting article %d in %s..."
431 (car maybe-expirable) newsgroup)
432 (nnfolder-delete-mail)
433 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
434 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
435 ;; Must remember which articles were actually deleted
436 (push (car maybe-expirable) deleted-articles)))
437 (setq maybe-expirable (cdr maybe-expirable)))
438 (unless nnfolder-inhibit-expiry
439 (nnheader-message 5 "Deleting articles...done"))
440 (nnfolder-save-buffer)
441 (nnfolder-adjust-min-active newsgroup)
442 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
443 (gnus-sorted-complement articles (nreverse deleted-articles)))))
445 (deffoo nnfolder-request-move-article (article group server
446 accept-form &optional last)
448 (let ((buf (get-buffer-create " *nnfolder move*"))
451 (nnfolder-request-article article group server)
455 (insert-buffer-substring nntp-server-buffer)
456 (goto-char (point-min))
457 (while (re-search-forward
458 (concat "^" nnfolder-article-marker)
459 (save-excursion (and (search-forward "\n\n" nil t) (point)))
461 (delete-region (progn (beginning-of-line) (point))
462 (progn (forward-line 1) (point))))
463 (setq result (eval accept-form))
467 (nnfolder-possibly-change-group group server)
468 (set-buffer nnfolder-current-buffer)
469 (goto-char (point-min))
470 (when (nnfolder-goto-article article)
471 (nnfolder-delete-mail))
472 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
473 (nnfolder-nov-delete-article group article))
475 (nnfolder-save-buffer)
476 (nnfolder-adjust-min-active group)
477 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
480 (deffoo nnfolder-request-accept-article (group &optional server last)
482 (nnfolder-possibly-change-group group server)
483 (nnmail-check-syntax)
484 (let ((buf (current-buffer))
486 (goto-char (point-min))
487 (when (looking-at "X-From-Line: ")
488 (replace-match "From "))
490 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
491 (nntp-server-buffer (current-buffer)))
492 (nnmail-find-file nnfolder-active-file)
493 (setq nnfolder-group-alist (nnmail-parse-active))))
495 (goto-char (point-min))
496 (if (search-forward "\n\n" nil t)
498 (goto-char (point-max)))
499 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
500 (delete-region (point) (progn (forward-line 1) (point))))
501 (when nnmail-cache-accepted-message-ids
502 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
503 (setq result (if (stringp group)
504 (list (cons group (nnfolder-active-number group)))
506 (nnmail-article-group 'nnfolder-active-number))))
507 (if (and (null result)
508 (yes-or-no-p "Moved to `junk' group; delete article? "))
511 (car (nnfolder-save-mail result)))))
514 (nnfolder-possibly-change-folder (or (caar art-group) group))
515 (nnfolder-save-buffer)
516 (when nnmail-cache-accepted-message-ids
517 (nnmail-cache-close))))
518 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
520 (nnheader-report 'nnfolder "Couldn't store article"))
523 (deffoo nnfolder-request-replace-article (article group buffer)
524 (nnfolder-possibly-change-group group)
527 (goto-char (point-min))
529 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
530 (setq xfrom (match-string 1))
532 (goto-char (point-min))
534 (insert "From " xfrom "\n")
535 (unless (looking-at "From ")
536 (insert "From nobody " (current-time-string) "\n"))))
537 (nnfolder-normalize-buffer)
538 (set-buffer nnfolder-current-buffer)
539 (goto-char (point-min))
540 (if (not (nnfolder-goto-article article))
542 (nnfolder-delete-mail)
543 (insert-buffer-substring buffer)
544 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
547 (let ((headers (nnfolder-parse-head article
548 (point-min) (point-max))))
549 (with-current-buffer (nnfolder-open-nov group)
550 (if (nnheader-find-nov-line article)
551 (delete-region (point) (progn (forward-line 1) (point))))
552 (nnheader-insert-nov headers)))))
553 (nnfolder-save-buffer)
556 (deffoo nnfolder-request-delete-group (group &optional force server)
557 (nnfolder-close-group group server t)
558 ;; Delete all articles in GROUP.
560 () ; Don't delete the articles.
561 ;; Delete the file that holds the group.
563 (delete-file (nnfolder-group-pathname group))
564 (when (file-exists-p (nnfolder-group-nov-pathname group))
565 (delete-file (nnfolder-group-nov-pathname group)))
566 (when (file-exists-p (nnfolder-group-marks-pathname group))
567 (delete-file (nnfolder-group-marks-pathname group)))))
568 ;; Remove the group from all structures.
569 (setq nnfolder-group-alist
570 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
571 nnfolder-current-group nil
572 nnfolder-current-buffer nil)
573 ;; Save the active file.
574 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
577 (deffoo nnfolder-request-rename-group (group new-name &optional server)
578 (nnfolder-possibly-change-group group server)
580 (set-buffer nnfolder-current-buffer)
581 (and (file-writable-p buffer-file-name)
583 (let ((new-file (nnfolder-group-pathname new-name)))
584 (gnus-make-directory (file-name-directory new-file))
585 (rename-file buffer-file-name new-file)
586 (when (file-exists-p (nnfolder-group-nov-pathname group))
587 (setq new-file (nnfolder-group-nov-pathname new-name))
588 (gnus-make-directory (file-name-directory new-file))
589 (rename-file (nnfolder-group-nov-pathname group) new-file))
590 (when (file-exists-p (nnfolder-group-marks-pathname group))
591 (setq new-file (nnfolder-group-marks-pathname new-name))
592 (gnus-make-directory (file-name-directory new-file))
593 (rename-file (nnfolder-group-marks-pathname group) new-file)))
595 ;; That went ok, so we change the internal structures.
596 (let ((entry (assoc group nnfolder-group-alist)))
597 (and entry (setcar entry new-name))
598 (setq nnfolder-current-buffer nil
599 nnfolder-current-group nil)
600 ;; Save the new group alist.
601 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
602 ;; We kill the buffer instead of renaming it and stuff.
603 (kill-buffer (current-buffer))
606 (deffoo nnfolder-request-regenerate (server)
607 (nnfolder-possibly-change-group nil server)
608 (nnfolder-generate-active-file)
612 ;;; Internal functions.
614 (defun nnfolder-adjust-min-active (group)
615 ;; Find the lowest active article in this group.
616 (let* ((active (cadr (assoc group nnfolder-group-alist)))
617 (marker (concat "\n" nnfolder-article-marker))
619 (activemin (cdr active)))
621 (set-buffer nnfolder-current-buffer)
622 (goto-char (point-min))
623 (while (and (search-forward marker nil t)
624 (re-search-forward number nil t))
625 (let ((newnum (string-to-number (match-string 0))))
626 (if (nnmail-within-headers-p)
627 (setq activemin (min activemin newnum)))))
628 (setcar active activemin))))
630 (defun nnfolder-article-string (article)
631 (if (numberp article)
632 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
633 (concat "\nMessage-ID: " article)))
635 (defun nnfolder-goto-article (article)
636 "Place point at the start of the headers of ARTICLE.
637 ARTICLE can be an article number or a Message-ID.
638 Returns t if successful, nil otherwise."
639 (let ((art-string (nnfolder-article-string article))
641 ;; It is likely that we are at or before the delimiter line.
642 ;; We therefore go to the end of the previous line, and start
643 ;; searching from there.
648 ;; First search forward.
649 (while (and (setq found (search-forward art-string nil t))
650 (not (nnmail-within-headers-p))))
651 ;; If unsuccessful, search backward from where we started,
654 (while (and (setq found (search-backward art-string nil t))
655 (not (nnmail-within-headers-p)))))
657 (nnmail-search-unix-mail-delim-backward))))
659 (defun nnfolder-delete-mail (&optional leave-delim)
660 "Delete the message that point is in.
661 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
662 deleted. Point is left where the deleted region was."
666 ;; In case point is at the beginning of the message already.
668 (nnmail-search-unix-mail-delim-backward)
669 (if leave-delim (progn (forward-line 1) (point))
673 (if (nnmail-search-unix-mail-delim)
676 (run-hooks 'nnfolder-delete-mail-hook)
677 (delete-region (point-min) (point-max))))
679 (defun nnfolder-possibly-change-group (group &optional server dont-check)
682 (not (nnfolder-server-opened server)))
683 (nnfolder-open-server server))
684 (unless (gnus-buffer-live-p nnfolder-current-buffer)
685 (setq nnfolder-current-buffer nil
686 nnfolder-current-group nil))
688 (let ((file-name-coding-system nnmail-pathname-coding-system))
690 (not (equal group nnfolder-current-group))
692 (nnmail-activate 'nnfolder)
693 (and (assoc group nnfolder-group-alist)
694 (file-exists-p (nnfolder-group-pathname group)))))
696 (setq nnfolder-current-group group
697 nnfolder-current-buffer nil)
699 ;; If we have to change groups, see if we don't already have
700 ;; the folder in memory. If we do, verify the modtime and
701 ;; destroy the folder if needed so we can rescan it.
702 (setq nnfolder-current-buffer
703 (nth 1 (assoc group nnfolder-buffer-alist)))
705 ;; If the buffer is not live, make sure it isn't in the
706 ;; alist. If it is live, verify that nobody else has
707 ;; touched the file since last time.
708 (when (and nnfolder-current-buffer
709 (not (gnus-buffer-live-p nnfolder-current-buffer)))
710 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
711 nnfolder-current-buffer nil))
713 (setq nnfolder-current-group group)
715 (when (or (not nnfolder-current-buffer)
716 (not (verify-visited-file-modtime
717 nnfolder-current-buffer)))
719 (setq file (nnfolder-group-pathname group))
720 ;; See whether we need to create the new file.
721 (unless (file-exists-p file)
722 (gnus-make-directory (file-name-directory file))
723 (let ((nnmail-file-coding-system
724 (or nnfolder-file-coding-system-for-write
725 nnfolder-file-coding-system-for-write)))
726 (nnmail-write-region 1 1 file t 'nomesg)))
727 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
728 (set-buffer nnfolder-current-buffer)
729 (push (list group nnfolder-current-buffer)
730 nnfolder-buffer-alist)))))))))
732 (defun nnfolder-save-mail (group-art-list)
733 "Called narrowed to an article."
734 (let* (save-list group-art)
735 (goto-char (point-min))
736 ;; The From line may have been quoted by movemail.
737 (when (looking-at ">From")
739 ;; This might come from somewhere else.
740 (unless (looking-at "From ")
741 (insert "From nobody " (current-time-string) "\n")
742 (goto-char (point-min)))
743 ;; Quote all "From " lines in the article.
745 (let (case-fold-search)
746 (while (re-search-forward "^From " nil t)
749 (setq save-list group-art-list)
750 (nnmail-insert-lines)
751 (nnmail-insert-xref group-art-list)
752 (run-hooks 'nnmail-prepare-save-mail-hook)
753 (run-hooks 'nnfolder-prepare-save-mail-hook)
755 ;; Insert the mail into each of the destination groups.
756 (while (setq group-art (pop group-art-list))
757 ;; Kill any previous newsgroup markers.
758 (goto-char (point-min))
759 (if (search-forward "\n\n" nil t)
761 (goto-char (point-max)))
762 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
763 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
765 ;; Insert the new newsgroup marker.
766 (nnfolder-insert-newsgroup-line group-art)
769 (let ((beg (point-min))
771 (obuf (current-buffer)))
772 (nnfolder-possibly-change-folder (car group-art))
773 (let ((buffer-read-only nil))
774 (nnfolder-normalize-buffer)
775 (insert-buffer-substring obuf beg end))
776 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
778 (nnfolder-add-nov (car group-art) (cdr group-art)
779 (nnfolder-parse-head nil beg end))))))
781 ;; Did we save it anywhere?
784 (defun nnfolder-normalize-buffer ()
785 "Make sure there are two newlines at the end of the buffer."
786 (goto-char (point-max))
787 (skip-chars-backward "\n")
788 (delete-region (point) (point-max))
791 (defun nnfolder-insert-newsgroup-line (group-art)
793 (goto-char (point-min))
794 (unless (search-forward "\n\n" nil t)
795 (goto-char (point-max))
798 (insert (format (concat nnfolder-article-marker "%d %s\n")
799 (cdr group-art) (current-time-string)))))
801 (defun nnfolder-active-number (group)
802 ;; Find the next article number in GROUP.
803 (let ((active (cadr (assoc group nnfolder-group-alist))))
805 (setcdr active (1+ (cdr active)))
806 ;; This group is new, so we create a new entry for it.
807 ;; This might be a bit naughty... creating groups on the drop of
808 ;; a hat, but I don't know...
809 (push (list group (setq active (cons 1 1)))
810 nnfolder-group-alist))
813 (defun nnfolder-possibly-change-folder (group)
814 (let ((inf (assoc group nnfolder-buffer-alist)))
816 (gnus-buffer-live-p (cadr inf)))
817 (set-buffer (cadr inf))
819 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
820 (when nnfolder-group-alist
821 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
822 (push (list group (nnfolder-read-folder group))
823 nnfolder-buffer-alist))))
825 ;; This method has a problem if you've accidentally let the active
826 ;; list get out of sync with the files. This could happen, say, if
827 ;; you've accidentally gotten new mail with something other than Gnus
828 ;; (but why would _that_ ever happen? :-). In that case, we will be
829 ;; in the middle of processing the file, ready to add new X-Gnus
830 ;; article number markers, and we'll run across a message with no ID
831 ;; yet - the active list _may_not_ be ready for us yet.
833 ;; To handle this, I'm modifying this routine to maintain the maximum
834 ;; ID seen so far, and when we hit a message with no ID, we will
835 ;; _manually_ scan the rest of the message looking for any more,
836 ;; possibly higher IDs. We'll assume the maximum that we find is the
837 ;; highest active. Note that this shouldn't cost us much extra time
838 ;; at all, but will be a lot less vulnerable to glitches between the
839 ;; mbox and the active file.
841 (defun nnfolder-read-folder (group)
842 (let* ((file (nnfolder-group-pathname group))
843 (nov (nnfolder-group-nov-pathname group))
845 (let ((nnheader-file-coding-system
846 nnfolder-file-coding-system))
847 (nnheader-find-file-noselect file)))))
848 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
849 (if (equal (cadr (assoc group nnfolder-scantime-alist))
850 (nth 5 (file-attributes file)))
851 ;; This looks up-to-date, so we don't do any scanning.
852 (if (file-exists-p file)
854 (push (list group buffer) nnfolder-buffer-alist)
855 (set-buffer-modified-p t)
856 (nnfolder-save-buffer))
857 ;; Parse the damn thing.
859 (goto-char (point-min))
860 ;; Remove any blank lines at the start.
861 (while (eq (following-char) ?\n)
863 (nnmail-activate 'nnfolder)
865 (let ((delim "^From ")
866 (marker (concat "\n" nnfolder-article-marker))
868 (active (or (cadr (assoc group nnfolder-group-alist))
870 (scantime (assoc group nnfolder-scantime-alist))
872 maxid start end newscantime
873 novbuf articles newnum
875 (buffer-disable-undo)
876 (setq maxid (cdr active))
878 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
879 (and (file-exists-p nov)
880 (file-newer-than-file-p nov file)))
881 (unless (file-exists-p nov)
882 (gnus-make-directory (file-name-directory nov)))
884 (setq novbuf (nnfolder-open-nov group))
885 (goto-char (point-min))
887 (push (read novbuf) articles)
889 (setq articles (nreverse articles))))
890 (goto-char (point-min))
892 ;; Anytime the active number is 1 or 0, it is suspect. In
893 ;; that case, search the file manually to find the active
894 ;; number. Or, of course, if we're being paranoid. (This
895 ;; would also be the place to build other lists from the
896 ;; header markers, such as expunge lists, etc., if we ever
897 ;; desired to abandon the active file entirely for mboxes.)
898 (when (or nnfolder-ignore-active-file
901 (while (and (search-forward marker nil t)
903 (setq newnum (string-to-number (match-string 0)))
904 (when (nnmail-within-headers-p)
905 (setq maxid (max maxid newnum)
906 minid (min minid newnum))
908 (if (memq newnum articles)
909 (setq articles (delq newnum articles))
910 (let ((headers (nnfolder-parse-head newnum)))
911 (with-current-buffer novbuf
912 (nnheader-find-nov-line newnum)
913 (nnheader-insert-nov headers)))))))
914 (when (and novbuf articles)
915 (with-current-buffer novbuf
916 (dolist (article articles)
917 (when (nnheader-find-nov-line article)
918 (delete-region (point)
919 (progn (forward-line 1) (point)))))))
920 (setcar active (max 1 (min minid maxid)))
921 (setcdr active (max maxid (cdr active)))
922 (goto-char (point-min)))
924 ;; As long as we trust that the user will only insert
925 ;; unmarked mail at the end, go to the end and search
926 ;; backwards for the last marker. Find the start of that
927 ;; message, and begin to search for unmarked messages from
929 (when (not (or nnfolder-distrust-mbox
931 (goto-char (point-max))
932 (unless (re-search-backward marker nil t)
933 (goto-char (point-min)))
934 ;;(when (nnmail-search-unix-mail-delim)
935 ;; (goto-char (point-min)))
938 ;; Keep track of the active number on our own, and insert it
939 ;; back into the active list when we're done. Also, prime
940 ;; the pump to cut down on the number of searches we do.
941 (unless (nnmail-search-unix-mail-delim)
942 (goto-char (point-max)))
943 (setq end (point-marker))
944 (while (not (= end (point-max)))
945 (setq start (marker-position end))
947 ;; There may be more than one "From " line, so we skip past
949 (while (looking-at delim)
951 (set-marker end (if (nnmail-search-unix-mail-delim)
955 (when (not (search-forward marker end t))
956 (narrow-to-region start end)
957 (nnmail-insert-lines)
958 (nnfolder-insert-newsgroup-line
961 (nnfolder-active-number nnfolder-current-group))))
963 (let ((headers (nnfolder-parse-head newnum (point-min)
965 (with-current-buffer novbuf
966 (goto-char (point-max))
967 (nnheader-insert-nov headers))))
971 ;; Make absolutely sure that the active list reflects
973 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
975 ;; Set the scantime for this group.
976 (setq newscantime (visited-file-modtime))
978 (setcdr scantime (list newscantime))
979 (push (list nnfolder-current-group newscantime)
980 nnfolder-scantime-alist))
984 (current-buffer))))))
987 (defun nnfolder-generate-active-file ()
988 "Look for mbox folders in the nnfolder directory and make them into groups.
989 This command does not work if you use short group names."
991 (nnmail-activate 'nnfolder)
992 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
993 (dolist (file (directory-files (or nnfolder-nov-directory
997 (regexp-quote nnfolder-nov-file-suffix)
999 (when (not (message-mail-file-mbox-p file))
1001 (delete-file file)))))
1002 (let ((files (directory-files nnfolder-directory))
1004 (while (setq file (pop files))
1005 (when (and (not (backup-file-name-p file))
1006 (message-mail-file-mbox-p
1007 (nnheader-concat nnfolder-directory file)))
1008 (let ((oldgroup (assoc file nnfolder-group-alist)))
1010 (nnheader-message 5 "Refreshing group %s..." file)
1011 (nnheader-message 5 "Adding group %s..." file))
1013 (setq nnfolder-group-alist
1014 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1015 (push (list file (cons 1 0)) nnfolder-group-alist)
1016 (nnfolder-possibly-change-folder file)
1017 (nnfolder-possibly-change-group file)
1018 (nnfolder-close-group file))))
1019 (nnheader-message 5 "")))
1021 (defun nnfolder-group-pathname (group)
1022 "Make pathname for GROUP."
1024 (mm-encode-coding-string group nnmail-pathname-coding-system))
1025 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1026 ;; If this file exists, we use it directly.
1027 (if (or nnmail-use-long-file-names
1028 (file-exists-p (concat dir group)))
1030 ;; If not, we translate dots into slashes.
1031 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1033 (defun nnfolder-group-nov-pathname (group)
1034 "Make pathname for GROUP NOV."
1035 (let ((nnfolder-directory
1036 (or nnfolder-nov-directory nnfolder-directory)))
1037 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1039 (defun nnfolder-save-buffer ()
1041 (when (buffer-modified-p)
1042 (run-hooks 'nnfolder-save-buffer-hook)
1043 (gnus-make-directory (file-name-directory (buffer-file-name)))
1044 (let ((coding-system-for-write
1045 (or nnfolder-file-coding-system-for-write
1046 nnfolder-file-coding-system)))
1048 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1049 (nnfolder-save-nov)))
1051 (defun nnfolder-save-active (group-alist active-file)
1052 (let ((nnmail-active-file-coding-system
1053 (or nnfolder-active-file-coding-system-for-write
1054 nnfolder-active-file-coding-system)))
1055 (nnmail-save-active group-alist active-file)))
1057 (defun nnfolder-open-nov (group)
1058 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1059 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1062 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1063 (nnfolder-group-nov-pathname group))
1065 (when (file-exists-p nnfolder-nov-buffer-file-name)
1066 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1067 (push (cons group buffer) nnfolder-nov-buffer-alist)
1070 (defun nnfolder-save-nov ()
1072 (while nnfolder-nov-buffer-alist
1073 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1074 (set-buffer (cdar nnfolder-nov-buffer-alist))
1075 (when (buffer-modified-p)
1076 (gnus-make-directory (file-name-directory
1077 nnfolder-nov-buffer-file-name))
1078 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1080 (set-buffer-modified-p nil)
1081 (kill-buffer (current-buffer)))
1082 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1084 (defun nnfolder-nov-delete-article (group article)
1086 (set-buffer (nnfolder-open-nov group))
1087 (when (nnheader-find-nov-line article)
1088 (delete-region (point) (progn (forward-line 1) (point))))
1091 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1092 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1094 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1095 (when (file-exists-p nov)
1097 (set-buffer nntp-server-buffer)
1099 (nnheader-insert-file-contents nov)
1101 (not (numberp fetch-old)))
1102 t ; Don't remove anything.
1103 (nnheader-nov-delete-outside-range
1104 (if fetch-old (max 1 (- (car articles) fetch-old))
1106 (car (last articles)))
1109 (defun nnfolder-parse-head (&optional number b e)
1110 "Parse the head of the current buffer."
1111 (let ((buf (current-buffer))
1115 (setq b (if (nnmail-search-unix-mail-delim-backward)
1116 (point) (point-min)))
1118 (setq e (if (nnmail-search-unix-mail-delim)
1119 (point) (point-max))))
1120 (setq chars (- e b))
1121 (unless (zerop chars)
1123 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1125 (insert-buffer-substring buf b e)
1126 ;; Fold continuation lines.
1127 (goto-char (point-min))
1128 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1129 (replace-match " " t t))
1130 ;; Remove any tabs; they are too confusing.
1131 (subst-char-in-region (point-min) (point-max) ?\t ? )
1132 (let ((headers (nnheader-parse-head t)))
1133 (mail-header-set-chars headers chars)
1134 (mail-header-set-number headers number)
1137 (defun nnfolder-add-nov (group article headers)
1138 "Add a nov line for the GROUP base."
1140 (set-buffer (nnfolder-open-nov group))
1141 (goto-char (point-max))
1142 (mail-header-set-number headers article)
1143 (nnheader-insert-nov headers)))
1145 (deffoo nnfolder-request-set-mark (group actions &optional server)
1146 (nnfolder-possibly-change-group group server)
1147 (unless nnfolder-marks-is-evil
1148 (nnfolder-open-marks group server)
1149 (dolist (action actions)
1150 (let ((range (nth 0 action))
1151 (what (nth 1 action))
1152 (marks (nth 2 action)))
1153 (assert (or (eq what 'add) (eq what 'del)) t
1154 "Unknown request-set-mark action: %s" what)
1155 (dolist (mark marks)
1156 (setq nnfolder-marks (gnus-update-alist-soft
1158 (funcall (if (eq what 'add) 'gnus-range-add
1159 'gnus-remove-from-range)
1160 (cdr (assoc mark nnfolder-marks)) range)
1162 (nnfolder-save-marks group server))
1165 (deffoo nnfolder-request-update-info (group info &optional server)
1166 (nnfolder-possibly-change-group group server)
1167 (unless nnfolder-marks-is-evil
1168 (nnfolder-open-marks group server)
1169 ;; Update info using `nnfolder-marks'.
1170 (mapcar (lambda (pred)
1171 (gnus-info-set-marks
1173 (gnus-update-alist-soft
1175 (cdr (assq (cdr pred) nnfolder-marks))
1176 (gnus-info-marks info))
1178 gnus-article-mark-lists)
1179 (let ((seen (cdr (assq 'read nnfolder-marks))))
1180 (gnus-info-set-read info
1181 (if (and (integerp (car seen))
1183 (list (cons (car seen) (car seen)))
1187 (defun nnfolder-group-marks-pathname (group)
1188 "Make pathname for GROUP NOV."
1189 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1190 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1192 (defun nnfolder-save-marks (group server)
1193 (let ((file-name-coding-system nnmail-pathname-coding-system)
1194 (file (nnfolder-group-marks-pathname group)))
1197 (gnus-make-directory group)
1198 (with-temp-file file
1200 (princ nnfolder-marks (current-buffer))
1202 (error (or (gnus-yes-or-no-p
1203 (format "Could not write to %s (%s). Continue? " file err))
1204 (error "Cannot write to %s (%s)" err))))))
1206 (defun nnfolder-open-marks (group server)
1207 (let ((file (nnfolder-group-marks-pathname group)))
1208 (if (file-exists-p file)
1209 (setq nnfolder-marks (condition-case err
1211 (nnheader-insert-file-contents file)
1212 (read (current-buffer)))
1213 (error (or (gnus-yes-or-no-p
1214 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1215 (error "Cannot read nnfolder marks file %s (%s)" file err)))))
1216 ;; User didn't have a .marks file. Probably first time
1217 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1218 (let ((info (gnus-get-info
1219 (gnus-group-prefixed-name
1221 (gnus-server-to-method (format "nnfolder:%s" server))))))
1222 (nnheader-message 6 "Bootstrapping nnfolder marks...")
1223 (setq nnfolder-marks (gnus-info-marks info))
1224 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1225 (nnfolder-save-marks group server)))))
1229 ;;; nnfolder.el ends here