Mercurial > emacs
annotate lisp/gnus/pop3.el @ 19860:c17fd465ea95 libc-970911 libc-970912 libc-970913 libc-970914 libc-970915 libc-970916 libc-970917 libc-970918 libc-970919 libc-970920 libc-970921 libc-970922 libc-970923 libc-970924 libc-970925 libc-970926 libc-970927 libc-970928 libc-970929 libc-970930 libc-971001 libc-971018 libc-971019 libc-971020 libc-971021 libc-971022 libc-971023 libc-971024 libc-971025 libc-971026 libc-971027 libc-971028 libc-971029 libc-971030 libc-971031 libc-971101 libc-971102 libc-971103 libc-971104 libc-971105 libc-971106 libc-971107 libc-971108 libc-971109 libc-971110 libc-971111 libc-971112 libc-971113 libc-971114 libc-971115 libc-971116 libc-971117 libc-971118 libc-971120 libc-971121 libc-971122 libc-971123 libc-971124 libc-971125 libc-971126 libc-971127 libc-971128 libc-971129 libc-971130 libc-971201 libc-971203 libc-971204 libc-971205 libc-971206 libc-971207 libc-971208 libc-971209 libc-971210 libc-971211 libc-971212 libc-971213 libc-971214 libc-971217 libc-971218 libc-971219 libc-971220 libc-971221 libc-971222 libc-971223 libc-971224 libc-971225 libc-971226 libc-971227 libc-971228 libc-971229 libc-971230 libc-971231 libc-980103 libc-980104 libc-980105 libc-980106 libc-980107 libc-980108 libc-980109 libc-980110 libc-980111 libc-980112 libc-980114 libc-980115 libc-980116 libc-980117 libc-980118 libc-980119 libc-980120 libc-980121 libc-980122 libc-980123 libc-980124 libc-980125 libc-980126 libc-980127 libc-980128
typos.
author | Jeff Law <law@redhat.com> |
---|---|
date | Wed, 10 Sep 1997 21:16:20 +0000 |
parents | 25317a11d7a1 |
children | 5f1ab3dd344d |
rev | line source |
---|---|
17493 | 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.3e | |
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.3c") | |
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 | |
19595
7184c8337963
(pop3-movemail-file-coding-system): Append it for
Kenichi Handa <handa@m17n.org>
parents:
17493
diff
changeset
|
63 (defvar pop3-movemail-file-coding-system nil |
7184c8337963
(pop3-movemail-file-coding-system): Append it for
Kenichi Handa <handa@m17n.org>
parents:
17493
diff
changeset
|
64 "Crashbox made by pop3-movemail with this coding system.") |
7184c8337963
(pop3-movemail-file-coding-system): Append it for
Kenichi Handa <handa@m17n.org>
parents:
17493
diff
changeset
|
65 |
17493 | 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) | |
19595
7184c8337963
(pop3-movemail-file-coding-system): Append it for
Kenichi Handa <handa@m17n.org>
parents:
17493
diff
changeset
|
91 (let ((coding-system-for-write pop3-movemail-file-coding-system)) |
7184c8337963
(pop3-movemail-file-coding-system): Append it for
Kenichi Handa <handa@m17n.org>
parents:
17493
diff
changeset
|
92 (append-to-file (point-min) (point-max) crashbox)) |
17493 | 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) | |
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 | |
178 (if (string-match regexp string) | |
179 (setq list (cons (substring string 0 (- (match-end 0) 1)) list) | |
180 string (substring string (match-end 0))) | |
181 (setq list (cons string list) | |
182 string nil))) | |
183 (nreverse list))) | |
184 | |
185 (defvar pop3-read-passwd nil) | |
186 (defun pop3-read-passwd (prompt) | |
187 (if (not pop3-read-passwd) | |
188 (if (load "passwd" t) | |
189 (setq pop3-read-passwd 'read-passwd) | |
190 (autoload 'ange-ftp-read-passwd "ange-ftp") | |
191 (setq pop3-read-passwd 'ange-ftp-read-passwd))) | |
192 (funcall pop3-read-passwd prompt)) | |
193 | |
194 (defun pop3-clean-region (start end) | |
195 (setq end (set-marker (make-marker) end)) | |
196 (save-excursion | |
197 (goto-char start) | |
198 (while (and (< (point) end) (search-forward "\r\n" end t)) | |
199 (replace-match "\n" t t)) | |
200 (goto-char start) | |
201 (while (and (< (point) end) (re-search-forward "^\\." end t)) | |
202 (replace-match "" t t) | |
203 (forward-char))) | |
204 (set-marker end nil)) | |
205 | |
206 (defun pop3-munge-message-separator (start end) | |
207 "Check to see if a message separator exists. If not, generate one." | |
208 (save-excursion | |
209 (save-restriction | |
210 (narrow-to-region start end) | |
211 (goto-char (point-min)) | |
212 (if (not (or (looking-at "From .?") ; Unix mail | |
213 (looking-at "\001\001\001\001\n") ; MMDF | |
214 (looking-at "BABYL OPTIONS:") ; Babyl | |
215 )) | |
216 (let ((from (mail-strip-quoted-names (mail-fetch-field "From"))) | |
217 (date (pop3-string-to-list (mail-fetch-field "Date"))) | |
218 (From_)) | |
219 ;; sample date formats I have seen | |
220 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT) | |
221 ;; Date: 08 Jul 1996 23:22:24 -0400 | |
222 ;; should be | |
223 ;; Tue Jul 9 09:04:21 1996 | |
224 (setq date | |
225 (cond ((string-match "[A-Z]" (nth 0 date)) | |
226 (format "%s %s %s %s %s" | |
227 (nth 0 date) (nth 2 date) (nth 1 date) | |
228 (nth 4 date) (nth 3 date))) | |
229 (t | |
230 ;; this really needs to be better but I don't feel | |
231 ;; like writing a date to day converter. | |
232 (format "Sun %s %s %s %s" | |
233 (nth 1 date) (nth 0 date) | |
234 (nth 3 date) (nth 2 date))) | |
235 )) | |
236 (setq From_ (format "\nFrom %s %s\n" from date)) | |
237 (while (string-match "," From_) | |
238 (setq From_ (concat (substring From_ 0 (match-beginning 0)) | |
239 (substring From_ (match-end 0))))) | |
240 (goto-char (point-min)) | |
241 (insert From_)))))) | |
242 | |
243 ;; The Command Set | |
244 | |
245 ;; AUTHORIZATION STATE | |
246 | |
247 (defun pop3-user (process user) | |
248 "Send USER information to POP3 server." | |
249 (pop3-send-command process (format "USER %s" user)) | |
250 (let ((response (pop3-read-response process t))) | |
251 (if (not (and response (string-match "+OK" response))) | |
252 (error (format "USER %s not valid." user))))) | |
253 | |
254 (defun pop3-pass (process) | |
255 "Send authentication information to the server." | |
256 (let ((pass pop3-password)) | |
257 (if (and pop3-password-required (not pass)) | |
258 (setq pass | |
259 (pop3-read-passwd (format "Password for %s: " pop3-maildrop)))) | |
260 (if pass | |
261 (progn | |
262 (pop3-send-command process (format "PASS %s" pass)) | |
263 (let ((response (pop3-read-response process t))) | |
264 (if (not (and response (string-match "+OK" response))) | |
265 (pop3-quit process))))) | |
266 )) | |
267 | |
19633
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
268 (defvar pop3-md5-program "md5" |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
269 "*Program to encode its input in MD5.") |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
270 |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
271 (defun pop3-md5 (string) |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
272 (with-temp-buffer |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
273 (insert string) |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
274 (call-process-region (point-min) (point-max) |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
275 (or shell-file-name "/bin/sh") |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
276 t (current-buffer) nil |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
277 "-c" pop3-md5-program) |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
278 ;; The meaningful output is the first 32 characters. |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
279 ;; Don't return the newline that follows them! |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
280 (buffer-substring (point-min) (+ (point-min) 32)))) |
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
281 |
17493 | 282 (defun pop3-apop (process user) |
283 "Send alternate authentication information to the server." | |
284 (let ((pass pop3-password)) | |
285 (if (and pop3-password-required (not pass)) | |
286 (setq pass | |
287 (pop3-read-passwd (format "Password for %s: " pop3-maildrop)))) | |
288 (if pass | |
19633
25317a11d7a1
(pop3-md5): New function.
Richard M. Stallman <rms@gnu.org>
parents:
19595
diff
changeset
|
289 (let ((hash (pop3-md5 (concat pop3-timestamp pass)))) |
17493 | 290 (pop3-send-command process (format "APOP %s %s" user hash)) |
291 (let ((response (pop3-read-response process t))) | |
292 (if (not (and response (string-match "+OK" response))) | |
293 (pop3-quit process))))) | |
294 )) | |
295 | |
296 ;; TRANSACTION STATE | |
297 | |
298 (defun pop3-stat (process) | |
299 "Return the number of messages in the maildrop and the maildrop's size." | |
300 (pop3-send-command process "STAT") | |
301 (let ((response (pop3-read-response process t))) | |
302 (list (string-to-int (nth 1 (pop3-string-to-list response))) | |
303 (string-to-int (nth 2 (pop3-string-to-list response)))) | |
304 )) | |
305 | |
306 (defun pop3-list (process &optional msg) | |
307 "Scan listing of available messages. | |
308 This function currently does nothing.") | |
309 | |
310 (defun pop3-retr (process msg crashbuf) | |
311 "Retrieve message-id MSG to buffer CRASHBUF." | |
312 (pop3-send-command process (format "RETR %s" msg)) | |
313 (pop3-read-response process) | |
314 (let ((start pop3-read-point) end) | |
315 (save-excursion | |
316 (set-buffer (process-buffer process)) | |
317 (while (not (re-search-forward "^\\.\r\n" nil t)) | |
318 (accept-process-output process) | |
319 ;; bill@att.com ... to save wear and tear on the heap | |
320 (if (> (buffer-size) 20000) (sleep-for 1)) | |
321 (if (> (buffer-size) 50000) (sleep-for 1)) | |
322 (if (> (buffer-size) 100000) (sleep-for 1)) | |
323 (if (> (buffer-size) 200000) (sleep-for 1)) | |
324 (if (> (buffer-size) 500000) (sleep-for 1)) | |
325 ;; bill@att.com | |
326 (goto-char start)) | |
327 (setq pop3-read-point (point-marker)) | |
328 ;; this code does not seem to work for some POP servers... | |
329 ;; and I cannot figure out why not. | |
330 ; (goto-char (match-beginning 0)) | |
331 ; (backward-char 2) | |
332 ; (if (not (looking-at "\r\n")) | |
333 ; (insert "\r\n")) | |
334 ; (re-search-forward "\\.\r\n") | |
335 (goto-char (match-beginning 0)) | |
336 (setq end (point-marker)) | |
337 (pop3-clean-region start end) | |
338 (pop3-munge-message-separator start end) | |
339 (save-excursion | |
340 (set-buffer crashbuf) | |
341 (erase-buffer)) | |
342 (copy-to-buffer crashbuf start end) | |
343 (delete-region start end) | |
344 ))) | |
345 | |
346 (defun pop3-dele (process msg) | |
347 "Mark message-id MSG as deleted." | |
348 (pop3-send-command process (format "DELE %s" msg)) | |
349 (pop3-read-response process)) | |
350 | |
351 (defun pop3-noop (process msg) | |
352 "No-operation." | |
353 (pop3-send-command process "NOOP") | |
354 (pop3-read-response process)) | |
355 | |
356 (defun pop3-last (process) | |
357 "Return highest accessed message-id number for the session." | |
358 (pop3-send-command process "LAST") | |
359 (let ((response (pop3-read-response process t))) | |
360 (string-to-int (nth 1 (pop3-string-to-list response))) | |
361 )) | |
362 | |
363 (defun pop3-rset (process) | |
364 "Remove all delete marks from current maildrop." | |
365 (pop3-send-command process "RSET") | |
366 (pop3-read-response process)) | |
367 | |
368 ;; UPDATE | |
369 | |
370 (defun pop3-quit (process) | |
371 "Close connection to POP3 server. | |
372 Tell server to remove all messages marked as deleted, unlock the maildrop, | |
373 and close the connection." | |
374 (pop3-send-command process "QUIT") | |
375 (pop3-read-response process t) | |
376 (if process | |
377 (save-excursion | |
378 (set-buffer (process-buffer process)) | |
379 (goto-char (point-max)) | |
380 (delete-process process)))) | |
381 | |
382 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses | |
383 | |
384 ;;; AUTHORIZATION STATE | |
385 | |
386 ;; Initial TCP connection | |
387 ;; Arguments: none | |
388 ;; Restrictions: none | |
389 ;; Possible responses: | |
390 ;; +OK [POP3 server ready] | |
391 | |
392 ;; USER name | |
393 ;; Arguments: a server specific user-id (required) | |
394 ;; Restrictions: authorization state [after unsuccessful USER or PASS | |
395 ;; Possible responses: | |
396 ;; +OK [valid user-id] | |
397 ;; -ERR [invalid user-id] | |
398 | |
399 ;; PASS string | |
400 ;; Arguments: a server/user-id specific password (required) | |
401 ;; Restrictions: authorization state, after successful USER | |
402 ;; Possible responses: | |
403 ;; +OK [maildrop locked and ready] | |
404 ;; -ERR [invalid password] | |
405 ;; -ERR [unable to lock maildrop] | |
406 | |
407 ;;; TRANSACTION STATE | |
408 | |
409 ;; STAT | |
410 ;; Arguments: none | |
411 ;; Restrictions: transaction state | |
412 ;; Possible responses: | |
413 ;; +OK nn mm [# of messages, size of maildrop] | |
414 | |
415 ;; LIST [msg] | |
416 ;; Arguments: a message-id (optional) | |
417 ;; Restrictions: transaction state; msg must not be deleted | |
418 ;; Possible responses: | |
419 ;; +OK [scan listing follows] | |
420 ;; -ERR [no such message] | |
421 | |
422 ;; RETR msg | |
423 ;; Arguments: a message-id (required) | |
424 ;; Restrictions: transaction state; msg must not be deleted | |
425 ;; Possible responses: | |
426 ;; +OK [message contents follow] | |
427 ;; -ERR [no such message] | |
428 | |
429 ;; DELE msg | |
430 ;; Arguments: a message-id (required) | |
431 ;; Restrictions: transaction state; msg must not be deleted | |
432 ;; Possible responses: | |
433 ;; +OK [message deleted] | |
434 ;; -ERR [no such message] | |
435 | |
436 ;; NOOP | |
437 ;; Arguments: none | |
438 ;; Restrictions: transaction state | |
439 ;; Possible responses: | |
440 ;; +OK | |
441 | |
442 ;; LAST | |
443 ;; Arguments: none | |
444 ;; Restrictions: transaction state | |
445 ;; Possible responses: | |
446 ;; +OK nn [highest numbered message accessed] | |
447 | |
448 ;; RSET | |
449 ;; Arguments: none | |
450 ;; Restrictions: transaction state | |
451 ;; Possible responses: | |
452 ;; +OK [all delete marks removed] | |
453 | |
454 ;;; UPDATE STATE | |
455 | |
456 ;; QUIT | |
457 ;; Arguments: none | |
458 ;; Restrictions: none | |
459 ;; Possible responses: | |
460 ;; +OK [TCP connection closed] |