1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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 2, 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")
67 (choice :format "%[Value Menu%] %v"
69 (cons :tag "Spool file"
70 (const :format "" file)
71 (checklist :tag "Options" :greedy t
73 (const :format "" :value :path)
75 (cons :tag "Several files in a directory"
76 (const :format "" directory)
77 (checklist :tag "Options" :greedy t
79 (const :format "" :value :path)
80 (directory :tag "Path"))
82 (const :format "" :value :suffix)
83 (string :tag "Suffix"))
85 (const :format "" :value :predicate)
86 (function :tag "Predicate"))
88 (const :format "" :value :prescript)
89 (choice :tag "Prescript"
92 (function :format "%v")))
94 (const :format "" :value :postscript)
95 (choice :tag "Postscript"
98 (function :format "%v")))
100 (const :format "" :value :plugged)
101 (boolean :tag "Plugged"))))
102 (cons :tag "POP3 server"
103 (const :format "" pop)
104 (checklist :tag "Options" :greedy t
106 (const :format "" :value :server)
107 (string :tag "Server"))
109 (const :format "" :value :port)
112 (number :format "%v")
113 (string :format "%v")))
115 (const :format "" :value :user)
116 (string :tag "User"))
118 (const :format "" :value :password)
119 (string :tag "Password"))
121 (const :format "" :value :program)
122 (string :tag "Program"))
124 (const :format "" :value :prescript)
125 (choice :tag "Prescript"
127 (string :format "%v")
128 (function :format "%v")))
130 (const :format "" :value :postscript)
131 (choice :tag "Postscript"
133 (string :format "%v")
134 (function :format "%v")))
136 (const :format "" :value :function)
137 (function :tag "Function"))
140 :value :authentication)
141 (choice :tag "Authentication"
146 (const :format "" :value :plugged)
147 (boolean :tag "Plugged"))))
148 (cons :tag "Maildir (qmail, postfix...)"
149 (const :format "" maildir)
150 (checklist :tag "Options" :greedy t
152 (const :format "" :value :path)
153 (directory :tag "Path"))
155 (const :format "" :value :plugged)
156 (boolean :tag "Plugged"))))
157 (cons :tag "IMAP server"
158 (const :format "" imap)
159 (checklist :tag "Options" :greedy t
161 (const :format "" :value :server)
162 (string :tag "Server"))
164 (const :format "" :value :port)
169 (const :format "" :value :user)
170 (string :tag "User"))
172 (const :format "" :value :password)
173 (string :tag "Password"))
175 (const :format "" :value :stream)
176 (choice :tag "Stream"
178 ,@mail-source-imap-streams))
180 (const :format "" :value :program)
181 (string :tag "Program"))
184 :value :authenticator)
185 (choice :tag "Authenticator"
187 ,@mail-source-imap-authenticators))
189 (const :format "" :value :mailbox)
190 (string :tag "Mailbox"
193 (const :format "" :value :predicate)
194 (string :tag "Predicate"
195 :value "UNSEEN UNDELETED"))
197 (const :format "" :value :fetchflag)
198 (string :tag "Fetchflag"
203 (boolean :tag "Dontexpunge"))
205 (const :format "" :value :plugged)
206 (boolean :tag "Plugged"))))
207 (cons :tag "Webmail server"
208 (const :format "" webmail)
209 (checklist :tag "Options" :greedy t
211 (const :format "" :value :subtype)
212 ;; Should be generated from
213 ;; `webmail-type-definition', but we
214 ;; can't require webmail without W3.
215 (choice :tag "Subtype"
223 (const :format "" :value :user)
224 (string :tag "User"))
226 (const :format "" :value :password)
227 (string :tag "Password"))
231 (boolean :tag "Dontexpunge"))
233 (const :format "" :value :plugged)
234 (boolean :tag "Plugged")))))))
236 (defcustom mail-source-ignore-errors nil
237 "*Ignore errors when querying mail sources.
238 If nil, the user will be prompted when an error occurs. If non-nil,
239 the error will be ignored."
244 (defcustom mail-source-primary-source nil
245 "*Primary source for incoming mail.
246 If non-nil, this maildrop will be checked periodically for new mail."
250 (defcustom mail-source-flash t
251 "*If non-nil, flash periodically when mail is available."
255 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
256 "File where mail will be stored while processing it."
260 (defcustom mail-source-directory message-directory
261 "Directory where incoming mail source files (if any) will be stored."
265 (defcustom mail-source-default-file-modes 384
266 "Set the mode bits of all new mail files to this integer."
270 (defcustom mail-source-delete-incoming t
271 "*If non-nil, delete incoming files after handling.
272 If t, delete immediately, if nil, never delete. If a positive number, delete
273 files older than number of days."
274 ;; Note: The removing happens in `mail-source-callback', i.e. no old
275 ;; incoming files will be deleted, unless you receive new mail.
277 ;; You may also set this to `nil' and call `mail-source-delete-old-incoming'
278 ;; from a hook or interactively.
280 :type '(choice (const :tag "immediately" t)
281 (const :tag "never" nil)
282 (integer :tag "days")))
284 (defcustom mail-source-delete-old-incoming-confirm t
285 "*If non-nil, ask for for confirmation before deleting old incoming files.
286 This variable only applies when `mail-source-delete-incoming' is a positive
292 (defcustom mail-source-incoming-file-prefix "Incoming"
293 "Prefix for file name for storing incoming mail"
297 (defcustom mail-source-report-new-mail-interval 5
298 "Interval in minutes between checks for new mail."
302 (defcustom mail-source-idle-time-delay 5
303 "Number of idle seconds to wait before checking for new mail."
307 (defcustom mail-source-movemail-program nil
308 "If non-nil, name of program for fetching new mail."
311 :type '(choice (const nil) string))
313 ;;; Internal variables.
315 (defvar mail-source-string ""
316 "A dynamically bound string that says what the current mail source is.")
318 (defvar mail-source-new-mail-available nil
319 "Flag indicating when new mail is available.")
322 (defvar mail-source-common-keyword-map
324 "Mapping from keywords to default values.
325 Common keywords should be listed here.")
327 (defvar mail-source-keyword-map
332 (:path (or (getenv "MAIL")
333 (expand-file-name (user-login-name) rmail-spool-directory))))
340 (:predicate identity))
345 (:server (getenv "MAILHOST"))
347 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
351 (:authentication password))
353 (:path (or (getenv "MAILDIR") "~/Maildir/"))
354 (:subdirs ("cur" "new"))
357 (:server (getenv "MAILHOST"))
362 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
365 (:predicate "UNSEEN UNDELETED")
366 (:fetchflag "\\Deleted")
373 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
376 (:authentication password)))
377 "Mapping from keywords to default values.
378 All keywords that can be used must be listed here."))
380 (defvar mail-source-fetcher-alist
381 '((file mail-source-fetch-file)
382 (directory mail-source-fetch-directory)
383 (pop mail-source-fetch-pop)
384 (maildir mail-source-fetch-maildir)
385 (imap mail-source-fetch-imap)
386 (webmail mail-source-fetch-webmail))
387 "A mapping from source type to fetcher function.")
389 (defvar mail-source-password-cache nil)
391 (defvar mail-source-plugged t)
396 (defun mail-source-strip-keyword (keyword)
397 "Strip the leading colon off the KEYWORD."
398 (intern (substring (symbol-name keyword) 1))))
401 (defun mail-source-bind-1 (type)
402 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
404 (while (setq default (pop defaults))
405 (push (list (mail-source-strip-keyword (car default))
410 (defmacro mail-source-bind (type-source &rest body)
411 "Return a `let' form that binds all variables in source TYPE.
412 TYPE-SOURCE is a list where the first element is the TYPE, and
413 the second variable is the SOURCE.
414 At run time, the mail source specifier SOURCE will be inspected,
415 and the variables will be set according to it. Variables not
416 specified will be given default values.
418 After this is done, BODY will be executed in the scope
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)))
433 default value keyword)
434 (while (setq default (pop defaults))
435 (set (mail-source-strip-keyword (setq keyword (car default)))
436 (if (setq value (plist-get source keyword))
437 (mail-source-value value)
438 (mail-source-value (cadr default)))))))
441 (defun mail-source-bind-common-1 ()
442 (let* ((defaults mail-source-common-keyword-map)
444 (while (setq default (pop defaults))
445 (push (list (mail-source-strip-keyword (car default))
450 (defun mail-source-set-common-1 (source)
451 (let* ((type (pop source))
452 (defaults mail-source-common-keyword-map)
453 (defaults-1 (cdr (assq type mail-source-keyword-map)))
454 default value keyword)
455 (while (setq default (pop defaults))
456 (set (mail-source-strip-keyword (setq keyword (car default)))
457 (if (setq value (plist-get source keyword))
458 (mail-source-value value)
459 (if (setq value (assq keyword defaults-1))
460 (mail-source-value (cadr value))
461 (mail-source-value (cadr default))))))))
463 (defmacro mail-source-bind-common (source &rest body)
464 "Return a `let' form that binds all common variables.
465 See `mail-source-bind'."
466 `(let ,(mail-source-bind-common-1)
467 (mail-source-set-common-1 source)
470 (put 'mail-source-bind-common 'lisp-indent-function 1)
471 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
473 (defun mail-source-value (value)
474 "Return the value of VALUE."
481 (functionp (car value)))
483 ;; Just return the value.
487 (defun mail-source-fetch (source callback)
488 "Fetch mail from SOURCE and call CALLBACK zero or more times.
489 CALLBACK will be called with the name of the file where (some of)
490 the mail from SOURCE is put.
491 Return the number of files that were found."
492 (mail-source-bind-common source
493 (if (or mail-source-plugged plugged)
495 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
498 (error "%S is an invalid mail source specification" source))
499 ;; If there's anything in the crash box, we do it first.
500 (when (file-exists-p mail-source-crash-box)
501 (message "Processing mail from %s..." mail-source-crash-box)
502 (setq found (mail-source-callback
503 callback mail-source-crash-box)))
505 (if (or debug-on-quit debug-on-error)
506 (funcall function source callback)
508 (funcall function source callback)
510 (if (and (not mail-source-ignore-errors)
513 (format "Mail source %s error (%s). Continue? "
514 (if (memq ':password source)
515 (let ((s (copy-sequence source)))
516 (setcar (cdr (memq ':password s))
521 (error "Cannot get new mail"))
524 (defun mail-source-delete-old-incoming (&optional age confirm)
525 "Remove incoming files older than AGE days.
526 If CONFIRM is non-nil, ask for confirmation before removing a file."
528 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
529 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
530 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
532 (setq files (directory-files
533 mail-source-directory t
534 (concat mail-source-incoming-file-prefix "*"))
535 currday (* (car (current-time)) high2days)
536 currday (+ currday (* low2days (nth 1 (current-time)))))
538 (let* ((ffile (car files))
539 (bfile (gnus-replace-in-string
540 ffile "\\`.*/\\([^/]+\\)\\'" "\\1"))
541 (filetime (nth 5 (file-attributes ffile)))
542 (fileday (* (car filetime) high2days))
543 (fileday (+ fileday (* low2days (nth 1 filetime)))))
544 (setq files (cdr files))
545 (when (and (> (- currday fileday) diff)
546 (gnus-message 8 "File `%s' is older than %s day(s)"
549 (y-or-n-p (concat "Remove file `" bfile "'? "))))
550 (delete-file ffile))))))
552 (defun mail-source-callback (callback info)
553 "Call CALLBACK on the mail file, and then remove the mail file.
554 Pass INFO on to CALLBACK."
555 (if (or (not (file-exists-p mail-source-crash-box))
556 (zerop (nth 7 (file-attributes mail-source-crash-box))))
558 (when (file-exists-p mail-source-crash-box)
559 (delete-file mail-source-crash-box))
562 (funcall callback mail-source-crash-box info)
563 (when (file-exists-p mail-source-crash-box)
564 ;; Delete or move the incoming mail out of the way.
565 (if (eq mail-source-delete-incoming t)
566 (delete-file mail-source-crash-box)
570 mail-source-incoming-file-prefix
571 mail-source-directory))))
572 (unless (file-exists-p (file-name-directory incoming))
573 (make-directory (file-name-directory incoming) t))
574 (rename-file mail-source-crash-box incoming t)
575 ;; remove old incoming files?
576 (when (natnump mail-source-delete-incoming)
577 (mail-source-delete-old-incoming
578 mail-source-delete-incoming
579 mail-source-delete-old-incoming-confirm))))))))
581 (defun mail-source-movemail (from to)
582 "Move FROM to TO using movemail."
583 (if (not (file-writable-p to))
584 (error "Can't write to crash box %s. Not moving mail" to)
585 (let ((to (file-truename (expand-file-name to)))
587 (setq to (file-truename to)
588 from (file-truename from))
589 ;; Set TO if have not already done so, and rename or copy
590 ;; the file FROM to TO if and as appropriate.
593 ;; The crash box exists already.
595 ((not (file-exists-p from))
596 ;; There is no inbox.
598 ((zerop (nth 7 (file-attributes from)))
602 ;; If getting from mail spool directory, use movemail to move
603 ;; rather than just renaming, so as to interlock with the
607 (setq errors (generate-new-buffer " *mail source loss*"))
608 (let ((default-directory "/"))
614 (or mail-source-movemail-program
615 (expand-file-name "movemail" exec-directory))
616 nil errors nil from to)))))
617 (when (file-exists-p to)
618 (set-file-modes to mail-source-default-file-modes))
619 (if (and (or (not (buffer-modified-p errors))
620 (zerop (buffer-size errors)))
621 (and (numberp result)
623 ;; No output => movemail won.
626 ;; There may be a warning about older revisions. We
628 (goto-char (point-min))
629 (if (search-forward "older revision" nil t)
631 ;; Probably a real error.
632 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
633 (goto-char (point-max))
634 (skip-chars-backward " \t")
635 (delete-region (point) (point-max))
636 (goto-char (point-min))
637 (when (looking-at "movemail: ")
638 (delete-region (point-min) (match-end 0)))
639 ;; Result may be a signal description string.
641 (format "movemail: %s (%s return). Continue? "
642 (buffer-string) result))
643 (error "%s" (buffer-string)))
646 (buffer-name errors))
647 (kill-buffer errors))
648 ;; Return whether we moved successfully or not.
651 (defun mail-source-movemail-and-remove (from to)
652 "Move FROM to TO using movemail, then remove FROM if empty."
653 (or (not (mail-source-movemail from to))
654 (not (zerop (nth 7 (file-attributes from))))
657 (defun mail-source-fetch-with-program (program)
658 (eq 0 (call-process shell-file-name nil nil nil
659 shell-command-switch program)))
661 (defun mail-source-run-script (script spec &optional delay)
663 (if (functionp script)
665 (mail-source-call-script
666 (format-spec script spec))))
670 (defun mail-source-call-script (script)
671 (let ((background nil))
672 (when (string-match "& *$" script)
673 (setq script (substring script 0 (match-beginning 0))
675 (call-process shell-file-name nil background nil
676 shell-command-switch script)))
679 ;;; Different fetchers
682 (defun mail-source-fetch-file (source callback)
683 "Fetcher for single-file sources."
684 (mail-source-bind (file source)
685 (mail-source-run-script
686 prescript (format-spec-make ?t mail-source-crash-box)
688 (let ((mail-source-string (format "file:%s" path)))
689 (if (mail-source-movemail path mail-source-crash-box)
691 (mail-source-callback callback path)
692 (mail-source-run-script
693 postscript (format-spec-make ?t mail-source-crash-box)))
696 (defun mail-source-fetch-directory (source callback)
697 "Fetcher for directory sources."
698 (mail-source-bind (directory source)
699 (mail-source-run-script
700 prescript (format-spec-make ?t path) prescript-delay)
702 (mail-source-string (format "directory:%s" path)))
703 (dolist (file (directory-files
704 path t (concat (regexp-quote suffix) "$")))
705 (when (and (file-regular-p file)
706 (funcall predicate file)
707 (mail-source-movemail file mail-source-crash-box))
708 (incf found (mail-source-callback callback file))))
709 (mail-source-run-script postscript (format-spec-make ?t path))
712 (defun mail-source-fetch-pop (source callback)
713 "Fetcher for single-file sources."
714 (mail-source-bind (pop source)
715 (mail-source-run-script
717 (format-spec-make ?p password ?t mail-source-crash-box
718 ?s server ?P port ?u user)
720 (let ((from (format "%s:%s:%s" server user port))
721 (mail-source-string (format "pop:%s@%s" user server))
723 (when (eq authentication 'password)
726 (cdr (assoc from mail-source-password-cache))
728 (format "Password for %s at %s: " user server)))))
730 (setenv "MAILHOST" server))
734 (mail-source-fetch-with-program
737 (format-spec-make ?p password ?t mail-source-crash-box
738 ?s server ?P port ?u user))))
740 (funcall function mail-source-crash-box))
741 ;; The default is to use pop3.el.
743 (let ((pop3-password password)
745 (pop3-mailhost server)
747 (pop3-authentication-scheme
748 (if (eq authentication 'apop) 'apop 'pass)))
749 (if (or debug-on-quit debug-on-error)
750 (save-excursion (pop3-movemail mail-source-crash-box))
752 (save-excursion (pop3-movemail mail-source-crash-box))
754 ;; We nix out the password in case the error
755 ;; was because of a wrong password being given.
756 (setq mail-source-password-cache
757 (delq (assoc from mail-source-password-cache)
758 mail-source-password-cache))
759 (signal (car err) (cdr err)))))))))
762 (when (eq authentication 'password)
763 (unless (assoc from mail-source-password-cache)
764 (push (cons from password) mail-source-password-cache)))
766 (mail-source-callback callback server)
767 ;; Update display-time's mail flag, if relevant.
768 (if (equal source mail-source-primary-source)
769 (setq mail-source-new-mail-available nil))
770 (mail-source-run-script
772 (format-spec-make ?p password ?t mail-source-crash-box
773 ?s server ?P port ?u user))))
774 ;; We nix out the password in case the error
775 ;; was because of a wrong password being given.
776 (setq mail-source-password-cache
777 (delq (assoc from mail-source-password-cache)
778 mail-source-password-cache))
781 (defun mail-source-check-pop (source)
782 "Check whether there is new mail."
783 (mail-source-bind (pop source)
784 (let ((from (format "%s:%s:%s" server user port))
785 (mail-source-string (format "pop:%s@%s" user server))
787 (when (eq authentication 'password)
790 (cdr (assoc from mail-source-password-cache))
792 (format "Password for %s at %s: " user server))))
793 (unless (assoc from mail-source-password-cache)
794 (push (cons from password) mail-source-password-cache)))
796 (setenv "MAILHOST" server))
799 ;; No easy way to check whether mail is waiting for these.
802 ;; The default is to use pop3.el.
804 (let ((pop3-password password)
806 (pop3-mailhost server)
808 (pop3-authentication-scheme
809 (if (eq authentication 'apop) 'apop 'pass)))
810 (if (or debug-on-quit debug-on-error)
811 (save-excursion (pop3-get-message-count))
813 (save-excursion (pop3-get-message-count))
815 ;; We nix out the password in case the error
816 ;; was because of a wrong password being given.
817 (setq mail-source-password-cache
818 (delq (assoc from mail-source-password-cache)
819 mail-source-password-cache))
820 (signal (car err) (cdr err)))))))))
822 ;; Inform display-time that we have new mail.
823 (setq mail-source-new-mail-available (> result 0))
824 ;; We nix out the password in case the error
825 ;; was because of a wrong password being given.
826 (setq mail-source-password-cache
827 (delq (assoc from mail-source-password-cache)
828 mail-source-password-cache)))
831 (defun mail-source-touch-pop ()
832 "Open and close a POP connection shortly.
833 POP server should be defined in `mail-source-primary-source' (which is
834 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
835 authentication. To do that, you need to set the
836 `message-send-mail-function' variable as `message-smtpmail-send-it'
837 and put the following line in your ~/.gnus.el file:
839 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
841 See the Gnus manual for details."
842 (let ((sources (if mail-source-primary-source
843 (list mail-source-primary-source)
846 (if (eq 'pop (car (car sources)))
847 (mail-source-check-pop (car sources)))
848 (setq sources (cdr sources)))))
850 (defun mail-source-new-mail-p ()
851 "Handler for `display-time' to indicate when new mail is available."
852 ;; Flash (ie. ring the visible bell) if mail is available.
853 (if (and mail-source-flash mail-source-new-mail-available)
854 (let ((visible-bell t))
856 ;; Only report flag setting; flag is updated on a different schedule.
857 mail-source-new-mail-available)
860 (defvar mail-source-report-new-mail nil)
861 (defvar mail-source-report-new-mail-timer nil)
862 (defvar mail-source-report-new-mail-idle-timer nil)
865 (if (featurep 'xemacs)
866 (require 'timer-funcs)
869 (defun mail-source-start-idle-timer ()
870 ;; Start our idle timer if necessary, so we delay the check until the
871 ;; user isn't typing.
872 (unless mail-source-report-new-mail-idle-timer
873 (setq mail-source-report-new-mail-idle-timer
875 mail-source-idle-time-delay
879 (mail-source-check-pop mail-source-primary-source)
880 (setq mail-source-report-new-mail-idle-timer nil)))))
881 ;; Since idle timers created when Emacs is already in the idle
882 ;; state don't get activated until Emacs _next_ becomes idle, we
883 ;; need to force our timer to be considered active now. We do
884 ;; this by being naughty and poking the timer internals directly
885 ;; (element 0 of the vector is nil if the timer is active).
886 (aset mail-source-report-new-mail-idle-timer 0 nil)))
888 (defun mail-source-report-new-mail (arg)
889 "Toggle whether to report when new mail is available.
890 This only works when `display-time' is enabled."
892 (if (not mail-source-primary-source)
893 (error "Need to set `mail-source-primary-source' to check for new mail"))
894 (let ((on (if (null arg)
895 (not mail-source-report-new-mail)
896 (> (prefix-numeric-value arg) 0))))
897 (setq mail-source-report-new-mail on)
898 (and mail-source-report-new-mail-timer
899 (nnheader-cancel-timer mail-source-report-new-mail-timer))
900 (and mail-source-report-new-mail-idle-timer
901 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
902 (setq mail-source-report-new-mail-timer nil)
903 (setq mail-source-report-new-mail-idle-timer nil)
907 ;; display-time-mail-function is an Emacs 21 feature.
908 (setq display-time-mail-function #'mail-source-new-mail-p)
909 ;; Set up the main timer.
910 (setq mail-source-report-new-mail-timer
911 (nnheader-run-at-time
912 (* 60 mail-source-report-new-mail-interval)
913 (* 60 mail-source-report-new-mail-interval)
914 #'mail-source-start-idle-timer))
915 ;; When you get new mail, clear "Mail" from the mode line.
916 (add-hook 'nnmail-post-get-new-mail-hook
917 'display-time-event-handler)
918 (message "Mail check enabled"))
919 (setq display-time-mail-function nil)
920 (remove-hook 'nnmail-post-get-new-mail-hook
921 'display-time-event-handler)
922 (message "Mail check disabled"))))
924 (defun mail-source-fetch-maildir (source callback)
925 "Fetcher for maildir sources."
926 (mail-source-bind (maildir source)
929 (unless (string-match "/$" path)
930 (setq path (concat path "/")))
931 (dolist (subdir subdirs)
932 (when (file-directory-p (concat path subdir))
933 (setq mail-source-string (format "maildir:%s%s" path subdir))
934 (dolist (file (directory-files (concat path subdir) t))
935 (when (and (not (file-directory-p file))
937 (funcall function file mail-source-crash-box)
938 (let ((coding-system-for-write
939 mm-text-coding-system)
940 (coding-system-for-read
941 mm-text-coding-system))
942 (with-temp-file mail-source-crash-box
943 (insert-file-contents file)
944 (goto-char (point-min))
945 ;;; ;; Unix mail format
946 ;;; (unless (looking-at "\n*From ")
947 ;;; (insert "From maildir "
948 ;;; (current-time-string) "\n"))
949 ;;; (while (re-search-forward "^From " nil t)
950 ;;; (replace-match ">From "))
951 ;;; (goto-char (point-max))
954 (insert "\001\001\001\001\n"))
955 (delete-file file)))))
956 (incf found (mail-source-callback callback file))))))
960 (autoload 'imap-open "imap")
961 (autoload 'imap-authenticate "imap")
962 (autoload 'imap-mailbox-select "imap")
963 (autoload 'imap-mailbox-unselect "imap")
964 (autoload 'imap-mailbox-close "imap")
965 (autoload 'imap-search "imap")
966 (autoload 'imap-fetch "imap")
967 (autoload 'imap-close "imap")
968 (autoload 'imap-error-text "imap")
969 (autoload 'imap-message-flags-add "imap")
970 (autoload 'imap-list-to-message-set "imap")
971 (autoload 'imap-range-to-message-set "imap")
972 (autoload 'nnheader-ms-strip-cr "nnheader"))
974 (defvar mail-source-imap-file-coding-system 'binary
975 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
977 (defun mail-source-fetch-imap (source callback)
978 "Fetcher for imap sources."
979 (mail-source-bind (imap source)
980 (mail-source-run-script
981 prescript (format-spec-make ?p password ?t mail-source-crash-box
982 ?s server ?P port ?u user)
984 (let ((from (format "%s:%s:%s" server user port))
986 (buf (generate-new-buffer " *imap source*"))
987 (mail-source-string (format "imap:%s:%s" server mailbox))
988 (imap-shell-program (or (list program) imap-shell-program))
990 (if (and (imap-open server port stream authentication buf)
992 user (or (cdr (assoc from mail-source-password-cache))
994 (imap-mailbox-select mailbox nil buf))
995 (let ((coding-system-for-write mail-source-imap-file-coding-system)
997 (with-temp-file mail-source-crash-box
998 ;; Avoid converting 8-bit chars from inserted strings to
1000 (mm-disable-multibyte)
1001 ;; remember password
1002 (with-current-buffer buf
1003 (when (and imap-password
1004 (not (assoc from mail-source-password-cache)))
1005 (push (cons from imap-password) mail-source-password-cache)))
1006 ;; if predicate is nil, use all uids
1007 (dolist (uid (imap-search (or predicate "1:*") buf))
1009 (if (imap-capability 'IMAP4rev1 buf)
1010 (caddar (imap-fetch uid "BODY.PEEK[]"
1011 'BODYDETAIL nil buf))
1012 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
1014 (insert "From imap " (current-time-string) "\n")
1016 (insert str "\n\n"))
1017 (while (re-search-forward "^From " nil t)
1018 (replace-match ">From "))
1019 (goto-char (point-max))))
1020 (nnheader-ms-strip-cr))
1021 (incf found (mail-source-callback callback server))
1022 (when (and remove fetchflag)
1023 (setq remove (nreverse remove))
1024 (imap-message-flags-add
1025 (imap-range-to-message-set (gnus-compress-sequence remove))
1028 (imap-mailbox-unselect buf)
1029 (imap-mailbox-close nil buf))
1032 ;; We nix out the password in case the error
1033 ;; was because of a wrong password being given.
1034 (setq mail-source-password-cache
1035 (delq (assoc from mail-source-password-cache)
1036 mail-source-password-cache))
1037 (error "IMAP error: %s" (imap-error-text buf)))
1039 (mail-source-run-script
1041 (format-spec-make ?p password ?t mail-source-crash-box
1042 ?s server ?P port ?u user))
1046 (autoload 'webmail-fetch "webmail"))
1048 (defun mail-source-fetch-webmail (source callback)
1049 "Fetch for webmail source."
1050 (mail-source-bind (webmail source)
1051 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1052 (webmail-newmail-only dontexpunge)
1053 (webmail-move-to-trash-can (not dontexpunge)))
1054 (when (eq authentication 'password)
1057 (cdr (assoc (format "webmail:%s:%s" subtype user)
1058 mail-source-password-cache))
1060 (format "Password for %s at %s: " user subtype))))
1062 (not (assoc (format "webmail:%s:%s" subtype user)
1063 mail-source-password-cache)))
1064 (push (cons (format "webmail:%s:%s" subtype user) password)
1065 mail-source-password-cache)))
1066 (webmail-fetch mail-source-crash-box subtype user password)
1067 (mail-source-callback callback (symbol-name subtype)))))
1069 (provide 'mail-source)
1071 ;;; arch-tag: 72948025-1d17-4d6c-bb12-ef1aa2c490fd
1072 ;;; mail-source.el ends here