1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 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 (defvar pop3-maildrop (or user-login-name (getenv "LOGNAME") (getenv "USER") nil)
42 (defvar pop3-mailhost (or (getenv "MAILHOST") nil)
47 (defvar pop3-password-required t
48 "*Non-nil if a password is required when connecting to POP server.")
49 (defvar pop3-password nil
50 "*Password to use when connecting to POP server.")
52 (defvar pop3-authentication-scheme 'pass
53 "*POP3 authentication scheme. Defaults to 'pass, for the standard
54 USER/PASS authentication. Other valid values are 'apop.")
56 (defvar pop3-timestamp nil
57 "Timestamp returned when initially connected to the POP server.
58 Used for APOP authentication.")
60 (defvar pop3-read-point nil)
61 (defvar pop3-debug nil)
63 (defun pop3-movemail (&optional crashbox)
64 "Transfer contents of a maildrop to the specified CRASHBOX."
65 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
66 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
67 (crashbuf (get-buffer-create " *pop3-retr*"))
71 (if pop3-debug (switch-to-buffer (process-buffer process)))
72 (cond ((equal 'apop pop3-authentication-scheme)
73 (pop3-apop process pop3-maildrop))
74 ((equal 'pass pop3-authentication-scheme)
75 (pop3-user process pop3-maildrop)
77 (t (error "Invalid POP3 authentication scheme.")))
78 (setq message-count (car (pop3-stat process)))
79 (while (<= n message-count)
80 (message (format "Retrieving message %d of %d from %s..."
81 n message-count pop3-mailhost))
82 (pop3-retr process n crashbuf)
85 (append-to-file (point-min) (point-max) crashbox))
89 (kill-buffer crashbuf)
93 (defun pop3-open-server (mailhost port)
94 "Open TCP connection to MAILHOST.
95 Returns the process associated with the connection."
97 (get-buffer-create (format "trace of POP session to %s" mailhost)))
100 (set-buffer process-buffer)
103 (open-network-stream "POP" process-buffer mailhost port))
104 (setq pop3-read-point (point-min))
105 (let ((response (pop3-read-response process t)))
107 (substring response (or (string-match "<" response) 0)
108 (+ 1 (or (string-match ">" response) -1)))))
114 (defun pop3-process-filter (process output)
116 (set-buffer (process-buffer process))
117 (goto-char (point-max))
120 (defun pop3-send-command (process command)
121 (set-buffer (process-buffer process))
122 (goto-char (point-max))
123 ;; (if (= (aref command 0) ?P)
124 ;; (insert "PASS <omitted>\r\n")
125 ;; (insert command "\r\n"))
126 (setq pop3-read-point (point))
127 (goto-char (point-max))
128 (process-send-string process command)
129 (process-send-string process "\r\n")
132 (defun pop3-read-response (process &optional return)
133 "Read the response from the server.
134 Return the response string if optional second argument is non-nil."
135 (let ((case-fold-search nil)
138 (set-buffer (process-buffer process))
139 (goto-char pop3-read-point)
140 (while (not (search-forward "\r\n" nil t))
141 (accept-process-output process)
142 (goto-char pop3-read-point))
143 (setq match-end (point))
144 (goto-char pop3-read-point)
145 (if (looking-at "-ERR")
146 (error (buffer-substring (point) (- match-end 2)))
147 (if (not (looking-at "+OK"))
148 (progn (setq pop3-read-point match-end) nil)
149 (setq pop3-read-point match-end)
151 (buffer-substring (point) match-end)
155 (defun pop3-string-to-list (string &optional regexp)
156 "Chop up a string into a list."
158 (regexp (or regexp " "))
159 (string (if (string-match "\r" string)
160 (substring string 0 (match-beginning 0))
162 (store-match-data nil)
164 (if (string-match regexp string)
165 (setq list (cons (substring string 0 (- (match-end 0) 1)) list)
166 string (substring string (match-end 0)))
167 (setq list (cons string list)
171 (defvar pop3-read-passwd nil)
172 (defun pop3-read-passwd (prompt)
173 (if (not pop3-read-passwd)
174 (if (load "passwd" t)
175 (setq pop3-read-passwd 'read-passwd)
176 (autoload 'ange-ftp-read-passwd "ange-ftp")
177 (setq pop3-read-passwd 'ange-ftp-read-passwd)))
178 (funcall pop3-read-passwd prompt))
180 (defun pop3-clean-region (start end)
181 (setq end (set-marker (make-marker) end))
184 (while (and (< (point) end) (search-forward "\r\n" end t))
185 (replace-match "\n" t t))
187 (while (and (< (point) end) (re-search-forward "^\\." end t))
188 (replace-match "" t t)
190 (set-marker end nil))
192 (defun pop3-munge-message-separator (start end)
193 "Check to see if a message separator exists. If not, generate one."
196 (narrow-to-region start end)
197 (goto-char (point-min))
198 (if (not (or (looking-at "From .?") ; Unix mail
199 (looking-at "\001\001\001\001\n") ; MMDF
200 (looking-at "BABYL OPTIONS:") ; Babyl
202 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
203 (date (pop3-string-to-list (mail-fetch-field "Date")))
205 ;; sample date formats I have seen
206 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
207 ;; Date: 08 Jul 1996 23:22:24 -0400
209 ;; Tue Jul 9 09:04:21 1996
211 (cond ((string-match "[A-Z]" (nth 0 date))
212 (format "%s %s %s %s %s"
213 (nth 0 date) (nth 2 date) (nth 1 date)
214 (nth 4 date) (nth 3 date)))
216 ;; this really needs to be better but I don't feel
217 ;; like writing a date to day converter.
218 (format "Sun %s %s %s %s"
219 (nth 1 date) (nth 0 date)
220 (nth 3 date) (nth 2 date)))
222 (setq From_ (format "From %s %s\n" from date))
223 (while (string-match "," From_)
224 (setq From_ (concat (substring From_ 0 (match-beginning 0))
225 (substring From_ (match-end 0)))))
226 (goto-char (point-min))
231 ;; AUTHORIZATION STATE
233 (defun pop3-user (process user)
234 "Send USER information to POP3 server."
235 (pop3-send-command process (format "USER %s" user))
236 (let ((response (pop3-read-response process t)))
237 (if (not (and response (string-match "+OK" response)))
238 (error (format "USER %s not valid." user)))))
240 (defun pop3-pass (process)
241 "Send authentication information to the server."
242 (let ((pass pop3-password))
243 (if (and pop3-password-required (not pass))
245 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
248 (pop3-send-command process (format "PASS %s" pass))
249 (let ((response (pop3-read-response process t)))
250 (if (not (and response (string-match "+OK" response)))
251 (pop3-quit process)))))
254 (defun pop3-apop (process user)
255 "Send alternate authentication information to the server."
256 (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
257 (let ((pass pop3-password))
258 (if (and pop3-password-required (not pass))
260 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
262 (let ((hash (md5 (concat pop3-timestamp pass))))
263 (pop3-send-command process (format "APOP %s %s" user hash))
264 (let ((response (pop3-read-response process t)))
265 (if (not (and response (string-match "+OK" response)))
266 (pop3-quit process)))))
271 (defun pop3-stat (process)
272 "Return a list of the number of messages in the maildrop and the size
274 (pop3-send-command process "STAT")
275 (let ((response (pop3-read-response process t)))
276 (list (string-to-int (nth 1 (pop3-string-to-list response)))
277 (string-to-int (nth 2 (pop3-string-to-list response))))
280 (defun pop3-list (process &optional msg)
281 "Scan listing of available messages.
282 This function currently does nothing.")
284 (defun pop3-retr (process msg crashbuf)
285 "Retrieve message-id MSG from the server and place the contents in
287 (pop3-send-command process (format "RETR %s" msg))
288 (pop3-read-response process)
289 (let ((start pop3-read-point) end)
291 (set-buffer (process-buffer process))
292 (while (not (re-search-forward "^\\.\r\n" nil t))
293 (accept-process-output process)
294 ;; bill@att.com ... to save wear and tear on the heap
295 (if (> (buffer-size) 20000) (sleep-for 1))
296 (if (> (buffer-size) 50000) (sleep-for 1))
297 (if (> (buffer-size) 100000) (sleep-for 1))
298 (if (> (buffer-size) 200000) (sleep-for 1))
299 (if (> (buffer-size) 500000) (sleep-for 1))
302 (setq pop3-read-point (point-marker))
303 (goto-char (match-beginning 0))
304 (setq end (point-marker))
305 (pop3-clean-region start end)
306 (pop3-munge-message-separator start end)
308 (set-buffer crashbuf)
310 (copy-to-buffer crashbuf start end)
311 (delete-region start end)
314 (defun pop3-dele (process msg)
315 "Mark message-id MSG as deleted."
316 (pop3-send-command process (format "DELE %s" msg))
317 (pop3-read-response process))
319 (defun pop3-noop (process msg)
321 (pop3-send-command process "NOOP")
322 (pop3-read-response process))
324 (defun pop3-last (process)
325 "Return highest accessed message-id number for the session."
326 (pop3-send-command process "LAST")
327 (let ((response (pop3-read-response process t)))
328 (string-to-int (nth 1 (pop3-string-to-list response)))
331 (defun pop3-rset (process)
332 "Remove all delete marks from current maildrop."
333 (pop3-send-command process "RSET")
334 (pop3-read-response process))
338 (defun pop3-quit (process)
339 "Tell server to remove all messages marked as deleted, unlock the
340 maildrop, and close the connection."
341 (pop3-send-command process "QUIT")
342 (pop3-read-response process t)
345 (set-buffer (process-buffer process))
346 (goto-char (point-max))
347 (delete-process process))))
349 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
351 ;;; AUTHORIZATION STATE
353 ;; Initial TCP connection
355 ;; Restrictions: none
356 ;; Possible responses:
357 ;; +OK [POP3 server ready]
360 ;; Arguments: a server specific user-id (required)
361 ;; Restrictions: authorization state [after unsuccessful USER or PASS
362 ;; Possible responses:
363 ;; +OK [valid user-id]
364 ;; -ERR [invalid user-id]
367 ;; Arguments: a server/user-id specific password (required)
368 ;; Restrictions: authorization state, after successful USER
369 ;; Possible responses:
370 ;; +OK [maildrop locked and ready]
371 ;; -ERR [invalid password]
372 ;; -ERR [unable to lock maildrop]
374 ;;; TRANSACTION STATE
378 ;; Restrictions: transaction state
379 ;; Possible responses:
380 ;; +OK nn mm [# of messages, size of maildrop]
383 ;; Arguments: a message-id (optional)
384 ;; Restrictions: transaction state; msg must not be deleted
385 ;; Possible responses:
386 ;; +OK [scan listing follows]
387 ;; -ERR [no such message]
390 ;; Arguments: a message-id (required)
391 ;; Restrictions: transaction state; msg must not be deleted
392 ;; Possible responses:
393 ;; +OK [message contents follow]
394 ;; -ERR [no such message]
397 ;; Arguments: a message-id (required)
398 ;; Restrictions: transaction state; msg must not be deleted
399 ;; Possible responses:
400 ;; +OK [message deleted]
401 ;; -ERR [no such message]
405 ;; Restrictions: transaction state
406 ;; Possible responses:
411 ;; Restrictions: transaction state
412 ;; Possible responses:
413 ;; +OK nn [highest numbered message accessed]
417 ;; Restrictions: transaction state
418 ;; Possible responses:
419 ;; +OK [all delete marks removed]
425 ;; Restrictions: none
426 ;; Possible responses:
427 ;; +OK [TCP connection closed]