1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996,1997,1998 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.3m+")
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-authentication-scheme-alist nil
60 "*Alist of host and POP3 authentication scheme.")
62 (defvar pop3-timestamp nil
63 "Timestamp returned when initially connected to the POP server.
64 Used for APOP authentication.")
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*"))
76 (pop3-password pop3-password)
77 (pop3-authentication-scheme pop3-authentication-scheme)
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 (let ((tmp-scheme (cdr (assoc pop3-mailhost
86 pop3-authentication-scheme-alist))))
88 (setq pop3-authentication-scheme tmp-scheme)))
89 (cond ((equal 'apop pop3-authentication-scheme)
90 (pop3-apop process pop3-maildrop))
91 ((equal 'pass pop3-authentication-scheme)
92 (pop3-user process pop3-maildrop)
94 (t (error "Invalid POP3 authentication scheme.")))
95 (setq message-count (car (pop3-stat process)))
96 (while (<= n message-count)
97 (message (format "Retrieving message %d of %d from %s..."
98 n message-count pop3-mailhost))
99 (pop3-retr process n crashbuf)
101 (set-buffer crashbuf)
102 (write-region-as-binary (point-min) (point-max) crashbox 'append)
103 (set-buffer (process-buffer process))
104 (while (> (buffer-size) 5000)
105 (goto-char (point-min))
107 (delete-region (point-min) (point))))
108 (pop3-dele process n)
110 (if pop3-debug (sit-for 1) (sit-for 0.1))
113 (kill-buffer crashbuf)
117 (defun pop3-open-server (mailhost port)
118 "Open TCP connection to MAILHOST.
119 Returns the process associated with the connection."
120 (let ((process-buffer
121 (get-buffer-create (format "trace of POP session to %s" mailhost)))
124 (set-buffer process-buffer)
126 (setq pop3-read-point (point-min))
129 (open-network-stream-as-binary "POP" process-buffer mailhost port))
130 (let ((response (pop3-read-response process t)))
132 (substring response (or (string-match "<" response) 0)
133 (+ 1 (or (string-match ">" response) -1)))))
138 (defun pop3-process-filter (process output)
140 (set-buffer (process-buffer process))
141 (goto-char (point-max))
144 (defun pop3-send-command (process command)
145 (set-buffer (process-buffer process))
146 (goto-char (point-max))
147 ;; (if (= (aref command 0) ?P)
148 ;; (insert "PASS <omitted>\r\n")
149 ;; (insert command "\r\n"))
150 (setq pop3-read-point (point))
151 (goto-char (point-max))
152 (process-send-string process command)
153 (process-send-string process "\r\n")
156 (defun pop3-read-response (process &optional return)
157 "Read the response from the server.
158 Return the response string if optional second argument is non-nil."
159 (let ((case-fold-search nil)
162 (set-buffer (process-buffer process))
163 (goto-char pop3-read-point)
164 (while (not (search-forward "\r\n" nil t))
165 (accept-process-output process 3)
166 (goto-char pop3-read-point))
167 (setq match-end (point))
168 (goto-char pop3-read-point)
169 (if (looking-at "-ERR")
170 (error (buffer-substring (point) (- match-end 2)))
171 (if (not (looking-at "+OK"))
172 (progn (setq pop3-read-point match-end) nil)
173 (setq pop3-read-point match-end)
175 (buffer-substring (point) match-end)
179 (defun pop3-string-to-list (string &optional regexp)
180 "Chop up a string into a list."
182 (regexp (or regexp " "))
183 (string (if (string-match "\r" string)
184 (substring string 0 (match-beginning 0))
186 (store-match-data nil)
188 (if (string-match regexp string)
189 (setq list (cons (substring string 0 (- (match-end 0) 1)) list)
190 string (substring string (match-end 0)))
191 (setq list (cons string list)
195 (defvar pop3-read-passwd nil)
196 (defun pop3-read-passwd (prompt)
197 (if (not pop3-read-passwd)
198 (if (functionp 'read-passwd)
199 (setq pop3-read-passwd 'read-passwd)
200 (if (load "passwd" t)
201 (setq pop3-read-passwd 'read-passwd)
202 (autoload 'ange-ftp-read-passwd "ange-ftp")
203 (setq pop3-read-passwd 'ange-ftp-read-passwd))))
204 (funcall pop3-read-passwd prompt))
206 (defun pop3-clean-region (start end)
207 (setq end (set-marker (make-marker) end))
210 (while (and (< (point) end) (search-forward "\r\n" end t))
211 (replace-match "\n" t t))
213 (while (and (< (point) end) (re-search-forward "^\\." end t))
214 (replace-match "" t t)
216 (set-marker end nil))
218 (defun pop3-munge-message-separator (start end)
219 "Check to see if a message separator exists. If not, generate one."
220 (if (not (fboundp 'message-make-date)) (autoload 'message-make-date "message"))
223 (narrow-to-region start end)
224 (goto-char (point-min))
225 (if (not (or (looking-at "From .?") ; Unix mail
226 (looking-at "\001\001\001\001\n") ; MMDF
227 (looking-at "BABYL OPTIONS:") ; Babyl
229 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
230 (date (pop3-string-to-list (or (mail-fetch-field "Date")
231 (message-make-date))))
233 ;; sample date formats I have seen
234 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
235 ;; Date: 08 Jul 1996 23:22:24 -0400
237 ;; Tue Jul 9 09:04:21 1996
239 (cond ((string-match "[A-Z]" (nth 0 date))
240 (format "%s %s %s %s %s"
241 (nth 0 date) (nth 2 date) (nth 1 date)
242 (nth 4 date) (nth 3 date)))
244 ;; this really needs to be better but I don't feel
245 ;; like writing a date to day converter.
246 (format "Sun %s %s %s %s"
247 (nth 1 date) (nth 0 date)
248 (nth 3 date) (nth 2 date)))
250 (setq From_ (format "\nFrom %s %s\n" from date))
251 (while (string-match "," From_)
252 (setq From_ (concat (substring From_ 0 (match-beginning 0))
253 (substring From_ (match-end 0)))))
254 (goto-char (point-min))
259 ;; AUTHORIZATION STATE
261 (defun pop3-user (process user)
262 "Send USER information to POP3 server."
263 (pop3-send-command process (format "USER %s" user))
264 (let ((response (pop3-read-response process t)))
265 (if (not (and response (string-match "+OK" response)))
266 (error (format "USER %s not valid." user)))))
268 (defun pop3-pass (process)
269 "Send authentication information to the server."
270 (pop3-send-command process (format "PASS %s" pop3-password))
271 (let ((response (pop3-read-response process t)))
272 (if (not (and response (string-match "+OK" response)))
273 (pop3-quit process))))
275 (defun pop3-apop (process user)
276 "Send alternate authentication information to the server."
277 (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
278 (let ((hash (md5 (concat pop3-timestamp pop3-password))))
279 (pop3-send-command process (format "APOP %s %s" user hash))
280 (let ((response (pop3-read-response process t)))
281 (if (not (and response (string-match "+OK" response)))
282 (pop3-quit process)))))
286 (defun pop3-stat (process)
287 "Return the number of messages in the maildrop and the maildrop's size."
288 (pop3-send-command process "STAT")
289 (let ((response (pop3-read-response process t)))
290 (list (string-to-int (nth 1 (pop3-string-to-list response)))
291 (string-to-int (nth 2 (pop3-string-to-list response))))
294 (defun pop3-list (process &optional msg)
295 "Scan listing of available messages.
296 This function currently does nothing.")
298 (defun pop3-retr (process msg crashbuf)
299 "Retrieve message-id MSG to buffer CRASHBUF."
300 (pop3-send-command process (format "RETR %s" msg))
301 (pop3-read-response process)
302 (let ((start pop3-read-point) end)
304 (set-buffer (process-buffer process))
305 (while (not (re-search-forward "^\\.\r\n" nil t))
306 (accept-process-output process 3)
307 ;; bill@att.com ... to save wear and tear on the heap
308 ;; uncommented because the condensed version below is a problem for
310 (if (> (buffer-size) 20000) (sleep-for 1))
311 (if (> (buffer-size) 50000) (sleep-for 1))
312 (if (> (buffer-size) 100000) (sleep-for 1))
313 (if (> (buffer-size) 200000) (sleep-for 1))
314 (if (> (buffer-size) 500000) (sleep-for 1))
317 ;; (sometimes causes problems for really large messages.)
318 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
320 (setq pop3-read-point (point-marker))
321 ;; this code does not seem to work for some POP servers...
322 ;; and I cannot figure out why not.
323 ; (goto-char (match-beginning 0))
325 ; (if (not (looking-at "\r\n"))
327 ; (re-search-forward "\\.\r\n")
328 (goto-char (match-beginning 0))
329 (setq end (point-marker))
330 (pop3-clean-region start end)
331 (pop3-munge-message-separator start end)
333 (set-buffer crashbuf)
335 (copy-to-buffer crashbuf start end)
336 (delete-region start end)
339 (defun pop3-dele (process msg)
340 "Mark message-id MSG as deleted."
341 (pop3-send-command process (format "DELE %s" msg))
342 (pop3-read-response process))
344 (defun pop3-noop (process msg)
346 (pop3-send-command process "NOOP")
347 (pop3-read-response process))
349 (defun pop3-last (process)
350 "Return highest accessed message-id number for the session."
351 (pop3-send-command process "LAST")
352 (let ((response (pop3-read-response process t)))
353 (string-to-int (nth 1 (pop3-string-to-list response)))
356 (defun pop3-rset (process)
357 "Remove all delete marks from current maildrop."
358 (pop3-send-command process "RSET")
359 (pop3-read-response process))
363 (defun pop3-quit (process)
364 "Close connection to POP3 server.
365 Tell server to remove all messages marked as deleted, unlock the maildrop,
366 and close the connection."
367 (pop3-send-command process "QUIT")
368 (pop3-read-response process t)
371 (set-buffer (process-buffer process))
372 (goto-char (point-max))
373 (delete-process process))))
375 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
377 ;;; AUTHORIZATION STATE
379 ;; Initial TCP connection
381 ;; Restrictions: none
382 ;; Possible responses:
383 ;; +OK [POP3 server ready]
386 ;; Arguments: a server specific user-id (required)
387 ;; Restrictions: authorization state [after unsuccessful USER or PASS
388 ;; Possible responses:
389 ;; +OK [valid user-id]
390 ;; -ERR [invalid user-id]
393 ;; Arguments: a server/user-id specific password (required)
394 ;; Restrictions: authorization state, after successful USER
395 ;; Possible responses:
396 ;; +OK [maildrop locked and ready]
397 ;; -ERR [invalid password]
398 ;; -ERR [unable to lock maildrop]
400 ;;; TRANSACTION STATE
404 ;; Restrictions: transaction state
405 ;; Possible responses:
406 ;; +OK nn mm [# of messages, size of maildrop]
409 ;; Arguments: a message-id (optional)
410 ;; Restrictions: transaction state; msg must not be deleted
411 ;; Possible responses:
412 ;; +OK [scan listing follows]
413 ;; -ERR [no such message]
416 ;; Arguments: a message-id (required)
417 ;; Restrictions: transaction state; msg must not be deleted
418 ;; Possible responses:
419 ;; +OK [message contents follow]
420 ;; -ERR [no such message]
423 ;; Arguments: a message-id (required)
424 ;; Restrictions: transaction state; msg must not be deleted
425 ;; Possible responses:
426 ;; +OK [message deleted]
427 ;; -ERR [no such message]
431 ;; Restrictions: transaction state
432 ;; Possible responses:
437 ;; Restrictions: transaction state
438 ;; Possible responses:
439 ;; +OK nn [highest numbered message accessed]
443 ;; Restrictions: transaction state
444 ;; Possible responses:
445 ;; +OK [all delete marks removed]
451 ;; Restrictions: none
452 ;; Possible responses:
453 ;; +OK [TCP connection closed]