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.3j")
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-read-point nil)
64 (defvar pop3-debug nil)
66 (defun pop3-movemail (&optional crashbox)
67 "Transfer contents of a maildrop to the specified CRASHBOX."
68 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
69 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
70 (crashbuf (get-buffer-create " *pop3-retr*"))
73 (pop3-password pop3-password)
76 (if pop3-debug (switch-to-buffer (process-buffer process)))
78 (if (and pop3-password-required (not pop3-password))
80 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
81 (cond ((equal 'apop pop3-authentication-scheme)
82 (pop3-apop process pop3-maildrop))
83 ((equal 'pass pop3-authentication-scheme)
84 (pop3-user process pop3-maildrop)
86 (t (error "Invalid POP3 authentication scheme.")))
87 (setq message-count (car (pop3-stat process)))
88 (while (<= n message-count)
89 (message (format "Retrieving message %d of %d from %s..."
90 n message-count pop3-mailhost))
91 (pop3-retr process n crashbuf)
94 (append-to-file (point-min) (point-max) crashbox)
95 (set-buffer (process-buffer process))
96 (while (> (buffer-size) 5000)
97 (goto-char (point-min))
99 (delete-region (point-min) (point))))
100 (pop3-dele process n)
102 (if pop3-debug (sit-for 1) (sit-for 0.1))
105 (kill-buffer crashbuf)
109 (defun pop3-open-server (mailhost port)
110 "Open TCP connection to MAILHOST.
111 Returns the process associated with the connection."
112 (let ((process-buffer
113 (get-buffer-create (format "trace of POP session to %s" mailhost)))
116 (set-buffer process-buffer)
118 (setq pop3-read-point (point-min))
121 (open-network-stream "POP" process-buffer mailhost port))
122 (let ((response (pop3-read-response process t)))
124 (substring response (or (string-match "<" response) 0)
125 (+ 1 (or (string-match ">" response) -1)))))
131 (defun pop3-process-filter (process output)
133 (set-buffer (process-buffer process))
134 (goto-char (point-max))
137 (defun pop3-send-command (process command)
138 (set-buffer (process-buffer process))
139 (goto-char (point-max))
140 ;; (if (= (aref command 0) ?P)
141 ;; (insert "PASS <omitted>\r\n")
142 ;; (insert command "\r\n"))
143 (setq pop3-read-point (point))
144 (goto-char (point-max))
145 (process-send-string process command)
146 (process-send-string process "\r\n")
149 (defun pop3-read-response (process &optional return)
150 "Read the response from the server.
151 Return the response string if optional second argument is non-nil."
152 (let ((case-fold-search nil)
155 (set-buffer (process-buffer process))
156 (goto-char pop3-read-point)
157 (while (not (search-forward "\r\n" nil t))
158 (accept-process-output process 3)
159 (goto-char pop3-read-point))
160 (setq match-end (point))
161 (goto-char pop3-read-point)
162 (if (looking-at "-ERR")
163 (error (buffer-substring (point) (- match-end 2)))
164 (if (not (looking-at "+OK"))
165 (progn (setq pop3-read-point match-end) nil)
166 (setq pop3-read-point match-end)
168 (buffer-substring (point) match-end)
172 (defun pop3-string-to-list (string &optional regexp)
173 "Chop up a string into a list."
175 (regexp (or regexp " "))
176 (string (if (string-match "\r" string)
177 (substring string 0 (match-beginning 0))
179 (store-match-data nil)
181 (if (string-match regexp string)
182 (setq list (cons (substring string 0 (- (match-end 0) 1)) list)
183 string (substring string (match-end 0)))
184 (setq list (cons string list)
188 (defvar pop3-read-passwd nil)
189 (defun pop3-read-passwd (prompt)
190 (if (not pop3-read-passwd)
191 (if (load "passwd" t)
192 (setq pop3-read-passwd 'read-passwd)
193 (autoload 'ange-ftp-read-passwd "ange-ftp")
194 (setq pop3-read-passwd 'ange-ftp-read-passwd)))
195 (funcall pop3-read-passwd prompt))
197 (defun pop3-clean-region (start end)
198 (setq end (set-marker (make-marker) end))
201 (while (and (< (point) end) (search-forward "\r\n" end t))
202 (replace-match "\n" t t))
204 (while (and (< (point) end) (re-search-forward "^\\." end t))
205 (replace-match "" t t)
207 (set-marker end nil))
209 (defun pop3-munge-message-separator (start end)
210 "Check to see if a message separator exists. If not, generate one."
211 (if (not (fboundp 'message-make-date)) (autoload 'message-make-date "message"))
214 (narrow-to-region start end)
215 (goto-char (point-min))
216 (if (not (or (looking-at "From .?") ; Unix mail
217 (looking-at "\001\001\001\001\n") ; MMDF
218 (looking-at "BABYL OPTIONS:") ; Babyl
220 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
221 (date (pop3-string-to-list (or (mail-fetch-field "Date")
222 (message-make-date))))
224 ;; sample date formats I have seen
225 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
226 ;; Date: 08 Jul 1996 23:22:24 -0400
228 ;; Tue Jul 9 09:04:21 1996
230 (cond ((string-match "[A-Z]" (nth 0 date))
231 (format "%s %s %s %s %s"
232 (nth 0 date) (nth 2 date) (nth 1 date)
233 (nth 4 date) (nth 3 date)))
235 ;; this really needs to be better but I don't feel
236 ;; like writing a date to day converter.
237 (format "Sun %s %s %s %s"
238 (nth 1 date) (nth 0 date)
239 (nth 3 date) (nth 2 date)))
241 (setq From_ (format "\nFrom %s %s\n" from date))
242 (while (string-match "," From_)
243 (setq From_ (concat (substring From_ 0 (match-beginning 0))
244 (substring From_ (match-end 0)))))
245 (goto-char (point-min))
250 ;; AUTHORIZATION STATE
252 (defun pop3-user (process user)
253 "Send USER information to POP3 server."
254 (pop3-send-command process (format "USER %s" user))
255 (let ((response (pop3-read-response process t)))
256 (if (not (and response (string-match "+OK" response)))
257 (error (format "USER %s not valid." user)))))
259 (defun pop3-pass (process)
260 "Send authentication information to the server."
261 (pop3-send-command process (format "PASS %s" pop3-password))
262 (let ((response (pop3-read-response process t)))
263 (if (not (and response (string-match "+OK" response)))
264 (pop3-quit process))))
266 (defun pop3-apop (process user)
267 "Send alternate authentication information to the server."
268 (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
269 (let ((hash (md5 (concat pop3-timestamp pop3-password))))
270 (pop3-send-command process (format "APOP %s %s" user hash))
271 (let ((response (pop3-read-response process t)))
272 (if (not (and response (string-match "+OK" response)))
273 (pop3-quit process)))))
277 (defun pop3-stat (process)
278 "Return the number of messages in the maildrop and the maildrop's size."
279 (pop3-send-command process "STAT")
280 (let ((response (pop3-read-response process t)))
281 (list (string-to-int (nth 1 (pop3-string-to-list response)))
282 (string-to-int (nth 2 (pop3-string-to-list response))))
285 (defun pop3-list (process &optional msg)
286 "Scan listing of available messages.
287 This function currently does nothing.")
289 (defun pop3-retr (process msg crashbuf)
290 "Retrieve message-id MSG to buffer CRASHBUF."
291 (pop3-send-command process (format "RETR %s" msg))
292 (pop3-read-response process)
293 (let ((start pop3-read-point) end)
295 (set-buffer (process-buffer process))
296 (while (not (re-search-forward "^\\.\r\n" nil t))
297 (accept-process-output process 3)
298 ; ;; bill@att.com ... to save wear and tear on the heap
299 ; (if (> (buffer-size) 20000) (sleep-for 1))
300 ; (if (> (buffer-size) 50000) (sleep-for 1))
301 ; (if (> (buffer-size) 100000) (sleep-for 1))
302 ; (if (> (buffer-size) 200000) (sleep-for 1))
303 ; (if (> (buffer-size) 500000) (sleep-for 1))
306 (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
308 (setq pop3-read-point (point-marker))
309 ;; this code does not seem to work for some POP servers...
310 ;; and I cannot figure out why not.
311 ; (goto-char (match-beginning 0))
313 ; (if (not (looking-at "\r\n"))
315 ; (re-search-forward "\\.\r\n")
316 (goto-char (match-beginning 0))
317 (setq end (point-marker))
318 (pop3-clean-region start end)
319 (pop3-munge-message-separator start end)
321 (set-buffer crashbuf)
323 (copy-to-buffer crashbuf start end)
324 (delete-region start end)
327 (defun pop3-dele (process msg)
328 "Mark message-id MSG as deleted."
329 (pop3-send-command process (format "DELE %s" msg))
330 (pop3-read-response process))
332 (defun pop3-noop (process msg)
334 (pop3-send-command process "NOOP")
335 (pop3-read-response process))
337 (defun pop3-last (process)
338 "Return highest accessed message-id number for the session."
339 (pop3-send-command process "LAST")
340 (let ((response (pop3-read-response process t)))
341 (string-to-int (nth 1 (pop3-string-to-list response)))
344 (defun pop3-rset (process)
345 "Remove all delete marks from current maildrop."
346 (pop3-send-command process "RSET")
347 (pop3-read-response process))
351 (defun pop3-quit (process)
352 "Close connection to POP3 server.
353 Tell server to remove all messages marked as deleted, unlock the maildrop,
354 and close the connection."
355 (pop3-send-command process "QUIT")
356 (pop3-read-response process t)
359 (set-buffer (process-buffer process))
360 (goto-char (point-max))
361 (delete-process process))))
363 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
365 ;;; AUTHORIZATION STATE
367 ;; Initial TCP connection
369 ;; Restrictions: none
370 ;; Possible responses:
371 ;; +OK [POP3 server ready]
374 ;; Arguments: a server specific user-id (required)
375 ;; Restrictions: authorization state [after unsuccessful USER or PASS
376 ;; Possible responses:
377 ;; +OK [valid user-id]
378 ;; -ERR [invalid user-id]
381 ;; Arguments: a server/user-id specific password (required)
382 ;; Restrictions: authorization state, after successful USER
383 ;; Possible responses:
384 ;; +OK [maildrop locked and ready]
385 ;; -ERR [invalid password]
386 ;; -ERR [unable to lock maildrop]
388 ;;; TRANSACTION STATE
392 ;; Restrictions: transaction state
393 ;; Possible responses:
394 ;; +OK nn mm [# of messages, size of maildrop]
397 ;; Arguments: a message-id (optional)
398 ;; Restrictions: transaction state; msg must not be deleted
399 ;; Possible responses:
400 ;; +OK [scan listing follows]
401 ;; -ERR [no such message]
404 ;; Arguments: a message-id (required)
405 ;; Restrictions: transaction state; msg must not be deleted
406 ;; Possible responses:
407 ;; +OK [message contents follow]
408 ;; -ERR [no such message]
411 ;; Arguments: a message-id (required)
412 ;; Restrictions: transaction state; msg must not be deleted
413 ;; Possible responses:
414 ;; +OK [message deleted]
415 ;; -ERR [no such message]
419 ;; Restrictions: transaction state
420 ;; Possible responses:
425 ;; Restrictions: transaction state
426 ;; Possible responses:
427 ;; +OK nn [highest numbered message accessed]
431 ;; Restrictions: transaction state
432 ;; Possible responses:
433 ;; +OK [all delete marks removed]
439 ;; Restrictions: none
440 ;; Possible responses:
441 ;; +OK [TCP connection closed]