1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (defvar display-time-mail-function)))
33 (autoload 'pop3-movemail "pop3")
34 (autoload 'pop3-get-message-count "pop3")
35 (autoload 'nnheader-cancel-timer "nnheader")
36 (autoload 'nnheader-run-at-time "nnheader"))
37 (require 'format-spec)
39 (require 'message) ;; for `message-directory'
41 (defgroup mail-source nil
42 "The mail-fetching library."
46 ;; Define these at compile time to avoid dragging in imap always.
47 (defconst mail-source-imap-authenticators
50 (list 'const (car a)))
51 imap-authenticator-alist)))
52 (defconst mail-source-imap-streams
55 (list 'const (car a)))
58 (defcustom mail-sources nil
59 "*Where the mail backends will look for incoming mail.
60 This variable is a list of mail source specifiers.
61 See Info node `(gnus)Mail Source Specifiers'."
63 :link '(custom-manual "(gnus)Mail Source Specifiers")
65 (choice :format "%[Value Menu%] %v"
67 (cons :tag "Spool file"
68 (const :format "" file)
69 (checklist :tag "Options" :greedy t
71 (const :format "" :value :path)
73 (cons :tag "Several files in a directory"
74 (const :format "" directory)
75 (checklist :tag "Options" :greedy t
77 (const :format "" :value :path)
78 (directory :tag "Path"))
80 (const :format "" :value :suffix)
81 (string :tag "Suffix"))
83 (const :format "" :value :predicate)
84 (function :tag "Predicate"))
86 (const :format "" :value :prescript)
87 (choice :tag "Prescript"
90 (function :format "%v")))
92 (const :format "" :value :postscript)
93 (choice :tag "Postscript"
96 (function :format "%v")))
98 (const :format "" :value :plugged)
99 (boolean :tag "Plugged"))))
100 (cons :tag "POP3 server"
101 (const :format "" pop)
102 (checklist :tag "Options" :greedy t
104 (const :format "" :value :server)
105 (string :tag "Server"))
107 (const :format "" :value :port)
110 (number :format "%v")
111 (string :format "%v")))
113 (const :format "" :value :user)
114 (string :tag "User"))
116 (const :format "" :value :password)
117 (string :tag "Password"))
119 (const :format "" :value :program)
120 (string :tag "Program"))
122 (const :format "" :value :prescript)
123 (choice :tag "Prescript"
125 (string :format "%v")
126 (function :format "%v")))
128 (const :format "" :value :postscript)
129 (choice :tag "Postscript"
131 (string :format "%v")
132 (function :format "%v")))
134 (const :format "" :value :function)
135 (function :tag "Function"))
138 :value :authentication)
139 (choice :tag "Authentication"
144 (const :format "" :value :plugged)
145 (boolean :tag "Plugged"))))
146 (cons :tag "Maildir (qmail, postfix...)"
147 (const :format "" maildir)
148 (checklist :tag "Options" :greedy t
150 (const :format "" :value :path)
151 (directory :tag "Path"))
153 (const :format "" :value :plugged)
154 (boolean :tag "Plugged"))))
155 (cons :tag "IMAP server"
156 (const :format "" imap)
157 (checklist :tag "Options" :greedy t
159 (const :format "" :value :server)
160 (string :tag "Server"))
162 (const :format "" :value :port)
167 (const :format "" :value :user)
168 (string :tag "User"))
170 (const :format "" :value :password)
171 (string :tag "Password"))
173 (const :format "" :value :stream)
174 (choice :tag "Stream"
176 ,@mail-source-imap-streams))
178 (const :format "" :value :program)
179 (string :tag "Program"))
182 :value :authenticator)
183 (choice :tag "Authenticator"
185 ,@mail-source-imap-authenticators))
187 (const :format "" :value :mailbox)
188 (string :tag "Mailbox"
191 (const :format "" :value :predicate)
192 (string :tag "Predicate"
193 :value "UNSEEN UNDELETED"))
195 (const :format "" :value :fetchflag)
196 (string :tag "Fetchflag"
201 (boolean :tag "Dontexpunge"))
203 (const :format "" :value :plugged)
204 (boolean :tag "Plugged"))))
205 (cons :tag "Webmail server"
206 (const :format "" webmail)
207 (checklist :tag "Options" :greedy t
209 (const :format "" :value :subtype)
210 ;; Should be generated from
211 ;; `webmail-type-definition', but we
212 ;; can't require webmail without W3.
213 (choice :tag "Subtype"
221 (const :format "" :value :user)
222 (string :tag "User"))
224 (const :format "" :value :password)
225 (string :tag "Password"))
229 (boolean :tag "Dontexpunge"))
231 (const :format "" :value :plugged)
232 (boolean :tag "Plugged")))))))
234 (defcustom mail-source-ignore-errors nil
235 "*Ignore errors when querying mail sources.
236 If nil, the user will be prompted when an error occurs. If non-nil,
237 the error will be ignored.")
239 (defcustom mail-source-primary-source nil
240 "*Primary source for incoming mail.
241 If non-nil, this maildrop will be checked periodically for new mail."
245 (defcustom mail-source-flash t
246 "*If non-nil, flash periodically when mail is available."
250 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
251 "File where mail will be stored while processing it."
255 (defcustom mail-source-directory message-directory
256 "Directory where files (if any) will be stored."
260 (defcustom mail-source-default-file-modes 384
261 "Set the mode bits of all new mail files to this integer."
265 (defcustom mail-source-delete-incoming nil
266 "*If non-nil, delete incoming files after handling.
267 If t, delete immediately, if nil, never delete. If a positive number, delete
268 files older than number of days."
269 ;; Note: The removing happens in `mail-source-callback', i.e. no old
270 ;; incoming files will be deleted, unless you receive new mail.
272 ;; You may also set this to `nil' and call `mail-source-delete-old-incoming'
273 ;; from a hook or interactively.
275 :type '(choice (const :tag "immediately" t)
276 (const :tag "never" nil)
277 (integer :tag "days")))
279 (defcustom mail-source-delete-old-incoming-confirm t
280 "*If non-nil, ask for for confirmation before deleting old incoming files.
281 This variable only applies when `mail-source-delete-incoming' is a positive
286 (defcustom mail-source-incoming-file-prefix "Incoming"
287 "Prefix for file name for storing incoming mail"
291 (defcustom mail-source-report-new-mail-interval 5
292 "Interval in minutes between checks for new mail."
296 (defcustom mail-source-idle-time-delay 5
297 "Number of idle seconds to wait before checking for new mail."
301 (defcustom mail-source-movemail-program nil
302 "If non-nil, name of program for fetching new mail."
304 :type '(choice (const nil) string))
306 ;;; Internal variables.
308 (defvar mail-source-string ""
309 "A dynamically bound string that says what the current mail source is.")
311 (defvar mail-source-new-mail-available nil
312 "Flag indicating when new mail is available.")
315 (defvar mail-source-common-keyword-map
317 "Mapping from keywords to default values.
318 Common keywords should be listed here.")
320 (defvar mail-source-keyword-map
325 (:path (or (getenv "MAIL")
326 (expand-file-name (user-login-name) rmail-spool-directory))))
333 (:predicate identity))
338 (:server (getenv "MAILHOST"))
340 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
344 (:authentication password))
346 (:path (or (getenv "MAILDIR") "~/Maildir/"))
347 (:subdirs ("cur" "new"))
350 (:server (getenv "MAILHOST"))
355 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
358 (:predicate "UNSEEN UNDELETED")
359 (:fetchflag "\\Deleted")
366 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
369 (:authentication password)))
370 "Mapping from keywords to default values.
371 All keywords that can be used must be listed here."))
373 (defvar mail-source-fetcher-alist
374 '((file mail-source-fetch-file)
375 (directory mail-source-fetch-directory)
376 (pop mail-source-fetch-pop)
377 (maildir mail-source-fetch-maildir)
378 (imap mail-source-fetch-imap)
379 (webmail mail-source-fetch-webmail))
380 "A mapping from source type to fetcher function.")
382 (defvar mail-source-password-cache nil)
384 (defvar mail-source-plugged t)
389 (defun mail-source-strip-keyword (keyword)
390 "Strip the leading colon off the KEYWORD."
391 (intern (substring (symbol-name keyword) 1))))
394 (defun mail-source-bind-1 (type)
395 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
397 (while (setq default (pop defaults))
398 (push (list (mail-source-strip-keyword (car default))
403 (defmacro mail-source-bind (type-source &rest body)
404 "Return a `let' form that binds all variables in source TYPE.
405 TYPE-SOURCE is a list where the first element is the TYPE, and
406 the second variable is the SOURCE.
407 At run time, the mail source specifier SOURCE will be inspected,
408 and the variables will be set according to it. Variables not
409 specified will be given default values.
411 After this is done, BODY will be executed in the scope
414 The variables bound and their default values are described by
415 the `mail-source-keyword-map' variable."
416 `(let ,(mail-source-bind-1 (car type-source))
417 (mail-source-set-1 ,(cadr type-source))
420 (put 'mail-source-bind 'lisp-indent-function 1)
421 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
423 (defun mail-source-set-1 (source)
424 (let* ((type (pop source))
425 (defaults (cdr (assq type mail-source-keyword-map)))
426 default value keyword)
427 (while (setq default (pop defaults))
428 (set (mail-source-strip-keyword (setq keyword (car default)))
429 (if (setq value (plist-get source keyword))
430 (mail-source-value value)
431 (mail-source-value (cadr default)))))))
434 (defun mail-source-bind-common-1 ()
435 (let* ((defaults mail-source-common-keyword-map)
437 (while (setq default (pop defaults))
438 (push (list (mail-source-strip-keyword (car default))
443 (defun mail-source-set-common-1 (source)
444 (let* ((type (pop source))
445 (defaults mail-source-common-keyword-map)
446 (defaults-1 (cdr (assq type mail-source-keyword-map)))
447 default value keyword)
448 (while (setq default (pop defaults))
449 (set (mail-source-strip-keyword (setq keyword (car default)))
450 (if (setq value (plist-get source keyword))
451 (mail-source-value value)
452 (if (setq value (assq keyword defaults-1))
453 (mail-source-value (cadr value))
454 (mail-source-value (cadr default))))))))
456 (defmacro mail-source-bind-common (source &rest body)
457 "Return a `let' form that binds all common variables.
458 See `mail-source-bind'."
459 `(let ,(mail-source-bind-common-1)
460 (mail-source-set-common-1 source)
463 (put 'mail-source-bind-common 'lisp-indent-function 1)
464 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
466 (defun mail-source-value (value)
467 "Return the value of VALUE."
474 (functionp (car value)))
476 ;; Just return the value.
480 (defun mail-source-fetch (source callback)
481 "Fetch mail from SOURCE and call CALLBACK zero or more times.
482 CALLBACK will be called with the name of the file where (some of)
483 the mail from SOURCE is put.
484 Return the number of files that were found."
485 (mail-source-bind-common source
486 (if (or mail-source-plugged plugged)
488 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
491 (error "%S is an invalid mail source specification" source))
492 ;; If there's anything in the crash box, we do it first.
493 (when (file-exists-p mail-source-crash-box)
494 (message "Processing mail from %s..." mail-source-crash-box)
495 (setq found (mail-source-callback
496 callback mail-source-crash-box)))
498 (if (or debug-on-quit debug-on-error)
499 (funcall function source callback)
501 (funcall function source callback)
503 (if (and (not mail-source-ignore-errors)
506 (format "Mail source %s error (%s). Continue? "
507 (if (memq ':password source)
508 (let ((s (copy-sequence source)))
509 (setcar (cdr (memq ':password s))
514 (error "Cannot get new mail"))
517 (defun mail-source-delete-old-incoming (&optional age confirm)
518 "Remove incoming files older than AGE days.
519 If CONFIRM is non-nil, ask for confirmation before removing a file."
521 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
522 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
523 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
525 (setq files (directory-files
526 mail-source-directory t
527 (concat mail-source-incoming-file-prefix "*"))
528 currday (* (car (current-time)) high2days)
529 currday (+ currday (* low2days (nth 1 (current-time)))))
531 (let* ((ffile (car files))
532 (bfile (gnus-replace-in-string
533 ffile "\\`.*/\\([^/]+\\)\\'" "\\1"))
534 (filetime (nth 5 (file-attributes ffile)))
535 (fileday (* (car filetime) high2days))
536 (fileday (+ fileday (* low2days (nth 1 filetime)))))
537 (setq files (cdr files))
538 (when (and (> (- currday fileday) diff)
539 (gnus-message 8 "File `%s' is older than %s day(s)"
542 (y-or-n-p (concat "Remove file `" bfile "'? "))))
543 (delete-file ffile))))))
545 (defun mail-source-callback (callback info)
546 "Call CALLBACK on the mail file, and then remove the mail file.
547 Pass INFO on to CALLBACK."
548 (if (or (not (file-exists-p mail-source-crash-box))
549 (zerop (nth 7 (file-attributes mail-source-crash-box))))
551 (when (file-exists-p mail-source-crash-box)
552 (delete-file mail-source-crash-box))
555 (funcall callback mail-source-crash-box info)
556 (when (file-exists-p mail-source-crash-box)
557 ;; Delete or move the incoming mail out of the way.
558 (if (eq mail-source-delete-incoming t)
559 (delete-file mail-source-crash-box)
563 mail-source-incoming-file-prefix
564 mail-source-directory))))
565 (unless (file-exists-p (file-name-directory incoming))
566 (make-directory (file-name-directory incoming) t))
567 (rename-file mail-source-crash-box incoming t)
568 ;; remove old incoming files?
569 (when (natnump mail-source-delete-incoming)
570 (mail-source-delete-old-incoming
571 mail-source-delete-incoming
572 mail-source-delete-old-incoming-confirm))))))))
574 (defun mail-source-movemail (from to)
575 "Move FROM to TO using movemail."
576 (if (not (file-writable-p to))
577 (error "Can't write to crash box %s. Not moving mail" to)
578 (let ((to (file-truename (expand-file-name to)))
580 (setq to (file-truename to)
581 from (file-truename from))
582 ;; Set TO if have not already done so, and rename or copy
583 ;; the file FROM to TO if and as appropriate.
586 ;; The crash box exists already.
588 ((not (file-exists-p from))
589 ;; There is no inbox.
591 ((zerop (nth 7 (file-attributes from)))
595 ;; If getting from mail spool directory, use movemail to move
596 ;; rather than just renaming, so as to interlock with the
600 (setq errors (generate-new-buffer " *mail source loss*"))
601 (let ((default-directory "/"))
607 (or mail-source-movemail-program
608 (expand-file-name "movemail" exec-directory))
609 nil errors nil from to)))))
610 (when (file-exists-p to)
611 (set-file-modes to mail-source-default-file-modes))
612 (if (and (or (not (buffer-modified-p errors))
613 (zerop (buffer-size errors)))
614 (and (numberp result)
616 ;; No output => movemail won.
619 ;; There may be a warning about older revisions. We
621 (goto-char (point-min))
622 (if (search-forward "older revision" nil t)
624 ;; Probably a real error.
625 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
626 (goto-char (point-max))
627 (skip-chars-backward " \t")
628 (delete-region (point) (point-max))
629 (goto-char (point-min))
630 (when (looking-at "movemail: ")
631 (delete-region (point-min) (match-end 0)))
632 ;; Result may be a signal description string.
634 (format "movemail: %s (%s return). Continue? "
635 (buffer-string) result))
636 (error "%s" (buffer-string)))
639 (buffer-name errors))
640 (kill-buffer errors))
641 ;; Return whether we moved successfully or not.
644 (defun mail-source-movemail-and-remove (from to)
645 "Move FROM to TO using movemail, then remove FROM if empty."
646 (or (not (mail-source-movemail from to))
647 (not (zerop (nth 7 (file-attributes from))))
650 (defun mail-source-fetch-with-program (program)
651 (eq 0 (call-process shell-file-name nil nil nil
652 shell-command-switch program)))
654 (defun mail-source-run-script (script spec &optional delay)
656 (if (functionp script)
658 (mail-source-call-script
659 (format-spec script spec))))
663 (defun mail-source-call-script (script)
664 (let ((background nil))
665 (when (string-match "& *$" script)
666 (setq script (substring script 0 (match-beginning 0))
668 (call-process shell-file-name nil background nil
669 shell-command-switch script)))
672 ;;; Different fetchers
675 (defun mail-source-fetch-file (source callback)
676 "Fetcher for single-file sources."
677 (mail-source-bind (file source)
678 (mail-source-run-script
679 prescript (format-spec-make ?t mail-source-crash-box)
681 (let ((mail-source-string (format "file:%s" path)))
682 (if (mail-source-movemail path mail-source-crash-box)
684 (mail-source-callback callback path)
685 (mail-source-run-script
686 postscript (format-spec-make ?t mail-source-crash-box)))
689 (defun mail-source-fetch-directory (source callback)
690 "Fetcher for directory sources."
691 (mail-source-bind (directory source)
692 (mail-source-run-script
693 prescript (format-spec-make ?t path) prescript-delay)
695 (mail-source-string (format "directory:%s" path)))
696 (dolist (file (directory-files
697 path t (concat (regexp-quote suffix) "$")))
698 (when (and (file-regular-p file)
699 (funcall predicate file)
700 (mail-source-movemail file mail-source-crash-box))
701 (incf found (mail-source-callback callback file))))
702 (mail-source-run-script postscript (format-spec-make ?t path))
705 (defun mail-source-fetch-pop (source callback)
706 "Fetcher for single-file sources."
707 (mail-source-bind (pop source)
708 (mail-source-run-script
710 (format-spec-make ?p password ?t mail-source-crash-box
711 ?s server ?P port ?u user)
713 (let ((from (format "%s:%s:%s" server user port))
714 (mail-source-string (format "pop:%s@%s" user server))
716 (when (eq authentication 'password)
719 (cdr (assoc from mail-source-password-cache))
721 (format "Password for %s at %s: " user server)))))
723 (setenv "MAILHOST" server))
727 (mail-source-fetch-with-program
730 (format-spec-make ?p password ?t mail-source-crash-box
731 ?s server ?P port ?u user))))
733 (funcall function mail-source-crash-box))
734 ;; The default is to use pop3.el.
736 (let ((pop3-password password)
738 (pop3-mailhost server)
740 (pop3-authentication-scheme
741 (if (eq authentication 'apop) 'apop 'pass)))
742 (if (or debug-on-quit debug-on-error)
743 (save-excursion (pop3-movemail mail-source-crash-box))
745 (save-excursion (pop3-movemail mail-source-crash-box))
747 ;; We nix out the password in case the error
748 ;; was because of a wrong password being given.
749 (setq mail-source-password-cache
750 (delq (assoc from mail-source-password-cache)
751 mail-source-password-cache))
752 (signal (car err) (cdr err)))))))))
755 (when (eq authentication 'password)
756 (unless (assoc from mail-source-password-cache)
757 (push (cons from password) mail-source-password-cache)))
759 (mail-source-callback callback server)
760 ;; Update display-time's mail flag, if relevant.
761 (if (equal source mail-source-primary-source)
762 (setq mail-source-new-mail-available nil))
763 (mail-source-run-script
765 (format-spec-make ?p password ?t mail-source-crash-box
766 ?s server ?P port ?u user))))
767 ;; We nix out the password in case the error
768 ;; was because of a wrong password being given.
769 (setq mail-source-password-cache
770 (delq (assoc from mail-source-password-cache)
771 mail-source-password-cache))
774 (defun mail-source-check-pop (source)
775 "Check whether there is new mail."
776 (mail-source-bind (pop source)
777 (let ((from (format "%s:%s:%s" server user port))
778 (mail-source-string (format "pop:%s@%s" user server))
780 (when (eq authentication 'password)
783 (cdr (assoc from mail-source-password-cache))
785 (format "Password for %s at %s: " user server))))
786 (unless (assoc from mail-source-password-cache)
787 (push (cons from password) mail-source-password-cache)))
789 (setenv "MAILHOST" server))
792 ;; No easy way to check whether mail is waiting for these.
795 ;; The default is to use pop3.el.
797 (let ((pop3-password password)
799 (pop3-mailhost server)
801 (pop3-authentication-scheme
802 (if (eq authentication 'apop) 'apop 'pass)))
803 (if (or debug-on-quit debug-on-error)
804 (save-excursion (pop3-get-message-count))
806 (save-excursion (pop3-get-message-count))
808 ;; We nix out the password in case the error
809 ;; was because of a wrong password being given.
810 (setq mail-source-password-cache
811 (delq (assoc from mail-source-password-cache)
812 mail-source-password-cache))
813 (signal (car err) (cdr err)))))))))
815 ;; Inform display-time that we have new mail.
816 (setq mail-source-new-mail-available (> result 0))
817 ;; We nix out the password in case the error
818 ;; was because of a wrong password being given.
819 (setq mail-source-password-cache
820 (delq (assoc from mail-source-password-cache)
821 mail-source-password-cache)))
824 (defun mail-source-touch-pop ()
825 "Open and close a POP connection shortly.
826 POP server should be defined in `mail-source-primary-source' (which is
827 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
828 authentication. To do that, you need to set the option
829 `message-send-mail-function' to `message-smtpmail-send-it' and put the
830 following line in .gnus file:
832 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
834 (let ((sources (if mail-source-primary-source
835 (list mail-source-primary-source)
838 (if (eq 'pop (car (car sources)))
839 (mail-source-check-pop (car sources)))
840 (setq sources (cdr sources)))))
842 (defun mail-source-new-mail-p ()
843 "Handler for `display-time' to indicate when new mail is available."
844 ;; Flash (ie. ring the visible bell) if mail is available.
845 (if (and mail-source-flash mail-source-new-mail-available)
846 (let ((visible-bell t))
848 ;; Only report flag setting; flag is updated on a different schedule.
849 mail-source-new-mail-available)
852 (defvar mail-source-report-new-mail nil)
853 (defvar mail-source-report-new-mail-timer nil)
854 (defvar mail-source-report-new-mail-idle-timer nil)
857 (if (featurep 'xemacs)
861 (defun mail-source-start-idle-timer ()
862 ;; Start our idle timer if necessary, so we delay the check until the
863 ;; user isn't typing.
864 (unless mail-source-report-new-mail-idle-timer
865 (setq mail-source-report-new-mail-idle-timer
867 mail-source-idle-time-delay
871 (mail-source-check-pop mail-source-primary-source)
872 (setq mail-source-report-new-mail-idle-timer nil)))))
873 ;; Since idle timers created when Emacs is already in the idle
874 ;; state don't get activated until Emacs _next_ becomes idle, we
875 ;; need to force our timer to be considered active now. We do
876 ;; this by being naughty and poking the timer internals directly
877 ;; (element 0 of the vector is nil if the timer is active).
878 (aset mail-source-report-new-mail-idle-timer 0 nil)))
880 (defun mail-source-report-new-mail (arg)
881 "Toggle whether to report when new mail is available.
882 This only works when `display-time' is enabled."
884 (if (not mail-source-primary-source)
885 (error "Need to set `mail-source-primary-source' to check for new mail"))
886 (let ((on (if (null arg)
887 (not mail-source-report-new-mail)
888 (> (prefix-numeric-value arg) 0))))
889 (setq mail-source-report-new-mail on)
890 (and mail-source-report-new-mail-timer
891 (nnheader-cancel-timer mail-source-report-new-mail-timer))
892 (and mail-source-report-new-mail-idle-timer
893 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
894 (setq mail-source-report-new-mail-timer nil)
895 (setq mail-source-report-new-mail-idle-timer nil)
899 ;; display-time-mail-function is an Emacs 21 feature.
900 (setq display-time-mail-function #'mail-source-new-mail-p)
901 ;; Set up the main timer.
902 (setq mail-source-report-new-mail-timer
903 (nnheader-run-at-time
904 (* 60 mail-source-report-new-mail-interval)
905 (* 60 mail-source-report-new-mail-interval)
906 #'mail-source-start-idle-timer))
907 ;; When you get new mail, clear "Mail" from the mode line.
908 (add-hook 'nnmail-post-get-new-mail-hook
909 'display-time-event-handler)
910 (message "Mail check enabled"))
911 (setq display-time-mail-function nil)
912 (remove-hook 'nnmail-post-get-new-mail-hook
913 'display-time-event-handler)
914 (message "Mail check disabled"))))
916 (defun mail-source-fetch-maildir (source callback)
917 "Fetcher for maildir sources."
918 (mail-source-bind (maildir source)
921 (unless (string-match "/$" path)
922 (setq path (concat path "/")))
923 (dolist (subdir subdirs)
924 (when (file-directory-p (concat path subdir))
925 (setq mail-source-string (format "maildir:%s%s" path subdir))
926 (dolist (file (directory-files (concat path subdir) t))
927 (when (and (not (file-directory-p file))
929 (funcall function file mail-source-crash-box)
930 (let ((coding-system-for-write
931 mm-text-coding-system)
932 (coding-system-for-read
933 mm-text-coding-system))
934 (with-temp-file mail-source-crash-box
935 (insert-file-contents file)
936 (goto-char (point-min))
937 ;;; ;; Unix mail format
938 ;;; (unless (looking-at "\n*From ")
939 ;;; (insert "From maildir "
940 ;;; (current-time-string) "\n"))
941 ;;; (while (re-search-forward "^From " nil t)
942 ;;; (replace-match ">From "))
943 ;;; (goto-char (point-max))
946 (insert "\001\001\001\001\n"))
947 (delete-file file)))))
948 (incf found (mail-source-callback callback file))))))
952 (autoload 'imap-open "imap")
953 (autoload 'imap-authenticate "imap")
954 (autoload 'imap-mailbox-select "imap")
955 (autoload 'imap-mailbox-unselect "imap")
956 (autoload 'imap-mailbox-close "imap")
957 (autoload 'imap-search "imap")
958 (autoload 'imap-fetch "imap")
959 (autoload 'imap-close "imap")
960 (autoload 'imap-error-text "imap")
961 (autoload 'imap-message-flags-add "imap")
962 (autoload 'imap-list-to-message-set "imap")
963 (autoload 'imap-range-to-message-set "imap")
964 (autoload 'nnheader-ms-strip-cr "nnheader"))
966 (defvar mail-source-imap-file-coding-system 'binary
967 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
969 (defun mail-source-fetch-imap (source callback)
970 "Fetcher for imap sources."
971 (mail-source-bind (imap source)
972 (mail-source-run-script
973 prescript (format-spec-make ?p password ?t mail-source-crash-box
974 ?s server ?P port ?u user)
976 (let ((from (format "%s:%s:%s" server user port))
978 (buf (generate-new-buffer " *imap source*"))
979 (mail-source-string (format "imap:%s:%s" server mailbox))
980 (imap-shell-program (or (list program) imap-shell-program))
982 (if (and (imap-open server port stream authentication buf)
984 user (or (cdr (assoc from mail-source-password-cache))
986 (imap-mailbox-select mailbox nil buf))
987 (let ((coding-system-for-write mail-source-imap-file-coding-system)
989 (with-temp-file mail-source-crash-box
990 ;; Avoid converting 8-bit chars from inserted strings to
992 (mm-disable-multibyte)
994 (with-current-buffer buf
995 (when (or imap-password
996 (assoc from mail-source-password-cache))
997 (push (cons from imap-password) mail-source-password-cache)))
998 ;; if predicate is nil, use all uids
999 (dolist (uid (imap-search (or predicate "1:*") buf))
1001 (if (imap-capability 'IMAP4rev1 buf)
1002 (caddar (imap-fetch uid "BODY.PEEK[]"
1003 'BODYDETAIL nil buf))
1004 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
1006 (insert "From imap " (current-time-string) "\n")
1008 (insert str "\n\n"))
1009 (while (re-search-forward "^From " nil t)
1010 (replace-match ">From "))
1011 (goto-char (point-max))))
1012 (nnheader-ms-strip-cr))
1013 (incf found (mail-source-callback callback server))
1014 (when (and remove fetchflag)
1015 (setq remove (nreverse remove))
1016 (imap-message-flags-add
1017 (imap-range-to-message-set (gnus-compress-sequence remove))
1020 (imap-mailbox-unselect buf)
1021 (imap-mailbox-close nil buf))
1024 ;; We nix out the password in case the error
1025 ;; was because of a wrong password being given.
1026 (setq mail-source-password-cache
1027 (delq (assoc from mail-source-password-cache)
1028 mail-source-password-cache))
1029 (error "IMAP error: %s" (imap-error-text buf)))
1031 (mail-source-run-script
1033 (format-spec-make ?p password ?t mail-source-crash-box
1034 ?s server ?P port ?u user))
1038 (autoload 'webmail-fetch "webmail"))
1040 (defun mail-source-fetch-webmail (source callback)
1041 "Fetch for webmail source."
1042 (mail-source-bind (webmail source)
1043 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1044 (webmail-newmail-only dontexpunge)
1045 (webmail-move-to-trash-can (not dontexpunge)))
1046 (when (eq authentication 'password)
1049 (cdr (assoc (format "webmail:%s:%s" subtype user)
1050 mail-source-password-cache))
1052 (format "Password for %s at %s: " user subtype))))
1054 (not (assoc (format "webmail:%s:%s" subtype user)
1055 mail-source-password-cache)))
1056 (push (cons (format "webmail:%s:%s" subtype user) password)
1057 mail-source-password-cache)))
1058 (webmail-fetch mail-source-crash-box subtype user password)
1059 (mail-source-callback callback (symbol-name subtype)))))
1061 (provide 'mail-source)
1063 ;;; mail-source.el ends here