1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999-2017 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 <https://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 (list :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* ((now (current-time))
595 (diff (if (natnump age
) age
30));; fallback, if no valid AGE given
597 (setq files
(directory-files
598 mail-source-directory t
600 (regexp-quote mail-source-incoming-file-prefix
))))
602 (let* ((ffile (car files
))
603 (bfile (replace-regexp-in-string "\\`.*/\\([^/]+\\)\\'" "\\1"
605 (filetime (nth 5 (file-attributes ffile
))))
606 (setq files
(cdr files
))
607 (when (and (> (time-to-number-of-days (time-subtract now filetime
))
612 Delete old (> %s day(s)) incoming mail file `%s'? " diff bfile
))
614 Deleting old (> %s day(s)) incoming mail file `%s'." diff bfile
)
616 (delete-file ffile
))))))
618 (defun mail-source-callback (callback info
)
619 "Call CALLBACK on the mail file. Pass INFO on to CALLBACK."
620 (if (or (not (file-exists-p mail-source-crash-box
))
621 (zerop (nth 7 (file-attributes mail-source-crash-box
))))
623 (when (file-exists-p mail-source-crash-box
)
624 (delete-file mail-source-crash-box
))
626 (funcall callback mail-source-crash-box info
)))
628 (defvar mail-source-incoming-last-checked-time nil
)
630 (defun mail-source-delete-crash-box ()
631 (when (file-exists-p mail-source-crash-box
)
632 ;; Delete or move the incoming mail out of the way.
633 (if (eq mail-source-delete-incoming t
)
634 (delete-file mail-source-crash-box
)
638 mail-source-incoming-file-prefix
639 mail-source-directory
))))
640 (unless (file-exists-p (file-name-directory incoming
))
641 (make-directory (file-name-directory incoming
) t
))
642 (rename-file mail-source-crash-box incoming t
)
643 ;; remove old incoming files?
644 (when (natnump mail-source-delete-incoming
)
645 ;; Don't check for old incoming files more than once per day to
646 ;; save a lot of file accesses.
647 (when (or (null mail-source-incoming-last-checked-time
)
649 (time-since mail-source-incoming-last-checked-time
))
651 (setq mail-source-incoming-last-checked-time
(current-time))
652 (mail-source-delete-old-incoming
653 mail-source-delete-incoming
654 mail-source-delete-old-incoming-confirm
)))))))
656 (defun mail-source-movemail (from to
)
657 "Move FROM to TO using movemail."
658 (if (not (file-writable-p to
))
659 (error "Can't write to crash box %s. Not moving mail" to
)
660 (let ((to (file-truename (expand-file-name to
)))
662 (setq to
(file-truename to
)
663 from
(file-truename from
))
664 ;; Set TO if have not already done so, and rename or copy
665 ;; the file FROM to TO if and as appropriate.
668 ;; The crash box exists already.
670 ((not (file-exists-p from
))
671 ;; There is no inbox.
673 ((zerop (nth 7 (file-attributes from
)))
677 ;; If getting from mail spool directory, use movemail to move
678 ;; rather than just renaming, so as to interlock with the
682 (setq errors
(generate-new-buffer " *mail source loss*"))
683 (let ((default-directory "/"))
689 (or mail-source-movemail-program
690 (expand-file-name "movemail" exec-directory
))
691 nil errors nil from to
)))))
692 (when (file-exists-p to
)
693 (set-file-modes to mail-source-default-file-modes
))
694 (if (and (or (not (buffer-modified-p errors
))
695 (zerop (buffer-size errors
)))
696 (and (numberp result
)
698 ;; No output => movemail won.
701 ;; There may be a warning about older revisions. We
703 (goto-char (point-min))
704 (if (search-forward "older revision" nil t
)
706 ;; Probably a real error.
707 (subst-char-in-region (point-min) (point-max) ?
\n ?\
)
708 (goto-char (point-max))
709 (skip-chars-backward " \t")
710 (delete-region (point) (point-max))
711 (goto-char (point-min))
712 (when (looking-at "movemail: ")
713 (delete-region (point-min) (match-end 0)))
714 ;; Result may be a signal description string.
716 (format "movemail: %s (%s return). Continue? "
717 (buffer-string) result
))
718 (error "%s" (buffer-string)))
721 (buffer-name errors
))
722 (kill-buffer errors
))
723 ;; Return whether we moved successfully or not.
726 (defun mail-source-fetch-with-program (program)
727 (eq 0 (call-process shell-file-name nil nil nil
728 shell-command-switch program
)))
730 (defun mail-source-run-script (script spec
&optional delay
)
732 (if (functionp script
)
734 (mail-source-call-script
735 (format-spec script spec
))))
739 (defun mail-source-call-script (script)
740 (let ((background nil
)
741 (stderr (get-buffer-create " *mail-source-stderr*"))
743 (when (string-match "& *$" script
)
744 (setq script
(substring script
0 (match-beginning 0))
747 (call-process shell-file-name nil stderr nil
748 shell-command-switch script
))
750 (not (zerop result
)))
752 (split-window-vertically)
754 (switch-to-buffer stderr
)
755 (message "Mail source error: %s " (buffer-string)))
756 (kill-buffer stderr
))))
759 ;;; Different fetchers
762 (defun mail-source-fetch-file (source callback
)
763 "Fetcher for single-file sources."
764 (mail-source-bind (file source
)
765 (mail-source-run-script
766 prescript
(format-spec-make ?t mail-source-crash-box
)
768 (let ((mail-source-string (format "file:%s" path
)))
769 (if (mail-source-movemail path mail-source-crash-box
)
771 (mail-source-callback callback path
)
772 (mail-source-run-script
773 postscript
(format-spec-make ?t mail-source-crash-box
))
774 (mail-source-delete-crash-box))
777 (defun mail-source-fetch-directory (source callback
)
778 "Fetcher for directory sources."
779 (mail-source-bind (directory source
)
780 (mail-source-run-script
781 prescript
(format-spec-make ?t path
) prescript-delay
)
783 (mail-source-string (format "directory:%s" path
)))
784 (dolist (file (directory-files
785 path t
(concat (regexp-quote suffix
) "$")))
786 (when (and (file-regular-p file
)
787 (funcall predicate file
)
788 (mail-source-movemail file mail-source-crash-box
))
789 (incf found
(mail-source-callback callback file
))
790 (mail-source-run-script postscript
(format-spec-make ?t path
))
791 (mail-source-delete-crash-box)))
794 (defun mail-source-fetch-pop (source callback
)
795 "Fetcher for single-file sources."
796 (mail-source-bind (pop source
)
797 ;; fixme: deal with stream type in format specs
798 (mail-source-run-script
800 (format-spec-make ?p password ?t mail-source-crash-box
801 ?s server ?P port ?u user
)
803 (let ((from (format "%s:%s:%s" server user port
))
804 (mail-source-string (format "pop:%s@%s" user server
))
805 (process-environment (if server
806 (cons (concat "MAILHOST=" server
)
808 process-environment
))
810 (when (eq authentication
'password
)
813 (cdr (assoc from mail-source-password-cache
))
815 (format "Password for %s at %s: " user server
)))))
819 (mail-source-fetch-with-program
822 (format-spec-make ?p password ?t mail-source-crash-box
823 ?s server ?P port ?u user
))))
825 (funcall function mail-source-crash-box
))
826 ;; The default is to use pop3.el.
829 (let ((pop3-password password
)
831 (pop3-mailhost server
)
833 (pop3-authentication-scheme
834 (if (eq authentication
'apop
) 'apop
'pass
))
835 (pop3-stream-type stream
)
836 (pop3-leave-mail-on-server leave
))
837 (if (or debug-on-quit debug-on-error
)
838 (save-excursion (pop3-movemail mail-source-crash-box
))
840 (save-excursion (pop3-movemail mail-source-crash-box
))
842 ;; We nix out the password in case the error
843 ;; was because of a wrong password being given.
844 (setq mail-source-password-cache
845 (delq (assoc from mail-source-password-cache
)
846 mail-source-password-cache
))
847 (signal (car err
) (cdr err
)))))))))
850 (when (eq authentication
'password
)
851 (unless (assoc from mail-source-password-cache
)
852 (push (cons from password
) mail-source-password-cache
)))
854 (mail-source-callback callback server
)
855 ;; Update display-time's mail flag, if relevant.
856 (if (equal source mail-source-primary-source
)
857 (setq mail-source-new-mail-available nil
))
858 (mail-source-run-script
860 (format-spec-make ?p password ?t mail-source-crash-box
861 ?s server ?P port ?u user
))
862 (mail-source-delete-crash-box)))
863 ;; We nix out the password in case the error
864 ;; was because of a wrong password being given.
865 (setq mail-source-password-cache
866 (delq (assoc from mail-source-password-cache
)
867 mail-source-password-cache
))
870 (defun mail-source-check-pop (source)
871 "Check whether there is new mail."
872 (mail-source-bind (pop source
)
873 (let ((from (format "%s:%s:%s" server user port
))
874 (mail-source-string (format "pop:%s@%s" user server
))
875 (process-environment (if server
876 (cons (concat "MAILHOST=" server
)
878 process-environment
))
880 (when (eq authentication
'password
)
883 (cdr (assoc from mail-source-password-cache
))
885 (format "Password for %s at %s: " user server
))))
886 (unless (assoc from mail-source-password-cache
)
887 (push (cons from password
) mail-source-password-cache
)))
890 ;; No easy way to check whether mail is waiting for these.
893 ;; The default is to use pop3.el.
896 (let ((pop3-password password
)
898 (pop3-mailhost server
)
900 (pop3-authentication-scheme
901 (if (eq authentication
'apop
) 'apop
'pass
)))
902 (if (or debug-on-quit debug-on-error
)
903 (save-excursion (pop3-get-message-count))
905 (save-excursion (pop3-get-message-count))
907 ;; We nix out the password in case the error
908 ;; was because of a wrong password being given.
909 (setq mail-source-password-cache
910 (delq (assoc from mail-source-password-cache
)
911 mail-source-password-cache
))
912 (signal (car err
) (cdr err
)))))))))
914 ;; Inform display-time that we have new mail.
915 (setq mail-source-new-mail-available
(> result
0))
916 ;; We nix out the password in case the error
917 ;; was because of a wrong password being given.
918 (setq mail-source-password-cache
919 (delq (assoc from mail-source-password-cache
)
920 mail-source-password-cache
)))
923 (defun mail-source-touch-pop ()
924 "Open and close a POP connection shortly.
925 POP server should be defined in `mail-source-primary-source' (which is
926 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
927 authentication. To do that, you need to set the
928 `message-send-mail-function' variable as `message-smtpmail-send-it'
929 and put the following line in your ~/.gnus.el file:
931 \(add-hook \\='message-send-mail-hook \\='mail-source-touch-pop)
933 See the Gnus manual for details."
934 (let ((sources (if mail-source-primary-source
935 (list mail-source-primary-source
)
938 (if (eq 'pop
(car (car sources
)))
939 (mail-source-check-pop (car sources
)))
940 (setq sources
(cdr sources
)))))
942 (defun mail-source-new-mail-p ()
943 "Handler for `display-time' to indicate when new mail is available."
944 ;; Flash (ie. ring the visible bell) if mail is available.
945 (if (and mail-source-flash mail-source-new-mail-available
)
946 (let ((visible-bell t
))
948 ;; Only report flag setting; flag is updated on a different schedule.
949 mail-source-new-mail-available
)
952 (defvar mail-source-report-new-mail nil
)
953 (defvar mail-source-report-new-mail-timer nil
)
954 (defvar mail-source-report-new-mail-idle-timer nil
)
956 (defun mail-source-start-idle-timer ()
957 ;; Start our idle timer if necessary, so we delay the check until the
958 ;; user isn't typing.
959 (unless mail-source-report-new-mail-idle-timer
960 (setq mail-source-report-new-mail-idle-timer
962 mail-source-idle-time-delay
966 (mail-source-check-pop mail-source-primary-source
)
967 (setq mail-source-report-new-mail-idle-timer nil
)))))
968 ;; Since idle timers created when Emacs is already in the idle
969 ;; state don't get activated until Emacs _next_ becomes idle, we
970 ;; need to force our timer to be considered active now. We do
971 ;; this by being naughty and poking the timer internals directly
972 ;; (element 0 of the vector is nil if the timer is active).
973 (aset mail-source-report-new-mail-idle-timer
0 nil
)))
975 (defun mail-source-report-new-mail (arg)
976 "Toggle whether to report when new mail is available.
977 This only works when `display-time' is enabled."
979 (if (not mail-source-primary-source
)
980 (error "Need to set `mail-source-primary-source' to check for new mail"))
981 (let ((on (if (null arg
)
982 (not mail-source-report-new-mail
)
983 (> (prefix-numeric-value arg
) 0))))
984 (setq mail-source-report-new-mail on
)
985 (and mail-source-report-new-mail-timer
986 (nnheader-cancel-timer mail-source-report-new-mail-timer
))
987 (and mail-source-report-new-mail-idle-timer
988 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer
))
989 (setq mail-source-report-new-mail-timer nil
)
990 (setq mail-source-report-new-mail-idle-timer nil
)
994 (setq display-time-mail-function
#'mail-source-new-mail-p
)
995 ;; Set up the main timer.
996 (setq mail-source-report-new-mail-timer
998 (* 60 mail-source-report-new-mail-interval
)
999 (* 60 mail-source-report-new-mail-interval
)
1000 #'mail-source-start-idle-timer
))
1001 ;; When you get new mail, clear "Mail" from the mode line.
1002 (add-hook 'nnmail-post-get-new-mail-hook
1003 'display-time-event-handler
)
1004 (message "Mail check enabled"))
1005 (setq display-time-mail-function nil
)
1006 (remove-hook 'nnmail-post-get-new-mail-hook
1007 'display-time-event-handler
)
1008 (message "Mail check disabled"))))
1010 (defun mail-source-fetch-maildir (source callback
)
1011 "Fetcher for maildir sources."
1012 (mail-source-bind (maildir source
)
1015 (unless (string-match "/$" path
)
1016 (setq path
(concat path
"/")))
1017 (dolist (subdir subdirs
)
1018 (when (file-directory-p (concat path subdir
))
1019 (setq mail-source-string
(format "maildir:%s%s" path subdir
))
1020 (dolist (file (directory-files (concat path subdir
) t
))
1021 (when (and (not (file-directory-p file
))
1023 ;; `function' should return nil if successful.
1024 (funcall function file mail-source-crash-box
)
1025 (let ((coding-system-for-write
1026 mm-text-coding-system
)
1027 (coding-system-for-read
1028 mm-text-coding-system
))
1029 (with-temp-file mail-source-crash-box
1030 (insert-file-contents file
)
1031 (goto-char (point-min))
1032 ;;; ;; Unix mail format
1033 ;;; (unless (looking-at "\n*From ")
1034 ;;; (insert "From maildir "
1035 ;;; (current-time-string) "\n"))
1036 ;;; (while (re-search-forward "^From " nil t)
1037 ;;; (replace-match ">From "))
1038 ;;; (goto-char (point-max))
1041 (insert "\001\001\001\001\n"))
1044 (incf found
(mail-source-callback callback file
))
1045 (mail-source-delete-crash-box)))))
1048 (autoload 'imap-open
"imap")
1049 (autoload 'imap-authenticate
"imap")
1050 (autoload 'imap-mailbox-select
"imap")
1051 (autoload 'imap-mailbox-unselect
"imap")
1052 (autoload 'imap-mailbox-close
"imap")
1053 (autoload 'imap-search
"imap")
1054 (autoload 'imap-fetch
"imap")
1055 (autoload 'imap-close
"imap")
1056 (autoload 'imap-error-text
"imap")
1057 (autoload 'imap-message-flags-add
"imap")
1058 (autoload 'imap-list-to-message-set
"imap")
1059 (autoload 'imap-range-to-message-set
"imap")
1060 (autoload 'nnheader-ms-strip-cr
"nnheader")
1062 (autoload 'gnus-compress-sequence
"gnus-range")
1064 (defvar mail-source-imap-file-coding-system
'binary
1065 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
1067 ;; Autoloads will bring in imap before this is called.
1068 (declare-function imap-capability
"imap" (&optional identifier buffer
))
1070 (defun mail-source-fetch-imap (source callback
)
1071 "Fetcher for imap sources."
1072 (mail-source-bind (imap source
)
1073 (mail-source-run-script
1074 prescript
(format-spec-make ?p password ?t mail-source-crash-box
1075 ?s server ?P port ?u user
)
1077 (let ((from (format "%s:%s:%s" server user port
))
1079 (buf (generate-new-buffer " *imap source*"))
1080 (mail-source-string (format "imap:%s:%s" server mailbox
))
1081 (imap-shell-program (or (list program
) imap-shell-program
))
1083 (if (and (imap-open server port stream authentication buf
)
1085 user
(or (cdr (assoc from mail-source-password-cache
))
1087 (let ((mailbox-list (if (listp mailbox
) mailbox
(list mailbox
))))
1088 (dolist (mailbox mailbox-list
)
1089 (when (imap-mailbox-select mailbox nil buf
)
1090 (let ((coding-system-for-write mail-source-imap-file-coding-system
)
1092 (message "Fetching from %s..." mailbox
)
1093 (with-temp-file mail-source-crash-box
1094 ;; Avoid converting 8-bit chars from inserted strings to
1096 (mm-disable-multibyte)
1097 ;; remember password
1098 (with-current-buffer buf
1099 (when (and imap-password
1100 (not (assoc from mail-source-password-cache
)))
1101 (push (cons from imap-password
) mail-source-password-cache
)))
1102 ;; if predicate is nil, use all uids
1103 (dolist (uid (imap-search (or predicate
"1:*") buf
))
1105 (if (imap-capability 'IMAP4rev1 buf
)
1106 (caddar (imap-fetch uid
"BODY.PEEK[]"
1107 'BODYDETAIL nil buf
))
1108 (imap-fetch uid
"RFC822.PEEK" 'RFC822 nil buf
)))
1110 (insert "From imap " (current-time-string) "\n")
1112 (insert str
"\n\n"))
1113 (while (let ((case-fold-search nil
))
1114 (re-search-forward "^From " nil t
))
1115 (replace-match ">From "))
1116 (goto-char (point-max))))
1117 (nnheader-ms-strip-cr))
1118 (incf found
(mail-source-callback callback server
))
1119 (mail-source-delete-crash-box)
1120 (when (and remove fetchflag
)
1121 (setq remove
(nreverse remove
))
1122 (imap-message-flags-add
1123 (imap-range-to-message-set (gnus-compress-sequence remove
))
1126 (imap-mailbox-unselect buf
)
1127 (imap-mailbox-close nil buf
)))))
1130 ;; We nix out the password in case the error
1131 ;; was because of a wrong password being given.
1132 (setq mail-source-password-cache
1133 (delq (assoc from mail-source-password-cache
)
1134 mail-source-password-cache
))
1135 (error "IMAP error: %s" (imap-error-text buf
)))
1137 (mail-source-run-script
1139 (format-spec-make ?p password ?t mail-source-crash-box
1140 ?s server ?P port ?u user
))
1143 (provide 'mail-source
)
1145 ;;; mail-source.el ends here