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 (autoload 'pop3-get-message-count "pop3"))
32 (require 'format-spec)
34 (defgroup mail-source nil
35 "The mail-fetching library."
38 (defcustom mail-sources nil
39 "*Where the mail backends will look for incoming mail.
40 This variable is a list of mail source specifiers."
44 (defcustom mail-source-primary-source nil
45 "*Primary source for incoming mail.
46 If non-nil, this maildrop will be checked periodically for new mail."
50 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
51 "File where mail will be stored while processing it."
55 (defcustom mail-source-directory "~/Mail/"
56 "Directory where files (if any) will be stored."
60 (defcustom mail-source-default-file-modes 384
61 "Set the mode bits of all new mail files to this integer."
65 (defcustom mail-source-delete-incoming t
66 "*If non-nil, delete incoming files after handling."
70 (defcustom mail-source-report-new-mail-interval 5
71 "Interval in minutes between checks for new mail."
75 (defcustom mail-source-idle-time-delay 5
76 "Number of idle seconds to wait before checking for new mail."
80 ;;; Internal variables.
82 (defvar mail-source-string ""
83 "A dynamically bound string that says what the current mail source is.")
85 (defvar mail-source-new-mail-available nil
86 "Flag indicating when new mail is available.")
89 (defvar mail-source-common-keyword-map
91 "Mapping from keywords to default values.
92 Common keywords should be listed here.")
94 (defvar mail-source-keyword-map
99 (:path (or (getenv "MAIL")
100 (concat "/usr/spool/mail/" (user-login-name)))))
104 (:predicate identity))
109 (:server (getenv "MAILHOST"))
111 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
115 (:authentication password))
117 (:path "~/Maildir/new/")
120 (:server (getenv "MAILHOST"))
124 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
127 (:predicate "UNSEEN UNDELETED")
128 (:fetchflag "\\Deleted")
132 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
135 (:authentication password)))
136 "Mapping from keywords to default values.
137 All keywords that can be used must be listed here."))
139 (defvar mail-source-fetcher-alist
140 '((file mail-source-fetch-file)
141 (directory mail-source-fetch-directory)
142 (pop mail-source-fetch-pop)
143 (maildir mail-source-fetch-maildir)
144 (imap mail-source-fetch-imap)
145 (webmail mail-source-fetch-webmail))
146 "A mapping from source type to fetcher function.")
148 (defvar mail-source-password-cache nil)
150 (defvar mail-source-plugged t)
155 (defun mail-source-strip-keyword (keyword)
156 "Strip the leading colon off the KEYWORD."
157 (intern (substring (symbol-name keyword) 1))))
160 (defun mail-source-bind-1 (type)
161 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
163 (while (setq default (pop defaults))
164 (push (list (mail-source-strip-keyword (car default))
169 (defmacro mail-source-bind (type-source &rest body)
170 "Return a `let' form that binds all variables in source TYPE.
171 TYPE-SOURCE is a list where the first element is the TYPE, and
172 the second variable is the SOURCE.
173 At run time, the mail source specifier SOURCE will be inspected,
174 and the variables will be set according to it. Variables not
175 specified will be given default values.
177 After this is done, BODY will be executed in the scope
180 The variables bound and their default values are described by
181 the `mail-source-keyword-map' variable."
182 `(let ,(mail-source-bind-1 (car type-source))
183 (mail-source-set-1 ,(cadr type-source))
186 (put 'mail-source-bind 'lisp-indent-function 1)
187 (put 'mail-source-bind 'edebug-form-spec '(form body))
189 (defun mail-source-set-1 (source)
190 (let* ((type (pop source))
191 (defaults (cdr (assq type mail-source-keyword-map)))
192 default value keyword)
193 (while (setq default (pop defaults))
194 (set (mail-source-strip-keyword (setq keyword (car default)))
195 (if (setq value (plist-get source keyword))
196 (mail-source-value value)
197 (mail-source-value (cadr default)))))))
200 (defun mail-source-bind-common-1 ()
201 (let* ((defaults mail-source-common-keyword-map)
203 (while (setq default (pop defaults))
204 (push (list (mail-source-strip-keyword (car default))
209 (defun mail-source-set-common-1 (source)
210 (let* ((type (pop source))
211 (defaults mail-source-common-keyword-map)
212 (defaults-1 (cdr (assq type mail-source-keyword-map)))
213 default value keyword)
214 (while (setq default (pop defaults))
215 (set (mail-source-strip-keyword (setq keyword (car default)))
216 (if (setq value (plist-get source keyword))
217 (mail-source-value value)
218 (if (setq value (assq keyword defaults-1))
219 (mail-source-value (cadr value))
220 (mail-source-value (cadr default))))))))
222 (defmacro mail-source-bind-common (source &rest body)
223 "Return a `let' form that binds all common variables.
224 See `mail-source-bind'."
225 `(let ,(mail-source-bind-common-1)
226 (mail-source-set-common-1 source)
229 (put 'mail-source-bind-common 'lisp-indent-function 1)
230 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
232 (defun mail-source-value (value)
233 "Return the value of VALUE."
240 (functionp (car value)))
242 ;; Just return the value.
246 (defun mail-source-fetch (source callback)
247 "Fetch mail from SOURCE and call CALLBACK zero or more times.
248 CALLBACK will be called with the name of the file where (some of)
249 the mail from SOURCE is put.
250 Return the number of files that were found."
251 (mail-source-bind-common source
252 (if (or mail-source-plugged plugged)
254 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
257 (error "%S is an invalid mail source specification" source))
258 ;; If there's anything in the crash box, we do it first.
259 (when (file-exists-p mail-source-crash-box)
260 (message "Processing mail from %s..." mail-source-crash-box)
261 (setq found (mail-source-callback
262 callback mail-source-crash-box)))
265 (funcall function source callback)
268 (format "Mail source error (%s). Continue? " err))
269 (error "Cannot get new mail."))
272 (defun mail-source-make-complex-temp-name (prefix)
273 (let ((newname (make-temp-name prefix))
275 (while (file-exists-p newname)
276 (setq newprefix (concat newprefix "x"))
277 (setq newname (make-temp-name newprefix)))
280 (defun mail-source-callback (callback info)
281 "Call CALLBACK on the mail file, and then remove the mail file.
282 Pass INFO on to CALLBACK."
283 (if (or (not (file-exists-p mail-source-crash-box))
284 (zerop (nth 7 (file-attributes mail-source-crash-box))))
286 (when (file-exists-p mail-source-crash-box)
287 (delete-file mail-source-crash-box))
290 (funcall callback mail-source-crash-box info)
291 (when (file-exists-p mail-source-crash-box)
292 ;; Delete or move the incoming mail out of the way.
293 (if mail-source-delete-incoming
294 (delete-file mail-source-crash-box)
296 (mail-source-make-complex-temp-name
298 "Incoming" mail-source-directory))))
299 (unless (file-exists-p (file-name-directory incoming))
300 (make-directory (file-name-directory incoming) t))
301 (rename-file mail-source-crash-box incoming t)))))))
303 (defun mail-source-movemail (from to)
304 "Move FROM to TO using movemail."
305 (if (not (file-writable-p to))
306 (error "Can't write to crash box %s. Not moving mail" to)
307 (let ((to (file-truename (expand-file-name to)))
309 (setq to (file-truename to)
310 from (file-truename from))
311 ;; Set TO if have not already done so, and rename or copy
312 ;; the file FROM to TO if and as appropriate.
315 ;; The crash box exists already.
317 ((not (file-exists-p from))
318 ;; There is no inbox.
320 ((zerop (nth 7 (file-attributes from)))
324 ;; If getting from mail spool directory, use movemail to move
325 ;; rather than just renaming, so as to interlock with the
329 (setq errors (generate-new-buffer " *mail source loss*"))
330 (let ((default-directory "/"))
336 (expand-file-name "movemail" exec-directory)
337 nil errors nil from to)))))
338 (when (file-exists-p to)
339 (set-file-modes to mail-source-default-file-modes))
340 (if (and (not (buffer-modified-p errors))
342 ;; No output => movemail won.
345 ;; There may be a warning about older revisions. We
347 (goto-char (point-min))
348 (if (search-forward "older revision" nil t)
350 ;; Probably a real error.
351 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
352 (goto-char (point-max))
353 (skip-chars-backward " \t")
354 (delete-region (point) (point-max))
355 (goto-char (point-min))
356 (when (looking-at "movemail: ")
357 (delete-region (point-min) (match-end 0)))
359 (format "movemail: %s (%d return). Continue? "
360 (buffer-string) result))
361 (error "%s" (buffer-string)))
364 (buffer-name errors))
365 (kill-buffer errors))
366 ;; Return whether we moved successfully or not.
369 (defun mail-source-movemail-and-remove (from to)
370 "Move FROM to TO using movemail, then remove FROM if empty."
371 (or (not (mail-source-movemail from to))
372 (not (zerop (nth 7 (file-attributes from))))
375 (defvar mail-source-read-passwd nil)
376 (defun mail-source-read-passwd (prompt &rest args)
377 "Read a password using PROMPT.
378 If ARGS, PROMPT is used as an argument to `format'."
381 (apply 'format prompt args)
383 (unless mail-source-read-passwd
384 (if (or (fboundp 'read-passwd) (load "passwd" t))
385 (setq mail-source-read-passwd 'read-passwd)
386 (unless (fboundp 'ange-ftp-read-passwd)
387 (autoload 'ange-ftp-read-passwd "ange-ftp"))
388 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
389 (funcall mail-source-read-passwd prompt)))
391 (defun mail-source-fetch-with-program (program)
392 (zerop (call-process shell-file-name nil nil nil
393 shell-command-switch program)))
395 (defun mail-source-run-script (script spec &optional delay)
397 (if (and (symbolp script) (fboundp script))
399 (mail-source-call-script
400 (format-spec script spec))))
404 (defun mail-source-call-script (script)
405 (let ((background nil))
406 (when (string-match "& *$" script)
407 (setq script (substring script 0 (match-beginning 0))
409 (call-process shell-file-name nil background nil
410 shell-command-switch script)))
413 ;;; Different fetchers
416 (defun mail-source-fetch-file (source callback)
417 "Fetcher for single-file sources."
418 (mail-source-bind (file source)
419 (mail-source-run-script
420 prescript (format-spec-make ?t mail-source-crash-box)
422 (let ((mail-source-string (format "file:%s" path)))
423 (if (mail-source-movemail path mail-source-crash-box)
425 (mail-source-callback callback path)
426 (mail-source-run-script
427 postscript (format-spec-make ?t mail-source-crash-box)))
430 (defun mail-source-fetch-directory (source callback)
431 "Fetcher for directory sources."
432 (mail-source-bind (directory source)
434 (mail-source-string (format "directory:%s" path)))
435 (dolist (file (directory-files
436 path t (concat (regexp-quote suffix) "$")))
437 (when (and (file-regular-p file)
438 (funcall predicate file)
439 (mail-source-movemail file mail-source-crash-box))
440 (incf found (mail-source-callback callback file))))
443 (defun mail-source-fetch-pop (source callback)
444 "Fetcher for single-file sources."
445 (mail-source-bind (pop source)
446 (mail-source-run-script
448 (format-spec-make ?p password ?t mail-source-crash-box
449 ?s server ?P port ?u user)
451 (let ((from (format "%s:%s:%s" server user port))
452 (mail-source-string (format "pop:%s@%s" user server))
454 (when (eq authentication 'password)
457 (cdr (assoc from mail-source-password-cache))
458 (mail-source-read-passwd
459 (format "Password for %s at %s: " user server)))))
461 (setenv "MAILHOST" server))
465 (mail-source-fetch-with-program
468 (format-spec-make ?p password ?t mail-source-crash-box
469 ?s server ?P port ?u user))))
471 (funcall function mail-source-crash-box))
472 ;; The default is to use pop3.el.
474 (let ((pop3-password password)
476 (pop3-mailhost server)
478 (pop3-authentication-scheme
479 (if (eq authentication 'apop) 'apop 'pass)))
480 (save-excursion (pop3-movemail mail-source-crash-box))))))
483 (when (eq authentication 'password)
484 (unless (assoc from mail-source-password-cache)
485 (push (cons from password) mail-source-password-cache)))
487 (mail-source-callback callback server)
488 ;; Update display-time's mail flag, if relevant.
489 (if (equal source mail-source-primary-source)
490 (setq mail-source-new-mail-available nil))
491 (mail-source-run-script
493 (format-spec-make ?p password ?t mail-source-crash-box
494 ?s server ?P port ?u user))))
495 ;; We nix out the password in case the error
496 ;; was because of a wrong password being given.
497 (setq mail-source-password-cache
498 (delq (assoc from mail-source-password-cache)
499 mail-source-password-cache))
502 (defun mail-source-check-pop (source)
503 "Check whether there is new mail."
504 (mail-source-bind (pop source)
505 (let ((from (format "%s:%s:%s" server user port))
506 (mail-source-string (format "pop:%s@%s" user server))
508 (when (eq authentication 'password)
511 (cdr (assoc from mail-source-password-cache))
512 (mail-source-read-passwd
513 (format "Password for %s at %s: " user server))))
514 (unless (assoc from mail-source-password-cache)
515 (push (cons from password) mail-source-password-cache)))
517 (setenv "MAILHOST" server))
520 ;; No easy way to check whether mail is waiting for these.
523 ;; The default is to use pop3.el.
525 (let ((pop3-password password)
527 (pop3-mailhost server)
529 (pop3-authentication-scheme
530 (if (eq authentication 'apop) 'apop 'pass)))
531 (save-excursion (pop3-get-message-count))))))
533 ;; Inform display-time that we have new mail.
534 (setq mail-source-new-mail-available (> result 0))
535 ;; We nix out the password in case the error
536 ;; was because of a wrong password being given.
537 (setq mail-source-password-cache
538 (delq (assoc from mail-source-password-cache)
539 mail-source-password-cache)))
542 (defun mail-source-new-mail-p ()
543 "Handler for `display-time' to indicate when new mail is available."
544 ;; Only report flag setting; flag is updated on a different schedule.
545 mail-source-new-mail-available)
548 (defvar mail-source-report-new-mail nil)
549 (defvar mail-source-report-new-mail-timer nil)
550 (defvar mail-source-report-new-mail-idle-timer nil)
552 (eval-when-compile (require 'timer))
554 (defun mail-source-start-idle-timer ()
555 ;; Start our idle timer if necessary, so we delay the check until the
556 ;; user isn't typing.
557 (unless mail-source-report-new-mail-idle-timer
558 (setq mail-source-report-new-mail-idle-timer
560 mail-source-idle-time-delay
563 (setq mail-source-report-new-mail-idle-timer nil)
564 (mail-source-check-pop mail-source-primary-source))))
565 ;; Since idle timers created when Emacs is already in the idle
566 ;; state don't get activated until Emacs _next_ becomes idle, we
567 ;; need to force our timer to be considered active now. We do
568 ;; this by being naughty and poking the timer internals directly
569 ;; (element 0 of the vector is nil if the timer is active).
570 (aset mail-source-report-new-mail-idle-timer 0 nil)))
572 (defun mail-source-report-new-mail (arg)
573 "Toggle whether to report when new mail is available.
574 This only works when `display-time' is enabled."
576 (if (not mail-source-primary-source)
577 (error "Need to set `mail-source-primary-source' to check for new mail."))
578 (let ((on (if (null arg)
579 (not mail-source-report-new-mail)
580 (> (prefix-numeric-value arg) 0))))
581 (setq mail-source-report-new-mail on)
582 (and mail-source-report-new-mail-timer
583 (cancel-timer mail-source-report-new-mail-timer))
584 (and mail-source-report-new-mail-idle-timer
585 (cancel-timer mail-source-report-new-mail-idle-timer))
586 (setq mail-source-report-new-mail-timer nil)
587 (setq mail-source-report-new-mail-idle-timer nil)
591 (setq display-time-mail-function #'mail-source-new-mail-p)
592 ;; Set up the main timer.
593 (setq mail-source-report-new-mail-timer
594 (run-at-time t (* 60 mail-source-report-new-mail-interval)
595 #'mail-source-start-idle-timer))
596 ;; When you get new mail, clear "Mail" from the mode line.
597 (add-hook 'nnmail-post-get-new-mail-hook
598 'display-time-event-handler)
599 (message "Mail check enabled"))
600 (setq display-time-mail-function nil)
601 (remove-hook 'nnmail-post-get-new-mail-hook
602 'display-time-event-handler)
603 (message "Mail check disabled"))))
605 (defun mail-source-fetch-maildir (source callback)
606 "Fetcher for maildir sources."
607 (mail-source-bind (maildir source)
609 (mail-source-string (format "maildir:%s" path)))
610 (dolist (file (directory-files path t))
611 (when (and (not (file-directory-p file))
613 (funcall function file mail-source-crash-box)
614 (rename-file file mail-source-crash-box))))
615 (incf found (mail-source-callback callback file))))
619 (autoload 'imap-open "imap")
620 (autoload 'imap-authenticate "imap")
621 (autoload 'imap-mailbox-select "imap")
622 (autoload 'imap-mailbox-unselect "imap")
623 (autoload 'imap-mailbox-close "imap")
624 (autoload 'imap-search "imap")
625 (autoload 'imap-fetch "imap")
626 (autoload 'imap-close "imap")
627 (autoload 'imap-error-text "imap")
628 (autoload 'imap-message-flags-add "imap")
629 (autoload 'imap-list-to-message-set "imap")
630 (autoload 'nnheader-ms-strip-cr "nnheader"))
632 (defun mail-source-fetch-imap (source callback)
633 "Fetcher for imap sources."
634 (mail-source-bind (imap source)
635 (let ((from (format "%s:%s:%s" server user port))
637 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
638 (mail-source-string (format "imap:%s:%s" server mailbox))
640 (if (and (imap-open server port stream authentication buf)
642 user (or (cdr (assoc from mail-source-password-cache))
644 (imap-mailbox-select mailbox nil buf))
645 (let (str (coding-system-for-write 'binary))
646 (with-temp-file mail-source-crash-box
648 (with-current-buffer buf
649 (when (or imap-password
650 (assoc from mail-source-password-cache))
651 (push (cons from imap-password) mail-source-password-cache)))
652 ;; if predicate is nil, use all uids
653 (dolist (uid (imap-search (or predicate "1:*") buf))
654 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
656 (insert "From imap " (current-time-string) "\n")
659 (while (re-search-forward "^From " nil t)
660 (replace-match ">From "))
661 (goto-char (point-max))))
662 (nnheader-ms-strip-cr))
663 (incf found (mail-source-callback callback server))
664 (when (and remove fetchflag)
665 (imap-message-flags-add
666 (imap-list-to-message-set remove) fetchflag nil buf))
668 (imap-mailbox-unselect buf)
669 (imap-mailbox-close buf))
672 ;; We nix out the password in case the error
673 ;; was because of a wrong password being given.
674 (setq mail-source-password-cache
675 (delq (assoc from mail-source-password-cache)
676 mail-source-password-cache))
677 (error (imap-error-text buf)))
682 (autoload 'webmail-fetch "webmail"))
684 (defun mail-source-fetch-webmail (source callback)
685 "Fetch for webmail source."
686 (mail-source-bind (webmail source)
687 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
688 (webmail-newmail-only dontexpunge)
689 (webmail-move-to-trash-can (not dontexpunge)))
690 (when (eq authentication 'password)
693 (cdr (assoc (format "webmail:%s:%s" subtype user)
694 mail-source-password-cache))
695 (mail-source-read-passwd
696 (format "Password for %s at %s: " user subtype))))
698 (not (assoc (format "webmail:%s:%s" subtype user)
699 mail-source-password-cache)))
700 (push (cons (format "webmail:%s:%s" subtype user) password)
701 mail-source-password-cache)))
702 (webmail-fetch mail-source-crash-box subtype user password)
703 (mail-source-callback callback (symbol-name subtype)))))
705 (provide 'mail-source)
707 ;;; mail-source.el ends here