]> code.delx.au - gnu-emacs/blob - lisp/net/pop3.el
; Fix breakage from previous commit
[gnu-emacs] / lisp / net / pop3.el
1 ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
2
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
4
5 ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
30
31 ;; This program was inspired by Kyle E. Jones's vm-pop program.
32
33 ;;; Code:
34
35 (eval-when-compile (require 'cl))
36
37 (require 'mail-utils)
38 (defvar parse-time-months)
39
40 (defgroup pop3 nil
41 "Post Office Protocol."
42 :group 'mail
43 :group 'mail-source)
44
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)
52
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)
59
60 (defcustom pop3-port 110
61 "POP3 port."
62 :version "22.1" ;; Oort Gnus
63 :type 'number
64 :group 'pop3)
65
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)
71
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)
77
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)
86
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)
95
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.
99
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.
104
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.
107
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)
117
118 (defcustom pop3-uidl-file "~/.pop3-uidl"
119 "File used to save UIDL."
120 :version "24.4"
121 :type 'file
122 :group 'pop3)
123
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).
129
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)
145
146 (defvar pop3-timestamp nil
147 "Timestamp returned when initially connected to the POP server.
148 Used for APOP authentication.")
149
150 (defvar pop3-read-point nil)
151 (defvar pop3-debug nil)
152
153 ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
154 ;; comments there for explanations about the values.
155
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 (string-match "windows-nt\\|os/2\\|cygwin"
163 (symbol-name system-type))
164 1.0
165 0.01)
166 "How long pop3 should wait between checking for the end of output.
167 Shorter values mean quicker response, but are more CPU intensive.")
168 (defun pop3-accept-process-output (process)
169 (accept-process-output
170 process
171 (truncate pop3-read-timeout)
172 (truncate (* (- pop3-read-timeout
173 (truncate pop3-read-timeout))
174 1000))))))
175
176 (defvar pop3-uidl)
177 ;; List of UIDLs of existing messages at present in the server:
178 ;; ("UIDL1" "UIDL2" "UIDL3"...)
179
180 (defvar pop3-uidl-saved)
181 ;; Locally saved UIDL data; an alist of the server, the user, and the UIDL
182 ;; and timestamp pairs:
183 ;; (("SERVER_A" ("USER_A1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
184 ;; ("USER_A2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
185 ;; ...)
186 ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
187 ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
188 ;; ...))
189 ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
190 ;; i.e. the return value of `current-time'.
191
192 ;;;###autoload
193 (defun pop3-movemail (file)
194 "Transfer contents of a maildrop to the specified FILE.
195 Use streaming commands."
196 (let ((process (pop3-open-server pop3-mailhost pop3-port))
197 messages total-size
198 pop3-uidl
199 pop3-uidl-saved)
200 (pop3-logon process)
201 (if pop3-leave-mail-on-server
202 (setq messages (pop3-uidl-stat process)
203 total-size (cadr messages)
204 messages (car messages))
205 (let ((size (pop3-stat process)))
206 (dotimes (i (car size)) (push (1+ i) messages))
207 (setq messages (nreverse messages)
208 total-size (cadr size))))
209 (when messages
210 (with-current-buffer (process-buffer process)
211 (pop3-send-streaming-command process "RETR" messages total-size)
212 (pop3-write-to-file file messages)
213 (unless pop3-leave-mail-on-server
214 (pop3-send-streaming-command process "DELE" messages nil))))
215 (if pop3-leave-mail-on-server
216 (when (prog1 (pop3-uidl-dele process) (pop3-quit process))
217 (pop3-uidl-save))
218 (pop3-quit process)
219 ;; Remove UIDL data for the account that got not to leave mails.
220 (setq pop3-uidl-saved (pop3-uidl-load))
221 (let ((elt (assoc pop3-maildrop
222 (cdr (assoc pop3-mailhost pop3-uidl-saved)))))
223 (when elt
224 (setcdr elt nil)
225 (pop3-uidl-save))))
226 t))
227
228 (defun pop3-send-streaming-command (process command messages total-size)
229 (erase-buffer)
230 (let ((count (length messages))
231 (i 1)
232 (start-point (point-min))
233 (waited-for 0))
234 (while messages
235 (process-send-string process (format "%s %d\r\n" command (pop messages)))
236 ;; Only do 100 messages at a time to avoid pipe stalls.
237 (when (zerop (% i pop3-stream-length))
238 (setq start-point
239 (pop3-wait-for-messages process pop3-stream-length
240 total-size start-point))
241 (incf waited-for pop3-stream-length))
242 (incf i))
243 (pop3-wait-for-messages process (- count waited-for)
244 total-size start-point)))
245
246 (defun pop3-wait-for-messages (process count total-size start-point)
247 (while (> count 0)
248 (goto-char start-point)
249 (while (or (and (re-search-forward "^\\+OK" nil t)
250 (or (not total-size)
251 (re-search-forward "^\\.\r?\n" nil t)))
252 (re-search-forward "^-ERR " nil t))
253 (decf count)
254 (setq start-point (point)))
255 (unless (memq (process-status process) '(open run))
256 (error "pop3 process died"))
257 (when total-size
258 (let ((size 0))
259 (goto-char (point-min))
260 (while (re-search-forward "^\\+OK.*\n" nil t)
261 (setq size (+ size (- (point))
262 (if (re-search-forward "^\\.\r?\n" nil 'move)
263 (match-beginning 0)
264 (point)))))
265 (message "pop3 retrieved %dKB (%d%%)"
266 (truncate (/ size 1000))
267 (truncate (* (/ (* size 1.0) total-size) 100)))))
268 (pop3-accept-process-output process))
269 start-point)
270
271 (defun pop3-write-to-file (file messages)
272 (let ((pop-buffer (current-buffer))
273 (start (point-min))
274 beg end
275 temp-buffer)
276 (with-temp-buffer
277 (setq temp-buffer (current-buffer))
278 (with-current-buffer pop-buffer
279 (goto-char (point-min))
280 (while (re-search-forward "^\\+OK" nil t)
281 (forward-line 1)
282 (setq beg (point))
283 (when (re-search-forward "^\\.\r?\n" nil t)
284 (setq start (point))
285 (forward-line -1)
286 (setq end (point)))
287 (with-current-buffer temp-buffer
288 (goto-char (point-max))
289 (let ((hstart (point)))
290 (insert-buffer-substring pop-buffer beg end)
291 (pop3-clean-region hstart (point))
292 (goto-char (point-max))
293 (pop3-munge-message-separator hstart (point))
294 (when pop3-leave-mail-on-server
295 (pop3-uidl-add-xheader hstart (pop messages)))
296 (goto-char (point-max))))))
297 (let ((coding-system-for-write 'binary))
298 (goto-char (point-min))
299 ;; Check whether something inserted a newline at the start and
300 ;; delete it.
301 (when (eolp)
302 (delete-char 1))
303 (write-region (point-min) (point-max) file nil 'nomesg)))))
304
305 (defun pop3-logon (process)
306 (let ((pop3-password pop3-password))
307 ;; for debugging only
308 (if pop3-debug (switch-to-buffer (process-buffer process)))
309 ;; query for password
310 (if (and pop3-password-required (not pop3-password))
311 (setq pop3-password
312 (read-passwd (format "Password for %s: " pop3-maildrop))))
313 (cond ((equal 'apop pop3-authentication-scheme)
314 (pop3-apop process pop3-maildrop))
315 ((equal 'pass pop3-authentication-scheme)
316 (pop3-user process pop3-maildrop)
317 (pop3-pass process))
318 (t (error "Invalid POP3 authentication scheme")))))
319
320 (defun pop3-get-message-count ()
321 "Return the number of messages in the maildrop."
322 (let* ((process (pop3-open-server pop3-mailhost pop3-port))
323 message-count
324 (pop3-password pop3-password))
325 ;; for debugging only
326 (if pop3-debug (switch-to-buffer (process-buffer process)))
327 ;; query for password
328 (if (and pop3-password-required (not pop3-password))
329 (setq pop3-password
330 (read-passwd (format "Password for %s: " pop3-maildrop))))
331 (cond ((equal 'apop pop3-authentication-scheme)
332 (pop3-apop process pop3-maildrop))
333 ((equal 'pass pop3-authentication-scheme)
334 (pop3-user process pop3-maildrop)
335 (pop3-pass process))
336 (t (error "Invalid POP3 authentication scheme")))
337 (setq message-count (car (pop3-stat process)))
338 (pop3-quit process)
339 message-count))
340
341 (defun pop3-uidl-stat (process)
342 "Return a list of unread message numbers and total size."
343 (pop3-send-command process "UIDL")
344 (let (err messages size)
345 (if (condition-case code
346 (progn
347 (pop3-read-response process)
348 t)
349 (error (setq err (error-message-string code))
350 nil))
351 (let ((start pop3-read-point)
352 saved list)
353 (with-current-buffer (process-buffer process)
354 (while (not (re-search-forward "^\\.\r\n" nil t))
355 (unless (memq (process-status process) '(open run))
356 (error "pop3 server closed the connection"))
357 (pop3-accept-process-output process)
358 (goto-char start))
359 (setq pop3-read-point (point-marker)
360 pop3-uidl nil)
361 (while (progn (forward-line -1) (>= (point) start))
362 (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
363 (push (match-string 1) pop3-uidl)))
364 (when pop3-uidl
365 (setq pop3-uidl-saved (pop3-uidl-load)
366 saved (cdr (assoc pop3-maildrop
367 (cdr (assoc pop3-mailhost
368 pop3-uidl-saved)))))
369 (let ((i (length pop3-uidl)))
370 (while (> i 0)
371 (unless (member (nth (1- i) pop3-uidl) saved)
372 (push i messages))
373 (decf i)))
374 (when messages
375 (setq list (pop3-list process)
376 size 0)
377 (dolist (msg messages)
378 (setq size (+ size (cdr (assq msg list)))))
379 (list messages size)))))
380 (message "%s doesn't support UIDL (%s), so we try a regressive way..."
381 pop3-mailhost err)
382 (sit-for 1)
383 (setq size (pop3-stat process))
384 (dotimes (i (car size)) (push (1+ i) messages))
385 (setcar size (nreverse messages))
386 size)))
387
388 (defun pop3-uidl-dele (process)
389 "Delete messages according to `pop3-leave-mail-on-server'.
390 Return non-nil if it is necessary to update the local UIDL file."
391 (let* ((ctime (current-time))
392 (srvr (assoc pop3-mailhost pop3-uidl-saved))
393 (saved (assoc pop3-maildrop (cdr srvr)))
394 i uidl mod new tstamp dele)
395 (setcdr (cdr ctime) nil)
396 ;; Add new messages to the data to be saved.
397 (cond ((and pop3-uidl saved)
398 (setq i (1- (length pop3-uidl)))
399 (while (>= i 0)
400 (unless (member (setq uidl (nth i pop3-uidl)) (cdr saved))
401 (push ctime new)
402 (push uidl new))
403 (decf i)))
404 (pop3-uidl
405 (setq new (apply 'nconc (mapcar (lambda (elt) (list elt ctime))
406 pop3-uidl)))))
407 (when new (setq mod t))
408 ;; List expirable messages and delete them from the data to be saved.
409 (setq ctime (when (numberp pop3-leave-mail-on-server)
410 (/ (+ (* (car ctime) 65536.0) (cadr ctime)) 86400))
411 i (1- (length saved)))
412 (while (> i 0)
413 (if (member (setq uidl (nth (1- i) saved)) pop3-uidl)
414 (progn
415 (setq tstamp (nth i saved))
416 (if (and ctime
417 (> (- ctime (/ (+ (* (car tstamp) 65536.0) (cadr tstamp))
418 86400))
419 pop3-leave-mail-on-server))
420 ;; Mails to delete.
421 (progn
422 (setq mod t)
423 (push uidl dele))
424 ;; Mails to keep.
425 (push tstamp new)
426 (push uidl new)))
427 ;; Mails having been deleted in the server.
428 (setq mod t))
429 (decf i 2))
430 (cond (saved
431 (setcdr saved new))
432 (srvr
433 (setcdr (last srvr) (list (cons pop3-maildrop new))))
434 (t
435 (add-to-list 'pop3-uidl-saved
436 (list pop3-mailhost (cons pop3-maildrop new))
437 t)))
438 ;; Actually delete the messages in the server.
439 (when dele
440 (setq uidl nil
441 i (length pop3-uidl))
442 (while (> i 0)
443 (when (member (nth (1- i) pop3-uidl) dele)
444 (push i uidl))
445 (decf i))
446 (when uidl
447 (pop3-send-streaming-command process "DELE" uidl nil)))
448 mod))
449
450 (defun pop3-uidl-load ()
451 "Load saved UIDL."
452 (when (file-exists-p pop3-uidl-file)
453 (with-temp-buffer
454 (condition-case code
455 (progn
456 (insert-file-contents pop3-uidl-file)
457 (goto-char (point-min))
458 (read (current-buffer)))
459 (error
460 (message "Error while loading %s (%s)"
461 pop3-uidl-file (error-message-string code))
462 (sit-for 1)
463 nil)))))
464
465 (defun pop3-uidl-save ()
466 "Save UIDL."
467 (with-temp-buffer
468 (if pop3-uidl-saved
469 (progn
470 (insert "(")
471 (dolist (srvr pop3-uidl-saved)
472 (when (cdr srvr)
473 (insert "(\"" (pop srvr) "\"\n ")
474 (dolist (elt srvr)
475 (when (cdr elt)
476 (insert "(\"" (pop elt) "\"\n ")
477 (while elt
478 (insert (format "\"%s\" %s\n " (pop elt) (pop elt))))
479 (delete-char -4)
480 (insert ")\n ")))
481 (delete-char -3)
482 (if (eq (char-before) ?\))
483 (insert ")\n ")
484 (goto-char (1+ (point-at-bol)))
485 (delete-region (point) (point-max)))))
486 (when (eq (char-before) ? )
487 (delete-char -2))
488 (insert ")\n"))
489 (insert "()\n"))
490 (let ((buffer-file-name pop3-uidl-file)
491 (delete-old-versions t)
492 (kept-new-versions kept-new-versions)
493 (kept-old-versions kept-old-versions)
494 (version-control version-control))
495 (if (consp pop3-uidl-file-backup)
496 (setq kept-new-versions (cadr pop3-uidl-file-backup)
497 kept-old-versions (car pop3-uidl-file-backup)
498 version-control t)
499 (setq version-control pop3-uidl-file-backup))
500 (save-buffer))))
501
502 (defun pop3-uidl-add-xheader (start msgno)
503 "Add X-UIDL header."
504 (let ((case-fold-search t))
505 (save-restriction
506 (narrow-to-region start (progn
507 (goto-char start)
508 (search-forward "\n\n" nil 'move)
509 (1- (point))))
510 (goto-char start)
511 (while (re-search-forward "^x-uidl:" nil t)
512 (while (progn
513 (forward-line 1)
514 (memq (char-after) '(?\t ? ))))
515 (delete-region (match-beginning 0) (point)))
516 (goto-char (point-max))
517 (insert "X-UIDL: " (nth (1- msgno) pop3-uidl) "\n"))))
518
519 (defcustom pop3-stream-type nil
520 "Transport security type for POP3 connections.
521 This may be either nil (plain connection), `ssl' (use an
522 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
523 to turn on TLS security after opening the stream). However, if
524 this is nil, `ssl' is assumed for connections to port
525 995 (pop3s)."
526 :version "23.1" ;; No Gnus
527 :group 'pop3
528 :type '(choice (const :tag "Plain" nil)
529 (const :tag "SSL/TLS" ssl)
530 (const starttls)))
531
532 (defun pop3-open-server (mailhost port)
533 "Open TCP connection to MAILHOST on PORT.
534 Returns the process associated with the connection."
535 (let ((coding-system-for-read 'binary)
536 (coding-system-for-write 'binary)
537 result)
538 (with-current-buffer
539 (get-buffer-create (concat " trace of POP session to "
540 mailhost))
541 (erase-buffer)
542 (setq pop3-read-point (point-min))
543 (setq result
544 (open-network-stream
545 "POP" (current-buffer) mailhost port
546 :type (cond
547 ((or (eq pop3-stream-type 'ssl)
548 (and (not pop3-stream-type)
549 (member port '(995 "pop3s"))))
550 'tls)
551 (t
552 (or pop3-stream-type 'network)))
553 :warn-unless-encrypted t
554 :capability-command "CAPA\r\n"
555 :end-of-command "^\\(-ERR\\|+OK\\).*\n"
556 :end-of-capability "^\\.\r?\n\\|^-ERR"
557 :success "^\\+OK.*\n"
558 :return-list t
559 :starttls-function
560 (lambda (capabilities)
561 (and (string-match "\\bSTLS\\b" capabilities)
562 "STLS\r\n"))))
563 (when result
564 (let ((response (plist-get (cdr result) :greeting)))
565 (setq pop3-timestamp
566 (substring response (or (string-match "<" response) 0)
567 (+ 1 (or (string-match ">" response) -1)))))
568 (set-process-query-on-exit-flag (car result) nil)
569 (erase-buffer)
570 (car result)))))
571
572 ;; Support functions
573
574 (defun pop3-send-command (process command)
575 (set-buffer (process-buffer process))
576 (goto-char (point-max))
577 ;; (if (= (aref command 0) ?P)
578 ;; (insert "PASS <omitted>\r\n")
579 ;; (insert command "\r\n"))
580 (setq pop3-read-point (point))
581 (goto-char (point-max))
582 (process-send-string process (concat command "\r\n")))
583
584 (defun pop3-read-response (process &optional return)
585 "Read the response from the server.
586 Return the response string if optional second argument is non-nil."
587 (let ((case-fold-search nil)
588 match-end)
589 (with-current-buffer (process-buffer process)
590 (goto-char pop3-read-point)
591 (while (and (memq (process-status process) '(open run))
592 (not (search-forward "\r\n" nil t)))
593 (pop3-accept-process-output process)
594 (goto-char pop3-read-point))
595 (setq match-end (point))
596 (goto-char pop3-read-point)
597 (if (looking-at "-ERR")
598 (error "%s" (buffer-substring (point) (- match-end 2)))
599 (if (not (looking-at "+OK"))
600 (progn (setq pop3-read-point match-end) nil)
601 (setq pop3-read-point match-end)
602 (if return
603 (buffer-substring (point) match-end)
604 t)
605 )))))
606
607 (defun pop3-clean-region (start end)
608 (setq end (set-marker (make-marker) end))
609 (save-excursion
610 (goto-char start)
611 (while (and (< (point) end) (search-forward "\r\n" end t))
612 (replace-match "\n" t t))
613 (goto-char start)
614 (while (and (< (point) end) (re-search-forward "^\\." end t))
615 (replace-match "" t t)
616 (forward-char)))
617 (set-marker end nil))
618
619 ;; Copied from message-make-date.
620 (defun pop3-make-date (&optional now)
621 "Make a valid date header.
622 If NOW, use that time instead."
623 (require 'parse-time)
624 (let* ((now (or now (current-time)))
625 (zone (nth 8 (decode-time now)))
626 (sign "+"))
627 (when (< zone 0)
628 (setq sign "-")
629 (setq zone (- zone)))
630 (concat
631 (format-time-string "%d" now)
632 ;; The month name of the %b spec is locale-specific. Pfff.
633 (format " %s "
634 (capitalize (car (rassoc (nth 4 (decode-time now))
635 parse-time-months))))
636 (format-time-string "%Y %H:%M:%S %z" now))))
637
638 (defun pop3-munge-message-separator (start end)
639 "Check to see if a message separator exists. If not, generate one."
640 (save-excursion
641 (save-restriction
642 (narrow-to-region start end)
643 (goto-char (point-min))
644 (if (not (or (looking-at "From .?") ; Unix mail
645 (looking-at "\001\001\001\001\n") ; MMDF
646 (looking-at "BABYL OPTIONS:") ; Babyl
647 ))
648 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
649 (tdate (mail-fetch-field "Date"))
650 (date (split-string (or (and tdate
651 (not (string= "" tdate))
652 tdate)
653 (pop3-make-date))
654 " "))
655 (From_))
656 ;; sample date formats I have seen
657 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
658 ;; Date: 08 Jul 1996 23:22:24 -0400
659 ;; should be
660 ;; Tue Jul 9 09:04:21 1996
661
662 ;; Fixme: This should use timezone on the date field contents.
663 (setq date
664 (cond ((not date)
665 "Tue Jan 1 00:00:0 1900")
666 ((string-match "[A-Z]" (nth 0 date))
667 (format "%s %s %s %s %s"
668 (nth 0 date) (nth 2 date) (nth 1 date)
669 (nth 4 date) (nth 3 date)))
670 (t
671 ;; this really needs to be better but I don't feel
672 ;; like writing a date to day converter.
673 (format "Sun %s %s %s %s"
674 (nth 1 date) (nth 0 date)
675 (nth 3 date) (nth 2 date)))
676 ))
677 (setq From_ (format "\nFrom %s %s\n" from date))
678 (while (string-match "," From_)
679 (setq From_ (concat (substring From_ 0 (match-beginning 0))
680 (substring From_ (match-end 0)))))
681 (goto-char (point-min))
682 (insert From_)
683 (if (search-forward "\n\n" nil t)
684 nil
685 (goto-char (point-max))
686 (insert "\n"))
687 (let ((size (- (point-max) (point))))
688 (forward-line -1)
689 (insert (format "Content-Length: %s\n" size)))
690 )))))
691
692 ;; The Command Set
693
694 ;; AUTHORIZATION STATE
695
696 (defun pop3-user (process user)
697 "Send USER information to POP3 server."
698 (pop3-send-command process (format "USER %s" user))
699 (let ((response (pop3-read-response process t)))
700 (if (not (and response (string-match "+OK" response)))
701 (error "USER %s not valid" user))))
702
703 (defun pop3-pass (process)
704 "Send authentication information to the server."
705 (pop3-send-command process (format "PASS %s" pop3-password))
706 (let ((response (pop3-read-response process t)))
707 (if (not (and response (string-match "+OK" response)))
708 (pop3-quit process))))
709
710 (defun pop3-apop (process user)
711 "Send alternate authentication information to the server."
712 (let ((pass pop3-password))
713 (if (and pop3-password-required (not pass))
714 (setq pass
715 (read-passwd (format "Password for %s: " pop3-maildrop))))
716 (if pass
717 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
718 (pop3-send-command process (format "APOP %s %s" user hash))
719 (let ((response (pop3-read-response process t)))
720 (if (not (and response (string-match "+OK" response)))
721 (pop3-quit process)))))
722 ))
723
724 ;; TRANSACTION STATE
725
726 (defun pop3-stat (process)
727 "Return the number of messages in the maildrop and the maildrop's size."
728 (pop3-send-command process "STAT")
729 (let ((response (pop3-read-response process t)))
730 (list (string-to-number (nth 1 (split-string response " ")))
731 (string-to-number (nth 2 (split-string response " "))))
732 ))
733
734 (defun pop3-list (process &optional msg)
735 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
736 Otherwise, return the size of the message-id MSG"
737 (pop3-send-command process (if msg
738 (format "LIST %d" msg)
739 "LIST"))
740 (let ((response (pop3-read-response process t)))
741 (if msg
742 (string-to-number (nth 2 (split-string response " ")))
743 (let ((start pop3-read-point) end)
744 (with-current-buffer (process-buffer process)
745 (while (not (re-search-forward "^\\.\r\n" nil t))
746 (pop3-accept-process-output process)
747 (goto-char start))
748 (setq pop3-read-point (point-marker))
749 (goto-char (match-beginning 0))
750 (setq end (point-marker))
751 (mapcar #'(lambda (s) (let ((split (split-string s " ")))
752 (cons (string-to-number (nth 0 split))
753 (string-to-number (nth 1 split)))))
754 (split-string (buffer-substring start end) "\r\n" t)))))))
755
756 (defun pop3-retr (process msg crashbuf)
757 "Retrieve message-id MSG to buffer CRASHBUF."
758 (pop3-send-command process (format "RETR %s" msg))
759 (pop3-read-response process)
760 (let ((start pop3-read-point) end)
761 (with-current-buffer (process-buffer process)
762 (while (not (re-search-forward "^\\.\r\n" nil t))
763 (unless (memq (process-status process) '(open run))
764 (error "pop3 server closed the connection"))
765 (pop3-accept-process-output process)
766 (goto-char start))
767 (setq pop3-read-point (point-marker))
768 ;; this code does not seem to work for some POP servers...
769 ;; and I cannot figure out why not.
770 ;; (goto-char (match-beginning 0))
771 ;; (backward-char 2)
772 ;; (if (not (looking-at "\r\n"))
773 ;; (insert "\r\n"))
774 ;; (re-search-forward "\\.\r\n")
775 (goto-char (match-beginning 0))
776 (setq end (point-marker))
777 (pop3-clean-region start end)
778 (pop3-munge-message-separator start end)
779 (with-current-buffer crashbuf
780 (erase-buffer))
781 (copy-to-buffer crashbuf start end)
782 (delete-region start end)
783 )))
784
785 (defun pop3-dele (process msg)
786 "Mark message-id MSG as deleted."
787 (pop3-send-command process (format "DELE %s" msg))
788 (pop3-read-response process))
789
790 (defun pop3-noop (process msg)
791 "No-operation."
792 (pop3-send-command process "NOOP")
793 (pop3-read-response process))
794
795 (defun pop3-last (process)
796 "Return highest accessed message-id number for the session."
797 (pop3-send-command process "LAST")
798 (let ((response (pop3-read-response process t)))
799 (string-to-number (nth 1 (split-string response " ")))
800 ))
801
802 (defun pop3-rset (process)
803 "Remove all delete marks from current maildrop."
804 (pop3-send-command process "RSET")
805 (pop3-read-response process))
806
807 ;; UPDATE
808
809 (defun pop3-quit (process)
810 "Close connection to POP3 server.
811 Tell server to remove all messages marked as deleted, unlock the maildrop,
812 and close the connection."
813 (pop3-send-command process "QUIT")
814 (pop3-read-response process t)
815 (if process
816 (with-current-buffer (process-buffer process)
817 (goto-char (point-max))
818 (delete-process process))))
819 \f
820 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
821
822 ;;; AUTHORIZATION STATE
823
824 ;; Initial TCP connection
825 ;; Arguments: none
826 ;; Restrictions: none
827 ;; Possible responses:
828 ;; +OK [POP3 server ready]
829
830 ;; USER name
831 ;; Arguments: a server specific user-id (required)
832 ;; Restrictions: authorization state [after unsuccessful USER or PASS
833 ;; Possible responses:
834 ;; +OK [valid user-id]
835 ;; -ERR [invalid user-id]
836
837 ;; PASS string
838 ;; Arguments: a server/user-id specific password (required)
839 ;; Restrictions: authorization state, after successful USER
840 ;; Possible responses:
841 ;; +OK [maildrop locked and ready]
842 ;; -ERR [invalid password]
843 ;; -ERR [unable to lock maildrop]
844
845 ;; STLS (RFC 2595)
846 ;; Arguments: none
847 ;; Restrictions: Only permitted in AUTHORIZATION state.
848 ;; Possible responses:
849 ;; +OK
850 ;; -ERR
851
852 ;;; TRANSACTION STATE
853
854 ;; STAT
855 ;; Arguments: none
856 ;; Restrictions: transaction state
857 ;; Possible responses:
858 ;; +OK nn mm [# of messages, size of maildrop]
859
860 ;; LIST [msg]
861 ;; Arguments: a message-id (optional)
862 ;; Restrictions: transaction state; msg must not be deleted
863 ;; Possible responses:
864 ;; +OK [scan listing follows]
865 ;; -ERR [no such message]
866
867 ;; RETR msg
868 ;; Arguments: a message-id (required)
869 ;; Restrictions: transaction state; msg must not be deleted
870 ;; Possible responses:
871 ;; +OK [message contents follow]
872 ;; -ERR [no such message]
873
874 ;; DELE msg
875 ;; Arguments: a message-id (required)
876 ;; Restrictions: transaction state; msg must not be deleted
877 ;; Possible responses:
878 ;; +OK [message deleted]
879 ;; -ERR [no such message]
880
881 ;; NOOP
882 ;; Arguments: none
883 ;; Restrictions: transaction state
884 ;; Possible responses:
885 ;; +OK
886
887 ;; LAST
888 ;; Arguments: none
889 ;; Restrictions: transaction state
890 ;; Possible responses:
891 ;; +OK nn [highest numbered message accessed]
892
893 ;; RSET
894 ;; Arguments: none
895 ;; Restrictions: transaction state
896 ;; Possible responses:
897 ;; +OK [all delete marks removed]
898
899 ;; UIDL [msg]
900 ;; Arguments: a message-id (optional)
901 ;; Restrictions: transaction state; msg must not be deleted
902 ;; Possible responses:
903 ;; +OK [uidl listing follows]
904 ;; -ERR [no such message]
905
906 ;;; UPDATE STATE
907
908 ;; QUIT
909 ;; Arguments: none
910 ;; Restrictions: none
911 ;; Possible responses:
912 ;; +OK [TCP connection closed]
913
914 (provide 'pop3)
915
916 ;;; pop3.el ends here