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