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