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.
36 (eval-when-compile (require 'cl
))
38 (defvar parse-time-months
)
41 "Post Office Protocol."
45 (defcustom pop3-maildrop
(or (user-login-name)
49 :version
"22.1" ;; Oort Gnus
53 (defcustom pop3-mailhost
(or (getenv "MAILHOST") ;; nil -> mismatch
56 :version
"22.1" ;; Oort Gnus
60 (defcustom pop3-port
110
62 :version
"22.1" ;; Oort Gnus
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
72 ;; Should this be customizable?
73 (defvar pop3-password nil
74 "*Password to use when connecting to POP server.")
76 (defcustom pop3-authentication-scheme
'pass
77 "*POP3 authentication scheme.
78 Defaults to `pass', for the standard USER/PASS authentication. The other
79 valid value is 'apop'."
80 :type
'(choice (const :tag
"Normal user/password" pass
)
81 (const :tag
"APOP" apop
))
82 :version
"22.1" ;; Oort Gnus
85 (defcustom pop3-stream-length
100
86 "How many messages should be requested at one time.
87 The lower the number, the more latency-sensitive the fetching
88 will be. If your pop3 server doesn't support streaming at all,
94 (defcustom pop3-leave-mail-on-server nil
95 "*Non-nil if the mail is to be left on the POP server after fetching.
97 If `pop3-leave-mail-on-server' is non-nil the mail is to be left
98 on the POP server after fetching. Note that POP servers maintain
99 no state information between sessions, so what the client
100 believes is there and what is actually there may not match up.
101 If they do not, then you may get duplicate mails or the whole
102 thing can fall apart and leave you with a corrupt mailbox."
103 ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org:
104 ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de
105 ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org
106 ;; Any volunteer to re-implement this?
107 :version
"22.1" ;; Oort Gnus
111 (defvar pop3-timestamp nil
112 "Timestamp returned when initially connected to the POP server.
113 Used for APOP authentication.")
115 (defvar pop3-read-point nil
)
116 (defvar pop3-debug nil
)
118 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
119 ;; comments there for explanations about the values.
122 (if (and (fboundp 'nnheader-accept-process-output
)
123 (boundp 'nnheader-read-timeout
))
124 (defalias 'pop3-accept-process-output
'nnheader-accept-process-output
)
125 ;; Borrowed from `nnheader.el':
126 (defvar pop3-read-timeout
127 (if (string-match "windows-nt\\|os/2\\|cygwin"
128 (symbol-name system-type
))
131 "How long pop3 should wait between checking for the end of output.
132 Shorter values mean quicker response, but are more CPU intensive.")
133 (defun pop3-accept-process-output (process)
134 (accept-process-output
136 (truncate pop3-read-timeout
)
137 (truncate (* (- pop3-read-timeout
138 (truncate pop3-read-timeout
))
142 (defun pop3-movemail (file)
143 "Transfer contents of a maildrop to the specified FILE.
144 Use streaming commands."
145 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
146 message-count message-total-size
)
148 (with-current-buffer (process-buffer process
)
149 (let ((size (pop3-stat process
)))
150 (setq message-count
(car size
)
151 message-total-size
(cadr size
)))
152 (when (> message-count
0)
153 (pop3-send-streaming-command
154 process
"RETR" message-count message-total-size
)
155 (pop3-write-to-file file
)
156 (unless pop3-leave-mail-on-server
157 (pop3-send-streaming-command
158 process
"DELE" message-count nil
))))
162 (defun pop3-send-streaming-command (process command count total-size
)
166 (process-send-string process
(format "%s %d\r\n" command i
))
167 ;; Only do 100 messages at a time to avoid pipe stalls.
168 (when (zerop (% i pop3-stream-length
))
169 (pop3-wait-for-messages process i total-size
))
171 (pop3-wait-for-messages process count total-size
))
173 (defun pop3-wait-for-messages (process count total-size
)
174 (while (< (pop3-number-of-responses total-size
) count
)
176 (message "pop3 retrieved %dKB (%d%%)"
177 (truncate (/ (buffer-size) 1000))
178 (truncate (* (/ (* (buffer-size) 1.0)
180 (pop3-accept-process-output process
)))
182 (defun pop3-write-to-file (file)
183 (let ((pop-buffer (current-buffer))
188 (setq temp-buffer
(current-buffer))
189 (with-current-buffer pop-buffer
190 (goto-char (point-min))
191 (while (re-search-forward "^\\+OK" nil t
)
194 (when (re-search-forward "^\\.\r?\n" nil t
)
198 (with-current-buffer temp-buffer
199 (goto-char (point-max))
200 (let ((hstart (point)))
201 (insert-buffer-substring pop-buffer beg end
)
202 (pop3-clean-region hstart
(point))
203 (goto-char (point-max))
204 (pop3-munge-message-separator hstart
(point))
205 (goto-char (point-max))))))
206 (let ((coding-system-for-write 'binary
))
207 (goto-char (point-min))
208 ;; Check whether something inserted a newline at the start and
212 (write-region (point-min) (point-max) file nil
'nomesg
)))))
214 (defun pop3-number-of-responses (endp)
217 (goto-char (point-min))
218 (while (or (and (re-search-forward "^\\+OK" nil t
)
220 (re-search-forward "^\\.\r?\n" nil t
)))
221 (re-search-forward "^-ERR " nil t
))
225 (defun pop3-logon (process)
226 (let ((pop3-password pop3-password
))
227 ;; for debugging only
228 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
229 ;; query for password
230 (if (and pop3-password-required
(not pop3-password
))
232 (read-passwd (format "Password for %s: " pop3-maildrop
))))
233 (cond ((equal 'apop pop3-authentication-scheme
)
234 (pop3-apop process pop3-maildrop
))
235 ((equal 'pass pop3-authentication-scheme
)
236 (pop3-user process pop3-maildrop
)
238 (t (error "Invalid POP3 authentication scheme")))))
240 (defun pop3-get-message-count ()
241 "Return the number of messages in the maildrop."
242 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
244 (pop3-password pop3-password
))
245 ;; for debugging only
246 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
247 ;; query for password
248 (if (and pop3-password-required
(not pop3-password
))
250 (read-passwd (format "Password for %s: " pop3-maildrop
))))
251 (cond ((equal 'apop pop3-authentication-scheme
)
252 (pop3-apop process pop3-maildrop
))
253 ((equal 'pass pop3-authentication-scheme
)
254 (pop3-user process pop3-maildrop
)
256 (t (error "Invalid POP3 authentication scheme")))
257 (setq message-count
(car (pop3-stat process
)))
261 (autoload 'open-tls-stream
"tls")
262 (autoload 'starttls-open-stream
"starttls")
263 (autoload 'starttls-negotiate
"starttls") ; avoid warning
265 (defcustom pop3-stream-type nil
266 "*Transport security type for POP3 connexions.
267 This may be either nil (plain connexion), `ssl' (use an
268 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
269 to turn on TLS security after opening the stream). However, if
270 this is nil, `ssl' is assumed for connexions to port
272 :version
"23.1" ;; No Gnus
274 :type
'(choice (const :tag
"Plain" nil
)
275 (const :tag
"SSL/TLS" ssl
)
279 (if (fboundp 'set-process-query-on-exit-flag
)
280 (defalias 'pop3-set-process-query-on-exit-flag
281 'set-process-query-on-exit-flag
)
282 (defalias 'pop3-set-process-query-on-exit-flag
283 'process-kill-without-query
)))
285 (defun pop3-open-server (mailhost port
)
286 "Open TCP connection to MAILHOST on PORT.
287 Returns the process associated with the connection."
288 (let ((coding-system-for-read 'binary
)
289 (coding-system-for-write 'binary
)
292 (get-buffer-create (concat " trace of POP session to "
295 (setq pop3-read-point
(point-min))
298 ((or (eq pop3-stream-type
'ssl
)
299 (and (not pop3-stream-type
) (member port
'(995 "pop3s"))))
300 ;; gnutls-cli, openssl don't accept service names
301 (if (or (equal port
"pop3s")
304 (let ((process (open-tls-stream "POP" (current-buffer)
307 ;; There's a load of info printed that needs deleting.
310 ;; - either we received the +OK line
311 ;; - or accept-process-output timed out without getting
314 (setq again
(memq (process-status process
)
316 (setq again
(pop3-accept-process-output process
))
317 (goto-char (point-max))
319 (cond ((looking-at "\\+OK")
321 (delete-region (point-min) (point)))
324 (error "POP SSL connexion failed")))))
326 ((eq pop3-stream-type
'starttls
)
327 ;; gnutls-cli, openssl don't accept service names
328 (if (equal port
"pop3")
330 (let ((process (starttls-open-stream "POP" (current-buffer)
331 mailhost
(or port
110))))
332 (pop3-send-command process
"STLS")
333 (let ((response (pop3-read-response process t
)))
334 (if (and response
(string-match "+OK" response
))
335 (starttls-negotiate process
)
337 (error "POP server doesn't support starttls")))
340 (open-network-stream "POP" (current-buffer) mailhost port
))))
341 (let ((response (pop3-read-response process t
)))
343 (substring response
(or (string-match "<" response
) 0)
344 (+ 1 (or (string-match ">" response
) -
1)))))
345 (pop3-set-process-query-on-exit-flag process nil
)
350 (defun pop3-send-command (process command
)
351 (set-buffer (process-buffer process
))
352 (goto-char (point-max))
353 ;; (if (= (aref command 0) ?P)
354 ;; (insert "PASS <omitted>\r\n")
355 ;; (insert command "\r\n"))
356 (setq pop3-read-point
(point))
357 (goto-char (point-max))
358 (process-send-string process
(concat command
"\r\n")))
360 (defun pop3-read-response (process &optional return
)
361 "Read the response from the server.
362 Return the response string if optional second argument is non-nil."
363 (let ((case-fold-search nil
)
365 (with-current-buffer (process-buffer process
)
366 (goto-char pop3-read-point
)
367 (while (and (memq (process-status process
) '(open run
))
368 (not (search-forward "\r\n" nil t
)))
369 (pop3-accept-process-output process
)
370 (goto-char pop3-read-point
))
371 (setq match-end
(point))
372 (goto-char pop3-read-point
)
373 (if (looking-at "-ERR")
374 (error "%s" (buffer-substring (point) (- match-end
2)))
375 (if (not (looking-at "+OK"))
376 (progn (setq pop3-read-point match-end
) nil
)
377 (setq pop3-read-point match-end
)
379 (buffer-substring (point) match-end
)
383 (defun pop3-clean-region (start end
)
384 (setq end
(set-marker (make-marker) end
))
387 (while (and (< (point) end
) (search-forward "\r\n" end t
))
388 (replace-match "\n" t t
))
390 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
391 (replace-match "" t t
)
393 (set-marker end nil
))
395 ;; Copied from message-make-date.
396 (defun pop3-make-date (&optional now
)
397 "Make a valid date header.
398 If NOW, use that time instead."
399 (require 'parse-time
)
400 (let* ((now (or now
(current-time)))
401 (zone (nth 8 (decode-time now
)))
405 (setq zone
(- zone
)))
407 (format-time-string "%d" now
)
408 ;; The month name of the %b spec is locale-specific. Pfff.
410 (capitalize (car (rassoc (nth 4 (decode-time now
))
411 parse-time-months
))))
412 (format-time-string "%Y %H:%M:%S " now
)
413 ;; We do all of this because XEmacs doesn't have the %z spec.
414 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
416 (defun pop3-munge-message-separator (start end
)
417 "Check to see if a message separator exists. If not, generate one."
420 (narrow-to-region start end
)
421 (goto-char (point-min))
422 (if (not (or (looking-at "From .?") ; Unix mail
423 (looking-at "\001\001\001\001\n") ; MMDF
424 (looking-at "BABYL OPTIONS:") ; Babyl
426 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
427 (tdate (mail-fetch-field "Date"))
428 (date (split-string (or (and tdate
429 (not (string= "" tdate
))
434 ;; sample date formats I have seen
435 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
436 ;; Date: 08 Jul 1996 23:22:24 -0400
438 ;; Tue Jul 9 09:04:21 1996
440 ;; Fixme: This should use timezone on the date field contents.
443 "Tue Jan 1 00:00:0 1900")
444 ((string-match "[A-Z]" (nth 0 date
))
445 (format "%s %s %s %s %s"
446 (nth 0 date
) (nth 2 date
) (nth 1 date
)
447 (nth 4 date
) (nth 3 date
)))
449 ;; this really needs to be better but I don't feel
450 ;; like writing a date to day converter.
451 (format "Sun %s %s %s %s"
452 (nth 1 date
) (nth 0 date
)
453 (nth 3 date
) (nth 2 date
)))
455 (setq From_
(format "\nFrom %s %s\n" from date
))
456 (while (string-match "," From_
)
457 (setq From_
(concat (substring From_
0 (match-beginning 0))
458 (substring From_
(match-end 0)))))
459 (goto-char (point-min))
461 (if (search-forward "\n\n" nil t
)
463 (goto-char (point-max))
465 (let ((size (- (point-max) (point))))
467 (insert (format "Content-Length: %s\n" size
)))
472 ;; AUTHORIZATION STATE
474 (defun pop3-user (process user
)
475 "Send USER information to POP3 server."
476 (pop3-send-command process
(format "USER %s" user
))
477 (let ((response (pop3-read-response process t
)))
478 (if (not (and response
(string-match "+OK" response
)))
479 (error "USER %s not valid" user
))))
481 (defun pop3-pass (process)
482 "Send authentication information to the server."
483 (pop3-send-command process
(format "PASS %s" pop3-password
))
484 (let ((response (pop3-read-response process t
)))
485 (if (not (and response
(string-match "+OK" response
)))
486 (pop3-quit process
))))
488 (defun pop3-apop (process user
)
489 "Send alternate authentication information to the server."
490 (let ((pass pop3-password
))
491 (if (and pop3-password-required
(not pass
))
493 (read-passwd (format "Password for %s: " pop3-maildrop
))))
495 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
496 (pop3-send-command process
(format "APOP %s %s" user hash
))
497 (let ((response (pop3-read-response process t
)))
498 (if (not (and response
(string-match "+OK" response
)))
499 (pop3-quit process
)))))
504 (defun pop3-stat (process)
505 "Return the number of messages in the maildrop and the maildrop's size."
506 (pop3-send-command process
"STAT")
507 (let ((response (pop3-read-response process t
)))
508 (list (string-to-number (nth 1 (split-string response
" ")))
509 (string-to-number (nth 2 (split-string response
" "))))
512 (defun pop3-list (process &optional msg
)
513 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
514 Otherwise, return the size of the message-id MSG"
515 (pop3-send-command process
(if msg
516 (format "LIST %d" msg
)
518 (let ((response (pop3-read-response process t
)))
520 (string-to-number (nth 2 (split-string response
" ")))
521 (let ((start pop3-read-point
) end
)
522 (with-current-buffer (process-buffer process
)
523 (while (not (re-search-forward "^\\.\r\n" nil t
))
524 (pop3-accept-process-output process
)
526 (setq pop3-read-point
(point-marker))
527 (goto-char (match-beginning 0))
528 (setq end
(point-marker))
529 (mapcar #'(lambda (s) (let ((split (split-string s
" ")))
530 (cons (string-to-number (nth 0 split
))
531 (string-to-number (nth 1 split
)))))
532 (split-string (buffer-substring start end
) "\r\n" t
)))))))
534 (defun pop3-retr (process msg crashbuf
)
535 "Retrieve message-id MSG to buffer CRASHBUF."
536 (pop3-send-command process
(format "RETR %s" msg
))
537 (pop3-read-response process
)
538 (let ((start pop3-read-point
) end
)
539 (with-current-buffer (process-buffer process
)
540 (while (not (re-search-forward "^\\.\r\n" nil t
))
541 (pop3-accept-process-output process
)
543 (setq pop3-read-point
(point-marker))
544 ;; this code does not seem to work for some POP servers...
545 ;; and I cannot figure out why not.
546 ;; (goto-char (match-beginning 0))
548 ;; (if (not (looking-at "\r\n"))
550 ;; (re-search-forward "\\.\r\n")
551 (goto-char (match-beginning 0))
552 (setq end
(point-marker))
553 (pop3-clean-region start end
)
554 (pop3-munge-message-separator start end
)
555 (with-current-buffer crashbuf
557 (copy-to-buffer crashbuf start end
)
558 (delete-region start end
)
561 (defun pop3-dele (process msg
)
562 "Mark message-id MSG as deleted."
563 (pop3-send-command process
(format "DELE %s" msg
))
564 (pop3-read-response process
))
566 (defun pop3-noop (process msg
)
568 (pop3-send-command process
"NOOP")
569 (pop3-read-response process
))
571 (defun pop3-last (process)
572 "Return highest accessed message-id number for the session."
573 (pop3-send-command process
"LAST")
574 (let ((response (pop3-read-response process t
)))
575 (string-to-number (nth 1 (split-string response
" ")))
578 (defun pop3-rset (process)
579 "Remove all delete marks from current maildrop."
580 (pop3-send-command process
"RSET")
581 (pop3-read-response process
))
585 (defun pop3-quit (process)
586 "Close connection to POP3 server.
587 Tell server to remove all messages marked as deleted, unlock the maildrop,
588 and close the connection."
589 (pop3-send-command process
"QUIT")
590 (pop3-read-response process t
)
592 (with-current-buffer (process-buffer process
)
593 (goto-char (point-max))
594 (delete-process process
))))
596 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
598 ;;; AUTHORIZATION STATE
600 ;; Initial TCP connection
602 ;; Restrictions: none
603 ;; Possible responses:
604 ;; +OK [POP3 server ready]
607 ;; Arguments: a server specific user-id (required)
608 ;; Restrictions: authorization state [after unsuccessful USER or PASS
609 ;; Possible responses:
610 ;; +OK [valid user-id]
611 ;; -ERR [invalid user-id]
614 ;; Arguments: a server/user-id specific password (required)
615 ;; Restrictions: authorization state, after successful USER
616 ;; Possible responses:
617 ;; +OK [maildrop locked and ready]
618 ;; -ERR [invalid password]
619 ;; -ERR [unable to lock maildrop]
623 ;; Restrictions: Only permitted in AUTHORIZATION state.
624 ;; Possible responses:
628 ;;; TRANSACTION STATE
632 ;; Restrictions: transaction state
633 ;; Possible responses:
634 ;; +OK nn mm [# of messages, size of maildrop]
637 ;; Arguments: a message-id (optional)
638 ;; Restrictions: transaction state; msg must not be deleted
639 ;; Possible responses:
640 ;; +OK [scan listing follows]
641 ;; -ERR [no such message]
644 ;; Arguments: a message-id (required)
645 ;; Restrictions: transaction state; msg must not be deleted
646 ;; Possible responses:
647 ;; +OK [message contents follow]
648 ;; -ERR [no such message]
651 ;; Arguments: a message-id (required)
652 ;; Restrictions: transaction state; msg must not be deleted
653 ;; Possible responses:
654 ;; +OK [message deleted]
655 ;; -ERR [no such message]
659 ;; Restrictions: transaction state
660 ;; Possible responses:
665 ;; Restrictions: transaction state
666 ;; Possible responses:
667 ;; +OK nn [highest numbered message accessed]
671 ;; Restrictions: transaction state
672 ;; Possible responses:
673 ;; +OK [all delete marks removed]
679 ;; Restrictions: none
680 ;; Possible responses:
681 ;; +OK [TCP connection closed]
685 ;;; pop3.el ends here