1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996-1999 Free Software Foundation, Inc.
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: mail, pop3
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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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 (defconst pop3-version "1.3s")
43 (defvar pop3-maildrop (or (user-login-name) (getenv "LOGNAME") (getenv "USER") nil)
45 (defvar pop3-mailhost (or (getenv "MAILHOST") nil)
49 (defvar pop3-connection-type nil
50 "*POP3 connection type.")
52 (defvar pop3-password-required t
53 "*Non-nil if a password is required when connecting to POP server.")
54 (defvar pop3-password nil
55 "*Password to use when connecting to POP server.")
57 (defvar pop3-authentication-scheme 'pass
58 "*POP3 authentication scheme.
59 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
62 (defvar pop3-timestamp nil
63 "Timestamp returned when initially connected to the POP server.
64 Used for APOP authentication.")
66 (defvar pop3-leave-mail-on-server nil
67 "Non-nil if mail is to be left on the server and UIDL used for
70 (defvar pop3-maximum-message-size nil
71 "If non-nil only download messages smaller than this.")
73 (defvar pop3-except-header-regexp nil
74 "If non-nil we do not retrieve messages whose headers are matching this regexp.")
76 (defvar pop3-uidl-file-name "~/.uidls"
77 "File in which to store the UIDL of processed messages.")
79 (defvar pop3-uidl-support 'dont-know
80 "Whether the server supports UIDL.
81 Nil means no, t means yes, not-nil-or-t means yet to be determined.")
83 (defvar pop3-uidl-obarray (make-vector 31 0)
86 (defvar pop3-read-point nil)
87 (defvar pop3-debug nil)
90 (autoload 'open-ssl-stream "ssl"))
92 (defvar pop3-ssl-program-arguments
94 "Arguments to be passed to the program `pop3-ssl-program-name'.")
96 (defun pop3-movemail (&optional crashbox)
97 "Transfer contents of a maildrop to the specified CRASHBOX."
98 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
99 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
100 (crashbuf (get-buffer-create " *pop3-retr*"))
105 (pop3-password pop3-password)
107 (concat pop3-uidl-file-name "-" pop3-mailhost))
108 (retrieved-messages nil)
110 ;; for debugging only
111 (if pop3-debug (switch-to-buffer (process-buffer process)))
112 ;; query for password
113 (if (and pop3-password-required (not pop3-password))
115 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
116 (cond ((equal 'apop pop3-authentication-scheme)
117 (pop3-apop process pop3-maildrop))
118 ((equal 'pass pop3-authentication-scheme)
119 (pop3-user process pop3-maildrop)
121 (t (error "Invalid POP3 authentication scheme.")))
122 ;; get messages that are suitable for download
123 (message "Retrieving message list...")
124 (setq messages (pop3-get-message-numbers process)
125 message-count (length messages))
126 (message (format "Retrieving message list...%d unread" message-count))
129 (while (<= n message-count)
130 (setq msgid (caar messages)
131 msglen (cdar messages)
132 messages (cdr messages))
133 ;; only retrieve messages matching our regexp or in the uidl list
134 (unless (or (not msgid)
135 ;; don't download messages that are too large
136 (and pop3-maximum-message-size
137 (> msglen pop3-maximum-message-size))
138 (and pop3-except-header-regexp
139 (string-match pop3-except-header-regexp
140 (pop3-top process msgid 0))))
141 (message (format "Retrieving message %d of %d from %s..."
142 n message-count pop3-mailhost))
143 (pop3-retr process msgid crashbuf)
144 (setq retrieved-messages (cons msgid retrieved-messages)))
146 (with-current-buffer crashbuf
147 (write-region-as-binary (point-min) (point-max)
148 crashbox 'append 'nomesg)
150 ;; mark messages as read
151 (when pop3-leave-mail-on-server
153 ;; now delete the messages we have retrieved
154 (unless pop3-leave-mail-on-server
155 (dolist (n retrieved-messages)
156 (message (format "Deleting message %d of %d from %s..."
157 n message-count pop3-mailhost))
158 (pop3-dele process n)))
161 (kill-buffer crashbuf)
164 (defun pop3-open-server (mailhost port)
165 "Open TCP connection to MAILHOST.
166 Returns the process associated with the connection."
167 (let ((process-buffer
168 (get-buffer-create (format "trace of POP session to %s" mailhost)))
171 (set-buffer process-buffer)
176 ((eq pop3-connection-type 'ssl)
177 (pop3-open-ssl-stream "POP" process-buffer mailhost port))
179 (open-network-stream-as-binary "POP" process-buffer mailhost port))))
180 (setq pop3-read-point (point-min))
181 (let ((response (pop3-read-response process t)))
183 (substring response (or (string-match "<" response) 0)
184 (+ 1 (or (string-match ">" response) -1)))))
187 (defun pop3-open-ssl-stream-1 (name buffer host service extra-arg)
188 (let* ((ssl-program-arguments
189 `(,@pop3-ssl-program-arguments ,extra-arg
190 "-connect" ,(format "%s:%d" host service)))
191 (process (open-ssl-stream name buffer host service)))
193 (with-current-buffer buffer
194 (goto-char (point-min))
195 (while (and (memq (process-status process) '(open run))
196 (goto-char (point-max))
198 (not (looking-at "+OK")))
199 (accept-process-output process 1)
201 (delete-region (point-min) (point)))
202 (and process (memq (process-status process) '(open run))
205 (defun pop3-open-ssl-stream (name buffer host service)
206 "Open a SSL connection for a service to a host."
208 (or (pop3-open-ssl-stream-1 name buffer host service "-ssl3")
209 (pop3-open-ssl-stream-1 name buffer host service "-ssl2"))))
213 (defun pop3-process-filter (process output)
215 (set-buffer (process-buffer process))
216 (goto-char (point-max))
219 (defun pop3-send-command (process command)
220 (set-buffer (process-buffer process))
221 (goto-char (point-max))
222 ;; (if (= (aref command 0) ?P)
223 ;; (insert "PASS <omitted>\r\n")
224 ;; (insert command "\r\n"))
225 (setq pop3-read-point (point))
226 (goto-char (point-max))
227 (process-send-string process (concat command "\r\n"))
230 (defun pop3-read-response (process &optional return)
231 "Read the response from the server.
232 Return the response string if optional second argument is non-nil."
233 (let ((case-fold-search nil)
236 (set-buffer (process-buffer process))
237 (goto-char pop3-read-point)
238 (while (not (search-forward "\r\n" nil t))
239 (accept-process-output process 3)
240 (goto-char pop3-read-point))
241 (setq match-end (point))
242 (goto-char pop3-read-point)
243 (if (looking-at "-ERR")
244 (signal 'error (list (buffer-substring (point) (- match-end 2))))
245 (if (not (looking-at "+OK"))
246 (progn (setq pop3-read-point match-end) nil)
247 (setq pop3-read-point match-end)
249 (buffer-substring (point) match-end)
253 (defvar pop3-read-passwd nil)
254 (defun pop3-read-passwd (prompt)
255 (if (not pop3-read-passwd)
256 (if (functionp 'read-passwd)
257 (setq pop3-read-passwd 'read-passwd)
258 (if (load "passwd" t)
259 (setq pop3-read-passwd 'read-passwd)
260 (autoload 'ange-ftp-read-passwd "ange-ftp")
261 (setq pop3-read-passwd 'ange-ftp-read-passwd))))
262 (funcall pop3-read-passwd prompt))
264 (defun pop3-clean-region (start end)
265 (setq end (set-marker (make-marker) end))
268 (while (and (< (point) end) (search-forward "\r\n" end t))
269 (replace-match "\n" t t))
271 (while (re-search-forward "\n\n\\(From \\)" end t)
272 (replace-match "\n\n>\\1" t nil))
274 (while (and (< (point) end) (re-search-forward "^\\." end t))
275 (replace-match "" t t)
277 (set-marker end nil))
279 (defun pop3-munge-message-separator (start end)
280 "Check to see if a message separator exists. If not, generate one."
281 (if (not (fboundp 'parse-time-string))
282 (autoload 'parse-time-string "parse-time"))
285 (narrow-to-region start end)
286 (goto-char (point-min))
287 (if (not (or (looking-at "From .?") ; Unix mail
288 (looking-at "\001\001\001\001\n") ; MMDF
289 (looking-at "BABYL OPTIONS:") ; Babyl
291 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
292 (date (mail-fetch-field "Date"))
294 ;; sample date formats I have seen
295 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
296 ;; Date: 08 Jul 1996 23:22:24 -0400
298 ;; Tue Jul 9 09:04:21 1996
299 (setq date (format-time-string
303 (apply 'encode-time (parse-time-string date))
304 (error (current-time)))
306 (setq From_ (format "\nFrom %s %s\n" from date))
307 (while (string-match "," From_)
308 (setq From_ (concat (substring From_ 0 (match-beginning 0))
309 (substring From_ (match-end 0)))))
310 (goto-char (point-min))
315 (defun pop3-get-message-numbers (process)
316 "Get the list of message numbers and lengths to retrieve via PROCESS."
317 ;; we use the LIST comand first anyway to get the message lengths.
318 ;; then if we're leaving mail on the server, see if the UIDL command
319 ;; is implemented. if so, we use it to get the message number list.
320 (let ((messages (pop3-list process))
321 (uidl (if pop3-leave-mail-on-server
322 (pop3-get-uidl process))))
326 ((eq pop3-uidl-support t)
327 ;; remove elements not in the uidl, this assumes the uidl is short
329 (lambda (message) (memq (car message) uidl))
333 (defun pop3-get-uidl (process)
334 "Use PROCESS to get a list of unread message numbers."
335 (let ((messages (pop3-uidl process)) uidl)
336 (if (or (null messages) (null pop3-uidl-support))
337 (setq pop3-uidl-support nil)
338 (setq pop3-uidl-support t)
341 (when (file-readable-p pop3-uidl-file-name)
342 (insert-file-contents pop3-uidl-file-name))
343 (goto-char (point-min))
344 (while (looking-at "\\([^ \n\t]+\\)")
345 (set (intern (match-string 1) pop3-uidl-obarray)
349 (dolist (message (cdr messages))
350 (if (setq uidl (intern-soft (cdr message) pop3-uidl-obarray))
351 (setcar (symbol-value uidl) (car message))
352 (set (intern (cdr message) pop3-uidl-obarray)
353 (cons (car message) nil))))
354 (pop3-get-unread-message-numbers))
357 (defun pop3-get-unread-message-numbers ()
358 "Return a sorted list of unread msg numbers to retrieve."
360 (mapatoms (lambda (atom)
361 (if (not (cdr (symbol-value atom)))
362 (push (car (symbol-value atom)) nums)))
366 (defun pop3-save-uidls ()
367 "Save the updated UIDLs to disk for use next time."
368 (when (and pop3-leave-mail-on-server
371 (dotimes (i (length pop3-uidl-obarray))
372 (if (symbolp (aref pop3-uidl-obarray i))
376 (when (file-readable-p pop3-uidl-file-name)
377 (copy-file pop3-uidl-file-name
378 (concat pop3-uidl-file-name ".old")
379 'overwrite 'keeptime))
382 (when (car (symbol-value atom))
383 (insert (format "%s\n" atom))))
385 (write-file pop3-uidl-file-name)))))
389 ;; AUTHORIZATION STATE
391 (defun pop3-user (process user)
392 "Send USER information to POP3 server."
393 (pop3-send-command process (format "USER %s" user))
394 (let ((response (pop3-read-response process t)))
395 (if (not (and response (string-match "+OK" response)))
396 (error (format "USER %s not valid." user)))))
398 (defun pop3-pass (process)
399 "Send authentication information to the server."
400 (pop3-send-command process (format "PASS %s" pop3-password))
401 (let ((response (pop3-read-response process t)))
402 (if (not (and response (string-match "+OK" response)))
403 (pop3-quit process))))
405 (defun pop3-apop (process user)
406 "Send alternate authentication information to the server."
407 (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
408 (let ((hash (md5 (concat pop3-timestamp pop3-password))))
409 (pop3-send-command process (format "APOP %s %s" user hash))
410 (let ((response (pop3-read-response process t)))
411 (if (not (and response (string-match "+OK" response)))
412 (pop3-quit process)))))
416 (defun pop3-stat (process)
417 "Return the number of messages in the maildrop and the maildrop's size."
418 (pop3-send-command process "STAT")
419 (let ((response (pop3-read-response process t)))
420 (list (string-to-int (nth 1 (split-string response)))
421 (string-to-int (nth 2 (split-string response))))
424 (defun pop3-retr (process msg crashbuf)
425 "Retrieve message-id MSG to buffer CRASHBUF."
426 (pop3-send-command process (format "RETR %s" msg))
427 (pop3-read-response process)
429 (let ((region (pop3-get-extended-response process)))
430 (pop3-munge-message-separator (car region) (cadr region))
431 (append-to-buffer crashbuf (car region) (cadr region))
432 (delete-region (car region) (cadr region))
435 (defun pop3-dele (process msg)
436 "Mark message-id MSG as deleted."
437 (pop3-send-command process (format "DELE %s" msg))
438 (pop3-read-response process))
440 (defun pop3-noop (process msg)
442 (pop3-send-command process "NOOP")
443 (pop3-read-response process))
445 (defun pop3-last (process)
446 "Return highest accessed message-id number for the session."
447 (pop3-send-command process "LAST")
448 (let ((response (pop3-read-response process t)))
449 (string-to-int (nth 1 (split-string response)))
452 (defun pop3-rset (process)
453 "Remove all delete marks from current maildrop."
454 (pop3-send-command process "RSET")
455 (pop3-read-response process))
459 (defun pop3-quit (process)
460 "Close connection to POP3 server.
461 Tell server to remove all messages marked as deleted, unlock the maildrop,
462 and close the connection."
463 (pop3-send-command process "QUIT")
464 (pop3-read-response process t)
467 (set-buffer (process-buffer process))
468 (goto-char (point-max))
469 (delete-process process)
472 (defun pop3-uidl (process &optional msgno)
473 "Return the results of a UIDL command in PROCESS for optional MSGNO.
474 If UIDL is unsupported on this mail server or if msgno is invalid, return nil.
475 Otherwise, return a list in the form
477 (N (1 UIDL-1) (2 UIDL-2) ... (N UIDL-N))
481 N is an integer for the number of UIDLs returned (could be 0)
485 (pop3-send-command process (format "UIDL %d" msgno))
486 (pop3-send-command process "UIDL"))
488 (if (null (pop3-read-response process t))
489 nil ;; UIDL is not supported on this server
493 (apply 'narrow-to-region (pop3-get-extended-response process))
494 (goto-char (point-min))
495 (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
496 (setq msgno (string-to-int (match-string 1))
497 uidl (match-string 2))
498 (push (cons msgno uidl) pairs)
499 (beginning-of-line 2))
500 (cons (length pairs) (nreverse pairs))
503 (defun pop3-list (process &optional msgno)
504 "Return the results of a LIST command for PROCESS and optional MSGNO.
505 If (optional) msgno is invalid, return nil. Otherwise, return a list
508 (N (1 LEN-1) (2 LEN-2) ... (N LEN-N))
512 N is an integer for the number of msg/len pairs (could be 0)
513 LEN-n is an integer."
515 (pop3-send-command process (format "LIST %d" msgno))
516 (pop3-send-command process "LIST"))
518 (if (null (pop3-read-response process t))
519 nil ;; MSGNO is not valid number
523 (apply 'narrow-to-region (pop3-get-extended-response process))
524 (goto-char (point-min))
525 (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
526 (setq msgno (string-to-int (match-string 1))
527 len (string-to-int (match-string 2)))
528 (push (cons msgno len) pairs)
529 (beginning-of-line 2))
530 (cons (length pairs) (nreverse pairs))
533 (defun pop3-top (process msgno &optional lines)
534 "Return the top LINES of messages for PROCESS and MSGNO.
535 If msgno is invalid, return nil. Otherwise, return a string."
536 (pop3-send-command process (format "TOP %d %d" msgno (or lines 1)))
537 (if (pop3-read-response process t)
538 nil ;; MSGNO is not valid number
540 (apply 'buffer-substring (pop3-get-extended-response process)))
545 (defun pop3-get-extended-response (process)
546 "Get the extended pop3 response in the PROCESS buffer."
547 (let ((start pop3-read-point) end)
548 (set-buffer (process-buffer process))
550 (while (not (re-search-forward "^\\.\r\n" nil t))
551 (accept-process-output process 3)
553 (setq pop3-read-point (point-marker))
554 (goto-char (match-beginning 0))
555 (setq end (point-marker))
556 (pop3-clean-region start end)
560 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
562 ;;; AUTHORIZATION STATE
564 ;; Initial TCP connection
566 ;; Restrictions: none
567 ;; Possible responses:
568 ;; +OK [POP3 server ready]
571 ;; Arguments: a server specific user-id (required)
572 ;; Restrictions: authorization state [after unsuccessful USER or PASS
573 ;; Possible responses:
574 ;; +OK [valid user-id]
575 ;; -ERR [invalid user-id]
578 ;; Arguments: a server/user-id specific password (required)
579 ;; Restrictions: authorization state, after successful USER
580 ;; Possible responses:
581 ;; +OK [maildrop locked and ready]
582 ;; -ERR [invalid password]
583 ;; -ERR [unable to lock maildrop]
585 ;;; TRANSACTION STATE
589 ;; Restrictions: transaction state
590 ;; Possible responses:
591 ;; +OK nn mm [# of messages, size of maildrop]
594 ;; Arguments: a message-id (optional)
595 ;; Restrictions: transaction state; msg must not be deleted
596 ;; Possible responses:
597 ;; +OK [scan listing follows]
598 ;; -ERR [no such message]
601 ;; Arguments: a message-id (required), number of lines (optional)
602 ;; Restrictions: transaction state; msg must not be deleted
603 ;; Possible responses:
604 ;; +OK [partial message listing follows]
605 ;; -ERR [no such message]
608 ;; Arguments: a message-id (optional)
609 ;; Restrictions: transaction state; msg must not be deleted
610 ;; Possible responses:
611 ;; +OK [uidl listing follows]
612 ;; -ERR [no such message]
615 ;; Arguments: a message-id (required)
616 ;; Restrictions: transaction state; msg must not be deleted
617 ;; Possible responses:
618 ;; +OK [message contents follow]
619 ;; -ERR [no such message]
622 ;; Arguments: a message-id (required)
623 ;; Restrictions: transaction state; msg must not be deleted
624 ;; Possible responses:
625 ;; +OK [message deleted]
626 ;; -ERR [no such message]
630 ;; Restrictions: transaction state
631 ;; Possible responses:
636 ;; Restrictions: transaction state
637 ;; Possible responses:
638 ;; +OK nn [highest numbered message accessed]
642 ;; Restrictions: transaction state
643 ;; Possible responses:
644 ;; +OK [all delete marks removed]
650 ;; Restrictions: none
651 ;; Possible responses:
652 ;; +OK [TCP connection closed]