1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 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 2, 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 the whole thing can fall apart and leave you
93 with a corrupt mailbox."
94 :version "22.1" ;; Oort Gnus
98 (defvar pop3-timestamp nil
99 "Timestamp returned when initially connected to the POP server.
100 Used for APOP authentication.")
102 (defvar pop3-read-point nil)
103 (defvar pop3-debug nil)
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
111 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
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?
119 "How long pop3 should wait between checking for the end of output.
120 Shorter values mean quicker response, but are more CPU intensive.")
122 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
123 (defun pop3-accept-process-output (process)
124 (accept-process-output
126 (truncate pop3-read-timeout)
127 (truncate (* (- pop3-read-timeout
128 (truncate pop3-read-timeout))
131 (defun pop3-movemail (&optional crashbox)
132 "Transfer contents of a maildrop to the specified CRASHBOX."
133 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
134 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
135 (crashbuf (get-buffer-create " *pop3-retr*"))
138 (pop3-password pop3-password))
139 ;; for debugging only
140 (if pop3-debug (switch-to-buffer (process-buffer process)))
141 ;; query for password
142 (if (and pop3-password-required (not pop3-password))
144 (read-passwd (format "Password for %s: " pop3-maildrop))))
145 (cond ((equal 'apop pop3-authentication-scheme)
146 (pop3-apop process pop3-maildrop))
147 ((equal 'pass pop3-authentication-scheme)
148 (pop3-user process pop3-maildrop)
150 (t (error "Invalid POP3 authentication scheme")))
151 (setq message-count (car (pop3-stat process)))
153 (while (<= n message-count)
154 (message "Retrieving message %d of %d from %s..."
155 n message-count pop3-mailhost)
156 (pop3-retr process n crashbuf)
158 (set-buffer crashbuf)
159 (let ((coding-system-for-write 'binary))
160 (write-region (point-min) (point-max) crashbox t 'nomesg))
161 (set-buffer (process-buffer process))
162 (while (> (buffer-size) 5000)
163 (goto-char (point-min))
165 (delete-region (point-min) (point))))
166 (unless pop3-leave-mail-on-server
167 (pop3-dele process n))
169 (if pop3-debug (sit-for 1) (sit-for 0.1))) ; why?
171 (kill-buffer crashbuf))
174 (defun pop3-get-message-count ()
175 "Return the number of messages in the maildrop."
176 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
178 (pop3-password pop3-password))
179 ;; for debugging only
180 (if pop3-debug (switch-to-buffer (process-buffer process)))
181 ;; query for password
182 (if (and pop3-password-required (not pop3-password))
184 (read-passwd (format "Password for %s: " pop3-maildrop))))
185 (cond ((equal 'apop pop3-authentication-scheme)
186 (pop3-apop process pop3-maildrop))
187 ((equal 'pass pop3-authentication-scheme)
188 (pop3-user process pop3-maildrop)
190 (t (error "Invalid POP3 authentication scheme")))
191 (setq message-count (car (pop3-stat process)))
195 (autoload 'open-tls-stream "tls")
196 (autoload 'starttls-open-stream "starttls")
197 (autoload 'starttls-negotiate "starttls") ; avoid warning
199 (defcustom pop3-stream-type nil
200 "*Transport security type for POP3 connexions.
201 This may be either nil (plain connexion), `ssl' (use an
202 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
203 to turn on TLS security after opening the stream). However, if
204 this is nil, `ssl' is assumed for connexions to port
206 :version "23.0" ;; No Gnus
208 :type '(choice (const :tag "Plain" nil)
209 (const :tag "SSL/TLS" ssl)
212 (defun pop3-open-server (mailhost port)
213 "Open TCP connection to MAILHOST on PORT.
214 Returns the process associated with the connection."
215 (let ((coding-system-for-read 'binary)
216 (coding-system-for-write 'binary)
219 (set-buffer (get-buffer-create (concat " trace of POP session to "
222 (setq pop3-read-point (point-min))
225 ((or (eq pop3-stream-type 'ssl)
226 (and (not pop3-stream-type) (member port '(995 "pop3s"))))
227 ;; gnutls-cli, openssl don't accept service names
228 (if (or (equal port "pop3s")
231 (let ((process (open-tls-stream "POP" (current-buffer)
234 ;; There's a load of info printed that needs deleting.
235 (while (when (memq (process-status process) '(open run))
236 (pop3-accept-process-output process)
237 (goto-char (point-max))
239 (if (looking-at "\\+OK")
241 (delete-region (point-min) (point))
244 (error "POP SSL connexion failed"))))
246 ((eq pop3-stream-type 'starttls)
247 ;; gnutls-cli, openssl don't accept service names
248 (if (equal port "pop3")
250 (let ((process (starttls-open-stream "POP" (current-buffer)
251 mailhost (or port 110))))
252 (pop3-send-command process "STLS")
253 (let ((response (pop3-read-response process t)))
254 (if (and response (string-match "+OK" response))
255 (starttls-negotiate process)
257 (error "POP server doesn't support starttls")))
260 (open-network-stream "POP" (current-buffer) mailhost port))))
261 (let ((response (pop3-read-response process t)))
263 (substring response (or (string-match "<" response) 0)
264 (+ 1 (or (string-match ">" response) -1)))))
269 (defun pop3-process-filter (process output)
271 (set-buffer (process-buffer process))
272 (goto-char (point-max))
275 (defun pop3-send-command (process command)
276 (set-buffer (process-buffer process))
277 (goto-char (point-max))
278 ;; (if (= (aref command 0) ?P)
279 ;; (insert "PASS <omitted>\r\n")
280 ;; (insert command "\r\n"))
281 (setq pop3-read-point (point))
282 (goto-char (point-max))
283 (process-send-string process (concat command "\r\n")))
285 (defun pop3-read-response (process &optional return)
286 "Read the response from the server.
287 Return the response string if optional second argument is non-nil."
288 (let ((case-fold-search nil)
291 (set-buffer (process-buffer process))
292 (goto-char pop3-read-point)
293 (while (and (memq (process-status process) '(open run))
294 (not (search-forward "\r\n" nil t)))
295 (pop3-accept-process-output process)
296 (goto-char pop3-read-point))
297 (setq match-end (point))
298 (goto-char pop3-read-point)
299 (if (looking-at "-ERR")
300 (error (buffer-substring (point) (- match-end 2)))
301 (if (not (looking-at "+OK"))
302 (progn (setq pop3-read-point match-end) nil)
303 (setq pop3-read-point match-end)
305 (buffer-substring (point) match-end)
309 (defun pop3-clean-region (start end)
310 (setq end (set-marker (make-marker) end))
313 (while (and (< (point) end) (search-forward "\r\n" end t))
314 (replace-match "\n" t t))
316 (while (and (< (point) end) (re-search-forward "^\\." end t))
317 (replace-match "" t t)
319 (set-marker end nil))
321 (eval-when-compile (defvar parse-time-months))
323 ;; Copied from message-make-date.
324 (defun pop3-make-date (&optional now)
325 "Make a valid date header.
326 If NOW, use that time instead."
327 (require 'parse-time)
328 (let* ((now (or now (current-time)))
329 (zone (nth 8 (decode-time now)))
333 (setq zone (- zone)))
335 (format-time-string "%d" now)
336 ;; The month name of the %b spec is locale-specific. Pfff.
338 (capitalize (car (rassoc (nth 4 (decode-time now))
339 parse-time-months))))
340 (format-time-string "%Y %H:%M:%S " now)
341 ;; We do all of this because XEmacs doesn't have the %z spec.
342 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
344 (defun pop3-munge-message-separator (start end)
345 "Check to see if a message separator exists. If not, generate one."
348 (narrow-to-region start end)
349 (goto-char (point-min))
350 (if (not (or (looking-at "From .?") ; Unix mail
351 (looking-at "\001\001\001\001\n") ; MMDF
352 (looking-at "BABYL OPTIONS:") ; Babyl
354 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
355 (tdate (mail-fetch-field "Date"))
356 (date (split-string (or (and tdate
357 (not (string= "" tdate))
362 ;; sample date formats I have seen
363 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
364 ;; Date: 08 Jul 1996 23:22:24 -0400
366 ;; Tue Jul 9 09:04:21 1996
368 ;; Fixme: This should use timezone on the date field contents.
371 "Tue Jan 1 00:00:0 1900")
372 ((string-match "[A-Z]" (nth 0 date))
373 (format "%s %s %s %s %s"
374 (nth 0 date) (nth 2 date) (nth 1 date)
375 (nth 4 date) (nth 3 date)))
377 ;; this really needs to be better but I don't feel
378 ;; like writing a date to day converter.
379 (format "Sun %s %s %s %s"
380 (nth 1 date) (nth 0 date)
381 (nth 3 date) (nth 2 date)))
383 (setq From_ (format "\nFrom %s %s\n" from date))
384 (while (string-match "," From_)
385 (setq From_ (concat (substring From_ 0 (match-beginning 0))
386 (substring From_ (match-end 0)))))
387 (goto-char (point-min))
389 (if (search-forward "\n\n" nil t)
391 (goto-char (point-max))
393 (narrow-to-region (point) (point-max))
394 (let ((size (- (point-max) (point-min))))
395 (goto-char (point-min))
398 (insert (format "Content-Length: %s\n" size)))
403 ;; AUTHORIZATION STATE
405 (defun pop3-user (process user)
406 "Send USER information to POP3 server."
407 (pop3-send-command process (format "USER %s" user))
408 (let ((response (pop3-read-response process t)))
409 (if (not (and response (string-match "+OK" response)))
410 (error "USER %s not valid" user))))
412 (defun pop3-pass (process)
413 "Send authentication information to the server."
414 (pop3-send-command process (format "PASS %s" pop3-password))
415 (let ((response (pop3-read-response process t)))
416 (if (not (and response (string-match "+OK" response)))
417 (pop3-quit process))))
419 (defun pop3-apop (process user)
420 "Send alternate authentication information to the server."
421 (let ((pass pop3-password))
422 (if (and pop3-password-required (not pass))
424 (read-passwd (format "Password for %s: " pop3-maildrop))))
426 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
427 (pop3-send-command process (format "APOP %s %s" user hash))
428 (let ((response (pop3-read-response process t)))
429 (if (not (and response (string-match "+OK" response)))
430 (pop3-quit process)))))
435 (defun pop3-stat (process)
436 "Return the number of messages in the maildrop and the maildrop's size."
437 (pop3-send-command process "STAT")
438 (let ((response (pop3-read-response process t)))
439 (list (string-to-number (nth 1 (split-string response " ")))
440 (string-to-number (nth 2 (split-string response " "))))
443 (defun pop3-list (process &optional msg)
444 "Scan listing of available messages.
445 This function currently does nothing.")
447 (defun pop3-retr (process msg crashbuf)
448 "Retrieve message-id MSG to buffer CRASHBUF."
449 (pop3-send-command process (format "RETR %s" msg))
450 (pop3-read-response process)
451 (let ((start pop3-read-point) end)
453 (set-buffer (process-buffer process))
454 (while (not (re-search-forward "^\\.\r\n" nil t))
455 (pop3-accept-process-output process)
457 (setq pop3-read-point (point-marker))
458 ;; this code does not seem to work for some POP servers...
459 ;; and I cannot figure out why not.
460 ;; (goto-char (match-beginning 0))
462 ;; (if (not (looking-at "\r\n"))
464 ;; (re-search-forward "\\.\r\n")
465 (goto-char (match-beginning 0))
466 (setq end (point-marker))
467 (pop3-clean-region start end)
468 (pop3-munge-message-separator start end)
470 (set-buffer crashbuf)
472 (copy-to-buffer crashbuf start end)
473 (delete-region start end)
476 (defun pop3-dele (process msg)
477 "Mark message-id MSG as deleted."
478 (pop3-send-command process (format "DELE %s" msg))
479 (pop3-read-response process))
481 (defun pop3-noop (process msg)
483 (pop3-send-command process "NOOP")
484 (pop3-read-response process))
486 (defun pop3-last (process)
487 "Return highest accessed message-id number for the session."
488 (pop3-send-command process "LAST")
489 (let ((response (pop3-read-response process t)))
490 (string-to-number (nth 1 (split-string response " ")))
493 (defun pop3-rset (process)
494 "Remove all delete marks from current maildrop."
495 (pop3-send-command process "RSET")
496 (pop3-read-response process))
500 (defun pop3-quit (process)
501 "Close connection to POP3 server.
502 Tell server to remove all messages marked as deleted, unlock the maildrop,
503 and close the connection."
504 (pop3-send-command process "QUIT")
505 (pop3-read-response process t)
508 (set-buffer (process-buffer process))
509 (goto-char (point-max))
510 (delete-process process))))
512 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
514 ;;; AUTHORIZATION STATE
516 ;; Initial TCP connection
518 ;; Restrictions: none
519 ;; Possible responses:
520 ;; +OK [POP3 server ready]
523 ;; Arguments: a server specific user-id (required)
524 ;; Restrictions: authorization state [after unsuccessful USER or PASS
525 ;; Possible responses:
526 ;; +OK [valid user-id]
527 ;; -ERR [invalid user-id]
530 ;; Arguments: a server/user-id specific password (required)
531 ;; Restrictions: authorization state, after successful USER
532 ;; Possible responses:
533 ;; +OK [maildrop locked and ready]
534 ;; -ERR [invalid password]
535 ;; -ERR [unable to lock maildrop]
539 ;; Restrictions: Only permitted in AUTHORIZATION state.
540 ;; Possible responses:
544 ;;; TRANSACTION STATE
548 ;; Restrictions: transaction state
549 ;; Possible responses:
550 ;; +OK nn mm [# of messages, size of maildrop]
553 ;; Arguments: a message-id (optional)
554 ;; Restrictions: transaction state; msg must not be deleted
555 ;; Possible responses:
556 ;; +OK [scan listing follows]
557 ;; -ERR [no such message]
560 ;; Arguments: a message-id (required)
561 ;; Restrictions: transaction state; msg must not be deleted
562 ;; Possible responses:
563 ;; +OK [message contents follow]
564 ;; -ERR [no such message]
567 ;; Arguments: a message-id (required)
568 ;; Restrictions: transaction state; msg must not be deleted
569 ;; Possible responses:
570 ;; +OK [message deleted]
571 ;; -ERR [no such message]
575 ;; Restrictions: transaction state
576 ;; Possible responses:
581 ;; Restrictions: transaction state
582 ;; Possible responses:
583 ;; +OK nn [highest numbered message accessed]
587 ;; Restrictions: transaction state
588 ;; Possible responses:
589 ;; +OK [all delete marks removed]
595 ;; Restrictions: none
596 ;; Possible responses:
597 ;; +OK [TCP connection closed]
601 ;;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
602 ;;; pop3.el ends here