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