Patched a wrong location.
[bpt/emacs.git] / lisp / gnus / mail-source.el
1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
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)
38
39 (defgroup mail-source nil
40 "The mail-fetching library."
41 :version "21.1"
42 :group 'gnus)
43
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)))
55
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")))))))
215
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)
221
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)
226
227 (defcustom mail-source-directory "~/Mail/"
228 "Directory where files (if any) will be stored."
229 :group 'mail-source
230 :type 'directory)
231
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)
236
237 (defcustom mail-source-delete-incoming t
238 "*If non-nil, delete incoming files after handling."
239 :group 'mail-source
240 :type 'boolean)
241
242 (defcustom mail-source-incoming-file-prefix "Incoming"
243 "Prefix for file name for storing incoming mail"
244 :group 'mail-source
245 :type 'string)
246
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)
251
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)
256
257 ;;; Internal variables.
258
259 (defvar mail-source-string ""
260 "A dynamically bound string that says what the current mail source is.")
261
262 (defvar mail-source-new-mail-available nil
263 "Flag indicating when new mail is available.")
264
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.")
270
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."))
319
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.")
328
329 (defvar mail-source-password-cache nil)
330
331 (defvar mail-source-plugged t)
332
333 ;;; Functions
334
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))))
339
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)))
349
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.
357
358 After this is done, BODY will be executed in the scope
359 of the `let' form.
360
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))
366
367 (put 'mail-source-bind 'lisp-indent-function 1)
368 (put 'mail-source-bind 'edebug-form-spec '(form body))
369
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)))))))
379
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)))
389
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))))))))
402
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))
409
410 (put 'mail-source-bind-common 'lisp-indent-function 1)
411 (put 'mail-source-bind-common 'edebug-form-spec '(form body))
412
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.
424 (t
425 value)))
426
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))))))))
452
453 (defun mail-source-make-complex-temp-name (prefix)
454 (let ((newname (make-temp-name prefix))
455 (newprefix prefix))
456 (while (file-exists-p newname)
457 (setq newprefix (concat newprefix "x"))
458 (setq newname (make-temp-name newprefix)))
459 newname))
460
461 (defun mail-source-callback (callback info)
462 "Call CALLBACK on the mail file, and then remove the mail file.
463 Pass INFO on to CALLBACK."
464 (if (or (not (file-exists-p mail-source-crash-box))
465 (zerop (nth 7 (file-attributes mail-source-crash-box))))
466 (progn
467 (when (file-exists-p mail-source-crash-box)
468 (delete-file mail-source-crash-box))
469 0)
470 (prog1
471 (funcall callback mail-source-crash-box info)
472 (when (file-exists-p mail-source-crash-box)
473 ;; Delete or move the incoming mail out of the way.
474 (if mail-source-delete-incoming
475 (delete-file mail-source-crash-box)
476 (let ((incoming
477 (mail-source-make-complex-temp-name
478 (expand-file-name
479 mail-source-incoming-file-prefix
480 mail-source-directory))))
481 (unless (file-exists-p (file-name-directory incoming))
482 (make-directory (file-name-directory incoming) t))
483 (rename-file mail-source-crash-box incoming t)))))))
484
485 (defun mail-source-movemail (from to)
486 "Move FROM to TO using movemail."
487 (if (not (file-writable-p to))
488 (error "Can't write to crash box %s. Not moving mail" to)
489 (let ((to (file-truename (expand-file-name to)))
490 errors result)
491 (setq to (file-truename to)
492 from (file-truename from))
493 ;; Set TO if have not already done so, and rename or copy
494 ;; the file FROM to TO if and as appropriate.
495 (cond
496 ((file-exists-p to)
497 ;; The crash box exists already.
498 t)
499 ((not (file-exists-p from))
500 ;; There is no inbox.
501 (setq to nil))
502 ((zerop (nth 7 (file-attributes from)))
503 ;; Empty file.
504 (setq to nil))
505 (t
506 ;; If getting from mail spool directory, use movemail to move
507 ;; rather than just renaming, so as to interlock with the
508 ;; mailer.
509 (unwind-protect
510 (save-excursion
511 (setq errors (generate-new-buffer " *mail source loss*"))
512 (let ((default-directory "/"))
513 (setq result
514 (apply
515 'call-process
516 (append
517 (list
518 (expand-file-name "movemail" exec-directory)
519 nil errors nil from to)))))
520 (when (file-exists-p to)
521 (set-file-modes to mail-source-default-file-modes))
522 (if (and (not (buffer-modified-p errors))
523 (zerop result))
524 ;; No output => movemail won.
525 t
526 (set-buffer errors)
527 ;; There may be a warning about older revisions. We
528 ;; ignore that.
529 (goto-char (point-min))
530 (if (search-forward "older revision" nil t)
531 t
532 ;; Probably a real error.
533 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
534 (goto-char (point-max))
535 (skip-chars-backward " \t")
536 (delete-region (point) (point-max))
537 (goto-char (point-min))
538 (when (looking-at "movemail: ")
539 (delete-region (point-min) (match-end 0)))
540 (unless (yes-or-no-p
541 (format "movemail: %s (%d return). Continue? "
542 (buffer-string) result))
543 (error "%s" (buffer-string)))
544 (setq to nil)))))))
545 (when (and errors
546 (buffer-name errors))
547 (kill-buffer errors))
548 ;; Return whether we moved successfully or not.
549 to)))
550
551 (defun mail-source-movemail-and-remove (from to)
552 "Move FROM to TO using movemail, then remove FROM if empty."
553 (or (not (mail-source-movemail from to))
554 (not (zerop (nth 7 (file-attributes from))))
555 (delete-file from)))
556
557 (defvar mail-source-read-passwd nil)
558 (defun mail-source-read-passwd (prompt &rest args)
559 "Read a password using PROMPT.
560 If ARGS, PROMPT is used as an argument to `format'."
561 (let ((prompt
562 (if args
563 (apply 'format prompt args)
564 prompt)))
565 (unless mail-source-read-passwd
566 (if (or (fboundp 'read-passwd) (load "passwd" t))
567 (setq mail-source-read-passwd 'read-passwd)
568 (unless (fboundp 'ange-ftp-read-passwd)
569 (autoload 'ange-ftp-read-passwd "ange-ftp"))
570 (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
571 (funcall mail-source-read-passwd prompt)))
572
573 (defun mail-source-fetch-with-program (program)
574 (zerop (call-process shell-file-name nil nil nil
575 shell-command-switch program)))
576
577 (defun mail-source-run-script (script spec &optional delay)
578 (when script
579 (if (and (symbolp script) (fboundp script))
580 (funcall script)
581 (mail-source-call-script
582 (format-spec script spec))))
583 (when delay
584 (sleep-for delay)))
585
586 (defun mail-source-call-script (script)
587 (let ((background nil))
588 (when (string-match "& *$" script)
589 (setq script (substring script 0 (match-beginning 0))
590 background 0))
591 (call-process shell-file-name nil background nil
592 shell-command-switch script)))
593
594 ;;;
595 ;;; Different fetchers
596 ;;;
597
598 (defun mail-source-fetch-file (source callback)
599 "Fetcher for single-file sources."
600 (mail-source-bind (file source)
601 (mail-source-run-script
602 prescript (format-spec-make ?t mail-source-crash-box)
603 prescript-delay)
604 (let ((mail-source-string (format "file:%s" path)))
605 (if (mail-source-movemail path mail-source-crash-box)
606 (prog1
607 (mail-source-callback callback path)
608 (mail-source-run-script
609 postscript (format-spec-make ?t mail-source-crash-box)))
610 0))))
611
612 (defun mail-source-fetch-directory (source callback)
613 "Fetcher for directory sources."
614 (mail-source-bind (directory source)
615 (mail-source-run-script
616 prescript (format-spec-make ?t path)
617 prescript-delay)
618 (let ((found 0)
619 (mail-source-string (format "directory:%s" path)))
620 (dolist (file (directory-files
621 path t (concat (regexp-quote suffix) "$")))
622 (when (and (file-regular-p file)
623 (funcall predicate file)
624 (mail-source-movemail file mail-source-crash-box))
625 (incf found (mail-source-callback callback file))))
626 (mail-source-run-script
627 postscript (format-spec-make ?t path))
628 found)))
629
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
634 prescript
635 (format-spec-make ?p password ?t mail-source-crash-box
636 ?s server ?P port ?u user)
637 prescript-delay)
638 (let ((from (format "%s:%s:%s" server user port))
639 (mail-source-string (format "pop:%s@%s" user server))
640 result)
641 (when (eq authentication 'password)
642 (setq password
643 (or password
644 (cdr (assoc from mail-source-password-cache))
645 (mail-source-read-passwd
646 (format "Password for %s at %s: " user server)))))
647 (when server
648 (setenv "MAILHOST" server))
649 (setq result
650 (cond
651 (program
652 (mail-source-fetch-with-program
653 (format-spec
654 program
655 (format-spec-make ?p password ?t mail-source-crash-box
656 ?s server ?P port ?u user))))
657 (function
658 (funcall function mail-source-crash-box))
659 ;; The default is to use pop3.el.
660 (t
661 (let ((pop3-password password)
662 (pop3-maildrop user)
663 (pop3-mailhost server)
664 (pop3-port port)
665 (pop3-authentication-scheme
666 (if (eq authentication 'apop) 'apop 'pass)))
667 (save-excursion (pop3-movemail mail-source-crash-box))))))
668 (if result
669 (progn
670 (when (eq authentication 'password)
671 (unless (assoc from mail-source-password-cache)
672 (push (cons from password) mail-source-password-cache)))
673 (prog1
674 (mail-source-callback callback server)
675 ;; Update display-time's mail flag, if relevant.
676 (if (equal source mail-source-primary-source)
677 (setq mail-source-new-mail-available nil))
678 (mail-source-run-script
679 postscript
680 (format-spec-make ?p password ?t mail-source-crash-box
681 ?s server ?P port ?u user))))
682 ;; We nix out the password in case the error
683 ;; was because of a wrong password being given.
684 (setq mail-source-password-cache
685 (delq (assoc from mail-source-password-cache)
686 mail-source-password-cache))
687 0))))
688
689 (defun mail-source-check-pop (source)
690 "Check whether there is new mail."
691 (mail-source-bind (pop source)
692 (let ((from (format "%s:%s:%s" server user port))
693 (mail-source-string (format "pop:%s@%s" user server))
694 result)
695 (when (eq authentication 'password)
696 (setq password
697 (or password
698 (cdr (assoc from mail-source-password-cache))
699 (mail-source-read-passwd
700 (format "Password for %s at %s: " user server))))
701 (unless (assoc from mail-source-password-cache)
702 (push (cons from password) mail-source-password-cache)))
703 (when server
704 (setenv "MAILHOST" server))
705 (setq result
706 (cond
707 ;; No easy way to check whether mail is waiting for these.
708 (program)
709 (function)
710 ;; The default is to use pop3.el.
711 (t
712 (let ((pop3-password password)
713 (pop3-maildrop user)
714 (pop3-mailhost server)
715 (pop3-port port)
716 (pop3-authentication-scheme
717 (if (eq authentication 'apop) 'apop 'pass)))
718 (save-excursion (pop3-get-message-count))))))
719 (if result
720 ;; Inform display-time that we have new mail.
721 (setq mail-source-new-mail-available (> result 0))
722 ;; We nix out the password in case the error
723 ;; was because of a wrong password being given.
724 (setq mail-source-password-cache
725 (delq (assoc from mail-source-password-cache)
726 mail-source-password-cache)))
727 result)))
728
729 (defun mail-source-new-mail-p ()
730 "Handler for `display-time' to indicate when new mail is available."
731 ;; Only report flag setting; flag is updated on a different schedule.
732 mail-source-new-mail-available)
733
734
735 (defvar mail-source-report-new-mail nil)
736 (defvar mail-source-report-new-mail-timer nil)
737 (defvar mail-source-report-new-mail-idle-timer nil)
738
739 (eval-when-compile
740 (if (featurep 'xemacs)
741 (require 'itimer)
742 (require 'timer)))
743
744 (defun mail-source-start-idle-timer ()
745 ;; Start our idle timer if necessary, so we delay the check until the
746 ;; user isn't typing.
747 (unless mail-source-report-new-mail-idle-timer
748 (setq mail-source-report-new-mail-idle-timer
749 (run-with-idle-timer
750 mail-source-idle-time-delay
751 nil
752 (lambda ()
753 (setq mail-source-report-new-mail-idle-timer nil)
754 (mail-source-check-pop mail-source-primary-source))))
755 ;; Since idle timers created when Emacs is already in the idle
756 ;; state don't get activated until Emacs _next_ becomes idle, we
757 ;; need to force our timer to be considered active now. We do
758 ;; this by being naughty and poking the timer internals directly
759 ;; (element 0 of the vector is nil if the timer is active).
760 (aset mail-source-report-new-mail-idle-timer 0 nil)))
761
762 (defun mail-source-report-new-mail (arg)
763 "Toggle whether to report when new mail is available.
764 This only works when `display-time' is enabled."
765 (interactive "P")
766 (if (not mail-source-primary-source)
767 (error "Need to set `mail-source-primary-source' to check for new mail"))
768 (let ((on (if (null arg)
769 (not mail-source-report-new-mail)
770 (> (prefix-numeric-value arg) 0))))
771 (setq mail-source-report-new-mail on)
772 (and mail-source-report-new-mail-timer
773 (nnheader-cancel-timer mail-source-report-new-mail-timer))
774 (and mail-source-report-new-mail-idle-timer
775 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
776 (setq mail-source-report-new-mail-timer nil)
777 (setq mail-source-report-new-mail-idle-timer nil)
778 (if on
779 (progn
780 (require 'time)
781 ;; display-time-mail-function is an Emacs 21 feature.
782 (setq display-time-mail-function #'mail-source-new-mail-p)
783 ;; Set up the main timer.
784 (setq mail-source-report-new-mail-timer
785 (run-at-time t (* 60 mail-source-report-new-mail-interval)
786 #'mail-source-start-idle-timer))
787 ;; When you get new mail, clear "Mail" from the mode line.
788 (add-hook 'nnmail-post-get-new-mail-hook
789 'display-time-event-handler)
790 (message "Mail check enabled"))
791 (setq display-time-mail-function nil)
792 (remove-hook 'nnmail-post-get-new-mail-hook
793 'display-time-event-handler)
794 (message "Mail check disabled"))))
795
796 (defun mail-source-fetch-maildir (source callback)
797 "Fetcher for maildir sources."
798 (mail-source-bind (maildir source)
799 (let ((found 0)
800 mail-source-string)
801 (unless (string-match "/$" path)
802 (setq path (concat path "/")))
803 (dolist (subdir subdirs)
804 (when (file-directory-p (concat path subdir))
805 (setq mail-source-string (format "maildir:%s%s" path subdir))
806 (dolist (file (directory-files (concat path subdir) t))
807 (when (and (not (file-directory-p file))
808 (not (if function
809 (funcall function file mail-source-crash-box)
810 (let ((coding-system-for-write
811 mm-text-coding-system)
812 (coding-system-for-read
813 mm-text-coding-system))
814 (with-temp-file mail-source-crash-box
815 (insert-file-contents file)
816 (goto-char (point-min))
817 ;;; ;; Unix mail format
818 ;;; (unless (looking-at "\n*From ")
819 ;;; (insert "From maildir "
820 ;;; (current-time-string) "\n"))
821 ;;; (while (re-search-forward "^From " nil t)
822 ;;; (replace-match ">From "))
823 ;;; (goto-char (point-max))
824 ;;; (insert "\n\n")
825 ;; MMDF mail format
826 (insert "\001\001\001\001\n"))
827 (delete-file file)))))
828 (incf found (mail-source-callback callback file))))))
829 found)))
830
831 (eval-and-compile
832 (autoload 'imap-open "imap")
833 (autoload 'imap-authenticate "imap")
834 (autoload 'imap-mailbox-select "imap")
835 (autoload 'imap-mailbox-unselect "imap")
836 (autoload 'imap-mailbox-close "imap")
837 (autoload 'imap-search "imap")
838 (autoload 'imap-fetch "imap")
839 (autoload 'imap-close "imap")
840 (autoload 'imap-error-text "imap")
841 (autoload 'imap-message-flags-add "imap")
842 (autoload 'imap-list-to-message-set "imap")
843 (autoload 'imap-range-to-message-set "imap")
844 (autoload 'nnheader-ms-strip-cr "nnheader"))
845
846 (defvar mail-source-imap-file-coding-system 'binary
847 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
848
849 (defun mail-source-fetch-imap (source callback)
850 "Fetcher for imap sources."
851 (mail-source-bind (imap source)
852 (let ((from (format "%s:%s:%s" server user port))
853 (found 0)
854 (buf (get-buffer-create (generate-new-buffer-name " *imap source*")))
855 (mail-source-string (format "imap:%s:%s" server mailbox))
856 remove)
857 (if (and (imap-open server port stream authentication buf)
858 (imap-authenticate
859 user (or (cdr (assoc from mail-source-password-cache))
860 password) buf)
861 (imap-mailbox-select mailbox nil buf))
862 (let ((coding-system-for-write mail-source-imap-file-coding-system)
863 str)
864 (with-temp-file mail-source-crash-box
865 ;; Avoid converting 8-bit chars from inserted strings to
866 ;; multibyte.
867 (mm-disable-multibyte)
868 ;; remember password
869 (with-current-buffer buf
870 (when (or imap-password
871 (assoc from mail-source-password-cache))
872 (push (cons from imap-password) mail-source-password-cache)))
873 ;; if predicate is nil, use all uids
874 (dolist (uid (imap-search (or predicate "1:*") buf))
875 (when (setq str (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))
876 (push uid remove)
877 (insert "From imap " (current-time-string) "\n")
878 (save-excursion
879 (insert str "\n\n"))
880 (while (re-search-forward "^From " nil t)
881 (replace-match ">From "))
882 (goto-char (point-max))))
883 (nnheader-ms-strip-cr))
884 (incf found (mail-source-callback callback server))
885 (when (and remove fetchflag)
886 (imap-message-flags-add
887 (imap-range-to-message-set (gnus-compress-sequence remove))
888 fetchflag nil buf))
889 (if dontexpunge
890 (imap-mailbox-unselect buf)
891 (imap-mailbox-close buf))
892 (imap-close buf))
893 (imap-close buf)
894 ;; We nix out the password in case the error
895 ;; was because of a wrong password being given.
896 (setq mail-source-password-cache
897 (delq (assoc from mail-source-password-cache)
898 mail-source-password-cache))
899 (error (imap-error-text buf)))
900 (kill-buffer buf)
901 found)))
902
903 (eval-and-compile
904 (autoload 'webmail-fetch "webmail"))
905
906 (defun mail-source-fetch-webmail (source callback)
907 "Fetch for webmail source."
908 (mail-source-bind (webmail source)
909 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
910 (webmail-newmail-only dontexpunge)
911 (webmail-move-to-trash-can (not dontexpunge)))
912 (when (eq authentication 'password)
913 (setq password
914 (or password
915 (cdr (assoc (format "webmail:%s:%s" subtype user)
916 mail-source-password-cache))
917 (mail-source-read-passwd
918 (format "Password for %s at %s: " user subtype))))
919 (when (and password
920 (not (assoc (format "webmail:%s:%s" subtype user)
921 mail-source-password-cache)))
922 (push (cons (format "webmail:%s:%s" subtype user) password)
923 mail-source-password-cache)))
924 (webmail-fetch mail-source-crash-box subtype user password)
925 (mail-source-callback callback (symbol-name subtype)))))
926
927 (provide 'mail-source)
928
929 ;;; mail-source.el ends here