Synch with Gnus.
[elisp/gnus.git-] / lisp / mail-source.el
index 64a9af8..71a73a3 100644 (file)
 
 ;;; Code:
 
-(eval-when-compile (require 'cl))
-(require 'nnheader)
+(eval-when-compile
+  (require 'cl)
+  (require 'imap)
+  (eval-when-compile (defvar display-time-mail-function)))
 (eval-and-compile
   (defvar pop3-leave-mail-on-server)
   (autoload 'pop3-movemail "pop3")
-  (autoload 'pop3-get-message-count "pop3"))
+  (autoload 'pop3-get-message-count "pop3")
+  (autoload 'nnheader-cancel-timer "nnheader")
+  (autoload 'nnheader-run-at-time "nnheader"))
 (require 'format-spec)
 
 (defgroup mail-source nil
   "The mail-fetching library."
   :group 'gnus)
 
+;; Define these at compile time to avoid dragging in imap always.
+(defconst mail-source-imap-authenticators
+  (eval-when-compile
+    (mapcar (lambda (a)
+             (list 'const (car a)))
+     imap-authenticator-alist)))
+(defconst mail-source-imap-streams
+  (eval-when-compile
+    (mapcar (lambda (a)
+             (list 'const (car a)))
+     imap-stream-alist)))
+
 (defcustom mail-sources nil
   "*Where the mail backends will look for incoming mail.
 This variable is a list of mail source specifiers.
@@ -142,33 +158,25 @@ See Info node `(gnus)Mail Source Specifiers'."
                                          (const :format "" :value :stream)
                                          (choice :tag "Stream"
                                                  :value network
-                                                 ,@(progn (require 'imap)
-                                                          (mapcar
-                                                           (lambda (a)
-                                                             (list 'const
-                                                                   (car a)))
-                                                           imap-stream-alist
-                                                           ))))
+                                                 ,@mail-source-imap-streams))
                                   (group :inline t
                                          (const :format ""
                                                 :value :authenticator)
                                          (choice :tag "Authenticator"
                                                  :value login
-                                                 ,@(progn (require 'imap)
-                                                          (mapcar
-                                                           (lambda (a)
-                                                             (list 'const
-                                                                   (car a)))
-                                                           imap-authenticator-alist))))
+                                                 ,@mail-source-imap-authenticators))
                                   (group :inline t
                                          (const :format "" :value :mailbox)
-                                         (string :tag "Mailbox"))
+                                         (string :tag "Mailbox"
+                                                 :value "INBOX"))
                                   (group :inline t
                                          (const :format "" :value :predicate)
-                                         (function :tag "Predicate"))
+                                         (string :tag "Predicate" 
+                                                 :value "UNSEEN UNDELETED"))
                                   (group :inline t
                                          (const :format "" :value :fetchflag)
-                                         (string :tag "Fetchflag"))
+                                         (string :tag "Fetchflag"
+                                                 :value  "\\Deleted"))
                                   (group :inline t
                                          (const :format ""
                                                 :value :dontexpunge)
@@ -655,7 +663,15 @@ If ARGS, PROMPT is used as an argument to `format'."
                     (or leave
                         (and (boundp 'pop3-leave-mail-on-server)
                              pop3-leave-mail-on-server))))
-               (save-excursion (pop3-movemail mail-source-crash-box))))))
+               (condition-case err
+                   (save-excursion (pop3-movemail mail-source-crash-box))
+                 (error
+                  ;; We nix out the password in case the error
+                  ;; was because of a wrong password being given.
+                  (setq mail-source-password-cache
+                        (delq (assoc from mail-source-password-cache)
+                              mail-source-password-cache))
+                  (signal (car err) (cdr err))))))))
       (if result
          (progn
            (when (eq authentication 'password)
@@ -706,7 +722,15 @@ If ARGS, PROMPT is used as an argument to `format'."
                    (pop3-port port)
                    (pop3-authentication-scheme
                     (if (eq authentication 'apop) 'apop 'pass)))
-               (save-excursion (pop3-get-message-count))))))
+               (condition-case err
+                   (save-excursion (pop3-get-message-count))
+                 (error
+                  ;; We nix out the password in case the error
+                  ;; was because of a wrong password being given.
+                  (setq mail-source-password-cache
+                        (delq (assoc from mail-source-password-cache)
+                              mail-source-password-cache))
+                  (signal (car err) (cdr err))))))))
       (if result
          ;; Inform display-time that we have new mail.
          (setq mail-source-new-mail-available (> result 0))
@@ -741,8 +765,8 @@ If ARGS, PROMPT is used as an argument to `format'."
           mail-source-idle-time-delay
           nil
           (lambda ()
-            (setq mail-source-report-new-mail-idle-timer nil)
-            (mail-source-check-pop mail-source-primary-source))))
+            (mail-source-check-pop mail-source-primary-source)
+            (setq mail-source-report-new-mail-idle-timer nil))))
     ;; Since idle timers created when Emacs is already in the idle
     ;; state don't get activated until Emacs _next_ becomes idle, we
     ;; need to force our timer to be considered active now.  We do
@@ -773,8 +797,10 @@ This only works when `display-time' is enabled."
          (setq display-time-mail-function #'mail-source-new-mail-p)
          ;; Set up the main timer.
          (setq mail-source-report-new-mail-timer
-               (run-at-time t (* 60 mail-source-report-new-mail-interval)
-                            #'mail-source-start-idle-timer))
+               (nnheader-run-at-time
+                (* 60 mail-source-report-new-mail-interval)
+                (* 60 mail-source-report-new-mail-interval)
+                #'mail-source-start-idle-timer))
          ;; When you get new mail, clear "Mail" from the mode line.
          (add-hook 'nnmail-post-get-new-mail-hook
                    'display-time-event-handler)