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-source-crash-box "~/.emacs-mail-crash-box"
38 "File where mail will be stored while processing it."
42 (defcustom mail-source-directory "~/Mail/"
43 "Directory where files (if any) will be stored."
47 (defcustom mail-source-default-file-modes 384
48 "Set the mode bits of all new mail files to this integer."
52 (defcustom mail-source-delete-incoming nil
53 "*If non-nil, delete incoming files after handling."
57 ;;; Internal variables.
59 (defvar mail-source-string ""
60 "A dynamically bound string that says what the current mail source is.")
63 (defvar mail-source-keyword-map
65 (:path (or (getenv "MAIL")
66 (concat "/usr/spool/mail/" (user-login-name)))))
70 (:predicate identity))
74 (:server (getenv "MAILHOST"))
76 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
80 (:authentication password))
82 (:path "~/Maildir/new/")))
83 "Mapping from keywords to default values.
84 All keywords that can be used must be listed here."))
86 (defvar mail-source-fetcher-alist
87 '((file mail-source-fetch-file)
88 (directory mail-source-fetch-directory)
89 (pop mail-source-fetch-pop)
90 (maildir mail-source-fetch-maildir))
91 "A mapping from source type to fetcher function.")
93 (defvar mail-source-password-cache nil)
98 (defun mail-source-strip-keyword (keyword)
99 "Strip the leading colon off the KEYWORD."
100 (intern (substring (symbol-name keyword) 1))))
103 (defun mail-source-bind-1 (type)
104 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
106 (while (setq default (pop defaults))
107 (push (list (mail-source-strip-keyword (car default))
112 (defmacro mail-source-bind (type-source &rest body)
113 "Return a `let' form that binds all variables in source TYPE.
114 TYPE-SOURCE is a list where the first element is the TYPE, and
115 the second variable is the SOURCE.
116 At run time, the mail source specifier SOURCE will be inspected,
117 and the variables will be set according to it. Variables not
118 specified will be given default values.
120 After this is done, BODY will be executed in the scope
123 The variables bound and their default values are described by
124 the `mail-source-keyword-map' variable."
125 `(let ,(mail-source-bind-1 (car type-source))
126 (mail-source-set-1 ,(cadr type-source))
129 (put 'mail-source-bind 'lisp-indent-function 1)
130 (put 'mail-source-bind 'edebug-form-spec '(form body))
132 (defun mail-source-set-1 (source)
133 (let* ((type (pop source))
134 (defaults (cdr (assq type mail-source-keyword-map)))
135 default value keyword)
136 (while (setq default (pop defaults))
137 (set (mail-source-strip-keyword (setq keyword (car default)))
138 (if (setq value (plist-get source keyword))
139 (mail-source-value value)
140 (mail-source-value (cadr default)))))))
142 (defun mail-source-value (value)
143 "Return the value of VALUE."
150 (functionp (car value)))
152 ;; Just return the value.
156 (defun mail-source-fetch (source callback)
157 "Fetch mail from SOURCE and call CALLBACK zero or more times.
158 CALLBACK will be called with the name of the file where (some of)
159 the mail from SOURCE is put.
160 Return the number of files that were found."
162 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
165 (error "%S is an invalid mail source specification" source))
166 ;; If there's anything in the crash box, we do it first.
167 (when (file-exists-p mail-source-crash-box)
168 (message "Processing mail from %s..." mail-source-crash-box)
169 (setq found (mail-source-callback
170 callback mail-source-crash-box)))
171 (+ found (funcall function source callback)))))
173 (defun mail-source-make-complex-temp-name (prefix)
174 (let ((newname (make-temp-name prefix))
176 (while (file-exists-p newname)
177 (setq newprefix (concat newprefix "x"))
178 (setq newname (make-temp-name newprefix)))
181 (defun mail-source-callback (callback info)
182 "Call CALLBACK on the mail file, and then remove the mail file.
183 Pass INFO on to CALLBACK."
184 (if (or (not (file-exists-p mail-source-crash-box))
185 (zerop (nth 7 (file-attributes mail-source-crash-box))))
187 (when (file-exists-p mail-source-crash-box)
188 (delete-file mail-source-crash-box))
190 (funcall callback mail-source-crash-box info)
191 (when (file-exists-p mail-source-crash-box)
192 ;; Delete or move the incoming mail out of the way.
193 (if mail-source-delete-incoming
194 (delete-file mail-source-crash-box)
196 (mail-source-make-complex-temp-name
198 "Incoming" mail-source-directory))))
199 (unless (file-exists-p (file-name-directory incoming))
200 (make-directory (file-name-directory incoming) t))
201 (rename-file mail-source-crash-box incoming t))))
204 (defun mail-source-movemail (from to)
205 "Move FROM to TO using movemail."
206 (if (not (file-writable-p to))
207 (error "Can't write to crash box %s. Not moving mail" to)
208 (let ((to (file-truename (expand-file-name to)))
210 (setq to (file-truename to)
211 from (file-truename from))
212 ;; Set TO if have not already done so, and rename or copy
213 ;; the file FROM to TO if and as appropriate.
216 ;; The crash box exists already.
218 ((not (file-exists-p from))
219 ;; There is no inbox.
221 ((zerop (nth 7 (file-attributes from)))
225 ;; If getting from mail spool directory, use movemail to move
226 ;; rather than just renaming, so as to interlock with the
230 (setq errors (generate-new-buffer " *mail source loss*"))
231 (let ((default-directory "/"))
237 (expand-file-name "movemail" exec-directory)
238 nil errors nil from to)))))
239 (when (file-exists-p to)
240 (set-file-modes to mail-source-default-file-modes))
241 (if (and (not (buffer-modified-p errors))
243 ;; No output => movemail won.
246 ;; There may be a warning about older revisions. We
248 (goto-char (point-min))
249 (if (search-forward "older revision" nil t)
251 ;; Probably a real error.
252 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
253 (goto-char (point-max))
254 (skip-chars-backward " \t")
255 (delete-region (point) (point-max))
256 (goto-char (point-min))
257 (when (looking-at "movemail: ")
258 (delete-region (point-min) (match-end 0)))
260 (format "movemail: %s (%d return). Continue? "
261 (buffer-string) result))
262 (error "%s" (buffer-string)))
265 (buffer-name errors))
266 (kill-buffer errors))
267 ;; Return whether we moved successfully or not.
270 (defvar mail-source-read-passwd nil)
271 (defun mail-source-read-passwd (prompt &rest args)
272 "Read a password using PROMPT.
273 If ARGS, PROMPT is used as an argument to `format'."
276 (apply 'format prompt args)
278 (unless mail-source-read-passwd
279 (if (or (fboundp 'read-passwd) (load "passwd" t))
280 (setq mail-source-read-passwd 'read-passwd)
281 (unless (fboundp 'ange-ftp-read-passwd)
282 (autoload 'ange-ftp-read-passwd "ange-ftp"))
283 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
284 (funcall mail-source-read-passwd prompt)))
286 (defun mail-source-fetch-with-program (program)
287 (zerop (call-process shell-file-name nil nil nil
288 shell-command-switch program)))
291 ;;; Different fetchers
294 (defun mail-source-fetch-file (source callback)
295 "Fetcher for single-file sources."
296 (mail-source-bind (file source)
297 (let ((mail-source-string (format "file:%s" path)))
298 (if (mail-source-movemail path mail-source-crash-box)
299 (mail-source-callback callback path)
302 (defun mail-source-fetch-directory (source callback)
303 "Fetcher for directory sources."
304 (mail-source-bind (directory source)
306 (mail-source-string (format "directory:%s" path)))
307 (dolist (file (directory-files
308 path t (concat (regexp-quote suffix) "$")))
309 (when (and (file-regular-p file)
310 (funcall predicate file)
311 (mail-source-movemail file mail-source-crash-box))
312 (incf found (mail-source-callback callback file))))
315 (defun mail-source-fetch-pop (source callback)
316 "Fetcher for single-file sources."
317 (mail-source-bind (pop source)
319 (if (fboundp prescript)
321 (call-process shell-file-name nil nil nil
325 (format-spec-make ?p password ?t mail-source-crash-box
326 ?s server ?P port ?u user)))))
327 (let ((from (format "%s:%s:%s" server user port))
328 (mail-source-string (format "pop:%s@%s" user server))
330 (when (not (eq authentication 'apop))
333 (cdr (assoc from mail-source-password-cache))
334 (mail-source-read-passwd
335 (format "Password for %s at %s: " user server))))
336 (unless (assoc from mail-source-password-cache)
337 (push (cons from password) mail-source-password-cache)))
339 (setenv "MAILHOST" server))
343 (mail-source-fetch-with-program
346 (format-spec-make ?p password ?t mail-source-crash-box
347 ?s server ?P port ?u user))))
349 (funcall function mail-source-crash-box))
350 ;; The default is to use pop3.el.
352 (let ((pop3-password password)
354 (pop3-mailhost server)
356 (pop3-authentication-scheme
357 (if (eq authentication 'apop) 'apop 'pass)))
358 (save-excursion (pop3-movemail mail-source-crash-box))))))
361 (mail-source-callback callback server)
363 (if (fboundp prescript)
365 (call-process shell-file-name nil nil nil
370 ?p password ?t mail-source-crash-box
371 ?s server ?P port ?u user))))))
372 ;; We nix out the password in case the error
373 ;; was because of a wrong password being given.
374 (setq mail-source-password-cache
375 (delq (assoc from mail-source-password-cache)
376 mail-source-password-cache))
379 (defun mail-source-fetch-maildir (source callback)
380 "Fetcher for maildir sources."
381 (mail-source-bind (maildir source)
383 (mail-source-string (format "maildir:%s" path)))
384 (dolist (file (directory-files path t))
385 (when (and (file-regular-p file)
386 (not (rename-file file mail-source-crash-box)))
387 (incf found (mail-source-callback callback file))))
390 (provide 'mail-source)
392 ;;; mail-source.el ends here