1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; For an overview of what the interface functions do, please see the
30 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
38 (defvar nnfolder-directory (expand-file-name "~/Mail/")
39 "The name of the mail box file in the users home directory.")
41 (defvar nnfolder-active-file
42 (concat (file-name-as-directory nnfolder-directory) "active")
43 "The name of the active file.")
45 ;; I renamed this variable to somehting more in keeping with the general GNU
48 (defvar nnfolder-ignore-active-file nil
49 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
50 Note that the active file is still saved, but it's values are not
51 used. This costs some extra time when scanning an mbox when opening
54 ;; Note that this variable may not be completely implemented yet. -SLB
56 (defvar nnfolder-always-close nil
57 "If non-nil, nnfolder attempts to only ever have one mbox open at a time.
58 This is a straight space/performance trade off, as the mboxes will have to
59 be scanned every time they are read in. If nil (default), nnfolder will
60 attempt to keep the buffers around (saving the nnfolder's buffer upon group
61 close, but not killing it), speeding some things up tremendously, especially
62 such things as moving mail. All buffers always get killed upon server close.")
64 (defvar nnfolder-newsgroups-file
65 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
66 "Mail newsgroups description file.")
68 (defvar nnfolder-get-new-mail t
69 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
71 (defvar nnfolder-prepare-save-mail-hook nil
72 "Hook run narrowed to an article before saving.")
76 (defconst nnfolder-version "nnfolder 1.0"
79 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
80 "String used to demarcate what the article number for a message is.")
82 (defvar nnfolder-current-group nil)
83 (defvar nnfolder-current-buffer nil)
84 (defvar nnfolder-status-string "")
85 (defvar nnfolder-group-alist nil)
86 (defvar nnfolder-buffer-alist nil)
87 (defvar nnfolder-active-timestamp nil)
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables
95 (list 'nnfolder-directory nnfolder-directory)
96 (list 'nnfolder-active-file nnfolder-active-file)
97 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99 '(nnfolder-current-group nil)
100 '(nnfolder-current-buffer nil)
101 '(nnfolder-status-string "")
102 '(nnfolder-group-alist nil)
103 '(nnfolder-buffer-alist nil)
104 '(nnfolder-active-timestamp nil)))
108 ;;; Interface functions
110 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
112 (set-buffer nntp-server-buffer)
114 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
115 article art-string start stop)
116 (nnfolder-possibly-change-group newsgroup)
117 (set-buffer nnfolder-current-buffer)
118 (goto-char (point-min))
119 (if (stringp (car sequence))
122 (setq article (car sequence))
123 (setq art-string (nnfolder-article-string article))
124 (set-buffer nnfolder-current-buffer)
125 (if (or (search-forward art-string nil t)
126 ;; Don't search the whole file twice! Also, articles
127 ;; probably have some locality by number, so searching
128 ;; backwards will be faster. Especially if we're at the
129 ;; beginning of the buffer :-). -SLB
130 (search-backward art-string nil t))
132 (setq start (or (re-search-backward delim-string nil t)
134 (search-forward "\n\n" nil t)
135 (setq stop (1- (point)))
136 (set-buffer nntp-server-buffer)
137 (insert (format "221 %d Article retrieved.\n" article))
138 (insert-buffer-substring nnfolder-current-buffer start stop)
139 (goto-char (point-max))
141 (setq sequence (cdr sequence)))
143 ;; Fold continuation lines.
144 (set-buffer nntp-server-buffer)
145 (goto-char (point-min))
146 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147 (replace-match " " t t))
150 (defun nnfolder-open-server (server &optional defs)
151 (nnheader-init-server-buffer)
152 (if (equal server nnfolder-current-server)
154 (if nnfolder-current-server
155 (setq nnfolder-server-alist
156 (cons (list nnfolder-current-server
157 (nnheader-save-variables nnfolder-server-variables))
158 nnfolder-server-alist)))
159 (let ((state (assoc server nnfolder-server-alist)))
162 (nnheader-restore-variables (nth 1 state))
163 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
164 (nnheader-set-init-variables nnfolder-server-variables defs)))
165 (setq nnfolder-current-server server)))
167 (defun nnfolder-close-server (&optional server)
170 (defun nnfolder-server-opened (&optional server)
171 (and (equal server nnfolder-current-server)
173 (buffer-name nntp-server-buffer)))
175 (defun nnfolder-request-close ()
176 (let ((alist nnfolder-buffer-alist))
178 (nnfolder-close-group (car (car alist)) nil t)
179 (setq alist (cdr alist))))
180 (setq nnfolder-buffer-alist nil
181 nnfolder-group-alist nil))
183 (defun nnfolder-status-message (&optional server)
184 nnfolder-status-string)
186 (defun nnfolder-request-article (article &optional newsgroup server buffer)
187 (nnfolder-possibly-change-group newsgroup)
189 (set-buffer nnfolder-current-buffer)
190 (goto-char (point-min))
191 (if (search-forward (nnfolder-article-string article) nil t)
193 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
196 (or (and (re-search-forward
197 (concat "^" rmail-unix-mail-delimiter) nil t)
199 (goto-char (point-max)))
201 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
202 (set-buffer nntp-server-buffer)
204 (insert-buffer-substring nnfolder-current-buffer start stop)
205 (goto-char (point-min))
206 (while (looking-at "From ")
208 (insert "X-From-Line: ")
210 (if (numberp article)
211 (cons nnfolder-current-group article)
212 (goto-char (point-min))
213 (search-forward (concat "\n" nnfolder-article-marker))
214 (cons nnfolder-current-group
217 (point) (progn (end-of-line) (point)))))))))))
219 (defun nnfolder-request-group (group &optional server dont-check)
221 (nnmail-activate 'nnfolder)
222 (nnfolder-possibly-change-group group)
223 (and (assoc group nnfolder-group-alist)
227 (let* ((active (assoc group nnfolder-group-alist))
229 (range (car (cdr active)))
230 (minactive (car range))
231 (maxactive (cdr range)))
232 ;; I've been getting stray 211 lines in my nnfolder active
233 ;; file. So, let's make sure that doesn't happen. -SLB
234 (set-buffer nntp-server-buffer)
238 (insert (format "211 %d %d %d %s\n"
239 (1+ (- maxactive minactive))
240 minactive maxactive group))
243 (defun nnfolder-request-scan (&optional group server)
247 (let ((bufs nnfolder-buffer-alist))
250 (if (not (buffer-name (nth 1 (car bufs))))
251 (setq nnfolder-buffer-alist
252 (delq (car bufs) nnfolder-buffer-alist))
253 (set-buffer (nth 1 (car bufs)))
254 (and (buffer-modified-p) (save-buffer)))
255 (setq bufs (cdr bufs))))))
259 ;; Don't close the buffer if we're not shutting down the server. This way,
260 ;; we can keep the buffer in the group buffer cache, and not have to grovel
261 ;; over the buffer again unless we add new mail to it or modify it in some
264 (defun nnfolder-close-group (group &optional server force)
265 ;; Make sure we _had_ the group open.
266 (if (or (assoc group nnfolder-buffer-alist)
267 (equal group nnfolder-current-group))
269 (nnfolder-possibly-change-group group)
271 (set-buffer nnfolder-current-buffer)
272 ;; If the buffer was modified, write the file out now.
273 (and (buffer-modified-p) (save-buffer))
275 nnfolder-always-close)
276 ;; If we're shutting the server down, we need to kill the
277 ;; buffer and remove it from the open buffer list. Or, of
278 ;; course, if we're trying to minimize our space impact.
280 (kill-buffer (current-buffer))
281 (setq nnfolder-buffer-alist (delq (assoc group
282 nnfolder-buffer-alist)
283 nnfolder-buffer-alist)))))))
284 (setq nnfolder-current-group nil
285 nnfolder-current-buffer nil)
288 (defun nnfolder-request-create-group (group &optional server)
289 (nnmail-activate 'nnfolder)
290 (or (assoc group nnfolder-group-alist)
292 (setq nnfolder-group-alist
293 (cons (list group (setq active (cons 1 0)))
294 nnfolder-group-alist))
295 (nnfolder-possibly-change-group group)
296 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
299 (defun nnfolder-request-list (&optional server)
301 (nnmail-find-file nnfolder-active-file)
302 (setq nnfolder-group-alist (nnmail-get-active))))
304 (defun nnfolder-request-newgroups (date &optional server)
305 (nnfolder-request-list server))
307 (defun nnfolder-request-list-newsgroups (&optional server)
309 (nnmail-find-file nnfolder-newsgroups-file)))
311 (defun nnfolder-request-post (&optional server)
312 (mail-send-and-exit nil))
314 (defun nnfolder-request-expire-articles
315 (articles newsgroup &optional server force)
316 (nnfolder-possibly-change-group newsgroup)
317 (let* ((days (or (and nnmail-expiry-wait-function
318 (funcall nnmail-expiry-wait-function newsgroup))
322 (nnmail-activate 'nnfolder)
325 (set-buffer nnfolder-current-buffer)
326 (while (and articles is-old)
327 (goto-char (point-min))
328 (if (search-forward (nnfolder-article-string (car articles)) nil t)
331 (> (nnmail-days-between
332 (current-time-string)
334 (point) (progn (end-of-line) (point))))
337 (and gnus-verbose-backends
338 (message "Deleting article %d..."
339 (car articles) newsgroup))
340 (nnfolder-delete-mail))
341 (setq rest (cons (car articles) rest))))
342 (setq articles (cdr articles)))
343 (and (buffer-modified-p) (save-buffer))
344 ;; Find the lowest active article in this group.
345 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
346 (marker (concat "\n" nnfolder-article-marker))
348 (activemin (cdr active)))
349 (goto-char (point-min))
350 (while (and (search-forward marker nil t)
351 (re-search-forward number nil t))
352 (setq activemin (min activemin
353 (string-to-number (buffer-substring
356 (setcar active activemin))
357 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
358 (nconc rest articles))))
360 (defun nnfolder-request-move-article
361 (article group server accept-form &optional last)
362 (nnfolder-possibly-change-group group)
363 (let ((buf (get-buffer-create " *nnfolder move*"))
366 (nnfolder-request-article article group server)
369 (buffer-disable-undo (current-buffer))
371 (insert-buffer-substring nntp-server-buffer)
372 (goto-char (point-min))
373 (while (re-search-forward
374 (concat "^" nnfolder-article-marker)
375 (save-excursion (search-forward "\n\n" nil t) (point)) t)
376 (delete-region (progn (beginning-of-line) (point))
377 (progn (forward-line 1) (point))))
378 (setq result (eval accept-form))
382 (nnfolder-possibly-change-group group)
383 (set-buffer nnfolder-current-buffer)
384 (goto-char (point-min))
385 (if (search-forward (nnfolder-article-string article) nil t)
386 (nnfolder-delete-mail))
392 (defun nnfolder-request-accept-article (group &optional last)
393 (and (stringp group) (nnfolder-possibly-change-group group))
394 (let ((buf (current-buffer))
396 (goto-char (point-min))
397 (cond ((looking-at "X-From-Line: ")
398 (replace-match "From "))
399 ((not (looking-at "From "))
400 (insert "From nobody " (current-time-string) "\n")))
402 (nnfolder-request-list)
405 (goto-char (point-min))
406 (search-forward "\n\n" nil t)
408 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
409 (delete-region (point) (progn (forward-line 1) (point))))
410 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
412 (set-buffer nnfolder-current-buffer)
413 (and last (buffer-modified-p) (save-buffer))))
414 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
417 (defun nnfolder-request-replace-article (article group buffer)
418 (nnfolder-possibly-change-group group)
420 (set-buffer nnfolder-current-buffer)
421 (goto-char (point-min))
422 (if (not (search-forward (nnfolder-article-string article) nil t))
424 (nnfolder-delete-mail t t)
425 (insert-buffer-substring buffer)
426 (and (buffer-modified-p) (save-buffer))
429 (defun nnfolder-request-delete-group (group &optional force server)
430 (nnfolder-possibly-change-group group)
431 ;; Delete all articles in GROUP.
433 () ; Don't delete the articles.
434 ;; Delete the file that holds the group and kill the buffer as
437 (and (set-buffer nnfolder-current-buffer)
439 (and (file-writable-p buffer-file-name)
440 (delete-file buffer-file-name))
441 (kill-buffer (current-buffer))))))
442 ;; Remove the group from all structures.
443 (setq nnfolder-group-alist
444 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
445 nnfolder-current-group nil
446 nnfolder-current-buffer nil)
447 ;; Save the active file.
448 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
451 (defun nnfolder-request-rename-group (group new-name &optional server)
452 (nnfolder-possibly-change-group group)
454 (set-buffer nnfolder-current-buffer)
455 (and (file-writable-p buffer-file-name)
458 (rename-file buffer-file-name
459 (concat (file-name-as-directory nnfolder-directory)
463 ;; That went ok, so we change the internal structures.
464 (let ((entry (assoc group nnfolder-group-alist)))
465 (and entry (setcar entry new-name))
466 (setq nnfolder-current-buffer nil
467 nnfolder-current-group nil)
468 ;; Save the new group alist.
469 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
470 ;; We kill the buffer instead of renaming it and stuff.
471 (kill-buffer (current-buffer))
475 ;;; Internal functions.
477 (defun nnfolder-article-string (article)
478 (if (numberp article)
479 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
480 (concat "\nMessage-ID: " article)))
482 (defun nnfolder-delete-mail (&optional force leave-delim)
483 ;; Beginning of the article.
488 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
489 (if leave-delim (progn (forward-line 1) (point))
490 (match-beginning 0)))
493 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
495 (if (and (not (bobp)) leave-delim)
496 (progn (forward-line -2) (point))
497 (match-beginning 0)))
499 (delete-region (point-min) (point-max)))))
501 (defun nnfolder-possibly-change-group (group)
502 (or (file-exists-p nnfolder-directory)
503 (make-directory (directory-file-name nnfolder-directory)))
504 (nnfolder-possibly-activate-groups nil)
505 (or (assoc group nnfolder-group-alist)
506 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
509 (setq nnfolder-group-alist
510 (cons (list group (cons 1 0)) nnfolder-group-alist))
511 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
513 (if (and (equal group nnfolder-current-group)
514 nnfolder-current-buffer
515 (buffer-name nnfolder-current-buffer))
517 (setq nnfolder-current-group group)
519 ;; If we have to change groups, see if we don't already have the mbox
520 ;; in memory. If we do, verify the modtime and destroy the mbox if
521 ;; needed so we can rescan it.
522 (if (setq inf (assoc group nnfolder-buffer-alist))
523 (setq nnfolder-current-buffer (nth 1 inf)))
525 ;; If the buffer is not live, make sure it isn't in the alist. If it
526 ;; is live, verify that nobody else has touched the file since last
528 (if (or (not (and nnfolder-current-buffer
529 (buffer-name nnfolder-current-buffer)))
530 (not (and (bufferp nnfolder-current-buffer)
531 (verify-visited-file-modtime
532 nnfolder-current-buffer))))
534 (if (and nnfolder-current-buffer
535 (buffer-name nnfolder-current-buffer)
536 (bufferp nnfolder-current-buffer))
537 (kill-buffer nnfolder-current-buffer))
538 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
544 (setq file (concat (file-name-as-directory nnfolder-directory)
546 (if (file-directory-p (file-truename file))
548 (if (not (file-exists-p file))
549 (write-region 1 1 file t 'nomesg))
550 (setq nnfolder-current-buffer
551 (set-buffer (nnfolder-read-folder file)))
552 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
553 nnfolder-buffer-alist)))))))
554 (setq nnfolder-current-group group))
556 (defun nnfolder-save-mail (&optional group)
557 "Called narrowed to an article."
558 (let* ((nnmail-split-methods
559 (if group (list (list group "")) nnmail-split-methods))
561 (nreverse (nnmail-article-group 'nnfolder-active-number)))
563 (setq save-list group-art-list)
564 (nnmail-insert-lines)
565 (nnmail-insert-xref group-art-list)
566 (run-hooks 'nnfolder-prepare-save-mail-hook)
568 ;; Insert the mail into each of the destination groups.
569 (while group-art-list
570 (setq group-art (car group-art-list)
571 group-art-list (cdr group-art-list))
573 ;; Kill the previous newsgroup markers.
574 (goto-char (point-min))
575 (search-forward "\n\n" nil t)
577 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
578 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
580 ;; Insert the new newsgroup marker.
581 (nnfolder-possibly-change-group (car group-art))
582 (nnfolder-insert-newsgroup-line group-art)
583 (let ((beg (point-min))
585 (obuf (current-buffer)))
586 (set-buffer nnfolder-current-buffer)
587 (goto-char (point-max))
588 (insert-buffer-substring obuf beg end)
591 ;; Did we save it anywhere?
594 (defun nnfolder-insert-newsgroup-line (group-art)
596 (goto-char (point-min))
597 (if (search-forward "\n\n" nil t)
600 (insert (format (concat nnfolder-article-marker "%d %s\n")
601 (cdr group-art) (current-time-string)))))))
603 (defun nnfolder-possibly-activate-groups (&optional group)
605 ;; If we're looking for the activation of a specific group, find out
606 ;; its real name and switch to it.
607 (if group (nnfolder-possibly-change-group group))
608 ;; If the group alist isn't active, activate it now.
609 (nnmail-activate 'nnfolder)))
611 (defun nnfolder-active-number (group)
613 ;; Find the next article number in GROUP.
615 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
617 (setcdr active (1+ (cdr active)))
618 ;; This group is new, so we create a new entry for it.
619 ;; This might be a bit naughty... creating groups on the drop of
620 ;; a hat, but I don't know...
621 (setq nnfolder-group-alist
622 (cons (list group (setq active (cons 1 1)))
623 nnfolder-group-alist)))
625 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
626 (nnfolder-possibly-activate-groups group)
630 ;; This method has a problem if you've accidentally let the active list get
631 ;; out of sync with the files. This could happen, say, if you've
632 ;; accidentally gotten new mail with something other than Gnus (but why
633 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
634 ;; processing the file, ready to add new X-Gnus article number markers, and
635 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
638 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
639 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
640 ;; rest of the message looking for any more, possibly higher IDs. We'll
641 ;; assume the maximum that we find is the highest active. Note that this
642 ;; shouldn't cost us much extra time at all, but will be a lot less
643 ;; vulnerable to glitches between the mbox and the active file.
645 (defun nnfolder-read-folder (file)
647 (nnfolder-possibly-activate-groups nil)
648 ;; We should be paranoid here and make sure the group is in the alist,
649 ;; and add it if it isn't.
650 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
651 (set-buffer (setq nnfolder-current-buffer
652 (nnheader-find-file-noselect file nil 'raw)))
653 (buffer-disable-undo (current-buffer))
654 (let ((delim (concat "^" rmail-unix-mail-delimiter))
655 (marker (concat "\n" nnfolder-article-marker))
657 (active (car (cdr (assoc nnfolder-current-group
658 nnfolder-group-alist))))
659 activenumber activemin start end)
660 (goto-char (point-min))
662 ;; Anytime the active number is 1 or 0, it is supect. In that case,
663 ;; search the file manually to find the active number. Or, of course,
664 ;; if we're being paranoid. (This would also be the place to build
665 ;; other lists from the header markers, such as expunge lists, etc., if
666 ;; we ever desired to abandon the active file entirely for mboxes.)
667 (setq activenumber (cdr active))
668 (if (or nnfolder-ignore-active-file
671 (setq activemin (max (1- (lsh 1 23))
674 (while (and (search-forward marker nil t)
675 (re-search-forward number nil t))
676 (let ((newnum (string-to-number (buffer-substring
679 (setq activenumber (max activenumber newnum))
680 (setq activemin (min activemin newnum))))
681 (setcar active (min activemin activenumber))
682 (setcdr active (max activenumber (cdr active)))
683 (goto-char (point-min))))
685 ;; Keep track of the active number on our own, and insert it back into
686 ;; the active list when we're done. Also, prime the pump to cut down on
687 ;; the number of searches we do.
688 (setq end (point-marker))
689 (set-marker end (or (and (re-search-forward delim nil t)
692 (while (not (= end (point-max)))
693 (setq start (marker-position end))
695 ;; There may be more than one "From " line, so we skip past
697 (while (looking-at delim)
699 (set-marker end (or (and (re-search-forward delim nil t)
703 (if (not (search-forward marker end t))
705 (narrow-to-region start end)
706 (nnmail-insert-lines)
707 (nnfolder-insert-newsgroup-line
708 (cons nil (nnfolder-active-number nnfolder-current-group)))
711 ;; Make absolutely sure that the active list reflects reality!
712 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
717 ;;; nnfolder.el ends here