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