(back-to-indentation): Skip all whitespace except for newlines.
[emacs.git] / lisp / gnus / mail-source.el
blobad0f5fc23d72fc6b3e1a6e10386d3344511a53de
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)
12 ;; any later version.
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.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'cl)
30 (require 'imap)
31 (eval-when-compile (defvar display-time-mail-function)))
32 (eval-and-compile
33 (autoload 'pop3-movemail "pop3")
34 (autoload 'pop3-get-message-count "pop3")
35 (autoload 'nnheader-cancel-timer "nnheader"))
36 (require 'format-spec)
37 (require 'mm-util)
39 (defgroup mail-source nil
40 "The mail-fetching library."
41 :version "21.1"
42 :group 'gnus)
44 ;; Define these at compile time to avoid dragging in imap always.
45 (defconst mail-source-imap-authenticators
46 (eval-when-compile
47 (mapcar (lambda (a)
48 (list 'const (car a)))
49 imap-authenticator-alist)))
50 (defconst mail-source-imap-streams
51 (eval-when-compile
52 (mapcar (lambda (a)
53 (list 'const (car a)))
54 imap-stream-alist)))
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'."
60 :group 'mail-source
61 :type `(repeat
62 (choice :format "%[Value Menu%] %v"
63 :value (file)
64 (cons :tag "Spool file"
65 (const :format "" file)
66 (checklist :tag "Options" :greedy t
67 (group :inline t
68 (const :format "" :value :path)
69 file)))
70 (cons :tag "Several files in a directory"
71 (const :format "" directory)
72 (checklist :tag "Options" :greedy t
73 (group :inline t
74 (const :format "" :value :path)
75 (directory :tag "Path"))
76 (group :inline t
77 (const :format "" :value :suffix)
78 (string :tag "Suffix"))
79 (group :inline t
80 (const :format "" :value :predicate)
81 (function :tag "Predicate"))
82 (group :inline t
83 (const :format "" :value :prescript)
84 (string :tag "Prescript"))
85 (group :inline t
86 (const :format "" :value :postscript)
87 (string :tag "Postscript"))
88 (group :inline t
89 (const :format "" :value :plugged)
90 (boolean :tag "Plugged"))))
91 (cons :tag "POP3 server"
92 (const :format "" pop)
93 (checklist :tag "Options" :greedy t
94 (group :inline t
95 (const :format "" :value :server)
96 (string :tag "Server"))
97 (group :inline t
98 (const :format "" :value :port)
99 (choice :tag "Port"
100 :value "pop3"
101 (number :format "%v")
102 (string :format "%v")))
103 (group :inline t
104 (const :format "" :value :user)
105 (string :tag "User"))
106 (group :inline t
107 (const :format "" :value :password)
108 (string :tag "Password"))
109 (group :inline t
110 (const :format "" :value :program)
111 (string :tag "Program"))
112 (group :inline t
113 (const :format "" :value :prescript)
114 (string :tag "Prescript"))
115 (group :inline t
116 (const :format "" :value :postscript)
117 (string :tag "Postscript"))
118 (group :inline t
119 (const :format "" :value :function)
120 (function :tag "Function"))
121 (group :inline t
122 (const :format ""
123 :value :authentication)
124 (choice :tag "Authentication"
125 :value apop
126 (const password)
127 (const apop)))
128 (group :inline t
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
134 (group :inline t
135 (const :format "" :value :path)
136 (directory :tag "Path"))
137 (group :inline t
138 (const :format "" :value :plugged)
139 (boolean :tag "Plugged"))))
140 (cons :tag "IMAP server"
141 (const :format "" imap)
142 (checklist :tag "Options" :greedy t
143 (group :inline t
144 (const :format "" :value :server)
145 (string :tag "Server"))
146 (group :inline t
147 (const :format "" :value :port)
148 (choice :tag "Port"
149 :value 143
150 number string))
151 (group :inline t
152 (const :format "" :value :user)
153 (string :tag "User"))
154 (group :inline t
155 (const :format "" :value :password)
156 (string :tag "Password"))
157 (group :inline t
158 (const :format "" :value :stream)
159 (choice :tag "Stream"
160 :value network
161 ,@mail-source-imap-streams))
162 (group :inline t
163 (const :format ""
164 :value :authenticator)
165 (choice :tag "Authenticator"
166 :value login
167 ,@mail-source-imap-authenticators))
168 (group :inline t
169 (const :format "" :value :mailbox)
170 (string :tag "Mailbox"
171 :value "INBOX"))
172 (group :inline t
173 (const :format "" :value :predicate)
174 (string :tag "Predicate"
175 :value "UNSEEN UNDELETED"))
176 (group :inline t
177 (const :format "" :value :fetchflag)
178 (string :tag "Fetchflag"
179 :value "\\Deleted"))
180 (group :inline t
181 (const :format ""
182 :value :dontexpunge)
183 (boolean :tag "Dontexpunge"))
184 (group :inline t
185 (const :format "" :value :plugged)
186 (boolean :tag "Plugged"))))
187 (cons :tag "Webmail server"
188 (const :format "" webmail)
189 (checklist :tag "Options" :greedy t
190 (group :inline 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"
196 :value hotmail
197 (const hotmail)
198 (const yahoo)
199 (const netaddress)
200 (const netscape)
201 (const my-deja)))
202 (group :inline t
203 (const :format "" :value :user)
204 (string :tag "User"))
205 (group :inline t
206 (const :format "" :value :password)
207 (string :tag "Password"))
208 (group :inline t
209 (const :format ""
210 :value :dontexpunge)
211 (boolean :tag "Dontexpunge"))
212 (group :inline t
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."
219 :group 'mail-source
220 :type 'sexp)
222 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
223 "File where mail will be stored while processing it."
224 :group 'mail-source
225 :type 'file)
227 (defcustom mail-source-directory "~/Mail/"
228 "Directory where files (if any) will be stored."
229 :group 'mail-source
230 :type 'directory)
232 (defcustom mail-source-default-file-modes 384
233 "Set the mode bits of all new mail files to this integer."
234 :group 'mail-source
235 :type 'integer)
237 (defcustom mail-source-delete-incoming t
238 "*If non-nil, delete incoming files after handling."
239 :group 'mail-source
240 :type 'boolean)
242 (defcustom mail-source-incoming-file-prefix "Incoming"
243 "Prefix for file name for storing incoming mail"
244 :group 'mail-source
245 :type 'string)
247 (defcustom mail-source-report-new-mail-interval 5
248 "Interval in minutes between checks for new mail."
249 :group 'mail-source
250 :type 'number)
252 (defcustom mail-source-idle-time-delay 5
253 "Number of idle seconds to wait before checking for new mail."
254 :group 'mail-source
255 :type 'number)
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.")
265 (eval-and-compile
266 (defvar mail-source-common-keyword-map
267 '((:plugged))
268 "Mapping from keywords to default values.
269 Common keywords should be listed here.")
271 (defvar mail-source-keyword-map
272 '((file
273 (:prescript)
274 (:prescript-delay)
275 (:postscript)
276 (:path (or (getenv "MAIL")
277 (expand-file-name (user-login-name) rmail-spool-directory))))
278 (directory
279 (:prescript)
280 (:prescript-delay)
281 (:postscript)
282 (:path)
283 (:suffix ".spool")
284 (:predicate identity))
285 (pop
286 (:prescript)
287 (:prescript-delay)
288 (:postscript)
289 (:server (getenv "MAILHOST"))
290 (:port 110)
291 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
292 (:program)
293 (:function)
294 (:password)
295 (:authentication password))
296 (maildir
297 (:path (or (getenv "MAILDIR") "~/Maildir/"))
298 (:subdirs ("new" "cur"))
299 (:function))
300 (imap
301 (:server (getenv "MAILHOST"))
302 (:port)
303 (:stream)
304 (:authentication)
305 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
306 (:password)
307 (:mailbox "INBOX")
308 (:predicate "UNSEEN UNDELETED")
309 (:fetchflag "\\Deleted")
310 (:dontexpunge))
311 (webmail
312 (:subtype hotmail)
313 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
314 (:password)
315 (:dontexpunge)
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)
333 ;;; Functions
335 (eval-and-compile
336 (defun mail-source-strip-keyword (keyword)
337 "Strip the leading colon off the KEYWORD."
338 (intern (substring (symbol-name keyword) 1))))
340 (eval-and-compile
341 (defun mail-source-bind-1 (type)
342 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
343 default bind)
344 (while (setq default (pop defaults))
345 (push (list (mail-source-strip-keyword (car default))
346 nil)
347 bind))
348 bind)))
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
359 of the `let' form.
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))
365 ,@body))
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)))))))
380 (eval-and-compile
381 (defun mail-source-bind-common-1 ()
382 (let* ((defaults mail-source-common-keyword-map)
383 default bind)
384 (while (setq default (pop defaults))
385 (push (list (mail-source-strip-keyword (car default))
386 nil)
387 bind))
388 bind)))
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)
408 ,@body))
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."
415 (cond
416 ;; String
417 ((stringp value)
418 value)
419 ;; Function
420 ((and (listp value)
421 (functionp (car value)))
422 (eval value))
423 ;; Just return the value.
425 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)
434 (save-excursion
435 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
436 (found 0))
437 (unless function
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)))
444 (+ found
445 (condition-case err
446 (funcall function source callback)
447 (error
448 (unless (yes-or-no-p
449 (format "Mail source error (%s). Continue? " err))
450 (error "Cannot get new mail"))
451 0))))))))
453 (eval-and-compile
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))
458 (newprefix prefix))
459 (while (file-exists-p newname)
460 (setq newprefix (concat newprefix "x"))
461 (setq newname (make-temp-name newprefix)))
462 newname))))
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))))
469 (progn
470 (when (file-exists-p mail-source-crash-box)
471 (delete-file mail-source-crash-box))
473 (prog1
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)
479 (let ((incoming
480 (mail-source-make-complex-temp-name
481 (expand-file-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)))
493 errors result)
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.
498 (cond
499 ((file-exists-p to)
500 ;; The crash box exists already.
502 ((not (file-exists-p from))
503 ;; There is no inbox.
504 (setq to nil))
505 ((zerop (nth 7 (file-attributes from)))
506 ;; Empty file.
507 (setq to nil))
509 ;; If getting from mail spool directory, use movemail to move
510 ;; rather than just renaming, so as to interlock with the
511 ;; mailer.
512 (unwind-protect
513 (save-excursion
514 (setq errors (generate-new-buffer " *mail source loss*"))
515 (let ((default-directory "/"))
516 (setq result
517 (apply
518 'call-process
519 (append
520 (list
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))
526 (zerop result))
527 ;; No output => movemail won.
529 (set-buffer errors)
530 ;; There may be a warning about older revisions. We
531 ;; ignore that.
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)))
543 (unless (yes-or-no-p
544 (format "movemail: %s (%d return). Continue? "
545 (buffer-string) result))
546 (error "%s" (buffer-string)))
547 (setq to nil)))))))
548 (when (and errors
549 (buffer-name errors))
550 (kill-buffer errors))
551 ;; Return whether we moved successfully or not.
552 to)))
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))))
558 (delete-file 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'."
564 (let ((prompt
565 (if args
566 (apply 'format prompt args)
567 prompt)))
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)
581 (when script
582 (if (and (symbolp script) (fboundp script))
583 (funcall script)
584 (mail-source-call-script
585 (format-spec script spec))))
586 (when delay
587 (sleep-for delay)))
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))
593 background 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)
606 prescript-delay)
607 (let ((mail-source-string (format "file:%s" path)))
608 (if (mail-source-movemail path mail-source-crash-box)
609 (prog1
610 (mail-source-callback callback path)
611 (mail-source-run-script
612 postscript (format-spec-make ?t mail-source-crash-box)))
613 0))))
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)
620 prescript-delay)
621 (let ((found 0)
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))
631 found)))
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
637 prescript
638 (format-spec-make ?p password ?t mail-source-crash-box
639 ?s server ?P port ?u user)
640 prescript-delay)
641 (let ((from (format "%s:%s:%s" server user port))
642 (mail-source-string (format "pop:%s@%s" user server))
643 result)
644 (when (eq authentication 'password)
645 (setq password
646 (or password
647 (cdr (assoc from mail-source-password-cache))
648 (mail-source-read-passwd
649 (format "Password for %s at %s: " user server)))))
650 (when server
651 (setenv "MAILHOST" server))
652 (setq result
653 (cond
654 (program
655 (mail-source-fetch-with-program
656 (format-spec
657 program
658 (format-spec-make ?p password ?t mail-source-crash-box
659 ?s server ?P port ?u user))))
660 (function
661 (funcall function mail-source-crash-box))
662 ;; The default is to use pop3.el.
664 (let ((pop3-password password)
665 (pop3-maildrop user)
666 (pop3-mailhost server)
667 (pop3-port port)
668 (pop3-authentication-scheme
669 (if (eq authentication 'apop) 'apop 'pass)))
670 (save-excursion (pop3-movemail mail-source-crash-box))))))
671 (if result
672 (progn
673 (when (eq authentication 'password)
674 (unless (assoc from mail-source-password-cache)
675 (push (cons from password) mail-source-password-cache)))
676 (prog1
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
682 postscript
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))
690 0))))
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))
697 result)
698 (when (eq authentication 'password)
699 (setq password
700 (or 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)))
706 (when server
707 (setenv "MAILHOST" server))
708 (setq result
709 (cond
710 ;; No easy way to check whether mail is waiting for these.
711 (program)
712 (function)
713 ;; The default is to use pop3.el.
715 (let ((pop3-password password)
716 (pop3-maildrop user)
717 (pop3-mailhost server)
718 (pop3-port port)
719 (pop3-authentication-scheme
720 (if (eq authentication 'apop) 'apop 'pass)))
721 (save-excursion (pop3-get-message-count))))))
722 (if result
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)))
730 result)))
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)
742 (eval-when-compile
743 (if (featurep 'xemacs)
744 (require 'itimer)
745 (require 'timer)))
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
752 (run-with-idle-timer
753 mail-source-idle-time-delay
755 (lambda ()
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."
768 (interactive "P")
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)
781 (if on
782 (progn
783 (require 'time)
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)
802 (let ((found 0)
803 mail-source-string)
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))
811 (not (if function
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))
827 ;;; (insert "\n\n")
828 ;; MMDF mail format
829 (insert "\001\001\001\001\n"))
830 (delete-file file)))))
831 (incf found (mail-source-callback callback file))))))
832 found)))
834 (eval-and-compile
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))
856 (found 0)
857 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
858 (mail-source-string (format "imap:%s:%s" server mailbox))
859 remove)
860 (if (and (imap-open server port stream authentication buf)
861 (imap-authenticate
862 user (or (cdr (assoc from mail-source-password-cache))
863 password) buf)
864 (imap-mailbox-select mailbox nil buf))
865 (let ((coding-system-for-write mail-source-imap-file-coding-system)
866 str)
867 (with-temp-file mail-source-crash-box
868 ;; Avoid converting 8-bit chars from inserted strings to
869 ;; multibyte.
870 (mm-disable-multibyte)
871 ;; remember password
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))
879 (push uid remove)
880 (insert "From imap " (current-time-string) "\n")
881 (save-excursion
882 (insert str "\n\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))
891 fetchflag nil buf))
892 (if dontexpunge
893 (imap-mailbox-unselect buf)
894 (imap-mailbox-close buf))
895 (imap-close buf))
896 (imap-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)))
903 (kill-buffer buf)
904 found)))
906 (eval-and-compile
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)
916 (setq password
917 (or 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))))
922 (when (and password
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