Mention `woman'.
[bpt/emacs.git] / lisp / gnus / pop3.el
CommitLineData
eec82323
LMI
1;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
b0e40a7f 3;; Copyright (C) 1996, 96, 97, 98, 1999 Free Software Foundation, Inc.
eec82323
LMI
4
5;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
dd5da9b8
DL
6;; Maintainer: FSF
7;; Keywords: mail
8;; Version: 1.3s
eec82323
LMI
9
10;; This file is part of GNU Emacs.
11
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)
15;; any later version.
16
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.
21
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.
26
27;;; Commentary:
28
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.
33
34;; This program was inspired by Kyle E. Jones's vm-pop program.
35
36;;; Code:
37
38(require 'mail-utils)
eec82323 39
dd5da9b8 40(defconst pop3-version "1.3s")
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 63(defvar pop3-movemail-file-coding-system nil
a9c810bf 64 "Coding system for the crashbox made by `pop3-movemail'.")
a16dd35a 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))
dd5da9b8 89 (t (error "Invalid POP3 authentication scheme")))
eec82323 90 (setq message-count (car (pop3-stat process)))
dd5da9b8
DL
91 (unwind-protect
92 (while (<= n message-count)
93 (message (format "Retrieving message %d of %d from %s..."
94 n message-count pop3-mailhost))
95 (pop3-retr process n crashbuf)
96 (save-excursion
97 (set-buffer crashbuf)
a9c810bf
DL
98 (let ((coding-system-for-write pop3-movemail-file-coding-system))
99 (write-region (point-min) (point-max) crashbox t 'nomesg))
dd5da9b8
DL
100 (set-buffer (process-buffer process))
101 (while (> (buffer-size) 5000)
102 (goto-char (point-min))
103 (forward-line 50)
104 (delete-region (point-min) (point))))
105 (pop3-dele process n)
106 (setq n (+ 1 n))
107 (if pop3-debug (sit-for 1) (sit-for 0.1))
108 )
109 (pop3-quit process))
eec82323
LMI
110 (kill-buffer crashbuf)
111 )
dd5da9b8 112 t)
eec82323
LMI
113
114(defun pop3-open-server (mailhost port)
dd5da9b8 115 "Open TCP connection to MAILHOST on PORT.
eec82323
LMI
116Returns the process associated with the connection."
117 (let ((process-buffer
118 (get-buffer-create (format "trace of POP session to %s" mailhost)))
cebe85e9 119 (process)
6748645f
LMI
120 (coding-system-for-read 'binary)
121 (coding-system-for-write 'binary)
122 )
eec82323
LMI
123 (save-excursion
124 (set-buffer process-buffer)
6748645f
LMI
125 (erase-buffer)
126 (setq pop3-read-point (point-min))
127 )
eec82323
LMI
128 (setq process
129 (open-network-stream "POP" process-buffer mailhost port))
eec82323
LMI
130 (let ((response (pop3-read-response process t)))
131 (setq pop3-timestamp
132 (substring response (or (string-match "<" response) 0)
133 (+ 1 (or (string-match ">" response) -1)))))
134 process
135 ))
136
137;; Support functions
138
139(defun pop3-process-filter (process output)
140 (save-excursion
141 (set-buffer (process-buffer process))
142 (goto-char (point-max))
143 (insert output)))
144
145(defun pop3-send-command (process command)
146 (set-buffer (process-buffer process))
147 (goto-char (point-max))
148;; (if (= (aref command 0) ?P)
149;; (insert "PASS <omitted>\r\n")
150;; (insert command "\r\n"))
151 (setq pop3-read-point (point))
152 (goto-char (point-max))
dd5da9b8 153 (process-send-string process (concat command "\r\n"))
eec82323
LMI
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))
dd5da9b8
DL
255 (insert From_)
256 (re-search-forward "\n\n")
257 (narrow-to-region (point) (point-max))
258 (let ((size (- (point-max) (point-min))))
259 (goto-char (point-min))
260 (widen)
261 (forward-line -1)
262 (insert (format "Content-Length: %s\n" size)))
263 )))))
eec82323
LMI
264
265;; The Command Set
266
267;; AUTHORIZATION STATE
268
269(defun pop3-user (process user)
270 "Send USER information to POP3 server."
271 (pop3-send-command process (format "USER %s" user))
272 (let ((response (pop3-read-response process t)))
273 (if (not (and response (string-match "+OK" response)))
274 (error (format "USER %s not valid." user)))))
275
276(defun pop3-pass (process)
277 "Send authentication information to the server."
6748645f
LMI
278 (pop3-send-command process (format "PASS %s" pop3-password))
279 (let ((response (pop3-read-response process t)))
280 (if (not (and response (string-match "+OK" response)))
281 (pop3-quit process))))
282
283(defun pop3-apop (process user)
284 "Send alternate authentication information to the server."
eec82323
LMI
285 (let ((pass pop3-password))
286 (if (and pop3-password-required (not pass))
287 (setq pass
288 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
289 (if pass
6748645f
LMI
290 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
291 (pop3-send-command process (format "APOP %s %s" user hash))
eec82323
LMI
292 (let ((response (pop3-read-response process t)))
293 (if (not (and response (string-match "+OK" response)))
294 (pop3-quit process)))))
295 ))
296
6748645f
LMI
297;; TRANSACTION STATE
298
838bf473
RS
299(defvar pop3-md5-program "md5"
300 "*Program to encode its input in MD5.")
301
302(defun pop3-md5 (string)
303 (with-temp-buffer
304 (insert string)
305 (call-process-region (point-min) (point-max)
306 (or shell-file-name "/bin/sh")
307 t (current-buffer) nil
308 "-c" pop3-md5-program)
309 ;; The meaningful output is the first 32 characters.
310 ;; Don't return the newline that follows them!
311 (buffer-substring (point-min) (+ (point-min) 32))))
312
eec82323
LMI
313(defun pop3-stat (process)
314 "Return the number of messages in the maildrop and the maildrop's size."
315 (pop3-send-command process "STAT")
316 (let ((response (pop3-read-response process t)))
317 (list (string-to-int (nth 1 (pop3-string-to-list response)))
318 (string-to-int (nth 2 (pop3-string-to-list response))))
319 ))
320
321(defun pop3-list (process &optional msg)
322 "Scan listing of available messages.
323This function currently does nothing.")
324
325(defun pop3-retr (process msg crashbuf)
326 "Retrieve message-id MSG to buffer CRASHBUF."
327 (pop3-send-command process (format "RETR %s" msg))
328 (pop3-read-response process)
329 (let ((start pop3-read-point) end)
330 (save-excursion
331 (set-buffer (process-buffer process))
332 (while (not (re-search-forward "^\\.\r\n" nil t))
a8151ef7 333 (accept-process-output process 3)
eec82323 334 ;; bill@att.com ... to save wear and tear on the heap
6748645f
LMI
335 ;; uncommented because the condensed version below is a problem for
336 ;; some.
eec82323
LMI
337 (if (> (buffer-size) 20000) (sleep-for 1))
338 (if (> (buffer-size) 50000) (sleep-for 1))
339 (if (> (buffer-size) 100000) (sleep-for 1))
340 (if (> (buffer-size) 200000) (sleep-for 1))
341 (if (> (buffer-size) 500000) (sleep-for 1))
342 ;; bill@att.com
6748645f
LMI
343 ;; condensed into:
344 ;; (sometimes causes problems for really large messages.)
345; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
eec82323
LMI
346 (goto-char start))
347 (setq pop3-read-point (point-marker))
348;; this code does not seem to work for some POP servers...
349;; and I cannot figure out why not.
350; (goto-char (match-beginning 0))
351; (backward-char 2)
352; (if (not (looking-at "\r\n"))
353; (insert "\r\n"))
354; (re-search-forward "\\.\r\n")
355 (goto-char (match-beginning 0))
356 (setq end (point-marker))
357 (pop3-clean-region start end)
358 (pop3-munge-message-separator start end)
359 (save-excursion
360 (set-buffer crashbuf)
361 (erase-buffer))
362 (copy-to-buffer crashbuf start end)
363 (delete-region start end)
364 )))
365
366(defun pop3-dele (process msg)
367 "Mark message-id MSG as deleted."
368 (pop3-send-command process (format "DELE %s" msg))
369 (pop3-read-response process))
370
371(defun pop3-noop (process msg)
372 "No-operation."
373 (pop3-send-command process "NOOP")
374 (pop3-read-response process))
375
376(defun pop3-last (process)
377 "Return highest accessed message-id number for the session."
378 (pop3-send-command process "LAST")
379 (let ((response (pop3-read-response process t)))
380 (string-to-int (nth 1 (pop3-string-to-list response)))
381 ))
382
383(defun pop3-rset (process)
384 "Remove all delete marks from current maildrop."
385 (pop3-send-command process "RSET")
386 (pop3-read-response process))
387
388;; UPDATE
389
390(defun pop3-quit (process)
391 "Close connection to POP3 server.
392Tell server to remove all messages marked as deleted, unlock the maildrop,
393and close the connection."
394 (pop3-send-command process "QUIT")
395 (pop3-read-response process t)
396 (if process
397 (save-excursion
398 (set-buffer (process-buffer process))
399 (goto-char (point-max))
400 (delete-process process))))
401\f
402;; Summary of POP3 (Post Office Protocol version 3) commands and responses
403
404;;; AUTHORIZATION STATE
405
406;; Initial TCP connection
407;; Arguments: none
408;; Restrictions: none
409;; Possible responses:
410;; +OK [POP3 server ready]
411
412;; USER name
413;; Arguments: a server specific user-id (required)
414;; Restrictions: authorization state [after unsuccessful USER or PASS
415;; Possible responses:
416;; +OK [valid user-id]
417;; -ERR [invalid user-id]
418
419;; PASS string
420;; Arguments: a server/user-id specific password (required)
421;; Restrictions: authorization state, after successful USER
422;; Possible responses:
423;; +OK [maildrop locked and ready]
424;; -ERR [invalid password]
425;; -ERR [unable to lock maildrop]
426
427;;; TRANSACTION STATE
428
429;; STAT
430;; Arguments: none
431;; Restrictions: transaction state
432;; Possible responses:
433;; +OK nn mm [# of messages, size of maildrop]
434
435;; LIST [msg]
436;; Arguments: a message-id (optional)
437;; Restrictions: transaction state; msg must not be deleted
438;; Possible responses:
439;; +OK [scan listing follows]
440;; -ERR [no such message]
441
442;; RETR msg
443;; Arguments: a message-id (required)
444;; Restrictions: transaction state; msg must not be deleted
445;; Possible responses:
446;; +OK [message contents follow]
447;; -ERR [no such message]
448
449;; DELE msg
450;; Arguments: a message-id (required)
451;; Restrictions: transaction state; msg must not be deleted
452;; Possible responses:
453;; +OK [message deleted]
454;; -ERR [no such message]
455
456;; NOOP
457;; Arguments: none
458;; Restrictions: transaction state
459;; Possible responses:
460;; +OK
461
462;; LAST
463;; Arguments: none
464;; Restrictions: transaction state
465;; Possible responses:
466;; +OK nn [highest numbered message accessed]
467
468;; RSET
469;; Arguments: none
470;; Restrictions: transaction state
471;; Possible responses:
472;; +OK [all delete marks removed]
473
474;;; UPDATE STATE
475
476;; QUIT
477;; Arguments: none
478;; Restrictions: none
479;; Possible responses:
480;; +OK [TCP connection closed]
dd5da9b8
DL
481
482(provide 'pop3)
483
484;;; pop3.el ends here