123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- (eval-when-compile (require 'cl))
- (eval-and-compile
-
-
- (unless (fboundp 'open-protocol-stream)
- (require 'proto-stream)))
- (require 'mail-utils)
- (defvar parse-time-months)
- (defgroup pop3 nil
- "Post Office Protocol."
- :group 'mail
- :group 'mail-source)
- (defcustom pop3-maildrop (or (user-login-name)
- (getenv "LOGNAME")
- (getenv "USER"))
- "*POP3 maildrop."
- :version "22.1"
- :type 'string
- :group 'pop3)
- (defcustom pop3-mailhost (or (getenv "MAILHOST")
- "pop3")
- "*POP3 mailhost."
- :version "22.1"
- :type 'string
- :group 'pop3)
- (defcustom pop3-port 110
- "*POP3 port."
- :version "22.1"
- :type 'number
- :group 'pop3)
- (defcustom pop3-password-required t
- "*Non-nil if a password is required when connecting to POP server."
- :version "22.1"
- :type 'boolean
- :group 'pop3)
- (defvar pop3-password nil
- "*Password to use when connecting to POP server.")
- (defcustom pop3-authentication-scheme 'pass
- "*POP3 authentication scheme.
- Defaults to `pass', for the standard USER/PASS authentication. The other
- valid value is 'apop'."
- :type '(choice (const :tag "Normal user/password" pass)
- (const :tag "APOP" apop))
- :version "22.1"
- :group 'pop3)
- (defcustom pop3-stream-length 100
- "How many messages should be requested at one time.
- The lower the number, the more latency-sensitive the fetching
- will be. If your pop3 server doesn't support streaming at all,
- set this to 1."
- :type 'number
- :version "24.1"
- :group 'pop3)
- (defcustom pop3-leave-mail-on-server nil
- "*Non-nil if the mail is to be left on the POP server after fetching.
- If `pop3-leave-mail-on-server' is non-nil the mail is to be left
- on the POP server after fetching. Note that POP servers maintain
- no state information between sessions, so what the client
- believes is there and what is actually there may not match up.
- If they do not, then you may get duplicate mails or the whole
- thing can fall apart and leave you with a corrupt mailbox."
-
-
-
-
- :version "22.1"
- :type 'boolean
- :group 'pop3)
- (defvar pop3-timestamp nil
- "Timestamp returned when initially connected to the POP server.
- Used for APOP authentication.")
- (defvar pop3-read-point nil)
- (defvar pop3-debug nil)
- (eval-and-compile
- (if (and (fboundp 'nnheader-accept-process-output)
- (boundp 'nnheader-read-timeout))
- (defalias 'pop3-accept-process-output 'nnheader-accept-process-output)
-
- (defvar pop3-read-timeout
- (if (string-match "windows-nt\\|os/2\\|cygwin"
- (symbol-name system-type))
- 1.0
- 0.01)
- "How long pop3 should wait between checking for the end of output.
- Shorter values mean quicker response, but are more CPU intensive.")
- (defun pop3-accept-process-output (process)
- (accept-process-output
- process
- (truncate pop3-read-timeout)
- (truncate (* (- pop3-read-timeout
- (truncate pop3-read-timeout))
- 1000))))))
- (defun pop3-movemail (file)
- "Transfer contents of a maildrop to the specified FILE.
- Use streaming commands."
- (let* ((process (pop3-open-server pop3-mailhost pop3-port))
- message-count message-total-size)
- (pop3-logon process)
- (with-current-buffer (process-buffer process)
- (let ((size (pop3-stat process)))
- (setq message-count (car size)
- message-total-size (cadr size)))
- (when (> message-count 0)
- (pop3-send-streaming-command
- process "RETR" message-count message-total-size)
- (pop3-write-to-file file)
- (unless pop3-leave-mail-on-server
- (pop3-send-streaming-command
- process "DELE" message-count nil))))
- (pop3-quit process)
- t))
- (defun pop3-send-streaming-command (process command count total-size)
- (erase-buffer)
- (let ((i 1)
- (start-point (point-min))
- (waited-for 0))
- (while (>= count i)
- (process-send-string process (format "%s %d\r\n" command i))
-
- (when (zerop (% i pop3-stream-length))
- (setq start-point
- (pop3-wait-for-messages process pop3-stream-length
- total-size start-point))
- (incf waited-for pop3-stream-length))
- (incf i))
- (pop3-wait-for-messages process (- count waited-for)
- total-size start-point)))
- (defun pop3-wait-for-messages (process count total-size start-point)
- (while (> count 0)
- (goto-char start-point)
- (while (or (and (re-search-forward "^\\+OK" nil t)
- (or (not total-size)
- (re-search-forward "^\\.\r?\n" nil t)))
- (re-search-forward "^-ERR " nil t))
- (decf count)
- (setq start-point (point)))
- (unless (memq (process-status process) '(open run))
- (error "pop3 process died"))
- (when total-size
- (message "pop3 retrieved %dKB (%d%%)"
- (truncate (/ (buffer-size) 1000))
- (truncate (* (/ (* (buffer-size) 1.0)
- total-size) 100))))
- (pop3-accept-process-output process))
- start-point)
- (defun pop3-write-to-file (file)
- (let ((pop-buffer (current-buffer))
- (start (point-min))
- beg end
- temp-buffer)
- (with-temp-buffer
- (setq temp-buffer (current-buffer))
- (with-current-buffer pop-buffer
- (goto-char (point-min))
- (while (re-search-forward "^\\+OK" nil t)
- (forward-line 1)
- (setq beg (point))
- (when (re-search-forward "^\\.\r?\n" nil t)
- (setq start (point))
- (forward-line -1)
- (setq end (point)))
- (with-current-buffer temp-buffer
- (goto-char (point-max))
- (let ((hstart (point)))
- (insert-buffer-substring pop-buffer beg end)
- (pop3-clean-region hstart (point))
- (goto-char (point-max))
- (pop3-munge-message-separator hstart (point))
- (goto-char (point-max))))))
- (let ((coding-system-for-write 'binary))
- (goto-char (point-min))
-
-
- (when (eolp)
- (delete-char 1))
- (write-region (point-min) (point-max) file nil 'nomesg)))))
- (defun pop3-logon (process)
- (let ((pop3-password pop3-password))
-
- (if pop3-debug (switch-to-buffer (process-buffer process)))
-
- (if (and pop3-password-required (not pop3-password))
- (setq pop3-password
- (read-passwd (format "Password for %s: " pop3-maildrop))))
- (cond ((equal 'apop pop3-authentication-scheme)
- (pop3-apop process pop3-maildrop))
- ((equal 'pass pop3-authentication-scheme)
- (pop3-user process pop3-maildrop)
- (pop3-pass process))
- (t (error "Invalid POP3 authentication scheme")))))
- (defun pop3-get-message-count ()
- "Return the number of messages in the maildrop."
- (let* ((process (pop3-open-server pop3-mailhost pop3-port))
- message-count
- (pop3-password pop3-password))
-
- (if pop3-debug (switch-to-buffer (process-buffer process)))
-
- (if (and pop3-password-required (not pop3-password))
- (setq pop3-password
- (read-passwd (format "Password for %s: " pop3-maildrop))))
- (cond ((equal 'apop pop3-authentication-scheme)
- (pop3-apop process pop3-maildrop))
- ((equal 'pass pop3-authentication-scheme)
- (pop3-user process pop3-maildrop)
- (pop3-pass process))
- (t (error "Invalid POP3 authentication scheme")))
- (setq message-count (car (pop3-stat process)))
- (pop3-quit process)
- message-count))
- (defcustom pop3-stream-type nil
- "*Transport security type for POP3 connections.
- This may be either nil (plain connection), `ssl' (use an
- SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
- to turn on TLS security after opening the stream). However, if
- this is nil, `ssl' is assumed for connections to port
- 995 (pop3s)."
- :version "23.1"
- :group 'pop3
- :type '(choice (const :tag "Plain" nil)
- (const :tag "SSL/TLS" ssl)
- (const starttls)))
- (eval-and-compile
- (if (fboundp 'set-process-query-on-exit-flag)
- (defalias 'pop3-set-process-query-on-exit-flag
- 'set-process-query-on-exit-flag)
- (defalias 'pop3-set-process-query-on-exit-flag
- 'process-kill-without-query)))
- (defun pop3-open-server (mailhost port)
- "Open TCP connection to MAILHOST on PORT.
- Returns the process associated with the connection."
- (let ((coding-system-for-read 'binary)
- (coding-system-for-write 'binary)
- result)
- (with-current-buffer
- (get-buffer-create (concat " trace of POP session to "
- mailhost))
- (erase-buffer)
- (setq pop3-read-point (point-min))
- (setq result
- (open-protocol-stream
- "POP" (current-buffer) mailhost port
- :type (cond
- ((or (eq pop3-stream-type 'ssl)
- (and (not pop3-stream-type)
- (member port '(995 "pop3s"))))
- 'tls)
- (t
- (or pop3-stream-type 'network)))
- :capability-command "CAPA\r\n"
- :end-of-command "^\\(-ERR\\|+OK\\).*\n"
- :end-of-capability "^\\.\r?\n\\|^-ERR"
- :success "^\\+OK.*\n"
- :return-list t
- :starttls-function
- (lambda (capabilities)
- (and (string-match "\\bSTLS\\b" capabilities)
- "STLS\r\n"))))
- (when result
- (let ((response (plist-get (cdr result) :greeting)))
- (setq pop3-timestamp
- (substring response (or (string-match "<" response) 0)
- (+ 1 (or (string-match ">" response) -1)))))
- (pop3-set-process-query-on-exit-flag (car result) nil)
- (erase-buffer)
- (car result)))))
- (defun pop3-send-command (process command)
- (set-buffer (process-buffer process))
- (goto-char (point-max))
-
-
-
- (setq pop3-read-point (point))
- (goto-char (point-max))
- (process-send-string process (concat command "\r\n")))
- (defun pop3-read-response (process &optional return)
- "Read the response from the server.
- Return the response string if optional second argument is non-nil."
- (let ((case-fold-search nil)
- match-end)
- (with-current-buffer (process-buffer process)
- (goto-char pop3-read-point)
- (while (and (memq (process-status process) '(open run))
- (not (search-forward "\r\n" nil t)))
- (pop3-accept-process-output process)
- (goto-char pop3-read-point))
- (setq match-end (point))
- (goto-char pop3-read-point)
- (if (looking-at "-ERR")
- (error "%s" (buffer-substring (point) (- match-end 2)))
- (if (not (looking-at "+OK"))
- (progn (setq pop3-read-point match-end) nil)
- (setq pop3-read-point match-end)
- (if return
- (buffer-substring (point) match-end)
- t)
- )))))
- (defun pop3-clean-region (start end)
- (setq end (set-marker (make-marker) end))
- (save-excursion
- (goto-char start)
- (while (and (< (point) end) (search-forward "\r\n" end t))
- (replace-match "\n" t t))
- (goto-char start)
- (while (and (< (point) end) (re-search-forward "^\\." end t))
- (replace-match "" t t)
- (forward-char)))
- (set-marker end nil))
- (defun pop3-make-date (&optional now)
- "Make a valid date header.
- If NOW, use that time instead."
- (require 'parse-time)
- (let* ((now (or now (current-time)))
- (zone (nth 8 (decode-time now)))
- (sign "+"))
- (when (< zone 0)
- (setq sign "-")
- (setq zone (- zone)))
- (concat
- (format-time-string "%d" now)
-
- (format " %s "
- (capitalize (car (rassoc (nth 4 (decode-time now))
- parse-time-months))))
- (format-time-string "%Y %H:%M:%S " now)
-
- (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
- (defun pop3-munge-message-separator (start end)
- "Check to see if a message separator exists. If not, generate one."
- (save-excursion
- (save-restriction
- (narrow-to-region start end)
- (goto-char (point-min))
- (if (not (or (looking-at "From .?")
- (looking-at "\001\001\001\001\n")
- (looking-at "BABYL OPTIONS:")
- ))
- (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
- (tdate (mail-fetch-field "Date"))
- (date (split-string (or (and tdate
- (not (string= "" tdate))
- tdate)
- (pop3-make-date))
- " "))
- (From_))
-
-
-
-
-
-
- (setq date
- (cond ((not date)
- "Tue Jan 1 00:00:0 1900")
- ((string-match "[A-Z]" (nth 0 date))
- (format "%s %s %s %s %s"
- (nth 0 date) (nth 2 date) (nth 1 date)
- (nth 4 date) (nth 3 date)))
- (t
-
-
- (format "Sun %s %s %s %s"
- (nth 1 date) (nth 0 date)
- (nth 3 date) (nth 2 date)))
- ))
- (setq From_ (format "\nFrom %s %s\n" from date))
- (while (string-match "," From_)
- (setq From_ (concat (substring From_ 0 (match-beginning 0))
- (substring From_ (match-end 0)))))
- (goto-char (point-min))
- (insert From_)
- (if (search-forward "\n\n" nil t)
- nil
- (goto-char (point-max))
- (insert "\n"))
- (let ((size (- (point-max) (point))))
- (forward-line -1)
- (insert (format "Content-Length: %s\n" size)))
- )))))
- (defun pop3-user (process user)
- "Send USER information to POP3 server."
- (pop3-send-command process (format "USER %s" user))
- (let ((response (pop3-read-response process t)))
- (if (not (and response (string-match "+OK" response)))
- (error "USER %s not valid" user))))
- (defun pop3-pass (process)
- "Send authentication information to the server."
- (pop3-send-command process (format "PASS %s" pop3-password))
- (let ((response (pop3-read-response process t)))
- (if (not (and response (string-match "+OK" response)))
- (pop3-quit process))))
- (defun pop3-apop (process user)
- "Send alternate authentication information to the server."
- (let ((pass pop3-password))
- (if (and pop3-password-required (not pass))
- (setq pass
- (read-passwd (format "Password for %s: " pop3-maildrop))))
- (if pass
- (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
- (pop3-send-command process (format "APOP %s %s" user hash))
- (let ((response (pop3-read-response process t)))
- (if (not (and response (string-match "+OK" response)))
- (pop3-quit process)))))
- ))
- (defun pop3-stat (process)
- "Return the number of messages in the maildrop and the maildrop's size."
- (pop3-send-command process "STAT")
- (let ((response (pop3-read-response process t)))
- (list (string-to-number (nth 1 (split-string response " ")))
- (string-to-number (nth 2 (split-string response " "))))
- ))
- (defun pop3-list (process &optional msg)
- "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
- Otherwise, return the size of the message-id MSG"
- (pop3-send-command process (if msg
- (format "LIST %d" msg)
- "LIST"))
- (let ((response (pop3-read-response process t)))
- (if msg
- (string-to-number (nth 2 (split-string response " ")))
- (let ((start pop3-read-point) end)
- (with-current-buffer (process-buffer process)
- (while (not (re-search-forward "^\\.\r\n" nil t))
- (pop3-accept-process-output process)
- (goto-char start))
- (setq pop3-read-point (point-marker))
- (goto-char (match-beginning 0))
- (setq end (point-marker))
- (mapcar #'(lambda (s) (let ((split (split-string s " ")))
- (cons (string-to-number (nth 0 split))
- (string-to-number (nth 1 split)))))
- (split-string (buffer-substring start end) "\r\n" t)))))))
- (defun pop3-retr (process msg crashbuf)
- "Retrieve message-id MSG to buffer CRASHBUF."
- (pop3-send-command process (format "RETR %s" msg))
- (pop3-read-response process)
- (let ((start pop3-read-point) end)
- (with-current-buffer (process-buffer process)
- (while (not (re-search-forward "^\\.\r\n" nil t))
- (unless (memq (process-status process) '(open run))
- (error "pop3 server closed the connection"))
- (pop3-accept-process-output process)
- (goto-char start))
- (setq pop3-read-point (point-marker))
-
-
-
-
-
-
-
- (goto-char (match-beginning 0))
- (setq end (point-marker))
- (pop3-clean-region start end)
- (pop3-munge-message-separator start end)
- (with-current-buffer crashbuf
- (erase-buffer))
- (copy-to-buffer crashbuf start end)
- (delete-region start end)
- )))
- (defun pop3-dele (process msg)
- "Mark message-id MSG as deleted."
- (pop3-send-command process (format "DELE %s" msg))
- (pop3-read-response process))
- (defun pop3-noop (process msg)
- "No-operation."
- (pop3-send-command process "NOOP")
- (pop3-read-response process))
- (defun pop3-last (process)
- "Return highest accessed message-id number for the session."
- (pop3-send-command process "LAST")
- (let ((response (pop3-read-response process t)))
- (string-to-number (nth 1 (split-string response " ")))
- ))
- (defun pop3-rset (process)
- "Remove all delete marks from current maildrop."
- (pop3-send-command process "RSET")
- (pop3-read-response process))
- (defun pop3-quit (process)
- "Close connection to POP3 server.
- Tell server to remove all messages marked as deleted, unlock the maildrop,
- and close the connection."
- (pop3-send-command process "QUIT")
- (pop3-read-response process t)
- (if process
- (with-current-buffer (process-buffer process)
- (goto-char (point-max))
- (delete-process process))))
- (provide 'pop3)
|