1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999-2013 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; For Emacs <22.2 and XEmacs.
29 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
31 (require 'format-spec
)
35 (autoload 'auth-source-search
"auth-source")
36 (autoload 'pop3-movemail
"pop3")
37 (autoload 'pop3-get-message-count
"pop3")
38 (autoload 'nnheader-cancel-timer
"nnheader")
40 (require 'message
) ;; for `message-directory'
42 (defvar display-time-mail-function
)
44 (defgroup mail-source nil
45 "The mail-fetching library."
49 ;; Define these at compile time to avoid dragging in imap always.
50 (defconst mail-source-imap-authenticators
53 (list 'const
(car a
)))
54 imap-authenticator-alist
)))
55 (defconst mail-source-imap-streams
58 (list 'const
(car a
)))
61 (defcustom mail-sources
'((file))
62 "Where the mail backends will look for incoming mail.
63 This variable is a list of mail source specifiers.
64 See Info node `(gnus)Mail Source Specifiers'."
67 :link
'(custom-manual "(gnus)Mail Source Specifiers")
69 (const :tag
"None" nil
)
71 (choice :format
"%[Value Menu%] %v"
73 (cons :tag
"Group parameter `mail-source'"
74 (const :format
"" group
))
75 (cons :tag
"Spool file"
76 (const :format
"" file
)
77 (checklist :tag
"Options" :greedy t
79 (const :format
"" :value
:path
)
81 (cons :tag
"Several files in a directory"
82 (const :format
"" directory
)
83 (checklist :tag
"Options" :greedy t
85 (const :format
"" :value
:path
)
86 (directory :tag
"Path"))
88 (const :format
"" :value
:suffix
)
89 (string :tag
"Suffix"))
91 (const :format
"" :value
:predicate
)
92 (function :tag
"Predicate"))
94 (const :format
"" :value
:prescript
)
95 (choice :tag
"Prescript"
98 (function :format
"%v")))
100 (const :format
"" :value
:postscript
)
101 (choice :tag
"Postscript"
103 (string :format
"%v")
104 (function :format
"%v")))
106 (const :format
"" :value
:plugged
)
107 (boolean :tag
"Plugged"))))
108 (cons :tag
"POP3 server"
109 (const :format
"" pop
)
110 (checklist :tag
"Options" :greedy t
112 (const :format
"" :value
:server
)
113 (string :tag
"Server"))
115 (const :format
"" :value
:port
)
118 (integer :format
"%v")
119 (string :format
"%v")))
121 (const :format
"" :value
:user
)
122 (string :tag
"User"))
124 (const :format
"" :value
:password
)
125 (string :tag
"Password"))
127 (const :format
"" :value
:program
)
128 (string :tag
"Program"))
130 (const :format
"" :value
:prescript
)
131 (choice :tag
"Prescript"
133 (string :format
"%v")
134 (function :format
"%v")
135 (const :tag
"None" nil
)))
137 (const :format
"" :value
:postscript
)
138 (choice :tag
"Postscript"
140 (string :format
"%v")
141 (function :format
"%v")
142 (const :tag
"None" nil
)))
144 (const :format
"" :value
:function
)
145 (function :tag
"Function"))
148 :value
:authentication
)
149 (choice :tag
"Authentication"
154 (const :format
"" :value
:plugged
)
155 (boolean :tag
"Plugged"))
157 (const :format
"" :value
:stream
)
158 (choice :tag
"Stream"
160 (const :tag
"Clear" nil
)
162 (const :tag
"SSL/TLS" ssl
)))
164 (const :format
"" :value
:leave
)
166 %{Leave mail on server%}:\n\t\t%[Value Menu%] %v"
169 Don't leave mails" nil
)
173 Leave mails for this many days" :value
14)))))
174 (cons :tag
"Maildir (qmail, postfix...)"
175 (const :format
"" maildir
)
176 (checklist :tag
"Options" :greedy t
178 (const :format
"" :value
:path
)
179 (directory :tag
"Path"))
181 (const :format
"" :value
:plugged
)
182 (boolean :tag
"Plugged"))))
183 (cons :tag
"IMAP server"
184 (const :format
"" imap
)
185 (checklist :tag
"Options" :greedy t
187 (const :format
"" :value
:server
)
188 (string :tag
"Server"))
190 (const :format
"" :value
:port
)
195 (const :format
"" :value
:user
)
196 (string :tag
"User"))
198 (const :format
"" :value
:password
)
199 (string :tag
"Password"))
201 (const :format
"" :value
:stream
)
202 (choice :tag
"Stream"
204 ,@mail-source-imap-streams
))
206 (const :format
"" :value
:program
)
207 (string :tag
"Program"))
210 :value
:authenticator
)
211 (choice :tag
"Authenticator"
213 ,@mail-source-imap-authenticators
))
215 (const :format
"" :value
:mailbox
)
216 (string :tag
"Mailbox"
219 (const :format
"" :value
:predicate
)
220 (string :tag
"Predicate"
221 :value
"UNSEEN UNDELETED"))
223 (const :format
"" :value
:fetchflag
)
224 (string :tag
"Fetchflag"
229 (boolean :tag
"Dontexpunge"))
231 (const :format
"" :value
:plugged
)
232 (boolean :tag
"Plugged"))))))))
234 (defcustom mail-source-ignore-errors nil
235 "*Ignore errors when querying mail sources.
236 If nil, the user will be prompted when an error occurs. If non-nil,
237 the error will be ignored."
242 (defcustom mail-source-primary-source nil
243 "*Primary source for incoming mail.
244 If non-nil, this maildrop will be checked periodically for new mail."
248 (defcustom mail-source-flash t
249 "*If non-nil, flash periodically when mail is available."
253 (defcustom mail-source-crash-box
"~/.emacs-mail-crash-box"
254 "File where mail will be stored while processing it."
258 (defcustom mail-source-directory message-directory
259 "Directory where incoming mail source files (if any) will be stored."
263 (defcustom mail-source-default-file-modes
384
264 "Set the mode bits of all new mail files to this integer."
268 (defcustom mail-source-delete-incoming
269 10 ;; development versions
270 ;; 2 ;; released versions
271 "If non-nil, delete incoming files after handling.
272 If t, delete immediately, if nil, never delete. If a positive number, delete
273 files older than number of days.
275 Removing of old files happens in `mail-source-callback', i.e. no
276 old incoming files will be deleted unless you receive new mail.
277 You may also set this variable to nil and call
278 `mail-source-delete-old-incoming' interactively."
280 :version
"22.2" ;; No Gnus / Gnus 5.10.10 (default changed)
281 :type
'(choice (const :tag
"immediately" t
)
282 (const :tag
"never" nil
)
283 (integer :tag
"days")))
285 (defcustom mail-source-delete-old-incoming-confirm nil
286 "If non-nil, ask for confirmation before deleting old incoming files.
287 This variable only applies when `mail-source-delete-incoming' is a positive
289 :version
"22.2" ;; No Gnus / Gnus 5.10.10 (default changed)
293 (defcustom mail-source-incoming-file-prefix
"Incoming"
294 "Prefix for file name for storing incoming mail"
298 (defcustom mail-source-report-new-mail-interval
5
299 "Interval in minutes between checks for new mail."
303 (defcustom mail-source-idle-time-delay
5
304 "Number of idle seconds to wait before checking for new mail."
308 (defcustom mail-source-movemail-program nil
309 "If non-nil, name of program for fetching new mail."
312 :type
'(choice (const nil
) string
))
314 ;;; Internal variables.
316 (defvar mail-source-string
""
317 "A dynamically bound string that says what the current mail source is.")
319 (defvar mail-source-new-mail-available nil
320 "Flag indicating when new mail is available.")
323 (defvar mail-source-common-keyword-map
325 "Mapping from keywords to default values.
326 Common keywords should be listed here.")
328 (defvar mail-source-keyword-map
333 (:path
(or (getenv "MAIL")
334 (expand-file-name (user-login-name) rmail-spool-directory
))))
341 (:predicate identity
))
346 ;; note server and port need to come before user and password
347 (:server
(getenv "MAILHOST"))
349 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
353 (:authentication password
)
357 (:path
(or (getenv "MAILDIR") "~/Maildir/"))
358 (:subdirs
("cur" "new"))
361 ;; note server and port need to come before user and password
362 (:server
(getenv "MAILHOST"))
367 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
370 (:predicate
"UNSEEN UNDELETED")
371 (:fetchflag
"\\Deleted")
376 "Mapping from keywords to default values.
377 All keywords that can be used must be listed here."))
379 (defvar mail-source-fetcher-alist
380 '((file mail-source-fetch-file
)
381 (directory mail-source-fetch-directory
)
382 (pop mail-source-fetch-pop
)
383 (maildir mail-source-fetch-maildir
)
384 (imap mail-source-fetch-imap
))
385 "A mapping from source type to fetcher function.")
387 (defvar mail-source-password-cache nil
)
389 (defvar mail-source-plugged t
)
394 (defun mail-source-strip-keyword (keyword)
395 "Strip the leading colon off the KEYWORD."
396 (intern (substring (symbol-name keyword
) 1))))
398 ;; generate a list of variable names paired with nil values
399 ;; suitable for usage in a `let' form
401 (defun mail-source-bind-1 (type)
402 (let* ((defaults (cdr (assq type mail-source-keyword-map
)))
404 (while (setq default
(pop defaults
))
405 (push (list (mail-source-strip-keyword (car default
))
410 (defmacro mail-source-bind
(type-source &rest body
)
411 "Return a `let' form that binds all variables in source TYPE.
412 TYPE-SOURCE is a list where the first element is the TYPE, and
413 the second variable is the SOURCE.
414 At run time, the mail source specifier SOURCE will be inspected,
415 and the variables will be set according to it. Variables not
416 specified will be given default values.
418 The user and password will be loaded from the auth-source values
419 if those are available. They override the original user and
420 password in a second `let' form.
422 After this is done, BODY will be executed in the scope
423 of the second `let' form.
425 The variables bound and their default values are described by
426 the `mail-source-keyword-map' variable."
427 `(let* ,(mail-source-bind-1 (car type-source
))
428 (mail-source-set-1 ,(cadr type-source
))
431 (put 'mail-source-bind
'lisp-indent-function
1)
432 (put 'mail-source-bind
'edebug-form-spec
'(sexp body
))
434 (defun mail-source-set-1 (source)
435 (let* ((type (pop source
))
436 (defaults (cdr (assq type mail-source-keyword-map
)))
438 found default value keyword auth-info user-auth pass-auth
)
440 ;; append to the search the useful info from the source and the defaults:
441 ;; user, host, and port
443 ;; the msname is the mail-source parameter
444 (dolist (msname '(:server
:user
:port
))
445 ;; the asname is the auth-source parameter
446 (let* ((asname (case msname
447 (:server
:host
) ; auth-source uses :host
449 ;; this is the mail-source default
450 (msdef1 (or (plist-get source msname
)
451 (nth 1 (assoc msname defaults
))))
453 (msdef (mail-source-value msdef1
)))
454 (setq search
(append (list asname
457 ;; if the port is unknown yet, get it from the mail-source type
458 (unless (plist-get search
:port
)
459 (setq search
(append (list :port
(symbol-name type
)))))
461 (while (setq default
(pop defaults
))
462 ;; for each default :SYMBOL, set SYMBOL to the plist value for :SYMBOL
463 ;; using `mail-source-value' to evaluate the plist value
464 (set (mail-source-strip-keyword (setq keyword
(car default
)))
465 ;; note the following reasons for this structure:
466 ;; 1) the auth-sources user and password override everything
467 ;; 2) it avoids macros, so it's cleaner
468 ;; 3) it falls through to the mail-sources and then default values
472 (setq user-auth
(plist-get
473 ;; cache the search result in `found'
475 (setq found
(nth 0 (apply 'auth-source-search
480 (eq keyword
:password
)
481 (setq pass-auth
(plist-get
482 ;; cache the search result in `found'
484 (setq found
(nth 0 (apply 'auth-source-search
487 ;; maybe set the password to the return of the :secret function
488 (if (functionp pass-auth
)
489 (setq pass-auth
(funcall pass-auth
))
491 (t (if (setq value
(plist-get source keyword
))
492 (mail-source-value value
)
493 (mail-source-value (cadr default
)))))))))
496 (defun mail-source-bind-common-1 ()
497 (let* ((defaults mail-source-common-keyword-map
)
499 (while (setq default
(pop defaults
))
500 (push (list (mail-source-strip-keyword (car default
))
505 (defun mail-source-set-common-1 (source)
506 (let* ((type (pop source
))
507 (defaults mail-source-common-keyword-map
)
508 (defaults-1 (cdr (assq type mail-source-keyword-map
)))
509 default value keyword
)
510 (while (setq default
(pop defaults
))
511 (set (mail-source-strip-keyword (setq keyword
(car default
)))
512 (if (setq value
(plist-get source keyword
))
513 (mail-source-value value
)
514 (if (setq value
(assq keyword defaults-1
))
515 (mail-source-value (cadr value
))
516 (mail-source-value (cadr default
))))))))
518 (defmacro mail-source-bind-common
(source &rest body
)
519 "Return a `let' form that binds all common variables.
520 See `mail-source-bind'."
521 `(let ,(mail-source-bind-common-1)
522 (mail-source-set-common-1 source
)
525 (put 'mail-source-bind-common
'lisp-indent-function
1)
526 (put 'mail-source-bind-common
'edebug-form-spec
'(sexp body
))
528 (defun mail-source-value (value)
529 "Return the value of VALUE."
535 ((and (listp value
) (symbolp (car value
)) (fboundp (car value
)))
537 ;; Just return the value.
541 (autoload 'nnheader-message
"nnheader")
543 (defun mail-source-fetch (source callback
&optional method
)
544 "Fetch mail from SOURCE and call CALLBACK zero or more times.
545 CALLBACK will be called with the name of the file where (some of)
546 the mail from SOURCE is put.
547 Return the number of files that were found."
548 (mail-source-bind-common source
549 (if (or mail-source-plugged plugged
)
551 ;; Special-case the `file' handler since it's so common and
553 (when (or (not (eq (car source
) 'file
))
554 (mail-source-bind (file source
)
555 (file-exists-p path
)))
556 (nnheader-message 4 "%sReading incoming mail from %s..."
558 (format "%s: " method
)
561 (let ((function (cadr (assq (car source
) mail-source-fetcher-alist
)))
564 (error "%S is an invalid mail source specification" source
))
565 ;; If there's anything in the crash box, we do it first.
566 (when (file-exists-p mail-source-crash-box
)
567 (message "Processing mail from %s..." mail-source-crash-box
)
568 (setq found
(mail-source-callback
569 callback mail-source-crash-box
))
570 (mail-source-delete-crash-box))
572 (if (or debug-on-quit debug-on-error
)
573 (funcall function source callback
)
575 (funcall function source callback
)
577 (if (and (not mail-source-ignore-errors
)
580 (format "Mail source %s error (%s). Continue? "
581 (if (memq ':password source
)
582 (let ((s (copy-sequence source
)))
583 (setcar (cdr (memq ':password s
))
588 (error "Cannot get new mail"))
591 (declare-function gnus-message
"gnus-util" (level &rest args
))
593 (defun mail-source-delete-old-incoming (&optional age confirm
)
594 "Remove incoming files older than AGE days.
595 If CONFIRM is non-nil, ask for confirmation before removing a file."
598 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
599 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
600 (diff (if (natnump age
) age
30));; fallback, if no valid AGE given
602 (setq files
(directory-files
603 mail-source-directory t
605 (regexp-quote mail-source-incoming-file-prefix
)))
606 currday
(* (car (current-time)) high2days
)
607 currday
(+ currday
(* low2days
(nth 1 (current-time)))))
609 (let* ((ffile (car files
))
610 (bfile (gnus-replace-in-string
611 ffile
"\\`.*/\\([^/]+\\)\\'" "\\1"))
612 (filetime (nth 5 (file-attributes ffile
)))
613 (fileday (* (car filetime
) high2days
))
614 (fileday (+ fileday
(* low2days
(nth 1 filetime
)))))
615 (setq files
(cdr files
))
616 (when (and (> (- currday fileday
) diff
)
620 Delete old (> %s day(s)) incoming mail file `%s'? " diff bfile
))
622 Deleting old (> %s day(s)) incoming mail file `%s'." diff bfile
)
624 (delete-file ffile
))))))
626 (defun mail-source-callback (callback info
)
627 "Call CALLBACK on the mail file. Pass INFO on to CALLBACK."
628 (if (or (not (file-exists-p mail-source-crash-box
))
629 (zerop (nth 7 (file-attributes mail-source-crash-box
))))
631 (when (file-exists-p mail-source-crash-box
)
632 (delete-file mail-source-crash-box
))
634 (funcall callback mail-source-crash-box info
)))
636 (autoload 'gnus-float-time
"gnus-util")
638 (defvar mail-source-incoming-last-checked-time nil
)
640 (defun mail-source-delete-crash-box ()
641 (when (file-exists-p mail-source-crash-box
)
642 ;; Delete or move the incoming mail out of the way.
643 (if (eq mail-source-delete-incoming t
)
644 (delete-file mail-source-crash-box
)
648 mail-source-incoming-file-prefix
649 mail-source-directory
))))
650 (unless (file-exists-p (file-name-directory incoming
))
651 (make-directory (file-name-directory incoming
) t
))
652 (rename-file mail-source-crash-box incoming t
)
653 ;; remove old incoming files?
654 (when (natnump mail-source-delete-incoming
)
655 ;; Don't check for old incoming files more than once per day to
656 ;; save a lot of file accesses.
657 (when (or (null mail-source-incoming-last-checked-time
)
659 (time-since mail-source-incoming-last-checked-time
))
661 (setq mail-source-incoming-last-checked-time
(current-time))
662 (mail-source-delete-old-incoming
663 mail-source-delete-incoming
664 mail-source-delete-old-incoming-confirm
)))))))
666 (defun mail-source-movemail (from to
)
667 "Move FROM to TO using movemail."
668 (if (not (file-writable-p to
))
669 (error "Can't write to crash box %s. Not moving mail" to
)
670 (let ((to (file-truename (expand-file-name to
)))
672 (setq to
(file-truename to
)
673 from
(file-truename from
))
674 ;; Set TO if have not already done so, and rename or copy
675 ;; the file FROM to TO if and as appropriate.
678 ;; The crash box exists already.
680 ((not (file-exists-p from
))
681 ;; There is no inbox.
683 ((zerop (nth 7 (file-attributes from
)))
687 ;; If getting from mail spool directory, use movemail to move
688 ;; rather than just renaming, so as to interlock with the
692 (setq errors
(generate-new-buffer " *mail source loss*"))
693 (let ((default-directory "/"))
699 (or mail-source-movemail-program
700 (expand-file-name "movemail" exec-directory
))
701 nil errors nil from to
)))))
702 (when (file-exists-p to
)
703 (set-file-modes to mail-source-default-file-modes
))
704 (if (and (or (not (buffer-modified-p errors
))
705 (zerop (buffer-size errors
)))
706 (and (numberp result
)
708 ;; No output => movemail won.
711 ;; There may be a warning about older revisions. We
713 (goto-char (point-min))
714 (if (search-forward "older revision" nil t
)
716 ;; Probably a real error.
717 (subst-char-in-region (point-min) (point-max) ?
\n ?\
)
718 (goto-char (point-max))
719 (skip-chars-backward " \t")
720 (delete-region (point) (point-max))
721 (goto-char (point-min))
722 (when (looking-at "movemail: ")
723 (delete-region (point-min) (match-end 0)))
724 ;; Result may be a signal description string.
726 (format "movemail: %s (%s return). Continue? "
727 (buffer-string) result
))
728 (error "%s" (buffer-string)))
731 (buffer-name errors
))
732 (kill-buffer errors
))
733 ;; Return whether we moved successfully or not.
736 (defun mail-source-fetch-with-program (program)
737 (eq 0 (call-process shell-file-name nil nil nil
738 shell-command-switch program
)))
740 (defun mail-source-run-script (script spec
&optional delay
)
742 (if (functionp script
)
744 (mail-source-call-script
745 (format-spec script spec
))))
749 (defun mail-source-call-script (script)
750 (let ((background nil
)
751 (stderr (get-buffer-create " *mail-source-stderr*"))
753 (when (string-match "& *$" script
)
754 (setq script
(substring script
0 (match-beginning 0))
757 (call-process shell-file-name nil background nil
758 shell-command-switch script
))
760 (not (zerop result
)))
762 (message "Mail source error: %s" (buffer-string)))
763 (kill-buffer stderr
)))
766 ;;; Different fetchers
769 (defun mail-source-fetch-file (source callback
)
770 "Fetcher for single-file sources."
771 (mail-source-bind (file source
)
772 (mail-source-run-script
773 prescript
(format-spec-make ?t mail-source-crash-box
)
775 (let ((mail-source-string (format "file:%s" path
)))
776 (if (mail-source-movemail path mail-source-crash-box
)
778 (mail-source-callback callback path
)
779 (mail-source-run-script
780 postscript
(format-spec-make ?t mail-source-crash-box
))
781 (mail-source-delete-crash-box))
784 (defun mail-source-fetch-directory (source callback
)
785 "Fetcher for directory sources."
786 (mail-source-bind (directory source
)
787 (mail-source-run-script
788 prescript
(format-spec-make ?t path
) prescript-delay
)
790 (mail-source-string (format "directory:%s" path
)))
791 (dolist (file (directory-files
792 path t
(concat (regexp-quote suffix
) "$")))
793 (when (and (file-regular-p file
)
794 (funcall predicate file
)
795 (mail-source-movemail file mail-source-crash-box
))
796 (incf found
(mail-source-callback callback file
))
797 (mail-source-run-script postscript
(format-spec-make ?t path
))
798 (mail-source-delete-crash-box)))
801 (defun mail-source-fetch-pop (source callback
)
802 "Fetcher for single-file sources."
803 (mail-source-bind (pop source
)
804 ;; fixme: deal with stream type in format specs
805 (mail-source-run-script
807 (format-spec-make ?p password ?t mail-source-crash-box
808 ?s server ?P port ?u user
)
810 (let ((from (format "%s:%s:%s" server user port
))
811 (mail-source-string (format "pop:%s@%s" user server
))
813 (when (eq authentication
'password
)
816 (cdr (assoc from mail-source-password-cache
))
818 (format "Password for %s at %s: " user server
)))))
820 (setenv "MAILHOST" server
))
824 (mail-source-fetch-with-program
827 (format-spec-make ?p password ?t mail-source-crash-box
828 ?s server ?P port ?u user
))))
830 (funcall function mail-source-crash-box
))
831 ;; The default is to use pop3.el.
834 (let ((pop3-password password
)
836 (pop3-mailhost server
)
838 (pop3-authentication-scheme
839 (if (eq authentication
'apop
) 'apop
'pass
))
840 (pop3-stream-type stream
)
841 (pop3-leave-mail-on-server leave
))
842 (if (or debug-on-quit debug-on-error
)
843 (save-excursion (pop3-movemail mail-source-crash-box
))
845 (save-excursion (pop3-movemail mail-source-crash-box
))
847 ;; We nix out the password in case the error
848 ;; was because of a wrong password being given.
849 (setq mail-source-password-cache
850 (delq (assoc from mail-source-password-cache
)
851 mail-source-password-cache
))
852 (signal (car err
) (cdr err
)))))))))
855 (when (eq authentication
'password
)
856 (unless (assoc from mail-source-password-cache
)
857 (push (cons from password
) mail-source-password-cache
)))
859 (mail-source-callback callback server
)
860 ;; Update display-time's mail flag, if relevant.
861 (if (equal source mail-source-primary-source
)
862 (setq mail-source-new-mail-available nil
))
863 (mail-source-run-script
865 (format-spec-make ?p password ?t mail-source-crash-box
866 ?s server ?P port ?u user
))
867 (mail-source-delete-crash-box)))
868 ;; We nix out the password in case the error
869 ;; was because of a wrong password being given.
870 (setq mail-source-password-cache
871 (delq (assoc from mail-source-password-cache
)
872 mail-source-password-cache
))
875 (defun mail-source-check-pop (source)
876 "Check whether there is new mail."
877 (mail-source-bind (pop source
)
878 (let ((from (format "%s:%s:%s" server user port
))
879 (mail-source-string (format "pop:%s@%s" user server
))
881 (when (eq authentication
'password
)
884 (cdr (assoc from mail-source-password-cache
))
886 (format "Password for %s at %s: " user server
))))
887 (unless (assoc from mail-source-password-cache
)
888 (push (cons from password
) mail-source-password-cache
)))
890 (setenv "MAILHOST" server
))
893 ;; No easy way to check whether mail is waiting for these.
896 ;; The default is to use pop3.el.
899 (let ((pop3-password password
)
901 (pop3-mailhost server
)
903 (pop3-authentication-scheme
904 (if (eq authentication
'apop
) 'apop
'pass
)))
905 (if (or debug-on-quit debug-on-error
)
906 (save-excursion (pop3-get-message-count))
908 (save-excursion (pop3-get-message-count))
910 ;; We nix out the password in case the error
911 ;; was because of a wrong password being given.
912 (setq mail-source-password-cache
913 (delq (assoc from mail-source-password-cache
)
914 mail-source-password-cache
))
915 (signal (car err
) (cdr err
)))))))))
917 ;; Inform display-time that we have new mail.
918 (setq mail-source-new-mail-available
(> result
0))
919 ;; We nix out the password in case the error
920 ;; was because of a wrong password being given.
921 (setq mail-source-password-cache
922 (delq (assoc from mail-source-password-cache
)
923 mail-source-password-cache
)))
926 (defun mail-source-touch-pop ()
927 "Open and close a POP connection shortly.
928 POP server should be defined in `mail-source-primary-source' (which is
929 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
930 authentication. To do that, you need to set the
931 `message-send-mail-function' variable as `message-smtpmail-send-it'
932 and put the following line in your ~/.gnus.el file:
934 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
936 See the Gnus manual for details."
937 (let ((sources (if mail-source-primary-source
938 (list mail-source-primary-source
)
941 (if (eq 'pop
(car (car sources
)))
942 (mail-source-check-pop (car sources
)))
943 (setq sources
(cdr sources
)))))
945 (defun mail-source-new-mail-p ()
946 "Handler for `display-time' to indicate when new mail is available."
947 ;; Flash (ie. ring the visible bell) if mail is available.
948 (if (and mail-source-flash mail-source-new-mail-available
)
949 (let ((visible-bell t
))
951 ;; Only report flag setting; flag is updated on a different schedule.
952 mail-source-new-mail-available
)
955 (defvar mail-source-report-new-mail nil
)
956 (defvar mail-source-report-new-mail-timer nil
)
957 (defvar mail-source-report-new-mail-idle-timer nil
)
959 (defun mail-source-start-idle-timer ()
960 ;; Start our idle timer if necessary, so we delay the check until the
961 ;; user isn't typing.
962 (unless mail-source-report-new-mail-idle-timer
963 (setq mail-source-report-new-mail-idle-timer
965 mail-source-idle-time-delay
969 (mail-source-check-pop mail-source-primary-source
)
970 (setq mail-source-report-new-mail-idle-timer nil
)))))
971 ;; Since idle timers created when Emacs is already in the idle
972 ;; state don't get activated until Emacs _next_ becomes idle, we
973 ;; need to force our timer to be considered active now. We do
974 ;; this by being naughty and poking the timer internals directly
975 ;; (element 0 of the vector is nil if the timer is active).
976 (aset mail-source-report-new-mail-idle-timer
0 nil
)))
978 (defun mail-source-report-new-mail (arg)
979 "Toggle whether to report when new mail is available.
980 This only works when `display-time' is enabled."
982 (if (not mail-source-primary-source
)
983 (error "Need to set `mail-source-primary-source' to check for new mail"))
984 (let ((on (if (null arg
)
985 (not mail-source-report-new-mail
)
986 (> (prefix-numeric-value arg
) 0))))
987 (setq mail-source-report-new-mail on
)
988 (and mail-source-report-new-mail-timer
989 (nnheader-cancel-timer mail-source-report-new-mail-timer
))
990 (and mail-source-report-new-mail-idle-timer
991 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer
))
992 (setq mail-source-report-new-mail-timer nil
)
993 (setq mail-source-report-new-mail-idle-timer nil
)
997 ;; display-time-mail-function is an Emacs feature.
998 (setq display-time-mail-function
#'mail-source-new-mail-p
)
999 ;; Set up the main timer.
1000 (setq mail-source-report-new-mail-timer
1002 (* 60 mail-source-report-new-mail-interval
)
1003 (* 60 mail-source-report-new-mail-interval
)
1004 #'mail-source-start-idle-timer
))
1005 ;; When you get new mail, clear "Mail" from the mode line.
1006 (add-hook 'nnmail-post-get-new-mail-hook
1007 'display-time-event-handler
)
1008 (message "Mail check enabled"))
1009 (setq display-time-mail-function nil
)
1010 (remove-hook 'nnmail-post-get-new-mail-hook
1011 'display-time-event-handler
)
1012 (message "Mail check disabled"))))
1014 (defun mail-source-fetch-maildir (source callback
)
1015 "Fetcher for maildir sources."
1016 (mail-source-bind (maildir source
)
1019 (unless (string-match "/$" path
)
1020 (setq path
(concat path
"/")))
1021 (dolist (subdir subdirs
)
1022 (when (file-directory-p (concat path subdir
))
1023 (setq mail-source-string
(format "maildir:%s%s" path subdir
))
1024 (dolist (file (directory-files (concat path subdir
) t
))
1025 (when (and (not (file-directory-p file
))
1027 ;; `function' should return nil if successful.
1028 (funcall function file mail-source-crash-box
)
1029 (let ((coding-system-for-write
1030 mm-text-coding-system
)
1031 (coding-system-for-read
1032 mm-text-coding-system
))
1033 (with-temp-file mail-source-crash-box
1034 (insert-file-contents file
)
1035 (goto-char (point-min))
1036 ;;; ;; Unix mail format
1037 ;;; (unless (looking-at "\n*From ")
1038 ;;; (insert "From maildir "
1039 ;;; (current-time-string) "\n"))
1040 ;;; (while (re-search-forward "^From " nil t)
1041 ;;; (replace-match ">From "))
1042 ;;; (goto-char (point-max))
1045 (insert "\001\001\001\001\n"))
1048 (incf found
(mail-source-callback callback file
))
1049 (mail-source-delete-crash-box)))))
1052 (autoload 'imap-open
"imap")
1053 (autoload 'imap-authenticate
"imap")
1054 (autoload 'imap-mailbox-select
"imap")
1055 (autoload 'imap-mailbox-unselect
"imap")
1056 (autoload 'imap-mailbox-close
"imap")
1057 (autoload 'imap-search
"imap")
1058 (autoload 'imap-fetch
"imap")
1059 (autoload 'imap-close
"imap")
1060 (autoload 'imap-error-text
"imap")
1061 (autoload 'imap-message-flags-add
"imap")
1062 (autoload 'imap-list-to-message-set
"imap")
1063 (autoload 'imap-range-to-message-set
"imap")
1064 (autoload 'nnheader-ms-strip-cr
"nnheader")
1066 (autoload 'gnus-compress-sequence
"gnus-range")
1068 (defvar mail-source-imap-file-coding-system
'binary
1069 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
1071 ;; Autoloads will bring in imap before this is called.
1072 (declare-function imap-capability
"imap" (&optional identifier buffer
))
1074 (defun mail-source-fetch-imap (source callback
)
1075 "Fetcher for imap sources."
1076 (mail-source-bind (imap source
)
1077 (mail-source-run-script
1078 prescript
(format-spec-make ?p password ?t mail-source-crash-box
1079 ?s server ?P port ?u user
)
1081 (let ((from (format "%s:%s:%s" server user port
))
1083 (buf (generate-new-buffer " *imap source*"))
1084 (mail-source-string (format "imap:%s:%s" server mailbox
))
1085 (imap-shell-program (or (list program
) imap-shell-program
))
1087 (if (and (imap-open server port stream authentication buf
)
1089 user
(or (cdr (assoc from mail-source-password-cache
))
1091 (imap-mailbox-select mailbox nil buf
))
1092 (let ((coding-system-for-write mail-source-imap-file-coding-system
)
1094 (with-temp-file mail-source-crash-box
1095 ;; Avoid converting 8-bit chars from inserted strings to
1097 (mm-disable-multibyte)
1098 ;; remember password
1099 (with-current-buffer buf
1100 (when (and imap-password
1101 (not (assoc from mail-source-password-cache
)))
1102 (push (cons from imap-password
) mail-source-password-cache
)))
1103 ;; if predicate is nil, use all uids
1104 (dolist (uid (imap-search (or predicate
"1:*") buf
))
1106 (if (imap-capability 'IMAP4rev1 buf
)
1107 (caddar (imap-fetch uid
"BODY.PEEK[]"
1108 'BODYDETAIL nil buf
))
1109 (imap-fetch uid
"RFC822.PEEK" 'RFC822 nil buf
)))
1111 (insert "From imap " (current-time-string) "\n")
1113 (insert str
"\n\n"))
1114 (while (let ((case-fold-search nil
))
1115 (re-search-forward "^From " nil t
))
1116 (replace-match ">From "))
1117 (goto-char (point-max))))
1118 (nnheader-ms-strip-cr))
1119 (incf found
(mail-source-callback callback server
))
1120 (mail-source-delete-crash-box)
1121 (when (and remove fetchflag
)
1122 (setq remove
(nreverse remove
))
1123 (imap-message-flags-add
1124 (imap-range-to-message-set (gnus-compress-sequence remove
))
1127 (imap-mailbox-unselect buf
)
1128 (imap-mailbox-close nil buf
))
1131 ;; We nix out the password in case the error
1132 ;; was because of a wrong password being given.
1133 (setq mail-source-password-cache
1134 (delq (assoc from mail-source-password-cache
)
1135 mail-source-password-cache
))
1136 (error "IMAP error: %s" (imap-error-text buf
)))
1138 (mail-source-run-script
1140 (format-spec-make ?p password ?t mail-source-crash-box
1141 ?s server ?P port ?u user
))
1144 (provide 'mail-source
)
1146 ;;; mail-source.el ends here