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