(GC_MARK_STACK): Define to GC_MAKE_GCPROS_NOOPS.
[bpt/emacs.git] / lisp / gnus / pop3.el
CommitLineData
eec82323
LMI
1;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
16409b0b
GM
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000
4;; Free Software Foundation, Inc.
eec82323
LMI
5
6;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
dd5da9b8
DL
7;; Maintainer: FSF
8;; Keywords: mail
eec82323
LMI
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
26
27;;; Commentary:
28
29;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
30;; are implemented. The LIST command has not been implemented due to lack
31;; of actual usefulness.
32;; The optional POP3 command TOP has not been implemented.
33
34;; This program was inspired by Kyle E. Jones's vm-pop program.
35
36;;; Code:
37
38(require 'mail-utils)
eec82323 39
6748645f 40(defvar pop3-maildrop (or (user-login-name) (getenv "LOGNAME") (getenv "USER") nil)
eec82323
LMI
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.
54Defaults to 'pass, for the standard USER/PASS authentication. Other valid
55values are 'apop.")
56
57(defvar pop3-timestamp nil
58 "Timestamp returned when initially connected to the POP server.
59Used for APOP authentication.")
60
61(defvar pop3-read-point nil)
62(defvar pop3-debug nil)
63
64(defun pop3-movemail (&optional crashbox)
65 "Transfer contents of a maildrop to the specified CRASHBOX."
66 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
67 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
68 (crashbuf (get-buffer-create " *pop3-retr*"))
69 (n 1)
6748645f
LMI
70 message-count
71 (pop3-password pop3-password)
72 )
eec82323
LMI
73 ;; for debugging only
74 (if pop3-debug (switch-to-buffer (process-buffer process)))
6748645f
LMI
75 ;; query for password
76 (if (and pop3-password-required (not pop3-password))
77 (setq pop3-password
78 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
eec82323
LMI
79 (cond ((equal 'apop pop3-authentication-scheme)
80 (pop3-apop process pop3-maildrop))
81 ((equal 'pass pop3-authentication-scheme)
82 (pop3-user process pop3-maildrop)
83 (pop3-pass process))
dd5da9b8 84 (t (error "Invalid POP3 authentication scheme")))
eec82323 85 (setq message-count (car (pop3-stat process)))
dd5da9b8
DL
86 (unwind-protect
87 (while (<= n message-count)
88 (message (format "Retrieving message %d of %d from %s..."
89 n message-count pop3-mailhost))
90 (pop3-retr process n crashbuf)
91 (save-excursion
92 (set-buffer crashbuf)
4c4b227a 93 (let ((coding-system-for-write 'binary))
a9c810bf 94 (write-region (point-min) (point-max) crashbox t 'nomesg))
dd5da9b8
DL
95 (set-buffer (process-buffer process))
96 (while (> (buffer-size) 5000)
97 (goto-char (point-min))
98 (forward-line 50)
99 (delete-region (point-min) (point))))
100 (pop3-dele process n)
101 (setq n (+ 1 n))
102 (if pop3-debug (sit-for 1) (sit-for 0.1))
103 )
104 (pop3-quit process))
eec82323
LMI
105 (kill-buffer crashbuf)
106 )
dd5da9b8 107 t)
eec82323
LMI
108
109(defun pop3-open-server (mailhost port)
dd5da9b8 110 "Open TCP connection to MAILHOST on PORT.
eec82323 111Returns the process associated with the connection."
4cac7481 112 (let ((coding-system-for-read 'binary)
6748645f 113 (coding-system-for-write 'binary)
4cac7481 114 process)
eec82323 115 (save-excursion
4cac7481
DL
116 (set-buffer (get-buffer-create (concat " trace of POP session to "
117 mailhost)))
6748645f
LMI
118 (erase-buffer)
119 (setq pop3-read-point (point-min))
4cac7481
DL
120 (setq process (open-network-stream "POP"(current-buffer) mailhost port))
121 (let ((response (pop3-read-response process t)))
122 (setq pop3-timestamp
123 (substring response (or (string-match "<" response) 0)
124 (+ 1 (or (string-match ">" response) -1)))))
125 process)))
eec82323
LMI
126
127;; Support functions
128
129(defun pop3-process-filter (process output)
130 (save-excursion
131 (set-buffer (process-buffer process))
132 (goto-char (point-max))
133 (insert output)))
134
135(defun pop3-send-command (process command)
136 (set-buffer (process-buffer process))
137 (goto-char (point-max))
138;; (if (= (aref command 0) ?P)
139;; (insert "PASS <omitted>\r\n")
140;; (insert command "\r\n"))
141 (setq pop3-read-point (point))
142 (goto-char (point-max))
dd5da9b8 143 (process-send-string process (concat command "\r\n"))
eec82323
LMI
144 )
145
146(defun pop3-read-response (process &optional return)
147 "Read the response from the server.
148Return 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))
a8151ef7 155 (accept-process-output process 3)
eec82323
LMI
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
eec82323
LMI
169(defvar pop3-read-passwd nil)
170(defun pop3-read-passwd (prompt)
171 (if (not pop3-read-passwd)
ff88af37 172 (if (fboundp 'read-passwd)
eec82323 173 (setq pop3-read-passwd 'read-passwd)
ff88af37
RS
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))))
eec82323
LMI
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
daaeed87
DL
192(eval-when-compile (defvar parse-time-months))
193
194;; Copied from message-make-date.
195(defun pop3-make-date (&optional now)
196 "Make a valid date header.
197If NOW, use that time instead."
198 (require 'parse-time)
199 (let* ((now (or now (current-time)))
200 (zone (nth 8 (decode-time now)))
201 (sign "+"))
202 (when (< zone 0)
203 (setq sign "-")
204 (setq zone (- zone)))
205 (concat
206 (format-time-string "%d" now)
207 ;; The month name of the %b spec is locale-specific. Pfff.
208 (format " %s "
209 (capitalize (car (rassoc (nth 4 (decode-time now))
210 parse-time-months))))
211 (format-time-string "%Y %H:%M:%S " now)
212 ;; We do all of this because XEmacs doesn't have the %z spec.
213 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
214
eec82323
LMI
215(defun pop3-munge-message-separator (start end)
216 "Check to see if a message separator exists. If not, generate one."
217 (save-excursion
218 (save-restriction
219 (narrow-to-region start end)
220 (goto-char (point-min))
221 (if (not (or (looking-at "From .?") ; Unix mail
222 (looking-at "\001\001\001\001\n") ; MMDF
223 (looking-at "BABYL OPTIONS:") ; Babyl
224 ))
225 (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
4cac7481 226 (date (split-string (or (mail-fetch-field "Date")
daaeed87 227 (pop3-make-date))
4cac7481 228 " "))
eec82323
LMI
229 (From_))
230 ;; sample date formats I have seen
231 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
232 ;; Date: 08 Jul 1996 23:22:24 -0400
233 ;; should be
234 ;; Tue Jul 9 09:04:21 1996
235 (setq date
236 (cond ((string-match "[A-Z]" (nth 0 date))
237 (format "%s %s %s %s %s"
238 (nth 0 date) (nth 2 date) (nth 1 date)
239 (nth 4 date) (nth 3 date)))
240 (t
241 ;; this really needs to be better but I don't feel
242 ;; like writing a date to day converter.
243 (format "Sun %s %s %s %s"
244 (nth 1 date) (nth 0 date)
245 (nth 3 date) (nth 2 date)))
246 ))
247 (setq From_ (format "\nFrom %s %s\n" from date))
248 (while (string-match "," From_)
249 (setq From_ (concat (substring From_ 0 (match-beginning 0))
250 (substring From_ (match-end 0)))))
251 (goto-char (point-min))
dd5da9b8 252 (insert From_)
daaeed87
DL
253 (if (search-forward "\n\n" nil t)
254 nil
255 (goto-char (point-max))
256 (insert "\n"))
dd5da9b8
DL
257 (narrow-to-region (point) (point-max))
258 (let ((size (- (point-max) (point-min))))
259 (goto-char (point-min))
260 (widen)
261 (forward-line -1)
262 (insert (format "Content-Length: %s\n" size)))
263 )))))
eec82323
LMI
264
265;; The Command Set
266
267;; AUTHORIZATION STATE
268
269(defun pop3-user (process user)
270 "Send USER information to POP3 server."
271 (pop3-send-command process (format "USER %s" user))
272 (let ((response (pop3-read-response process t)))
273 (if (not (and response (string-match "+OK" response)))
274 (error (format "USER %s not valid." user)))))
275
276(defun pop3-pass (process)
277 "Send authentication information to the server."
6748645f
LMI
278 (pop3-send-command process (format "PASS %s" pop3-password))
279 (let ((response (pop3-read-response process t)))
280 (if (not (and response (string-match "+OK" response)))
281 (pop3-quit process))))
282
283(defun pop3-apop (process user)
284 "Send alternate authentication information to the server."
eec82323
LMI
285 (let ((pass pop3-password))
286 (if (and pop3-password-required (not pass))
287 (setq pass
288 (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
289 (if pass
6748645f
LMI
290 (let ((hash (pop3-md5 (concat pop3-timestamp pass))))
291 (pop3-send-command process (format "APOP %s %s" user hash))
eec82323
LMI
292 (let ((response (pop3-read-response process t)))
293 (if (not (and response (string-match "+OK" response)))
294 (pop3-quit process)))))
295 ))
296
6748645f
LMI
297;; TRANSACTION STATE
298
daaeed87
DL
299(eval-and-compile
300 (if (fboundp 'md5)
301 (defalias 'pop3-md5 'md5)
302 (defvar pop3-md5-program "md5"
303 "*Program to encode its input in MD5.")
304
305 (defun pop3-md5 (string)
306 (with-temp-buffer
307 (insert string)
308 (call-process-region (point-min) (point-max)
309 pop3-md5-program
310 t (current-buffer) nil)
311 ;; The meaningful output is the first 32 characters.
312 ;; Don't return the newline that follows them!
313 (buffer-substring 1 33)))))
838bf473 314
eec82323
LMI
315(defun pop3-stat (process)
316 "Return the number of messages in the maildrop and the maildrop's size."
317 (pop3-send-command process "STAT")
318 (let ((response (pop3-read-response process t)))
4cac7481
DL
319 (list (string-to-int (nth 1 (split-string response " ")))
320 (string-to-int (nth 2 (split-string response " "))))
eec82323
LMI
321 ))
322
323(defun pop3-list (process &optional msg)
324 "Scan listing of available messages.
325This function currently does nothing.")
326
327(defun pop3-retr (process msg crashbuf)
328 "Retrieve message-id MSG to buffer CRASHBUF."
329 (pop3-send-command process (format "RETR %s" msg))
330 (pop3-read-response process)
331 (let ((start pop3-read-point) end)
332 (save-excursion
333 (set-buffer (process-buffer process))
334 (while (not (re-search-forward "^\\.\r\n" nil t))
a8151ef7 335 (accept-process-output process 3)
eec82323 336 ;; bill@att.com ... to save wear and tear on the heap
6748645f
LMI
337 ;; uncommented because the condensed version below is a problem for
338 ;; some.
eec82323
LMI
339 (if (> (buffer-size) 20000) (sleep-for 1))
340 (if (> (buffer-size) 50000) (sleep-for 1))
341 (if (> (buffer-size) 100000) (sleep-for 1))
342 (if (> (buffer-size) 200000) (sleep-for 1))
343 (if (> (buffer-size) 500000) (sleep-for 1))
344 ;; bill@att.com
6748645f
LMI
345 ;; condensed into:
346 ;; (sometimes causes problems for really large messages.)
347; (if (> (buffer-size) 20000) (sleep-for (/ (buffer-size) 20000)))
eec82323
LMI
348 (goto-char start))
349 (setq pop3-read-point (point-marker))
350;; this code does not seem to work for some POP servers...
351;; and I cannot figure out why not.
352; (goto-char (match-beginning 0))
353; (backward-char 2)
354; (if (not (looking-at "\r\n"))
355; (insert "\r\n"))
356; (re-search-forward "\\.\r\n")
357 (goto-char (match-beginning 0))
358 (setq end (point-marker))
359 (pop3-clean-region start end)
360 (pop3-munge-message-separator start end)
361 (save-excursion
362 (set-buffer crashbuf)
363 (erase-buffer))
364 (copy-to-buffer crashbuf start end)
365 (delete-region start end)
366 )))
367
368(defun pop3-dele (process msg)
369 "Mark message-id MSG as deleted."
370 (pop3-send-command process (format "DELE %s" msg))
371 (pop3-read-response process))
372
373(defun pop3-noop (process msg)
374 "No-operation."
375 (pop3-send-command process "NOOP")
376 (pop3-read-response process))
377
378(defun pop3-last (process)
379 "Return highest accessed message-id number for the session."
380 (pop3-send-command process "LAST")
381 (let ((response (pop3-read-response process t)))
4cac7481 382 (string-to-int (nth 1 (split-string response " ")))
eec82323
LMI
383 ))
384
385(defun pop3-rset (process)
386 "Remove all delete marks from current maildrop."
387 (pop3-send-command process "RSET")
388 (pop3-read-response process))
389
390;; UPDATE
391
392(defun pop3-quit (process)
393 "Close connection to POP3 server.
394Tell server to remove all messages marked as deleted, unlock the maildrop,
395and close the connection."
396 (pop3-send-command process "QUIT")
397 (pop3-read-response process t)
398 (if process
399 (save-excursion
400 (set-buffer (process-buffer process))
401 (goto-char (point-max))
402 (delete-process process))))
403\f
404;; Summary of POP3 (Post Office Protocol version 3) commands and responses
405
406;;; AUTHORIZATION STATE
407
408;; Initial TCP connection
409;; Arguments: none
410;; Restrictions: none
411;; Possible responses:
412;; +OK [POP3 server ready]
413
414;; USER name
415;; Arguments: a server specific user-id (required)
416;; Restrictions: authorization state [after unsuccessful USER or PASS
417;; Possible responses:
418;; +OK [valid user-id]
419;; -ERR [invalid user-id]
420
421;; PASS string
422;; Arguments: a server/user-id specific password (required)
423;; Restrictions: authorization state, after successful USER
424;; Possible responses:
425;; +OK [maildrop locked and ready]
426;; -ERR [invalid password]
427;; -ERR [unable to lock maildrop]
428
429;;; TRANSACTION STATE
430
431;; STAT
432;; Arguments: none
433;; Restrictions: transaction state
434;; Possible responses:
435;; +OK nn mm [# of messages, size of maildrop]
436
437;; LIST [msg]
438;; Arguments: a message-id (optional)
439;; Restrictions: transaction state; msg must not be deleted
440;; Possible responses:
441;; +OK [scan listing follows]
442;; -ERR [no such message]
443
444;; RETR msg
445;; Arguments: a message-id (required)
446;; Restrictions: transaction state; msg must not be deleted
447;; Possible responses:
448;; +OK [message contents follow]
449;; -ERR [no such message]
450
451;; DELE msg
452;; Arguments: a message-id (required)
453;; Restrictions: transaction state; msg must not be deleted
454;; Possible responses:
455;; +OK [message deleted]
456;; -ERR [no such message]
457
458;; NOOP
459;; Arguments: none
460;; Restrictions: transaction state
461;; Possible responses:
462;; +OK
463
464;; LAST
465;; Arguments: none
466;; Restrictions: transaction state
467;; Possible responses:
468;; +OK nn [highest numbered message accessed]
469
470;; RSET
471;; Arguments: none
472;; Restrictions: transaction state
473;; Possible responses:
474;; +OK [all delete marks removed]
475
476;;; UPDATE STATE
477
478;; QUIT
479;; Arguments: none
480;; Restrictions: none
481;; Possible responses:
482;; +OK [TCP connection closed]
dd5da9b8
DL
483
484(provide 'pop3)
485
486;;; pop3.el ends here