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