1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
7 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
8 ;; Katsumi Yamaoka <yamaoka@jpl.org>
9 ;; Maintainer: Volunteers
12 ;; This file is part of T-gnus.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
32 ;; are implemented. The LIST command has not been implemented due to lack
33 ;; of actual usefulness.
34 ;; The optional POP3 command TOP has not been implemented.
36 ;; This program was inspired by Kyle E. Jones's vm-pop program.
40 ;; You can use this program for Gnus, without needing any modification.
41 ;; There are two ways to do that; one is to replace Gnus' pop3.el with
42 ;; it when installing Gnus; the other is to replace Gnus' pop3.el which
43 ;; has been installed with this module and byte-compile it.
45 ;; Note: you should not modify the value for the `pop' section of the
46 ;; `mail-source-keyword-map' variable.
48 ;; This program provides the following features in addition to Gnus:
50 ;; 1. You can use SSL or STARTTLS stream to connect to mail servers.
51 ;; For example, specify the `:connection' keyword and the value pair
52 ;; in a mail-source as follows:
54 ;;(setq mail-sources '((pop :server "pop3.mail.server" :port 995
55 ;; :connection ssl :authentication apop)))
57 ;; For STARTTLS stream, use `tls' isntead of `ssl'. The default
58 ;; connection type is defined by `pop3-connection-type' which
61 ;; 2. You can fetch mails without deleting them in mail servers. To do
62 ;; that, specify the `:leave' keyword with the value t as follows:
64 ;;(setq mail-sources '((pop :server "pop3.mail.server" :leave t)))
66 ;; Already read mails are registered into the ~/.uidls-SERVER file
67 ;; (which is the default, see `pop3-uidl-file-name'), and you will
68 ;; never need to fetch them twice. The default value for the
69 ;; `:leave' keyword is specified by the `pop3-leave-mail-on-server'
70 ;; variable. You have no need to modify that value normally.
72 ;; 3. See the source code for some other miscellaneous extended features.
82 "Post Office Protocol"
86 (defcustom pop3-maildrop (or (user-login-name)
90 :version "21.4" ;; Oort Gnus
94 (defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
97 :version "21.4" ;; Oort Gnus
101 (defcustom pop3-port 110
103 :version "21.4" ;; Oort Gnus
107 (defcustom pop3-connection-type nil
108 "*POP3 connection type."
109 :type '(choice (const :tag "Not specified" nil)
114 (defcustom pop3-password-required t
115 "*Non-nil if a password is required when connecting to POP server."
116 :version "21.4" ;; Oort Gnus
120 ;; Should this be customizable?
121 (defvar pop3-password nil
122 "*Password to use when connecting to POP server.")
124 (defcustom pop3-authentication-scheme 'pass
125 "*POP3 authentication scheme.
126 Defaults to 'pass, for the standard USER/PASS authentication. Other valid
128 :version "21.4" ;; Oort Gnus
129 :type '(choice (const :tag "USER/PASS" pass)
130 (const :tag "APOP" apop))
133 (defcustom pop3-leave-mail-on-server nil
134 "*Non-nil if the mail is to be left on the POP server after fetching."
135 :version "21.4" ;; Oort Gnus
139 (defvar pop3-timestamp nil
140 "Timestamp returned when initially connected to the POP server.
141 Used for APOP authentication.")
143 (defcustom pop3-maximum-message-size nil
144 "If non-nil only download messages smaller than this."
145 :type '(choice (const :tag "Unlimited" nil)
146 (integer :tag "Maximum size" :format "%t: %v\n" :size 0))
149 (defcustom pop3-except-header-regexp nil
150 "If non-nil we do not retrieve messages whose headers are matching this regexp."
151 :type '(choice (const :tag "Retrieve any messages" nil)
152 (regexp :format "%t: %v\n" :size 0))
155 (defcustom pop3-uidl-file-name "~/.uidls"
156 "File in which to store the UIDL of processed messages."
157 :type '(file :format "%t: %v\n" :size 0)
160 (defvar pop3-uidl-support nil
161 "Alist of servers and flags of whether they support UIDLs.
162 Users don't have to set this value.")
164 (defvar pop3-uidl-obarray (make-vector 31 0)
167 (defvar pop3-read-point nil)
168 (defvar pop3-debug nil)
171 (autoload 'starttls-open-stream "starttls")
172 (autoload 'starttls-negotiate "starttls"))
174 (defcustom pop3-ssl-program-name
175 (if (executable-find "openssl")
178 "The program to run in a subprocess to open an SSL connection."
179 :type '(string :format "%t: %v\n" :size 0)
182 (defcustom pop3-ssl-program-arguments
183 '("s_client" "-quiet")
184 "Arguments to be passed to the program `pop3-ssl-program-name'."
185 :type '(repeat (string :format "%v\n" :size 0))
188 (defun pop3-progress-message (format percent &rest args)
189 (apply (function message) format args))
191 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
192 (defvar pop3-read-timeout
193 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
194 (symbol-name system-type))
195 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
197 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
199 ;; There should probably be a runtime test to determine the timing
200 ;; resolution, or a primitive to report it. I don't know off-hand
201 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
202 ;; could round up non-zero timeouts to a minimum of 1.0?
205 "How long pop3 should wait between checking for the end of output.
206 Shorter values mean quicker response, but are more CPU intensive.")
208 ;; Borrowed from nnheader-accept-process-output in nnheader.el.
209 (defun pop3-accept-process-output (process)
210 (accept-process-output
212 (truncate pop3-read-timeout)
213 (truncate (* (- pop3-read-timeout
214 (truncate pop3-read-timeout))
217 (defun pop3-movemail (&optional crashbox)
218 "Transfer contents of a maildrop to the specified CRASHBOX."
219 (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
220 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
221 (crashbuf (get-buffer-create " *pop3-retr*"))
224 (pop3-password pop3-password)
225 (pop3-uidl-file-name (convert-standard-filename
226 (concat pop3-uidl-file-name "-"
228 retrieved-messages messages)
229 ;; for debugging only
230 (if pop3-debug (switch-to-buffer (process-buffer process)))
231 ;; query for password
232 (if (and pop3-password-required (not pop3-password))
234 (read-passwd (format "Password for %s: " pop3-maildrop))))
235 (cond ((equal 'apop pop3-authentication-scheme)
236 (pop3-apop process pop3-maildrop))
237 ((equal 'pass pop3-authentication-scheme)
238 (pop3-user process pop3-maildrop)
240 (t (error "Invalid POP3 authentication scheme")))
241 ;; get messages that are suitable for download
242 (message "Retrieving message list...")
243 (setq messages (pop3-get-message-numbers process)
244 message-count (length (cdr messages)))
245 (message "Retrieving message list...%d of %d unread"
246 message-count (pop messages))
248 (unless (not (stringp crashbox))
250 (pop3-progress-message
251 "Retrieving message %d of %d (%d octets) from %s..."
252 (floor (* (/ (float n) message-count) 100))
253 n message-count (cdar messages) pop3-mailhost)
254 (pop3-retr process (caar messages) crashbuf)
255 (push (caar messages) retrieved-messages)
256 (setq messages (cdr messages)
258 (with-current-buffer crashbuf
259 (let ((coding-system-for-write 'binary)
260 jka-compr-compression-info-list jam-zcat-filename-list)
261 (write-region (point-min) (point-max)
262 crashbox 'append 'nomesg)))
263 ;; mark messages as read
264 (when pop3-leave-mail-on-server
266 ;; now delete the messages we have retrieved
267 (unless pop3-leave-mail-on-server
268 (dolist (n retrieved-messages)
269 (message "Deleting message %d of %d from %s..."
270 n message-count pop3-mailhost)
271 (pop3-dele process n))))
273 (kill-buffer crashbuf)
276 (defun pop3-get-message-count ()
277 "Return the number of messages in the maildrop."
278 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
280 (pop3-password pop3-password))
281 ;; for debugging only
282 (if pop3-debug (switch-to-buffer (process-buffer process)))
283 ;; query for password
284 (if (and pop3-password-required (not pop3-password))
286 (read-passwd (format "Password for %s: " pop3-maildrop))))
287 (cond ((equal 'apop pop3-authentication-scheme)
288 (pop3-apop process pop3-maildrop))
289 ((equal 'pass pop3-authentication-scheme)
290 (pop3-user process pop3-maildrop)
292 (t (error "Invalid POP3 authentication scheme")))
293 (setq message-count (car (pop3-stat process)))
297 (defun pop3-open-server (mailhost port)
298 "Open TCP connection to MAILHOST on PORT.
299 Returns the process associated with the connection.
300 Argument PORT specifies connecting port."
303 (set-buffer (get-buffer-create (concat " trace of POP session to "
306 (setq pop3-read-point (point-min))
310 ((eq pop3-connection-type 'ssl)
311 (pop3-open-ssl-stream "POP" (current-buffer) mailhost port))
312 ((eq pop3-connection-type 'tls)
313 (pop3-open-tls-stream "POP" (current-buffer) mailhost port))
315 (let ((coding-system-for-read 'binary)
316 (coding-system-for-write 'binary))
317 (open-network-stream "POP" (current-buffer) mailhost port)))))
318 (let ((response (pop3-read-response process t)))
320 (substring response (or (string-match "<" response) 0)
321 (+ 1 (or (string-match ">" response) -1)))))
325 (autoload 'open-ssl-stream "ssl"))
327 (defun pop3-open-ssl-stream-1 (name buffer host service extra-arg)
329 (let* ((ssl-program-name
330 pop3-ssl-program-name)
331 (ssl-program-arguments
332 `(,@pop3-ssl-program-arguments
334 "-connect" ,(format "%s:%d" host service)))
335 (process (open-ssl-stream name buffer host service)))
337 (with-current-buffer buffer
338 (goto-char (point-min))
339 (while (and (memq (process-status process) '(open run))
340 (goto-char (point-max))
342 (not (looking-at "+OK")))
343 (pop3-accept-process-output process)
345 (delete-region (point-min) (point)))
346 (and process (memq (process-status process) '(open run))
349 (defun pop3-open-ssl-stream (name buffer host service)
350 "Open a SSL connection for a service to a host.
351 Returns a subprocess-object to represent the connection.
352 Args are NAME BUFFER HOST SERVICE."
353 (let (selective-display ;; Disable ^M to nl translation.
354 (coding-system-for-read 'binary)
355 (coding-system-for-write 'binary))
356 (or (pop3-open-ssl-stream-1 name buffer host service "-ssl3")
357 (pop3-open-ssl-stream-1 name buffer host service "-ssl2"))))
359 (defun pop3-open-tls-stream (name buffer host service)
360 "Open a TLSv1 connection for a service to a host.
361 Returns a subprocess-object to represent the connection.
362 Args are NAME BUFFER HOST SERVICE."
363 (let* (selective-display ;; Disable ^M to nl translation.
364 (coding-system-for-read 'binary)
365 (coding-system-for-write 'binary)
366 (process (starttls-open-stream name buffer host service)))
368 (starttls-negotiate process)
373 (defun pop3-process-filter (process output)
375 (set-buffer (process-buffer process))
376 (goto-char (point-max))
379 (defun pop3-send-command (process command)
380 (set-buffer (process-buffer process))
381 (goto-char (point-max))
382 ;; (if (= (aref command 0) ?P)
383 ;; (insert "PASS <omitted>\r\n")
384 ;; (insert command "\r\n"))
385 (setq pop3-read-point (point))
386 (goto-char (point-max))
387 (process-send-string process (concat command "\r\n")))
389 (defun pop3-read-response (process &optional return)
390 "Read the response from the server PROCESS.
391 Return the response string if optional second argument RETURN is non-nil."
392 (let ((case-fold-search nil)
395 (set-buffer (process-buffer process))
396 (goto-char pop3-read-point)
397 (while (and (memq (process-status process) '(open run))
398 (not (search-forward "\r\n" nil t)))
399 (pop3-accept-process-output process)
400 (goto-char pop3-read-point))
401 (setq match-end (point))
402 (goto-char pop3-read-point)
403 (if (looking-at "-ERR")
404 (error (buffer-substring (point) (- match-end 2)))
405 (if (not (looking-at "+OK"))
406 (progn (setq pop3-read-point match-end) nil)
407 (setq pop3-read-point match-end)
409 (buffer-substring (point) match-end)
412 (defun pop3-clean-region (start end)
413 (setq end (set-marker (make-marker) end))
416 (while (and (< (point) end) (search-forward "\r\n" end t))
417 (replace-match "\n" t t))
419 (while (re-search-forward "\n\n\\(From \\)" end t)
420 (replace-match "\n\n>\\1" t nil))
422 (while (and (< (point) end) (re-search-forward "^\\." end t))
423 (replace-match "" t t)
425 (set-marker end nil))
427 (eval-when-compile (defvar parse-time-months))
429 ;; Copied from message-make-date.
430 (defun pop3-make-date (&optional now)
431 "Make a valid date header.
432 If NOW, use that time instead."
433 (require 'parse-time)
434 (let* ((now (or now (current-time)))
435 (zone (nth 8 (decode-time now)))
439 (setq zone (- zone)))
441 (format-time-string "%d" now)
442 ;; The month name of the %b spec is locale-specific. Pfff.
444 (capitalize (car (rassoc (nth 4 (decode-time now))
445 parse-time-months))))
446 (format-time-string "%Y %H:%M:%S " now)
447 ;; We do all of this because XEmacs doesn't have the %z spec.
448 (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
450 (defun pop3-munge-message-separator (start end)
451 "Check to see if a message separator exists. If not, generate one."
454 (narrow-to-region start end)
455 (goto-char (point-min))
456 (if (not (or (looking-at "From .?") ; Unix mail
457 (looking-at "\001\001\001\001\n") ; MMDF
458 (looking-at "BABYL OPTIONS:"))) ; Babyl
459 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
460 (tdate (mail-fetch-field "Date"))
461 (date (split-string (or (and tdate
462 (not (string= "" tdate))
467 ;; sample date formats I have seen
468 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
469 ;; Date: 08 Jul 1996 23:22:24 -0400
471 ;; Tue Jul 9 09:04:21 1996
474 "Tue Jan 1 00:00:0 1900")
475 ((string-match "[A-Z]" (nth 0 date))
476 (format "%s %s %s %s %s"
477 (nth 0 date) (nth 2 date) (nth 1 date)
478 (nth 4 date) (nth 3 date)))
480 ;; this really needs to be better but I don't feel
481 ;; like writing a date to day converter.
482 (format "Sun %s %s %s %s"
483 (nth 1 date) (nth 0 date)
484 (nth 3 date) (nth 2 date)))))
485 (setq From_ (format "\nFrom %s %s\n" from date))
486 (while (string-match "," From_)
487 (setq From_ (concat (substring From_ 0 (match-beginning 0))
488 (substring From_ (match-end 0)))))
489 (goto-char (point-min))
491 (if (search-forward "\n\n" nil t)
493 (goto-char (point-max))
495 (narrow-to-region (point) (point-max))
496 (let ((size (- (point-max) (point-min))))
497 (goto-char (point-min))
500 (insert (format "Content-Length: %s\n" size))))))))
504 (defun pop3-get-message-numbers (process)
505 "Get the list of message numbers and lengths to retrieve via PROCESS."
506 ;; we use the LIST comand first anyway to get the message lengths.
507 ;; then if we're leaving mail on the server, see if the UIDL command
508 ;; is implemented. if so, we use it to get the message number list.
509 (let* ((messages (pop3-list process))
510 (total (or (pop messages) 0))
511 (uidl (if pop3-leave-mail-on-server
512 (pop3-get-uidl process)))
515 ;; only retrieve messages matching our regexp or in the uidl list
517 ;; remove elements not in the uidl, this assumes the uidl is short
518 (or (not (and pop3-leave-mail-on-server
519 (cdr (assoc pop3-mailhost pop3-uidl-support))))
520 (memq (caar messages) uidl))
522 ;; don't download messages that are too large
523 (not (and pop3-maximum-message-size
524 (> (cdar messages) pop3-maximum-message-size)))
525 (not (and pop3-except-header-regexp
526 (string-match pop3-except-header-regexp
527 (pop3-top process (caar messages) 0)))))
528 (push (car messages) out))
529 (setq messages (cdr messages)))
530 (cons total (nreverse out))))
532 (defun pop3-get-uidl (process)
533 "Use PROCESS to get a list of unread message numbers."
534 (let ((messages (pop3-uidl process))
535 (support (assoc pop3-mailhost pop3-uidl-support))
538 (setcdr support (and messages t))
539 (push (cons pop3-mailhost (and messages t))
544 (when (file-readable-p pop3-uidl-file-name)
545 (insert-file-contents pop3-uidl-file-name))
546 (goto-char (point-min))
547 (while (looking-at "\\([^ \n\t]+\\)")
548 (set (intern (match-string 1) pop3-uidl-obarray)
551 (dolist (message (cdr messages))
552 (if (setq uidl (intern-soft (cdr message) pop3-uidl-obarray))
553 (setcar (symbol-value uidl) (car message))
554 (set (intern (cdr message) pop3-uidl-obarray)
555 (cons (car message) nil))))
556 (pop3-get-unread-message-numbers))))
558 (defun pop3-get-unread-message-numbers ()
559 "Return a sorted list of unread msg numbers to retrieve."
561 (mapatoms (lambda (atom)
562 (if (not (cdr (symbol-value atom)))
563 (push (car (symbol-value atom)) nums)))
567 (defun pop3-save-uidls ()
568 "Save the updated UIDLs to disk for use next time."
569 (when (and pop3-leave-mail-on-server
570 ;; UIDL hash table is non-empty
571 (let ((len (length pop3-uidl-obarray)))
573 (setq len (if (symbolp (aref pop3-uidl-obarray (1- len)))
576 (when (file-readable-p pop3-uidl-file-name)
577 (copy-file pop3-uidl-file-name
578 (concat pop3-uidl-file-name ".old")
579 'overwrite 'keeptime))
581 (with-temp-file pop3-uidl-file-name
584 (when (car (symbol-value atom))
585 (insert (format "%s\n" atom))))
587 (fillarray pop3-uidl-obarray 0)))
592 ;; AUTHORIZATION STATE
594 (defun pop3-user (process user)
595 "Send USER information to POP3 server."
596 (pop3-send-command process (format "USER %s" user))
597 (let ((response (pop3-read-response process t)))
598 (if (not (and response (string-match "+OK" response)))
599 (error (format "USER %s not valid" user)))))
601 (defun pop3-pass (process)
602 "Send authentication information to the server."
603 (pop3-send-command process (format "PASS %s" pop3-password))
604 (let ((response (pop3-read-response process t)))
605 (if (not (and response (string-match "+OK" response)))
606 (pop3-quit process))))
608 (defun pop3-apop (process user)
609 "Send alternate authentication information to the server."
610 (let ((pass pop3-password))
611 (if (and pop3-password-required (not pass))
613 (read-passwd (format "Password for %s: " pop3-maildrop))))
615 ;; Note that `md5' should never encode a given string to use for
616 ;; the apop authentication, so we should specify `binary'.
617 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
618 (pop3-send-command process (format "APOP %s %s" user hash))
619 (let ((response (pop3-read-response process t)))
620 (if (not (and response (string-match "+OK" response)))
621 (pop3-quit process)))))))
623 (defun pop3-stls (process)
624 "Query whether TLS extension is supported"
625 (pop3-send-command process "STLS")
626 (let ((response (pop3-read-response process t)))
627 (if (not (and response (string-match "+OK" response)))
628 (pop3-quit process))))
632 (defun pop3-stat (process)
633 "Return the number of messages in the maildrop and the maildrop's size."
634 (pop3-send-command process "STAT")
635 (let ((response (pop3-read-response process t)))
636 (list (string-to-int (nth 1 (split-string response " ")))
637 (string-to-int (nth 2 (split-string response " "))))))
639 (defun pop3-retr (process msg crashbuf)
640 "Retrieve message-id MSG to buffer CRASHBUF."
641 (pop3-send-command process (format "RETR %s" msg))
642 (pop3-read-response process)
644 (let ((region (pop3-get-extended-response process)))
645 (pop3-munge-message-separator (car region) (cadr region))
646 (append-to-buffer crashbuf (car region) (cadr region))
647 (delete-region (car region) (cadr region)))))
649 (defun pop3-dele (process msg)
650 "Mark message-id MSG as deleted."
651 (pop3-send-command process (format "DELE %s" msg))
652 (pop3-read-response process))
654 (defun pop3-noop (process msg)
656 (pop3-send-command process "NOOP")
657 (pop3-read-response process))
659 (defun pop3-last (process)
660 "Return highest accessed message-id number for the session."
661 (pop3-send-command process "LAST")
662 (let ((response (pop3-read-response process t)))
663 (string-to-int (nth 1 (split-string response " ")))))
665 (defun pop3-rset (process)
666 "Remove all delete marks from current maildrop."
667 (pop3-send-command process "RSET")
668 (pop3-read-response process))
672 (defun pop3-quit (process)
673 "Close connection to POP3 server.
674 Tell server to remove all messages marked as deleted, unlock the maildrop,
675 and close the connection."
676 (pop3-send-command process "QUIT")
677 (pop3-read-response process t)
680 (set-buffer (process-buffer process))
681 (goto-char (point-max))
682 (delete-process process))))
684 (defun pop3-uidl (process &optional msgno)
685 "Return the results of a UIDL command in PROCESS for optional MSGNO.
686 If UIDL is unsupported on this mail server or if msgno is invalid, return nil.
687 Otherwise, return a list in the form
689 (N (1 UIDL-1) (2 UIDL-2) ... (N UIDL-N))
693 N is an integer for the number of UIDLs returned (could be 0)
697 (pop3-send-command process (format "UIDL %d" msgno))
698 (pop3-send-command process "UIDL"))
700 (if (null (pop3-read-response process t))
701 nil ;; UIDL is not supported on this server
705 (apply 'narrow-to-region (pop3-get-extended-response process))
706 (goto-char (point-min))
707 (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
708 (setq msgno (string-to-int (match-string 1))
709 uidl (match-string 2))
710 (push (cons msgno uidl) pairs)
711 (beginning-of-line 2))
712 (cons (length pairs) (nreverse pairs)))))))
714 (defun pop3-list (process &optional msgno)
715 "Return the results of a LIST command for PROCESS and optional MSGNO.
716 If (optional) msgno is invalid, return nil. Otherwise, return a list
719 (N (1 LEN-1) (2 LEN-2) ... (N LEN-N))
723 N is an integer for the number of msg/len pairs (could be 0)
724 LEN-n is an integer."
726 (pop3-send-command process (format "LIST %d" msgno))
727 (pop3-send-command process "LIST"))
729 (if (null (pop3-read-response process t))
730 nil ;; MSGNO is not valid number
734 (apply 'narrow-to-region (pop3-get-extended-response process))
735 (goto-char (point-min))
736 (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
737 (setq msgno (string-to-int (match-string 1))
738 len (string-to-int (match-string 2)))
739 (push (cons msgno len) pairs)
740 (beginning-of-line 2))
741 (cons (length pairs) (nreverse pairs)))))))
743 (defun pop3-top (process msgno &optional lines)
744 "Return the top LINES of messages for PROCESS and MSGNO.
745 If msgno is invalid, return nil. Otherwise, return a string."
746 (pop3-send-command process (format "TOP %d %d" msgno (or lines 1)))
747 (if (pop3-read-response process t)
748 nil ;; MSGNO is not valid number
750 (apply 'buffer-substring (pop3-get-extended-response process)))))
754 (defun pop3-get-extended-response (process)
755 "Get the extended pop3 response in the PROCESS buffer."
756 (let ((start pop3-read-point) end)
757 (set-buffer (process-buffer process))
759 (while (not (re-search-forward "^\\.\r\n" nil t))
760 (pop3-accept-process-output process)
762 (setq pop3-read-point (point-marker))
763 (goto-char (match-beginning 0))
764 (setq end (point-marker))
765 (pop3-clean-region start end)
768 ;;; Advise the mail-source function in order to use this module in Gnus.
770 (eval-after-load "mail-source"
771 '(if (member '(:connection)
772 (assq 'pop (symbol-value 'mail-source-keyword-map)))
773 nil ;; T-gnus is running.
774 (defadvice mail-source-fetch-pop (around bind-t-gnus-keywords activate)
775 "Bind `pop3-connection-type' and `pop3-leave-mail-on-server' according
776 to `mail-sources' while fetching mails with Gnus."
777 (let ((pop3-connection-type (or (plist-get (cdr (ad-get-arg 0))
779 pop3-connection-type))
780 (pop3-leave-mail-on-server (or (plist-get (cdr (ad-get-arg 0))
782 pop3-leave-mail-on-server)))
786 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
788 ;;; AUTHORIZATION STATE
790 ;; Initial TCP connection
792 ;; Restrictions: none
793 ;; Possible responses:
794 ;; +OK [POP3 server ready]
797 ;; Arguments: a server specific user-id (required)
798 ;; Restrictions: authorization state [after unsuccessful USER or PASS
799 ;; Possible responses:
800 ;; +OK [valid user-id]
801 ;; -ERR [invalid user-id]
804 ;; Arguments: a server/user-id specific password (required)
805 ;; Restrictions: authorization state, after successful USER
806 ;; Possible responses:
807 ;; +OK [maildrop locked and ready]
808 ;; -ERR [invalid password]
809 ;; -ERR [unable to lock maildrop]
813 ;; Restrictions: authorization state
814 ;; Possible responses:
815 ;; +OK [negotiation is ready]
816 ;; -ERR [security layer is already active]
818 ;;; TRANSACTION STATE
822 ;; Restrictions: transaction state
823 ;; Possible responses:
824 ;; +OK nn mm [# of messages, size of maildrop]
827 ;; Arguments: a message-id (optional)
828 ;; Restrictions: transaction state; msg must not be deleted
829 ;; Possible responses:
830 ;; +OK [scan listing follows]
831 ;; -ERR [no such message]
834 ;; Arguments: a message-id (required), number of lines (optional)
835 ;; Restrictions: transaction state; msg must not be deleted
836 ;; Possible responses:
837 ;; +OK [partial message listing follows]
838 ;; -ERR [no such message]
841 ;; Arguments: a message-id (optional)
842 ;; Restrictions: transaction state; msg must not be deleted
843 ;; Possible responses:
844 ;; +OK [uidl listing follows]
845 ;; -ERR [no such message]
848 ;; Arguments: a message-id (required)
849 ;; Restrictions: transaction state; msg must not be deleted
850 ;; Possible responses:
851 ;; +OK [message contents follow]
852 ;; -ERR [no such message]
855 ;; Arguments: a message-id (required)
856 ;; Restrictions: transaction state; msg must not be deleted
857 ;; Possible responses:
858 ;; +OK [message deleted]
859 ;; -ERR [no such message]
863 ;; Restrictions: transaction state
864 ;; Possible responses:
869 ;; Restrictions: transaction state
870 ;; Possible responses:
871 ;; +OK nn [highest numbered message accessed]
875 ;; Restrictions: transaction state
876 ;; Possible responses:
877 ;; +OK [all delete marks removed]
883 ;; Restrictions: none
884 ;; Possible responses:
885 ;; +OK [TCP connection closed]
889 ;;; pop3.el ends here