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