1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
32 (defvar display-time-mail-function))
34 (autoload 'pop3-movemail "pop3")
35 (autoload 'pop3-get-message-count "pop3")
36 (autoload 'nnheader-cancel-timer "nnheader"))
37 (require 'format-spec)
38 (require 'message) ;; for `message-directory'
40 (defgroup mail-source nil
41 "The mail-fetching library."
45 ;; Define these at compile time to avoid dragging in imap always.
46 (defconst mail-source-imap-authenticators
49 (list 'const (car a)))
50 imap-authenticator-alist)))
51 (defconst mail-source-imap-streams
54 (list 'const (car a)))
57 (defcustom mail-sources nil
58 "*Where the mail backends will look for incoming mail.
59 This variable is a list of mail source specifiers.
60 See Info node `(gnus)Mail Source Specifiers'."
62 :link '(custom-manual "(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 (choice :tag "Prescript"
89 (function :format "%v")))
91 (const :format "" :value :postscript)
92 (choice :tag "Postscript"
95 (function :format "%v")))
97 (const :format "" :value :plugged)
98 (boolean :tag "Plugged"))))
99 (cons :tag "POP3 server"
100 (const :format "" pop)
101 (checklist :tag "Options" :greedy t
103 (const :format "" :value :server)
104 (string :tag "Server"))
106 (const :format "" :value :port)
109 (number :format "%v")
110 (string :format "%v")))
112 (const :format "" :value :user)
113 (string :tag "User"))
115 (const :format "" :value :password)
116 (string :tag "Password"))
118 (const :format "" :value :program)
119 (string :tag "Program"))
121 (const :format "" :value :prescript)
122 (choice :tag "Prescript"
124 (string :format "%v")
125 (function :format "%v")))
127 (const :format "" :value :postscript)
128 (choice :tag "Postscript"
130 (string :format "%v")
131 (function :format "%v")))
133 (const :format "" :value :function)
134 (function :tag "Function"))
137 :value :authentication)
138 (choice :tag "Authentication"
143 (const :format "" :value :plugged)
144 (boolean :tag "Plugged"))))
145 (cons :tag "Maildir (qmail, postfix...)"
146 (const :format "" maildir)
147 (checklist :tag "Options" :greedy t
149 (const :format "" :value :path)
150 (directory :tag "Path"))
152 (const :format "" :value :plugged)
153 (boolean :tag "Plugged"))))
154 (cons :tag "IMAP server"
155 (const :format "" imap)
156 (checklist :tag "Options" :greedy t
158 (const :format "" :value :server)
159 (string :tag "Server"))
161 (const :format "" :value :port)
166 (const :format "" :value :user)
167 (string :tag "User"))
169 (const :format "" :value :password)
170 (string :tag "Password"))
172 (const :format "" :value :stream)
173 (choice :tag "Stream"
175 ,@mail-source-imap-streams))
177 (const :format "" :value :program)
178 (string :tag "Program"))
181 :value :authenticator)
182 (choice :tag "Authenticator"
184 ,@mail-source-imap-authenticators))
186 (const :format "" :value :mailbox)
187 (string :tag "Mailbox"
190 (const :format "" :value :predicate)
191 (string :tag "Predicate"
192 :value "UNSEEN UNDELETED"))
194 (const :format "" :value :fetchflag)
195 (string :tag "Fetchflag"
200 (boolean :tag "Dontexpunge"))
202 (const :format "" :value :plugged)
203 (boolean :tag "Plugged"))))
204 (cons :tag "Webmail server"
205 (const :format "" webmail)
206 (checklist :tag "Options" :greedy t
208 (const :format "" :value :subtype)
209 ;; Should be generated from
210 ;; `webmail-type-definition', but we
211 ;; can't require webmail without W3.
212 (choice :tag "Subtype"
220 (const :format "" :value :user)
221 (string :tag "User"))
223 (const :format "" :value :password)
224 (string :tag "Password"))
228 (boolean :tag "Dontexpunge"))
230 (const :format "" :value :plugged)
231 (boolean :tag "Plugged")))))))
233 (defcustom mail-source-ignore-errors nil
234 "*Ignore errors when querying mail sources.
235 If nil, the user will be prompted when an error occurs. If non-nil,
236 the error will be ignored.")
238 (defcustom mail-source-primary-source nil
239 "*Primary source for incoming mail.
240 If non-nil, this maildrop will be checked periodically for new mail."
244 (defcustom mail-source-flash t
245 "*If non-nil, flash periodically when mail is available."
249 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
250 "File where mail will be stored while processing it."
254 (defcustom mail-source-directory message-directory
255 "Directory where incoming mail source files (if any) will be stored."
259 (defcustom mail-source-default-file-modes 384
260 "Set the mode bits of all new mail files to this integer."
264 (defcustom mail-source-delete-incoming nil
265 "*If non-nil, delete incoming files after handling.
266 If t, delete immediately, if nil, never delete. If a positive number, delete
267 files older than number of days."
268 ;; Note: The removing happens in `mail-source-callback', i.e. no old
269 ;; incoming files will be deleted, unless you receive new mail.
271 ;; You may also set this to `nil' and call `mail-source-delete-old-incoming'
272 ;; from a hook or interactively.
274 :type '(choice (const :tag "immediately" t)
275 (const :tag "never" nil)
276 (integer :tag "days")))
278 (defcustom mail-source-delete-old-incoming-confirm t
279 "*If non-nil, ask for for confirmation before deleting old incoming files.
280 This variable only applies when `mail-source-delete-incoming' is a positive
285 (defcustom mail-source-incoming-file-prefix "Incoming"
286 "Prefix for file name for storing incoming mail"
290 (defcustom mail-source-report-new-mail-interval 5
291 "Interval in minutes between checks for new mail."
295 (defcustom mail-source-idle-time-delay 5
296 "Number of idle seconds to wait before checking for new mail."
300 (defcustom mail-source-movemail-program nil
301 "If non-nil, name of program for fetching new mail."
303 :type '(choice (const nil) string))
305 ;;; Internal variables.
307 (defvar mail-source-string ""
308 "A dynamically bound string that says what the current mail source is.")
310 (defvar mail-source-new-mail-available nil
311 "Flag indicating when new mail is available.")
314 (defvar mail-source-common-keyword-map
316 "Mapping from keywords to default values.
317 Common keywords should be listed here.")
319 (defvar mail-source-keyword-map
324 (:path (or (getenv "MAIL")
325 (expand-file-name (user-login-name) rmail-spool-directory))))
332 (:predicate identity))
337 (:server (getenv "MAILHOST"))
339 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
344 (:authentication password)
347 (:path (or (getenv "MAILDIR") "~/Maildir/"))
348 (:subdirs ("cur" "new"))
351 (:server (getenv "MAILHOST"))
356 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
359 (:predicate "UNSEEN UNDELETED")
360 (:fetchflag "\\Deleted")
367 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
370 (:authentication password)))
371 "Mapping from keywords to default values.
372 All keywords that can be used must be listed here."))
374 (defvar mail-source-fetcher-alist
375 '((file mail-source-fetch-file)
376 (directory mail-source-fetch-directory)
377 (pop mail-source-fetch-pop)
378 (maildir mail-source-fetch-maildir)
379 (imap mail-source-fetch-imap)
380 (webmail mail-source-fetch-webmail))
381 "A mapping from source type to fetcher function.")
383 (defvar mail-source-password-cache nil)
385 (defvar mail-source-plugged t)
390 (defun mail-source-strip-keyword (keyword)
391 "Strip the leading colon off the KEYWORD."
392 (intern (substring (symbol-name keyword) 1))))
395 (defun mail-source-bind-1 (type)
396 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
398 (while (setq default (pop defaults))
399 (push (list (mail-source-strip-keyword (car default))
404 (defmacro mail-source-bind (type-source &rest body)
405 "Return a `let' form that binds all variables in source TYPE.
406 TYPE-SOURCE is a list where the first element is the TYPE, and
407 the second variable is the SOURCE.
408 At run time, the mail source specifier SOURCE will be inspected,
409 and the variables will be set according to it. Variables not
410 specified will be given default values.
412 After this is done, BODY will be executed in the scope
415 The variables bound and their default values are described by
416 the `mail-source-keyword-map' variable."
417 `(let ,(mail-source-bind-1 (car type-source))
418 (mail-source-set-1 ,(cadr type-source))
421 (put 'mail-source-bind 'lisp-indent-function 1)
422 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
424 (defun mail-source-set-1 (source)
425 (let* ((type (pop source))
426 (defaults (cdr (assq type mail-source-keyword-map)))
427 default value keyword)
428 (while (setq default (pop defaults))
429 (set (mail-source-strip-keyword (setq keyword (car default)))
430 (if (setq value (plist-get source keyword))
431 (mail-source-value value)
432 (mail-source-value (cadr default)))))))
435 (defun mail-source-bind-common-1 ()
436 (let* ((defaults mail-source-common-keyword-map)
438 (while (setq default (pop defaults))
439 (push (list (mail-source-strip-keyword (car default))
444 (defun mail-source-set-common-1 (source)
445 (let* ((type (pop source))
446 (defaults mail-source-common-keyword-map)
447 (defaults-1 (cdr (assq type mail-source-keyword-map)))
448 default value keyword)
449 (while (setq default (pop defaults))
450 (set (mail-source-strip-keyword (setq keyword (car default)))
451 (if (setq value (plist-get source keyword))
452 (mail-source-value value)
453 (if (setq value (assq keyword defaults-1))
454 (mail-source-value (cadr value))
455 (mail-source-value (cadr default))))))))
457 (defmacro mail-source-bind-common (source &rest body)
458 "Return a `let' form that binds all common variables.
459 See `mail-source-bind'."
460 `(let ,(mail-source-bind-common-1)
461 (mail-source-set-common-1 source)
464 (put 'mail-source-bind-common 'lisp-indent-function 1)
465 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
467 (defun mail-source-value (value)
468 "Return the value of VALUE."
475 (functionp (car value)))
477 ;; Just return the value.
481 (defun mail-source-fetch (source callback)
482 "Fetch mail from SOURCE and call CALLBACK zero or more times.
483 CALLBACK will be called with the name of the file where (some of)
484 the mail from SOURCE is put.
485 Return the number of files that were found."
486 (mail-source-bind-common source
487 (if (or mail-source-plugged plugged)
489 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
492 (error "%S is an invalid mail source specification" source))
493 ;; If there's anything in the crash box, we do it first.
494 (when (file-exists-p mail-source-crash-box)
495 (message "Processing mail from %s..." mail-source-crash-box)
496 (setq found (mail-source-callback
497 callback mail-source-crash-box))
498 (mail-source-delete-crash-box))
500 (if (or debug-on-quit debug-on-error)
501 (funcall function source callback)
503 (funcall function source callback)
505 (if (and (not mail-source-ignore-errors)
508 (format "Mail source %s error (%s). Continue? "
509 (if (memq ':password source)
510 (let ((s (copy-sequence source)))
511 (setcar (cdr (memq ':password s))
516 (error "Cannot get new mail"))
519 (defun mail-source-delete-old-incoming (&optional age confirm)
520 "Remove incoming files older than AGE days.
521 If CONFIRM is non-nil, ask for confirmation before removing a file."
523 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
524 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
525 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
527 (setq files (directory-files
528 mail-source-directory t
529 (concat mail-source-incoming-file-prefix "*"))
530 currday (* (car (current-time)) high2days)
531 currday (+ currday (* low2days (nth 1 (current-time)))))
533 (let* ((ffile (car files))
534 (bfile (gnus-replace-in-string
535 ffile "\\`.*/\\([^/]+\\)\\'" "\\1"))
536 (filetime (nth 5 (file-attributes ffile)))
537 (fileday (* (car filetime) high2days))
538 (fileday (+ fileday (* low2days (nth 1 filetime)))))
539 (setq files (cdr files))
540 (when (and (> (- currday fileday) diff)
541 (gnus-message 8 "File `%s' is older than %s day(s)"
544 (y-or-n-p (concat "Remove file `" bfile "'? "))))
545 (delete-file ffile))))))
547 (defun mail-source-callback (callback info)
548 "Call CALLBACK on the mail file. Pass INFO on to CALLBACK."
549 (if (or (not (file-exists-p mail-source-crash-box))
550 (zerop (nth 7 (file-attributes mail-source-crash-box))))
552 (when (file-exists-p mail-source-crash-box)
553 (delete-file mail-source-crash-box))
555 (funcall callback mail-source-crash-box info)))
557 (defun mail-source-delete-crash-box ()
558 (when (file-exists-p mail-source-crash-box)
559 ;; Delete or move the incoming mail out of the way.
560 (if (eq mail-source-delete-incoming t)
561 (delete-file mail-source-crash-box)
565 mail-source-incoming-file-prefix
566 mail-source-directory))))
567 (unless (file-exists-p (file-name-directory incoming))
568 (make-directory (file-name-directory incoming) t))
569 (rename-file mail-source-crash-box incoming t)
570 ;; remove old incoming files?
571 (when (natnump mail-source-delete-incoming)
572 (mail-source-delete-old-incoming
573 mail-source-delete-incoming
574 mail-source-delete-old-incoming-confirm))))))
576 (defun mail-source-movemail (from to)
577 "Move FROM to TO using movemail."
578 (if (not (file-writable-p to))
579 (error "Can't write to crash box %s. Not moving mail" to)
580 (let ((to (file-truename (expand-file-name to)))
582 (setq to (file-truename to)
583 from (file-truename from))
584 ;; Set TO if have not already done so, and rename or copy
585 ;; the file FROM to TO if and as appropriate.
588 ;; The crash box exists already.
590 ((not (file-exists-p from))
591 ;; There is no inbox.
593 ((zerop (nth 7 (file-attributes from)))
597 ;; If getting from mail spool directory, use movemail to move
598 ;; rather than just renaming, so as to interlock with the
602 (setq errors (generate-new-buffer " *mail source loss*"))
603 (let ((default-directory "/"))
609 (or mail-source-movemail-program
610 (expand-file-name "movemail" exec-directory))
611 nil errors nil from to)))))
612 (when (file-exists-p to)
613 (set-file-modes to mail-source-default-file-modes))
614 (if (and (or (not (buffer-modified-p errors))
615 (zerop (buffer-size errors)))
616 (and (numberp result)
618 ;; No output => movemail won.
621 ;; There may be a warning about older revisions. We
623 (goto-char (point-min))
624 (if (search-forward "older revision" nil t)
626 ;; Probably a real error.
627 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
628 (goto-char (point-max))
629 (skip-chars-backward " \t")
630 (delete-region (point) (point-max))
631 (goto-char (point-min))
632 (when (looking-at "movemail: ")
633 (delete-region (point-min) (match-end 0)))
634 ;; Result may be a signal description string.
636 (format "movemail: %s (%s return). Continue? "
637 (buffer-string) result))
638 (error "%s" (buffer-string)))
641 (buffer-name errors))
642 (kill-buffer errors))
643 ;; Return whether we moved successfully or not.
646 (defun mail-source-movemail-and-remove (from to)
647 "Move FROM to TO using movemail, then remove FROM if empty."
648 (or (not (mail-source-movemail from to))
649 (not (zerop (nth 7 (file-attributes from))))
652 (defun mail-source-fetch-with-program (program)
653 (eq 0 (call-process shell-file-name nil nil nil
654 shell-command-switch program)))
656 (defun mail-source-run-script (script spec &optional delay)
658 (if (functionp script)
660 (mail-source-call-script
661 (format-spec script spec))))
665 (defun mail-source-call-script (script)
666 (let ((background nil))
667 (when (string-match "& *$" script)
668 (setq script (substring script 0 (match-beginning 0))
670 (call-process shell-file-name nil background nil
671 shell-command-switch script)))
674 ;;; Different fetchers
677 (defun mail-source-fetch-file (source callback)
678 "Fetcher for single-file sources."
679 (mail-source-bind (file source)
680 (mail-source-run-script
681 prescript (format-spec-make ?t mail-source-crash-box)
683 (let ((mail-source-string (format "file:%s" path)))
684 (if (mail-source-movemail path mail-source-crash-box)
686 (mail-source-callback callback path)
687 (mail-source-run-script
688 postscript (format-spec-make ?t mail-source-crash-box))
689 (mail-source-delete-crash-box))
692 (defun mail-source-fetch-directory (source callback)
693 "Fetcher for directory sources."
694 (mail-source-bind (directory source)
695 (mail-source-run-script
696 prescript (format-spec-make ?t path) prescript-delay)
698 (mail-source-string (format "directory:%s" path)))
699 (dolist (file (directory-files
700 path t (concat (regexp-quote suffix) "$")))
701 (when (and (file-regular-p file)
702 (funcall predicate file)
703 (mail-source-movemail file mail-source-crash-box))
704 (incf found (mail-source-callback callback file))
705 (mail-source-run-script postscript (format-spec-make ?t path))
706 (mail-source-delete-crash-box)))
709 (defun mail-source-fetch-pop (source callback)
710 "Fetcher for single-file sources."
711 (mail-source-bind (pop source)
712 (mail-source-run-script
714 (format-spec-make ?p password ?t mail-source-crash-box
715 ?s server ?P port ?u user)
717 (let ((from (format "%s:%s:%s" server user port))
718 (mail-source-string (format "pop:%s@%s" user server))
720 (when (eq authentication 'password)
723 (cdr (assoc from mail-source-password-cache))
725 (format "Password for %s at %s: " user server)))))
727 (setenv "MAILHOST" server))
731 (mail-source-fetch-with-program
734 (format-spec-make ?p password ?t mail-source-crash-box
735 ?s server ?P port ?u user))))
737 (funcall function mail-source-crash-box))
738 ;; The default is to use pop3.el.
741 (let ((pop3-password password)
743 (pop3-mailhost server)
745 (pop3-authentication-scheme
746 (if (eq authentication 'apop) 'apop 'pass))
747 (pop3-connection-type connection)
748 (pop3-leave-mail-on-server
750 (and (boundp 'pop3-leave-mail-on-server)
751 (symbol-value 'pop3-leave-mail-on-server)))))
752 (if (or debug-on-quit debug-on-error)
753 (save-excursion (pop3-movemail mail-source-crash-box))
755 (save-excursion (pop3-movemail mail-source-crash-box))
757 ;; We nix out the password in case the error
758 ;; was because of a wrong password being given.
759 (setq mail-source-password-cache
760 (delq (assoc from mail-source-password-cache)
761 mail-source-password-cache))
762 (signal (car err) (cdr err)))))))))
765 (when (eq authentication 'password)
766 (unless (assoc from mail-source-password-cache)
767 (push (cons from password) mail-source-password-cache)))
769 (mail-source-callback callback server)
770 ;; Update display-time's mail flag, if relevant.
771 (if (equal source mail-source-primary-source)
772 (setq mail-source-new-mail-available nil))
773 (mail-source-run-script
775 (format-spec-make ?p password ?t mail-source-crash-box
776 ?s server ?P port ?u user))
777 (mail-source-delete-crash-box)))
778 ;; We nix out the password in case the error
779 ;; was because of a wrong password being given.
780 (setq mail-source-password-cache
781 (delq (assoc from mail-source-password-cache)
782 mail-source-password-cache))
785 (defun mail-source-check-pop (source)
786 "Check whether there is new mail."
787 (mail-source-bind (pop source)
788 (let ((from (format "%s:%s:%s" server user port))
789 (mail-source-string (format "pop:%s@%s" user server))
791 (when (eq authentication 'password)
794 (cdr (assoc from mail-source-password-cache))
796 (format "Password for %s at %s: " user server))))
797 (unless (assoc from mail-source-password-cache)
798 (push (cons from password) mail-source-password-cache)))
800 (setenv "MAILHOST" server))
803 ;; No easy way to check whether mail is waiting for these.
806 ;; The default is to use pop3.el.
809 (let ((pop3-password password)
811 (pop3-mailhost server)
813 (pop3-authentication-scheme
814 (if (eq authentication 'apop) 'apop 'pass)))
815 (if (or debug-on-quit debug-on-error)
816 (save-excursion (pop3-get-message-count))
818 (save-excursion (pop3-get-message-count))
820 ;; We nix out the password in case the error
821 ;; was because of a wrong password being given.
822 (setq mail-source-password-cache
823 (delq (assoc from mail-source-password-cache)
824 mail-source-password-cache))
825 (signal (car err) (cdr err)))))))))
827 ;; Inform display-time that we have new mail.
828 (setq mail-source-new-mail-available (> result 0))
829 ;; We nix out the password in case the error
830 ;; was because of a wrong password being given.
831 (setq mail-source-password-cache
832 (delq (assoc from mail-source-password-cache)
833 mail-source-password-cache)))
836 (defun mail-source-touch-pop ()
837 "Open and close a POP connection shortly.
838 POP server should be defined in `mail-source-primary-source' (which is
839 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
840 authentication. To do that, you need to set the
841 `message-send-mail-function' variable as `message-send-mail-with-smtp'
842 or `message-smtpmail-send-it' and put the following line in your
845 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
847 See the Gnus manual for details."
848 (let ((sources (if mail-source-primary-source
849 (list mail-source-primary-source)
852 (if (eq 'pop (car (car sources)))
853 (mail-source-check-pop (car sources)))
854 (setq sources (cdr sources)))))
856 (defun mail-source-new-mail-p ()
857 "Handler for `display-time' to indicate when new mail is available."
858 ;; Flash (ie. ring the visible bell) if mail is available.
859 (if (and mail-source-flash mail-source-new-mail-available)
860 (let ((visible-bell t))
862 ;; Only report flag setting; flag is updated on a different schedule.
863 mail-source-new-mail-available)
866 (defvar mail-source-report-new-mail nil)
867 (defvar mail-source-report-new-mail-timer nil)
868 (defvar mail-source-report-new-mail-idle-timer nil)
871 (if (featurep 'xemacs)
875 (defun mail-source-start-idle-timer ()
876 ;; Start our idle timer if necessary, so we delay the check until the
877 ;; user isn't typing.
878 (unless mail-source-report-new-mail-idle-timer
879 (setq mail-source-report-new-mail-idle-timer
881 mail-source-idle-time-delay
885 (mail-source-check-pop mail-source-primary-source)
886 (setq mail-source-report-new-mail-idle-timer nil)))))
887 ;; Since idle timers created when Emacs is already in the idle
888 ;; state don't get activated until Emacs _next_ becomes idle, we
889 ;; need to force our timer to be considered active now. We do
890 ;; this by being naughty and poking the timer internals directly
891 ;; (element 0 of the vector is nil if the timer is active).
892 (aset mail-source-report-new-mail-idle-timer 0 nil)))
894 (defun mail-source-report-new-mail (arg)
895 "Toggle whether to report when new mail is available.
896 This only works when `display-time' is enabled."
898 (if (not mail-source-primary-source)
899 (error "Need to set `mail-source-primary-source' to check for new mail"))
900 (let ((on (if (null arg)
901 (not mail-source-report-new-mail)
902 (> (prefix-numeric-value arg) 0))))
903 (setq mail-source-report-new-mail on)
904 (and mail-source-report-new-mail-timer
905 (nnheader-cancel-timer mail-source-report-new-mail-timer))
906 (and mail-source-report-new-mail-idle-timer
907 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
908 (setq mail-source-report-new-mail-timer nil)
909 (setq mail-source-report-new-mail-idle-timer nil)
913 ;; display-time-mail-function is an Emacs 21 feature.
914 (setq display-time-mail-function #'mail-source-new-mail-p)
915 ;; Set up the main timer.
916 (setq mail-source-report-new-mail-timer
918 (* 60 mail-source-report-new-mail-interval)
919 (* 60 mail-source-report-new-mail-interval)
920 #'mail-source-start-idle-timer))
921 ;; When you get new mail, clear "Mail" from the mode line.
922 (add-hook 'nnmail-post-get-new-mail-hook
923 'display-time-event-handler)
924 (message "Mail check enabled"))
925 (setq display-time-mail-function nil)
926 (remove-hook 'nnmail-post-get-new-mail-hook
927 'display-time-event-handler)
928 (message "Mail check disabled"))))
930 (defun mail-source-fetch-maildir (source callback)
931 "Fetcher for maildir sources."
932 (mail-source-bind (maildir source)
935 (unless (string-match "/$" path)
936 (setq path (concat path "/")))
937 (dolist (subdir subdirs)
938 (when (file-directory-p (concat path subdir))
939 (setq mail-source-string (format "maildir:%s%s" path subdir))
940 (dolist (file (directory-files (concat path subdir) t))
941 (when (and (not (file-directory-p file))
943 (funcall function file mail-source-crash-box)
944 (let ((coding-system-for-write
945 nnheader-text-coding-system)
946 (coding-system-for-read
947 nnheader-text-coding-system))
948 (with-temp-file mail-source-crash-box
949 (insert-file-contents file)
950 (goto-char (point-min))
951 ;;; ;; Unix mail format
952 ;;; (unless (looking-at "\n*From ")
953 ;;; (insert "From maildir "
954 ;;; (current-time-string) "\n"))
955 ;;; (while (re-search-forward "^From " nil t)
956 ;;; (replace-match ">From "))
957 ;;; (goto-char (point-max))
960 (insert "\001\001\001\001\n"))
961 (delete-file file)))))
962 (incf found (mail-source-callback callback file))
963 (mail-source-delete-crash-box)))))
967 (autoload 'imap-open "imap")
968 (autoload 'imap-authenticate "imap")
969 (autoload 'imap-mailbox-select "imap")
970 (autoload 'imap-mailbox-unselect "imap")
971 (autoload 'imap-mailbox-close "imap")
972 (autoload 'imap-search "imap")
973 (autoload 'imap-fetch "imap")
974 (autoload 'imap-close "imap")
975 (autoload 'imap-error-text "imap")
976 (autoload 'imap-message-flags-add "imap")
977 (autoload 'imap-list-to-message-set "imap")
978 (autoload 'imap-range-to-message-set "imap"))
980 (defvar mail-source-imap-file-coding-system 'binary
981 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
983 (defun mail-source-fetch-imap (source callback)
984 "Fetcher for imap sources."
985 (mail-source-bind (imap source)
986 (mail-source-run-script
987 prescript (format-spec-make ?p password ?t mail-source-crash-box
988 ?s server ?P port ?u user)
990 (let ((from (format "%s:%s:%s" server user port))
992 (buf (generate-new-buffer " *imap source*"))
993 (mail-source-string (format "imap:%s:%s" server mailbox))
994 (imap-shell-program (or (list program) imap-shell-program))
996 (if (and (imap-open server port stream authentication buf)
998 user (or (cdr (assoc from mail-source-password-cache))
1000 (imap-mailbox-select mailbox nil buf))
1001 (let ((coding-system-for-write mail-source-imap-file-coding-system)
1003 (with-temp-file mail-source-crash-box
1004 ;; Avoid converting 8-bit chars from inserted strings to
1006 (set-buffer-multibyte nil)
1007 ;; remember password
1008 (with-current-buffer buf
1009 (when (and imap-password
1010 (not (assoc from mail-source-password-cache)))
1011 (push (cons from imap-password) mail-source-password-cache)))
1012 ;; if predicate is nil, use all uids
1013 (dolist (uid (imap-search (or predicate "1:*") buf))
1015 (if (imap-capability 'IMAP4rev1 buf)
1016 (caddar (imap-fetch uid "BODY.PEEK[]"
1017 'BODYDETAIL nil buf))
1018 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
1020 (insert "From imap " (current-time-string) "\n")
1022 (insert str "\n\n"))
1023 (while (re-search-forward "^From " nil t)
1024 (replace-match ">From "))
1025 (goto-char (point-max))))
1026 (nnheader-ms-strip-cr))
1027 (incf found (mail-source-callback callback server))
1028 (mail-source-delete-crash-box)
1029 (when (and remove fetchflag)
1030 (setq remove (nreverse remove))
1031 (imap-message-flags-add
1032 (imap-range-to-message-set (gnus-compress-sequence remove))
1035 (imap-mailbox-unselect buf)
1036 (imap-mailbox-close nil buf))
1039 ;; We nix out the password in case the error
1040 ;; was because of a wrong password being given.
1041 (setq mail-source-password-cache
1042 (delq (assoc from mail-source-password-cache)
1043 mail-source-password-cache))
1044 (error "IMAP error: %s" (imap-error-text buf)))
1046 (mail-source-run-script
1048 (format-spec-make ?p password ?t mail-source-crash-box
1049 ?s server ?P port ?u user))
1053 (autoload 'webmail-fetch "webmail"))
1055 (defun mail-source-fetch-webmail (source callback)
1056 "Fetch for webmail source."
1057 (mail-source-bind (webmail source)
1058 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1059 (webmail-newmail-only dontexpunge)
1060 (webmail-move-to-trash-can (not dontexpunge)))
1061 (when (eq authentication 'password)
1064 (cdr (assoc (format "webmail:%s:%s" subtype user)
1065 mail-source-password-cache))
1067 (format "Password for %s at %s: " user subtype))))
1069 (not (assoc (format "webmail:%s:%s" subtype user)
1070 mail-source-password-cache)))
1071 (push (cons (format "webmail:%s:%s" subtype user) password)
1072 mail-source-password-cache)))
1073 (webmail-fetch mail-source-crash-box subtype user password)
1074 (mail-source-callback callback (symbol-name subtype))
1075 (mail-source-delete-crash-box))))
1077 (provide 'mail-source)
1079 ;;; mail-source.el ends here