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