1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (defvar display-time-mail-function)))
33 (autoload 'pop3-movemail "pop3")
34 (autoload 'pop3-get-message-count "pop3")
35 (autoload 'nnheader-cancel-timer "nnheader")
36 (autoload 'nnheader-run-at-time "nnheader"))
37 (require 'format-spec)
39 (require 'message) ;; for `message-directory'
41 (defgroup mail-source nil
42 "The mail-fetching library."
46 ;; Define these at compile time to avoid dragging in imap always.
47 (defconst mail-source-imap-authenticators
50 (list 'const (car a)))
51 imap-authenticator-alist)))
52 (defconst mail-source-imap-streams
55 (list 'const (car a)))
58 (defcustom mail-sources nil
59 "*Where the mail backends will look for incoming mail.
60 This variable is a list of mail source specifiers.
61 See Info node `(gnus)Mail Source Specifiers'."
64 (choice :format "%[Value Menu%] %v"
66 (cons :tag "Spool file"
67 (const :format "" file)
68 (checklist :tag "Options" :greedy t
70 (const :format "" :value :path)
72 (cons :tag "Several files in a directory"
73 (const :format "" directory)
74 (checklist :tag "Options" :greedy t
76 (const :format "" :value :path)
77 (directory :tag "Path"))
79 (const :format "" :value :suffix)
80 (string :tag "Suffix"))
82 (const :format "" :value :predicate)
83 (function :tag "Predicate"))
85 (const :format "" :value :prescript)
86 (string :tag "Prescript"))
88 (const :format "" :value :postscript)
89 (string :tag "Postscript"))
91 (const :format "" :value :plugged)
92 (boolean :tag "Plugged"))))
93 (cons :tag "POP3 server"
94 (const :format "" pop)
95 (checklist :tag "Options" :greedy t
97 (const :format "" :value :server)
98 (string :tag "Server"))
100 (const :format "" :value :port)
103 (number :format "%v")
104 (string :format "%v")))
106 (const :format "" :value :user)
107 (string :tag "User"))
109 (const :format "" :value :password)
110 (string :tag "Password"))
112 (const :format "" :value :program)
113 (string :tag "Program"))
115 (const :format "" :value :prescript)
116 (string :tag "Prescript"))
118 (const :format "" :value :postscript)
119 (string :tag "Postscript"))
121 (const :format "" :value :function)
122 (function :tag "Function"))
125 :value :authentication)
126 (choice :tag "Authentication"
131 (const :format "" :value :plugged)
132 (boolean :tag "Plugged"))))
133 (cons :tag "Maildir (qmail, postfix...)"
134 (const :format "" maildir)
135 (checklist :tag "Options" :greedy t
137 (const :format "" :value :path)
138 (directory :tag "Path"))
140 (const :format "" :value :plugged)
141 (boolean :tag "Plugged"))))
142 (cons :tag "IMAP server"
143 (const :format "" imap)
144 (checklist :tag "Options" :greedy t
146 (const :format "" :value :server)
147 (string :tag "Server"))
149 (const :format "" :value :port)
154 (const :format "" :value :user)
155 (string :tag "User"))
157 (const :format "" :value :password)
158 (string :tag "Password"))
160 (const :format "" :value :stream)
161 (choice :tag "Stream"
163 ,@mail-source-imap-streams))
165 (const :format "" :value :program)
166 (string :tag "Program"))
169 :value :authenticator)
170 (choice :tag "Authenticator"
172 ,@mail-source-imap-authenticators))
174 (const :format "" :value :mailbox)
175 (string :tag "Mailbox"
178 (const :format "" :value :predicate)
179 (string :tag "Predicate"
180 :value "UNSEEN UNDELETED"))
182 (const :format "" :value :fetchflag)
183 (string :tag "Fetchflag"
188 (boolean :tag "Dontexpunge"))
190 (const :format "" :value :plugged)
191 (boolean :tag "Plugged"))))
192 (cons :tag "Webmail server"
193 (const :format "" webmail)
194 (checklist :tag "Options" :greedy t
196 (const :format "" :value :subtype)
197 ;; Should be generated from
198 ;; `webmail-type-definition', but we
199 ;; can't require webmail without W3.
200 (choice :tag "Subtype"
208 (const :format "" :value :user)
209 (string :tag "User"))
211 (const :format "" :value :password)
212 (string :tag "Password"))
216 (boolean :tag "Dontexpunge"))
218 (const :format "" :value :plugged)
219 (boolean :tag "Plugged")))))))
221 (defcustom mail-source-primary-source nil
222 "*Primary source for incoming mail.
223 If non-nil, this maildrop will be checked periodically for new mail."
227 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
228 "File where mail will be stored while processing it."
232 (defcustom mail-source-directory message-directory
233 "Directory where files (if any) will be stored."
237 (defcustom mail-source-default-file-modes 384
238 "Set the mode bits of all new mail files to this integer."
242 (defcustom mail-source-delete-incoming nil
243 "*If non-nil, delete incoming files after handling."
247 (defcustom mail-source-incoming-file-prefix "Incoming"
248 "Prefix for file name for storing incoming mail"
252 (defcustom mail-source-report-new-mail-interval 5
253 "Interval in minutes between checks for new mail."
257 (defcustom mail-source-idle-time-delay 5
258 "Number of idle seconds to wait before checking for new mail."
262 ;;; Internal variables.
264 (defvar mail-source-string ""
265 "A dynamically bound string that says what the current mail source is.")
267 (defvar mail-source-new-mail-available nil
268 "Flag indicating when new mail is available.")
271 (defvar mail-source-common-keyword-map
273 "Mapping from keywords to default values.
274 Common keywords should be listed here.")
276 (defvar mail-source-keyword-map
281 (:path (or (getenv "MAIL")
282 (expand-file-name (user-login-name) rmail-spool-directory))))
286 (:predicate identity))
291 (:server (getenv "MAILHOST"))
293 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
297 (:authentication password))
299 (:path (or (getenv "MAILDIR") "~/Maildir/"))
300 (:subdirs ("new" "cur"))
303 (:server (getenv "MAILHOST"))
308 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
311 (:predicate "UNSEEN UNDELETED")
312 (:fetchflag "\\Deleted")
316 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
319 (:authentication password)))
320 "Mapping from keywords to default values.
321 All keywords that can be used must be listed here."))
323 (defvar mail-source-fetcher-alist
324 '((file mail-source-fetch-file)
325 (directory mail-source-fetch-directory)
326 (pop mail-source-fetch-pop)
327 (maildir mail-source-fetch-maildir)
328 (imap mail-source-fetch-imap)
329 (webmail mail-source-fetch-webmail))
330 "A mapping from source type to fetcher function.")
332 (defvar mail-source-password-cache nil)
334 (defvar mail-source-plugged t)
339 (defun mail-source-strip-keyword (keyword)
340 "Strip the leading colon off the KEYWORD."
341 (intern (substring (symbol-name keyword) 1))))
344 (defun mail-source-bind-1 (type)
345 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
347 (while (setq default (pop defaults))
348 (push (list (mail-source-strip-keyword (car default))
353 (defmacro mail-source-bind (type-source &rest body)
354 "Return a `let' form that binds all variables in source TYPE.
355 TYPE-SOURCE is a list where the first element is the TYPE, and
356 the second variable is the SOURCE.
357 At run time, the mail source specifier SOURCE will be inspected,
358 and the variables will be set according to it. Variables not
359 specified will be given default values.
361 After this is done, BODY will be executed in the scope
364 The variables bound and their default values are described by
365 the `mail-source-keyword-map' variable."
366 `(let ,(mail-source-bind-1 (car type-source))
367 (mail-source-set-1 ,(cadr type-source))
370 (put 'mail-source-bind 'lisp-indent-function 1)
371 (put 'mail-source-bind 'edebug-form-spec '(form body))
373 (defun mail-source-set-1 (source)
374 (let* ((type (pop source))
375 (defaults (cdr (assq type mail-source-keyword-map)))
376 default value keyword)
377 (while (setq default (pop defaults))
378 (set (mail-source-strip-keyword (setq keyword (car default)))
379 (if (setq value (plist-get source keyword))
380 (mail-source-value value)
381 (mail-source-value (cadr default)))))))
384 (defun mail-source-bind-common-1 ()
385 (let* ((defaults mail-source-common-keyword-map)
387 (while (setq default (pop defaults))
388 (push (list (mail-source-strip-keyword (car default))
393 (defun mail-source-set-common-1 (source)
394 (let* ((type (pop source))
395 (defaults mail-source-common-keyword-map)
396 (defaults-1 (cdr (assq type mail-source-keyword-map)))
397 default value keyword)
398 (while (setq default (pop defaults))
399 (set (mail-source-strip-keyword (setq keyword (car default)))
400 (if (setq value (plist-get source keyword))
401 (mail-source-value value)
402 (if (setq value (assq keyword defaults-1))
403 (mail-source-value (cadr value))
404 (mail-source-value (cadr default))))))))
406 (defmacro mail-source-bind-common (source &rest body)
407 "Return a `let' form that binds all common variables.
408 See `mail-source-bind'."
409 `(let ,(mail-source-bind-common-1)
410 (mail-source-set-common-1 source)
413 (put 'mail-source-bind-common 'lisp-indent-function 1)
414 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
416 (defun mail-source-value (value)
417 "Return the value of VALUE."
424 (functionp (car value)))
426 ;; Just return the value.
430 (defun mail-source-fetch (source callback)
431 "Fetch mail from SOURCE and call CALLBACK zero or more times.
432 CALLBACK will be called with the name of the file where (some of)
433 the mail from SOURCE is put.
434 Return the number of files that were found."
435 (mail-source-bind-common source
436 (if (or mail-source-plugged plugged)
438 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
441 (error "%S is an invalid mail source specification" source))
442 ;; If there's anything in the crash box, we do it first.
443 (when (file-exists-p mail-source-crash-box)
444 (message "Processing mail from %s..." mail-source-crash-box)
445 (setq found (mail-source-callback
446 callback mail-source-crash-box)))
449 (funcall function source callback)
452 (format "Mail source error (%s). Continue? " err))
453 (error "Cannot get new mail"))
456 (defun mail-source-make-complex-temp-name (prefix)
457 (let ((newname (make-temp-name prefix))
459 (while (file-exists-p newname)
460 (setq newprefix (concat newprefix "x"))
461 (setq newname (make-temp-name newprefix)))
464 (defun mail-source-callback (callback info)
465 "Call CALLBACK on the mail file, and then remove the mail file.
466 Pass INFO on to CALLBACK."
467 (if (or (not (file-exists-p mail-source-crash-box))
468 (zerop (nth 7 (file-attributes mail-source-crash-box))))
470 (when (file-exists-p mail-source-crash-box)
471 (delete-file mail-source-crash-box))
474 (funcall callback mail-source-crash-box info)
475 (when (file-exists-p mail-source-crash-box)
476 ;; Delete or move the incoming mail out of the way.
477 (if mail-source-delete-incoming
478 (delete-file mail-source-crash-box)
480 (mail-source-make-complex-temp-name
482 mail-source-incoming-file-prefix
483 mail-source-directory))))
484 (unless (file-exists-p (file-name-directory incoming))
485 (make-directory (file-name-directory incoming) t))
486 (rename-file mail-source-crash-box incoming t)))))))
488 (defun mail-source-movemail (from to)
489 "Move FROM to TO using movemail."
490 (if (not (file-writable-p to))
491 (error "Can't write to crash box %s. Not moving mail" to)
492 (let ((to (file-truename (expand-file-name to)))
494 (setq to (file-truename to)
495 from (file-truename from))
496 ;; Set TO if have not already done so, and rename or copy
497 ;; the file FROM to TO if and as appropriate.
500 ;; The crash box exists already.
502 ((not (file-exists-p from))
503 ;; There is no inbox.
505 ((zerop (nth 7 (file-attributes from)))
509 ;; If getting from mail spool directory, use movemail to move
510 ;; rather than just renaming, so as to interlock with the
514 (setq errors (generate-new-buffer " *mail source loss*"))
515 (let ((default-directory "/"))
521 (expand-file-name "movemail" exec-directory)
522 nil errors nil from to)))))
523 (when (file-exists-p to)
524 (set-file-modes to mail-source-default-file-modes))
525 (if (and (or (not (buffer-modified-p errors))
526 (zerop (buffer-size errors)))
528 ;; No output => movemail won.
531 ;; There may be a warning about older revisions. We
533 (goto-char (point-min))
534 (if (search-forward "older revision" nil t)
536 ;; Probably a real error.
537 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
538 (goto-char (point-max))
539 (skip-chars-backward " \t")
540 (delete-region (point) (point-max))
541 (goto-char (point-min))
542 (when (looking-at "movemail: ")
543 (delete-region (point-min) (match-end 0)))
544 ;; Result may be a signal description string.
546 (format "movemail: %s (%s return). Continue? "
547 (buffer-string) result))
548 (error "%s" (buffer-string)))
551 (buffer-name errors))
552 (kill-buffer errors))
553 ;; Return whether we moved successfully or not.
556 (defun mail-source-movemail-and-remove (from to)
557 "Move FROM to TO using movemail, then remove FROM if empty."
558 (or (not (mail-source-movemail from to))
559 (not (zerop (nth 7 (file-attributes from))))
562 (defvar mail-source-read-passwd nil)
563 (defun mail-source-read-passwd (prompt &rest args)
564 "Read a password using PROMPT.
565 If ARGS, PROMPT is used as an argument to `format'."
568 (apply 'format prompt args)
570 (unless mail-source-read-passwd
571 (if (or (fboundp 'read-passwd) (load "passwd" t))
572 (setq mail-source-read-passwd 'read-passwd)
573 (unless (fboundp 'ange-ftp-read-passwd)
574 (autoload 'ange-ftp-read-passwd "ange-ftp"))
575 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
576 (funcall mail-source-read-passwd prompt)))
578 (defun mail-source-fetch-with-program (program)
579 (zerop (call-process shell-file-name nil nil nil
580 shell-command-switch program)))
582 (defun mail-source-run-script (script spec &optional delay)
584 (if (and (symbolp script) (fboundp script))
586 (mail-source-call-script
587 (format-spec script spec))))
591 (defun mail-source-call-script (script)
592 (let ((background nil))
593 (when (string-match "& *$" script)
594 (setq script (substring script 0 (match-beginning 0))
596 (call-process shell-file-name nil background nil
597 shell-command-switch script)))
600 ;;; Different fetchers
603 (defun mail-source-fetch-file (source callback)
604 "Fetcher for single-file sources."
605 (mail-source-bind (file source)
606 (mail-source-run-script
607 prescript (format-spec-make ?t mail-source-crash-box)
609 (let ((mail-source-string (format "file:%s" path)))
610 (if (mail-source-movemail path mail-source-crash-box)
612 (mail-source-callback callback path)
613 (mail-source-run-script
614 postscript (format-spec-make ?t mail-source-crash-box)))
617 (defun mail-source-fetch-directory (source callback)
618 "Fetcher for directory sources."
619 (mail-source-bind (directory source)
621 (mail-source-string (format "directory:%s" path)))
622 (dolist (file (directory-files
623 path t (concat (regexp-quote suffix) "$")))
624 (when (and (file-regular-p file)
625 (funcall predicate file)
626 (mail-source-movemail file mail-source-crash-box))
627 (incf found (mail-source-callback callback file))))
630 (defun mail-source-fetch-pop (source callback)
631 "Fetcher for single-file sources."
632 (mail-source-bind (pop source)
633 (mail-source-run-script
635 (format-spec-make ?p password ?t mail-source-crash-box
636 ?s server ?P port ?u user)
638 (let ((from (format "%s:%s:%s" server user port))
639 (mail-source-string (format "pop:%s@%s" user server))
641 (when (eq authentication 'password)
644 (cdr (assoc from mail-source-password-cache))
645 (mail-source-read-passwd
646 (format "Password for %s at %s: " user server)))))
648 (setenv "MAILHOST" server))
652 (mail-source-fetch-with-program
655 (format-spec-make ?p password ?t mail-source-crash-box
656 ?s server ?P port ?u user))))
658 (funcall function mail-source-crash-box))
659 ;; The default is to use pop3.el.
661 (let ((pop3-password password)
663 (pop3-mailhost server)
665 (pop3-authentication-scheme
666 (if (eq authentication 'apop) 'apop 'pass)))
668 (save-excursion (pop3-movemail mail-source-crash-box))
670 ;; We nix out the password in case the error
671 ;; was because of a wrong password being given.
672 (setq mail-source-password-cache
673 (delq (assoc from mail-source-password-cache)
674 mail-source-password-cache))
675 (signal (car err) (cdr err))))))))
678 (when (eq authentication 'password)
679 (unless (assoc from mail-source-password-cache)
680 (push (cons from password) mail-source-password-cache)))
682 (mail-source-callback callback server)
683 ;; Update display-time's mail flag, if relevant.
684 (if (equal source mail-source-primary-source)
685 (setq mail-source-new-mail-available nil))
686 (mail-source-run-script
688 (format-spec-make ?p password ?t mail-source-crash-box
689 ?s server ?P port ?u user))))
690 ;; We nix out the password in case the error
691 ;; was because of a wrong password being given.
692 (setq mail-source-password-cache
693 (delq (assoc from mail-source-password-cache)
694 mail-source-password-cache))
697 (defun mail-source-check-pop (source)
698 "Check whether there is new mail."
699 (mail-source-bind (pop source)
700 (let ((from (format "%s:%s:%s" server user port))
701 (mail-source-string (format "pop:%s@%s" user server))
703 (when (eq authentication 'password)
706 (cdr (assoc from mail-source-password-cache))
707 (mail-source-read-passwd
708 (format "Password for %s at %s: " user server))))
709 (unless (assoc from mail-source-password-cache)
710 (push (cons from password) mail-source-password-cache)))
712 (setenv "MAILHOST" server))
715 ;; No easy way to check whether mail is waiting for these.
718 ;; The default is to use pop3.el.
720 (let ((pop3-password password)
722 (pop3-mailhost server)
724 (pop3-authentication-scheme
725 (if (eq authentication 'apop) 'apop 'pass)))
727 (save-excursion (pop3-get-message-count))
729 ;; We nix out the password in case the error
730 ;; was because of a wrong password being given.
731 (setq mail-source-password-cache
732 (delq (assoc from mail-source-password-cache)
733 mail-source-password-cache))
734 (signal (car err) (cdr err))))))))
736 ;; Inform display-time that we have new mail.
737 (setq mail-source-new-mail-available (> result 0))
738 ;; We nix out the password in case the error
739 ;; was because of a wrong password being given.
740 (setq mail-source-password-cache
741 (delq (assoc from mail-source-password-cache)
742 mail-source-password-cache)))
745 (defun mail-source-new-mail-p ()
746 "Handler for `display-time' to indicate when new mail is available."
747 ;; Only report flag setting; flag is updated on a different schedule.
748 mail-source-new-mail-available)
751 (defvar mail-source-report-new-mail nil)
752 (defvar mail-source-report-new-mail-timer nil)
753 (defvar mail-source-report-new-mail-idle-timer nil)
756 (if (featurep 'xemacs)
760 (defun mail-source-start-idle-timer ()
761 ;; Start our idle timer if necessary, so we delay the check until the
762 ;; user isn't typing.
763 (unless mail-source-report-new-mail-idle-timer
764 (setq mail-source-report-new-mail-idle-timer
766 mail-source-idle-time-delay
769 (mail-source-check-pop mail-source-primary-source)
770 (setq mail-source-report-new-mail-idle-timer nil))))
771 ;; Since idle timers created when Emacs is already in the idle
772 ;; state don't get activated until Emacs _next_ becomes idle, we
773 ;; need to force our timer to be considered active now. We do
774 ;; this by being naughty and poking the timer internals directly
775 ;; (element 0 of the vector is nil if the timer is active).
776 (aset mail-source-report-new-mail-idle-timer 0 nil)))
778 (defun mail-source-report-new-mail (arg)
779 "Toggle whether to report when new mail is available.
780 This only works when `display-time' is enabled."
782 (if (not mail-source-primary-source)
783 (error "Need to set `mail-source-primary-source' to check for new mail"))
784 (let ((on (if (null arg)
785 (not mail-source-report-new-mail)
786 (> (prefix-numeric-value arg) 0))))
787 (setq mail-source-report-new-mail on)
788 (and mail-source-report-new-mail-timer
789 (nnheader-cancel-timer mail-source-report-new-mail-timer))
790 (and mail-source-report-new-mail-idle-timer
791 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
792 (setq mail-source-report-new-mail-timer nil)
793 (setq mail-source-report-new-mail-idle-timer nil)
797 ;; display-time-mail-function is an Emacs 21 feature.
798 (setq display-time-mail-function #'mail-source-new-mail-p)
799 ;; Set up the main timer.
800 (setq mail-source-report-new-mail-timer
801 (nnheader-run-at-time
802 (* 60 mail-source-report-new-mail-interval)
803 (* 60 mail-source-report-new-mail-interval)
804 #'mail-source-start-idle-timer))
805 ;; When you get new mail, clear "Mail" from the mode line.
806 (add-hook 'nnmail-post-get-new-mail-hook
807 'display-time-event-handler)
808 (message "Mail check enabled"))
809 (setq display-time-mail-function nil)
810 (remove-hook 'nnmail-post-get-new-mail-hook
811 'display-time-event-handler)
812 (message "Mail check disabled"))))
814 (defun mail-source-fetch-maildir (source callback)
815 "Fetcher for maildir sources."
816 (mail-source-bind (maildir source)
819 (unless (string-match "/$" path)
820 (setq path (concat path "/")))
821 (dolist (subdir subdirs)
822 (when (file-directory-p (concat path subdir))
823 (setq mail-source-string (format "maildir:%s%s" path subdir))
824 (dolist (file (directory-files (concat path subdir) t))
825 (when (and (not (file-directory-p file))
827 (funcall function file mail-source-crash-box)
828 (let ((coding-system-for-write
829 mm-text-coding-system)
830 (coding-system-for-read
831 mm-text-coding-system))
832 (with-temp-file mail-source-crash-box
833 (insert-file-contents file)
834 (goto-char (point-min))
835 ;;; ;; Unix mail format
836 ;;; (unless (looking-at "\n*From ")
837 ;;; (insert "From maildir "
838 ;;; (current-time-string) "\n"))
839 ;;; (while (re-search-forward "^From " nil t)
840 ;;; (replace-match ">From "))
841 ;;; (goto-char (point-max))
844 (insert "\001\001\001\001\n"))
845 (delete-file file)))))
846 (incf found (mail-source-callback callback file))))))
850 (autoload 'imap-open "imap")
851 (autoload 'imap-authenticate "imap")
852 (autoload 'imap-mailbox-select "imap")
853 (autoload 'imap-mailbox-unselect "imap")
854 (autoload 'imap-mailbox-close "imap")
855 (autoload 'imap-search "imap")
856 (autoload 'imap-fetch "imap")
857 (autoload 'imap-close "imap")
858 (autoload 'imap-error-text "imap")
859 (autoload 'imap-message-flags-add "imap")
860 (autoload 'imap-list-to-message-set "imap")
861 (autoload 'imap-range-to-message-set "imap")
862 (autoload 'nnheader-ms-strip-cr "nnheader"))
864 (defvar mail-source-imap-file-coding-system 'binary
865 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
867 (defun mail-source-fetch-imap (source callback)
868 "Fetcher for imap sources."
869 (mail-source-bind (imap source)
870 (let ((from (format "%s:%s:%s" server user port))
872 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
873 (mail-source-string (format "imap:%s:%s" server mailbox))
874 (imap-shell-program (or (list program) imap-shell-program))
876 (if (and (imap-open server port stream authentication buf)
878 user (or (cdr (assoc from mail-source-password-cache))
880 (imap-mailbox-select mailbox nil buf))
881 (let ((coding-system-for-write mail-source-imap-file-coding-system)
883 (with-temp-file mail-source-crash-box
884 ;; Avoid converting 8-bit chars from inserted strings to
886 (mm-disable-multibyte)
888 (with-current-buffer buf
889 (when (or imap-password
890 (assoc from mail-source-password-cache))
891 (push (cons from imap-password) mail-source-password-cache)))
892 ;; if predicate is nil, use all uids
893 (dolist (uid (imap-search (or predicate "1:*") buf))
894 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
896 (insert "From imap " (current-time-string) "\n")
899 (while (re-search-forward "^From " nil t)
900 (replace-match ">From "))
901 (goto-char (point-max))))
902 (nnheader-ms-strip-cr))
903 (incf found (mail-source-callback callback server))
904 (when (and remove fetchflag)
905 (imap-message-flags-add
906 (imap-range-to-message-set (gnus-compress-sequence remove))
909 (imap-mailbox-unselect buf)
910 (imap-mailbox-close buf))
913 ;; We nix out the password in case the error
914 ;; was because of a wrong password being given.
915 (setq mail-source-password-cache
916 (delq (assoc from mail-source-password-cache)
917 mail-source-password-cache))
918 (error (imap-error-text buf)))
923 (autoload 'webmail-fetch "webmail"))
925 (defun mail-source-fetch-webmail (source callback)
926 "Fetch for webmail source."
927 (mail-source-bind (webmail source)
928 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
929 (webmail-newmail-only dontexpunge)
930 (webmail-move-to-trash-can (not dontexpunge)))
931 (when (eq authentication 'password)
934 (cdr (assoc (format "webmail:%s:%s" subtype user)
935 mail-source-password-cache))
936 (mail-source-read-passwd
937 (format "Password for %s at %s: " user subtype))))
939 (not (assoc (format "webmail:%s:%s" subtype user)
940 mail-source-password-cache)))
941 (push (cons (format "webmail:%s:%s" subtype user) password)
942 mail-source-password-cache)))
943 (webmail-fetch mail-source-crash-box subtype user password)
944 (mail-source-callback callback (symbol-name subtype)))))
946 (provide 'mail-source)
948 ;;; mail-source.el ends here