1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
30 (autoload 'pop3-movemail "pop3"))
31 (require 'format-spec)
33 (defgroup mail-source nil
34 "The mail-fetching library."
37 (defcustom mail-sources nil
38 "*Where the mail backends will look for incoming mail.
39 This variable is a list of mail source specifiers."
43 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
44 "File where mail will be stored while processing it."
48 (defcustom mail-source-directory "~/Mail/"
49 "Directory where files (if any) will be stored."
53 (defcustom mail-source-default-file-modes 384
54 "Set the mode bits of all new mail files to this integer."
58 (defcustom mail-source-delete-incoming nil
59 "*If non-nil, delete incoming files after handling."
63 ;;; Internal variables.
65 (defvar mail-source-string ""
66 "A dynamically bound string that says what the current mail source is.")
69 (defvar mail-source-keyword-map
73 (:path (or (getenv "MAIL")
74 (concat "/usr/spool/mail/" (user-login-name)))))
78 (:predicate identity))
82 (:server (getenv "MAILHOST"))
84 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
88 (:authentication password))
90 (:path "~/Maildir/new/")))
91 "Mapping from keywords to default values.
92 All keywords that can be used must be listed here."))
94 (defvar mail-source-fetcher-alist
95 '((file mail-source-fetch-file)
96 (directory mail-source-fetch-directory)
97 (pop mail-source-fetch-pop)
98 (maildir mail-source-fetch-maildir))
99 "A mapping from source type to fetcher function.")
101 (defvar mail-source-password-cache nil)
106 (defun mail-source-strip-keyword (keyword)
107 "Strip the leading colon off the KEYWORD."
108 (intern (substring (symbol-name keyword) 1))))
111 (defun mail-source-bind-1 (type)
112 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
114 (while (setq default (pop defaults))
115 (push (list (mail-source-strip-keyword (car default))
120 (defmacro mail-source-bind (type-source &rest body)
121 "Return a `let' form that binds all variables in source TYPE.
122 TYPE-SOURCE is a list where the first element is the TYPE, and
123 the second variable is the SOURCE.
124 At run time, the mail source specifier SOURCE will be inspected,
125 and the variables will be set according to it. Variables not
126 specified will be given default values.
128 After this is done, BODY will be executed in the scope
131 The variables bound and their default values are described by
132 the `mail-source-keyword-map' variable."
133 `(let ,(mail-source-bind-1 (car type-source))
134 (mail-source-set-1 ,(cadr type-source))
137 (put 'mail-source-bind 'lisp-indent-function 1)
138 (put 'mail-source-bind 'edebug-form-spec '(form body))
140 (defun mail-source-set-1 (source)
141 (let* ((type (pop source))
142 (defaults (cdr (assq type mail-source-keyword-map)))
143 default value keyword)
144 (while (setq default (pop defaults))
145 (set (mail-source-strip-keyword (setq keyword (car default)))
146 (if (setq value (plist-get source keyword))
147 (mail-source-value value)
148 (mail-source-value (cadr default)))))))
150 (defun mail-source-value (value)
151 "Return the value of VALUE."
158 (functionp (car value)))
160 ;; Just return the value.
164 (defun mail-source-fetch (source callback)
165 "Fetch mail from SOURCE and call CALLBACK zero or more times.
166 CALLBACK will be called with the name of the file where (some of)
167 the mail from SOURCE is put.
168 Return the number of files that were found."
170 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
173 (error "%S is an invalid mail source specification" source))
174 ;; If there's anything in the crash box, we do it first.
175 (when (file-exists-p mail-source-crash-box)
176 (message "Processing mail from %s..." mail-source-crash-box)
177 (setq found (mail-source-callback
178 callback mail-source-crash-box)))
181 (funcall function source callback)
184 (format "Mail source error (%s). Continue? " err))
185 (error "Cannot get new mail."))
188 (defun mail-source-make-complex-temp-name (prefix)
189 (let ((newname (make-temp-name prefix))
191 (while (file-exists-p newname)
192 (setq newprefix (concat newprefix "x"))
193 (setq newname (make-temp-name newprefix)))
196 (defun mail-source-callback (callback info)
197 "Call CALLBACK on the mail file, and then remove the mail file.
198 Pass INFO on to CALLBACK."
199 (if (or (not (file-exists-p mail-source-crash-box))
200 (zerop (nth 7 (file-attributes mail-source-crash-box))))
202 (when (file-exists-p mail-source-crash-box)
203 (delete-file mail-source-crash-box))
206 (funcall callback mail-source-crash-box info)
207 (when (file-exists-p mail-source-crash-box)
208 ;; Delete or move the incoming mail out of the way.
209 (if mail-source-delete-incoming
210 (delete-file mail-source-crash-box)
212 (mail-source-make-complex-temp-name
214 "Incoming" mail-source-directory))))
215 (unless (file-exists-p (file-name-directory incoming))
216 (make-directory (file-name-directory incoming) t))
217 (rename-file mail-source-crash-box incoming t)))))))
219 (defun mail-source-movemail (from to)
220 "Move FROM to TO using movemail."
221 (if (not (file-writable-p to))
222 (error "Can't write to crash box %s. Not moving mail" to)
223 (let ((to (file-truename (expand-file-name to)))
225 (setq to (file-truename to)
226 from (file-truename from))
227 ;; Set TO if have not already done so, and rename or copy
228 ;; the file FROM to TO if and as appropriate.
231 ;; The crash box exists already.
233 ((not (file-exists-p from))
234 ;; There is no inbox.
236 ((zerop (nth 7 (file-attributes from)))
240 ;; If getting from mail spool directory, use movemail to move
241 ;; rather than just renaming, so as to interlock with the
245 (setq errors (generate-new-buffer " *mail source loss*"))
246 (let ((default-directory "/"))
252 (expand-file-name "movemail" exec-directory)
253 nil errors nil from to)))))
254 (when (file-exists-p to)
255 (set-file-modes to mail-source-default-file-modes))
256 (if (and (not (buffer-modified-p errors))
258 ;; No output => movemail won.
261 ;; There may be a warning about older revisions. We
263 (goto-char (point-min))
264 (if (search-forward "older revision" nil t)
266 ;; Probably a real error.
267 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
268 (goto-char (point-max))
269 (skip-chars-backward " \t")
270 (delete-region (point) (point-max))
271 (goto-char (point-min))
272 (when (looking-at "movemail: ")
273 (delete-region (point-min) (match-end 0)))
275 (format "movemail: %s (%d return). Continue? "
276 (buffer-string) result))
277 (error "%s" (buffer-string)))
280 (buffer-name errors))
281 (kill-buffer errors))
282 ;; Return whether we moved successfully or not.
285 (defvar mail-source-read-passwd nil)
286 (defun mail-source-read-passwd (prompt &rest args)
287 "Read a password using PROMPT.
288 If ARGS, PROMPT is used as an argument to `format'."
291 (apply 'format prompt args)
293 (unless mail-source-read-passwd
294 (if (or (fboundp 'read-passwd) (load "passwd" t))
295 (setq mail-source-read-passwd 'read-passwd)
296 (unless (fboundp 'ange-ftp-read-passwd)
297 (autoload 'ange-ftp-read-passwd "ange-ftp"))
298 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
299 (funcall mail-source-read-passwd prompt)))
301 (defun mail-source-fetch-with-program (program)
302 (zerop (call-process shell-file-name nil nil nil
303 shell-command-switch program)))
305 (defun mail-source-call-script (script)
306 (let ((background nil))
307 (when (string-match "& *$" script)
308 (setq script (substring script 0 (match-beginning 0))
310 (call-process shell-file-name nil background nil
311 shell-command-switch script)))
314 ;;; Different fetchers
317 (defun mail-source-fetch-file (source callback)
318 "Fetcher for single-file sources."
319 (mail-source-bind (file source)
321 (if (and (symbolp prescript) (fboundp prescript))
323 (mail-source-call-script
325 prescript (format-spec-make ?t mail-source-crash-box)))))
326 (let ((mail-source-string (format "file:%s" path)))
327 (if (mail-source-movemail path mail-source-crash-box)
329 (mail-source-callback callback path)
331 (if (and (symbolp prescript) (fboundp prescript))
333 (mail-source-call-script
335 postscript (format-spec-make ?t mail-source-crash-box))))))
338 (defun mail-source-fetch-directory (source callback)
339 "Fetcher for directory sources."
340 (mail-source-bind (directory source)
342 (mail-source-string (format "directory:%s" path)))
343 (dolist (file (directory-files
344 path t (concat (regexp-quote suffix) "$")))
345 (when (and (file-regular-p file)
346 (funcall predicate file)
347 (mail-source-movemail file mail-source-crash-box))
348 (incf found (mail-source-callback callback file))))
351 (defun mail-source-fetch-pop (source callback)
352 "Fetcher for single-file sources."
353 (mail-source-bind (pop source)
355 (if (and (symbolp prescript)
358 (mail-source-call-script
360 prescript (format-spec-make ?p password ?t mail-source-crash-box
361 ?s server ?P port ?u user)))))
362 (let ((from (format "%s:%s:%s" server user port))
363 (mail-source-string (format "pop:%s@%s" user server))
365 (when (eq authentication 'password)
368 (cdr (assoc from mail-source-password-cache))
369 (mail-source-read-passwd
370 (format "Password for %s at %s: " user server))))
371 (unless (assoc from mail-source-password-cache)
372 (push (cons from password) mail-source-password-cache)))
374 (setenv "MAILHOST" server))
378 (mail-source-fetch-with-program
381 (format-spec-make ?p password ?t mail-source-crash-box
382 ?s server ?P port ?u user))))
384 (funcall function mail-source-crash-box))
385 ;; The default is to use pop3.el.
387 (let ((pop3-password password)
389 (pop3-mailhost server)
391 (pop3-authentication-scheme
392 (if (eq authentication 'apop) 'apop 'pass)))
393 (save-excursion (pop3-movemail mail-source-crash-box))))))
396 (mail-source-callback callback server)
398 (if (and (symbolp postscript)
399 (fboundp postscript))
401 (mail-source-call-script
403 postscript (format-spec-make
404 ?p password ?t mail-source-crash-box
405 ?s server ?P port ?u user))))))
406 ;; We nix out the password in case the error
407 ;; was because of a wrong password being given.
408 (setq mail-source-password-cache
409 (delq (assoc from mail-source-password-cache)
410 mail-source-password-cache))
413 (defun mail-source-fetch-maildir (source callback)
414 "Fetcher for maildir sources."
415 (mail-source-bind (maildir source)
417 (mail-source-string (format "maildir:%s" path)))
418 (dolist (file (directory-files path t))
419 (when (and (file-regular-p file)
420 (not (rename-file file mail-source-crash-box)))
421 (incf found (mail-source-callback callback file))))
424 (provide 'mail-source)
426 ;;; mail-source.el ends here