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))
31 (defvar pop3-leave-mail-on-server)
32 (autoload 'pop3-movemail "pop3")
33 (autoload 'pop3-get-message-count "pop3"))
34 (require 'format-spec)
36 (defgroup mail-source nil
37 "The mail-fetching library."
40 (defcustom mail-sources '((file))
41 "*Where the mail backends will look for incoming mail.
42 This variable is a list of mail source specifiers.
43 See Info node `(gnus)Mail Source Specifiers'."
45 ;; This specification should be tidied up, particularly to avoid
46 ;; constant items appearing. (Perhaps there's scope for improvment
47 ;; in the widget code.)
49 (choice (const :tag "Default spool file" (file))
50 (list :tag "Specified spool file"
54 (cons :tag "Several files in a directory"
58 (const :tag "None" nil)
61 (list :inline t :tag "path"
62 (const :value :path) directory)
63 (list :inline t :tag "suffix"
64 (const :value :suffix) string)
65 (list :inline t :tag "predicate"
66 (const :value :predicate) function)
67 (list :inline t :tag "prescript"
68 (const :value :prescript) string)
69 (list :inline t :tag "postscript"
70 (const :value :postscript) string)
71 (list :inline t :tag "plugged"
72 (const :value :plugged) boolean)))))
73 (cons :tag "POP3 server"
77 (const :tag "None" nil)
80 (list :inline t :tag "server"
81 (const :value :server) string)
82 (list :inline t :tag "port"
83 (const :value :port) (choice number string))
84 (list :inline t :tag "user"
85 (const :value :user) string)
86 (list :inline t :tag "password"
87 (const :value :password) string)
88 (list :inline t :tag "program"
89 (const :value :program) string)
90 (list :inline t :tag "prescript"
91 (const :value :prescript) string)
92 (list :inline t :tag "postscript"
93 (const :value :postscript) string)
94 (list :inline t :tag "function"
95 (const :value :function) function)
96 (list :inline t :tag "authentication"
97 (const :value :authentication)
98 (choice (const password)
100 (list :inline t :tag "plugged"
101 (const :value :plugged) boolean)))))
102 (cons :tag "Maildir (qmail, postfix...)"
106 (const :tag "None" nil)
109 (list :inline t :tag "path"
110 (const :value :path) directory)
111 (list :inline t :tag "plugged"
112 (const :value :plugged) boolean)))))
113 (cons :tag "IMAP server"
117 (const :tag "None" nil)
120 (list :inline t :tag "server"
121 (const :value :server) string)
122 (list :inline t :tag "port"
124 (choice number string))
125 (list :inline t :tag "user"
126 (const :value :user) string)
127 (list :inline t :tag "password"
128 (const :value :password) string)
129 (list :inline t :tag "stream"
130 (const :value :stream)
131 (choice ,@(progn (require 'imap)
134 (list 'const (car a)))
135 imap-stream-alist))))
136 (list :inline t :tag "authenticator"
137 (const :value :authenticator)
138 (choice ,@(progn (require 'imap)
141 (list 'const (car a)))
142 imap-authenticator-alist))))
143 (list :inline t :tag "mailbox"
144 (const :value :mailbox) string)
145 (list :inline t :tag "predicate"
146 (const :value :predicate) function)
147 (list :inline t :tag "fetchflag"
148 (const :value :fetchflag) string)
149 (list :inline t :tag "dontexpunge"
150 (const :value :dontexpunge) boolean)
151 (list :inline t :tag "plugged"
152 (const :value :plugged) )))))
153 (cons :tag "Webmail server"
157 (const :tag "None" nil)
160 (list :inline t :tag "subtype"
161 (const :value :subtype)
162 ;; Should be generated from
163 ;; `webmail-type-definition', but we
164 ;; can't require webmail without W3.
165 (choice (const hotmail) (const yahoo)
166 (const netaddress) (const netscape)
168 (list :inline t :tag "user"
169 (const :value :user) string)
170 (list :inline t :tag "password"
171 (const :value :password) string)
172 (list :inline t :tag "dontexpunge"
173 (const :value :dontexpunge) boolean)
174 (list :inline t :tag "plugged"
175 (const :value :plugged) boolean))))))))
177 (defcustom mail-source-primary-source nil
178 "*Primary source for incoming mail.
179 If non-nil, this maildrop will be checked periodically for new mail."
183 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
184 "File where mail will be stored while processing it."
188 (defcustom mail-source-directory "~/Mail/"
189 "Directory where files (if any) will be stored."
193 (defcustom mail-source-default-file-modes 384
194 "Set the mode bits of all new mail files to this integer."
198 (defcustom mail-source-delete-incoming nil
199 "*If non-nil, delete incoming files after handling."
203 (defcustom mail-source-incoming-file-prefix "Incoming"
204 "Prefix for file name for storing incoming mail"
208 (defcustom mail-source-report-new-mail-interval 5
209 "Interval in minutes between checks for new mail."
213 (defcustom mail-source-idle-time-delay 5
214 "Number of idle seconds to wait before checking for new mail."
218 ;;; Internal variables.
220 (defvar mail-source-string ""
221 "A dynamically bound string that says what the current mail source is.")
223 (defvar mail-source-new-mail-available nil
224 "Flag indicating when new mail is available.")
227 (defvar mail-source-common-keyword-map
229 "Mapping from keywords to default values.
230 Common keywords should be listed here.")
232 (defvar mail-source-keyword-map
237 (:path (or (getenv "MAIL")
238 (concat "/usr/spool/mail/" (user-login-name)))))
242 (:predicate identity))
247 (:server (getenv "MAILHOST"))
249 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
254 (:authentication password)
257 (:path (or (getenv "MAILDIR") "~/Maildir/"))
258 (:subdirs ("new" "cur"))
261 (:server (getenv "MAILHOST"))
265 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
268 (:predicate "UNSEEN UNDELETED")
269 (:fetchflag "\\Deleted")
273 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
276 (:authentication password)))
277 "Mapping from keywords to default values.
278 All keywords that can be used must be listed here."))
280 (defvar mail-source-fetcher-alist
281 '((file mail-source-fetch-file)
282 (directory mail-source-fetch-directory)
283 (pop mail-source-fetch-pop)
284 (maildir mail-source-fetch-maildir)
285 (imap mail-source-fetch-imap)
286 (webmail mail-source-fetch-webmail))
287 "A mapping from source type to fetcher function.")
289 (defvar mail-source-password-cache nil)
291 (defvar mail-source-plugged t)
296 (defun mail-source-strip-keyword (keyword)
297 "Strip the leading colon off the KEYWORD."
298 (intern (substring (symbol-name keyword) 1))))
301 (defun mail-source-bind-1 (type)
302 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
304 (while (setq default (pop defaults))
305 (push (list (mail-source-strip-keyword (car default))
310 (defmacro mail-source-bind (type-source &rest body)
311 "Return a `let' form that binds all variables in source TYPE.
312 TYPE-SOURCE is a list where the first element is the TYPE, and
313 the second variable is the SOURCE.
314 At run time, the mail source specifier SOURCE will be inspected,
315 and the variables will be set according to it. Variables not
316 specified will be given default values.
318 After this is done, BODY will be executed in the scope
321 The variables bound and their default values are described by
322 the `mail-source-keyword-map' variable."
323 `(let ,(mail-source-bind-1 (car type-source))
324 (mail-source-set-1 ,(cadr type-source))
327 (put 'mail-source-bind 'lisp-indent-function 1)
328 (put 'mail-source-bind 'edebug-form-spec '(form body))
330 (defun mail-source-set-1 (source)
331 (let* ((type (pop source))
332 (defaults (cdr (assq type mail-source-keyword-map)))
333 default value keyword)
334 (while (setq default (pop defaults))
335 (set (mail-source-strip-keyword (setq keyword (car default)))
336 (if (setq value (plist-get source keyword))
337 (mail-source-value value)
338 (mail-source-value (cadr default)))))))
341 (defun mail-source-bind-common-1 ()
342 (let* ((defaults mail-source-common-keyword-map)
344 (while (setq default (pop defaults))
345 (push (list (mail-source-strip-keyword (car default))
350 (defun mail-source-set-common-1 (source)
351 (let* ((type (pop source))
352 (defaults mail-source-common-keyword-map)
353 (defaults-1 (cdr (assq type mail-source-keyword-map)))
354 default value keyword)
355 (while (setq default (pop defaults))
356 (set (mail-source-strip-keyword (setq keyword (car default)))
357 (if (setq value (plist-get source keyword))
358 (mail-source-value value)
359 (if (setq value (assq keyword defaults-1))
360 (mail-source-value (cadr value))
361 (mail-source-value (cadr default))))))))
363 (defmacro mail-source-bind-common (source &rest body)
364 "Return a `let' form that binds all common variables.
365 See `mail-source-bind'."
366 `(let ,(mail-source-bind-common-1)
367 (mail-source-set-common-1 source)
370 (put 'mail-source-bind-common 'lisp-indent-function 1)
371 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
373 (defun mail-source-value (value)
374 "Return the value of VALUE."
381 (functionp (car value)))
383 ;; Just return the value.
387 (defun mail-source-fetch (source callback)
388 "Fetch mail from SOURCE and call CALLBACK zero or more times.
389 CALLBACK will be called with the name of the file where (some of)
390 the mail from SOURCE is put.
391 Return the number of files that were found."
392 (mail-source-bind-common source
393 (if (or mail-source-plugged plugged)
395 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
398 (error "%S is an invalid mail source specification" source))
399 ;; If there's anything in the crash box, we do it first.
400 (when (file-exists-p mail-source-crash-box)
401 (message "Processing mail from %s..." mail-source-crash-box)
402 (setq found (mail-source-callback
403 callback mail-source-crash-box)))
406 (funcall function source callback)
409 (format "Mail source error (%s). Continue? " err))
410 (error "Cannot get new mail."))
413 (defun mail-source-make-complex-temp-name (prefix)
414 (let ((newname (make-temp-name prefix))
416 (while (file-exists-p newname)
417 (setq newprefix (concat newprefix "x"))
418 (setq newname (make-temp-name newprefix)))
421 (defun mail-source-callback (callback info)
422 "Call CALLBACK on the mail file, and then remove the mail file.
423 Pass INFO on to CALLBACK."
424 (if (or (not (file-exists-p mail-source-crash-box))
425 (zerop (nth 7 (file-attributes mail-source-crash-box))))
427 (when (file-exists-p mail-source-crash-box)
428 (delete-file mail-source-crash-box))
431 (funcall callback mail-source-crash-box info)
432 (when (file-exists-p mail-source-crash-box)
433 ;; Delete or move the incoming mail out of the way.
434 (if mail-source-delete-incoming
435 (delete-file mail-source-crash-box)
437 (mail-source-make-complex-temp-name
439 mail-source-incoming-file-prefix
440 mail-source-directory))))
441 (unless (file-exists-p (file-name-directory incoming))
442 (make-directory (file-name-directory incoming) t))
443 (rename-file mail-source-crash-box incoming t)))))))
445 (defun mail-source-movemail (from to)
446 "Move FROM to TO using movemail."
447 (if (not (file-writable-p to))
448 (error "Can't write to crash box %s. Not moving mail" to)
449 (let ((to (file-truename (expand-file-name to)))
451 (setq to (file-truename to)
452 from (file-truename from))
453 ;; Set TO if have not already done so, and rename or copy
454 ;; the file FROM to TO if and as appropriate.
457 ;; The crash box exists already.
459 ((not (file-exists-p from))
460 ;; There is no inbox.
462 ((zerop (nth 7 (file-attributes from)))
466 ;; If getting from mail spool directory, use movemail to move
467 ;; rather than just renaming, so as to interlock with the
471 (setq errors (generate-new-buffer " *mail source loss*"))
472 (let ((default-directory "/"))
478 (expand-file-name "movemail" exec-directory)
479 nil errors nil from to)))))
480 (when (file-exists-p to)
481 (set-file-modes to mail-source-default-file-modes))
482 (if (and (not (buffer-modified-p errors))
484 ;; No output => movemail won.
487 ;; There may be a warning about older revisions. We
489 (goto-char (point-min))
490 (if (search-forward "older revision" nil t)
492 ;; Probably a real error.
493 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
494 (goto-char (point-max))
495 (skip-chars-backward " \t")
496 (delete-region (point) (point-max))
497 (goto-char (point-min))
498 (when (looking-at "movemail: ")
499 (delete-region (point-min) (match-end 0)))
501 (format "movemail: %s (%d return). Continue? "
502 (buffer-string) result))
503 (error "%s" (buffer-string)))
506 (buffer-name errors))
507 (kill-buffer errors))
508 ;; Return whether we moved successfully or not.
511 (defun mail-source-movemail-and-remove (from to)
512 "Move FROM to TO using movemail, then remove FROM if empty."
513 (or (not (mail-source-movemail from to))
514 (not (zerop (nth 7 (file-attributes from))))
517 (defvar mail-source-read-passwd nil)
518 (defun mail-source-read-passwd (prompt &rest args)
519 "Read a password using PROMPT.
520 If ARGS, PROMPT is used as an argument to `format'."
523 (apply 'format prompt args)
525 (unless mail-source-read-passwd
526 (if (or (fboundp 'read-passwd) (load "passwd" t))
527 (setq mail-source-read-passwd 'read-passwd)
528 (unless (fboundp 'ange-ftp-read-passwd)
529 (autoload 'ange-ftp-read-passwd "ange-ftp"))
530 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
531 (funcall mail-source-read-passwd prompt)))
533 (defun mail-source-fetch-with-program (program)
534 (zerop (call-process shell-file-name nil nil nil
535 shell-command-switch program)))
537 (defun mail-source-run-script (script spec &optional delay)
539 (if (and (symbolp script) (fboundp script))
541 (mail-source-call-script
542 (format-spec script spec))))
546 (defun mail-source-call-script (script)
547 (let ((background nil))
548 (when (string-match "& *$" script)
549 (setq script (substring script 0 (match-beginning 0))
551 (call-process shell-file-name nil background nil
552 shell-command-switch script)))
555 ;;; Different fetchers
558 (defun mail-source-fetch-file (source callback)
559 "Fetcher for single-file sources."
560 (mail-source-bind (file source)
561 (mail-source-run-script
562 prescript (format-spec-make ?t mail-source-crash-box)
564 (let ((mail-source-string (format "file:%s" path)))
565 (if (mail-source-movemail path mail-source-crash-box)
567 (mail-source-callback callback path)
568 (mail-source-run-script
569 postscript (format-spec-make ?t mail-source-crash-box)))
572 (defun mail-source-fetch-directory (source callback)
573 "Fetcher for directory sources."
574 (mail-source-bind (directory source)
576 (mail-source-string (format "directory:%s" path)))
577 (dolist (file (directory-files
578 path t (concat (regexp-quote suffix) "$")))
579 (when (and (file-regular-p file)
580 (funcall predicate file)
581 (mail-source-movemail file mail-source-crash-box))
582 (incf found (mail-source-callback callback file))))
585 (defun mail-source-fetch-pop (source callback)
586 "Fetcher for single-file sources."
587 (mail-source-bind (pop source)
588 (mail-source-run-script
590 (format-spec-make ?p password ?t mail-source-crash-box
591 ?s server ?P port ?u user)
593 (let ((from (format "%s:%s:%s" server user port))
594 (mail-source-string (format "pop:%s@%s" user server))
596 (when (eq authentication 'password)
599 (cdr (assoc from mail-source-password-cache))
600 (mail-source-read-passwd
601 (format "Password for %s at %s: " user server)))))
603 (setenv "MAILHOST" server))
607 (mail-source-fetch-with-program
610 (format-spec-make ?p password ?t mail-source-crash-box
611 ?s server ?P port ?u user))))
613 (funcall function mail-source-crash-box))
614 ;; The default is to use pop3.el.
616 (let ((pop3-password password)
618 (pop3-mailhost server)
620 (pop3-authentication-scheme
621 (if (eq authentication 'apop) 'apop 'pass))
622 (pop3-connection-type connection)
623 (pop3-leave-mail-on-server
625 (and (boundp 'pop3-leave-mail-on-server)
626 pop3-leave-mail-on-server))))
627 (save-excursion (pop3-movemail mail-source-crash-box))))))
630 (when (eq authentication 'password)
631 (unless (assoc from mail-source-password-cache)
632 (push (cons from password) mail-source-password-cache)))
634 (mail-source-callback callback server)
635 ;; Update display-time's mail flag, if relevant.
636 (if (equal source mail-source-primary-source)
637 (setq mail-source-new-mail-available nil))
638 (mail-source-run-script
640 (format-spec-make ?p password ?t mail-source-crash-box
641 ?s server ?P port ?u user))))
642 ;; We nix out the password in case the error
643 ;; was because of a wrong password being given.
644 (setq mail-source-password-cache
645 (delq (assoc from mail-source-password-cache)
646 mail-source-password-cache))
649 (defun mail-source-check-pop (source)
650 "Check whether there is new mail."
651 (mail-source-bind (pop source)
652 (let ((from (format "%s:%s:%s" server user port))
653 (mail-source-string (format "pop:%s@%s" user server))
655 (when (eq authentication 'password)
658 (cdr (assoc from mail-source-password-cache))
659 (mail-source-read-passwd
660 (format "Password for %s at %s: " user server))))
661 (unless (assoc from mail-source-password-cache)
662 (push (cons from password) mail-source-password-cache)))
664 (setenv "MAILHOST" server))
667 ;; No easy way to check whether mail is waiting for these.
670 ;; The default is to use pop3.el.
672 (let ((pop3-password password)
674 (pop3-mailhost server)
676 (pop3-authentication-scheme
677 (if (eq authentication 'apop) 'apop 'pass)))
678 (save-excursion (pop3-get-message-count))))))
680 ;; Inform display-time that we have new mail.
681 (setq mail-source-new-mail-available (> result 0))
682 ;; We nix out the password in case the error
683 ;; was because of a wrong password being given.
684 (setq mail-source-password-cache
685 (delq (assoc from mail-source-password-cache)
686 mail-source-password-cache)))
689 (defun mail-source-new-mail-p ()
690 "Handler for `display-time' to indicate when new mail is available."
691 ;; Only report flag setting; flag is updated on a different schedule.
692 mail-source-new-mail-available)
695 (defvar mail-source-report-new-mail nil)
696 (defvar mail-source-report-new-mail-timer nil)
697 (defvar mail-source-report-new-mail-idle-timer nil)
699 (eval-when-compile (require 'timer))
701 (defun mail-source-start-idle-timer ()
702 ;; Start our idle timer if necessary, so we delay the check until the
703 ;; user isn't typing.
704 (unless mail-source-report-new-mail-idle-timer
705 (setq mail-source-report-new-mail-idle-timer
707 mail-source-idle-time-delay
710 (setq mail-source-report-new-mail-idle-timer nil)
711 (mail-source-check-pop mail-source-primary-source))))
712 ;; Since idle timers created when Emacs is already in the idle
713 ;; state don't get activated until Emacs _next_ becomes idle, we
714 ;; need to force our timer to be considered active now. We do
715 ;; this by being naughty and poking the timer internals directly
716 ;; (element 0 of the vector is nil if the timer is active).
717 (aset mail-source-report-new-mail-idle-timer 0 nil)))
719 (defun mail-source-report-new-mail (arg)
720 "Toggle whether to report when new mail is available.
721 This only works when `display-time' is enabled."
723 (if (not mail-source-primary-source)
724 (error "Need to set `mail-source-primary-source' to check for new mail."))
725 (let ((on (if (null arg)
726 (not mail-source-report-new-mail)
727 (> (prefix-numeric-value arg) 0))))
728 (setq mail-source-report-new-mail on)
729 (and mail-source-report-new-mail-timer
730 (cancel-timer mail-source-report-new-mail-timer))
731 (and mail-source-report-new-mail-idle-timer
732 (cancel-timer mail-source-report-new-mail-idle-timer))
733 (setq mail-source-report-new-mail-timer nil)
734 (setq mail-source-report-new-mail-idle-timer nil)
738 ;; display-time-mail-function is an Emacs 21 feature.
739 (setq display-time-mail-function #'mail-source-new-mail-p)
740 ;; Set up the main timer.
741 (setq mail-source-report-new-mail-timer
742 (run-at-time t (* 60 mail-source-report-new-mail-interval)
743 #'mail-source-start-idle-timer))
744 ;; When you get new mail, clear "Mail" from the mode line.
745 (add-hook 'nnmail-post-get-new-mail-hook
746 'display-time-event-handler)
747 (message "Mail check enabled"))
748 (setq display-time-mail-function nil)
749 (remove-hook 'nnmail-post-get-new-mail-hook
750 'display-time-event-handler)
751 (message "Mail check disabled"))))
753 (defun mail-source-fetch-maildir (source callback)
754 "Fetcher for maildir sources."
755 (mail-source-bind (maildir source)
758 (unless (string-match "/$" path)
759 (setq path (concat path "/")))
760 (dolist (subdir subdirs)
761 (when (file-directory-p (concat path subdir))
762 (setq mail-source-string (format "maildir:%s%s" path subdir))
763 (dolist (file (directory-files (concat path subdir) t))
764 (when (and (not (file-directory-p file))
766 (funcall function file mail-source-crash-box)
767 (let ((coding-system-for-write
768 nnheader-text-coding-system)
769 (coding-system-for-read
770 nnheader-text-coding-system)
771 (output-coding-system
772 nnheader-text-coding-system)
774 nnheader-text-coding-system))
775 (with-temp-file mail-source-crash-box
776 (insert-file-contents file)
777 (goto-char (point-min))
778 ;;; ;; Unix mail format
779 ;;; (unless (looking-at "\n*From ")
780 ;;; (insert "From maildir "
781 ;;; (current-time-string) "\n"))
782 ;;; (while (re-search-forward "^From " nil t)
783 ;;; (replace-match ">From "))
785 (insert "\001\001\001\001\n")
786 (goto-char (point-max))
788 (delete-file file)))))
789 (incf found (mail-source-callback callback file))))))
793 (autoload 'imap-open "imap")
794 (autoload 'imap-authenticate "imap")
795 (autoload 'imap-mailbox-select "imap")
796 (autoload 'imap-mailbox-unselect "imap")
797 (autoload 'imap-mailbox-close "imap")
798 (autoload 'imap-search "imap")
799 (autoload 'imap-fetch "imap")
800 (autoload 'imap-close "imap")
801 (autoload 'imap-error-text "imap")
802 (autoload 'imap-message-flags-add "imap")
803 (autoload 'imap-list-to-message-set "imap"))
805 (defun mail-source-fetch-imap (source callback)
806 "Fetcher for imap sources."
807 (mail-source-bind (imap source)
808 (let ((from (format "%s:%s:%s" server user port))
810 (buf (get-buffer-create
811 (format " *imap source %s:%s:%s *" server user mailbox)))
812 (mail-source-string (format "imap:%s:%s" server mailbox))
814 (if (and (imap-open server port stream authentication buf)
816 user (or (cdr (assoc from mail-source-password-cache))
818 (imap-mailbox-select mailbox nil buf))
820 (coding-system-for-write 'binary)
821 (output-coding-system 'binary))
822 (with-temp-file mail-source-crash-box
823 ;; In some versions of FSF Emacs, inserting unibyte
824 ;; string into multibyte buffer may convert 8-bit chars
825 ;; into latin-iso8859-1 chars, which results \201's.
826 (set-buffer-multibyte nil)
828 (with-current-buffer buf
829 (when (or imap-password
830 (assoc from mail-source-password-cache))
831 (push (cons from imap-password) mail-source-password-cache)))
832 ;; if predicate is nil, use all uids
833 (dolist (uid (imap-search (or predicate "1:*") buf))
834 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
836 (insert "From imap " (current-time-string) "\n")
839 (while (re-search-forward "^From " nil t)
840 (replace-match ">From "))
841 (goto-char (point-max))))
842 (nnheader-ms-strip-cr))
843 (incf found (mail-source-callback callback server))
844 (when (and remove fetchflag)
845 (imap-message-flags-add
846 (imap-list-to-message-set remove) fetchflag nil buf))
848 (imap-mailbox-unselect buf)
849 (imap-mailbox-close buf))
852 ;; We nix out the password in case the error
853 ;; was because of a wrong password being given.
854 (setq mail-source-password-cache
855 (delq (assoc from mail-source-password-cache)
856 mail-source-password-cache))
857 (error (imap-error-text buf)))
862 (autoload 'webmail-fetch "webmail"))
864 (defun mail-source-fetch-webmail (source callback)
865 "Fetch for webmail source."
866 (mail-source-bind (webmail source)
867 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
868 (webmail-newmail-only dontexpunge)
869 (webmail-move-to-trash-can (not dontexpunge)))
870 (when (eq authentication 'password)
873 (cdr (assoc (format "webmail:%s:%s" subtype user)
874 mail-source-password-cache))
875 (mail-source-read-passwd
876 (format "Password for %s at %s: " user subtype))))
878 (not (assoc (format "webmail:%s:%s" subtype user)
879 mail-source-password-cache)))
880 (push (cons (format "webmail:%s:%s" subtype user) password)
881 mail-source-password-cache)))
882 (webmail-fetch mail-source-crash-box subtype user password)
883 (mail-source-callback callback (symbol-name subtype)))))
885 (provide 'mail-source)
887 ;;; mail-source.el ends here