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.
76 (defvar mail-source-string ""
77 "A dynamically bound string that says what the current mail source is.")
80 (defvar mail-source-keyword-map
82 (:path (or (getenv "MAIL")
83 (concat "/usr/spool/mail/" (user-login-name)))))
88 (:server (getenv "MAILHOST"))
90 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
94 "Mapping from keywords to default values.
95 All keywords that can be used must be listed here."))
97 (defvar mail-source-fetcher-alist
98 '((file mail-source-fetch-file)
99 (directory mail-source-fetch-directory)
100 (pop mail-source-fetch-pop)
101 (qmail mail-source-fetch-qmail))
102 "A mapping from source type to fetcher function.")
104 (defvar mail-source-password-cache nil)
109 (defun mail-source-strip-keyword (keyword)
110 "Strip the leading colon off the KEYWORD."
111 (intern (substring (symbol-name keyword) 1))))
114 (defun mail-source-bind-1 (type)
115 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
117 (while (setq default (pop defaults))
118 (push (list (mail-source-strip-keyword (car default))
123 (defmacro mail-source-bind (type-source &rest body)
124 "Return a `let' form that binds all variables in source TYPE.
125 At run time, the mail source specifier SOURCE will be inspected,
126 and the variables will be set according to it. Variables not
127 specified will be given default values.
129 After this is done, BODY will be executed in the scope
131 `(let ,(mail-source-bind-1 (car type-source))
132 (mail-source-set-1 ,(cadr type-source))
135 (put 'mail-source-bind 'lisp-indent-function 1)
136 (put 'mail-source-bind 'edebug-form-spec '(form body))
138 (defun mail-source-set-1 (source)
139 (let* ((type (pop source))
140 (defaults (cdr (assq type mail-source-keyword-map)))
141 default value keyword)
142 (while (setq default (pop defaults))
143 (set (mail-source-strip-keyword (setq keyword (car default)))
144 (if (setq value (plist-get source keyword))
145 (mail-source-value value)
146 (mail-source-value (cadr default)))))))
148 (defun mail-source-value (value)
149 "Return the value of VALUE."
156 (functionp (car value)))
159 ((and (symbolp value)
161 (symbol-value value))
162 ;; Just return the value.
166 (defun mail-source-fetch (source callback)
167 "Fetch mail from SOURCE and call CALLBACK zero or more times.
168 CALLBACK will be called with the name of the file where (some of)
169 the mail from SOURCE is put.
170 Return the number of files that were found."
171 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
174 (error "%S is an invalid mail source specification" source))
175 ;; If there's anything in the crash box, we do it first.
176 (when (file-exists-p mail-source-crash-box)
177 (message "Processing mail from %s..." mail-source-crash-box)
178 (setq found (mail-source-callback
179 callback mail-source-crash-box)))
180 (+ found (funcall function source callback))))
182 (defun mail-source-make-complex-temp-name (prefix)
183 (let ((newname (make-temp-name prefix))
185 (while (file-exists-p newname)
186 (setq newprefix (concat newprefix "x"))
187 (setq newname (make-temp-name newprefix)))
190 (defun mail-source-callback (callback info)
191 "Call CALLBACK on the mail file, and then remove the mail file.
192 Pass INFO on to CALLBACK."
193 (if (or (not (file-exists-p mail-source-crash-box))
194 (zerop (nth 7 (file-attributes mail-source-crash-box))))
196 (when (file-exists-p mail-source-crash-box)
197 (delete-file mail-source-crash-box))
199 (funcall callback mail-source-crash-box info)
200 (if mail-source-delete-incoming
201 (when (file-exists-p mail-source-crash-box)
202 (delete-file mail-source-crash-box))
204 (mail-source-make-complex-temp-name
206 "Incoming" mail-source-directory))))
207 (unless (file-exists-p (file-name-directory incoming))
208 (make-directory (file-name-directory incoming) t))
209 (rename-file mail-source-crash-box incoming t)))
212 (defun mail-source-movemail (from to)
213 "Move FROM to TO using movemail."
214 (if (not (file-writable-p to))
215 (error "Can't write to crash box %s. Not moving mail" to)
216 (let ((to (file-truename (expand-file-name to)))
218 (setq to (file-truename to)
219 from (file-truename from))
220 ;; Set TO if have not already done so, and rename or copy
221 ;; the file FROM to TO if and as appropriate.
224 ;; The crash box exists already.
226 ((not (file-exists-p from))
227 ;; There is no inbox.
229 ((zerop (nth 7 (file-attributes from)))
233 ;; If getting from mail spool directory, use movemail to move
234 ;; rather than just renaming, so as to interlock with the
238 (setq errors (generate-new-buffer " *mail source loss*"))
239 (buffer-disable-undo errors)
240 (if (functionp mail-source-movemail-program)
243 (funcall mail-source-movemail-program from to)
248 (insert (prin1-to-string err))
250 (let ((default-directory "/"))
257 mail-source-movemail-program exec-directory)
258 nil errors nil from to)
259 (when mail-source-movemail-args
260 mail-source-movemail-args))))))
261 (when (file-exists-p to)
262 (set-file-modes to mail-source-default-file-modes))
263 (if (and (not (buffer-modified-p errors))
265 ;; No output => movemail won.
268 ;; There may be a warning about older revisions. We
270 (goto-char (point-min))
271 (if (search-forward "older revision" nil t)
273 ;; Probably a real error.
274 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
275 (goto-char (point-max))
276 (skip-chars-backward " \t")
277 (delete-region (point) (point-max))
278 (goto-char (point-min))
279 (when (looking-at "movemail: ")
280 (delete-region (point-min) (match-end 0)))
282 (format "movemail: %s (%d return). Continue? "
283 (buffer-string) result))
284 (error "%s" (buffer-string)))
287 (buffer-name errors))
288 (kill-buffer errors))
289 ;; Return whether we moved successfully or not.
292 (defvar mail-source-read-passwd nil)
293 (defun mail-source-read-passwd (prompt &rest args)
294 "Read a password using PROMPT.
295 If ARGS, PROMPT is used as an argument to `format'."
298 (apply 'format prompt args)
300 (unless mail-source-read-passwd
301 (if (load "passwd" t)
302 (setq mail-source-read-passwd 'read-passwd)
303 (unless (fboundp 'ange-ftp-read-passwd)
304 (autoload 'ange-ftp-read-passwd "ange-ftp"))
305 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
306 (funcall mail-source-read-passwd prompt)))
308 (defun mail-source-fetch-file (source callback)
309 "Fetcher for single-file sources."
310 (mail-source-bind (file source)
311 (let ((mail-source-string (format "file:%s" path)))
312 (if (mail-source-movemail path mail-source-crash-box)
313 (mail-source-callback callback path)
316 (defun mail-source-fetch-directory (source callback)
317 "Fetcher for directory sources."
318 (mail-source-bind (directory source)
319 (let ((files (directory-files
321 (concat (regexp-quote suffix) "$")))
323 (mail-source-string (format "directory:%s" path))
325 (while (setq file (pop files))
326 (when (mail-source-movemail file mail-source-crash-box)
327 (incf found (mail-source-callback callback file))))
330 (defun mail-source-fetch-pop (source callback)
331 "Fetcher for single-file sources."
332 (mail-source-bind (pop source)
333 (let ((from (format "%s:%s:%s" server user port))
334 (mail-source-string (format "pop:%s@%s" user server)))
337 (cdr (assoc from mail-source-password-cache))
338 (mail-source-read-passwd
339 (format "Password for %s at %s: " user server))))
340 (unless (assoc from mail-source-password-cache)
341 (push (cons from password) mail-source-password-cache))
342 (let ((pop3-password password)
344 (pop3-mailhost server))
345 (if (pop3-movemail mail-source-crash-box)
346 (mail-source-callback callback server)
347 ;; We nix out the password in case the error
348 ;; was because of a wrong password being given.
349 (setq mail-source-password-cache
350 (delq (assoc from mail-source-password-cache)
351 mail-source-password-cache))
354 (provide 'mail-source)
356 ;;; mail-source.el ends here