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