1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002 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)))
59 (defcustom mail-sources nil
60 "*Where the mail backends will look for incoming mail.
61 This variable is a list of mail source specifiers.
62 See Info node `(gnus)Mail Source Specifiers'."
64 :link '(custom-manual "(gnus)Mail Source Specifiers")
66 (choice :format "%[Value Menu%] %v"
68 (cons :tag "Spool file"
69 (const :format "" file)
70 (checklist :tag "Options" :greedy t
72 (const :format "" :value :path)
74 (cons :tag "Several files in a directory"
75 (const :format "" directory)
76 (checklist :tag "Options" :greedy t
78 (const :format "" :value :path)
79 (directory :tag "Path"))
81 (const :format "" :value :suffix)
82 (string :tag "Suffix"))
84 (const :format "" :value :predicate)
85 (function :tag "Predicate"))
87 (const :format "" :value :prescript)
88 (choice :tag "Prescript"
91 (function :format "%v")))
93 (const :format "" :value :postscript)
94 (choice :tag "Postscript"
97 (function :format "%v")))
99 (const :format "" :value :plugged)
100 (boolean :tag "Plugged"))))
101 (cons :tag "POP3 server"
102 (const :format "" pop)
103 (checklist :tag "Options" :greedy t
105 (const :format "" :value :server)
106 (string :tag "Server"))
108 (const :format "" :value :port)
111 (number :format "%v")
112 (string :format "%v")))
114 (const :format "" :value :user)
115 (string :tag "User"))
117 (const :format "" :value :password)
118 (string :tag "Password"))
120 (const :format "" :value :program)
121 (string :tag "Program"))
123 (const :format "" :value :prescript)
124 (choice :tag "Prescript"
126 (string :format "%v")
127 (function :format "%v")))
129 (const :format "" :value :postscript)
130 (choice :tag "Postscript"
132 (string :format "%v")
133 (function :format "%v")))
135 (const :format "" :value :function)
136 (function :tag "Function"))
139 :value :authentication)
140 (choice :tag "Authentication"
145 (const :format "" :value :plugged)
146 (boolean :tag "Plugged"))))
147 (cons :tag "Maildir (qmail, postfix...)"
148 (const :format "" maildir)
149 (checklist :tag "Options" :greedy t
151 (const :format "" :value :path)
152 (directory :tag "Path"))
154 (const :format "" :value :plugged)
155 (boolean :tag "Plugged"))))
156 (cons :tag "IMAP server"
157 (const :format "" imap)
158 (checklist :tag "Options" :greedy t
160 (const :format "" :value :server)
161 (string :tag "Server"))
163 (const :format "" :value :port)
168 (const :format "" :value :user)
169 (string :tag "User"))
171 (const :format "" :value :password)
172 (string :tag "Password"))
174 (const :format "" :value :stream)
175 (choice :tag "Stream"
177 ,@mail-source-imap-streams))
179 (const :format "" :value :program)
180 (string :tag "Program"))
183 :value :authenticator)
184 (choice :tag "Authenticator"
186 ,@mail-source-imap-authenticators))
188 (const :format "" :value :mailbox)
189 (string :tag "Mailbox"
192 (const :format "" :value :predicate)
193 (string :tag "Predicate"
194 :value "UNSEEN UNDELETED"))
196 (const :format "" :value :fetchflag)
197 (string :tag "Fetchflag"
202 (boolean :tag "Dontexpunge"))
204 (const :format "" :value :plugged)
205 (boolean :tag "Plugged"))))
206 (cons :tag "Webmail server"
207 (const :format "" webmail)
208 (checklist :tag "Options" :greedy t
210 (const :format "" :value :subtype)
211 ;; Should be generated from
212 ;; `webmail-type-definition', but we
213 ;; can't require webmail without W3.
214 (choice :tag "Subtype"
222 (const :format "" :value :user)
223 (string :tag "User"))
225 (const :format "" :value :password)
226 (string :tag "Password"))
230 (boolean :tag "Dontexpunge"))
232 (const :format "" :value :plugged)
233 (boolean :tag "Plugged")))))))
235 (defcustom mail-source-primary-source nil
236 "*Primary source for incoming mail.
237 If non-nil, this maildrop will be checked periodically for new mail."
241 (defcustom mail-source-flash t
242 "*If non-nil, flash periodically when mail is available."
246 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
247 "File where mail will be stored while processing it."
251 (defcustom mail-source-directory message-directory
252 "Directory where files (if any) will be stored."
256 (defcustom mail-source-default-file-modes 384
257 "Set the mode bits of all new mail files to this integer."
261 (defcustom mail-source-delete-incoming nil
262 "*If non-nil, delete incoming files after handling."
266 (defcustom mail-source-incoming-file-prefix "Incoming"
267 "Prefix for file name for storing incoming mail"
271 (defcustom mail-source-report-new-mail-interval 5
272 "Interval in minutes between checks for new mail."
276 (defcustom mail-source-idle-time-delay 5
277 "Number of idle seconds to wait before checking for new mail."
281 (defcustom mail-source-movemail-program nil
282 "If non-nil, name of program for fetching new mail."
284 :type '(choice (const nil) string))
286 ;;; Internal variables.
288 (defvar mail-source-string ""
289 "A dynamically bound string that says what the current mail source is.")
291 (defvar mail-source-new-mail-available nil
292 "Flag indicating when new mail is available.")
295 (defvar mail-source-common-keyword-map
297 "Mapping from keywords to default values.
298 Common keywords should be listed here.")
300 (defvar mail-source-keyword-map
305 (:path (or (getenv "MAIL")
306 (expand-file-name (user-login-name) rmail-spool-directory))))
313 (:predicate identity))
318 (:server (getenv "MAILHOST"))
320 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
324 (:authentication password))
326 (:path (or (getenv "MAILDIR") "~/Maildir/"))
327 (:subdirs ("new" "cur"))
330 (:server (getenv "MAILHOST"))
335 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
338 (:predicate "UNSEEN UNDELETED")
339 (:fetchflag "\\Deleted")
343 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
346 (:authentication password)))
347 "Mapping from keywords to default values.
348 All keywords that can be used must be listed here."))
350 (defvar mail-source-fetcher-alist
351 '((file mail-source-fetch-file)
352 (directory mail-source-fetch-directory)
353 (pop mail-source-fetch-pop)
354 (maildir mail-source-fetch-maildir)
355 (imap mail-source-fetch-imap)
356 (webmail mail-source-fetch-webmail))
357 "A mapping from source type to fetcher function.")
359 (defvar mail-source-password-cache nil)
361 (defvar mail-source-plugged t)
366 (defun mail-source-strip-keyword (keyword)
367 "Strip the leading colon off the KEYWORD."
368 (intern (substring (symbol-name keyword) 1))))
371 (defun mail-source-bind-1 (type)
372 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
374 (while (setq default (pop defaults))
375 (push (list (mail-source-strip-keyword (car default))
380 (defmacro mail-source-bind (type-source &rest body)
381 "Return a `let' form that binds all variables in source TYPE.
382 TYPE-SOURCE is a list where the first element is the TYPE, and
383 the second variable is the SOURCE.
384 At run time, the mail source specifier SOURCE will be inspected,
385 and the variables will be set according to it. Variables not
386 specified will be given default values.
388 After this is done, BODY will be executed in the scope
391 The variables bound and their default values are described by
392 the `mail-source-keyword-map' variable."
393 `(let ,(mail-source-bind-1 (car type-source))
394 (mail-source-set-1 ,(cadr type-source))
397 (put 'mail-source-bind 'lisp-indent-function 1)
398 (put 'mail-source-bind 'edebug-form-spec '(sexp body))
400 (defun mail-source-set-1 (source)
401 (let* ((type (pop source))
402 (defaults (cdr (assq type mail-source-keyword-map)))
403 default value keyword)
404 (while (setq default (pop defaults))
405 (set (mail-source-strip-keyword (setq keyword (car default)))
406 (if (setq value (plist-get source keyword))
407 (mail-source-value value)
408 (mail-source-value (cadr default)))))))
411 (defun mail-source-bind-common-1 ()
412 (let* ((defaults mail-source-common-keyword-map)
414 (while (setq default (pop defaults))
415 (push (list (mail-source-strip-keyword (car default))
420 (defun mail-source-set-common-1 (source)
421 (let* ((type (pop source))
422 (defaults mail-source-common-keyword-map)
423 (defaults-1 (cdr (assq type mail-source-keyword-map)))
424 default value keyword)
425 (while (setq default (pop defaults))
426 (set (mail-source-strip-keyword (setq keyword (car default)))
427 (if (setq value (plist-get source keyword))
428 (mail-source-value value)
429 (if (setq value (assq keyword defaults-1))
430 (mail-source-value (cadr value))
431 (mail-source-value (cadr default))))))))
433 (defmacro mail-source-bind-common (source &rest body)
434 "Return a `let' form that binds all common variables.
435 See `mail-source-bind'."
436 `(let ,(mail-source-bind-common-1)
437 (mail-source-set-common-1 source)
440 (put 'mail-source-bind-common 'lisp-indent-function 1)
441 (put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
443 (defun mail-source-value (value)
444 "Return the value of VALUE."
451 (functionp (car value)))
453 ;; Just return the value.
457 (defun mail-source-fetch (source callback)
458 "Fetch mail from SOURCE and call CALLBACK zero or more times.
459 CALLBACK will be called with the name of the file where (some of)
460 the mail from SOURCE is put.
461 Return the number of files that were found."
462 (mail-source-bind-common source
463 (if (or mail-source-plugged plugged)
465 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
468 (error "%S is an invalid mail source specification" source))
469 ;; If there's anything in the crash box, we do it first.
470 (when (file-exists-p mail-source-crash-box)
471 (message "Processing mail from %s..." mail-source-crash-box)
472 (setq found (mail-source-callback
473 callback mail-source-crash-box)))
475 (if (or debug-on-quit debug-on-error)
476 (funcall function source callback)
478 (funcall function source callback)
481 (format "Mail source %s error (%s). Continue? "
482 (if (memq ':password source)
483 (let ((s (copy-sequence source)))
484 (setcar (cdr (memq ':password s))
489 (error "Cannot get new mail"))
493 (if (fboundp 'make-temp-file)
494 (defalias 'mail-source-make-complex-temp-name 'make-temp-file)
495 (defun mail-source-make-complex-temp-name (prefix)
496 (let ((newname (make-temp-name prefix))
498 (while (file-exists-p newname)
499 (setq newprefix (concat newprefix "x"))
500 (setq newname (make-temp-name newprefix)))
503 (defun mail-source-callback (callback info)
504 "Call CALLBACK on the mail file, and then remove the mail file.
505 Pass INFO on to CALLBACK."
506 (if (or (not (file-exists-p mail-source-crash-box))
507 (zerop (nth 7 (file-attributes mail-source-crash-box))))
509 (when (file-exists-p mail-source-crash-box)
510 (delete-file mail-source-crash-box))
513 (funcall callback mail-source-crash-box info)
514 (when (file-exists-p mail-source-crash-box)
515 ;; Delete or move the incoming mail out of the way.
516 (if mail-source-delete-incoming
517 (delete-file mail-source-crash-box)
519 (mail-source-make-complex-temp-name
521 mail-source-incoming-file-prefix
522 mail-source-directory))))
523 (unless (file-exists-p (file-name-directory incoming))
524 (make-directory (file-name-directory incoming) t))
525 (rename-file mail-source-crash-box incoming t)))))))
527 (defun mail-source-movemail (from to)
528 "Move FROM to TO using movemail."
529 (if (not (file-writable-p to))
530 (error "Can't write to crash box %s. Not moving mail" to)
531 (let ((to (file-truename (expand-file-name to)))
533 (setq to (file-truename to)
534 from (file-truename from))
535 ;; Set TO if have not already done so, and rename or copy
536 ;; the file FROM to TO if and as appropriate.
539 ;; The crash box exists already.
541 ((not (file-exists-p from))
542 ;; There is no inbox.
544 ((zerop (nth 7 (file-attributes from)))
548 ;; If getting from mail spool directory, use movemail to move
549 ;; rather than just renaming, so as to interlock with the
553 (setq errors (generate-new-buffer " *mail source loss*"))
554 (let ((default-directory "/"))
560 (or mail-source-movemail-program
561 (expand-file-name "movemail" exec-directory))
562 nil errors nil from to)))))
563 (when (file-exists-p to)
564 (set-file-modes to mail-source-default-file-modes))
565 (if (and (or (not (buffer-modified-p errors))
566 (zerop (buffer-size errors)))
568 ;; No output => movemail won.
571 ;; There may be a warning about older revisions. We
573 (goto-char (point-min))
574 (if (search-forward "older revision" nil t)
576 ;; Probably a real error.
577 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
578 (goto-char (point-max))
579 (skip-chars-backward " \t")
580 (delete-region (point) (point-max))
581 (goto-char (point-min))
582 (when (looking-at "movemail: ")
583 (delete-region (point-min) (match-end 0)))
584 ;; Result may be a signal description string.
586 (format "movemail: %s (%s return). Continue? "
587 (buffer-string) result))
588 (error "%s" (buffer-string)))
591 (buffer-name errors))
592 (kill-buffer errors))
593 ;; Return whether we moved successfully or not.
596 (defun mail-source-movemail-and-remove (from to)
597 "Move FROM to TO using movemail, then remove FROM if empty."
598 (or (not (mail-source-movemail from to))
599 (not (zerop (nth 7 (file-attributes from))))
602 (defvar mail-source-read-passwd nil)
603 (defun mail-source-read-passwd (prompt &rest args)
604 "Read a password using PROMPT.
605 If ARGS, PROMPT is used as an argument to `format'."
608 (apply 'format prompt args)
610 (unless mail-source-read-passwd
611 (if (or (fboundp 'read-passwd) (load "passwd" t))
612 (setq mail-source-read-passwd 'read-passwd)
613 (unless (fboundp 'ange-ftp-read-passwd)
614 (autoload 'ange-ftp-read-passwd "ange-ftp"))
615 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
616 (funcall mail-source-read-passwd prompt)))
618 (defun mail-source-fetch-with-program (program)
619 (zerop (call-process shell-file-name nil nil nil
620 shell-command-switch program)))
622 (defun mail-source-run-script (script spec &optional delay)
624 (if (functionp script)
626 (mail-source-call-script
627 (format-spec script spec))))
631 (defun mail-source-call-script (script)
632 (let ((background nil))
633 (when (string-match "& *$" script)
634 (setq script (substring script 0 (match-beginning 0))
636 (call-process shell-file-name nil background nil
637 shell-command-switch script)))
640 ;;; Different fetchers
643 (defun mail-source-fetch-file (source callback)
644 "Fetcher for single-file sources."
645 (mail-source-bind (file source)
646 (mail-source-run-script
647 prescript (format-spec-make ?t mail-source-crash-box)
649 (let ((mail-source-string (format "file:%s" path)))
650 (if (mail-source-movemail path mail-source-crash-box)
652 (mail-source-callback callback path)
653 (mail-source-run-script
654 postscript (format-spec-make ?t mail-source-crash-box)))
657 (defun mail-source-fetch-directory (source callback)
658 "Fetcher for directory sources."
659 (mail-source-bind (directory source)
660 (mail-source-run-script
661 prescript (format-spec-make ?t path)
664 (mail-source-string (format "directory:%s" path)))
665 (dolist (file (directory-files
666 path t (concat (regexp-quote suffix) "$")))
667 (when (and (file-regular-p file)
668 (funcall predicate file)
669 (mail-source-movemail file mail-source-crash-box))
670 (incf found (mail-source-callback callback file))))
671 (mail-source-run-script
672 postscript (format-spec-make ?t path))
675 (defun mail-source-fetch-pop (source callback)
676 "Fetcher for single-file sources."
677 (mail-source-bind (pop source)
678 (mail-source-run-script
680 (format-spec-make ?p password ?t mail-source-crash-box
681 ?s server ?P port ?u user)
683 (let ((from (format "%s:%s:%s" server user port))
684 (mail-source-string (format "pop:%s@%s" user server))
686 (when (eq authentication 'password)
689 (cdr (assoc from mail-source-password-cache))
690 (mail-source-read-passwd
691 (format "Password for %s at %s: " user server)))))
693 (setenv "MAILHOST" server))
697 (mail-source-fetch-with-program
700 (format-spec-make ?p password ?t mail-source-crash-box
701 ?s server ?P port ?u user))))
703 (funcall function mail-source-crash-box))
704 ;; The default is to use pop3.el.
706 (let ((pop3-password password)
708 (pop3-mailhost server)
710 (pop3-authentication-scheme
711 (if (eq authentication 'apop) 'apop 'pass)))
712 (if (or debug-on-quit debug-on-error)
713 (save-excursion (pop3-movemail mail-source-crash-box))
715 (save-excursion (pop3-movemail mail-source-crash-box))
717 ;; We nix out the password in case the error
718 ;; was because of a wrong password being given.
719 (setq mail-source-password-cache
720 (delq (assoc from mail-source-password-cache)
721 mail-source-password-cache))
722 (signal (car err) (cdr err)))))))))
725 (when (eq authentication 'password)
726 (unless (assoc from mail-source-password-cache)
727 (push (cons from password) mail-source-password-cache)))
729 (mail-source-callback callback server)
730 ;; Update display-time's mail flag, if relevant.
731 (if (equal source mail-source-primary-source)
732 (setq mail-source-new-mail-available nil))
733 (mail-source-run-script
735 (format-spec-make ?p password ?t mail-source-crash-box
736 ?s server ?P port ?u user))))
737 ;; We nix out the password in case the error
738 ;; was because of a wrong password being given.
739 (setq mail-source-password-cache
740 (delq (assoc from mail-source-password-cache)
741 mail-source-password-cache))
744 (defun mail-source-check-pop (source)
745 "Check whether there is new mail."
746 (mail-source-bind (pop source)
747 (let ((from (format "%s:%s:%s" server user port))
748 (mail-source-string (format "pop:%s@%s" user server))
750 (when (eq authentication 'password)
753 (cdr (assoc from mail-source-password-cache))
754 (mail-source-read-passwd
755 (format "Password for %s at %s: " user server))))
756 (unless (assoc from mail-source-password-cache)
757 (push (cons from password) mail-source-password-cache)))
759 (setenv "MAILHOST" server))
762 ;; No easy way to check whether mail is waiting for these.
765 ;; The default is to use pop3.el.
767 (let ((pop3-password password)
769 (pop3-mailhost server)
771 (pop3-authentication-scheme
772 (if (eq authentication 'apop) 'apop 'pass)))
773 (if (or debug-on-quit debug-on-error)
774 (save-excursion (pop3-get-message-count))
776 (save-excursion (pop3-get-message-count))
778 ;; We nix out the password in case the error
779 ;; was because of a wrong password being given.
780 (setq mail-source-password-cache
781 (delq (assoc from mail-source-password-cache)
782 mail-source-password-cache))
783 (signal (car err) (cdr err)))))))))
785 ;; Inform display-time that we have new mail.
786 (setq mail-source-new-mail-available (> result 0))
787 ;; We nix out the password in case the error
788 ;; was because of a wrong password being given.
789 (setq mail-source-password-cache
790 (delq (assoc from mail-source-password-cache)
791 mail-source-password-cache)))
794 (defun mail-source-touch-pop ()
795 "Open and close a POP connection shortly.
796 POP server should be defined in `mail-source-primary-source' (which is
797 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
798 authentication. To do that, you need to set the option
799 `message-send-mail-function' to `message-smtpmail-send-it' and put the
800 following line in .gnus file:
802 \(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
804 (let ((sources (if mail-source-primary-source
805 (list mail-source-primary-source)
808 (if (eq 'pop (car (car sources)))
809 (mail-source-check-pop (car sources)))
810 (setq sources (cdr sources)))))
812 (defun mail-source-new-mail-p ()
813 "Handler for `display-time' to indicate when new mail is available."
814 ;; Flash (ie. ring the visible bell) if mail is available.
815 (if (and mail-source-flash mail-source-new-mail-available)
816 (let ((visible-bell t))
818 ;; Only report flag setting; flag is updated on a different schedule.
819 mail-source-new-mail-available)
822 (defvar mail-source-report-new-mail nil)
823 (defvar mail-source-report-new-mail-timer nil)
824 (defvar mail-source-report-new-mail-idle-timer nil)
827 (if (featurep 'xemacs)
831 (defun mail-source-start-idle-timer ()
832 ;; Start our idle timer if necessary, so we delay the check until the
833 ;; user isn't typing.
834 (unless mail-source-report-new-mail-idle-timer
835 (setq mail-source-report-new-mail-idle-timer
837 mail-source-idle-time-delay
841 (mail-source-check-pop mail-source-primary-source)
842 (setq mail-source-report-new-mail-idle-timer nil)))))
843 ;; Since idle timers created when Emacs is already in the idle
844 ;; state don't get activated until Emacs _next_ becomes idle, we
845 ;; need to force our timer to be considered active now. We do
846 ;; this by being naughty and poking the timer internals directly
847 ;; (element 0 of the vector is nil if the timer is active).
848 (aset mail-source-report-new-mail-idle-timer 0 nil)))
850 (defun mail-source-report-new-mail (arg)
851 "Toggle whether to report when new mail is available.
852 This only works when `display-time' is enabled."
854 (if (not mail-source-primary-source)
855 (error "Need to set `mail-source-primary-source' to check for new mail"))
856 (let ((on (if (null arg)
857 (not mail-source-report-new-mail)
858 (> (prefix-numeric-value arg) 0))))
859 (setq mail-source-report-new-mail on)
860 (and mail-source-report-new-mail-timer
861 (nnheader-cancel-timer mail-source-report-new-mail-timer))
862 (and mail-source-report-new-mail-idle-timer
863 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
864 (setq mail-source-report-new-mail-timer nil)
865 (setq mail-source-report-new-mail-idle-timer nil)
869 ;; display-time-mail-function is an Emacs 21 feature.
870 (setq display-time-mail-function #'mail-source-new-mail-p)
871 ;; Set up the main timer.
872 (setq mail-source-report-new-mail-timer
873 (nnheader-run-at-time
874 (* 60 mail-source-report-new-mail-interval)
875 (* 60 mail-source-report-new-mail-interval)
876 #'mail-source-start-idle-timer))
877 ;; When you get new mail, clear "Mail" from the mode line.
878 (add-hook 'nnmail-post-get-new-mail-hook
879 'display-time-event-handler)
880 (message "Mail check enabled"))
881 (setq display-time-mail-function nil)
882 (remove-hook 'nnmail-post-get-new-mail-hook
883 'display-time-event-handler)
884 (message "Mail check disabled"))))
886 (defun mail-source-fetch-maildir (source callback)
887 "Fetcher for maildir sources."
888 (mail-source-bind (maildir source)
891 (unless (string-match "/$" path)
892 (setq path (concat path "/")))
893 (dolist (subdir subdirs)
894 (when (file-directory-p (concat path subdir))
895 (setq mail-source-string (format "maildir:%s%s" path subdir))
896 (dolist (file (directory-files (concat path subdir) t))
897 (when (and (not (file-directory-p file))
899 (funcall function file mail-source-crash-box)
900 (let ((coding-system-for-write
901 mm-text-coding-system)
902 (coding-system-for-read
903 mm-text-coding-system))
904 (with-temp-file mail-source-crash-box
905 (insert-file-contents file)
906 (goto-char (point-min))
907 ;;; ;; Unix mail format
908 ;;; (unless (looking-at "\n*From ")
909 ;;; (insert "From maildir "
910 ;;; (current-time-string) "\n"))
911 ;;; (while (re-search-forward "^From " nil t)
912 ;;; (replace-match ">From "))
913 ;;; (goto-char (point-max))
916 (insert "\001\001\001\001\n"))
917 (delete-file file)))))
918 (incf found (mail-source-callback callback file))))))
922 (autoload 'imap-open "imap")
923 (autoload 'imap-authenticate "imap")
924 (autoload 'imap-mailbox-select "imap")
925 (autoload 'imap-mailbox-unselect "imap")
926 (autoload 'imap-mailbox-close "imap")
927 (autoload 'imap-search "imap")
928 (autoload 'imap-fetch "imap")
929 (autoload 'imap-close "imap")
930 (autoload 'imap-error-text "imap")
931 (autoload 'imap-message-flags-add "imap")
932 (autoload 'imap-list-to-message-set "imap")
933 (autoload 'imap-range-to-message-set "imap")
934 (autoload 'nnheader-ms-strip-cr "nnheader"))
936 (defvar mail-source-imap-file-coding-system 'binary
937 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
939 (defun mail-source-fetch-imap (source callback)
940 "Fetcher for imap sources."
941 (mail-source-bind (imap source)
942 (let ((from (format "%s:%s:%s" server user port))
944 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
945 (mail-source-string (format "imap:%s:%s" server mailbox))
946 (imap-shell-program (or (list program) imap-shell-program))
948 (if (and (imap-open server port stream authentication buf)
950 user (or (cdr (assoc from mail-source-password-cache))
952 (imap-mailbox-select mailbox nil buf))
953 (let ((coding-system-for-write mail-source-imap-file-coding-system)
955 (with-temp-file mail-source-crash-box
956 ;; Avoid converting 8-bit chars from inserted strings to
958 (mm-disable-multibyte)
960 (with-current-buffer buf
961 (when (or imap-password
962 (assoc from mail-source-password-cache))
963 (push (cons from imap-password) mail-source-password-cache)))
964 ;; if predicate is nil, use all uids
965 (dolist (uid (imap-search (or predicate "1:*") buf))
967 (if (imap-capability 'IMAP4rev1 buf)
968 (caddar (imap-fetch uid "BODY.PEEK[]"
969 'BODYDETAIL nil buf))
970 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
972 (insert "From imap " (current-time-string) "\n")
975 (while (re-search-forward "^From " nil t)
976 (replace-match ">From "))
977 (goto-char (point-max))))
978 (nnheader-ms-strip-cr))
979 (incf found (mail-source-callback callback server))
980 (when (and remove fetchflag)
981 (imap-message-flags-add
982 (imap-range-to-message-set (gnus-compress-sequence remove))
985 (imap-mailbox-unselect buf)
986 (imap-mailbox-close nil buf))
989 ;; We nix out the password in case the error
990 ;; was because of a wrong password being given.
991 (setq mail-source-password-cache
992 (delq (assoc from mail-source-password-cache)
993 mail-source-password-cache))
994 (error (imap-error-text buf)))
999 (autoload 'webmail-fetch "webmail"))
1001 (defun mail-source-fetch-webmail (source callback)
1002 "Fetch for webmail source."
1003 (mail-source-bind (webmail source)
1004 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1005 (webmail-newmail-only dontexpunge)
1006 (webmail-move-to-trash-can (not dontexpunge)))
1007 (when (eq authentication 'password)
1010 (cdr (assoc (format "webmail:%s:%s" subtype user)
1011 mail-source-password-cache))
1012 (mail-source-read-passwd
1013 (format "Password for %s at %s: " user subtype))))
1015 (not (assoc (format "webmail:%s:%s" subtype user)
1016 mail-source-password-cache)))
1017 (push (cons (format "webmail:%s:%s" subtype user) password)
1018 mail-source-password-cache)))
1019 (webmail-fetch mail-source-crash-box subtype user password)
1020 (mail-source-callback callback (symbol-name subtype)))))
1022 (provide 'mail-source)
1024 ;;; mail-source.el ends here