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.
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'gnus-clfns))
45 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
46 (autoload 'gnus-intersection "gnus-range"))
48 (nnoo-declare nnfolder)
50 (defvoo nnfolder-directory (expand-file-name message-directory)
51 "The name of the nnfolder directory.
53 This variable is a virtual server slot. See the Gnus manual for details.")
55 (defvoo nnfolder-nov-directory nil
56 "The name of the nnfolder NOV directory.
57 If nil, `nnfolder-directory' is used.")
59 (defvoo nnfolder-marks-directory nil
60 "The name of the nnfolder MARKS directory.
61 If nil, `nnfolder-directory' is used.")
63 (defvoo nnfolder-active-file
64 (nnheader-concat nnfolder-directory "active")
65 "The name of the active file.
67 This variable is a virtual server slot. See the Gnus manual for details.")
69 ;; I renamed this variable to something more in keeping with the general GNU
72 (defvoo nnfolder-ignore-active-file nil
73 "If non-nil, the active file is ignored.
74 This causes nnfolder to do some extra work in order to determine the
75 true active ranges of an mbox file. Note that the active file is
76 still saved, but its values are not used. This costs some extra time
77 when scanning an mbox when opening it.")
79 (defvoo nnfolder-distrust-mbox nil
80 "If non-nil, the folder will be distrusted.
81 This means that nnfolder will not trust the user with respect to
82 inserting unaccounted for mail in the middle of an mbox file. This
83 can greatly slow down scans, which now must scan the entire file for
84 unmarked messages. When nil, scans occur forward from the last marked
85 message, a huge time saver for large mailboxes.")
87 (defvoo nnfolder-newsgroups-file
88 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
89 "Mail newsgroups description file.
91 This variable is a virtual server slot. See the Gnus manual for details.")
93 (defvoo nnfolder-get-new-mail t
94 "If non-nil, nnfolder will check the incoming mail file and split the mail.
96 This variable is a virtual server slot. See the Gnus manual for details.")
98 (defvoo nnfolder-prepare-save-mail-hook nil
99 "Hook run narrowed to an article before saving.")
101 (defvoo nnfolder-save-buffer-hook nil
102 "Hook run before saving the nnfolder mbox buffer.
104 This variable is a virtual server slot. See the Gnus manual for details.")
107 (defvoo nnfolder-inhibit-expiry nil
108 "If non-nil, inhibit expiry.
110 This variable is a virtual server slot. See the Gnus manual for details.")
114 (defconst nnfolder-version "nnfolder 2.0"
117 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
118 "String used to demarcate what the article number for a message is.")
120 (defvoo nnfolder-current-group nil)
121 (defvoo nnfolder-current-buffer nil)
122 (defvoo nnfolder-status-string "")
123 (defvoo nnfolder-group-alist nil)
124 (defvoo nnfolder-buffer-alist nil)
125 (defvoo nnfolder-scantime-alist nil)
126 (defvoo nnfolder-active-timestamp nil)
127 (defvoo nnfolder-active-file-coding-system nnheader-text-coding-system)
128 (defvoo nnfolder-active-file-coding-system-for-write
129 nnmail-active-file-coding-system)
130 (defvoo nnfolder-file-coding-system nnheader-text-coding-system)
131 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
132 "Coding system for save nnfolder file.
133 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
135 (defvoo nnfolder-nov-is-evil nil
136 "If non-nil, Gnus will never generate and use nov databases for mail groups.
137 Using nov databases will speed up header fetching considerably.
138 This variable shouldn't be flipped much. If you have, for some reason,
139 set this to t, and want to set it to nil again, you should always run
140 the `nnfolder-generate-active-file' command. The function will go
141 through all nnfolder directories and generate nov databases for them
142 all. This may very well take some time.
144 This variable is a virtual server slot. See the Gnus manual for details.")
146 (defvoo nnfolder-nov-file-suffix ".nov")
148 (defvoo nnfolder-nov-buffer-alist nil)
150 (defvar nnfolder-nov-buffer-file-name nil)
152 (defvoo nnfolder-marks-is-evil nil
153 "If non-nil, Gnus will never generate and use marks file for mail groups.
154 Using marks files makes it possible to backup and restore mail groups
155 separately from `.newsrc.eld'. If you have, for some reason, set
156 this to t, and want to set it to nil again, you should always remove
157 the corresponding marks file (usually base nnfolder file name
158 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
159 the group. Then the marks file will be regenerated properly by Gnus.
161 This variable is a virtual server slot. See the Gnus manual for details.")
163 (defvoo nnfolder-marks nil)
165 (defvoo nnfolder-marks-file-suffix ".mrk")
167 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
171 ;;; Interface functions
173 (nnoo-define-basics nnfolder)
175 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
177 (set-buffer nntp-server-buffer)
179 (let (article start stop)
180 (nnfolder-possibly-change-group group server)
181 (when nnfolder-current-buffer
182 (set-buffer nnfolder-current-buffer)
183 (goto-char (point-min))
184 (if (stringp (car articles))
186 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
188 (setq articles (gnus-sorted-intersection
189 ;; Is ARTICLES sorted?
191 (nnfolder-existing-articles)))
192 (while (setq article (pop articles))
193 (set-buffer nnfolder-current-buffer)
194 (when (nnfolder-goto-article article)
196 (setq stop (if (search-forward "\n\n" nil t)
199 (set-buffer nntp-server-buffer)
200 (insert (format "221 %d Article retrieved.\n" article))
201 (insert-buffer-substring nnfolder-current-buffer start stop)
202 (goto-char (point-max))
204 (set-buffer nntp-server-buffer)
205 (nnheader-fold-continuation-lines)
208 (deffoo nnfolder-open-server (server &optional defs)
209 (nnoo-change-server 'nnfolder server defs)
210 (nnmail-activate 'nnfolder t)
211 (gnus-make-directory nnfolder-directory)
212 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
213 (and nnfolder-nov-directory
214 (gnus-make-directory nnfolder-nov-directory)))
215 (unless nnfolder-marks-is-evil
216 (and nnfolder-marks-directory
217 (gnus-make-directory nnfolder-marks-directory)))
219 ((not (file-exists-p nnfolder-directory))
220 (nnfolder-close-server)
221 (nnheader-report 'nnfolder "Couldn't create directory: %s"
223 ((not (file-directory-p (file-truename nnfolder-directory)))
224 (nnfolder-close-server)
225 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
227 (nnmail-activate 'nnfolder)
228 (nnheader-report 'nnfolder "Opened server %s using directory %s"
229 server nnfolder-directory)
232 (deffoo nnfolder-request-close ()
233 (let ((alist nnfolder-buffer-alist))
235 (nnfolder-close-group (caar alist) nil t)
236 (setq alist (cdr alist))))
237 (nnoo-close-server 'nnfolder)
238 (setq nnfolder-buffer-alist nil
239 nnfolder-group-alist nil))
241 (deffoo nnfolder-request-article (article &optional group server buffer)
242 (nnfolder-possibly-change-group group server)
244 (set-buffer nnfolder-current-buffer)
245 (goto-char (point-min))
246 (when (nnfolder-goto-article article)
250 (unless (and (nnmail-search-unix-mail-delim)
252 (goto-char (point-max)))
254 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
255 (set-buffer nntp-server-buffer)
257 (insert-buffer-substring nnfolder-current-buffer start stop)
258 (goto-char (point-min))
259 (while (looking-at "From ")
261 (insert "X-From-Line: ")
263 (if (numberp article)
264 (cons nnfolder-current-group article)
265 (goto-char (point-min))
266 (cons nnfolder-current-group
267 (if (search-forward (concat "\n" nnfolder-article-marker)
271 (point) (progn (end-of-line) (point))))
274 (deffoo nnfolder-request-group (group &optional server dont-check)
275 (nnfolder-possibly-change-group group server t)
277 (if (not (assoc group nnfolder-group-alist))
278 (nnheader-report 'nnfolder "No such group: %s" group)
281 (nnheader-report 'nnfolder "Selected group %s" group)
283 (let* ((active (assoc group nnfolder-group-alist))
285 (range (cadr active)))
288 (nnheader-report 'nnfolder "No such group: %s" group))
289 ((null nnfolder-current-group)
290 (nnheader-report 'nnfolder "Empty group: %s" group))
292 (nnheader-report 'nnfolder "Selected group %s" group)
293 (nnheader-insert "211 %d %d %d %s\n"
294 (1+ (- (cdr range) (car range)))
295 (car range) (cdr range) group))))))))
297 (deffoo nnfolder-request-scan (&optional group server)
298 (nnfolder-possibly-change-group nil server)
299 (when nnfolder-get-new-mail
300 (nnfolder-possibly-change-group group server)
304 (let ((bufs nnfolder-buffer-alist))
307 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
308 (setq nnfolder-buffer-alist
309 (delq (car bufs) nnfolder-buffer-alist))
310 (set-buffer (nth 1 (car bufs)))
311 (nnfolder-save-buffer)
312 (kill-buffer (current-buffer)))
313 (setq bufs (cdr bufs))))))
317 ;; Don't close the buffer if we're not shutting down the server. This way,
318 ;; we can keep the buffer in the group buffer cache, and not have to grovel
319 ;; over the buffer again unless we add new mail to it or modify it in some
322 (deffoo nnfolder-close-group (group &optional server force)
323 ;; Make sure we _had_ the group open.
324 (when (or (assoc group nnfolder-buffer-alist)
325 (equal group nnfolder-current-group))
326 (let ((inf (assoc group nnfolder-buffer-alist)))
328 (when (and nnfolder-current-group
329 nnfolder-current-buffer)
330 (push (list nnfolder-current-group nnfolder-current-buffer)
331 nnfolder-buffer-alist))
332 (setq nnfolder-buffer-alist
333 (delq inf nnfolder-buffer-alist))
334 (setq nnfolder-current-buffer (cadr inf)
335 nnfolder-current-group (car inf))))
336 (when (and nnfolder-current-buffer
337 (buffer-name nnfolder-current-buffer))
339 (set-buffer nnfolder-current-buffer)
340 ;; If the buffer was modified, write the file out now.
341 (nnfolder-save-buffer)
342 ;; If we're shutting the server down, we need to kill the
343 ;; buffer and remove it from the open buffer list. Or, of
344 ;; course, if we're trying to minimize our space impact.
345 (kill-buffer (current-buffer))
346 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
347 nnfolder-buffer-alist)))))
348 (setq nnfolder-current-group nil
349 nnfolder-current-buffer nil)
352 (deffoo nnfolder-request-create-group (group &optional server args)
353 (nnfolder-possibly-change-group nil server)
354 (nnmail-activate 'nnfolder)
356 (unless (assoc group nnfolder-group-alist)
357 (push (list group (cons 1 0)) nnfolder-group-alist)
358 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
359 (nnfolder-read-folder group)))
362 (deffoo nnfolder-request-list (&optional server)
363 (nnfolder-possibly-change-group nil server)
365 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
366 (nnmail-find-file nnfolder-active-file)
367 (setq nnfolder-group-alist (nnmail-get-active)))
370 (deffoo nnfolder-request-newgroups (date &optional server)
371 (nnfolder-possibly-change-group nil server)
372 (nnfolder-request-list server))
374 (deffoo nnfolder-request-list-newsgroups (&optional server)
375 (nnfolder-possibly-change-group nil server)
377 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
378 (nnmail-find-file nnfolder-newsgroups-file))))
380 ;; Return a list consisting of all article numbers existing in the
383 (defun nnfolder-existing-articles ()
385 (when nnfolder-current-buffer
386 (set-buffer nnfolder-current-buffer)
387 (goto-char (point-min))
388 (let ((marker (concat "\n" nnfolder-article-marker))
391 (while (and (search-forward marker nil t)
392 (re-search-forward number nil t))
393 (let ((newnum (string-to-number (match-string 0))))
394 (if (nnmail-within-headers-p)
395 (push newnum numbers))))
396 ;; The article numbers are increasing, so this result is sorted.
397 (nreverse numbers)))))
399 (deffoo nnfolder-request-expire-articles
400 (articles newsgroup &optional server force)
401 (nnfolder-possibly-change-group newsgroup server)
403 ;; The articles we have deleted so far.
404 (deleted-articles nil)
405 ;; The articles that really exist and will
406 ;; be expired if they are old enough.
408 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
409 (nnmail-activate 'nnfolder)
412 (set-buffer nnfolder-current-buffer)
413 ;; Since messages are sorted in arrival order and expired in the
414 ;; same order, we can stop as soon as we find a message that is
416 (while (and maybe-expirable is-old)
417 (goto-char (point-min))
418 (when (and (nnfolder-goto-article (car maybe-expirable))
419 (search-forward (concat "\n" nnfolder-article-marker)
423 (nnmail-expired-article-p
426 (point) (progn (end-of-line) (point)))
427 force nnfolder-inhibit-expiry))
428 (unless (eq nnmail-expiry-target 'delete)
430 (nnfolder-request-article (car maybe-expirable)
431 newsgroup server (current-buffer))
432 (let ((nnfolder-current-directory nil))
433 (nnmail-expiry-target-group
434 nnmail-expiry-target newsgroup)))
435 (nnfolder-possibly-change-group newsgroup server))
436 (nnheader-message 5 "Deleting article %d in %s..."
437 (car maybe-expirable) newsgroup)
438 (nnfolder-delete-mail)
439 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
440 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
441 ;; Must remember which articles were actually deleted
442 (push (car maybe-expirable) deleted-articles)))
443 (setq maybe-expirable (cdr maybe-expirable)))
444 (unless nnfolder-inhibit-expiry
445 (nnheader-message 5 "Deleting articles...done"))
446 (nnfolder-save-buffer)
447 (nnfolder-adjust-min-active newsgroup)
448 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
449 (gnus-sorted-complement articles (nreverse deleted-articles)))))
451 (deffoo nnfolder-request-move-article (article group server
452 accept-form &optional last)
454 (let ((buf (get-buffer-create " *nnfolder move*"))
457 (nnfolder-request-article article group server)
461 (insert-buffer-substring nntp-server-buffer)
462 (goto-char (point-min))
463 (while (re-search-forward
464 (concat "^" nnfolder-article-marker)
465 (save-excursion (and (search-forward "\n\n" nil t) (point)))
467 (delete-region (progn (beginning-of-line) (point))
468 (progn (forward-line 1) (point))))
469 (setq result (eval accept-form))
473 (nnfolder-possibly-change-group group server)
474 (set-buffer nnfolder-current-buffer)
475 (goto-char (point-min))
476 (when (nnfolder-goto-article article)
477 (nnfolder-delete-mail))
478 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
479 (nnfolder-nov-delete-article group article))
481 (nnfolder-save-buffer)
482 (nnfolder-adjust-min-active group)
483 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
486 (deffoo nnfolder-request-accept-article (group &optional server last)
488 (nnfolder-possibly-change-group group server)
489 (nnmail-check-syntax)
490 (let ((buf (current-buffer))
492 (goto-char (point-min))
493 (when (looking-at "X-From-Line: ")
494 (replace-match "From "))
496 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
497 (nntp-server-buffer (current-buffer)))
498 (nnmail-find-file nnfolder-active-file)
499 (setq nnfolder-group-alist (nnmail-parse-active))))
501 (goto-char (point-min))
502 (if (search-forward "\n\n" nil t)
504 (goto-char (point-max)))
505 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
506 (delete-region (point) (progn (forward-line 1) (point))))
507 (when nnmail-cache-accepted-message-ids
508 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
509 (setq result (if (stringp group)
510 (list (cons group (nnfolder-active-number group)))
512 (nnmail-article-group 'nnfolder-active-number))))
513 (if (and (null result)
514 (yes-or-no-p "Moved to `junk' group; delete article? "))
517 (car (nnfolder-save-mail result)))))
520 (nnfolder-possibly-change-folder (or (caar art-group) group))
521 (nnfolder-save-buffer)
522 (when nnmail-cache-accepted-message-ids
523 (nnmail-cache-close))))
524 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
526 (nnheader-report 'nnfolder "Couldn't store article"))
529 (deffoo nnfolder-request-replace-article (article group buffer)
530 (nnfolder-possibly-change-group group)
533 (goto-char (point-min))
535 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
536 (setq xfrom (match-string 1))
538 (goto-char (point-min))
540 (insert "From " xfrom "\n")
541 (unless (looking-at "From ")
542 (insert "From nobody " (current-time-string) "\n"))))
543 (nnfolder-normalize-buffer)
544 (set-buffer nnfolder-current-buffer)
545 (goto-char (point-min))
546 (if (not (nnfolder-goto-article article))
548 (nnfolder-delete-mail)
549 (insert-buffer-substring buffer)
550 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
553 (let ((headers (nnfolder-parse-head article
554 (point-min) (point-max))))
555 (with-current-buffer (nnfolder-open-nov group)
556 (if (nnheader-find-nov-line article)
557 (delete-region (point) (progn (forward-line 1) (point))))
558 (nnheader-insert-nov headers)))))
559 (nnfolder-save-buffer)
562 (deffoo nnfolder-request-delete-group (group &optional force server)
563 (nnfolder-close-group group server t)
564 ;; Delete all articles in GROUP.
566 () ; Don't delete the articles.
567 ;; Delete the file that holds the group.
569 (delete-file (nnfolder-group-pathname group))
570 (when (file-exists-p (nnfolder-group-nov-pathname group))
571 (delete-file (nnfolder-group-nov-pathname group)))
572 (when (file-exists-p (nnfolder-group-marks-pathname group))
573 (delete-file (nnfolder-group-marks-pathname group)))))
574 ;; Remove the group from all structures.
575 (setq nnfolder-group-alist
576 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
577 nnfolder-current-group nil
578 nnfolder-current-buffer nil)
579 ;; Save the active file.
580 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
583 (deffoo nnfolder-request-rename-group (group new-name &optional server)
584 (nnfolder-possibly-change-group group server)
586 (set-buffer nnfolder-current-buffer)
587 (and (file-writable-p buffer-file-name)
589 (let ((new-file (nnfolder-group-pathname new-name)))
590 (gnus-make-directory (file-name-directory new-file))
591 (rename-file buffer-file-name new-file)
592 (when (file-exists-p (nnfolder-group-nov-pathname group))
593 (setq new-file (nnfolder-group-nov-pathname new-name))
594 (gnus-make-directory (file-name-directory new-file))
595 (rename-file (nnfolder-group-nov-pathname group) new-file))
596 (when (file-exists-p (nnfolder-group-marks-pathname group))
597 (setq new-file (nnfolder-group-marks-pathname new-name))
598 (gnus-make-directory (file-name-directory new-file))
599 (rename-file (nnfolder-group-marks-pathname group) new-file)))
601 ;; That went ok, so we change the internal structures.
602 (let ((entry (assoc group nnfolder-group-alist)))
603 (and entry (setcar entry new-name))
604 (setq nnfolder-current-buffer nil
605 nnfolder-current-group nil)
606 ;; Save the new group alist.
607 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
608 ;; We kill the buffer instead of renaming it and stuff.
609 (kill-buffer (current-buffer))
612 (deffoo nnfolder-request-regenerate (server)
613 (nnfolder-possibly-change-group nil server)
614 (nnfolder-generate-active-file)
618 ;;; Internal functions.
620 (defun nnfolder-adjust-min-active (group)
621 ;; Find the lowest active article in this group.
622 (let* ((active (cadr (assoc group nnfolder-group-alist)))
623 (marker (concat "\n" nnfolder-article-marker))
625 (activemin (cdr active)))
627 (set-buffer nnfolder-current-buffer)
628 (goto-char (point-min))
629 (while (and (search-forward marker nil t)
630 (re-search-forward number nil t))
631 (let ((newnum (string-to-number (match-string 0))))
632 (if (nnmail-within-headers-p)
633 (setq activemin (min activemin newnum)))))
634 (setcar active activemin))))
636 (defun nnfolder-article-string (article)
637 (if (numberp article)
638 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
639 (concat "\nMessage-ID: " article)))
641 (defun nnfolder-goto-article (article)
642 "Place point at the start of the headers of ARTICLE.
643 ARTICLE can be an article number or a Message-ID.
644 Returns t if successful, nil otherwise."
645 (let ((art-string (nnfolder-article-string article))
647 ;; It is likely that we are at or before the delimiter line.
648 ;; We therefore go to the end of the previous line, and start
649 ;; searching from there.
654 ;; First search forward.
655 (while (and (setq found (search-forward art-string nil t))
656 (not (nnmail-within-headers-p))))
657 ;; If unsuccessful, search backward from where we started,
660 (while (and (setq found (search-backward art-string nil t))
661 (not (nnmail-within-headers-p)))))
663 (nnmail-search-unix-mail-delim-backward))))
665 (defun nnfolder-delete-mail (&optional leave-delim)
666 "Delete the message that point is in.
667 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
668 deleted. Point is left where the deleted region was."
672 ;; In case point is at the beginning of the message already.
674 (nnmail-search-unix-mail-delim-backward)
675 (if leave-delim (progn (forward-line 1) (point))
679 (if (nnmail-search-unix-mail-delim)
682 (run-hooks 'nnfolder-delete-mail-hook)
683 (delete-region (point-min) (point-max))))
685 (defun nnfolder-possibly-change-group (group &optional server dont-check)
688 (not (nnfolder-server-opened server)))
689 (nnfolder-open-server server))
690 (unless (gnus-buffer-live-p nnfolder-current-buffer)
691 (setq nnfolder-current-buffer nil
692 nnfolder-current-group nil))
694 (let ((file-name-coding-system nnmail-pathname-coding-system)
695 (pathname-coding-system nnmail-pathname-coding-system))
697 (not (equal group nnfolder-current-group))
699 (nnmail-activate 'nnfolder)
700 (and (assoc group nnfolder-group-alist)
701 (file-exists-p (nnfolder-group-pathname group)))))
703 (setq nnfolder-current-group group
704 nnfolder-current-buffer nil)
706 ;; If we have to change groups, see if we don't already have the
707 ;; folder in memory. If we do, verify the modtime and destroy
708 ;; the folder if needed so we can rescan it.
709 (setq nnfolder-current-buffer
710 (nth 1 (assoc group nnfolder-buffer-alist)))
712 ;; If the buffer is not live, make sure it isn't in the alist. If it
713 ;; is live, verify that nobody else has touched the file since last
715 (when (and nnfolder-current-buffer
716 (not (gnus-buffer-live-p nnfolder-current-buffer)))
717 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
718 nnfolder-current-buffer nil))
720 (setq nnfolder-current-group group)
722 (when (or (not nnfolder-current-buffer)
723 (not (verify-visited-file-modtime
724 nnfolder-current-buffer)))
726 (setq file (nnfolder-group-pathname group))
727 ;; See whether we need to create the new file.
728 (unless (file-exists-p file)
729 (gnus-make-directory (file-name-directory file))
730 (let ((nnmail-file-coding-system
731 (or nnfolder-file-coding-system-for-write
732 nnfolder-file-coding-system-for-write)))
733 (nnmail-write-region 1 1 file t 'nomesg)))
734 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
735 (set-buffer nnfolder-current-buffer)
736 (push (list group nnfolder-current-buffer)
737 nnfolder-buffer-alist)))))))))
739 (defun nnfolder-save-mail (group-art-list)
740 "Called narrowed to an article."
741 (let* (save-list group-art)
742 (goto-char (point-min))
743 ;; The From line may have been quoted by movemail.
744 (when (looking-at ">From")
746 ;; This might come from somewhere else.
747 (unless (looking-at "From ")
748 (insert "From nobody " (current-time-string) "\n")
749 (goto-char (point-min)))
750 ;; Quote all "From " lines in the article.
752 (let (case-fold-search)
753 (while (re-search-forward "^From " nil t)
756 (setq save-list group-art-list)
757 (nnmail-insert-lines)
758 (nnmail-insert-xref group-art-list)
759 (run-hooks 'nnmail-prepare-save-mail-hook)
760 (run-hooks 'nnfolder-prepare-save-mail-hook)
762 ;; Insert the mail into each of the destination groups.
763 (while (setq group-art (pop group-art-list))
764 ;; Kill any previous newsgroup markers.
765 (goto-char (point-min))
766 (if (search-forward "\n\n" nil t)
768 (goto-char (point-max)))
769 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
770 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
772 ;; Insert the new newsgroup marker.
773 (nnfolder-insert-newsgroup-line group-art)
776 (let ((beg (point-min))
778 (obuf (current-buffer)))
779 (nnfolder-possibly-change-folder (car group-art))
780 (let ((buffer-read-only nil))
781 (nnfolder-normalize-buffer)
782 (insert-buffer-substring obuf beg end))
783 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
785 (nnfolder-add-nov (car group-art) (cdr group-art)
786 (nnfolder-parse-head nil beg end))))))
788 ;; Did we save it anywhere?
791 (defun nnfolder-normalize-buffer ()
792 "Make sure there are two newlines at the end of the buffer."
793 (goto-char (point-max))
794 (skip-chars-backward "\n")
795 (delete-region (point) (point-max))
799 (defun nnfolder-insert-newsgroup-line (group-art)
801 (goto-char (point-min))
802 (unless (search-forward "\n\n" nil t)
803 (goto-char (point-max))
806 (insert (format (concat nnfolder-article-marker "%d %s\n")
807 (cdr group-art) (current-time-string)))))
809 (defun nnfolder-active-number (group)
810 ;; Find the next article number in GROUP.
811 (let ((active (cadr (assoc group nnfolder-group-alist))))
813 (setcdr active (1+ (cdr active)))
814 ;; This group is new, so we create a new entry for it.
815 ;; This might be a bit naughty... creating groups on the drop of
816 ;; a hat, but I don't know...
817 (push (list group (setq active (cons 1 1)))
818 nnfolder-group-alist))
821 (defun nnfolder-possibly-change-folder (group)
822 (let ((inf (assoc group nnfolder-buffer-alist)))
824 (gnus-buffer-live-p (cadr inf)))
825 (set-buffer (cadr inf))
827 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
828 (when nnfolder-group-alist
829 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
830 (push (list group (nnfolder-read-folder group))
831 nnfolder-buffer-alist))))
833 ;; This method has a problem if you've accidentally let the active
834 ;; list get out of sync with the files. This could happen, say, if
835 ;; you've accidentally gotten new mail with something other than Gnus
836 ;; (but why would _that_ ever happen? :-). In that case, we will be
837 ;; in the middle of processing the file, ready to add new X-Gnus
838 ;; article number markers, and we'll run across a message with no ID
839 ;; yet - the active list _may_not_ be ready for us yet.
841 ;; To handle this, I'm modifying this routine to maintain the maximum
842 ;; ID seen so far, and when we hit a message with no ID, we will
843 ;; _manually_ scan the rest of the message looking for any more,
844 ;; possibly higher IDs. We'll assume the maximum that we find is the
845 ;; highest active. Note that this shouldn't cost us much extra time
846 ;; at all, but will be a lot less vulnerable to glitches between the
847 ;; mbox and the active file.
849 (defun nnfolder-read-folder (group)
850 (let* ((file (nnfolder-group-pathname group))
851 (nov (nnfolder-group-nov-pathname group))
853 (let ((nnheader-file-coding-system
854 nnfolder-file-coding-system))
855 (nnheader-find-file-noselect file)))))
856 (if (equal (cadr (assoc group nnfolder-scantime-alist))
857 (nth 5 (file-attributes file)))
858 ;; This looks up-to-date, so we don't do any scanning.
859 (if (file-exists-p file)
861 (push (list group buffer) nnfolder-buffer-alist)
862 (set-buffer-modified-p t)
863 (nnfolder-save-buffer))
864 ;; Parse the damn thing.
866 (goto-char (point-min))
867 ;; Remove any blank lines at the start.
868 (while (eq (following-char) ?\n)
870 (nnmail-activate 'nnfolder)
872 (let ((delim "^From ")
873 (marker (concat "\n" nnfolder-article-marker))
875 (active (or (cadr (assoc group nnfolder-group-alist))
877 (scantime (assoc group nnfolder-scantime-alist))
879 maxid start end newscantime
880 novbuf articles newnum
882 (buffer-disable-undo)
883 (setq maxid (cdr active))
885 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
886 (and (file-exists-p nov)
887 (file-newer-than-file-p nov file)))
888 (unless (file-exists-p nov)
889 (gnus-make-directory (file-name-directory nov)))
891 (setq novbuf (nnfolder-open-nov group))
892 (goto-char (point-min))
894 (push (read novbuf) articles)
896 (setq articles (nreverse articles))))
897 (goto-char (point-min))
899 ;; Anytime the active number is 1 or 0, it is suspect. In that
900 ;; case, search the file manually to find the active number. Or,
901 ;; of course, if we're being paranoid. (This would also be the
902 ;; place to build other lists from the header markers, such as
903 ;; expunge lists, etc., if we ever desired to abandon the active
904 ;; file entirely for mboxes.)
905 (when (or nnfolder-ignore-active-file
908 (while (and (search-forward marker nil t)
910 (setq newnum (string-to-number (match-string 0)))
911 (when (nnmail-within-headers-p)
912 (setq maxid (max maxid newnum)
913 minid (min minid newnum))
915 (if (memq newnum articles)
916 (setq articles (delq newnum articles))
917 (let ((headers (nnfolder-parse-head newnum)))
918 (with-current-buffer novbuf
919 (nnheader-find-nov-line newnum)
920 (nnheader-insert-nov headers)))))))
921 (when (and novbuf articles)
922 (with-current-buffer novbuf
923 (dolist (article articles)
924 (when (nnheader-find-nov-line article)
925 (delete-region (point)
926 (progn (forward-line 1) (point)))))))
927 (setcar active (max 1 (min minid maxid)))
928 (setcdr active (max maxid (cdr active)))
929 (goto-char (point-min)))
931 ;; As long as we trust that the user will only insert unmarked mail
932 ;; at the end, go to the end and search backwards for the last
933 ;; marker. Find the start of that message, and begin to search for
934 ;; unmarked messages from there.
935 (when (not (or nnfolder-distrust-mbox
937 (goto-char (point-max))
938 (unless (re-search-backward marker nil t)
939 (goto-char (point-min)))
940 ;;(when (nnmail-search-unix-mail-delim)
941 ;; (goto-char (point-min)))
944 ;; Keep track of the active number on our own, and insert it back
945 ;; into the active list when we're done. Also, prime the pump to
946 ;; cut down on the number of searches we do.
947 (unless (nnmail-search-unix-mail-delim)
948 (goto-char (point-max)))
949 (setq end (point-marker))
950 (while (not (= end (point-max)))
951 (setq start (marker-position end))
953 ;; There may be more than one "From " line, so we skip past
955 (while (looking-at delim)
957 (set-marker end (if (nnmail-search-unix-mail-delim)
961 (when (not (search-forward marker end t))
962 (narrow-to-region start end)
963 (nnmail-insert-lines)
964 (nnfolder-insert-newsgroup-line
967 (nnfolder-active-number group))))
969 (let ((headers (nnfolder-parse-head newnum (point-min)
971 (with-current-buffer novbuf
972 (goto-char (point-max))
973 (nnheader-insert-nov headers))))
977 ;; Make absolutely sure that the active list reflects reality!
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 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 (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))
1052 (output-coding-system coding-system-for-write))
1054 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1055 (nnfolder-save-nov)))
1057 (defun nnfolder-save-active (group-alist active-file)
1058 (let ((nnmail-active-file-coding-system
1059 (or nnfolder-active-file-coding-system-for-write
1060 nnfolder-active-file-coding-system)))
1061 (nnmail-save-active group-alist active-file)))
1063 (defun nnfolder-open-nov (group)
1064 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1065 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1068 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1069 (nnfolder-group-nov-pathname group))
1071 (when (file-exists-p nnfolder-nov-buffer-file-name)
1072 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1073 (push (cons group buffer) nnfolder-nov-buffer-alist)
1076 (defun nnfolder-save-nov ()
1078 (while nnfolder-nov-buffer-alist
1079 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1080 (set-buffer (cdar nnfolder-nov-buffer-alist))
1081 (when (buffer-modified-p)
1082 (gnus-make-directory (file-name-directory
1083 nnfolder-nov-buffer-file-name))
1084 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1086 (set-buffer-modified-p nil)
1087 (kill-buffer (current-buffer)))
1088 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1090 (defun nnfolder-nov-delete-article (group article)
1092 (set-buffer (nnfolder-open-nov group))
1093 (when (nnheader-find-nov-line article)
1094 (delete-region (point) (progn (forward-line 1) (point))))
1097 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1098 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1100 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1101 (when (file-exists-p nov)
1103 (set-buffer nntp-server-buffer)
1105 (nnheader-insert-file-contents nov)
1107 (not (numberp fetch-old)))
1108 t ; Don't remove anything.
1109 (nnheader-nov-delete-outside-range
1110 (if fetch-old (max 1 (- (car articles) fetch-old))
1112 (car (last articles)))
1115 (defun nnfolder-parse-head (&optional number b e)
1116 "Parse the head of the current buffer."
1117 (let ((buf (current-buffer))
1121 (setq b (if (nnmail-search-unix-mail-delim-backward)
1122 (point) (point-min)))
1124 (setq e (if (nnmail-search-unix-mail-delim)
1125 (point) (point-max))))
1126 (setq chars (- e b))
1127 (unless (zerop chars)
1129 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1131 (insert-buffer-substring buf b e)
1132 ;; Fold continuation lines.
1133 (goto-char (point-min))
1134 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1135 (replace-match " " t t))
1136 ;; Remove any tabs; they are too confusing.
1137 (subst-char-in-region (point-min) (point-max) ?\t ? )
1138 (let ((headers (nnheader-parse-head t)))
1139 (mail-header-set-chars headers chars)
1140 (mail-header-set-number headers number)
1143 (defun nnfolder-add-nov (group article headers)
1144 "Add a nov line for the GROUP base."
1146 (set-buffer (nnfolder-open-nov group))
1147 (goto-char (point-max))
1148 (mail-header-set-number headers article)
1149 (nnheader-insert-nov headers)))
1151 (deffoo nnfolder-request-set-mark (group actions &optional server)
1153 (not (nnfolder-server-opened server)))
1154 (nnfolder-open-server server))
1155 (unless nnfolder-marks-is-evil
1156 (nnfolder-open-marks group server)
1157 (dolist (action actions)
1158 (let ((range (nth 0 action))
1159 (what (nth 1 action))
1160 (marks (nth 2 action)))
1161 (assert (or (eq what 'add) (eq what 'del)) t
1162 "Unknown request-set-mark action: %s" what)
1163 (dolist (mark marks)
1164 (setq nnfolder-marks (gnus-update-alist-soft
1166 (funcall (if (eq what 'add) 'gnus-range-add
1167 'gnus-remove-from-range)
1168 (cdr (assoc mark nnfolder-marks)) range)
1170 (nnfolder-save-marks group server))
1173 (deffoo nnfolder-request-update-info (group info &optional server)
1176 (not (nnfolder-server-opened server)))
1177 (nnfolder-open-server server))
1178 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1179 (nnheader-message 8 "Updating marks for %s..." group)
1180 (nnfolder-open-marks group server)
1181 ;; Update info using `nnfolder-marks'.
1182 (mapcar (lambda (pred)
1183 (gnus-info-set-marks
1185 (gnus-update-alist-soft
1187 (cdr (assq (cdr pred) nnfolder-marks))
1188 (gnus-info-marks info))
1190 gnus-article-mark-lists)
1191 (let ((seen (cdr (assq 'read nnfolder-marks))))
1192 (gnus-info-set-read info
1193 (if (and (integerp (car seen))
1195 (list (cons (car seen) (car seen)))
1197 (nnheader-message 8 "Updating marks for %s...done" group))
1200 (defun nnfolder-group-marks-pathname (group)
1201 "Make pathname for GROUP NOV."
1202 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1203 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1205 (defun nnfolder-marks-changed-p (group)
1206 (let ((file (nnfolder-group-marks-pathname group)))
1207 (if (null (gnus-gethash file nnfolder-marks-modtime))
1208 t ;; never looked at marks file, assume it has changed
1209 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1210 (nth 5 (file-attributes file)))))))
1212 (defun nnfolder-save-marks (group server)
1213 (let ((file-name-coding-system nnmail-pathname-coding-system)
1214 (file (nnfolder-group-marks-pathname group)))
1217 (with-temp-file file
1219 (gnus-prin1 nnfolder-marks)
1222 (nth 5 (file-attributes file))
1223 nnfolder-marks-modtime))
1224 (error (or (gnus-yes-or-no-p
1225 (format "Could not write to %s (%s). Continue? " file err))
1226 (error "Cannot write to %s (%s)" err))))))
1228 (defun nnfolder-open-marks (group server)
1229 (let ((file (nnfolder-group-marks-pathname group)))
1230 (if (file-exists-p file)
1233 (gnus-sethash file (nth 5 (file-attributes file))
1234 nnfolder-marks-modtime)
1235 (nnheader-insert-file-contents file)
1236 (setq nnfolder-marks (read (current-buffer)))
1237 (dolist (el gnus-article-unpropagated-mark-lists)
1238 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1239 (error (or (gnus-yes-or-no-p
1240 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1241 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1242 ;; User didn't have a .marks file. Probably first time
1243 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1244 (let ((info (gnus-get-info
1245 (gnus-group-prefixed-name
1247 (gnus-server-to-method (format "nnfolder:%s" server))))))
1248 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1249 (setq nnfolder-marks (gnus-info-marks info))
1250 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1251 (dolist (el gnus-article-unpropagated-mark-lists)
1252 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1253 (nnfolder-save-marks group server)))))
1257 ;;; nnfolder.el ends here