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 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.
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-leave-mail-on-server nil
86 "*Non-nil if the mail is to be left on the POP server after fetching.
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 you may get duplicate mails or the whole
93 thing can fall apart and leave you with a corrupt mailbox."
94 ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org:
95 ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de
96 ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org
97 ;; Any volunteer to re-implement this?
98 :version
"22.1" ;; Oort Gnus
102 (defvar pop3-timestamp nil
103 "Timestamp returned when initially connected to the POP server.
104 Used for APOP authentication.")
106 (defvar pop3-read-point nil
)
107 (defvar pop3-debug nil
)
109 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
110 (defvar pop3-read-timeout
111 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
112 (symbol-name system-type
))
113 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
115 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
117 ;; There should probably be a runtime test to determine the timing
118 ;; resolution, or a primitive to report it. I don't know off-hand
119 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
120 ;; could round up non-zero timeouts to a minimum of 1.0?
123 "How long pop3 should wait between checking for the end of output.
124 Shorter values mean quicker response, but are more CPU intensive.")
126 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
127 (defun pop3-accept-process-output (process)
128 (accept-process-output
130 (truncate pop3-read-timeout
)
131 (truncate (* (- pop3-read-timeout
132 (truncate pop3-read-timeout
))
135 (defun pop3-movemail (&optional crashbox
)
136 "Transfer contents of a maildrop to the specified CRASHBOX."
137 (or crashbox
(setq crashbox
(expand-file-name "~/.crashbox")))
138 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
139 (crashbuf (get-buffer-create " *pop3-retr*"))
142 (pop3-password pop3-password
))
143 ;; for debugging only
144 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
145 ;; query for password
146 (if (and pop3-password-required
(not pop3-password
))
148 (read-passwd (format "Password for %s: " pop3-maildrop
))))
149 (cond ((equal 'apop pop3-authentication-scheme
)
150 (pop3-apop process pop3-maildrop
))
151 ((equal 'pass pop3-authentication-scheme
)
152 (pop3-user process pop3-maildrop
)
154 (t (error "Invalid POP3 authentication scheme")))
155 (setq message-count
(car (pop3-stat process
)))
157 (while (<= n message-count
)
158 (message "Retrieving message %d of %d from %s..."
159 n message-count pop3-mailhost
)
160 (pop3-retr process n crashbuf
)
162 (set-buffer crashbuf
)
163 (let ((coding-system-for-write 'binary
))
164 (write-region (point-min) (point-max) crashbox t
'nomesg
))
165 (set-buffer (process-buffer process
))
166 (while (> (buffer-size) 5000)
167 (goto-char (point-min))
169 (delete-region (point-min) (point))))
170 (unless pop3-leave-mail-on-server
171 (pop3-dele process n
))
173 (if pop3-debug
(sit-for 1) (sit-for 0.1))) ; why?
174 (when (and pop3-leave-mail-on-server
176 (message "pop3.el doesn't support UIDL. Setting `pop3-leave-mail-on-server'
177 to %s might not give the result you'd expect." pop3-leave-mail-on-server
)
180 (kill-buffer crashbuf
))
183 (defun pop3-get-message-count ()
184 "Return the number of messages in the maildrop."
185 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
187 (pop3-password pop3-password
))
188 ;; for debugging only
189 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
190 ;; query for password
191 (if (and pop3-password-required
(not pop3-password
))
193 (read-passwd (format "Password for %s: " pop3-maildrop
))))
194 (cond ((equal 'apop pop3-authentication-scheme
)
195 (pop3-apop process pop3-maildrop
))
196 ((equal 'pass pop3-authentication-scheme
)
197 (pop3-user process pop3-maildrop
)
199 (t (error "Invalid POP3 authentication scheme")))
200 (setq message-count
(car (pop3-stat process
)))
204 (autoload 'open-tls-stream
"tls")
205 (autoload 'starttls-open-stream
"starttls")
206 (autoload 'starttls-negotiate
"starttls") ; avoid warning
208 (defcustom pop3-stream-type nil
209 "*Transport security type for POP3 connexions.
210 This may be either nil (plain connexion), `ssl' (use an
211 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
212 to turn on TLS security after opening the stream). However, if
213 this is nil, `ssl' is assumed for connexions to port
215 :version
"23.0" ;; No Gnus
217 :type
'(choice (const :tag
"Plain" nil
)
218 (const :tag
"SSL/TLS" ssl
)
221 (defun pop3-open-server (mailhost port
)
222 "Open TCP connection to MAILHOST on PORT.
223 Returns the process associated with the connection."
224 (let ((coding-system-for-read 'binary
)
225 (coding-system-for-write 'binary
)
228 (set-buffer (get-buffer-create (concat " trace of POP session to "
231 (setq pop3-read-point
(point-min))
234 ((or (eq pop3-stream-type
'ssl
)
235 (and (not pop3-stream-type
) (member port
'(995 "pop3s"))))
236 ;; gnutls-cli, openssl don't accept service names
237 (if (or (equal port
"pop3s")
240 (let ((process (open-tls-stream "POP" (current-buffer)
243 ;; There's a load of info printed that needs deleting.
244 (while (when (memq (process-status process
) '(open run
))
245 (pop3-accept-process-output process
)
246 (goto-char (point-max))
248 (if (looking-at "\\+OK")
250 (delete-region (point-min) (point))
253 (error "POP SSL connexion failed"))))
255 ((eq pop3-stream-type
'starttls
)
256 ;; gnutls-cli, openssl don't accept service names
257 (if (equal port
"pop3")
259 (let ((process (starttls-open-stream "POP" (current-buffer)
260 mailhost
(or port
110))))
261 (pop3-send-command process
"STLS")
262 (let ((response (pop3-read-response process t
)))
263 (if (and response
(string-match "+OK" response
))
264 (starttls-negotiate process
)
266 (error "POP server doesn't support starttls")))
269 (open-network-stream "POP" (current-buffer) mailhost port
))))
270 (let ((response (pop3-read-response process t
)))
272 (substring response
(or (string-match "<" response
) 0)
273 (+ 1 (or (string-match ">" response
) -
1)))))
278 (defun pop3-process-filter (process output
)
280 (set-buffer (process-buffer process
))
281 (goto-char (point-max))
284 (defun pop3-send-command (process command
)
285 (set-buffer (process-buffer process
))
286 (goto-char (point-max))
287 ;; (if (= (aref command 0) ?P)
288 ;; (insert "PASS <omitted>\r\n")
289 ;; (insert command "\r\n"))
290 (setq pop3-read-point
(point))
291 (goto-char (point-max))
292 (process-send-string process
(concat command
"\r\n")))
294 (defun pop3-read-response (process &optional return
)
295 "Read the response from the server.
296 Return the response string if optional second argument is non-nil."
297 (let ((case-fold-search nil
)
300 (set-buffer (process-buffer process
))
301 (goto-char pop3-read-point
)
302 (while (and (memq (process-status process
) '(open run
))
303 (not (search-forward "\r\n" nil t
)))
304 (pop3-accept-process-output process
)
305 (goto-char pop3-read-point
))
306 (setq match-end
(point))
307 (goto-char pop3-read-point
)
308 (if (looking-at "-ERR")
309 (error (buffer-substring (point) (- match-end
2)))
310 (if (not (looking-at "+OK"))
311 (progn (setq pop3-read-point match-end
) nil
)
312 (setq pop3-read-point match-end
)
314 (buffer-substring (point) match-end
)
318 (defun pop3-clean-region (start end
)
319 (setq end
(set-marker (make-marker) end
))
322 (while (and (< (point) end
) (search-forward "\r\n" end t
))
323 (replace-match "\n" t t
))
325 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
326 (replace-match "" t t
)
328 (set-marker end nil
))
330 (eval-when-compile (defvar parse-time-months
))
332 ;; Copied from message-make-date.
333 (defun pop3-make-date (&optional now
)
334 "Make a valid date header.
335 If NOW, use that time instead."
336 (require 'parse-time
)
337 (let* ((now (or now
(current-time)))
338 (zone (nth 8 (decode-time now
)))
342 (setq zone
(- zone
)))
344 (format-time-string "%d" now
)
345 ;; The month name of the %b spec is locale-specific. Pfff.
347 (capitalize (car (rassoc (nth 4 (decode-time now
))
348 parse-time-months
))))
349 (format-time-string "%Y %H:%M:%S " now
)
350 ;; We do all of this because XEmacs doesn't have the %z spec.
351 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
353 (defun pop3-munge-message-separator (start end
)
354 "Check to see if a message separator exists. If not, generate one."
357 (narrow-to-region start end
)
358 (goto-char (point-min))
359 (if (not (or (looking-at "From .?") ; Unix mail
360 (looking-at "\001\001\001\001\n") ; MMDF
361 (looking-at "BABYL OPTIONS:") ; Babyl
363 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
364 (tdate (mail-fetch-field "Date"))
365 (date (split-string (or (and tdate
366 (not (string= "" tdate
))
371 ;; sample date formats I have seen
372 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
373 ;; Date: 08 Jul 1996 23:22:24 -0400
375 ;; Tue Jul 9 09:04:21 1996
377 ;; Fixme: This should use timezone on the date field contents.
380 "Tue Jan 1 00:00:0 1900")
381 ((string-match "[A-Z]" (nth 0 date
))
382 (format "%s %s %s %s %s"
383 (nth 0 date
) (nth 2 date
) (nth 1 date
)
384 (nth 4 date
) (nth 3 date
)))
386 ;; this really needs to be better but I don't feel
387 ;; like writing a date to day converter.
388 (format "Sun %s %s %s %s"
389 (nth 1 date
) (nth 0 date
)
390 (nth 3 date
) (nth 2 date
)))
392 (setq From_
(format "\nFrom %s %s\n" from date
))
393 (while (string-match "," From_
)
394 (setq From_
(concat (substring From_
0 (match-beginning 0))
395 (substring From_
(match-end 0)))))
396 (goto-char (point-min))
398 (if (search-forward "\n\n" nil t
)
400 (goto-char (point-max))
402 (narrow-to-region (point) (point-max))
403 (let ((size (- (point-max) (point-min))))
404 (goto-char (point-min))
407 (insert (format "Content-Length: %s\n" size
)))
412 ;; AUTHORIZATION STATE
414 (defun pop3-user (process user
)
415 "Send USER information to POP3 server."
416 (pop3-send-command process
(format "USER %s" user
))
417 (let ((response (pop3-read-response process t
)))
418 (if (not (and response
(string-match "+OK" response
)))
419 (error "USER %s not valid" user
))))
421 (defun pop3-pass (process)
422 "Send authentication information to the server."
423 (pop3-send-command process
(format "PASS %s" pop3-password
))
424 (let ((response (pop3-read-response process t
)))
425 (if (not (and response
(string-match "+OK" response
)))
426 (pop3-quit process
))))
428 (defun pop3-apop (process user
)
429 "Send alternate authentication information to the server."
430 (let ((pass pop3-password
))
431 (if (and pop3-password-required
(not pass
))
433 (read-passwd (format "Password for %s: " pop3-maildrop
))))
435 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
436 (pop3-send-command process
(format "APOP %s %s" user hash
))
437 (let ((response (pop3-read-response process t
)))
438 (if (not (and response
(string-match "+OK" response
)))
439 (pop3-quit process
)))))
444 (defun pop3-stat (process)
445 "Return the number of messages in the maildrop and the maildrop's size."
446 (pop3-send-command process
"STAT")
447 (let ((response (pop3-read-response process t
)))
448 (list (string-to-number (nth 1 (split-string response
" ")))
449 (string-to-number (nth 2 (split-string response
" "))))
452 (defun pop3-list (process &optional msg
)
453 "Scan listing of available messages.
454 This function currently does nothing.")
456 (defun pop3-retr (process msg crashbuf
)
457 "Retrieve message-id MSG to buffer CRASHBUF."
458 (pop3-send-command process
(format "RETR %s" msg
))
459 (pop3-read-response process
)
460 (let ((start pop3-read-point
) end
)
462 (set-buffer (process-buffer process
))
463 (while (not (re-search-forward "^\\.\r\n" nil t
))
464 (pop3-accept-process-output process
)
466 (setq pop3-read-point
(point-marker))
467 ;; this code does not seem to work for some POP servers...
468 ;; and I cannot figure out why not.
469 ;; (goto-char (match-beginning 0))
471 ;; (if (not (looking-at "\r\n"))
473 ;; (re-search-forward "\\.\r\n")
474 (goto-char (match-beginning 0))
475 (setq end
(point-marker))
476 (pop3-clean-region start end
)
477 (pop3-munge-message-separator start end
)
479 (set-buffer crashbuf
)
481 (copy-to-buffer crashbuf start end
)
482 (delete-region start end
)
485 (defun pop3-dele (process msg
)
486 "Mark message-id MSG as deleted."
487 (pop3-send-command process
(format "DELE %s" msg
))
488 (pop3-read-response process
))
490 (defun pop3-noop (process msg
)
492 (pop3-send-command process
"NOOP")
493 (pop3-read-response process
))
495 (defun pop3-last (process)
496 "Return highest accessed message-id number for the session."
497 (pop3-send-command process
"LAST")
498 (let ((response (pop3-read-response process t
)))
499 (string-to-number (nth 1 (split-string response
" ")))
502 (defun pop3-rset (process)
503 "Remove all delete marks from current maildrop."
504 (pop3-send-command process
"RSET")
505 (pop3-read-response process
))
509 (defun pop3-quit (process)
510 "Close connection to POP3 server.
511 Tell server to remove all messages marked as deleted, unlock the maildrop,
512 and close the connection."
513 (pop3-send-command process
"QUIT")
514 (pop3-read-response process t
)
517 (set-buffer (process-buffer process
))
518 (goto-char (point-max))
519 (delete-process process
))))
521 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
523 ;;; AUTHORIZATION STATE
525 ;; Initial TCP connection
527 ;; Restrictions: none
528 ;; Possible responses:
529 ;; +OK [POP3 server ready]
532 ;; Arguments: a server specific user-id (required)
533 ;; Restrictions: authorization state [after unsuccessful USER or PASS
534 ;; Possible responses:
535 ;; +OK [valid user-id]
536 ;; -ERR [invalid user-id]
539 ;; Arguments: a server/user-id specific password (required)
540 ;; Restrictions: authorization state, after successful USER
541 ;; Possible responses:
542 ;; +OK [maildrop locked and ready]
543 ;; -ERR [invalid password]
544 ;; -ERR [unable to lock maildrop]
548 ;; Restrictions: Only permitted in AUTHORIZATION state.
549 ;; Possible responses:
553 ;;; TRANSACTION STATE
557 ;; Restrictions: transaction state
558 ;; Possible responses:
559 ;; +OK nn mm [# of messages, size of maildrop]
562 ;; Arguments: a message-id (optional)
563 ;; Restrictions: transaction state; msg must not be deleted
564 ;; Possible responses:
565 ;; +OK [scan listing follows]
566 ;; -ERR [no such message]
569 ;; Arguments: a message-id (required)
570 ;; Restrictions: transaction state; msg must not be deleted
571 ;; Possible responses:
572 ;; +OK [message contents follow]
573 ;; -ERR [no such message]
576 ;; Arguments: a message-id (required)
577 ;; Restrictions: transaction state; msg must not be deleted
578 ;; Possible responses:
579 ;; +OK [message deleted]
580 ;; -ERR [no such message]
584 ;; Restrictions: transaction state
585 ;; Possible responses:
590 ;; Restrictions: transaction state
591 ;; Possible responses:
592 ;; +OK nn [highest numbered message accessed]
596 ;; Restrictions: transaction state
597 ;; Possible responses:
598 ;; +OK [all delete marks removed]
604 ;; Restrictions: none
605 ;; Possible responses:
606 ;; +OK [TCP connection closed]
610 ;;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
611 ;;; pop3.el ends here