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-movemail-file-coding-system nil
62 "Coding system for the crashbox made by `pop3-movemail'.")
64 (defvar pop3-read-point nil
)
65 (defvar pop3-debug nil
)
67 (defun pop3-movemail (&optional crashbox
)
68 "Transfer contents of a maildrop to the specified CRASHBOX."
69 (or crashbox
(setq crashbox
(expand-file-name "~/.crashbox")))
70 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
71 (crashbuf (get-buffer-create " *pop3-retr*"))
74 (pop3-password pop3-password
)
77 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
79 (if (and pop3-password-required
(not pop3-password
))
81 (pop3-read-passwd (format "Password for %s: " pop3-maildrop
))))
82 (cond ((equal 'apop pop3-authentication-scheme
)
83 (pop3-apop process pop3-maildrop
))
84 ((equal 'pass pop3-authentication-scheme
)
85 (pop3-user process pop3-maildrop
)
87 (t (error "Invalid POP3 authentication scheme")))
88 (setq message-count
(car (pop3-stat process
)))
90 (while (<= n message-count
)
91 (message (format "Retrieving message %d of %d from %s..."
92 n message-count pop3-mailhost
))
93 (pop3-retr process n crashbuf
)
96 (let ((coding-system-for-write pop3-movemail-file-coding-system
))
97 (write-region (point-min) (point-max) crashbox t
'nomesg
))
98 (set-buffer (process-buffer process
))
99 (while (> (buffer-size) 5000)
100 (goto-char (point-min))
102 (delete-region (point-min) (point))))
103 (pop3-dele process n
)
105 (if pop3-debug
(sit-for 1) (sit-for 0.1))
108 (kill-buffer crashbuf
)
112 (defun pop3-open-server (mailhost port
)
113 "Open TCP connection to MAILHOST on PORT.
114 Returns the process associated with the connection."
115 (let ((coding-system-for-read 'binary
)
116 (coding-system-for-write 'binary
)
119 (set-buffer (get-buffer-create (concat " trace of POP session to "
122 (setq pop3-read-point
(point-min))
123 (setq process
(open-network-stream "POP"(current-buffer) mailhost port
))
124 (let ((response (pop3-read-response process t
)))
126 (substring response
(or (string-match "<" response
) 0)
127 (+ 1 (or (string-match ">" response
) -
1)))))
132 (defun pop3-process-filter (process output
)
134 (set-buffer (process-buffer process
))
135 (goto-char (point-max))
138 (defun pop3-send-command (process command
)
139 (set-buffer (process-buffer process
))
140 (goto-char (point-max))
141 ;; (if (= (aref command 0) ?P)
142 ;; (insert "PASS <omitted>\r\n")
143 ;; (insert command "\r\n"))
144 (setq pop3-read-point
(point))
145 (goto-char (point-max))
146 (process-send-string process
(concat command
"\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 (defvar pop3-read-passwd nil
)
173 (defun pop3-read-passwd (prompt)
174 (if (not pop3-read-passwd
)
175 (if (fboundp 'read-passwd
)
176 (setq pop3-read-passwd
'read-passwd
)
177 (if (load "passwd" t
)
178 (setq pop3-read-passwd
'read-passwd
)
179 (autoload 'ange-ftp-read-passwd
"ange-ftp")
180 (setq pop3-read-passwd
'ange-ftp-read-passwd
))))
181 (funcall pop3-read-passwd prompt
))
183 (defun pop3-clean-region (start end
)
184 (setq end
(set-marker (make-marker) end
))
187 (while (and (< (point) end
) (search-forward "\r\n" end t
))
188 (replace-match "\n" t t
))
190 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
191 (replace-match "" t t
)
193 (set-marker end nil
))
195 (eval-when-compile (defvar parse-time-months
))
197 ;; Copied from message-make-date.
198 (defun pop3-make-date (&optional now
)
199 "Make a valid date header.
200 If NOW, use that time instead."
201 (require 'parse-time
)
202 (let* ((now (or now
(current-time)))
203 (zone (nth 8 (decode-time now
)))
207 (setq zone
(- zone
)))
209 (format-time-string "%d" now
)
210 ;; The month name of the %b spec is locale-specific. Pfff.
212 (capitalize (car (rassoc (nth 4 (decode-time now
))
213 parse-time-months
))))
214 (format-time-string "%Y %H:%M:%S " now
)
215 ;; We do all of this because XEmacs doesn't have the %z spec.
216 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
218 (defun pop3-munge-message-separator (start end
)
219 "Check to see if a message separator exists. If not, generate one."
222 (narrow-to-region start end
)
223 (goto-char (point-min))
224 (if (not (or (looking-at "From .?") ; Unix mail
225 (looking-at "\001\001\001\001\n") ; MMDF
226 (looking-at "BABYL OPTIONS:") ; Babyl
228 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
229 (date (split-string (or (mail-fetch-field "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))
256 (if (search-forward "\n\n" nil t
)
258 (goto-char (point-max))
260 (narrow-to-region (point) (point-max))
261 (let ((size (- (point-max) (point-min))))
262 (goto-char (point-min))
265 (insert (format "Content-Length: %s\n" size
)))
270 ;; AUTHORIZATION STATE
272 (defun pop3-user (process user
)
273 "Send USER information to POP3 server."
274 (pop3-send-command process
(format "USER %s" user
))
275 (let ((response (pop3-read-response process t
)))
276 (if (not (and response
(string-match "+OK" response
)))
277 (error (format "USER %s not valid." user
)))))
279 (defun pop3-pass (process)
280 "Send authentication information to the server."
281 (pop3-send-command process
(format "PASS %s" pop3-password
))
282 (let ((response (pop3-read-response process t
)))
283 (if (not (and response
(string-match "+OK" response
)))
284 (pop3-quit process
))))
286 (defun pop3-apop (process user
)
287 "Send alternate authentication information to the server."
288 (let ((pass pop3-password
))
289 (if (and pop3-password-required
(not pass
))
291 (pop3-read-passwd (format "Password for %s: " pop3-maildrop
))))
293 (let ((hash (pop3-md5 (concat pop3-timestamp pass
))))
294 (pop3-send-command process
(format "APOP %s %s" user hash
))
295 (let ((response (pop3-read-response process t
)))
296 (if (not (and response
(string-match "+OK" response
)))
297 (pop3-quit process
)))))
304 (defalias 'pop3-md5
'md5
)
305 (defvar pop3-md5-program
"md5"
306 "*Program to encode its input in MD5.")
308 (defun pop3-md5 (string)
311 (call-process-region (point-min) (point-max)
313 t
(current-buffer) nil
)
314 ;; The meaningful output is the first 32 characters.
315 ;; Don't return the newline that follows them!
316 (buffer-substring 1 33)))))
318 (defun pop3-stat (process)
319 "Return the number of messages in the maildrop and the maildrop's size."
320 (pop3-send-command process
"STAT")
321 (let ((response (pop3-read-response process t
)))
322 (list (string-to-int (nth 1 (split-string response
" ")))
323 (string-to-int (nth 2 (split-string response
" "))))
326 (defun pop3-list (process &optional msg
)
327 "Scan listing of available messages.
328 This function currently does nothing.")
330 (defun pop3-retr (process msg crashbuf
)
331 "Retrieve message-id MSG to buffer CRASHBUF."
332 (pop3-send-command process
(format "RETR %s" msg
))
333 (pop3-read-response process
)
334 (let ((start pop3-read-point
) end
)
336 (set-buffer (process-buffer process
))
337 (while (not (re-search-forward "^\\.\r\n" nil t
))
338 (accept-process-output process
3)
339 ;; bill@att.com ... to save wear and tear on the heap
340 ;; uncommented because the condensed version below is a problem for
342 (if (> (buffer-size) 20000) (sleep-for 1))
343 (if (> (buffer-size) 50000) (sleep-for 1))
344 (if (> (buffer-size) 100000) (sleep-for 1))
345 (if (> (buffer-size) 200000) (sleep-for 1))
346 (if (> (buffer-size) 500000) (sleep-for 1))
349 ;; (sometimes causes problems for really large messages.)
350 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
352 (setq pop3-read-point
(point-marker))
353 ;; this code does not seem to work for some POP servers...
354 ;; and I cannot figure out why not.
355 ; (goto-char (match-beginning 0))
357 ; (if (not (looking-at "\r\n"))
359 ; (re-search-forward "\\.\r\n")
360 (goto-char (match-beginning 0))
361 (setq end
(point-marker))
362 (pop3-clean-region start end
)
363 (pop3-munge-message-separator start end
)
365 (set-buffer crashbuf
)
367 (copy-to-buffer crashbuf start end
)
368 (delete-region start end
)
371 (defun pop3-dele (process msg
)
372 "Mark message-id MSG as deleted."
373 (pop3-send-command process
(format "DELE %s" msg
))
374 (pop3-read-response process
))
376 (defun pop3-noop (process msg
)
378 (pop3-send-command process
"NOOP")
379 (pop3-read-response process
))
381 (defun pop3-last (process)
382 "Return highest accessed message-id number for the session."
383 (pop3-send-command process
"LAST")
384 (let ((response (pop3-read-response process t
)))
385 (string-to-int (nth 1 (split-string response
" ")))
388 (defun pop3-rset (process)
389 "Remove all delete marks from current maildrop."
390 (pop3-send-command process
"RSET")
391 (pop3-read-response process
))
395 (defun pop3-quit (process)
396 "Close connection to POP3 server.
397 Tell server to remove all messages marked as deleted, unlock the maildrop,
398 and close the connection."
399 (pop3-send-command process
"QUIT")
400 (pop3-read-response process t
)
403 (set-buffer (process-buffer process
))
404 (goto-char (point-max))
405 (delete-process process
))))
407 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
409 ;;; AUTHORIZATION STATE
411 ;; Initial TCP connection
413 ;; Restrictions: none
414 ;; Possible responses:
415 ;; +OK [POP3 server ready]
418 ;; Arguments: a server specific user-id (required)
419 ;; Restrictions: authorization state [after unsuccessful USER or PASS
420 ;; Possible responses:
421 ;; +OK [valid user-id]
422 ;; -ERR [invalid user-id]
425 ;; Arguments: a server/user-id specific password (required)
426 ;; Restrictions: authorization state, after successful USER
427 ;; Possible responses:
428 ;; +OK [maildrop locked and ready]
429 ;; -ERR [invalid password]
430 ;; -ERR [unable to lock maildrop]
432 ;;; TRANSACTION STATE
436 ;; Restrictions: transaction state
437 ;; Possible responses:
438 ;; +OK nn mm [# of messages, size of maildrop]
441 ;; Arguments: a message-id (optional)
442 ;; Restrictions: transaction state; msg must not be deleted
443 ;; Possible responses:
444 ;; +OK [scan listing follows]
445 ;; -ERR [no such message]
448 ;; Arguments: a message-id (required)
449 ;; Restrictions: transaction state; msg must not be deleted
450 ;; Possible responses:
451 ;; +OK [message contents follow]
452 ;; -ERR [no such message]
455 ;; Arguments: a message-id (required)
456 ;; Restrictions: transaction state; msg must not be deleted
457 ;; Possible responses:
458 ;; +OK [message deleted]
459 ;; -ERR [no such message]
463 ;; Restrictions: transaction state
464 ;; Possible responses:
469 ;; Restrictions: transaction state
470 ;; Possible responses:
471 ;; +OK nn [highest numbered message accessed]
475 ;; Restrictions: transaction state
476 ;; Possible responses:
477 ;; +OK [all delete marks removed]
483 ;; Restrictions: none
484 ;; Possible responses:
485 ;; +OK [TCP connection closed]
489 ;;; pop3.el ends here