1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002 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."
44 ;; Define these at compile time to avoid dragging in imap always.
45 (defconst mail-source-imap-authenticators
48 (list 'const
(car a
)))
49 imap-authenticator-alist
)))
50 (defconst mail-source-imap-streams
53 (list 'const
(car a
)))
56 (defcustom mail-sources nil
57 "*Where the mail backends will look for incoming mail.
58 This variable is a list of mail source specifiers.
59 See Info node `(gnus)Mail Source Specifiers'."
62 (choice :format
"%[Value Menu%] %v"
64 (cons :tag
"Spool file"
65 (const :format
"" file
)
66 (checklist :tag
"Options" :greedy t
68 (const :format
"" :value
:path
)
70 (cons :tag
"Several files in a directory"
71 (const :format
"" directory
)
72 (checklist :tag
"Options" :greedy t
74 (const :format
"" :value
:path
)
75 (directory :tag
"Path"))
77 (const :format
"" :value
:suffix
)
78 (string :tag
"Suffix"))
80 (const :format
"" :value
:predicate
)
81 (function :tag
"Predicate"))
83 (const :format
"" :value
:prescript
)
84 (string :tag
"Prescript"))
86 (const :format
"" :value
:postscript
)
87 (string :tag
"Postscript"))
89 (const :format
"" :value
:plugged
)
90 (boolean :tag
"Plugged"))))
91 (cons :tag
"POP3 server"
92 (const :format
"" pop
)
93 (checklist :tag
"Options" :greedy t
95 (const :format
"" :value
:server
)
96 (string :tag
"Server"))
98 (const :format
"" :value
:port
)
101 (number :format
"%v")
102 (string :format
"%v")))
104 (const :format
"" :value
:user
)
105 (string :tag
"User"))
107 (const :format
"" :value
:password
)
108 (string :tag
"Password"))
110 (const :format
"" :value
:program
)
111 (string :tag
"Program"))
113 (const :format
"" :value
:prescript
)
114 (string :tag
"Prescript"))
116 (const :format
"" :value
:postscript
)
117 (string :tag
"Postscript"))
119 (const :format
"" :value
:function
)
120 (function :tag
"Function"))
123 :value
:authentication
)
124 (choice :tag
"Authentication"
129 (const :format
"" :value
:plugged
)
130 (boolean :tag
"Plugged"))))
131 (cons :tag
"Maildir (qmail, postfix...)"
132 (const :format
"" maildir
)
133 (checklist :tag
"Options" :greedy t
135 (const :format
"" :value
:path
)
136 (directory :tag
"Path"))
138 (const :format
"" :value
:plugged
)
139 (boolean :tag
"Plugged"))))
140 (cons :tag
"IMAP server"
141 (const :format
"" imap
)
142 (checklist :tag
"Options" :greedy t
144 (const :format
"" :value
:server
)
145 (string :tag
"Server"))
147 (const :format
"" :value
:port
)
152 (const :format
"" :value
:user
)
153 (string :tag
"User"))
155 (const :format
"" :value
:password
)
156 (string :tag
"Password"))
158 (const :format
"" :value
:stream
)
159 (choice :tag
"Stream"
161 ,@mail-source-imap-streams
))
164 :value
:authenticator
)
165 (choice :tag
"Authenticator"
167 ,@mail-source-imap-authenticators
))
169 (const :format
"" :value
:mailbox
)
170 (string :tag
"Mailbox"
173 (const :format
"" :value
:predicate
)
174 (string :tag
"Predicate"
175 :value
"UNSEEN UNDELETED"))
177 (const :format
"" :value
:fetchflag
)
178 (string :tag
"Fetchflag"
183 (boolean :tag
"Dontexpunge"))
185 (const :format
"" :value
:plugged
)
186 (boolean :tag
"Plugged"))))
187 (cons :tag
"Webmail server"
188 (const :format
"" webmail
)
189 (checklist :tag
"Options" :greedy t
191 (const :format
"" :value
:subtype
)
192 ;; Should be generated from
193 ;; `webmail-type-definition', but we
194 ;; can't require webmail without W3.
195 (choice :tag
"Subtype"
203 (const :format
"" :value
:user
)
204 (string :tag
"User"))
206 (const :format
"" :value
:password
)
207 (string :tag
"Password"))
211 (boolean :tag
"Dontexpunge"))
213 (const :format
"" :value
:plugged
)
214 (boolean :tag
"Plugged")))))))
216 (defcustom mail-source-primary-source nil
217 "*Primary source for incoming mail.
218 If non-nil, this maildrop will be checked periodically for new mail."
222 (defcustom mail-source-crash-box
"~/.emacs-mail-crash-box"
223 "File where mail will be stored while processing it."
227 (defcustom mail-source-directory
"~/Mail/"
228 "Directory where files (if any) will be stored."
232 (defcustom mail-source-default-file-modes
384
233 "Set the mode bits of all new mail files to this integer."
237 (defcustom mail-source-delete-incoming t
238 "*If non-nil, delete incoming files after handling."
242 (defcustom mail-source-incoming-file-prefix
"Incoming"
243 "Prefix for file name for storing incoming mail"
247 (defcustom mail-source-report-new-mail-interval
5
248 "Interval in minutes between checks for new mail."
252 (defcustom mail-source-idle-time-delay
5
253 "Number of idle seconds to wait before checking for new mail."
257 ;;; Internal variables.
259 (defvar mail-source-string
""
260 "A dynamically bound string that says what the current mail source is.")
262 (defvar mail-source-new-mail-available nil
263 "Flag indicating when new mail is available.")
266 (defvar mail-source-common-keyword-map
268 "Mapping from keywords to default values.
269 Common keywords should be listed here.")
271 (defvar mail-source-keyword-map
276 (:path
(or (getenv "MAIL")
277 (expand-file-name (user-login-name) rmail-spool-directory
))))
284 (:predicate identity
))
289 (:server
(getenv "MAILHOST"))
291 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
295 (:authentication password
))
297 (:path
(or (getenv "MAILDIR") "~/Maildir/"))
298 (:subdirs
("new" "cur"))
301 (:server
(getenv "MAILHOST"))
305 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
308 (:predicate
"UNSEEN UNDELETED")
309 (:fetchflag
"\\Deleted")
313 (:user
(or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
316 (:authentication password
)))
317 "Mapping from keywords to default values.
318 All keywords that can be used must be listed here."))
320 (defvar mail-source-fetcher-alist
321 '((file mail-source-fetch-file
)
322 (directory mail-source-fetch-directory
)
323 (pop mail-source-fetch-pop
)
324 (maildir mail-source-fetch-maildir
)
325 (imap mail-source-fetch-imap
)
326 (webmail mail-source-fetch-webmail
))
327 "A mapping from source type to fetcher function.")
329 (defvar mail-source-password-cache nil
)
331 (defvar mail-source-plugged t
)
336 (defun mail-source-strip-keyword (keyword)
337 "Strip the leading colon off the KEYWORD."
338 (intern (substring (symbol-name keyword
) 1))))
341 (defun mail-source-bind-1 (type)
342 (let* ((defaults (cdr (assq type mail-source-keyword-map
)))
344 (while (setq default
(pop defaults
))
345 (push (list (mail-source-strip-keyword (car default
))
350 (defmacro mail-source-bind
(type-source &rest body
)
351 "Return a `let' form that binds all variables in source TYPE.
352 TYPE-SOURCE is a list where the first element is the TYPE, and
353 the second variable is the SOURCE.
354 At run time, the mail source specifier SOURCE will be inspected,
355 and the variables will be set according to it. Variables not
356 specified will be given default values.
358 After this is done, BODY will be executed in the scope
361 The variables bound and their default values are described by
362 the `mail-source-keyword-map' variable."
363 `(let ,(mail-source-bind-1 (car type-source
))
364 (mail-source-set-1 ,(cadr type-source
))
367 (put 'mail-source-bind
'lisp-indent-function
1)
368 (put 'mail-source-bind
'edebug-form-spec
'(form body
))
370 (defun mail-source-set-1 (source)
371 (let* ((type (pop source
))
372 (defaults (cdr (assq type mail-source-keyword-map
)))
373 default value keyword
)
374 (while (setq default
(pop defaults
))
375 (set (mail-source-strip-keyword (setq keyword
(car default
)))
376 (if (setq value
(plist-get source keyword
))
377 (mail-source-value value
)
378 (mail-source-value (cadr default
)))))))
381 (defun mail-source-bind-common-1 ()
382 (let* ((defaults mail-source-common-keyword-map
)
384 (while (setq default
(pop defaults
))
385 (push (list (mail-source-strip-keyword (car default
))
390 (defun mail-source-set-common-1 (source)
391 (let* ((type (pop source
))
392 (defaults mail-source-common-keyword-map
)
393 (defaults-1 (cdr (assq type mail-source-keyword-map
)))
394 default value keyword
)
395 (while (setq default
(pop defaults
))
396 (set (mail-source-strip-keyword (setq keyword
(car default
)))
397 (if (setq value
(plist-get source keyword
))
398 (mail-source-value value
)
399 (if (setq value
(assq keyword defaults-1
))
400 (mail-source-value (cadr value
))
401 (mail-source-value (cadr default
))))))))
403 (defmacro mail-source-bind-common
(source &rest body
)
404 "Return a `let' form that binds all common variables.
405 See `mail-source-bind'."
406 `(let ,(mail-source-bind-common-1)
407 (mail-source-set-common-1 source
)
410 (put 'mail-source-bind-common
'lisp-indent-function
1)
411 (put 'mail-source-bind-common
'edebug-form-spec
'(form body
))
413 (defun mail-source-value (value)
414 "Return the value of VALUE."
421 (functionp (car value
)))
423 ;; Just return the value.
427 (defun mail-source-fetch (source callback
)
428 "Fetch mail from SOURCE and call CALLBACK zero or more times.
429 CALLBACK will be called with the name of the file where (some of)
430 the mail from SOURCE is put.
431 Return the number of files that were found."
432 (mail-source-bind-common source
433 (if (or mail-source-plugged plugged
)
435 (let ((function (cadr (assq (car source
) mail-source-fetcher-alist
)))
438 (error "%S is an invalid mail source specification" source
))
439 ;; If there's anything in the crash box, we do it first.
440 (when (file-exists-p mail-source-crash-box
)
441 (message "Processing mail from %s..." mail-source-crash-box
)
442 (setq found
(mail-source-callback
443 callback mail-source-crash-box
)))
446 (funcall function source callback
)
449 (format "Mail source error (%s). Continue? " err
))
450 (error "Cannot get new mail"))
454 (if (fboundp 'make-temp-file
)
455 (defalias 'mail-source-make-complex-temp-name
'make-temp-file
)
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 (not (buffer-modified-p errors
))
527 ;; No output => movemail won.
530 ;; There may be a warning about older revisions. We
532 (goto-char (point-min))
533 (if (search-forward "older revision" nil t
)
535 ;; Probably a real error.
536 (subst-char-in-region (point-min) (point-max) ?
\n ?\
)
537 (goto-char (point-max))
538 (skip-chars-backward " \t")
539 (delete-region (point) (point-max))
540 (goto-char (point-min))
541 (when (looking-at "movemail: ")
542 (delete-region (point-min) (match-end 0)))
544 (format "movemail: %s (%d return). Continue? "
545 (buffer-string) result
))
546 (error "%s" (buffer-string)))
549 (buffer-name errors
))
550 (kill-buffer errors
))
551 ;; Return whether we moved successfully or not.
554 (defun mail-source-movemail-and-remove (from to
)
555 "Move FROM to TO using movemail, then remove FROM if empty."
556 (or (not (mail-source-movemail from to
))
557 (not (zerop (nth 7 (file-attributes from
))))
560 (defvar mail-source-read-passwd nil
)
561 (defun mail-source-read-passwd (prompt &rest args
)
562 "Read a password using PROMPT.
563 If ARGS, PROMPT is used as an argument to `format'."
566 (apply 'format prompt args
)
568 (unless mail-source-read-passwd
569 (if (or (fboundp 'read-passwd
) (load "passwd" t
))
570 (setq mail-source-read-passwd
'read-passwd
)
571 (unless (fboundp 'ange-ftp-read-passwd
)
572 (autoload 'ange-ftp-read-passwd
"ange-ftp"))
573 (setq mail-source-read-passwd
'ange-ftp-read-passwd
)))
574 (funcall mail-source-read-passwd prompt
)))
576 (defun mail-source-fetch-with-program (program)
577 (zerop (call-process shell-file-name nil nil nil
578 shell-command-switch program
)))
580 (defun mail-source-run-script (script spec
&optional delay
)
582 (if (and (symbolp script
) (fboundp script
))
584 (mail-source-call-script
585 (format-spec script spec
))))
589 (defun mail-source-call-script (script)
590 (let ((background nil
))
591 (when (string-match "& *$" script
)
592 (setq script
(substring script
0 (match-beginning 0))
594 (call-process shell-file-name nil background nil
595 shell-command-switch script
)))
598 ;;; Different fetchers
601 (defun mail-source-fetch-file (source callback
)
602 "Fetcher for single-file sources."
603 (mail-source-bind (file source
)
604 (mail-source-run-script
605 prescript
(format-spec-make ?t mail-source-crash-box
)
607 (let ((mail-source-string (format "file:%s" path
)))
608 (if (mail-source-movemail path mail-source-crash-box
)
610 (mail-source-callback callback path
)
611 (mail-source-run-script
612 postscript
(format-spec-make ?t mail-source-crash-box
)))
615 (defun mail-source-fetch-directory (source callback
)
616 "Fetcher for directory sources."
617 (mail-source-bind (directory source
)
618 (mail-source-run-script
619 prescript
(format-spec-make ?t path
)
622 (mail-source-string (format "directory:%s" path
)))
623 (dolist (file (directory-files
624 path t
(concat (regexp-quote suffix
) "$")))
625 (when (and (file-regular-p file
)
626 (funcall predicate file
)
627 (mail-source-movemail file mail-source-crash-box
))
628 (incf found
(mail-source-callback callback file
))))
629 (mail-source-run-script
630 postscript
(format-spec-make ?t path
))
633 (defun mail-source-fetch-pop (source callback
)
634 "Fetcher for single-file sources."
635 (mail-source-bind (pop source
)
636 (mail-source-run-script
638 (format-spec-make ?p password ?t mail-source-crash-box
639 ?s server ?P port ?u user
)
641 (let ((from (format "%s:%s:%s" server user port
))
642 (mail-source-string (format "pop:%s@%s" user server
))
644 (when (eq authentication
'password
)
647 (cdr (assoc from mail-source-password-cache
))
648 (mail-source-read-passwd
649 (format "Password for %s at %s: " user server
)))))
651 (setenv "MAILHOST" server
))
655 (mail-source-fetch-with-program
658 (format-spec-make ?p password ?t mail-source-crash-box
659 ?s server ?P port ?u user
))))
661 (funcall function mail-source-crash-box
))
662 ;; The default is to use pop3.el.
664 (let ((pop3-password password
)
666 (pop3-mailhost server
)
668 (pop3-authentication-scheme
669 (if (eq authentication
'apop
) 'apop
'pass
)))
670 (save-excursion (pop3-movemail mail-source-crash-box
))))))
673 (when (eq authentication
'password
)
674 (unless (assoc from mail-source-password-cache
)
675 (push (cons from password
) mail-source-password-cache
)))
677 (mail-source-callback callback server
)
678 ;; Update display-time's mail flag, if relevant.
679 (if (equal source mail-source-primary-source
)
680 (setq mail-source-new-mail-available nil
))
681 (mail-source-run-script
683 (format-spec-make ?p password ?t mail-source-crash-box
684 ?s server ?P port ?u user
))))
685 ;; We nix out the password in case the error
686 ;; was because of a wrong password being given.
687 (setq mail-source-password-cache
688 (delq (assoc from mail-source-password-cache
)
689 mail-source-password-cache
))
692 (defun mail-source-check-pop (source)
693 "Check whether there is new mail."
694 (mail-source-bind (pop source
)
695 (let ((from (format "%s:%s:%s" server user port
))
696 (mail-source-string (format "pop:%s@%s" user server
))
698 (when (eq authentication
'password
)
701 (cdr (assoc from mail-source-password-cache
))
702 (mail-source-read-passwd
703 (format "Password for %s at %s: " user server
))))
704 (unless (assoc from mail-source-password-cache
)
705 (push (cons from password
) mail-source-password-cache
)))
707 (setenv "MAILHOST" server
))
710 ;; No easy way to check whether mail is waiting for these.
713 ;; The default is to use pop3.el.
715 (let ((pop3-password password
)
717 (pop3-mailhost server
)
719 (pop3-authentication-scheme
720 (if (eq authentication
'apop
) 'apop
'pass
)))
721 (save-excursion (pop3-get-message-count))))))
723 ;; Inform display-time that we have new mail.
724 (setq mail-source-new-mail-available
(> result
0))
725 ;; We nix out the password in case the error
726 ;; was because of a wrong password being given.
727 (setq mail-source-password-cache
728 (delq (assoc from mail-source-password-cache
)
729 mail-source-password-cache
)))
732 (defun mail-source-new-mail-p ()
733 "Handler for `display-time' to indicate when new mail is available."
734 ;; Only report flag setting; flag is updated on a different schedule.
735 mail-source-new-mail-available
)
738 (defvar mail-source-report-new-mail nil
)
739 (defvar mail-source-report-new-mail-timer nil
)
740 (defvar mail-source-report-new-mail-idle-timer nil
)
743 (if (featurep 'xemacs
)
747 (defun mail-source-start-idle-timer ()
748 ;; Start our idle timer if necessary, so we delay the check until the
749 ;; user isn't typing.
750 (unless mail-source-report-new-mail-idle-timer
751 (setq mail-source-report-new-mail-idle-timer
753 mail-source-idle-time-delay
756 (setq mail-source-report-new-mail-idle-timer nil
)
757 (mail-source-check-pop mail-source-primary-source
))))
758 ;; Since idle timers created when Emacs is already in the idle
759 ;; state don't get activated until Emacs _next_ becomes idle, we
760 ;; need to force our timer to be considered active now. We do
761 ;; this by being naughty and poking the timer internals directly
762 ;; (element 0 of the vector is nil if the timer is active).
763 (aset mail-source-report-new-mail-idle-timer
0 nil
)))
765 (defun mail-source-report-new-mail (arg)
766 "Toggle whether to report when new mail is available.
767 This only works when `display-time' is enabled."
769 (if (not mail-source-primary-source
)
770 (error "Need to set `mail-source-primary-source' to check for new mail"))
771 (let ((on (if (null arg
)
772 (not mail-source-report-new-mail
)
773 (> (prefix-numeric-value arg
) 0))))
774 (setq mail-source-report-new-mail on
)
775 (and mail-source-report-new-mail-timer
776 (nnheader-cancel-timer mail-source-report-new-mail-timer
))
777 (and mail-source-report-new-mail-idle-timer
778 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer
))
779 (setq mail-source-report-new-mail-timer nil
)
780 (setq mail-source-report-new-mail-idle-timer nil
)
784 ;; display-time-mail-function is an Emacs 21 feature.
785 (setq display-time-mail-function
#'mail-source-new-mail-p
)
786 ;; Set up the main timer.
787 (setq mail-source-report-new-mail-timer
788 (run-at-time t
(* 60 mail-source-report-new-mail-interval
)
789 #'mail-source-start-idle-timer
))
790 ;; When you get new mail, clear "Mail" from the mode line.
791 (add-hook 'nnmail-post-get-new-mail-hook
792 'display-time-event-handler
)
793 (message "Mail check enabled"))
794 (setq display-time-mail-function nil
)
795 (remove-hook 'nnmail-post-get-new-mail-hook
796 'display-time-event-handler
)
797 (message "Mail check disabled"))))
799 (defun mail-source-fetch-maildir (source callback
)
800 "Fetcher for maildir sources."
801 (mail-source-bind (maildir source
)
804 (unless (string-match "/$" path
)
805 (setq path
(concat path
"/")))
806 (dolist (subdir subdirs
)
807 (when (file-directory-p (concat path subdir
))
808 (setq mail-source-string
(format "maildir:%s%s" path subdir
))
809 (dolist (file (directory-files (concat path subdir
) t
))
810 (when (and (not (file-directory-p file
))
812 (funcall function file mail-source-crash-box
)
813 (let ((coding-system-for-write
814 mm-text-coding-system
)
815 (coding-system-for-read
816 mm-text-coding-system
))
817 (with-temp-file mail-source-crash-box
818 (insert-file-contents file
)
819 (goto-char (point-min))
820 ;;; ;; Unix mail format
821 ;;; (unless (looking-at "\n*From ")
822 ;;; (insert "From maildir "
823 ;;; (current-time-string) "\n"))
824 ;;; (while (re-search-forward "^From " nil t)
825 ;;; (replace-match ">From "))
826 ;;; (goto-char (point-max))
829 (insert "\001\001\001\001\n"))
830 (delete-file file
)))))
831 (incf found
(mail-source-callback callback file
))))))
835 (autoload 'imap-open
"imap")
836 (autoload 'imap-authenticate
"imap")
837 (autoload 'imap-mailbox-select
"imap")
838 (autoload 'imap-mailbox-unselect
"imap")
839 (autoload 'imap-mailbox-close
"imap")
840 (autoload 'imap-search
"imap")
841 (autoload 'imap-fetch
"imap")
842 (autoload 'imap-close
"imap")
843 (autoload 'imap-error-text
"imap")
844 (autoload 'imap-message-flags-add
"imap")
845 (autoload 'imap-list-to-message-set
"imap")
846 (autoload 'imap-range-to-message-set
"imap")
847 (autoload 'nnheader-ms-strip-cr
"nnheader"))
849 (defvar mail-source-imap-file-coding-system
'binary
850 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
852 (defun mail-source-fetch-imap (source callback
)
853 "Fetcher for imap sources."
854 (mail-source-bind (imap source
)
855 (let ((from (format "%s:%s:%s" server user port
))
857 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
858 (mail-source-string (format "imap:%s:%s" server mailbox
))
860 (if (and (imap-open server port stream authentication buf
)
862 user
(or (cdr (assoc from mail-source-password-cache
))
864 (imap-mailbox-select mailbox nil buf
))
865 (let ((coding-system-for-write mail-source-imap-file-coding-system
)
867 (with-temp-file mail-source-crash-box
868 ;; Avoid converting 8-bit chars from inserted strings to
870 (mm-disable-multibyte)
872 (with-current-buffer buf
873 (when (or imap-password
874 (assoc from mail-source-password-cache
))
875 (push (cons from imap-password
) mail-source-password-cache
)))
876 ;; if predicate is nil, use all uids
877 (dolist (uid (imap-search (or predicate
"1:*") buf
))
878 (when (setq str
(imap-fetch uid
"RFC822.PEEK" 'RFC822 nil buf
))
880 (insert "From imap " (current-time-string) "\n")
883 (while (re-search-forward "^From " nil t
)
884 (replace-match ">From "))
885 (goto-char (point-max))))
886 (nnheader-ms-strip-cr))
887 (incf found
(mail-source-callback callback server
))
888 (when (and remove fetchflag
)
889 (imap-message-flags-add
890 (imap-range-to-message-set (gnus-compress-sequence remove
))
893 (imap-mailbox-unselect buf
)
894 (imap-mailbox-close buf
))
897 ;; We nix out the password in case the error
898 ;; was because of a wrong password being given.
899 (setq mail-source-password-cache
900 (delq (assoc from mail-source-password-cache
)
901 mail-source-password-cache
))
902 (error (imap-error-text buf
)))
907 (autoload 'webmail-fetch
"webmail"))
909 (defun mail-source-fetch-webmail (source callback
)
910 "Fetch for webmail source."
911 (mail-source-bind (webmail source
)
912 (let ((mail-source-string (format "webmail:%s:%s" subtype user
))
913 (webmail-newmail-only dontexpunge
)
914 (webmail-move-to-trash-can (not dontexpunge
)))
915 (when (eq authentication
'password
)
918 (cdr (assoc (format "webmail:%s:%s" subtype user
)
919 mail-source-password-cache
))
920 (mail-source-read-passwd
921 (format "Password for %s at %s: " user subtype
))))
923 (not (assoc (format "webmail:%s:%s" subtype user
)
924 mail-source-password-cache
)))
925 (push (cons (format "webmail:%s:%s" subtype user
) password
)
926 mail-source-password-cache
)))
927 (webmail-fetch mail-source-crash-box subtype user password
)
928 (mail-source-callback callback
(symbol-name subtype
)))))
930 (provide 'mail-source
)
932 ;;; mail-source.el ends here