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 (require 'format-spec)
39 (defgroup mail-source nil
40 "The mail-fetching library."
43 ;; Define these at compile time to avoid dragging in imap always.
44 (defconst mail-source-imap-authenticators
47 (list 'const (car a)))
48 imap-authenticator-alist)))
49 (defconst mail-source-imap-streams
52 (list 'const (car a)))
55 (defcustom mail-sources nil
56 "*Where the mail backends will look for incoming mail.
57 This variable is a list of mail source specifiers.
58 See Info node `(gnus)Mail Source Specifiers'."
61 (choice :format "%[Value Menu%] %v"
63 (cons :tag "Spool file"
64 (const :format "" file)
65 (checklist :tag "Options" :greedy t
67 (const :format "" :value :path)
69 (cons :tag "Several files in a directory"
70 (const :format "" directory)
71 (checklist :tag "Options" :greedy t
73 (const :format "" :value :path)
74 (directory :tag "Path"))
76 (const :format "" :value :suffix)
77 (string :tag "Suffix"))
79 (const :format "" :value :predicate)
80 (function :tag "Predicate"))
82 (const :format "" :value :prescript)
83 (string :tag "Prescript"))
85 (const :format "" :value :postscript)
86 (string :tag "Postscript"))
88 (const :format "" :value :plugged)
89 (boolean :tag "Plugged"))))
90 (cons :tag "POP3 server"
91 (const :format "" pop)
92 (checklist :tag "Options" :greedy t
94 (const :format "" :value :server)
95 (string :tag "Server"))
97 (const :format "" :value :port)
100 (number :format "%v")
101 (string :format "%v")))
103 (const :format "" :value :user)
104 (string :tag "User"))
106 (const :format "" :value :password)
107 (string :tag "Password"))
109 (const :format "" :value :program)
110 (string :tag "Program"))
112 (const :format "" :value :prescript)
113 (string :tag "Prescript"))
115 (const :format "" :value :postscript)
116 (string :tag "Postscript"))
118 (const :format "" :value :function)
119 (function :tag "Function"))
122 :value :authentication)
123 (choice :tag "Authentication"
128 (const :format "" :value :plugged)
129 (boolean :tag "Plugged"))))
130 (cons :tag "Maildir (qmail, postfix...)"
131 (const :format "" maildir)
132 (checklist :tag "Options" :greedy t
134 (const :format "" :value :path)
135 (directory :tag "Path"))
137 (const :format "" :value :plugged)
138 (boolean :tag "Plugged"))))
139 (cons :tag "IMAP server"
140 (const :format "" imap)
141 (checklist :tag "Options" :greedy t
143 (const :format "" :value :server)
144 (string :tag "Server"))
146 (const :format "" :value :port)
151 (const :format "" :value :user)
152 (string :tag "User"))
154 (const :format "" :value :password)
155 (string :tag "Password"))
157 (const :format "" :value :stream)
158 (choice :tag "Stream"
160 ,@mail-source-imap-streams))
163 :value :authenticator)
164 (choice :tag "Authenticator"
166 ,@mail-source-imap-authenticators))
168 (const :format "" :value :mailbox)
169 (string :tag "Mailbox"
172 (const :format "" :value :predicate)
173 (string :tag "Predicate"
174 :value "UNSEEN UNDELETED"))
176 (const :format "" :value :fetchflag)
177 (string :tag "Fetchflag"
182 (boolean :tag "Dontexpunge"))
184 (const :format "" :value :plugged)
185 (boolean :tag "Plugged"))))
186 (cons :tag "Webmail server"
187 (const :format "" webmail)
188 (checklist :tag "Options" :greedy t
190 (const :format "" :value :subtype)
191 ;; Should be generated from
192 ;; `webmail-type-definition', but we
193 ;; can't require webmail without W3.
194 (choice :tag "Subtype"
202 (const :format "" :value :user)
203 (string :tag "User"))
205 (const :format "" :value :password)
206 (string :tag "Password"))
210 (boolean :tag "Dontexpunge"))
212 (const :format "" :value :plugged)
213 (boolean :tag "Plugged")))))))
215 (defcustom mail-source-primary-source nil
216 "*Primary source for incoming mail.
217 If non-nil, this maildrop will be checked periodically for new mail."
221 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
222 "File where mail will be stored while processing it."
226 (defcustom mail-source-directory "~/Mail/"
227 "Directory where files (if any) will be stored."
231 (defcustom mail-source-default-file-modes 384
232 "Set the mode bits of all new mail files to this integer."
236 (defcustom mail-source-delete-incoming nil
237 "*If non-nil, delete incoming files after handling."
241 (defcustom mail-source-incoming-file-prefix "Incoming"
242 "Prefix for file name for storing incoming mail"
246 (defcustom mail-source-report-new-mail-interval 5
247 "Interval in minutes between checks for new mail."
251 (defcustom mail-source-idle-time-delay 5
252 "Number of idle seconds to wait before checking for new mail."
256 ;;; Internal variables.
258 (defvar mail-source-string ""
259 "A dynamically bound string that says what the current mail source is.")
261 (defvar mail-source-new-mail-available nil
262 "Flag indicating when new mail is available.")
265 (defvar mail-source-common-keyword-map
267 "Mapping from keywords to default values.
268 Common keywords should be listed here.")
270 (defvar mail-source-keyword-map
275 (:path (or (getenv "MAIL")
276 (expand-file-name (user-login-name) rmail-spool-directory))))
280 (:predicate identity))
285 (:server (getenv "MAILHOST"))
287 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
291 (:authentication password))
293 (:path (or (getenv "MAILDIR") "~/Maildir/"))
294 (:subdirs ("new" "cur"))
297 (:server (getenv "MAILHOST"))
301 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
304 (:predicate "UNSEEN UNDELETED")
305 (:fetchflag "\\Deleted")
309 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
312 (:authentication password)))
313 "Mapping from keywords to default values.
314 All keywords that can be used must be listed here."))
316 (defvar mail-source-fetcher-alist
317 '((file mail-source-fetch-file)
318 (directory mail-source-fetch-directory)
319 (pop mail-source-fetch-pop)
320 (maildir mail-source-fetch-maildir)
321 (imap mail-source-fetch-imap)
322 (webmail mail-source-fetch-webmail))
323 "A mapping from source type to fetcher function.")
325 (defvar mail-source-password-cache nil)
327 (defvar mail-source-plugged t)
332 (defun mail-source-strip-keyword (keyword)
333 "Strip the leading colon off the KEYWORD."
334 (intern (substring (symbol-name keyword) 1))))
337 (defun mail-source-bind-1 (type)
338 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
340 (while (setq default (pop defaults))
341 (push (list (mail-source-strip-keyword (car default))
346 (defmacro mail-source-bind (type-source &rest body)
347 "Return a `let' form that binds all variables in source TYPE.
348 TYPE-SOURCE is a list where the first element is the TYPE, and
349 the second variable is the SOURCE.
350 At run time, the mail source specifier SOURCE will be inspected,
351 and the variables will be set according to it. Variables not
352 specified will be given default values.
354 After this is done, BODY will be executed in the scope
357 The variables bound and their default values are described by
358 the `mail-source-keyword-map' variable."
359 `(let ,(mail-source-bind-1 (car type-source))
360 (mail-source-set-1 ,(cadr type-source))
363 (put 'mail-source-bind 'lisp-indent-function 1)
364 (put 'mail-source-bind 'edebug-form-spec '(form body))
366 (defun mail-source-set-1 (source)
367 (let* ((type (pop source))
368 (defaults (cdr (assq type mail-source-keyword-map)))
369 default value keyword)
370 (while (setq default (pop defaults))
371 (set (mail-source-strip-keyword (setq keyword (car default)))
372 (if (setq value (plist-get source keyword))
373 (mail-source-value value)
374 (mail-source-value (cadr default)))))))
377 (defun mail-source-bind-common-1 ()
378 (let* ((defaults mail-source-common-keyword-map)
380 (while (setq default (pop defaults))
381 (push (list (mail-source-strip-keyword (car default))
386 (defun mail-source-set-common-1 (source)
387 (let* ((type (pop source))
388 (defaults mail-source-common-keyword-map)
389 (defaults-1 (cdr (assq type mail-source-keyword-map)))
390 default value keyword)
391 (while (setq default (pop defaults))
392 (set (mail-source-strip-keyword (setq keyword (car default)))
393 (if (setq value (plist-get source keyword))
394 (mail-source-value value)
395 (if (setq value (assq keyword defaults-1))
396 (mail-source-value (cadr value))
397 (mail-source-value (cadr default))))))))
399 (defmacro mail-source-bind-common (source &rest body)
400 "Return a `let' form that binds all common variables.
401 See `mail-source-bind'."
402 `(let ,(mail-source-bind-common-1)
403 (mail-source-set-common-1 source)
406 (put 'mail-source-bind-common 'lisp-indent-function 1)
407 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
409 (defun mail-source-value (value)
410 "Return the value of VALUE."
417 (functionp (car value)))
419 ;; Just return the value.
423 (defun mail-source-fetch (source callback)
424 "Fetch mail from SOURCE and call CALLBACK zero or more times.
425 CALLBACK will be called with the name of the file where (some of)
426 the mail from SOURCE is put.
427 Return the number of files that were found."
428 (mail-source-bind-common source
429 (if (or mail-source-plugged plugged)
431 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
434 (error "%S is an invalid mail source specification" source))
435 ;; If there's anything in the crash box, we do it first.
436 (when (file-exists-p mail-source-crash-box)
437 (message "Processing mail from %s..." mail-source-crash-box)
438 (setq found (mail-source-callback
439 callback mail-source-crash-box)))
442 (funcall function source callback)
445 (format "Mail source error (%s). Continue? " err))
446 (error "Cannot get new mail."))
449 (defun mail-source-make-complex-temp-name (prefix)
450 (let ((newname (make-temp-name prefix))
452 (while (file-exists-p newname)
453 (setq newprefix (concat newprefix "x"))
454 (setq newname (make-temp-name newprefix)))
457 (defun mail-source-callback (callback info)
458 "Call CALLBACK on the mail file, and then remove the mail file.
459 Pass INFO on to CALLBACK."
460 (if (or (not (file-exists-p mail-source-crash-box))
461 (zerop (nth 7 (file-attributes mail-source-crash-box))))
463 (when (file-exists-p mail-source-crash-box)
464 (delete-file mail-source-crash-box))
467 (funcall callback mail-source-crash-box info)
468 (when (file-exists-p mail-source-crash-box)
469 ;; Delete or move the incoming mail out of the way.
470 (if mail-source-delete-incoming
471 (delete-file mail-source-crash-box)
473 (mail-source-make-complex-temp-name
475 mail-source-incoming-file-prefix
476 mail-source-directory))))
477 (unless (file-exists-p (file-name-directory incoming))
478 (make-directory (file-name-directory incoming) t))
479 (rename-file mail-source-crash-box incoming t)))))))
481 (defun mail-source-movemail (from to)
482 "Move FROM to TO using movemail."
483 (if (not (file-writable-p to))
484 (error "Can't write to crash box %s. Not moving mail" to)
485 (let ((to (file-truename (expand-file-name to)))
487 (setq to (file-truename to)
488 from (file-truename from))
489 ;; Set TO if have not already done so, and rename or copy
490 ;; the file FROM to TO if and as appropriate.
493 ;; The crash box exists already.
495 ((not (file-exists-p from))
496 ;; There is no inbox.
498 ((zerop (nth 7 (file-attributes from)))
502 ;; If getting from mail spool directory, use movemail to move
503 ;; rather than just renaming, so as to interlock with the
507 (setq errors (generate-new-buffer " *mail source loss*"))
508 (let ((default-directory "/"))
514 (expand-file-name "movemail" exec-directory)
515 nil errors nil from to)))))
516 (when (file-exists-p to)
517 (set-file-modes to mail-source-default-file-modes))
518 (if (and (not (buffer-modified-p errors))
520 ;; No output => movemail won.
523 ;; There may be a warning about older revisions. We
525 (goto-char (point-min))
526 (if (search-forward "older revision" nil t)
528 ;; Probably a real error.
529 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
530 (goto-char (point-max))
531 (skip-chars-backward " \t")
532 (delete-region (point) (point-max))
533 (goto-char (point-min))
534 (when (looking-at "movemail: ")
535 (delete-region (point-min) (match-end 0)))
537 (format "movemail: %s (%d return). Continue? "
538 (buffer-string) result))
539 (error "%s" (buffer-string)))
542 (buffer-name errors))
543 (kill-buffer errors))
544 ;; Return whether we moved successfully or not.
547 (defun mail-source-movemail-and-remove (from to)
548 "Move FROM to TO using movemail, then remove FROM if empty."
549 (or (not (mail-source-movemail from to))
550 (not (zerop (nth 7 (file-attributes from))))
553 (defvar mail-source-read-passwd nil)
554 (defun mail-source-read-passwd (prompt &rest args)
555 "Read a password using PROMPT.
556 If ARGS, PROMPT is used as an argument to `format'."
559 (apply 'format prompt args)
561 (unless mail-source-read-passwd
562 (if (or (fboundp 'read-passwd) (load "passwd" t))
563 (setq mail-source-read-passwd 'read-passwd)
564 (unless (fboundp 'ange-ftp-read-passwd)
565 (autoload 'ange-ftp-read-passwd "ange-ftp"))
566 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
567 (funcall mail-source-read-passwd prompt)))
569 (defun mail-source-fetch-with-program (program)
570 (zerop (call-process shell-file-name nil nil nil
571 shell-command-switch program)))
573 (defun mail-source-run-script (script spec &optional delay)
575 (if (and (symbolp script) (fboundp script))
577 (mail-source-call-script
578 (format-spec script spec))))
582 (defun mail-source-call-script (script)
583 (let ((background nil))
584 (when (string-match "& *$" script)
585 (setq script (substring script 0 (match-beginning 0))
587 (call-process shell-file-name nil background nil
588 shell-command-switch script)))
591 ;;; Different fetchers
594 (defun mail-source-fetch-file (source callback)
595 "Fetcher for single-file sources."
596 (mail-source-bind (file source)
597 (mail-source-run-script
598 prescript (format-spec-make ?t mail-source-crash-box)
600 (let ((mail-source-string (format "file:%s" path)))
601 (if (mail-source-movemail path mail-source-crash-box)
603 (mail-source-callback callback path)
604 (mail-source-run-script
605 postscript (format-spec-make ?t mail-source-crash-box)))
608 (defun mail-source-fetch-directory (source callback)
609 "Fetcher for directory sources."
610 (mail-source-bind (directory source)
612 (mail-source-string (format "directory:%s" path)))
613 (dolist (file (directory-files
614 path t (concat (regexp-quote suffix) "$")))
615 (when (and (file-regular-p file)
616 (funcall predicate file)
617 (mail-source-movemail file mail-source-crash-box))
618 (incf found (mail-source-callback callback file))))
621 (defun mail-source-fetch-pop (source callback)
622 "Fetcher for single-file sources."
623 (mail-source-bind (pop source)
624 (mail-source-run-script
626 (format-spec-make ?p password ?t mail-source-crash-box
627 ?s server ?P port ?u user)
629 (let ((from (format "%s:%s:%s" server user port))
630 (mail-source-string (format "pop:%s@%s" user server))
632 (when (eq authentication 'password)
635 (cdr (assoc from mail-source-password-cache))
636 (mail-source-read-passwd
637 (format "Password for %s at %s: " user server)))))
639 (setenv "MAILHOST" server))
643 (mail-source-fetch-with-program
646 (format-spec-make ?p password ?t mail-source-crash-box
647 ?s server ?P port ?u user))))
649 (funcall function mail-source-crash-box))
650 ;; The default is to use pop3.el.
652 (let ((pop3-password password)
654 (pop3-mailhost server)
656 (pop3-authentication-scheme
657 (if (eq authentication 'apop) 'apop 'pass)))
658 (save-excursion (pop3-movemail mail-source-crash-box))))))
661 (when (eq authentication 'password)
662 (unless (assoc from mail-source-password-cache)
663 (push (cons from password) mail-source-password-cache)))
665 (mail-source-callback callback server)
666 ;; Update display-time's mail flag, if relevant.
667 (if (equal source mail-source-primary-source)
668 (setq mail-source-new-mail-available nil))
669 (mail-source-run-script
671 (format-spec-make ?p password ?t mail-source-crash-box
672 ?s server ?P port ?u user))))
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-check-pop (source)
681 "Check whether there is new mail."
682 (mail-source-bind (pop source)
683 (let ((from (format "%s:%s:%s" server user port))
684 (mail-source-string (format "pop:%s@%s" user server))
686 (when (eq authentication 'password)
689 (cdr (assoc from mail-source-password-cache))
690 (mail-source-read-passwd
691 (format "Password for %s at %s: " user server))))
692 (unless (assoc from mail-source-password-cache)
693 (push (cons from password) mail-source-password-cache)))
695 (setenv "MAILHOST" server))
698 ;; No easy way to check whether mail is waiting for these.
701 ;; The default is to use pop3.el.
703 (let ((pop3-password password)
705 (pop3-mailhost server)
707 (pop3-authentication-scheme
708 (if (eq authentication 'apop) 'apop 'pass)))
709 (save-excursion (pop3-get-message-count))))))
711 ;; Inform display-time that we have new mail.
712 (setq mail-source-new-mail-available (> result 0))
713 ;; We nix out the password in case the error
714 ;; was because of a wrong password being given.
715 (setq mail-source-password-cache
716 (delq (assoc from mail-source-password-cache)
717 mail-source-password-cache)))
720 (defun mail-source-new-mail-p ()
721 "Handler for `display-time' to indicate when new mail is available."
722 ;; Only report flag setting; flag is updated on a different schedule.
723 mail-source-new-mail-available)
726 (defvar mail-source-report-new-mail nil)
727 (defvar mail-source-report-new-mail-timer nil)
728 (defvar mail-source-report-new-mail-idle-timer nil)
731 (if (featurep 'xemacs)
735 (defun mail-source-start-idle-timer ()
736 ;; Start our idle timer if necessary, so we delay the check until the
737 ;; user isn't typing.
738 (unless mail-source-report-new-mail-idle-timer
739 (setq mail-source-report-new-mail-idle-timer
741 mail-source-idle-time-delay
744 (setq mail-source-report-new-mail-idle-timer nil)
745 (mail-source-check-pop mail-source-primary-source))))
746 ;; Since idle timers created when Emacs is already in the idle
747 ;; state don't get activated until Emacs _next_ becomes idle, we
748 ;; need to force our timer to be considered active now. We do
749 ;; this by being naughty and poking the timer internals directly
750 ;; (element 0 of the vector is nil if the timer is active).
751 (aset mail-source-report-new-mail-idle-timer 0 nil)))
753 (defun mail-source-report-new-mail (arg)
754 "Toggle whether to report when new mail is available.
755 This only works when `display-time' is enabled."
757 (if (not mail-source-primary-source)
758 (error "Need to set `mail-source-primary-source' to check for new mail."))
759 (let ((on (if (null arg)
760 (not mail-source-report-new-mail)
761 (> (prefix-numeric-value arg) 0))))
762 (setq mail-source-report-new-mail on)
763 (and mail-source-report-new-mail-timer
764 (nnheader-cancel-timer mail-source-report-new-mail-timer))
765 (and mail-source-report-new-mail-idle-timer
766 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
767 (setq mail-source-report-new-mail-timer nil)
768 (setq mail-source-report-new-mail-idle-timer nil)
772 ;; display-time-mail-function is an Emacs 21 feature.
773 (setq display-time-mail-function #'mail-source-new-mail-p)
774 ;; Set up the main timer.
775 (setq mail-source-report-new-mail-timer
776 (run-at-time t (* 60 mail-source-report-new-mail-interval)
777 #'mail-source-start-idle-timer))
778 ;; When you get new mail, clear "Mail" from the mode line.
779 (add-hook 'nnmail-post-get-new-mail-hook
780 'display-time-event-handler)
781 (message "Mail check enabled"))
782 (setq display-time-mail-function nil)
783 (remove-hook 'nnmail-post-get-new-mail-hook
784 'display-time-event-handler)
785 (message "Mail check disabled"))))
787 (defun mail-source-fetch-maildir (source callback)
788 "Fetcher for maildir sources."
789 (mail-source-bind (maildir source)
792 (unless (string-match "/$" path)
793 (setq path (concat path "/")))
794 (dolist (subdir subdirs)
795 (when (file-directory-p (concat path subdir))
796 (setq mail-source-string (format "maildir:%s%s" path subdir))
797 (dolist (file (directory-files (concat path subdir) t))
798 (when (and (not (file-directory-p file))
800 (funcall function file mail-source-crash-box)
801 (let ((coding-system-for-write
802 mm-text-coding-system)
803 (coding-system-for-read
804 mm-text-coding-system))
805 (with-temp-file mail-source-crash-box
806 (insert-file-contents file)
807 (goto-char (point-min))
808 ;;; ;; Unix mail format
809 ;;; (unless (looking-at "\n*From ")
810 ;;; (insert "From maildir "
811 ;;; (current-time-string) "\n"))
812 ;;; (while (re-search-forward "^From " nil t)
813 ;;; (replace-match ">From "))
814 ;;; (goto-char (point-max))
817 (insert "\001\001\001\001\n"))
818 (delete-file file)))))
819 (incf found (mail-source-callback callback file))))))
823 (autoload 'imap-open "imap")
824 (autoload 'imap-authenticate "imap")
825 (autoload 'imap-mailbox-select "imap")
826 (autoload 'imap-mailbox-unselect "imap")
827 (autoload 'imap-mailbox-close "imap")
828 (autoload 'imap-search "imap")
829 (autoload 'imap-fetch "imap")
830 (autoload 'imap-close "imap")
831 (autoload 'imap-error-text "imap")
832 (autoload 'imap-message-flags-add "imap")
833 (autoload 'imap-list-to-message-set "imap")
834 (autoload 'imap-range-to-message-set "imap")
835 (autoload 'nnheader-ms-strip-cr "nnheader"))
837 (defvar mail-source-imap-file-coding-system 'binary
838 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
840 (defun mail-source-fetch-imap (source callback)
841 "Fetcher for imap sources."
842 (mail-source-bind (imap source)
843 (let ((from (format "%s:%s:%s" server user port))
845 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
846 (mail-source-string (format "imap:%s:%s" server mailbox))
848 (if (and (imap-open server port stream authentication buf)
850 user (or (cdr (assoc from mail-source-password-cache))
852 (imap-mailbox-select mailbox nil buf))
853 (let ((coding-system-for-write mail-source-imap-file-coding-system)
855 (with-temp-file mail-source-crash-box
856 ;; Avoid converting 8-bit chars from inserted strings to
858 (mm-disable-multibyte)
860 (with-current-buffer buf
861 (when (or imap-password
862 (assoc from mail-source-password-cache))
863 (push (cons from imap-password) mail-source-password-cache)))
864 ;; if predicate is nil, use all uids
865 (dolist (uid (imap-search (or predicate "1:*") buf))
866 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
868 (insert "From imap " (current-time-string) "\n")
871 (while (re-search-forward "^From " nil t)
872 (replace-match ">From "))
873 (goto-char (point-max))))
874 (nnheader-ms-strip-cr))
875 (incf found (mail-source-callback callback server))
876 (when (and remove fetchflag)
877 (imap-message-flags-add
878 (imap-range-to-message-set (gnus-compress-sequence remove))
881 (imap-mailbox-unselect buf)
882 (imap-mailbox-close buf))
885 ;; We nix out the password in case the error
886 ;; was because of a wrong password being given.
887 (setq mail-source-password-cache
888 (delq (assoc from mail-source-password-cache)
889 mail-source-password-cache))
890 (error (imap-error-text buf)))
895 (autoload 'webmail-fetch "webmail"))
897 (defun mail-source-fetch-webmail (source callback)
898 "Fetch for webmail source."
899 (mail-source-bind (webmail source)
900 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
901 (webmail-newmail-only dontexpunge)
902 (webmail-move-to-trash-can (not dontexpunge)))
903 (when (eq authentication 'password)
906 (cdr (assoc (format "webmail:%s:%s" subtype user)
907 mail-source-password-cache))
908 (mail-source-read-passwd
909 (format "Password for %s at %s: " user subtype))))
911 (not (assoc (format "webmail:%s:%s" subtype user)
912 mail-source-password-cache)))
913 (push (cons (format "webmail:%s:%s" subtype user) password)
914 mail-source-password-cache)))
915 (webmail-fetch mail-source-crash-box subtype user password)
916 (mail-source-callback callback (symbol-name subtype)))))
918 (provide 'mail-source)
920 ;;; mail-source.el ends here