1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (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 "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-get-message-count ()
110 "Return the number of messages in the maildrop."
111 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
113 (pop3-password pop3-password)
115 ;; for debugging only
116 (if pop3-debug (switch-to-buffer (process-buffer process)))
117 ;; query for password
118 (if (and pop3-password-required (not pop3-password))
120 (read-passwd (format "Password for %s: " pop3-maildrop))))
121 (cond ((equal 'apop pop3-authentication-scheme)
122 (pop3-apop process pop3-maildrop))
123 ((equal 'pass pop3-authentication-scheme)
124 (pop3-user process pop3-maildrop)
126 (t (error "Invalid POP3 authentication scheme")))
127 (setq message-count (car (pop3-stat process)))
131 (defun pop3-open-server (mailhost port)
132 "Open TCP connection to MAILHOST on PORT.
133 Returns the process associated with the connection."
134 (let ((coding-system-for-read 'binary)
135 (coding-system-for-write 'binary)
138 (set-buffer (get-buffer-create (concat " trace of POP session to "
141 (setq pop3-read-point (point-min))
142 (setq process (open-network-stream "POP" (current-buffer) mailhost port))
143 (let ((response (pop3-read-response process t)))
145 (substring response (or (string-match "<" response) 0)
146 (+ 1 (or (string-match ">" response) -1)))))
151 (defun pop3-process-filter (process output)
153 (set-buffer (process-buffer process))
154 (goto-char (point-max))
157 (defun pop3-send-command (process command)
158 (set-buffer (process-buffer process))
159 (goto-char (point-max))
160 ;; (if (= (aref command 0) ?P)
161 ;; (insert "PASS <omitted>\r\n")
162 ;; (insert command "\r\n"))
163 (setq pop3-read-point (point))
164 (goto-char (point-max))
165 (process-send-string process (concat command "\r\n"))
168 (defun pop3-read-response (process &optional return)
169 "Read the response from the server.
170 Return the response string if optional second argument is non-nil."
171 (let ((case-fold-search nil)
174 (set-buffer (process-buffer process))
175 (goto-char pop3-read-point)
176 (while (not (search-forward "\r\n" nil t))
177 (nnheader-accept-process-output process)
178 (goto-char pop3-read-point))
179 (setq match-end (point))
180 (goto-char pop3-read-point)
181 (if (looking-at "-ERR")
182 (error (buffer-substring (point) (- match-end 2)))
183 (if (not (looking-at "+OK"))
184 (progn (setq pop3-read-point match-end) nil)
185 (setq pop3-read-point match-end)
187 (buffer-substring (point) match-end)
191 (defun pop3-clean-region (start end)
192 (setq end (set-marker (make-marker) end))
195 (while (and (< (point) end) (search-forward "\r\n" end t))
196 (replace-match "\n" t t))
198 (while (and (< (point) end) (re-search-forward "^\\." end t))
199 (replace-match "" t t)
201 (set-marker end nil))
203 (eval-when-compile (defvar parse-time-months))
205 ;; Copied from message-make-date.
206 (defun pop3-make-date (&optional now)
207 "Make a valid date header.
208 If NOW, use that time instead."
209 (require 'parse-time)
210 (let* ((now (or now (current-time)))
211 (zone (nth 8 (decode-time now)))
215 (setq zone (- zone)))
217 (format-time-string "%d" now)
218 ;; The month name of the %b spec is locale-specific. Pfff.
220 (capitalize (car (rassoc (nth 4 (decode-time now))
221 parse-time-months))))
222 (format-time-string "%Y %H:%M:%S " now)
223 ;; We do all of this because XEmacs doesn't have the %z spec.
224 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
226 (defun pop3-munge-message-separator (start end)
227 "Check to see if a message separator exists. If not, generate one."
230 (narrow-to-region start end)
231 (goto-char (point-min))
232 (if (not (or (looking-at "From .?") ; Unix mail
233 (looking-at "\001\001\001\001\n") ; MMDF
234 (looking-at "BABYL OPTIONS:") ; Babyl
236 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
237 (tdate (mail-fetch-field "Date"))
238 (date (split-string (or (and tdate
239 (not (string= "" tdate))
244 ;; sample date formats I have seen
245 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
246 ;; Date: 08 Jul 1996 23:22:24 -0400
248 ;; Tue Jul 9 09:04:21 1996
251 "Tue Jan 1 00:00:0 1900")
252 ((string-match "[A-Z]" (nth 0 date))
253 (format "%s %s %s %s %s"
254 (nth 0 date) (nth 2 date) (nth 1 date)
255 (nth 4 date) (nth 3 date)))
257 ;; this really needs to be better but I don't feel
258 ;; like writing a date to day converter.
259 (format "Sun %s %s %s %s"
260 (nth 1 date) (nth 0 date)
261 (nth 3 date) (nth 2 date)))
263 (setq From_ (format "\nFrom %s %s\n" from date))
264 (while (string-match "," From_)
265 (setq From_ (concat (substring From_ 0 (match-beginning 0))
266 (substring From_ (match-end 0)))))
267 (goto-char (point-min))
269 (if (search-forward "\n\n" nil t)
271 (goto-char (point-max))
273 (narrow-to-region (point) (point-max))
274 (let ((size (- (point-max) (point-min))))
275 (goto-char (point-min))
278 (insert (format "Content-Length: %s\n" size)))
283 ;; AUTHORIZATION STATE
285 (defun pop3-user (process user)
286 "Send USER information to POP3 server."
287 (pop3-send-command process (format "USER %s" user))
288 (let ((response (pop3-read-response process t)))
289 (if (not (and response (string-match "+OK" response)))
290 (error (format "USER %s not valid" user)))))
292 (defun pop3-pass (process)
293 "Send authentication information to the server."
294 (pop3-send-command process (format "PASS %s" pop3-password))
295 (let ((response (pop3-read-response process t)))
296 (if (not (and response (string-match "+OK" response)))
297 (pop3-quit process))))
299 (defun pop3-apop (process user)
300 "Send alternate authentication information to the server."
301 (let ((pass pop3-password))
302 (if (and pop3-password-required (not pass))
304 (read-passwd (format "Password for %s: " pop3-maildrop))))
306 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
307 (pop3-send-command process (format "APOP %s %s" user hash))
308 (let ((response (pop3-read-response process t)))
309 (if (not (and response (string-match "+OK" response)))
310 (pop3-quit process)))))
317 (defalias 'pop3-md5 'md5)
318 (defvar pop3-md5-program "md5"
319 "*Program to encode its input in MD5.")
321 (defun pop3-md5 (string)
324 (call-process-region (point-min) (point-max)
326 t (current-buffer) nil)
327 ;; The meaningful output is the first 32 characters.
328 ;; Don't return the newline that follows them!
329 (buffer-substring (point-min) (+ 32 (point-min)))))))
331 (defun pop3-stat (process)
332 "Return the number of messages in the maildrop and the maildrop's size."
333 (pop3-send-command process "STAT")
334 (let ((response (pop3-read-response process t)))
335 (list (string-to-int (nth 1 (split-string response " ")))
336 (string-to-int (nth 2 (split-string response " "))))
339 (defun pop3-list (process &optional msg)
340 "Scan listing of available messages.
341 This function currently does nothing.")
343 (defun pop3-retr (process msg crashbuf)
344 "Retrieve message-id MSG to buffer CRASHBUF."
345 (pop3-send-command process (format "RETR %s" msg))
346 (pop3-read-response process)
347 (let ((start pop3-read-point) end)
349 (set-buffer (process-buffer process))
350 (while (not (re-search-forward "^\\.\r\n" nil t))
351 ;; Fixme: Shouldn't depend on nnheader.
352 (nnheader-accept-process-output process)
353 ;; bill@att.com ... to save wear and tear on the heap
354 ;; uncommented because the condensed version below is a problem for
356 (if (> (buffer-size) 20000) (sleep-for 1))
357 (if (> (buffer-size) 50000) (sleep-for 1))
358 (if (> (buffer-size) 100000) (sleep-for 1))
359 (if (> (buffer-size) 200000) (sleep-for 1))
360 (if (> (buffer-size) 500000) (sleep-for 1))
363 ;; (sometimes causes problems for really large messages.)
364 ; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
366 (setq pop3-read-point (point-marker))
367 ;; this code does not seem to work for some POP servers...
368 ;; and I cannot figure out why not.
369 ; (goto-char (match-beginning 0))
371 ; (if (not (looking-at "\r\n"))
373 ; (re-search-forward "\\.\r\n")
374 (goto-char (match-beginning 0))
375 (setq end (point-marker))
376 (pop3-clean-region start end)
377 (pop3-munge-message-separator start end)
379 (set-buffer crashbuf)
381 (copy-to-buffer crashbuf start end)
382 (delete-region start end)
385 (defun pop3-dele (process msg)
386 "Mark message-id MSG as deleted."
387 (pop3-send-command process (format "DELE %s" msg))
388 (pop3-read-response process))
390 (defun pop3-noop (process msg)
392 (pop3-send-command process "NOOP")
393 (pop3-read-response process))
395 (defun pop3-last (process)
396 "Return highest accessed message-id number for the session."
397 (pop3-send-command process "LAST")
398 (let ((response (pop3-read-response process t)))
399 (string-to-int (nth 1 (split-string response " ")))
402 (defun pop3-rset (process)
403 "Remove all delete marks from current maildrop."
404 (pop3-send-command process "RSET")
405 (pop3-read-response process))
409 (defun pop3-quit (process)
410 "Close connection to POP3 server.
411 Tell server to remove all messages marked as deleted, unlock the maildrop,
412 and close the connection."
413 (pop3-send-command process "QUIT")
414 (pop3-read-response process t)
417 (set-buffer (process-buffer process))
418 (goto-char (point-max))
419 (delete-process process))))
421 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
423 ;;; AUTHORIZATION STATE
425 ;; Initial TCP connection
427 ;; Restrictions: none
428 ;; Possible responses:
429 ;; +OK [POP3 server ready]
432 ;; Arguments: a server specific user-id (required)
433 ;; Restrictions: authorization state [after unsuccessful USER or PASS
434 ;; Possible responses:
435 ;; +OK [valid user-id]
436 ;; -ERR [invalid user-id]
439 ;; Arguments: a server/user-id specific password (required)
440 ;; Restrictions: authorization state, after successful USER
441 ;; Possible responses:
442 ;; +OK [maildrop locked and ready]
443 ;; -ERR [invalid password]
444 ;; -ERR [unable to lock maildrop]
446 ;;; TRANSACTION STATE
450 ;; Restrictions: transaction state
451 ;; Possible responses:
452 ;; +OK nn mm [# of messages, size of maildrop]
455 ;; Arguments: a message-id (optional)
456 ;; Restrictions: transaction state; msg must not be deleted
457 ;; Possible responses:
458 ;; +OK [scan listing follows]
459 ;; -ERR [no such message]
462 ;; Arguments: a message-id (required)
463 ;; Restrictions: transaction state; msg must not be deleted
464 ;; Possible responses:
465 ;; +OK [message contents follow]
466 ;; -ERR [no such message]
469 ;; Arguments: a message-id (required)
470 ;; Restrictions: transaction state; msg must not be deleted
471 ;; Possible responses:
472 ;; +OK [message deleted]
473 ;; -ERR [no such message]
477 ;; Restrictions: transaction state
478 ;; Possible responses:
483 ;; Restrictions: transaction state
484 ;; Possible responses:
485 ;; +OK nn [highest numbered message accessed]
489 ;; Restrictions: transaction state
490 ;; Possible responses:
491 ;; +OK [all delete marks removed]
497 ;; Restrictions: none
498 ;; Possible responses:
499 ;; +OK [TCP connection closed]
503 ;;; pop3.el ends here