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).
37 (eval-when-compile (require 'cl))
39 (defvar nnfolder-directory (expand-file-name "~/Mail/")
40 "The name of the mail box file in the users home directory.")
42 (defvar nnfolder-active-file
43 (concat (file-name-as-directory nnfolder-directory) "active")
44 "The name of the active file.")
46 ;; I renamed this variable to somehting more in keeping with the general GNU
49 (defvar nnfolder-ignore-active-file nil
50 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
51 Note that the active file is still saved, but it's values are not
52 used. This costs some extra time when scanning an mbox when opening
55 ;; Note that this variable may not be completely implemented yet. -SLB
57 (defvar nnfolder-always-close nil
58 "If non-nil, nnfolder attempts to only ever have one mbox open at a time.
59 This is a straight space/performance trade off, as the mboxes will have to
60 be scanned every time they are read in. If nil (default), nnfolder will
61 attempt to keep the buffers around (saving the nnfolder's buffer upon group
62 close, but not killing it), speeding some things up tremendously, especially
63 such things as moving mail. All buffers always get killed upon server close.")
65 (defvar nnfolder-newsgroups-file
66 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
67 "Mail newsgroups description file.")
69 (defvar nnfolder-get-new-mail t
70 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
72 (defvar nnfolder-prepare-save-mail-hook nil
73 "Hook run narrowed to an article before saving.")
77 (defconst nnfolder-version "nnfolder 1.0"
80 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
81 "String used to demarcate what the article number for a message is.")
83 (defvar nnfolder-current-group nil)
84 (defvar nnfolder-current-buffer nil)
85 (defvar nnfolder-status-string "")
86 (defvar nnfolder-group-alist nil)
87 (defvar nnfolder-buffer-alist nil)
88 (defvar nnfolder-active-timestamp nil)
92 (defvar nnfolder-current-server nil)
93 (defvar nnfolder-server-alist nil)
94 (defvar nnfolder-server-variables
96 (list 'nnfolder-directory nnfolder-directory)
97 (list 'nnfolder-active-file nnfolder-active-file)
98 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
99 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
100 '(nnfolder-current-group nil)
101 '(nnfolder-current-buffer nil)
102 '(nnfolder-status-string "")
103 '(nnfolder-group-alist nil)
104 '(nnfolder-buffer-alist nil)
105 '(nnfolder-active-timestamp nil)))
109 ;;; Interface functions
111 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
113 (set-buffer nntp-server-buffer)
115 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
116 article art-string start stop)
117 (nnfolder-possibly-change-group newsgroup)
118 (set-buffer nnfolder-current-buffer)
119 (goto-char (point-min))
120 (if (stringp (car sequence))
123 (setq article (car sequence))
124 (setq art-string (nnfolder-article-string article))
125 (set-buffer nnfolder-current-buffer)
126 (if (or (search-forward art-string nil t)
127 ;; Don't search the whole file twice! Also, articles
128 ;; probably have some locality by number, so searching
129 ;; backwards will be faster. Especially if we're at the
130 ;; beginning of the buffer :-). -SLB
131 (search-backward art-string nil t))
133 (setq start (or (re-search-backward delim-string nil t)
135 (search-forward "\n\n" nil t)
136 (setq stop (1- (point)))
137 (set-buffer nntp-server-buffer)
138 (insert (format "221 %d Article retrieved.\n" article))
139 (insert-buffer-substring nnfolder-current-buffer start stop)
140 (goto-char (point-max))
142 (setq sequence (cdr sequence)))
144 (set-buffer nntp-server-buffer)
145 (nnheader-fold-continuation-lines)
148 (defun nnfolder-open-server (server &optional defs)
149 (nnheader-init-server-buffer)
150 (if (equal server nnfolder-current-server)
152 (if nnfolder-current-server
153 (setq nnfolder-server-alist
154 (cons (list nnfolder-current-server
155 (nnheader-save-variables nnfolder-server-variables))
156 nnfolder-server-alist)))
157 (let ((state (assoc server nnfolder-server-alist)))
160 (nnheader-restore-variables (nth 1 state))
161 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
162 (nnheader-set-init-variables nnfolder-server-variables defs)))
163 (setq nnfolder-current-server server)))
165 (defun nnfolder-close-server (&optional server)
166 (setq nnfolder-current-server nil)
169 (defun nnfolder-server-opened (&optional server)
170 (and (equal server nnfolder-current-server)
172 (buffer-name nntp-server-buffer)))
174 (defun nnfolder-request-close ()
175 (let ((alist nnfolder-buffer-alist))
177 (nnfolder-close-group (car (car alist)) nil t)
178 (setq alist (cdr alist))))
179 (setq nnfolder-buffer-alist nil
180 nnfolder-group-alist nil))
182 (defun nnfolder-status-message (&optional server)
183 nnfolder-status-string)
185 (defun nnfolder-request-article (article &optional newsgroup server buffer)
186 (nnfolder-possibly-change-group newsgroup)
188 (set-buffer nnfolder-current-buffer)
189 (goto-char (point-min))
190 (if (search-forward (nnfolder-article-string article) nil t)
192 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
195 (or (and (re-search-forward
196 (concat "^" rmail-unix-mail-delimiter) nil t)
198 (goto-char (point-max)))
200 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
201 (set-buffer nntp-server-buffer)
203 (insert-buffer-substring nnfolder-current-buffer start stop)
204 (goto-char (point-min))
205 (while (looking-at "From ")
207 (insert "X-From-Line: ")
209 (if (numberp article)
210 (cons nnfolder-current-group article)
211 (goto-char (point-min))
212 (search-forward (concat "\n" nnfolder-article-marker))
213 (cons nnfolder-current-group
216 (point) (progn (end-of-line) (point)))))))))))
218 (defun nnfolder-request-group (group &optional server dont-check)
220 (nnmail-activate 'nnfolder)
221 (when (assoc group nnfolder-group-alist)
224 (let* ((active (assoc group nnfolder-group-alist))
226 (range (car (cdr active)))
227 (minactive (car range))
228 (maxactive (cdr range)))
229 (set-buffer nntp-server-buffer)
232 (insert (format "211 %d %d %d %s\n"
233 (1+ (- maxactive minactive))
234 minactive maxactive group))
237 (defun nnfolder-request-scan (&optional group server)
241 (let ((bufs nnfolder-buffer-alist))
244 (if (not (buffer-name (nth 1 (car bufs))))
245 (setq nnfolder-buffer-alist
246 (delq (car bufs) nnfolder-buffer-alist))
247 (set-buffer (nth 1 (car bufs)))
248 (and (buffer-modified-p) (save-buffer)))
249 (setq bufs (cdr bufs))))))
253 ;; Don't close the buffer if we're not shutting down the server. This way,
254 ;; we can keep the buffer in the group buffer cache, and not have to grovel
255 ;; over the buffer again unless we add new mail to it or modify it in some
258 (defun nnfolder-close-group (group &optional server force)
259 ;; Make sure we _had_ the group open.
260 (if (or (assoc group nnfolder-buffer-alist)
261 (equal group nnfolder-current-group))
263 (nnfolder-possibly-change-group group)
265 (set-buffer nnfolder-current-buffer)
266 ;; If the buffer was modified, write the file out now.
267 (and (buffer-modified-p) (save-buffer))
269 nnfolder-always-close)
270 ;; If we're shutting the server down, we need to kill the
271 ;; buffer and remove it from the open buffer list. Or, of
272 ;; course, if we're trying to minimize our space impact.
274 (kill-buffer (current-buffer))
275 (setq nnfolder-buffer-alist (delq (assoc group
276 nnfolder-buffer-alist)
277 nnfolder-buffer-alist)))))))
278 (setq nnfolder-current-group nil
279 nnfolder-current-buffer nil)
282 (defun nnfolder-request-create-group (group &optional server)
283 (nnmail-activate 'nnfolder)
284 (or (assoc group nnfolder-group-alist)
286 (setq nnfolder-group-alist
287 (cons (list group (setq active (cons 1 0)))
288 nnfolder-group-alist))
289 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
292 (defun nnfolder-request-list (&optional server)
294 (nnmail-find-file nnfolder-active-file)
295 (setq nnfolder-group-alist (nnmail-get-active))))
297 (defun nnfolder-request-newgroups (date &optional server)
298 (nnfolder-request-list server))
300 (defun nnfolder-request-list-newsgroups (&optional server)
302 (nnmail-find-file nnfolder-newsgroups-file)))
304 (defun nnfolder-request-post (&optional server)
305 (mail-send-and-exit nil))
307 (defun nnfolder-request-expire-articles
308 (articles newsgroup &optional server force)
309 (nnfolder-possibly-change-group newsgroup)
312 (nnmail-activate 'nnfolder)
315 (set-buffer nnfolder-current-buffer)
316 (while (and articles is-old)
317 (goto-char (point-min))
318 (if (search-forward (nnfolder-article-string (car articles)) nil t)
320 (nnmail-expired-article-p
323 (point) (progn (end-of-line) (point))) force))
325 (and gnus-verbose-backends
326 (message "Deleting article %d..."
327 (car articles) newsgroup))
328 (nnfolder-delete-mail))
329 (setq rest (cons (car articles) rest))))
330 (setq articles (cdr articles)))
331 (and (buffer-modified-p) (save-buffer))
332 ;; Find the lowest active article in this group.
333 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
334 (marker (concat "\n" nnfolder-article-marker))
336 (activemin (cdr active)))
337 (goto-char (point-min))
338 (while (and (search-forward marker nil t)
339 (re-search-forward number nil t))
340 (setq activemin (min activemin
341 (string-to-number (buffer-substring
344 (setcar active activemin))
345 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
346 (nconc rest articles))))
348 (defun nnfolder-request-move-article
349 (article group server accept-form &optional last)
350 (nnfolder-possibly-change-group group)
351 (let ((buf (get-buffer-create " *nnfolder move*"))
354 (nnfolder-request-article article group server)
357 (buffer-disable-undo (current-buffer))
359 (insert-buffer-substring nntp-server-buffer)
360 (goto-char (point-min))
361 (while (re-search-forward
362 (concat "^" nnfolder-article-marker)
363 (save-excursion (search-forward "\n\n" nil t) (point)) t)
364 (delete-region (progn (beginning-of-line) (point))
365 (progn (forward-line 1) (point))))
366 (setq result (eval accept-form))
370 (nnfolder-possibly-change-group group)
371 (set-buffer nnfolder-current-buffer)
372 (goto-char (point-min))
373 (if (search-forward (nnfolder-article-string article) nil t)
374 (nnfolder-delete-mail))
380 (defun nnfolder-request-accept-article (group &optional last)
381 (and (stringp group) (nnfolder-possibly-change-group group))
382 (let ((buf (current-buffer))
384 (goto-char (point-min))
385 (when (looking-at "X-From-Line: ")
386 (replace-match "From "))
388 (nnfolder-request-list)
391 (goto-char (point-min))
392 (search-forward "\n\n" nil t)
394 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
395 (delete-region (point) (progn (forward-line 1) (point))))
396 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
398 (set-buffer nnfolder-current-buffer)
399 (and last (buffer-modified-p) (save-buffer))))
400 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
403 (defun nnfolder-request-replace-article (article group buffer)
404 (nnfolder-possibly-change-group group)
406 (set-buffer nnfolder-current-buffer)
407 (goto-char (point-min))
408 (if (not (search-forward (nnfolder-article-string article) nil t))
410 (nnfolder-delete-mail t t)
411 (insert-buffer-substring buffer)
412 (and (buffer-modified-p) (save-buffer))
415 (defun nnfolder-request-delete-group (group &optional force server)
416 (nnfolder-close-group group server t)
417 ;; Delete all articles in GROUP.
419 () ; Don't delete the articles.
420 ;; Delete the file that holds the group.
422 (delete-file (concat (file-name-as-directory nnfolder-directory)
425 ;; Remove the group from all structures.
426 (setq nnfolder-group-alist
427 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
428 nnfolder-current-group nil
429 nnfolder-current-buffer nil)
430 ;; Save the active file.
431 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
434 (defun nnfolder-request-rename-group (group new-name &optional server)
435 (nnfolder-possibly-change-group group)
437 (set-buffer nnfolder-current-buffer)
438 (and (file-writable-p buffer-file-name)
441 (rename-file buffer-file-name
442 (concat (file-name-as-directory nnfolder-directory)
446 ;; That went ok, so we change the internal structures.
447 (let ((entry (assoc group nnfolder-group-alist)))
448 (and entry (setcar entry new-name))
449 (setq nnfolder-current-buffer nil
450 nnfolder-current-group nil)
451 ;; Save the new group alist.
452 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
453 ;; We kill the buffer instead of renaming it and stuff.
454 (kill-buffer (current-buffer))
458 ;;; Internal functions.
460 (defun nnfolder-article-string (article)
461 (if (numberp article)
462 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
463 (concat "\nMessage-ID: " article)))
465 (defun nnfolder-delete-mail (&optional force leave-delim)
466 ;; Beginning of the article.
471 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
472 (if leave-delim (progn (forward-line 1) (point))
473 (match-beginning 0)))
476 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
478 (if (and (not (bobp)) leave-delim)
479 (progn (forward-line -2) (point))
480 (match-beginning 0)))
482 (delete-region (point-min) (point-max)))))
484 (defun nnfolder-possibly-change-group (group)
485 (or (file-exists-p nnfolder-directory)
486 (make-directory (directory-file-name nnfolder-directory)))
487 (nnfolder-possibly-activate-groups nil)
488 (or (assoc group nnfolder-group-alist)
489 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
492 (setq nnfolder-group-alist
493 (cons (list group (cons 1 0)) nnfolder-group-alist))
494 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
496 (if (and (equal group nnfolder-current-group)
497 nnfolder-current-buffer
498 (buffer-name nnfolder-current-buffer))
500 (setq nnfolder-current-group group)
502 ;; If we have to change groups, see if we don't already have the mbox
503 ;; in memory. If we do, verify the modtime and destroy the mbox if
504 ;; needed so we can rescan it.
505 (if (setq inf (assoc group nnfolder-buffer-alist))
506 (setq nnfolder-current-buffer (nth 1 inf)))
508 ;; If the buffer is not live, make sure it isn't in the alist. If it
509 ;; is live, verify that nobody else has touched the file since last
511 (if (or (not (and nnfolder-current-buffer
512 (buffer-name nnfolder-current-buffer)))
513 (not (and (bufferp nnfolder-current-buffer)
514 (verify-visited-file-modtime
515 nnfolder-current-buffer))))
517 (if (and nnfolder-current-buffer
518 (buffer-name nnfolder-current-buffer)
519 (bufferp nnfolder-current-buffer))
520 (kill-buffer nnfolder-current-buffer))
521 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
527 (setq file (concat (file-name-as-directory nnfolder-directory)
529 (if (file-directory-p (file-truename file))
531 (if (not (file-exists-p file))
532 (write-region 1 1 file t 'nomesg))
533 (setq nnfolder-current-buffer
534 (set-buffer (nnfolder-read-folder file)))
535 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
536 nnfolder-buffer-alist)))))))
537 (setq nnfolder-current-group group))
539 (defun nnfolder-save-mail (&optional group)
540 "Called narrowed to an article."
541 (let* ((nnmail-split-methods
542 (if group (list (list group "")) nnmail-split-methods))
544 (nreverse (nnmail-article-group 'nnfolder-active-number)))
545 (delim (concat "^" rmail-unix-mail-delimiter))
547 (goto-char (point-min))
548 ;; This might come from somewhere else.
549 (unless (looking-at delim)
550 (insert "From nobody " (current-time-string) "\n")
551 (goto-char (point-min)))
552 ;; Quote all "From " lines in the article.
554 (while (re-search-forward delim nil t)
557 (setq save-list group-art-list)
558 (nnmail-insert-lines)
559 (nnmail-insert-xref group-art-list)
560 (run-hooks 'nnfolder-prepare-save-mail-hook)
562 ;; Insert the mail into each of the destination groups.
563 (while group-art-list
564 (setq group-art (car group-art-list)
565 group-art-list (cdr group-art-list))
567 ;; Kill the previous newsgroup markers.
568 (goto-char (point-min))
569 (search-forward "\n\n" nil t)
571 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
572 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
574 ;; Insert the new newsgroup marker.
575 (nnfolder-possibly-change-group (car group-art))
576 (nnfolder-insert-newsgroup-line group-art)
577 (let ((beg (point-min))
579 (obuf (current-buffer)))
580 (set-buffer nnfolder-current-buffer)
581 (goto-char (point-max))
582 (insert-buffer-substring obuf beg end)
585 ;; Did we save it anywhere?
588 (defun nnfolder-insert-newsgroup-line (group-art)
590 (goto-char (point-min))
591 (if (search-forward "\n\n" nil t)
594 (insert (format (concat nnfolder-article-marker "%d %s\n")
595 (cdr group-art) (current-time-string)))))))
597 (defun nnfolder-possibly-activate-groups (&optional group)
599 ;; If we're looking for the activation of a specific group, find out
600 ;; its real name and switch to it.
601 (if group (nnfolder-possibly-change-group group))
602 ;; If the group alist isn't active, activate it now.
603 (nnmail-activate 'nnfolder)))
605 (defun nnfolder-active-number (group)
607 ;; Find the next article number in GROUP.
609 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
611 (setcdr active (1+ (cdr active)))
612 ;; This group is new, so we create a new entry for it.
613 ;; This might be a bit naughty... creating groups on the drop of
614 ;; a hat, but I don't know...
615 (setq nnfolder-group-alist
616 (cons (list group (setq active (cons 1 1)))
617 nnfolder-group-alist)))
619 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
620 (nnfolder-possibly-activate-groups group))))
623 ;; This method has a problem if you've accidentally let the active list get
624 ;; out of sync with the files. This could happen, say, if you've
625 ;; accidentally gotten new mail with something other than Gnus (but why
626 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
627 ;; processing the file, ready to add new X-Gnus article number markers, and
628 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
631 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
632 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
633 ;; rest of the message looking for any more, possibly higher IDs. We'll
634 ;; assume the maximum that we find is the highest active. Note that this
635 ;; shouldn't cost us much extra time at all, but will be a lot less
636 ;; vulnerable to glitches between the mbox and the active file.
638 (defun nnfolder-read-folder (file)
640 (nnfolder-possibly-activate-groups nil)
641 ;; We should be paranoid here and make sure the group is in the alist,
642 ;; and add it if it isn't.
643 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
644 (set-buffer (setq nnfolder-current-buffer
645 (nnheader-find-file-noselect file nil 'raw)))
646 (buffer-disable-undo (current-buffer))
647 (let ((delim (concat "^" rmail-unix-mail-delimiter))
648 (marker (concat "\n" nnfolder-article-marker))
650 (active (car (cdr (assoc nnfolder-current-group
651 nnfolder-group-alist))))
652 activenumber activemin start end)
653 (goto-char (point-min))
655 ;; Anytime the active number is 1 or 0, it is supect. In that case,
656 ;; search the file manually to find the active number. Or, of course,
657 ;; if we're being paranoid. (This would also be the place to build
658 ;; other lists from the header markers, such as expunge lists, etc., if
659 ;; we ever desired to abandon the active file entirely for mboxes.)
660 (setq activenumber (cdr active))
661 (if (or nnfolder-ignore-active-file
664 (setq activemin (max (1- (lsh 1 23))
667 (while (and (search-forward marker nil t)
668 (re-search-forward number nil t))
669 (let ((newnum (string-to-number (buffer-substring
672 (setq activenumber (max activenumber newnum))
673 (setq activemin (min activemin newnum))))
674 (setcar active (max 1 (min activemin activenumber)))
675 (setcdr active (max activenumber (cdr active)))
676 (goto-char (point-min))))
678 ;; Keep track of the active number on our own, and insert it back into
679 ;; the active list when we're done. Also, prime the pump to cut down on
680 ;; the number of searches we do.
681 (setq end (point-marker))
682 (set-marker end (or (and (re-search-forward delim nil t)
685 (while (not (= end (point-max)))
686 (setq start (marker-position end))
688 ;; There may be more than one "From " line, so we skip past
690 (while (looking-at delim)
692 (set-marker end (or (and (re-search-forward delim nil t)
696 (if (not (search-forward marker end t))
698 (narrow-to-region start end)
699 (nnmail-insert-lines)
700 (nnfolder-insert-newsgroup-line
701 (cons nil (nnfolder-active-number nnfolder-current-group)))
704 ;; Make absolutely sure that the active list reflects reality!
705 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
710 ;;; nnfolder.el ends here