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