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