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"))
32 (defgroup mail-source nil
33 "The mail-fetching library."
36 (defcustom mail-source-movemail-program "movemail"
37 "*A command to be executed to move mail from the inbox.
38 The default is \"movemail\".
40 This can also be a function. In that case, the function will be
41 called with two parameters -- the name of the INBOX file, and the file
47 (defcustom mail-source-movemail-args nil
48 "*Extra arguments to give to `mail-source-movemail-program' to move mail from the inbox.
54 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
55 "File where mail will be stored while processing it."
59 (defcustom mail-source-directory "~/Mail/"
60 "Directory where files (if any) will be stored."
64 (defcustom mail-source-default-file-modes 384
65 "Set the mode bits of all new mail files to this integer."
69 (defcustom mail-source-delete-incoming nil
70 "*If non-nil, delete incoming files after handling."
74 ;;; Internal variables.
77 (defvar mail-source-keyword-map
79 (:path (or (getenv "MAIL")
80 (concat "/usr/spool/mail/" (user-login-name)))))
86 (:server (getenv "MAILHOST"))
88 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
92 "Mapping from keywords to default values.
93 All keywords that can be used must be listed here."))
95 (defvar mail-source-fetcher-alist
96 '((file mail-source-fetch-file)
97 (directory mail-source-fetch-directory)
98 (pop mail-source-fetch-pop)
99 (qmail mail-source-fetch-qmail))
100 "A mapping from source type to fetcher function.")
102 (defvar mail-source-password-cache nil)
107 (defun mail-source-strip-keyword (keyword)
108 "Strip the leading colon off the KEYWORD."
109 (intern (substring (symbol-name keyword) 1))))
112 (defun mail-source-bind-1 (type)
113 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
115 (while (setq default (pop defaults))
116 (push (list (mail-source-strip-keyword (car default))
121 (defmacro mail-source-bind (type source &rest body)
122 "Bind all variables in SOURCE."
123 `(let ,(mail-source-bind-1 type)
124 (mail-source-set-1 source)
127 (put 'mail-source-bind 'lisp-indent-function 2)
128 (put 'mail-source-bind 'edebug-form-spec '(form form body))
130 (defun mail-source-set-1 (source)
131 (let* ((type (pop source))
132 (defaults (cdr (assq type mail-source-keyword-map)))
133 default value keyword)
134 (while (setq default (pop defaults))
135 (set (mail-source-strip-keyword (setq keyword (car default)))
136 (if (setq value (plist-get source keyword))
137 (mail-source-value value)
138 (mail-source-value (cadr default)))))))
140 (defun mail-source-value (value)
141 "Return the value of VALUE."
148 (functionp (car value)))
151 ((and (symbolp value)
153 (symbol-value value))
154 ;; Just return the value.
158 (defun mail-source-fetch (source callback)
159 "Fetch mail from SOURCE and call CALLBACK zero or more times.
160 CALLBACK will be called with the name of the file where (some of)
161 the mail from SOURCE is put.
162 Return the number of files that were found."
163 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
166 (error "%S is an invalid mail source specification" source))
167 ;; If there's anything in the crash box, we do it first.
168 (when (file-exists-p mail-source-crash-box)
169 (message "Processing mail from %s..." mail-source-crash-box)
170 (setq found (mail-source-callback
171 callback mail-source-crash-box)))
172 (+ found (funcall function source callback))))
174 (defun mail-source-make-complex-temp-name (prefix)
175 (let ((newname (make-temp-name prefix))
177 (while (file-exists-p newname)
178 (setq newprefix (concat newprefix "x"))
179 (setq newname (make-temp-name newprefix)))
182 (defun mail-source-callback (callback info)
183 "Call CALLBACK on the mail file, and then remove the mail file.
184 Pass INFO on to CALLBACK."
185 (if (or (not (file-exists-p mail-source-crash-box))
186 (zerop (nth 7 (file-attributes mail-source-crash-box))))
188 (delete-file mail-source-crash-box)
190 (funcall callback mail-source-crash-box info)
191 (if mail-source-delete-incoming
192 (delete-file mail-source-crash-box)
194 (mail-source-make-complex-temp-name
196 "Incoming" mail-source-directory))))
197 (unless (file-exists-p (file-name-directory incoming))
198 (make-directory (file-name-directory incoming) t))
199 (rename-file mail-source-crash-box incoming t)))
202 (defun mail-source-movemail (from to)
203 "Move FROM to TO using movemail."
204 (if (not (file-writable-p to))
205 (error "Can't write to crash box %s. Not moving mail" to)
206 (let ((to (file-truename (expand-file-name to)))
208 (setq to (file-truename to)
209 from (file-truename from))
210 ;; Set TO if have not already done so, and rename or copy
211 ;; the file FROM to TO if and as appropriate.
214 ;; The crash box exists already.
216 ((not (file-exists-p from))
217 ;; There is no inbox.
220 ;; If getting from mail spool directory, use movemail to move
221 ;; rather than just renaming, so as to interlock with the
225 (setq errors (generate-new-buffer " *mail source loss*"))
226 (buffer-disable-undo errors)
227 (if (functionp mail-source-movemail-program)
230 (funcall mail-source-movemail-program from to)
235 (insert (prin1-to-string err))
237 (let ((default-directory "/"))
244 mail-source-movemail-program exec-directory)
245 nil errors nil from to)
246 (when mail-source-movemail-args
247 mail-source-movemail-args))))))
248 (when (file-exists-p to)
249 (set-file-modes to mail-source-default-file-modes))
250 (if (and (not (buffer-modified-p errors))
252 ;; No output => movemail won.
255 ;; There may be a warning about older revisions. We
257 (goto-char (point-min))
258 (if (search-forward "older revision" nil t)
260 ;; Probably a real error.
261 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
262 (goto-char (point-max))
263 (skip-chars-backward " \t")
264 (delete-region (point) (point-max))
265 (goto-char (point-min))
266 (when (looking-at "movemail: ")
267 (delete-region (point-min) (match-end 0)))
269 (format "movemail: %s (%d return). Continue? "
270 (buffer-string) result))
271 (error "%s" (buffer-string)))
273 (when (buffer-name errors)
274 (kill-buffer errors))
275 ;; Return whether we moved successfully or not.
278 (defvar mail-source-read-passwd nil)
279 (defun mail-source-read-passwd (prompt &rest args)
280 "Read a password using PROMPT.
281 If ARGS, PROMPT is used as an argument to `format'."
284 (apply 'format prompt args)
286 (unless mail-source-read-passwd
287 (if (load "passwd" t)
288 (setq mail-source-read-passwd 'read-passwd)
289 (unless (fboundp 'ange-ftp-read-passwd)
290 (autoload 'ange-ftp-read-passwd "ange-ftp"))
291 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
292 (funcall mail-source-read-passwd prompt)))
294 (defun mail-source-fetch-file (source callback)
295 "Fetcher for single-file sources."
296 (mail-source-bind file source
297 (if (mail-source-movemail path mail-source-crash-box)
298 (mail-source-callback callback path)
301 (defun mail-source-fetch-directory (source callback)
302 "Fetcher for directory sources."
303 (mail-source-bind directory source
304 (let ((files (directory-files
306 (or match (concat (regexp-quote suffix) "$"))))
309 (while (setq file (pop files))
310 (when (mail-source-movemail file mail-source-crash-box)
311 (incf found (mail-source-callback callback file))))
314 (defun mail-source-fetch-pop (source callback)
315 "Fetcher for single-file sources."
316 (mail-source-bind pop source
317 (let ((from (format "%s:%s:%s" server user port)))
320 (cdr (assoc from mail-source-password-cache))
321 (mail-source-read-passwd
322 (format "Password for %s at %s: " user server))))
323 (unless (assoc from mail-source-password-cache)
324 (push (cons from password) mail-source-password-cache))
325 (let ((pop3-password password)
327 (pop3-mailhost server))
328 (if (pop3-movemail mail-source-crash-box)
329 (mail-source-callback callback server)
330 ;; We nix out the password in case the error
331 ;; was because of a wrong password being given.
332 (setq mail-source-password-cache
333 (delq (assoc from mail-source-password-cache)
334 mail-source-password-cache))
337 (provide 'mail-source)
339 ;;; mail-source.el ends here