nnimap.el (nnimap-open-connection): Look for the "imaps" entry in the .authinfo if...
[bpt/emacs.git] / lisp / gnus / pop3.el
CommitLineData
eec82323
LMI
1;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
e84b4b86 3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
114f9c96 4;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 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
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
eec82323
LMI
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
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
24
25;;; Commentary:
26
27;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
28;; are implemented. The LIST command has not been implemented due to lack
29;; of actual usefulness.
30;; The optional POP3 command TOP has not been implemented.
31
32;; This program was inspired by Kyle E. Jones's vm-pop program.
33
34;;; Code:
35
36(require 'mail-utils)
9efa445f 37(defvar parse-time-months)
eec82323 38
e62e7654 39(defgroup pop3 nil
62a3378e 40 "Post Office Protocol."
e62e7654
MB
41 :group 'mail
42 :group 'mail-source)
43
44(defcustom pop3-maildrop (or (user-login-name)
45 (getenv "LOGNAME")
46 (getenv "USER"))
47 "*POP3 maildrop."
bf247b6e 48 :version "22.1" ;; Oort Gnus
e62e7654
MB
49 :type 'string
50 :group 'pop3)
51
52(defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
53 "pop3")
54 "*POP3 mailhost."
bf247b6e 55 :version "22.1" ;; Oort Gnus
e62e7654
MB
56 :type 'string
57 :group 'pop3)
58
59(defcustom pop3-port 110
60 "*POP3 port."
bf247b6e 61 :version "22.1" ;; Oort Gnus
e62e7654
MB
62 :type 'number
63 :group 'pop3)
64
65(defcustom pop3-password-required t
66 "*Non-nil if a password is required when connecting to POP server."
bf247b6e 67 :version "22.1" ;; Oort Gnus
e62e7654
MB
68 :type 'boolean
69 :group 'pop3)
70
71;; Should this be customizable?
eec82323
LMI
72(defvar pop3-password nil
73 "*Password to use when connecting to POP server.")
74
e62e7654 75(defcustom pop3-authentication-scheme 'pass
eec82323 76 "*POP3 authentication scheme.
996aa8c1
MB
77Defaults to `pass', for the standard USER/PASS authentication. The other
78valid value is 'apop'."
79 :type '(choice (const :tag "Normal user/password" pass)
e62e7654 80 (const :tag "APOP" apop))
996aa8c1 81 :version "22.1" ;; Oort Gnus
e62e7654
MB
82 :group 'pop3)
83
84(defcustom pop3-leave-mail-on-server nil
8903a9c8
MB
85 "*Non-nil if the mail is to be left on the POP server after fetching.
86
996aa8c1
MB
87If `pop3-leave-mail-on-server' is non-nil the mail is to be left
88on the POP server after fetching. Note that POP servers maintain
89no state information between sessions, so what the client
90believes is there and what is actually there may not match up.
91If they do not, then you may get duplicate mails or the whole
92thing can fall apart and leave you with a corrupt mailbox."
b110774a
MB
93 ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org:
94 ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de
95 ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org
96 ;; Any volunteer to re-implement this?
bf247b6e 97 :version "22.1" ;; Oort Gnus
e62e7654
MB
98 :type 'boolean
99 :group 'pop3)
3e7b210c 100
eec82323
LMI
101(defvar pop3-timestamp nil
102 "Timestamp returned when initially connected to the POP server.
103Used for APOP authentication.")
104
105(defvar pop3-read-point nil)
106(defvar pop3-debug nil)
107
e3e955fe
MB
108;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
109;; comments there for explanations about the values.
110
111(eval-and-compile
112 (if (and (fboundp 'nnheader-accept-process-output)
113 (boundp 'nnheader-read-timeout))
114 (defalias 'pop3-accept-process-output 'nnheader-accept-process-output)
115 ;; Borrowed from `nnheader.el':
116 (defvar pop3-read-timeout
f5ec697d 117 (if (string-match "windows-nt\\|os/2\\|cygwin"
e3e955fe
MB
118 (symbol-name system-type))
119 1.0
120 0.01)
121 "How long pop3 should wait between checking for the end of output.
8903a9c8 122Shorter values mean quicker response, but are more CPU intensive.")
e3e955fe
MB
123 (defun pop3-accept-process-output (process)
124 (accept-process-output
125 process
126 (truncate pop3-read-timeout)
127 (truncate (* (- pop3-read-timeout
128 (truncate pop3-read-timeout))
129 1000))))))
6459e35e 130
a2bb410e
LMI
131(defun pop3-streaming-movemail (file)
132 "Transfer contents of a maildrop to the specified FILE.
133Use streaming commands."
eec82323 134 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
a2bb410e
LMI
135 message-count message-total-size)
136 (pop3-logon process)
137 (with-current-buffer (process-buffer process)
138 (let ((size (pop3-stat process)))
139 (setq message-count (car size)
140 message-total-size (cadr size)))
141 (when (plusp message-count)
142 (pop3-send-streaming-command
143 process "RETR" message-count message-total-size)
144 (pop3-write-to-file file)
145 (unless pop3-leave-mail-on-server
146 (pop3-send-streaming-command
ff3eb82e
LMI
147 process "DELE" message-count nil))))
148 (pop3-quit process)))
a2bb410e
LMI
149
150(defun pop3-send-streaming-command (process command count total-size)
151 (erase-buffer)
152 (let ((i 1))
530b8957 153 (while (>= count i)
a2bb410e
LMI
154 (process-send-string process (format "%s %d\r\n" command i))
155 ;; Only do 100 messages at a time to avoid pipe stalls.
156 (when (zerop (% i 100))
157 (pop3-wait-for-messages process i total-size))
158 (incf i)))
159 (pop3-wait-for-messages process count total-size))
160
161(defun pop3-wait-for-messages (process count total-size)
162 (while (< (pop3-number-of-responses total-size) count)
163 (when total-size
164 (message "pop3 retrieved %dKB (%d%%)"
165 (truncate (/ (buffer-size) 1000))
166 (truncate (* (/ (* (buffer-size) 1.0)
167 total-size) 100))))
168 (nnheader-accept-process-output process)))
169
170(defun pop3-write-to-file (file)
171 (let ((pop-buffer (current-buffer))
172 (start (point-min))
173 beg end
174 temp-buffer)
175 (with-temp-buffer
176 (setq temp-buffer (current-buffer))
177 (with-current-buffer pop-buffer
178 (goto-char (point-min))
179 (while (re-search-forward "^\\+OK" nil t)
180 (forward-line 1)
181 (setq beg (point))
182 (when (re-search-forward "^\\.\r?\n" nil t)
183 (setq start (point))
184 (forward-line -1)
185 (setq end (point)))
186 (with-current-buffer temp-buffer
187 (goto-char (point-max))
188 (let ((hstart (point)))
189 (insert-buffer-substring pop-buffer beg end)
190 (pop3-clean-region hstart (point))
191 (goto-char (point-max))
192 (pop3-munge-message-separator hstart (point))
193 (goto-char (point-max))))))
194 (let ((coding-system-for-write 'binary))
195 (goto-char (point-min))
196 ;; Check whether something inserted a newline at the start and
197 ;; delete it.
198 (when (eolp)
199 (delete-char 1))
530b8957 200 (write-region (point-min) (point-max) file nil 'nomesg)))))
a2bb410e
LMI
201
202(defun pop3-number-of-responses (endp)
203 (let ((responses 0))
204 (save-excursion
205 (goto-char (point-min))
206 (while (or (and (re-search-forward "^\\+OK " nil t)
207 (or (not endp)
208 (re-search-forward "^\\.\r?\n" nil t)))
209 (re-search-forward "^-ERR " nil t))
210 (incf responses)))
211 responses))
212
213(defun pop3-logon (process)
214 (let ((pop3-password pop3-password))
eec82323
LMI
215 ;; for debugging only
216 (if pop3-debug (switch-to-buffer (process-buffer process)))
6748645f
LMI
217 ;; query for password
218 (if (and pop3-password-required (not pop3-password))
219 (setq pop3-password
23f87bed 220 (read-passwd (format "Password for %s: " pop3-maildrop))))
eec82323
LMI
221 (cond ((equal 'apop pop3-authentication-scheme)
222 (pop3-apop process pop3-maildrop))
223 ((equal 'pass pop3-authentication-scheme)
224 (pop3-user process pop3-maildrop)
225 (pop3-pass process))
a2bb410e
LMI
226 (t (error "Invalid POP3 authentication scheme")))))
227
228(defun pop3-movemail (&optional crashbox)
229 "Transfer contents of a maildrop to the specified CRASHBOX."
230 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
231 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
232 (crashbuf (get-buffer-create " *pop3-retr*"))
233 (n 1)
234 message-count
235 message-sizes)
236 (pop3-logon process)
eec82323 237 (setq message-count (car (pop3-stat process)))
a2bb410e 238 (when (> message-count 0)
ec7995fa 239 (setq message-sizes (pop3-list process)))
dd5da9b8
DL
240 (unwind-protect
241 (while (<= n message-count)
34e03853
LMI
242 (message "Retrieving message %d of %d from %s... (%.1fk)"
243 n message-count pop3-mailhost
244 (/ (cdr (assoc n message-sizes))
245 1024.0))
dd5da9b8
DL
246 (pop3-retr process n crashbuf)
247 (save-excursion
248 (set-buffer crashbuf)
4c4b227a 249 (let ((coding-system-for-write 'binary))
a9c810bf 250 (write-region (point-min) (point-max) crashbox t 'nomesg))
dd5da9b8 251 (set-buffer (process-buffer process))
ef45ee6f 252 (erase-buffer))
3e7b210c
SS
253 (unless pop3-leave-mail-on-server
254 (pop3-dele process n))
dd5da9b8 255 (setq n (+ 1 n))
e3e955fe 256 (pop3-accept-process-output process))
996aa8c1
MB
257 (when (and pop3-leave-mail-on-server
258 (> n 1))
259 (message "pop3.el doesn't support UIDL. Setting `pop3-leave-mail-on-server'
260to %s might not give the result you'd expect." pop3-leave-mail-on-server)
261 (sit-for 1))
dd5da9b8 262 (pop3-quit process))
996aa8c1 263 (kill-buffer crashbuf))
dd5da9b8 264 t)
eec82323 265
619ac84f
SZ
266(defun pop3-get-message-count ()
267 "Return the number of messages in the maildrop."
268 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
269 message-count
e62e7654 270 (pop3-password pop3-password))
619ac84f
SZ
271 ;; for debugging only
272 (if pop3-debug (switch-to-buffer (process-buffer process)))
273 ;; query for password
274 (if (and pop3-password-required (not pop3-password))
275 (setq pop3-password
23f87bed 276 (read-passwd (format "Password for %s: " pop3-maildrop))))
619ac84f
SZ
277 (cond ((equal 'apop pop3-authentication-scheme)
278 (pop3-apop process pop3-maildrop))
279 ((equal 'pass pop3-authentication-scheme)
280 (pop3-user process pop3-maildrop)
281 (pop3-pass process))
55535639 282 (t (error "Invalid POP3 authentication scheme")))
619ac84f
SZ
283 (setq message-count (car (pop3-stat process)))
284 (pop3-quit process)
285 message-count))
286
01c52d31
MB
287(autoload 'open-tls-stream "tls")
288(autoload 'starttls-open-stream "starttls")
289(autoload 'starttls-negotiate "starttls") ; avoid warning
290
291(defcustom pop3-stream-type nil
292 "*Transport security type for POP3 connexions.
293This may be either nil (plain connexion), `ssl' (use an
294SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
295to turn on TLS security after opening the stream). However, if
296this is nil, `ssl' is assumed for connexions to port
297995 (pop3s)."
330f707b 298 :version "23.1" ;; No Gnus
01c52d31
MB
299 :group 'pop3
300 :type '(choice (const :tag "Plain" nil)
301 (const :tag "SSL/TLS" ssl)
302 (const starttls)))
303
c6faacb4
KY
304(eval-and-compile
305 (if (fboundp 'set-process-query-on-exit-flag)
306 (defalias 'pop3-set-process-query-on-exit-flag
307 'set-process-query-on-exit-flag)
308 (defalias 'pop3-set-process-query-on-exit-flag
309 'process-kill-without-query)))
310
eec82323 311(defun pop3-open-server (mailhost port)
dd5da9b8 312 "Open TCP connection to MAILHOST on PORT.
eec82323 313Returns the process associated with the connection."
4cac7481 314 (let ((coding-system-for-read 'binary)
6748645f 315 (coding-system-for-write 'binary)
4cac7481 316 process)
eec82323 317 (save-excursion
4cac7481
DL
318 (set-buffer (get-buffer-create (concat " trace of POP session to "
319 mailhost)))
6748645f
LMI
320 (erase-buffer)
321 (setq pop3-read-point (point-min))
01c52d31
MB
322 (setq process
323 (cond
324 ((or (eq pop3-stream-type 'ssl)
325 (and (not pop3-stream-type) (member port '(995 "pop3s"))))
326 ;; gnutls-cli, openssl don't accept service names
327 (if (or (equal port "pop3s")
328 (null port))
329 (setq port 995))
330 (let ((process (open-tls-stream "POP" (current-buffer)
331 mailhost port)))
332 (when process
333 ;; There's a load of info printed that needs deleting.
1428d46b
MB
334 (let ((again 't))
335 ;; repeat until
336 ;; - either we received the +OK line
337 ;; - or accept-process-output timed out without getting
338 ;; anything
339 (while (and again
340 (setq again (memq (process-status process)
341 '(open run))))
342 (setq again (pop3-accept-process-output process))
343 (goto-char (point-max))
344 (forward-line -1)
345 (cond ((looking-at "\\+OK")
346 (setq again nil)
347 (delete-region (point-min) (point)))
348 ((not again)
01c52d31 349 (pop3-quit process)
1428d46b 350 (error "POP SSL connexion failed")))))
01c52d31
MB
351 process)))
352 ((eq pop3-stream-type 'starttls)
353 ;; gnutls-cli, openssl don't accept service names
354 (if (equal port "pop3")
355 (setq port 110))
356 (let ((process (starttls-open-stream "POP" (current-buffer)
357 mailhost (or port 110))))
358 (pop3-send-command process "STLS")
359 (let ((response (pop3-read-response process t)))
360 (if (and response (string-match "+OK" response))
361 (starttls-negotiate process)
362 (pop3-quit process)
363 (error "POP server doesn't support starttls")))
364 process))
c9fc72fa 365 (t
01c52d31 366 (open-network-stream "POP" (current-buffer) mailhost port))))
4cac7481
DL
367 (let ((response (pop3-read-response process t)))
368 (setq pop3-timestamp
369 (substring response (or (string-match "<" response) 0)
370 (+ 1 (or (string-match ">" response) -1)))))
c6faacb4 371 (pop3-set-process-query-on-exit-flag process nil)
4cac7481 372 process)))
eec82323
LMI
373
374;; Support functions
375
eec82323 376(defun pop3-send-command (process command)
e62e7654
MB
377 (set-buffer (process-buffer process))
378 (goto-char (point-max))
379 ;; (if (= (aref command 0) ?P)
380 ;; (insert "PASS <omitted>\r\n")
381 ;; (insert command "\r\n"))
382 (setq pop3-read-point (point))
383 (goto-char (point-max))
384 (process-send-string process (concat command "\r\n")))
eec82323
LMI
385
386(defun pop3-read-response (process &optional return)
387 "Read the response from the server.
388Return the response string if optional second argument is non-nil."
389 (let ((case-fold-search nil)
390 match-end)
391 (save-excursion
392 (set-buffer (process-buffer process))
393 (goto-char pop3-read-point)
23f87bed
MB
394 (while (and (memq (process-status process) '(open run))
395 (not (search-forward "\r\n" nil t)))
8903a9c8 396 (pop3-accept-process-output process)
eec82323
LMI
397 (goto-char pop3-read-point))
398 (setq match-end (point))
399 (goto-char pop3-read-point)
400 (if (looking-at "-ERR")
26b4a51d 401 (error "%s" (buffer-substring (point) (- match-end 2)))
eec82323
LMI
402 (if (not (looking-at "+OK"))
403 (progn (setq pop3-read-point match-end) nil)
404 (setq pop3-read-point match-end)
405 (if return
406 (buffer-substring (point) match-end)
407 t)
408 )))))
409
eec82323
LMI
410(defun pop3-clean-region (start end)
411 (setq end (set-marker (make-marker) end))
412 (save-excursion
413 (goto-char start)
414 (while (and (< (point) end) (search-forward "\r\n" end t))
415 (replace-match "\n" t t))
416 (goto-char start)
417 (while (and (< (point) end) (re-search-forward "^\\." end t))
418 (replace-match "" t t)
419 (forward-char)))
420 (set-marker end nil))
421
daaeed87
DL
422;; Copied from message-make-date.
423(defun pop3-make-date (&optional now)
424 "Make a valid date header.
425If NOW, use that time instead."
426 (require 'parse-time)
427 (let* ((now (or now (current-time)))
428 (zone (nth 8 (decode-time now)))
429 (sign "+"))
430 (when (< zone 0)
431 (setq sign "-")
432 (setq zone (- zone)))
433 (concat
434 (format-time-string "%d" now)
435 ;; The month name of the %b spec is locale-specific. Pfff.
436 (format " %s "
437 (capitalize (car (rassoc (nth 4 (decode-time now))
438 parse-time-months))))
439 (format-time-string "%Y %H:%M:%S " now)
440 ;; We do all of this because XEmacs doesn't have the %z spec.
441 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
442
eec82323
LMI
443(defun pop3-munge-message-separator (start end)
444 "Check to see if a message separator exists. If not, generate one."
445 (save-excursion
446 (save-restriction
447 (narrow-to-region start end)
448 (goto-char (point-min))
449 (if (not (or (looking-at "From .?") ; Unix mail
450 (looking-at "\001\001\001\001\n") ; MMDF
451 (looking-at "BABYL OPTIONS:") ; Babyl
452 ))
ae496852
SZ
453 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
454 (tdate (mail-fetch-field "Date"))
455 (date (split-string (or (and tdate
456 (not (string= "" tdate))
457 tdate)
458 (pop3-make-date))
459 " "))
460 (From_))
eec82323
LMI
461 ;; sample date formats I have seen
462 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
463 ;; Date: 08 Jul 1996 23:22:24 -0400
464 ;; should be
465 ;; Tue Jul 9 09:04:21 1996
996aa8c1
MB
466
467 ;; Fixme: This should use timezone on the date field contents.
eec82323 468 (setq date
ae496852 469 (cond ((not date)
23f87bed 470 "Tue Jan 1 00:00:0 1900")
ae496852 471 ((string-match "[A-Z]" (nth 0 date))
eec82323
LMI
472 (format "%s %s %s %s %s"
473 (nth 0 date) (nth 2 date) (nth 1 date)
474 (nth 4 date) (nth 3 date)))
475 (t
476 ;; this really needs to be better but I don't feel
477 ;; like writing a date to day converter.
478 (format "Sun %s %s %s %s"
479 (nth 1 date) (nth 0 date)
480 (nth 3 date) (nth 2 date)))
481 ))
482 (setq From_ (format "\nFrom %s %s\n" from date))
483 (while (string-match "," From_)
484 (setq From_ (concat (substring From_ 0 (match-beginning 0))
485 (substring From_ (match-end 0)))))
486 (goto-char (point-min))
dd5da9b8 487 (insert From_)
daaeed87
DL
488 (if (search-forward "\n\n" nil t)
489 nil
490 (goto-char (point-max))
491 (insert "\n"))
a2bb410e 492 (let ((size (- (point-max) (point))))
dd5da9b8
DL
493 (forward-line -1)
494 (insert (format "Content-Length: %s\n" size)))
495 )))))
eec82323
LMI
496
497;; The Command Set
498
499;; AUTHORIZATION STATE
500
501(defun pop3-user (process user)
502 "Send USER information to POP3 server."
503 (pop3-send-command process (format "USER %s" user))
504 (let ((response (pop3-read-response process t)))
505 (if (not (and response (string-match "+OK" response)))
2c2b732f 506 (error "USER %s not valid" user))))
eec82323
LMI
507
508(defun pop3-pass (process)
509 "Send authentication information to the server."
6748645f
LMI
510 (pop3-send-command process (format "PASS %s" pop3-password))
511 (let ((response (pop3-read-response process t)))
512 (if (not (and response (string-match "+OK" response)))
513 (pop3-quit process))))
514
515(defun pop3-apop (process user)
516 "Send alternate authentication information to the server."
eec82323
LMI
517 (let ((pass pop3-password))
518 (if (and pop3-password-required (not pass))
519 (setq pass
23f87bed 520 (read-passwd (format "Password for %s: " pop3-maildrop))))
eec82323 521 (if pass
01c52d31 522 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
6748645f 523 (pop3-send-command process (format "APOP %s %s" user hash))
eec82323
LMI
524 (let ((response (pop3-read-response process t)))
525 (if (not (and response (string-match "+OK" response)))
526 (pop3-quit process)))))
527 ))
528
6748645f
LMI
529;; TRANSACTION STATE
530
eec82323
LMI
531(defun pop3-stat (process)
532 "Return the number of messages in the maildrop and the maildrop's size."
533 (pop3-send-command process "STAT")
534 (let ((response (pop3-read-response process t)))
e9bd5782
MB
535 (list (string-to-number (nth 1 (split-string response " ")))
536 (string-to-number (nth 2 (split-string response " "))))
eec82323
LMI
537 ))
538
539(defun pop3-list (process &optional msg)
ec7995fa
KY
540 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
541Otherwise, return the size of the message-id MSG"
c9fc72fa 542 (pop3-send-command process (if msg
ec7995fa
KY
543 (format "LIST %d" msg)
544 "LIST"))
545 (let ((response (pop3-read-response process t)))
546 (if msg
547 (string-to-number (nth 2 (split-string response " ")))
548 (let ((start pop3-read-point) end)
549 (save-excursion
550 (set-buffer (process-buffer process))
551 (while (not (re-search-forward "^\\.\r\n" nil t))
552 (pop3-accept-process-output process)
553 (goto-char start))
554 (setq pop3-read-point (point-marker))
555 (goto-char (match-beginning 0))
556 (setq end (point-marker))
557 (mapcar #'(lambda (s) (let ((split (split-string s " ")))
558 (cons (string-to-number (nth 0 split))
559 (string-to-number (nth 1 split)))))
560 (delete "" (split-string (buffer-substring start end)
561 "\r\n"))))))))
eec82323
LMI
562
563(defun pop3-retr (process msg crashbuf)
564 "Retrieve message-id MSG to buffer CRASHBUF."
565 (pop3-send-command process (format "RETR %s" msg))
566 (pop3-read-response process)
567 (let ((start pop3-read-point) end)
568 (save-excursion
569 (set-buffer (process-buffer process))
570 (while (not (re-search-forward "^\\.\r\n" nil t))
8903a9c8 571 (pop3-accept-process-output process)
eec82323
LMI
572 (goto-char start))
573 (setq pop3-read-point (point-marker))
e62e7654
MB
574 ;; this code does not seem to work for some POP servers...
575 ;; and I cannot figure out why not.
576 ;; (goto-char (match-beginning 0))
577 ;; (backward-char 2)
578 ;; (if (not (looking-at "\r\n"))
579 ;; (insert "\r\n"))
580 ;; (re-search-forward "\\.\r\n")
eec82323
LMI
581 (goto-char (match-beginning 0))
582 (setq end (point-marker))
583 (pop3-clean-region start end)
584 (pop3-munge-message-separator start end)
585 (save-excursion
586 (set-buffer crashbuf)
587 (erase-buffer))
588 (copy-to-buffer crashbuf start end)
589 (delete-region start end)
590 )))
591
592(defun pop3-dele (process msg)
593 "Mark message-id MSG as deleted."
594 (pop3-send-command process (format "DELE %s" msg))
595 (pop3-read-response process))
596
597(defun pop3-noop (process msg)
598 "No-operation."
599 (pop3-send-command process "NOOP")
600 (pop3-read-response process))
601
602(defun pop3-last (process)
603 "Return highest accessed message-id number for the session."
604 (pop3-send-command process "LAST")
605 (let ((response (pop3-read-response process t)))
e9bd5782 606 (string-to-number (nth 1 (split-string response " ")))
eec82323
LMI
607 ))
608
609(defun pop3-rset (process)
610 "Remove all delete marks from current maildrop."
611 (pop3-send-command process "RSET")
612 (pop3-read-response process))
613
614;; UPDATE
615
616(defun pop3-quit (process)
617 "Close connection to POP3 server.
618Tell server to remove all messages marked as deleted, unlock the maildrop,
619and close the connection."
620 (pop3-send-command process "QUIT")
621 (pop3-read-response process t)
622 (if process
623 (save-excursion
624 (set-buffer (process-buffer process))
625 (goto-char (point-max))
626 (delete-process process))))
627\f
628;; Summary of POP3 (Post Office Protocol version 3) commands and responses
629
630;;; AUTHORIZATION STATE
631
632;; Initial TCP connection
633;; Arguments: none
634;; Restrictions: none
635;; Possible responses:
636;; +OK [POP3 server ready]
637
638;; USER name
639;; Arguments: a server specific user-id (required)
640;; Restrictions: authorization state [after unsuccessful USER or PASS
641;; Possible responses:
642;; +OK [valid user-id]
643;; -ERR [invalid user-id]
644
645;; PASS string
646;; Arguments: a server/user-id specific password (required)
647;; Restrictions: authorization state, after successful USER
648;; Possible responses:
649;; +OK [maildrop locked and ready]
650;; -ERR [invalid password]
651;; -ERR [unable to lock maildrop]
652
01c52d31
MB
653;; STLS (RFC 2595)
654;; Arguments: none
655;; Restrictions: Only permitted in AUTHORIZATION state.
656;; Possible responses:
657;; +OK
658;; -ERR
659
eec82323
LMI
660;;; TRANSACTION STATE
661
662;; STAT
663;; Arguments: none
664;; Restrictions: transaction state
665;; Possible responses:
666;; +OK nn mm [# of messages, size of maildrop]
667
668;; LIST [msg]
669;; Arguments: a message-id (optional)
670;; Restrictions: transaction state; msg must not be deleted
671;; Possible responses:
672;; +OK [scan listing follows]
673;; -ERR [no such message]
674
675;; RETR msg
676;; Arguments: a message-id (required)
677;; Restrictions: transaction state; msg must not be deleted
678;; Possible responses:
679;; +OK [message contents follow]
680;; -ERR [no such message]
681
682;; DELE msg
683;; Arguments: a message-id (required)
684;; Restrictions: transaction state; msg must not be deleted
685;; Possible responses:
686;; +OK [message deleted]
687;; -ERR [no such message]
688
689;; NOOP
690;; Arguments: none
691;; Restrictions: transaction state
692;; Possible responses:
693;; +OK
694
695;; LAST
696;; Arguments: none
697;; Restrictions: transaction state
698;; Possible responses:
699;; +OK nn [highest numbered message accessed]
700
701;; RSET
702;; Arguments: none
703;; Restrictions: transaction state
704;; Possible responses:
705;; +OK [all delete marks removed]
706
707;;; UPDATE STATE
708
709;; QUIT
710;; Arguments: none
711;; Restrictions: none
712;; Possible responses:
713;; +OK [TCP connection closed]
dd5da9b8
DL
714
715(provide 'pop3)
716
717;;; pop3.el ends here