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.
28 (eval-when-compile (require 'cl))
30 (autoload 'pop3-movemail "pop3")
31 (autoload 'pop3-get-message-count "pop3"))
32 (require 'format-spec)
35 (defgroup mail-source nil
36 "The mail-fetching library."
39 (defcustom mail-sources nil
40 "*Where the mail backends will look for incoming mail.
41 This variable is a list of mail source specifiers.
42 See Info node `(gnus)Mail Source Specifiers'."
44 ;; This specification should be tidied up, particularly to avoid
45 ;; constant items appearing. (Perhaps there's scope for improvment
46 ;; in the widget code.)
48 (choice (const :tag "Default spool file" (file))
49 (list :tag "Specified spool file"
53 (cons :tag "Several files in a directory"
57 (const :tag "None" nil)
60 (list :inline t :tag "path"
61 (const :value :path) directory)
62 (list :inline t :tag "suffix"
63 (const :value :suffix) string)
64 (list :inline t :tag "predicate"
65 (const :value :predicate) function)
66 (list :inline t :tag "prescript"
67 (const :value :prescript) string)
68 (list :inline t :tag "postscript"
69 (const :value :postscript) string)
70 (list :inline t :tag "plugged"
71 (const :value :plugged) boolean)))))
72 (cons :tag "POP3 server"
76 (const :tag "None" nil)
79 (list :inline t :tag "server"
80 (const :value :server) string)
81 (list :inline t :tag "port"
82 (const :value :port) (choice number string))
83 (list :inline t :tag "user"
84 (const :value :user) string)
85 (list :inline t :tag "password"
86 (const :value :password) string)
87 (list :inline t :tag "program"
88 (const :value :program) string)
89 (list :inline t :tag "prescript"
90 (const :value :prescript) string)
91 (list :inline t :tag "postscript"
92 (const :value :postscript) string)
93 (list :inline t :tag "function"
94 (const :value :function) function)
95 (list :inline t :tag "authentication"
96 (const :value :authentication)
97 (choice (const password)
99 (list :inline t :tag "plugged"
100 (const :value :plugged) boolean)))))
101 (cons :tag "Maildir (qmail, postfix...)"
105 (const :tag "None" nil)
108 (list :inline t :tag "path"
109 (const :value :path) directory)
110 (list :inline t :tag "plugged"
111 (const :value :plugged) boolean)))))
112 (cons :tag "IMAP server"
116 (const :tag "None" nil)
119 (list :inline t :tag "server"
120 (const :value :server) string)
121 (list :inline t :tag "port"
123 (choice number string))
124 (list :inline t :tag "user"
125 (const :value :user) string)
126 (list :inline t :tag "password"
127 (const :value :password) string)
128 (list :inline t :tag "stream"
129 (const :value :stream)
130 (choice ,@(progn (require 'imap)
133 (list 'const (car a)))
134 imap-stream-alist))))
135 (list :inline t :tag "authenticator"
136 (const :value :authenticator)
137 (choice ,@(progn (require 'imap)
140 (list 'const (car a)))
141 imap-authenticator-alist))))
142 (list :inline t :tag "mailbox"
143 (const :value :mailbox) string)
144 (list :inline t :tag "predicate"
145 (const :value :predicate) function)
146 (list :inline t :tag "fetchflag"
147 (const :value :fetchflag) string)
148 (list :inline t :tag "dontexpunge"
149 (const :value :dontexpunge) boolean)
150 (list :inline t :tag "plugged"
151 (const :value :plugged) )))))
152 (cons :tag "Webmail server"
156 (const :tag "None" nil)
159 (list :inline t :tag "subtype"
160 (const :value :subtype)
161 ;; Should be generated from
162 ;; `webmail-type-definition', but we
163 ;; can't require webmail without W3.
164 (choice (const hotmail) (const yahoo)
165 (const netaddress) (const netscape)
167 (list :inline t :tag "user"
168 (const :value :user) string)
169 (list :inline t :tag "password"
170 (const :value :password) string)
171 (list :inline t :tag "dontexpunge"
172 (const :value :dontexpunge) boolean)
173 (list :inline t :tag "plugged"
174 (const :value :plugged) boolean))))))))
176 (defcustom mail-source-primary-source nil
177 "*Primary source for incoming mail.
178 If non-nil, this maildrop will be checked periodically for new mail."
182 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
183 "File where mail will be stored while processing it."
187 (defcustom mail-source-directory "~/Mail/"
188 "Directory where files (if any) will be stored."
192 (defcustom mail-source-default-file-modes 384
193 "Set the mode bits of all new mail files to this integer."
197 (defcustom mail-source-delete-incoming nil
198 "*If non-nil, delete incoming files after handling."
202 (defcustom mail-source-incoming-file-prefix "Incoming"
203 "Prefix for file name for storing incoming mail"
207 (defcustom mail-source-report-new-mail-interval 5
208 "Interval in minutes between checks for new mail."
212 (defcustom mail-source-idle-time-delay 5
213 "Number of idle seconds to wait before checking for new mail."
217 ;;; Internal variables.
219 (defvar mail-source-string ""
220 "A dynamically bound string that says what the current mail source is.")
222 (defvar mail-source-new-mail-available nil
223 "Flag indicating when new mail is available.")
226 (defvar mail-source-common-keyword-map
228 "Mapping from keywords to default values.
229 Common keywords should be listed here.")
231 (defvar mail-source-keyword-map
236 (:path (or (getenv "MAIL")
237 (expand-file-name (user-login-name) rmail-spool-directory))))
241 (:predicate identity))
246 (:server (getenv "MAILHOST"))
248 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
252 (:authentication password))
254 (:path (or (getenv "MAILDIR") "~/Maildir/"))
255 (:subdirs ("new" "cur"))
258 (:server (getenv "MAILHOST"))
262 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
265 (:predicate "UNSEEN UNDELETED")
266 (:fetchflag "\\Deleted")
270 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
273 (:authentication password)))
274 "Mapping from keywords to default values.
275 All keywords that can be used must be listed here."))
277 (defvar mail-source-fetcher-alist
278 '((file mail-source-fetch-file)
279 (directory mail-source-fetch-directory)
280 (pop mail-source-fetch-pop)
281 (maildir mail-source-fetch-maildir)
282 (imap mail-source-fetch-imap)
283 (webmail mail-source-fetch-webmail))
284 "A mapping from source type to fetcher function.")
286 (defvar mail-source-password-cache nil)
288 (defvar mail-source-plugged t)
293 (defun mail-source-strip-keyword (keyword)
294 "Strip the leading colon off the KEYWORD."
295 (intern (substring (symbol-name keyword) 1))))
298 (defun mail-source-bind-1 (type)
299 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
301 (while (setq default (pop defaults))
302 (push (list (mail-source-strip-keyword (car default))
307 (defmacro mail-source-bind (type-source &rest body)
308 "Return a `let' form that binds all variables in source TYPE.
309 TYPE-SOURCE is a list where the first element is the TYPE, and
310 the second variable is the SOURCE.
311 At run time, the mail source specifier SOURCE will be inspected,
312 and the variables will be set according to it. Variables not
313 specified will be given default values.
315 After this is done, BODY will be executed in the scope
318 The variables bound and their default values are described by
319 the `mail-source-keyword-map' variable."
320 `(let ,(mail-source-bind-1 (car type-source))
321 (mail-source-set-1 ,(cadr type-source))
324 (put 'mail-source-bind 'lisp-indent-function 1)
325 (put 'mail-source-bind 'edebug-form-spec '(form body))
327 (defun mail-source-set-1 (source)
328 (let* ((type (pop source))
329 (defaults (cdr (assq type mail-source-keyword-map)))
330 default value keyword)
331 (while (setq default (pop defaults))
332 (set (mail-source-strip-keyword (setq keyword (car default)))
333 (if (setq value (plist-get source keyword))
334 (mail-source-value value)
335 (mail-source-value (cadr default)))))))
338 (defun mail-source-bind-common-1 ()
339 (let* ((defaults mail-source-common-keyword-map)
341 (while (setq default (pop defaults))
342 (push (list (mail-source-strip-keyword (car default))
347 (defun mail-source-set-common-1 (source)
348 (let* ((type (pop source))
349 (defaults mail-source-common-keyword-map)
350 (defaults-1 (cdr (assq type mail-source-keyword-map)))
351 default value keyword)
352 (while (setq default (pop defaults))
353 (set (mail-source-strip-keyword (setq keyword (car default)))
354 (if (setq value (plist-get source keyword))
355 (mail-source-value value)
356 (if (setq value (assq keyword defaults-1))
357 (mail-source-value (cadr value))
358 (mail-source-value (cadr default))))))))
360 (defmacro mail-source-bind-common (source &rest body)
361 "Return a `let' form that binds all common variables.
362 See `mail-source-bind'."
363 `(let ,(mail-source-bind-common-1)
364 (mail-source-set-common-1 source)
367 (put 'mail-source-bind-common 'lisp-indent-function 1)
368 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
370 (defun mail-source-value (value)
371 "Return the value of VALUE."
378 (functionp (car value)))
380 ;; Just return the value.
384 (defun mail-source-fetch (source callback)
385 "Fetch mail from SOURCE and call CALLBACK zero or more times.
386 CALLBACK will be called with the name of the file where (some of)
387 the mail from SOURCE is put.
388 Return the number of files that were found."
389 (mail-source-bind-common source
390 (if (or mail-source-plugged plugged)
392 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
395 (error "%S is an invalid mail source specification" source))
396 ;; If there's anything in the crash box, we do it first.
397 (when (file-exists-p mail-source-crash-box)
398 (message "Processing mail from %s..." mail-source-crash-box)
399 (setq found (mail-source-callback
400 callback mail-source-crash-box)))
403 (funcall function source callback)
406 (format "Mail source error (%s). Continue? " err))
407 (error "Cannot get new mail."))
410 (defun mail-source-make-complex-temp-name (prefix)
411 (let ((newname (make-temp-name prefix))
413 (while (file-exists-p newname)
414 (setq newprefix (concat newprefix "x"))
415 (setq newname (make-temp-name newprefix)))
418 (defun mail-source-callback (callback info)
419 "Call CALLBACK on the mail file, and then remove the mail file.
420 Pass INFO on to CALLBACK."
421 (if (or (not (file-exists-p mail-source-crash-box))
422 (zerop (nth 7 (file-attributes mail-source-crash-box))))
424 (when (file-exists-p mail-source-crash-box)
425 (delete-file mail-source-crash-box))
428 (funcall callback mail-source-crash-box info)
429 (when (file-exists-p mail-source-crash-box)
430 ;; Delete or move the incoming mail out of the way.
431 (if mail-source-delete-incoming
432 (delete-file mail-source-crash-box)
434 (mail-source-make-complex-temp-name
436 mail-source-incoming-file-prefix
437 mail-source-directory))))
438 (unless (file-exists-p (file-name-directory incoming))
439 (make-directory (file-name-directory incoming) t))
440 (rename-file mail-source-crash-box incoming t)))))))
442 (defun mail-source-movemail (from to)
443 "Move FROM to TO using movemail."
444 (if (not (file-writable-p to))
445 (error "Can't write to crash box %s. Not moving mail" to)
446 (let ((to (file-truename (expand-file-name to)))
448 (setq to (file-truename to)
449 from (file-truename from))
450 ;; Set TO if have not already done so, and rename or copy
451 ;; the file FROM to TO if and as appropriate.
454 ;; The crash box exists already.
456 ((not (file-exists-p from))
457 ;; There is no inbox.
459 ((zerop (nth 7 (file-attributes from)))
463 ;; If getting from mail spool directory, use movemail to move
464 ;; rather than just renaming, so as to interlock with the
468 (setq errors (generate-new-buffer " *mail source loss*"))
469 (let ((default-directory "/"))
475 (expand-file-name "movemail" exec-directory)
476 nil errors nil from to)))))
477 (when (file-exists-p to)
478 (set-file-modes to mail-source-default-file-modes))
479 (if (and (not (buffer-modified-p errors))
481 ;; No output => movemail won.
484 ;; There may be a warning about older revisions. We
486 (goto-char (point-min))
487 (if (search-forward "older revision" nil t)
489 ;; Probably a real error.
490 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
491 (goto-char (point-max))
492 (skip-chars-backward " \t")
493 (delete-region (point) (point-max))
494 (goto-char (point-min))
495 (when (looking-at "movemail: ")
496 (delete-region (point-min) (match-end 0)))
498 (format "movemail: %s (%d return). Continue? "
499 (buffer-string) result))
500 (error "%s" (buffer-string)))
503 (buffer-name errors))
504 (kill-buffer errors))
505 ;; Return whether we moved successfully or not.
508 (defun mail-source-movemail-and-remove (from to)
509 "Move FROM to TO using movemail, then remove FROM if empty."
510 (or (not (mail-source-movemail from to))
511 (not (zerop (nth 7 (file-attributes from))))
514 (defvar mail-source-read-passwd nil)
515 (defun mail-source-read-passwd (prompt &rest args)
516 "Read a password using PROMPT.
517 If ARGS, PROMPT is used as an argument to `format'."
520 (apply 'format prompt args)
522 (unless mail-source-read-passwd
523 (if (or (fboundp 'read-passwd) (load "passwd" t))
524 (setq mail-source-read-passwd 'read-passwd)
525 (unless (fboundp 'ange-ftp-read-passwd)
526 (autoload 'ange-ftp-read-passwd "ange-ftp"))
527 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
528 (funcall mail-source-read-passwd prompt)))
530 (defun mail-source-fetch-with-program (program)
531 (zerop (call-process shell-file-name nil nil nil
532 shell-command-switch program)))
534 (defun mail-source-run-script (script spec &optional delay)
536 (if (and (symbolp script) (fboundp script))
538 (mail-source-call-script
539 (format-spec script spec))))
543 (defun mail-source-call-script (script)
544 (let ((background nil))
545 (when (string-match "& *$" script)
546 (setq script (substring script 0 (match-beginning 0))
548 (call-process shell-file-name nil background nil
549 shell-command-switch script)))
552 ;;; Different fetchers
555 (defun mail-source-fetch-file (source callback)
556 "Fetcher for single-file sources."
557 (mail-source-bind (file source)
558 (mail-source-run-script
559 prescript (format-spec-make ?t mail-source-crash-box)
561 (let ((mail-source-string (format "file:%s" path)))
562 (if (mail-source-movemail path mail-source-crash-box)
564 (mail-source-callback callback path)
565 (mail-source-run-script
566 postscript (format-spec-make ?t mail-source-crash-box)))
569 (defun mail-source-fetch-directory (source callback)
570 "Fetcher for directory sources."
571 (mail-source-bind (directory source)
573 (mail-source-string (format "directory:%s" path)))
574 (dolist (file (directory-files
575 path t (concat (regexp-quote suffix) "$")))
576 (when (and (file-regular-p file)
577 (funcall predicate file)
578 (mail-source-movemail file mail-source-crash-box))
579 (incf found (mail-source-callback callback file))))
582 (defun mail-source-fetch-pop (source callback)
583 "Fetcher for single-file sources."
584 (mail-source-bind (pop source)
585 (mail-source-run-script
587 (format-spec-make ?p password ?t mail-source-crash-box
588 ?s server ?P port ?u user)
590 (let ((from (format "%s:%s:%s" server user port))
591 (mail-source-string (format "pop:%s@%s" user server))
593 (when (eq authentication 'password)
596 (cdr (assoc from mail-source-password-cache))
597 (mail-source-read-passwd
598 (format "Password for %s at %s: " user server)))))
600 (setenv "MAILHOST" server))
604 (mail-source-fetch-with-program
607 (format-spec-make ?p password ?t mail-source-crash-box
608 ?s server ?P port ?u user))))
610 (funcall function mail-source-crash-box))
611 ;; The default is to use pop3.el.
613 (let ((pop3-password password)
615 (pop3-mailhost server)
617 (pop3-authentication-scheme
618 (if (eq authentication 'apop) 'apop 'pass)))
619 (save-excursion (pop3-movemail mail-source-crash-box))))))
622 (when (eq authentication 'password)
623 (unless (assoc from mail-source-password-cache)
624 (push (cons from password) mail-source-password-cache)))
626 (mail-source-callback callback server)
627 ;; Update display-time's mail flag, if relevant.
628 (if (equal source mail-source-primary-source)
629 (setq mail-source-new-mail-available nil))
630 (mail-source-run-script
632 (format-spec-make ?p password ?t mail-source-crash-box
633 ?s server ?P port ?u user))))
634 ;; We nix out the password in case the error
635 ;; was because of a wrong password being given.
636 (setq mail-source-password-cache
637 (delq (assoc from mail-source-password-cache)
638 mail-source-password-cache))
641 (defun mail-source-check-pop (source)
642 "Check whether there is new mail."
643 (mail-source-bind (pop source)
644 (let ((from (format "%s:%s:%s" server user port))
645 (mail-source-string (format "pop:%s@%s" user server))
647 (when (eq authentication 'password)
650 (cdr (assoc from mail-source-password-cache))
651 (mail-source-read-passwd
652 (format "Password for %s at %s: " user server))))
653 (unless (assoc from mail-source-password-cache)
654 (push (cons from password) mail-source-password-cache)))
656 (setenv "MAILHOST" server))
659 ;; No easy way to check whether mail is waiting for these.
662 ;; The default is to use pop3.el.
664 (let ((pop3-password password)
666 (pop3-mailhost server)
668 (pop3-authentication-scheme
669 (if (eq authentication 'apop) 'apop 'pass)))
670 (save-excursion (pop3-get-message-count))))))
672 ;; Inform display-time that we have new mail.
673 (setq mail-source-new-mail-available (> result 0))
674 ;; We nix out the password in case the error
675 ;; was because of a wrong password being given.
676 (setq mail-source-password-cache
677 (delq (assoc from mail-source-password-cache)
678 mail-source-password-cache)))
681 (defun mail-source-new-mail-p ()
682 "Handler for `display-time' to indicate when new mail is available."
683 ;; Only report flag setting; flag is updated on a different schedule.
684 mail-source-new-mail-available)
687 (defvar mail-source-report-new-mail nil)
688 (defvar mail-source-report-new-mail-timer nil)
689 (defvar mail-source-report-new-mail-idle-timer nil)
692 (if (featurep 'xemacs)
696 (defun mail-source-start-idle-timer ()
697 ;; Start our idle timer if necessary, so we delay the check until the
698 ;; user isn't typing.
699 (unless mail-source-report-new-mail-idle-timer
700 (setq mail-source-report-new-mail-idle-timer
702 mail-source-idle-time-delay
705 (setq mail-source-report-new-mail-idle-timer nil)
706 (mail-source-check-pop mail-source-primary-source))))
707 ;; Since idle timers created when Emacs is already in the idle
708 ;; state don't get activated until Emacs _next_ becomes idle, we
709 ;; need to force our timer to be considered active now. We do
710 ;; this by being naughty and poking the timer internals directly
711 ;; (element 0 of the vector is nil if the timer is active).
712 (aset mail-source-report-new-mail-idle-timer 0 nil)))
714 (defun mail-source-report-new-mail (arg)
715 "Toggle whether to report when new mail is available.
716 This only works when `display-time' is enabled."
718 (if (not mail-source-primary-source)
719 (error "Need to set `mail-source-primary-source' to check for new mail."))
720 (let ((on (if (null arg)
721 (not mail-source-report-new-mail)
722 (> (prefix-numeric-value arg) 0))))
723 (setq mail-source-report-new-mail on)
724 (and mail-source-report-new-mail-timer
725 (nnheader-cancel-timer mail-source-report-new-mail-timer))
726 (and mail-source-report-new-mail-idle-timer
727 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
728 (setq mail-source-report-new-mail-timer nil)
729 (setq mail-source-report-new-mail-idle-timer nil)
733 ;; display-time-mail-function is an Emacs 21 feature.
734 (setq display-time-mail-function #'mail-source-new-mail-p)
735 ;; Set up the main timer.
736 (setq mail-source-report-new-mail-timer
737 (run-at-time t (* 60 mail-source-report-new-mail-interval)
738 #'mail-source-start-idle-timer))
739 ;; When you get new mail, clear "Mail" from the mode line.
740 (add-hook 'nnmail-post-get-new-mail-hook
741 'display-time-event-handler)
742 (message "Mail check enabled"))
743 (setq display-time-mail-function nil)
744 (remove-hook 'nnmail-post-get-new-mail-hook
745 'display-time-event-handler)
746 (message "Mail check disabled"))))
748 (defun mail-source-fetch-maildir (source callback)
749 "Fetcher for maildir sources."
750 (mail-source-bind (maildir source)
753 (unless (string-match "/$" path)
754 (setq path (concat path "/")))
755 (dolist (subdir subdirs)
756 (when (file-directory-p (concat path subdir))
757 (setq mail-source-string (format "maildir:%s%s" path subdir))
758 (dolist (file (directory-files (concat path subdir) t))
759 (when (and (not (file-directory-p file))
761 (funcall function file mail-source-crash-box)
762 (let ((coding-system-for-write
763 mm-text-coding-system)
764 (coding-system-for-read
765 mm-text-coding-system))
766 (with-temp-file mail-source-crash-box
767 (insert-file-contents file)
768 (goto-char (point-min))
769 ;;; ;; Unix mail format
770 ;;; (unless (looking-at "\n*From ")
771 ;;; (insert "From maildir "
772 ;;; (current-time-string) "\n"))
773 ;;; (while (re-search-forward "^From " nil t)
774 ;;; (replace-match ">From "))
775 ;;; (goto-char (point-max))
778 (insert "\001\001\001\001\n"))
779 (delete-file file)))))
780 (incf found (mail-source-callback callback file))))))
784 (autoload 'imap-open "imap")
785 (autoload 'imap-authenticate "imap")
786 (autoload 'imap-mailbox-select "imap")
787 (autoload 'imap-mailbox-unselect "imap")
788 (autoload 'imap-mailbox-close "imap")
789 (autoload 'imap-search "imap")
790 (autoload 'imap-fetch "imap")
791 (autoload 'imap-close "imap")
792 (autoload 'imap-error-text "imap")
793 (autoload 'imap-message-flags-add "imap")
794 (autoload 'imap-list-to-message-set "imap")
795 (autoload 'imap-range-to-message-set "imap")
796 (autoload 'nnheader-ms-strip-cr "nnheader"))
798 (defvar mail-source-imap-file-coding-system 'binary
799 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
801 (defun mail-source-fetch-imap (source callback)
802 "Fetcher for imap sources."
803 (mail-source-bind (imap source)
804 (let ((from (format "%s:%s:%s" server user port))
806 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
807 (mail-source-string (format "imap:%s:%s" server mailbox))
809 (if (and (imap-open server port stream authentication buf)
811 user (or (cdr (assoc from mail-source-password-cache))
813 (imap-mailbox-select mailbox nil buf))
814 (let ((coding-system-for-write mail-source-imap-file-coding-system)
816 (with-temp-file mail-source-crash-box
817 ;; Avoid converting 8-bit chars from inserted strings to
819 (mm-disable-multibyte)
821 (with-current-buffer buf
822 (when (or imap-password
823 (assoc from mail-source-password-cache))
824 (push (cons from imap-password) mail-source-password-cache)))
825 ;; if predicate is nil, use all uids
826 (dolist (uid (imap-search (or predicate "1:*") buf))
827 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
829 (insert "From imap " (current-time-string) "\n")
832 (while (re-search-forward "^From " nil t)
833 (replace-match ">From "))
834 (goto-char (point-max))))
835 (nnheader-ms-strip-cr))
836 (incf found (mail-source-callback callback server))
837 (when (and remove fetchflag)
838 (imap-message-flags-add
839 (imap-range-to-message-set (gnus-compress-sequence remove))
842 (imap-mailbox-unselect buf)
843 (imap-mailbox-close buf))
846 ;; We nix out the password in case the error
847 ;; was because of a wrong password being given.
848 (setq mail-source-password-cache
849 (delq (assoc from mail-source-password-cache)
850 mail-source-password-cache))
851 (error (imap-error-text buf)))
856 (autoload 'webmail-fetch "webmail"))
858 (defun mail-source-fetch-webmail (source callback)
859 "Fetch for webmail source."
860 (mail-source-bind (webmail source)
861 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
862 (webmail-newmail-only dontexpunge)
863 (webmail-move-to-trash-can (not dontexpunge)))
864 (when (eq authentication 'password)
867 (cdr (assoc (format "webmail:%s:%s" subtype user)
868 mail-source-password-cache))
869 (mail-source-read-passwd
870 (format "Password for %s at %s: " user subtype))))
872 (not (assoc (format "webmail:%s:%s" subtype user)
873 mail-source-password-cache)))
874 (push (cons (format "webmail:%s:%s" subtype user) password)
875 mail-source-password-cache)))
876 (webmail-fetch mail-source-crash-box subtype user password)
877 (mail-source-callback callback (symbol-name subtype)))))
879 (provide 'mail-source)
881 ;;; mail-source.el ends here