1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
31 ;; This program was inspired by Kyle E. Jones's vm-pop program.
35 (eval-when-compile (require 'cl
))
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
)))
44 (defvar parse-time-months
)
47 "Post Office Protocol."
51 (defcustom pop3-maildrop
(or (user-login-name)
55 :version
"22.1" ;; Oort Gnus
59 (defcustom pop3-mailhost
(or (getenv "MAILHOST") ;; nil -> mismatch
62 :version
"22.1" ;; Oort Gnus
66 (defcustom pop3-port
110
68 :version
"22.1" ;; Oort Gnus
72 (defcustom pop3-password-required t
73 "*Non-nil if a password is required when connecting to POP server."
74 :version
"22.1" ;; Oort Gnus
78 ;; Should this be customizable?
79 (defvar pop3-password nil
80 "*Password to use when connecting to POP server.")
82 (defcustom pop3-authentication-scheme
'pass
83 "*POP3 authentication scheme.
84 Defaults to `pass', for the standard USER/PASS authentication. The other
85 valid value is 'apop'."
86 :type
'(choice (const :tag
"Normal user/password" pass
)
87 (const :tag
"APOP" apop
))
88 :version
"22.1" ;; Oort Gnus
91 (defcustom pop3-stream-length
100
92 "How many messages should be requested at one time.
93 The lower the number, the more latency-sensitive the fetching
94 will be. If your pop3 server doesn't support streaming at all,
100 (defcustom pop3-leave-mail-on-server nil
101 "Non-nil if the mail is to be left on the POP server after fetching.
102 Mails once fetched will never be fetched again by the UIDL control.
104 If this is neither nil nor a number, all mails will be left on the
105 server. If this is a number, leave mails on the server for this many
106 days since you first checked new mails. If this is nil, mails will be
107 deleted on the server right after fetching.
109 Gnus users should use the `:leave' keyword in a mail source to direct
110 the behaviour per server, rather than directly modifying this value.
112 Note that POP servers maintain no state information between sessions,
113 so what the client believes is there and what is actually there may
114 not match up. If they do not, then you may get duplicate mails or
115 the whole thing can fall apart and leave you with a corrupt mailbox."
117 :type
'(choice (const :tag
"Don't leave mails" nil
)
118 (const :tag
"Leave all mails" t
)
119 (number :tag
"Leave mails for this many days" :value
14))
122 (defcustom pop3-uidl-file
"~/.pop3-uidl"
123 "File used to save UIDL."
128 (defcustom pop3-uidl-file-backup
'(0 9)
129 "How to backup the UIDL file `pop3-uidl-file' when updating.
130 If it is a list of numbers, the first one binds `kept-old-versions' and
131 the other binds `kept-new-versions' to keep number of oldest and newest
132 versions. Otherwise, the value binds `version-control' (which see).
134 Note: Backup will take place whenever you check new mails on a server.
135 So, you may lose the backup files having been saved before a trouble
136 if you set it so as to make too few backups whereas you have access to
139 :type
'(choice (group :tag
"Keep versions" :format
"\n%v" :indent
3
140 (number :tag
"oldest")
141 (number :tag
"newest"))
143 :match
(lambda (widget value
)
145 (not (and (numberp (car value
))
146 (numberp (car (cdr value
)))))
150 (defvar pop3-timestamp nil
151 "Timestamp returned when initially connected to the POP server.
152 Used for APOP authentication.")
154 (defvar pop3-read-point nil
)
155 (defvar pop3-debug nil
)
157 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
158 ;; comments there for explanations about the values.
161 (if (and (fboundp 'nnheader-accept-process-output
)
162 (boundp 'nnheader-read-timeout
))
163 (defalias 'pop3-accept-process-output
'nnheader-accept-process-output
)
164 ;; Borrowed from `nnheader.el':
165 (defvar pop3-read-timeout
166 (if (string-match "windows-nt\\|os/2\\|cygwin"
167 (symbol-name system-type
))
170 "How long pop3 should wait between checking for the end of output.
171 Shorter values mean quicker response, but are more CPU intensive.")
172 (defun pop3-accept-process-output (process)
173 (accept-process-output
175 (truncate pop3-read-timeout
)
176 (truncate (* (- pop3-read-timeout
177 (truncate pop3-read-timeout
))
181 ;; List of UIDLs of existing messages at present in the server:
182 ;; ("UIDL1" "UIDL2" "UIDL3"...)
184 (defvar pop3-uidl-saved
)
185 ;; Locally saved UIDL data; an alist of the server, the user, and the UIDL
186 ;; and timestamp pairs:
187 ;; (("SERVER_A" ("USER_A1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
188 ;; ("USER_A2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
190 ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
191 ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
193 ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
194 ;; i.e. the return value of `current-time'.
197 (defun pop3-movemail (file)
198 "Transfer contents of a maildrop to the specified FILE.
199 Use streaming commands."
200 (let ((process (pop3-open-server pop3-mailhost pop3-port
))
205 (if pop3-leave-mail-on-server
206 (setq messages
(pop3-uidl-stat process
)
207 total-size
(cadr messages
)
208 messages
(car messages
))
209 (let ((size (pop3-stat process
)))
210 (dotimes (i (car size
)) (push (1+ i
) messages
))
211 (setq messages
(nreverse messages
)
212 total-size
(cadr size
))))
214 (with-current-buffer (process-buffer process
)
215 (pop3-send-streaming-command process
"RETR" messages total-size
)
216 (pop3-write-to-file file messages
)
217 (unless pop3-leave-mail-on-server
218 (pop3-send-streaming-command process
"DELE" messages nil
))))
219 (if pop3-leave-mail-on-server
220 (when (prog1 (pop3-uidl-dele process
) (pop3-quit process
))
223 ;; Remove UIDL data for the account that got not to leave mails.
224 (setq pop3-uidl-saved
(pop3-uidl-load))
225 (let ((elt (assoc pop3-maildrop
226 (cdr (assoc pop3-mailhost pop3-uidl-saved
)))))
232 (defun pop3-send-streaming-command (process command messages total-size
)
234 (let ((count (length messages
))
236 (start-point (point-min))
239 (process-send-string process
(format "%s %d\r\n" command
(pop messages
)))
240 ;; Only do 100 messages at a time to avoid pipe stalls.
241 (when (zerop (% i pop3-stream-length
))
243 (pop3-wait-for-messages process pop3-stream-length
244 total-size start-point
))
245 (incf waited-for pop3-stream-length
))
247 (pop3-wait-for-messages process
(- count waited-for
)
248 total-size start-point
)))
250 (defun pop3-wait-for-messages (process count total-size start-point
)
252 (goto-char start-point
)
253 (while (or (and (re-search-forward "^\\+OK" nil t
)
255 (re-search-forward "^\\.\r?\n" nil t
)))
256 (re-search-forward "^-ERR " nil t
))
258 (setq start-point
(point)))
259 (unless (memq (process-status process
) '(open run
))
260 (error "pop3 process died"))
263 (goto-char (point-min))
264 (while (re-search-forward "^\\+OK.*\n" nil t
)
265 (setq size
(+ size
(- (point))
266 (if (re-search-forward "^\\.\r?\n" nil
'move
)
269 (message "pop3 retrieved %dKB (%d%%)"
270 (truncate (/ size
1000))
271 (truncate (* (/ (* size
1.0) total-size
) 100)))))
272 (pop3-accept-process-output process
))
275 (defun pop3-write-to-file (file messages
)
276 (let ((pop-buffer (current-buffer))
281 (setq temp-buffer
(current-buffer))
282 (with-current-buffer pop-buffer
283 (goto-char (point-min))
284 (while (re-search-forward "^\\+OK" nil t
)
287 (when (re-search-forward "^\\.\r?\n" nil t
)
291 (with-current-buffer temp-buffer
292 (goto-char (point-max))
293 (let ((hstart (point)))
294 (insert-buffer-substring pop-buffer beg end
)
295 (pop3-clean-region hstart
(point))
296 (goto-char (point-max))
297 (pop3-munge-message-separator hstart
(point))
298 (when pop3-leave-mail-on-server
299 (pop3-uidl-add-xheader hstart
(pop messages
)))
300 (goto-char (point-max))))))
301 (let ((coding-system-for-write 'binary
))
302 (goto-char (point-min))
303 ;; Check whether something inserted a newline at the start and
307 (write-region (point-min) (point-max) file nil
'nomesg
)))))
309 (defun pop3-logon (process)
310 (let ((pop3-password pop3-password
))
311 ;; for debugging only
312 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
313 ;; query for password
314 (if (and pop3-password-required
(not pop3-password
))
316 (read-passwd (format "Password for %s: " pop3-maildrop
))))
317 (cond ((equal 'apop pop3-authentication-scheme
)
318 (pop3-apop process pop3-maildrop
))
319 ((equal 'pass pop3-authentication-scheme
)
320 (pop3-user process pop3-maildrop
)
322 (t (error "Invalid POP3 authentication scheme")))))
324 (defun pop3-get-message-count ()
325 "Return the number of messages in the maildrop."
326 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
328 (pop3-password pop3-password
))
329 ;; for debugging only
330 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
331 ;; query for password
332 (if (and pop3-password-required
(not pop3-password
))
334 (read-passwd (format "Password for %s: " pop3-maildrop
))))
335 (cond ((equal 'apop pop3-authentication-scheme
)
336 (pop3-apop process pop3-maildrop
))
337 ((equal 'pass pop3-authentication-scheme
)
338 (pop3-user process pop3-maildrop
)
340 (t (error "Invalid POP3 authentication scheme")))
341 (setq message-count
(car (pop3-stat process
)))
345 (defun pop3-uidl-stat (process)
346 "Return a list of unread message numbers and total size."
347 (pop3-send-command process
"UIDL")
348 (let (err messages size
)
349 (if (condition-case code
351 (pop3-read-response process
)
353 (error (setq err
(error-message-string code
))
355 (let ((start pop3-read-point
)
357 (with-current-buffer (process-buffer process
)
358 (while (not (re-search-forward "^\\.\r\n" nil t
))
359 (unless (memq (process-status process
) '(open run
))
360 (error "pop3 server closed the connection"))
361 (pop3-accept-process-output process
)
363 (setq pop3-read-point
(point-marker)
365 (while (progn (forward-line -
1) (>= (point) start
))
366 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
367 (push (match-string 1) pop3-uidl
)))
369 (setq pop3-uidl-saved
(pop3-uidl-load)
370 saved
(cdr (assoc pop3-maildrop
371 (cdr (assoc pop3-mailhost
373 (let ((i (length pop3-uidl
)))
375 (unless (member (nth (1- i
) pop3-uidl
) saved
)
379 (setq list
(pop3-list process
)
381 (dolist (msg messages
)
382 (setq size
(+ size
(cdr (assq msg list
)))))
383 (list messages size
)))))
384 (message "%s doesn't support UIDL (%s), so we try a regressive way..."
387 (setq size
(pop3-stat process
))
388 (dotimes (i (car size
)) (push (1+ i
) messages
))
389 (setcar size
(nreverse messages
))
392 (defun pop3-uidl-dele (process)
393 "Delete messages according to `pop3-leave-mail-on-server'.
394 Return non-nil if it is necessary to update the local UIDL file."
395 (let* ((ctime (current-time))
396 (srvr (assoc pop3-mailhost pop3-uidl-saved
))
397 (saved (assoc pop3-maildrop
(cdr srvr
)))
398 i uidl mod new tstamp dele
)
399 (setcdr (cdr ctime
) nil
)
400 ;; Add new messages to the data to be saved.
401 (cond ((and pop3-uidl saved
)
402 (setq i
(1- (length pop3-uidl
)))
404 (unless (member (setq uidl
(nth i pop3-uidl
)) (cdr saved
))
409 (setq new
(apply 'nconc
(mapcar (lambda (elt) (list elt ctime
))
411 (when new
(setq mod t
))
412 ;; List expirable messages and delete them from the data to be saved.
413 (setq ctime
(when (numberp pop3-leave-mail-on-server
)
414 (/ (+ (* (car ctime
) 65536.0) (cadr ctime
)) 86400))
415 i
(1- (length saved
)))
417 (if (member (setq uidl
(nth (1- i
) saved
)) pop3-uidl
)
419 (setq tstamp
(nth i saved
))
421 (> (- ctime
(/ (+ (* (car tstamp
) 65536.0) (cadr tstamp
))
423 pop3-leave-mail-on-server
))
431 ;; Mails having been deleted in the server.
437 (setcdr (last srvr
) (list (cons pop3-maildrop new
))))
439 (add-to-list 'pop3-uidl-saved
440 (list pop3-mailhost
(cons pop3-maildrop new
))
442 ;; Actually delete the messages in the server.
445 i
(length pop3-uidl
))
447 (when (member (nth (1- i
) pop3-uidl
) dele
)
451 (pop3-send-streaming-command process
"DELE" uidl nil
)))
454 (defun pop3-uidl-load ()
456 (when (file-exists-p pop3-uidl-file
)
460 (insert-file-contents pop3-uidl-file
)
461 (goto-char (point-min))
462 (read (current-buffer)))
464 (message "Error while loading %s (%s)"
465 pop3-uidl-file
(error-message-string code
))
469 (defun pop3-uidl-save ()
475 (dolist (srvr pop3-uidl-saved
)
477 (insert "(\"" (pop srvr
) "\"\n ")
480 (insert "(\"" (pop elt
) "\"\n ")
482 (insert (format "\"%s\" %s\n " (pop elt
) (pop elt
))))
486 (if (eq (char-before) ?\
))
488 (goto-char (1+ (point-at-bol)))
489 (delete-region (point) (point-max)))))
490 (when (eq (char-before) ?
)
494 (let ((buffer-file-name pop3-uidl-file
)
495 (delete-old-versions t
)
496 (kept-new-versions kept-new-versions
)
497 (kept-old-versions kept-old-versions
)
498 (version-control version-control
))
499 (if (consp pop3-uidl-file-backup
)
500 (setq kept-new-versions
(cadr pop3-uidl-file-backup
)
501 kept-old-versions
(car pop3-uidl-file-backup
)
503 (setq version-control pop3-uidl-file-backup
))
506 (defun pop3-uidl-add-xheader (start msgno
)
508 (let ((case-fold-search t
))
510 (narrow-to-region start
(progn
512 (search-forward "\n\n" nil
'move
)
515 (while (re-search-forward "^x-uidl:" nil t
)
518 (memq (char-after) '(?
\t ?
))))
519 (delete-region (match-beginning 0) (point)))
520 (goto-char (point-max))
521 (insert "X-UIDL: " (nth (1- msgno
) pop3-uidl
) "\n"))))
523 (defcustom pop3-stream-type nil
524 "*Transport security type for POP3 connections.
525 This may be either nil (plain connection), `ssl' (use an
526 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
527 to turn on TLS security after opening the stream). However, if
528 this is nil, `ssl' is assumed for connections to port
530 :version
"23.1" ;; No Gnus
532 :type
'(choice (const :tag
"Plain" nil
)
533 (const :tag
"SSL/TLS" ssl
)
537 (if (fboundp 'set-process-query-on-exit-flag
)
538 (defalias 'pop3-set-process-query-on-exit-flag
539 'set-process-query-on-exit-flag
)
540 (defalias 'pop3-set-process-query-on-exit-flag
541 'process-kill-without-query
)))
543 (defun pop3-open-server (mailhost port
)
544 "Open TCP connection to MAILHOST on PORT.
545 Returns the process associated with the connection."
546 (let ((coding-system-for-read 'binary
)
547 (coding-system-for-write 'binary
)
550 (get-buffer-create (concat " trace of POP session to "
553 (setq pop3-read-point
(point-min))
555 (open-protocol-stream
556 "POP" (current-buffer) mailhost port
558 ((or (eq pop3-stream-type
'ssl
)
559 (and (not pop3-stream-type
)
560 (member port
'(995 "pop3s"))))
563 (or pop3-stream-type
'network
)))
564 :capability-command
"CAPA\r\n"
565 :end-of-command
"^\\(-ERR\\|+OK\\).*\n"
566 :end-of-capability
"^\\.\r?\n\\|^-ERR"
567 :success
"^\\+OK.*\n"
570 (lambda (capabilities)
571 (and (string-match "\\bSTLS\\b" capabilities
)
574 (let ((response (plist-get (cdr result
) :greeting
)))
576 (substring response
(or (string-match "<" response
) 0)
577 (+ 1 (or (string-match ">" response
) -
1)))))
578 (pop3-set-process-query-on-exit-flag (car result
) nil
)
584 (defun pop3-send-command (process command
)
585 (set-buffer (process-buffer process
))
586 (goto-char (point-max))
587 ;; (if (= (aref command 0) ?P)
588 ;; (insert "PASS <omitted>\r\n")
589 ;; (insert command "\r\n"))
590 (setq pop3-read-point
(point))
591 (goto-char (point-max))
592 (process-send-string process
(concat command
"\r\n")))
594 (defun pop3-read-response (process &optional return
)
595 "Read the response from the server.
596 Return the response string if optional second argument is non-nil."
597 (let ((case-fold-search nil
)
599 (with-current-buffer (process-buffer process
)
600 (goto-char pop3-read-point
)
601 (while (and (memq (process-status process
) '(open run
))
602 (not (search-forward "\r\n" nil t
)))
603 (pop3-accept-process-output process
)
604 (goto-char pop3-read-point
))
605 (setq match-end
(point))
606 (goto-char pop3-read-point
)
607 (if (looking-at "-ERR")
608 (error "%s" (buffer-substring (point) (- match-end
2)))
609 (if (not (looking-at "+OK"))
610 (progn (setq pop3-read-point match-end
) nil
)
611 (setq pop3-read-point match-end
)
613 (buffer-substring (point) match-end
)
617 (defun pop3-clean-region (start end
)
618 (setq end
(set-marker (make-marker) end
))
621 (while (and (< (point) end
) (search-forward "\r\n" end t
))
622 (replace-match "\n" t t
))
624 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
625 (replace-match "" t t
)
627 (set-marker end nil
))
629 ;; Copied from message-make-date.
630 (defun pop3-make-date (&optional now
)
631 "Make a valid date header.
632 If NOW, use that time instead."
633 (require 'parse-time
)
634 (let* ((now (or now
(current-time)))
635 (zone (nth 8 (decode-time now
)))
639 (setq zone
(- zone
)))
641 (format-time-string "%d" now
)
642 ;; The month name of the %b spec is locale-specific. Pfff.
644 (capitalize (car (rassoc (nth 4 (decode-time now
))
645 parse-time-months
))))
646 (format-time-string "%Y %H:%M:%S " now
)
647 ;; We do all of this because XEmacs doesn't have the %z spec.
648 (format "%s%02d%02d" sign
(/ zone
3600) (/ (% zone
3600) 60)))))
650 (defun pop3-munge-message-separator (start end
)
651 "Check to see if a message separator exists. If not, generate one."
654 (narrow-to-region start end
)
655 (goto-char (point-min))
656 (if (not (or (looking-at "From .?") ; Unix mail
657 (looking-at "\001\001\001\001\n") ; MMDF
658 (looking-at "BABYL OPTIONS:") ; Babyl
660 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
661 (tdate (mail-fetch-field "Date"))
662 (date (split-string (or (and tdate
663 (not (string= "" tdate
))
668 ;; sample date formats I have seen
669 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
670 ;; Date: 08 Jul 1996 23:22:24 -0400
672 ;; Tue Jul 9 09:04:21 1996
674 ;; Fixme: This should use timezone on the date field contents.
677 "Tue Jan 1 00:00:0 1900")
678 ((string-match "[A-Z]" (nth 0 date
))
679 (format "%s %s %s %s %s"
680 (nth 0 date
) (nth 2 date
) (nth 1 date
)
681 (nth 4 date
) (nth 3 date
)))
683 ;; this really needs to be better but I don't feel
684 ;; like writing a date to day converter.
685 (format "Sun %s %s %s %s"
686 (nth 1 date
) (nth 0 date
)
687 (nth 3 date
) (nth 2 date
)))
689 (setq From_
(format "\nFrom %s %s\n" from date
))
690 (while (string-match "," From_
)
691 (setq From_
(concat (substring From_
0 (match-beginning 0))
692 (substring From_
(match-end 0)))))
693 (goto-char (point-min))
695 (if (search-forward "\n\n" nil t
)
697 (goto-char (point-max))
699 (let ((size (- (point-max) (point))))
701 (insert (format "Content-Length: %s\n" size
)))
706 ;; AUTHORIZATION STATE
708 (defun pop3-user (process user
)
709 "Send USER information to POP3 server."
710 (pop3-send-command process
(format "USER %s" user
))
711 (let ((response (pop3-read-response process t
)))
712 (if (not (and response
(string-match "+OK" response
)))
713 (error "USER %s not valid" user
))))
715 (defun pop3-pass (process)
716 "Send authentication information to the server."
717 (pop3-send-command process
(format "PASS %s" pop3-password
))
718 (let ((response (pop3-read-response process t
)))
719 (if (not (and response
(string-match "+OK" response
)))
720 (pop3-quit process
))))
722 (defun pop3-apop (process user
)
723 "Send alternate authentication information to the server."
724 (let ((pass pop3-password
))
725 (if (and pop3-password-required
(not pass
))
727 (read-passwd (format "Password for %s: " pop3-maildrop
))))
729 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
730 (pop3-send-command process
(format "APOP %s %s" user hash
))
731 (let ((response (pop3-read-response process t
)))
732 (if (not (and response
(string-match "+OK" response
)))
733 (pop3-quit process
)))))
738 (defun pop3-stat (process)
739 "Return the number of messages in the maildrop and the maildrop's size."
740 (pop3-send-command process
"STAT")
741 (let ((response (pop3-read-response process t
)))
742 (list (string-to-number (nth 1 (split-string response
" ")))
743 (string-to-number (nth 2 (split-string response
" "))))
746 (defun pop3-list (process &optional msg
)
747 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
748 Otherwise, return the size of the message-id MSG"
749 (pop3-send-command process
(if msg
750 (format "LIST %d" msg
)
752 (let ((response (pop3-read-response process t
)))
754 (string-to-number (nth 2 (split-string response
" ")))
755 (let ((start pop3-read-point
) end
)
756 (with-current-buffer (process-buffer process
)
757 (while (not (re-search-forward "^\\.\r\n" nil t
))
758 (pop3-accept-process-output process
)
760 (setq pop3-read-point
(point-marker))
761 (goto-char (match-beginning 0))
762 (setq end
(point-marker))
763 (mapcar #'(lambda (s) (let ((split (split-string s
" ")))
764 (cons (string-to-number (nth 0 split
))
765 (string-to-number (nth 1 split
)))))
766 (split-string (buffer-substring start end
) "\r\n" t
)))))))
768 (defun pop3-retr (process msg crashbuf
)
769 "Retrieve message-id MSG to buffer CRASHBUF."
770 (pop3-send-command process
(format "RETR %s" msg
))
771 (pop3-read-response process
)
772 (let ((start pop3-read-point
) end
)
773 (with-current-buffer (process-buffer process
)
774 (while (not (re-search-forward "^\\.\r\n" nil t
))
775 (unless (memq (process-status process
) '(open run
))
776 (error "pop3 server closed the connection"))
777 (pop3-accept-process-output process
)
779 (setq pop3-read-point
(point-marker))
780 ;; this code does not seem to work for some POP servers...
781 ;; and I cannot figure out why not.
782 ;; (goto-char (match-beginning 0))
784 ;; (if (not (looking-at "\r\n"))
786 ;; (re-search-forward "\\.\r\n")
787 (goto-char (match-beginning 0))
788 (setq end
(point-marker))
789 (pop3-clean-region start end
)
790 (pop3-munge-message-separator start end
)
791 (with-current-buffer crashbuf
793 (copy-to-buffer crashbuf start end
)
794 (delete-region start end
)
797 (defun pop3-dele (process msg
)
798 "Mark message-id MSG as deleted."
799 (pop3-send-command process
(format "DELE %s" msg
))
800 (pop3-read-response process
))
802 (defun pop3-noop (process msg
)
804 (pop3-send-command process
"NOOP")
805 (pop3-read-response process
))
807 (defun pop3-last (process)
808 "Return highest accessed message-id number for the session."
809 (pop3-send-command process
"LAST")
810 (let ((response (pop3-read-response process t
)))
811 (string-to-number (nth 1 (split-string response
" ")))
814 (defun pop3-rset (process)
815 "Remove all delete marks from current maildrop."
816 (pop3-send-command process
"RSET")
817 (pop3-read-response process
))
821 (defun pop3-quit (process)
822 "Close connection to POP3 server.
823 Tell server to remove all messages marked as deleted, unlock the maildrop,
824 and close the connection."
825 (pop3-send-command process
"QUIT")
826 (pop3-read-response process t
)
828 (with-current-buffer (process-buffer process
)
829 (goto-char (point-max))
830 (delete-process process
))))
832 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
834 ;;; AUTHORIZATION STATE
836 ;; Initial TCP connection
838 ;; Restrictions: none
839 ;; Possible responses:
840 ;; +OK [POP3 server ready]
843 ;; Arguments: a server specific user-id (required)
844 ;; Restrictions: authorization state [after unsuccessful USER or PASS
845 ;; Possible responses:
846 ;; +OK [valid user-id]
847 ;; -ERR [invalid user-id]
850 ;; Arguments: a server/user-id specific password (required)
851 ;; Restrictions: authorization state, after successful USER
852 ;; Possible responses:
853 ;; +OK [maildrop locked and ready]
854 ;; -ERR [invalid password]
855 ;; -ERR [unable to lock maildrop]
859 ;; Restrictions: Only permitted in AUTHORIZATION state.
860 ;; Possible responses:
864 ;;; TRANSACTION STATE
868 ;; Restrictions: transaction state
869 ;; Possible responses:
870 ;; +OK nn mm [# of messages, size of maildrop]
873 ;; Arguments: a message-id (optional)
874 ;; Restrictions: transaction state; msg must not be deleted
875 ;; Possible responses:
876 ;; +OK [scan listing follows]
877 ;; -ERR [no such message]
880 ;; Arguments: a message-id (required)
881 ;; Restrictions: transaction state; msg must not be deleted
882 ;; Possible responses:
883 ;; +OK [message contents follow]
884 ;; -ERR [no such message]
887 ;; Arguments: a message-id (required)
888 ;; Restrictions: transaction state; msg must not be deleted
889 ;; Possible responses:
890 ;; +OK [message deleted]
891 ;; -ERR [no such message]
895 ;; Restrictions: transaction state
896 ;; Possible responses:
901 ;; Restrictions: transaction state
902 ;; Possible responses:
903 ;; +OK nn [highest numbered message accessed]
907 ;; Restrictions: transaction state
908 ;; Possible responses:
909 ;; +OK [all delete marks removed]
912 ;; Arguments: a message-id (optional)
913 ;; Restrictions: transaction state; msg must not be deleted
914 ;; Possible responses:
915 ;; +OK [uidl listing follows]
916 ;; -ERR [no such message]
922 ;; Restrictions: none
923 ;; Possible responses:
924 ;; +OK [TCP connection closed]
928 ;;; pop3.el ends here