1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
10 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
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.
34 ;; This program was inspired by Kyle E. Jones's vm-pop program.
39 (defvar parse-time-months
)
42 "Post Office Protocol."
46 (defcustom pop3-maildrop
(or (user-login-name)
50 :version
"22.1" ;; Oort Gnus
54 (defcustom pop3-mailhost
(or (getenv "MAILHOST") ;; nil -> mismatch
57 :version
"22.1" ;; Oort Gnus
61 (defcustom pop3-port
110
63 :version
"22.1" ;; Oort Gnus
67 (defcustom pop3-password-required t
68 "*Non-nil if a password is required when connecting to POP server."
69 :version
"22.1" ;; Oort Gnus
73 ;; Should this be customizable?
74 (defvar pop3-password nil
75 "*Password to use when connecting to POP server.")
77 (defcustom pop3-authentication-scheme
'pass
78 "*POP3 authentication scheme.
79 Defaults to `pass', for the standard USER/PASS authentication. The other
80 valid value is 'apop'."
81 :type
'(choice (const :tag
"Normal user/password" pass
)
82 (const :tag
"APOP" apop
))
83 :version
"22.1" ;; Oort Gnus
86 (defcustom pop3-leave-mail-on-server nil
87 "*Non-nil if the mail is to be left on the POP server after fetching.
89 If `pop3-leave-mail-on-server' is non-nil the mail is to be left
90 on the POP server after fetching. Note that POP servers maintain
91 no state information between sessions, so what the client
92 believes is there and what is actually there may not match up.
93 If they do not, then you may get duplicate mails or the whole
94 thing can fall apart and leave you with a corrupt mailbox."
95 ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org:
96 ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de
97 ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org
98 ;; Any volunteer to re-implement this?
99 :version
"22.1" ;; Oort Gnus
103 (defvar pop3-timestamp nil
104 "Timestamp returned when initially connected to the POP server.
105 Used for APOP authentication.")
107 (defvar pop3-read-point nil
)
108 (defvar pop3-debug nil
)
110 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
111 (defvar pop3-read-timeout
112 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
113 (symbol-name system-type
))
114 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
116 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
118 ;; There should probably be a runtime test to determine the timing
119 ;; resolution, or a primitive to report it. I don't know off-hand
120 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
121 ;; could round up non-zero timeouts to a minimum of 1.0?
124 "How long pop3 should wait between checking for the end of output.
125 Shorter values mean quicker response, but are more CPU intensive.")
127 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
128 (defun pop3-accept-process-output (process)
129 (accept-process-output
131 (truncate pop3-read-timeout
)
132 (truncate (* (- pop3-read-timeout
133 (truncate pop3-read-timeout
))
136 (defun pop3-movemail (&optional crashbox
)
137 "Transfer contents of a maildrop to the specified CRASHBOX."
138 (or crashbox
(setq crashbox
(expand-file-name "~/.crashbox")))
139 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
140 (crashbuf (get-buffer-create " *pop3-retr*"))
143 (pop3-password pop3-password
))
144 ;; for debugging only
145 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
146 ;; query for password
147 (if (and pop3-password-required
(not pop3-password
))
149 (read-passwd (format "Password for %s: " pop3-maildrop
))))
150 (cond ((equal 'apop pop3-authentication-scheme
)
151 (pop3-apop process pop3-maildrop
))
152 ((equal 'pass pop3-authentication-scheme
)
153 (pop3-user process pop3-maildrop
)
155 (t (error "Invalid POP3 authentication scheme")))
156 (setq message-count
(car (pop3-stat process
)))
158 (while (<= n message-count
)
159 (message "Retrieving message %d of %d from %s..."
160 n message-count pop3-mailhost
)
161 (pop3-retr process n crashbuf
)
163 (set-buffer crashbuf
)
164 (let ((coding-system-for-write 'binary
))
165 (write-region (point-min) (point-max) crashbox t
'nomesg
))
166 (set-buffer (process-buffer process
))
167 (while (> (buffer-size) 5000)
168 (goto-char (point-min))
170 (delete-region (point-min) (point))))
171 (unless pop3-leave-mail-on-server
172 (pop3-dele process n
))
174 (if pop3-debug
(sit-for 1) (sit-for 0.1))) ; why?
175 (when (and pop3-leave-mail-on-server
177 (message "pop3.el doesn't support UIDL. Setting `pop3-leave-mail-on-server'
178 to %s might not give the result you'd expect." pop3-leave-mail-on-server
)
181 (kill-buffer crashbuf
))
184 (defun pop3-get-message-count ()
185 "Return the number of messages in the maildrop."
186 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
188 (pop3-password pop3-password
))
189 ;; for debugging only
190 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
191 ;; query for password
192 (if (and pop3-password-required
(not pop3-password
))
194 (read-passwd (format "Password for %s: " pop3-maildrop
))))
195 (cond ((equal 'apop pop3-authentication-scheme
)
196 (pop3-apop process pop3-maildrop
))
197 ((equal 'pass pop3-authentication-scheme
)
198 (pop3-user process pop3-maildrop
)
200 (t (error "Invalid POP3 authentication scheme")))
201 (setq message-count
(car (pop3-stat process
)))
205 (autoload 'open-tls-stream
"tls")
206 (autoload 'starttls-open-stream
"starttls")
207 (autoload 'starttls-negotiate
"starttls") ; avoid warning
209 (defcustom pop3-stream-type nil
210 "*Transport security type for POP3 connexions.
211 This may be either nil (plain connexion), `ssl' (use an
212 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
213 to turn on TLS security after opening the stream). However, if
214 this is nil, `ssl' is assumed for connexions to port
216 :version
"23.1" ;; No Gnus
218 :type
'(choice (const :tag
"Plain" nil
)
219 (const :tag
"SSL/TLS" ssl
)
222 (defun pop3-open-server (mailhost port
)
223 "Open TCP connection to MAILHOST on PORT.
224 Returns the process associated with the connection."
225 (let ((coding-system-for-read 'binary
)
226 (coding-system-for-write 'binary
)
229 (set-buffer (get-buffer-create (concat " trace of POP session to "
232 (setq pop3-read-point
(point-min))
235 ((or (eq pop3-stream-type
'ssl
)
236 (and (not pop3-stream-type
) (member port
'(995 "pop3s"))))
237 ;; gnutls-cli, openssl don't accept service names
238 (if (or (equal port
"pop3s")
241 (let ((process (open-tls-stream "POP" (current-buffer)
244 ;; There's a load of info printed that needs deleting.
247 ;; - either we received the +OK line
248 ;; - or accept-process-output timed out without getting
251 (setq again
(memq (process-status process
)
253 (setq again
(pop3-accept-process-output process
))
254 (goto-char (point-max))
256 (cond ((looking-at "\\+OK")
258 (delete-region (point-min) (point)))
261 (error "POP SSL connexion failed")))))
263 ((eq pop3-stream-type
'starttls
)
264 ;; gnutls-cli, openssl don't accept service names
265 (if (equal port
"pop3")
267 (let ((process (starttls-open-stream "POP" (current-buffer)
268 mailhost
(or port
110))))
269 (pop3-send-command process
"STLS")
270 (let ((response (pop3-read-response process t
)))
271 (if (and response
(string-match "+OK" response
))
272 (starttls-negotiate process
)
274 (error "POP server doesn't support starttls")))
277 (open-network-stream "POP" (current-buffer) mailhost port
))))
278 (let ((response (pop3-read-response process t
)))
280 (substring response
(or (string-match "<" response
) 0)
281 (+ 1 (or (string-match ">" response
) -
1)))))
286 (defun pop3-process-filter (process output
)
288 (set-buffer (process-buffer process
))
289 (goto-char (point-max))
292 (defun pop3-send-command (process command
)
293 (set-buffer (process-buffer process
))
294 (goto-char (point-max))
295 ;; (if (= (aref command 0) ?P)
296 ;; (insert "PASS <omitted>\r\n")
297 ;; (insert command "\r\n"))
298 (setq pop3-read-point
(point))
299 (goto-char (point-max))
300 (process-send-string process
(concat command
"\r\n")))
302 (defun pop3-read-response (process &optional return
)
303 "Read the response from the server.
304 Return the response string if optional second argument is non-nil."
305 (let ((case-fold-search nil
)
308 (set-buffer (process-buffer process
))
309 (goto-char pop3-read-point
)
310 (while (and (memq (process-status process
) '(open run
))
311 (not (search-forward "\r\n" nil t
)))
312 (pop3-accept-process-output process
)
313 (goto-char pop3-read-point
))
314 (setq match-end
(point))
315 (goto-char pop3-read-point
)
316 (if (looking-at "-ERR")
317 (error "%s" (buffer-substring (point) (- match-end
2)))
318 (if (not (looking-at "+OK"))
319 (progn (setq pop3-read-point match-end
) nil
)
320 (setq pop3-read-point match-end
)
322 (buffer-substring (point) match-end
)
326 (defun pop3-clean-region (start end
)
327 (setq end
(set-marker (make-marker) end
))
330 (while (and (< (point) end
) (search-forward "\r\n" end t
))
331 (replace-match "\n" t t
))
333 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
334 (replace-match "" t t
)
336 (set-marker end nil
))
338 ;; Copied from message-make-date.
339 (defun pop3-make-date (&optional now
)
340 "Make a valid date header.
341 If NOW, use that time instead."
342 (require 'parse-time
)
343 (let* ((now (or now
(current-time)))
344 (zone (nth 8 (decode-time now
)))
348 (setq zone
(- zone
)))
350 (format-time-string "%d" now
)
351 ;; The month name of the %b spec is locale-specific. Pfff.
353 (capitalize (car (rassoc (nth 4 (decode-time now
))
354 parse-time-months
))))
355 (format-time-string "%Y %H:%M:%S " now
)
356 ;; We do all of this because XEmacs doesn't have the %z spec.
357 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
359 (defun pop3-munge-message-separator (start end
)
360 "Check to see if a message separator exists. If not, generate one."
363 (narrow-to-region start end
)
364 (goto-char (point-min))
365 (if (not (or (looking-at "From .?") ; Unix mail
366 (looking-at "\001\001\001\001\n") ; MMDF
367 (looking-at "BABYL OPTIONS:") ; Babyl
369 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
370 (tdate (mail-fetch-field "Date"))
371 (date (split-string (or (and tdate
372 (not (string= "" tdate
))
377 ;; sample date formats I have seen
378 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
379 ;; Date: 08 Jul 1996 23:22:24 -0400
381 ;; Tue Jul 9 09:04:21 1996
383 ;; Fixme: This should use timezone on the date field contents.
386 "Tue Jan 1 00:00:0 1900")
387 ((string-match "[A-Z]" (nth 0 date
))
388 (format "%s %s %s %s %s"
389 (nth 0 date
) (nth 2 date
) (nth 1 date
)
390 (nth 4 date
) (nth 3 date
)))
392 ;; this really needs to be better but I don't feel
393 ;; like writing a date to day converter.
394 (format "Sun %s %s %s %s"
395 (nth 1 date
) (nth 0 date
)
396 (nth 3 date
) (nth 2 date
)))
398 (setq From_
(format "\nFrom %s %s\n" from date
))
399 (while (string-match "," From_
)
400 (setq From_
(concat (substring From_
0 (match-beginning 0))
401 (substring From_
(match-end 0)))))
402 (goto-char (point-min))
404 (if (search-forward "\n\n" nil t
)
406 (goto-char (point-max))
408 (narrow-to-region (point) (point-max))
409 (let ((size (- (point-max) (point-min))))
410 (goto-char (point-min))
413 (insert (format "Content-Length: %s\n" size
)))
418 ;; AUTHORIZATION STATE
420 (defun pop3-user (process user
)
421 "Send USER information to POP3 server."
422 (pop3-send-command process
(format "USER %s" user
))
423 (let ((response (pop3-read-response process t
)))
424 (if (not (and response
(string-match "+OK" response
)))
425 (error "USER %s not valid" user
))))
427 (defun pop3-pass (process)
428 "Send authentication information to the server."
429 (pop3-send-command process
(format "PASS %s" pop3-password
))
430 (let ((response (pop3-read-response process t
)))
431 (if (not (and response
(string-match "+OK" response
)))
432 (pop3-quit process
))))
434 (defun pop3-apop (process user
)
435 "Send alternate authentication information to the server."
436 (let ((pass pop3-password
))
437 (if (and pop3-password-required
(not pass
))
439 (read-passwd (format "Password for %s: " pop3-maildrop
))))
441 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
442 (pop3-send-command process
(format "APOP %s %s" user hash
))
443 (let ((response (pop3-read-response process t
)))
444 (if (not (and response
(string-match "+OK" response
)))
445 (pop3-quit process
)))))
450 (defun pop3-stat (process)
451 "Return the number of messages in the maildrop and the maildrop's size."
452 (pop3-send-command process
"STAT")
453 (let ((response (pop3-read-response process t
)))
454 (list (string-to-number (nth 1 (split-string response
" ")))
455 (string-to-number (nth 2 (split-string response
" "))))
458 (defun pop3-list (process &optional msg
)
459 "Scan listing of available messages.
460 This function currently does nothing.")
462 (defun pop3-retr (process msg crashbuf
)
463 "Retrieve message-id MSG to buffer CRASHBUF."
464 (pop3-send-command process
(format "RETR %s" msg
))
465 (pop3-read-response process
)
466 (let ((start pop3-read-point
) end
)
468 (set-buffer (process-buffer process
))
469 (while (not (re-search-forward "^\\.\r\n" nil t
))
470 (pop3-accept-process-output process
)
472 (setq pop3-read-point
(point-marker))
473 ;; this code does not seem to work for some POP servers...
474 ;; and I cannot figure out why not.
475 ;; (goto-char (match-beginning 0))
477 ;; (if (not (looking-at "\r\n"))
479 ;; (re-search-forward "\\.\r\n")
480 (goto-char (match-beginning 0))
481 (setq end
(point-marker))
482 (pop3-clean-region start end
)
483 (pop3-munge-message-separator start end
)
485 (set-buffer crashbuf
)
487 (copy-to-buffer crashbuf start end
)
488 (delete-region start end
)
491 (defun pop3-dele (process msg
)
492 "Mark message-id MSG as deleted."
493 (pop3-send-command process
(format "DELE %s" msg
))
494 (pop3-read-response process
))
496 (defun pop3-noop (process msg
)
498 (pop3-send-command process
"NOOP")
499 (pop3-read-response process
))
501 (defun pop3-last (process)
502 "Return highest accessed message-id number for the session."
503 (pop3-send-command process
"LAST")
504 (let ((response (pop3-read-response process t
)))
505 (string-to-number (nth 1 (split-string response
" ")))
508 (defun pop3-rset (process)
509 "Remove all delete marks from current maildrop."
510 (pop3-send-command process
"RSET")
511 (pop3-read-response process
))
515 (defun pop3-quit (process)
516 "Close connection to POP3 server.
517 Tell server to remove all messages marked as deleted, unlock the maildrop,
518 and close the connection."
519 (pop3-send-command process
"QUIT")
520 (pop3-read-response process t
)
523 (set-buffer (process-buffer process
))
524 (goto-char (point-max))
525 (delete-process process
))))
527 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
529 ;;; AUTHORIZATION STATE
531 ;; Initial TCP connection
533 ;; Restrictions: none
534 ;; Possible responses:
535 ;; +OK [POP3 server ready]
538 ;; Arguments: a server specific user-id (required)
539 ;; Restrictions: authorization state [after unsuccessful USER or PASS
540 ;; Possible responses:
541 ;; +OK [valid user-id]
542 ;; -ERR [invalid user-id]
545 ;; Arguments: a server/user-id specific password (required)
546 ;; Restrictions: authorization state, after successful USER
547 ;; Possible responses:
548 ;; +OK [maildrop locked and ready]
549 ;; -ERR [invalid password]
550 ;; -ERR [unable to lock maildrop]
554 ;; Restrictions: Only permitted in AUTHORIZATION state.
555 ;; Possible responses:
559 ;;; TRANSACTION STATE
563 ;; Restrictions: transaction state
564 ;; Possible responses:
565 ;; +OK nn mm [# of messages, size of maildrop]
568 ;; Arguments: a message-id (optional)
569 ;; Restrictions: transaction state; msg must not be deleted
570 ;; Possible responses:
571 ;; +OK [scan listing follows]
572 ;; -ERR [no such message]
575 ;; Arguments: a message-id (required)
576 ;; Restrictions: transaction state; msg must not be deleted
577 ;; Possible responses:
578 ;; +OK [message contents follow]
579 ;; -ERR [no such message]
582 ;; Arguments: a message-id (required)
583 ;; Restrictions: transaction state; msg must not be deleted
584 ;; Possible responses:
585 ;; +OK [message deleted]
586 ;; -ERR [no such message]
590 ;; Restrictions: transaction state
591 ;; Possible responses:
596 ;; Restrictions: transaction state
597 ;; Possible responses:
598 ;; +OK nn [highest numbered message accessed]
602 ;; Restrictions: transaction state
603 ;; Possible responses:
604 ;; +OK [all delete marks removed]
610 ;; Restrictions: none
611 ;; Possible responses:
612 ;; +OK [TCP connection closed]
616 ;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
617 ;;; pop3.el ends here