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 (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)
40 (defgroup mail-source nil
41 "The mail-fetching library."
45 ;; Define these at compile time to avoid dragging in imap always.
46 (defconst mail-source-imap-authenticators
49 (list 'const (car a)))
50 imap-authenticator-alist)))
51 (defconst mail-source-imap-streams
54 (list 'const (car a)))
57 (defcustom mail-sources nil
58 "*Where the mail backends will look for incoming mail.
59 This variable is a list of mail source specifiers.
60 See Info node `(gnus)Mail Source Specifiers'."
63 (choice :format "%[Value Menu%] %v"
65 (cons :tag "Spool file"
66 (const :format "" file)
67 (checklist :tag "Options" :greedy t
69 (const :format "" :value :path)
71 (cons :tag "Several files in a directory"
72 (const :format "" directory)
73 (checklist :tag "Options" :greedy t
75 (const :format "" :value :path)
76 (directory :tag "Path"))
78 (const :format "" :value :suffix)
79 (string :tag "Suffix"))
81 (const :format "" :value :predicate)
82 (function :tag "Predicate"))
84 (const :format "" :value :prescript)
85 (string :tag "Prescript"))
87 (const :format "" :value :postscript)
88 (string :tag "Postscript"))
90 (const :format "" :value :plugged)
91 (boolean :tag "Plugged"))))
92 (cons :tag "POP3 server"
93 (const :format "" pop)
94 (checklist :tag "Options" :greedy t
96 (const :format "" :value :server)
97 (string :tag "Server"))
99 (const :format "" :value :port)
102 (number :format "%v")
103 (string :format "%v")))
105 (const :format "" :value :user)
106 (string :tag "User"))
108 (const :format "" :value :password)
109 (string :tag "Password"))
111 (const :format "" :value :program)
112 (string :tag "Program"))
114 (const :format "" :value :prescript)
115 (string :tag "Prescript"))
117 (const :format "" :value :postscript)
118 (string :tag "Postscript"))
120 (const :format "" :value :function)
121 (function :tag "Function"))
124 :value :authentication)
125 (choice :tag "Authentication"
130 (const :format "" :value :plugged)
131 (boolean :tag "Plugged"))))
132 (cons :tag "Maildir (qmail, postfix...)"
133 (const :format "" maildir)
134 (checklist :tag "Options" :greedy t
136 (const :format "" :value :path)
137 (directory :tag "Path"))
139 (const :format "" :value :plugged)
140 (boolean :tag "Plugged"))))
141 (cons :tag "IMAP server"
142 (const :format "" imap)
143 (checklist :tag "Options" :greedy t
145 (const :format "" :value :server)
146 (string :tag "Server"))
148 (const :format "" :value :port)
153 (const :format "" :value :user)
154 (string :tag "User"))
156 (const :format "" :value :password)
157 (string :tag "Password"))
159 (const :format "" :value :stream)
160 (choice :tag "Stream"
162 ,@mail-source-imap-streams))
165 :value :authenticator)
166 (choice :tag "Authenticator"
168 ,@mail-source-imap-authenticators))
170 (const :format "" :value :mailbox)
171 (string :tag "Mailbox"
174 (const :format "" :value :predicate)
175 (string :tag "Predicate"
176 :value "UNSEEN UNDELETED"))
178 (const :format "" :value :fetchflag)
179 (string :tag "Fetchflag"
184 (boolean :tag "Dontexpunge"))
186 (const :format "" :value :plugged)
187 (boolean :tag "Plugged"))))
188 (cons :tag "Webmail server"
189 (const :format "" webmail)
190 (checklist :tag "Options" :greedy t
192 (const :format "" :value :subtype)
193 ;; Should be generated from
194 ;; `webmail-type-definition', but we
195 ;; can't require webmail without W3.
196 (choice :tag "Subtype"
204 (const :format "" :value :user)
205 (string :tag "User"))
207 (const :format "" :value :password)
208 (string :tag "Password"))
212 (boolean :tag "Dontexpunge"))
214 (const :format "" :value :plugged)
215 (boolean :tag "Plugged")))))))
217 (defcustom mail-source-primary-source nil
218 "*Primary source for incoming mail.
219 If non-nil, this maildrop will be checked periodically for new mail."
223 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
224 "File where mail will be stored while processing it."
228 (defcustom mail-source-directory "~/Mail/"
229 "Directory where files (if any) will be stored."
233 (defcustom mail-source-default-file-modes 384
234 "Set the mode bits of all new mail files to this integer."
238 (defcustom mail-source-delete-incoming nil
239 "*If non-nil, delete incoming files after handling."
243 (defcustom mail-source-incoming-file-prefix "Incoming"
244 "Prefix for file name for storing incoming mail"
248 (defcustom mail-source-report-new-mail-interval 5
249 "Interval in minutes between checks for new mail."
253 (defcustom mail-source-idle-time-delay 5
254 "Number of idle seconds to wait before checking for new mail."
258 ;;; Internal variables.
260 (defvar mail-source-string ""
261 "A dynamically bound string that says what the current mail source is.")
263 (defvar mail-source-new-mail-available nil
264 "Flag indicating when new mail is available.")
267 (defvar mail-source-common-keyword-map
269 "Mapping from keywords to default values.
270 Common keywords should be listed here.")
272 (defvar mail-source-keyword-map
277 (:path (or (getenv "MAIL")
278 (expand-file-name (user-login-name) rmail-spool-directory))))
282 (:predicate identity))
287 (:server (getenv "MAILHOST"))
289 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
293 (:authentication password))
295 (:path (or (getenv "MAILDIR") "~/Maildir/"))
296 (:subdirs ("new" "cur"))
299 (:server (getenv "MAILHOST"))
303 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
306 (:predicate "UNSEEN UNDELETED")
307 (:fetchflag "\\Deleted")
311 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
314 (:authentication password)))
315 "Mapping from keywords to default values.
316 All keywords that can be used must be listed here."))
318 (defvar mail-source-fetcher-alist
319 '((file mail-source-fetch-file)
320 (directory mail-source-fetch-directory)
321 (pop mail-source-fetch-pop)
322 (maildir mail-source-fetch-maildir)
323 (imap mail-source-fetch-imap)
324 (webmail mail-source-fetch-webmail))
325 "A mapping from source type to fetcher function.")
327 (defvar mail-source-password-cache nil)
329 (defvar mail-source-plugged t)
334 (defun mail-source-strip-keyword (keyword)
335 "Strip the leading colon off the KEYWORD."
336 (intern (substring (symbol-name keyword) 1))))
339 (defun mail-source-bind-1 (type)
340 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
342 (while (setq default (pop defaults))
343 (push (list (mail-source-strip-keyword (car default))
348 (defmacro mail-source-bind (type-source &rest body)
349 "Return a `let' form that binds all variables in source TYPE.
350 TYPE-SOURCE is a list where the first element is the TYPE, and
351 the second variable is the SOURCE.
352 At run time, the mail source specifier SOURCE will be inspected,
353 and the variables will be set according to it. Variables not
354 specified will be given default values.
356 After this is done, BODY will be executed in the scope
359 The variables bound and their default values are described by
360 the `mail-source-keyword-map' variable."
361 `(let ,(mail-source-bind-1 (car type-source))
362 (mail-source-set-1 ,(cadr type-source))
365 (put 'mail-source-bind 'lisp-indent-function 1)
366 (put 'mail-source-bind 'edebug-form-spec '(form body))
368 (defun mail-source-set-1 (source)
369 (let* ((type (pop source))
370 (defaults (cdr (assq type mail-source-keyword-map)))
371 default value keyword)
372 (while (setq default (pop defaults))
373 (set (mail-source-strip-keyword (setq keyword (car default)))
374 (if (setq value (plist-get source keyword))
375 (mail-source-value value)
376 (mail-source-value (cadr default)))))))
379 (defun mail-source-bind-common-1 ()
380 (let* ((defaults mail-source-common-keyword-map)
382 (while (setq default (pop defaults))
383 (push (list (mail-source-strip-keyword (car default))
388 (defun mail-source-set-common-1 (source)
389 (let* ((type (pop source))
390 (defaults mail-source-common-keyword-map)
391 (defaults-1 (cdr (assq type mail-source-keyword-map)))
392 default value keyword)
393 (while (setq default (pop defaults))
394 (set (mail-source-strip-keyword (setq keyword (car default)))
395 (if (setq value (plist-get source keyword))
396 (mail-source-value value)
397 (if (setq value (assq keyword defaults-1))
398 (mail-source-value (cadr value))
399 (mail-source-value (cadr default))))))))
401 (defmacro mail-source-bind-common (source &rest body)
402 "Return a `let' form that binds all common variables.
403 See `mail-source-bind'."
404 `(let ,(mail-source-bind-common-1)
405 (mail-source-set-common-1 source)
408 (put 'mail-source-bind-common 'lisp-indent-function 1)
409 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
411 (defun mail-source-value (value)
412 "Return the value of VALUE."
419 (functionp (car value)))
421 ;; Just return the value.
425 (defun mail-source-fetch (source callback)
426 "Fetch mail from SOURCE and call CALLBACK zero or more times.
427 CALLBACK will be called with the name of the file where (some of)
428 the mail from SOURCE is put.
429 Return the number of files that were found."
430 (mail-source-bind-common source
431 (if (or mail-source-plugged plugged)
433 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
436 (error "%S is an invalid mail source specification" source))
437 ;; If there's anything in the crash box, we do it first.
438 (when (file-exists-p mail-source-crash-box)
439 (message "Processing mail from %s..." mail-source-crash-box)
440 (setq found (mail-source-callback
441 callback mail-source-crash-box)))
444 (funcall function source callback)
447 (format "Mail source error (%s). Continue? " err))
448 (error "Cannot get new mail."))
451 (defun mail-source-make-complex-temp-name (prefix)
452 (let ((newname (make-temp-name prefix))
454 (while (file-exists-p newname)
455 (setq newprefix (concat newprefix "x"))
456 (setq newname (make-temp-name newprefix)))
459 (defun mail-source-callback (callback info)
460 "Call CALLBACK on the mail file, and then remove the mail file.
461 Pass INFO on to CALLBACK."
462 (if (or (not (file-exists-p mail-source-crash-box))
463 (zerop (nth 7 (file-attributes mail-source-crash-box))))
465 (when (file-exists-p mail-source-crash-box)
466 (delete-file mail-source-crash-box))
469 (funcall callback mail-source-crash-box info)
470 (when (file-exists-p mail-source-crash-box)
471 ;; Delete or move the incoming mail out of the way.
472 (if mail-source-delete-incoming
473 (delete-file mail-source-crash-box)
475 (mail-source-make-complex-temp-name
477 mail-source-incoming-file-prefix
478 mail-source-directory))))
479 (unless (file-exists-p (file-name-directory incoming))
480 (make-directory (file-name-directory incoming) t))
481 (rename-file mail-source-crash-box incoming t)))))))
483 (defun mail-source-movemail (from to)
484 "Move FROM to TO using movemail."
485 (if (not (file-writable-p to))
486 (error "Can't write to crash box %s. Not moving mail" to)
487 (let ((to (file-truename (expand-file-name to)))
489 (setq to (file-truename to)
490 from (file-truename from))
491 ;; Set TO if have not already done so, and rename or copy
492 ;; the file FROM to TO if and as appropriate.
495 ;; The crash box exists already.
497 ((not (file-exists-p from))
498 ;; There is no inbox.
500 ((zerop (nth 7 (file-attributes from)))
504 ;; If getting from mail spool directory, use movemail to move
505 ;; rather than just renaming, so as to interlock with the
509 (setq errors (generate-new-buffer " *mail source loss*"))
510 (let ((default-directory "/"))
516 (expand-file-name "movemail" exec-directory)
517 nil errors nil from to)))))
518 (when (file-exists-p to)
519 (set-file-modes to mail-source-default-file-modes))
520 (if (and (not (buffer-modified-p errors))
522 ;; No output => movemail won.
525 ;; There may be a warning about older revisions. We
527 (goto-char (point-min))
528 (if (search-forward "older revision" nil t)
530 ;; Probably a real error.
531 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
532 (goto-char (point-max))
533 (skip-chars-backward " \t")
534 (delete-region (point) (point-max))
535 (goto-char (point-min))
536 (when (looking-at "movemail: ")
537 (delete-region (point-min) (match-end 0)))
539 (format "movemail: %s (%d return). Continue? "
540 (buffer-string) result))
541 (error "%s" (buffer-string)))
544 (buffer-name errors))
545 (kill-buffer errors))
546 ;; Return whether we moved successfully or not.
549 (defun mail-source-movemail-and-remove (from to)
550 "Move FROM to TO using movemail, then remove FROM if empty."
551 (or (not (mail-source-movemail from to))
552 (not (zerop (nth 7 (file-attributes from))))
555 (defvar mail-source-read-passwd nil)
556 (defun mail-source-read-passwd (prompt &rest args)
557 "Read a password using PROMPT.
558 If ARGS, PROMPT is used as an argument to `format'."
561 (apply 'format prompt args)
563 (unless mail-source-read-passwd
564 (if (or (fboundp 'read-passwd) (load "passwd" t))
565 (setq mail-source-read-passwd 'read-passwd)
566 (unless (fboundp 'ange-ftp-read-passwd)
567 (autoload 'ange-ftp-read-passwd "ange-ftp"))
568 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
569 (funcall mail-source-read-passwd prompt)))
571 (defun mail-source-fetch-with-program (program)
572 (zerop (call-process shell-file-name nil nil nil
573 shell-command-switch program)))
575 (defun mail-source-run-script (script spec &optional delay)
577 (if (and (symbolp script) (fboundp script))
579 (mail-source-call-script
580 (format-spec script spec))))
584 (defun mail-source-call-script (script)
585 (let ((background nil))
586 (when (string-match "& *$" script)
587 (setq script (substring script 0 (match-beginning 0))
589 (call-process shell-file-name nil background nil
590 shell-command-switch script)))
593 ;;; Different fetchers
596 (defun mail-source-fetch-file (source callback)
597 "Fetcher for single-file sources."
598 (mail-source-bind (file source)
599 (mail-source-run-script
600 prescript (format-spec-make ?t mail-source-crash-box)
602 (let ((mail-source-string (format "file:%s" path)))
603 (if (mail-source-movemail path mail-source-crash-box)
605 (mail-source-callback callback path)
606 (mail-source-run-script
607 postscript (format-spec-make ?t mail-source-crash-box)))
610 (defun mail-source-fetch-directory (source callback)
611 "Fetcher for directory sources."
612 (mail-source-bind (directory source)
614 (mail-source-string (format "directory:%s" path)))
615 (dolist (file (directory-files
616 path t (concat (regexp-quote suffix) "$")))
617 (when (and (file-regular-p file)
618 (funcall predicate file)
619 (mail-source-movemail file mail-source-crash-box))
620 (incf found (mail-source-callback callback file))))
623 (defun mail-source-fetch-pop (source callback)
624 "Fetcher for single-file sources."
625 (mail-source-bind (pop source)
626 (mail-source-run-script
628 (format-spec-make ?p password ?t mail-source-crash-box
629 ?s server ?P port ?u user)
631 (let ((from (format "%s:%s:%s" server user port))
632 (mail-source-string (format "pop:%s@%s" user server))
634 (when (eq authentication 'password)
637 (cdr (assoc from mail-source-password-cache))
638 (mail-source-read-passwd
639 (format "Password for %s at %s: " user server)))))
641 (setenv "MAILHOST" server))
645 (mail-source-fetch-with-program
648 (format-spec-make ?p password ?t mail-source-crash-box
649 ?s server ?P port ?u user))))
651 (funcall function mail-source-crash-box))
652 ;; The default is to use pop3.el.
654 (let ((pop3-password password)
656 (pop3-mailhost server)
658 (pop3-authentication-scheme
659 (if (eq authentication 'apop) 'apop 'pass)))
661 (save-excursion (pop3-movemail mail-source-crash-box))
663 ;; We nix out the password in case the error
664 ;; was because of a wrong password being given.
665 (setq mail-source-password-cache
666 (delq (assoc from mail-source-password-cache)
667 mail-source-password-cache))
668 (signal (car err) (cdr err))))))))
671 (when (eq authentication 'password)
672 (unless (assoc from mail-source-password-cache)
673 (push (cons from password) mail-source-password-cache)))
675 (mail-source-callback callback server)
676 ;; Update display-time's mail flag, if relevant.
677 (if (equal source mail-source-primary-source)
678 (setq mail-source-new-mail-available nil))
679 (mail-source-run-script
681 (format-spec-make ?p password ?t mail-source-crash-box
682 ?s server ?P port ?u user))))
683 ;; We nix out the password in case the error
684 ;; was because of a wrong password being given.
685 (setq mail-source-password-cache
686 (delq (assoc from mail-source-password-cache)
687 mail-source-password-cache))
690 (defun mail-source-check-pop (source)
691 "Check whether there is new mail."
692 (mail-source-bind (pop source)
693 (let ((from (format "%s:%s:%s" server user port))
694 (mail-source-string (format "pop:%s@%s" user server))
696 (when (eq authentication 'password)
699 (cdr (assoc from mail-source-password-cache))
700 (mail-source-read-passwd
701 (format "Password for %s at %s: " user server))))
702 (unless (assoc from mail-source-password-cache)
703 (push (cons from password) mail-source-password-cache)))
705 (setenv "MAILHOST" server))
708 ;; No easy way to check whether mail is waiting for these.
711 ;; The default is to use pop3.el.
713 (let ((pop3-password password)
715 (pop3-mailhost server)
717 (pop3-authentication-scheme
718 (if (eq authentication 'apop) 'apop 'pass)))
720 (save-excursion (pop3-get-message-count))
722 ;; We nix out the password in case the error
723 ;; was because of a wrong password being given.
724 (setq mail-source-password-cache
725 (delq (assoc from mail-source-password-cache)
726 mail-source-password-cache))
727 (signal (car err) (cdr err))))))))
729 ;; Inform display-time that we have new mail.
730 (setq mail-source-new-mail-available (> result 0))
731 ;; We nix out the password in case the error
732 ;; was because of a wrong password being given.
733 (setq mail-source-password-cache
734 (delq (assoc from mail-source-password-cache)
735 mail-source-password-cache)))
738 (defun mail-source-new-mail-p ()
739 "Handler for `display-time' to indicate when new mail is available."
740 ;; Only report flag setting; flag is updated on a different schedule.
741 mail-source-new-mail-available)
744 (defvar mail-source-report-new-mail nil)
745 (defvar mail-source-report-new-mail-timer nil)
746 (defvar mail-source-report-new-mail-idle-timer nil)
749 (if (featurep 'xemacs)
753 (defun mail-source-start-idle-timer ()
754 ;; Start our idle timer if necessary, so we delay the check until the
755 ;; user isn't typing.
756 (unless mail-source-report-new-mail-idle-timer
757 (setq mail-source-report-new-mail-idle-timer
759 mail-source-idle-time-delay
762 (mail-source-check-pop mail-source-primary-source)
763 (setq mail-source-report-new-mail-idle-timer nil))))
764 ;; Since idle timers created when Emacs is already in the idle
765 ;; state don't get activated until Emacs _next_ becomes idle, we
766 ;; need to force our timer to be considered active now. We do
767 ;; this by being naughty and poking the timer internals directly
768 ;; (element 0 of the vector is nil if the timer is active).
769 (aset mail-source-report-new-mail-idle-timer 0 nil)))
771 (defun mail-source-report-new-mail (arg)
772 "Toggle whether to report when new mail is available.
773 This only works when `display-time' is enabled."
775 (if (not mail-source-primary-source)
776 (error "Need to set `mail-source-primary-source' to check for new mail."))
777 (let ((on (if (null arg)
778 (not mail-source-report-new-mail)
779 (> (prefix-numeric-value arg) 0))))
780 (setq mail-source-report-new-mail on)
781 (and mail-source-report-new-mail-timer
782 (nnheader-cancel-timer mail-source-report-new-mail-timer))
783 (and mail-source-report-new-mail-idle-timer
784 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
785 (setq mail-source-report-new-mail-timer nil)
786 (setq mail-source-report-new-mail-idle-timer nil)
790 ;; display-time-mail-function is an Emacs 21 feature.
791 (setq display-time-mail-function #'mail-source-new-mail-p)
792 ;; Set up the main timer.
793 (setq mail-source-report-new-mail-timer
794 (nnheader-run-at-time
795 (* 60 mail-source-report-new-mail-interval)
796 (* 60 mail-source-report-new-mail-interval)
797 #'mail-source-start-idle-timer))
798 ;; When you get new mail, clear "Mail" from the mode line.
799 (add-hook 'nnmail-post-get-new-mail-hook
800 'display-time-event-handler)
801 (message "Mail check enabled"))
802 (setq display-time-mail-function nil)
803 (remove-hook 'nnmail-post-get-new-mail-hook
804 'display-time-event-handler)
805 (message "Mail check disabled"))))
807 (defun mail-source-fetch-maildir (source callback)
808 "Fetcher for maildir sources."
809 (mail-source-bind (maildir source)
812 (unless (string-match "/$" path)
813 (setq path (concat path "/")))
814 (dolist (subdir subdirs)
815 (when (file-directory-p (concat path subdir))
816 (setq mail-source-string (format "maildir:%s%s" path subdir))
817 (dolist (file (directory-files (concat path subdir) t))
818 (when (and (not (file-directory-p file))
820 (funcall function file mail-source-crash-box)
821 (let ((coding-system-for-write
822 mm-text-coding-system)
823 (coding-system-for-read
824 mm-text-coding-system))
825 (with-temp-file mail-source-crash-box
826 (insert-file-contents file)
827 (goto-char (point-min))
828 ;;; ;; Unix mail format
829 ;;; (unless (looking-at "\n*From ")
830 ;;; (insert "From maildir "
831 ;;; (current-time-string) "\n"))
832 ;;; (while (re-search-forward "^From " nil t)
833 ;;; (replace-match ">From "))
834 ;;; (goto-char (point-max))
837 (insert "\001\001\001\001\n"))
838 (delete-file file)))))
839 (incf found (mail-source-callback callback file))))))
843 (autoload 'imap-open "imap")
844 (autoload 'imap-authenticate "imap")
845 (autoload 'imap-mailbox-select "imap")
846 (autoload 'imap-mailbox-unselect "imap")
847 (autoload 'imap-mailbox-close "imap")
848 (autoload 'imap-search "imap")
849 (autoload 'imap-fetch "imap")
850 (autoload 'imap-close "imap")
851 (autoload 'imap-error-text "imap")
852 (autoload 'imap-message-flags-add "imap")
853 (autoload 'imap-list-to-message-set "imap")
854 (autoload 'imap-range-to-message-set "imap")
855 (autoload 'nnheader-ms-strip-cr "nnheader"))
857 (defvar mail-source-imap-file-coding-system 'binary
858 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
860 (defun mail-source-fetch-imap (source callback)
861 "Fetcher for imap sources."
862 (mail-source-bind (imap source)
863 (let ((from (format "%s:%s:%s" server user port))
865 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
866 (mail-source-string (format "imap:%s:%s" server mailbox))
868 (if (and (imap-open server port stream authentication buf)
870 user (or (cdr (assoc from mail-source-password-cache))
872 (imap-mailbox-select mailbox nil buf))
873 (let ((coding-system-for-write mail-source-imap-file-coding-system)
875 (with-temp-file mail-source-crash-box
876 ;; Avoid converting 8-bit chars from inserted strings to
878 (mm-disable-multibyte)
880 (with-current-buffer buf
881 (when (or imap-password
882 (assoc from mail-source-password-cache))
883 (push (cons from imap-password) mail-source-password-cache)))
884 ;; if predicate is nil, use all uids
885 (dolist (uid (imap-search (or predicate "1:*") buf))
886 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
888 (insert "From imap " (current-time-string) "\n")
891 (while (re-search-forward "^From " nil t)
892 (replace-match ">From "))
893 (goto-char (point-max))))
894 (nnheader-ms-strip-cr))
895 (incf found (mail-source-callback callback server))
896 (when (and remove fetchflag)
897 (imap-message-flags-add
898 (imap-range-to-message-set (gnus-compress-sequence remove))
901 (imap-mailbox-unselect buf)
902 (imap-mailbox-close buf))
905 ;; We nix out the password in case the error
906 ;; was because of a wrong password being given.
907 (setq mail-source-password-cache
908 (delq (assoc from mail-source-password-cache)
909 mail-source-password-cache))
910 (error (imap-error-text buf)))
915 (autoload 'webmail-fetch "webmail"))
917 (defun mail-source-fetch-webmail (source callback)
918 "Fetch for webmail source."
919 (mail-source-bind (webmail source)
920 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
921 (webmail-newmail-only dontexpunge)
922 (webmail-move-to-trash-can (not dontexpunge)))
923 (when (eq authentication 'password)
926 (cdr (assoc (format "webmail:%s:%s" subtype user)
927 mail-source-password-cache))
928 (mail-source-read-passwd
929 (format "Password for %s at %s: " user subtype))))
931 (not (assoc (format "webmail:%s:%s" subtype user)
932 mail-source-password-cache)))
933 (push (cons (format "webmail:%s:%s" subtype user) password)
934 mail-source-password-cache)))
935 (webmail-fetch mail-source-crash-box subtype user password)
936 (mail-source-callback callback (symbol-name subtype)))))
938 (provide 'mail-source)
940 ;;; mail-source.el ends here