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)
89 (defmacro nnfolder-article-string (article)
90 (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
94 (defvar nnfolder-current-server nil)
95 (defvar nnfolder-server-alist nil)
96 (defvar nnfolder-server-variables
98 (list 'nnfolder-directory nnfolder-directory)
99 (list 'nnfolder-active-file nnfolder-active-file)
100 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
101 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
102 '(nnfolder-current-group nil)
103 '(nnfolder-current-buffer nil)
104 '(nnfolder-status-string "")
105 '(nnfolder-group-alist nil)
106 '(nnfolder-buffer-alist nil)
107 '(nnfolder-active-timestamp nil)))
111 ;;; Interface functions
113 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
115 (set-buffer nntp-server-buffer)
117 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
118 article art-string start stop)
119 (nnfolder-possibly-change-group newsgroup)
120 (set-buffer nnfolder-current-buffer)
121 (goto-char (point-min))
122 (if (stringp (car sequence))
125 (setq article (car sequence))
126 (setq art-string (nnfolder-article-string article))
127 (set-buffer nnfolder-current-buffer)
128 (if (or (search-forward art-string nil t)
129 ;; Don't search the whole file twice! Also, articles
130 ;; probably have some locality by number, so searching
131 ;; backwards will be faster. Especially if we're at the
132 ;; beginning of the buffer :-). -SLB
133 (search-backward art-string nil t))
135 (setq start (or (re-search-backward delim-string nil t)
137 (search-forward "\n\n" nil t)
138 (setq stop (1- (point)))
139 (set-buffer nntp-server-buffer)
140 (insert (format "221 %d Article retrieved.\n" article))
141 (insert-buffer-substring nnfolder-current-buffer start stop)
142 (goto-char (point-max))
144 (setq sequence (cdr sequence)))
146 ;; Fold continuation lines.
147 (set-buffer nntp-server-buffer)
148 (goto-char (point-min))
149 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
150 (replace-match " " t t))
153 (defun nnfolder-open-server (server &optional defs)
154 (nnheader-init-server-buffer)
155 (if (equal server nnfolder-current-server)
157 (if nnfolder-current-server
158 (setq nnfolder-server-alist
159 (cons (list nnfolder-current-server
160 (nnheader-save-variables nnfolder-server-variables))
161 nnfolder-server-alist)))
162 (let ((state (assoc server nnfolder-server-alist)))
165 (nnheader-restore-variables (nth 1 state))
166 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
167 (nnheader-set-init-variables nnfolder-server-variables defs)))
168 (setq nnfolder-current-server server)))
170 (defun nnfolder-close-server (&optional server)
173 (defun nnfolder-server-opened (&optional server)
174 (and (equal server nnfolder-current-server)
176 (buffer-name nntp-server-buffer)))
178 (defun nnfolder-request-close ()
179 (let ((alist nnfolder-buffer-alist))
181 (nnfolder-close-group (car (car alist)) nil t)
182 (setq alist (cdr alist))))
183 (setq nnfolder-buffer-alist nil
184 nnfolder-group-alist nil))
186 (defun nnfolder-status-message (&optional server)
187 nnfolder-status-string)
189 (defun nnfolder-request-article (article &optional newsgroup server buffer)
190 (nnfolder-possibly-change-group newsgroup)
191 (if (stringp article)
194 (set-buffer nnfolder-current-buffer)
195 (goto-char (point-min))
196 (if (search-forward (nnfolder-article-string article) nil t)
198 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
201 (or (and (re-search-forward
202 (concat "^" rmail-unix-mail-delimiter) nil t)
204 (goto-char (point-max)))
206 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
207 (set-buffer nntp-server-buffer)
209 (insert-buffer-substring nnfolder-current-buffer start stop)
210 (goto-char (point-min))
211 (while (looking-at "From ")
213 (insert "X-From-Line: ")
217 (defun nnfolder-request-group (group &optional server dont-check)
219 (nnmail-activate 'nnfolder)
220 (nnfolder-possibly-change-group group)
221 (and (assoc group nnfolder-group-alist)
225 (nnfolder-get-new-mail group)
226 (let* ((active (assoc group nnfolder-group-alist))
228 (range (car (cdr active)))
229 (minactive (car range))
230 (maxactive (cdr range)))
231 ;; I've been getting stray 211 lines in my nnfolder active
232 ;; file. So, let's make sure that doesn't happen. -SLB
233 (set-buffer nntp-server-buffer)
237 (insert (format "211 %d %d %d %s\n"
238 (1+ (- maxactive minactive))
239 minactive maxactive group))
242 ;; Don't close the buffer if we're not shutting down the server. This way,
243 ;; we can keep the buffer in the group buffer cache, and not have to grovel
244 ;; over the buffer again unless we add new mail to it or modify it in some
247 (defun nnfolder-close-group (group &optional server force)
248 ;; Make sure we _had_ the group open.
249 (if (or (assoc group nnfolder-buffer-alist)
250 (equal group nnfolder-current-group))
252 (nnfolder-possibly-change-group group)
254 (set-buffer nnfolder-current-buffer)
255 ;; If the buffer was modified, write the file out now.
256 (and (buffer-modified-p) (save-buffer))
258 nnfolder-always-close)
259 ;; If we're shutting the server down, we need to kill the
260 ;; buffer and remove it from the open buffer list. Or, of
261 ;; course, if we're trying to minimize our space impact.
263 (kill-buffer (current-buffer))
264 (setq nnfolder-buffer-alist (delq (assoc group
265 nnfolder-buffer-alist)
266 nnfolder-buffer-alist)))))))
267 (setq nnfolder-current-group nil
268 nnfolder-current-buffer nil)
271 (defun nnfolder-request-create-group (group &optional server)
272 (nnmail-activate 'nnfolder)
273 (or (assoc group nnfolder-group-alist)
275 (setq nnfolder-group-alist
276 (cons (list group (setq active (cons 1 0)))
277 nnfolder-group-alist))
278 (nnfolder-possibly-change-group group)
279 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
282 (defun nnfolder-request-list (&optional server)
283 (if server (nnfolder-get-new-mail))
285 (nnmail-find-file nnfolder-active-file)
286 (setq nnfolder-group-alist (nnmail-get-active))))
288 (defun nnfolder-request-newgroups (date &optional server)
289 (nnfolder-request-list server))