1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002 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.
31 (eval-when-compile (defvar display-time-mail-function)))
33 (autoload 'pop3-movemail "pop3")
34 (autoload 'pop3-get-message-count "pop3")
35 (autoload 'nnheader-cancel-timer "nnheader")
36 (autoload 'nnheader-run-at-time "nnheader"))
37 (require 'format-spec)
39 (require 'message) ;; for `message-directory'
41 (defgroup mail-source nil
42 "The mail-fetching library."
46 ;; Define these at compile time to avoid dragging in imap always.
47 (defconst mail-source-imap-authenticators
50 (list 'const (car a)))
51 imap-authenticator-alist)))
52 (defconst mail-source-imap-streams
55 (list 'const (car a)))
58 (defcustom mail-sources nil
59 "*Where the mail backends will look for incoming mail.
60 This variable is a list of mail source specifiers.
61 See Info node `(gnus)Mail Source Specifiers'."
63 :link '(custom-manual "(gnus)Mail Source Specifiers")
65 (choice :format "%[Value Menu%] %v"
67 (cons :tag "Spool file"
68 (const :format "" file)
69 (checklist :tag "Options" :greedy t
71 (const :format "" :value :path)
73 (cons :tag "Several files in a directory"
74 (const :format "" directory)
75 (checklist :tag "Options" :greedy t
77 (const :format "" :value :path)
78 (directory :tag "Path"))
80 (const :format "" :value :suffix)
81 (string :tag "Suffix"))
83 (const :format "" :value :predicate)
84 (function :tag "Predicate"))
86 (const :format "" :value :prescript)
87 (choice :tag "Prescript"
90 (function :format "%v")))
92 (const :format "" :value :postscript)
93 (choice :tag "Postscript"
96 (function :format "%v")))
98 (const :format "" :value :plugged)
99 (boolean :tag "Plugged"))))
100 (cons :tag "POP3 server"
101 (const :format "" pop)
102 (checklist :tag "Options" :greedy t
104 (const :format "" :value :server)
105 (string :tag "Server"))
107 (const :format "" :value :port)
110 (number :format "%v")
111 (string :format "%v")))
113 (const :format "" :value :user)
114 (string :tag "User"))
116 (const :format "" :value :password)
117 (string :tag "Password"))
119 (const :format "" :value :program)
120 (string :tag "Program"))
122 (const :format "" :value :prescript)
123 (choice :tag "Prescript"
125 (string :format "%v")
126 (function :format "%v")))
128 (const :format "" :value :postscript)
129 (choice :tag "Postscript"
131 (string :format "%v")
132 (function :format "%v")))
134 (const :format "" :value :function)
135 (function :tag "Function"))
138 :value :authentication)
139 (choice :tag "Authentication"
144 (const :format "" :value :plugged)
145 (boolean :tag "Plugged"))))
146 (cons :tag "Maildir (qmail, postfix...)"
147 (const :format "" maildir)
148 (checklist :tag "Options" :greedy t
150 (const :format "" :value :path)
151 (directory :tag "Path"))
153 (const :format "" :value :plugged)
154 (boolean :tag "Plugged"))))
155 (cons :tag "IMAP server"
156 (const :format "" imap)
157 (checklist :tag "Options" :greedy t
159 (const :format "" :value :server)
160 (string :tag "Server"))
162 (const :format "" :value :port)
167 (const :format "" :value :user)
168 (string :tag "User"))
170 (const :format "" :value :password)
171 (string :tag "Password"))
173 (const :format "" :value :stream)
174 (choice :tag "Stream"
176 ,@mail-source-imap-streams))
178 (const :format "" :value :program)
179 (string :tag "Program"))
182 :value :authenticator)
183 (choice :tag "Authenticator"
185 ,@mail-source-imap-authenticators))
187 (const :format "" :value :mailbox)
188 (string :tag "Mailbox"
191 (const :format "" :value :predicate)
192 (string :tag "Predicate"
193 :value "UNSEEN UNDELETED"))
195 (const :format "" :value :fetchflag)
196 (string :tag "Fetchflag"
201 (boolean :tag "Dontexpunge"))
203 (const :format "" :value :plugged)
204 (boolean :tag "Plugged"))))
205 (cons :tag "Webmail server"
206 (const :format "" webmail)
207 (checklist :tag "Options" :greedy t
209 (const :format "" :value :subtype)
210 ;; Should be generated from
211 ;; `webmail-type-definition', but we
212 ;; can't require webmail without W3.
213 (choice :tag "Subtype"
221 (const :format "" :value :user)
222 (string :tag "User"))
224 (const :format "" :value :password)
225 (string :tag "Password"))
229 (boolean :tag "Dontexpunge"))
231 (const :format "" :value :plugged)
232 (boolean :tag "Plugged")))))))
234 (defcustom mail-source-primary-source nil
235 "*Primary source for incoming mail.
236 If non-nil, this maildrop will be checked periodically for new mail."
240 (defcustom mail-source-flash t
241 "*If non-nil, flash periodically when mail is available."
245 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
246 "File where mail will be stored while processing it."
250 (defcustom mail-source-directory message-directory
251 "Directory where files (if any) will be stored."
255 (defcustom mail-source-default-file-modes 384
256 "Set the mode bits of all new mail files to this integer."
260 (defcustom mail-source-delete-incoming nil
261 "*If non-nil, delete incoming files after handling."
265 (defcustom mail-source-incoming-file-prefix "Incoming"
266 "Prefix for file name for storing incoming mail"
270 (defcustom mail-source-report-new-mail-interval 5
271 "Interval in minutes between checks for new mail."
275 (defcustom mail-source-idle-time-delay 5
276 "Number of idle seconds to wait before checking for new mail."
280 (defcustom mail-source-movemail-program nil
281 "If non-nil, name of program for fetching new mail."
283 :type '(choice (const nil) string))
285 ;;; Internal variables.
287 (defvar mail-source-string ""
288 "A dynamically bound string that says what the current mail source is.")
290 (defvar mail-source-new-mail-available nil
291 "Flag indicating when new mail is available.")
294 (defvar mail-source-common-keyword-map
296 "Mapping from keywords to default values.
297 Common keywords should be listed here.")
299 (defvar mail-source-keyword-map
304 (:path (or (getenv "MAIL")
305 (expand-file-name (user-login-name) rmail-spool-directory))))
312 (:predicate identity))
317 (:server (getenv "MAILHOST"))
319 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
323 (:authentication password))
325 (:path (or (getenv "MAILDIR") "~/Maildir/"))
326 (:subdirs ("new" "cur"))
329 (:server (getenv "MAILHOST"))
334 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
337 (:predicate "UNSEEN UNDELETED")
338 (:fetchflag "\\Deleted")
342 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
345 (:authentication password)))
346 "Mapping from keywords to default values.
347 All keywords that can be used must be listed here."))
349 (defvar mail-source-fetcher-alist
350 '((file mail-source-fetch-file)
351 (directory mail-source-fetch-directory)
352 (pop mail-source-fetch-pop)
353 (maildir mail-source-fetch-maildir)
354 (imap mail-source-fetch-imap)
355 (webmail mail-source-fetch-webmail))
356 "A mapping from source type to fetcher function.")
358 (defvar mail-source-password-cache nil)
360 (defvar mail-source-plugged t)
365 (defun mail-source-strip-keyword (keyword)
366 "Strip the leading colon off the KEYWORD."
367 (intern (substring (symbol-name keyword) 1))))
370 (defun mail-source-bind-1 (type)
371 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
373 (while (setq default (pop defaults))
374 (push (list (mail-source-strip-keyword (car default))
379 (defmacro mail-source-bind (type-source &rest body)
380 "Return a `let' form that binds all variables in source TYPE.
381 TYPE-SOURCE is a list where the first element is the TYPE, and
382 the second variable is the SOURCE.
383 At run time, the mail source specifier SOURCE will be inspected,
384 and the variables will be set according to it. Variables not
385 specified will be given default values.
387 After this is done, BODY will be executed in the scope
390 The variables bound and their default values are described by
391 the `mail-source-keyword-map' variable."
392 `(let ,(mail-source-bind-1 (car type-source))
393 (mail-source-set-1 ,(cadr type-source))
396 (put 'mail-source-bind 'lisp-indent-function 1)
397 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
399 (defun mail-source-set-1 (source)
400 (let* ((type (pop source))
401 (defaults (cdr (assq type mail-source-keyword-map)))
402 default value keyword)
403 (while (setq default (pop defaults))
404 (set (mail-source-strip-keyword (setq keyword (car default)))
405 (if (setq value (plist-get source keyword))
406 (mail-source-value value)
407 (mail-source-value (cadr default)))))))
410 (defun mail-source-bind-common-1 ()
411 (let* ((defaults mail-source-common-keyword-map)
413 (while (setq default (pop defaults))
414 (push (list (mail-source-strip-keyword (car default))
419 (defun mail-source-set-common-1 (source)
420 (let* ((type (pop source))
421 (defaults mail-source-common-keyword-map)
422 (defaults-1 (cdr (assq type mail-source-keyword-map)))
423 default value keyword)
424 (while (setq default (pop defaults))
425 (set (mail-source-strip-keyword (setq keyword (car default)))
426 (if (setq value (plist-get source keyword))
427 (mail-source-value value)
428 (if (setq value (assq keyword defaults-1))
429 (mail-source-value (cadr value))
430 (mail-source-value (cadr default))))))))
432 (defmacro mail-source-bind-common (source &rest body)
433 "Return a `let' form that binds all common variables.
434 See `mail-source-bind'."
435 `(let ,(mail-source-bind-common-1)
436 (mail-source-set-common-1 source)
439 (put 'mail-source-bind-common 'lisp-indent-function 1)
440 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
442 (defun mail-source-value (value)
443 "Return the value of VALUE."
450 (functionp (car value)))
452 ;; Just return the value.
456 (defun mail-source-fetch (source callback)
457 "Fetch mail from SOURCE and call CALLBACK zero or more times.
458 CALLBACK will be called with the name of the file where (some of)
459 the mail from SOURCE is put.
460 Return the number of files that were found."
461 (mail-source-bind-common source
462 (if (or mail-source-plugged plugged)
464 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
467 (error "%S is an invalid mail source specification" source))
468 ;; If there's anything in the crash box, we do it first.
469 (when (file-exists-p mail-source-crash-box)
470 (message "Processing mail from %s..." mail-source-crash-box)
471 (setq found (mail-source-callback
472 callback mail-source-crash-box)))
474 (if (or debug-on-quit debug-on-error)
475 (funcall function source callback)
477 (funcall function source callback)
480 (format "Mail source %s error (%s). Continue? "
481 (if (memq ':password source)
482 (let ((s (copy-sequence source)))
483 (setcar (cdr (memq ':password s))
488 (error "Cannot get new mail"))
492 (if (fboundp 'make-temp-file)
493 (defalias 'mail-source-make-complex-temp-name 'make-temp-file)
494 (defun mail-source-make-complex-temp-name (prefix)
495 (let ((newname (make-temp-name prefix))
497 (while (file-exists-p newname)
498 (setq newprefix (concat newprefix "x"))
499 (setq newname (make-temp-name newprefix)))
502 (defun mail-source-callback (callback info)
503 "Call CALLBACK on the mail file, and then remove the mail file.
504 Pass INFO on to CALLBACK."
505 (if (or (not (file-exists-p mail-source-crash-box))
506 (zerop (nth 7 (file-attributes mail-source-crash-box))))
508 (when (file-exists-p mail-source-crash-box)
509 (delete-file mail-source-crash-box))
512 (funcall callback mail-source-crash-box info)
513 (when (file-exists-p mail-source-crash-box)
514 ;; Delete or move the incoming mail out of the way.
515 (if mail-source-delete-incoming
516 (delete-file mail-source-crash-box)
518 (mail-source-make-complex-temp-name
520 mail-source-incoming-file-prefix
521 mail-source-directory))))
522 (unless (file-exists-p (file-name-directory incoming))
523 (make-directory (file-name-directory incoming) t))
524 (rename-file mail-source-crash-box incoming t)))))))
526 (defun mail-source-movemail (from to)
527 "Move FROM to TO using movemail."
528 (if (not (file-writable-p to))
529 (error "Can't write to crash box %s. Not moving mail" to)
530 (let ((to (file-truename (expand-file-name to)))
532 (setq to (file-truename to)
533 from (file-truename from))
534 ;; Set TO if have not already done so, and rename or copy
535 ;; the file FROM to TO if and as appropriate.
538 ;; The crash box exists already.
540 ((not (file-exists-p from))
541 ;; There is no inbox.
543 ((zerop (nth 7 (file-attributes from)))
547 ;; If getting from mail spool directory, use movemail to move
548 ;; rather than just renaming, so as to interlock with the
552 (setq errors (generate-new-buffer " *mail source loss*"))
553 (let ((default-directory "/"))
559 (or mail-source-movemail-program
560 (expand-file-name "movemail" exec-directory))
561 nil errors nil from to)))))
562 (when (file-exists-p to)
563 (set-file-modes to mail-source-default-file-modes))
564 (if (and (or (not (buffer-modified-p errors))
565 (zerop (buffer-size errors)))
567 ;; No output => movemail won.
570 ;; There may be a warning about older revisions. We
572 (goto-char (point-min))
573 (if (search-forward "older revision" nil t)
575 ;; Probably a real error.
576 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
577 (goto-char (point-max))
578 (skip-chars-backward " \t")
579 (delete-region (point) (point-max))
580 (goto-char (point-min))
581 (when (looking-at "movemail: ")
582 (delete-region (point-min) (match-end 0)))
583 ;; Result may be a signal description string.
585 (format "movemail: %s (%s return). Continue? "
586 (buffer-string) result))
587 (error "%s" (buffer-string)))
590 (buffer-name errors))
591 (kill-buffer errors))
592 ;; Return whether we moved successfully or not.
595 (defun mail-source-movemail-and-remove (from to)
596 "Move FROM to TO using movemail, then remove FROM if empty."
597 (or (not (mail-source-movemail from to))
598 (not (zerop (nth 7 (file-attributes from))))
601 (defvar mail-source-read-passwd nil)
602 (defun mail-source-read-passwd (prompt &rest args)
603 "Read a password using PROMPT.
604 If ARGS, PROMPT is used as an argument to `format'."
607 (apply 'format prompt args)
609 (unless mail-source-read-passwd
610 (if (or (fboundp 'read-passwd) (load "passwd" t))
611 (setq mail-source-read-passwd 'read-passwd)
612 (unless (fboundp 'ange-ftp-read-passwd)
613 (autoload 'ange-ftp-read-passwd "ange-ftp"))
614 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
615 (funcall mail-source-read-passwd prompt)))
617 (defun mail-source-fetch-with-program (program)
618 (zerop (call-process shell-file-name nil nil nil
619 shell-command-switch program)))
621 (defun mail-source-run-script (script spec &optional delay)
623 (if (functionp script)
625 (mail-source-call-script
626 (format-spec script spec))))
630 (defun mail-source-call-script (script)
631 (let ((background nil))
632 (when (string-match "& *$" script)
633 (setq script (substring script 0 (match-beginning 0))
635 (call-process shell-file-name nil background nil
636 shell-command-switch script)))
639 ;;; Different fetchers