Initial revision
[bpt/emacs.git] / lisp / gnus / pop3.el
CommitLineData
eec82323
LMI
1;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3;; Copyright (C) 1996, Free Software Foundation, Inc.
4
5;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6;; Keywords: mail, pop3
7;; Version: 1.3e
8
9;; This file is part of GNU Emacs.
10
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)
14;; any later version.
15
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.
20
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.
25
26;;; Commentary:
27
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.
32
33;; This program was inspired by Kyle E. Jones's vm-pop program.
34
35;;; Code:
36
37(require 'mail-utils)
38(provide 'pop3)
39
40(defconst pop3-version "1.3c")
41
42(defvar pop3-maildrop (or user-login-name (getenv "LOGNAME") (getenv "USER") nil)
43 "*POP3 maildrop.")
44(defvar pop3-mailhost (or (getenv "MAILHOST") nil)
45 "*POP3 mailhost.")
46(defvar pop3-port 110
47 "*POP3 port.")
48
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.")
53
54(defvar pop3-authentication-scheme 'pass
55 "*POP3 authentication scheme.
56Defaults to 'pass, for the standard USER/PASS authentication. Other valid
57values are 'apop.")
58
59(defvar pop3-timestamp nil
60 "Timestamp returned when initially connected to the POP server.
61Used for APOP authentication.")
62
63(defvar pop3-read-point nil)
64(defvar pop3-debug nil)
65
66(defun pop3-movemail (&optional crashbox)
67 "Transfer contents of a maildrop to the specified CRASHBOX."
68 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
69 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
70 (crashbuf (get-buffer-create " *pop3-retr*"))
71 (n 1)
72 message-count)
73 ;; for debugging only
74 (if pop3-debug (switch-to-buffer (process-buffer process)))
75 (cond ((equal 'apop pop3-authentication-scheme)
76 (pop3-apop process pop3-maildrop))
77 ((equal 'pass pop3-authentication-scheme)
78 (pop3-user process pop3-maildrop)
79 (pop3-pass process))
80 (t (error "Invalid POP3 authentication scheme.")))
81 (setq message-count (car (pop3-stat process)))
82 (while (<= n message-count)
83 (message (format "Retrieving message %d of %d from %s..."
84 n message-count pop3-mailhost))
85 (pop3-retr process n crashbuf)
86 (save-excursion
87 (set-buffer crashbuf)
88 (append-to-file (point-min) (point-max) crashbox)
89 (set-buffer (process-buffer process))
90 (while (> (buffer-size) 5000)
91 (goto-char (point-min))
92 (forward-line 50)
93 (delete-region (point-min) (point))))
94 (pop3-dele process n)
95 (setq n (+ 1 n))
96 (if pop3-debug (sit-for 1) (sit-for 0.1))
97 )
98 (pop3-quit process)
99 (kill-buffer crashbuf)
100 )
101 )
102
103(defun pop3-open-server (mailhost port)
104 "Open TCP connection to MAILHOST.
105Returns the process associated with the connection."
106 (let ((process-buffer
107 (get-buffer-create (format "trace of POP session to %s" mailhost)))
108 (process))
109 (save-excursion
110 (set-buffer process-buffer)
111 (erase-buffer))
112 (setq process
113 (open-network-stream "POP" process-buffer mailhost port))
114 (setq pop3-read-point (point-min))
115 (let ((response (pop3-read-response process t)))
116 (setq pop3-timestamp
117 (substring response (or (string-match "<" response) 0)
118 (+ 1 (or (string-match ">" response) -1)))))
119 process
120 ))
121
122;; Support functions
123
124(defun pop3-process-filter (process output)
125 (save-excursion
126 (set-buffer (process-buffer process))
127 (goto-char (point-max))
128 (insert output)))
129
130(defun pop3-send-command (process command)
131 (set-buffer (process-buffer process))
132 (goto-char (point-max))
133;; (if (= (aref command 0) ?P)
134;; (insert "PASS <omitted>\r\n")
135;; (insert command "\r\n"))
136 (setq pop3-read-point (point))
137 (goto-char (point-max))
138 (process-send-string process command)
139 (process-send-string process "\r\n")
140 )
141
142(defun pop3-read-response (process &optional return)
143 "Read the response from the server.
144Return the response string if optional second argument is non-nil."
145 (let ((case-fold-search nil)
146 match-end)
147 (save-excursion
148 (set-buffer (process-buffer process))
149 (goto-char pop3-read-point)
150 (while (not (search-forward "\r\n" nil t))
151 (accept-process-output process)
152 (goto-char pop3-read-point))
153 (setq match-end (point))
154 (goto-char pop3-read-point)
155 (if (looking-at "-ERR")
156 (error (buffer-substring (point) (- match-end 2)))
157 (if (not (looking-at "+OK"))
158 (progn (setq pop3-read-point match-end) nil)
159 (setq pop3-read-point match-end)
160 (if return
161 (buffer-substring (point) match-end)
162 t)
163 )))))
164
165(defun pop3-string-to-list (string &optional regexp)
166 "Chop up a string into a list."
167 (let ((list)
168 (regexp (or regexp " "))
169 (string (if (string-match "\r" string)
170 (substring string 0 (match-beginning 0))
171 string)))
172 (store-match-data nil)
173 (while string
174 (if (string-match regexp string)
175 (setq list (cons (substring string 0 (- (match-end 0) 1)) list)
176 string (substring string (match-end 0)))
177 (setq list (cons string list)
178 string nil)))
179 (nreverse list)))
180
181(defvar pop3-read-passwd nil)
182(defun pop3-read-passwd (prompt)
183 (if (not pop3-read-passwd)
184 (if (load "passwd" t)
185 (setq pop3-read-passwd 'read-passwd)
186 (autoload 'ange-ftp-read-passwd "ange-ftp")
187 (setq pop3-read-passwd 'ange-ftp-read-passwd)))
188 (funcall pop3-read-passwd prompt))
189
190(defun pop3-clean-region (start end)
191 (setq end (set-marker (make-marker) end))
192 (save-excursion
193 (goto-char start)
194 (while (and (< (point) end) (search-forward "\r\n" end t))
195 (replace-match "\n" t t))
196 (goto-char start)
197 (while (and (< (point) end) (re-search-forward "^\\." end t))
198 (replace-match "" t t)
199 (forward-char)))
200 (set-marker end nil))
201
202(defun pop3-munge-message-separator (start end)
203 "Check to see if a message separator exists. If not, generate one."
204 (save-excursion
205 (save-restriction
206 (narrow-to-region start end)
207 (goto-char (point-min))
208 (if (not (or (looking-at "From .?") ; Unix mail
209 (looking-at "\001\001\001\001\n") ; MMDF
210 (looking-at "BABYL OPTIONS:") ; Babyl
211 ))
212 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
213 (date (pop3-string-to-list (mail-fetch-field "Date")))
214 (From_))
215 ;; sample date formats I have seen
216 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
217 ;; Date: 08 Jul 1996 23:22:24 -0400
218 ;; should be
219 ;; Tue Jul 9 09:04:21 1996
220 (setq date
221 (cond ((string-match "[A-Z]" (nth 0 date))
222 (format "%s %s %s %s %s"
223 (nth 0 date) (nth 2 date) (nth 1 date)
224 (nth 4 date) (nth 3 date)))
225 (t
226 ;; this really needs to be better but I don't feel
227 ;; like writing a date to day converter.
228 (format "Sun %s %s %s %s"
229 (nth 1 date) (nth 0 date)
230 (nth 3 date) (nth 2 date)))
231 ))
232 (setq From_ (format "\nFrom %s %s\n" from date))
233 (while (string-match "," From_)
234 (setq From_ (concat (substring From_ 0 (match-beginning 0))
235 (substring From_ (match-end 0)))))
236 (goto-char (point-min))
237 (insert From_))))))
238
239;; The Command Set
240
241;; AUTHORIZATION STATE
242
243(defun pop3-user (process user)
244 "Send USER information to POP3 server."
245 (pop3-send-command process (format "USER %s" user))
246 (let ((response (pop3-read-response process t)))
247 (if (not (and response (string-match "+OK" response)))
248 (error (format "USER %s not valid." user)))))
249
250(defun pop3-pass (process)
251 "Send authentication information to the server."
252 (let ((pass pop3-password))
253 (if (and pop3-password-required (not pass))
254 (setq pass
255 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
256 (if pass
257 (progn
258 (pop3-send-command process (format "PASS %s" pass))
259 (let ((response (pop3-read-response process t)))
260 (if (not (and response (string-match "+OK" response)))
261 (pop3-quit process)))))
262 ))
263
264(defun pop3-apop (process user)
265 "Send alternate authentication information to the server."
266 (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
267 (let ((pass pop3-password))
268 (if (and pop3-password-required (not pass))
269 (setq pass
270 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
271 (if pass
272 (let ((hash (md5 (concat pop3-timestamp pass))))
273 (pop3-send-command process (format "APOP %s %s" user hash))
274 (let ((response (pop3-read-response process t)))
275 (if (not (and response (string-match "+OK" response)))
276 (pop3-quit process)))))
277 ))
278
279;; TRANSACTION STATE
280
281(defun pop3-stat (process)
282 "Return the number of messages in the maildrop and the maildrop's size."
283 (pop3-send-command process "STAT")
284 (let ((response (pop3-read-response process t)))
285 (list (string-to-int (nth 1 (pop3-string-to-list response)))
286 (string-to-int (nth 2 (pop3-string-to-list response))))
287 ))
288
289(defun pop3-list (process &optional msg)
290 "Scan listing of available messages.
291This function currently does nothing.")
292
293(defun pop3-retr (process msg crashbuf)
294 "Retrieve message-id MSG to buffer CRASHBUF."
295 (pop3-send-command process (format "RETR %s" msg))
296 (pop3-read-response process)
297 (let ((start pop3-read-point) end)
298 (save-excursion
299 (set-buffer (process-buffer process))
300 (while (not (re-search-forward "^\\.\r\n" nil t))
301 (accept-process-output process)
302 ;; bill@att.com ... to save wear and tear on the heap
303 (if (> (buffer-size) 20000) (sleep-for 1))
304 (if (> (buffer-size) 50000) (sleep-for 1))
305 (if (> (buffer-size) 100000) (sleep-for 1))
306 (if (> (buffer-size) 200000) (sleep-for 1))
307 (if (> (buffer-size) 500000) (sleep-for 1))
308 ;; bill@att.com
309 (goto-char start))
310 (setq pop3-read-point (point-marker))
311;; this code does not seem to work for some POP servers...
312;; and I cannot figure out why not.
313; (goto-char (match-beginning 0))
314; (backward-char 2)
315; (if (not (looking-at "\r\n"))
316; (insert "\r\n"))
317; (re-search-forward "\\.\r\n")
318 (goto-char (match-beginning 0))
319 (setq end (point-marker))
320 (pop3-clean-region start end)
321 (pop3-munge-message-separator start end)
322 (save-excursion
323 (set-buffer crashbuf)
324 (erase-buffer))
325 (copy-to-buffer crashbuf start end)
326 (delete-region start end)
327 )))
328
329(defun pop3-dele (process msg)
330 "Mark message-id MSG as deleted."
331 (pop3-send-command process (format "DELE %s" msg))
332 (pop3-read-response process))
333
334(defun pop3-noop (process msg)
335 "No-operation."
336 (pop3-send-command process "NOOP")
337 (pop3-read-response process))
338
339(defun pop3-last (process)
340 "Return highest accessed message-id number for the session."
341 (pop3-send-command process "LAST")
342 (let ((response (pop3-read-response process t)))
343 (string-to-int (nth 1 (pop3-string-to-list response)))
344 ))
345
346(defun pop3-rset (process)
347 "Remove all delete marks from current maildrop."
348 (pop3-send-command process "RSET")
349 (pop3-read-response process))
350
351;; UPDATE
352
353(defun pop3-quit (process)
354 "Close connection to POP3 server.
355Tell server to remove all messages marked as deleted, unlock the maildrop,
356and close the connection."
357 (pop3-send-command process "QUIT")
358 (pop3-read-response process t)
359 (if process
360 (save-excursion
361 (set-buffer (process-buffer process))
362 (goto-char (point-max))
363 (delete-process process))))
364\f
365;; Summary of POP3 (Post Office Protocol version 3) commands and responses
366
367;;; AUTHORIZATION STATE
368
369;; Initial TCP connection
370;; Arguments: none
371;; Restrictions: none
372;; Possible responses:
373;; +OK [POP3 server ready]
374
375;; USER name
376;; Arguments: a server specific user-id (required)
377;; Restrictions: authorization state [after unsuccessful USER or PASS
378;; Possible responses:
379;; +OK [valid user-id]
380;; -ERR [invalid user-id]
381
382;; PASS string
383;; Arguments: a server/user-id specific password (required)
384;; Restrictions: authorization state, after successful USER
385;; Possible responses:
386;; +OK [maildrop locked and ready]
387;; -ERR [invalid password]
388;; -ERR [unable to lock maildrop]
389
390;;; TRANSACTION STATE
391
392;; STAT
393;; Arguments: none
394;; Restrictions: transaction state
395;; Possible responses:
396;; +OK nn mm [# of messages, size of maildrop]
397
398;; LIST [msg]
399;; Arguments: a message-id (optional)
400;; Restrictions: transaction state; msg must not be deleted
401;; Possible responses:
402;; +OK [scan listing follows]
403;; -ERR [no such message]
404
405;; RETR msg
406;; Arguments: a message-id (required)
407;; Restrictions: transaction state; msg must not be deleted
408;; Possible responses:
409;; +OK [message contents follow]
410;; -ERR [no such message]
411
412;; DELE msg
413;; Arguments: a message-id (required)
414;; Restrictions: transaction state; msg must not be deleted
415;; Possible responses:
416;; +OK [message deleted]
417;; -ERR [no such message]
418
419;; NOOP
420;; Arguments: none
421;; Restrictions: transaction state
422;; Possible responses:
423;; +OK
424
425;; LAST
426;; Arguments: none
427;; Restrictions: transaction state
428;; Possible responses:
429;; +OK nn [highest numbered message accessed]
430
431;; RSET
432;; Arguments: none
433;; Restrictions: transaction state
434;; Possible responses:
435;; +OK [all delete marks removed]
436
437;;; UPDATE STATE
438
439;; QUIT
440;; Arguments: none
441;; Restrictions: none
442;; Possible responses:
443;; +OK [TCP connection closed]