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