Document reserved keys
[emacs.git] / lisp / gnus / mail-source.el
blobd2850f4cee328d468b5c5dcd84a0005767b2f45f
1 ;;; mail-source.el --- functions for fetching mail
3 ;; Copyright (C) 1999-2018 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 <https://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* ((now (current-time))
595 (diff (if (natnump age) age 30));; fallback, if no valid AGE given
596 files)
597 (setq files (directory-files
598 mail-source-directory t
599 (concat "\\`"
600 (regexp-quote mail-source-incoming-file-prefix))))
601 (while files
602 (let* ((ffile (car files))
603 (bfile (replace-regexp-in-string "\\`.*/\\([^/]+\\)\\'" "\\1"
604 ffile))
605 (filetime (nth 5 (file-attributes ffile))))
606 (setq files (cdr files))
607 (when (and (> (time-to-number-of-days (time-subtract now filetime))
608 diff)
609 (if confirm
610 (y-or-n-p
611 (format-message "\
612 Delete old (> %s day(s)) incoming mail file `%s'? " diff bfile))
613 (gnus-message 8 "\
614 Deleting old (> %s day(s)) incoming mail file `%s'." diff bfile)
616 (delete-file ffile))))))
618 (defun mail-source-callback (callback info)
619 "Call CALLBACK on the mail file. Pass INFO on to CALLBACK."
620 (if (or (not (file-exists-p mail-source-crash-box))
621 (zerop (nth 7 (file-attributes mail-source-crash-box))))
622 (progn
623 (when (file-exists-p mail-source-crash-box)
624 (delete-file mail-source-crash-box))
626 (funcall callback mail-source-crash-box info)))
628 (defvar mail-source-incoming-last-checked-time nil)
630 (defun mail-source-delete-crash-box ()
631 (when (file-exists-p mail-source-crash-box)
632 ;; Delete or move the incoming mail out of the way.
633 (if (eq mail-source-delete-incoming t)
634 (delete-file mail-source-crash-box)
635 (let ((incoming
636 (make-temp-file
637 (expand-file-name
638 mail-source-incoming-file-prefix
639 mail-source-directory))))
640 (unless (file-exists-p (file-name-directory incoming))
641 (make-directory (file-name-directory incoming) t))
642 (rename-file mail-source-crash-box incoming t)
643 ;; remove old incoming files?
644 (when (natnump mail-source-delete-incoming)
645 ;; Don't check for old incoming files more than once per day to
646 ;; save a lot of file accesses.
647 (when (or (null mail-source-incoming-last-checked-time)
648 (> (float-time
649 (time-since mail-source-incoming-last-checked-time))
650 (* 24 60 60)))
651 (setq mail-source-incoming-last-checked-time (current-time))
652 (mail-source-delete-old-incoming
653 mail-source-delete-incoming
654 mail-source-delete-old-incoming-confirm)))))))
656 (defun mail-source-movemail (from to)
657 "Move FROM to TO using movemail."
658 (if (not (file-writable-p to))
659 (error "Can't write to crash box %s. Not moving mail" to)
660 (let ((to (file-truename (expand-file-name to)))
661 errors result)
662 (setq to (file-truename to)
663 from (file-truename from))
664 ;; Set TO if have not already done so, and rename or copy
665 ;; the file FROM to TO if and as appropriate.
666 (cond
667 ((file-exists-p to)
668 ;; The crash box exists already.
670 ((not (file-exists-p from))
671 ;; There is no inbox.
672 (setq to nil))
673 ((zerop (nth 7 (file-attributes from)))
674 ;; Empty file.
675 (setq to nil))
677 ;; If getting from mail spool directory, use movemail to move
678 ;; rather than just renaming, so as to interlock with the
679 ;; mailer.
680 (unwind-protect
681 (save-excursion
682 (setq errors (generate-new-buffer " *mail source loss*"))
683 (let ((default-directory "/"))
684 (setq result
685 (apply
686 'call-process
687 (append
688 (list
689 (or mail-source-movemail-program
690 (expand-file-name "movemail" exec-directory))
691 nil errors nil from to)))))
692 (when (file-exists-p to)
693 (set-file-modes to mail-source-default-file-modes))
694 (if (and (or (not (buffer-modified-p errors))
695 (zerop (buffer-size errors)))
696 (and (numberp result)
697 (zerop result)))
698 ;; No output => movemail won.
700 (set-buffer errors)
701 ;; There may be a warning about older revisions. We
702 ;; ignore that.
703 (goto-char (point-min))
704 (if (search-forward "older revision" nil t)
706 ;; Probably a real error.
707 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
708 (goto-char (point-max))
709 (skip-chars-backward " \t")
710 (delete-region (point) (point-max))
711 (goto-char (point-min))
712 (when (looking-at "movemail: ")
713 (delete-region (point-min) (match-end 0)))
714 ;; Result may be a signal description string.
715 (unless (yes-or-no-p
716 (format "movemail: %s (%s return). Continue? "
717 (buffer-string) result))
718 (error "%s" (buffer-string)))
719 (setq to nil)))))))
720 (when (and errors
721 (buffer-name errors))
722 (kill-buffer errors))
723 ;; Return whether we moved successfully or not.
724 to)))
726 (defun mail-source-fetch-with-program (program)
727 (eq 0 (call-process shell-file-name nil nil nil
728 shell-command-switch program)))
730 (defun mail-source-run-script (script spec &optional delay)
731 (when script
732 (if (functionp script)
733 (funcall script)
734 (mail-source-call-script
735 (format-spec script spec))))
736 (when delay
737 (sleep-for delay)))
739 (defun mail-source-call-script (script)
740 (let ((background nil)
741 (stderr (get-buffer-create " *mail-source-stderr*"))
742 result)
743 (when (string-match "& *$" script)
744 (setq script (substring script 0 (match-beginning 0))
745 background 0))
746 (setq result
747 (call-process shell-file-name nil stderr nil
748 shell-command-switch script))
749 (if (and result
750 (not (zerop result)))
751 (progn
752 (split-window-vertically)
753 (other-window 1)
754 (switch-to-buffer stderr)
755 (message "Mail source error: %s " (buffer-string)))
756 (kill-buffer stderr))))
759 ;;; Different fetchers
762 (defun mail-source-fetch-file (source callback)
763 "Fetcher for single-file sources."
764 (mail-source-bind (file source)
765 (mail-source-run-script
766 prescript (format-spec-make ?t mail-source-crash-box)
767 prescript-delay)
768 (let ((mail-source-string (format "file:%s" path)))
769 (if (mail-source-movemail path mail-source-crash-box)
770 (prog1
771 (mail-source-callback callback path)
772 (mail-source-run-script
773 postscript (format-spec-make ?t mail-source-crash-box))
774 (mail-source-delete-crash-box))
775 0))))
777 (defun mail-source-fetch-directory (source callback)
778 "Fetcher for directory sources."
779 (mail-source-bind (directory source)
780 (mail-source-run-script
781 prescript (format-spec-make ?t path) prescript-delay)
782 (let ((found 0)
783 (mail-source-string (format "directory:%s" path)))
784 (dolist (file (directory-files
785 path t (concat (regexp-quote suffix) "$")))
786 (when (and (file-regular-p file)
787 (funcall predicate file)
788 (mail-source-movemail file mail-source-crash-box))
789 (incf found (mail-source-callback callback file))
790 (mail-source-run-script postscript (format-spec-make ?t path))
791 (mail-source-delete-crash-box)))
792 found)))
794 (defun mail-source-fetch-pop (source callback)
795 "Fetcher for single-file sources."
796 (mail-source-bind (pop source)
797 ;; fixme: deal with stream type in format specs
798 (mail-source-run-script
799 prescript
800 (format-spec-make ?p password ?t mail-source-crash-box
801 ?s server ?P port ?u user)
802 prescript-delay)
803 (let ((from (format "%s:%s:%s" server user port))
804 (mail-source-string (format "pop:%s@%s" user server))
805 (process-environment (if server
806 (cons (concat "MAILHOST=" server)
807 process-environment)
808 process-environment))
809 result)
810 (when (eq authentication 'password)
811 (setq password
812 (or password
813 (cdr (assoc from mail-source-password-cache))
814 (read-passwd
815 (format "Password for %s at %s: " user server)))))
816 (setq result
817 (cond
818 (program
819 (mail-source-fetch-with-program
820 (format-spec
821 program
822 (format-spec-make ?p password ?t mail-source-crash-box
823 ?s server ?P port ?u user))))
824 (function
825 (funcall function mail-source-crash-box))
826 ;; The default is to use pop3.el.
828 (require 'pop3)
829 (let ((pop3-password password)
830 (pop3-maildrop user)
831 (pop3-mailhost server)
832 (pop3-port port)
833 (pop3-authentication-scheme
834 (if (eq authentication 'apop) 'apop 'pass))
835 (pop3-stream-type stream)
836 (pop3-leave-mail-on-server leave))
837 (if (or debug-on-quit debug-on-error)
838 (save-excursion (pop3-movemail mail-source-crash-box))
839 (condition-case err
840 (save-excursion (pop3-movemail mail-source-crash-box))
841 (error
842 ;; We nix out the password in case the error
843 ;; was because of a wrong password being given.
844 (setq mail-source-password-cache
845 (delq (assoc from mail-source-password-cache)
846 mail-source-password-cache))
847 (signal (car err) (cdr err)))))))))
848 (if result
849 (progn
850 (when (eq authentication 'password)
851 (unless (assoc from mail-source-password-cache)
852 (push (cons from password) mail-source-password-cache)))
853 (prog1
854 (mail-source-callback callback server)
855 ;; Update display-time's mail flag, if relevant.
856 (if (equal source mail-source-primary-source)
857 (setq mail-source-new-mail-available nil))
858 (mail-source-run-script
859 postscript
860 (format-spec-make ?p password ?t mail-source-crash-box
861 ?s server ?P port ?u user))
862 (mail-source-delete-crash-box)))
863 ;; We nix out the password in case the error
864 ;; was because of a wrong password being given.
865 (setq mail-source-password-cache
866 (delq (assoc from mail-source-password-cache)
867 mail-source-password-cache))
868 0))))
870 (defun mail-source-check-pop (source)
871 "Check whether there is new mail."
872 (mail-source-bind (pop source)
873 (let ((from (format "%s:%s:%s" server user port))
874 (mail-source-string (format "pop:%s@%s" user server))
875 (process-environment (if server
876 (cons (concat "MAILHOST=" server)
877 process-environment)
878 process-environment))
879 result)
880 (when (eq authentication 'password)
881 (setq password
882 (or password
883 (cdr (assoc from mail-source-password-cache))
884 (read-passwd
885 (format "Password for %s at %s: " user server))))
886 (unless (assoc from mail-source-password-cache)
887 (push (cons from password) mail-source-password-cache)))
888 (setq result
889 (cond
890 ;; No easy way to check whether mail is waiting for these.
891 (program)
892 (function)
893 ;; The default is to use pop3.el.
895 (require 'pop3)
896 (let ((pop3-password password)
897 (pop3-maildrop user)
898 (pop3-mailhost server)
899 (pop3-port port)
900 (pop3-authentication-scheme
901 (if (eq authentication 'apop) 'apop 'pass)))
902 (if (or debug-on-quit debug-on-error)
903 (save-excursion (pop3-get-message-count))
904 (condition-case err
905 (save-excursion (pop3-get-message-count))
906 (error
907 ;; We nix out the password in case the error
908 ;; was because of a wrong password being given.
909 (setq mail-source-password-cache
910 (delq (assoc from mail-source-password-cache)
911 mail-source-password-cache))
912 (signal (car err) (cdr err)))))))))
913 (if result
914 ;; Inform display-time that we have new mail.
915 (setq mail-source-new-mail-available (> result 0))
916 ;; We nix out the password in case the error
917 ;; was because of a wrong password being given.
918 (setq mail-source-password-cache
919 (delq (assoc from mail-source-password-cache)
920 mail-source-password-cache)))
921 result)))
923 (defun mail-source-touch-pop ()
924 "Open and close a POP connection shortly.
925 POP server should be defined in `mail-source-primary-source' (which is
926 preferred) or `mail-sources'. You may use it for the POP-before-SMTP
927 authentication. To do that, you need to set the
928 `message-send-mail-function' variable as `message-smtpmail-send-it'
929 and put the following line in your ~/.gnus.el file:
931 \(add-hook \\='message-send-mail-hook \\='mail-source-touch-pop)
933 See the Gnus manual for details."
934 (let ((sources (if mail-source-primary-source
935 (list mail-source-primary-source)
936 mail-sources)))
937 (while sources
938 (if (eq 'pop (car (car sources)))
939 (mail-source-check-pop (car sources)))
940 (setq sources (cdr sources)))))
942 (defun mail-source-new-mail-p ()
943 "Handler for `display-time' to indicate when new mail is available."
944 ;; Flash (ie. ring the visible bell) if mail is available.
945 (if (and mail-source-flash mail-source-new-mail-available)
946 (let ((visible-bell t))
947 (ding)))
948 ;; Only report flag setting; flag is updated on a different schedule.
949 mail-source-new-mail-available)
952 (defvar mail-source-report-new-mail nil)
953 (defvar mail-source-report-new-mail-timer nil)
954 (defvar mail-source-report-new-mail-idle-timer nil)
956 (defun mail-source-start-idle-timer ()
957 ;; Start our idle timer if necessary, so we delay the check until the
958 ;; user isn't typing.
959 (unless mail-source-report-new-mail-idle-timer
960 (setq mail-source-report-new-mail-idle-timer
961 (run-with-idle-timer
962 mail-source-idle-time-delay
964 (lambda ()
965 (unwind-protect
966 (mail-source-check-pop mail-source-primary-source)
967 (setq mail-source-report-new-mail-idle-timer nil)))))
968 ;; Since idle timers created when Emacs is already in the idle
969 ;; state don't get activated until Emacs _next_ becomes idle, we
970 ;; need to force our timer to be considered active now. We do
971 ;; this by being naughty and poking the timer internals directly
972 ;; (element 0 of the vector is nil if the timer is active).
973 (aset mail-source-report-new-mail-idle-timer 0 nil)))
975 (defun mail-source-report-new-mail (arg)
976 "Toggle whether to report when new mail is available.
977 This only works when `display-time' is enabled."
978 (interactive "P")
979 (if (not mail-source-primary-source)
980 (error "Need to set `mail-source-primary-source' to check for new mail"))
981 (let ((on (if (null arg)
982 (not mail-source-report-new-mail)
983 (> (prefix-numeric-value arg) 0))))
984 (setq mail-source-report-new-mail on)
985 (and mail-source-report-new-mail-timer
986 (nnheader-cancel-timer mail-source-report-new-mail-timer))
987 (and mail-source-report-new-mail-idle-timer
988 (nnheader-cancel-timer mail-source-report-new-mail-idle-timer))
989 (setq mail-source-report-new-mail-timer nil)
990 (setq mail-source-report-new-mail-idle-timer nil)
991 (if on
992 (progn
993 (require 'time)
994 (setq display-time-mail-function #'mail-source-new-mail-p)
995 ;; Set up the main timer.
996 (setq mail-source-report-new-mail-timer
997 (run-at-time
998 (* 60 mail-source-report-new-mail-interval)
999 (* 60 mail-source-report-new-mail-interval)
1000 #'mail-source-start-idle-timer))
1001 ;; When you get new mail, clear "Mail" from the mode line.
1002 (add-hook 'nnmail-post-get-new-mail-hook
1003 'display-time-event-handler)
1004 (message "Mail check enabled"))
1005 (setq display-time-mail-function nil)
1006 (remove-hook 'nnmail-post-get-new-mail-hook
1007 'display-time-event-handler)
1008 (message "Mail check disabled"))))
1010 (defun mail-source-fetch-maildir (source callback)
1011 "Fetcher for maildir sources."
1012 (mail-source-bind (maildir source)
1013 (let ((found 0)
1014 mail-source-string)
1015 (unless (string-match "/$" path)
1016 (setq path (concat path "/")))
1017 (dolist (subdir subdirs)
1018 (when (file-directory-p (concat path subdir))
1019 (setq mail-source-string (format "maildir:%s%s" path subdir))
1020 (dolist (file (directory-files (concat path subdir) t))
1021 (when (and (not (file-directory-p file))
1022 (not (if function
1023 ;; `function' should return nil if successful.
1024 (funcall function file mail-source-crash-box)
1025 (let ((coding-system-for-write
1026 mm-text-coding-system)
1027 (coding-system-for-read
1028 mm-text-coding-system))
1029 (with-temp-file mail-source-crash-box
1030 (insert-file-contents file)
1031 (goto-char (point-min))
1032 ;;; ;; Unix mail format
1033 ;;; (unless (looking-at "\n*From ")
1034 ;;; (insert "From maildir "
1035 ;;; (current-time-string) "\n"))
1036 ;;; (while (re-search-forward "^From " nil t)
1037 ;;; (replace-match ">From "))
1038 ;;; (goto-char (point-max))
1039 ;;; (insert "\n\n")
1040 ;; MMDF mail format
1041 (insert "\001\001\001\001\n"))
1042 (delete-file file)
1043 nil))))
1044 (incf found (mail-source-callback callback file))
1045 (mail-source-delete-crash-box)))))
1046 found)))
1048 (autoload 'imap-open "imap")
1049 (autoload 'imap-authenticate "imap")
1050 (autoload 'imap-mailbox-select "imap")
1051 (autoload 'imap-mailbox-unselect "imap")
1052 (autoload 'imap-mailbox-close "imap")
1053 (autoload 'imap-search "imap")
1054 (autoload 'imap-fetch "imap")
1055 (autoload 'imap-close "imap")
1056 (autoload 'imap-error-text "imap")
1057 (autoload 'imap-message-flags-add "imap")
1058 (autoload 'imap-list-to-message-set "imap")
1059 (autoload 'imap-range-to-message-set "imap")
1060 (autoload 'nnheader-ms-strip-cr "nnheader")
1062 (autoload 'gnus-compress-sequence "gnus-range")
1064 (defvar mail-source-imap-file-coding-system 'binary
1065 "Coding system for the crashbox made by `mail-source-fetch-imap'.")
1067 ;; Autoloads will bring in imap before this is called.
1068 (declare-function imap-capability "imap" (&optional identifier buffer))
1070 (defun mail-source-fetch-imap (source callback)
1071 "Fetcher for imap sources."
1072 (mail-source-bind (imap source)
1073 (mail-source-run-script
1074 prescript (format-spec-make ?p password ?t mail-source-crash-box
1075 ?s server ?P port ?u user)
1076 prescript-delay)
1077 (let ((from (format "%s:%s:%s" server user port))
1078 (found 0)
1079 (buf (generate-new-buffer " *imap source*"))
1080 (mail-source-string (format "imap:%s:%s" server mailbox))
1081 (imap-shell-program (or (list program) imap-shell-program))
1082 remove)
1083 (if (and (imap-open server port stream authentication buf)
1084 (imap-authenticate
1085 user (or (cdr (assoc from mail-source-password-cache))
1086 password) buf))
1087 (let ((mailbox-list (if (listp mailbox) mailbox (list mailbox))))
1088 (dolist (mailbox mailbox-list)
1089 (when (imap-mailbox-select mailbox nil buf)
1090 (let ((coding-system-for-write mail-source-imap-file-coding-system)
1091 str)
1092 (message "Fetching from %s..." mailbox)
1093 (with-temp-file mail-source-crash-box
1094 ;; Avoid converting 8-bit chars from inserted strings to
1095 ;; multibyte.
1096 (mm-disable-multibyte)
1097 ;; remember password
1098 (with-current-buffer buf
1099 (when (and imap-password
1100 (not (member (cons from imap-password)
1101 mail-source-password-cache)))
1102 (push (cons from imap-password) mail-source-password-cache)))
1103 ;; if predicate is nil, use all uids
1104 (dolist (uid (imap-search (or predicate "1:*") buf))
1105 (when (setq str
1106 (if (imap-capability 'IMAP4rev1 buf)
1107 (caddar (imap-fetch uid "BODY.PEEK[]"
1108 'BODYDETAIL nil buf))
1109 (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf)))
1110 (push uid remove)
1111 (insert "From imap " (current-time-string) "\n")
1112 (save-excursion
1113 (insert str "\n\n"))
1114 (while (let ((case-fold-search nil))
1115 (re-search-forward "^From " nil t))
1116 (replace-match ">From "))
1117 (goto-char (point-max))))
1118 (nnheader-ms-strip-cr))
1119 (incf found (mail-source-callback callback server))
1120 (mail-source-delete-crash-box)
1121 (when (and remove fetchflag)
1122 (setq remove (nreverse remove))
1123 (imap-message-flags-add
1124 (imap-range-to-message-set (gnus-compress-sequence remove))
1125 fetchflag nil buf))
1126 (if dontexpunge
1127 (imap-mailbox-unselect buf)
1128 (imap-mailbox-close nil buf)))))
1129 (imap-close buf))
1130 (imap-close buf)
1131 ;; We nix out the password in case the error
1132 ;; was because of a wrong password being given.
1133 (setq mail-source-password-cache
1134 (delq (assoc from mail-source-password-cache)
1135 mail-source-password-cache))
1136 (error "IMAP error: %s" (imap-error-text buf)))
1137 (kill-buffer buf)
1138 (mail-source-run-script
1139 postscript
1140 (format-spec-make ?p password ?t mail-source-crash-box
1141 ?s server ?P port ?u user))
1142 found)))
1144 (provide 'mail-source)
1146 ;;; mail-source.el ends here