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)
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 (when (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 start stop)
184 (goto-char (point-max))
186 (set-buffer nntp-server-buffer)
187 (nnheader-fold-continuation-lines)
190 (deffoo nnfolder-open-server (server &optional defs)
191 (nnoo-change-server 'nnfolder server defs)
192 (nnmail-activate 'nnfolder t)
193 (gnus-make-directory nnfolder-directory)
194 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
195 (and nnfolder-nov-directory
196 (gnus-make-directory nnfolder-nov-directory)))
197 (unless nnfolder-marks-is-evil
198 (and nnfolder-marks-directory
199 (gnus-make-directory nnfolder-marks-directory)))
201 ((not (file-exists-p nnfolder-directory))
202 (nnfolder-close-server)
203 (nnheader-report 'nnfolder "Couldn't create directory: %s"
205 ((not (file-directory-p (file-truename nnfolder-directory)))
206 (nnfolder-close-server)
207 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
209 (nnmail-activate 'nnfolder)
210 (nnheader-report 'nnfolder "Opened server %s using directory %s"
211 server nnfolder-directory)
214 (deffoo nnfolder-request-close ()
215 (let ((alist nnfolder-buffer-alist))
217 (nnfolder-close-group (caar alist) nil t)
218 (setq alist (cdr alist))))
219 (nnoo-close-server 'nnfolder)
220 (setq nnfolder-buffer-alist nil
221 nnfolder-group-alist nil))
223 (deffoo nnfolder-request-article (article &optional group server buffer)
224 (nnfolder-possibly-change-group group server)
226 (set-buffer nnfolder-current-buffer)
227 (goto-char (point-min))
228 (when (nnfolder-goto-article article)
232 (unless (and (nnmail-search-unix-mail-delim)
234 (goto-char (point-max)))
236 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
237 (set-buffer nntp-server-buffer)
239 (insert-buffer-substring nnfolder-current-buffer start stop)
240 (goto-char (point-min))
241 (while (looking-at "From ")
243 (insert "X-From-Line: ")
245 (if (numberp article)
246 (cons nnfolder-current-group article)
247 (goto-char (point-min))
248 (cons nnfolder-current-group
249 (if (search-forward (concat "\n" nnfolder-article-marker)
253 (point) (progn (end-of-line) (point))))
256 (deffoo nnfolder-request-group (group &optional server dont-check)
257 (nnfolder-possibly-change-group group server t)
259 (if (not (assoc group nnfolder-group-alist))
260 (nnheader-report 'nnfolder "No such group: %s" group)
263 (nnheader-report 'nnfolder "Selected group %s" group)
265 (let* ((active (assoc group nnfolder-group-alist))
267 (range (cadr active)))
270 (nnheader-report 'nnfolder "No such group: %s" group))
271 ((null nnfolder-current-group)
272 (nnheader-report 'nnfolder "Empty group: %s" group))
274 (nnheader-report 'nnfolder "Selected group %s" group)
275 (nnheader-insert "211 %d %d %d %s\n"
276 (1+ (- (cdr range) (car range)))
277 (car range) (cdr range) group))))))))
279 (deffoo nnfolder-request-scan (&optional group server)
280 (nnfolder-possibly-change-group nil server)
281 (when nnfolder-get-new-mail
282 (nnfolder-possibly-change-group group server)
286 (let ((bufs nnfolder-buffer-alist))
289 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
290 (setq nnfolder-buffer-alist
291 (delq (car bufs) nnfolder-buffer-alist))
292 (set-buffer (nth 1 (car bufs)))
293 (nnfolder-save-buffer)
294 (kill-buffer (current-buffer)))
295 (setq bufs (cdr bufs))))))
299 ;; Don't close the buffer if we're not shutting down the server. This way,
300 ;; we can keep the buffer in the group buffer cache, and not have to grovel
301 ;; over the buffer again unless we add new mail to it or modify it in some
304 (deffoo nnfolder-close-group (group &optional server force)
305 ;; Make sure we _had_ the group open.
306 (when (or (assoc group nnfolder-buffer-alist)
307 (equal group nnfolder-current-group))
308 (let ((inf (assoc group nnfolder-buffer-alist)))
310 (when (and nnfolder-current-group
311 nnfolder-current-buffer)
312 (push (list nnfolder-current-group nnfolder-current-buffer)
313 nnfolder-buffer-alist))
314 (setq nnfolder-buffer-alist
315 (delq inf nnfolder-buffer-alist))
316 (setq nnfolder-current-buffer (cadr inf)
317 nnfolder-current-group (car inf))))
318 (when (and nnfolder-current-buffer
319 (buffer-name nnfolder-current-buffer))
321 (set-buffer nnfolder-current-buffer)
322 ;; If the buffer was modified, write the file out now.
323 (nnfolder-save-buffer)
324 ;; If we're shutting the server down, we need to kill the
325 ;; buffer and remove it from the open buffer list. Or, of
326 ;; course, if we're trying to minimize our space impact.
327 (kill-buffer (current-buffer))
328 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
329 nnfolder-buffer-alist)))))
330 (setq nnfolder-current-group nil
331 nnfolder-current-buffer nil)
334 (deffoo nnfolder-request-create-group (group &optional server args)
335 (nnfolder-possibly-change-group nil server)
336 (nnmail-activate 'nnfolder)
338 (unless (assoc group nnfolder-group-alist)
339 (push (list group (cons 1 0)) nnfolder-group-alist)
340 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
341 (nnfolder-read-folder group)))
344 (deffoo nnfolder-request-list (&optional server)
345 (nnfolder-possibly-change-group nil server)
347 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
348 (nnmail-find-file nnfolder-active-file)
349 (setq nnfolder-group-alist (nnmail-get-active)))
352 (deffoo nnfolder-request-newgroups (date &optional server)
353 (nnfolder-possibly-change-group nil server)
354 (nnfolder-request-list server))
356 (deffoo nnfolder-request-list-newsgroups (&optional server)
357 (nnfolder-possibly-change-group nil server)
359 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
360 (nnmail-find-file nnfolder-newsgroups-file))))
362 ;; Return a list consisting of all article numbers existing in the
365 (defun nnfolder-existing-articles ()
367 (when nnfolder-current-buffer
368 (set-buffer nnfolder-current-buffer)
369 (goto-char (point-min))
370 (let ((marker (concat "\n" nnfolder-article-marker))
373 (while (and (search-forward marker nil t)
374 (re-search-forward number nil t))
375 (let ((newnum (string-to-number (match-string 0))))
376 (if (nnmail-within-headers-p)
377 (push newnum numbers))))
378 ;; The article numbers are increasing, so this result is sorted.
379 (nreverse numbers)))))
381 (deffoo nnfolder-request-expire-articles
382 (articles newsgroup &optional server force)
383 (nnfolder-possibly-change-group newsgroup server)
385 ;; The articles we have deleted so far.
386 (deleted-articles nil)
387 ;; The articles that really exist and will
388 ;; be expired if they are old enough.
390 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
391 (nnmail-activate 'nnfolder)
394 (set-buffer nnfolder-current-buffer)
395 ;; Since messages are sorted in arrival order and expired in the
396 ;; same order, we can stop as soon as we find a message that is
398 (while (and maybe-expirable is-old)
399 (goto-char (point-min))
400 (when (and (nnfolder-goto-article (car maybe-expirable))
401 (search-forward (concat "\n" nnfolder-article-marker)
405 (nnmail-expired-article-p
408 (point) (progn (end-of-line) (point)))
409 force nnfolder-inhibit-expiry))
410 (unless (eq nnmail-expiry-target 'delete)
412 (nnfolder-request-article (car maybe-expirable)
413 newsgroup server (current-buffer))
414 (let ((nnfolder-current-directory nil))
415 (nnmail-expiry-target-group
416 nnmail-expiry-target newsgroup)))
417 (nnfolder-possibly-change-group newsgroup server))
418 (nnheader-message 5 "Deleting article %d in %s..."
419 (car maybe-expirable) newsgroup)
420 (nnfolder-delete-mail)
421 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
422 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
423 ;; Must remember which articles were actually deleted
424 (push (car maybe-expirable) deleted-articles)))
425 (setq maybe-expirable (cdr maybe-expirable)))
426 (unless nnfolder-inhibit-expiry
427 (nnheader-message 5 "Deleting articles...done"))
428 (nnfolder-save-buffer)
429 (nnfolder-adjust-min-active newsgroup)
430 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
431 (gnus-sorted-difference articles (nreverse deleted-articles)))))
433 (deffoo nnfolder-request-move-article (article group server
434 accept-form &optional last)
436 (let ((buf (get-buffer-create " *nnfolder move*"))
439 (nnfolder-request-article article group server)
443 (insert-buffer-substring nntp-server-buffer)
444 (goto-char (point-min))
445 (while (re-search-forward
446 (concat "^" nnfolder-article-marker)
447 (save-excursion (and (search-forward "\n\n" nil t) (point)))
449 (delete-region (progn (beginning-of-line) (point))
450 (progn (forward-line 1) (point))))
451 (setq result (eval accept-form))
455 (nnfolder-possibly-change-group group server)
456 (set-buffer nnfolder-current-buffer)
457 (goto-char (point-min))
458 (when (nnfolder-goto-article article)
459 (nnfolder-delete-mail))
460 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
461 (nnfolder-nov-delete-article group article))
463 (nnfolder-save-buffer)
464 (nnfolder-adjust-min-active group)
465 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
468 (deffoo nnfolder-request-accept-article (group &optional server last)
470 (nnfolder-possibly-change-group group server)
471 (nnmail-check-syntax)
472 (let ((buf (current-buffer))
474 (goto-char (point-min))
475 (when (looking-at "X-From-Line: ")
477 (mail-header-unfold-field))
478 (replace-match "From "))
480 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
481 (nntp-server-buffer (current-buffer)))
482 (nnmail-find-file nnfolder-active-file)
483 (setq nnfolder-group-alist (nnmail-parse-active))))
485 (goto-char (point-min))
486 (if (search-forward "\n\n" nil t)
488 (goto-char (point-max)))
489 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
490 (delete-region (point) (progn (forward-line 1) (point))))
491 (when nnmail-cache-accepted-message-ids
492 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
493 (setq result (if (stringp group)
494 (list (cons group (nnfolder-active-number group)))
496 (nnmail-article-group 'nnfolder-active-number))))
497 (if (and (null result)
498 (yes-or-no-p "Moved to `junk' group; delete article? "))
501 (car (nnfolder-save-mail result)))))
504 (nnfolder-possibly-change-folder (or (caar art-group) group))
505 (nnfolder-save-buffer)
506 (when nnmail-cache-accepted-message-ids
507 (nnmail-cache-close))))
508 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
510 (nnheader-report 'nnfolder "Couldn't store article"))
513 (deffoo nnfolder-request-replace-article (article group buffer)
514 (nnfolder-possibly-change-group group)
517 (goto-char (point-min))
518 (if (not (looking-at "X-From-Line: "))
519 (insert "From nobody " (current-time-string) "\n")
520 (replace-match "From ")
522 (while (looking-at "[ \t]")
525 (nnfolder-normalize-buffer)
526 (set-buffer nnfolder-current-buffer)
527 (goto-char (point-min))
528 (if (not (nnfolder-goto-article article))
530 (nnfolder-delete-mail)
531 (insert-buffer-substring buffer)
532 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
535 (let ((headers (nnfolder-parse-head article
536 (point-min) (point-max))))
537 (with-current-buffer (nnfolder-open-nov group)
538 (if (nnheader-find-nov-line article)
539 (delete-region (point) (progn (forward-line 1) (point))))
540 (nnheader-insert-nov headers)))))
541 (nnfolder-save-buffer)
544 (deffoo nnfolder-request-delete-group (group &optional force server)
545 (nnfolder-close-group group server t)
546 ;; Delete all articles in GROUP.
548 () ; Don't delete the articles.
549 ;; Delete the file that holds the group.
551 (delete-file (nnfolder-group-pathname group))
552 (when (file-exists-p (nnfolder-group-nov-pathname group))
553 (delete-file (nnfolder-group-nov-pathname group)))
554 (when (file-exists-p (nnfolder-group-marks-pathname group))
555 (delete-file (nnfolder-group-marks-pathname group)))))
556 ;; Remove the group from all structures.
557 (setq nnfolder-group-alist
558 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
559 nnfolder-current-group nil
560 nnfolder-current-buffer nil)
561 ;; Save the active file.
562 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
565 (deffoo nnfolder-request-rename-group (group new-name &optional server)
566 (nnfolder-possibly-change-group group server)
568 (set-buffer nnfolder-current-buffer)
569 (and (file-writable-p buffer-file-name)
571 (let ((new-file (nnfolder-group-pathname new-name)))
572 (gnus-make-directory (file-name-directory new-file))
573 (rename-file buffer-file-name new-file)
574 (when (file-exists-p (nnfolder-group-nov-pathname group))
575 (setq new-file (nnfolder-group-nov-pathname new-name))
576 (gnus-make-directory (file-name-directory new-file))
577 (rename-file (nnfolder-group-nov-pathname group) new-file))
578 (when (file-exists-p (nnfolder-group-marks-pathname group))
579 (setq new-file (nnfolder-group-marks-pathname new-name))
580 (gnus-make-directory (file-name-directory new-file))
581 (rename-file (nnfolder-group-marks-pathname group) new-file)))
583 ;; That went ok, so we change the internal structures.
584 (let ((entry (assoc group nnfolder-group-alist)))
585 (and entry (setcar entry new-name))
586 (setq nnfolder-current-buffer nil
587 nnfolder-current-group nil)
588 ;; Save the new group alist.
589 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
590 ;; We kill the buffer instead of renaming it and stuff.
591 (kill-buffer (current-buffer))
594 (deffoo nnfolder-request-regenerate (server)
595 (nnfolder-possibly-change-group nil server)
596 (nnfolder-generate-active-file)
600 ;;; Internal functions.
602 (defun nnfolder-adjust-min-active (group)
603 ;; Find the lowest active article in this group.
604 (let* ((active (cadr (assoc group nnfolder-group-alist)))
605 (marker (concat "\n" nnfolder-article-marker))
607 (activemin (cdr active)))
609 (set-buffer nnfolder-current-buffer)
610 (goto-char (point-min))
611 (while (and (search-forward marker nil t)
612 (re-search-forward number nil t))
613 (let ((newnum (string-to-number (match-string 0))))
614 (if (nnmail-within-headers-p)
615 (setq activemin (min activemin newnum)))))
616 (setcar active activemin))))
618 (defun nnfolder-article-string (article)
619 (if (numberp article)
620 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
621 (concat "\nMessage-ID: " article)))
623 (defun nnfolder-goto-article (article)
624 "Place point at the start of the headers of ARTICLE.
625 ARTICLE can be an article number or a Message-ID.
626 Returns t if successful, nil otherwise."
627 (let ((art-string (nnfolder-article-string article))
629 ;; It is likely that we are at or before the delimiter line.
630 ;; We therefore go to the end of the previous line, and start
631 ;; searching from there.
636 ;; First search forward.
637 (while (and (setq found (search-forward art-string nil t))
638 (not (nnmail-within-headers-p))))
639 ;; If unsuccessful, search backward from where we started,
642 (while (and (setq found (search-backward art-string nil t))
643 (not (nnmail-within-headers-p)))))
645 (nnmail-search-unix-mail-delim-backward))))
647 (defun nnfolder-delete-mail (&optional leave-delim)
648 "Delete the message that point is in.
649 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
650 deleted. Point is left where the deleted region was."
654 ;; In case point is at the beginning of the message already.
656 (nnmail-search-unix-mail-delim-backward)
657 (if leave-delim (progn (forward-line 1) (point))
661 (if (nnmail-search-unix-mail-delim)
664 (run-hooks 'nnfolder-delete-mail-hook)
665 (delete-region (point-min) (point-max))))
667 (defun nnfolder-possibly-change-group (group &optional server dont-check)
670 (not (nnfolder-server-opened server)))
671 (nnfolder-open-server server))
672 (unless (gnus-buffer-live-p nnfolder-current-buffer)
673 (setq nnfolder-current-buffer nil
674 nnfolder-current-group nil))
676 (let ((file-name-coding-system nnmail-pathname-coding-system))
678 (not (equal group nnfolder-current-group))
680 (nnmail-activate 'nnfolder)
681 (and (assoc group nnfolder-group-alist)
682 (file-exists-p (nnfolder-group-pathname group)))))
684 (setq nnfolder-current-group group
685 nnfolder-current-buffer nil)
687 ;; If we have to change groups, see if we don't already have
688 ;; the folder in memory. If we do, verify the modtime and
689 ;; destroy the folder if needed so we can rescan it.
690 (setq nnfolder-current-buffer
691 (nth 1 (assoc group nnfolder-buffer-alist)))
693 ;; If the buffer is not live, make sure it isn't in the
694 ;; alist. If it is live, verify that nobody else has
695 ;; touched the file since last time.
696 (when (and nnfolder-current-buffer
697 (not (gnus-buffer-live-p nnfolder-current-buffer)))
698 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
699 nnfolder-current-buffer nil))
701 (setq nnfolder-current-group group)
703 (when (or (not nnfolder-current-buffer)
704 (not (verify-visited-file-modtime
705 nnfolder-current-buffer)))
707 (setq file (nnfolder-group-pathname group))
708 ;; See whether we need to create the new file.
709 (unless (file-exists-p file)
710 (gnus-make-directory (file-name-directory file))
711 (let ((nnmail-file-coding-system
712 (or nnfolder-file-coding-system-for-write
713 nnfolder-file-coding-system-for-write)))
714 (nnmail-write-region (point-min) (point-min)
716 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
717 (set-buffer nnfolder-current-buffer)
718 (push (list group nnfolder-current-buffer)
719 nnfolder-buffer-alist)))))))))
721 (defun nnfolder-save-mail (group-art-list)
722 "Called narrowed to an article."
723 (let* (save-list group-art)
724 (goto-char (point-min))
725 ;; The From line may have been quoted by movemail.
726 (when (looking-at ">From")
728 ;; This might come from somewhere else.
729 (unless (looking-at "From ")
730 (insert "From nobody " (current-time-string) "\n")
731 (goto-char (point-min)))
732 ;; Quote all "From " lines in the article.
734 (let (case-fold-search)
735 (while (re-search-forward "^From " nil t)
738 (setq save-list group-art-list)
739 (nnmail-insert-lines)
740 (nnmail-insert-xref group-art-list)
741 (run-hooks 'nnmail-prepare-save-mail-hook)
742 (run-hooks 'nnfolder-prepare-save-mail-hook)
744 ;; Insert the mail into each of the destination groups.
745 (while (setq group-art (pop group-art-list))
746 ;; Kill any previous newsgroup markers.
747 (goto-char (point-min))
748 (if (search-forward "\n\n" nil t)
750 (goto-char (point-max)))
751 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
752 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
754 ;; Insert the new newsgroup marker.
755 (nnfolder-insert-newsgroup-line group-art)
758 (let ((beg (point-min))
760 (obuf (current-buffer)))
761 (nnfolder-possibly-change-folder (car group-art))
762 (let ((buffer-read-only nil))
763 (nnfolder-normalize-buffer)
764 (insert-buffer-substring obuf beg end))
765 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
767 (nnfolder-add-nov (car group-art) (cdr group-art)
768 (nnfolder-parse-head nil beg end))))))
770 ;; Did we save it anywhere?
773 (defun nnfolder-normalize-buffer ()
774 "Make sure there are two newlines at the end of the buffer."
775 (goto-char (point-max))
776 (skip-chars-backward "\n")
777 (delete-region (point) (point-max))
781 (defun nnfolder-insert-newsgroup-line (group-art)
783 (goto-char (point-min))
784 (unless (search-forward "\n\n" nil t)
785 (goto-char (point-max))
788 (insert (format (concat nnfolder-article-marker "%d %s\n")
789 (cdr group-art) (current-time-string)))))
791 (defun nnfolder-active-number (group)
792 ;; Find the next article number in GROUP.
793 (let ((active (cadr (assoc group nnfolder-group-alist))))
795 (setcdr active (1+ (cdr active)))
796 ;; This group is new, so we create a new entry for it.
797 ;; This might be a bit naughty... creating groups on the drop of
798 ;; a hat, but I don't know...
799 (push (list group (setq active (cons 1 1)))
800 nnfolder-group-alist))
803 (defun nnfolder-possibly-change-folder (group)
804 (let ((inf (assoc group nnfolder-buffer-alist)))
806 (gnus-buffer-live-p (cadr inf)))
807 (set-buffer (cadr inf))
809 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
810 (when nnfolder-group-alist
811 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
812 (push (list group (nnfolder-read-folder group))
813 nnfolder-buffer-alist))))
815 ;; This method has a problem if you've accidentally let the active
816 ;; list get out of sync with the files. This could happen, say, if
817 ;; you've accidentally gotten new mail with something other than Gnus
818 ;; (but why would _that_ ever happen? :-). In that case, we will be
819 ;; in the middle of processing the file, ready to add new X-Gnus
820 ;; article number markers, and we'll run across a message with no ID
821 ;; yet - the active list _may_not_ be ready for us yet.
823 ;; To handle this, I'm modifying this routine to maintain the maximum
824 ;; ID seen so far, and when we hit a message with no ID, we will
825 ;; _manually_ scan the rest of the message looking for any more,
826 ;; possibly higher IDs. We'll assume the maximum that we find is the
827 ;; highest active. Note that this shouldn't cost us much extra time
828 ;; at all, but will be a lot less vulnerable to glitches between the
829 ;; mbox and the active file.
831 (defun nnfolder-read-folder (group)
832 (let* ((file (nnfolder-group-pathname group))
833 (nov (nnfolder-group-nov-pathname group))
835 (let ((nnheader-file-coding-system
836 nnfolder-file-coding-system))
837 (nnheader-find-file-noselect file)))))
838 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
839 (if (equal (cadr (assoc group nnfolder-scantime-alist))
840 (nth 5 (file-attributes file)))
841 ;; This looks up-to-date, so we don't do any scanning.
842 (if (file-exists-p file)
844 (push (list group buffer) nnfolder-buffer-alist)
845 (set-buffer-modified-p t)
846 (nnfolder-save-buffer))
847 ;; Parse the damn thing.
849 (goto-char (point-min))
850 ;; Remove any blank lines at the start.
851 (while (eq (following-char) ?\n)
853 (nnmail-activate 'nnfolder)
855 (let ((delim "^From ")
856 (marker (concat "\n" nnfolder-article-marker))
858 (active (or (cadr (assoc group nnfolder-group-alist))
860 (scantime (assoc group nnfolder-scantime-alist))
862 maxid start end newscantime
863 novbuf articles newnum
865 (buffer-disable-undo)
866 (setq maxid (cdr active))
868 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
869 (and (file-exists-p nov)
870 (file-newer-than-file-p nov file)))
871 (unless (file-exists-p nov)
872 (gnus-make-directory (file-name-directory nov)))
874 (setq novbuf (nnfolder-open-nov group))
875 (goto-char (point-min))
877 (push (read novbuf) articles)
879 (setq articles (nreverse articles))))
880 (goto-char (point-min))
882 ;; Anytime the active number is 1 or 0, it is suspect. In
883 ;; that case, search the file manually to find the active
884 ;; number. Or, of course, if we're being paranoid. (This
885 ;; would also be the place to build other lists from the
886 ;; header markers, such as expunge lists, etc., if we ever
887 ;; desired to abandon the active file entirely for mboxes.)
888 (when (or nnfolder-ignore-active-file
891 (while (and (search-forward marker nil t)
893 (setq newnum (string-to-number (match-string 0)))
894 (when (nnmail-within-headers-p)
895 (setq maxid (max maxid newnum)
896 minid (min minid newnum))
898 (if (memq newnum articles)
899 (setq articles (delq newnum articles))
900 (let ((headers (nnfolder-parse-head newnum)))
901 (with-current-buffer novbuf
902 (nnheader-find-nov-line newnum)
903 (nnheader-insert-nov headers)))))))
904 (when (and novbuf articles)
905 (with-current-buffer novbuf
906 (dolist (article articles)
907 (when (nnheader-find-nov-line article)
908 (delete-region (point)
909 (progn (forward-line 1) (point)))))))
910 (setcar active (max 1 (min minid maxid)))
911 (setcdr active (max maxid (cdr active)))
912 (goto-char (point-min)))
914 ;; As long as we trust that the user will only insert
915 ;; unmarked mail at the end, go to the end and search
916 ;; backwards for the last marker. Find the start of that
917 ;; message, and begin to search for unmarked messages from
919 (when (not (or nnfolder-distrust-mbox
921 (goto-char (point-max))
922 (unless (re-search-backward marker nil t)
923 (goto-char (point-min)))
924 ;;(when (nnmail-search-unix-mail-delim)
925 ;; (goto-char (point-min)))
928 ;; Keep track of the active number on our own, and insert it
929 ;; back into the active list when we're done. Also, prime
930 ;; the pump to cut down on the number of searches we do.
931 (unless (nnmail-search-unix-mail-delim)
932 (goto-char (point-max)))
933 (setq end (point-marker))
934 (while (not (= end (point-max)))
935 (setq start (marker-position end))
937 ;; There may be more than one "From " line, so we skip past
939 (while (looking-at delim)
941 (set-marker end (if (nnmail-search-unix-mail-delim)
945 (when (not (search-forward marker end t))
946 (narrow-to-region start end)
947 (nnmail-insert-lines)
948 (nnfolder-insert-newsgroup-line
951 (nnfolder-active-number group))))
953 (let ((headers (nnfolder-parse-head newnum (point-min)
955 (with-current-buffer novbuf
956 (goto-char (point-max))
957 (nnheader-insert-nov headers))))
961 ;; Make absolutely sure that the active list reflects
963 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
965 ;; Set the scantime for this group.
966 (setq newscantime (visited-file-modtime))
968 (setcdr scantime (list newscantime))
969 (push (list group newscantime)
970 nnfolder-scantime-alist))
974 (current-buffer))))))
977 (defun nnfolder-generate-active-file ()
978 "Look for mbox folders in the nnfolder directory and make them into groups.
979 This command does not work if you use short group names."
981 (nnmail-activate 'nnfolder)
982 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
983 (dolist (file (directory-files (or nnfolder-nov-directory
987 (regexp-quote nnfolder-nov-file-suffix)
989 (when (not (message-mail-file-mbox-p file))
991 (delete-file file)))))
992 (let ((files (directory-files nnfolder-directory))
994 (while (setq file (pop files))
995 (when (and (not (backup-file-name-p file))
996 (message-mail-file-mbox-p
997 (nnheader-concat nnfolder-directory file)))
998 (let ((oldgroup (assoc file nnfolder-group-alist)))
1000 (nnheader-message 5 "Refreshing group %s..." file)
1001 (nnheader-message 5 "Adding group %s..." file))
1003 (setq nnfolder-group-alist
1004 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1005 (push (list file (cons 1 0)) nnfolder-group-alist)
1006 (nnfolder-possibly-change-folder file)
1007 (nnfolder-possibly-change-group file)
1008 (nnfolder-close-group file))))
1009 (nnheader-message 5 "")))
1011 (defun nnfolder-group-pathname (group)
1012 "Make pathname for GROUP."
1014 (mm-encode-coding-string group nnmail-pathname-coding-system))
1015 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1016 ;; If this file exists, we use it directly.
1017 (if (or nnmail-use-long-file-names
1018 (file-exists-p (concat dir group)))
1020 ;; If not, we translate dots into slashes.
1021 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1023 (defun nnfolder-group-nov-pathname (group)
1024 "Make pathname for GROUP NOV."
1025 (let ((nnfolder-directory
1026 (or nnfolder-nov-directory nnfolder-directory)))
1027 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1029 (defun nnfolder-save-buffer ()
1031 (when (buffer-modified-p)
1032 (run-hooks 'nnfolder-save-buffer-hook)
1033 (gnus-make-directory (file-name-directory (buffer-file-name)))
1034 (let ((coding-system-for-write
1035 (or nnfolder-file-coding-system-for-write
1036 nnfolder-file-coding-system)))
1038 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1039 (nnfolder-save-nov)))
1041 (defun nnfolder-save-active (group-alist active-file)
1042 (let ((nnmail-active-file-coding-system
1043 (or nnfolder-active-file-coding-system-for-write
1044 nnfolder-active-file-coding-system)))
1045 (nnmail-save-active group-alist active-file)))
1047 (defun nnfolder-open-nov (group)
1048 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1049 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1052 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1053 (nnfolder-group-nov-pathname group))
1055 (when (file-exists-p nnfolder-nov-buffer-file-name)
1056 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1057 (push (cons group buffer) nnfolder-nov-buffer-alist)
1060 (defun nnfolder-save-nov ()
1062 (while nnfolder-nov-buffer-alist
1063 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1064 (set-buffer (cdar nnfolder-nov-buffer-alist))
1065 (when (buffer-modified-p)
1066 (gnus-make-directory (file-name-directory
1067 nnfolder-nov-buffer-file-name))
1068 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1070 (set-buffer-modified-p nil)
1071 (kill-buffer (current-buffer)))
1072 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1074 (defun nnfolder-nov-delete-article (group article)
1076 (set-buffer (nnfolder-open-nov group))
1077 (when (nnheader-find-nov-line article)
1078 (delete-region (point) (progn (forward-line 1) (point))))
1081 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1082 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1084 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1085 (when (file-exists-p nov)
1087 (set-buffer nntp-server-buffer)
1089 (nnheader-insert-file-contents nov)
1091 (not (numberp fetch-old)))
1092 t ; Don't remove anything.
1093 (nnheader-nov-delete-outside-range
1094 (if fetch-old (max 1 (- (car articles) fetch-old))
1096 (car (last articles)))
1099 (defun nnfolder-parse-head (&optional number b e)
1100 "Parse the head of the current buffer."
1101 (let ((buf (current-buffer))
1105 (setq b (if (nnmail-search-unix-mail-delim-backward)
1106 (point) (point-min)))
1108 (setq e (if (nnmail-search-unix-mail-delim)
1109 (point) (point-max))))
1110 (setq chars (- e b))
1111 (unless (zerop chars)
1113 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1115 (insert-buffer-substring buf b e)
1116 ;; Fold continuation lines.
1117 (goto-char (point-min))
1118 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1119 (replace-match " " t t))
1120 ;; Remove any tabs; they are too confusing.
1121 (subst-char-in-region (point-min) (point-max) ?\t ? )
1122 (let ((headers (nnheader-parse-head t)))
1123 (mail-header-set-chars headers chars)
1124 (mail-header-set-number headers number)
1127 (defun nnfolder-add-nov (group article headers)
1128 "Add a nov line for the GROUP base."
1130 (set-buffer (nnfolder-open-nov group))
1131 (goto-char (point-max))
1132 (mail-header-set-number headers article)
1133 (nnheader-insert-nov headers)))
1135 (deffoo nnfolder-request-set-mark (group actions &optional server)
1137 (not (nnfolder-server-opened server)))
1138 (nnfolder-open-server server))
1139 (unless nnfolder-marks-is-evil
1140 (nnfolder-open-marks group server)
1141 (dolist (action actions)
1142 (let ((range (nth 0 action))
1143 (what (nth 1 action))
1144 (marks (nth 2 action)))
1145 (assert (or (eq what 'add) (eq what 'del)) t
1146 "Unknown request-set-mark action: %s" what)
1147 (dolist (mark marks)
1148 (setq nnfolder-marks (gnus-update-alist-soft
1150 (funcall (if (eq what 'add) 'gnus-range-add
1151 'gnus-remove-from-range)
1152 (cdr (assoc mark nnfolder-marks)) range)
1154 (nnfolder-save-marks group server))
1157 (deffoo nnfolder-request-update-info (group info &optional server)
1160 (not (nnfolder-server-opened server)))
1161 (nnfolder-open-server server))
1162 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1163 (nnheader-message 8 "Updating marks for %s..." group)
1164 (nnfolder-open-marks group server)
1165 ;; Update info using `nnfolder-marks'.
1166 (mapcar (lambda (pred)
1167 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1168 (gnus-info-set-marks
1170 (gnus-update-alist-soft
1172 (cdr (assq (cdr pred) nnfolder-marks))
1173 (gnus-info-marks info))
1175 gnus-article-mark-lists)
1176 (let ((seen (cdr (assq 'read nnfolder-marks))))
1177 (gnus-info-set-read info
1178 (if (and (integerp (car seen))
1180 (list (cons (car seen) (car seen)))
1182 (nnheader-message 8 "Updating marks for %s...done" group))
1185 (defun nnfolder-group-marks-pathname (group)
1186 "Make pathname for GROUP NOV."
1187 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1188 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1190 (defun nnfolder-marks-changed-p (group)
1191 (let ((file (nnfolder-group-marks-pathname group)))
1192 (if (null (gnus-gethash file nnfolder-marks-modtime))
1193 t ;; never looked at marks file, assume it has changed
1194 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1195 (nth 5 (file-attributes file)))))))
1197 (defun nnfolder-save-marks (group server)
1198 (let ((file-name-coding-system nnmail-pathname-coding-system)
1199 (file (nnfolder-group-marks-pathname group)))
1202 (with-temp-file file
1204 (gnus-prin1 nnfolder-marks)
1207 (nth 5 (file-attributes file))
1208 nnfolder-marks-modtime))
1209 (error (or (gnus-yes-or-no-p
1210 (format "Could not write to %s (%s). Continue? " file err))
1211 (error "Cannot write to %s (%s)" err))))))
1213 (defun nnfolder-open-marks (group server)
1214 (let ((file (nnfolder-group-marks-pathname group)))
1215 (if (file-exists-p file)
1218 (gnus-sethash file (nth 5 (file-attributes file))
1219 nnfolder-marks-modtime)
1220 (nnheader-insert-file-contents file)
1221 (setq nnfolder-marks (read (current-buffer)))
1222 (dolist (el gnus-article-unpropagated-mark-lists)
1223 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1224 (error (or (gnus-yes-or-no-p
1225 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1226 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1227 ;; User didn't have a .marks file. Probably first time
1228 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1229 (let ((info (gnus-get-info
1230 (gnus-group-prefixed-name
1232 (gnus-server-to-method (format "nnfolder:%s" server))))))
1233 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1234 (setq nnfolder-marks (gnus-info-marks info))
1235 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1236 (dolist (el gnus-article-unpropagated-mark-lists)
1237 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1238 (nnfolder-save-marks group server)
1239 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1243 ;;; nnfolder.el ends here