1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
33 (eval-when-compile (defvar display-time-mail-function)))
35 (autoload 'pop3-movemail "pop3")
36 (autoload 'pop3-get-message-count "pop3")
37 (autoload 'nnheader-cancel-timer "nnheader")
38 (autoload 'nnheader-run-at-time "nnheader"))
39 (require 'format-spec)
41 (require 'message) ;; for `message-directory'
43 (defgroup mail-source nil
44 "The mail-fetching library."
48 ;; Define these at compile time to avoid dragging in imap always.
49 (defconst mail-source-imap-authenticators
52 (list 'const (car a)))
53 imap-authenticator-alist)))
54 (defconst mail-source-imap-streams
57 (list 'const (car a)))
60 (defcustom mail-sources nil
61 "*Where the mail backends will look for incoming mail.
62 This variable is a list of mail source specifiers.
63 See Info node `(gnus)Mail Source Specifiers'."
65 :link '(custom-manual "(gnus)Mail Source Specifiers")
69 (choice :format "%[Value Menu%] %v"
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 (number :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")))
132 (const :format "" :value :postscript)
133 (choice :tag "Postscript"
135 (string :format "%v")
136 (function :format "%v")))
138 (const :format "" :value :function)
139 (function :tag "Function"))
142 :value :authentication)
143 (choice :tag "Authentication"
148 (const :format "" :value :plugged)
149 (boolean :tag "Plugged"))))
150 (cons :tag "Maildir (qmail, postfix...)"
151 (const :format "" maildir)
152 (checklist :tag "Options" :greedy t
154 (const :format "" :value :path)
155 (directory :tag "Path"))
157 (const :format "" :value :plugged)
158 (boolean :tag "Plugged"))))
159 (cons :tag "IMAP server"
160 (const :format "" imap)
161 (checklist :tag "Options" :greedy t
163 (const :format "" :value :server)
164 (string :tag "Server"))
166 (const :format "" :value :port)
171 (const :format "" :value :user)
172 (string :tag "User"))
174 (const :format "" :value :password)
175 (string :tag "Password"))
177 (const :format "" :value :stream)
178 (choice :tag "Stream"
180 ,@mail-source-imap-streams))
182 (const :format "" :value :program)
183 (string :tag "Program"))
186 :value :authenticator)
187 (choice :tag "Authenticator"
189 ,@mail-source-imap-authenticators))
191 (const :format "" :value :mailbox)
192 (string :tag "Mailbox"
195 (const :format "" :value :predicate)
196 (string :tag "Predicate"
197 :value "UNSEEN UNDELETED"))
199 (const :format "" :value :fetchflag)
200 (string :tag "Fetchflag"
205 (boolean :tag "Dontexpunge"))
207 (const :format "" :value :plugged)
208 (boolean :tag "Plugged"))))
209 (cons :tag "Webmail server"
210 (const :format "" webmail)
211 (checklist :tag "Options" :greedy t
213 (const :format "" :value :subtype)
214 ;; Should be generated from
215 ;; `webmail-type-definition', but we
216 ;; can't require webmail without W3.
217 (choice :tag "Subtype"
225 (const :format "" :value :user)
226 (string :tag "User"))
228 (const :format "" :value :password)
229 (string :tag "Password"))
233 (boolean :tag "Dontexpunge"))
235 (const :format "" :value :plugged)
236 (boolean :tag "Plugged"))))))))
238 (defcustom mail-source-ignore-errors nil
239 "*Ignore errors when querying mail sources.
240 If nil, the user will be prompted when an error occurs. If non-nil,
241 the error will be ignored."
246 (defcustom mail-source-primary-source nil
247 "*Primary source for incoming mail.
248 If non-nil, this maildrop will be checked periodically for new mail."
252 (defcustom mail-source-flash t
253 "*If non-nil, flash periodically when mail is available."
257 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
258 "File where mail will be stored while processing it."
262 (defcustom mail-source-directory message-directory
263 "Directory where incoming mail source files (if any) will be stored."
267 (defcustom mail-source-default-file-modes 384
268 "Set the mode bits of all new mail files to this integer."
272 (defcustom mail-source-delete-incoming
273 ;; 10 ;; development versions
274 2 ;; released versions
275 "If non-nil, delete incoming files after handling.
276 If t, delete immediately, if nil, never delete. If a positive number, delete
277 files older than number of days.
279 Removing of old files happens in `mail-source-callback', i.e. no
280 old incoming files will be deleted unless you receive new mail.
281 You may also set this variable to nil and call
282 `mail-source-delete-old-incoming' interactively."
284 :version "22.2" ;; No Gnus / Gnus 5.10.10 (default changed)
285 :type '(choice (const :tag "immediately" t)
286 (const :tag "never" nil)
287 (integer :tag "days")))
289 (defcustom mail-source-delete-old-incoming-confirm t
290 "*If non-nil, ask for confirmation before deleting old incoming files.
291 This variable only applies when `mail-source-delete-incoming' is a positive
297 (defcustom mail-source-incoming-file-prefix "Incoming"
298 "Prefix for file name for storing incoming mail"
302 (defcustom mail-source-report-new-mail-interval 5
303 "Interval in minutes between checks for new mail."
307 (defcustom mail-source-idle-time-delay 5
308 "Number of idle seconds to wait before checking for new mail."
312 (defcustom mail-source-movemail-program nil
313 "If non-nil, name of program for fetching new mail."
316 :type '(choice (const nil) string))
318 ;;; Internal variables.
320 (defvar mail-source-string ""
321 "A dynamically bound string that says what the current mail source is.")
323 (defvar mail-source-new-mail-available nil
324 "Flag indicating when new mail is available.")
327 (defvar mail-source-common-keyword-map
329 "Mapping from keywords to default values.
330 Common keywords should be listed here.")
332 (defvar mail-source-keyword-map
337 (:path (or (getenv "MAIL")
338 (expand-file-name (user-login-name) rmail-spool-directory))))
345 (:predicate identity))
350 (:server (getenv "MAILHOST"))
352 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
356 (:authentication password))
358 (:path (or (getenv "MAILDIR") "~/Maildir/"))
359 (:subdirs ("cur" "new"))
362 (:server (getenv "MAILHOST"))
367 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
370 (:predicate "UNSEEN UNDELETED")
371 (:fetchflag "\\Deleted")
378 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
381 (:authentication password)))
382 "Mapping from keywords to default values.
383 All keywords that can be used must be listed here."))
385 (defvar mail-source-fetcher-alist
386 '((file mail-source-fetch-file)
387 (directory mail-source-fetch-directory)
388 (pop mail-source-fetch-pop)
389 (maildir mail-source-fetch-maildir)
390 (imap mail-source-fetch-imap)
391 (webmail mail-source-fetch-webmail))
392 "A mapping from source type to fetcher function.")
394 (defvar mail-source-password-cache nil)
396 (defvar mail-source-plugged t)
401 (defun mail-source-strip-keyword (keyword)
402 "Strip the leading colon off the KEYWORD."
403 (intern (substring (symbol-name keyword) 1))))
406 (defun mail-source-bind-1 (type)
407 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
409 (while (setq default (pop defaults))
410 (push (list (mail-source-strip-keyword (car default))
415 (defmacro mail-source-bind (type-source &rest body)
416 "Return a `let' form that binds all variables in source TYPE.
417 TYPE-SOURCE is a list where the first element is the TYPE, and
418 the second variable is the SOURCE.
419 At run time, the mail source specifier SOURCE will be inspected,
420 and the variables will be set according to it. Variables not
421 specified will be given default values.
423 After this is done, BODY will be executed in the scope
426 The variables bound and their default values are described by
427 the `mail-source-keyword-map' variable."
428 `(let ,(mail-source-bind-1 (car type-source))
429 (mail-source-set-1 ,(cadr type-source))
432 (put 'mail-source-bind 'lisp-indent-function 1)
433 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
435 (defun mail-source-set-1 (source)
436 (let* ((type (pop source))
437 (defaults (cdr (assq type mail-source-keyword-map)))
438 default value keyword)
439 (while (setq default (pop defaults))
440 (set (mail-source-strip-keyword (setq keyword (car default)))
441 (if (setq value (plist-get source keyword))
442 (mail-source-value value)
443 (mail-source-value (cadr default)))))))
446 (defun mail-source-bind-common-1 ()
447 (let* ((defaults mail-source-common-keyword-map)
449 (while (setq default (pop defaults))
450 (push (list (mail-source-strip-keyword (car default))
455 (defun mail-source-set-common-1 (source)
456 (let* ((type (pop source))
457 (defaults mail-source-common-keyword-map)
458 (defaults-1 (cdr (assq type mail-source-keyword-map)))
459 default value keyword)
460 (while (setq default (pop defaults))
461 (set (mail-source-strip-keyword (setq keyword (car default)))
462 (if (setq value (plist-get source keyword))
463 (mail-source-value value)
464 (if (setq value (assq keyword defaults-1))
465 (mail-source-value (cadr value))
466 (mail-source-value (cadr default))))))))
468 (defmacro mail-source-bind-common (source &rest body)
469 "Return a `let' form that binds all common variables.
470 See `mail-source-bind'."
471 `(let ,(mail-source-bind-common-1)
472 (mail-source-set-common-1 source)
475 (put 'mail-source-bind-common 'lisp-indent-function 1)
476 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
478 (defun mail-source-value (value)
479 "Return the value of VALUE."
486 (functionp (car value)))
488 ;; Just return the value.
492 (defun mail-source-fetch (source callback)
493 "Fetch mail from SOURCE and call CALLBACK zero or more times.
494 CALLBACK will be called with the name of the file where (some of)
495 the mail from SOURCE is put.
496 Return the number of files that were found."
497 (mail-source-bind-common source
498 (if (or mail-source-plugged plugged)
500 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
503 (error "%S is an invalid mail source specification" source))
504 ;; If there's anything in the crash box, we do it first.
505 (when (file-exists-p mail-source-crash-box)
506 (message "Processing mail from %s..." mail-source-crash-box)
507 (setq found (mail-source-callback
508 callback mail-source-crash-box)))
510 (if (or debug-on-quit debug-on-error)
511 (funcall function source callback)
513 (funcall function source callback)
515 (if (and (not mail-source-ignore-errors)
518 (format "Mail source %s error (%s). Continue? "
519 (if (memq ':password source)
520 (let ((s (copy-sequence source)))
521 (setcar (cdr (memq ':password s))
526 (error "Cannot get new mail"))
529 (defun mail-source-delete-old-incoming (&optional age confirm)
530 "Remove incoming files older than AGE days.
531 If CONFIRM is non-nil, ask for confirmation before removing a file."
533 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
534 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
535 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
537 (setq files (directory-files
538 mail-source-directory t
539 (concat mail-source-incoming-file-prefix "*"))
540 currday (* (car (current-time)) high2days)
541 currday (+ currday (* low2days (nth 1 (current-time)))))
543 (let* ((ffile (car files))
544 (bfile (gnus-replace-in-string
545 ffile "\\`.*/\\([^/]+\\)\\'" "\\1"))
546 (filetime (nth 5 (file-attributes ffile)))
547 (fileday (* (car filetime) high2days))
548 (fileday (+ fileday (* low2days (nth 1 filetime)))))
549 (setq files (cdr files))
550 (when (and (> (- currday fileday) diff)
551 (gnus-message 8 "File `%s' is older than %s day(s)"
554 (y-or-n-p (concat "Remove file `" bfile "'? "))))
555 (delete-file ffile))))))
557 (defun mail-source-callback (callback info)
558 "Call CALLBACK on the mail file, and then remove the mail file.
559 Pass INFO on to CALLBACK."
560 (if (or (not (file-exists-p mail-source-crash-box))
561 (zerop (nth 7 (file-attributes mail-source-crash-box))))
563 (when (file-exists-p mail-source-crash-box)
564 (delete-file mail-source-crash-box))
567 (funcall callback mail-source-crash-box info)
568 (when (file-exists-p mail-source-crash-box)
569 ;; Delete or move the incoming mail out of the way.
570 (if (eq mail-source-delete-incoming t)
571 (delete-file mail-source-crash-box)
575 mail-source-incoming-file-prefix
576 mail-source-directory))))
577 (unless (file-exists-p (file-name-directory incoming))
578 (make-directory (file-name-directory incoming) t))
579 (rename-file mail-source-crash-box incoming t)
580 ;; remove old incoming files?
581 (when (natnump mail-source-delete-incoming)
582 (mail-source-delete-old-incoming
583 mail-source-delete-incoming
584 mail-source-delete-old-incoming-confirm))))))))
586 (defun mail-source-movemail (from to)
587 "Move FROM to TO using movemail."
588 (if (not (file-writable-p to))
589 (error "Can't write to crash box %s. Not moving mail" to)
590 (let ((to (file-truename (expand-file-name to)))
592 (setq to (file-truename to)
593 from (file-truename from))
594 ;; Set TO if have not already done so, and rename or copy
595 ;; the file FROM to TO if and as appropriate.
598 ;; The crash box exists already.
600 ((not (file-exists-p from))
601 ;; There is no inbox.
603 ((zerop (nth 7 (file-attributes from)))
607 ;; If getting from mail spool directory, use movemail to move
608 ;; rather than just renaming, so as to interlock with the
612 (setq errors (generate-new-buffer " *mail source loss*"))
613 (let ((default-directory "/"))
619 (or mail-source-movemail-program
620 (expand-file-name "movemail" exec-directory))
621 nil errors nil from to)))))
622 (when (file-exists-p to)
623 (set-file-modes to mail-source-default-file-modes))
624 (if (and (or (not (buffer-modified-p errors))
625 (zerop (buffer-size errors)))
626 (and (numberp result)
628 ;; No output => movemail won.
631 ;; There may be a warning about older revisions. We
633 (goto-char (point-min))
634 (if (search-forward "older revision" nil t)
636 ;; Probably a real error.
637 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
638 (goto-char (point-max))
639 (skip-chars-backward " \t")
640 (delete-region (point) (point-max))
641 (goto-char (point-min))
642 (when (looking-at "movemail: ")
643 (delete-region (point-min) (match-end 0)))
644 ;; Result may be a signal description string.
646 (format "movemail: %s (%s return). Continue? "
647 (buffer-string) result))
648 (error "%s" (buffer-string)))
651 (buffer-name errors))
652 (kill-buffer errors))
653 ;; Return whether we moved successfully or not.
656 (defun mail-source-movemail-and-remove (from to)
657 "Move FROM to TO using movemail, then remove FROM if empty."
658 (or (not (mail-source-movemail from to))
659 (not (zerop (nth 7 (file-attributes from))))
662 (defun mail-source-fetch-with-program (program)
663 (eq 0 (call-process shell-file-name nil nil nil
664 shell-command-switch program)))
666 (defun mail-source-run-script (script spec &optional delay)
668 (if (functionp script)
670 (mail-source-call-script
671 (format-spec script spec))))
675 (defun mail-source-call-script (script)
676 (let ((background nil))
677 (when (string-match "& *$" script)
678 (setq script (substring script 0 (match-beginning 0))
680 (call-process shell-file-name nil background nil
681 shell-command-switch script)))
684 ;;; Different fetchers
687 (defun mail-source-fetch-file (source callback)
688 "Fetcher for single-file sources."
689 (mail-source-bind (file source)
690 (mail-source-run-script
691 prescript (format-spec-make ?t mail-source-crash-box)
693 (let ((mail-source-string (format "file:%s" path)))
694 (if (mail-source-movemail path mail-source-crash-box)
696 (mail-source-callback callback path)
697 (mail-source-run-script
698 postscript (format-spec-make ?t mail-source-crash-box)))
701 (defun mail-source-fetch-directory (source callback)
702 "Fetcher for directory sources."
703 (mail-source-bind (directory source)
704 (mail-source-run-script
705 prescript (format-spec-make ?t path) prescript-delay)
707 (mail-source-string (format "directory:%s" path)))
708 (dolist (file (directory-files
709 path t (concat (regexp-quote suffix) "$")))
710 (when (and (file-regular-p file)
711 (funcall predicate file)
712 (mail-source-movemail file mail-source-crash-box))
713 (incf found (mail-source-callback callback file))))
714 (mail-source-run-script postscript (format-spec-make ?t path))
717 (defun mail-source-fetch-pop (source callback)
718 "Fetcher for single-file sources."
719 (mail-source-bind (pop source)
720 (mail-source-run-script
722 (format-spec-make ?p password ?t mail-source-crash-box
723 ?s server ?P port ?u user)
725 (let ((from (format "%s:%s:%s" server user port))
726 (mail-source-string (format "pop:%s@%s" user server))
728 (when (eq authentication 'password)
731 (cdr (assoc from mail-source-password-cache))
733 (format "Password for %s at %s: " user server)))))
735 (setenv "MAILHOST" server))
739 (mail-source-fetch-with-program
742 (format-spec-make ?p password ?t mail-source-crash-box
743 ?s server ?P port ?u user))))
745 (funcall function mail-source-crash-box))
746 ;; The default is to use pop3.el.
749 (let ((pop3-password password)
751 (pop3-mailhost server)
753 (pop3-authentication-scheme
754 (if (eq authentication 'apop) 'apop 'pass)))
755 (if (or debug-on-quit debug-on-error)
756 (save-excursion (pop3-movemail mail-source-crash-box))
758 (save-excursion (pop3-movemail mail-source-crash-box))
760 ;; We nix out the password in case the error
761 ;; was because of a wrong password being given.
762 (setq mail-source-password-cache
763 (delq (assoc from mail-source-password-cache)
764 mail-source-password-cache))
765 (signal (car err) (cdr err)))))))))
768 (when (eq authentication 'password)
769 (unless (assoc from mail-source-password-cache)
770 (push (cons from password) mail-source-password-cache)))
772 (mail-source-callback callback server)
773 ;; Update display-time's mail flag, if relevant.
774 (if (equal source mail-source-primary-source)
775 (setq mail-source-new-mail-available nil))
776 (mail-source-run-script
778 (format-spec-make ?p password ?t mail-source-crash-box
779 ?s server ?P port ?u user))))
780 ;; We nix out the password in case the error
781 ;; was because of a wrong password being given.
782 (setq mail-source-password-cache
783 (delq (assoc from mail-source-password-cache)
784 mail-source-password-cache))
787 (defun mail-source-check-pop (source)
788 "Check whether there is new mail."
789 (mail-source-bind (pop source)
790 (let ((from (format "%s:%s:%s" server user port))
791 (mail-source-string (format "pop:%s@%s" user server))
793 (when (eq authentication 'password)
796 (cdr (assoc from mail-source-password-cache))
798 (format "Password for %s at %s: " user server))))
799 (unless (assoc from mail-source-password-cache)
800 (push (cons from password) mail-source-password-cache)))
802 (setenv "MAILHOST" server))
805 ;; No easy way to check whether mail is waiting for these.
808 ;; The default is to use pop3.el.
811 (let ((pop3-password password)
813 (pop3-mailhost server)
815 (pop3-authentication-scheme
816 (if (eq authentication 'apop) 'apop 'pass)))
817 (if (or debug-on-quit debug-on-error)
818 (save-excursion (pop3-get-message-count))
820 (save-excursion (pop3-get-message-count))
822 ;; We nix out the password in case the error
823 ;; was because of a wrong password being given.
824 (setq mail-source-password-cache
825 (delq (assoc from mail-source-password-cache)
826 mail-source-password-cache))
827 (signal (car err) (cdr err)))))))))
829 ;; Inform display-time that we have new mail.
830 (setq mail-source-new-mail-available (> result 0))
831 ;; We nix out the password in case the error
832 ;; was because of a wrong password being given.
833 (setq mail-source-password-cache
834 (delq (assoc from mail-source-password-cache)
835 mail-source-password-cache)))
838 (defun mail-source-touch-pop ()
839 "Open and close a POP connection shortly.
840 POP server should be defined in `mail-source-primary-source' (which is
841 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
842 authentication. To do that, you need to set the
843 `message-send-mail-function' variable as `message-smtpmail-send-it'
844 and put the following line in your ~/.gnus.el file:
846 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
848 See the Gnus manual for details."
849 (let ((sources (if mail-source-primary-source
850 (list mail-source-primary-source)
853 (if (eq 'pop (car (car sources)))
854 (mail-source-check-pop (car sources)))
855 (setq sources (cdr sources)))))
857 (defun mail-source-new-mail-p ()
858 "Handler for `display-time' to indicate when new mail is available."
859 ;; Flash (ie. ring the visible bell) if mail is available.
860 (if (and mail-source-flash mail-source-new-mail-available)
861 (let ((visible-bell t))
863 ;; Only report flag setting; flag is updated on a different schedule.
864 mail-source-new-mail-available)
867 (defvar mail-source-report-new-mail nil)
868 (defvar mail-source-report-new-mail-timer nil)
869 (defvar mail-source-report-new-mail-idle-timer nil)
872 (if (featurep 'xemacs)
873 (require 'timer-funcs)
876 (defun mail-source-start-idle-timer ()
877 ;; Start our idle timer if necessary, so we delay the check until the
878 ;; user isn't typing.
879 (unless mail-source-report-new-mail-idle-timer
880 (setq mail-source-report-new-mail-idle-timer
882 mail-source-idle-time-delay
886 (mail-source-check-pop mail-source-primary-source)
887 (setq mail-source-report-new-mail-idle-timer nil)))))
888 ;; Since idle timers created when Emacs is already in the idle
889 ;; state don't get activated until Emacs _next_ becomes idle, we
890 ;; need to force our timer to be considered active now. We do
891 ;; this by being naughty and poking the timer internals directly
892 ;; (element 0 of the vector is nil if the timer is active).
893 (aset mail-source-report-new-mail-idle-timer 0 nil)))
895 (defun mail-source-report-new-mail (arg)
896 "Toggle whether to report when new mail is available.
897 This only works when `display-time' is enabled."
899 (if (not mail-source-primary-source)
900 (error "Need to set `mail-source-primary-source' to check for new mail"))
901 (let ((on (if (null arg)
902 (not mail-source-report-new-mail)
903 (> (prefix-numeric-value arg) 0))))
904 (setq mail-source-report-new-mail on)
905 (and mail-source-report-new-mail-timer
906 (nnheader-cancel-timer mail-source-report-new-mail-timer))
907 (and mail-source-report-new-mail-idle-timer
908 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
909 (setq mail-source-report-new-mail-timer nil)
910 (setq mail-source-report-new-mail-idle-timer nil)
914 ;; display-time-mail-function is an Emacs 21 feature.
915 (setq display-time-mail-function #'mail-source-new-mail-p)
916 ;; Set up the main timer.
917 (setq mail-source-report-new-mail-timer
918 (nnheader-run-at-time
919 (* 60 mail-source-report-new-mail-interval)
920 (* 60 mail-source-report-new-mail-interval)
921 #'mail-source-start-idle-timer))
922 ;; When you get new mail, clear "Mail" from the mode line.
923 (add-hook 'nnmail-post-get-new-mail-hook
924 'display-time-event-handler)
925 (message "Mail check enabled"))
926 (setq display-time-mail-function nil)
927 (remove-hook 'nnmail-post-get-new-mail-hook
928 'display-time-event-handler)
929 (message "Mail check disabled"))))
931 (defun mail-source-fetch-maildir (source callback)
932 "Fetcher for maildir sources."
933 (mail-source-bind (maildir source)
936 (unless (string-match "/$" path)
937 (setq path (concat path "/")))
938 (dolist (subdir subdirs)
939 (when (file-directory-p (concat path subdir))
940 (setq mail-source-string (format "maildir:%s%s" path subdir))
941 (dolist (file (directory-files (concat path subdir) t))
942 (when (and (not (file-directory-p file))
944 (funcall function file mail-source-crash-box)
945 (let ((coding-system-for-write
946 mm-text-coding-system)
947 (coding-system-for-read
948 mm-text-coding-system))
949 (with-temp-file mail-source-crash-box
950 (insert-file-contents file)
951 (goto-char (point-min))
952 ;;; ;; Unix mail format
953 ;;; (unless (looking-at "\n*From ")
954 ;;; (insert "From maildir "
955 ;;; (current-time-string) "\n"))
956 ;;; (while (re-search-forward "^From " nil t)
957 ;;; (replace-match ">From "))
958 ;;; (goto-char (point-max))
961 (insert "\001\001\001\001\n"))
962 (delete-file file)))))
963 (incf found (mail-source-callback callback file))))))
967 (autoload 'imap-open "imap")
968 (autoload 'imap-authenticate "imap")
969 (autoload 'imap-mailbox-select "imap")
970 (autoload 'imap-mailbox-unselect "imap")
971 (autoload 'imap-mailbox-close "imap")
972 (autoload 'imap-search "imap")
973 (autoload 'imap-fetch "imap")
974 (autoload 'imap-close "imap")
975 (autoload 'imap-error-text "imap")
976 (autoload 'imap-message-flags-add "imap")
977 (autoload 'imap-list-to-message-set "imap")
978 (autoload 'imap-range-to-message-set "imap")
979 (autoload 'nnheader-ms-strip-cr "nnheader"))
981 (defvar mail-source-imap-file-coding-system 'binary
982 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
984 (defun mail-source-fetch-imap (source callback)
985 "Fetcher for imap sources."
986 (mail-source-bind (imap source)
987 (mail-source-run-script
988 prescript (format-spec-make ?p password ?t mail-source-crash-box
989 ?s server ?P port ?u user)
991 (let ((from (format "%s:%s:%s" server user port))
993 (buf (generate-new-buffer " *imap source*"))
994 (mail-source-string (format "imap:%s:%s" server mailbox))
995 (imap-shell-program (or (list program) imap-shell-program))
997 (if (and (imap-open server port stream authentication buf)
999 user (or (cdr (assoc from mail-source-password-cache))
1001 (imap-mailbox-select mailbox nil buf))
1002 (let ((coding-system-for-write mail-source-imap-file-coding-system)
1004 (with-temp-file mail-source-crash-box
1005 ;; Avoid converting 8-bit chars from inserted strings to
1007 (mm-disable-multibyte)
1008 ;; remember password
1009 (with-current-buffer buf
1010 (when (and imap-password
1011 (not (assoc from mail-source-password-cache)))
1012 (push (cons from imap-password) mail-source-password-cache)))
1013 ;; if predicate is nil, use all uids
1014 (dolist (uid (imap-search (or predicate "1:*") buf))
1016 (if (imap-capability 'IMAP4rev1 buf)
1017 (caddar (imap-fetch uid "BODY.PEEK[]"
1018 'BODYDETAIL nil buf))
1019 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
1021 (insert "From imap " (current-time-string) "\n")
1023 (insert str "\n\n"))
1024 (while (re-search-forward "^From " nil t)
1025 (replace-match ">From "))
1026 (goto-char (point-max))))
1027 (nnheader-ms-strip-cr))
1028 (incf found (mail-source-callback callback server))
1029 (when (and remove fetchflag)
1030 (setq remove (nreverse remove))
1031 (imap-message-flags-add
1032 (imap-range-to-message-set (gnus-compress-sequence remove))
1035 (imap-mailbox-unselect buf)
1036 (imap-mailbox-close nil buf))
1039 ;; We nix out the password in case the error
1040 ;; was because of a wrong password being given.
1041 (setq mail-source-password-cache
1042 (delq (assoc from mail-source-password-cache)
1043 mail-source-password-cache))
1044 (error "IMAP error: %s" (imap-error-text buf)))
1046 (mail-source-run-script
1048 (format-spec-make ?p password ?t mail-source-crash-box
1049 ?s server ?P port ?u user))
1053 (autoload 'webmail-fetch "webmail"))
1055 (defun mail-source-fetch-webmail (source callback)
1056 "Fetch for webmail source."
1057 (mail-source-bind (webmail source)
1058 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1059 (webmail-newmail-only dontexpunge)
1060 (webmail-move-to-trash-can (not dontexpunge)))
1061 (when (eq authentication 'password)
1064 (cdr (assoc (format "webmail:%s:%s" subtype user)
1065 mail-source-password-cache))
1067 (format "Password for %s at %s: " user subtype))))
1069 (not (assoc (format "webmail:%s:%s" subtype user)
1070 mail-source-password-cache)))
1071 (push (cons (format "webmail:%s:%s" subtype user) password)
1072 mail-source-password-cache)))
1073 (webmail-fetch mail-source-crash-box subtype user password)
1074 (mail-source-callback callback (symbol-name subtype)))))
1076 (provide 'mail-source)
1078 ;;; arch-tag: 72948025-1d17-4d6c-bb12-ef1aa2c490fd
1079 ;;; mail-source.el ends here