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