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 (defvar pop3-leave-mail-on-server)
34 (autoload 'pop3-movemail "pop3")
35 (autoload 'pop3-get-message-count "pop3")
36 (autoload 'nnheader-cancel-timer "nnheader")
37 (autoload 'nnheader-run-at-time "nnheader"))
38 (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'."
64 (choice :format "%[Value Menu%] %v"
66 (cons :tag "Spool file"
67 (const :format "" file)
68 (checklist :tag "Options" :greedy t
70 (const :format "" :value :path)
72 (cons :tag "Several files in a directory"
73 (const :format "" directory)
74 (checklist :tag "Options" :greedy t
76 (const :format "" :value :path)
77 (directory :tag "Path"))
79 (const :format "" :value :suffix)
80 (string :tag "Suffix"))
82 (const :format "" :value :predicate)
83 (function :tag "Predicate"))
85 (const :format "" :value :prescript)
86 (string :tag "Prescript"))
88 (const :format "" :value :postscript)
89 (string :tag "Postscript"))
91 (const :format "" :value :plugged)
92 (boolean :tag "Plugged"))))
93 (cons :tag "POP3 server"
94 (const :format "" pop)
95 (checklist :tag "Options" :greedy t
97 (const :format "" :value :server)
98 (string :tag "Server"))
100 (const :format "" :value :port)
103 (number :format "%v")
104 (string :format "%v")))
106 (const :format "" :value :user)
107 (string :tag "User"))
109 (const :format "" :value :password)
110 (string :tag "Password"))
112 (const :format "" :value :program)
113 (string :tag "Program"))
115 (const :format "" :value :prescript)
116 (string :tag "Prescript"))
118 (const :format "" :value :postscript)
119 (string :tag "Postscript"))
121 (const :format "" :value :function)
122 (function :tag "Function"))
125 :value :authentication)
126 (choice :tag "Authentication"
131 (const :format "" :value :plugged)
132 (boolean :tag "Plugged"))))
133 (cons :tag "Maildir (qmail, postfix...)"
134 (const :format "" maildir)
135 (checklist :tag "Options" :greedy t
137 (const :format "" :value :path)
138 (directory :tag "Path"))
140 (const :format "" :value :plugged)
141 (boolean :tag "Plugged"))))
142 (cons :tag "IMAP server"
143 (const :format "" imap)
144 (checklist :tag "Options" :greedy t
146 (const :format "" :value :server)
147 (string :tag "Server"))
149 (const :format "" :value :port)
154 (const :format "" :value :user)
155 (string :tag "User"))
157 (const :format "" :value :password)
158 (string :tag "Password"))
160 (const :format "" :value :stream)
161 (choice :tag "Stream"
163 ,@mail-source-imap-streams))
165 (const :format "" :value :program)
166 (string :tag "Program"))
169 :value :authenticator)
170 (choice :tag "Authenticator"
172 ,@mail-source-imap-authenticators))
174 (const :format "" :value :mailbox)
175 (string :tag "Mailbox"
178 (const :format "" :value :predicate)
179 (string :tag "Predicate"
180 :value "UNSEEN UNDELETED"))
182 (const :format "" :value :fetchflag)
183 (string :tag "Fetchflag"
188 (boolean :tag "Dontexpunge"))
190 (const :format "" :value :plugged)
191 (boolean :tag "Plugged"))))
192 (cons :tag "Webmail server"
193 (const :format "" webmail)
194 (checklist :tag "Options" :greedy t
196 (const :format "" :value :subtype)
197 ;; Should be generated from
198 ;; `webmail-type-definition', but we
199 ;; can't require webmail without W3.
200 (choice :tag "Subtype"
208 (const :format "" :value :user)
209 (string :tag "User"))
211 (const :format "" :value :password)
212 (string :tag "Password"))
216 (boolean :tag "Dontexpunge"))
218 (const :format "" :value :plugged)
219 (boolean :tag "Plugged")))))))
221 (defcustom mail-source-primary-source nil
222 "*Primary source for incoming mail.
223 If non-nil, this maildrop will be checked periodically for new mail."
227 (defcustom mail-source-flash t
228 "*If non-nil, flash periodically when mail is available."
232 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
233 "File where mail will be stored while processing it."
237 (defcustom mail-source-directory message-directory
238 "Directory where files (if any) will be stored."
242 (defcustom mail-source-default-file-modes 384
243 "Set the mode bits of all new mail files to this integer."
247 (defcustom mail-source-delete-incoming nil
248 "*If non-nil, delete incoming files after handling."
252 (defcustom mail-source-incoming-file-prefix "Incoming"
253 "Prefix for file name for storing incoming mail"
257 (defcustom mail-source-report-new-mail-interval 5
258 "Interval in minutes between checks for new mail."
262 (defcustom mail-source-idle-time-delay 5
263 "Number of idle seconds to wait before checking for new mail."
267 (defcustom mail-source-movemail-program nil
268 "If non-nil, name of program for fetching new mail."
270 :type '(choice (const nil) string))
272 ;;; Internal variables.
274 (defvar mail-source-string ""
275 "A dynamically bound string that says what the current mail source is.")
277 (defvar mail-source-new-mail-available nil
278 "Flag indicating when new mail is available.")
281 (defvar mail-source-common-keyword-map
283 "Mapping from keywords to default values.
284 Common keywords should be listed here.")
286 (defvar mail-source-keyword-map
291 (:path (or (getenv "MAIL")
292 (expand-file-name (user-login-name) rmail-spool-directory))))
299 (:predicate identity))
304 (:server (getenv "MAILHOST"))
306 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
311 (:authentication password)
314 (:path (or (getenv "MAILDIR") "~/Maildir/"))
315 (:subdirs ("new" "cur"))
318 (:server (getenv "MAILHOST"))
323 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
326 (:predicate "UNSEEN UNDELETED")
327 (:fetchflag "\\Deleted")
331 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
334 (:authentication password)))
335 "Mapping from keywords to default values.
336 All keywords that can be used must be listed here."))
338 (defvar mail-source-fetcher-alist
339 '((file mail-source-fetch-file)
340 (directory mail-source-fetch-directory)
341 (pop mail-source-fetch-pop)
342 (maildir mail-source-fetch-maildir)
343 (imap mail-source-fetch-imap)
344 (webmail mail-source-fetch-webmail))
345 "A mapping from source type to fetcher function.")
347 (defvar mail-source-password-cache nil)
349 (defvar mail-source-plugged t)
354 (defun mail-source-strip-keyword (keyword)
355 "Strip the leading colon off the KEYWORD."
356 (intern (substring (symbol-name keyword) 1))))
359 (defun mail-source-bind-1 (type)
360 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
362 (while (setq default (pop defaults))
363 (push (list (mail-source-strip-keyword (car default))
368 (defmacro mail-source-bind (type-source &rest body)
369 "Return a `let' form that binds all variables in source TYPE.
370 TYPE-SOURCE is a list where the first element is the TYPE, and
371 the second variable is the SOURCE.
372 At run time, the mail source specifier SOURCE will be inspected,
373 and the variables will be set according to it. Variables not
374 specified will be given default values.
376 After this is done, BODY will be executed in the scope
379 The variables bound and their default values are described by
380 the `mail-source-keyword-map' variable."
381 `(let ,(mail-source-bind-1 (car type-source))
382 (mail-source-set-1 ,(cadr type-source))
385 (put 'mail-source-bind 'lisp-indent-function 1)
386 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
388 (defun mail-source-set-1 (source)
389 (let* ((type (pop source))
390 (defaults (cdr (assq type mail-source-keyword-map)))
391 default value keyword)
392 (while (setq default (pop defaults))
393 (set (mail-source-strip-keyword (setq keyword (car default)))
394 (if (setq value (plist-get source keyword))
395 (mail-source-value value)
396 (mail-source-value (cadr default)))))))
399 (defun mail-source-bind-common-1 ()
400 (let* ((defaults mail-source-common-keyword-map)
402 (while (setq default (pop defaults))
403 (push (list (mail-source-strip-keyword (car default))
408 (defun mail-source-set-common-1 (source)
409 (let* ((type (pop source))
410 (defaults mail-source-common-keyword-map)
411 (defaults-1 (cdr (assq type mail-source-keyword-map)))
412 default value keyword)
413 (while (setq default (pop defaults))
414 (set (mail-source-strip-keyword (setq keyword (car default)))
415 (if (setq value (plist-get source keyword))
416 (mail-source-value value)
417 (if (setq value (assq keyword defaults-1))
418 (mail-source-value (cadr value))
419 (mail-source-value (cadr default))))))))
421 (defmacro mail-source-bind-common (source &rest body)
422 "Return a `let' form that binds all common variables.
423 See `mail-source-bind'."
424 `(let ,(mail-source-bind-common-1)
425 (mail-source-set-common-1 source)
428 (put 'mail-source-bind-common 'lisp-indent-function 1)
429 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
431 (defun mail-source-value (value)
432 "Return the value of VALUE."
439 (functionp (car value)))
441 ;; Just return the value.
445 (defun mail-source-fetch (source callback)
446 "Fetch mail from SOURCE and call CALLBACK zero or more times.
447 CALLBACK will be called with the name of the file where (some of)
448 the mail from SOURCE is put.
449 Return the number of files that were found."
450 (mail-source-bind-common source
451 (if (or mail-source-plugged plugged)
453 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
456 (error "%S is an invalid mail source specification" source))
457 ;; If there's anything in the crash box, we do it first.
458 (when (file-exists-p mail-source-crash-box)
459 (message "Processing mail from %s..." mail-source-crash-box)
460 (setq found (mail-source-callback
461 callback mail-source-crash-box)))
463 (if (or debug-on-quit debug-on-error)
464 (funcall function source callback)
466 (funcall function source callback)
469 (format "Mail source %s error (%s). Continue? "
472 (error "Cannot get new mail"))
476 (if (fboundp 'make-temp-file)
477 (defalias 'mail-source-make-complex-temp-name 'make-temp-file)
478 (defun mail-source-make-complex-temp-name (prefix)
479 (let ((newname (make-temp-name prefix))
481 (while (file-exists-p newname)
482 (setq newprefix (concat newprefix "x"))
483 (setq newname (make-temp-name newprefix)))
486 (defun mail-source-callback (callback info)
487 "Call CALLBACK on the mail file, and then remove the mail file.
488 Pass INFO on to CALLBACK."
489 (if (or (not (file-exists-p mail-source-crash-box))
490 (zerop (nth 7 (file-attributes mail-source-crash-box))))
492 (when (file-exists-p mail-source-crash-box)
493 (delete-file mail-source-crash-box))
496 (funcall callback mail-source-crash-box info)
497 (when (file-exists-p mail-source-crash-box)
498 ;; Delete or move the incoming mail out of the way.
499 (if mail-source-delete-incoming
500 (delete-file mail-source-crash-box)
502 (mail-source-make-complex-temp-name
504 mail-source-incoming-file-prefix
505 mail-source-directory))))
506 (unless (file-exists-p (file-name-directory incoming))
507 (make-directory (file-name-directory incoming) t))
508 (rename-file mail-source-crash-box incoming t)))))))
510 (defun mail-source-movemail (from to)
511 "Move FROM to TO using movemail."
512 (if (not (file-writable-p to))
513 (error "Can't write to crash box %s. Not moving mail" to)
514 (let ((to (file-truename (expand-file-name to)))
516 (setq to (file-truename to)
517 from (file-truename from))
518 ;; Set TO if have not already done so, and rename or copy
519 ;; the file FROM to TO if and as appropriate.
522 ;; The crash box exists already.
524 ((not (file-exists-p from))
525 ;; There is no inbox.
527 ((zerop (nth 7 (file-attributes from)))
531 ;; If getting from mail spool directory, use movemail to move
532 ;; rather than just renaming, so as to interlock with the
536 (setq errors (generate-new-buffer " *mail source loss*"))
537 (let ((default-directory "/"))
543 (or mail-source-movemail-program
544 (expand-file-name "movemail" exec-directory))
545 nil errors nil from to)))))
546 (when (file-exists-p to)
547 (set-file-modes to mail-source-default-file-modes))
548 (if (and (or (not (buffer-modified-p errors))
549 (zerop (buffer-size errors)))
551 ;; No output => movemail won.
554 ;; There may be a warning about older revisions. We
556 (goto-char (point-min))
557 (if (search-forward "older revision" nil t)
559 ;; Probably a real error.
560 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
561 (goto-char (point-max))
562 (skip-chars-backward " \t")
563 (delete-region (point) (point-max))
564 (goto-char (point-min))
565 (when (looking-at "movemail: ")
566 (delete-region (point-min) (match-end 0)))
567 ;; Result may be a signal description string.
569 (format "movemail: %s (%s return). Continue? "
570 (buffer-string) result))
571 (error "%s" (buffer-string)))
574 (buffer-name errors))
575 (kill-buffer errors))
576 ;; Return whether we moved successfully or not.
579 (defun mail-source-movemail-and-remove (from to)
580 "Move FROM to TO using movemail, then remove FROM if empty."
581 (or (not (mail-source-movemail from to))
582 (not (zerop (nth 7 (file-attributes from))))
585 (defvar mail-source-read-passwd nil)
586 (defun mail-source-read-passwd (prompt &rest args)
587 "Read a password using PROMPT.
588 If ARGS, PROMPT is used as an argument to `format'."
591 (apply 'format prompt args)
593 (unless mail-source-read-passwd
594 (if (or (fboundp 'read-passwd) (load "passwd" t))
595 (setq mail-source-read-passwd 'read-passwd)
596 (unless (fboundp 'ange-ftp-read-passwd)
597 (autoload 'ange-ftp-read-passwd "ange-ftp"))
598 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
599 (funcall mail-source-read-passwd prompt)))
601 (defun mail-source-fetch-with-program (program)
602 (zerop (call-process shell-file-name nil nil nil
603 shell-command-switch program)))
605 (defun mail-source-run-script (script spec &optional delay)
607 (if (and (symbolp script) (fboundp script))
609 (mail-source-call-script
610 (format-spec script spec))))
614 (defun mail-source-call-script (script)
615 (let ((background nil))
616 (when (string-match "& *$" script)
617 (setq script (substring script 0 (match-beginning 0))
619 (call-process shell-file-name nil background nil
620 shell-command-switch script)))
623 ;;; Different fetchers
626 (defun mail-source-fetch-file (source callback)
627 "Fetcher for single-file sources."
628 (mail-source-bind (file source)
629 (mail-source-run-script
630 prescript (format-spec-make ?t mail-source-crash-box)
632 (let ((mail-source-string (format "file:%s" path)))
633 (if (mail-source-movemail path mail-source-crash-box)
635 (mail-source-callback callback path)
636 (mail-source-run-script
637 postscript (format-spec-make ?t mail-source-crash-box)))
640 (defun mail-source-fetch-directory (source callback)
641 "Fetcher for directory sources."
642 (mail-source-bind (directory source)
643 (mail-source-run-script
644 prescript (format-spec-make ?t path)
647 (mail-source-string (format "directory:%s" path)))
648 (dolist (file (directory-files
649 path t (concat (regexp-quote suffix) "$")))
650 (when (and (file-regular-p file)
651 (funcall predicate file)
652 (mail-source-movemail file mail-source-crash-box))
653 (incf found (mail-source-callback callback file))))
654 (mail-source-run-script
655 postscript (format-spec-make ?t path))
658 (defun mail-source-fetch-pop (source callback)
659 "Fetcher for single-file sources."
660 (mail-source-bind (pop source)
661 (mail-source-run-script
663 (format-spec-make ?p password ?t mail-source-crash-box
664 ?s server ?P port ?u user)
666 (let ((from (format "%s:%s:%s" server user port))
667 (mail-source-string (format "pop:%s@%s" user server))
669 (when (eq authentication 'password)
672 (cdr (assoc from mail-source-password-cache))
673 (mail-source-read-passwd
674 (format "Password for %s at %s: " user server)))))
676 (setenv "MAILHOST" server))
680 (mail-source-fetch-with-program
683 (format-spec-make ?p password ?t mail-source-crash-box
684 ?s server ?P port ?u user))))
686 (funcall function mail-source-crash-box))
687 ;; The default is to use pop3.el.
689 (let ((pop3-password password)
691 (pop3-mailhost server)
693 (pop3-authentication-scheme
694 (if (eq authentication 'apop) 'apop 'pass))
695 (pop3-connection-type connection)
696 (pop3-leave-mail-on-server
698 (and (boundp 'pop3-leave-mail-on-server)
699 pop3-leave-mail-on-server))))
700 (if (or debug-on-quit debug-on-error)
701 (save-excursion (pop3-movemail mail-source-crash-box))
703 (save-excursion (pop3-movemail mail-source-crash-box))
705 ;; We nix out the password in case the error
706 ;; was because of a wrong password being given.
707 (setq mail-source-password-cache
708 (delq (assoc from mail-source-password-cache)
709 mail-source-password-cache))
710 (signal (car err) (cdr err)))))))))
713 (when (eq authentication 'password)
714 (unless (assoc from mail-source-password-cache)
715 (push (cons from password) mail-source-password-cache)))
717 (mail-source-callback callback server)
718 ;; Update display-time's mail flag, if relevant.
719 (if (equal source mail-source-primary-source)
720 (setq mail-source-new-mail-available nil))
721 (mail-source-run-script
723 (format-spec-make ?p password ?t mail-source-crash-box
724 ?s server ?P port ?u user))))
725 ;; We nix out the password in case the error
726 ;; was because of a wrong password being given.
727 (setq mail-source-password-cache
728 (delq (assoc from mail-source-password-cache)
729 mail-source-password-cache))
732 (defun mail-source-check-pop (source)
733 "Check whether there is new mail."
734 (mail-source-bind (pop source)
735 (let ((from (format "%s:%s:%s" server user port))
736 (mail-source-string (format "pop:%s@%s" user server))
738 (when (eq authentication 'password)
741 (cdr (assoc from mail-source-password-cache))
742 (mail-source-read-passwd
743 (format "Password for %s at %s: " user server))))
744 (unless (assoc from mail-source-password-cache)
745 (push (cons from password) mail-source-password-cache)))
747 (setenv "MAILHOST" server))
750 ;; No easy way to check whether mail is waiting for these.
753 ;; The default is to use pop3.el.
755 (let ((pop3-password password)
757 (pop3-mailhost server)
759 (pop3-authentication-scheme
760 (if (eq authentication 'apop) 'apop 'pass)))
761 (if (or debug-on-quit debug-on-error)
762 (save-excursion (pop3-get-message-count))
764 (save-excursion (pop3-get-message-count))
766 ;; We nix out the password in case the error
767 ;; was because of a wrong password being given.
768 (setq mail-source-password-cache
769 (delq (assoc from mail-source-password-cache)
770 mail-source-password-cache))
771 (signal (car err) (cdr err)))))))))
773 ;; Inform display-time that we have new mail.
774 (setq mail-source-new-mail-available (> result 0))
775 ;; We nix out the password in case the error
776 ;; was because of a wrong password being given.
777 (setq mail-source-password-cache
778 (delq (assoc from mail-source-password-cache)
779 mail-source-password-cache)))
782 (defun mail-source-touch-pop ()
783 "Open and close a POP connection shortly.
784 POP server should be defined in `mail-source-primary-source' (which is
785 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
786 authentication. To do that, you need to set the option
787 `message-send-mail-function' to `message-send-mail-with-smtp' or
788 `message-smtpmail-send-it' and put the following line in .gnus file:
790 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
792 (let ((sources (if mail-source-primary-source
793 (list mail-source-primary-source)
796 (if (eq 'pop (car (car sources)))
797 (mail-source-check-pop (car sources)))
798 (setq sources (cdr sources)))))
800 (defun mail-source-new-mail-p ()
801 "Handler for `display-time' to indicate when new mail is available."
802 ;; Flash (ie. ring the visible bell) if mail is available.
803 (if (and mail-source-flash mail-source-new-mail-available)
804 (let ((visible-bell t))
806 ;; Only report flag setting; flag is updated on a different schedule.
807 mail-source-new-mail-available)
810 (defvar mail-source-report-new-mail nil)
811 (defvar mail-source-report-new-mail-timer nil)
812 (defvar mail-source-report-new-mail-idle-timer nil)
815 (if (featurep 'xemacs)
819 (defun mail-source-start-idle-timer ()
820 ;; Start our idle timer if necessary, so we delay the check until the
821 ;; user isn't typing.
822 (unless mail-source-report-new-mail-idle-timer
823 (setq mail-source-report-new-mail-idle-timer
825 mail-source-idle-time-delay
829 (mail-source-check-pop mail-source-primary-source)
830 (setq mail-source-report-new-mail-idle-timer nil)))))
831 ;; Since idle timers created when Emacs is already in the idle
832 ;; state don't get activated until Emacs _next_ becomes idle, we
833 ;; need to force our timer to be considered active now. We do
834 ;; this by being naughty and poking the timer internals directly
835 ;; (element 0 of the vector is nil if the timer is active).
836 (aset mail-source-report-new-mail-idle-timer 0 nil)))
838 (defun mail-source-report-new-mail (arg)
839 "Toggle whether to report when new mail is available.
840 This only works when `display-time' is enabled."
842 (if (not mail-source-primary-source)
843 (error "Need to set `mail-source-primary-source' to check for new mail"))
844 (let ((on (if (null arg)
845 (not mail-source-report-new-mail)
846 (> (prefix-numeric-value arg) 0))))
847 (setq mail-source-report-new-mail on)
848 (and mail-source-report-new-mail-timer
849 (nnheader-cancel-timer mail-source-report-new-mail-timer))
850 (and mail-source-report-new-mail-idle-timer
851 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
852 (setq mail-source-report-new-mail-timer nil)
853 (setq mail-source-report-new-mail-idle-timer nil)
857 ;; display-time-mail-function is an Emacs 21 feature.
858 (setq display-time-mail-function #'mail-source-new-mail-p)
859 ;; Set up the main timer.
860 (setq mail-source-report-new-mail-timer
861 (nnheader-run-at-time
862 (* 60 mail-source-report-new-mail-interval)
863 (* 60 mail-source-report-new-mail-interval)
864 #'mail-source-start-idle-timer))
865 ;; When you get new mail, clear "Mail" from the mode line.
866 (add-hook 'nnmail-post-get-new-mail-hook
867 'display-time-event-handler)
868 (message "Mail check enabled"))
869 (setq display-time-mail-function nil)
870 (remove-hook 'nnmail-post-get-new-mail-hook
871 'display-time-event-handler)
872 (message "Mail check disabled"))))
874 (defun mail-source-fetch-maildir (source callback)
875 "Fetcher for maildir sources."
876 (mail-source-bind (maildir source)
879 (unless (string-match "/$" path)
880 (setq path (concat path "/")))
881 (dolist (subdir subdirs)
882 (when (file-directory-p (concat path subdir))
883 (setq mail-source-string (format "maildir:%s%s" path subdir))
884 (dolist (file (directory-files (concat path subdir) t))
885 (when (and (not (file-directory-p file))
887 (funcall function file mail-source-crash-box)
888 (let ((coding-system-for-write
889 nnheader-text-coding-system)
890 (coding-system-for-read
891 nnheader-text-coding-system)
892 (output-coding-system
893 nnheader-text-coding-system)
895 nnheader-text-coding-system))
896 (with-temp-file mail-source-crash-box
897 (insert-file-contents file)
898 (goto-char (point-min))
899 ;;; ;; Unix mail format
900 ;;; (unless (looking-at "\n*From ")
901 ;;; (insert "From maildir "
902 ;;; (current-time-string) "\n"))
903 ;;; (while (re-search-forward "^From " nil t)
904 ;;; (replace-match ">From "))
905 ;;; (goto-char (point-max))
908 (insert "\001\001\001\001\n"))
909 (delete-file file)))))
910 (incf found (mail-source-callback callback file))))))
914 (autoload 'imap-open "imap")
915 (autoload 'imap-authenticate "imap")
916 (autoload 'imap-mailbox-select "imap")
917 (autoload 'imap-mailbox-unselect "imap")
918 (autoload 'imap-mailbox-close "imap")
919 (autoload 'imap-search "imap")
920 (autoload 'imap-fetch "imap")
921 (autoload 'imap-close "imap")
922 (autoload 'imap-error-text "imap")
923 (autoload 'imap-message-flags-add "imap")
924 (autoload 'imap-list-to-message-set "imap")
925 (autoload 'imap-range-to-message-set "imap"))
927 (defvar mail-source-imap-file-coding-system 'binary
928 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
930 (defun mail-source-fetch-imap (source callback)
931 "Fetcher for imap sources."
932 (mail-source-bind (imap source)
933 (let ((from (format "%s:%s:%s" server user port))
935 (buf (get-buffer-create
936 (format " *imap source %s:%s:%s *" server user mailbox)))
937 (mail-source-string (format "imap:%s:%s" server mailbox))
938 (imap-shell-program (or (list program) imap-shell-program))
940 (if (and (imap-open server port stream authentication buf)
942 user (or (cdr (assoc from mail-source-password-cache))
944 (imap-mailbox-select mailbox nil buf))
945 (let ((coding-system-for-write mail-source-imap-file-coding-system)
946 (output-coding-system mail-source-imap-file-coding-system)
948 (with-temp-file mail-source-crash-box
949 ;; Avoid converting 8-bit chars from inserted strings to
951 (set-buffer-multibyte nil)
953 (with-current-buffer buf
954 (when (or imap-password
955 (assoc from mail-source-password-cache))
956 (push (cons from imap-password) mail-source-password-cache)))
957 ;; if predicate is nil, use all uids
958 (dolist (uid (imap-search (or predicate "1:*") buf))
960 (if (imap-capability 'IMAP4rev1 buf)
961 (caddar (imap-fetch uid "BODY.PEEK[]"
962 'BODYDETAIL nil buf))
963 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
965 (insert "From imap " (current-time-string) "\n")
968 (while (re-search-forward "^From " nil t)
969 (replace-match ">From "))
970 (goto-char (point-max))))
971 (nnheader-ms-strip-cr))
972 (incf found (mail-source-callback callback server))
973 (when (and remove fetchflag)
974 (imap-message-flags-add
975 (imap-range-to-message-set (gnus-compress-sequence remove))
978 (imap-mailbox-unselect buf)
979 (imap-mailbox-close nil buf))
982 ;; We nix out the password in case the error
983 ;; was because of a wrong password being given.
984 (setq mail-source-password-cache
985 (delq (assoc from mail-source-password-cache)
986 mail-source-password-cache))
987 (error (imap-error-text buf)))
992 (autoload 'webmail-fetch "webmail"))
994 (defun mail-source-fetch-webmail (source callback)
995 "Fetch for webmail source."
996 (mail-source-bind (webmail source)
997 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
998 (webmail-newmail-only dontexpunge)
999 (webmail-move-to-trash-can (not dontexpunge)))
1000 (when (eq authentication 'password)
1003 (cdr (assoc (format "webmail:%s:%s" subtype user)
1004 mail-source-password-cache))
1005 (mail-source-read-passwd
1006 (format "Password for %s at %s: " user subtype))))
1008 (not (assoc (format "webmail:%s:%s" subtype user)
1009 mail-source-password-cache)))
1010 (push (cons (format "webmail:%s:%s" subtype user) password)
1011 mail-source-password-cache)))
1012 (webmail-fetch mail-source-crash-box subtype user password)
1013 (mail-source-callback callback (symbol-name subtype)))))
1015 (provide 'mail-source)
1017 ;;; mail-source.el ends here