1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000 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 (defvar pop3-leave-mail-on-server)
34 (autoload 'pop3-movemail "pop3")
35 (autoload 'pop3-get-message-count "pop3")
36 (autoload 'nnheader-cancel-timer "nnheader")
37 (autoload 'nnheader-run-at-time "nnheader"))
38 (require 'format-spec)
40 (defgroup mail-source nil
41 "The mail-fetching library."
44 ;; Define these at compile time to avoid dragging in imap always.
45 (defconst mail-source-imap-authenticators
48 (list 'const (car a)))
49 imap-authenticator-alist)))
50 (defconst mail-source-imap-streams
53 (list 'const (car a)))
56 (defcustom mail-sources nil
57 "*Where the mail backends will look for incoming mail.
58 This variable is a list of mail source specifiers.
59 See Info node `(gnus)Mail Source Specifiers'."
62 (choice :format "%[Value Menu%] %v"
64 (cons :tag "Spool file"
65 (const :format "" file)
66 (checklist :tag "Options" :greedy t
68 (const :format "" :value :path)
70 (cons :tag "Several files in a directory"
71 (const :format "" directory)
72 (checklist :tag "Options" :greedy t
74 (const :format "" :value :path)
75 (directory :tag "Path"))
77 (const :format "" :value :suffix)
78 (string :tag "Suffix"))
80 (const :format "" :value :predicate)
81 (function :tag "Predicate"))
83 (const :format "" :value :prescript)
84 (string :tag "Prescript"))
86 (const :format "" :value :postscript)
87 (string :tag "Postscript"))
89 (const :format "" :value :plugged)
90 (boolean :tag "Plugged"))))
91 (cons :tag "POP3 server"
92 (const :format "" pop)
93 (checklist :tag "Options" :greedy t
95 (const :format "" :value :server)
96 (string :tag "Server"))
98 (const :format "" :value :port)
101 (number :format "%v")
102 (string :format "%v")))
104 (const :format "" :value :user)
105 (string :tag "User"))
107 (const :format "" :value :password)
108 (string :tag "Password"))
110 (const :format "" :value :program)
111 (string :tag "Program"))
113 (const :format "" :value :prescript)
114 (string :tag "Prescript"))
116 (const :format "" :value :postscript)
117 (string :tag "Postscript"))
119 (const :format "" :value :function)
120 (function :tag "Function"))
123 :value :authentication)
124 (choice :tag "Authentication"
129 (const :format "" :value :plugged)
130 (boolean :tag "Plugged"))))
131 (cons :tag "Maildir (qmail, postfix...)"
132 (const :format "" maildir)
133 (checklist :tag "Options" :greedy t
135 (const :format "" :value :path)
136 (directory :tag "Path"))
138 (const :format "" :value :plugged)
139 (boolean :tag "Plugged"))))
140 (cons :tag "IMAP server"
141 (const :format "" imap)
142 (checklist :tag "Options" :greedy t
144 (const :format "" :value :server)
145 (string :tag "Server"))
147 (const :format "" :value :port)
152 (const :format "" :value :user)
153 (string :tag "User"))
155 (const :format "" :value :password)
156 (string :tag "Password"))
158 (const :format "" :value :stream)
159 (choice :tag "Stream"
161 ,@mail-source-imap-streams))
164 :value :authenticator)
165 (choice :tag "Authenticator"
167 ,@mail-source-imap-authenticators))
169 (const :format "" :value :mailbox)
170 (string :tag "Mailbox"
173 (const :format "" :value :predicate)
174 (string :tag "Predicate"
175 :value "UNSEEN UNDELETED"))
177 (const :format "" :value :fetchflag)
178 (string :tag "Fetchflag"
183 (boolean :tag "Dontexpunge"))
185 (const :format "" :value :plugged)
186 (boolean :tag "Plugged"))))
187 (cons :tag "Webmail server"
188 (const :format "" webmail)
189 (checklist :tag "Options" :greedy t
191 (const :format "" :value :subtype)
192 ;; Should be generated from
193 ;; `webmail-type-definition', but we
194 ;; can't require webmail without W3.
195 (choice :tag "Subtype"
203 (const :format "" :value :user)
204 (string :tag "User"))
206 (const :format "" :value :password)
207 (string :tag "Password"))
211 (boolean :tag "Dontexpunge"))
213 (const :format "" :value :plugged)
214 (boolean :tag "Plugged")))))))
216 (defcustom mail-source-primary-source nil
217 "*Primary source for incoming mail.
218 If non-nil, this maildrop will be checked periodically for new mail."
222 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
223 "File where mail will be stored while processing it."
227 (defcustom mail-source-directory "~/Mail/"
228 "Directory where files (if any) will be stored."
232 (defcustom mail-source-default-file-modes 384
233 "Set the mode bits of all new mail files to this integer."
237 (defcustom mail-source-delete-incoming t
238 "*If non-nil, delete incoming files after handling."
242 (defcustom mail-source-incoming-file-prefix "Incoming"
243 "Prefix for file name for storing incoming mail"
247 (defcustom mail-source-report-new-mail-interval 5
248 "Interval in minutes between checks for new mail."
252 (defcustom mail-source-idle-time-delay 5
253 "Number of idle seconds to wait before checking for new mail."
257 ;;; Internal variables.
259 (defvar mail-source-string ""
260 "A dynamically bound string that says what the current mail source is.")
262 (defvar mail-source-new-mail-available nil
263 "Flag indicating when new mail is available.")
266 (defvar mail-source-common-keyword-map
268 "Mapping from keywords to default values.
269 Common keywords should be listed here.")
271 (defvar mail-source-keyword-map
276 (:path (or (getenv "MAIL")
277 (expand-file-name (user-login-name) rmail-spool-directory))))
281 (:predicate identity))
286 (:server (getenv "MAILHOST"))
288 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
293 (:authentication password)
296 (:path (or (getenv "MAILDIR") "~/Maildir/"))
297 (:subdirs ("new" "cur"))
300 (:server (getenv "MAILHOST"))
304 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
307 (:predicate "UNSEEN UNDELETED")
308 (:fetchflag "\\Deleted")
312 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
315 (:authentication password)))
316 "Mapping from keywords to default values.
317 All keywords that can be used must be listed here."))
319 (defvar mail-source-fetcher-alist
320 '((file mail-source-fetch-file)
321 (directory mail-source-fetch-directory)
322 (pop mail-source-fetch-pop)
323 (maildir mail-source-fetch-maildir)
324 (imap mail-source-fetch-imap)
325 (webmail mail-source-fetch-webmail))
326 "A mapping from source type to fetcher function.")
328 (defvar mail-source-password-cache nil)
330 (defvar mail-source-plugged t)
335 (defun mail-source-strip-keyword (keyword)
336 "Strip the leading colon off the KEYWORD."
337 (intern (substring (symbol-name keyword) 1))))
340 (defun mail-source-bind-1 (type)
341 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
343 (while (setq default (pop defaults))
344 (push (list (mail-source-strip-keyword (car default))
349 (defmacro mail-source-bind (type-source &rest body)
350 "Return a `let' form that binds all variables in source TYPE.
351 TYPE-SOURCE is a list where the first element is the TYPE, and
352 the second variable is the SOURCE.
353 At run time, the mail source specifier SOURCE will be inspected,
354 and the variables will be set according to it. Variables not
355 specified will be given default values.
357 After this is done, BODY will be executed in the scope
360 The variables bound and their default values are described by
361 the `mail-source-keyword-map' variable."
362 `(let ,(mail-source-bind-1 (car type-source))
363 (mail-source-set-1 ,(cadr type-source))
366 (put 'mail-source-bind 'lisp-indent-function 1)
367 (put 'mail-source-bind 'edebug-form-spec '(form body))
369 (defun mail-source-set-1 (source)
370 (let* ((type (pop source))
371 (defaults (cdr (assq type mail-source-keyword-map)))
372 default value keyword)
373 (while (setq default (pop defaults))
374 (set (mail-source-strip-keyword (setq keyword (car default)))
375 (if (setq value (plist-get source keyword))
376 (mail-source-value value)
377 (mail-source-value (cadr default)))))))
380 (defun mail-source-bind-common-1 ()
381 (let* ((defaults mail-source-common-keyword-map)
383 (while (setq default (pop defaults))
384 (push (list (mail-source-strip-keyword (car default))
389 (defun mail-source-set-common-1 (source)
390 (let* ((type (pop source))
391 (defaults mail-source-common-keyword-map)
392 (defaults-1 (cdr (assq type mail-source-keyword-map)))
393 default value keyword)
394 (while (setq default (pop defaults))
395 (set (mail-source-strip-keyword (setq keyword (car default)))
396 (if (setq value (plist-get source keyword))
397 (mail-source-value value)
398 (if (setq value (assq keyword defaults-1))
399 (mail-source-value (cadr value))
400 (mail-source-value (cadr default))))))))
402 (defmacro mail-source-bind-common (source &rest body)
403 "Return a `let' form that binds all common variables.
404 See `mail-source-bind'."
405 `(let ,(mail-source-bind-common-1)
406 (mail-source-set-common-1 source)
409 (put 'mail-source-bind-common 'lisp-indent-function 1)
410 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
412 (defun mail-source-value (value)
413 "Return the value of VALUE."
420 (functionp (car value)))
422 ;; Just return the value.
426 (defun mail-source-fetch (source callback)
427 "Fetch mail from SOURCE and call CALLBACK zero or more times.
428 CALLBACK will be called with the name of the file where (some of)
429 the mail from SOURCE is put.
430 Return the number of files that were found."
431 (mail-source-bind-common source
432 (if (or mail-source-plugged plugged)
434 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
437 (error "%S is an invalid mail source specification" source))
438 ;; If there's anything in the crash box, we do it first.
439 (when (file-exists-p mail-source-crash-box)
440 (message "Processing mail from %s..." mail-source-crash-box)
441 (setq found (mail-source-callback
442 callback mail-source-crash-box)))
445 (funcall function source callback)
448 (format "Mail source error (%s). Continue? " err))
449 (error "Cannot get new mail."))
452 (defun mail-source-make-complex-temp-name (prefix)
453 (let ((newname (make-temp-name prefix))
455 (while (file-exists-p newname)
456 (setq newprefix (concat newprefix "x"))
457 (setq newname (make-temp-name newprefix)))
460 (defun mail-source-callback (callback info)
461 "Call CALLBACK on the mail file, and then remove the mail file.
462 Pass INFO on to CALLBACK."
463 (if (or (not (file-exists-p mail-source-crash-box))
464 (zerop (nth 7 (file-attributes mail-source-crash-box))))
466 (when (file-exists-p mail-source-crash-box)
467 (delete-file mail-source-crash-box))
470 (funcall callback mail-source-crash-box info)
471 (when (file-exists-p mail-source-crash-box)
472 ;; Delete or move the incoming mail out of the way.
473 (if mail-source-delete-incoming
474 (delete-file mail-source-crash-box)
476 (mail-source-make-complex-temp-name
478 mail-source-incoming-file-prefix
479 mail-source-directory))))
480 (unless (file-exists-p (file-name-directory incoming))
481 (make-directory (file-name-directory incoming) t))
482 (rename-file mail-source-crash-box incoming t)))))))
484 (defun mail-source-movemail (from to)
485 "Move FROM to TO using movemail."
486 (if (not (file-writable-p to))
487 (error "Can't write to crash box %s. Not moving mail" to)
488 (let ((to (file-truename (expand-file-name to)))
490 (setq to (file-truename to)
491 from (file-truename from))
492 ;; Set TO if have not already done so, and rename or copy
493 ;; the file FROM to TO if and as appropriate.
496 ;; The crash box exists already.
498 ((not (file-exists-p from))
499 ;; There is no inbox.
501 ((zerop (nth 7 (file-attributes from)))
505 ;; If getting from mail spool directory, use movemail to move
506 ;; rather than just renaming, so as to interlock with the
510 (setq errors (generate-new-buffer " *mail source loss*"))
511 (let ((default-directory "/"))
517 (expand-file-name "movemail" exec-directory)
518 nil errors nil from to)))))
519 (when (file-exists-p to)
520 (set-file-modes to mail-source-default-file-modes))
521 (if (and (not (buffer-modified-p errors))
523 ;; No output => movemail won.
526 ;; There may be a warning about older revisions. We
528 (goto-char (point-min))
529 (if (search-forward "older revision" nil t)
531 ;; Probably a real error.
532 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
533 (goto-char (point-max))
534 (skip-chars-backward " \t")
535 (delete-region (point) (point-max))
536 (goto-char (point-min))
537 (when (looking-at "movemail: ")
538 (delete-region (point-min) (match-end 0)))
540 (format "movemail: %s (%d return). Continue? "
541 (buffer-string) result))
542 (error "%s" (buffer-string)))
545 (buffer-name errors))
546 (kill-buffer errors))
547 ;; Return whether we moved successfully or not.
550 (defun mail-source-movemail-and-remove (from to)
551 "Move FROM to TO using movemail, then remove FROM if empty."
552 (or (not (mail-source-movemail from to))
553 (not (zerop (nth 7 (file-attributes from))))
556 (defvar mail-source-read-passwd nil)
557 (defun mail-source-read-passwd (prompt &rest args)
558 "Read a password using PROMPT.
559 If ARGS, PROMPT is used as an argument to `format'."
562 (apply 'format prompt args)
564 (unless mail-source-read-passwd
565 (if (or (fboundp 'read-passwd) (load "passwd" t))
566 (setq mail-source-read-passwd 'read-passwd)
567 (unless (fboundp 'ange-ftp-read-passwd)
568 (autoload 'ange-ftp-read-passwd "ange-ftp"))
569 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
570 (funcall mail-source-read-passwd prompt)))
572 (defun mail-source-fetch-with-program (program)
573 (zerop (call-process shell-file-name nil nil nil
574 shell-command-switch program)))
576 (defun mail-source-run-script (script spec &optional delay)
578 (if (and (symbolp script) (fboundp script))
580 (mail-source-call-script
581 (format-spec script spec))))
585 (defun mail-source-call-script (script)
586 (let ((background nil))
587 (when (string-match "& *$" script)
588 (setq script (substring script 0 (match-beginning 0))
590 (call-process shell-file-name nil background nil
591 shell-command-switch script)))
594 ;;; Different fetchers
597 (defun mail-source-fetch-file (source callback)
598 "Fetcher for single-file sources."
599 (mail-source-bind (file source)
600 (mail-source-run-script
601 prescript (format-spec-make ?t mail-source-crash-box)
603 (let ((mail-source-string (format "file:%s" path)))
604 (if (mail-source-movemail path mail-source-crash-box)
606 (mail-source-callback callback path)
607 (mail-source-run-script
608 postscript (format-spec-make ?t mail-source-crash-box)))
611 (defun mail-source-fetch-directory (source callback)
612 "Fetcher for directory sources."
613 (mail-source-bind (directory source)
615 (mail-source-string (format "directory:%s" path)))
616 (dolist (file (directory-files
617 path t (concat (regexp-quote suffix) "$")))
618 (when (and (file-regular-p file)
619 (funcall predicate file)
620 (mail-source-movemail file mail-source-crash-box))
621 (incf found (mail-source-callback callback file))))
624 (defun mail-source-fetch-pop (source callback)
625 "Fetcher for single-file sources."
626 (mail-source-bind (pop source)
627 (mail-source-run-script
629 (format-spec-make ?p password ?t mail-source-crash-box
630 ?s server ?P port ?u user)
632 (let ((from (format "%s:%s:%s" server user port))
633 (mail-source-string (format "pop:%s@%s" user server))
635 (when (eq authentication 'password)
638 (cdr (assoc from mail-source-password-cache))
639 (mail-source-read-passwd
640 (format "Password for %s at %s: " user server)))))
642 (setenv "MAILHOST" server))
646 (mail-source-fetch-with-program
649 (format-spec-make ?p password ?t mail-source-crash-box
650 ?s server ?P port ?u user))))
652 (funcall function mail-source-crash-box))
653 ;; The default is to use pop3.el.
655 (let ((pop3-password password)
657 (pop3-mailhost server)
659 (pop3-authentication-scheme
660 (if (eq authentication 'apop) 'apop 'pass))
661 (pop3-connection-type connection)
662 (pop3-leave-mail-on-server
664 (and (boundp 'pop3-leave-mail-on-server)
665 pop3-leave-mail-on-server))))
667 (save-excursion (pop3-movemail mail-source-crash-box))
669 ;; We nix out the password in case the error
670 ;; was because of a wrong password being given.
671 (setq mail-source-password-cache
672 (delq (assoc from mail-source-password-cache)
673 mail-source-password-cache))
674 (signal (car err) (cdr err))))))))
677 (when (eq authentication 'password)
678 (unless (assoc from mail-source-password-cache)
679 (push (cons from password) mail-source-password-cache)))
681 (mail-source-callback callback server)
682 ;; Update display-time's mail flag, if relevant.
683 (if (equal source mail-source-primary-source)
684 (setq mail-source-new-mail-available nil))
685 (mail-source-run-script
687 (format-spec-make ?p password ?t mail-source-crash-box
688 ?s server ?P port ?u user))))
689 ;; We nix out the password in case the error
690 ;; was because of a wrong password being given.
691 (setq mail-source-password-cache
692 (delq (assoc from mail-source-password-cache)
693 mail-source-password-cache))
696 (defun mail-source-check-pop (source)
697 "Check whether there is new mail."
698 (mail-source-bind (pop source)
699 (let ((from (format "%s:%s:%s" server user port))
700 (mail-source-string (format "pop:%s@%s" user server))
702 (when (eq authentication 'password)
705 (cdr (assoc from mail-source-password-cache))
706 (mail-source-read-passwd
707 (format "Password for %s at %s: " user server))))
708 (unless (assoc from mail-source-password-cache)
709 (push (cons from password) mail-source-password-cache)))
711 (setenv "MAILHOST" server))
714 ;; No easy way to check whether mail is waiting for these.
717 ;; The default is to use pop3.el.
719 (let ((pop3-password password)
721 (pop3-mailhost server)
723 (pop3-authentication-scheme
724 (if (eq authentication 'apop) 'apop 'pass)))
726 (save-excursion (pop3-get-message-count))
728 ;; We nix out the password in case the error
729 ;; was because of a wrong password being given.
730 (setq mail-source-password-cache
731 (delq (assoc from mail-source-password-cache)
732 mail-source-password-cache))
733 (signal (car err) (cdr err))))))))
735 ;; Inform display-time that we have new mail.
736 (setq mail-source-new-mail-available (> result 0))
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-new-mail-p ()
745 "Handler for `display-time' to indicate when new mail is available."
746 ;; Only report flag setting; flag is updated on a different schedule.
747 mail-source-new-mail-available)
750 (defvar mail-source-report-new-mail nil)
751 (defvar mail-source-report-new-mail-timer nil)
752 (defvar mail-source-report-new-mail-idle-timer nil)
755 (if (featurep 'xemacs)
759 (defun mail-source-start-idle-timer ()
760 ;; Start our idle timer if necessary, so we delay the check until the
761 ;; user isn't typing.
762 (unless mail-source-report-new-mail-idle-timer
763 (setq mail-source-report-new-mail-idle-timer
765 mail-source-idle-time-delay
768 (mail-source-check-pop mail-source-primary-source)
769 (setq mail-source-report-new-mail-idle-timer nil))))
770 ;; Since idle timers created when Emacs is already in the idle
771 ;; state don't get activated until Emacs _next_ becomes idle, we
772 ;; need to force our timer to be considered active now. We do
773 ;; this by being naughty and poking the timer internals directly
774 ;; (element 0 of the vector is nil if the timer is active).
775 (aset mail-source-report-new-mail-idle-timer 0 nil)))
777 (defun mail-source-report-new-mail (arg)
778 "Toggle whether to report when new mail is available.
779 This only works when `display-time' is enabled."
781 (if (not mail-source-primary-source)
782 (error "Need to set `mail-source-primary-source' to check for new mail."))
783 (let ((on (if (null arg)
784 (not mail-source-report-new-mail)
785 (> (prefix-numeric-value arg) 0))))
786 (setq mail-source-report-new-mail on)
787 (and mail-source-report-new-mail-timer
788 (nnheader-cancel-timer mail-source-report-new-mail-timer))
789 (and mail-source-report-new-mail-idle-timer
790 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
791 (setq mail-source-report-new-mail-timer nil)
792 (setq mail-source-report-new-mail-idle-timer nil)
796 ;; display-time-mail-function is an Emacs 21 feature.
797 (setq display-time-mail-function #'mail-source-new-mail-p)
798 ;; Set up the main timer.
799 (setq mail-source-report-new-mail-timer
800 (nnheader-run-at-time
801 (* 60 mail-source-report-new-mail-interval)
802 (* 60 mail-source-report-new-mail-interval)
803 #'mail-source-start-idle-timer))
804 ;; When you get new mail, clear "Mail" from the mode line.
805 (add-hook 'nnmail-post-get-new-mail-hook
806 'display-time-event-handler)
807 (message "Mail check enabled"))
808 (setq display-time-mail-function nil)
809 (remove-hook 'nnmail-post-get-new-mail-hook
810 'display-time-event-handler)
811 (message "Mail check disabled"))))
813 (defun mail-source-fetch-maildir (source callback)
814 "Fetcher for maildir sources."
815 (mail-source-bind (maildir source)
818 (unless (string-match "/$" path)
819 (setq path (concat path "/")))
820 (dolist (subdir subdirs)
821 (when (file-directory-p (concat path subdir))
822 (setq mail-source-string (format "maildir:%s%s" path subdir))
823 (dolist (file (directory-files (concat path subdir) t))
824 (when (and (not (file-directory-p file))
826 (funcall function file mail-source-crash-box)
827 (let ((coding-system-for-write
828 nnheader-text-coding-system)
829 (coding-system-for-read
830 nnheader-text-coding-system)
831 (output-coding-system
832 nnheader-text-coding-system)
834 nnheader-text-coding-system))
835 (with-temp-file mail-source-crash-box
836 (insert-file-contents file)
837 (goto-char (point-min))
838 ;;; ;; Unix mail format
839 ;;; (unless (looking-at "\n*From ")
840 ;;; (insert "From maildir "
841 ;;; (current-time-string) "\n"))
842 ;;; (while (re-search-forward "^From " nil t)
843 ;;; (replace-match ">From "))
844 ;;; (goto-char (point-max))
847 (insert "\001\001\001\001\n"))
848 (delete-file file)))))
849 (incf found (mail-source-callback callback file))))))
853 (autoload 'imap-open "imap")
854 (autoload 'imap-authenticate "imap")
855 (autoload 'imap-mailbox-select "imap")
856 (autoload 'imap-mailbox-unselect "imap")
857 (autoload 'imap-mailbox-close "imap")
858 (autoload 'imap-search "imap")
859 (autoload 'imap-fetch "imap")
860 (autoload 'imap-close "imap")
861 (autoload 'imap-error-text "imap")
862 (autoload 'imap-message-flags-add "imap")
863 (autoload 'imap-list-to-message-set "imap")
864 (autoload 'imap-range-to-message-set "imap")
865 (autoload 'nnheader-ms-strip-cr "nnheader"))
867 (defvar mail-source-imap-file-coding-system 'binary
868 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
870 (defun mail-source-fetch-imap (source callback)
871 "Fetcher for imap sources."
872 (mail-source-bind (imap source)
873 (let ((from (format "%s:%s:%s" server user port))
875 (buf (get-buffer-create
876 (format " *imap source %s:%s:%s *" server user mailbox)))
877 (mail-source-string (format "imap:%s:%s" server mailbox))
879 (if (and (imap-open server port stream authentication buf)
881 user (or (cdr (assoc from mail-source-password-cache))
883 (imap-mailbox-select mailbox nil buf))
884 (let ((coding-system-for-write mail-source-imap-file-coding-system)
885 (output-coding-system mail-source-imap-file-coding-system)
887 (with-temp-file mail-source-crash-box
888 ;; Avoid converting 8-bit chars from inserted strings to
890 (set-buffer-multibyte nil)
892 (with-current-buffer buf
893 (when (or imap-password
894 (assoc from mail-source-password-cache))
895 (push (cons from imap-password) mail-source-password-cache)))
896 ;; if predicate is nil, use all uids
897 (dolist (uid (imap-search (or predicate "1:*") buf))
898 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
900 (insert "From imap " (current-time-string) "\n")
903 (while (re-search-forward "^From " nil t)
904 (replace-match ">From "))
905 (goto-char (point-max))))
906 (nnheader-ms-strip-cr))
907 (incf found (mail-source-callback callback server))
908 (when (and remove fetchflag)
909 (imap-message-flags-add
910 (imap-range-to-message-set (gnus-compress-sequence remove))
913 (imap-mailbox-unselect buf)
914 (imap-mailbox-close buf))
917 ;; We nix out the password in case the error
918 ;; was because of a wrong password being given.
919 (setq mail-source-password-cache
920 (delq (assoc from mail-source-password-cache)
921 mail-source-password-cache))
922 (error (imap-error-text buf)))
927 (autoload 'webmail-fetch "webmail"))
929 (defun mail-source-fetch-webmail (source callback)
930 "Fetch for webmail source."
931 (mail-source-bind (webmail source)
932 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
933 (webmail-newmail-only dontexpunge)
934 (webmail-move-to-trash-can (not dontexpunge)))
935 (when (eq authentication 'password)
938 (cdr (assoc (format "webmail:%s:%s" subtype user)
939 mail-source-password-cache))
940 (mail-source-read-passwd
941 (format "Password for %s at %s: " user subtype))))
943 (not (assoc (format "webmail:%s:%s" subtype user)
944 mail-source-password-cache)))
945 (push (cons (format "webmail:%s:%s" subtype user) password)
946 mail-source-password-cache)))
947 (webmail-fetch mail-source-crash-box subtype user password)
948 (mail-source-callback callback (symbol-name subtype)))))
950 (provide 'mail-source)
952 ;;; mail-source.el ends here