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>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
30 ;; are implemented. The LIST command has not been implemented due to lack
31 ;; of actual usefulness.
32 ;; The optional POP3 command TOP has not been implemented.
34 ;; 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.
54 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
57 (defvar pop3-timestamp nil
58 "Timestamp returned when initially connected to the POP server.
59 Used for APOP authentication.")
61 (defvar pop3-read-point nil)
62 (defvar pop3-debug nil)
64 (defun pop3-movemail (&optional crashbox)
65 "Transfer contents of a maildrop to the specified CRASHBOX."
66 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
67 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
68 (crashbuf (get-buffer-create " *pop3-retr*"))
71 (pop3-password pop3-password)
74 (if pop3-debug (switch-to-buffer (process-buffer process)))
76 (if (and pop3-password-required (not pop3-password))
78 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
79 (cond ((equal 'apop pop3-authentication-scheme)
80 (pop3-apop process pop3-maildrop))
81 ((equal 'pass pop3-authentication-scheme)
82 (pop3-user process pop3-maildrop)
84 (t (error "Invalid POP3 authentication scheme")))
85 (setq message-count (car (pop3-stat process)))
87 (while (<= n message-count)
88 (message (format "Retrieving message %d of %d from %s..."
89 n message-count pop3-mailhost))
90 (pop3-retr process n crashbuf)
93 (let ((coding-system-for-write 'binary))
94 (write-region (point-min) (point-max) crashbox t 'nomesg))
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 on PORT.
111 Returns the process associated with the connection."
112 (let ((coding-system-for-read 'binary)
113 (coding-system-for-write 'binary)
116 (set-buffer (get-buffer-create (concat " trace of POP session to "
119 (setq pop3-read-point (point-min))
120 (setq process (open-network-stream "POP"(current-buffer) mailhost port))
121 (let ((response (pop3-read-response process t)))
123 (substring response (or (string-match "<" response) 0)
124 (+ 1 (or (string-match ">" response) -1)))))
129 (defun pop3-process-filter (process output)
131 (set-buffer (process-buffer process))
132 (goto-char (point-max))
135 (defun pop3-send-command (process command)
136 (set-buffer (process-buffer process))
137 (goto-char (point-max))
138 ;; (if (= (aref command 0) ?P)
139 ;; (insert "PASS <omitted>\r\n")
140 ;; (insert command "\r\n"))
141 (setq pop3-read-point (point))
142 (goto-char (point-max))
143 (process-send-string process (concat command "\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 (defvar pop3-read-passwd nil)
170 (defun pop3-read-passwd (prompt)
171 (if (not pop3-read-passwd)
172 (if (fboundp 'read-passwd)
173 (setq pop3-read-passwd '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 (eval-when-compile (defvar parse-time-months))
194 ;; Copied from message-make-date.
195 (defun pop3-make-date (&optional now)
196 "Make a valid date header.
197 If NOW, use that time instead."
198 (require 'parse-time)
199 (let* ((now (or now (current-time)))
200 (zone (nth 8 (decode-time now)))
204 (setq zone (- zone)))
206 (format-time-string "%d" now)
207 ;; The month name of the %b spec is locale-specific. Pfff.
209 (capitalize (car (rassoc (nth 4 (decode-time now))
210 parse-time-months))))
211 (format-time-string "%Y %H:%M:%S " now)
212 ;; We do all of this because XEmacs doesn't have the %z spec.
213 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
215 (defun pop3-munge-message-separator (start end)
216 "Check to see if a message separator exists. If not, generate one."
219 (narrow-to-region start end)
220 (goto-char (point-min))
221 (if (not (or (looking-at "From .?") ; Unix mail
222 (looking-at "\001\001\001\001\n") ; MMDF
223 (looking-at "BABYL OPTIONS:") ; Babyl
225 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
226 (date (split-string (or (mail-fetch-field "Date")
230 ;; sample date formats I have seen
231 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
232 ;; Date: 08 Jul 1996 23:22:24 -0400
234 ;; Tue Jul 9 09:04:21 1996
236 (cond ((string-match "[A-Z]" (nth 0 date))
237 (format "%s %s %s %s %s"
238 (nth 0 date) (nth 2 date) (nth 1 date)
239 (nth 4 date) (nth 3 date)))
241 ;; this really needs to be better but I don't feel
242 ;; like writing a date to day converter.
243 (format "Sun %s %s %s %s"
244 (nth 1 date) (nth 0 date)
245 (nth 3 date) (nth 2 date)))
247 (setq From_ (format "\nFrom %s %s\n" from date))
248 (while (string-match "," From_)
249 (setq From_ (concat (substring From_ 0 (match-beginning 0))
250 (substring From_ (match-end 0)))))
251 (goto-char (point-min))
253 (if (search-forward "\n\n" nil t)
255 (goto-char (point-max))
257 (narrow-to-region (point) (point-max))
258 (let ((size (- (point-max) (point-min))))
259 (goto-char (point-min))
262 (insert (format "Content-Length: %s\n" size)))
267 ;; AUTHORIZATION STATE
269 (defun pop3-user (process user)
270 "Send USER information to POP3 server."
271 (pop3-send-command process (format "USER %s" user))
272 (let ((response (pop3-read-response process t)))
273 (if (not (and response (string-match "+OK" response)))
274 (error (format "USER %s not valid." user)))))
276 (defun pop3-pass (process)
277 "Send authentication information to the server."
278 (pop3-send-command process (format "PASS %s" pop3-password))
279 (let ((response (pop3-read-response process t)))
280 (if (not (and response (string-match "+OK" response)))
281 (pop3-quit process))))
283 (defun pop3-apop (process user)
284 "Send alternate authentication information to the server."
285 (let ((pass pop3-password))
286 (if (and pop3-password-required (not pass))
288 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
290 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
291 (pop3-send-command process (format "APOP %s %s" user hash))
292 (let ((response (pop3-read-response process t)))
293 (if (not (and response (string-match "+OK" response)))
294 (pop3-quit process)))))
301 (defalias 'pop3-md5 'md5)
302 (defvar pop3-md5-program "md5"
303 "*Program to encode its input in MD5.")
305 (defun pop3-md5 (string)
308 (call-process-region (point-min) (point-max)
310 t (current-buffer) nil)
311 ;; The meaningful output is the first 32 characters.
312 ;; Don't return the newline that follows them!
313 (buffer-substring 1 33)))))
315 (defun pop3-stat (process)
316 "Return the number of messages in the maildrop and the maildrop's size."
317 (pop3-send-command process "STAT")
318 (let ((response (pop3-read-response process t)))
319 (list (string-to-int (nth 1 (split-string response " ")))
320 (string-to-int (nth 2 (split-string response " "))))
323 (defun pop3-list (process &optional msg)
324 "Scan listing of available messages.
325 This function currently does nothing.")
327 (defun pop3-retr (process msg crashbuf)
328 "Retrieve message-id MSG to buffer CRASHBUF."
329 (pop3-send-command process (format "RETR %s" msg))
330 (pop3-read-response process)
331 (let ((start pop3-read-point) end)
333 (set-buffer (process-buffer process))
334 (while (not (re-search-forward "^\\.\r\n" nil t))
335 (accept-process-output process 3)
336 ;; bill@att.com ... to save wear and tear on the heap
337 ;; uncommented because the condensed version below is a problem for
339 (if (> (buffer-size) 20000) (sleep-for 1))
340 (if (> (buffer-size) 50000) (sleep-for 1))
341 (if (> (buffer-size) 100000) (sleep-for 1))
342 (if (> (buffer-size) 200000) (sleep-for 1))
343 (if (> (buffer-size) 500000) (sleep-for 1))
346 ;; (sometimes causes problems for really large messages.)
347 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
349 (setq pop3-read-point (point-marker))
350 ;; this code does not seem to work for some POP servers...
351 ;; and I cannot figure out why not.
352 ; (goto-char (match-beginning 0))
354 ; (if (not (looking-at "\r\n"))
356 ; (re-search-forward "\\.\r\n")
357 (goto-char (match-beginning 0))
358 (setq end (point-marker))
359 (pop3-clean-region start end)
360 (pop3-munge-message-separator start end)
362 (set-buffer crashbuf)
364 (copy-to-buffer crashbuf start end)
365 (delete-region start end)
368 (defun pop3-dele (process msg)
369 "Mark message-id MSG as deleted."
370 (pop3-send-command process (format "DELE %s" msg))
371 (pop3-read-response process))
373 (defun pop3-noop (process msg)
375 (pop3-send-command process "NOOP")
376 (pop3-read-response process))
378 (defun pop3-last (process)
379 "Return highest accessed message-id number for the session."
380 (pop3-send-command process "LAST")
381 (let ((response (pop3-read-response process t)))
382 (string-to-int (nth 1 (split-string response " ")))
385 (defun pop3-rset (process)
386 "Remove all delete marks from current maildrop."
387 (pop3-send-command process "RSET")
388 (pop3-read-response process))
392 (defun pop3-quit (process)
393 "Close connection to POP3 server.
394 Tell server to remove all messages marked as deleted, unlock the maildrop,
395 and close the connection."
396 (pop3-send-command process "QUIT")
397 (pop3-read-response process t)
400 (set-buffer (process-buffer process))
401 (goto-char (point-max))
402 (delete-process process))))
404 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
406 ;;; AUTHORIZATION STATE
408 ;; Initial TCP connection
410 ;; Restrictions: none
411 ;; Possible responses:
412 ;; +OK [POP3 server ready]
415 ;; Arguments: a server specific user-id (required)
416 ;; Restrictions: authorization state [after unsuccessful USER or PASS
417 ;; Possible responses:
418 ;; +OK [valid user-id]
419 ;; -ERR [invalid user-id]
422 ;; Arguments: a server/user-id specific password (required)
423 ;; Restrictions: authorization state, after successful USER
424 ;; Possible responses:
425 ;; +OK [maildrop locked and ready]
426 ;; -ERR [invalid password]
427 ;; -ERR [unable to lock maildrop]
429 ;;; TRANSACTION STATE
433 ;; Restrictions: transaction state
434 ;; Possible responses:
435 ;; +OK nn mm [# of messages, size of maildrop]
438 ;; Arguments: a message-id (optional)
439 ;; Restrictions: transaction state; msg must not be deleted
440 ;; Possible responses:
441 ;; +OK [scan listing follows]
442 ;; -ERR [no such message]
445 ;; Arguments: a message-id (required)
446 ;; Restrictions: transaction state; msg must not be deleted
447 ;; Possible responses:
448 ;; +OK [message contents follow]
449 ;; -ERR [no such message]
452 ;; Arguments: a message-id (required)
453 ;; Restrictions: transaction state; msg must not be deleted
454 ;; Possible responses:
455 ;; +OK [message deleted]
456 ;; -ERR [no such message]
460 ;; Restrictions: transaction state
461 ;; Possible responses:
466 ;; Restrictions: transaction state
467 ;; Possible responses:
468 ;; +OK nn [highest numbered message accessed]
472 ;; Restrictions: transaction state
473 ;; Possible responses:
474 ;; +OK [all delete marks removed]
480 ;; Restrictions: none
481 ;; Possible responses:
482 ;; +OK [TCP connection closed]
486 ;;; pop3.el ends here