1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Maintainer: emacs-devel@gnu.org
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 (defvar parse-time-months
)
41 "Post Office Protocol."
45 (defcustom pop3-maildrop
(or (user-login-name)
49 :version
"22.1" ;; Oort Gnus
53 (defcustom pop3-mailhost
(or (getenv "MAILHOST") ;; nil -> mismatch
56 :version
"22.1" ;; Oort Gnus
60 (defcustom pop3-port
110
62 :version
"22.1" ;; Oort Gnus
66 (defcustom pop3-password-required t
67 "*Non-nil if a password is required when connecting to POP server."
68 :version
"22.1" ;; Oort Gnus
72 ;; Should this be customizable?
73 (defvar pop3-password nil
74 "*Password to use when connecting to POP server.")
76 (defcustom pop3-authentication-scheme
'pass
77 "*POP3 authentication scheme.
78 Defaults to `pass', for the standard USER/PASS authentication. The other
79 valid value is `apop'."
80 :type
'(choice (const :tag
"Normal user/password" pass
)
81 (const :tag
"APOP" apop
))
82 :version
"22.1" ;; Oort Gnus
85 (defcustom pop3-stream-length
100
86 "How many messages should be requested at one time.
87 The lower the number, the more latency-sensitive the fetching
88 will be. If your pop3 server doesn't support streaming at all,
94 (defcustom pop3-leave-mail-on-server nil
95 "Non-nil if the mail is to be left on the POP server after fetching.
96 Mails once fetched will never be fetched again by the UIDL control.
98 If this is neither nil nor a number, all mails will be left on the
99 server. If this is a number, leave mails on the server for this many
100 days since you first checked new mails. If this is nil, mails will be
101 deleted on the server right after fetching.
103 Gnus users should use the `:leave' keyword in a mail source to direct
104 the behavior per server, rather than directly modifying this value.
106 Note that POP servers maintain no state information between sessions,
107 so what the client believes is there and what is actually there may
108 not match up. If they do not, then you may get duplicate mails or
109 the whole thing can fall apart and leave you with a corrupt mailbox."
111 :type
'(choice (const :tag
"Don't leave mails" nil
)
112 (const :tag
"Leave all mails" t
)
113 (number :tag
"Leave mails for this many days" :value
14))
116 (defcustom pop3-uidl-file
"~/.pop3-uidl"
117 "File used to save UIDL."
122 (defcustom pop3-uidl-file-backup
'(0 9)
123 "How to backup the UIDL file `pop3-uidl-file' when updating.
124 If it is a list of numbers, the first one binds `kept-old-versions' and
125 the other binds `kept-new-versions' to keep number of oldest and newest
126 versions. Otherwise, the value binds `version-control' (which see).
128 Note: Backup will take place whenever you check new mails on a server.
129 So, you may lose the backup files having been saved before a trouble
130 if you set it so as to make too few backups whereas you have access to
133 :type
'(choice (group :tag
"Keep versions" :format
"\n%v" :indent
3
134 (number :tag
"oldest")
135 (number :tag
"newest"))
137 :match
(lambda (widget value
)
139 (not (and (numberp (car value
))
140 (numberp (car (cdr value
)))))
144 (defvar pop3-timestamp nil
145 "Timestamp returned when initially connected to the POP server.
146 Used for APOP authentication.")
148 (defvar pop3-read-point nil
)
149 (defvar pop3-debug nil
)
151 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
152 ;; comments there for explanations about the values.
155 (if (and (fboundp 'nnheader-accept-process-output
)
156 (boundp 'nnheader-read-timeout
))
157 (defalias 'pop3-accept-process-output
'nnheader-accept-process-output
)
158 ;; Borrowed from `nnheader.el':
159 (defvar pop3-read-timeout
160 (if (string-match "windows-nt\\|os/2\\|cygwin"
161 (symbol-name system-type
))
164 "How long pop3 should wait between checking for the end of output.
165 Shorter values mean quicker response, but are more CPU intensive.")
166 (defun pop3-accept-process-output (process)
167 (accept-process-output
169 (truncate pop3-read-timeout
)
170 (truncate (* (- pop3-read-timeout
171 (truncate pop3-read-timeout
))
175 ;; List of UIDLs of existing messages at present in the server:
176 ;; ("UIDL1" "UIDL2" "UIDL3"...)
178 (defvar pop3-uidl-saved
)
179 ;; Locally saved UIDL data; an alist of the server, the user, and the UIDL
180 ;; and timestamp pairs:
181 ;; (("SERVER_A" ("USER_A1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
182 ;; ("USER_A2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
184 ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
185 ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
187 ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
188 ;; i.e. the return value of `current-time'.
191 (defun pop3-movemail (file)
192 "Transfer contents of a maildrop to the specified FILE.
193 Use streaming commands."
194 (let ((process (pop3-open-server pop3-mailhost pop3-port
))
199 (if pop3-leave-mail-on-server
200 (setq messages
(pop3-uidl-stat process
)
201 total-size
(cadr messages
)
202 messages
(car messages
))
203 (let ((size (pop3-stat process
)))
204 (dotimes (i (car size
)) (push (1+ i
) messages
))
205 (setq messages
(nreverse messages
)
206 total-size
(cadr size
))))
208 (with-current-buffer (process-buffer process
)
209 (pop3-send-streaming-command process
"RETR" messages total-size
)
210 (pop3-write-to-file file messages
)
211 (unless pop3-leave-mail-on-server
212 (pop3-send-streaming-command process
"DELE" messages nil
))))
213 (if pop3-leave-mail-on-server
214 (when (prog1 (pop3-uidl-dele process
) (pop3-quit process
))
217 ;; Remove UIDL data for the account that got not to leave mails.
218 (setq pop3-uidl-saved
(pop3-uidl-load))
219 (let ((elt (assoc pop3-maildrop
220 (cdr (assoc pop3-mailhost pop3-uidl-saved
)))))
226 (defun pop3-send-streaming-command (process command messages total-size
)
228 (let ((count (length messages
))
230 (start-point (point-min))
233 (process-send-string process
(format "%s %d\r\n" command
(pop messages
)))
234 ;; Only do 100 messages at a time to avoid pipe stalls.
235 (when (zerop (% i pop3-stream-length
))
237 (pop3-wait-for-messages process pop3-stream-length
238 total-size start-point
))
239 (incf waited-for pop3-stream-length
))
241 (pop3-wait-for-messages process
(- count waited-for
)
242 total-size start-point
)))
244 (defun pop3-wait-for-messages (process count total-size start-point
)
246 (goto-char start-point
)
247 (while (or (and (re-search-forward "^\\+OK" nil t
)
249 (re-search-forward "^\\.\r?\n" nil t
)))
250 (re-search-forward "^-ERR " nil t
))
252 (setq start-point
(point)))
253 (unless (memq (process-status process
) '(open run
))
254 (error "pop3 process died"))
257 (goto-char (point-min))
258 (while (re-search-forward "^\\+OK.*\n" nil t
)
259 (setq size
(+ size
(- (point))
260 (if (re-search-forward "^\\.\r?\n" nil
'move
)
263 (message "pop3 retrieved %dKB (%d%%)"
264 (truncate (/ size
1000))
265 (truncate (* (/ (* size
1.0) total-size
) 100)))))
266 (pop3-accept-process-output process
))
269 (defun pop3-write-to-file (file messages
)
270 (let ((pop-buffer (current-buffer))
275 (setq temp-buffer
(current-buffer))
276 (with-current-buffer pop-buffer
277 (goto-char (point-min))
278 (while (re-search-forward "^\\+OK" nil t
)
281 (when (re-search-forward "^\\.\r?\n" nil t
)
285 (with-current-buffer temp-buffer
286 (goto-char (point-max))
287 (let ((hstart (point)))
288 (insert-buffer-substring pop-buffer beg end
)
289 (pop3-clean-region hstart
(point))
290 (goto-char (point-max))
291 (pop3-munge-message-separator hstart
(point))
292 (when pop3-leave-mail-on-server
293 (pop3-uidl-add-xheader hstart
(pop messages
)))
294 (goto-char (point-max))))))
295 (let ((coding-system-for-write 'binary
))
296 (goto-char (point-min))
297 ;; Check whether something inserted a newline at the start and
301 (write-region (point-min) (point-max) file nil
'nomesg
)))))
303 (defun pop3-logon (process)
304 (let ((pop3-password pop3-password
))
305 ;; for debugging only
306 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
307 ;; query for password
308 (if (and pop3-password-required
(not pop3-password
))
310 (read-passwd (format "Password for %s: " pop3-maildrop
))))
311 (cond ((equal 'apop pop3-authentication-scheme
)
312 (pop3-apop process pop3-maildrop
))
313 ((equal 'pass pop3-authentication-scheme
)
314 (pop3-user process pop3-maildrop
)
316 (t (error "Invalid POP3 authentication scheme")))))
318 (defun pop3-get-message-count ()
319 "Return the number of messages in the maildrop."
320 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
322 (pop3-password pop3-password
))
323 ;; for debugging only
324 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
325 ;; query for password
326 (if (and pop3-password-required
(not pop3-password
))
328 (read-passwd (format "Password for %s: " pop3-maildrop
))))
329 (cond ((equal 'apop pop3-authentication-scheme
)
330 (pop3-apop process pop3-maildrop
))
331 ((equal 'pass pop3-authentication-scheme
)
332 (pop3-user process pop3-maildrop
)
334 (t (error "Invalid POP3 authentication scheme")))
335 (setq message-count
(car (pop3-stat process
)))
339 (defun pop3-uidl-stat (process)
340 "Return a list of unread message numbers and total size."
341 (pop3-send-command process
"UIDL")
342 (let (err messages size
)
343 (if (condition-case code
345 (pop3-read-response process
)
347 (error (setq err
(error-message-string code
))
349 (let ((start pop3-read-point
)
351 (with-current-buffer (process-buffer process
)
352 (while (not (re-search-forward "^\\.\r\n" nil t
))
353 (unless (memq (process-status process
) '(open run
))
354 (error "pop3 server closed the connection"))
355 (pop3-accept-process-output process
)
357 (setq pop3-read-point
(point-marker)
359 (while (progn (forward-line -
1) (>= (point) start
))
360 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
361 (push (match-string 1) pop3-uidl
)))
363 (setq pop3-uidl-saved
(pop3-uidl-load)
364 saved
(cdr (assoc pop3-maildrop
365 (cdr (assoc pop3-mailhost
367 (let ((i (length pop3-uidl
)))
369 (unless (member (nth (1- i
) pop3-uidl
) saved
)
373 (setq list
(pop3-list process
)
375 (dolist (msg messages
)
376 (setq size
(+ size
(cdr (assq msg list
)))))
377 (list messages size
)))))
378 (message "%s doesn't support UIDL (%s), so we try a regressive way..."
381 (setq size
(pop3-stat process
))
382 (dotimes (i (car size
)) (push (1+ i
) messages
))
383 (setcar size
(nreverse messages
))
386 (defun pop3-uidl-dele (process)
387 "Delete messages according to `pop3-leave-mail-on-server'.
388 Return non-nil if it is necessary to update the local UIDL file."
389 (let* ((ctime (current-time))
390 (srvr (assoc pop3-mailhost pop3-uidl-saved
))
391 (saved (assoc pop3-maildrop
(cdr srvr
)))
392 i uidl mod new tstamp dele
)
393 (setcdr (cdr ctime
) nil
)
394 ;; Add new messages to the data to be saved.
395 (cond ((and pop3-uidl saved
)
396 (setq i
(1- (length pop3-uidl
)))
398 (unless (member (setq uidl
(nth i pop3-uidl
)) (cdr saved
))
403 (setq new
(apply 'nconc
(mapcar (lambda (elt) (list elt ctime
))
405 (when new
(setq mod t
))
406 ;; List expirable messages and delete them from the data to be saved.
407 (setq ctime
(when (numberp pop3-leave-mail-on-server
)
408 (/ (+ (* (car ctime
) 65536.0) (cadr ctime
)) 86400))
409 i
(1- (length saved
)))
411 (if (member (setq uidl
(nth (1- i
) saved
)) pop3-uidl
)
413 (setq tstamp
(nth i saved
))
415 (> (- ctime
(/ (+ (* (car tstamp
) 65536.0) (cadr tstamp
))
417 pop3-leave-mail-on-server
))
425 ;; Mails having been deleted in the server.
431 (setcdr (last srvr
) (list (cons pop3-maildrop new
))))
433 (add-to-list 'pop3-uidl-saved
434 (list pop3-mailhost
(cons pop3-maildrop new
))
436 ;; Actually delete the messages in the server.
439 i
(length pop3-uidl
))
441 (when (member (nth (1- i
) pop3-uidl
) dele
)
445 (pop3-send-streaming-command process
"DELE" uidl nil
)))
448 (defun pop3-uidl-load ()
450 (when (file-exists-p pop3-uidl-file
)
454 (insert-file-contents pop3-uidl-file
)
455 (goto-char (point-min))
456 (read (current-buffer)))
458 (message "Error while loading %s (%s)"
459 pop3-uidl-file
(error-message-string code
))
463 (defun pop3-uidl-save ()
469 (dolist (srvr pop3-uidl-saved
)
471 (insert "(\"" (pop srvr
) "\"\n ")
474 (insert "(\"" (pop elt
) "\"\n ")
476 (insert (format "\"%s\" %s\n " (pop elt
) (pop elt
))))
480 (if (eq (char-before) ?\
))
482 (goto-char (1+ (point-at-bol)))
483 (delete-region (point) (point-max)))))
484 (when (eq (char-before) ?
)
488 (let ((buffer-file-name pop3-uidl-file
)
489 (delete-old-versions t
)
490 (kept-new-versions kept-new-versions
)
491 (kept-old-versions kept-old-versions
)
492 (version-control version-control
))
493 (if (consp pop3-uidl-file-backup
)
494 (setq kept-new-versions
(cadr pop3-uidl-file-backup
)
495 kept-old-versions
(car pop3-uidl-file-backup
)
497 (setq version-control pop3-uidl-file-backup
))
500 (defun pop3-uidl-add-xheader (start msgno
)
502 (let ((case-fold-search t
))
504 (narrow-to-region start
(progn
506 (search-forward "\n\n" nil
'move
)
509 (while (re-search-forward "^x-uidl:" nil t
)
512 (memq (char-after) '(?
\t ?
))))
513 (delete-region (match-beginning 0) (point)))
514 (goto-char (point-max))
515 (insert "X-UIDL: " (nth (1- msgno
) pop3-uidl
) "\n"))))
517 (defcustom pop3-stream-type nil
518 "*Transport security type for POP3 connections.
519 This may be either nil (plain connection), `ssl' (use an
520 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
521 to turn on TLS security after opening the stream). However, if
522 this is nil, `ssl' is assumed for connections to port
524 :version
"23.1" ;; No Gnus
526 :type
'(choice (const :tag
"Plain" nil
)
527 (const :tag
"SSL/TLS" ssl
)
530 (defun pop3-open-server (mailhost port
)
531 "Open TCP connection to MAILHOST on PORT.
532 Returns the process associated with the connection."
533 (let ((coding-system-for-read 'binary
)
534 (coding-system-for-write 'binary
)
537 (get-buffer-create (concat " trace of POP session to "
540 (setq pop3-read-point
(point-min))
543 "POP" (current-buffer) mailhost port
545 ((or (eq pop3-stream-type
'ssl
)
546 (and (not pop3-stream-type
)
547 (member port
'(995 "pop3s"))))
550 (or pop3-stream-type
'network
)))
551 :warn-unless-encrypted t
552 :capability-command
"CAPA\r\n"
553 :end-of-command
"^\\(-ERR\\|+OK\\).*\n"
554 :end-of-capability
"^\\.\r?\n\\|^-ERR"
555 :success
"^\\+OK.*\n"
558 (lambda (capabilities)
559 (and (string-match "\\bSTLS\\b" capabilities
)
562 (let ((response (plist-get (cdr result
) :greeting
)))
564 (substring response
(or (string-match "<" response
) 0)
565 (+ 1 (or (string-match ">" response
) -
1)))))
566 (set-process-query-on-exit-flag (car result
) nil
)
572 (defun pop3-send-command (process command
)
573 (set-buffer (process-buffer process
))
574 (goto-char (point-max))
575 ;; (if (= (aref command 0) ?P)
576 ;; (insert "PASS <omitted>\r\n")
577 ;; (insert command "\r\n"))
578 (setq pop3-read-point
(point))
579 (goto-char (point-max))
580 (process-send-string process
(concat command
"\r\n")))
582 (defun pop3-read-response (process &optional return
)
583 "Read the response from the server.
584 Return the response string if optional second argument is non-nil."
585 (let ((case-fold-search nil
)
587 (with-current-buffer (process-buffer process
)
588 (goto-char pop3-read-point
)
589 (while (and (memq (process-status process
) '(open run
))
590 (not (search-forward "\r\n" nil t
)))
591 (pop3-accept-process-output process
)
592 (goto-char pop3-read-point
))
593 (setq match-end
(point))
594 (goto-char pop3-read-point
)
595 (if (looking-at "-ERR")
596 (error "%s" (buffer-substring (point) (- match-end
2)))
597 (if (not (looking-at "+OK"))
598 (progn (setq pop3-read-point match-end
) nil
)
599 (setq pop3-read-point match-end
)
601 (buffer-substring (point) match-end
)
605 (defun pop3-clean-region (start end
)
606 (setq end
(set-marker (make-marker) end
))
609 (while (and (< (point) end
) (search-forward "\r\n" end t
))
610 (replace-match "\n" t t
))
612 (while (and (< (point) end
) (re-search-forward "^\\." end t
))
613 (replace-match "" t t
)
615 (set-marker end nil
))
617 ;; Copied from message-make-date.
618 (defun pop3-make-date (&optional now
)
619 "Make a valid date header.
620 If NOW, use that time instead."
621 (require 'parse-time
)
622 (let* ((now (or now
(current-time)))
623 (zone (nth 8 (decode-time now
)))
627 (setq zone
(- zone
)))
629 (format-time-string "%d" now
)
630 ;; The month name of the %b spec is locale-specific. Pfff.
632 (capitalize (car (rassoc (nth 4 (decode-time now
))
633 parse-time-months
))))
634 (format-time-string "%Y %H:%M:%S %z" now
))))
636 (defun pop3-munge-message-separator (start end
)
637 "Check to see if a message separator exists. If not, generate one."
640 (narrow-to-region start end
)
641 (goto-char (point-min))
642 (if (not (or (looking-at "From .?") ; Unix mail
643 (looking-at "\001\001\001\001\n") ; MMDF
644 (looking-at "BABYL OPTIONS:") ; Babyl
646 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
647 (tdate (mail-fetch-field "Date"))
648 (date (split-string (or (and tdate
649 (not (string= "" tdate
))
654 ;; sample date formats I have seen
655 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
656 ;; Date: 08 Jul 1996 23:22:24 -0400
658 ;; Tue Jul 9 09:04:21 1996
660 ;; Fixme: This should use timezone on the date field contents.
663 "Tue Jan 1 00:00:0 1900")
664 ((string-match "[A-Z]" (nth 0 date
))
665 (format "%s %s %s %s %s"
666 (nth 0 date
) (nth 2 date
) (nth 1 date
)
667 (nth 4 date
) (nth 3 date
)))
669 ;; this really needs to be better but I don't feel
670 ;; like writing a date to day converter.
671 (format "Sun %s %s %s %s"
672 (nth 1 date
) (nth 0 date
)
673 (nth 3 date
) (nth 2 date
)))
675 (setq From_
(format "\nFrom %s %s\n" from date
))
676 (while (string-match "," From_
)
677 (setq From_
(concat (substring From_
0 (match-beginning 0))
678 (substring From_
(match-end 0)))))
679 (goto-char (point-min))
681 (if (search-forward "\n\n" nil t
)
683 (goto-char (point-max))
685 (let ((size (- (point-max) (point))))
687 (insert (format "Content-Length: %s\n" size
)))
692 ;; AUTHORIZATION STATE
694 (defun pop3-user (process user
)
695 "Send USER information to POP3 server."
696 (pop3-send-command process
(format "USER %s" user
))
697 (let ((response (pop3-read-response process t
)))
698 (if (not (and response
(string-match "+OK" response
)))
699 (error "USER %s not valid" user
))))
701 (defun pop3-pass (process)
702 "Send authentication information to the server."
703 (pop3-send-command process
(format "PASS %s" pop3-password
))
704 (let ((response (pop3-read-response process t
)))
705 (if (not (and response
(string-match "+OK" response
)))
706 (pop3-quit process
))))
708 (defun pop3-apop (process user
)
709 "Send alternate authentication information to the server."
710 (let ((pass pop3-password
))
711 (if (and pop3-password-required
(not pass
))
713 (read-passwd (format "Password for %s: " pop3-maildrop
))))
715 (let ((hash (md5 (concat pop3-timestamp pass
) nil nil
'binary
)))
716 (pop3-send-command process
(format "APOP %s %s" user hash
))
717 (let ((response (pop3-read-response process t
)))
718 (if (not (and response
(string-match "+OK" response
)))
719 (pop3-quit process
)))))
724 (defun pop3-stat (process)
725 "Return the number of messages in the maildrop and the maildrop's size."
726 (pop3-send-command process
"STAT")
727 (let ((response (pop3-read-response process t
)))
728 (list (string-to-number (nth 1 (split-string response
" ")))
729 (string-to-number (nth 2 (split-string response
" "))))
732 (defun pop3-list (process &optional msg
)
733 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
734 Otherwise, return the size of the message-id MSG"
735 (pop3-send-command process
(if msg
736 (format "LIST %d" msg
)
738 (let ((response (pop3-read-response process t
)))
740 (string-to-number (nth 2 (split-string response
" ")))
741 (let ((start pop3-read-point
) end
)
742 (with-current-buffer (process-buffer process
)
743 (while (not (re-search-forward "^\\.\r\n" nil t
))
744 (pop3-accept-process-output process
)
746 (setq pop3-read-point
(point-marker))
747 (goto-char (match-beginning 0))
748 (setq end
(point-marker))
749 (mapcar #'(lambda (s) (let ((split (split-string s
" ")))
750 (cons (string-to-number (nth 0 split
))
751 (string-to-number (nth 1 split
)))))
752 (split-string (buffer-substring start end
) "\r\n" t
)))))))
754 (defun pop3-retr (process msg crashbuf
)
755 "Retrieve message-id MSG to buffer CRASHBUF."
756 (pop3-send-command process
(format "RETR %s" msg
))
757 (pop3-read-response process
)
758 (let ((start pop3-read-point
) end
)
759 (with-current-buffer (process-buffer process
)
760 (while (not (re-search-forward "^\\.\r\n" nil t
))
761 (unless (memq (process-status process
) '(open run
))
762 (error "pop3 server closed the connection"))
763 (pop3-accept-process-output process
)
765 (setq pop3-read-point
(point-marker))
766 ;; this code does not seem to work for some POP servers...
767 ;; and I cannot figure out why not.
768 ;; (goto-char (match-beginning 0))
770 ;; (if (not (looking-at "\r\n"))
772 ;; (re-search-forward "\\.\r\n")
773 (goto-char (match-beginning 0))
774 (setq end
(point-marker))
775 (pop3-clean-region start end
)
776 (pop3-munge-message-separator start end
)
777 (with-current-buffer crashbuf
779 (copy-to-buffer crashbuf start end
)
780 (delete-region start end
)
783 (defun pop3-dele (process msg
)
784 "Mark message-id MSG as deleted."
785 (pop3-send-command process
(format "DELE %s" msg
))
786 (pop3-read-response process
))
788 (defun pop3-noop (process msg
)
790 (pop3-send-command process
"NOOP")
791 (pop3-read-response process
))
793 (defun pop3-last (process)
794 "Return highest accessed message-id number for the session."
795 (pop3-send-command process
"LAST")
796 (let ((response (pop3-read-response process t
)))
797 (string-to-number (nth 1 (split-string response
" ")))
800 (defun pop3-rset (process)
801 "Remove all delete marks from current maildrop."
802 (pop3-send-command process
"RSET")
803 (pop3-read-response process
))
807 (defun pop3-quit (process)
808 "Close connection to POP3 server.
809 Tell server to remove all messages marked as deleted, unlock the maildrop,
810 and close the connection."
811 (pop3-send-command process
"QUIT")
812 (pop3-read-response process t
)
814 (with-current-buffer (process-buffer process
)
815 (goto-char (point-max))
816 (delete-process process
))))
818 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
820 ;;; AUTHORIZATION STATE
822 ;; Initial TCP connection
824 ;; Restrictions: none
825 ;; Possible responses:
826 ;; +OK [POP3 server ready]
829 ;; Arguments: a server specific user-id (required)
830 ;; Restrictions: authorization state [after unsuccessful USER or PASS
831 ;; Possible responses:
832 ;; +OK [valid user-id]
833 ;; -ERR [invalid user-id]
836 ;; Arguments: a server/user-id specific password (required)
837 ;; Restrictions: authorization state, after successful USER
838 ;; Possible responses:
839 ;; +OK [maildrop locked and ready]
840 ;; -ERR [invalid password]
841 ;; -ERR [unable to lock maildrop]
845 ;; Restrictions: Only permitted in AUTHORIZATION state.
846 ;; Possible responses:
850 ;;; TRANSACTION STATE
854 ;; Restrictions: transaction state
855 ;; Possible responses:
856 ;; +OK nn mm [# of messages, size of maildrop]
859 ;; Arguments: a message-id (optional)
860 ;; Restrictions: transaction state; msg must not be deleted
861 ;; Possible responses:
862 ;; +OK [scan listing follows]
863 ;; -ERR [no such message]
866 ;; Arguments: a message-id (required)
867 ;; Restrictions: transaction state; msg must not be deleted
868 ;; Possible responses:
869 ;; +OK [message contents follow]
870 ;; -ERR [no such message]
873 ;; Arguments: a message-id (required)
874 ;; Restrictions: transaction state; msg must not be deleted
875 ;; Possible responses:
876 ;; +OK [message deleted]
877 ;; -ERR [no such message]
881 ;; Restrictions: transaction state
882 ;; Possible responses:
887 ;; Restrictions: transaction state
888 ;; Possible responses:
889 ;; +OK nn [highest numbered message accessed]
893 ;; Restrictions: transaction state
894 ;; Possible responses:
895 ;; +OK [all delete marks removed]
898 ;; Arguments: a message-id (optional)
899 ;; Restrictions: transaction state; msg must not be deleted
900 ;; Possible responses:
901 ;; +OK [uidl listing follows]
902 ;; -ERR [no such message]
908 ;; Restrictions: none
909 ;; Possible responses:
910 ;; +OK [TCP connection closed]
914 ;;; pop3.el ends here