(uncompface-use-external): Default to undecided.
[gnus] / lisp / nnfolder.el
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.
4
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>
10 ;; Keywords: mail
11
12 ;; This file is part of GNU Emacs.
13
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)
17 ;; any later version.
18
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.
23
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.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'message)
35 (require 'nnmail)
36 (require 'nnoo)
37 (eval-when-compile (require 'cl))
38 (require 'gnus)
39 (require 'gnus-util)
40 (require 'gnus-range)
41
42 (eval-and-compile
43   (autoload 'gnus-article-unpropagatable-p "gnus-sum")
44   (autoload 'gnus-intersection "gnus-range"))
45
46 (nnoo-declare nnfolder)
47
48 (defvoo nnfolder-directory (expand-file-name message-directory)
49   "The name of the nnfolder directory.")
50
51 (defvoo nnfolder-nov-directory nil
52   "The name of the nnfolder NOV directory.
53 If nil, `nnfolder-directory' is used.")
54
55 (defvoo nnfolder-marks-directory nil
56   "The name of the nnfolder MARKS directory.
57 If nil, `nnfolder-directory' is used.")
58
59 (defvoo nnfolder-active-file
60     (nnheader-concat nnfolder-directory "active")
61   "The name of the active file.")
62
63 ;; I renamed this variable to something more in keeping with the general GNU
64 ;; style. -SLB
65
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.")
72
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.")
80
81 (defvoo nnfolder-newsgroups-file
82     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
83   "Mail newsgroups description file.")
84
85 (defvoo nnfolder-get-new-mail t
86   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
87
88 (defvoo nnfolder-prepare-save-mail-hook nil
89   "Hook run narrowed to an article before saving.")
90
91 (defvoo nnfolder-save-buffer-hook nil
92   "Hook run before saving the nnfolder mbox buffer.")
93
94
95 (defvoo nnfolder-inhibit-expiry nil
96   "If non-nil, inhibit expiry.")
97
98 \f
99
100 (defconst nnfolder-version "nnfolder 2.0"
101   "nnfolder version.")
102
103 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
104   "String used to demarcate what the article number for a message is.")
105
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
120
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.")
129
130 (defvoo nnfolder-nov-file-suffix ".nov")
131
132 (defvoo nnfolder-nov-buffer-alist nil)
133
134 (defvar nnfolder-nov-buffer-file-name nil)
135
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.")
144
145 (defvoo nnfolder-marks nil)
146
147 (defvoo nnfolder-marks-file-suffix ".mrk")
148
149 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
150
151 \f
152
153 ;;; Interface functions
154
155 (nnoo-define-basics nnfolder)
156
157 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
158   (save-excursion
159     (set-buffer nntp-server-buffer)
160     (erase-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))
167             'headers
168           (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
169               'nov
170             (setq articles (gnus-sorted-intersection
171                             ;; Is ARTICLES sorted?
172                             (sort articles '<)
173                             (nnfolder-existing-articles)))
174             (while (setq article (pop articles))
175               (set-buffer nnfolder-current-buffer)
176               (cond ((nnfolder-goto-article article)
177                      (setq start (point))
178                      (setq stop (if (search-forward "\n\n" nil t)
179                                     (1- (point))
180                                   (point-max)))
181                      (set-buffer nntp-server-buffer)
182                      (insert (format "221 %d Article retrieved.\n" article))
183                      (insert-buffer-substring nnfolder-current-buffer
184                                               start stop)
185                      (goto-char (point-max))
186                      (insert ".\n"))
187
188                     ;; If we couldn't find this article, skip over ranges
189                     ;; of missing articles so we don't search the whole file
190                     ;; for each of them.
191                     ((numberp article)
192                      (setq start (point))
193                      (and
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.
198                       (cond
199                        ((bobp))
200                        ((search-backward (concat "\n" nnfolder-article-marker)
201                                          nil t)
202                         (goto-char (match-end 0))
203                         (setq num (string-to-int
204                                    (buffer-substring
205                                     (point) (point-at-eol))))
206                         (goto-char start)
207                         (< num article)))
208                       ;; Check that we are before an article with a
209                       ;; higher number.
210                       (search-forward (concat "\n" nnfolder-article-marker)
211                                       nil t)
212                       (progn
213                         (setq num (string-to-int
214                                    (buffer-substring
215                                     (point) (point-at-eol))))
216                         (> num article))
217                       ;; Discard any article numbers before the one we're
218                       ;; now looking at.
219                       (while (and articles
220                                   (< (car articles) num))
221                         (setq articles (cdr articles))))
222                      (goto-char start))))
223             (set-buffer nntp-server-buffer)
224             (nnheader-fold-continuation-lines)
225             'headers))))))
226
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)))
237   (cond
238    ((not (file-exists-p nnfolder-directory))
239     (nnfolder-close-server)
240     (nnheader-report 'nnfolder "Couldn't create directory: %s"
241                      nnfolder-directory))
242    ((not (file-directory-p (file-truename nnfolder-directory)))
243     (nnfolder-close-server)
244     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
245    (t
246     (nnmail-activate 'nnfolder)
247     (nnheader-report 'nnfolder "Opened server %s using directory %s"
248                      server nnfolder-directory)
249     t)))
250
251 (deffoo nnfolder-request-close ()
252   (let ((alist nnfolder-buffer-alist))
253     (while 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))
259
260 (deffoo nnfolder-request-article (article &optional group server buffer)
261   (nnfolder-possibly-change-group group server)
262   (save-excursion
263     (set-buffer nnfolder-current-buffer)
264     (goto-char (point-min))
265     (when (nnfolder-goto-article article)
266       (let (start stop)
267         (setq start (point))
268         (forward-line 1)
269         (unless (and (nnmail-search-unix-mail-delim)
270                      (forward-line -1))
271           (goto-char (point-max)))
272         (setq stop (point))
273         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
274           (set-buffer nntp-server-buffer)
275           (erase-buffer)
276           (insert-buffer-substring nnfolder-current-buffer start stop)
277           (goto-char (point-min))
278           (while (looking-at "From ")
279             (delete-char 5)
280             (insert "X-From-Line: ")
281             (forward-line 1))
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)
287                                       nil t)
288                       (string-to-int (buffer-substring
289                                       (point) (point-at-eol)))
290                     -1))))))))
291
292 (deffoo nnfolder-request-group (group &optional server dont-check)
293   (nnfolder-possibly-change-group group server t)
294   (save-excursion
295     (if (not (assoc group nnfolder-group-alist))
296         (nnheader-report 'nnfolder "No such group: %s" group)
297       (if dont-check
298           (progn
299             (nnheader-report 'nnfolder "Selected group %s" group)
300             t)
301         (let* ((active (assoc group nnfolder-group-alist))
302                (group (car active))
303                (range (cadr active)))
304           (cond
305            ((null active)
306             (nnheader-report 'nnfolder "No such group: %s" group))
307            ((null nnfolder-current-group)
308             (nnheader-report 'nnfolder "Empty group: %s" group))
309            (t
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))))))))
314
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)
319     (nnmail-get-new-mail
320      'nnfolder
321      (lambda ()
322        (let ((bufs nnfolder-buffer-alist))
323          (save-excursion
324            (while bufs
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))))))
332      nnfolder-directory
333      group)))
334
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
338 ;; way.
339
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)))
345       (when inf
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))
356       (save-excursion
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)
368   t)
369
370 (deffoo nnfolder-request-create-group (group &optional server args)
371   (nnfolder-possibly-change-group nil server)
372   (nnmail-activate 'nnfolder)
373   (when group
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)))
378   t)
379
380 (deffoo nnfolder-request-list (&optional server)
381   (nnfolder-possibly-change-group nil server)
382   (save-excursion
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)))
386     t))
387
388 (deffoo nnfolder-request-newgroups (date &optional server)
389   (nnfolder-possibly-change-group nil server)
390   (nnfolder-request-list server))
391
392 (deffoo nnfolder-request-list-newsgroups (&optional server)
393   (nnfolder-possibly-change-group nil server)
394   (save-excursion
395     (let ((nnmail-file-coding-system nnfolder-file-coding-system))
396       (nnmail-find-file nnfolder-newsgroups-file))))
397
398 ;; Return a list consisting of all article numbers existing in the
399 ;; current folder.
400
401 (defun nnfolder-existing-articles ()
402   (save-excursion
403     (when nnfolder-current-buffer
404       (set-buffer nnfolder-current-buffer)
405       (goto-char (point-min))
406       (let ((marker (concat "\n" nnfolder-article-marker))
407             (number "[0-9]+")
408             numbers)
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)))))
416
417 (deffoo nnfolder-request-expire-articles
418     (articles newsgroup &optional server force)
419   (nnfolder-possibly-change-group newsgroup server)
420   (let* ((is-old t)
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.
425          (maybe-expirable
426           (gnus-sorted-intersection articles (nnfolder-existing-articles))))
427     (nnmail-activate 'nnfolder)
428
429     (save-excursion
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
433       ;; too old.
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)
438                                    nil t))
439           (forward-sexp)
440           (when (setq is-old
441                       (nnmail-expired-article-p
442                        newsgroup
443                        (buffer-substring
444                         (point) (progn (end-of-line) (point)))
445                        force nnfolder-inhibit-expiry))
446             (unless (eq nnmail-expiry-target 'delete)
447               (with-temp-buffer
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)))
453        &n