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, 2009, 2010 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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
28 ;; are implemented. The LIST command has not been implemented due to lack
29 ;; of actual usefulness.
30 ;; The optional POP3 command TOP has not been implemented.
32 ;; This program was inspired by Kyle E. Jones's vm-pop program.
37 (defvar parse-time-months
)
40 "Post Office Protocol."
44 (defcustom pop3-maildrop
(or (user-login-name)
48 :version
"22.1" ;; Oort Gnus
52 (defcustom pop3-mailhost
(or (getenv "MAILHOST") ;; nil -> mismatch
55 :version
"22.1" ;; Oort Gnus
59 (defcustom pop3-port
110
61 :version
"22.1" ;; Oort Gnus
65 (defcustom pop3-password-required t
66 "*Non-nil if a password is required when connecting to POP server."
67 :version
"22.1" ;; Oort Gnus
71 ;; Should this be customizable?
72 (defvar pop3-password nil
73 "*Password to use when connecting to POP server.")
75 (defcustom pop3-authentication-scheme
'pass
76 "*POP3 authentication scheme.
77 Defaults to `pass', for the standard USER/PASS authentication. The other
78 valid value is 'apop'."
79 :type
'(choice (const :tag
"Normal user/password" pass
)
80 (const :tag
"APOP" apop
))
81 :version
"22.1" ;; Oort Gnus
84 (defcustom pop3-leave-mail-on-server nil
85 "*Non-nil if the mail is to be left on the POP server after fetching.
87 If `pop3-leave-mail-on-server' is non-nil the mail is to be left
88 on the POP server after fetching. Note that POP servers maintain
89 no state information between sessions, so what the client
90 believes is there and what is actually there may not match up.
91 If they do not, then you may get duplicate mails or the whole
92 thing can fall apart and leave you with a corrupt mailbox."
93 ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org:
94 ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de
95 ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org
96 ;; Any volunteer to re-implement this?
97 :version
"22.1" ;; Oort Gnus
101 (defcustom pop3-display-message-size-flag t
102 "*If non-nil, display the size of the message that is being fetched."
103 :version
"22.1" ;; Oort Gnus
107 (defvar pop3-timestamp nil
108 "Timestamp returned when initially connected to the POP server.
109 Used for APOP authentication.")
111 (defvar pop3-read-point nil
)
112 (defvar pop3-debug nil
)
114 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
115 ;; comments there for explanations about the values.
118 (if (and (fboundp 'nnheader-accept-process-output
)
119 (boundp 'nnheader-read-timeout
))
120 (defalias 'pop3-accept-process-output
'nnheader-accept-process-output
)
121 ;; Borrowed from `nnheader.el':
122 (defvar pop3-read-timeout
123 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
124 (symbol-name system-type
))
127 "How long pop3 should wait between checking for the end of output.
128 Shorter values mean quicker response, but are more CPU intensive.")
129 (defun pop3-accept-process-output (process)
130 (accept-process-output
132 (truncate pop3-read-timeout
)
133 (truncate (* (- pop3-read-timeout
134 (truncate pop3-read-timeout
))
137 (defun pop3-movemail (&optional crashbox
)
138 "Transfer contents of a maildrop to the specified CRASHBOX."
139 (or crashbox
(setq crashbox
(expand-file-name "~/.crashbox")))
140 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
141 (crashbuf (get-buffer-create " *pop3-retr*"))
145 (pop3-password pop3-password
))
146 ;; for debugging only
147 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
148 ;; query for password
149 (if (and pop3-password-required
(not pop3-password
))
151 (read-passwd (format "Password for %s: " pop3-maildrop
))))
152 (cond ((equal 'apop pop3-authentication-scheme
)
153 (pop3-apop process pop3-maildrop
))
154 ((equal 'pass pop3-authentication-scheme
)
155 (pop3-user process pop3-maildrop
)
157 (t (error "Invalid POP3 authentication scheme")))
158 (setq message-count
(car (pop3-stat process
)))
159 (when (and pop3-display-message-size-flag
161 (setq message-sizes
(pop3-list process
)))
163 (while (<= n message-count
)
164 (if pop3-display-message-size-flag
165 (message "Retrieving message %d of %d from %s... (%.1fk)"
166 n message-count pop3-mailhost
167 (/ (cdr (assoc n message-sizes
))
169 (message "Retrieving message %d of %d from %s..."
170 n message-count pop3-mailhost
))
171 (pop3-retr process n crashbuf
)
173 (set-buffer crashbuf
)
174 (let ((coding-system-for-write 'binary
))
175 (write-region (point-min) (point-max) crashbox t
'nomesg
))
176 (set-buffer (process-buffer process
))
177 (while (> (buffer-size) 5000)
178 (goto-char (point-min))
180 (delete-region (point-min) (point))))
181 (unless pop3-leave-mail-on-server
182 (pop3-dele process n
))
184 (pop3-accept-process-output process
))
185 (when (and pop3-leave-mail-on-server
187 (message "pop3.el doesn't support UIDL. Setting `pop3-leave-mail-on-server'
188 to %s might not give the result you'd expect." pop3-leave-mail-on-server
)
191 (kill-buffer crashbuf
))
194 (defun pop3-get-message-count ()
195 "Return the number of messages in the maildrop."
196 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
198 (pop3-password pop3-password
))
199 ;; for debugging only
200 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
201 ;; query for password
202 (if (and pop3-password-required
(not pop3-password
))
204 (read-passwd (format "Password for %s: " pop3-maildrop
))))
205 (cond ((equal 'apop pop3-authentication-scheme
)
206 (pop3-apop process pop3-maildrop
))
207 ((equal 'pass pop3-authentication-scheme
)
208 (pop3-user process pop3-maildrop
)
210 (t (error "Invalid POP3 authentication scheme")))
211 (setq message-count
(car (pop3-stat process
)))
215 (autoload 'open-tls-stream
"tls")
216 (autoload 'starttls-open-stream
"starttls")
217 (autoload 'starttls-negotiate
"starttls") ; avoid warning
219 (defcustom pop3-stream-type nil
220 "*Transport security type for POP3 connexions.
221 This may be either nil (plain connexion), `ssl' (use an
222 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
223 to turn on TLS security after opening the stream). However, if
224 this is nil, `ssl' is assumed for connexions to port
226 :version
"23.1" ;; No Gnus
228 :type
'(choice (const :tag
"Plain" nil
)
229 (const :tag
"SSL/TLS" ssl
)
232 (defun pop3-open-server (mailhost port
)
233 "Open TCP connection to MAILHOST on PORT.
234 Returns the process associated with the connection."
235 (let ((coding-system-for-read 'binary
)
236 (coding-system-for-write 'binary
)
239 (set-buffer (get-buffer-create (concat " trace of POP session to "
242 (setq pop3-read-point
(point-min))
245 ((or (eq pop3-stream-type
'ssl
)
246 (and (not pop3-stream-type
) (member port
'(995 "pop3s"))))
247 ;; gnutls-cli, openssl don't accept service names
248 (if (or (equal port
"pop3s")
251 (let ((process (open-tls-stream "POP" (current-buffer)
254 ;; There's a load of info printed that needs deleting.
257 ;; - either we received the +OK line
258 ;; - or accept-process-output timed out without getting
261 (setq again
(memq (process-status process
)
263 (setq again
(pop3-accept-process-output process
))
264 (goto-char (point-max))
266 (cond ((looking-at "\\+OK")
268 (delete-region (point-min) (point)))
271 (error "POP SSL connexion failed")))))
273 ((eq pop3-stream-type
'starttls
)
274 ;; gnutls-cli, openssl don't accept service names
275 (if (equal port
"pop3")
277 (let ((process (starttls-open-stream "POP" (current-buffer)
278 mailhost
(or port
110))))
279 (pop3-send-command process
"STLS")
280 (let ((response (pop3-read-response process t
)))
281 (if (and response
(string-match "+OK" response
))
282 (starttls-negotiate process
)
284 (error "POP server doesn't support starttls")))
287 (open-network-stream "POP" (current-buffer) mailhost port
))))
288 (let ((response (pop3-read-response process t
)))
290 (substring response
(or (string-match "<" response
) 0)
291 (+ 1 (or (string-match ">" response
) -
1)))))
296 (defun pop3-process-filter (process output
)
298 (set-buffer (process-buffer process
))
299 (goto-char (point-max))
302 (defun pop3-send-command (process command
)
303 (set-buffer (process-buffer process
))
304 (goto-char (point-max))
305 ;; (if (= (aref command 0) ?P)
306 ;; (insert "PASS <omitted>\r\n")
307 ;; (insert command "\r\n"))
308 (setq pop3-read-point
(point))
309 (goto-char (point-max))
310 (process-send-string process
(concat command
"\r\n")))
312 (defun pop3-read-response (process &optional return
)
313 "Read the response from the server.
314 Return the response string if optional second argument is non-nil."
315 (let ((case-fold-search nil
)
318 (set-buffer (process-buffer process
))
319 (goto-char pop3-read-point
)
320 (while (and (memq (process-status process
) '(open run
))
321 (not (search-forward "\r\n" nil t
)))
322 (pop3-accept-process-output process
)
323 (goto-char pop3-read-point
))
324 (setq match-end
(point))
325 (goto-char pop3-read-point
)
326 (if (looking-at "-ERR")
327 (error "%s" (buffer-substring (point) (- match-end
2)))
328 (if (not (looking-at "+OK"))
329 (progn (setq pop3-read-point match-end
) nil
)
330 (setq pop3-read-point match-end
)
332 (buffer-substring (point) match-end
)
336 (defun pop3-clean-region (start end
)
337 (setq end
(set-marker (make-marker) end
))
340 (while (and (< (point) end
) (search-forward "\r\n" end t
))
341 (replace-match "\n" t t
))
343 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
344 (replace-match "" t t
)
346 (set-marker end nil
))
348 ;; Copied from message-make-date.
349 (defun pop3-make-date (&optional now
)
350 "Make a valid date header.
351 If NOW, use that time instead."
352 (require 'parse-time
)
353 (let* ((now (or now
(current-time)))
354 (zone (nth 8 (decode-time now
)))
358 (setq zone
(- zone
)))
360 (format-time-string "%d" now
)
361 ;; The month name of the %b spec is locale-specific. Pfff.
363 (capitalize (car (rassoc (nth 4 (decode-time now
))
364 parse-time-months
))))
365 (format-time-string "%Y %H:%M:%S " now
)
366 ;; We do all of this because XEmacs doesn't have the %z spec.
367 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
369 (defun pop3-munge-message-separator (start end
)
370 "Check to see if a message separator exists. If not, generate one."
373 (narrow-to-region start end
)
374 (goto-char (point-min))
375 (if (not (or (looking-at "From .?") ; Unix mail
376 (looking-at "\001\001\001\001\n") ; MMDF
377 (looking-at "BABYL OPTIONS:") ; Babyl
379 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
380 (tdate (mail-fetch-field "Date"))
381 (date (split-string (or (and tdate
382 (not (string= "" tdate
))
387 ;; sample date formats I have seen
388 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
389 ;; Date: 08 Jul 1996 23:22:24 -0400
391 ;; Tue Jul 9 09:04:21 1996
393 ;; Fixme: This should use timezone on the date field contents.
396 "Tue Jan 1 00:00:0 1900")
397 ((string-match "[A-Z]" (nth 0 date
))
398 (format "%s %s %s %s %s"
399 (nth 0 date
) (nth 2 date
) (nth 1 date
)
400 (nth 4 date
) (nth 3 date
)))
402 ;; this really needs to be better but I don't feel
403 ;; like writing a date to day converter.
404 (format "Sun %s %s %s %s"
405 (nth 1 date
) (nth 0 date
)
406 (nth 3 date
) (nth 2 date
)))
408 (setq From_
(format "\nFrom %s %s\n" from date
))
409 (while (string-match "," From_
)
410 (setq From_
(concat (substring From_
0 (match-beginning 0))
411 (substring From_
(match-end 0)))))
412 (goto-char (point-min))
414 (if (search-forward "\n\n" nil t
)
416 (goto-char (point-max))
418 (narrow-to-region (point) (point-max))
419 (let ((size (- (point-max) (point-min))))
420 (goto-char (point-min))
423 (insert (format "Content-Length: %s\n" size
)))
428 ;; AUTHORIZATION STATE
430 (defun pop3-user (process user
)
431 "Send USER information to POP3 server."
432 (pop3-send-command process
(format "USER %s" user
))
433 (let ((response (pop3-read-response process t
)))
434 (if (not (and response
(string-match "+OK" response
)))
435 (error "USER %s not valid" user
))))
437 (defun pop3-pass (process)
438 "Send authentication information to the server."
439 (pop3-send-command process
(format "PASS %s" pop3-password
))
440 (let ((response (pop3-read-response process t
)))
441 (if (not (and response
(string-match "+OK" response
)))
442 (pop3-quit process
))))
444 (defun pop3-apop (process user
)
445 "Send alternate authentication information to the server."
446 (let ((pass pop3-password
))
447 (if (and pop3-password-required
(not pass
))
449 (read-passwd (format "Password for %s: " pop3-maildrop
))))
451 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
452 (pop3-send-command process
(format "APOP %s %s" user hash
))
453 (let ((response (pop3-read-response process t
)))
454 (if (not (and response
(string-match "+OK" response
)))
455 (pop3-quit process
)))))
460 (defun pop3-stat (process)
461 "Return the number of messages in the maildrop and the maildrop's size."
462 (pop3-send-command process
"STAT")
463 (let ((response (pop3-read-response process t
)))
464 (list (string-to-number (nth 1 (split-string response
" ")))
465 (string-to-number (nth 2 (split-string response
" "))))
468 (defun pop3-list (process &optional msg
)
469 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
470 Otherwise, return the size of the message-id MSG"
471 (pop3-send-command process
(if msg
472 (format "LIST %d" msg
)
474 (let ((response (pop3-read-response process t
)))
476 (string-to-number (nth 2 (split-string response
" ")))
477 (let ((start pop3-read-point
) end
)
479 (set-buffer (process-buffer process
))
480 (while (not (re-search-forward "^\\.\r\n" nil t
))
481 (pop3-accept-process-output process
)
483 (setq pop3-read-point
(point-marker))
484 (goto-char (match-beginning 0))
485 (setq end
(point-marker))
486 (mapcar #'(lambda (s) (let ((split (split-string s
" ")))
487 (cons (string-to-number (nth 0 split
))
488 (string-to-number (nth 1 split
)))))
489 (delete "" (split-string (buffer-substring start end
)
492 (defun pop3-retr (process msg crashbuf
)
493 "Retrieve message-id MSG to buffer CRASHBUF."
494 (pop3-send-command process
(format "RETR %s" msg
))
495 (pop3-read-response process
)
496 (let ((start pop3-read-point
) end
)
498 (set-buffer (process-buffer process
))
499 (while (not (re-search-forward "^\\.\r\n" nil t
))
500 (pop3-accept-process-output process
)
502 (setq pop3-read-point
(point-marker))
503 ;; this code does not seem to work for some POP servers...
504 ;; and I cannot figure out why not.
505 ;; (goto-char (match-beginning 0))
507 ;; (if (not (looking-at "\r\n"))
509 ;; (re-search-forward "\\.\r\n")
510 (goto-char (match-beginning 0))
511 (setq end
(point-marker))
512 (pop3-clean-region start end
)
513 (pop3-munge-message-separator start end
)
515 (set-buffer crashbuf
)
517 (copy-to-buffer crashbuf start end
)
518 (delete-region start end
)
521 (defun pop3-dele (process msg
)
522 "Mark message-id MSG as deleted."
523 (pop3-send-command process
(format "DELE %s" msg
))
524 (pop3-read-response process
))
526 (defun pop3-noop (process msg
)
528 (pop3-send-command process
"NOOP")
529 (pop3-read-response process
))
531 (defun pop3-last (process)
532 "Return highest accessed message-id number for the session."
533 (pop3-send-command process
"LAST")
534 (let ((response (pop3-read-response process t
)))
535 (string-to-number (nth 1 (split-string response
" ")))
538 (defun pop3-rset (process)
539 "Remove all delete marks from current maildrop."
540 (pop3-send-command process
"RSET")
541 (pop3-read-response process
))
545 (defun pop3-quit (process)
546 "Close connection to POP3 server.
547 Tell server to remove all messages marked as deleted, unlock the maildrop,
548 and close the connection."
549 (pop3-send-command process
"QUIT")
550 (pop3-read-response process t
)
553 (set-buffer (process-buffer process
))
554 (goto-char (point-max))
555 (delete-process process
))))
557 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
559 ;;; AUTHORIZATION STATE
561 ;; Initial TCP connection
563 ;; Restrictions: none
564 ;; Possible responses:
565 ;; +OK [POP3 server ready]
568 ;; Arguments: a server specific user-id (required)
569 ;; Restrictions: authorization state [after unsuccessful USER or PASS
570 ;; Possible responses:
571 ;; +OK [valid user-id]
572 ;; -ERR [invalid user-id]
575 ;; Arguments: a server/user-id specific password (required)
576 ;; Restrictions: authorization state, after successful USER
577 ;; Possible responses:
578 ;; +OK [maildrop locked and ready]
579 ;; -ERR [invalid password]
580 ;; -ERR [unable to lock maildrop]
584 ;; Restrictions: Only permitted in AUTHORIZATION state.
585 ;; Possible responses:
589 ;;; TRANSACTION STATE
593 ;; Restrictions: transaction state
594 ;; Possible responses:
595 ;; +OK nn mm [# of messages, size of maildrop]
598 ;; Arguments: a message-id (optional)
599 ;; Restrictions: transaction state; msg must not be deleted
600 ;; Possible responses:
601 ;; +OK [scan listing follows]
602 ;; -ERR [no such message]
605 ;; Arguments: a message-id (required)
606 ;; Restrictions: transaction state; msg must not be deleted
607 ;; Possible responses:
608 ;; +OK [message contents follow]
609 ;; -ERR [no such message]
612 ;; Arguments: a message-id (required)
613 ;; Restrictions: transaction state; msg must not be deleted
614 ;; Possible responses:
615 ;; +OK [message deleted]
616 ;; -ERR [no such message]
620 ;; Restrictions: transaction state
621 ;; Possible responses:
626 ;; Restrictions: transaction state
627 ;; Possible responses:
628 ;; +OK nn [highest numbered message accessed]
632 ;; Restrictions: transaction state
633 ;; Possible responses:
634 ;; +OK [all delete marks removed]
640 ;; Restrictions: none
641 ;; Possible responses:
642 ;; +OK [TCP connection closed]
646 ;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
647 ;;; pop3.el ends here