1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999-2015 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 (require 'format-spec
)
31 (autoload 'auth-source-search
"auth-source")
32 (autoload 'pop3-movemail
"pop3")
33 (autoload 'pop3-get-message-count
"pop3")
34 (autoload 'nnheader-cancel-timer
"nnheader")
36 (require 'message
) ;; for `message-directory'
38 (defvar display-time-mail-function
)
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
'((file))
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'."
63 :link
'(custom-manual "(gnus)Mail Source Specifiers")
65 (const :tag
"None" nil
)
67 (choice :format
"%[Value Menu%] %v"
69 (cons :tag
"Group parameter `mail-source'"
70 (const :format
"" group
))
71 (cons :tag
"Spool file"
72 (const :format
"" file
)
73 (checklist :tag
"Options" :greedy t
75 (const :format
"" :value
:path
)
77 (cons :tag
"Several files in a directory"
78 (const :format
"" directory
)
79 (checklist :tag
"Options" :greedy t
81 (const :format
"" :value
:path
)
82 (directory :tag
"Path"))
84 (const :format
"" :value
:suffix
)
85 (string :tag
"Suffix"))
87 (const :format
"" :value
:predicate
)
88 (function :tag
"Predicate"))
90 (const :format
"" :value
:prescript
)
91 (choice :tag
"Prescript"
94 (function :format
"%v")))
96 (const :format
"" :value
:postscript
)
97 (choice :tag
"Postscript"
100 (function :format
"%v")))
102 (const :format
"" :value
:plugged
)
103 (boolean :tag
"Plugged"))))
104 (cons :tag
"POP3 server"
105 (const :format
"" pop
)
106 (checklist :tag
"Options" :greedy t
108 (const :format
"" :value
:server
)
109 (string :tag
"Server"))
111 (const :format
"" :value
:port
)
114 (integer :format
"%v")
115 (string :format
"%v")))
117 (const :format
"" :value
:user
)
118 (string :tag
"User"))
120 (const :format
"" :value
:password
)
121 (string :tag
"Password"))
123 (const :format
"" :value
:program
)
124 (string :tag
"Program"))
126 (const :format
"" :value
:prescript
)
127 (choice :tag
"Prescript"
129 (string :format
"%v")
130 (function :format
"%v")
131 (const :tag
"None" nil
)))
133 (const :format
"" :value
:postscript
)
134 (choice :tag
"Postscript"
136 (string :format
"%v")
137 (function :format
"%v")
138 (const :tag
"None" nil
)))
140 (const :format
"" :value
:function
)
141 (function :tag
"Function"))
144 :value
:authentication
)
145 (choice :tag
"Authentication"
150 (const :format
"" :value
:plugged
)
151 (boolean :tag
"Plugged"))
153 (const :format
"" :value
:stream
)
154 (choice :tag
"Stream"
156 (const :tag
"Clear" nil
)
158 (const :tag
"SSL/TLS" ssl
)))
160 (const :format
"" :value
:leave
)
162 %{Leave mail on server%}:\n\t\t%[Value Menu%] %v"
165 Don't leave mails" nil
)
169 Leave mails for this many days" :value
14)))))
170 (cons :tag
"Maildir (qmail, postfix...)"
171 (const :format
"" maildir
)
172 (checklist :tag
"Options" :greedy t
174 (const :format
"" :value
:path
)
175 (directory :tag
"Path"))
177 (const :format
"" :value
:plugged
)
178 (boolean :tag
"Plugged"))))
179 (cons :tag
"IMAP server"
180 (const :format
"" imap
)
181 (checklist :tag
"Options" :greedy t
183 (const :format
"" :value
:server
)
184 (string :tag
"Server"))
186 (const :format
"" :value
:port
)
191 (const :format
"" :value
:user
)
192 (string :tag
"User"))
194 (const :format
"" :value
:password
)
195 (string :tag
"Password"))
197 (const :format
"" :value
:stream
)
198 (choice :tag
"Stream"
200 ,@mail-source-imap-streams
))
202 (const :format
"" :value
:program
)
203 (string :tag
"Program"))
206 :value
:authenticator
)
207 (choice :tag
"Authenticator"
209 ,@mail-source-imap-authenticators
))
211 (const :format
"" :value
:mailbox
)
212 (string :tag
"Mailbox"
215 (const :format
"" :value
:predicate
)
216 (string :tag
"Predicate"
217 :value
"UNSEEN UNDELETED"))
219 (const :format
"" :value
:fetchflag
)
220 (string :tag
"Fetchflag"
225 (boolean :tag
"Dontexpunge"))
227 (const :format
"" :value
:plugged
)
228 (boolean :tag
"Plugged"))))))))
230 (defcustom mail-source-ignore-errors nil
231 "*Ignore errors when querying mail sources.
232 If nil, the user will be prompted when an error occurs. If non-nil,
233 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
265 10 ;; development versions
266 ;; 2 ;; released versions
267 "If non-nil, delete incoming files after handling.
268 If t, delete immediately, if nil, never delete. If a positive number, delete
269 files older than number of days.
271 Removing of old files happens in `mail-source-callback', i.e. no
272 old incoming files will be deleted unless you receive new mail.
273 You may also set this variable to nil and call
274 `mail-source-delete-old-incoming' interactively."
276 :version
"22.2" ;; No Gnus / Gnus 5.10.10 (default changed)
277 :type
'(choice (const :tag
"immediately" t
)
278 (const :tag
"never" nil
)
279 (integer :tag
"days")))
281 (defcustom mail-source-delete-old-incoming-confirm nil
282 "If non-nil, ask for confirmation before deleting old incoming files.
283 This variable only applies when `mail-source-delete-incoming' is a positive
285 :version
"22.2" ;; No Gnus / Gnus 5.10.10 (default changed)
289 (defcustom mail-source-incoming-file-prefix
"Incoming"
290 "Prefix for file name for storing incoming mail"
294 (defcustom mail-source-report-new-mail-interval
5
295 "Interval in minutes between checks for new mail."
299 (defcustom mail-source-idle-time-delay
5
300 "Number of idle seconds to wait before checking for new mail."
304 (defcustom mail-source-movemail-program nil
305 "If non-nil, name of program for fetching new mail."
308 :type
'(choice (const nil
) string
))
310 ;;; Internal variables.
312 (defvar mail-source-string
""
313 "A dynamically bound string that says what the current mail source is.")
315 (defvar mail-source-new-mail-available nil
316 "Flag indicating when new mail is available.")
319 (defvar mail-source-common-keyword-map
321 "Mapping from keywords to default values.
322 Common keywords should be listed here.")
324 (defvar mail-source-keyword-map
329 (:path
(or (getenv "MAIL")
330 (expand-file-name (user-login-name) rmail-spool-directory
))))
337 (:predicate identity
))
342 ;; note server and port need to come before user and password
343 (:server
(getenv "MAILHOST"))
345 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
349 (:authentication password
)
353 (:path
(or (getenv "MAILDIR") "~/Maildir/"))
354 (:subdirs
("cur" "new"))
357 ;; note server and port need to come before user and password
358 (:server
(getenv "MAILHOST"))
363 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
366 (:predicate
"UNSEEN UNDELETED")
367 (:fetchflag
"\\Deleted")
372 "Mapping from keywords to default values.
373 All keywords that can be used must be listed here."))
375 (defvar mail-source-fetcher-alist
376 '((file mail-source-fetch-file
)
377 (directory mail-source-fetch-directory
)
378 (pop mail-source-fetch-pop
)
379 (maildir mail-source-fetch-maildir
)
380 (imap mail-source-fetch-imap
))
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))))
394 ;; generate a list of variable names paired with nil values
395 ;; suitable for usage in a `let' form
397 (defun mail-source-bind-1 (type)
398 (let* ((defaults (cdr (assq type mail-source-keyword-map
)))
400 (while (setq default
(pop defaults
))
401 (push (list (mail-source-strip-keyword (car default
))
406 (defmacro mail-source-bind
(type-source &rest body
)
407 "Return a `let' form that binds all variables in source TYPE.
408 TYPE-SOURCE is a list where the first element is the TYPE, and
409 the second variable is the SOURCE.
410 At run time, the mail source specifier SOURCE will be inspected,
411 and the variables will be set according to it. Variables not
412 specified will be given default values.
414 The user and password will be loaded from the auth-source values
415 if those are available. They override the original user and
416 password in a second `let' form.
418 After this is done, BODY will be executed in the scope
419 of the second `let' form.
421 The variables bound and their default values are described by
422 the `mail-source-keyword-map' variable."
423 `(let* ,(mail-source-bind-1 (car type-source
))
424 (mail-source-set-1 ,(cadr type-source
))
427 (put 'mail-source-bind
'lisp-indent-function
1)
428 (put 'mail-source-bind
'edebug-form-spec
'(sexp body
))
430 (defun mail-source-set-1 (source)
431 (let* ((type (pop source
))
432 (defaults (cdr (assq type mail-source-keyword-map
)))
434 found default value keyword auth-info user-auth pass-auth
)
436 ;; append to the search the useful info from the source and the defaults:
437 ;; user, host, and port
439 ;; the msname is the mail-source parameter
440 (dolist (msname '(:server
:user
:port
))
441 ;; the asname is the auth-source parameter
442 (let* ((asname (case msname
443 (:server
:host
) ; auth-source uses :host
445 ;; this is the mail-source default
446 (msdef1 (or (plist-get source msname
)
447 (nth 1 (assoc msname defaults
))))
449 (msdef (mail-source-value msdef1
)))
450 (setq search
(append (list asname
453 ;; if the port is unknown yet, get it from the mail-source type
454 (unless (plist-get search
:port
)
455 (setq search
(append (list :port
(symbol-name type
)))))
457 (while (setq default
(pop defaults
))
458 ;; for each default :SYMBOL, set SYMBOL to the plist value for :SYMBOL
459 ;; using `mail-source-value' to evaluate the plist value
460 (set (mail-source-strip-keyword (setq keyword
(car default
)))
461 ;; note the following reasons for this structure:
462 ;; 1) the auth-sources user and password override everything
463 ;; 2) it avoids macros, so it's cleaner
464 ;; 3) it falls through to the mail-sources and then default values
468 (setq user-auth
(plist-get
469 ;; cache the search result in `found'
471 (setq found
(nth 0 (apply 'auth-source-search
476 (eq keyword
:password
)
477 (setq pass-auth
(plist-get
478 ;; cache the search result in `found'
480 (setq found
(nth 0 (apply 'auth-source-search
483 ;; maybe set the password to the return of the :secret function
484 (if (functionp pass-auth
)
485 (setq pass-auth
(funcall pass-auth
))
487 (t (if (setq value
(plist-get source keyword
))
488 (mail-source-value value
)
489 (mail-source-value (cadr default
)))))))))
492 (defun mail-source-bind-common-1 ()
493 (let* ((defaults mail-source-common-keyword-map
)
495 (while (setq default
(pop defaults
))
496 (push (list (mail-source-strip-keyword (car default
))
501 (defun mail-source-set-common-1 (source)
502 (let* ((type (pop source
))
503 (defaults mail-source-common-keyword-map
)
504 (defaults-1 (cdr (assq type mail-source-keyword-map
)))
505 default value keyword
)
506 (while (setq default
(pop defaults
))
507 (set (mail-source-strip-keyword (setq keyword
(car default
)))
508 (if (setq value
(plist-get source keyword
))
509 (mail-source-value value
)
510 (if (setq value
(assq keyword defaults-1
))
511 (mail-source-value (cadr value
))
512 (mail-source-value (cadr default
))))))))
514 (defmacro mail-source-bind-common
(source &rest body
)
515 "Return a `let' form that binds all common variables.
516 See `mail-source-bind'."
517 `(let ,(mail-source-bind-common-1)
518 (mail-source-set-common-1 source
)
521 (put 'mail-source-bind-common
'lisp-indent-function
1)
522 (put 'mail-source-bind-common
'edebug-form-spec
'(sexp body
))
524 (defun mail-source-value (value)
525 "Return the value of VALUE."
531 ((and (listp value
) (symbolp (car value
)) (fboundp (car value
)))
533 ;; Just return the value.
537 (autoload 'nnheader-message
"nnheader")
539 (defun mail-source-fetch (source callback
&optional method
)
540 "Fetch mail from SOURCE and call CALLBACK zero or more times.
541 CALLBACK will be called with the name of the file where (some of)
542 the mail from SOURCE is put.
543 Return the number of files that were found."
544 (mail-source-bind-common source
545 (if (or mail-source-plugged plugged
)
547 ;; Special-case the `file' handler since it's so common and
549 (when (or (not (eq (car source
) 'file
))
550 (mail-source-bind (file source
)
551 (file-exists-p path
)))
552 (nnheader-message 4 "%sReading incoming mail from %s..."
554 (format "%s: " method
)
557 (let ((function (cadr (assq (car source
) mail-source-fetcher-alist
)))
560 (error "%S is an invalid mail source specification" source
))
561 ;; If there's anything in the crash box, we do it first.
562 (when (file-exists-p mail-source-crash-box
)
563 (message "Processing mail from %s..." mail-source-crash-box
)
564 (setq found
(mail-source-callback
565 callback mail-source-crash-box
))
566 (mail-source-delete-crash-box))
568 (if (or debug-on-quit debug-on-error
)
569 (funcall function source callback
)
571 (funcall function source callback
)
573 (if (and (not mail-source-ignore-errors
)
576 (format "Mail source %s error (%s). Continue? "
577 (if (memq ':password source
)
578 (let ((s (copy-sequence source
)))
579 (setcar (cdr (memq ':password s
))
584 (error "Cannot get new mail"))
587 (declare-function gnus-message
"gnus-util" (level &rest args
))
589 (defun mail-source-delete-old-incoming (&optional age confirm
)
590 "Remove incoming files older than AGE days.
591 If CONFIRM is non-nil, ask for confirmation before removing a file."
594 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
595 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
596 (diff (if (natnump age
) age
30));; fallback, if no valid AGE given
598 (setq files
(directory-files
599 mail-source-directory t
601 (regexp-quote mail-source-incoming-file-prefix
)))
602 currday
(* (car (current-time)) high2days
)
603 currday
(+ currday
(* low2days
(nth 1 (current-time)))))
605 (let* ((ffile (car files
))
606 (bfile (gnus-replace-in-string
607 ffile
"\\`.*/\\([^/]+\\)\\'" "\\1"))
608 (filetime (nth 5 (file-attributes ffile
)))
609 (fileday (* (car filetime
) high2days
))
610 (fileday (+ fileday
(* low2days
(nth 1 filetime
)))))
611 (setq files
(cdr files
))
612 (when (and (> (- currday fileday
) diff
)
616 Delete old (> %s day(s)) incoming mail file `%s'? " diff bfile
))
618 Deleting old (> %s day(s)) incoming mail file `%s'." diff bfile
)
620 (delete-file ffile
))))))
622 (defun mail-source-callback (callback info
)
623 "Call CALLBACK on the mail file. Pass INFO on to CALLBACK."
624 (if (or (not (file-exists-p mail-source-crash-box
))
625 (zerop (nth 7 (file-attributes mail-source-crash-box
))))
627 (when (file-exists-p mail-source-crash-box
)
628 (delete-file mail-source-crash-box
))
630 (funcall callback mail-source-crash-box info
)))
632 (autoload 'gnus-float-time
"gnus-util")
634 (defvar mail-source-incoming-last-checked-time nil
)
636 (defun mail-source-delete-crash-box ()
637 (when (file-exists-p mail-source-crash-box
)
638 ;; Delete or move the incoming mail out of the way.
639 (if (eq mail-source-delete-incoming t
)
640 (delete-file mail-source-crash-box
)
644 mail-source-incoming-file-prefix
645 mail-source-directory
))))
646 (unless (file-exists-p (file-name-directory incoming
))
647 (make-directory (file-name-directory incoming
) t
))
648 (rename-file mail-source-crash-box incoming t
)
649 ;; remove old incoming files?
650 (when (natnump mail-source-delete-incoming
)
651 ;; Don't check for old incoming files more than once per day to
652 ;; save a lot of file accesses.
653 (when (or (null mail-source-incoming-last-checked-time
)
655 (time-since mail-source-incoming-last-checked-time
))
657 (setq mail-source-incoming-last-checked-time
(current-time))
658 (mail-source-delete-old-incoming
659 mail-source-delete-incoming
660 mail-source-delete-old-incoming-confirm
)))))))
662 (defun mail-source-movemail (from to
)
663 "Move FROM to TO using movemail."
664 (if (not (file-writable-p to
))
665 (error "Can't write to crash box %s. Not moving mail" to
)
666 (let ((to (file-truename (expand-file-name to
)))
668 (setq to
(file-truename to
)
669 from
(file-truename from
))
670 ;; Set TO if have not already done so, and rename or copy
671 ;; the file FROM to TO if and as appropriate.
674 ;; The crash box exists already.
676 ((not (file-exists-p from
))
677 ;; There is no inbox.
679 ((zerop (nth 7 (file-attributes from
)))
683 ;; If getting from mail spool directory, use movemail to move
684 ;; rather than just renaming, so as to interlock with the
688 (setq errors
(generate-new-buffer " *mail source loss*"))
689 (let ((default-directory "/"))
695 (or mail-source-movemail-program
696 (expand-file-name "movemail" exec-directory
))
697 nil errors nil from to
)))))
698 (when (file-exists-p to
)
699 (set-file-modes to mail-source-default-file-modes
))
700 (if (and (or (not (buffer-modified-p errors
))
701 (zerop (buffer-size errors
)))
702 (and (numberp result
)
704 ;; No output => movemail won.
707 ;; There may be a warning about older revisions. We
709 (goto-char (point-min))
710 (if (search-forward "older revision" nil t
)
712 ;; Probably a real error.
713 (subst-char-in-region (point-min) (point-max) ?
\n ?\
)
714 (goto-char (point-max))
715 (skip-chars-backward " \t")
716 (delete-region (point) (point-max))
717 (goto-char (point-min))
718 (when (looking-at "movemail: ")
719 (delete-region (point-min) (match-end 0)))
720 ;; Result may be a signal description string.
722 (format "movemail: %s (%s return). Continue? "
723 (buffer-string) result
))
724 (error "%s" (buffer-string)))
727 (buffer-name errors
))
728 (kill-buffer errors
))
729 ;; Return whether we moved successfully or not.
732 (defun mail-source-fetch-with-program (program)
733 (eq 0 (call-process shell-file-name nil nil nil
734 shell-command-switch program
)))
736 (defun mail-source-run-script (script spec
&optional delay
)
738 (if (functionp script
)
740 (mail-source-call-script
741 (format-spec script spec
))))
745 (defun mail-source-call-script (script)
746 (let ((background nil
)
747 (stderr (get-buffer-create " *mail-source-stderr*"))
749 (when (string-match "& *$" script
)
750 (setq script
(substring script
0 (match-beginning 0))
753 (call-process shell-file-name nil stderr nil
754 shell-command-switch script
))
756 (not (zerop result
)))
758 (split-window-vertically)
760 (switch-to-buffer stderr
)
761 (message "Mail source error: %s " (buffer-string)))
762 (kill-buffer stderr
))))
765 ;;; Different fetchers
768 (defun mail-source-fetch-file (source callback
)
769 "Fetcher for single-file sources."
770 (mail-source-bind (file source
)
771 (mail-source-run-script
772 prescript
(format-spec-make ?t mail-source-crash-box
)
774 (let ((mail-source-string (format "file:%s" path
)))
775 (if (mail-source-movemail path mail-source-crash-box
)
777 (mail-source-callback callback path
)
778 (mail-source-run-script
779 postscript
(format-spec-make ?t mail-source-crash-box
))
780 (mail-source-delete-crash-box))
783 (defun mail-source-fetch-directory (source callback
)
784 "Fetcher for directory sources."
785 (mail-source-bind (directory source
)
786 (mail-source-run-script
787 prescript
(format-spec-make ?t path
) prescript-delay
)
789 (mail-source-string (format "directory:%s" path
)))
790 (dolist (file (directory-files
791 path t
(concat (regexp-quote suffix
) "$")))
792 (when (and (file-regular-p file
)
793 (funcall predicate file
)
794 (mail-source-movemail file mail-source-crash-box
))
795 (incf found
(mail-source-callback callback file
))
796 (mail-source-run-script postscript
(format-spec-make ?t path
))
797 (mail-source-delete-crash-box)))
800 (defun mail-source-fetch-pop (source callback
)
801 "Fetcher for single-file sources."
802 (mail-source-bind (pop source
)
803 ;; fixme: deal with stream type in format specs
804 (mail-source-run-script
806 (format-spec-make ?p password ?t mail-source-crash-box
807 ?s server ?P port ?u user
)
809 (let ((from (format "%s:%s:%s" server user port
))
810 (mail-source-string (format "pop:%s@%s" user server
))
811 (process-environment (if server
812 (cons (concat "MAILHOST=" server
)
814 process-environment
))
816 (when (eq authentication
'password
)
819 (cdr (assoc from mail-source-password-cache
))
821 (format "Password for %s at %s: " user server
)))))
825 (mail-source-fetch-with-program
828 (format-spec-make ?p password ?t mail-source-crash-box
829 ?s server ?P port ?u user
))))
831 (funcall function mail-source-crash-box
))
832 ;; The default is to use pop3.el.
835 (let ((pop3-password password
)
837 (pop3-mailhost server
)
839 (pop3-authentication-scheme
840 (if (eq authentication
'apop
) 'apop
'pass
))
841 (pop3-stream-type stream
)
842 (pop3-leave-mail-on-server leave
))
843 (if (or debug-on-quit debug-on-error
)
844 (save-excursion (pop3-movemail mail-source-crash-box
))
846 (save-excursion (pop3-movemail mail-source-crash-box
))
848 ;; We nix out the password in case the error
849 ;; was because of a wrong password being given.
850 (setq mail-source-password-cache
851 (delq (assoc from mail-source-password-cache
)
852 mail-source-password-cache
))
853 (signal (car err
) (cdr err
)))))))))
856 (when (eq authentication
'password
)
857 (unless (assoc from mail-source-password-cache
)
858 (push (cons from password
) mail-source-password-cache
)))
860 (mail-source-callback callback server
)
861 ;; Update display-time's mail flag, if relevant.
862 (if (equal source mail-source-primary-source
)
863 (setq mail-source-new-mail-available nil
))
864 (mail-source-run-script
866 (format-spec-make ?p password ?t mail-source-crash-box
867 ?s server ?P port ?u user
))
868 (mail-source-delete-crash-box)))
869 ;; We nix out the password in case the error
870 ;; was because of a wrong password being given.
871 (setq mail-source-password-cache
872 (delq (assoc from mail-source-password-cache
)
873 mail-source-password-cache
))
876 (defun mail-source-check-pop (source)
877 "Check whether there is new mail."
878 (mail-source-bind (pop source
)
879 (let ((from (format "%s:%s:%s" server user port
))
880 (mail-source-string (format "pop:%s@%s" user server
))
881 (process-environment (if server
882 (cons (concat "MAILHOST=" server
)
884 process-environment
))
886 (when (eq authentication
'password
)
889 (cdr (assoc from mail-source-password-cache
))
891 (format "Password for %s at %s: " user server
))))
892 (unless (assoc from mail-source-password-cache
)
893 (push (cons from password
) mail-source-password-cache
)))
896 ;; No easy way to check whether mail is waiting for these.
899 ;; The default is to use pop3.el.
902 (let ((pop3-password password
)
904 (pop3-mailhost server
)
906 (pop3-authentication-scheme
907 (if (eq authentication
'apop
) 'apop
'pass
)))
908 (if (or debug-on-quit debug-on-error
)
909 (save-excursion (pop3-get-message-count))
911 (save-excursion (pop3-get-message-count))
913 ;; We nix out the password in case the error
914 ;; was because of a wrong password being given.
915 (setq mail-source-password-cache
916 (delq (assoc from mail-source-password-cache
)
917 mail-source-password-cache
))
918 (signal (car err
) (cdr err
)))))))))
920 ;; Inform display-time that we have new mail.
921 (setq mail-source-new-mail-available
(> result
0))
922 ;; We nix out the password in case the error
923 ;; was because of a wrong password being given.
924 (setq mail-source-password-cache
925 (delq (assoc from mail-source-password-cache
)
926 mail-source-password-cache
)))
929 (defun mail-source-touch-pop ()
930 "Open and close a POP connection shortly.
931 POP server should be defined in `mail-source-primary-source' (which is
932 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
933 authentication. To do that, you need to set the
934 `message-send-mail-function' variable as `message-smtpmail-send-it'
935 and put the following line in your ~/.gnus.el file:
937 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
939 See the Gnus manual for details."
940 (let ((sources (if mail-source-primary-source
941 (list mail-source-primary-source
)
944 (if (eq 'pop
(car (car sources
)))
945 (mail-source-check-pop (car sources
)))
946 (setq sources
(cdr sources
)))))
948 (defun mail-source-new-mail-p ()
949 "Handler for `display-time' to indicate when new mail is available."
950 ;; Flash (ie. ring the visible bell) if mail is available.
951 (if (and mail-source-flash mail-source-new-mail-available
)
952 (let ((visible-bell t
))
954 ;; Only report flag setting; flag is updated on a different schedule.
955 mail-source-new-mail-available
)
958 (defvar mail-source-report-new-mail nil
)
959 (defvar mail-source-report-new-mail-timer nil
)
960 (defvar mail-source-report-new-mail-idle-timer nil
)
962 (defun mail-source-start-idle-timer ()
963 ;; Start our idle timer if necessary, so we delay the check until the
964 ;; user isn't typing.
965 (unless mail-source-report-new-mail-idle-timer
966 (setq mail-source-report-new-mail-idle-timer
968 mail-source-idle-time-delay
972 (mail-source-check-pop mail-source-primary-source
)
973 (setq mail-source-report-new-mail-idle-timer nil
)))))
974 ;; Since idle timers created when Emacs is already in the idle
975 ;; state don't get activated until Emacs _next_ becomes idle, we
976 ;; need to force our timer to be considered active now. We do
977 ;; this by being naughty and poking the timer internals directly
978 ;; (element 0 of the vector is nil if the timer is active).
979 (aset mail-source-report-new-mail-idle-timer
0 nil
)))
981 (defun mail-source-report-new-mail (arg)
982 "Toggle whether to report when new mail is available.
983 This only works when `display-time' is enabled."
985 (if (not mail-source-primary-source
)
986 (error "Need to set `mail-source-primary-source' to check for new mail"))
987 (let ((on (if (null arg
)
988 (not mail-source-report-new-mail
)
989 (> (prefix-numeric-value arg
) 0))))
990 (setq mail-source-report-new-mail on
)
991 (and mail-source-report-new-mail-timer
992 (nnheader-cancel-timer mail-source-report-new-mail-timer
))
993 (and mail-source-report-new-mail-idle-timer
994 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer
))
995 (setq mail-source-report-new-mail-timer nil
)
996 (setq mail-source-report-new-mail-idle-timer nil
)
1000 ;; display-time-mail-function is an Emacs feature.
1001 (setq display-time-mail-function
#'mail-source-new-mail-p
)
1002 ;; Set up the main timer.
1003 (setq mail-source-report-new-mail-timer
1005 (* 60 mail-source-report-new-mail-interval
)
1006 (* 60 mail-source-report-new-mail-interval
)
1007 #'mail-source-start-idle-timer
))
1008 ;; When you get new mail, clear "Mail" from the mode line.
1009 (add-hook 'nnmail-post-get-new-mail-hook
1010 'display-time-event-handler
)
1011 (message "Mail check enabled"))
1012 (setq display-time-mail-function nil
)
1013 (remove-hook 'nnmail-post-get-new-mail-hook
1014 'display-time-event-handler
)
1015 (message "Mail check disabled"))))
1017 (defun mail-source-fetch-maildir (source callback
)
1018 "Fetcher for maildir sources."
1019 (mail-source-bind (maildir source
)
1022 (unless (string-match "/$" path
)
1023 (setq path
(concat path
"/")))
1024 (dolist (subdir subdirs
)
1025 (when (file-directory-p (concat path subdir
))
1026 (setq mail-source-string
(format "maildir:%s%s" path subdir
))
1027 (dolist (file (directory-files (concat path subdir
) t
))
1028 (when (and (not (file-directory-p file
))
1030 ;; `function' should return nil if successful.
1031 (funcall function file mail-source-crash-box
)
1032 (let ((coding-system-for-write
1033 mm-text-coding-system
)
1034 (coding-system-for-read
1035 mm-text-coding-system
))
1036 (with-temp-file mail-source-crash-box
1037 (insert-file-contents file
)
1038 (goto-char (point-min))
1039 ;;; ;; Unix mail format
1040 ;;; (unless (looking-at "\n*From ")
1041 ;;; (insert "From maildir "
1042 ;;; (current-time-string) "\n"))
1043 ;;; (while (re-search-forward "^From " nil t)
1044 ;;; (replace-match ">From "))
1045 ;;; (goto-char (point-max))
1048 (insert "\001\001\001\001\n"))
1051 (incf found
(mail-source-callback callback file
))
1052 (mail-source-delete-crash-box)))))
1055 (autoload 'imap-open
"imap")
1056 (autoload 'imap-authenticate
"imap")
1057 (autoload 'imap-mailbox-select
"imap")
1058 (autoload 'imap-mailbox-unselect
"imap")
1059 (autoload 'imap-mailbox-close
"imap")
1060 (autoload 'imap-search
"imap")
1061 (autoload 'imap-fetch
"imap")
1062 (autoload 'imap-close
"imap")
1063 (autoload 'imap-error-text
"imap")
1064 (autoload 'imap-message-flags-add
"imap")
1065 (autoload 'imap-list-to-message-set
"imap")
1066 (autoload 'imap-range-to-message-set
"imap")
1067 (autoload 'nnheader-ms-strip-cr
"nnheader")
1069 (autoload 'gnus-compress-sequence
"gnus-range")
1071 (defvar mail-source-imap-file-coding-system
'binary
1072 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
1074 ;; Autoloads will bring in imap before this is called.
1075 (declare-function imap-capability
"imap" (&optional identifier buffer
))
1077 (defun mail-source-fetch-imap (source callback
)
1078 "Fetcher for imap sources."
1079 (mail-source-bind (imap source
)
1080 (mail-source-run-script
1081 prescript
(format-spec-make ?p password ?t mail-source-crash-box
1082 ?s server ?P port ?u user
)
1084 (let ((from (format "%s:%s:%s" server user port
))
1086 (buf (generate-new-buffer " *imap source*"))
1087 (mail-source-string (format "imap:%s:%s" server mailbox
))
1088 (imap-shell-program (or (list program
) imap-shell-program
))
1090 (if (and (imap-open server port stream authentication buf
)
1092 user
(or (cdr (assoc from mail-source-password-cache
))
1094 (imap-mailbox-select mailbox nil buf
))
1095 (let ((coding-system-for-write mail-source-imap-file-coding-system
)
1097 (with-temp-file mail-source-crash-box
1098 ;; Avoid converting 8-bit chars from inserted strings to
1100 (mm-disable-multibyte)
1101 ;; remember password
1102 (with-current-buffer buf
1103 (when (and imap-password
1104 (not (assoc from mail-source-password-cache
)))
1105 (push (cons from imap-password
) mail-source-password-cache
)))
1106 ;; if predicate is nil, use all uids
1107 (dolist (uid (imap-search (or predicate
"1:*") buf
))
1109 (if (imap-capability 'IMAP4rev1 buf
)
1110 (caddar (imap-fetch uid
"BODY.PEEK[]"
1111 'BODYDETAIL nil buf
))
1112 (imap-fetch uid
"RFC822.PEEK" 'RFC822 nil buf
)))
1114 (insert "From imap " (current-time-string) "\n")
1116 (insert str
"\n\n"))
1117 (while (let ((case-fold-search nil
))
1118 (re-search-forward "^From " nil t
))
1119 (replace-match ">From "))
1120 (goto-char (point-max))))
1121 (nnheader-ms-strip-cr))
1122 (incf found
(mail-source-callback callback server
))
1123 (mail-source-delete-crash-box)
1124 (when (and remove fetchflag
)
1125 (setq remove
(nreverse remove
))
1126 (imap-message-flags-add
1127 (imap-range-to-message-set (gnus-compress-sequence remove
))
1130 (imap-mailbox-unselect buf
)
1131 (imap-mailbox-close nil buf
))
1134 ;; We nix out the password in case the error
1135 ;; was because of a wrong password being given.
1136 (setq mail-source-password-cache
1137 (delq (assoc from mail-source-password-cache
)
1138 mail-source-password-cache
))
1139 (error "IMAP error: %s" (imap-error-text buf
)))
1141 (mail-source-run-script
1143 (format-spec-make ?p password ?t mail-source-crash-box
1144 ?s server ?P port ?u user
))
1147 (provide 'mail-source
)
1149 ;;; mail-source.el ends here