de881c2e503e6c5a74c3b377275142a1e8c0494e
[gnus] / lisp / pop3.el
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
7 ;; Maintainer: FSF
8 ;; Keywords: mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
30 ;; are implemented.  The LIST command has not been implemented due to lack
31 ;; of actual usefulness.
32 ;; The optional POP3 command TOP has not been implemented.
33
34 ;; This program was inspired by Kyle E. Jones's vm-pop program.
35
36 ;;; Code:
37
38 (require 'mail-utils)
39
40 (defgroup pop3 nil
41   "Post Office Protocol"
42   :group 'mail
43   :group 'mail-source)
44
45 (defcustom pop3-maildrop (or (user-login-name)
46                              (getenv "LOGNAME")
47                              (getenv "USER"))
48   "*POP3 maildrop."
49   :version "21.4" ;; Oort Gnus
50   :type 'string
51   :group 'pop3)
52
53 (defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
54                              "pop3")
55   "*POP3 mailhost."
56   :version "21.4" ;; Oort Gnus
57   :type 'string
58   :group 'pop3)
59
60 (defcustom pop3-port 110
61   "*POP3 port."
62   :version "21.4" ;; Oort Gnus
63   :type 'number
64   :group 'pop3)
65
66 (defcustom pop3-password-required t
67   "*Non-nil if a password is required when connecting to POP server."
68   :version "21.4" ;; Oort Gnus
69   :type 'boolean
70   :group 'pop3)
71
72 ;; Should this be customizable?
73 (defvar pop3-password nil
74   "*Password to use when connecting to POP server.")
75
76 (defcustom pop3-authentication-scheme 'pass
77   "*POP3 authentication scheme.
78 Defaults to 'pass, for the standard USER/PASS authentication.  Other valid
79 values are 'apop."
80   :version "21.4" ;; Oort Gnus
81   :type '(choice (const :tag "USER/PASS" pass)
82                  (const :tag "APOP" apop))
83   :group 'pop3)
84
85 (defcustom pop3-leave-mail-on-server nil
86   "*Non-nil if the mail is to be left on the POP server after fetching.
87
88 If `pop3-leave-mail-on-server' is non-nil the mail is to be left
89 on the POP server after fetching.  Note that POP servers maintain
90 no state information between sessions, so what the client
91 believes is there and what is actually there may not match up.
92 If they do not, then the whole thing can fall apart and leave you
93 with a corrupt mailbox."
94   :version "21.4" ;; Oort Gnus
95   :type 'boolean
96   :group 'pop3)
97
98 (defvar pop3-timestamp nil
99   "Timestamp returned when initially connected to the POP server.
100 Used for APOP authentication.")
101
102 (defvar pop3-read-point nil)
103 (defvar pop3-debug nil)
104
105 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
106 (defvar pop3-read-timeout
107   (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
108                     (symbol-name system-type))
109       ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
110       ;;
111       ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
112       ;;
113       ;; There should probably be a runtime test to determine the timing
114       ;; resolution, or a primitive to report it.  I don't know off-hand
115       ;; what's possible.  Perhaps better, maybe the Windows/DOS primitive
116       ;; could round up non-zero timeouts to a minimum of 1.0?
117       1.0
118     0.1)
119   "How long pop3 should wait between checking for the end of output.
120 Shorter values mean quicker response, but are more CPU intensive.")
121
122 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
123 (defun pop3-accept-process-output (process)
124   (accept-process-output
125    process
126    (truncate pop3-read-timeout)
127    (truncate (* (- pop3-read-timeout
128                    (truncate pop3-read-timeout))
129                 1000))))
130
131 (defun pop3-movemail (&optional crashbox)
132   "Transfer contents of a maildrop to the specified CRASHBOX."
133   (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
134   (let* ((process (pop3-open-server pop3-mailhost pop3-port))
135          (crashbuf (get-buffer-create " *pop3-retr*"))
136          (n 1)
137          message-count
138          (pop3-password pop3-password))
139     ;; for debugging only
140     (if pop3-debug (switch-to-buffer (process-buffer process)))
141     ;; query for password
142     (if (and pop3-password-required (not pop3-password))
143         (setq pop3-password
144               (read-passwd (format "Password for %s: " pop3-maildrop))))
145     (cond ((equal 'apop pop3-authentication-scheme)
146            (pop3-apop process pop3-maildrop))
147           ((equal 'pass pop3-authentication-scheme)
148            (pop3-user process pop3-maildrop)
149            (pop3-pass process))
150           (t (error "Invalid POP3 authentication scheme")))
151     (setq message-count (car (pop3-stat process)))
152     (unwind-protect
153         (while (<= n message-count)
154           (message "Retrieving message %d of %d from %s..."
155                    n message-count pop3-mailhost)
156           (pop3-retr process n crashbuf)
157           (save-excursion
158             (set-buffer crashbuf)
159             (let ((coding-system-for-write 'binary))
160               (write-region (point-min) (point-max) crashbox t 'nomesg))
161             (set-buffer (process-buffer process))
162             (while (> (buffer-size) 5000)
163               (goto-char (point-min))
164               (forward-line 50)
165               (delete-region (point-min) (point))))
166           (unless pop3-leave-mail-on-server
167             (pop3-dele process n))
168           (setq n (+ 1 n))
169           (if pop3-debug (sit-for 1) (sit-for 0.1))
170           )
171       (pop3-quit process))
172     (kill-buffer crashbuf)
173     )