1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996-2017 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 <https://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 (defcustom pop3-password nil
74 "Password to use when connecting to POP server."
75 :type
'(choice (const nil
) string
)
78 (defcustom pop3-authentication-scheme
'pass
79 "POP3 authentication scheme.
80 Defaults to `pass', for the standard USER/PASS authentication. The other
81 valid value is `apop'."
82 :type
'(choice (const :tag
"Normal user/password" pass
)
83 (const :tag
"APOP" apop
))
84 :version
"22.1" ;; Oort Gnus
87 (defcustom pop3-stream-length
100
88 "How many messages should be requested at one time.
89 The lower the number, the more latency-sensitive the fetching
90 will be. If your pop3 server doesn't support streaming at all,
96 (defcustom pop3-leave-mail-on-server nil
97 "Non-nil if the mail is to be left on the POP server after fetching.
98 Mails once fetched will never be fetched again by the UIDL control.
100 If this is neither nil nor a number, all mails will be left on the
101 server. If this is a number, leave mails on the server for this many
102 days since you first checked new mails. If this is nil, mails will be
103 deleted on the server right after fetching.
105 Gnus users should use the `:leave' keyword in a mail source to direct
106 the behavior per server, rather than directly modifying this value.
108 Note that POP servers maintain no state information between sessions,
109 so what the client believes is there and what is actually there may
110 not match up. If they do not, then you may get duplicate mails or
111 the whole thing can fall apart and leave you with a corrupt mailbox."
113 :type
'(choice (const :tag
"Don't leave mails" nil
)
114 (const :tag
"Leave all mails" t
)
115 (number :tag
"Leave mails for this many days" :value
14))
118 (defcustom pop3-uidl-file
"~/.pop3-uidl"
119 "File used to save UIDL."
124 (defcustom pop3-uidl-file-backup
'(0 9)
125 "How to backup the UIDL file `pop3-uidl-file' when updating.
126 If it is a list of numbers, the first one binds `kept-old-versions' and
127 the other binds `kept-new-versions' to keep number of oldest and newest
128 versions. Otherwise, the value binds `version-control' (which see).
130 Note: Backup will take place whenever you check new mails on a server.
131 So, you may lose the backup files having been saved before a trouble
132 if you set it so as to make too few backups whereas you have access to
135 :type
'(choice (group :tag
"Keep versions" :format
"\n%v" :indent
3
136 (number :tag
"oldest")
137 (number :tag
"newest"))
139 :match
(lambda (widget value
)
141 (not (and (numberp (car value
))
142 (numberp (car (cdr value
)))))
146 (defvar pop3-timestamp nil
147 "Timestamp returned when initially connected to the POP server.
148 Used for APOP authentication.")
150 (defvar pop3-read-point nil
)
151 (defvar pop3-debug nil
)
153 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
154 ;; comments there for explanations about the values.
157 (if (and (fboundp 'nnheader-accept-process-output
)
158 (boundp 'nnheader-read-timeout
))
159 (defalias 'pop3-accept-process-output
'nnheader-accept-process-output
)
160 ;; Borrowed from `nnheader.el':
161 (defvar pop3-read-timeout
162 (if (memq system-type
'(windows-nt cygwin
))
165 "How long pop3 should wait between checking for the end of output.
166 Shorter values mean quicker response, but are more CPU intensive.")
167 (defun pop3-accept-process-output (process)
168 (accept-process-output
170 (truncate pop3-read-timeout
)
171 (truncate (* (- pop3-read-timeout
172 (truncate pop3-read-timeout
))
176 ;; List of UIDLs of existing messages at present in the server:
177 ;; ("UIDL1" "UIDL2" "UIDL3"...)
179 (defvar pop3-uidl-saved
)
180 ;; Locally saved UIDL data; an alist of the server, the user, and the UIDL
181 ;; and timestamp pairs:
182 ;; (("SERVER_A" ("USER_A1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
183 ;; ("USER_A2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
185 ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
186 ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
188 ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
189 ;; i.e. the return value of `current-time'.
192 (defun pop3-movemail (file)
193 "Transfer contents of a maildrop to the specified FILE.
194 Use streaming commands."
195 (let ((process (pop3-open-server pop3-mailhost pop3-port
))
200 (if pop3-leave-mail-on-server
201 (setq messages
(pop3-uidl-stat process
)
202 total-size
(cadr messages
)
203 messages
(car messages
))
204 (let ((size (pop3-stat process
)))
205 (dotimes (i (car size
)) (push (1+ i
) messages
))
206 (setq messages
(nreverse messages
)
207 total-size
(cadr size
))))
209 (with-current-buffer (process-buffer process
)
210 (pop3-send-streaming-command process
"RETR" messages total-size
)
211 (pop3-write-to-file file messages
)
212 (unless pop3-leave-mail-on-server
213 (pop3-send-streaming-command process
"DELE" messages nil
))))
214 (if pop3-leave-mail-on-server
215 (when (prog1 (pop3-uidl-dele process
) (pop3-quit process
))
218 ;; Remove UIDL data for the account that got not to leave mails.
219 (setq pop3-uidl-saved
(pop3-uidl-load))
220 (let ((elt (assoc pop3-maildrop
221 (cdr (assoc pop3-mailhost pop3-uidl-saved
)))))
227 (defun pop3-send-streaming-command (process command messages total-size
)
229 (let ((count (length messages
))
231 (start-point (point-min))
234 (process-send-string process
(format "%s %d\r\n" command
(pop messages
)))
235 ;; Only do 100 messages at a time to avoid pipe stalls.
236 (when (zerop (% i pop3-stream-length
))
238 (pop3-wait-for-messages process pop3-stream-length
239 total-size start-point
))
240 (incf waited-for pop3-stream-length
))
242 (pop3-wait-for-messages process
(- count waited-for
)
243 total-size start-point
)))
245 (defun pop3-wait-for-messages (process count total-size start-point
)
247 (goto-char start-point
)
248 (while (or (and (re-search-forward "^\\+OK" nil t
)
250 (re-search-forward "^\\.\r?\n" nil t
)))
251 (re-search-forward "^-ERR " nil t
))
253 (setq start-point
(point)))
254 (unless (memq (process-status process
) '(open run
))
255 (error "pop3 process died"))
258 (goto-char (point-min))
259 (while (re-search-forward "^\\+OK.*\n" nil t
)
260 (setq size
(+ size
(- (point))
261 (if (re-search-forward "^\\.\r?\n" nil
'move
)
264 (message "pop3 retrieved %dKB (%d%%)"
265 (truncate (/ size
1000))
266 (truncate (* (/ (* size
1.0) total-size
) 100)))))
267 (pop3-accept-process-output process
))
270 (defun pop3-write-to-file (file messages
)
271 (let ((pop-buffer (current-buffer))
276 (setq temp-buffer
(current-buffer))
277 (with-current-buffer pop-buffer
278 (goto-char (point-min))
279 (while (re-search-forward "^\\+OK" nil t
)
282 (when (re-search-forward "^\\.\r?\n" nil t
)
286 (with-current-buffer temp-buffer
287 (goto-char (point-max))
288 (let ((hstart (point)))
289 (insert-buffer-substring pop-buffer beg end
)
290 (pop3-clean-region hstart
(point))
291 (goto-char (point-max))
292 (pop3-munge-message-separator hstart
(point))
293 (when pop3-leave-mail-on-server
294 (pop3-uidl-add-xheader hstart
(pop messages
)))
295 (goto-char (point-max))))))
296 (let ((coding-system-for-write 'binary
))
297 (goto-char (point-min))
298 ;; Check whether something inserted a newline at the start and
302 (write-region (point-min) (point-max) file nil
'nomesg
)))))
304 (defun pop3-logon (process)
305 (let ((pop3-password pop3-password
))
306 ;; for debugging only
307 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
308 ;; query for password
309 (if (and pop3-password-required
(not pop3-password
))
311 (read-passwd (format "Password for %s: " pop3-maildrop
))))
312 (cond ((equal 'apop pop3-authentication-scheme
)
313 (pop3-apop process pop3-maildrop
))
314 ((equal 'pass pop3-authentication-scheme
)
315 (pop3-user process pop3-maildrop
)
317 (t (error "Invalid POP3 authentication scheme")))))
319 (defun pop3-get-message-count ()
320 "Return the number of messages in the maildrop."
321 (let* ((process (pop3-open-server pop3-mailhost pop3-port
))
323 (pop3-password pop3-password
))
324 ;; for debugging only
325 (if pop3-debug
(switch-to-buffer (process-buffer process
)))
326 ;; query for password
327 (if (and pop3-password-required
(not pop3-password
))
329 (read-passwd (format "Password for %s: " pop3-maildrop
))))
330 (cond ((equal 'apop pop3-authentication-scheme
)
331 (pop3-apop process pop3-maildrop
))
332 ((equal 'pass pop3-authentication-scheme
)
333 (pop3-user process pop3-maildrop
)
335 (t (error "Invalid POP3 authentication scheme")))
336 (setq message-count
(car (pop3-stat process
)))
340 (defun pop3-uidl-stat (process)
341 "Return a list of unread message numbers and total size."
342 (pop3-send-command process
"UIDL")
343 (let (err messages size
)
344 (if (condition-case code
346 (pop3-read-response process
)
348 (error (setq err
(error-message-string code
))
350 (let ((start pop3-read-point
)
352 (with-current-buffer (process-buffer process
)
353 (while (not (re-search-forward "^\\.\r\n" nil t
))
354 (unless (memq (process-status process
) '(open run
))
355 (error "pop3 server closed the connection"))
356 (pop3-accept-process-output process
)
358 (setq pop3-read-point
(point-marker)
360 (while (progn (forward-line -
1) (>= (point) start
))
361 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
362 (push (match-string 1) pop3-uidl
)))
364 (setq pop3-uidl-saved
(pop3-uidl-load)
365 saved
(cdr (assoc pop3-maildrop
366 (cdr (assoc pop3-mailhost
368 (let ((i (length pop3-uidl
)))
370 (unless (member (nth (1- i
) pop3-uidl
) saved
)
374 (setq list
(pop3-list process
)
376 (dolist (msg messages
)
377 (setq size
(+ size
(cdr (assq msg list
)))))
378 (list messages size
)))))
379 (message "%s doesn't support UIDL (%s), so we try a regressive way..."
382 (setq size
(pop3-stat process
))
383 (dotimes (i (car size
)) (push (1+ i
) messages
))
384 (setcar size
(nreverse messages
))
387 (defun pop3-uidl-dele (process)
388 "Delete messages according to `pop3-leave-mail-on-server'.
389 Return non-nil if it is necessary to update the local UIDL file."
390 (let* ((ctime (current-time))
391 (srvr (assoc pop3-mailhost pop3-uidl-saved
))
392 (saved (assoc pop3-maildrop
(cdr srvr
)))
393 i uidl mod new tstamp dele
)
394 (setcdr (cdr ctime
) nil
)
395 ;; Add new messages to the data to be saved.
396 (cond ((and pop3-uidl saved
)
397 (setq i
(1- (length pop3-uidl
)))
399 (unless (member (setq uidl
(nth i pop3-uidl
)) (cdr saved
))
404 (setq new
(mapcan (lambda (elt) (list elt ctime
)) pop3-uidl
))))
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