0
|
1 ;;; Simple POP (RFC 1460) client for VM
|
|
2 ;;; Copyright (C) 1993, 1994 Kyle E. Jones
|
|
3 ;;;
|
|
4 ;;; This program is free software; you can redistribute it and/or modify
|
|
5 ;;; it under the terms of the GNU General Public License as published by
|
|
6 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
7 ;;; any later version.
|
|
8 ;;;
|
|
9 ;;; This program is distributed in the hope that it will be useful,
|
|
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 ;;; GNU General Public License for more details.
|
|
13 ;;;
|
|
14 ;;; You should have received a copy of the GNU General Public License
|
|
15 ;;; along with this program; if not, write to the Free Software
|
|
16 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
|
18 (provide 'vm-pop)
|
|
19
|
|
20 ;; Nothing fancy here.
|
|
21 ;; Our goal is to drag the mail from the POP maildrop to the crash box.
|
|
22 ;; just as if we were using movemail on a spool file.
|
|
23 (defun vm-pop-move-mail (source destination)
|
|
24 (let ((process nil)
|
|
25 (folder-type vm-folder-type)
|
|
26 (saved-password t)
|
24
|
27 (m-per-session vm-pop-messages-per-session)
|
|
28 (b-per-session vm-pop-bytes-per-session)
|
0
|
29 (handler (and (fboundp 'find-file-name-handler)
|
|
30 (condition-case ()
|
|
31 (find-file-name-handler source 'vm-pop-move-mail)
|
|
32 (wrong-number-of-arguments
|
|
33 (find-file-name-handler source)))))
|
|
34 (popdrop (vm-safe-popdrop-string source))
|
24
|
35 mailbox-count mailbox-size message-size response
|
|
36 n retrieved retrieved-bytes process-buffer)
|
0
|
37 (unwind-protect
|
|
38 (catch 'done
|
|
39 (if handler
|
|
40 (throw 'done
|
|
41 (funcall handler 'vm-pop-move-mail source destination)))
|
24
|
42 (setq process (vm-pop-make-session source))
|
|
43 (or process (throw 'done nil))
|
|
44 (setq process-buffer (process-buffer process))
|
|
45 (save-excursion
|
|
46 (set-buffer process-buffer)
|
|
47 (setq vm-folder-type (or folder-type vm-default-folder-type))
|
|
48 ;; find out how many messages are in the box.
|
|
49 (vm-pop-send-command process "STAT")
|
|
50 (setq response (vm-pop-read-stat-response process)
|
|
51 mailbox-count (nth 0 response)
|
|
52 mailbox-size (nth 1 response))
|
|
53 ;; forget it if the command fails
|
|
54 ;; or if there are no messages present.
|
|
55 (if (or (null mailbox-count)
|
|
56 (< mailbox-count 1))
|
|
57 (throw 'done nil))
|
|
58 ;; loop through the maildrop retrieving and deleting
|
|
59 ;; messages as we go.
|
|
60 (setq n 1 retrieved 0 retrieved-bytes 0)
|
|
61 (while (and (<= n mailbox-count)
|
|
62 (or (not (natnump m-per-session))
|
|
63 (< retrieved m-per-session))
|
|
64 (or (not (natnump b-per-session))
|
|
65 (< retrieved-bytes b-per-session)))
|
|
66 (if (or vm-pop-max-message-size
|
|
67 b-per-session)
|
|
68 (progn
|
|
69 (vm-pop-send-command process (format "LIST %d" n))
|
|
70 (setq message-size
|
|
71 (vm-pop-read-list-response process))))
|
|
72 (if (and (integerp vm-pop-max-message-size)
|
|
73 (> message-size vm-pop-max-message-size)
|
|
74 (progn
|
|
75 (setq response
|
|
76 (if vm-pop-ok-to-ask
|
|
77 (vm-pop-ask-about-large-message process
|
|
78 message-size
|
|
79 n)
|
|
80 'skip))
|
|
81 (not (eq response 'retrieve))))
|
|
82 (if (eq response 'delete)
|
|
83 (progn
|
|
84 (message "Deleting message %d..." n)
|
|
85 (vm-pop-send-command process (format "DELE %d" n))
|
|
86 (and (null (vm-pop-read-response process))
|
|
87 (throw 'done (not (equal retrieved 0)))))
|
|
88 (if vm-pop-ok-to-ask
|
|
89 (message "Skipping message %d..." n)
|
|
90 (message "Skipping message %d in %s, too large (%d > %d)..."
|
|
91 n popdrop message-size vm-pop-max-message-size)))
|
|
92 (message "Retrieving message %d (of %d) from %s..."
|
|
93 n mailbox-count popdrop)
|
|
94 (vm-pop-send-command process (format "RETR %d" n))
|
|
95 (and (null (vm-pop-read-response process))
|
|
96 (throw 'done (not (equal retrieved 0))))
|
|
97 (and (null (vm-pop-retrieve-to-crashbox process destination))
|
|
98 (throw 'done (not (equal retrieved 0))))
|
|
99 (vm-increment retrieved)
|
|
100 (and b-per-session
|
|
101 (setq retrieved-bytes (+ retrieved-bytes message-size)))
|
|
102 (vm-pop-send-command process (format "DELE %d" n))
|
|
103 ;; DELE can't fail but Emacs or this code might
|
|
104 ;; blow a gasket and spew filth down the
|
|
105 ;; connection, so...
|
|
106 (and (null (vm-pop-read-response process))
|
|
107 (throw 'done (not (equal retrieved 0)))))
|
|
108 (vm-increment n))
|
|
109 (not (equal retrieved 0)) ))
|
|
110 (if process
|
|
111 (vm-pop-end-session process)))))
|
|
112
|
|
113 (defun vm-pop-check-mail (source)
|
|
114 (let ((process nil)
|
|
115 (handler (and (fboundp 'find-file-name-handler)
|
|
116 (condition-case ()
|
|
117 (find-file-name-handler source 'vm-pop-check-mail)
|
|
118 (wrong-number-of-arguments
|
|
119 (find-file-name-handler source)))))
|
|
120 response)
|
|
121 (unwind-protect
|
|
122 (save-excursion
|
|
123 (catch 'done
|
|
124 (if handler
|
|
125 (throw 'done
|
|
126 (funcall handler 'vm-pop-check-mail source)))
|
|
127 (setq process (vm-pop-make-session source))
|
|
128 (or process (throw 'done nil))
|
|
129 (set-buffer (process-buffer process))
|
|
130 (vm-pop-send-command process "STAT")
|
|
131 (setq response (vm-pop-read-stat-response process))
|
|
132 (if (null response)
|
|
133 nil
|
|
134 (not (equal 0 (car response))))))
|
|
135 (and process (vm-pop-end-session process)))))
|
|
136
|
|
137 (defun vm-pop-make-session (source)
|
|
138 (let ((process-to-shutdown nil)
|
|
139 process
|
|
140 (saved-password t)
|
|
141 (popdrop (vm-safe-popdrop-string source))
|
|
142 greeting timestamp
|
|
143 host port auth user pass source-list process-buffer)
|
|
144 (unwind-protect
|
|
145 (catch 'done
|
0
|
146 ;; parse the maildrop
|
|
147 (setq source-list (vm-parse source "\\([^:]+\\):?")
|
|
148 host (nth 0 source-list)
|
|
149 port (nth 1 source-list)
|
|
150 auth (nth 2 source-list)
|
|
151 user (nth 3 source-list)
|
|
152 pass (nth 4 source-list))
|
|
153 ;; carp if parts are missing
|
|
154 (if (null host)
|
|
155 (error "No host in POP maildrop specification, \"%s\""
|
|
156 source))
|
|
157 (if (null port)
|
|
158 (error "No port in POP maildrop specification, \"%s\""
|
|
159 source))
|
|
160 (if (string-match "^[0-9]+$" port)
|
|
161 (setq port (string-to-int port)))
|
|
162 (if (null auth)
|
|
163 (error
|
|
164 "No authentication method in POP maildrop specification, \"%s\""
|
|
165 source))
|
|
166 (if (null user)
|
|
167 (error "No user in POP maildrop specification, \"%s\""
|
|
168 source))
|
|
169 (if (null pass)
|
|
170 (error "No password in POP maildrop specification, \"%s\""
|
|
171 source))
|
|
172 (if (equal pass "*")
|
|
173 (progn
|
|
174 (setq pass (car (cdr (assoc source vm-pop-passwords))))
|
|
175 (if (null pass)
|
24
|
176 (if (null vm-pop-ok-to-ask)
|
|
177 (progn (message "Need password for %s" popdrop)
|
|
178 (throw 'done nil))
|
|
179 (setq pass
|
|
180 (vm-read-password
|
|
181 (format "POP password for %s: "
|
|
182 popdrop))
|
|
183 vm-pop-passwords (cons (list source pass)
|
|
184 vm-pop-passwords)
|
|
185 saved-password t)))))
|
0
|
186 ;; get the trace buffer
|
|
187 (setq process-buffer
|
|
188 (get-buffer-create (format "trace of POP session to %s" host)))
|
24
|
189 ;; Tell XEmacs/MULE not to mess with the text.
|
26
|
190 (and (vm-xemacs-mule-p)
|
24
|
191 (set-file-coding-system 'binary t))
|
0
|
192 ;; clear the trace buffer of old output
|
|
193 (save-excursion
|
|
194 (set-buffer process-buffer)
|
|
195 (erase-buffer))
|
|
196 ;; open the connection to the server
|
|
197 (setq process (open-network-stream "POP" process-buffer host port))
|
|
198 (and (null process) (throw 'done nil))
|
24
|
199 (process-kill-without-query process)
|
0
|
200 (save-excursion
|
|
201 (set-buffer process-buffer)
|
|
202 (make-local-variable 'vm-pop-read-point)
|
24
|
203 (setq vm-pop-read-point (point-min))
|
|
204 (if (null (setq greeting (vm-pop-read-response process t)))
|
|
205 (progn (delete-process process)
|
|
206 (throw 'done nil)))
|
|
207 (setq process-to-shutdown process)
|
0
|
208 ;; authentication
|
|
209 (cond ((equal auth "pass")
|
|
210 (vm-pop-send-command process (format "USER %s" user))
|
|
211 (and (null (vm-pop-read-response process))
|
|
212 (throw 'done nil))
|
|
213 (vm-pop-send-command process (format "PASS %s" pass))
|
|
214 (if (null (vm-pop-read-response process))
|
|
215 (progn
|
|
216 (if saved-password
|
|
217 (setq vm-pop-passwords
|
|
218 (delete (list source pass)
|
|
219 vm-pop-passwords)))
|
|
220 (throw 'done nil))))
|
|
221 ((equal auth "rpop")
|
|
222 (vm-pop-send-command process (format "USER %s" user))
|
|
223 (and (null (vm-pop-read-response process))
|
|
224 (throw 'done nil))
|
|
225 (vm-pop-send-command process (format "RPOP %s" pass))
|
|
226 (and (null (vm-pop-read-response process))
|
|
227 (throw 'done nil)))
|
|
228 ((equal auth "apop")
|
|
229 (setq timestamp (vm-parse greeting "[^<]+\\(<[^>]+>\\)")
|
|
230 timestamp (car timestamp))
|
|
231 (if (null timestamp)
|
|
232 (progn
|
|
233 (goto-char (point-max))
|
24
|
234 (insert-before-markers "<<< ooops, no timestamp found in greeting! >>>\n")
|
0
|
235 (throw 'done nil)))
|
|
236 (vm-pop-send-command
|
|
237 process
|
|
238 (format "APOP %s %s"
|
|
239 user
|
|
240 (vm-pop-md5 (concat timestamp pass))))
|
|
241 (and (null (vm-pop-read-response process))
|
|
242 (throw 'done nil)))
|
|
243 (t (error "Don't know how to authenticate with %s" auth)))
|
24
|
244 (setq process-to-shutdown nil)
|
|
245 process ))
|
|
246 (if process-to-shutdown
|
|
247 (vm-pop-end-session process-to-shutdown)))))
|
0
|
248
|
24
|
249 (defun vm-pop-end-session (process)
|
0
|
250 (save-excursion
|
|
251 (set-buffer (process-buffer process))
|
24
|
252 (vm-pop-send-command process "QUIT")
|
|
253 (vm-pop-read-response process)
|
|
254 (if (fboundp 'add-async-timeout)
|
|
255 (add-async-timeout 2 'delete-process process)
|
|
256 (run-at-time 2 nil 'delete-process process))))
|
0
|
257
|
|
258 (defun vm-pop-send-command (process command)
|
|
259 (goto-char (point-max))
|
|
260 (if (= (aref command 0) ?P)
|
24
|
261 (insert-before-markers "PASS <omitted>\r\n")
|
|
262 (insert-before-markers command "\r\n"))
|
0
|
263 (setq vm-pop-read-point (point))
|
|
264 (process-send-string process command)
|
|
265 (process-send-string process "\r\n"))
|
|
266
|
|
267 (defun vm-pop-read-response (process &optional return-response-string)
|
|
268 (let ((case-fold-search nil)
|
|
269 match-end)
|
|
270 (goto-char vm-pop-read-point)
|
|
271 (while (not (search-forward "\r\n" nil t))
|
|
272 (accept-process-output process)
|
|
273 (goto-char vm-pop-read-point))
|
|
274 (setq match-end (point))
|
|
275 (goto-char vm-pop-read-point)
|
|
276 (if (not (looking-at "+OK"))
|
|
277 (progn (setq vm-pop-read-point match-end) nil)
|
|
278 (setq vm-pop-read-point match-end)
|
|
279 (if return-response-string
|
|
280 (buffer-substring (point) match-end)
|
|
281 t ))))
|
|
282
|
24
|
283 (defun vm-pop-read-past-dot-sentinel-line (process)
|
|
284 (let ((case-fold-search nil))
|
|
285 (goto-char vm-pop-read-point)
|
|
286 (while (not (search-forward "^.\r\n" nil 0))
|
|
287 (beginning-of-line)
|
|
288 ;; save-excursion doesn't work right
|
|
289 (let ((opoint (point)))
|
|
290 (accept-process-output process)
|
|
291 (goto-char opoint)))
|
|
292 (setq vm-pop-read-point (point))))
|
|
293
|
0
|
294 (defun vm-pop-read-stat-response (process)
|
24
|
295 (let ((response (vm-pop-read-response process t))
|
|
296 list)
|
|
297 (setq list (vm-parse response "\\([^ ]+\\) *"))
|
|
298 (list (string-to-int (nth 1 list)) (string-to-int (nth 2 list)))))
|
|
299
|
|
300 (defun vm-pop-read-list-response (process)
|
0
|
301 (let ((response (vm-pop-read-response process t)))
|
24
|
302 (string-to-int (nth 2 (vm-parse response "\\([^ ]+\\) *")))))
|
|
303
|
|
304 (defun vm-pop-ask-about-large-message (process size n)
|
|
305 (let ((work-buffer nil)
|
|
306 (pop-buffer (current-buffer))
|
|
307 start end)
|
|
308 (unwind-protect
|
|
309 (save-excursion
|
|
310 (save-window-excursion
|
|
311 (vm-pop-send-command process (format "TOP %d %d" n 0))
|
|
312 (if (vm-pop-read-response process)
|
|
313 (progn
|
|
314 (setq start vm-pop-read-point)
|
|
315 (vm-pop-read-past-dot-sentinel-line process)
|
|
316 (setq end vm-pop-read-point)
|
|
317 (setq work-buffer (generate-new-buffer "*pop-glop*"))
|
|
318 (set-buffer work-buffer)
|
|
319 (insert-buffer-substring pop-buffer start end)
|
|
320 (forward-line -1)
|
|
321 (delete-region (point) (point-max))
|
|
322 (vm-pop-cleanup-region (point-min) (point-max))
|
|
323 (vm-display-buffer work-buffer)
|
|
324 (setq minibuffer-scroll-window (selected-window))
|
|
325 (goto-char (point-min))
|
|
326 (if (re-search-forward "^Received:" nil t)
|
|
327 (progn
|
|
328 (goto-char (match-beginning 0))
|
|
329 (vm-reorder-message-headers
|
|
330 nil vm-visible-headers
|
|
331 vm-invisible-header-regexp)))
|
|
332 (set-window-point (selected-window) (point))))
|
|
333 (if (y-or-n-p (format "Message %d, size = %d, retrieve? " n size))
|
|
334 'retrieve
|
|
335 (if (y-or-n-p (format "Delete message %d from popdrop? " n size))
|
|
336 'delete
|
|
337 'skip))))
|
|
338 (and work-buffer (kill-buffer work-buffer)))))
|
0
|
339
|
|
340 (defun vm-pop-retrieve-to-crashbox (process crash)
|
|
341 (let ((start vm-pop-read-point) end)
|
|
342 (goto-char start)
|
24
|
343 (while (not (re-search-forward "^\\.\r\n" nil 0))
|
|
344 (beginning-of-line)
|
|
345 ;; save-excursion doesn't work right
|
|
346 (let ((opoint (point)))
|
|
347 (accept-process-output process)
|
|
348 (goto-char opoint)))
|
0
|
349 (setq vm-pop-read-point (point-marker))
|
|
350 (goto-char (match-beginning 0))
|
|
351 (setq end (point-marker))
|
|
352 (vm-pop-cleanup-region start end)
|
|
353 ;; Some POP servers strip leading and trailing message
|
|
354 ;; separators, some don't. Figure out what kind we're
|
|
355 ;; talking to and do the right thing.
|
|
356 (if (eq (vm-get-folder-type nil start end) 'unknown)
|
|
357 (progn
|
|
358 (vm-munge-message-separators vm-folder-type start end)
|
|
359 (goto-char start)
|
|
360 ;; avoid the consing and stat() call for all but babyl
|
|
361 ;; files, since this will probably slow things down.
|
|
362 ;; only babyl files have the folder header, and we
|
|
363 ;; should only insert it if the crash box is empty.
|
|
364 (if (and (eq vm-folder-type 'babyl)
|
|
365 (let ((attrs (file-attributes crash)))
|
|
366 (or (null attrs) (equal 0 (nth 7 attrs)))))
|
|
367 (let ((opoint (point)))
|
|
368 (vm-convert-folder-header nil vm-folder-type)
|
|
369 ;; if start is a marker, then it was moved
|
|
370 ;; forward by the insertion. restore it.
|
|
371 (setq start opoint)
|
|
372 (goto-char start)
|
|
373 (vm-skip-past-folder-header)))
|
|
374 (insert (vm-leading-message-separator))
|
|
375 ;; this will not find the trailing message separator but
|
|
376 ;; for the Content-Length stuff counting from eob is
|
|
377 ;; the same thing in this case.
|
|
378 (vm-convert-folder-type-headers nil vm-folder-type)
|
|
379 (goto-char end)
|
|
380 (insert-before-markers (vm-trailing-message-separator))))
|
20
|
381 ;; Set file type to binary for DOS/Windows. I don't know if
|
|
382 ;; this is correct to do or not; it depends on whether the
|
|
383 ;; the CRLF or the LF newline convention is used on the inbox
|
|
384 ;; associated with this crashbox. This setting assumes the LF
|
|
385 ;; newline convention is used.
|
|
386 (let ((buffer-file-type t))
|
|
387 (write-region start end crash t 0))
|
0
|
388 (delete-region start end)
|
|
389 t ))
|
|
390
|
|
391 (defun vm-pop-cleanup-region (start end)
|
|
392 (setq end (vm-marker end))
|
|
393 (save-excursion
|
|
394 (goto-char start)
|
|
395 ;; CRLF -> LF
|
|
396 (while (and (< (point) end) (search-forward "\r\n" end t))
|
|
397 (replace-match "\n" t t))
|
|
398 (goto-char start)
|
|
399 ;; chop leading dots
|
|
400 (while (and (< (point) end) (re-search-forward "^\\." end t))
|
|
401 (replace-match "" t t)
|
|
402 (forward-char)))
|
|
403 (set-marker end nil))
|
|
404
|
|
405 (defun vm-pop-md5 (string)
|
|
406 (let ((buffer nil))
|
|
407 (unwind-protect
|
|
408 (save-excursion
|
|
409 (setq buffer (generate-new-buffer "*vm-work*"))
|
|
410 (set-buffer buffer)
|
|
411 (insert string)
|
|
412 (call-process-region (point-min) (point-max)
|
|
413 "/bin/sh" t buffer nil
|
20
|
414 shell-command-switch vm-pop-md5-program)
|
0
|
415 ;; MD5 digest is 32 chars long
|
|
416 ;; mddriver adds a newline to make neaten output for tty
|
|
417 ;; viewing, make sure we leave it behind.
|
|
418 (vm-buffer-substring-no-properties (point-min) (+ (point-min) 32)))
|
|
419 (and buffer (kill-buffer buffer)))))
|