(ido-file-extensions-order): New defcustom.
[bpt/emacs.git] / lisp / gnus / pop3.el
CommitLineData
eec82323
LMI
1;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
23f87bed 3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
16409b0b 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
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
e62e7654
MB
40(defgroup pop3 nil
41 "Post Office Protocol"
42 :group 'mail
43 :group 'mail-source)
44
45(defcustom pop3-maildrop (or (user-login-name)
46 (getenv "LOGNAME")
47 (getenv "USER"))
48 "*POP3 maildrop."
49 :version "21.4" ;; Oort Gnus
50 :type 'string
51 :group 'pop3)
52
53(defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
54 "pop3")
55 "*POP3 mailhost."
56 :version "21.4" ;; Oort Gnus
57 :type 'string
58 :group 'pop3)
59
60(defcustom pop3-port 110
61 "*POP3 port."
62 :version "21.4" ;; Oort Gnus
63 :type 'number
64 :group 'pop3)
65
66(defcustom pop3-password-required t
67 "*Non-nil if a password is required when connecting to POP server."
68 :version "21.4" ;; Oort Gnus
69 :type 'boolean
70 :group 'pop3)
71
72;; Should this be customizable?
eec82323
LMI
73(defvar pop3-password nil
74 "*Password to use when connecting to POP server.")
75
e62e7654 76(defcustom pop3-authentication-scheme 'pass
eec82323
LMI
77 "*POP3 authentication scheme.
78Defaults to 'pass, for the standard USER/PASS authentication. Other valid
e62e7654
MB
79values are 'apop."
80 :version "21.4" ;; Oort Gnus
81 :type '(choice (const :tag "USER/PASS" pass)
82 (const :tag "APOP" apop))
83 :group 'pop3)
84
85(defcustom pop3-leave-mail-on-server nil
8903a9c8
MB
86 "*Non-nil if the mail is to be left on the POP server after fetching.
87
88If the `pop3-leave-mail-on-server' is non-`nil' the mail is to be
89left on the POP server after fetching. Note that POP servers
90maintain no state information between sessions, so what the
91client believes is there and what is actually there may not match
92up. If they do not, then the whole thing can fall apart and
93leave you with a corrupt mailbox."
e62e7654
MB
94 :version "21.4" ;; Oort Gnus
95 :type 'boolean
96 :group 'pop3)
3e7b210c 97
eec82323
LMI
98(defvar pop3-timestamp nil
99 "Timestamp returned when initially connected to the POP server.
100Used for APOP authentication.")
101
102(defvar pop3-read-point nil)
103(defvar pop3-debug nil)
104
8903a9c8
MB
105;; Borrowed from nnheader-accept-process-output in nnheader.el.
106(defvar pop3-read-timeout
107 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
108 (symbol-name system-type))
109 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
110 ;;
111 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
112 ;;
113 ;; There should probably be a runtime test to determine the timing
114 ;; resolution, or a primitive to report it. I don't know off-hand
115 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
116 ;; could round up non-zero timeouts to a minimum of 1.0?
117 1.0
118 0.1)
119 "How long pop3 should wait between checking for the end of output.
120Shorter values mean quicker response, but are more CPU intensive.")
121
122;; Borrowed from nnheader-accept-process-output in nnheader.el.
123(defun pop3-accept-process-output (process)
124 (accept-process-output
125 process
126 (truncate pop3-read-timeout)
127 (truncate (* (- pop3-read-timeout
128 (truncate pop3-read-timeout))
129 1000))))
130
eec82323
LMI
131(defun pop3-movemail (&optional crashbox)
132 "Transfer contents of a maildrop to the specified CRASHBOX."
133 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
134 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
135 (crashbuf (get-buffer-create " *pop3-retr*"))
136 (n 1)
6748645f 137 message-count
e62e7654 138 (pop3-password pop3-password))
eec82323
LMI
139 ;; for debugging only
140 (if pop3-debug (switch-to-buffer (process-buffer process)))
6748645f
LMI
141 ;; query for password
142 (if (and pop3-password-required (not pop3-password))
143 (setq pop3-password
23f87bed 144 (read-passwd (format "Password for %s: " pop3-maildrop))))
eec82323
LMI
145 (cond ((equal 'apop pop3-authentication-scheme)
146 (pop3-apop process pop3-maildrop))
147 ((equal 'pass pop3-authentication-scheme)
148 (pop3-user process pop3-maildrop)
149 (pop3-pass process))
dd5da9b8 150 (t (error "Invalid POP3 authentication scheme")))
eec82323 151 (setq message-count (car (pop3-stat process)))
dd5da9b8
DL
152 (unwind-protect
153 (while (<= n message-count)
23f87bed
MB
154 (message "Retrieving message %d of %d from %s..."
155 n message-count pop3-mailhost)
dd5da9b8
DL
156 (pop3-retr process n crashbuf)
157 (save-excursion
158 (set-buffer crashbuf)
4c4b227a 159 (let ((coding-system-for-write 'binary))
a9c810bf 160 (write-region (point-min) (point-max) crashbox t 'nomesg))
dd5da9b8
DL
161 (set-buffer (process-buffer process))
162 (while (> (buffer-size) 5000)
163 (goto-char (point-min))
164 (forward-line 50)
165 (delete-region (point-min) (point))))
3e7b210c
SS
166 (unless pop3-leave-mail-on-server
167 (pop3-dele process n))
dd5da9b8
DL
168 (setq n (+ 1 n))
169 (if pop3-debug (sit-for 1) (sit-for 0.1))
170 )
171 (pop3-quit process))
eec82323
LMI
172 (kill-buffer crashbuf)
173 )
dd5da9b8 174 t)
eec82323 175
619ac84f
SZ
176(defun pop3-get-message-count ()
177 "Return the number of messages in the maildrop."
178 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
179 message-count
e62e7654 180 (pop3-password pop3-password))
619ac84f
SZ
181 ;; for debugging only
182 (if pop3-debug (switch-to-buffer (process-buffer process)))
183 ;; query for password
184 (if (and pop3-password-required (not pop3-password))
185 (setq pop3-password
23f87bed 186 (read-passwd (format "Password for %s: " pop3-maildrop))))
619ac84f
SZ
187 (cond ((equal 'apop pop3-authentication-scheme)
188 (pop3-apop process pop3-maildrop))
189 ((equal 'pass pop3-authentication-scheme)
190 (pop3-user process pop3-maildrop)
191 (pop3-pass process))
55535639 192 (t (error "Invalid POP3 authentication scheme")))
619ac84f
SZ
193 (setq message-count (car (pop3-stat process)))
194 (pop3-quit process)
195 message-count))
196
eec82323 197(defun pop3-open-server (mailhost port)
dd5da9b8 198 "Open TCP connection to MAILHOST on PORT.
eec82323 199Returns the process associated with the connection."
4cac7481 200 (let ((coding-system-for-read 'binary)
6748645f 201 (coding-system-for-write 'binary)
4cac7481 202 process)
eec82323 203 (save-excursion
4cac7481
DL
204 (set-buffer (get-buffer-create (concat " trace of POP session to "
205 mailhost)))
6748645f
LMI
206 (erase-buffer)
207 (setq pop3-read-point (point-min))
d3b055c2 208 (setq process (open-network-stream "POP" (current-buffer) mailhost port))
4cac7481
DL
209 (let ((response (pop3-read-response process t)))
210 (setq pop3-timestamp
211 (substring response (or (string-match "<" response) 0)
212 (+ 1 (or (string-match ">" response) -1)))))
213 process)))
eec82323
LMI
214
215;; Support functions
216
217(defun pop3-process-filter (process output)
218 (save-excursion
219 (set-buffer (process-buffer process))
220 (goto-char (point-max))
221 (insert output)))
222
223(defun pop3-send-command (process command)
e62e7654
MB
224 (set-buffer (process-buffer process))
225 (goto-char (point-max))
226 ;; (if (= (aref command 0) ?P)
227 ;; (insert "PASS <omitted>\r\n")
228 ;; (insert command "\r\n"))
229 (setq pop3-read-point (point))
230 (goto-char (point-max))
231 (process-send-string process (concat command "\r\n")))
eec82323
LMI
232
233(defun pop3-read-response (process &optional return)
234 "Read the response from the server.
235Return the response string if optional second argument is non-nil."
236 (let ((case-fold-search nil)
237 match-end)
238 (save-excursion
239 (set-buffer (process-buffer process))
240 (goto-char pop3-read-point)
23f87bed
MB
241 (while (and (memq (process-status process) '(open run))
242 (not (search-forward "\r\n" nil t)))
8903a9c8 243 (pop3-accept-process-output process)
eec82323
LMI
244 (goto-char pop3-read-point))
245 (setq match-end (point))
246 (goto-char pop3-read-point)
247 (if (looking-at "-ERR")
248 (error (buffer-substring (point) (- match-end 2)))
249 (if (not (looking-at "+OK"))
250 (progn (setq pop3-read-point match-end) nil)
251 (setq pop3-read-point match-end)
252 (if return
253 (buffer-substring (point) match-end)
254 t)
255 )))))
256
eec82323
LMI
257(defun pop3-clean-region (start end)
258 (setq end (set-marker (make-marker) end))
259 (save-excursion
260 (goto-char start)
261 (while (and (< (point) end) (search-forward "\r\n" end t))
262 (replace-match "\n" t t))
263 (goto-char start)
264 (while (and (< (point) end) (re-search-forward "^\\." end t))
265 (replace-match "" t t)
266 (forward-char)))
267 (set-marker end nil))
268
daaeed87
DL
269(eval-when-compile (defvar parse-time-months))
270
271;; Copied from message-make-date.
272(defun pop3-make-date (&optional now)
273 "Make a valid date header.
274If NOW, use that time instead."
275 (require 'parse-time)
276 (let* ((now (or now (current-time)))
277 (zone (nth 8 (decode-time now)))
278 (sign "+"))
279 (when (< zone 0)
280 (setq sign "-")
281 (setq zone (- zone)))
282 (concat
283 (format-time-string "%d" now)
284 ;; The month name of the %b spec is locale-specific. Pfff.
285 (format " %s "
286 (capitalize (car (rassoc (nth 4 (decode-time now))
287 parse-time-months))))
288 (format-time-string "%Y %H:%M:%S " now)
289 ;; We do all of this because XEmacs doesn't have the %z spec.
290 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
291
eec82323
LMI
292(defun pop3-munge-message-separator (start end)
293 "Check to see if a message separator exists. If not, generate one."
294 (save-excursion
295 (save-restriction
296 (narrow-to-region start end)
297 (goto-char (point-min))
298 (if (not (or (looking-at "From .?") ; Unix mail
299 (looking-at "\001\001\001\001\n") ; MMDF
300 (looking-at "BABYL OPTIONS:") ; Babyl
301 ))
ae496852
SZ
302 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
303 (tdate (mail-fetch-field "Date"))
304 (date (split-string (or (and tdate
305 (not (string= "" tdate))
306 tdate)
307 (pop3-make-date))
308 " "))
309 (From_))
eec82323
LMI
310 ;; sample date formats I have seen
311 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
312 ;; Date: 08 Jul 1996 23:22:24 -0400
313 ;; should be
314 ;; Tue Jul 9 09:04:21 1996
315 (setq date
ae496852 316 (cond ((not date)
23f87bed 317 "Tue Jan 1 00:00:0 1900")
ae496852 318 ((string-match "[A-Z]" (nth 0 date))
eec82323
LMI
319 (format "%s %s %s %s %s"
320 (nth 0 date) (nth 2 date) (nth 1 date)
321 (nth 4 date) (nth 3 date)))
322 (t
323 ;; this really needs to be better but I don't feel
324 ;; like writing a date to day converter.
325 (format "Sun %s %s %s %s"
326 (nth 1 date) (nth 0 date)
327 (nth 3 date) (nth 2 date)))
328 ))
329 (setq From_ (format "\nFrom %s %s\n" from date))
330 (while (string-match "," From_)
331 (setq From_ (concat (substring From_ 0 (match-beginning 0))
332 (substring From_ (match-end 0)))))
333 (goto-char (point-min))
dd5da9b8 334 (insert From_)
daaeed87
DL
335 (if (search-forward "\n\n" nil t)
336 nil
337 (goto-char (point-max))
338 (insert "\n"))
dd5da9b8
DL
339 (narrow-to-region (point) (point-max))
340 (let ((size (- (point-max) (point-min))))
341 (goto-char (point-min))
342 (widen)
343 (forward-line -1)
344 (insert (format "Content-Length: %s\n" size)))
345 )))))
eec82323
LMI
346
347;; The Command Set
348
349;; AUTHORIZATION STATE
350
ad136a7c
MB
351(eval-and-compile
352 (if (fboundp 'md5)
353 (defalias 'pop3-md5 'md5)
354 (defvar pop3-md5-program "md5"
355 "*Program to encode its input in MD5.")
356
357 (defun pop3-md5 (string)
358 (with-temp-buffer
359 (insert string)
360 (call-process-region (point-min) (point-max)
361 pop3-md5-program
362 t (current-buffer) nil)
363 ;; The meaningful output is the first 32 characters.
364 ;; Don't return the newline that follows them!
365 (buffer-substring (point-min) (+ 32 (point-min)))))))
366
eec82323
LMI
367(defun pop3-user (process user)
368 "Send USER information to POP3 server."
369 (pop3-send-command process (format "USER %s" user))
370 (let ((response (pop3-read-response process t)))
371 (if (not (and response (string-match "+OK" response)))
55535639 372 (error (format "USER %s not valid" user)))))
eec82323
LMI
373
374(defun pop3-pass (process)
375 "Send authentication information to the server."
6748645f
LMI
376 (pop3-send-command process (format "PASS %s" pop3-password))
377 (let ((response (pop3-read-response process t)))
378 (if (not (and response (string-match "+OK" response)))
379 (pop3-quit process))))
380
381(defun pop3-apop (process user)
382 "Send alternate authentication information to the server."
eec82323
LMI
383 (let ((pass pop3-password))
384 (if (and pop3-password-required (not pass))
385 (setq pass
23f87bed 386 (read-passwd (format "Password for %s: " pop3-maildrop))))
eec82323 387 (if pass
6748645f
LMI
388 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
389 (pop3-send-command process (format "APOP %s %s" user hash))
eec82323
LMI
390 (let ((response (pop3-read-response process t)))
391 (if (not (and response (string-match "+OK" response)))
392 (pop3-quit process)))))
393 ))
394
6748645f
LMI
395;; TRANSACTION STATE
396
eec82323
LMI
397(defun pop3-stat (process)
398 "Return the number of messages in the maildrop and the maildrop's size."
399 (pop3-send-command process "STAT")
400 (let ((response (pop3-read-response process t)))
4cac7481
DL
401 (list (string-to-int (nth 1 (split-string response " ")))
402 (string-to-int (nth 2 (split-string response " "))))
eec82323
LMI
403 ))
404
405(defun pop3-list (process &optional msg)
406 "Scan listing of available messages.
407This function currently does nothing.")
408
409(defun pop3-retr (process msg crashbuf)
410 "Retrieve message-id MSG to buffer CRASHBUF."
411 (pop3-send-command process (format "RETR %s" msg))
412 (pop3-read-response process)
413 (let ((start pop3-read-point) end)
414 (save-excursion
415 (set-buffer (process-buffer process))
416 (while (not (re-search-forward "^\\.\r\n" nil t))
8903a9c8 417 (pop3-accept-process-output process)
eec82323
LMI
418 (goto-char start))
419 (setq pop3-read-point (point-marker))
e62e7654
MB
420 ;; this code does not seem to work for some POP servers...
421 ;; and I cannot figure out why not.
422 ;; (goto-char (match-beginning 0))
423 ;; (backward-char 2)
424 ;; (if (not (looking-at "\r\n"))
425 ;; (insert "\r\n"))
426 ;; (re-search-forward "\\.\r\n")
eec82323
LMI
427 (goto-char (match-beginning 0))
428 (setq end (point-marker))
429 (pop3-clean-region start end)
430 (pop3-munge-message-separator start end)
431 (save-excursion
432 (set-buffer crashbuf)
433 (erase-buffer))
434 (copy-to-buffer crashbuf start end)
435 (delete-region start end)
436 )))
437
438(defun pop3-dele (process msg)
439 "Mark message-id MSG as deleted."
440 (pop3-send-command process (format "DELE %s" msg))
441 (pop3-read-response process))
442
443(defun pop3-noop (process msg)
444 "No-operation."
445 (pop3-send-command process "NOOP")
446 (pop3-read-response process))
447
448(defun pop3-last (process)
449 "Return highest accessed message-id number for the session."
450 (pop3-send-command process "LAST")
451 (let ((response (pop3-read-response process t)))
4cac7481 452 (string-to-int (nth 1 (split-string response " ")))
eec82323
LMI
453 ))
454
455(defun pop3-rset (process)
456 "Remove all delete marks from current maildrop."
457 (pop3-send-command process "RSET")
458 (pop3-read-response process))
459
460;; UPDATE
461
462(defun pop3-quit (process)
463 "Close connection to POP3 server.
464Tell server to remove all messages marked as deleted, unlock the maildrop,
465and close the connection."
466 (pop3-send-command process "QUIT")
467 (pop3-read-response process t)
468 (if process
469 (save-excursion
470 (set-buffer (process-buffer process))
471 (goto-char (point-max))
472 (delete-process process))))
473\f
474;; Summary of POP3 (Post Office Protocol version 3) commands and responses
475
476;;; AUTHORIZATION STATE
477
478;; Initial TCP connection
479;; Arguments: none
480;; Restrictions: none
481;; Possible responses:
482;; +OK [POP3 server ready]
483
484;; USER name
485;; Arguments: a server specific user-id (required)
486;; Restrictions: authorization state [after unsuccessful USER or PASS
487;; Possible responses:
488;; +OK [valid user-id]
489;; -ERR [invalid user-id]
490
491;; PASS string
492;; Arguments: a server/user-id specific password (required)
493;; Restrictions: authorization state, after successful USER
494;; Possible responses:
495;; +OK [maildrop locked and ready]
496;; -ERR [invalid password]
497;; -ERR [unable to lock maildrop]
498
499;;; TRANSACTION STATE
500
501;; STAT
502;; Arguments: none
503;; Restrictions: transaction state
504;; Possible responses:
505;; +OK nn mm [# of messages, size of maildrop]
506
507;; LIST [msg]
508;; Arguments: a message-id (optional)
509;; Restrictions: transaction state; msg must not be deleted
510;; Possible responses:
511;; +OK [scan listing follows]
512;; -ERR [no such message]
513
514;; RETR msg
515;; Arguments: a message-id (required)
516;; Restrictions: transaction state; msg must not be deleted
517;; Possible responses:
518;; +OK [message contents follow]
519;; -ERR [no such message]
520
521;; DELE msg
522;; Arguments: a message-id (required)
523;; Restrictions: transaction state; msg must not be deleted
524;; Possible responses:
525;; +OK [message deleted]
526;; -ERR [no such message]
527
528;; NOOP
529;; Arguments: none
530;; Restrictions: transaction state
531;; Possible responses:
532;; +OK
533
534;; LAST
535;; Arguments: none
536;; Restrictions: transaction state
537;; Possible responses:
538;; +OK nn [highest numbered message accessed]
539
540;; RSET
541;; Arguments: none
542;; Restrictions: transaction state
543;; Possible responses:
544;; +OK [all delete marks removed]
545
546;;; UPDATE STATE
547
548;; QUIT
549;; Arguments: none
550;; Restrictions: none
551;; Possible responses:
552;; +OK [TCP connection closed]
dd5da9b8
DL
553
554(provide 'pop3)
555
ab5796a9 556;;; arch-tag: 2facc142-1d74-498e-82af-4659b64cac12
dd5da9b8 557;;; pop3.el ends here