1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996,1997 Free Software Foundation, Inc.
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Keywords: mail, pop3
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
29 ;; are implemented. The LIST command has not been implemented due to lack
30 ;; of actual usefulness.
31 ;; The optional POP3 command TOP has not been implemented.
33 ;; This program was inspired by Kyle E. Jones's vm-pop program.
40 (defconst pop3-version "1.3g")
42 (defvar pop3-maildrop (or user-login-name (getenv "LOGNAME") (getenv "USER") nil)
44 (defvar pop3-mailhost (or (getenv "MAILHOST") nil)
49 (defvar pop3-password-required t
50 "*Non-nil if a password is required when connecting to POP server.")
51 (defvar pop3-password nil
52 "*Password to use when connecting to POP server.")
54 (defvar pop3-authentication-scheme 'pass
55 "*POP3 authentication scheme.
56 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
59 (defvar pop3-timestamp nil
60 "Timestamp returned when initially connected to the POP server.
61 Used for APOP authentication.")
63 (defvar pop3-movemail-file-coding-system nil
64 "Crashbox made by pop3-movemail with this coding system.")
66 (defvar pop3-read-point nil)
67 (defvar pop3-debug nil)
69 (defun pop3-movemail (&optional crashbox)
70 "Transfer contents of a maildrop to the specified CRASHBOX."
71 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
72 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
73 (crashbuf (get-buffer-create " *pop3-retr*"))
77 (if pop3-debug (switch-to-buffer (process-buffer process)))
78 (cond ((equal 'apop pop3-authentication-scheme)
79 (pop3-apop process pop3-maildrop))
80 ((equal 'pass pop3-authentication-scheme)
81 (pop3-user process pop3-maildrop)
83 (t (error "Invalid POP3 authentication scheme.")))
84 (setq message-count (car (pop3-stat process)))
85 (while (<= n message-count)
86 (message (format "Retrieving message %d of %d from %s..."
87 n message-count pop3-mailhost))
88 (pop3-retr process n crashbuf)
91 (let ((coding-system-for-write pop3-movemail-file-coding-system))
92 (append-to-file (point-min) (point-max) crashbox))
93 (set-buffer (process-buffer process))
94 (while (> (buffer-size) 5000)
95 (goto-char (point-min))
97 (delete-region (point-min) (point))))
100 (if pop3-debug (sit-for 1) (sit-for 0.1))
103 (kill-buffer crashbuf)
107 (defun pop3-open-server (mailhost port)
108 "Open TCP connection to MAILHOST.
109 Returns the process associated with the connection."
110 (let ((process-buffer
111 (get-buffer-create (format "trace of POP session to %s" mailhost)))
114 (set-buffer process-buffer)
117 (open-network-stream "POP" process-buffer mailhost port))
118 (setq pop3-read-point (point-min))
119 (let ((response (pop3-read-response process t)))
121 (substring response (or (string-match "<" response) 0)
122 (+ 1 (or (string-match ">" response) -1)))))
128 (defun pop3-process-filter (process output)
130 (set-buffer (process-buffer process))
131 (goto-char (point-max))
134 (defun pop3-send-command (process command)
135 (set-buffer (process-buffer process))
136 (goto-char (point-max))
137 ;; (if (= (aref command 0) ?P)
138 ;; (insert "PASS <omitted>\r\n")
139 ;; (insert command "\r\n"))
140 (setq pop3-read-point (point))
141 (goto-char (point-max))
142 (process-send-string process command)
143 (process-send-string process "\r\n")
146 (defun pop3-read-response (process &optional return)
147 "Read the response from the server.
148 Return the response string if optional second argument is non-nil."
149 (let ((case-fold-search nil)
152 (set-buffer (process-buffer process))
153 (goto-char pop3-read-point)
154 (while (not (search-forward "\r\n" nil t))
155 (accept-process-output process 3)
156 (goto-char pop3-read-point))
157 (setq match-end (point))
158 (goto-char pop3-read-point)
159 (if (looking-at "-ERR")
160 (error (buffer-substring (point) (- match-end 2)))
161 (if (not (looking-at "+OK"))
162 (progn (setq pop3-read-point match-end) nil)
163 (setq pop3-read-point match-end)
165 (buffer-substring (point) match-end)
169 (defun pop3-string-to-list (string &optional regexp)
170 "Chop up a string into a list."
172 (regexp (or regexp " "))
173 (string (if (string-match "\r" string)
174 (substring string 0 (match-beginning 0))
176 (store-match-data nil)