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