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