1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 (defvoo nnfolder-nov-directory nil
52 "The name of the nnfolder NOV directory.
53 If nil, `nnfolder-directory' is used.")
55 (defvoo nnfolder-marks-directory nil
56 "The name of the nnfolder MARKS directory.
57 If nil, `nnfolder-directory' is used.")
59 (defvoo nnfolder-active-file
60 (nnheader-concat nnfolder-directory "active")
61 "The name of the active file.")
63 ;; I renamed this variable to something more in keeping with the general GNU
66 (defvoo nnfolder-ignore-active-file nil
67 "If non-nil, the active file is ignored.
68 This causes nnfolder to do some extra work in order to determine the
69 true active ranges of an mbox file. Note that the active file is
70 still saved, but its values are not used. This costs some extra time
71 when scanning an mbox when opening it.")
73 (defvoo nnfolder-distrust-mbox nil
74 "If non-nil, the folder will be distrusted.
75 This means that nnfolder will not trust the user with respect to
76 inserting unaccounted for mail in the middle of an mbox file. This
77 can greatly slow down scans, which now must scan the entire file for
78 unmarked messages. When nil, scans occur forward from the last marked
79 message, a huge time saver for large mailboxes.")
81 (defvoo nnfolder-newsgroups-file
82 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
83 "Mail newsgroups description file.")
85 (defvoo nnfolder-get-new-mail t
86 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
88 (defvoo nnfolder-prepare-save-mail-hook nil
89 "Hook run narrowed to an article before saving.")
91 (defvoo nnfolder-save-buffer-hook nil
92 "Hook run before saving the nnfolder mbox buffer.")
95 (defvoo nnfolder-inhibit-expiry nil
96 "If non-nil, inhibit expiry.")
100 (defconst nnfolder-version "nnfolder 2.0"
103 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
104 "String used to demarcate what the article number for a message is.")
106 (defvoo nnfolder-current-group nil)
107 (defvoo nnfolder-current-buffer nil)
108 (defvoo nnfolder-status-string "")
109 (defvoo nnfolder-group-alist nil)
110 (defvoo nnfolder-buffer-alist nil)
111 (defvoo nnfolder-scantime-alist nil)
112 (defvoo nnfolder-active-timestamp nil)
113 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
114 (defvoo nnfolder-active-file-coding-system-for-write
115 nnmail-active-file-coding-system)
116 (defvoo nnfolder-file-coding-system mm-text-coding-system)
117 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
118 "Coding system for save nnfolder file.
119 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
121 (defvoo nnfolder-nov-is-evil nil
122 "If non-nil, Gnus will never generate and use nov databases for mail groups.
123 Using nov databases will speed up header fetching considerably.
124 This variable shouldn't be flipped much. If you have, for some reason,
125 set this to t, and want to set it to nil again, you should always run
126 the `nnfolder-generate-active-file' command. The function will go
127 through all nnfolder directories and generate nov databases for them
128 all. This may very well take some time.")
130 (defvoo nnfolder-nov-file-suffix ".nov")
132 (defvoo nnfolder-nov-buffer-alist nil)
134 (defvar nnfolder-nov-buffer-file-name nil)
136 (defvoo nnfolder-marks-is-evil nil
137 "If non-nil, Gnus will never generate and use marks file for mail groups.
138 Using marks files makes it possible to backup and restore mail groups
139 separately from `.newsrc.eld'. If you have, for some reason, set
140 this to t, and want to set it to nil again, you should always remove
141 the corresponding marks file (usually base nnfolder file name
142 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
143 the group. Then the marks file will be regenerated properly by Gnus.")
145 (defvoo nnfolder-marks nil)
147 (defvoo nnfolder-marks-file-suffix ".mrk")
149 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
153 ;;; Interface functions
155 (nnoo-define-basics nnfolder)
157 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
159 (set-buffer nntp-server-buffer)
161 (let (article start stop num)
162 (nnfolder-possibly-change-group group server)
163 (when nnfolder-current-buffer
164 (set-buffer nnfolder-current-buffer)
165 (goto-char (point-min))
166 (if (stringp (car articles))
168 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
170 (setq articles (gnus-sorted-intersection
171 ;; Is ARTICLES sorted?
173 (nnfolder-existing-articles)))
174 (while (setq article (pop articles))
175 (set-buffer nnfolder-current-buffer)
176 (cond ((nnfolder-goto-article article)
178 (setq stop (if (search-forward "\n\n" nil t)
181 (set-buffer nntp-server-buffer)
182 (insert (format "221 %d Article retrieved.\n" article))
183 (insert-buffer-substring nnfolder-current-buffer
185 (goto-char (point-max))
188 ;; If we couldn't find this article, skip over ranges
189 ;; of missing articles so we don't search the whole file
194 ;; Check that we are either at BOF or after an
195 ;; article with a lower number. We do this so we
196 ;; won't be confused by out-of-order article numbers,
197 ;; as caused by active file bogosity.
200 ((search-backward (concat "\n" nnfolder-article-marker)
202 (goto-char (match-end 0))
203 (setq num (string-to-int
205 (point) (progn (end-of-line) (point)))))
208 ;; Check that we are before an article with a
210 (search-forward (concat "\n" nnfolder-article-marker)
213 (setq num (string-to-int
215 (point) (progn (end-of-line) (point)))))
217 ;; Discard any article numbers before the one we're
220 (< (car articles) num))
221 (setq articles (cdr articles))))
223 (set-buffer nntp-server-buffer)
224 (nnheader-fold-continuation-lines)
227 (deffoo nnfolder-open-server (server &optional defs)
228 (nnoo-change-server 'nnfolder server defs)
229 (nnmail-activate 'nnfolder t)
230 (gnus-make-directory nnfolder-directory)
231 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
232 (and nnfolder-nov-directory
233 (gnus-make-directory nnfolder-nov-directory)))
234 (unless nnfolder-marks-is-evil
235 (and nnfolder-marks-directory
236 (gnus-make-directory nnfolder-marks-directory)))
238 ((not (file-exists-p nnfolder-directory))
239 (nnfolder-close-server)
240 (nnheader-report 'nnfolder "Couldn't create directory: %s"
242 ((not (file-directory-p (file-truename nnfolder-directory)))
243 (nnfolder-close-server)
244 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
246 (nnmail-activate 'nnfolder)
247 (nnheader-report 'nnfolder "Opened server %s using directory %s"
248 server nnfolder-directory)
251 (deffoo nnfolder-request-close ()
252 (let ((alist nnfolder-buffer-alist))
254 (nnfolder-close-group (caar alist) nil t)
255 (setq alist (cdr alist))))
256 (nnoo-close-server 'nnfolder)
257 (setq nnfolder-buffer-alist nil
258 nnfolder-group-alist nil))
260 (deffoo nnfolder-request-article (article &optional group server buffer)
261 (nnfolder-possibly-change-group group server)
263 (set-buffer nnfolder-current-buffer)
264 (goto-char (point-min))
265 (when (nnfolder-goto-article article)
269 (unless (and (nnmail-search-unix-mail-delim)
271 (goto-char (point-max)))
273 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
274 (set-buffer nntp-server-buffer)
276 (insert-buffer-substring nnfolder-current-buffer start stop)
277 (goto-char (point-min))
278 (while (looking-at "From ")
280 (insert "X-From-Line: ")
282 (if (numberp article)
283 (cons nnfolder-current-group article)
284 (goto-char (point-min))
285 (cons nnfolder-current-group
286 (if (search-forward (concat "\n" nnfolder-article-marker)
290 (point) (progn (end-of-line) (point))))
293 (deffoo nnfolder-request-group (group &optional server dont-check)
294 (nnfolder-possibly-change-group group server t)
296 (if (not (assoc group nnfolder-group-alist))
297 (nnheader-report 'nnfolder "No such group: %s" group)
300 (nnheader-report 'nnfolder "Selected group %s" group)
302 (let* ((active (assoc group nnfolder-group-alist))
304 (range (cadr active)))
307 (nnheader-report 'nnfolder "No such group: %s" group))
308 ((null nnfolder-current-group)
309 (nnheader-report 'nnfolder "Empty group: %s" group))
311 (nnheader-report 'nnfolder "Selected group %s" group)
312 (nnheader-insert "211 %d %d %d %s\n"
313 (1+ (- (cdr range) (car range)))
314 (car range) (cdr range) group))))))))
316 (deffoo nnfolder-request-scan (&optional group server)
317 (nnfolder-possibly-change-group nil server)
318 (when nnfolder-get-new-mail
319 (nnfolder-possibly-change-group group server)
323 (let ((bufs nnfolder-buffer-alist))
326 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
327 (setq nnfolder-buffer-alist
328 (delq (car bufs) nnfolder-buffer-alist))
329 (set-buffer (nth 1 (car bufs)))
330 (nnfolder-save-buffer)
331 (kill-buffer (current-buffer)))
332 (setq bufs (cdr bufs))))))
336 ;; Don't close the buffer if we're not shutting down the server. This way,
337 ;; we can keep the buffer in the group buffer cache, and not have to grovel
338 ;; over the buffer again unless we add new mail to it or modify it in some
341 (deffoo nnfolder-close-group (group &optional server force)
342 ;; Make sure we _had_ the group open.
343 (when (or (assoc group nnfolder-buffer-alist)
344 (equal group nnfolder-current-group))
345 (let ((inf (assoc group nnfolder-buffer-alist)))
347 (when (and nnfolder-current-group
348 nnfolder-current-buffer)
349 (push (list nnfolder-current-group nnfolder-current-buffer)
350 nnfolder-buffer-alist))
351 (setq nnfolder-buffer-alist
352 (delq inf nnfolder-buffer-alist))
353 (setq nnfolder-current-buffer (cadr inf)
354 nnfolder-current-group (car inf))))
355 (when (and nnfolder-current-buffer
356 (buffer-name nnfolder-current-buffer))
358 (set-buffer nnfolder-current-buffer)
359 ;; If the buffer was modified, write the file out now.
360 (nnfolder-save-buffer)
361 ;; If we're shutting the server down, we need to kill the
362 ;; buffer and remove it from the open buffer list. Or, of
363 ;; course, if we're trying to minimize our space impact.
364 (kill-buffer (current-buffer))
365 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
366 nnfolder-buffer-alist)))))
367 (setq nnfolder-current-group nil
368 nnfolder-current-buffer nil)
371 (deffoo nnfolder-request-create-group (group &optional server args)
372 (nnfolder-possibly-change-group nil server)
373 (nnmail-activate 'nnfolder)
375 (unless (assoc group nnfolder-group-alist)
376 (push (list group (cons 1 0)) nnfolder-group-alist)
377 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
378 (nnfolder-read-folder group)))
381 (deffoo nnfolder-request-list (&optional server)
382 (nnfolder-possibly-change-group nil server)
384 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
385 (nnmail-find-file nnfolder-active-file)
386 (setq nnfolder-group-alist (nnmail-get-active)))
389 (deffoo nnfolder-request-newgroups (date &optional server)
390 (nnfolder-possibly-change-group nil server)
391 (nnfolder-request-list server))
393 (deffoo nnfolder-request-list-newsgroups (&optional server)
394 (nnfolder-possibly-change-group nil server)
396 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
397 (nnmail-find-file nnfolder-newsgroups-file))))
399 ;; Return a list consisting of all article numbers existing in the
402 (defun nnfolder-existing-articles ()
404 (when nnfolder-current-buffer
405 (set-buffer nnfolder-current-buffer)
406 (goto-char (point-min))
407 (let ((marker (concat "\n" nnfolder-article-marker))
410 (while (and (search-forward marker nil t)
411 (re-search-forward number nil t))
412 (let ((newnum (string-to-number (match-string 0))))
413 (if (nnmail-within-headers-p)
414 (push newnum numbers))))
415 ;; The article numbers are increasing, so this result is sorted.
416 (nreverse numbers)))))
418 (deffoo nnfolder-request-expire-articles
419 (articles newsgroup &optional server force)
420 (nnfolder-possibly-change-group newsgroup server)
422 ;; The articles we have deleted so far.
423 (deleted-articles nil)
424 ;; The articles that really exist and will
425 ;; be expired if they are old enough.
427 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
428 (nnmail-activate 'nnfolder)
431 (set-buffer nnfolder-current-buffer)
432 ;; Since messages are sorted in arrival order and expired in the
433 ;; same order, we can stop as soon as we find a message that is
435 (while (and maybe-expirable is-old)
436 (goto-char (point-min))
437 (when (and (nnfolder-goto-article (car maybe-expirable))
438 (search-forward (concat "\n" nnfolder-article-marker)
442 (nnmail-expired-article-p
445 (point) (progn (end-of-line) (point)))
446 force nnfolder-inhibit-expiry))
447 (unless (eq nnmail-expiry-target 'delete)
449 (nnfolder-request-article (car maybe-expirable)
450 newsgroup server (current-buffer))
451 (let ((nnfolder-current-directory nil))
452 (nnmail-expiry-target-group
453 nnmail-expiry-target newsgroup)))
454 (nnfolder-possibly-change-group newsgroup server))
455 (nnheader-message 5 "Deleting article %d in %s..."
456 (car maybe-expirable) newsgroup)
457 (nnfolder-delete-mail)
458 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
459 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
460 ;; Must remember which articles were actually deleted
461 (push (car maybe-expirable) deleted-articles)))
462 (setq maybe-expirable (cdr maybe-expirable)))
463 (unless nnfolder-inhibit-expiry
464 (nnheader-message 5 "Deleting articles...done"))
465 (nnfolder-save-buffer)
466 (nnfolder-adjust-min-active newsgroup)
467 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
468 (gnus-sorted-difference articles (nreverse deleted-articles)))))
470 (deffoo nnfolder-request-move-article (article group server
471 accept-form &optional last)
473 (let ((buf (get-buffer-create " *nnfolder move*"))
476 (nnfolder-request-article article group server)
480 (insert-buffer-substring nntp-server-buffer)
481 (goto-char (point-min))
482 (while (re-search-forward
483 (concat "^" nnfolder-article-marker)
484 (save-excursion (and (search-forward "\n\n" nil t) (point)))
486 (delete-region (progn (beginning-of-line) (point))
487 (progn (forward-line 1) (point))))
488 (setq result (eval accept-form))
492 (nnfolder-possibly-change-group group server)
493 (set-buffer nnfolder-current-buffer)
494 (goto-char (point-min))
495 (when (nnfolder-goto-article article)
496 (nnfolder-delete-mail))
497 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
498 (nnfolder-nov-delete-article group article))
500 (nnfolder-save-buffer)
501 (nnfolder-adjust-min-active group)
502 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
505 (deffoo nnfolder-request-accept-article (group &optional server last)
507 (nnfolder-possibly-change-group group server)
508 (nnmail-check-syntax)
509 (let ((buf (current-buffer))
511 (goto-char (point-min))
512 (when (looking-at "X-From-Line: ")
514 (mail-header-unfold-field))
515 (replace-match "From "))
517 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
518 (nntp-server-buffer (current-buffer)))
519 (nnmail-find-file nnfolder-active-file)
520 (setq nnfolder-group-alist (nnmail-parse-active))))
522 (goto-char (point-min))
523 (if (search-forward "\n\n" nil t)
525 (goto-char (point-max)))
526 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
527 (delete-region (point) (progn (forward-line 1) (point))))
528 (when nnmail-cache-accepted-message-ids
529 (nnmail-cache-insert (nnmail-fetch-field "message-id") group))
530 (setq result (if (stringp group)
531 (list (cons group (nnfolder-active-number group)))
533 (nnmail-article-group 'nnfolder-active-number))))
534 (if (and (null result)
535 (yes-or-no-p "Moved to `junk' group; delete article? "))
538 (car (nnfolder-save-mail result)))))
541 (nnfolder-possibly-change-folder (or (caar art-group) group))
542 (nnfolder-save-buffer)
543 (when nnmail-cache-accepted-message-ids
544 (nnmail-cache-close))))
545 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
547 (nnheader-report 'nnfolder "Couldn't store article"))
550 (deffoo nnfolder-request-replace-article (article group buffer)
551 (nnfolder-possibly-change-group group)
554 (goto-char (point-min))
555 (if (not (looking-at "X-From-Line: "))
556 (insert "From nobody " (current-time-string) "\n")
557 (replace-match "From ")
559 (while (looking-at "[ \t]")
562 (nnfolder-normalize-buffer)
563 (set-buffer nnfolder-current-buffer)
564 (goto-char (point-min))
565 (if (not (nnfolder-goto-article article))
567 (nnfolder-delete-mail)
568 (insert-buffer-substring buffer)
569 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
572 (let ((headers (nnfolder-parse-head article
573 (point-min) (point-max))))
574 (with-current-buffer (nnfolder-open-nov group)
575 (if (nnheader-find-nov-line article)
576 (delete-region (point) (progn (forward-line 1) (point))))
577 (nnheader-insert-nov headers)))))
578 (nnfolder-save-buffer)
581 (deffoo nnfolder-request-delete-group (group &optional force server)
582 (nnfolder-close-group group server t)
583 ;; Delete all articles in GROUP.
585 () ; Don't delete the articles.
586 ;; Delete the file that holds the group.
587 (let ((data (nnfolder-group-pathname group))
588 (nov (nnfolder-group-nov-pathname group))
589 (mrk (nnfolder-group-marks-pathname group)))
590 (ignore-errors (delete-file data))
591 (ignore-errors (delete-file nov))
592 (ignore-errors (delete-file mrk))))
593 ;; Remove the group from all structures.
594 (setq nnfolder-group-alist
595 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
596 nnfolder-current-group nil
597 nnfolder-current-buffer nil)
598 ;; Save the active file.
599 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
602 (deffoo nnfolder-request-rename-group (group new-name &optional server)
603 (nnfolder-possibly-change-group group server)
605 (set-buffer nnfolder-current-buffer)
606 (and (file-writable-p buffer-file-name)
608 (let ((new-file (nnfolder-group-pathname new-name)))
609 (gnus-make-directory (file-name-directory new-file))
610 (rename-file buffer-file-name new-file)
611 (when (file-exists-p (nnfolder-group-nov-pathname group))
612 (setq new-file (nnfolder-group-nov-pathname new-name))
613 (gnus-make-directory (file-name-directory new-file))
614 (rename-file (nnfolder-group-nov-pathname group) new-file))
615 (when (file-exists-p (nnfolder-group-marks-pathname group))
616 (setq new-file (nnfolder-group-marks-pathname new-name))
617 (gnus-make-directory (file-name-directory new-file))
618 (rename-file (nnfolder-group-marks-pathname group) new-file)))
620 ;; That went ok, so we change the internal structures.
621 (let ((entry (assoc group nnfolder-group-alist)))
622 (and entry (setcar entry new-name))
623 (setq nnfolder-current-buffer nil
624 nnfolder-current-group nil)
625 ;; Save the new group alist.
626 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
627 ;; We kill the buffer instead of renaming it and stuff.
628 (kill-buffer (current-buffer))
631 (deffoo nnfolder-request-regenerate (server)
632 (nnfolder-possibly-change-group nil server)
633 (nnfolder-generate-active-file)
637 ;;; Internal functions.
639 (defun nnfolder-adjust-min-active (group)
640 ;; Find the lowest active article in this group.
641 (let* ((active (cadr (assoc group nnfolder-group-alist)))
642 (marker (concat "\n" nnfolder-article-marker))
644 (activemin (cdr active)))
646 (set-buffer nnfolder-current-buffer)
647 (goto-char (point-min))
648 (while (and (search-forward marker nil t)
649 (re-search-forward number nil t))
650 (let ((newnum (string-to-number (match-string 0))))
651 (if (nnmail-within-headers-p)
652 (setq activemin (min activemin newnum)))))
653 (setcar active activemin))))
655 (defun nnfolder-article-string (article)
656 (if (numberp article)
657 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
658 (concat "\nMessage-ID: " article)))
660 (defun nnfolder-goto-article (article)
661 "Place point at the start of the headers of ARTICLE.
662 ARTICLE can be an article number or a Message-ID.
663 Returns t if successful, nil otherwise."
664 (let ((art-string (nnfolder-article-string article))
666 ;; It is likely that we are at or before the delimiter line.
667 ;; We therefore go to the end of the previous line, and start
668 ;; searching from there.
673 ;; First search forward.
674 (while (and (setq found (search-forward art-string nil t))
675 (not (nnmail-within-headers-p))))
676 ;; If unsuccessful, search backward from where we started,
679 (while (and (setq found (search-backward art-string nil t))
680 (not (nnmail-within-headers-p)))))
682 (nnmail-search-unix-mail-delim-backward))))
684 (defun nnfolder-delete-mail (&optional leave-delim)
685 "Delete the message that point is in.
686 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
687 deleted. Point is left where the deleted region was."
691 ;; In case point is at the beginning of the message already.
693 (nnmail-search-unix-mail-delim-backward)
694 (if leave-delim (progn (forward-line 1) (point))
698 (if (nnmail-search-unix-mail-delim)
701 (run-hooks 'nnfolder-delete-mail-hook)
702 (delete-region (point-min) (point-max))))
704 (defun nnfolder-possibly-change-group (group &optional server dont-check)
707 (not (nnfolder-server-opened server)))
708 (nnfolder-open-server server))
709 (unless (gnus-buffer-live-p nnfolder-current-buffer)
710 (setq nnfolder-current-buffer nil
711 nnfolder-current-group nil))
713 (let ((file-name-coding-system nnmail-pathname-coding-system))
715 (not (equal group nnfolder-current-group))
717 (nnmail-activate 'nnfolder)
718 (and (assoc group nnfolder-group-alist)
719 (file-exists-p (nnfolder-group-pathname group)))))
721 (setq nnfolder-current-group group
722 nnfolder-current-buffer nil)
724 ;; If we have to change groups, see if we don't already have
725 ;; the folder in memory. If we do, verify the modtime and
726 ;; destroy the folder if needed so we can rescan it.
727 (setq nnfolder-current-buffer
728 (nth 1 (assoc group nnfolder-buffer-alist)))
730 ;; If the buffer is not live, make sure it isn't in the
731 ;; alist. If it is live, verify that nobody else has
732 ;; touched the file since last time.
733 (when (and nnfolder-current-buffer
734 (not (gnus-buffer-live-p nnfolder-current-buffer)))
735 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
736 nnfolder-current-buffer nil))
738 (setq nnfolder-current-group group)
740 (when (or (not nnfolder-current-buffer)
741 (not (verify-visited-file-modtime
742 nnfolder-current-buffer)))
744 (setq file (nnfolder-group-pathname group))
745 ;; See whether we need to create the new file.
746 (unless (file-exists-p file)
747 (gnus-make-directory (file-name-directory file))
748 (let ((nnmail-file-coding-system
749 (or nnfolder-file-coding-system-for-write
750 nnfolder-file-coding-system-for-write)))
751 (nnmail-write-region (point-min) (point-min)
753 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
754 (set-buffer nnfolder-current-buffer)
755 (push (list group nnfolder-current-buffer)
756 nnfolder-buffer-alist)))))))))
758 (defun nnfolder-save-mail (group-art-list)
759 "Called narrowed to an article."
760 (let* (save-list group-art)
761 (goto-char (point-min))
762 ;; The From line may have been quoted by movemail.
763 (when (looking-at ">From")
765 ;; This might come from somewhere else.
766 (unless (looking-at "From ")
767 (insert "From nobody " (current-time-string) "\n")
768 (goto-char (point-min)))
769 ;; Quote all "From " lines in the article.
771 (let (case-fold-search)
772 (while (re-search-forward "^From " nil t)
775 (setq save-list group-art-list)
776 (nnmail-insert-lines)
777 (nnmail-insert-xref group-art-list)
778 (run-hooks 'nnmail-prepare-save-mail-hook)
779 (run-hooks 'nnfolder-prepare-save-mail-hook)
781 ;; Insert the mail into each of the destination groups.
782 (while (setq group-art (pop group-art-list))
783 ;; Kill any previous newsgroup markers.
784 (goto-char (point-min))
785 (if (search-forward "\n\n" nil t)
787 (goto-char (point-max)))
788 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
789 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
791 ;; Insert the new newsgroup marker.
792 (nnfolder-insert-newsgroup-line group-art)
795 (let ((beg (point-min))
797 (obuf (current-buffer)))
798 (nnfolder-possibly-change-folder (car group-art))
799 (let ((buffer-read-only nil))
800 (nnfolder-normalize-buffer)
801 (insert-buffer-substring obuf beg end))
802 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
804 (nnfolder-add-nov (car group-art) (cdr group-art)
805 (nnfolder-parse-head nil beg end))))))
807 ;; Did we save it anywhere?
810 (defun nnfolder-normalize-buffer ()
811 "Make sure there are two newlines at the end of the buffer."
812 (goto-char (point-max))
813 (skip-chars-backward "\n")
814 (delete-region (point) (point-max))
818 (defun nnfolder-insert-newsgroup-line (group-art)
820 (goto-char (point-min))
821 (unless (search-forward "\n\n" nil t)
822 (goto-char (point-max))
825 (insert (format (concat nnfolder-article-marker "%d %s\n")
826 (cdr group-art) (current-time-string)))))
828 (defun nnfolder-active-number (group)
829 ;; Find the next article number in GROUP.
830 (let ((active (cadr (assoc group nnfolder-group-alist))))
832 (setcdr active (1+ (cdr active)))
833 ;; This group is new, so we create a new entry for it.
834 ;; This might be a bit naughty... creating groups on the drop of
835 ;; a hat, but I don't know...
836 (push (list group (setq active (cons 1 1)))
837 nnfolder-group-alist))
840 (defun nnfolder-possibly-change-folder (group)
841 (let ((inf (assoc group nnfolder-buffer-alist)))
843 (gnus-buffer-live-p (cadr inf)))
844 (set-buffer (cadr inf))
846 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
847 (when nnfolder-group-alist
848 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
849 (push (list group (nnfolder-read-folder group))
850 nnfolder-buffer-alist))))
852 ;; This method has a problem if you've accidentally let the active
853 ;; list get out of sync with the files. This could happen, say, if
854 ;; you've accidentally gotten new mail with something other than Gnus
855 ;; (but why would _that_ ever happen? :-). In that case, we will be
856 ;; in the middle of processing the file, ready to add new X-Gnus
857 ;; article number markers, and we'll run across a message with no ID
858 ;; yet - the active list _may_not_ be ready for us yet.
860 ;; To handle this, I'm modifying this routine to maintain the maximum
861 ;; ID seen so far, and when we hit a message with no ID, we will
862 ;; _manually_ scan the rest of the message looking for any more,
863 ;; possibly higher IDs. We'll assume the maximum that we find is the
864 ;; highest active. Note that this shouldn't cost us much extra time
865 ;; at all, but will be a lot less vulnerable to glitches between the
866 ;; mbox and the active file.
868 (defun nnfolder-read-folder (group)
869 (let* ((file (nnfolder-group-pathname group))
870 (nov (nnfolder-group-nov-pathname group))
872 (let ((nnheader-file-coding-system
873 nnfolder-file-coding-system))
874 (nnheader-find-file-noselect file)))))
875 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
876 (if (equal (cadr (assoc group nnfolder-scantime-alist))
877 (nth 5 (file-attributes file)))
878 ;; This looks up-to-date, so we don't do any scanning.
879 (if (file-exists-p file)
881 (push (list group buffer) nnfolder-buffer-alist)
882 (set-buffer-modified-p t)
883 (nnfolder-save-buffer))
884 ;; Parse the damn thing.
886 (goto-char (point-min))
887 ;; Remove any blank lines at the start.
888 (while (eq (following-char) ?\n)
890 (nnmail-activate 'nnfolder)
892 (let ((delim "^From ")
893 (marker (concat "\n" nnfolder-article-marker))
895 (active (or (cadr (assoc group nnfolder-group-alist))
897 (scantime (assoc group nnfolder-scantime-alist))
899 maxid start end newscantime
900 novbuf articles newnum
902 (buffer-disable-undo)
903 (setq maxid (cdr active))
905 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
906 (and (file-exists-p nov)
907 (file-newer-than-file-p nov file)))
908 (unless (file-exists-p nov)
909 (gnus-make-directory (file-name-directory nov)))
911 (setq novbuf (nnfolder-open-nov group))
912 (goto-char (point-min))
914 (push (read novbuf) articles)
916 (setq articles (nreverse articles))))
917 (goto-char (point-min))
919 ;; Anytime the active number is 1 or 0, it is suspect. In
920 ;; that case, search the file manually to find the active
921 ;; number. Or, of course, if we're being paranoid. (This
922 ;; would also be the place to build other lists from the
923 ;; header markers, such as expunge lists, etc., if we ever
924 ;; desired to abandon the active file entirely for mboxes.)
925 (when (or nnfolder-ignore-active-file
928 (while (and (search-forward marker nil t)
930 (setq newnum (string-to-number (match-string 0)))
931 (when (nnmail-within-headers-p)
932 (setq maxid (max maxid newnum)
933 minid (min minid newnum))
935 (if (memq newnum articles)
936 (setq articles (delq newnum articles))
937 (let ((headers (nnfolder-parse-head newnum)))
938 (with-current-buffer novbuf
939 (nnheader-find-nov-line newnum)
940 (nnheader-insert-nov headers)))))))
941 (when (and novbuf articles)
942 (with-current-buffer novbuf
943 (dolist (article articles)
944 (when (nnheader-find-nov-line article)
945 (delete-region (point)
946 (progn (forward-line 1) (point)))))))
947 (setcar active (max 1 (min minid maxid)))
948 (setcdr active (max maxid (cdr active)))
949 (goto-char (point-min)))
951 ;; As long as we trust that the user will only insert
952 ;; unmarked mail at the end, go to the end and search
953 ;; backwards for the last marker. Find the start of that
954 ;; message, and begin to search for unmarked messages from
956 (when (not (or nnfolder-distrust-mbox
958 (goto-char (point-max))
959 (unless (re-search-backward marker nil t)
960 (goto-char (point-min)))
961 ;;(when (nnmail-search-unix-mail-delim)
962 ;; (goto-char (point-min)))
965 ;; Keep track of the active number on our own, and insert it
966 ;; back into the active list when we're done. Also, prime
967 ;; the pump to cut down on the number of searches we do.
968 (unless (nnmail-search-unix-mail-delim)
969 (goto-char (point-max)))
970 (setq end (point-marker))
971 (while (not (= end (point-max)))
972 (setq start (marker-position end))
974 ;; There may be more than one "From " line, so we skip past
976 (while (looking-at delim)
978 (set-marker end (if (nnmail-search-unix-mail-delim)
982 (when (not (search-forward marker end t))
983 (narrow-to-region start end)
984 (nnmail-insert-lines)
985 (nnfolder-insert-newsgroup-line
988 (nnfolder-active-number group))))
990 (let ((headers (nnfolder-parse-head newnum (point-min)
992 (with-current-buffer novbuf
993 (goto-char (point-max))
994 (nnheader-insert-nov headers))))
998 ;; Make absolutely sure that the active list reflects
1000 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
1002 ;; Set the scantime for this group.
1003 (setq newscantime (visited-file-modtime))
1005 (setcdr scantime (list newscantime))
1006 (push (list group newscantime)
1007 nnfolder-scantime-alist))
1010 (nnfolder-save-nov))
1011 (current-buffer))))))
1014 (defun nnfolder-generate-active-file ()
1015 "Look for mbox folders in the nnfolder directory and make them into groups.
1016 This command does not work if you use short group names."
1018 (nnmail-activate 'nnfolder)
1019 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1020 (dolist (file (directory-files (or nnfolder-nov-directory
1024 (regexp-quote nnfolder-nov-file-suffix)
1026 (when (not (message-mail-file-mbox-p file))
1028 (delete-file file)))))
1029 (let ((files (directory-files nnfolder-directory))
1031 (while (setq file (pop files))
1032 (when (and (not (backup-file-name-p file))
1033 (message-mail-file-mbox-p
1034 (nnheader-concat nnfolder-directory file)))
1035 (let ((oldgroup (assoc file nnfolder-group-alist)))
1037 (nnheader-message 5 "Refreshing group %s..." file)
1038 (nnheader-message 5 "Adding group %s..." file))
1040 (setq nnfolder-group-alist
1041 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1042 (push (list file (cons 1 0)) nnfolder-group-alist)
1043 (nnfolder-possibly-change-folder file)
1044 (nnfolder-possibly-change-group file)
1045 (nnfolder-close-group file))))
1046 (nnheader-message 5 "")))
1048 (defun nnfolder-group-pathname (group)
1049 "Make pathname for GROUP."
1051 (mm-encode-coding-string group nnmail-pathname-coding-system))
1052 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1053 ;; If this file exists, we use it directly.
1054 (if (or nnmail-use-long-file-names
1055 (file-exists-p (concat dir group)))
1057 ;; If not, we translate dots into slashes.
1058 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1060 (defun nnfolder-group-nov-pathname (group)
1061 "Make pathname for GROUP NOV."
1062 (let ((nnfolder-directory
1063 (or nnfolder-nov-directory nnfolder-directory)))
1064 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1066 (defun nnfolder-save-buffer ()
1068 (when (buffer-modified-p)
1069 (run-hooks 'nnfolder-save-buffer-hook)
1070 (gnus-make-directory (file-name-directory (buffer-file-name)))
1071 (let ((coding-system-for-write
1072 (or nnfolder-file-coding-system-for-write
1073 nnfolder-file-coding-system)))
1075 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1076 (nnfolder-save-nov)))
1078 (defun nnfolder-save-active (group-alist active-file)
1079 (let ((nnmail-active-file-coding-system
1080 (or nnfolder-active-file-coding-system-for-write
1081 nnfolder-active-file-coding-system)))
1082 (nnmail-save-active group-alist active-file)))
1084 (defun nnfolder-open-nov (group)
1085 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1086 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1089 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1090 (nnfolder-group-nov-pathname group))
1092 (when (file-exists-p nnfolder-nov-buffer-file-name)
1093 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1094 (push (cons group buffer) nnfolder-nov-buffer-alist)
1097 (defun nnfolder-save-nov ()
1099 (while nnfolder-nov-buffer-alist
1100 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1101 (set-buffer (cdar nnfolder-nov-buffer-alist))
1102 (when (buffer-modified-p)
1103 (gnus-make-directory (file-name-directory
1104 nnfolder-nov-buffer-file-name))
1105 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1107 (set-buffer-modified-p nil)
1108 (kill-buffer (current-buffer)))
1109 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1111 (defun nnfolder-nov-delete-article (group article)
1113 (set-buffer (nnfolder-open-nov group))
1114 (when (nnheader-find-nov-line article)
1115 (delete-region (point) (progn (forward-line 1) (point))))
1118 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1119 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1121 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1122 (when (file-exists-p nov)
1124 (set-buffer nntp-server-buffer)
1126 (nnheader-insert-file-contents nov)
1128 (not (numberp fetch-old)))
1129 t ; Don't remove anything.
1130 (nnheader-nov-delete-outside-range
1131 (if fetch-old (max 1 (- (car articles) fetch-old))
1133 (car (last articles)))
1136 (defun nnfolder-parse-head (&optional number b e)
1137 "Parse the head of the current buffer."
1138 (let ((buf (current-buffer))
1142 (setq b (if (nnmail-search-unix-mail-delim-backward)
1143 (point) (point-min)))
1145 (setq e (if (nnmail-search-unix-mail-delim)
1146 (point) (point-max))))
1147 (setq chars (- e b))
1148 (unless (zerop chars)
1150 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1152 (insert-buffer-substring buf b e)
1153 (let ((headers (nnheader-parse-naked-head)))
1154 (mail-header-set-chars headers chars)
1155 (mail-header-set-number headers number)
1158 (defun nnfolder-add-nov (group article headers)
1159 "Add a nov line for the GROUP base."
1161 (set-buffer (nnfolder-open-nov group))
1162 (goto-char (point-max))
1163 (mail-header-set-number headers article)
1164 (nnheader-insert-nov headers)))
1166 (deffoo nnfolder-request-set-mark (group actions &optional server)
1168 (not (nnfolder-server-opened server)))
1169 (nnfolder-open-server server))
1170 (unless nnfolder-marks-is-evil
1171 (nnfolder-open-marks group server)
1172 (dolist (action actions)
1173 (let ((range (nth 0 action))
1174 (what (nth 1 action))
1175 (marks (nth 2 action)))
1176 (assert (or (eq what 'add) (eq what 'del)) t
1177 "Unknown request-set-mark action: %s" what)
1178 (dolist (mark marks)
1179 (setq nnfolder-marks (gnus-update-alist-soft
1181 (funcall (if (eq what 'add) 'gnus-range-add
1182 'gnus-remove-from-range)
1183 (cdr (assoc mark nnfolder-marks)) range)
1185 (nnfolder-save-marks group server))
1188 (deffoo nnfolder-request-update-info (group info &optional server)
1191 (not (nnfolder-server-opened server)))
1192 (nnfolder-open-server server))
1193 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1194 (nnheader-message 8 "Updating marks for %s..." group)
1195 (nnfolder-open-marks group server)
1196 ;; Update info using `nnfolder-marks'.
1197 (mapcar (lambda (pred)
1198 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1199 (gnus-info-set-marks
1201 (gnus-update-alist-soft
1203 (cdr (assq (cdr pred) nnfolder-marks))
1204 (gnus-info-marks info))
1206 gnus-article-mark-lists)
1207 (let ((seen (cdr (assq 'read nnfolder-marks))))
1208 (gnus-info-set-read info
1209 (if (and (integerp (car seen))
1211 (list (cons (car seen) (car seen)))
1213 (nnheader-message 8 "Updating marks for %s...done" group))
1216 (defun nnfolder-group-marks-pathname (group)
1217 "Make pathname for GROUP NOV."
1218 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1219 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1221 (defun nnfolder-marks-changed-p (group)
1222 (let ((file (nnfolder-group-marks-pathname group)))
1223 (if (null (gnus-gethash file nnfolder-marks-modtime))
1224 t ;; never looked at marks file, assume it has changed
1225 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1226 (nth 5 (file-attributes file)))))))
1228 (defun nnfolder-save-marks (group server)
1229 (let ((file-name-coding-system nnmail-pathname-coding-system)
1230 (file (nnfolder-group-marks-pathname group)))
1233 (with-temp-file file
1235 (gnus-prin1 nnfolder-marks)
1238 (nth 5 (file-attributes file))
1239 nnfolder-marks-modtime))
1240 (error (or (gnus-yes-or-no-p
1241 (format "Could not write to %s (%s). Continue? " file err))
1242 (error "Cannot write to %s (%s)" err))))))
1244 (defun nnfolder-open-marks (group server)
1245 (let ((file (nnfolder-group-marks-pathname group)))
1246 (if (file-exists-p file)
1249 (gnus-sethash file (nth 5 (file-attributes file))
1250 nnfolder-marks-modtime)
1251 (nnheader-insert-file-contents file)
1252 (setq nnfolder-marks (read (current-buffer)))
1253 (dolist (el gnus-article-unpropagated-mark-lists)
1254 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1255 (error (or (gnus-yes-or-no-p
1256 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1257 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1258 ;; User didn't have a .marks file. Probably first time
1259 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1260 (let ((info (gnus-get-info
1261 (gnus-group-prefixed-name
1263 (gnus-server-to-method (format "nnfolder:%s" server))))))
1264 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1265 (setq nnfolder-marks (gnus-info-marks info))
1266 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1267 (dolist (el gnus-article-unpropagated-mark-lists)
1268 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1269 (nnfolder-save-marks group server)
1270 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1274 ;;; nnfolder.el ends here