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)
34 (defgroup mail-source nil
35 "The mail-fetching library."
38 (defcustom mail-sources '((file))
39 "*Where the mail backends will look for incoming mail.
40 This variable is a list of mail source specifiers.
41 See Info node `(gnus)Mail Source Specifiers'."
43 ;; This specification should be tidied up, particularly to avoid
44 ;; constant items appearing. (Perhaps there's scope for improvment
45 ;; in the widget code.)
47 (choice (const :tag "Default spool file" (file))
48 (list :tag "Specified spool file"
52 (cons :tag "Several files in a directory"
56 (const :tag "None" nil)
59 (list :inline t :tag "path"
60 (const :value :path) directory)
61 (list :inline t :tag "suffix"
62 (const :value :suffix) string)
63 (list :inline t :tag "predicate"
64 (const :value :predicate) function)
65 (list :inline t :tag "prescript"
66 (const :value :prescript) string)
67 (list :inline t :tag "postscript"
68 (const :value :postscript) string)
69 (list :inline t :tag "plugged"
70 (const :value :plugged) boolean)))))
71 (cons :tag "POP3 server"
75 (const :tag "None" nil)
78 (list :inline t :tag "server"
79 (const :value :server) string)
80 (list :inline t :tag "port"
81 (const :value :port) (choice number string))
82 (list :inline t :tag "user"
83 (const :value :user) string)
84 (list :inline t :tag "password"
85 (const :value :password) string)
86 (list :inline t :tag "program"
87 (const :value :program) string)
88 (list :inline t :tag "prescript"
89 (const :value :prescript) string)
90 (list :inline t :tag "postscript"
91 (const :value :postscript) string)
92 (list :inline t :tag "function"
93 (const :value :function) function)
94 (list :inline t :tag "authentication"
95 (const :value :authentication)
96 (choice (const password)
98 (list :inline t :tag "plugged"
99 (const :value :plugged) boolean)))))
100 (cons :tag "Maildir (qmail, postfix...)"
104 (const :tag "None" nil)
107 (list :inline t :tag "path"
108 (const :value :path) directory)
109 (list :inline t :tag "plugged"
110 (const :value :plugged) boolean)))))
111 (cons :tag "IMAP server"
115 (const :tag "None" nil)
118 (list :inline t :tag "server"
119 (const :value :server) string)
120 (list :inline t :tag "port"
122 (choice number string))
123 (list :inline t :tag "user"
124 (const :value :user) string)
125 (list :inline t :tag "password"
126 (const :value :password) string)
127 (list :inline t :tag "stream"
128 (const :value :stream)
129 (choice ,@(progn (require 'imap)
132 (list 'const (car a)))
133 imap-stream-alist))))
134 (list :inline t :tag "authenticator"
135 (const :value :authenticator)
136 (choice ,@(progn (require 'imap)
139 (list 'const (car a)))
140 imap-authenticator-alist))))
141 (list :inline t :tag "mailbox"
142 (const :value :mailbox) string)
143 (list :inline t :tag "predicate"
144 (const :value :predicate) function)
145 (list :inline t :tag "fetchflag"
146 (const :value :fetchflag) string)
147 (list :inline t :tag "dontexpunge"
148 (const :value :dontexpunge) boolean)
149 (list :inline t :tag "plugged"
150 (const :value :plugged) )))))
151 (cons :tag "Webmail server"
155 (const :tag "None" nil)
158 (list :inline t :tag "subtype"
159 (const :value :subtype)
160 ;; Should be generated from
161 ;; `webmail-type-definition', but we
162 ;; can't require webmail without W3.
163 (choice (const hotmail) (const yahoo)
164 (const netaddress) (const netscape)
166 (list :inline t :tag "user"
167 (const :value :user) string)
168 (list :inline t :tag "password"
169 (const :value :password) string)
170 (list :inline t :tag "dontexpunge"
171 (const :value :dontexpunge) boolean)
172 (list :inline t :tag "plugged"
173 (const :value :plugged) boolean))))))))
175 (defcustom mail-source-primary-source nil
176 "*Primary source for incoming mail.
177 If non-nil, this maildrop will be checked periodically for new mail."
181 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
182 "File where mail will be stored while processing it."
186 (defcustom mail-source-directory "~/Mail/"
187 "Directory where files (if any) will be stored."
191 (defcustom mail-source-default-file-modes 384
192 "Set the mode bits of all new mail files to this integer."
196 (defcustom mail-source-delete-incoming nil
197 "*If non-nil, delete incoming files after handling."
201 (defcustom mail-source-incoming-file-prefix "Incoming"
202 "Prefix for file name for storing incoming mail"
206 (defcustom mail-source-report-new-mail-interval 5
207 "Interval in minutes between checks for new mail."
211 (defcustom mail-source-idle-time-delay 5
212 "Number of idle seconds to wait before checking for new mail."
216 ;;; Internal variables.
218 (defvar mail-source-string ""
219 "A dynamically bound string that says what the current mail source is.")
221 (defvar mail-source-new-mail-available nil
222 "Flag indicating when new mail is available.")
225 (defvar mail-source-common-keyword-map
227 "Mapping from keywords to default values.
228 Common keywords should be listed here.")
230 (defvar mail-source-keyword-map
235 (:path (or (getenv "MAIL")
236 (concat "/usr/spool/mail/" (user-login-name)))))
240 (:predicate identity))
245 (:server (getenv "MAILHOST"))
247 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
251 (:authentication password))
253 (:path (or (getenv "MAILDIR") "~/Maildir/"))
254 (:subdirs ("new" "cur"))
257 (:server (getenv "MAILHOST"))
261 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
264 (:predicate "UNSEEN UNDELETED")
265 (:fetchflag "\\Deleted")
269 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
272 (:authentication password)))
273 "Mapping from keywords to default values.
274 All keywords that can be used must be listed here."))
276 (defvar mail-source-fetcher-alist
277 '((file mail-source-fetch-file)
278 (directory mail-source-fetch-directory)
279 (pop mail-source-fetch-pop)
280 (maildir mail-source-fetch-maildir)
281 (imap mail-source-fetch-imap)
282 (webmail mail-source-fetch-webmail))
283 "A mapping from source type to fetcher function.")
285 (defvar mail-source-password-cache nil)
287 (defvar mail-source-plugged t)
292 (defun mail-source-strip-keyword (keyword)
293 "Strip the leading colon off the KEYWORD."
294 (intern (substring (symbol-name keyword) 1))))
297 (defun mail-source-bind-1 (type)
298 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
300 (while (setq default (pop defaults))
301 (push (list (mail-source-strip-keyword (car default))
306 (defmacro mail-source-bind (type-source &rest body)
307 "Return a `let' form that binds all variables in source TYPE.
308 TYPE-SOURCE is a list where the first element is the TYPE, and
309 the second variable is the SOURCE.
310 At run time, the mail source specifier SOURCE will be inspected,
311 and the variables will be set according to it. Variables not
312 specified will be given default values.
314 After this is done, BODY will be executed in the scope
317 The variables bound and their default values are described by
318 the `mail-source-keyword-map' variable."
319 `(let ,(mail-source-bind-1 (car type-source))
320 (mail-source-set-1 ,(cadr type-source))
323 (put 'mail-source-bind 'lisp-indent-function 1)
324 (put 'mail-source-bind 'edebug-form-spec '(form body))
326 (defun mail-source-set-1 (source)
327 (let* ((type (pop source))
328 (defaults (cdr (assq type mail-source-keyword-map)))
329 default value keyword)
330 (while (setq default (pop defaults))
331 (set (mail-source-strip-keyword (setq keyword (car default)))
332 (if (setq value (plist-get source keyword))
333 (mail-source-value value)
334 (mail-source-value (cadr default)))))))
337 (defun mail-source-bind-common-1 ()
338 (let* ((defaults mail-source-common-keyword-map)
340 (while (setq default (pop defaults))
341 (push (list (mail-source-strip-keyword (car default))
346 (defun mail-source-set-common-1 (source)
347 (let* ((type (pop source))
348 (defaults mail-source-common-keyword-map)
349 (defaults-1 (cdr (assq type mail-source-keyword-map)))
350 default value keyword)
351 (while (setq default (pop defaults))
352 (set (mail-source-strip-keyword (setq keyword (car default)))
353 (if (setq value (plist-get source keyword))
354 (mail-source-value value)
355 (if (setq value (assq keyword defaults-1))
356 (mail-source-value (cadr value))
357 (mail-source-value (cadr default))))))))
359 (defmacro mail-source-bind-common (source &rest body)
360 "Return a `let' form that binds all common variables.
361 See `mail-source-bind'."
362 `(let ,(mail-source-bind-common-1)
363 (mail-source-set-common-1 source)
366 (put 'mail-source-bind-common 'lisp-indent-function 1)
367 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
369 (defun mail-source-value (value)
370 "Return the value of VALUE."
377 (functionp (car value)))
379 ;; Just return the value.
383 (defun mail-source-fetch (source callback)
384 "Fetch mail from SOURCE and call CALLBACK zero or more times.
385 CALLBACK will be called with the name of the file where (some of)
386 the mail from SOURCE is put.
387 Return the number of files that were found."
388 (mail-source-bind-common source
389 (if (or mail-source-plugged plugged)
391 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
394 (error "%S is an invalid mail source specification" source))
395 ;; If there's anything in the crash box, we do it first.
396 (when (file-exists-p mail-source-crash-box)
397 (message "Processing mail from %s..." mail-source-crash-box)
398 (setq found (mail-source-callback
399 callback mail-source-crash-box)))
402 (funcall function source callback)
405 (format "Mail source error (%s). Continue? " err))
406 (error "Cannot get new mail."))
409 (defun mail-source-make-complex-temp-name (prefix)
410 (let ((newname (make-temp-name prefix))
412 (while (file-exists-p newname)
413 (setq newprefix (concat newprefix "x"))
414 (setq newname (make-temp-name newprefix)))
417 (defun mail-source-callback (callback info)
418 "Call CALLBACK on the mail file, and then remove the mail file.
419 Pass INFO on to CALLBACK."
420 (if (or (not (file-exists-p mail-source-crash-box))
421 (zerop (nth 7 (file-attributes mail-source-crash-box))))
423 (when (file-exists-p mail-source-crash-box)
424 (delete-file mail-source-crash-box))
427 (funcall callback mail-source-crash-box info)
428 (when (file-exists-p mail-source-crash-box)
429 ;; Delete or move the incoming mail out of the way.
430 (if mail-source-delete-incoming
431 (delete-file mail-source-crash-box)
433 (mail-source-make-complex-temp-name
435 mail-source-incoming-file-prefix
436 mail-source-directory))))
437 (unless (file-exists-p (file-name-directory incoming))
438 (make-directory (file-name-directory incoming) t))
439 (rename-file mail-source-crash-box incoming t)))))))
441 (defun mail-source-movemail (from to)
442 "Move FROM to TO using movemail."
443 (if (not (file-writable-p to))
444 (error "Can't write to crash box %s. Not moving mail" to)
445 (let ((to (file-truename (expand-file-name to)))
447 (setq to (file-truename to)
448 from (file-truename from))
449 ;; Set TO if have not already done so, and rename or copy
450 ;; the file FROM to TO if and as appropriate.
453 ;; The crash box exists already.
455 ((not (file-exists-p from))
456 ;; There is no inbox.
458 ((zerop (nth 7 (file-attributes from)))
462 ;; If getting from mail spool directory, use movemail to move
463 ;; rather than just renaming, so as to interlock with the
467 (setq errors (generate-new-buffer " *mail source loss*"))
468 (let ((default-directory "/"))
474 (expand-file-name "movemail" exec-directory)
475 nil errors nil from to)))))
476 (when (file-exists-p to)
477 (set-file-modes to mail-source-default-file-modes))
478 (if (and (not (buffer-modified-p errors))
480 ;; No output => movemail won.
483 ;; There may be a warning about older revisions. We
485 (goto-char (point-min))
486 (if (search-forward "older revision" nil t)
488 ;; Probably a real error.
489 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
490 (goto-char (point-max))
491 (skip-chars-backward " \t")
492 (delete-region (point) (point-max))
493 (goto-char (point-min))
494 (when (looking-at "movemail: ")
495 (delete-region (point-min) (match-end 0)))
497 (format "movemail: %s (%d return). Continue? "
498 (buffer-string) result))
499 (error "%s" (buffer-string)))
502 (buffer-name errors))
503 (kill-buffer errors))
504 ;; Return whether we moved successfully or not.
507 (defun mail-source-movemail-and-remove (from to)
508 "Move FROM to TO using movemail, then remove FROM if empty."
509 (or (not (mail-source-movemail from to))
510 (not (zerop (nth 7 (file-attributes from))))
513 (defvar mail-source-read-passwd nil)
514 (defun mail-source-read-passwd (prompt &rest args)
515 "Read a password using PROMPT.
516 If ARGS, PROMPT is used as an argument to `format'."
519 (apply 'format prompt args)
521 (unless mail-source-read-passwd
522 (if (or (fboundp 'read-passwd) (load "passwd" t))
523 (setq mail-source-read-passwd 'read-passwd)
524 (unless (fboundp 'ange-ftp-read-passwd)
525 (autoload 'ange-ftp-read-passwd "ange-ftp"))
526 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
527 (funcall mail-source-read-passwd prompt)))
529 (defun mail-source-fetch-with-program (program)
530 (zerop (call-process shell-file-name nil nil nil
531 shell-command-switch program)))
533 (defun mail-source-run-script (script spec &optional delay)
535 (if (and (symbolp script) (fboundp script))
537 (mail-source-call-script
538 (format-spec script spec))))
542 (defun mail-source-call-script (script)
543 (let ((background nil))
544 (when (string-match "& *$" script)
545 (setq script (substring script 0 (match-beginning 0))
547 (call-process shell-file-name nil background nil
548 shell-command-switch script)))
551 ;;; Different fetchers
554 (defun mail-source-fetch-file (source callback)
555 "Fetcher for single-file sources."
556 (mail-source-bind (file source)
557 (mail-source-run-script
558 prescript (format-spec-make ?t mail-source-crash-box)
560 (let ((mail-source-string (format "file:%s" path)))
561 (if (mail-source-movemail path mail-source-crash-box)
563 (mail-source-callback callback path)
564 (mail-source-run-script
565 postscript (format-spec-make ?t mail-source-crash-box)))
568 (defun mail-source-fetch-directory (source callback)
569 "Fetcher for directory sources."
570 (mail-source-bind (directory source)
572 (mail-source-string (format "directory:%s" path)))
573 (dolist (file (directory-files
574 path t (concat (regexp-quote suffix) "$")))
575 (when (and (file-regular-p file)
576 (funcall predicate file)
577 (mail-source-movemail file mail-source-crash-box))
578 (incf found (mail-source-callback callback file))))
581 (defun mail-source-fetch-pop (source callback)
582 "Fetcher for single-file sources."
583 (mail-source-bind (pop source)
584 (mail-source-run-script
586 (format-spec-make ?p password ?t mail-source-crash-box
587 ?s server ?P port ?u user)
589 (let ((from (format "%s:%s:%s" server user port))
590 (mail-source-string (format "pop:%s@%s" user server))
592 (when (eq authentication 'password)
595 (cdr (assoc from mail-source-password-cache))
596 (mail-source-read-passwd
597 (format "Password for %s at %s: " user server)))))
599 (setenv "MAILHOST" server))
603 (mail-source-fetch-with-program
606 (format-spec-make ?p password ?t mail-source-crash-box
607 ?s server ?P port ?u user))))
609 (funcall function mail-source-crash-box))
610 ;; The default is to use pop3.el.
612 (let ((pop3-password password)
614 (pop3-mailhost server)
616 (pop3-authentication-scheme
617 (if (eq authentication 'apop) 'apop 'pass)))
618 (save-excursion (pop3-movemail mail-source-crash-box))))))
621 (when (eq authentication 'password)
622 (unless (assoc from mail-source-password-cache)
623 (push (cons from password) mail-source-password-cache)))
625 (mail-source-callback callback server)
626 ;; Update display-time's mail flag, if relevant.
627 (if (equal source mail-source-primary-source)
628 (setq mail-source-new-mail-available nil))
629 (mail-source-run-script
631 (format-spec-make ?p password ?t mail-source-crash-box
632 ?s server ?P port ?u user))))
633 ;; We nix out the password in case the error
634 ;; was because of a wrong password being given.
635 (setq mail-source-password-cache
636 (delq (assoc from mail-source-password-cache)
637 mail-source-password-cache))
640 (defun mail-source-check-pop (source)
641 "Check whether there is new mail."
642 (mail-source-bind (pop source)
643 (let ((from (format "%s:%s:%s" server user port))
644 (mail-source-string (format "pop:%s@%s" user server))
646 (when (eq authentication 'password)
649 (cdr (assoc from mail-source-password-cache))
650 (mail-source-read-passwd
651 (format "Password for %s at %s: " user server))))
652 (unless (assoc from mail-source-password-cache)
653 (push (cons from password) mail-source-password-cache)))
655 (setenv "MAILHOST" server))
658 ;; No easy way to check whether mail is waiting for these.
661 ;; The default is to use pop3.el.
663 (let ((pop3-password password)
665 (pop3-mailhost server)
667 (pop3-authentication-scheme
668 (if (eq authentication 'apop) 'apop 'pass)))
669 (save-excursion (pop3-get-message-count))))))
671 ;; Inform display-time that we have new mail.
672 (setq mail-source-new-mail-available (> result 0))
673 ;; We nix out the password in case the error
674 ;; was because of a wrong password being given.
675 (setq mail-source-password-cache
676 (delq (assoc from mail-source-password-cache)
677 mail-source-password-cache)))
680 (defun mail-source-new-mail-p ()
681 "Handler for `display-time' to indicate when new mail is available."
682 ;; Only report flag setting; flag is updated on a different schedule.
683 mail-source-new-mail-available)
686 (defvar mail-source-report-new-mail nil)
687 (defvar mail-source-report-new-mail-timer nil)
688 (defvar mail-source-report-new-mail-idle-timer nil)
691 (if (featurep 'xemacs)
695 (defun mail-source-start-idle-timer ()
696 ;; Start our idle timer if necessary, so we delay the check until the
697 ;; user isn't typing.
698 (unless mail-source-report-new-mail-idle-timer
699 (setq mail-source-report-new-mail-idle-timer
701 mail-source-idle-time-delay
704 (setq mail-source-report-new-mail-idle-timer nil)
705 (mail-source-check-pop mail-source-primary-source))))
706 ;; Since idle timers created when Emacs is already in the idle
707 ;; state don't get activated until Emacs _next_ becomes idle, we
708 ;; need to force our timer to be considered active now. We do
709 ;; this by being naughty and poking the timer internals directly
710 ;; (element 0 of the vector is nil if the timer is active).
711 (aset mail-source-report-new-mail-idle-timer 0 nil)))
713 (defun mail-source-report-new-mail (arg)
714 "Toggle whether to report when new mail is available.
715 This only works when `display-time' is enabled."
717 (if (not mail-source-primary-source)
718 (error "Need to set `mail-source-primary-source' to check for new mail."))
719 (let ((on (if (null arg)
720 (not mail-source-report-new-mail)
721 (> (prefix-numeric-value arg) 0))))
722 (setq mail-source-report-new-mail on)
723 (and mail-source-report-new-mail-timer
724 (cancel-timer mail-source-report-new-mail-timer))
725 (and mail-source-report-new-mail-idle-timer
726 (cancel-timer mail-source-report-new-mail-idle-timer))
727 (setq mail-source-report-new-mail-timer nil)
728 (setq mail-source-report-new-mail-idle-timer nil)
732 ;; display-time-mail-function is an Emacs 21 feature.
733 (setq display-time-mail-function #'mail-source-new-mail-p)
734 ;; Set up the main timer.
735 (setq mail-source-report-new-mail-timer
736 (run-at-time t (* 60 mail-source-report-new-mail-interval)
737 #'mail-source-start-idle-timer))
738 ;; When you get new mail, clear "Mail" from the mode line.
739 (add-hook 'nnmail-post-get-new-mail-hook
740 'display-time-event-handler)
741 (message "Mail check enabled"))
742 (setq display-time-mail-function nil)
743 (remove-hook 'nnmail-post-get-new-mail-hook
744 'display-time-event-handler)
745 (message "Mail check disabled"))))
747 (defun mail-source-fetch-maildir (source callback)
748 "Fetcher for maildir sources."
749 (mail-source-bind (maildir source)
752 (unless (string-match "/$" path)
753 (setq path (concat path "/")))
754 (dolist (subdir subdirs)
755 (when (file-directory-p (concat path subdir))
756 (setq mail-source-string (format "maildir:%s%s" path subdir))
757 (dolist (file (directory-files (concat path subdir) t))
758 (when (and (not (file-directory-p file))
760 (funcall function file mail-source-crash-box)
761 (let ((coding-system-for-write
762 mm-text-coding-system)
763 (coding-system-for-read
764 mm-text-coding-system))
765 (with-temp-file mail-source-crash-box
766 (insert-file-contents file)
767 (goto-char (point-min))
768 ;;; ;; Unix mail format
769 ;;; (unless (looking-at "\n*From ")
770 ;;; (insert "From maildir "
771 ;;; (current-time-string) "\n"))
772 ;;; (while (re-search-forward "^From " nil t)
773 ;;; (replace-match ">From "))
775 (insert "\001\001\001\001\n")
776 (goto-char (point-max))
778 (delete-file file)))))
779 (incf found (mail-source-callback callback file))))))
783 (autoload 'imap-open "imap")
784 (autoload 'imap-authenticate "imap")
785 (autoload 'imap-mailbox-select "imap")
786 (autoload 'imap-mailbox-unselect "imap")
787 (autoload 'imap-mailbox-close "imap")
788 (autoload 'imap-search "imap")
789 (autoload 'imap-fetch "imap")
790 (autoload 'imap-close "imap")
791 (autoload 'imap-error-text "imap")
792 (autoload 'imap-message-flags-add "imap")
793 (autoload 'imap-list-to-message-set "imap")
794 (autoload 'nnheader-ms-strip-cr "nnheader"))
796 (defun mail-source-fetch-imap (source callback)
797 "Fetcher for imap sources."
798 (mail-source-bind (imap source)
799 (let ((from (format "%s:%s:%s" server user port))
801 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
802 (mail-source-string (format "imap:%s:%s" server mailbox))
804 (if (and (imap-open server port stream authentication buf)
806 user (or (cdr (assoc from mail-source-password-cache))
808 (imap-mailbox-select mailbox nil buf))
809 (let (str (coding-system-for-write 'binary))
810 (with-temp-file mail-source-crash-box
811 ;; In some versions of FSF Emacs, inserting unibyte
812 ;; string into multibyte buffer may convert 8-bit chars
813 ;; into latin-iso8859-1 chars, which results \201's.
814 (mm-disable-multibyte)
816 (with-current-buffer buf
817 (when (or imap-password
818 (assoc from mail-source-password-cache))
819 (push (cons from imap-password) mail-source-password-cache)))
820 ;; if predicate is nil, use all uids
821 (dolist (uid (imap-search (or predicate "1:*") buf))
822 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
824 (insert "From imap " (current-time-string) "\n")
827 (while (re-search-forward "^From " nil t)
828 (replace-match ">From "))
829 (goto-char (point-max))))
830 (nnheader-ms-strip-cr))
831 (incf found (mail-source-callback callback server))
832 (when (and remove fetchflag)
833 (imap-message-flags-add
834 (imap-list-to-message-set remove) fetchflag nil buf))
836 (imap-mailbox-unselect buf)
837 (imap-mailbox-close buf))
840 ;; We nix out the password in case the error
841 ;; was because of a wrong password being given.
842 (setq mail-source-password-cache
843 (delq (assoc from mail-source-password-cache)
844 mail-source-password-cache))
845 (error (imap-error-text buf)))
850 (autoload 'webmail-fetch "webmail"))
852 (defun mail-source-fetch-webmail (source callback)
853 "Fetch for webmail source."
854 (mail-source-bind (webmail source)
855 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
856 (webmail-newmail-only dontexpunge)
857 (webmail-move-to-trash-can (not dontexpunge)))
858 (when (eq authentication 'password)
861 (cdr (assoc (format "webmail:%s:%s" subtype user)
862 mail-source-password-cache))
863 (mail-source-read-passwd
864 (format "Password for %s at %s: " user subtype))))
866 (not (assoc (format "webmail:%s:%s" subtype user)
867 mail-source-password-cache)))
868 (push (cons (format "webmail:%s:%s" subtype user) password)
869 mail-source-password-cache)))
870 (webmail-fetch mail-source-crash-box subtype user password)
871 (mail-source-callback callback (symbol-name subtype)))))
873 (provide 'mail-source)
875 ;;; mail-source.el ends here