Synch with Gnus.
[elisp/gnus.git-] / lisp / pop3.el
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
7 ;;      Daiki Ueno  <ueno@ueda.info.waseda.ac.jp>
8 ;; Maintainer: FSF
9 ;; Keywords: mail
10 ;; Version: 1.3s
11
12 ;; This file is part of GNU Emacs.
13
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)
17 ;; any later version.
18
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.
23
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.
28
29 ;;; Commentary:
30
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.
35
36 ;; This program was inspired by Kyle E. Jones's vm-pop program.
37
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))
41 (eval-when-compile (require 'static))
42
43 (require 'mail-utils)
44
45 (defconst pop3-version "1.3s")
46
47 (defvar pop3-maildrop (or (user-login-name) (getenv "LOGNAME") (getenv "USER") nil)
48   "*POP3 maildrop.")
49 (defvar pop3-mailhost (or (getenv "MAILHOST") nil)
50   "*POP3 mailhost.")
51 (defvar pop3-port 110
52   "*POP3 port.")
53 (defvar pop3-connection-type nil
54   "*POP3 connection type.")
55
56 (defvar pop3-password-required t
57   "*Non-nil if a password is required when connecting to POP server.")
58 (defvar pop3-password nil
59   "*Password to use when connecting to POP server.")
60
61 (defvar pop3-authentication-scheme 'pass
62   "*POP3 authentication scheme.
63 Defaults to 'pass, for the standard USER/PASS authentication.  Other valid
64 values are 'apop.")
65
66 (defvar pop3-timestamp nil
67   "Timestamp returned when initially connected to the POP server.
68 Used for APOP authentication.")
69
70 (defvar pop3-leave-mail-on-server nil
71   "Non-nil if mail is to be left on the server and UIDL used for message retrieval.")
72
73 (defvar pop3-maximum-message-size nil
74   "If non-nil only download messages smaller than this.")
75
76 (defvar pop3-except-header-regexp nil
77   "If non-nil we do not retrieve messages whose headers are matching this regexp.")
78
79 (defvar pop3-uidl-file-name "~/.uidls"
80   "File in which to store the UIDL of processed messages.")
81
82 (defvar pop3-uidl-support 'dont-know
83   "Whether the server supports UIDL.
84 Nil means no, t means yes, not-nil-or-t means yet to be determined.")
85
86 (defvar pop3-uidl-obarray (make-vector 31 0)
87   "Uidl hash table.")
88
89 (defvar pop3-read-point nil)
90 (defvar pop3-debug nil)
91
92 (eval-and-compile
93   (autoload 'open-ssl-stream "ssl")
94   (autoload 'starttls-open-stream "starttls")
95   (autoload 'starttls-negotiate "starttls"))
96
97 (defvar pop3-ssl-program-name
98   (if (exec-installed-p "openssl")
99       "openssl"
100     "ssleay")
101   "The program to run in a subprocess to open an SSL connection.")
102
103 (defvar pop3-ssl-program-arguments
104   '("s_client" "-quiet")
105   "Arguments to be passed to the program `pop3-ssl-program-name'.")
106
107 (defun pop3-progress-message (format percent &rest args)
108   (apply (function message) format args))
109
110 (defun pop3-movemail (&optional crashbox)
111   "Transfer contents of a maildrop to the specified CRASHBOX."
112   (or crashbox (setq crashbox (expand-file-name "~/.crashbox")))
113   (let* ((process (pop3-open-server pop3-mailhost pop3-port))
114          (crashbuf (get-buffer-create " *pop3-retr*"))
115          (n 1)
116          message-count
117          (pop3-password pop3-password)
118          (pop3-uidl-file-name (convert-standard-filename
119                                (concat pop3-uidl-file-name "-"
120                                        pop3-mailhost)))
121          retrieved-messages messages)
122     ;; for debugging only
123     (if pop3-debug (switch-to-buffer (process-buffer process)))
124     ;; query for password
125     (if (and pop3-password-required (not pop3-password))
126         (setq pop3-password
127               (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
128     (cond ((equal 'apop pop3-authentication-scheme)
129            (pop3-apop process pop3-maildrop))
130           ((equal 'pass pop3-authentication-scheme)
131            (pop3-user process pop3-maildrop)
132            (pop3-pass process))
133           (t (error "Invalid POP3 authentication scheme")))
134     ;; get messages that are suitable for download
135     (message "Retrieving message list...")
136     (setq messages (pop3-get-message-numbers process)
137           message-count (length (cdr messages)))
138     (message "Retrieving message list...%d of %d unread"
139              message-count (pop messages))
140     (unwind-protect
141         (unless (not (stringp crashbox))
142           (while messages
143             (pop3-progress-message
144              "Retrieving message %d of %d (%d octets) from %s..."
145              (floor (* (/ (float n) message-count) 100))
146              n message-count (cdar messages) pop3-mailhost)
147             (pop3-retr process (caar messages) crashbuf)
148             (push (caar messages) retrieved-messages)
149             (setq messages (cdr messages)
150                   n (1+ n)))
151           (with-current-buffer crashbuf
152             (write-region-as-binary (point-min) (point-max)
153                                     crashbox 'append 'nomesg))
154           ;; mark messages as read
155           (when pop3-leave-mail-on-server
156             (pop3-save-uidls))
157           ;; now delete the messages we have retrieved
158           (unless pop3-leave-mail-on-server
159             (dolist (n retrieved-messages)
160               (message "Deleting message %d of %d from %s..."
161                        n message-count pop3-mailhost)
162               (pop3-dele process n)))
163           )
164       (pop3-quit process))
165     (kill-buffer crashbuf)
166     message-count))
167
168 (defun pop3-open-server (mailhost port)
169   "Open TCP connection to MAILHOST on PORT.
170 Returns the process associated with the connection.
171 Argument PORT specifies connecting port."
172   (let (process)
173     (save-excursion
174       (set-buffer (get-buffer-create (concat " trace of POP session to "
175                                              mailhost)))
176       (erase-buffer)
177       (setq pop3-read-point (point-min))
178       (setq
179        process
180        (cond
181         ((eq pop3-connection-type 'ssl)
182          (pop3-open-ssl-stream "POP" (current-buffer) mailhost port))
183         ((eq pop3-connection-type 'tls)
184          (pop3-open-tls-stream "POP" (current-buffer) mailhost port))
185         (t
186          (open-network-stream-as-binary "POP" (current-buffer)
187                                         mailhost port))))
188       (let ((response (pop3-read-response process t)))
189         (setq pop3-timestamp
190               (substring response (or (string-match "<" response) 0)
191                          (+ 1 (or (string-match ">" response) -1)))))
192       process)))
193
194 (defun pop3-open-ssl-stream-1 (name buffer host service extra-arg)
195   (require 'path-util)
196   (let* ((ssl-program-name
197           pop3-ssl-program-name)
198          (ssl-program-arguments
199           `(,@pop3-ssl-program-arguments ,extra-arg
200             "-connect" ,(format "%s:%d" host service)))
201          (process (open-ssl-stream name buffer host service)))
202     (when process
203       (with-current-buffer buffer
204         (goto-char (point-min))
205         (while (and (memq (process-status process) '(open run))
206                     (goto-char (point-max))
207                     (forward-line -1)
208                     (not (looking-at "+OK")))
209           (accept-process-output process 1)
210           (sit-for 1))
211         (delete-region (point-min) (point)))
212       (and process (memq (process-status process) '(open run))
213            process))))
214
215 (defun pop3-open-ssl-stream (name buffer host service)
216   "Open a SSL connection for a service to a host.
217 Returns a subprocess-object to represent the connection.
218 Args are NAME BUFFER HOST SERVICE."
219   (cond ((eq system-type 'windows-nt)
220          (let (selective-display
221                (coding-system-for-write 'binary)
222                (coding-system-for-read 'raw-text-dos))
223            (or (pop3-open-ssl-stream-1 name buffer host service "-ssl3")
224                (pop3-open-ssl-stream-1 name buffer host service "-ssl2"))))
225         (t
226          (as-binary-process
227            (or (pop3-open-ssl-stream-1 name buffer host service "-ssl3")
228                (pop3-open-ssl-stream-1 name buffer host service "-ssl2"))))))
229
230 (defun pop3-open-tls-stream (name buffer host service)
231   "Open a TLSv1 connection for a service to a host.
232 Returns a subprocess-object to represent the connection.
233 Args are NAME BUFFER HOST SERVICE."
234   (let ((process
235          (as-binary-process (starttls-open-stream
236                              name buffer host service))))
237     (pop3-stls process)
238     (starttls-negotiate process)
239     process))
240
241 ;; Support functions
242
243 (defun pop3-process-filter (process output)
244   (save-excursion
245     (set-buffer (process-buffer process))
246     (goto-char (point-max))
247     (insert output)))
248
249 (defun pop3-send-command (process command)
250     (set-buffer (process-buffer process))
251     (goto-char (point-max))
252 ;;    (if (= (aref command 0) ?P)
253 ;;      (insert "PASS <omitted>\r\n")
254 ;;      (insert command "\r\n"))
255     (setq pop3-read-point (point))
256     (goto-char (point-max))
257     (process-send-string process (concat command "\r\n"))
258     )
259
260 (defun pop3-read-response (process &optional return)
261   "Read the response from the server PROCESS.
262 Return the response string if optional second argument RETURN is non-nil."
263   (let ((case-fold-search nil)
264         match-end)
265     (save-excursion
266       (set-buffer (process-buffer process))
267       (goto-char pop3-read-point)
268       (while (not (search-forward "\r\n" nil t))
269         (accept-process-output process 3)
270         (goto-char pop3-read-point))
271       (setq match-end (point))
272       (goto-char pop3-read-point)
273       (if (looking-at "-ERR")
274           (error (buffer-substring (point) (- match-end 2)))
275         (if (not (looking-at "+OK"))
276             (progn (setq pop3-read-point match-end) nil)
277           (setq pop3-read-point match-end)
278           (if return
279               (buffer-substring (point) match-end)
280             t)
281           )))))
282
283 (defvar pop3-read-passwd nil)
284 (defun pop3-read-passwd (prompt)
285   (if (not pop3-read-passwd)
286       (if (fboundp 'read-passwd)
287           (setq pop3-read-passwd 'read-passwd)
288         (if (load "passwd" t)
289             (setq pop3-read-passwd 'read-passwd)
290           (autoload 'ange-ftp-read-passwd "ange-ftp")
291           (setq pop3-read-passwd 'ange-ftp-read-passwd))))
292   (funcall pop3-read-passwd prompt))
293
294 (defun pop3-clean-region (start end)
295   (setq end (set-marker (make-marker) end))
296   (save-excursion
297     (goto-char start)
298     (while (and (< (point) end) (search-forward "\r\n" end t))
299       (replace-match "\n" t t))
300     (goto-char start)
301     (while (re-search-forward "\n\n\\(From \\)" end t)
302       (replace-match "\n\n>\\1" t nil))
303     (goto-char start)
304     (while (and (< (point) end) (re-search-forward "^\\." end t))
305       (replace-match "" t t)
306       (forward-char)))
307   (set-marker end nil))
308
309 (defun pop3-munge-message-separator (start end)
310   "Check to see if a message separator exists.  If not, generate one."
311   (if (not (fboundp 'parse-time-string))
312       (autoload 'parse-time-string "parse-time"))
313   (save-excursion
314     (save-restriction
315       (narrow-to-region start end)
316       (goto-char (point-min))
317       (if (not (or (looking-at "From .?") ; Unix mail
318                    (looking-at "\001\001\001\001\n") ; MMDF
319                    (looking-at "BABYL OPTIONS:") ; Babyl
320                    ))
321           (let ((from (mail-strip-quoted-names (mail-fetch-field "From")))
322                 (date (mail-fetch-field "Date"))
323                 (From_))
324             ;; sample date formats I have seen
325             ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
326             ;; Date: 08 Jul 1996 23:22:24 -0400
327             ;; should be
328             ;; Tue Jul 9 09:04:21 1996
329             (setq date (format-time-string
330                         "%a %b %e %T %Y"
331                         (if date
332                             (condition-case nil
333                                 (apply 'encode-time (parse-time-string date))
334                               (error (current-time)))
335                           (current-time))))
336             (setq From_ (format "\nFrom %s  %s\n" from date))
337             (while (string-match "," From_)
338               (setq From_ (concat (substring From_ 0 (match-beginning 0))
339                                   (substring From_ (match-end 0)))))
340             (goto-char (point-min))
341             (insert From_)
342             (if (search-forward "\n\n" nil t)
343                 nil
344               (goto-char (point-max))
345               (insert "\n"))
346             (narrow-to-region (point) (point-max))
347             (let ((size (- (point-max) (point-min))))
348               (goto-char (point-min))
349               (widen)
350               (forward-line -1)
351               (insert (format "Content-Length: %s\n" size)))
352             )))))
353
354 ;; UIDL support
355
356 (defun pop3-get-message-numbers (process)
357   "Get the list of message numbers and lengths to retrieve via PROCESS."
358   ;; we use the LIST comand first anyway to get the message lengths.
359   ;; then if we're leaving mail on the server, see if the UIDL command
360   ;; is implemented. if so, we use it to get the message number list.
361   (let* ((messages (pop3-list process))
362          (total (or (pop messages) 0))
363          (uidl (if pop3-leave-mail-on-server
364                    (pop3-get-uidl process)))
365          out)
366     (while messages
367       ;; only retrieve messages matching our regexp or in the uidl list
368       (when (and
369              ;; remove elements not in the uidl, this assumes the uidl is short
370              (or (not (eq pop3-uidl-support t))
371                  (memq (caar messages) uidl))
372              (caar messages)
373              ;; don't download messages that are too large
374              (not (and pop3-maximum-message-size
375                        (> (cdar messages) pop3-maximum-message-size)))
376              (not (and pop3-except-header-regexp
377                        (string-match pop3-except-header-regexp
378                                      (pop3-top process (caar messages) 0)))))
379         (push (car messages) out))
380       (setq messages (cdr messages)))
381     (cons total (reverse out))))
382
383 (defun pop3-get-uidl (process)
384   "Use PROCESS to get a list of unread message numbers."
385   (let ((messages (pop3-uidl process)) uidl)
386     (if (or (null messages) (null pop3-uidl-support))
387         (setq pop3-uidl-support nil)
388       (setq pop3-uidl-support t)
389       (save-excursion
390         (with-temp-buffer
391           (when (file-readable-p pop3-uidl-file-name)
392             (insert-file-contents pop3-uidl-file-name))
393           (goto-char (point-min))
394           (while (looking-at "\\([^ \n\t]+\\)")
395             (set (intern (match-string 1) pop3-uidl-obarray)
396                  (cons nil t))
397             (forward-line 1))
398           ))
399       (dolist (message (cdr messages))
400         (if (setq uidl (intern-soft (cdr message) pop3-uidl-obarray))
401             (setcar (symbol-value uidl) (car message))
402           (set (intern (cdr message) pop3-uidl-obarray)
403                (cons (car message) nil))))
404       (pop3-get-unread-message-numbers))
405     ))
406
407 (defun pop3-get-unread-message-numbers ()
408   "Return a sorted list of unread msg numbers to retrieve."
409   (let (nums)
410     (mapatoms (lambda (atom)
411                 (if (not (cdr (symbol-value atom)))
412                     (push (car (symbol-value atom)) nums)))
413               pop3-uidl-obarray)
414     (sort nums '<)))
415
416 (defun pop3-save-uidls ()
417   "Save the updated UIDLs to disk for use next time."
418   (when (and pop3-leave-mail-on-server
419              ;; UIDL hash table is non-empty
420              (let ((len (length pop3-uidl-obarray)))
421                (while (< 0 len)
422                  (setq len (if (symbolp (aref pop3-uidl-obarray (1- len)))
423                                -1 (1- len))))
424                (minusp len)))
425     (when (file-readable-p pop3-uidl-file-name)
426       (copy-file pop3-uidl-file-name
427                  (concat pop3-uidl-file-name ".old")
428                  'overwrite 'keeptime))
429     (save-excursion
430       (with-temp-file pop3-uidl-file-name
431         (mapatoms
432          (lambda (atom)
433            (when (car (symbol-value atom))
434              (insert (format "%s\n" atom))))
435          pop3-uidl-obarray)))
436     (fillarray pop3-uidl-obarray 0)))
437
438
439 ;; The Command Set
440
441 ;; AUTHORIZATION STATE
442
443 (defun pop3-user (process user)
444   "Send USER information to POP3 server."
445   (pop3-send-command process (format "USER %s" user))
446   (let ((response (pop3-read-response process t)))
447     (if (not (and response (string-match "+OK" response)))
448         (error (format "USER %s not valid." user)))))
449
450 (defun pop3-pass (process)
451   "Send authentication information to the server."
452   (pop3-send-command process (format "PASS %s" pop3-password))
453   (let ((response (pop3-read-response process t)))
454     (if (not (and response (string-match "+OK" response)))
455         (pop3-quit process))))
456
457 (static-unless (and (fboundp 'md5) (subrp (symbol-function 'md5)))
458   (eval-and-compile
459     (require 'path-util)
460     (if (module-installed-p 'md5)
461         (progn
462           (autoload 'md5 "md5")
463           (fset 'pop3-md5 'md5))
464
465       (defvar pop3-md5-program "md5"
466         "*Program to encode its input in MD5.")
467
468       (defun pop3-md5 (string)
469         (with-temp-buffer
470           (insert string)
471           (call-process-region (point-min) (point-max)
472                                (or shell-file-name "/bin/sh")
473                                t (current-buffer) nil
474                                "-c" pop3-md5-program)
475           ;; The meaningful output is the first 32 characters.
476           ;; Don't return the newline that follows them!
477           (buffer-substring (point-min) (+ (point-min) 32))))
478       )))
479
480 (defun pop3-apop (process user)
481   "Send alternate authentication information to the server."
482   (let ((pass pop3-password))
483     (if (and pop3-password-required (not pass))
484         (setq pass
485               (pop3-read-passwd (format "Password for %s: " pop3-maildrop))))
486     (if pass
487         (let ((hash (static-if (and (fboundp 'md5)
488                                     (subrp (symbol-function 'md5)))
489                         (md5 (concat pop3-timestamp pass))
490                       (pop3-md5 (concat pop3-timestamp pass)))))
491           (pop3-send-command process (format "APOP %s %s" user hash))
492           (let ((response (pop3-read-response process t)))
493             (if (not (and response (string-match "+OK" response)))
494                 (pop3-quit process)))))
495     ))
496
497 (defun pop3-stls (process)
498   "Query whether TLS extension is supported"
499   (pop3-send-command process "STLS")
500   (let ((response (pop3-read-response process t)))
501     (if (not (and response (string-match "+OK" response)))
502         (pop3-quit process))))
503
504 ;; TRANSACTION STATE
505
506 (defun pop3-stat (process)
507   "Return the number of messages in the maildrop and the maildrop's size."
508   (pop3-send-command process "STAT")
509   (let ((response (pop3-read-response process t)))
510     (list (string-to-int (nth 1 (split-string response " ")))
511           (string-to-int (nth 2 (split-string response " "))))
512     ))
513
514 (defun pop3-retr (process msg crashbuf)
515   "Retrieve message-id MSG to buffer CRASHBUF."
516   (pop3-send-command process (format "RETR %s" msg))
517   (pop3-read-response process)
518   (save-excursion
519     (let ((region (pop3-get-extended-response process)))
520       (pop3-munge-message-separator (car region) (cadr region))
521       (append-to-buffer crashbuf (car region) (cadr region))
522       (delete-region (car region) (cadr region))
523       )))
524
525 (defun pop3-dele (process msg)
526   "Mark message-id MSG as deleted."
527   (pop3-send-command process (format "DELE %s" msg))
528   (pop3-read-response process))
529
530 (defun pop3-noop (process msg)
531   "No-operation."
532   (pop3-send-command process "NOOP")
533   (pop3-read-response process))
534
535 (defun pop3-last (process)
536   "Return highest accessed message-id number for the session."
537   (pop3-send-command process "LAST")
538   (let ((response (pop3-read-response process t)))
539     (string-to-int (nth 1 (split-string response " ")))
540     ))
541
542 (defun pop3-rset (process)
543   "Remove all delete marks from current maildrop."
544   (pop3-send-command process "RSET")
545   (pop3-read-response process))
546
547 ;; UPDATE
548
549 (defun pop3-quit (process)
550   "Close connection to POP3 server.
551 Tell server to remove all messages marked as deleted, unlock the maildrop,
552 and close the connection."
553   (pop3-send-command process "QUIT")
554   (pop3-read-response process t)
555   (when process
556     (save-excursion
557       (set-buffer (process-buffer process))
558       (goto-char (point-max))
559       (delete-process process))))
560
561 (defun pop3-uidl (process &optional msgno)
562   "Return the results of a UIDL command in PROCESS for optional MSGNO.
563 If UIDL is unsupported on this mail server or if msgno is invalid, return nil.
564 Otherwise, return a list in the form
565
566    (N (1 UIDL-1) (2 UIDL-2) ... (N UIDL-N))
567
568 where
569
570    N is an integer for the number of UIDLs returned (could be 0)
571    UIDL-n is a string."
572
573   (if msgno
574       (pop3-send-command process (format "UIDL %d" msgno))
575     (pop3-send-command process "UIDL"))
576   
577   (if (null (pop3-read-response process t))
578       nil ;; UIDL is not supported on this server
579     (let (pairs uidl)
580       (save-excursion
581         (save-restriction
582           (apply 'narrow-to-region (pop3-get-extended-response process))
583           (goto-char (point-min))
584           (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
585             (setq msgno (string-to-int (match-string 1))
586                   uidl (match-string 2))
587             (push (cons msgno uidl) pairs)
588             (beginning-of-line 2))
589           (cons (length pairs) (nreverse pairs))
590           )))))
591
592 (defun pop3-list (process &optional msgno)
593   "Return the results of a LIST command for PROCESS and optional MSGNO.
594 If (optional) msgno is invalid, return nil.  Otherwise, return a list
595 in the form
596
597    (N (1 LEN-1) (2 LEN-2) ... (N LEN-N))
598
599 where
600
601    N is an integer for the number of msg/len pairs (could be 0)
602    LEN-n is an integer."
603   (if msgno
604       (pop3-send-command process (format "LIST %d" msgno))
605     (pop3-send-command process "LIST"))
606
607   (if (null (pop3-read-response process t))
608       nil ;; MSGNO is not valid number
609     (let (pairs len)
610       (save-excursion
611         (save-restriction
612           (apply 'narrow-to-region (pop3-get-extended-response process))
613           (goto-char (point-min))
614           (while (looking-at "\\([^ \n\t]*\\) \\([^ \n\t]*\\)")
615             (setq msgno (string-to-int (match-string 1))
616                   len (string-to-int (match-string 2)))
617             (push (cons msgno len) pairs)
618             (beginning-of-line 2))
619           (cons (length pairs) (nreverse pairs))
620           )))))
621
622 (defun pop3-top (process msgno &optional lines)
623   "Return the top LINES of messages for PROCESS and MSGNO.
624 If msgno is invalid, return nil.  Otherwise, return a string."
625   (pop3-send-command process (format "TOP %d %d" msgno (or lines 1)))
626   (if (pop3-read-response process t)
627       nil ;; MSGNO is not valid number
628     (save-excursion
629       (apply 'buffer-substring (pop3-get-extended-response process)))
630     ))
631
632 ;;; Utility code
633
634 (defun pop3-get-extended-response (process)
635   "Get the extended pop3 response in the PROCESS buffer."
636   (let ((start pop3-read-point) end)
637     (set-buffer (process-buffer process))
638     (goto-char start)
639     (while (not (re-search-forward "^\\.\r\n" nil t))
640       (accept-process-output process 3)
641       (goto-char start))
642     (setq pop3-read-point (point-marker))
643     (goto-char (match-beginning 0))
644     (setq end (point-marker))
645     (pop3-clean-region start end)
646     (list start end)))
647
648 \f
649 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
650
651 ;;; AUTHORIZATION STATE
652
653 ;; Initial TCP connection
654 ;; Arguments: none
655 ;; Restrictions: none
656 ;; Possible responses:
657 ;;  +OK [POP3 server ready]
658
659 ;; USER name
660 ;; Arguments: a server specific user-id (required)
661 ;; Restrictions: authorization state [after unsuccessful USER or PASS
662 ;; Possible responses:
663 ;;  +OK [valid user-id]
664 ;;  -ERR [invalid user-id]
665
666 ;; PASS string
667 ;; Arguments: a server/user-id specific password (required)
668 ;; Restrictions: authorization state, after successful USER
669 ;; Possible responses:
670 ;;  +OK [maildrop locked and ready]
671 ;;  -ERR [invalid password]
672 ;;  -ERR [unable to lock maildrop]
673
674 ;; STLS
675 ;; Arguments: none
676 ;; Restrictions: authorization state
677 ;; Possible responses:
678 ;;  +OK [negotiation is ready]
679 ;;  -ERR [security layer is already active]
680
681 ;;; TRANSACTION STATE
682
683 ;; STAT
684 ;; Arguments: none
685 ;; Restrictions: transaction state
686 ;; Possible responses:
687 ;;  +OK nn mm [# of messages, size of maildrop]
688
689 ;; LIST [msg]
690 ;; Arguments: a message-id (optional)
691 ;; Restrictions: transaction state; msg must not be deleted
692 ;; Possible responses:
693 ;;  +OK [scan listing follows]
694 ;;  -ERR [no such message]
695
696 ;; TOP msg [lines]
697 ;; Arguments: a message-id (required), number of lines (optional)
698 ;; Restrictions: transaction state; msg must not be deleted
699 ;; Possible responses:
700 ;;  +OK [partial message listing follows]
701 ;;  -ERR [no such message]
702
703 ;; UIDL [msg]
704 ;; Arguments: a message-id (optional)
705 ;; Restrictions: transaction state; msg must not be deleted
706 ;; Possible responses:
707 ;;  +OK [uidl listing follows]
708 ;;  -ERR [no such message]
709
710 ;; RETR msg
711 ;; Arguments: a message-id (required)
712 ;; Restrictions: transaction state; msg must not be deleted
713 ;; Possible responses:
714 ;;  +OK [message contents follow]
715 ;;  -ERR [no such message]
716
717 ;; DELE msg
718 ;; Arguments: a message-id (required)
719 ;; Restrictions: transaction state; msg must not be deleted
720 ;; Possible responses:
721 ;;  +OK [message deleted]
722 ;;  -ERR [no such message]
723
724 ;; NOOP
725 ;; Arguments: none
726 ;; Restrictions: transaction state
727 ;; Possible responses:
728 ;;  +OK
729
730 ;; LAST
731 ;; Arguments: none
732 ;; Restrictions: transaction state
733 ;; Possible responses:
734 ;;  +OK nn [highest numbered message accessed]
735
736 ;; RSET
737 ;; Arguments: none
738 ;; Restrictions: transaction state
739 ;; Possible responses:
740 ;;  +OK [all delete marks removed]
741
742 ;;; UPDATE STATE
743
744 ;; QUIT
745 ;; Arguments: none
746 ;; Restrictions: none
747 ;; Possible responses:
748 ;;  +OK [TCP connection closed]
749
750 (provide 'pop3)
751
752 ;;; pop3.el ends here