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