1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
31 ;; are implemented. The LIST command has not been implemented due to lack
32 ;; of actual usefulness.
33 ;; The optional POP3 command TOP has not been implemented.
35 ;; 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)
50 (defvar pop3-password-required t
51 "*Non-nil if a password is required when connecting to POP server.")
52 (defvar pop3-password nil
53 "*Password to use when connecting to POP server.")
55 (defvar pop3-authentication-scheme 'pass
56 "*POP3 authentication scheme.
57 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
60 (defvar pop3-timestamp nil
61 "Timestamp returned when initially connected to the POP server.
62 Used for APOP authentication.")
64 (defvar pop3-movemail-file-coding-system nil
65 "Coding system for the crashbox made by `pop3-movemail'.")
67 (defvar pop3-read-point nil)
68 (defvar pop3-debug nil)
70 (defun pop3-movemail (&optional crashbox)
71 "Transfer contents of a maildrop to the specified CRASHBOX."
72 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
73 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
74 (crashbuf (get-buffer-create " *pop3-retr*"))
77 (pop3-password pop3-password)
80 (if pop3-debug (switch-to-buffer (process-buffer process)))
82 (if (and pop3-password-required (not pop3-password))
84 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
85 (cond ((equal 'apop pop3-authentication-scheme)
86 (pop3-apop process pop3-maildrop))
87 ((equal 'pass pop3-authentication-scheme)
88 (pop3-user process pop3-maildrop)
90 (t (error "Invalid POP3 authentication scheme")))
91 (setq message-count (car (pop3-stat process)))
93 (while (<= n message-count)
94 (message (format "Retrieving message %d of %d from %s..."
95 n message-count pop3-mailhost))
96 (pop3-retr process n crashbuf)
99 (let ((coding-system-for-write pop3-movemail-file-coding-system))
100 (write-region (point-min) (point-max) crashbox t 'nomesg))
101 (set-buffer (process-buffer process))
102 (while (> (buffer-size) 5000)
103 (goto-char (point-min))
105 (delete-region (point-min) (point))))
106 (pop3-dele process n)
108 (if pop3-debug (sit-for 1) (sit-for 0.1))
111 (kill-buffer crashbuf)
115 (defun pop3-open-server (mailhost port)
116 "Open TCP connection to MAILHOST on PORT.
117 Returns the process associated with the connection."
118 (let ((coding-system-for-read 'binary)
119 (coding-system-for-write 'binary)
122 (set-buffer (get-buffer-create (concat " trace of POP session to %s"
125 (setq pop3-read-point (point-min))
126 (setq process (open-network-stream "POP"(current-buffer) mailhost port))
127 (let ((response (pop3-read-response process t)))
129 (substring response (or (string-match "<" response) 0)
130 (+ 1 (or (string-match ">" response) -1)))))
135 (defun pop3-process-filter (process output)
137 (set-buffer (process-buffer process))
138 (goto-char (point-max))
141 (defun pop3-send-command (process command)
142 (set-buffer (process-buffer process))
143 (goto-char (point-max))
144 ;; (if (= (aref command 0) ?P)
145 ;; (insert "PASS <omitted>\r\n")
146 ;; (insert command "\r\n"))
147 (setq pop3-read-point (point))
148 (goto-char (point-max))
149 (process-send-string process (concat command "\r\n"))
152 (defun pop3-read-response (process &optional return)
153 "Read the response from the server.
154 Return the response string if optional second argument is non-nil."
155 (let ((case-fold-search nil)
158 (set-buffer (process-buffer process))
159 (goto-char pop3-read-point)
160 (while (not (search-forward "\r\n" nil t))
161 (accept-process-output process 3)
162 (goto-char pop3-read-point))
163 (setq match-end (point))
164 (goto-char pop3-read-point)
165 (if (looking-at "-ERR")
166 (error (buffer-substring (point) (- match-end 2)))
167 (if (not (looking-at "+OK"))
168 (progn (setq pop3-read-point match-end) nil)
169 (setq pop3-read-point match-end)
171 (buffer-substring (point) match-end)
175 (defvar pop3-read-passwd nil)
176 (defun pop3-read-passwd (prompt)
177 (if (not pop3-read-passwd)
178 (if (fboundp 'read-passwd)
179 (setq pop3-read-passwd 'read-passwd)
180 (if (load "passwd" t)
181 (setq pop3-read-passwd 'read-passwd)
182 (autoload 'ange-ftp-read-passwd "ange-ftp")
183 (setq pop3-read-passwd 'ange-ftp-read-passwd))))
184 (funcall pop3-read-passwd prompt))
186 (defun pop3-clean-region (start end)
187 (setq end (set-marker (make-marker) end))
190 (while (and (< (point) end) (search-forward "\r\n" end t))
191 (replace-match "\n" t t))
193 (while (and (< (point) end) (re-search-forward "^\\." end t))
194 (replace-match "" t t)
196 (set-marker end nil))
198 (defun pop3-munge-message-separator (start end)
199 "Check to see if a message separator exists. If not, generate one."
200 (if (not (fboundp 'message-make-date)) (autoload 'message-make-date "message"))
203 (narrow-to-region start end)
204 (goto-char (point-min))
205 (if (not (or (looking-at "From .?") ; Unix mail
206 (looking-at "\001\001\001\001\n") ; MMDF
207 (looking-at "BABYL OPTIONS:") ; Babyl
209 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
210 (date (split-string (or (mail-fetch-field "Date")
214 ;; sample date formats I have seen
215 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
216 ;; Date: 08 Jul 1996 23:22:24 -0400
218 ;; Tue Jul 9 09:04:21 1996
220 (cond ((string-match "[A-Z]" (nth 0 date))
221 (format "%s %s %s %s %s"
222 (nth 0 date) (nth 2 date) (nth 1 date)
223 (nth 4 date) (nth 3 date)))
225 ;; this really needs to be better but I don't feel
226 ;; like writing a date to day converter.
227 (format "Sun %s %s %s %s"
228 (nth 1 date) (nth 0 date)
229 (nth 3 date) (nth 2 date)))
231 (setq From_ (format "\nFrom %s %s\n" from date))
232 (while (string-match "," From_)
233 (setq From_ (concat (substring From_ 0 (match-beginning 0))
234 (substring From_ (match-end 0)))))
235 (goto-char (point-min))
237 (re-search-forward "\n\n")
238 (narrow-to-region (point) (point-max))
239 (let ((size (- (point-max) (point-min))))
240 (goto-char (point-min))
243 (insert (format "Content-Length: %s\n" size)))
248 ;; AUTHORIZATION STATE
250 (defun pop3-user (process user)
251 "Send USER information to POP3 server."
252 (pop3-send-command process (format "USER %s" user))
253 (let ((response (pop3-read-response process t)))
254 (if (not (and response (string-match "+OK" response)))
255 (error (format "USER %s not valid." user)))))
257 (defun pop3-pass (process)
258 "Send authentication information to the server."
259 (pop3-send-command process (format "PASS %s" pop3-password))
260 (let ((response (pop3-read-response process t)))
261 (if (not (and response (string-match "+OK" response)))
262 (pop3-quit process))))
264 (defun pop3-apop (process user)
265 "Send alternate authentication information to the server."
266 (let ((pass pop3-password))
267 (if (and pop3-password-required (not pass))
269 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
271 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
272 (pop3-send-command process (format "APOP %s %s" user hash))
273 (let ((response (pop3-read-response process t)))
274 (if (not (and response (string-match "+OK" response)))
275 (pop3-quit process)))))
280 (defvar pop3-md5-program "md5"
281 "*Program to encode its input in MD5.")
283 (defun pop3-md5 (string)
286 (call-process-region (point-min) (point-max)
287 (or shell-file-name "/bin/sh")
288 t (current-buffer) nil
289 "-c" pop3-md5-program)
290 ;; The meaningful output is the first 32 characters.
291 ;; Don't return the newline that follows them!
292 (buffer-substring (point-min) (+ (point-min) 32))))
294 (defun pop3-stat (process)
295 "Return the number of messages in the maildrop and the maildrop's size."
296 (pop3-send-command process "STAT")
297 (let ((response (pop3-read-response process t)))
298 (list (string-to-int (nth 1 (split-string response " ")))
299 (string-to-int (nth 2 (split-string response " "))))
302 (defun pop3-list (process &optional msg)
303 "Scan listing of available messages.
304 This function currently does nothing.")
306 (defun pop3-retr (process msg crashbuf)
307 "Retrieve message-id MSG to buffer CRASHBUF."
308 (pop3-send-command process (format "RETR %s" msg))
309 (pop3-read-response process)
310 (let ((start pop3-read-point) end)
312 (set-buffer (process-buffer process))
313 (while (not (re-search-forward "^\\.\r\n" nil t))
314 (accept-process-output process 3)
315 ;; bill@att.com ... to save wear and tear on the heap
316 ;; uncommented because the condensed version below is a problem for
318 (if (> (buffer-size) 20000) (sleep-for 1))
319 (if (> (buffer-size) 50000) (sleep-for 1))
320 (if (> (buffer-size) 100000) (sleep-for 1))
321 (if (> (buffer-size) 200000) (sleep-for 1))
322 (if (> (buffer-size) 500000) (sleep-for 1))
325 ;; (sometimes causes problems for really large messages.)
326 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
328 (setq pop3-read-point (point-marker))
329 ;; this code does not seem to work for some POP servers...
330 ;; and I cannot figure out why not.
331 ; (goto-char (match-beginning 0))
333 ; (if (not (looking-at "\r\n"))
335 ; (re-search-forward "\\.\r\n")
336 (goto-char (match-beginning 0))
337 (setq end (point-marker))
338 (pop3-clean-region start end)
339 (pop3-munge-message-separator start end)
341 (set-buffer crashbuf)
343 (copy-to-buffer crashbuf start end)
344 (delete-region start end)
347 (defun pop3-dele (process msg)
348 "Mark message-id MSG as deleted."
349 (pop3-send-command process (format "DELE %s" msg))
350 (pop3-read-response process))
352 (defun pop3-noop (process msg)
354 (pop3-send-command process "NOOP")
355 (pop3-read-response process))
357 (defun pop3-last (process)
358 "Return highest accessed message-id number for the session."
359 (pop3-send-command process "LAST")
360 (let ((response (pop3-read-response process t)))
361 (string-to-int (nth 1 (split-string response " ")))
364 (defun pop3-rset (process)
365 "Remove all delete marks from current maildrop."
366 (pop3-send-command process "RSET")
367 (pop3-read-response process))
371 (defun pop3-quit (process)
372 "Close connection to POP3 server.
373 Tell server to remove all messages marked as deleted, unlock the maildrop,
374 and close the connection."
375 (pop3-send-command process "QUIT")
376 (pop3-read-response process t)
379 (set-buffer (process-buffer process))
380 (goto-char (point-max))
381 (delete-process process))))
383 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
385 ;;; AUTHORIZATION STATE
387 ;; Initial TCP connection
389 ;; Restrictions: none
390 ;; Possible responses:
391 ;; +OK [POP3 server ready]
394 ;; Arguments: a server specific user-id (required)
395 ;; Restrictions: authorization state [after unsuccessful USER or PASS
396 ;; Possible responses:
397 ;; +OK [valid user-id]
398 ;; -ERR [invalid user-id]
401 ;; Arguments: a server/user-id specific password (required)
402 ;; Restrictions: authorization state, after successful USER
403 ;; Possible responses:
404 ;; +OK [maildrop locked and ready]
405 ;; -ERR [invalid password]
406 ;; -ERR [unable to lock maildrop]
408 ;;; TRANSACTION STATE
412 ;; Restrictions: transaction state
413 ;; Possible responses:
414 ;; +OK nn mm [# of messages, size of maildrop]
417 ;; Arguments: a message-id (optional)
418 ;; Restrictions: transaction state; msg must not be deleted
419 ;; Possible responses:
420 ;; +OK [scan listing follows]
421 ;; -ERR [no such message]
424 ;; Arguments: a message-id (required)
425 ;; Restrictions: transaction state; msg must not be deleted
426 ;; Possible responses:
427 ;; +OK [message contents follow]
428 ;; -ERR [no such message]
431 ;; Arguments: a message-id (required)
432 ;; Restrictions: transaction state; msg must not be deleted
433 ;; Possible responses:
434 ;; +OK [message deleted]
435 ;; -ERR [no such message]
439 ;; Restrictions: transaction state
440 ;; Possible responses:
445 ;; Restrictions: transaction state
446 ;; Possible responses:
447 ;; +OK nn [highest numbered message accessed]
451 ;; Restrictions: transaction state
452 ;; Possible responses:
453 ;; +OK [all delete marks removed]
459 ;; Restrictions: none
460 ;; Possible responses:
461 ;; +OK [TCP connection closed]
465 ;;; pop3.el ends here