Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / net / pop3.el
blob2a6807e1aca4de98934ba38e407f08bc689ba9e7
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface -*- lexical-binding:t -*-
3 ;; Copyright (C) 1996-2018 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 <https://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-lib))
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 (cl-incf waited-for pop3-stream-length))
241 (cl-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 (cl-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 beg end
273 temp-buffer)
274 (with-temp-buffer
275 (setq temp-buffer (current-buffer))
276 (with-current-buffer pop-buffer
277 (goto-char (point-min))
278 (while (re-search-forward "^\\+OK" nil t)
279 (forward-line 1)
280 (setq beg (point))
281 (when (re-search-forward "^\\.\r?\n" nil t)
282 (forward-line -1)
283 (setq end (point)))
284 (with-current-buffer temp-buffer
285 (goto-char (point-max))
286 (let ((hstart (point)))
287 (insert-buffer-substring pop-buffer beg end)
288 (pop3-clean-region hstart (point))
289 (goto-char (point-max))
290 (pop3-munge-message-separator hstart (point))
291 (when pop3-leave-mail-on-server
292 (pop3-uidl-add-xheader hstart (pop messages)))
293 (goto-char (point-max))))))
294 (let ((coding-system-for-write 'binary))
295 (goto-char (point-min))
296 ;; Check whether something inserted a newline at the start and
297 ;; delete it.
298 (when (eolp)
299 (delete-char 1))
300 (write-region (point-min) (point-max) file nil 'nomesg)))))
302 (defun pop3-logon (process)
303 (let ((pop3-password pop3-password))
304 ;; for debugging only
305 (if pop3-debug (switch-to-buffer (process-buffer process)))
306 ;; query for password
307 (if (and pop3-password-required (not pop3-password))
308 (setq pop3-password
309 (read-passwd (format "Password for %s: " pop3-maildrop))))
310 (cond ((equal 'apop pop3-authentication-scheme)
311 (pop3-apop process pop3-maildrop))
312 ((equal 'pass pop3-authentication-scheme)
313 (pop3-user process pop3-maildrop)
314 (pop3-pass process))
315 (t (error "Invalid POP3 authentication scheme")))))
317 (defun pop3-get-message-count ()
318 "Return the number of messages in the maildrop."
319 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
320 message-count
321 (pop3-password pop3-password))
322 ;; for debugging only
323 (if pop3-debug (switch-to-buffer (process-buffer process)))
324 ;; query for password
325 (if (and pop3-password-required (not pop3-password))
326 (setq pop3-password
327 (read-passwd (format "Password for %s: " pop3-maildrop))))
328 (cond ((equal 'apop pop3-authentication-scheme)
329 (pop3-apop process pop3-maildrop))
330 ((equal 'pass pop3-authentication-scheme)
331 (pop3-user process pop3-maildrop)
332 (pop3-pass process))
333 (t (error "Invalid POP3 authentication scheme")))
334 (setq message-count (car (pop3-stat process)))
335 (pop3-quit process)
336 message-count))
338 (defun pop3-uidl-stat (process)
339 "Return a list of unread message numbers and total size."
340 (pop3-send-command process "UIDL")
341 (let (err messages size)
342 (if (condition-case code
343 (progn
344 (pop3-read-response process)
346 (error (setq err (error-message-string code))
347 nil))
348 (let ((start pop3-read-point)
349 saved list)
350 (with-current-buffer (process-buffer process)
351 (while (not (re-search-forward "^\\.\r\n" nil t))
352 (unless (memq (process-status process) '(open run))
353 (error "pop3 server closed the connection"))
354 (pop3-accept-process-output process)
355 (goto-char start))
356 (setq pop3-read-point (point-marker)
357 pop3-uidl nil)
358 (while (progn (forward-line -1) (>= (point) start))
359 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
360 (push (match-string 1) pop3-uidl)))
361 (when pop3-uidl
362 (setq pop3-uidl-saved (pop3-uidl-load)
363 saved (cdr (assoc pop3-maildrop
364 (cdr (assoc pop3-mailhost
365 pop3-uidl-saved)))))
366 (let ((i (length pop3-uidl)))
367 (while (> i 0)
368 (unless (member (nth (1- i) pop3-uidl) saved)
369 (push i messages))
370 (cl-decf i)))
371 (when messages
372 (setq list (pop3-list process)
373 size 0)
374 (dolist (msg messages)
375 (setq size (+ size (cdr (assq msg list)))))
376 (list messages size)))))
377 (message "%s doesn't support UIDL (%s), so we try a regressive way..."
378 pop3-mailhost err)
379 (sit-for 1)
380 (setq size (pop3-stat process))
381 (dotimes (i (car size)) (push (1+ i) messages))
382 (setcar size (nreverse messages))
383 size)))
385 (defun pop3-uidl-dele (process)
386 "Delete messages according to `pop3-leave-mail-on-server'.
387 Return non-nil if it is necessary to update the local UIDL file."
388 (let* ((ctime (current-time))
389 (srvr (assoc pop3-mailhost pop3-uidl-saved))
390 (saved (assoc pop3-maildrop (cdr srvr)))
391 i uidl mod new tstamp dele)
392 (setcdr (cdr ctime) nil)
393 ;; Add new messages to the data to be saved.
394 (cond ((and pop3-uidl saved)
395 (setq i (1- (length pop3-uidl)))
396 (while (>= i 0)
397 (unless (member (setq uidl (nth i pop3-uidl)) (cdr saved))
398 (push ctime new)
399 (push uidl new))
400 (cl-decf i)))
401 (pop3-uidl
402 (setq new (mapcan (lambda (elt) (list elt ctime)) pop3-uidl))))
403 (when new (setq mod t))
404 ;; List expirable messages and delete them from the data to be saved.
405 (setq ctime (when (numberp pop3-leave-mail-on-server)
406 (/ (+ (* (car ctime) 65536.0) (cadr ctime)) 86400))
407 i (1- (length saved)))
408 (while (> i 0)
409 (if (member (setq uidl (nth (1- i) saved)) pop3-uidl)
410 (progn
411 (setq tstamp (nth i saved))
412 (if (and ctime
413 (> (- ctime (/ (+ (* (car tstamp) 65536.0) (cadr tstamp))
414 86400))
415 pop3-leave-mail-on-server))
416 ;; Mails to delete.
417 (progn
418 (setq mod t)
419 (push uidl dele))
420 ;; Mails to keep.
421 (push tstamp new)
422 (push uidl new)))
423 ;; Mails having been deleted in the server.
424 (setq mod t))
425 (cl-decf i 2))
426 (cond (saved
427 (setcdr saved new))
428 (srvr
429 (setcdr (last srvr) (list (cons pop3-maildrop new))))
431 (add-to-list 'pop3-uidl-saved
432 (list pop3-mailhost (cons pop3-maildrop new))
433 t)))
434 ;; Actually delete the messages in the server.
435 (when dele
436 (setq uidl nil
437 i (length pop3-uidl))
438 (while (> i 0)
439 (when (member (nth (1- i) pop3-uidl) dele)
440 (push i uidl))
441 (cl-decf i))
442 (when uidl
443 (pop3-send-streaming-command process "DELE" uidl nil)))
444 mod))
446 (defun pop3-uidl-load ()
447 "Load saved UIDL."
448 (when (file-exists-p pop3-uidl-file)
449 (with-temp-buffer
450 (condition-case code
451 (progn
452 (insert-file-contents pop3-uidl-file)
453 (goto-char (point-min))
454 (read (current-buffer)))
455 (error
456 (message "Error while loading %s (%s)"
457 pop3-uidl-file (error-message-string code))
458 (sit-for 1)
459 nil)))))
461 (defun pop3-uidl-save ()
462 "Save UIDL."
463 (with-temp-buffer
464 (if pop3-uidl-saved
465 (progn
466 (insert "(")
467 (dolist (srvr pop3-uidl-saved)
468 (when (cdr srvr)
469 (insert "(\"" (pop srvr) "\"\n ")
470 (dolist (elt srvr)
471 (when (cdr elt)
472 (insert "(\"" (pop elt) "\"\n ")
473 (while elt
474 (insert (format "\"%s\" %s\n " (pop elt) (pop elt))))
475 (delete-char -4)
476 (insert ")\n ")))
477 (delete-char -3)
478 (if (eq (char-before) ?\))
479 (insert ")\n ")
480 (goto-char (1+ (point-at-bol)))
481 (delete-region (point) (point-max)))))
482 (when (eq (char-before) ? )
483 (delete-char -2))
484 (insert ")\n"))
485 (insert "()\n"))
486 (let ((buffer-file-name pop3-uidl-file)
487 (delete-old-versions t)
488 (kept-new-versions kept-new-versions)
489 (kept-old-versions kept-old-versions)
490 (version-control version-control))
491 (if (consp pop3-uidl-file-backup)
492 (setq kept-new-versions (cadr pop3-uidl-file-backup)
493 kept-old-versions (car pop3-uidl-file-backup)
494 version-control t)
495 (setq version-control pop3-uidl-file-backup))
496 (save-buffer))))
498 (defun pop3-uidl-add-xheader (start msgno)
499 "Add X-UIDL header."
500 (let ((case-fold-search t))
501 (save-restriction
502 (narrow-to-region start (progn
503 (goto-char start)
504 (search-forward "\n\n" nil 'move)
505 (1- (point))))
506 (goto-char start)
507 (while (re-search-forward "^x-uidl:" nil t)
508 (while (progn
509 (forward-line 1)
510 (memq (char-after) '(?\t ? ))))
511 (delete-region (match-beginning 0) (point)))
512 (goto-char (point-max))
513 (insert "X-UIDL: " (nth (1- msgno) pop3-uidl) "\n"))))
515 (defcustom pop3-stream-type nil
516 "Transport security type for POP3 connections.
517 This may be either nil (plain connection), `ssl' (use an
518 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
519 to turn on TLS security after opening the stream). However, if
520 this is nil, `ssl' is assumed for connections to port
521 995 (pop3s)."
522 :version "23.1" ;; No Gnus
523 :group 'pop3
524 :type '(choice (const :tag "Plain" nil)
525 (const :tag "SSL/TLS" ssl)
526 (const starttls)))
528 (defun pop3-open-server (mailhost port)
529 "Open TCP connection to MAILHOST on PORT.
530 Returns the process associated with the connection."
531 (let ((coding-system-for-read 'binary)
532 (coding-system-for-write 'binary)
533 result)
534 (with-current-buffer
535 (get-buffer-create (concat " trace of POP session to "
536 mailhost))
537 (erase-buffer)
538 (setq pop3-read-point (point-min))
539 (setq result
540 (open-network-stream
541 "POP" (current-buffer) mailhost port
542 :type (cond
543 ((or (eq pop3-stream-type 'ssl)
544 (and (not pop3-stream-type)
545 (member port '(995 "pop3s"))))
546 'tls)
548 (or pop3-stream-type 'network)))
549 :warn-unless-encrypted t
550 :capability-command "CAPA\r\n"
551 :end-of-command "^\\(-ERR\\|+OK\\).*\n"
552 :end-of-capability "^\\.\r?\n\\|^-ERR"
553 :success "^\\+OK.*\n"
554 :return-list t
555 :starttls-function
556 (lambda (capabilities)
557 (and (string-match "\\bSTLS\\b" capabilities)
558 "STLS\r\n"))))
559 (when result
560 (let ((response (plist-get (cdr result) :greeting)))
561 (setq pop3-timestamp
562 (substring response (or (string-match "<" response) 0)
563 (+ 1 (or (string-match ">" response) -1)))))
564 (set-process-query-on-exit-flag (car result) nil)
565 (erase-buffer)
566 (car result)))))
568 ;; Support functions
570 (defun pop3-send-command (process command)
571 (set-buffer (process-buffer process))
572 (goto-char (point-max))
573 ;; (if (= (aref command 0) ?P)
574 ;; (insert "PASS <omitted>\r\n")
575 ;; (insert command "\r\n"))
576 (setq pop3-read-point (point))
577 (goto-char (point-max))
578 (process-send-string process (concat command "\r\n")))
580 (defun pop3-read-response (process &optional return)
581 "Read the response from the server.
582 Return the response string if optional second argument is non-nil."
583 (let ((case-fold-search nil)
584 match-end)
585 (with-current-buffer (process-buffer process)
586 (goto-char pop3-read-point)
587 (while (and (memq (process-status process) '(open run))
588 (not (search-forward "\r\n" nil t)))
589 (pop3-accept-process-output process)
590 (goto-char pop3-read-point))
591 (setq match-end (point))
592 (goto-char pop3-read-point)
593 (if (looking-at "-ERR")
594 (error "%s" (buffer-substring (point) (- match-end 2)))
595 (if (not (looking-at "+OK"))
596 (progn (setq pop3-read-point match-end) nil)
597 (setq pop3-read-point match-end)
598 (if return
599 (buffer-substring (point) match-end)
601 )))))
603 (defun pop3-clean-region (start end)
604 (setq end (set-marker (make-marker) end))
605 (save-excursion
606 (goto-char start)
607 (while (and (< (point) end) (search-forward "\r\n" end t))
608 (replace-match "\n" t t))
609 (goto-char start)
610 (while (and (< (point) end) (re-search-forward "^\\." end t))
611 (replace-match "" t t)
612 (forward-char)))
613 (set-marker end nil))
615 ;; Copied from message-make-date.
616 (defun pop3-make-date (&optional now)
617 "Make a valid date header.
618 If NOW, use that time instead."
619 (require 'parse-time)
620 (let* ((now (or now (current-time)))
621 (zone (nth 8 (decode-time now))))
622 (when (< zone 0)
623 (setq zone (- zone)))
624 (concat
625 (format-time-string "%d" now)
626 ;; The month name of the %b spec is locale-specific. Pfff.
627 (format " %s "
628 (capitalize (car (rassoc (nth 4 (decode-time now))
629 parse-time-months))))
630 (format-time-string "%Y %H:%M:%S %z" now))))
632 (defun pop3-munge-message-separator (start end)
633 "Check to see if a message separator exists. If not, generate one."
634 (save-excursion
635 (save-restriction
636 (narrow-to-region start end)
637 (goto-char (point-min))
638 (if (not (or (looking-at "From .?") ; Unix mail
639 (looking-at "\001\001\001\001\n") ; MMDF
640 (looking-at "BABYL OPTIONS:") ; Babyl
642 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
643 (tdate (mail-fetch-field "Date"))
644 (date (split-string (or (and tdate
645 (not (string= "" tdate))
646 tdate)
647 (pop3-make-date))
648 " "))
649 (From_))
650 ;; sample date formats I have seen
651 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
652 ;; Date: 08 Jul 1996 23:22:24 -0400
653 ;; should be
654 ;; Tue Jul 9 09:04:21 1996
656 ;; Fixme: This should use timezone on the date field contents.
657 (setq date
658 (cond ((not date)
659 "Tue Jan 1 00:00:0 1900")
660 ((string-match "[A-Z]" (nth 0 date))
661 (format "%s %s %s %s %s"
662 (nth 0 date) (nth 2 date) (nth 1 date)
663 (nth 4 date) (nth 3 date)))
665 ;; this really needs to be better but I don't feel
666 ;; like writing a date to day converter.
667 (format "Sun %s %s %s %s"
668 (nth 1 date) (nth 0 date)
669 (nth 3 date) (nth 2 date)))
671 (setq From_ (format "\nFrom %s %s\n" from date))
672 (while (string-match "," From_)
673 (setq From_ (concat (substring From_ 0 (match-beginning 0))
674 (substring From_ (match-end 0)))))
675 (goto-char (point-min))
676 (insert From_)
677 (if (search-forward "\n\n" nil t)
679 (goto-char (point-max))
680 (insert "\n"))
681 (let ((size (- (point-max) (point))))
682 (forward-line -1)
683 (insert (format "Content-Length: %s\n" size)))
684 )))))
686 ;; The Command Set
688 ;; AUTHORIZATION STATE
690 (defun pop3-user (process user)
691 "Send USER information to POP3 server."
692 (pop3-send-command process (format "USER %s" user))
693 (let ((response (pop3-read-response process t)))
694 (if (not (and response (string-match "+OK" response)))
695 (error "USER %s not valid" user))))
697 (defun pop3-pass (process)
698 "Send authentication information to the server."
699 (pop3-send-command process (format "PASS %s" pop3-password))
700 (let ((response (pop3-read-response process t)))
701 (if (not (and response (string-match "+OK" response)))
702 (pop3-quit process))))
704 (defun pop3-apop (process user)
705 "Send alternate authentication information to the server."
706 (let ((pass pop3-password))
707 (if (and pop3-password-required (not pass))
708 (setq pass
709 (read-passwd (format "Password for %s: " pop3-maildrop))))
710 (if pass
711 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
712 (pop3-send-command process (format "APOP %s %s" user hash))
713 (let ((response (pop3-read-response process t)))
714 (if (not (and response (string-match "+OK" response)))
715 (pop3-quit process)))))
718 ;; TRANSACTION STATE
720 (defun pop3-stat (process)
721 "Return the number of messages in the maildrop and the maildrop's size."
722 (pop3-send-command process "STAT")
723 (let ((response (pop3-read-response process t)))
724 (list (string-to-number (nth 1 (split-string response " ")))
725 (string-to-number (nth 2 (split-string response " "))))
728 (defun pop3-list (process &optional msg)
729 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
730 Otherwise, return the size of the message-id MSG"
731 (pop3-send-command process (if msg
732 (format "LIST %d" msg)
733 "LIST"))
734 (let ((response (pop3-read-response process t)))
735 (if msg
736 (string-to-number (nth 2 (split-string response " ")))
737 (let ((start pop3-read-point) end)
738 (with-current-buffer (process-buffer process)
739 (while (not (re-search-forward "^\\.\r\n" nil t))
740 (pop3-accept-process-output process)
741 (goto-char start))
742 (setq pop3-read-point (point-marker))
743 (goto-char (match-beginning 0))
744 (setq end (point-marker))
745 (mapcar #'(lambda (s) (let ((split (split-string s " ")))
746 (cons (string-to-number (nth 0 split))
747 (string-to-number (nth 1 split)))))
748 (split-string (buffer-substring start end) "\r\n" t)))))))
750 (defun pop3-retr (process msg crashbuf)
751 "Retrieve message-id MSG to buffer CRASHBUF."
752 (pop3-send-command process (format "RETR %s" msg))
753 (pop3-read-response process)
754 (let ((start pop3-read-point) end)
755 (with-current-buffer (process-buffer process)
756 (while (not (re-search-forward "^\\.\r\n" nil t))
757 (unless (memq (process-status process) '(open run))
758 (error "pop3 server closed the connection"))
759 (pop3-accept-process-output process)
760 (goto-char start))
761 (setq pop3-read-point (point-marker))
762 ;; this code does not seem to work for some POP servers...
763 ;; and I cannot figure out why not.
764 ;; (goto-char (match-beginning 0))
765 ;; (backward-char 2)
766 ;; (if (not (looking-at "\r\n"))
767 ;; (insert "\r\n"))
768 ;; (re-search-forward "\\.\r\n")
769 (goto-char (match-beginning 0))
770 (setq end (point-marker))
771 (pop3-clean-region start end)
772 (pop3-munge-message-separator start end)
773 (with-current-buffer crashbuf
774 (erase-buffer))
775 (copy-to-buffer crashbuf start end)
776 (delete-region start end)
779 (defun pop3-dele (process msg)
780 "Mark message-id MSG as deleted."
781 (pop3-send-command process (format "DELE %s" msg))
782 (pop3-read-response process))
784 (defun pop3-noop (process _msg)
785 "No-operation."
786 (pop3-send-command process "NOOP")
787 (pop3-read-response process))
789 (defun pop3-last (process)
790 "Return highest accessed message-id number for the session."
791 (pop3-send-command process "LAST")
792 (let ((response (pop3-read-response process t)))
793 (string-to-number (nth 1 (split-string response " ")))
796 (defun pop3-rset (process)
797 "Remove all delete marks from current maildrop."
798 (pop3-send-command process "RSET")
799 (pop3-read-response process))
801 ;; UPDATE
803 (defun pop3-quit (process)
804 "Close connection to POP3 server.
805 Tell server to remove all messages marked as deleted, unlock the maildrop,
806 and close the connection."
807 (pop3-send-command process "QUIT")
808 (pop3-read-response process t)
809 (if process
810 (with-current-buffer (process-buffer process)
811 (goto-char (point-max))
812 (delete-process process))))
814 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
816 ;;; AUTHORIZATION STATE
818 ;; Initial TCP connection
819 ;; Arguments: none
820 ;; Restrictions: none
821 ;; Possible responses:
822 ;; +OK [POP3 server ready]
824 ;; USER name
825 ;; Arguments: a server specific user-id (required)
826 ;; Restrictions: authorization state [after unsuccessful USER or PASS
827 ;; Possible responses:
828 ;; +OK [valid user-id]
829 ;; -ERR [invalid user-id]
831 ;; PASS string
832 ;; Arguments: a server/user-id specific password (required)
833 ;; Restrictions: authorization state, after successful USER
834 ;; Possible responses:
835 ;; +OK [maildrop locked and ready]
836 ;; -ERR [invalid password]
837 ;; -ERR [unable to lock maildrop]
839 ;; STLS (RFC 2595)
840 ;; Arguments: none
841 ;; Restrictions: Only permitted in AUTHORIZATION state.
842 ;; Possible responses:
843 ;; +OK
844 ;; -ERR
846 ;;; TRANSACTION STATE
848 ;; STAT
849 ;; Arguments: none
850 ;; Restrictions: transaction state
851 ;; Possible responses:
852 ;; +OK nn mm [# of messages, size of maildrop]
854 ;; LIST [msg]
855 ;; Arguments: a message-id (optional)
856 ;; Restrictions: transaction state; msg must not be deleted
857 ;; Possible responses:
858 ;; +OK [scan listing follows]
859 ;; -ERR [no such message]
861 ;; RETR msg
862 ;; Arguments: a message-id (required)
863 ;; Restrictions: transaction state; msg must not be deleted
864 ;; Possible responses:
865 ;; +OK [message contents follow]
866 ;; -ERR [no such message]
868 ;; DELE msg
869 ;; Arguments: a message-id (required)
870 ;; Restrictions: transaction state; msg must not be deleted
871 ;; Possible responses:
872 ;; +OK [message deleted]
873 ;; -ERR [no such message]
875 ;; NOOP
876 ;; Arguments: none
877 ;; Restrictions: transaction state
878 ;; Possible responses:
879 ;; +OK
881 ;; LAST
882 ;; Arguments: none
883 ;; Restrictions: transaction state
884 ;; Possible responses:
885 ;; +OK nn [highest numbered message accessed]
887 ;; RSET
888 ;; Arguments: none
889 ;; Restrictions: transaction state
890 ;; Possible responses:
891 ;; +OK [all delete marks removed]
893 ;; UIDL [msg]
894 ;; Arguments: a message-id (optional)
895 ;; Restrictions: transaction state; msg must not be deleted
896 ;; Possible responses:
897 ;; +OK [uidl listing follows]
898 ;; -ERR [no such message]
900 ;;; UPDATE STATE
902 ;; QUIT
903 ;; Arguments: none
904 ;; Restrictions: none
905 ;; Possible responses:
906 ;; +OK [TCP connection closed]
908 (provide 'pop3)
910 ;;; pop3.el ends here