comparison lisp/gnus/pop3.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents a5381c78296d
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface 1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2 2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; Free Software Foundation, Inc. 4 ;; 2004, 2005 Free Software Foundation, Inc.
5 5
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net> 6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
7 ;; Maintainer: FSF 7 ;; Maintainer: FSF
8 ;; Keywords: mail 8 ;; Keywords: mail
9 9
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details. 20 ;; GNU General Public License for more details.
21 21
22 ;; You should have received a copy of the GNU General Public License 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 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02111-1307, USA. 25 ;; Boston, MA 02110-1301, USA.
26 26
27 ;;; Commentary: 27 ;;; Commentary:
28 28
29 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands 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 30 ;; are implemented. The LIST command has not been implemented due to lack
35 35
36 ;;; Code: 36 ;;; Code:
37 37
38 (require 'mail-utils) 38 (require 'mail-utils)
39 39
40 (defvar pop3-maildrop (or (user-login-name) (getenv "LOGNAME") (getenv "USER") nil) 40 (defgroup pop3 nil
41 "*POP3 maildrop.") 41 "Post Office Protocol."
42 (defvar pop3-mailhost (or (getenv "MAILHOST") nil) 42 :group 'mail
43 "*POP3 mailhost.") 43 :group 'mail-source)
44 (defvar pop3-port 110 44
45 "*POP3 port.") 45 (defcustom pop3-maildrop (or (user-login-name)
46 46 (getenv "LOGNAME")
47 (defvar pop3-password-required t 47 (getenv "USER"))
48 "*Non-nil if a password is required when connecting to POP server.") 48 "*POP3 maildrop."
49 :version "22.1" ;; 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 "22.1" ;; Oort Gnus
57 :type 'string
58 :group 'pop3)
59
60 (defcustom pop3-port 110
61 "*POP3 port."
62 :version "22.1" ;; 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 "22.1" ;; Oort Gnus
69 :type 'boolean
70 :group 'pop3)
71
72 ;; Should this be customizable?
49 (defvar pop3-password nil 73 (defvar pop3-password nil
50 "*Password to use when connecting to POP server.") 74 "*Password to use when connecting to POP server.")
51 75
52 (defvar pop3-authentication-scheme 'pass 76 (defcustom pop3-authentication-scheme 'pass
53 "*POP3 authentication scheme. 77 "*POP3 authentication scheme.
54 Defaults to 'pass, for the standard USER/PASS authentication. Other valid 78 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
55 values are 'apop.") 79 values are 'apop."
80 :version "22.1" ;; 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 the `pop3-leave-mail-on-server' is non-`nil' the mail is to be
89 left on the POP server after fetching. Note that POP servers
90 maintain no state information between sessions, so what the
91 client believes is there and what is actually there may not match
92 up. If they do not, then the whole thing can fall apart and
93 leave you with a corrupt mailbox."
94 :version "22.1" ;; Oort Gnus
95 :type 'boolean
96 :group 'pop3)
56 97
57 (defvar pop3-timestamp nil 98 (defvar pop3-timestamp nil
58 "Timestamp returned when initially connected to the POP server. 99 "Timestamp returned when initially connected to the POP server.
59 Used for APOP authentication.") 100 Used for APOP authentication.")
60 101
61 (defvar pop3-read-point nil) 102 (defvar pop3-read-point nil)
62 (defvar pop3-debug 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))))
63 130
64 (defun pop3-movemail (&optional crashbox) 131 (defun pop3-movemail (&optional crashbox)
65 "Transfer contents of a maildrop to the specified CRASHBOX." 132 "Transfer contents of a maildrop to the specified CRASHBOX."
66 (or crashbox (setq crashbox (expand-file-name "~/.crashbox"))) 133 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
67 (let* ((process (pop3-open-server pop3-mailhost pop3-port)) 134 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
68 (crashbuf (get-buffer-create " *pop3-retr*")) 135 (crashbuf (get-buffer-create " *pop3-retr*"))
69 (n 1) 136 (n 1)
70 message-count 137 message-count
71 (pop3-password pop3-password) 138 (pop3-password pop3-password))
72 )
73 ;; for debugging only 139 ;; for debugging only
74 (if pop3-debug (switch-to-buffer (process-buffer process))) 140 (if pop3-debug (switch-to-buffer (process-buffer process)))
75 ;; query for password 141 ;; query for password
76 (if (and pop3-password-required (not pop3-password)) 142 (if (and pop3-password-required (not pop3-password))
77 (setq pop3-password 143 (setq pop3-password
78 (pop3-read-passwd (format "Password for %s: " pop3-maildrop)))) 144 (read-passwd (format "Password for %s: " pop3-maildrop))))
79 (cond ((equal 'apop pop3-authentication-scheme) 145 (cond ((equal 'apop pop3-authentication-scheme)
80 (pop3-apop process pop3-maildrop)) 146 (pop3-apop process pop3-maildrop))
81 ((equal 'pass pop3-authentication-scheme) 147 ((equal 'pass pop3-authentication-scheme)
82 (pop3-user process pop3-maildrop) 148 (pop3-user process pop3-maildrop)
83 (pop3-pass process)) 149 (pop3-pass process))
84 (t (error "Invalid POP3 authentication scheme"))) 150 (t (error "Invalid POP3 authentication scheme")))
85 (setq message-count (car (pop3-stat process))) 151 (setq message-count (car (pop3-stat process)))
86 (unwind-protect 152 (unwind-protect
87 (while (<= n message-count) 153 (while (<= n message-count)
88 (message (format "Retrieving message %d of %d from %s..." 154 (message "Retrieving message %d of %d from %s..."
89 n message-count pop3-mailhost)) 155 n message-count pop3-mailhost)
90 (pop3-retr process n crashbuf) 156 (pop3-retr process n crashbuf)
91 (save-excursion 157 (save-excursion
92 (set-buffer crashbuf) 158 (set-buffer crashbuf)
93 (let ((coding-system-for-write 'binary)) 159 (let ((coding-system-for-write 'binary))
94 (write-region (point-min) (point-max) crashbox t 'nomesg)) 160 (write-region (point-min) (point-max) crashbox t 'nomesg))
95 (set-buffer (process-buffer process)) 161 (set-buffer (process-buffer process))
96 (while (> (buffer-size) 5000) 162 (while (> (buffer-size) 5000)
97 (goto-char (point-min)) 163 (goto-char (point-min))
98 (forward-line 50) 164 (forward-line 50)
99 (delete-region (point-min) (point)))) 165 (delete-region (point-min) (point))))
100 (pop3-dele process n) 166 (unless pop3-leave-mail-on-server
167 (pop3-dele process n))
101 (setq n (+ 1 n)) 168 (setq n (+ 1 n))
102 (if pop3-debug (sit-for 1) (sit-for 0.1)) 169 (if pop3-debug (sit-for 1) (sit-for 0.1))
103 ) 170 )
104 (pop3-quit process)) 171 (pop3-quit process))
105 (kill-buffer crashbuf) 172 (kill-buffer crashbuf)
108 175
109 (defun pop3-get-message-count () 176 (defun pop3-get-message-count ()
110 "Return the number of messages in the maildrop." 177 "Return the number of messages in the maildrop."
111 (let* ((process (pop3-open-server pop3-mailhost pop3-port)) 178 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
112 message-count 179 message-count
113 (pop3-password pop3-password) 180 (pop3-password pop3-password))
114 )
115 ;; for debugging only 181 ;; for debugging only
116 (if pop3-debug (switch-to-buffer (process-buffer process))) 182 (if pop3-debug (switch-to-buffer (process-buffer process)))
117 ;; query for password 183 ;; query for password
118 (if (and pop3-password-required (not pop3-password)) 184 (if (and pop3-password-required (not pop3-password))
119 (setq pop3-password 185 (setq pop3-password
120 (pop3-read-passwd (format "Password for %s: " pop3-maildrop)))) 186 (read-passwd (format "Password for %s: " pop3-maildrop))))
121 (cond ((equal 'apop pop3-authentication-scheme) 187 (cond ((equal 'apop pop3-authentication-scheme)
122 (pop3-apop process pop3-maildrop)) 188 (pop3-apop process pop3-maildrop))
123 ((equal 'pass pop3-authentication-scheme) 189 ((equal 'pass pop3-authentication-scheme)
124 (pop3-user process pop3-maildrop) 190 (pop3-user process pop3-maildrop)
125 (pop3-pass process)) 191 (pop3-pass process))
153 (set-buffer (process-buffer process)) 219 (set-buffer (process-buffer process))
154 (goto-char (point-max)) 220 (goto-char (point-max))
155 (insert output))) 221 (insert output)))
156 222
157 (defun pop3-send-command (process command) 223 (defun pop3-send-command (process command)
158 (set-buffer (process-buffer process)) 224 (set-buffer (process-buffer process))
159 (goto-char (point-max)) 225 (goto-char (point-max))
160 ;; (if (= (aref command 0) ?P) 226 ;; (if (= (aref command 0) ?P)
161 ;; (insert "PASS <omitted>\r\n") 227 ;; (insert "PASS <omitted>\r\n")
162 ;; (insert command "\r\n")) 228 ;; (insert command "\r\n"))
163 (setq pop3-read-point (point)) 229 (setq pop3-read-point (point))
164 (goto-char (point-max)) 230 (goto-char (point-max))
165 (process-send-string process (concat command "\r\n")) 231 (process-send-string process (concat command "\r\n")))
166 )
167 232
168 (defun pop3-read-response (process &optional return) 233 (defun pop3-read-response (process &optional return)
169 "Read the response from the server. 234 "Read the response from the server.
170 Return the response string if optional second argument is non-nil." 235 Return the response string if optional second argument is non-nil."
171 (let ((case-fold-search nil) 236 (let ((case-fold-search nil)
172 match-end) 237 match-end)
173 (save-excursion 238 (save-excursion
174 (set-buffer (process-buffer process)) 239 (set-buffer (process-buffer process))
175 (goto-char pop3-read-point) 240 (goto-char pop3-read-point)
176 (while (not (search-forward "\r\n" nil t)) 241 (while (and (memq (process-status process) '(open run))
177 (accept-process-output process 3) 242 (not (search-forward "\r\n" nil t)))
243 (pop3-accept-process-output process)
178 (goto-char pop3-read-point)) 244 (goto-char pop3-read-point))
179 (setq match-end (point)) 245 (setq match-end (point))
180 (goto-char pop3-read-point) 246 (goto-char pop3-read-point)
181 (if (looking-at "-ERR") 247 (if (looking-at "-ERR")
182 (error (buffer-substring (point) (- match-end 2))) 248 (error (buffer-substring (point) (- match-end 2)))
185 (setq pop3-read-point match-end) 251 (setq pop3-read-point match-end)
186 (if return 252 (if return
187 (buffer-substring (point) match-end) 253 (buffer-substring (point) match-end)
188 t) 254 t)
189 ))))) 255 )))))
190
191 (defvar pop3-read-passwd nil)
192 (defun pop3-read-passwd (prompt)
193 (if (not pop3-read-passwd)
194 (if (fboundp 'read-passwd)
195 (setq pop3-read-passwd 'read-passwd)
196 (if (load "passwd" t)
197 (setq pop3-read-passwd 'read-passwd)
198 (autoload 'ange-ftp-read-passwd "ange-ftp")
199 (setq pop3-read-passwd 'ange-ftp-read-passwd))))
200 (funcall pop3-read-passwd prompt))
201 256
202 (defun pop3-clean-region (start end) 257 (defun pop3-clean-region (start end)
203 (setq end (set-marker (make-marker) end)) 258 (setq end (set-marker (make-marker) end))
204 (save-excursion 259 (save-excursion
205 (goto-char start) 260 (goto-char start)
257 ;; Date: 08 Jul 1996 23:22:24 -0400 312 ;; Date: 08 Jul 1996 23:22:24 -0400
258 ;; should be 313 ;; should be
259 ;; Tue Jul 9 09:04:21 1996 314 ;; Tue Jul 9 09:04:21 1996
260 (setq date 315 (setq date
261 (cond ((not date) 316 (cond ((not date)
262 "Tue Jan 1 00:00:0 1900") 317 "Tue Jan 1 00:00:0 1900")
263 ((string-match "[A-Z]" (nth 0 date)) 318 ((string-match "[A-Z]" (nth 0 date))
264 (format "%s %s %s %s %s" 319 (format "%s %s %s %s %s"
265 (nth 0 date) (nth 2 date) (nth 1 date) 320 (nth 0 date) (nth 2 date) (nth 1 date)
266 (nth 4 date) (nth 3 date))) 321 (nth 4 date) (nth 3 date)))
267 (t 322 (t
291 346
292 ;; The Command Set 347 ;; The Command Set
293 348
294 ;; AUTHORIZATION STATE 349 ;; AUTHORIZATION STATE
295 350
351 (eval-when-compile
352 (if (not (fboundp 'md5)) ;; Emacs 20
353 (defalias 'md5 'ignore)))
354
355 (eval-and-compile
356 (if (and (fboundp 'md5)
357 ;; There might be an incompatible implementation.
358 (condition-case nil
359 (md5 "Check whether the 4th argument is allowed"
360 nil nil 'binary)
361 (error nil)))
362 (defun pop3-md5 (string)
363 (md5 string nil nil 'binary))
364 (defvar pop3-md5-program "md5"
365 "*Program to encode its input in MD5.
366 \"openssl\" is a popular alternative; set `pop3-md5-program-args' to
367 '(\"md5\") if you use it.")
368 (defvar pop3-md5-program-args nil
369 "*List of arguments passed to `pop3-md5-program'.")
370 (defun pop3-md5 (string)
371 (let ((default-enable-multibyte-characters t)
372 (coding-system-for-write 'binary))
373 (with-temp-buffer
374 (insert string)
375 (apply 'call-process-region (point-min) (point-max)
376 pop3-md5-program t (current-buffer) nil
377 pop3-md5-program-args)
378 ;; The meaningful output is the first 32 characters.
379 ;; Don't return the newline that follows them!
380 (buffer-substring (point-min) (+ 32 (point-min))))))))
381
296 (defun pop3-user (process user) 382 (defun pop3-user (process user)
297 "Send USER information to POP3 server." 383 "Send USER information to POP3 server."
298 (pop3-send-command process (format "USER %s" user)) 384 (pop3-send-command process (format "USER %s" user))
299 (let ((response (pop3-read-response process t))) 385 (let ((response (pop3-read-response process t)))
300 (if (not (and response (string-match "+OK" response))) 386 (if (not (and response (string-match "+OK" response)))
301 (error (format "USER %s not valid" user))))) 387 (error "USER %s not valid" user))))
302 388
303 (defun pop3-pass (process) 389 (defun pop3-pass (process)
304 "Send authentication information to the server." 390 "Send authentication information to the server."
305 (pop3-send-command process (format "PASS %s" pop3-password)) 391 (pop3-send-command process (format "PASS %s" pop3-password))
306 (let ((response (pop3-read-response process t))) 392 (let ((response (pop3-read-response process t)))
310 (defun pop3-apop (process user) 396 (defun pop3-apop (process user)
311 "Send alternate authentication information to the server." 397 "Send alternate authentication information to the server."
312 (let ((pass pop3-password)) 398 (let ((pass pop3-password))
313 (if (and pop3-password-required (not pass)) 399 (if (and pop3-password-required (not pass))
314 (setq pass 400 (setq pass
315 (pop3-read-passwd (format "Password for %s: " pop3-maildrop)))) 401 (read-passwd (format "Password for %s: " pop3-maildrop))))
316 (if pass 402 (if pass
317 (let ((hash (pop3-md5 (concat pop3-timestamp pass)))) 403 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
318 (pop3-send-command process (format "APOP %s %s" user hash)) 404 (pop3-send-command process (format "APOP %s %s" user hash))
319 (let ((response (pop3-read-response process t))) 405 (let ((response (pop3-read-response process t)))
320 (if (not (and response (string-match "+OK" response))) 406 (if (not (and response (string-match "+OK" response)))
321 (pop3-quit process))))) 407 (pop3-quit process)))))
322 )) 408 ))
323 409
324 ;; TRANSACTION STATE 410 ;; TRANSACTION STATE
325 411
326 (eval-and-compile
327 (if (fboundp 'md5)
328 (defalias 'pop3-md5 'md5)
329 (defvar pop3-md5-program "md5"
330 "*Program to encode its input in MD5.")
331
332 (defun pop3-md5 (string)
333 (with-temp-buffer
334 (insert string)
335 (call-process-region (point-min) (point-max)
336 pop3-md5-program
337 t (current-buffer) nil)
338 ;; The meaningful output is the first 32 characters.
339 ;; Don't return the newline that follows them!
340 (buffer-substring (point-min) (+ 32 (point-min)))))))
341
342 (defun pop3-stat (process) 412 (defun pop3-stat (process)
343 "Return the number of messages in the maildrop and the maildrop's size." 413 "Return the number of messages in the maildrop and the maildrop's size."
344 (pop3-send-command process "STAT") 414 (pop3-send-command process "STAT")
345 (let ((response (pop3-read-response process t))) 415 (let ((response (pop3-read-response process t)))
346 (list (string-to-int (nth 1 (split-string response " "))) 416 (list (string-to-number (nth 1 (split-string response " ")))
347 (string-to-int (nth 2 (split-string response " ")))) 417 (string-to-number (nth 2 (split-string response " "))))
348 )) 418 ))
349 419
350 (defun pop3-list (process &optional msg) 420 (defun pop3-list (process &optional msg)
351 "Scan listing of available messages. 421 "Scan listing of available messages.
352 This function currently does nothing.") 422 This function currently does nothing.")
357 (pop3-read-response process) 427 (pop3-read-response process)
358 (let ((start pop3-read-point) end) 428 (let ((start pop3-read-point) end)
359 (save-excursion 429 (save-excursion
360 (set-buffer (process-buffer process)) 430 (set-buffer (process-buffer process))
361 (while (not (re-search-forward "^\\.\r\n" nil t)) 431 (while (not (re-search-forward "^\\.\r\n" nil t))
362 (accept-process-output process 3) 432 (pop3-accept-process-output process)
363 ;; bill@att.com ... to save wear and tear on the heap
364 ;; uncommented because the condensed version below is a problem for
365 ;; some.
366 (if (> (buffer-size) 20000) (sleep-for 1))
367 (if (> (buffer-size) 50000) (sleep-for 1))
368 (if (> (buffer-size) 100000) (sleep-for 1))
369 (if (> (buffer-size) 200000) (sleep-for 1))
370 (if (> (buffer-size) 500000) (sleep-for 1))
371 ;; bill@att.com
372 ;; condensed into:
373 ;; (sometimes causes problems for really large messages.)
374 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
375 (goto-char start)) 433 (goto-char start))
376 (setq pop3-read-point (point-marker)) 434 (setq pop3-read-point (point-marker))
377 ;; this code does not seem to work for some POP servers... 435 ;; this code does not seem to work for some POP servers...
378 ;; and I cannot figure out why not. 436 ;; and I cannot figure out why not.
379 ; (goto-char (match-beginning 0)) 437 ;; (goto-char (match-beginning 0))
380 ; (backward-char 2) 438 ;; (backward-char 2)
381 ; (if (not (looking-at "\r\n")) 439 ;; (if (not (looking-at "\r\n"))
382 ; (insert "\r\n")) 440 ;; (insert "\r\n"))
383 ; (re-search-forward "\\.\r\n") 441 ;; (re-search-forward "\\.\r\n")
384 (goto-char (match-beginning 0)) 442 (goto-char (match-beginning 0))
385 (setq end (point-marker)) 443 (setq end (point-marker))
386 (pop3-clean-region start end) 444 (pop3-clean-region start end)
387 (pop3-munge-message-separator start end) 445 (pop3-munge-message-separator start end)
388 (save-excursion 446 (save-excursion
404 462
405 (defun pop3-last (process) 463 (defun pop3-last (process)
406 "Return highest accessed message-id number for the session." 464 "Return highest accessed message-id number for the session."
407 (pop3-send-command process "LAST") 465 (pop3-send-command process "LAST")
408 (let ((response (pop3-read-response process t))) 466 (let ((response (pop3-read-response process t)))
409 (string-to-int (nth 1 (split-string response " "))) 467 (string-to-number (nth 1 (split-string response " ")))
410 )) 468 ))
411 469
412 (defun pop3-rset (process) 470 (defun pop3-rset (process)
413 "Remove all delete marks from current maildrop." 471 "Remove all delete marks from current maildrop."
414 (pop3-send-command process "RSET") 472 (pop3-send-command process "RSET")
508 ;; Possible responses: 566 ;; Possible responses:
509 ;; +OK [TCP connection closed] 567 ;; +OK [TCP connection closed]
510 568
511 (provide 'pop3) 569 (provide 'pop3)
512 570
571 ;;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
513 ;;; pop3.el ends here 572 ;;; pop3.el ends here