*** empty log message ***
[bpt/emacs.git] / lisp / gnus / mail-source.el
CommitLineData
c113de23 1;;; mail-source.el --- functions for fetching mail
e84b4b86
TTN
2
3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
c113de23
GM
5
6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7;; Keywords: news, mail
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
c113de23
GM
25
26;;; Commentary:
27
28;;; Code:
29
4f926b3e
DL
30(eval-when-compile
31 (require 'cl)
32 (require 'imap)
33 (eval-when-compile (defvar display-time-mail-function)))
c113de23
GM
34(eval-and-compile
35 (autoload 'pop3-movemail "pop3")
4f926b3e 36 (autoload 'pop3-get-message-count "pop3")
23f87bed
MB
37 (autoload 'nnheader-cancel-timer "nnheader")
38 (autoload 'nnheader-run-at-time "nnheader"))
c113de23 39(require 'format-spec)
4f926b3e 40(require 'mm-util)
23f87bed 41(require 'message) ;; for `message-directory'
c113de23
GM
42
43(defgroup mail-source nil
44 "The mail-fetching library."
ce9401f3 45 :version "21.1"
c113de23
GM
46 :group 'gnus)
47
4f926b3e
DL
48;; Define these at compile time to avoid dragging in imap always.
49(defconst mail-source-imap-authenticators
50 (eval-when-compile
51 (mapcar (lambda (a)
52 (list 'const (car a)))
53 imap-authenticator-alist)))
54(defconst mail-source-imap-streams
55 (eval-when-compile
56 (mapcar (lambda (a)
57 (list 'const (car a)))
58 imap-stream-alist)))
59
72fc0418 60(defcustom mail-sources nil
c113de23 61 "*Where the mail backends will look for incoming mail.
ce9401f3
DL
62This variable is a list of mail source specifiers.
63See Info node `(gnus)Mail Source Specifiers'."
c113de23 64 :group 'mail-source
23f87bed 65 :link '(custom-manual "(gnus)Mail Source Specifiers")
ce9401f3 66 :type `(repeat
4f926b3e
DL
67 (choice :format "%[Value Menu%] %v"
68 :value (file)
69 (cons :tag "Spool file"
70 (const :format "" file)
71 (checklist :tag "Options" :greedy t
72 (group :inline t
73 (const :format "" :value :path)
74 file)))
ce9401f3 75 (cons :tag "Several files in a directory"
4f926b3e
DL
76 (const :format "" directory)
77 (checklist :tag "Options" :greedy t
78 (group :inline t
79 (const :format "" :value :path)
80 (directory :tag "Path"))
81 (group :inline t
82 (const :format "" :value :suffix)
83 (string :tag "Suffix"))
84 (group :inline t
85 (const :format "" :value :predicate)
86 (function :tag "Predicate"))
87 (group :inline t
88 (const :format "" :value :prescript)
23f87bed
MB
89 (choice :tag "Prescript"
90 :value nil
91 (string :format "%v")
92 (function :format "%v")))
4f926b3e
DL
93 (group :inline t
94 (const :format "" :value :postscript)
23f87bed
MB
95 (choice :tag "Postscript"
96 :value nil
97 (string :format "%v")
98 (function :format "%v")))
4f926b3e
DL
99 (group :inline t
100 (const :format "" :value :plugged)
101 (boolean :tag "Plugged"))))
ce9401f3 102 (cons :tag "POP3 server"
4f926b3e
DL
103 (const :format "" pop)
104 (checklist :tag "Options" :greedy t
105 (group :inline t
a1506d29 106 (const :format "" :value :server)
4f926b3e
DL
107 (string :tag "Server"))
108 (group :inline t
a1506d29 109 (const :format "" :value :port)
4f926b3e 110 (choice :tag "Port"
a1506d29 111 :value "pop3"
4f926b3e
DL
112 (number :format "%v")
113 (string :format "%v")))
114 (group :inline t
115 (const :format "" :value :user)
116 (string :tag "User"))
117 (group :inline t
118 (const :format "" :value :password)
119 (string :tag "Password"))
120 (group :inline t
121 (const :format "" :value :program)
122 (string :tag "Program"))
123 (group :inline t
124 (const :format "" :value :prescript)
23f87bed
MB
125 (choice :tag "Prescript"
126 :value nil
127 (string :format "%v")
128 (function :format "%v")))
4f926b3e
DL
129 (group :inline t
130 (const :format "" :value :postscript)
23f87bed
MB
131 (choice :tag "Postscript"
132 :value nil
133 (string :format "%v")
134 (function :format "%v")))
4f926b3e
DL
135 (group :inline t
136 (const :format "" :value :function)
137 (function :tag "Function"))
138 (group :inline t
a1506d29 139 (const :format ""
4f926b3e
DL
140 :value :authentication)
141 (choice :tag "Authentication"
142 :value apop
143 (const password)
144 (const apop)))
145 (group :inline t
146 (const :format "" :value :plugged)
147 (boolean :tag "Plugged"))))
ce9401f3 148 (cons :tag "Maildir (qmail, postfix...)"
4f926b3e
DL
149 (const :format "" maildir)
150 (checklist :tag "Options" :greedy t
151 (group :inline t
152 (const :format "" :value :path)
153 (directory :tag "Path"))
154 (group :inline t
155 (const :format "" :value :plugged)
156 (boolean :tag "Plugged"))))
ce9401f3 157 (cons :tag "IMAP server"
4f926b3e
DL
158 (const :format "" imap)
159 (checklist :tag "Options" :greedy t
160 (group :inline t
161 (const :format "" :value :server)
162 (string :tag "Server"))
163 (group :inline t
164 (const :format "" :value :port)
a1506d29
JB
165 (choice :tag "Port"
166 :value 143
4f926b3e
DL
167 number string))
168 (group :inline t
169 (const :format "" :value :user)
170 (string :tag "User"))
171 (group :inline t
172 (const :format "" :value :password)
173 (string :tag "Password"))
174 (group :inline t
175 (const :format "" :value :stream)
176 (choice :tag "Stream"
177 :value network
178 ,@mail-source-imap-streams))
23f87bed
MB
179 (group :inline t
180 (const :format "" :value :program)
181 (string :tag "Program"))
4f926b3e
DL
182 (group :inline t
183 (const :format ""
184 :value :authenticator)
185 (choice :tag "Authenticator"
186 :value login
187 ,@mail-source-imap-authenticators))
188 (group :inline t
189 (const :format "" :value :mailbox)
5dd7fd91
DL
190 (string :tag "Mailbox"
191 :value "INBOX"))
4f926b3e
DL
192 (group :inline t
193 (const :format "" :value :predicate)
a1506d29 194 (string :tag "Predicate"
5dd7fd91 195 :value "UNSEEN UNDELETED"))
4f926b3e
DL
196 (group :inline t
197 (const :format "" :value :fetchflag)
5dd7fd91
DL
198 (string :tag "Fetchflag"
199 :value "\\Deleted"))
4f926b3e
DL
200 (group :inline t
201 (const :format ""
202 :value :dontexpunge)
203 (boolean :tag "Dontexpunge"))
204 (group :inline t
205 (const :format "" :value :plugged)
206 (boolean :tag "Plugged"))))
ce9401f3 207 (cons :tag "Webmail server"
4f926b3e
DL
208 (const :format "" webmail)
209 (checklist :tag "Options" :greedy t
a1506d29 210 (group :inline t
4f926b3e
DL
211 (const :format "" :value :subtype)
212 ;; Should be generated from
213 ;; `webmail-type-definition', but we
214 ;; can't require webmail without W3.
215 (choice :tag "Subtype"
216 :value hotmail
217 (const hotmail)
218 (const yahoo)
219 (const netaddress)
220 (const netscape)
221 (const my-deja)))
222 (group :inline t
223 (const :format "" :value :user)
224 (string :tag "User"))
225 (group :inline t
226 (const :format "" :value :password)
227 (string :tag "Password"))
228 (group :inline t
229 (const :format ""
230 :value :dontexpunge)
231 (boolean :tag "Dontexpunge"))
232 (group :inline t
233 (const :format "" :value :plugged)
234 (boolean :tag "Plugged")))))))
c113de23 235
23f87bed
MB
236(defcustom mail-source-ignore-errors nil
237 "*Ignore errors when querying mail sources.
238If nil, the user will be prompted when an error occurs. If non-nil,
a08b59c9 239the error will be ignored."
bf247b6e 240 :version "22.1"
a08b59c9
MB
241 :group 'mail-source
242 :type 'boolean)
23f87bed 243
c113de23
GM
244(defcustom mail-source-primary-source nil
245 "*Primary source for incoming mail.
246If non-nil, this maildrop will be checked periodically for new mail."
247 :group 'mail-source
248 :type 'sexp)
249
23f87bed
MB
250(defcustom mail-source-flash t
251 "*If non-nil, flash periodically when mail is available."
252 :group 'mail-source
253 :type 'boolean)
254
c113de23
GM
255(defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
256 "File where mail will be stored while processing it."
257 :group 'mail-source
258 :type 'file)
259
23f87bed 260(defcustom mail-source-directory message-directory
531e5812 261 "Directory where incoming mail source files (if any) will be stored."
c113de23
GM
262 :group 'mail-source
263 :type 'directory)
264
265(defcustom mail-source-default-file-modes 384
266 "Set the mode bits of all new mail files to this integer."
267 :group 'mail-source
268 :type 'integer)
269
805b7fc0 270(defcustom mail-source-delete-incoming t
23f87bed
MB
271 "*If non-nil, delete incoming files after handling.
272If t, delete immediately, if nil, never delete. If a positive number, delete
273files older than number of days."
274 ;; Note: The removing happens in `mail-source-callback', i.e. no old
275 ;; incoming files will be deleted, unless you receive new mail.
276 ;;
277 ;; You may also set this to `nil' and call `mail-source-delete-old-incoming'
278 ;; from a hook or interactively.
279 :group 'mail-source
280 :type '(choice (const :tag "immediately" t)
281 (const :tag "never" nil)
282 (integer :tag "days")))
283
284(defcustom mail-source-delete-old-incoming-confirm t
285 "*If non-nil, ask for for confirmation before deleting old incoming files.
286This variable only applies when `mail-source-delete-incoming' is a positive
287number."
bf247b6e 288 :version "22.1"
c113de23
GM
289 :group 'mail-source
290 :type 'boolean)
291
292(defcustom mail-source-incoming-file-prefix "Incoming"
293 "Prefix for file name for storing incoming mail"
294 :group 'mail-source
295 :type 'string)
296
297(defcustom mail-source-report-new-mail-interval 5
298 "Interval in minutes between checks for new mail."
299 :group 'mail-source
300 :type 'number)
301
302(defcustom mail-source-idle-time-delay 5
303 "Number of idle seconds to wait before checking for new mail."
304 :group 'mail-source
305 :type 'number)
306
23f87bed
MB
307(defcustom mail-source-movemail-program nil
308 "If non-nil, name of program for fetching new mail."
bf247b6e 309 :version "22.1"
23f87bed
MB
310 :group 'mail-source
311 :type '(choice (const nil) string))
312
c113de23
GM
313;;; Internal variables.
314
315(defvar mail-source-string ""
316 "A dynamically bound string that says what the current mail source is.")
317
318(defvar mail-source-new-mail-available nil
319 "Flag indicating when new mail is available.")
320
321(eval-and-compile
322 (defvar mail-source-common-keyword-map
323 '((:plugged))
324 "Mapping from keywords to default values.
325Common keywords should be listed here.")
326
327 (defvar mail-source-keyword-map
328 '((file
329 (:prescript)
330 (:prescript-delay)
331 (:postscript)
332 (:path (or (getenv "MAIL")
4f926b3e 333 (expand-file-name (user-login-name) rmail-spool-directory))))
c113de23 334 (directory
cf92160d
SZ
335 (:prescript)
336 (:prescript-delay)
337 (:postscript)
c113de23
GM
338 (:path)
339 (:suffix ".spool")
340 (:predicate identity))
341 (pop
342 (:prescript)
343 (:prescript-delay)
344 (:postscript)
345 (:server (getenv "MAILHOST"))
346 (:port 110)
347 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
348 (:program)
349 (:function)
350 (:password)
351 (:authentication password))
352 (maildir
353 (:path (or (getenv "MAILDIR") "~/Maildir/"))
23f87bed 354 (:subdirs ("cur" "new"))
c113de23
GM
355 (:function))
356 (imap
357 (:server (getenv "MAILHOST"))
358 (:port)
359 (:stream)
23f87bed 360 (:program)
c113de23
GM
361 (:authentication)
362 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
363 (:password)
364 (:mailbox "INBOX")
365 (:predicate "UNSEEN UNDELETED")
366 (:fetchflag "\\Deleted")
23f87bed
MB
367 (:prescript)
368 (:prescript-delay)
369 (:postscript)
c113de23
GM
370 (:dontexpunge))
371 (webmail
372 (:subtype hotmail)
373 (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
374 (:password)
375 (:dontexpunge)
376 (:authentication password)))
377 "Mapping from keywords to default values.
378All keywords that can be used must be listed here."))
379
380(defvar mail-source-fetcher-alist
381 '((file mail-source-fetch-file)
382 (directory mail-source-fetch-directory)
383 (pop mail-source-fetch-pop)
384 (maildir mail-source-fetch-maildir)
385 (imap mail-source-fetch-imap)
386 (webmail mail-source-fetch-webmail))
387 "A mapping from source type to fetcher function.")
388
389(defvar mail-source-password-cache nil)
390
391(defvar mail-source-plugged t)
392
393;;; Functions
394
395(eval-and-compile
396 (defun mail-source-strip-keyword (keyword)
397 "Strip the leading colon off the KEYWORD."
398 (intern (substring (symbol-name keyword) 1))))
399
400(eval-and-compile
401 (defun mail-source-bind-1 (type)
402 (let* ((defaults (cdr (assq type mail-source-keyword-map)))
403 default bind)
404 (while (setq default (pop defaults))
405 (push (list (mail-source-strip-keyword (car default))
406 nil)
407 bind))
408 bind)))
409
410(defmacro mail-source-bind (type-source &rest body)
411 "Return a `let' form that binds all variables in source TYPE.
412TYPE-SOURCE is a list where the first element is the TYPE, and
413the second variable is the SOURCE.
414At run time, the mail source specifier SOURCE will be inspected,
415and the variables will be set according to it. Variables not
416specified will be given default values.
417
418After this is done, BODY will be executed in the scope
419of the `let' form.
420
421The variables bound and their default values are described by
422the `mail-source-keyword-map' variable."
423 `(let ,(mail-source-bind-1 (car type-source))
424 (mail-source-set-1 ,(cadr type-source))
425 ,@body))
426
427(put 'mail-source-bind 'lisp-indent-function 1)
23f87bed 428(put 'mail-source-bind 'edebug-form-spec '(sexp body))
c113de23
GM
429
430(defun mail-source-set-1 (source)
431 (let* ((type (pop source))
432 (defaults (cdr (assq type mail-source-keyword-map)))
433 default value keyword)
434 (while (setq default (pop defaults))
435 (set (mail-source-strip-keyword (setq keyword (car default)))
436 (if (setq value (plist-get source keyword))
437 (mail-source-value value)
438 (mail-source-value (cadr default)))))))
439
440(eval-and-compile
441 (defun mail-source-bind-common-1 ()
442 (let* ((defaults mail-source-common-keyword-map)
443 default bind)
444 (while (setq default (pop defaults))
445 (push (list (mail-source-strip-keyword (car default))
446 nil)
447 bind))
448 bind)))
449
450(defun mail-source-set-common-1 (source)
451 (let* ((type (pop source))
452 (defaults mail-source-common-keyword-map)
453 (defaults-1 (cdr (assq type mail-source-keyword-map)))
454 default value keyword)
455 (while (setq default (pop defaults))
456 (set (mail-source-strip-keyword (setq keyword (car default)))
457 (if (setq value (plist-get source keyword))
458 (mail-source-value value)
459 (if (setq value (assq keyword defaults-1))
460 (mail-source-value (cadr value))
461 (mail-source-value (cadr default))))))))
462
463(defmacro mail-source-bind-common (source &rest body)
464 "Return a `let' form that binds all common variables.
465See `mail-source-bind'."
466 `(let ,(mail-source-bind-common-1)
467 (mail-source-set-common-1 source)
468 ,@body))
469
470(put 'mail-source-bind-common 'lisp-indent-function 1)
23f87bed 471(put 'mail-source-bind-common 'edebug-form-spec '(sexp body))
c113de23
GM
472
473(defun mail-source-value (value)
474 "Return the value of VALUE."
475 (cond
476 ;; String
477 ((stringp value)
478 value)
479 ;; Function
480 ((and (listp value)
481 (functionp (car value)))
482 (eval value))
483 ;; Just return the value.
484 (t
485 value)))
486
487(defun mail-source-fetch (source callback)
488 "Fetch mail from SOURCE and call CALLBACK zero or more times.
489CALLBACK will be called with the name of the file where (some of)
490the mail from SOURCE is put.
491Return the number of files that were found."
492 (mail-source-bind-common source
493 (if (or mail-source-plugged plugged)
494 (save-excursion
495 (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
496 (found 0))
497 (unless function
498 (error "%S is an invalid mail source specification" source))
499 ;; If there's anything in the crash box, we do it first.
500 (when (file-exists-p mail-source-crash-box)
501 (message "Processing mail from %s..." mail-source-crash-box)
502 (setq found (mail-source-callback
503 callback mail-source-crash-box)))
504 (+ found
23f87bed 505 (if (or debug-on-quit debug-on-error)
c113de23 506 (funcall function source callback)
23f87bed
MB
507 (condition-case err
508 (funcall function source callback)
509 (error
510 (if (and (not mail-source-ignore-errors)
511 (not
512 (yes-or-no-p
513 (format "Mail source %s error (%s). Continue? "
514 (if (memq ':password source)
515 (let ((s (copy-sequence source)))
bf247b6e 516 (setcar (cdr (memq ':password s))
23f87bed
MB
517 "********")
518 s)
519 source)
520 (cadr err)))))
521 (error "Cannot get new mail"))
522 0)))))))))
523
524(defun mail-source-delete-old-incoming (&optional age confirm)
525 "Remove incoming files older than AGE days.
526If CONFIRM is non-nil, ask for confirmation before removing a file."
527 (interactive "P")
528 (let* ((high2days (/ 65536.0 60 60 24));; convert high bits to days
529 (low2days (/ 1.0 65536.0)) ;; convert low bits to days
530 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
531 currday files)
532 (setq files (directory-files
533 mail-source-directory t
534 (concat mail-source-incoming-file-prefix "*"))
535 currday (* (car (current-time)) high2days)
536 currday (+ currday (* low2days (nth 1 (current-time)))))
537 (while files
538 (let* ((ffile (car files))
539 (bfile (gnus-replace-in-string
540 ffile "\\`.*/\\([^/]+\\)\\'" "\\1"))
541 (filetime (nth 5 (file-attributes ffile)))
542 (fileday (* (car filetime) high2days))
543 (fileday (+ fileday (* low2days (nth 1 filetime)))))
544 (setq files (cdr files))
545 (when (and (> (- currday fileday) diff)
546 (gnus-message 8 "File `%s' is older than %s day(s)"
547 bfile diff)
548 (or (not confirm)
549 (y-or-n-p (concat "Remove file `" bfile "'? "))))
550 (delete-file ffile))))))
c113de23
GM
551
552(defun mail-source-callback (callback info)
553 "Call CALLBACK on the mail file, and then remove the mail file.
554Pass INFO on to CALLBACK."
555 (if (or (not (file-exists-p mail-source-crash-box))
556 (zerop (nth 7 (file-attributes mail-source-crash-box))))
557 (progn
558 (when (file-exists-p mail-source-crash-box)
559 (delete-file mail-source-crash-box))
560 0)
561 (prog1
562 (funcall callback mail-source-crash-box info)
563 (when (file-exists-p mail-source-crash-box)
564 ;; Delete or move the incoming mail out of the way.
23f87bed 565 (if (eq mail-source-delete-incoming t)
c113de23
GM
566 (delete-file mail-source-crash-box)
567 (let ((incoming
23f87bed 568 (mm-make-temp-file
c113de23
GM
569 (expand-file-name
570 mail-source-incoming-file-prefix
571 mail-source-directory))))
572 (unless (file-exists-p (file-name-directory incoming))
573 (make-directory (file-name-directory incoming) t))
23f87bed
MB
574 (rename-file mail-source-crash-box incoming t)
575 ;; remove old incoming files?
576 (when (natnump mail-source-delete-incoming)
577 (mail-source-delete-old-incoming
578 mail-source-delete-incoming
579 mail-source-delete-old-incoming-confirm))))))))
c113de23
GM
580
581(defun mail-source-movemail (from to)
582 "Move FROM to TO using movemail."
583 (if (not (file-writable-p to))
584 (error "Can't write to crash box %s. Not moving mail" to)
585 (let ((to (file-truename (expand-file-name to)))
586 errors result)
587 (setq to (file-truename to)
588 from (file-truename from))
589 ;; Set TO if have not already done so, and rename or copy
590 ;; the file FROM to TO if and as appropriate.
591 (cond
592 ((file-exists-p to)
593 ;; The crash box exists already.
594 t)
595 ((not (file-exists-p from))
596 ;; There is no inbox.
597 (setq to nil))
598 ((zerop (nth 7 (file-attributes from)))
599 ;; Empty file.
600 (setq to nil))
601 (t
602 ;; If getting from mail spool directory, use movemail to move
603 ;; rather than just renaming, so as to interlock with the
604 ;; mailer.
605 (unwind-protect
606 (save-excursion
607 (setq errors (generate-new-buffer " *mail source loss*"))
608 (let ((default-directory "/"))
609 (setq result
610 (apply
611 'call-process
612 (append
613 (list
23f87bed
MB
614 (or mail-source-movemail-program
615 (expand-file-name "movemail" exec-directory))
c113de23
GM
616 nil errors nil from to)))))
617 (when (file-exists-p to)
618 (set-file-modes to mail-source-default-file-modes))
23f87bed
MB
619 (if (and (or (not (buffer-modified-p errors))
620 (zerop (buffer-size errors)))
621 (and (numberp result)
622 (zerop result)))
c113de23
GM
623 ;; No output => movemail won.
624 t
625 (set-buffer errors)
626 ;; There may be a warning about older revisions. We
627 ;; ignore that.
628 (goto-char (point-min))
629 (if (search-forward "older revision" nil t)
630 t
631 ;; Probably a real error.
632 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
633 (goto-char (point-max))
634 (skip-chars-backward " \t")
635 (delete-region (point) (point-max))
636 (goto-char (point-min))
637 (when (looking-at "movemail: ")
638 (delete-region (point-min) (match-end 0)))
23f87bed 639 ;; Result may be a signal description string.
c113de23 640 (unless (yes-or-no-p
23f87bed 641 (format "movemail: %s (%s return). Continue? "
c113de23
GM
642 (buffer-string) result))
643 (error "%s" (buffer-string)))
644 (setq to nil)))))))
645 (when (and errors
646 (buffer-name errors))
647 (kill-buffer errors))
648 ;; Return whether we moved successfully or not.
649 to)))
650
651(defun mail-source-movemail-and-remove (from to)
652 "Move FROM to TO using movemail, then remove FROM if empty."
653 (or (not (mail-source-movemail from to))
654 (not (zerop (nth 7 (file-attributes from))))
655 (delete-file from)))
656
c113de23 657(defun mail-source-fetch-with-program (program)
23f87bed
MB
658 (eq 0 (call-process shell-file-name nil nil nil
659 shell-command-switch program)))
c113de23
GM
660
661(defun mail-source-run-script (script spec &optional delay)
662 (when script
23f87bed 663 (if (functionp script)
c113de23
GM
664 (funcall script)
665 (mail-source-call-script
666 (format-spec script spec))))
667 (when delay
668 (sleep-for delay)))
669
670(defun mail-source-call-script (script)
671 (let ((background nil))
672 (when (string-match "& *$" script)
673 (setq script (substring script 0 (match-beginning 0))
674 background 0))
675 (call-process shell-file-name nil background nil
676 shell-command-switch script)))
677
678;;;
679;;; Different fetchers
680;;;
681
682(defun mail-source-fetch-file (source callback)
683 "Fetcher for single-file sources."
684 (mail-source-bind (file source)
685 (mail-source-run-script
686 prescript (format-spec-make ?t mail-source-crash-box)
687 prescript-delay)
688 (let ((mail-source-string (format "file:%s" path)))
689 (if (mail-source-movemail path mail-source-crash-box)
690 (prog1
691 (mail-source-callback callback path)
692 (mail-source-run-script
693 postscript (format-spec-make ?t mail-source-crash-box)))
694 0))))
695
696(defun mail-source-fetch-directory (source callback)
697 "Fetcher for directory sources."
698 (mail-source-bind (directory source)
35037882 699 (mail-source-run-script
23f87bed 700 prescript (format-spec-make ?t path) prescript-delay)
c113de23
GM
701 (let ((found 0)
702 (mail-source-string (format "directory:%s" path)))
703 (dolist (file (directory-files
704 path t (concat (regexp-quote suffix) "$")))
705 (when (and (file-regular-p file)
706 (funcall predicate file)
707 (mail-source-movemail file mail-source-crash-box))
708 (incf found (mail-source-callback callback file))))
23f87bed 709 (mail-source-run-script postscript (format-spec-make ?t path))
c113de23
GM
710 found)))
711
712(defun mail-source-fetch-pop (source callback)
713 "Fetcher for single-file sources."
714 (mail-source-bind (pop source)
715 (mail-source-run-script
716 prescript
717 (format-spec-make ?p password ?t mail-source-crash-box
718 ?s server ?P port ?u user)
719 prescript-delay)
720 (let ((from (format "%s:%s:%s" server user port))
721 (mail-source-string (format "pop:%s@%s" user server))
722 result)
723 (when (eq authentication 'password)
724 (setq password
725 (or password
726 (cdr (assoc from mail-source-password-cache))
23f87bed 727 (read-passwd
c113de23
GM
728 (format "Password for %s at %s: " user server)))))
729 (when server
730 (setenv "MAILHOST" server))
731 (setq result
732 (cond
733 (program
734 (mail-source-fetch-with-program
735 (format-spec
736 program
737 (format-spec-make ?p password ?t mail-source-crash-box
738 ?s server ?P port ?u user))))
739 (function
740 (funcall function mail-source-crash-box))
741 ;; The default is to use pop3.el.
742 (t
743 (let ((pop3-password password)
744 (pop3-maildrop user)
745 (pop3-mailhost server)
746 (pop3-port port)
747 (pop3-authentication-scheme
748 (if (eq authentication 'apop) 'apop 'pass)))
23f87bed
MB
749 (if (or debug-on-quit debug-on-error)
750 (save-excursion (pop3-movemail mail-source-crash-box))
751 (condition-case err
752 (save-excursion (pop3-movemail mail-source-crash-box))
753 (error
754 ;; We nix out the password in case the error
755 ;; was because of a wrong password being given.
756 (setq mail-source-password-cache
757 (delq (assoc from mail-source-password-cache)
758 mail-source-password-cache))
759 (signal (car err) (cdr err)))))))))
c113de23
GM
760 (if result
761 (progn
762 (when (eq authentication 'password)
763 (unless (assoc from mail-source-password-cache)
764 (push (cons from password) mail-source-password-cache)))
765 (prog1
766 (mail-source-callback callback server)
767 ;; Update display-time's mail flag, if relevant.
768 (if (equal source mail-source-primary-source)
769 (setq mail-source-new-mail-available nil))
770 (mail-source-run-script
771 postscript
772 (format-spec-make ?p password ?t mail-source-crash-box
773 ?s server ?P port ?u user))))
774 ;; We nix out the password in case the error
775 ;; was because of a wrong password being given.
776 (setq mail-source-password-cache
777 (delq (assoc from mail-source-password-cache)
778 mail-source-password-cache))
779 0))))
780
781(defun mail-source-check-pop (source)
782 "Check whether there is new mail."
783 (mail-source-bind (pop source)
784 (let ((from (format "%s:%s:%s" server user port))
785 (mail-source-string (format "pop:%s@%s" user server))
786 result)
787 (when (eq authentication 'password)
788 (setq password
789 (or password
790 (cdr (assoc from mail-source-password-cache))
23f87bed 791 (read-passwd
c113de23
GM
792 (format "Password for %s at %s: " user server))))
793 (unless (assoc from mail-source-password-cache)
794 (push (cons from password) mail-source-password-cache)))
795 (when server
796 (setenv "MAILHOST" server))
797 (setq result
798 (cond
799 ;; No easy way to check whether mail is waiting for these.
800 (program)
801 (function)
802 ;; The default is to use pop3.el.
803 (t
804 (let ((pop3-password password)
805 (pop3-maildrop user)
806 (pop3-mailhost server)
807 (pop3-port port)
808 (pop3-authentication-scheme
809 (if (eq authentication 'apop) 'apop 'pass)))
23f87bed
MB
810 (if (or debug-on-quit debug-on-error)
811 (save-excursion (pop3-get-message-count))
812 (condition-case err
813 (save-excursion (pop3-get-message-count))
814 (error
815 ;; We nix out the password in case the error
816 ;; was because of a wrong password being given.
817 (setq mail-source-password-cache
818 (delq (assoc from mail-source-password-cache)
819 mail-source-password-cache))
820 (signal (car err) (cdr err)))))))))
c113de23
GM
821 (if result
822 ;; Inform display-time that we have new mail.
823 (setq mail-source-new-mail-available (> result 0))
824 ;; We nix out the password in case the error
825 ;; was because of a wrong password being given.
826 (setq mail-source-password-cache
827 (delq (assoc from mail-source-password-cache)
828 mail-source-password-cache)))
829 result)))
830
23f87bed
MB
831(defun mail-source-touch-pop ()
832 "Open and close a POP connection shortly.
833POP server should be defined in `mail-source-primary-source' (which is
834preferred) or `mail-sources'. You may use it for the POP-before-SMTP
835authentication. To do that, you need to set the
836`message-send-mail-function' variable as `message-smtpmail-send-it'
837and put the following line in your ~/.gnus.el file:
838
839\(add-hook 'message-send-mail-hook 'mail-source-touch-pop)
840
841See the Gnus manual for details."
842 (let ((sources (if mail-source-primary-source
843 (list mail-source-primary-source)
844 mail-sources)))
845 (while sources
846 (if (eq 'pop (car (car sources)))
847 (mail-source-check-pop (car sources)))
848 (setq sources (cdr sources)))))
849
c113de23
GM
850(defun mail-source-new-mail-p ()
851 "Handler for `display-time' to indicate when new mail is available."
23f87bed
MB
852 ;; Flash (ie. ring the visible bell) if mail is available.
853 (if (and mail-source-flash mail-source-new-mail-available)
854 (let ((visible-bell t))
855 (ding)))
c113de23
GM
856 ;; Only report flag setting; flag is updated on a different schedule.
857 mail-source-new-mail-available)
858
859
860(defvar mail-source-report-new-mail nil)
861(defvar mail-source-report-new-mail-timer nil)
862(defvar mail-source-report-new-mail-idle-timer nil)
863
a1506d29 864(eval-when-compile
126cbb42 865 (if (featurep 'xemacs)
fa9a04e1 866 (require 'timer-funcs)
126cbb42 867 (require 'timer)))
c113de23
GM
868
869(defun mail-source-start-idle-timer ()
870 ;; Start our idle timer if necessary, so we delay the check until the
871 ;; user isn't typing.
872 (unless mail-source-report-new-mail-idle-timer
873 (setq mail-source-report-new-mail-idle-timer
874 (run-with-idle-timer
875 mail-source-idle-time-delay
876 nil
877 (lambda ()
23f87bed
MB
878 (unwind-protect
879 (mail-source-check-pop mail-source-primary-source)
880 (setq mail-source-report-new-mail-idle-timer nil)))))
c113de23
GM
881 ;; Since idle timers created when Emacs is already in the idle
882 ;; state don't get activated until Emacs _next_ becomes idle, we
883 ;; need to force our timer to be considered active now. We do
884 ;; this by being naughty and poking the timer internals directly
885 ;; (element 0 of the vector is nil if the timer is active).
886 (aset mail-source-report-new-mail-idle-timer 0 nil)))
887
888(defun mail-source-report-new-mail (arg)
889 "Toggle whether to report when new mail is available.
890This only works when `display-time' is enabled."
891 (interactive "P")
892 (if (not mail-source-primary-source)
715a2ca2 893 (error "Need to set `mail-source-primary-source' to check for new mail"))
c113de23
GM
894 (let ((on (if (null arg)
895 (not mail-source-report-new-mail)
896 (> (prefix-numeric-value arg) 0))))
897 (setq mail-source-report-new-mail on)
898 (and mail-source-report-new-mail-timer
72fc0418 899 (nnheader-cancel-timer mail-source-report-new-mail-timer))
c113de23 900 (and mail-source-report-new-mail-idle-timer
72fc0418 901 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
c113de23
GM
902 (setq mail-source-report-new-mail-timer nil)
903 (setq mail-source-report-new-mail-idle-timer nil)
904 (if on
905 (progn
906 (require 'time)
ce9401f3 907 ;; display-time-mail-function is an Emacs 21 feature.
c113de23
GM
908 (setq display-time-mail-function #'mail-source-new-mail-p)
909 ;; Set up the main timer.
910 (setq mail-source-report-new-mail-timer
23f87bed
MB
911 (nnheader-run-at-time
912 (* 60 mail-source-report-new-mail-interval)
913 (* 60 mail-source-report-new-mail-interval)
914 #'mail-source-start-idle-timer))
c113de23
GM
915 ;; When you get new mail, clear "Mail" from the mode line.
916 (add-hook 'nnmail-post-get-new-mail-hook
917 'display-time-event-handler)
918 (message "Mail check enabled"))
919 (setq display-time-mail-function nil)
920 (remove-hook 'nnmail-post-get-new-mail-hook
921 'display-time-event-handler)
922 (message "Mail check disabled"))))
923
924(defun mail-source-fetch-maildir (source callback)
925 "Fetcher for maildir sources."
926 (mail-source-bind (maildir source)
927 (let ((found 0)
928 mail-source-string)
929 (unless (string-match "/$" path)
930 (setq path (concat path "/")))
931 (dolist (subdir subdirs)
932 (when (file-directory-p (concat path subdir))
933 (setq mail-source-string (format "maildir:%s%s" path subdir))
934 (dolist (file (directory-files (concat path subdir) t))
935 (when (and (not (file-directory-p file))
936 (not (if function
937 (funcall function file mail-source-crash-box)
a1506d29 938 (let ((coding-system-for-write
c113de23 939 mm-text-coding-system)
a1506d29 940 (coding-system-for-read
c113de23
GM
941 mm-text-coding-system))
942 (with-temp-file mail-source-crash-box
943 (insert-file-contents file)
944 (goto-char (point-min))
23f87bed
MB
945;;; ;; Unix mail format
946;;; (unless (looking-at "\n*From ")
947;;; (insert "From maildir "
948;;; (current-time-string) "\n"))
949;;; (while (re-search-forward "^From " nil t)
950;;; (replace-match ">From "))
951;;; (goto-char (point-max))
126cbb42 952;;; (insert "\n\n")
c113de23 953 ;; MMDF mail format
126cbb42 954 (insert "\001\001\001\001\n"))
c113de23
GM
955 (delete-file file)))))
956 (incf found (mail-source-callback callback file))))))
957 found)))
958
959(eval-and-compile
960 (autoload 'imap-open "imap")
961 (autoload 'imap-authenticate "imap")
962 (autoload 'imap-mailbox-select "imap")
963 (autoload 'imap-mailbox-unselect "imap")
964 (autoload 'imap-mailbox-close "imap")
965 (autoload 'imap-search "imap")
966 (autoload 'imap-fetch "imap")
967 (autoload 'imap-close "imap")
968 (autoload 'imap-error-text "imap")
969 (autoload 'imap-message-flags-add "imap")
970 (autoload 'imap-list-to-message-set "imap")
72fc0418 971 (autoload 'imap-range-to-message-set "imap")
c113de23
GM
972 (autoload 'nnheader-ms-strip-cr "nnheader"))
973
72fc0418
DL
974(defvar mail-source-imap-file-coding-system 'binary
975 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
976
c113de23
GM
977(defun mail-source-fetch-imap (source callback)
978 "Fetcher for imap sources."
979 (mail-source-bind (imap source)
23f87bed
MB
980 (mail-source-run-script
981 prescript (format-spec-make ?p password ?t mail-source-crash-box
982 ?s server ?P port ?u user)
983 prescript-delay)
c113de23
GM
984 (let ((from (format "%s:%s:%s" server user port))
985 (found 0)
23f87bed 986 (buf (generate-new-buffer " *imap source*"))
c113de23 987 (mail-source-string (format "imap:%s:%s" server mailbox))
23f87bed 988 (imap-shell-program (or (list program) imap-shell-program))
c113de23
GM
989 remove)
990 (if (and (imap-open server port stream authentication buf)
991 (imap-authenticate
992 user (or (cdr (assoc from mail-source-password-cache))
993 password) buf)
994 (imap-mailbox-select mailbox nil buf))
4f926b3e 995 (let ((coding-system-for-write mail-source-imap-file-coding-system)
126cbb42 996 str)
c113de23 997 (with-temp-file mail-source-crash-box
4f926b3e
DL
998 ;; Avoid converting 8-bit chars from inserted strings to
999 ;; multibyte.
1000 (mm-disable-multibyte)
c113de23
GM
1001 ;; remember password
1002 (with-current-buffer buf
23f87bed
MB
1003 (when (and imap-password
1004 (not (assoc from mail-source-password-cache)))
c113de23
GM
1005 (push (cons from imap-password) mail-source-password-cache)))
1006 ;; if predicate is nil, use all uids
1007 (dolist (uid (imap-search (or predicate "1:*") buf))
23f87bed
MB
1008 (when (setq str
1009 (if (imap-capability 'IMAP4rev1 buf)
1010 (caddar (imap-fetch uid "BODY.PEEK[]"
1011 'BODYDETAIL nil buf))
1012 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
c113de23
GM
1013 (push uid remove)
1014 (insert "From imap " (current-time-string) "\n")
1015 (save-excursion
1016 (insert str "\n\n"))
1017 (while (re-search-forward "^From " nil t)
1018 (replace-match ">From "))
1019 (goto-char (point-max))))
1020 (nnheader-ms-strip-cr))
1021 (incf found (mail-source-callback callback server))
1022 (when (and remove fetchflag)
23f87bed 1023 (setq remove (nreverse remove))
c113de23 1024 (imap-message-flags-add
72fc0418
DL
1025 (imap-range-to-message-set (gnus-compress-sequence remove))
1026 fetchflag nil buf))
c113de23
GM
1027 (if dontexpunge
1028 (imap-mailbox-unselect buf)
23f87bed 1029 (imap-mailbox-close nil buf))
c113de23
GM
1030 (imap-close buf))
1031 (imap-close buf)
1032 ;; We nix out the password in case the error
1033 ;; was because of a wrong password being given.
1034 (setq mail-source-password-cache
1035 (delq (assoc from mail-source-password-cache)
1036 mail-source-password-cache))
23f87bed 1037 (error "IMAP error: %s" (imap-error-text buf)))
c113de23 1038 (kill-buffer buf)
23f87bed
MB
1039 (mail-source-run-script
1040 postscript
1041 (format-spec-make ?p password ?t mail-source-crash-box
1042 ?s server ?P port ?u user))
c113de23
GM
1043 found)))
1044
1045(eval-and-compile
1046 (autoload 'webmail-fetch "webmail"))
1047
1048(defun mail-source-fetch-webmail (source callback)
1049 "Fetch for webmail source."
1050 (mail-source-bind (webmail source)
1051 (let ((mail-source-string (format "webmail:%s:%s" subtype user))
1052 (webmail-newmail-only dontexpunge)
1053 (webmail-move-to-trash-can (not dontexpunge)))
1054 (when (eq authentication 'password)
1055 (setq password
1056 (or password
a1506d29 1057 (cdr (assoc (format "webmail:%s:%s" subtype user)
c113de23 1058 mail-source-password-cache))
23f87bed 1059 (read-passwd
c113de23
GM
1060 (format "Password for %s at %s: " user subtype))))
1061 (when (and password
a1506d29 1062 (not (assoc (format "webmail:%s:%s" subtype user)
c113de23 1063 mail-source-password-cache)))
a1506d29 1064 (push (cons (format "webmail:%s:%s" subtype user) password)
c113de23
GM
1065 mail-source-password-cache)))
1066 (webmail-fetch mail-source-crash-box subtype user password)
1067 (mail-source-callback callback (symbol-name subtype)))))
1068
1069(provide 'mail-source)
1070
ab5796a9 1071;;; arch-tag: 72948025-1d17-4d6c-bb12-ef1aa2c490fd
c113de23 1072;;; mail-source.el ends here