b76e74f8feda80ce61364356b5969e118b7c33e3
[gnus] / lisp / pop3.el
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3 ;; Copyright (C) 1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Keywords: mail, pop3
7 ;; Version: 1.3g
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
29 ;; are implemented.  The LIST command has not been implemented due to lack
30 ;; of actual usefulness.
31 ;; The optional POP3 command TOP has not been implemented.
32
33 ;; This program was inspired by Kyle E. Jones's vm-pop program.
34
35 ;;; Code:
36
37 (require 'mail-utils)
38 (provide 'pop3)
39
40 (defconst pop3-version "1.3g")
41
42 (defvar pop3-maildrop (or user-login-name (getenv "LOGNAME") (getenv "USER") nil)
43   "*POP3 maildrop.")
44 (defvar pop3-mailhost (or (getenv "MAILHOST") nil)
45   "*POP3 mailhost.")
46 (defvar pop3-port 110
47   "*POP3 port.")
48
49 (defvar pop3-password-required t
50   "*Non-nil if a password is required when connecting to POP server.")
51 (defvar pop3-password nil
52   "*Password to use when connecting to POP server.")
53
54 (defvar pop3-authentication-scheme 'pass
55   "*POP3 authentication scheme.
56 Defaults to 'pass, for the standard USER/PASS authentication.  Other valid
57 values are 'apop.")
58
59 (defvar pop3-timestamp nil
60   "Timestamp returned when initially connected to the POP server.
61 Used for APOP authentication.")
62
63 (defvar pop3-movemail-file-coding-system nil
64   "Crashbox made by pop3-movemail with this coding system.")
65
66 (defvar pop3-read-point nil)
67 (defvar pop3-debug nil)
68
69 (defun pop3-movemail (&optional crashbox)
70   "Transfer contents of a maildrop to the specified CRASHBOX."
71   (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
72   (let* ((process (pop3-open-server pop3-mailhost pop3-port))
73          (crashbuf (get-buffer-create " *pop3-retr*"))
74          (n 1)
75          message-count)
76     ;; for debugging only
77     (if pop3-debug (switch-to-buffer (process-buffer process)))
78     (cond ((equal 'apop pop3-authentication-scheme)
79            (pop3-apop process pop3-maildrop))
80           ((equal 'pass pop3-authentication-scheme)
81            (pop3-user process pop3-maildrop)
82            (pop3-pass process))
83           (t (error "Invalid POP3 authentication scheme.")))
84     (setq message-count (car (pop3-stat process)))
85     (while (<= n message-count)
86       (message (format "Retrieving message %d of %d from %s..."
87                        n message-count pop3-mailhost))
88       (pop3-retr process n crashbuf)
89       (save-excursion
90         (set-buffer crashbuf)
91         (let ((coding-system-for-write pop3-movemail-file-coding-system))
92           (append-to-file (point-min) (point-max) crashbox))
93         (set-buffer (process-buffer process))
94         (while (> (buffer-size) 5000)
95           (goto-char (point-min))
96           (forward-line 50)
97           (delete-region (point-min) (point))))
98       (pop3-dele process n)
99       (setq n (+ 1 n))
100       (if pop3-debug (sit-for 1) (sit-for 0.1))
101       )
102     (pop3-quit process)
103     (kill-buffer crashbuf)
104     )
105   )
106
107 (defun pop3-open-server (mailhost port)
108   "Open TCP connection to MAILHOST.
109 Returns the process associated with the connection."
110   (let ((process-buffer
111          (get-buffer-create (format "trace of POP session to %s" mailhost)))
112         (process))
113     (save-excursion
114       (set-buffer process-buffer)
115       (erase-buffer))
116     (setq process
117           (open-network-stream "POP" process-buffer mailhost port))
118     (setq pop3-read-point (point-min))
119     (let ((response (pop3-read-response process t)))
120       (setq pop3-timestamp
121             (substring response (or (string-match "<" response) 0)
122                        (+ 1 (or (string-match ">" response) -1)))))
123     process
124     ))
125
126 ;; Support functions
127
128 (defun pop3-process-filter (process output)
129   (save-excursion
130     (set-buffer (process-buffer process))
131     (goto-char (point-max))
132     (insert output)))
133
134 (defun pop3-send-command (process command)
135     (set-buffer (process-buffer process))
136     (goto-char (point-max))
137 ;;    (if (= (aref command 0) ?P)
138 ;;      (insert "PASS <omitted>\r\n")
139 ;;      (insert command "\r\n"))
140     (setq pop3-read-point (point))
141     (goto-char (point-max))
142     (process-send-string process command)
143     (process-send-string process "\r\n")
144     )
145
146 (defun pop3-read-response (process &optional return)
147   "Read the response from the server.
148 Return the response string if optional second argument is non-nil."
149   (let ((case-fold-search nil)
150         match-end)
151     (save-excursion
152       (set-buffer (process-buffer process))
153       (goto-char pop3-read-point)
154       (while (not (search-forward "\r\n" nil t))
155         (accept-process-output process 3)
156         (goto-char pop3-read-point))
157       (setq match-end (point))
158       (goto-char pop3-read-point)
159       (if (looking-at "-ERR")
160           (error (buffer-substring (point) (- match-end 2)))
161         (if (not (looking-at "+OK"))
162             (progn (setq pop3-read-point match-end) nil)
163           (setq pop3-read-point match-end)
164           (if return
165               (buffer-substring (point) match-end)
166             t)
167           )))))
168
169 (defun pop3-string-to-list (string &optional regexp)
170   "Chop up a string into a list."
171   (let ((list)
172         (regexp (or regexp " "))
173         (string (if (string-match "\r" string)
174                     (substring string 0 (match-beginning 0))
175                   string)))
176     (store-match-data nil)
177     (while string