Fix a comment whitespace typo.
[emacs.git] / lisp / net / pop3.el
blob2ef63217256c8beb3b430b46f486175d672b930c
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
7 ;; Keywords: mail
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/>.
24 ;;; Commentary:
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.
33 ;;; Code:
35 (eval-when-compile (require 'cl))
37 (require 'mail-utils)
38 (defvar parse-time-months)
40 (defgroup pop3 nil
41 "Post Office Protocol."
42 :group 'mail
43 :group 'mail-source)
45 (defcustom pop3-maildrop (or (user-login-name)
46 (getenv "LOGNAME")
47 (getenv "USER"))
48 "POP3 maildrop."
49 :version "22.1" ;; Oort Gnus
50 :type 'string
51 :group 'pop3)
53 (defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
54 "pop3")
55 "POP3 mailhost."
56 :version "22.1" ;; Oort Gnus
57 :type 'string
58 :group 'pop3)
60 (defcustom pop3-port 110
61 "POP3 port."
62 :version "22.1" ;; Oort Gnus
63 :type 'number
64 :group 'pop3)
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
69 :type 'boolean
70 :group 'pop3)
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)
76 :group 'pop3)
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
85 :group 'pop3)
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,
91 set this to 1."
92 :type 'number
93 :version "24.1"
94 :group 'pop3)
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."
112 :version "24.4"
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))
116 :group 'pop3)
118 (defcustom pop3-uidl-file "~/.pop3-uidl"
119 "File used to save UIDL."
120 :version "24.4"
121 :type 'file
122 :group 'pop3)
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
133 many servers."
134 :version "24.4"
135 :type '(choice (group :tag "Keep versions" :format "\n%v" :indent 3
136 (number :tag "oldest")
137 (number :tag "newest"))
138 (sexp :format "%v"
139 :match (lambda (widget value)
140 (condition-case nil
141 (not (and (numberp (car value))
142 (numberp (car (cdr value)))))
143 (error t)))))
144 :group 'pop3)
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.
156 (eval-and-compile
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))
164 0.01)
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
169 process
170 (truncate pop3-read-timeout)
171 (truncate (* (- pop3-read-timeout
172 (truncate pop3-read-timeout))
173 1000))))))
175 (defvar pop3-uidl)
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...)
184 ;; ...)
185 ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
186 ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
187 ;; ...))
188 ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
189 ;; i.e. the return value of `current-time'.
191 ;;;###autoload
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))
196 messages total-size
197 pop3-uidl
198 pop3-uidl-saved)
199 (pop3-logon process)
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))))
208 (when messages
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))
216 (pop3-uidl-save))
217 (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)))))
222 (when elt
223 (setcdr elt nil)
224 (pop3-uidl-save))))
227 (defun pop3-send-streaming-command (process command messages total-size)
228 (erase-buffer)
229 (let ((count (length messages))
230 (i 1)
231 (start-point (point-min))
232 (waited-for 0))
233 (while messages
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))
237 (setq start-point
238 (pop3-wait-for-messages process pop3-stream-length
239 total-size start-point))
240 (incf waited-for pop3-stream-length))
241 (incf i))
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)
246 (while (> count 0)
247 (goto-char start-point)
248 (while (or (and (re-search-forward "^\\+OK" nil t)
249 (or (not total-size)
250 (re-search-forward "^\\.\r?\n" nil t)))
251 (re-search-forward "^-ERR " nil t))
252 (decf count)
253 (setq start-point (point)))
254 (unless (memq (process-status process) '(open run))
255 (error "pop3 process died"))
256 (when total-size
257 (let ((size 0))
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)
262 (match-beginning 0)
263 (point)))))
264 (message "pop3 retrieved %dKB (%d%%)"
265 (truncate (/ size 1000))
266 (truncate (* (/ (* size 1.0) total-size) 100)))))
267 (pop3-accept-process-output process))
268 start-point)
270 (defun pop3-write-to-file (file messages)
271 (let ((pop-buffer (current-buffer))
272 (start (point-min))
273 beg end
274 temp-buffer)
275 (with-temp-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)
280 (forward-line 1)
281 (setq beg (point))
282 (when (re-search-forward "^\\.\r?\n" nil t)
283 (setq start (point))
284 (forward-line -1)
285 (setq end (point)))
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
299 ;; delete it.
300 (when (eolp)
301 (delete-char 1))
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))
310 (setq 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)
316 (pop3-pass process))
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))
322 message-count
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))
328 (setq 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)
334 (pop3-pass process))
335 (t (error "Invalid POP3 authentication scheme")))
336 (setq message-count (car (pop3-stat process)))
337 (pop3-quit process)
338 message-count))
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
345 (progn
346 (pop3-read-response process)
348 (error (setq err (error-message-string code))
349 nil))
350 (let ((start pop3-read-point)
351 saved list)
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)
357 (goto-char start))
358 (setq pop3-read-point (point-marker)
359 pop3-uidl nil)
360 (while (progn (forward-line -1) (>= (point) start))
361 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
362 (push (match-string 1) pop3-uidl)))
363 (when pop3-uidl
364 (setq pop3-uidl-saved (pop3-uidl-load)
365 saved (cdr (assoc pop3-maildrop
366 (cdr (assoc pop3-mailhost
367 pop3-uidl-saved)))))
368 (let ((i (length pop3-uidl)))
369 (while (> i 0)
370 (unless (member (nth (1- i) pop3-uidl) saved)
371 (push i messages))
372 (decf i)))
373 (when messages
374 (setq list (pop3-list process)
375 size 0)
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..."
380 pop3-mailhost err)
381 (sit-for 1)
382 (setq size (pop3-stat process))
383 (dotimes (i (car size)) (push (1+ i) messages))
384 (setcar size (nreverse messages))
385 size)))
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)))
398 (while (>= i 0)
399 (unless (member (setq uidl (nth i pop3-uidl)) (cdr saved))
400 (push ctime new)
401 (push uidl new))
402 (decf i)))
403 (pop3-uidl
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)))
410 (while (> i 0)
411 (if (member (setq uidl (nth (1- i) saved)) pop3-uidl)
412 (progn
413 (setq tstamp (nth i saved))
414 (if (and ctime
415 (> (- ctime (/ (+ (* (car tstamp) 65536.0) (cadr tstamp))
416 86400))
417 pop3-leave-mail-on-server))
418 ;; Mails to delete.
419 (progn
420 (setq mod t)
421 (push uidl dele))
422 ;; Mails to keep.
423 (push tstamp new)
424 (push uidl new)))
425 ;; Mails having been deleted in the server.
426 (setq mod t))
427 (decf i 2))
428 (cond (saved
429 (setcdr saved new))
430 (srvr
431 (setcdr (last srvr) (list (cons pop3-maildrop new))))
433 (add-to-list 'pop3-uidl-saved
434 (list pop3-mailhost (cons pop3-maildrop new))
435 t)))
436 ;; Actually delete the messages in the server.
437 (when dele
438 (setq uidl nil
439 i (length pop3-uidl))
440 (while (> i 0)
441 (when (member (nth (1- i) pop3-uidl) dele)
442 (push i uidl))
443 (decf i))
444 (when uidl
445 (pop3-send-streaming-command process "DELE" uidl nil)))
446 mod))
448 (defun pop3-uidl-load ()
449 "Load saved UIDL."
450 (when (file-exists-p pop3-uidl-file)
451 (with-temp-buffer
452 (condition-case code
453 (progn
454 (insert-file-contents pop3-uidl-file)
455 (goto-char (point-min))
456 (read (current-buffer)))
457 (error
458 (message "Error while loading %s (%s)"
459 pop3-uidl-file (error-message-string code))
460 (sit-for 1)
461 nil)))))
463 (defun pop3-uidl-save ()
464 "Save UIDL."
465 (with-temp-buffer
466 (if pop3-uidl-saved
467 (progn
468 (insert "(")
469 (dolist (srvr pop3-uidl-saved)
470 (when (cdr srvr)
471 (insert "(\"" (pop srvr) "\"\n ")
472 (dolist (elt srvr)
473 (when (cdr elt)
474 (insert "(\"" (pop elt) "\"\n ")
475 (while elt
476 (insert (format "\"%s\" %s\n " (pop elt) (pop elt))))
477 (delete-char -4)
478 (insert ")\n ")))
479 (delete-char -3)
480 (if (eq (char-before) ?\))
481 (insert ")\n ")
482 (goto-char (1+ (point-at-bol)))
483 (delete-region (point) (point-max)))))
484 (when (eq (char-before) ? )
485 (delete-char -2))
486 (insert ")\n"))
487 (insert "()\n"))
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)
496 version-control t)
497 (setq version-control pop3-uidl-file-backup))
498 (save-buffer))))
500 (defun pop3-uidl-add-xheader (start msgno)
501 "Add X-UIDL header."
502 (let ((case-fold-search t))
503 (save-restriction
504 (narrow-to-region start (progn
505 (goto-char start)
506 (search-forward "\n\n" nil 'move)
507 (1- (point))))
508 (goto-char start)
509 (while (re-search-forward "^x-uidl:" nil t)
510 (while (progn
511 (forward-line 1)
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
523 995 (pop3s)."
524 :version "23.1" ;; No Gnus
525 :group 'pop3
526 :type '(choice (const :tag "Plain" nil)
527 (const :tag "SSL/TLS" ssl)
528 (const starttls)))
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)
535 result)
536 (with-current-buffer
537 (get-buffer-create (concat " trace of POP session to "
538 mailhost))
539 (erase-buffer)
540 (setq pop3-read-point (point-min))
541 (setq result
542 (open-network-stream
543 "POP" (current-buffer) mailhost port
544 :type (cond
545 ((or (eq pop3-stream-type 'ssl)
546 (and (not pop3-stream-type)
547 (member port '(995 "pop3s"))))
548 'tls)
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"
556 :return-list t
557 :starttls-function
558 (lambda (capabilities)
559 (and (string-match "\\bSTLS\\b" capabilities)
560 "STLS\r\n"))))
561 (when result
562 (let ((response (plist-get (cdr result) :greeting)))
563 (setq pop3-timestamp
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)
567 (erase-buffer)
568 (car result)))))
570 ;; Support functions
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)
586 match-end)
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)
600 (if return
601 (buffer-substring (point) match-end)
603 )))))
605 (defun pop3-clean-region (start end)
606 (setq end (set-marker (make-marker) end))
607 (save-excursion
608 (goto-char start)
609 (while (and (< (point) end) (search-forward "\r\n" end t))
610 (replace-match "\n" t t))
611 (goto-char start)
612 (while (and (< (point) end) (re-search-forward "^\\." end t))
613 (replace-match "" t t)
614 (forward-char)))
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)))
624 (sign "+"))
625 (when (< zone 0)
626 (setq sign "-")
627 (setq zone (- zone)))
628 (concat
629 (format-time-string "%d" now)
630 ;; The month name of the %b spec is locale-specific. Pfff.
631 (format " %s "
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."
638 (save-excursion
639 (save-restriction
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))
650 tdate)
651 (pop3-make-date))
652 " "))
653 (From_))
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
657 ;; should be
658 ;; Tue Jul 9 09:04:21 1996
660 ;; Fixme: This should use timezone on the date field contents.
661 (setq date
662 (cond ((not date)
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))
680 (insert From_)
681 (if (search-forward "\n\n" nil t)
683 (goto-char (point-max))
684 (insert "\n"))
685 (let ((size (- (point-max) (point))))
686 (forward-line -1)
687 (insert (format "Content-Length: %s\n" size)))
688 )))))
690 ;; The Command Set
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))
712 (setq pass
713 (read-passwd (format "Password for %s: " pop3-maildrop))))
714 (if pass
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)))))
722 ;; TRANSACTION STATE
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)
737 "LIST"))
738 (let ((response (pop3-read-response process t)))
739 (if msg
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)
745 (goto-char start))
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)
764 (goto-char start))
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))
769 ;; (backward-char 2)
770 ;; (if (not (looking-at "\r\n"))
771 ;; (insert "\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
778 (erase-buffer))
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)
789 "No-operation."
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))
805 ;; UPDATE
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)
813 (if process
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
823 ;; Arguments: none
824 ;; Restrictions: none
825 ;; Possible responses:
826 ;; +OK [POP3 server ready]
828 ;; USER name
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]
835 ;; PASS string
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]
843 ;; STLS (RFC 2595)
844 ;; Arguments: none
845 ;; Restrictions: Only permitted in AUTHORIZATION state.
846 ;; Possible responses:
847 ;; +OK
848 ;; -ERR
850 ;;; TRANSACTION STATE
852 ;; STAT
853 ;; Arguments: none
854 ;; Restrictions: transaction state
855 ;; Possible responses:
856 ;; +OK nn mm [# of messages, size of maildrop]
858 ;; LIST [msg]
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]
865 ;; RETR msg
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]
872 ;; DELE msg
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]
879 ;; NOOP
880 ;; Arguments: none
881 ;; Restrictions: transaction state
882 ;; Possible responses:
883 ;; +OK
885 ;; LAST
886 ;; Arguments: none
887 ;; Restrictions: transaction state
888 ;; Possible responses:
889 ;; +OK nn [highest numbered message accessed]
891 ;; RSET
892 ;; Arguments: none
893 ;; Restrictions: transaction state
894 ;; Possible responses:
895 ;; +OK [all delete marks removed]
897 ;; UIDL [msg]
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]
904 ;;; UPDATE STATE
906 ;; QUIT
907 ;; Arguments: none
908 ;; Restrictions: none
909 ;; Possible responses:
910 ;; +OK [TCP connection closed]
912 (provide 'pop3)
914 ;;; pop3.el ends here