Commit | Line | Data |
---|---|---|
eec82323 LMI |
1 | ;;; pop3.el --- Post Office Protocol (RFC 1460) interface |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1996-2012 Free Software Foundation, Inc. |
eec82323 LMI |
4 | |
5 | ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net> | |
dd5da9b8 DL |
6 | ;; Maintainer: FSF |
7 | ;; Keywords: mail | |
eec82323 LMI |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
5e809f55 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
eec82323 | 12 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
eec82323 LMI |
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 | |
5e809f55 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
eec82323 LMI |
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 | ||
ed96ace9 | 35 | (eval-when-compile (require 'cl)) |
b87f32fc G |
36 | |
37 | (eval-and-compile | |
38 | ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for | |
39 | ;; `make-network-stream'. | |
40 | (unless (fboundp 'open-protocol-stream) | |
41 | (require 'proto-stream))) | |
42 | ||
eec82323 | 43 | (require 'mail-utils) |
9efa445f | 44 | (defvar parse-time-months) |
eec82323 | 45 | |
e62e7654 | 46 | (defgroup pop3 nil |
62a3378e | 47 | "Post Office Protocol." |
e62e7654 MB |
48 | :group 'mail |
49 | :group 'mail-source) | |
50 | ||
51 | (defcustom pop3-maildrop (or (user-login-name) | |
52 | (getenv "LOGNAME") | |
53 | (getenv "USER")) | |
54 | "*POP3 maildrop." | |
bf247b6e | 55 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
56 | :type 'string |
57 | :group 'pop3) | |
58 | ||
59 | (defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch | |
60 | "pop3") | |
61 | "*POP3 mailhost." | |
bf247b6e | 62 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
63 | :type 'string |
64 | :group 'pop3) | |
65 | ||
66 | (defcustom pop3-port 110 | |
67 | "*POP3 port." | |
bf247b6e | 68 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
69 | :type 'number |
70 | :group 'pop3) | |
71 | ||
72 | (defcustom pop3-password-required t | |
73 | "*Non-nil if a password is required when connecting to POP server." | |
bf247b6e | 74 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
75 | :type 'boolean |
76 | :group 'pop3) | |
77 | ||
78 | ;; Should this be customizable? | |
eec82323 LMI |
79 | (defvar pop3-password nil |
80 | "*Password to use when connecting to POP server.") | |
81 | ||
e62e7654 | 82 | (defcustom pop3-authentication-scheme 'pass |
eec82323 | 83 | "*POP3 authentication scheme. |
996aa8c1 MB |
84 | Defaults to `pass', for the standard USER/PASS authentication. The other |
85 | valid value is 'apop'." | |
86 | :type '(choice (const :tag "Normal user/password" pass) | |
e62e7654 | 87 | (const :tag "APOP" apop)) |
996aa8c1 | 88 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
89 | :group 'pop3) |
90 | ||
229b59da G |
91 | (defcustom pop3-stream-length 100 |
92 | "How many messages should be requested at one time. | |
93 | The lower the number, the more latency-sensitive the fetching | |
94 | will be. If your pop3 server doesn't support streaming at all, | |
95 | set this to 1." | |
96 | :type 'number | |
97 | :version "24.1" | |
98 | :group 'pop3) | |
99 | ||
e62e7654 | 100 | (defcustom pop3-leave-mail-on-server nil |
8903a9c8 MB |
101 | "*Non-nil if the mail is to be left on the POP server after fetching. |
102 | ||
996aa8c1 MB |
103 | If `pop3-leave-mail-on-server' is non-nil the mail is to be left |
104 | on the POP server after fetching. Note that POP servers maintain | |
105 | no state information between sessions, so what the client | |
106 | believes is there and what is actually there may not match up. | |
107 | If they do not, then you may get duplicate mails or the whole | |
108 | thing can fall apart and leave you with a corrupt mailbox." | |
b110774a MB |
109 | ;; We can't use the UILD support from XEmacs mail-lib or cvs.m17n.org: |
110 | ;; http://thread.gmane.org/v9lld8fml4.fsf@marauder.physik.uni-ulm.de | |
111 | ;; http://thread.gmane.org/b9yy8hzy9ej.fsf@jpl.org | |
112 | ;; Any volunteer to re-implement this? | |
bf247b6e | 113 | :version "22.1" ;; Oort Gnus |
e62e7654 MB |
114 | :type 'boolean |
115 | :group 'pop3) | |
3e7b210c | 116 | |
eec82323 LMI |
117 | (defvar pop3-timestamp nil |
118 | "Timestamp returned when initially connected to the POP server. | |
119 | Used for APOP authentication.") | |
120 | ||
121 | (defvar pop3-read-point nil) | |
122 | (defvar pop3-debug nil) | |
123 | ||
e3e955fe MB |
124 | ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the |
125 | ;; comments there for explanations about the values. | |
126 | ||
127 | (eval-and-compile | |
128 | (if (and (fboundp 'nnheader-accept-process-output) | |
129 | (boundp 'nnheader-read-timeout)) | |
130 | (defalias 'pop3-accept-process-output 'nnheader-accept-process-output) | |
131 | ;; Borrowed from `nnheader.el': | |
132 | (defvar pop3-read-timeout | |
f5ec697d | 133 | (if (string-match "windows-nt\\|os/2\\|cygwin" |
e3e955fe MB |
134 | (symbol-name system-type)) |
135 | 1.0 | |
136 | 0.01) | |
137 | "How long pop3 should wait between checking for the end of output. | |
8903a9c8 | 138 | Shorter values mean quicker response, but are more CPU intensive.") |
e3e955fe MB |
139 | (defun pop3-accept-process-output (process) |
140 | (accept-process-output | |
141 | process | |
142 | (truncate pop3-read-timeout) | |
143 | (truncate (* (- pop3-read-timeout | |
144 | (truncate pop3-read-timeout)) | |
145 | 1000)))))) | |
6459e35e | 146 | |
e574f629 LMI |
147 | ;;;###autoload |
148 | (defun pop3-movemail (file) | |
a2bb410e LMI |
149 | "Transfer contents of a maildrop to the specified FILE. |
150 | Use streaming commands." | |
eec82323 | 151 | (let* ((process (pop3-open-server pop3-mailhost pop3-port)) |
a2bb410e LMI |
152 | message-count message-total-size) |
153 | (pop3-logon process) | |
154 | (with-current-buffer (process-buffer process) | |
155 | (let ((size (pop3-stat process))) | |
156 | (setq message-count (car size) | |
157 | message-total-size (cadr size))) | |
d99a4591 | 158 | (when (> message-count 0) |
a2bb410e LMI |
159 | (pop3-send-streaming-command |
160 | process "RETR" message-count message-total-size) | |
161 | (pop3-write-to-file file) | |
162 | (unless pop3-leave-mail-on-server | |
163 | (pop3-send-streaming-command | |
ff3eb82e | 164 | process "DELE" message-count nil)))) |
3ff31c90 LMI |
165 | (pop3-quit process) |
166 | t)) | |
a2bb410e LMI |
167 | |
168 | (defun pop3-send-streaming-command (process command count total-size) | |
169 | (erase-buffer) | |
2146e256 LMI |
170 | (let ((i 1) |
171 | (start-point (point-min)) | |
172 | (waited-for 0)) | |
530b8957 | 173 | (while (>= count i) |
a2bb410e LMI |
174 | (process-send-string process (format "%s %d\r\n" command i)) |
175 | ;; Only do 100 messages at a time to avoid pipe stalls. | |
229b59da | 176 | (when (zerop (% i pop3-stream-length)) |
2146e256 LMI |
177 | (setq start-point |
178 | (pop3-wait-for-messages process pop3-stream-length | |
179 | total-size start-point)) | |
180 | (incf waited-for pop3-stream-length)) | |
181 | (incf i)) | |
182 | (pop3-wait-for-messages process (- count waited-for) | |
183 | total-size start-point))) | |
184 | ||
185 | (defun pop3-wait-for-messages (process count total-size start-point) | |
186 | (while (> count 0) | |
187 | (goto-char start-point) | |
188 | (while (or (and (re-search-forward "^\\+OK" nil t) | |
189 | (or (not total-size) | |
190 | (re-search-forward "^\\.\r?\n" nil t))) | |
191 | (re-search-forward "^-ERR " nil t)) | |
192 | (decf count) | |
193 | (setq start-point (point))) | |
1e91d506 G |
194 | (unless (memq (process-status process) '(open run)) |
195 | (error "pop3 process died")) | |
a2bb410e LMI |
196 | (when total-size |
197 | (message "pop3 retrieved %dKB (%d%%)" | |
198 | (truncate (/ (buffer-size) 1000)) | |
199 | (truncate (* (/ (* (buffer-size) 1.0) | |
200 | total-size) 100)))) | |
2146e256 LMI |
201 | (pop3-accept-process-output process)) |
202 | start-point) | |
a2bb410e LMI |
203 | |
204 | (defun pop3-write-to-file (file) | |
205 | (let ((pop-buffer (current-buffer)) | |
206 | (start (point-min)) | |
207 | beg end | |
208 | temp-buffer) | |
209 | (with-temp-buffer | |
210 | (setq temp-buffer (current-buffer)) | |
211 | (with-current-buffer pop-buffer | |
212 | (goto-char (point-min)) | |
213 | (while (re-search-forward "^\\+OK" nil t) | |
214 | (forward-line 1) | |
215 | (setq beg (point)) | |
216 | (when (re-search-forward "^\\.\r?\n" nil t) | |
217 | (setq start (point)) | |
218 | (forward-line -1) | |
219 | (setq end (point))) | |
220 | (with-current-buffer temp-buffer | |
221 | (goto-char (point-max)) | |
222 | (let ((hstart (point))) | |
223 | (insert-buffer-substring pop-buffer beg end) | |
224 | (pop3-clean-region hstart (point)) | |
225 | (goto-char (point-max)) | |
226 | (pop3-munge-message-separator hstart (point)) | |
227 | (goto-char (point-max)))))) | |
228 | (let ((coding-system-for-write 'binary)) | |
229 | (goto-char (point-min)) | |
230 | ;; Check whether something inserted a newline at the start and | |
231 | ;; delete it. | |
232 | (when (eolp) | |
233 | (delete-char 1)) | |
530b8957 | 234 | (write-region (point-min) (point-max) file nil 'nomesg))))) |
a2bb410e | 235 | |
a2bb410e LMI |
236 | (defun pop3-logon (process) |
237 | (let ((pop3-password pop3-password)) | |
eec82323 LMI |
238 | ;; for debugging only |
239 | (if pop3-debug (switch-to-buffer (process-buffer process))) | |
6748645f LMI |
240 | ;; query for password |
241 | (if (and pop3-password-required (not pop3-password)) | |
242 | (setq pop3-password | |
23f87bed | 243 | (read-passwd (format "Password for %s: " pop3-maildrop)))) |
eec82323 LMI |
244 | (cond ((equal 'apop pop3-authentication-scheme) |
245 | (pop3-apop process pop3-maildrop)) | |
246 | ((equal 'pass pop3-authentication-scheme) | |
247 | (pop3-user process pop3-maildrop) | |
248 | (pop3-pass process)) | |
a2bb410e LMI |
249 | (t (error "Invalid POP3 authentication scheme"))))) |
250 | ||
619ac84f SZ |
251 | (defun pop3-get-message-count () |
252 | "Return the number of messages in the maildrop." | |
253 | (let* ((process (pop3-open-server pop3-mailhost pop3-port)) | |
254 | message-count | |
e62e7654 | 255 | (pop3-password pop3-password)) |
619ac84f SZ |
256 | ;; for debugging only |
257 | (if pop3-debug (switch-to-buffer (process-buffer process))) | |
258 | ;; query for password | |
259 | (if (and pop3-password-required (not pop3-password)) | |
260 | (setq pop3-password | |
23f87bed | 261 | (read-passwd (format "Password for %s: " pop3-maildrop)))) |
619ac84f SZ |
262 | (cond ((equal 'apop pop3-authentication-scheme) |
263 | (pop3-apop process pop3-maildrop)) | |
264 | ((equal 'pass pop3-authentication-scheme) | |
265 | (pop3-user process pop3-maildrop) | |
266 | (pop3-pass process)) | |
55535639 | 267 | (t (error "Invalid POP3 authentication scheme"))) |
619ac84f SZ |
268 | (setq message-count (car (pop3-stat process))) |
269 | (pop3-quit process) | |
270 | message-count)) | |
271 | ||
01c52d31 | 272 | (defcustom pop3-stream-type nil |
e4769531 PE |
273 | "*Transport security type for POP3 connections. |
274 | This may be either nil (plain connection), `ssl' (use an | |
01c52d31 MB |
275 | SSL/TSL-secured stream) or `starttls' (use the starttls mechanism |
276 | to turn on TLS security after opening the stream). However, if | |
e4769531 | 277 | this is nil, `ssl' is assumed for connections to port |
01c52d31 | 278 | 995 (pop3s)." |
330f707b | 279 | :version "23.1" ;; No Gnus |
01c52d31 MB |
280 | :group 'pop3 |
281 | :type '(choice (const :tag "Plain" nil) | |
282 | (const :tag "SSL/TLS" ssl) | |
283 | (const starttls))) | |
284 | ||
c6faacb4 KY |
285 | (eval-and-compile |
286 | (if (fboundp 'set-process-query-on-exit-flag) | |
287 | (defalias 'pop3-set-process-query-on-exit-flag | |
288 | 'set-process-query-on-exit-flag) | |
289 | (defalias 'pop3-set-process-query-on-exit-flag | |
290 | 'process-kill-without-query))) | |
291 | ||
eec82323 | 292 | (defun pop3-open-server (mailhost port) |
dd5da9b8 | 293 | "Open TCP connection to MAILHOST on PORT. |
eec82323 | 294 | Returns the process associated with the connection." |
4cac7481 | 295 | (let ((coding-system-for-read 'binary) |
6748645f | 296 | (coding-system-for-write 'binary) |
b87f32fc | 297 | result) |
88ed5ce8 KY |
298 | (with-current-buffer |
299 | (get-buffer-create (concat " trace of POP session to " | |
300 | mailhost)) | |
6748645f LMI |
301 | (erase-buffer) |
302 | (setq pop3-read-point (point-min)) | |
b87f32fc G |
303 | (setq result |
304 | (open-protocol-stream | |
305 | "POP" (current-buffer) mailhost port | |
306 | :type (cond | |
307 | ((or (eq pop3-stream-type 'ssl) | |
308 | (and (not pop3-stream-type) | |
309 | (member port '(995 "pop3s")))) | |
817bcc7c | 310 | 'tls) |
b87f32fc G |
311 | (t |
312 | (or pop3-stream-type 'network))) | |
313 | :capability-command "CAPA\r\n" | |
a5166359 | 314 | :end-of-command "^\\(-ERR\\|+OK\\).*\n" |
d30dd079 | 315 | :end-of-capability "^\\.\r?\n\\|^-ERR" |
b87f32fc G |
316 | :success "^\\+OK.*\n" |
317 | :return-list t | |
318 | :starttls-function | |
319 | (lambda (capabilities) | |
320 | (and (string-match "\\bSTLS\\b" capabilities) | |
321 | "STLS\r\n")))) | |
322 | (when result | |
323 | (let ((response (plist-get (cdr result) :greeting))) | |
324 | (setq pop3-timestamp | |
325 | (substring response (or (string-match "<" response) 0) | |
326 | (+ 1 (or (string-match ">" response) -1))))) | |
327 | (pop3-set-process-query-on-exit-flag (car result) nil) | |
b5244046 | 328 | (erase-buffer) |
b87f32fc | 329 | (car result))))) |
eec82323 LMI |
330 | |
331 | ;; Support functions | |
332 | ||
eec82323 | 333 | (defun pop3-send-command (process command) |
e62e7654 MB |
334 | (set-buffer (process-buffer process)) |
335 | (goto-char (point-max)) | |
336 | ;; (if (= (aref command 0) ?P) | |
337 | ;; (insert "PASS <omitted>\r\n") | |
338 | ;; (insert command "\r\n")) | |
339 | (setq pop3-read-point (point)) | |
340 | (goto-char (point-max)) | |
341 | (process-send-string process (concat command "\r\n"))) | |
eec82323 LMI |
342 | |
343 | (defun pop3-read-response (process &optional return) | |
344 | "Read the response from the server. | |
345 | Return the response string if optional second argument is non-nil." | |
346 | (let ((case-fold-search nil) | |
347 | match-end) | |
88ed5ce8 | 348 | (with-current-buffer (process-buffer process) |
eec82323 | 349 | (goto-char pop3-read-point) |
23f87bed MB |
350 | (while (and (memq (process-status process) '(open run)) |
351 | (not (search-forward "\r\n" nil t))) | |
8903a9c8 | 352 | (pop3-accept-process-output process) |
eec82323 LMI |
353 | (goto-char pop3-read-point)) |
354 | (setq match-end (point)) | |
355 | (goto-char pop3-read-point) | |
356 | (if (looking-at "-ERR") | |
26b4a51d | 357 | (error "%s" (buffer-substring (point) (- match-end 2))) |
eec82323 LMI |
358 | (if (not (looking-at "+OK")) |
359 | (progn (setq pop3-read-point match-end) nil) | |
360 | (setq pop3-read-point match-end) | |
361 | (if return | |
362 | (buffer-substring (point) match-end) | |
363 | t) | |
364 | ))))) | |
365 | ||
eec82323 LMI |
366 | (defun pop3-clean-region (start end) |
367 | (setq end (set-marker (make-marker) end)) | |
368 | (save-excursion | |
369 | (goto-char start) | |
370 | (while (and (< (point) end) (search-forward "\r\n" end t)) | |
371 | (replace-match "\n" t t)) | |
372 | (goto-char start) | |
373 | (while (and (< (point) end) (re-search-forward "^\\." end t)) | |
374 | (replace-match "" t t) | |
375 | (forward-char))) | |
376 | (set-marker end nil)) | |
377 | ||
daaeed87 DL |
378 | ;; Copied from message-make-date. |
379 | (defun pop3-make-date (&optional now) | |
380 | "Make a valid date header. | |
381 | If NOW, use that time instead." | |
382 | (require 'parse-time) | |
383 | (let* ((now (or now (current-time))) | |
384 | (zone (nth 8 (decode-time now))) | |
385 | (sign "+")) | |
386 | (when (< zone 0) | |
387 | (setq sign "-") | |
388 | (setq zone (- zone))) | |
389 | (concat | |
390 | (format-time-string "%d" now) | |
391 | ;; The month name of the %b spec is locale-specific. Pfff. | |
392 | (format " %s " | |
393 | (capitalize (car (rassoc (nth 4 (decode-time now)) | |
394 | parse-time-months)))) | |
395 | (format-time-string "%Y %H:%M:%S " now) | |
396 | ;; We do all of this because XEmacs doesn't have the %z spec. | |
397 | (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60))))) | |
398 | ||
eec82323 LMI |
399 | (defun pop3-munge-message-separator (start end) |
400 | "Check to see if a message separator exists. If not, generate one." | |
401 | (save-excursion | |
402 | (save-restriction | |
403 | (narrow-to-region start end) | |
404 | (goto-char (point-min)) | |
405 | (if (not (or (looking-at "From .?") ; Unix mail | |
406 | (looking-at "\001\001\001\001\n") ; MMDF | |
407 | (looking-at "BABYL OPTIONS:") ; Babyl | |
408 | )) | |
ae496852 SZ |
409 | (let* ((from (mail-strip-quoted-names (mail-fetch-field "From"))) |
410 | (tdate (mail-fetch-field "Date")) | |
411 | (date (split-string (or (and tdate | |
412 | (not (string= "" tdate)) | |
413 | tdate) | |
414 | (pop3-make-date)) | |
415 | " ")) | |
416 | (From_)) | |
eec82323 LMI |
417 | ;; sample date formats I have seen |
418 | ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT) | |
419 | ;; Date: 08 Jul 1996 23:22:24 -0400 | |
420 | ;; should be | |
421 | ;; Tue Jul 9 09:04:21 1996 | |
996aa8c1 MB |
422 | |
423 | ;; Fixme: This should use timezone on the date field contents. | |
eec82323 | 424 | (setq date |
ae496852 | 425 | (cond ((not date) |
23f87bed | 426 | "Tue Jan 1 00:00:0 1900") |
ae496852 | 427 | ((string-match "[A-Z]" (nth 0 date)) |
eec82323 LMI |
428 | (format "%s %s %s %s %s" |
429 | (nth 0 date) (nth 2 date) (nth 1 date) | |
430 | (nth 4 date) (nth 3 date))) | |
431 | (t | |
432 | ;; this really needs to be better but I don't feel | |
433 | ;; like writing a date to day converter. | |
434 | (format "Sun %s %s %s %s" | |
435 | (nth 1 date) (nth 0 date) | |
436 | (nth 3 date) (nth 2 date))) | |
437 | )) | |
438 | (setq From_ (format "\nFrom %s %s\n" from date)) | |
439 | (while (string-match "," From_) | |
440 | (setq From_ (concat (substring From_ 0 (match-beginning 0)) | |
441 | (substring From_ (match-end 0))))) | |
442 | (goto-char (point-min)) | |
dd5da9b8 | 443 | (insert From_) |
daaeed87 DL |
444 | (if (search-forward "\n\n" nil t) |
445 | nil | |
446 | (goto-char (point-max)) | |
447 | (insert "\n")) | |
a2bb410e | 448 | (let ((size (- (point-max) (point)))) |
dd5da9b8 DL |
449 | (forward-line -1) |
450 | (insert (format "Content-Length: %s\n" size))) | |
451 | ))))) | |
eec82323 LMI |
452 | |
453 | ;; The Command Set | |
454 | ||
455 | ;; AUTHORIZATION STATE | |
456 | ||
457 | (defun pop3-user (process user) | |
458 | "Send USER information to POP3 server." | |
459 | (pop3-send-command process (format "USER %s" user)) | |
460 | (let ((response (pop3-read-response process t))) | |
461 | (if (not (and response (string-match "+OK" response))) | |
2c2b732f | 462 | (error "USER %s not valid" user)))) |
eec82323 LMI |
463 | |
464 | (defun pop3-pass (process) | |
465 | "Send authentication information to the server." | |
6748645f LMI |
466 | (pop3-send-command process (format "PASS %s" pop3-password)) |
467 | (let ((response (pop3-read-response process t))) | |
468 | (if (not (and response (string-match "+OK" response))) | |
469 | (pop3-quit process)))) | |
470 | ||
471 | (defun pop3-apop (process user) | |
472 | "Send alternate authentication information to the server." | |
eec82323 LMI |
473 | (let ((pass pop3-password)) |
474 | (if (and pop3-password-required (not pass)) | |
475 | (setq pass | |
23f87bed | 476 | (read-passwd (format "Password for %s: " pop3-maildrop)))) |
eec82323 | 477 | (if pass |
01c52d31 | 478 | (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary))) |
6748645f | 479 | (pop3-send-command process (format "APOP %s %s" user hash)) |
eec82323 LMI |
480 | (let ((response (pop3-read-response process t))) |
481 | (if (not (and response (string-match "+OK" response))) | |
482 | (pop3-quit process))))) | |
483 | )) | |
484 | ||
6748645f LMI |
485 | ;; TRANSACTION STATE |
486 | ||
eec82323 LMI |
487 | (defun pop3-stat (process) |
488 | "Return the number of messages in the maildrop and the maildrop's size." | |
489 | (pop3-send-command process "STAT") | |
490 | (let ((response (pop3-read-response process t))) | |
e9bd5782 MB |
491 | (list (string-to-number (nth 1 (split-string response " "))) |
492 | (string-to-number (nth 2 (split-string response " ")))) | |
eec82323 LMI |
493 | )) |
494 | ||
495 | (defun pop3-list (process &optional msg) | |
ec7995fa KY |
496 | "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs. |
497 | Otherwise, return the size of the message-id MSG" | |
c9fc72fa | 498 | (pop3-send-command process (if msg |
ec7995fa KY |
499 | (format "LIST %d" msg) |
500 | "LIST")) | |
501 | (let ((response (pop3-read-response process t))) | |
502 | (if msg | |
503 | (string-to-number (nth 2 (split-string response " "))) | |
504 | (let ((start pop3-read-point) end) | |
88ed5ce8 | 505 | (with-current-buffer (process-buffer process) |
ec7995fa KY |
506 | (while (not (re-search-forward "^\\.\r\n" nil t)) |
507 | (pop3-accept-process-output process) | |
508 | (goto-char start)) | |
509 | (setq pop3-read-point (point-marker)) | |
510 | (goto-char (match-beginning 0)) | |
511 | (setq end (point-marker)) | |
512 | (mapcar #'(lambda (s) (let ((split (split-string s " "))) | |
513 | (cons (string-to-number (nth 0 split)) | |
514 | (string-to-number (nth 1 split))))) | |
4def29e7 | 515 | (split-string (buffer-substring start end) "\r\n" t))))))) |
eec82323 LMI |
516 | |
517 | (defun pop3-retr (process msg crashbuf) | |
518 | "Retrieve message-id MSG to buffer CRASHBUF." | |
519 | (pop3-send-command process (format "RETR %s" msg)) | |
520 | (pop3-read-response process) | |
521 | (let ((start pop3-read-point) end) | |
88ed5ce8 | 522 | (with-current-buffer (process-buffer process) |
eec82323 | 523 | (while (not (re-search-forward "^\\.\r\n" nil t)) |
b5244046 LMI |
524 | (unless (memq (process-status process) '(open run)) |
525 | (error "pop3 server closed the connection")) | |
8903a9c8 | 526 | (pop3-accept-process-output process) |
eec82323 LMI |
527 | (goto-char start)) |
528 | (setq pop3-read-point (point-marker)) | |
e62e7654 MB |
529 | ;; this code does not seem to work for some POP servers... |
530 | ;; and I cannot figure out why not. | |
531 | ;; (goto-char (match-beginning 0)) | |
532 | ;; (backward-char 2) | |
533 | ;; (if (not (looking-at "\r\n")) | |
534 | ;; (insert "\r\n")) | |
535 | ;; (re-search-forward "\\.\r\n") | |
eec82323 LMI |
536 | (goto-char (match-beginning 0)) |
537 | (setq end (point-marker)) | |
538 | (pop3-clean-region start end) | |
539 | (pop3-munge-message-separator start end) | |
88ed5ce8 | 540 | (with-current-buffer crashbuf |
eec82323 LMI |
541 | (erase-buffer)) |
542 | (copy-to-buffer crashbuf start end) | |
543 | (delete-region start end) | |
544 | ))) | |
545 | ||
546 | (defun pop3-dele (process msg) | |
547 | "Mark message-id MSG as deleted." | |
548 | (pop3-send-command process (format "DELE %s" msg)) | |
549 | (pop3-read-response process)) | |
550 | ||
551 | (defun pop3-noop (process msg) | |
552 | "No-operation." | |
553 | (pop3-send-command process "NOOP") | |
554 | (pop3-read-response process)) | |
555 | ||
556 | (defun pop3-last (process) | |
557 | "Return highest accessed message-id number for the session." | |
558 | (pop3-send-command process "LAST") | |
559 | (let ((response (pop3-read-response process t))) | |
e9bd5782 | 560 | (string-to-number (nth 1 (split-string response " "))) |
eec82323 LMI |
561 | )) |
562 | ||
563 | (defun pop3-rset (process) | |
564 | "Remove all delete marks from current maildrop." | |
565 | (pop3-send-command process "RSET") | |
566 | (pop3-read-response process)) | |
567 | ||
568 | ;; UPDATE | |
569 | ||
570 | (defun pop3-quit (process) | |
571 | "Close connection to POP3 server. | |
572 | Tell server to remove all messages marked as deleted, unlock the maildrop, | |
573 | and close the connection." | |
574 | (pop3-send-command process "QUIT") | |
575 | (pop3-read-response process t) | |
576 | (if process | |
88ed5ce8 | 577 | (with-current-buffer (process-buffer process) |
eec82323 LMI |
578 | (goto-char (point-max)) |
579 | (delete-process process)))) | |
580 | \f | |
581 | ;; Summary of POP3 (Post Office Protocol version 3) commands and responses | |
582 | ||
583 | ;;; AUTHORIZATION STATE | |
584 | ||
585 | ;; Initial TCP connection | |
586 | ;; Arguments: none | |
587 | ;; Restrictions: none | |
588 | ;; Possible responses: | |
589 | ;; +OK [POP3 server ready] | |
590 | ||
591 | ;; USER name | |
592 | ;; Arguments: a server specific user-id (required) | |
593 | ;; Restrictions: authorization state [after unsuccessful USER or PASS | |
594 | ;; Possible responses: | |
595 | ;; +OK [valid user-id] | |
596 | ;; -ERR [invalid user-id] | |
597 | ||
598 | ;; PASS string | |
599 | ;; Arguments: a server/user-id specific password (required) | |
600 | ;; Restrictions: authorization state, after successful USER | |
601 | ;; Possible responses: | |
602 | ;; +OK [maildrop locked and ready] | |
603 | ;; -ERR [invalid password] | |
604 | ;; -ERR [unable to lock maildrop] | |
605 | ||
01c52d31 MB |
606 | ;; STLS (RFC 2595) |
607 | ;; Arguments: none | |
608 | ;; Restrictions: Only permitted in AUTHORIZATION state. | |
609 | ;; Possible responses: | |
610 | ;; +OK | |
611 | ;; -ERR | |
612 | ||
eec82323 LMI |
613 | ;;; TRANSACTION STATE |
614 | ||
615 | ;; STAT | |
616 | ;; Arguments: none | |
617 | ;; Restrictions: transaction state | |
618 | ;; Possible responses: | |
619 | ;; +OK nn mm [# of messages, size of maildrop] | |
620 | ||
621 | ;; LIST [msg] | |
622 | ;; Arguments: a message-id (optional) | |
623 | ;; Restrictions: transaction state; msg must not be deleted | |
624 | ;; Possible responses: | |
625 | ;; +OK [scan listing follows] | |
626 | ;; -ERR [no such message] | |
627 | ||
628 | ;; RETR msg | |
629 | ;; Arguments: a message-id (required) | |
630 | ;; Restrictions: transaction state; msg must not be deleted | |
631 | ;; Possible responses: | |
632 | ;; +OK [message contents follow] | |
633 | ;; -ERR [no such message] | |
634 | ||
635 | ;; DELE msg | |
636 | ;; Arguments: a message-id (required) | |
637 | ;; Restrictions: transaction state; msg must not be deleted | |
638 | ;; Possible responses: | |
639 | ;; +OK [message deleted] | |
640 | ;; -ERR [no such message] | |
641 | ||
642 | ;; NOOP | |
643 | ;; Arguments: none | |
644 | ;; Restrictions: transaction state | |
645 | ;; Possible responses: | |
646 | ;; +OK | |
647 | ||
648 | ;; LAST | |
649 | ;; Arguments: none | |
650 | ;; Restrictions: transaction state | |
651 | ;; Possible responses: | |
652 | ;; +OK nn [highest numbered message accessed] | |
653 | ||
654 | ;; RSET | |
655 | ;; Arguments: none | |
656 | ;; Restrictions: transaction state | |
657 | ;; Possible responses: | |
658 | ;; +OK [all delete marks removed] | |
659 | ||
660 | ;;; UPDATE STATE | |
661 | ||
662 | ;; QUIT | |
663 | ;; Arguments: none | |
664 | ;; Restrictions: none | |
665 | ;; Possible responses: | |
666 | ;; +OK [TCP connection closed] | |
dd5da9b8 DL |
667 | |
668 | (provide 'pop3) | |
669 | ||
670 | ;;; pop3.el ends here |