1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 num)
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 (cond ((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
185 (goto-char (point-max))
188 ;; If we couldn't find this article, skip over ranges
189 ;; of missing articles so we don't search the whole file
194 ;; Check that we are either at BOF or after an
195 ;; article with a lower number. We do this so we
196 ;; won't be confused by out-of-order article numbers,
197 ;; as caused by active file bogosity.
200 ((search-backward (concat "\n" nnfolder-article-marker)
202 (goto-char (match-end 0))
203 (setq num (string-to-int
205 (point) (point-at-eol))))
208 ;; Check that we are before an article with a
210 (search-forward (concat "\n" nnfolder-article-marker)
213 (setq num (string-to-int
215 (point) (point-at-eol))))
217 ;; Discard any article numbers before the one we're
220 (< (car articles) num))
221 (setq articles (cdr articles))))
223 (set-buffer nntp-server-buffer)
224 (nnheader-fold-continuation-lines)
227 (deffoo nnfolder-open-server (server &optional defs)
228 (nnoo-change-server 'nnfolder server defs)
229 (nnmail-activate 'nnfolder t)
230 (gnus-make-directory nnfolder-directory)
231 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
232 (and nnfolder-nov-directory
233 (gnus-make-directory nnfolder-nov-directory)))
234 (unless nnfolder-marks-is-evil
235 (and nnfolder-marks-directory
236 (gnus-make-directory nnfolder-marks-directory)))
238 ((not (file-exists-p nnfolder-directory))
239 (nnfolder-close-server)
240 (nnheader-report 'nnfolder "Couldn't create directory: %s"
242 ((not (file-directory-p (file-truename nnfolder-directory)))
243 (nnfolder-close-server)
244 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
246 (nnmail-activate 'nnfolder)
247 (nnheader-report 'nnfolder "Opened server %s using directory %s"
248 server nnfolder-directory)
251 (deffoo nnfolder-request-close ()
252 (let ((alist nnfolder-buffer-alist))
254 (nnfolder-close-group (caar alist) nil t)
255 (setq alist (cdr alist))))
256 (nnoo-close-server 'nnfolder)
257 (setq nnfolder-buffer-alist nil
258 nnfolder-group-alist nil))
260 (deffoo nnfolder-request-article (article &optional group server buffer)
261 (nnfolder-possibly-change-group group server)
263 (set-buffer nnfolder-current-buffer)
264 (goto-char (point-min))
265 (when (nnfolder-goto-article article)
269 (unless (and (nnmail-search-unix-mail-delim)
271 (goto-char (point-max)))
273 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
274 (set-buffer nntp-server-buffer)
276 (insert-buffer-substring nnfolder-current-buffer start stop)
277 (goto-char (point-min))
278 (while (looking-at "From ")
280 (insert "X-From-Line: ")
282 (if (numberp article)
283 (cons nnfolder-current-group article)
284 (goto-char (point-min))
285 (cons nnfolder-current-group
286 (if (search-forward (concat "\n" nnfolder-article-marker)
288 (string-to-int (buffer-substring
289 (point) (point-at-eol)))
292 (deffoo nnfolder-request-group (group &optional server dont-check)
293 (nnfolder-possibly-change-group group server t)
295 (if (not (assoc group nnfolder-group-alist))
296 (nnheader-report 'nnfolder "No such group: %s" group)
299 (nnheader-report 'nnfolder "Selected group %s" group)
301 (let* ((active (assoc group nnfolder-group-alist))
303 (range (cadr active)))
306 (nnheader-report 'nnfolder "No such group: %s" group))
307 ((null nnfolder-current-group)
308 (nnheader-report 'nnfolder "Empty group: %s" group))
310 (nnheader-report 'nnfolder "Selected group %s" group)
311 (nnheader-insert "211 %d %d %d %s\n"
312 (1+ (- (cdr range) (car range)))
313 (car range) (cdr range) group))))))))
315 (deffoo nnfolder-request-scan (&optional group server)
316 (nnfolder-possibly-change-group nil server)
317 (when nnfolder-get-new-mail
318 (nnfolder-possibly-change-group group server)
322 (let ((bufs nnfolder-buffer-alist))
325 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
326 (setq nnfolder-buffer-alist
327 (delq (car bufs) nnfolder-buffer-alist))
328 (set-buffer (nth 1 (car bufs)))
329 (nnfolder-save-buffer)
330 (kill-buffer (current-buffer)))
331 (setq bufs (cdr bufs))))))
335 ;; Don't close the buffer if we're not shutting down the server. This way,
336 ;; we can keep the buffer in the group buffer cache, and not have to grovel
337 ;; over the buffer again unless we add new mail to it or modify it in some
340 (deffoo nnfolder-close-group (group &optional server force)
341 ;; Make sure we _had_ the group open.
342 (when (or (assoc group nnfolder-buffer-alist)
343 (equal group nnfolder-current-group))
344 (let ((inf (assoc group nnfolder-buffer-alist)))
346 (when (and nnfolder-current-group
347 nnfolder-current-buffer)
348 (push (list nnfolder-current-group nnfolder-current-buffer)
349 nnfolder-buffer-alist))
350 (setq nnfolder-buffer-alist
351 (delq inf nnfolder-buffer-alist))
352 (setq nnfolder-current-buffer (cadr inf)
353 nnfolder-current-group (car inf))))
354 (when (and nnfolder-current-buffer
355 (buffer-name nnfolder-current-buffer))
357 (set-buffer nnfolder-current-buffer)
358 ;; If the buffer was modified, write the file out now.
359 (nnfolder-save-buffer)
360 ;; If we're shutting the server down, we need to kill the
361 ;; buffer and remove it from the open buffer list. Or, of
362 ;; course, if we're trying to minimize our space impact.
363 (kill-buffer (current-buffer))
364 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
365 nnfolder-buffer-alist)))))
366 (setq nnfolder-current-group nil
367 nnfolder-current-buffer nil)
370 (deffoo nnfolder-request-create-group (group &optional server args)
371 (nnfolder-possibly-change-group nil server)
372 (nnmail-activate 'nnfolder)
374 (unless (assoc group nnfolder-group-alist)
375 (push (list group (cons 1 0)) nnfolder-group-alist)
376 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
377 (nnfolder-read-folder group)))
380 (deffoo nnfolder-request-list (&optional server)
381 (nnfolder-possibly-change-group nil server)
383 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
384 (nnmail-find-file nnfolder-active-file)
385 (setq nnfolder-group-alist (nnmail-get-active)))
388 (deffoo nnfolder-request-newgroups (date &optional server)
389 (nnfolder-possibly-change-group nil server)
390 (nnfolder-request-list server))
392 (deffoo nnfolder-request-list-newsgroups (&optional server)
393 (nnfolder-possibly-change-group nil server)
395 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
396 (nnmail-find-file nnfolder-newsgroups-file))))
398 ;; Return a list consisting of all article numbers existing in the
401 (defun nnfolder-existing-articles ()
403 (when nnfolder-current-buffer
404 (set-buffer nnfolder-current-buffer)
405 (goto-char (point-min))
406 (let ((marker (concat "\n" nnfolder-article-marker))
409 (while (and (search-forward marker nil t)
410 (re-search-forward number nil t))
411 (let ((newnum (string-to-number (match-string 0))))
412 (if (nnmail-within-headers-p)
413 (push newnum numbers))))
414 ;; The article numbers are increasing, so this result is sorted.
415 (nreverse numbers)))))
417 (deffoo nnfolder-request-expire-articles
418 (articles newsgroup &optional server force)
419 (nnfolder-possibly-change-group newsgroup server)
421 ;; The articles we have deleted so far.
422 (deleted-articles nil)
423 ;; The articles that really exist and will
424 ;; be expired if they are old enough.
426 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
427 (nnmail-activate 'nnfolder)
430 (set-buffer nnfolder-current-buffer)
431 ;; Since messages are sorted in arrival order and expired in the
432 ;; same order, we can stop as soon as we find a message that is
434 (while (and maybe-expirable is-old)
435 (goto-char (point-min))
436 (when (and (nnfolder-goto-article (car maybe-expirable))
437 (search-forward (concat "\n" nnfolder-article-marker)
441 (nnmail-expired-article-p
444 (point) (progn (end-of-line) (point)))
445 force nnfolder-inhibit-expiry))
446 (unless (eq nnmail-expiry-target 'delete)
448 (nnfolder-request-article (car maybe-expirable)
449 newsgroup server (current-buffer))
450 (let ((nnfolder-current-directory nil))
451 (nnmail-expiry-target-group
452 nnmail-expiry-target newsgroup)))