]> code.delx.au - gnu-emacs/blob - lisp/net/pop3.el
Fix shr.el/image build problem
[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 (mapcan (lambda (elt) (list elt ctime)) pop3-uidl))))
406 (when new (setq mod t))
407 ;; List expirable messages and delete them from the data to be saved.
408 (setq ctime (when (numberp pop3-leave-mail-on-server)
409 (/ (+ (* (car ctime) 65536.0) (cadr ctime)) 86400))
410 i (1- (length saved)))
411 (while (> i 0)
412 (if (member (setq uidl (nth (1- i) saved)) pop3-uidl)
413 (progn
414 (setq tstamp (nth i saved))
415 (if (and ctime
416 (> (- ctime (/ (+ (* (car tstamp) 65536.0) (cadr tstamp))
417 86400))
418 pop3-leave-mail-on-server))
419 ;; Mails to delete.
420 (progn
421 (setq mod t)
422 (push uidl dele))
423 ;; Mails to keep.
424 (push tstamp new)
425 (push uidl new)))
426 ;; Mails having been deleted in the server.
427 (setq mod t))
428 (decf i 2))
429 (cond (saved
430 (setcdr saved new))
431 (srvr
432 (setcdr (last srvr) (list (cons pop3-maildrop new))))
433 (t
434 (add-to-list 'pop3-uidl-saved
435 (list pop3-mailhost (cons pop3-maildrop new))
436 t)))
437 ;; Actually delete the messages in the server.
438 (when dele
439 (setq uidl nil
440 i (length pop3-uidl))
441 (while (> i 0)
442 (when (member (nth (1- i) pop3-uidl) dele)
443 (push i uidl))
444 (decf i))
445 (when uidl
446 (pop3-send-streaming-command process "DELE" uidl nil)))
447 mod))
448
449 (defun pop3-uidl-load ()
450 "Load saved UIDL."
451 (when (file-exists-p pop3-uidl-file)
452 (with-temp-buffer
453 (condition-case code
454 (progn
455 (insert-file-contents pop3-uidl-file)
456 (goto-char (point-min))
457 (read (current-buffer)))
458 (error
459 (message "Error while loading %s (%s)"
460 pop3-uidl-file (error-message-string code))
461 (sit-for 1)
462 nil)))))
463
464 (defun pop3-uidl-save ()
465 "Save UIDL."
466 (with-temp-buffer
467 (if pop3-uidl-saved
468 (progn
469 (insert "(")
470 (dolist (srvr pop3-uidl-saved)
471 (when (cdr srvr)
472 (insert "(\"" (pop srvr) "\"\n ")
473 (dolist (elt srvr)
474 (when (cdr elt)
475 (insert "(\"" (pop elt) "\"\n ")
476 (while elt
477 (insert (format "\"%s\" %s\n " (pop elt) (pop elt))))
478 (delete-char -4)
479 (insert ")\n ")))
480 (delete-char -3)
481 (if (eq (char-before) ?\))
482 (insert ")\n ")
483 (goto-char (1+ (point-at-bol)))
484 (delete-region (point) (point-max)))))
485 (when (eq (char-before) ? )
486 (delete-char -2))
487 (insert ")\n"))
488 (insert "()\n"))
489 (let ((buffer-file-name pop3-uidl-file)
490 (delete-old-versions t)
491 (kept-new-versions kept-new-versions)
492 (kept-old-versions kept-old-versions)
493 (version-control version-control))
494 (if (consp pop3-uidl-file-backup)
495 (setq kept-new-versions (cadr pop3-uidl-file-backup)
496 kept-old-versions (car pop3-uidl-file-backup)
497 version-control t)
498 (setq version-control pop3-uidl-file-backup))
499 (save-buffer))))
500
501 (defun pop3-uidl-add-xheader (start msgno)
502 "Add X-UIDL header."
503 (let ((case-fold-search t))
504 (save-restriction
505 (narrow-to-region start (progn
506 (goto-char start)
507 (search-forward "\n\n" nil 'move)
508 (1- (point))))
509 (goto-char start)
510 (while (re-search-forward "^x-uidl:" nil t)
511 (while (progn
512 (forward-line 1)
513 (memq (char-after) '(?\t ? ))))
514 (delete-region (match-beginning 0) (point)))
515 (goto-char (point-max))
516 (insert "X-UIDL: " (nth (1- msgno) pop3-uidl) "\n"))))
517
518 (defcustom pop3-stream-type nil
519 "Transport security type for POP3 connections.
520 This may be either nil (plain connection), `ssl' (use an
521 SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
522 to turn on TLS security after opening the stream). However, if
523 this is nil, `ssl' is assumed for connections to port
524 995 (pop3s)."
525 :version "23.1" ;; No Gnus
526 :group 'pop3
527 :type '(choice (const :tag "Plain" nil)
528 (const :tag "SSL/TLS" ssl)
529 (const starttls)))
530
531 (defun pop3-open-server (mailhost port)
532 "Open TCP connection to MAILHOST on PORT.
533 Returns the process associated with the connection."
534 (let ((coding-system-for-read 'binary)
535 (coding-system-for-write 'binary)
536 result)
537 (with-current-buffer
538 (get-buffer-create (concat " trace of POP session to "
539 mailhost))
540 (erase-buffer)
541 (setq pop3-read-point (point-min))
542 (setq result
543 (open-network-stream
544 "POP" (current-buffer) mailhost port
545 :type (cond
546 ((or (eq pop3-stream-type 'ssl)
547 (and (not pop3-stream-type)
548 (member port '(995 "pop3s"))))
549 'tls)
550 (t
551 (or pop3-stream-type 'network)))
552 :warn-unless-encrypted t
553 :capability-command "CAPA\r\n"
554 :end-of-command "^\\(-ERR\\|+OK\\).*\n"
555 :end-of-capability "^\\.\r?\n\\|^-ERR"
556 :success "^\\+OK.*\n"
557 :return-list t
558 :starttls-function
559 (lambda (capabilities)
560 (and (string-match "\\bSTLS\\b" capabilities)
561 "STLS\r\n"))))
562 (when result
563 (let ((response (plist-get (cdr result) :greeting)))
564 (setq pop3-timestamp
565 (substring response (or (string-match "<" response) 0)
566 (+ 1 (or (string-match ">" response) -1)))))
567 (set-process-query-on-exit-flag (car result) nil)
568 (erase-buffer)
569 (car result)))))
570
571 ;; Support functions
572
573 (defun pop3-send-command (process command)
574 (set-buffer (process-buffer process))
575 (goto-char (point-max))
576 ;; (if (= (aref command 0) ?P)
577 ;; (insert "PASS <omitted>\r\n")
578 ;; (insert command "\r\n"))
579 (setq pop3-read-point (point))
580 (goto-char (point-max))
581 (process-send-string process (concat command "\r\n")))
582
583 (defun pop3-read-response (process &optional return)
584 "Read the response from the server.
585 Return the response string if optional second argument is non-nil."
586 (let ((case-fold-search nil)
587 match-end)
588 (with-current-buffer (process-buffer process)
589 (goto-char pop3-read-point)
590 (while (and (memq (process-status process) '(open run))
591 (not (search-forward "\r\n" nil t)))
592 (pop3-accept-process-output process)
593 (goto-char pop3-read-point))
594 (setq match-end (point))
595 (goto-char pop3-read-point)
596 (if (looking-at "-ERR")
597 (error "%s" (buffer-substring (point) (- match-end 2)))
598 (if (not (looking-at "+OK"))
599 (progn (setq pop3-read-point match-end) nil)
600 (setq pop3-read-point match-end)
601 (if return
602 (buffer-substring (point) match-end)
603 t)
604 )))))
605
606 (defun pop3-clean-region (start end)
607 (setq end (set-marker (make-marker) end))
608 (save-excursion
609 (goto-char start)
610 (while (and (< (point) end) (search-forward "\r\n" end t))
611 (replace-match "\n" t t))
612 (goto-char start)
613 (while (and (< (point) end) (re-search-forward "^\\." end t))
614 (replace-match "" t t)
615 (forward-char)))
616 (set-marker end nil))
617
618 ;; Copied from message-make-date.
619 (defun pop3-make-date (&optional now)
620 "Make a valid date header.
621 If NOW, use that time instead."
622 (require 'parse-time)
623 (let* ((now (or now (current-time)))
624 (zone (nth 8 (decode-time now)))
625 (sign "+"))
626 (when (< zone 0)
627 (setq sign "-")
628 (setq zone (- zone)))
629 (concat
630 (format-time-string "%d" now)
631 ;; The month name of the %b spec is locale-specific. Pfff.
632 (format " %s "
633 (capitalize (car (rassoc (nth 4 (decode-time now))
634 parse-time-months))))
635 (format-time-string "%Y %H:%M:%S %z" now))))
636
637 (defun pop3-munge-message-separator (start end)
638 "Check to see if a message separator exists. If not, generate one."
639 (save-excursion
640 (save-restriction
641 (narrow-to-region start end)
642 (goto-char (point-min))
643 (if (not (or (looking-at "From .?") ; Unix mail
644 (looking-at "\001\001\001\001\n") ; MMDF
645 (looking-at "BABYL OPTIONS:") ; Babyl
646 ))
647 (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
648 (tdate (mail-fetch-field "Date"))
649 (date (split-string (or (and tdate
650 (not (string= "" tdate))
651 tdate)
652 (pop3-make-date))
653 " "))
654 (From_))
655 ;; sample date formats I have seen
656 ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
657 ;; Date: 08 Jul 1996 23:22:24 -0400
658 ;; should be
659 ;; Tue Jul 9 09:04:21 1996
660
661 ;; Fixme: This should use timezone on the date field contents.
662 (setq date
663 (cond ((not date)
664 "Tue Jan 1 00:00:0 1900")
665 ((string-match "[A-Z]" (nth 0 date))
666 (format "%s %s %s %s %s"
667 (nth 0 date) (nth 2 date) (nth 1 date)
668 (nth 4 date) (nth 3 date)))
669 (t
670 ;; this really needs to be better but I don't feel
671 ;; like writing a date to day converter.
672 (format "Sun %s %s %s %s"
673 (nth 1 date) (nth 0 date)
674 (nth 3 date) (nth 2 date)))
675 ))
676 (setq From_ (format "\nFrom %s %s\n" from date))
677 (while (string-match "," From_)
678 (setq From_ (concat (substring From_ 0 (match-beginning 0))
679 (substring From_ (match-end 0)))))
680 (goto-char (point-min))
681 (insert From_)
682 (if (search-forward "\n\n" nil t)
683 nil
684 (goto-char (point-max))
685 (insert "\n"))
686 (let ((size (- (point-max) (point))))
687 (forward-line -1)
688 (insert (format "Content-Length: %s\n" size)))
689 )))))
690
691 ;; The Command Set
692
693 ;; AUTHORIZATION STATE
694
695 (defun pop3-user (process user)
696 "Send USER information to POP3 server."
697 (pop3-send-command process (format "USER %s" user))
698 (let ((response (pop3-read-response process t)))
699 (if (not (and response (string-match "+OK" response)))
700 (error "USER %s not valid" user))))
701
702 (defun pop3-pass (process)
703 "Send authentication information to the server."
704 (pop3-send-command process (format "PASS %s" pop3-password))
705 (let ((response (pop3-read-response process t)))
706 (if (not (and response (string-match "+OK" response)))
707 (pop3-quit process))))
708
709 (defun pop3-apop (process user)
710 "Send alternate authentication information to the server."
711 (let ((pass pop3-password))
712 (if (and pop3-password-required (not pass))
713 (setq pass
714 (read-passwd (format "Password for %s: " pop3-maildrop))))
715 (if pass
716 (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
717 (pop3-send-command process (format "APOP %s %s" user hash))
718 (let ((response (pop3-read-response process t)))
719 (if (not (and response (string-match "+OK" response)))
720 (pop3-quit process)))))
721 ))
722
723 ;; TRANSACTION STATE
724
725 (defun pop3-stat (process)
726 "Return the number of messages in the maildrop and the maildrop's size."
727 (pop3-send-command process "STAT")
728 (let ((response (pop3-read-response process t)))
729 (list (string-to-number (nth 1 (split-string response " ")))
730 (string-to-number (nth 2 (split-string response " "))))
731 ))
732
733 (defun pop3-list (process &optional msg)
734 "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
735 Otherwise, return the size of the message-id MSG"
736 (pop3-send-command process (if msg
737 (format "LIST %d" msg)
738 "LIST"))
739 (let ((response (pop3-read-response process t)))
740 (if msg
741 (string-to-number (nth 2 (split-string response " ")))
742 (let ((start pop3-read-point) end)
743 (with-current-buffer (process-buffer process)
744 (while (not (re-search-forward "^\\.\r\n" nil t))
745 (pop3-accept-process-output process)
746 (goto-char start))
747 (setq pop3-read-point (point-marker))
748 (goto-char (match-beginning 0))
749 (setq end (point-marker))
750 (mapcar #'(lambda (s) (let ((split (split-string s " ")))
751 (cons (string-to-number (nth 0 split))
752 (string-to-number (nth 1 split)))))
753 (split-string (buffer-substring start end) "\r\n" t)))))))
754
755 (defun pop3-retr (process msg crashbuf)
756 "Retrieve message-id MSG to buffer CRASHBUF."
757 (pop3-send-command process (format "RETR %s" msg))
758 (pop3-read-response process)
759 (let ((start pop3-read-point) end)
760 (with-current-buffer (process-buffer process)
761 (while (not (re-search-forward "^\\.\r\n" nil t))
762 (unless (memq (process-status process) '(open run))
763 (error "pop3 server closed the connection"))
764 (pop3-accept-process-output process)
765 (goto-char start))
766 (setq pop3-read-point (point-marker))
767 ;; this code does not seem to work for some POP servers...
768 ;; and I cannot figure out why not.
769 ;; (goto-char (match-beginning 0))
770 ;; (backward-char 2)
771 ;; (if (not (looking-at "\r\n"))
772 ;; (insert "\r\n"))
773 ;; (re-search-forward "\\.\r\n")
774 (goto-char (match-beginning 0))
775 (setq end (point-marker))
776 (pop3-clean-region start end)
777 (pop3-munge-message-separator start end)
778 (with-current-buffer crashbuf
779 (erase-buffer))
780 (copy-to-buffer crashbuf start end)
781 (delete-region start end)
782 )))
783
784 (defun pop3-dele (process msg)
785 "Mark message-id MSG as deleted."
786 (pop3-send-command process (format "DELE %s" msg))
787 (pop3-read-response process))
788
789 (defun pop3-noop (process msg)
790 "No-operation."
791 (pop3-send-command process "NOOP")
792 (pop3-read-response process))
793
794 (defun pop3-last (process)
795 "Return highest accessed message-id number for the session."
796 (pop3-send-command process "LAST")
797 (let ((response (pop3-read-response process t)))
798 (string-to-number (nth 1 (split-string response " ")))
799 ))
800
801 (defun pop3-rset (process)
802 "Remove all delete marks from current maildrop."
803 (pop3-send-command process "RSET")
804 (pop3-read-response process))
805
806 ;; UPDATE
807
808 (defun pop3-quit (process)
809 "Close connection to POP3 server.
810 Tell server to remove all messages marked as deleted, unlock the maildrop,
811 and close the connection."
812 (pop3-send-command process "QUIT")
813 (pop3-read-response process t)
814 (if process
815 (with-current-buffer (process-buffer process)
816 (goto-char (point-max))
817 (delete-process process))))
818 \f
819 ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
820
821 ;;; AUTHORIZATION STATE
822
823 ;; Initial TCP connection
824 ;; Arguments: none
825 ;; Restrictions: none
826 ;; Possible responses:
827 ;; +OK [POP3 server ready]
828
829 ;; USER name
830 ;; Arguments: a server specific user-id (required)
831 ;; Restrictions: authorization state [after unsuccessful USER or PASS
832 ;; Possible responses:
833 ;; +OK [valid user-id]
834 ;; -ERR [invalid user-id]
835
836 ;; PASS string
837 ;; Arguments: a server/user-id specific password (required)
838 ;; Restrictions: authorization state, after successful USER
839 ;; Possible responses:
840 ;; +OK [maildrop locked and ready]
841 ;; -ERR [invalid password]
842 ;; -ERR [unable to lock maildrop]
843
844 ;; STLS (RFC 2595)
845 ;; Arguments: none
846 ;; Restrictions: Only permitted in AUTHORIZATION state.
847 ;; Possible responses:
848 ;; +OK
849 ;; -ERR
850
851 ;;; TRANSACTION STATE
852
853 ;; STAT
854 ;; Arguments: none
855 ;; Restrictions: transaction state
856 ;; Possible responses:
857 ;; +OK nn mm [# of messages, size of maildrop]
858
859 ;; LIST [msg]
860 ;; Arguments: a message-id (optional)
861 ;; Restrictions: transaction state; msg must not be deleted
862 ;; Possible responses:
863 ;; +OK [scan listing follows]
864 ;; -ERR [no such message]
865
866 ;; RETR msg
867 ;; Arguments: a message-id (required)
868 ;; Restrictions: transaction state; msg must not be deleted
869 ;; Possible responses:
870 ;; +OK [message contents follow]
871 ;; -ERR [no such message]
872
873 ;; DELE msg
874 ;; Arguments: a message-id (required)
875 ;; Restrictions: transaction state; msg must not be deleted
876 ;; Possible responses:
877 ;; +OK [message deleted]
878 ;; -ERR [no such message]
879
880 ;; NOOP
881 ;; Arguments: none
882 ;; Restrictions: transaction state
883 ;; Possible responses:
884 ;; +OK
885
886 ;; LAST
887 ;; Arguments: none
888 ;; Restrictions: transaction state
889 ;; Possible responses:
890 ;; +OK nn [highest numbered message accessed]
891
892 ;; RSET
893 ;; Arguments: none
894 ;; Restrictions: transaction state
895 ;; Possible responses:
896 ;; +OK [all delete marks removed]
897
898 ;; UIDL [msg]
899 ;; Arguments: a message-id (optional)
900 ;; Restrictions: transaction state; msg must not be deleted
901 ;; Possible responses:
902 ;; +OK [uidl listing follows]
903 ;; -ERR [no such message]
904
905 ;;; UPDATE STATE
906
907 ;; QUIT
908 ;; Arguments: none
909 ;; Restrictions: none
910 ;; Possible responses:
911 ;; +OK [TCP connection closed]
912
913 (provide 'pop3)
914
915 ;;; pop3.el ends here