1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995-2013 Free Software Foundation, Inc.
5 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
6 ;; Simon Josefsson <simon@josefsson.org>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; Keywords: news, mail
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
29 ;; For an overview of what the interface functions do, please see the
38 (eval-when-compile (require 'cl))
40 ;; FIXME first is unused in this file.
41 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
42 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
46 (defvoo nnml-directory message-directory
47 "Spool directory for the nnml mail backend.")
49 (defvoo nnml-active-file
50 (expand-file-name "active" nnml-directory)
53 (defvoo nnml-newsgroups-file
54 (expand-file-name "newsgroups" nnml-directory)
55 "Mail newsgroups description file.")
57 (defvoo nnml-get-new-mail t
58 "If non-nil, nnml will check the incoming mail file and split the mail.")
60 (defvoo nnml-nov-is-evil nil
61 "If non-nil, Gnus will never generate and use nov databases for mail spools.
62 Using nov databases will speed up header fetching considerably.
63 This variable shouldn't be flipped much. If you have, for some reason,
64 set this to t, and want to set it to nil again, you should always run
65 the `nnml-generate-nov-databases' command. The function will go
66 through all nnml directories and generate nov databases for them
67 all. This may very well take some time.")
69 (defvoo nnml-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
72 (defvoo nnml-inhibit-expiry nil
73 "If non-nil, inhibit expiry.")
75 (defvoo nnml-use-compressed-files nil
76 "If non-nil, allow using compressed message files.
78 If it is a string, use it as the file extension which specifies
79 the compression program. You can set it to \".bz2\" if your Emacs
80 supports auto-compression using the bzip2 program. A value of t
81 is equivalent to \".gz\".")
83 (defvoo nnml-compressed-files-size-threshold 1000
84 "Default size threshold for compressed message files.
85 Message files with bodies larger than that many characters will
86 be automatically compressed if `nnml-use-compressed-files' is
91 (defconst nnml-version "nnml 1.0"
94 (defvoo nnml-nov-file-name ".overview")
96 (defvoo nnml-current-directory nil)
97 (defvoo nnml-current-group nil)
98 (defvoo nnml-status-string "")
99 (defvoo nnml-nov-buffer-alist nil)
100 (defvoo nnml-group-alist nil)
101 (defvoo nnml-active-timestamp nil)
102 (defvoo nnml-article-file-alist nil)
104 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
106 (defvar nnml-nov-buffer-file-name nil)
108 (defvoo nnml-file-coding-system nnmail-file-coding-system)
111 ;;; Interface functions.
113 (nnoo-define-basics nnml)
116 (defsubst nnml-group-name-charset (group server-or-method)
117 (gnus-group-name-charset
118 (if (stringp server-or-method)
119 (gnus-server-to-method
120 (if (string-match "\\+" server-or-method)
121 (concat (substring server-or-method 0 (match-beginning 0))
122 ":" (substring server-or-method (match-end 0)))
123 (concat "nnml:" server-or-method)))
124 (or server-or-method gnus-command-method '(nnml "")))
127 (defun nnml-decoded-group-name (group &optional server-or-method)
128 "Return a decoded group name of GROUP on SERVER-OR-METHOD."
129 (if nnmail-group-names-not-encoded-p
131 (mm-decode-coding-string
133 (nnml-group-name-charset group server-or-method))))
135 (defun nnml-encoded-group-name (group &optional server-or-method)
136 "Return an encoded group name of GROUP on SERVER-OR-METHOD."
137 (mm-encode-coding-string
139 (nnml-group-name-charset group server-or-method)))
141 (defun nnml-group-pathname (group &optional file server)
142 "Return an absolute file name of FILE for GROUP on SERVER."
143 (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
144 nnml-directory file))
146 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
147 (when (nnml-possibly-change-directory group server)
148 (with-current-buffer nntp-server-buffer
151 (number (length sequence))
153 (file-name-coding-system nnmail-pathname-coding-system)
155 (if (stringp (car sequence))
157 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
160 (setq article (car sequence))
161 (setq file (nnml-article-to-file article))
164 (not (file-directory-p file)))
165 (insert (format "221 %d Article retrieved.\n" article))
167 (nnheader-insert-head file)
169 (if (re-search-forward "\n\r?\n" nil t)
171 (goto-char (point-max))
174 (delete-region (point) (point-max)))
175 (setq sequence (cdr sequence))
176 (setq count (1+ count))
177 (and (numberp nnmail-large-newsgroup)
178 (> number nnmail-large-newsgroup)
180 (nnheader-message 6 "nnml: Receiving headers... %d%%"
181 (/ (* count 100) number))))
183 (and (numberp nnmail-large-newsgroup)
184 (> number nnmail-large-newsgroup)
185 (nnheader-message 6 "nnml: Receiving headers...done"))
187 (nnheader-fold-continuation-lines)
190 (deffoo nnml-open-server (server &optional defs)
191 (nnoo-change-server 'nnml server defs)
192 (when (not (file-exists-p nnml-directory))
193 (ignore-errors (make-directory nnml-directory t)))
195 ((not (file-exists-p nnml-directory))
197 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
198 ((not (file-directory-p (file-truename nnml-directory)))
200 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
202 (nnheader-report 'nnml "Opened server %s using directory %s"
203 server nnml-directory)
206 (deffoo nnml-request-regenerate (server)
207 (nnml-possibly-change-directory nil server)
208 (nnml-generate-nov-databases server)
211 (deffoo nnml-request-article (id &optional group server buffer)
212 (nnml-possibly-change-directory group server)
213 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
214 (file-name-coding-system nnmail-pathname-coding-system)
215 path gpath group-num)
217 (when (and (setq group-num (nnml-find-group-number id server))
219 (assq (cdr group-num)
220 (nnheader-article-to-file-alist
221 (setq gpath (nnml-group-pathname (car group-num)
223 (nnml-update-file-alist)
224 (setq path (concat gpath (if nnml-use-compressed-files
225 (cdr (assq (cdr group-num)
226 nnml-article-file-alist))
227 (number-to-string (cdr group-num))))))
228 (setq path (nnml-article-to-file id)))
231 (nnheader-report 'nnml "No such article: %s" id))
232 ((not (file-exists-p path))
233 (nnheader-report 'nnml "No such file: %s" path))
234 ((file-directory-p path)
235 (nnheader-report 'nnml "File is a directory: %s" path))
236 ((not (save-excursion (let ((nnmail-file-coding-system
237 nnml-file-coding-system))
238 (nnmail-find-file path))))
239 (nnheader-report 'nnml "Couldn't read file: %s" path))
241 (nnheader-report 'nnml "Article %s retrieved" id)
242 ;; We return the article number.
243 (cons (if group-num (car group-num) group)
244 (string-to-number (file-name-nondirectory path)))))))
246 (deffoo nnml-request-group (group &optional server dont-check info)
247 (let ((file-name-coding-system nnmail-pathname-coding-system)
248 (decoded (nnml-decoded-group-name group server)))
250 ((not (nnml-possibly-change-directory group server))
251 (nnheader-report 'nnml "Invalid group (no such directory)"))
252 ((not (file-exists-p nnml-current-directory))
253 (nnheader-report 'nnml "Directory %s does not exist"
254 nnml-current-directory))
255 ((not (file-directory-p nnml-current-directory))
256 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
258 (nnheader-report 'nnml "Group %s selected" decoded)
261 (nnheader-re-read-dir nnml-current-directory)
262 (nnmail-activate 'nnml)
263 (let ((active (nth 1 (assoc group nnml-group-alist))))
265 (nnheader-report 'nnml "No such group: %s" decoded)
266 (nnheader-report 'nnml "Selected group %s" decoded)
267 (nnheader-insert "211 %d %d %d %s\n"
268 (max (1+ (- (cdr active) (car active))) 0)
269 (car active) (cdr active) group)))))))
271 (deffoo nnml-request-scan (&optional group server)
272 (setq nnml-article-file-alist nil)
273 (nnml-possibly-change-directory group server)
274 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
276 (deffoo nnml-close-group (group &optional server)
277 (setq nnml-article-file-alist nil)
280 (deffoo nnml-request-create-group (group &optional server args)
281 (nnml-possibly-change-directory nil server)
282 (nnmail-activate 'nnml)
284 ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
285 (file-name-coding-system nnmail-pathname-coding-system))
286 (and (file-exists-p file)
287 (not (file-directory-p file))))
288 (nnheader-report 'nnml "%s is a file"
289 (directory-file-name (nnml-group-pathname group
291 ((assoc group nnml-group-alist)
295 (push (list group (setq active (cons 1 0)))
297 (nnml-possibly-create-directory group server)
298 (nnml-possibly-change-directory group server)
299 (let* ((file-name-coding-system nnmail-pathname-coding-system)
300 (articles (nnml-directory-articles nnml-current-directory)))
302 (setcar active (apply 'min articles))
303 (setcdr active (apply 'max articles))))
304 (nnmail-save-active nnml-group-alist nnml-active-file)
307 (deffoo nnml-request-list (&optional server)
309 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
310 (file-name-coding-system nnmail-pathname-coding-system))
311 (nnmail-find-file nnml-active-file))
312 (setq nnml-group-alist (nnmail-get-active))
315 (deffoo nnml-request-newgroups (date &optional server)
316 (nnml-request-list server))
318 (deffoo nnml-request-list-newsgroups (&optional server)
320 (nnmail-find-file nnml-newsgroups-file)))
322 (deffoo nnml-request-expire-articles (articles group &optional server force)
323 (nnml-possibly-change-directory group server)
324 (let* ((file-name-coding-system nnmail-pathname-coding-system)
326 (nnml-directory-articles nnml-current-directory))