*** empty log message ***
[gnus] / lisp / pop3.el
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3 ;; Copyright (C) 1996, Free Software Foundation, Inc.
4
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Keywords: mail, pop3
7 ;; Version: 1.3
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 (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.  Defaults to 'pass, for the standard
54 USER/PASS authentication.  Other valid values are 'apop.")
55
56 (defvar pop3-timestamp nil
57   "Timestamp returned when initially connected to the POP server.
58 Used for APOP authentication.")
59
60 (defvar pop3-read-point nil)
61 (defvar pop3-debug nil)
62
63 (defun pop3-movemail (&optional crashbox)
64   "Transfer contents of a maildrop to the specified CRASHBOX."
65   (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
66   (let* ((process (pop3-open-server pop3-mailhost pop3-port))
67          (crashbuf (get-buffer-create " *pop3-retr*"))
68          (n 1)
69          message-count)
70     ;; for debugging only
71     (if pop3-debug (switch-to-buffer (process-buffer process)))
72     (cond ((equal 'apop pop3-authentication-scheme)
73            (pop3-apop process pop3-maildrop))
74           ((equal 'pass pop3-authentication-scheme)
75            (pop3-user process pop3-maildrop)
76            (pop3-pass process))
77           (t (error "Invalid POP3 authentication scheme.")))
78     (setq message-count (car (pop3-stat process)))
79     (while (<= n message-count)
80       (message (format "Retrieving message %d of %d from %s..."
81                        n message-count pop3-mailhost))
82       (pop3-retr process n crashbuf)
83       (save-excursion
84         (set-buffer crashbuf)
85         (append-to-file (point-min) (point-max) crashbox))
86       (pop3-dele process n)
87       (setq n (+ 1 n)))
88     (pop3-quit process)
89     (kill-buffer crashbuf)
90     )
91   )
92
93 (defun pop3-open-server (mailhost port)
94   "Open TCP connection to MAILHOST.
95 Returns the process associated with the connection."
96   (let ((process-buffer
97          (get-buffer-create (format "trace of POP session to %s" mailhost)))
98         (process))
99     (save-excursion
100       (set-buffer process-buffer)
101       (erase-buffer))
102     (setq process
103           (open-network-stream "POP" process-buffer mailhost port))
104     (setq pop3-read-point (point-min))
105     (let ((response (pop3-read-response process t)))
106       (setq pop3-timestamp
107             (substring response (or (string-match "<" response) 0)
108                        (+ 1 (or (string-match ">" response) -1)))))
109     process
110     ))
111
112 ;; Support functions
113
114 (defun pop3-process-filter (process output)
115   (save-excursion
116     (set-buffer (process-buffer process))
117     (goto-char (point-max))
118     (insert output)))
119
120 (defun pop3-send-command (process command)
121     (set-buffer (process-buffer process))
122     (goto-char (point-max))
123 ;;    (if (= (aref command 0) ?P)
124 ;;      (insert "PASS <omitted>\r\n")
125 ;;      (insert command "\r\n"))
126     (setq pop3-read-point (point))
127     (goto-char (point-max))
128     (process-send-string process command)
129     (process-send-string process "\r\n")
130     )
131
132 (defun pop3-read-response (process &optional return)
133   "Read the response from the server.
134 Return the response string if optional second argument is non-nil."
135   (let ((case-fold-search nil)
136         match-end)
137     (save-excursion
138       (set-buffer (process-buffer process))
139       (goto-char pop3-read-point)
140       (while (not (search-forward "\r\n" nil t))
141         (accept-process-output process)
142         (goto-char pop3-read-point))
143       (setq match-end (point))
144       (goto-char pop3-read-point)
145       (if (looking-at "-ERR")
146           (error (buffer-substring (point) (- match-end 2)))
147         (if (not (looking-at "+OK"))
148             (progn (setq pop3-read-point match-end) nil)
149           (setq pop3-read-point match-end)
150           (if return
151               (buffer-substring (point) match-end)
152             t)
153           )))))
154
155 (defun pop3-string-to-list (string &optional regexp)
156   "Chop up a string into a list."
157   (let ((list)
158         (regexp (or regexp " "))
159         (string (if (string-match "\r" string)
160                     (substring string 0 (match-beginning 0))
161                   string)))
162     (store-match-data nil)
163     (while string
164       (if (string-match regexp string)
165           (setq list (cons (substring string 0 (- (match-end 0) 1)) list)
166                 string (substring string (match-end 0)))
167         (setq list (cons string list)
168               string nil)))
169     (nreverse list)))
170
171 (defvar pop3-read-passwd nil)
172 (defun pop3-read-passwd (prompt)
173   (if (not pop3-read-passwd)
174       (if (load "passwd" t)
175           (setq pop3-read-passwd 'read-passwd)
176         (autoload 'ange-ftp-read-passwd "ange-ftp")
177         (setq pop3-read-passwd 'ange-ftp-read-passwd)))
178   (funcall pop3-read-passwd prompt))
179
180 (defun pop3-clean-region (start end)
181   (setq end (set-marker (make-marker) end))
182   (save-excursion
183     (goto-char start)
184     (while (and (< (point) end) (search-forward "\r\n" end t))
185       (replace-match "\n" t t))
186     (goto-char start)
187     (while (and (< (point) end) (re-search-forward "^\\." end t))
188       (replace-match "" t t)
189       (forward-char)))
190   (set-marker end nil))
191
192 (defun pop3-munge-message-separator (start end)
193   "Check to see if a message separator exists.  If not, generate one."
194   (save-excursion
195     (save-restriction
196       (narrow-to-region start end)
197       (goto-char (point-min))
198       (if (not (or (looking-at "From .?") ; Unix mail
199                    (looking-at "\001\001\001\001\n") ; MMDF
200                    (looking-at "BABYL OPTIONS:") ; Babyl
201                    ))
202           (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
203                 (date (pop3-string-to-list (mail-fetch-field "Date")))
204                 (From_))
205             ;; sample date formats I have seen
206             ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
207             ;; Date: 08 Jul 1996 23:22:24 -0400
208             ;; should be
209             ;; Tue Jul 9 09:04:21 1996
210             (setq date
211                   (cond ((string-match "[A-Z]" (nth 0 date))
212                          (format "%s %s %s %s %s"
213                                  (nth 0 date) (nth 2 date) (nth 1 date)
214                                  (nth 4 date) (nth 3 date)))
215                         (t
216                          ;; this really needs to be better but I don't feel
217                          ;; like writing a date to day converter.
218                          (format "Sun %s %s %s %s"
219                                  (nth 1 date) (nth 0 date)
220                                  (nth 3 date) (nth 2 date)))
221                         ))
222             (setq From_ (format "From %s  %s\n" from date))
223             (while (string-match "," From_)
224               (setq From_ (concat (substring From_ 0 (match-beginning 0))
225                                   (substring From_ (match-end 0)))))
226             (goto-char (point-min))
227             (insert From_))))))
228
229 ;; The Command Set
230
231 ;; AUTHORIZATION STATE
232
233 (defun pop3-user (process user)
234   "Send USER information to POP3 server."
235   (pop3-send-command process (format "USER %s" user))
236   (let ((response (pop3-read-response process t)))
237     (if (not (and response (string-match "+OK" response)))
238         (error (format "USER %s not valid." user)))))
239
240 (defun pop3-pass (process)
241   "Send authentication information to the server."
242   (let ((pass pop3-password))
243     (if (and pop3-password-required (not pass))
244         (setq pass
245               (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
246     (if pass
247         (progn
248           (pop3-send-command process (format "PASS %s" pass))
249           (let ((response (pop3-read-response process t)))
250             (if (not (and response (string-match "+OK" response)))
251                 (pop3-quit process)))))
252     ))
253
254 (defun pop3-apop (process user)
255   "Send alternate authentication information to the server."
256   (if (not (fboundp 'md5)) (autoload 'md5 "md5"))
257   (let ((pass pop3-password))
258     (if (and pop3-password-required (not pass))
259         (setq pass
260               (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
261     (if pass
262         (let ((hash (md5 (concat pop3-timestamp pass))))
263           (pop3-send-command process (format "APOP %s %s" user hash))
264           (let ((response (pop3-read-response process t)))
265             (if (not (and response (string-match "+OK" response)))
266                 (pop3-quit process)))))
267     ))
268
269 ;; TRANSACTION STATE
270
271 (defun pop3-stat (process)
272   "Return a list of the number of messages in the maildrop and the size
273 of the maildrop."
274   (pop3-send-command process "STAT")
275   (let ((response (pop3-read-response process t)))
276     (list (string-to-int (nth 1 (pop3-string-to-list response)))
277           (string-to-int (nth 2 (pop3-string-to-list response))))
278     ))
279
280 (defun pop3-list (process &optional msg)
281   "Scan listing of available messages.
282 This function currently does nothing.")
283
284 (defun pop3-retr (process msg crashbuf)
285   "Retrieve message-id MSG from the server and place the contents in
286 buffer CRASHBUF."
287   (pop3-send-command process (format "RETR %s" msg))
288   (pop3-read-response process)
289   (let ((start pop3-read-point) end)
290     (save-excursion
291       (set-buffer (process-buffer process))
292       (while (not (re-search-forward "^\\.\r\n" nil t))
293         (accept-process-output process)
294         ;; bill@att.com ... to save wear and tear on the heap
295         (if (> (buffer-size)  20000) (sleep-for 1))
296         (if (> (buffer-size)  50000) (sleep-for 1))
297         (if (> (buffer-size) 100000) (sleep-for 1))
298         (if (> (buffer-size) 200000) (sleep-for 1))
299         (if (> (buffer-size) 500000) (sleep-for 1))
300         ;; bill@att.com
301         (goto-char start))
302       (setq pop3-read-point (point-marker))
303       (goto-char (match-beginning 0))
304       (setq end (point-marker))
305       (pop3-clean-region start end)
306       (pop3-munge-message-separator start end)
307       (save-excursion
308         (set-buffer crashbuf)
309         (erase-buffer))
310       (copy-to-buffer crashbuf start end)
311       (delete-region start end)
312       )))
313
314 (defun pop3-dele (process msg)
315   "Mark message-id MSG as deleted."
316   (pop3-send-command process (format "DELE %s" msg))
317   (pop3-read-response process))
318
319 (defun pop3-noop (process msg)
320   "No-operation."
321   (pop3-send-command process "NOOP")
322   (pop3-read-response process))
323
324 (defun pop3-last (process)
325   "Return highest accessed message-id number for the session."
326   (pop3-send-command process "LAST")
327   (let ((response (pop3-read-response process t)))
328     (string-to-int (nth 1 (pop3-string-to-list response)))
329     ))
330
331 (defun pop3-rset (process)
332   "Remove all delete marks from current maildrop."
333   (pop3-send-command process "RSET")
334   (pop3-read-response process))
335
336 ;; UPDATE
337
338 (defun pop3-quit (process)
339   "Tell server to remove all messages marked as deleted, unlock the
340 maildrop, and close the connection."
341   (pop3-send-command process "QUIT")
342   (pop3-read-response process t)
343   (if process
344       (save-excursion
345         (set-buffer (process-buffer process))
346         (goto-char (point-max))
347         (delete-process process))))
348 \f
349 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
350
351 ;;; AUTHORIZATION STATE
352
353 ;; Initial TCP connection
354 ;; Arguments: none
355 ;; Restrictions: none
356 ;; Possible responses:
357 ;;  +OK [POP3 server ready]
358
359 ;; USER name
360 ;; Arguments: a server specific user-id (required)
361 ;; Restrictions: authorization state [after unsuccessful USER or PASS
362 ;; Possible responses:
363 ;;  +OK [valid user-id]
364 ;;  -ERR [invalid user-id]
365
366 ;; PASS string
367 ;; Arguments: a server/user-id specific password (required)
368 ;; Restrictions: authorization state, after successful USER
369 ;; Possible responses:
370 ;;  +OK [maildrop locked and ready]
371 ;;  -ERR [invalid password]
372 ;;  -ERR [unable to lock maildrop]
373
374 ;;; TRANSACTION STATE
375
376 ;; STAT
377 ;; Arguments: none
378 ;; Restrictions: transaction state
379 ;; Possible responses:
380 ;;  +OK nn mm [# of messages, size of maildrop]
381
382 ;; LIST [msg]
383 ;; Arguments: a message-id (optional)
384 ;; Restrictions: transaction state; msg must not be deleted
385 ;; Possible responses:
386 ;;  +OK [scan listing follows]
387 ;;  -ERR [no such message]
388
389 ;; RETR msg
390 ;; Arguments: a message-id (required)
391 ;; Restrictions: transaction state; msg must not be deleted
392 ;; Possible responses:
393 ;;  +OK [message contents follow]
394 ;;  -ERR [no such message]
395
396 ;; DELE msg
397 ;; Arguments: a message-id (required)
398 ;; Restrictions: transaction state; msg must not be deleted
399 ;; Possible responses:
400 ;;  +OK [message deleted]
401 ;;  -ERR [no such message]
402
403 ;; NOOP
404 ;; Arguments: none
405 ;; Restrictions: transaction state
406 ;; Possible responses:
407 ;;  +OK
408
409 ;; LAST
410 ;; Arguments: none
411 ;; Restrictions: transaction state
412 ;; Possible responses:
413 ;;  +OK nn [highest numbered message accessed]
414
415 ;; RSET
416 ;; Arguments: none
417 ;; Restrictions: transaction state
418 ;; Possible responses:
419 ;;  +OK [all delete marks removed]
420
421 ;;; UPDATE STATE
422
423 ;; QUIT
424 ;; Arguments: none
425 ;; Restrictions: none
426 ;; Possible responses:
427 ;;  +OK [TCP connection closed]