1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995-2011 Free Software
6 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7 ;; Simon Josefsson <simon@josefsson.org> (adding MARKS)
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: news, mail
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 3 of the License, or
17 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
39 (eval-when-compile (require 'cl))
41 ;; FIXME first is unused in this file.
42 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
43 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
47 (defvoo nnml-directory message-directory
48 "Spool directory for the nnml mail backend.")
50 (defvoo nnml-active-file
51 (expand-file-name "active" nnml-directory)
54 (defvoo nnml-newsgroups-file
55 (expand-file-name "newsgroups" nnml-directory)
56 "Mail newsgroups description file.")
58 (defvoo nnml-get-new-mail t
59 "If non-nil, nnml will check the incoming mail file and split the mail.")
61 (defvoo nnml-nov-is-evil nil
62 "If non-nil, Gnus will never generate and use nov databases for mail spools.
63 Using nov databases will speed up header fetching considerably.
64 This variable shouldn't be flipped much. If you have, for some reason,
65 set this to t, and want to set it to nil again, you should always run
66 the `nnml-generate-nov-databases' command. The function will go
67 through all nnml directories and generate nov databases for them
68 all. This may very well take some time.")
70 (defvoo nnml-marks-is-evil nil
71 "If non-nil, Gnus will never generate and use marks file for mail spools.
72 Using marks files makes it possible to backup and restore mail groups
73 separately from `.newsrc.eld'. If you have, for some reason, set this
74 to t, and want to set it to nil again, you should always remove the
75 corresponding marks file (usually named `.marks' in the nnml group
76 directory, but see `nnml-marks-file-name') for the group. Then the
77 marks file will be regenerated properly by Gnus.")
79 (defvoo nnml-prepare-save-mail-hook nil
80 "Hook run narrowed to an article before saving.")
82 (defvoo nnml-inhibit-expiry nil
83 "If non-nil, inhibit expiry.")
85 (defvoo nnml-use-compressed-files nil
86 "If non-nil, allow using compressed message files.
88 If it is a string, use it as the file extension which specifies
89 the compression program. You can set it to \".bz2\" if your Emacs
90 supports auto-compression using the bzip2 program. A value of t
91 is equivalent to \".gz\".")
93 (defvoo nnml-compressed-files-size-threshold 1000
94 "Default size threshold for compressed message files.
95 Message files with bodies larger than that many characters will
96 be automatically compressed if `nnml-use-compressed-files' is
101 (defconst nnml-version "nnml 1.0"
104 (defvoo nnml-nov-file-name ".overview")
105 (defvoo nnml-marks-file-name ".marks")
107 (defvoo nnml-current-directory nil)
108 (defvoo nnml-current-group nil)
109 (defvoo nnml-status-string "")
110 (defvoo nnml-nov-buffer-alist nil)
111 (defvoo nnml-group-alist nil)
112 (defvoo nnml-active-timestamp nil)
113 (defvoo nnml-article-file-alist nil)
115 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
117 (defvar nnml-nov-buffer-file-name nil)
119 (defvoo nnml-file-coding-system nnmail-file-coding-system)
121 (defvoo nnml-marks nil)
123 (defvar nnml-marks-modtime (gnus-make-hashtable))
126 ;;; Interface functions.
128 (nnoo-define-basics nnml)
131 (defsubst nnml-group-name-charset (group server-or-method)
132 (gnus-group-name-charset
133 (if (stringp server-or-method)
134 (gnus-server-to-method
135 (if (string-match "\\+" server-or-method)
136 (concat (substring server-or-method 0 (match-beginning 0))
137 ":" (substring server-or-method (match-end 0)))
138 (concat "nnml:" server-or-method)))
139 (or server-or-method gnus-command-method '(nnml "")))
142 (defun nnml-decoded-group-name (group &optional server-or-method)
143 "Return a decoded group name of GROUP on SERVER-OR-METHOD."
144 (if nnmail-group-names-not-encoded-p
146 (mm-decode-coding-string
148 (nnml-group-name-charset group server-or-method))))
150 (defun nnml-encoded-group-name (group &optional server-or-method)
151 "Return an encoded group name of GROUP on SERVER-OR-METHOD."
152 (mm-encode-coding-string
154 (nnml-group-name-charset group server-or-method)))
156 (defun nnml-group-pathname (group &optional file server)
157 "Return an absolute file name of FILE for GROUP on SERVER."
158 (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
159 nnml-directory file))
161 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
162 (when (nnml-possibly-change-directory group server)
163 (with-current-buffer nntp-server-buffer
166 (number (length sequence))
168 (file-name-coding-system nnmail-pathname-coding-system)
170 (if (stringp (car sequence))
172 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
175 (setq article (car sequence))
176 (setq file (nnml-article-to-file article))
179 (not (file-directory-p file)))
180 (insert (format "221 %d Article retrieved.\n" article))
182 (nnheader-insert-head file)
184 (if (re-search-forward "\n\r?\n" nil t)
186 (goto-char (point-max))
189 (delete-region (point) (point-max)))
190 (setq sequence (cdr sequence))
191 (setq count (1+ count))
192 (and (numberp nnmail-large-newsgroup)
193 (> number nnmail-large-newsgroup)
195 (nnheader-message 6 "nnml: Receiving headers... %d%%"
196 (/ (* count 100) number))))
198 (and (numberp nnmail-large-newsgroup)
199 (> number nnmail-large-newsgroup)
200 (nnheader-message 6 "nnml: Receiving headers...done"))
202 (nnheader-fold-continuation-lines)
205 (deffoo nnml-open-server (server &optional defs)
206 (nnoo-change-server 'nnml server defs)
207 (when (not (file-exists-p nnml-directory))
208 (ignore-errors (make-directory nnml-directory t)))
210 ((not (file-exists-p nnml-directory))
212 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
213 ((not (file-directory-p (file-truename nnml-directory)))
215 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
217 (nnheader-report 'nnml "Opened server %s using directory %s"
218 server nnml-directory)
221 (deffoo nnml-request-regenerate (server)
222 (nnml-possibly-change-directory nil server)
223 (nnml-generate-nov-databases server)
226 (deffoo nnml-request-article (id &optional group server buffer)
227 (nnml-possibly-change-directory group server)
228 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
229 (file-name-coding-system nnmail-pathname-coding-system)
230 path gpath group-num)
232 (when (and (setq group-num (nnml-find-group-number id server))
234 (assq (cdr group-num)
235 (nnheader-article-to-file-alist
236 (setq gpath (nnml-group-pathname (car group-num)
238 (nnml-update-file-alist)
239 (setq path (concat gpath (if nnml-use-compressed-files
240 (cdr (assq (cdr group-num)
241 nnml-article-file-alist))
242 (number-to-string (cdr group-num))))))
243 (setq path (nnml-article-to-file id)))
246 (nnheader-report 'nnml "No such article: %s" id))
247 ((not (file-exists-p path))
248 (nnheader-report 'nnml "No such file: %s" path))
249 ((file-directory-p path)
250 (nnheader-report 'nnml "File is a directory: %s" path))
251 ((not (save-excursion (let ((nnmail-file-coding-system
252 nnml-file-coding-system))
253 (nnmail-find-file path))))
254 (nnheader-report 'nnml "Couldn't read file: %s" path))
256 (nnheader-report 'nnml "Article %s retrieved" id)
257 ;; We return the article number.
258 (cons (if group-num (car group-num) group)
259 (string-to-number (file-name-nondirectory path)))))))
261 (deffoo nnml-request-group (group &optional server dont-check info)
262 (let ((file-name-coding-system nnmail-pathname-coding-system)
263 (decoded (nnml-decoded-group-name group server)))
265 ((not (nnml-possibly-change-directory group server))
266 (nnheader-report 'nnml "Invalid group (no such directory)"))
267 ((not (file-exists-p nnml-current-directory))
268 (nnheader-report 'nnml "Directory %s does not exist"
269 nnml-current-directory))
270 ((not (file-directory-p nnml-current-directory))
271 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
273 (nnheader-report 'nnml "Group %s selected" decoded)
276 (nnheader-re-read-dir nnml-current-directory)
277 (nnmail-activate 'nnml)
278 (let ((active (nth 1 (assoc group nnml-group-alist))))
280 (nnheader-report 'nnml "No such group: %s" decoded)
281 (nnheader-report 'nnml "Selected group %s" decoded)
282 (nnheader-insert "211 %d %d %d %s\n"
283 (max (1+ (- (cdr active) (car active))) 0)
284 (car active) (cdr active) group)))))))
286 (deffoo nnml-request-scan (&optional group server)
287 (setq nnml-article-file-alist nil)
288 (nnml-possibly-change-directory group server)
289 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
291 (deffoo nnml-close-group (group &optional server)
292 (setq nnml-article-file-alist nil)
295 (deffoo nnml-request-create-group (group &optional server args)
296 (nnml-possibly-change-directory nil server)
297 (nnmail-activate 'nnml)
299 ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
300 (file-name-coding-system nnmail-pathname-coding-system))
301 (and (file-exists-p file)
302 (not (file-directory-p file))))
303 (nnheader-report 'nnml "%s is a file"
304 (directory-file-name (nnml-group-pathname group
306 ((assoc group nnml-group-alist)
310 (push (list group (setq active (cons 1 0)))
312 (nnml-possibly-create-directory group server)
313 (nnml-possibly-change-directory group server)
314 (let* ((file-name-coding-system nnmail-pathname-coding-system)
315 (articles (nnml-directory-articles nnml-current-directory)))
317 (setcar active (apply 'min articles))
318 (setcdr active (apply 'max articles))))
319 (nnmail-save-active nnml-group-alist nnml-active-file)
322 (deffoo nnml-request-list (&optional server)
324 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
325 (file-name-coding-system nnmail-pathname-coding-system))
326 (nnmail-find-file nnml-active-file))
327 (setq nnml-group-alist (nnmail-get-active))
330 (deffoo nnml-request-newgroups (date &optional server)
331 (nnml-request-list server))
333 (deffoo nnml-request-list-newsgroups (&optional server)
335 (nnmail-find-file nnml-newsgroups-file)))
337 (deffoo nnml-request-expire-articles (articles group &optional server force)
338 (nnml-possibly-change-directory group server)
339 (let* ((file-name-coding-system nnmail-pathname-coding-system)
341 (nnml-directory-articles nnml-current-directory))
343 (decoded (nnml-decoded-group-name group server))
344 article rest mod-time number target)
345 (nnmail-activate 'nnml)
347 (setq active-articles (sort active-articles '<))
348 ;; Articles not listed in active-articles are already gone,
349 ;; so don't try to expire them.
350 (setq articles (gnus-sorted-intersection articles active-articles))
352 (while (and articles is-old)
353 (if (and (setq article (nnml-article-to-file
354 (setq number (pop articles))))
355 (setq mod-time (nth 5 (file-attributes article)))
356 (nnml-deletable-article-p group number)
357 (setq is-old (nnmail-expired-article-p group mod-time force
358 nnml-inhibit-expiry)))
360 ;; Allow a special target group.
361 (setq target nnmail-expiry-target)
362 (unless (eq target 'delete)
364 (nnml-request-article number group server (current-buffer))
365 (let (nnml-current-directory
367 nnml-article-file-alist)
368 (when (functionp target)
369 (setq target (funcall target group)))
370 (when (and target (not (eq target 'delete)))
371 (if (or (gnus-request-group target)
372 (gnus-request-create-group target))
373 (nnmail-expiry-target-group target group)
374 (setq target nil)))))
375 ;; Maybe directory is changed during nnmail-expiry-target-group.
376 (nnml-possibly-change-directory group server))
379 (nnheader-message 5 "Deleting article %s in %s"
382 (funcall nnmail-delete-file-function article)
385 (setq active-articles (delq number active-articles))
386 (nnml-nov-delete-article group number))
389 (let ((active (nth 1 (assoc group nnml-group-alist))))
391 (setcar active (or (and active-articles
392 (apply 'min active-articles))
394 (nnmail-save-active nnml-group-alist nnml-active-file))
396 (nconc rest articles)))
398 (deffoo nnml-request-move-article
399 (article group server accept-form &optional last move-is-internal)
400 (let ((buf (get-buffer-create " *nnml move*"))
401 (file-name-coding-system nnmail-pathname-coding-system)
403 (nnml-possibly-change-directory group server)
404 (nnml-update-file-alist)
406 (nnml-deletable-article-p group article)
407 (nnml-request-article article&