Mercurial > emacs
annotate lisp/gnus/imap.el @ 33127:eca95f9d7f05
(base64): Require unconditionally.
(message-posting-charset): Defvar when compiling.
(rfc2047-encode-message-header, rfc2047-encodable-p): Require
message.
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 01 Nov 2000 19:07:36 +0000 |
parents | 3720ccaca161 |
children | be95f43e08db |
rev | line source |
---|---|
31717 | 1 ;;; imap.el --- imap library |
2 ;; Copyright (C) 1998, 1999, 2000 | |
3 ;; Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Simon Josefsson <jas@pdc.kth.se> | |
6 ;; Keywords: mail | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; imap.el is a elisp library providing an interface for talking to | |
28 ;; IMAP servers. | |
29 ;; | |
30 ;; imap.el is roughly divided in two parts, one that parses IMAP | |
31 ;; responses from the server and storing data into buffer-local | |
32 ;; variables, and one for utility functions which send commands to | |
33 ;; server, waits for an answer, and return information. The latter | |
34 ;; part is layered on top of the previous. | |
35 ;; | |
36 ;; The imap.el API consist of the following functions, other functions | |
37 ;; in this file should not be called directly and the result of doing | |
38 ;; so are at best undefined. | |
39 ;; | |
40 ;; Global commands: | |
41 ;; | |
42 ;; imap-open, imap-opened, imap-authenticate, imap-close, | |
43 ;; imap-capability, imap-namespace, imap-error-text | |
44 ;; | |
45 ;; Mailbox commands: | |
46 ;; | |
47 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox, | |
48 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select, | |
49 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge | |
50 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete | |
51 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list | |
52 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status | |
53 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete | |
54 ;; | |
55 ;; Message commands: | |
56 ;; | |
57 ;; imap-fetch-asynch, imap-fetch, | |
58 ;; imap-current-message, imap-list-to-message-set, | |
59 ;; imap-message-get, imap-message-map | |
60 ;; imap-message-envelope-date, imap-message-envelope-subject, | |
61 ;; imap-message-envelope-from, imap-message-envelope-sender, | |
62 ;; imap-message-envelope-reply-to, imap-message-envelope-to, | |
63 ;; imap-message-envelope-cc, imap-message-envelope-bcc | |
64 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id | |
65 ;; imap-message-body, imap-message-flag-permanent-p | |
66 ;; imap-message-flags-set, imap-message-flags-del | |
67 ;; imap-message-flags-add, imap-message-copyuid | |
68 ;; imap-message-copy, imap-message-appenduid | |
69 ;; imap-message-append, imap-envelope-from | |
70 ;; imap-body-lines | |
71 ;; | |
72 ;; It is my hope that theese commands should be pretty self | |
73 ;; explanatory for someone that know IMAP. All functions have | |
74 ;; additional documentation on how to invoke them. | |
75 ;; | |
76 ;; imap.el support RFC1730/2060 (IMAP4/IMAP4rev1), implemented IMAP | |
77 ;; extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342 | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
78 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS, |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
79 ;; LOGINDISABLED) (with use of external library starttls.el and |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
80 ;; program starttls) and the GSSAPI / kerberos V4 sections of RFC1731 |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
81 ;; (with use of external program `imtest'). It also take advantage |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
82 ;; the UNSELECT extension in Cyrus IMAPD. |
31717 | 83 ;; |
84 ;; Without the work of John McClary Prevost and Jim Radford this library | |
85 ;; would not have seen the light of day. Many thanks. | |
86 ;; | |
87 ;; This is a transcript of short interactive session for demonstration | |
88 ;; purposes. | |
89 ;; | |
90 ;; (imap-open "my.mail.server") | |
91 ;; => " *imap* my.mail.server:0" | |
92 ;; | |
93 ;; The rest are invoked with current buffer as the buffer returned by | |
94 ;; `imap-open'. It is possible to do all without this, but it would | |
95 ;; look ugly here since `buffer' is always the last argument for all | |
96 ;; imap.el API functions. | |
97 ;; | |
98 ;; (imap-authenticate "myusername" "mypassword") | |
99 ;; => auth | |
100 ;; | |
101 ;; (imap-mailbox-lsub "*") | |
102 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam") | |
103 ;; | |
104 ;; (imap-mailbox-list "INBOX.n%") | |
105 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq") | |
106 ;; | |
107 ;; (imap-mailbox-select "INBOX.nnimap") | |
108 ;; => "INBOX.nnimap" | |
109 ;; | |
110 ;; (imap-mailbox-get 'exists) | |
111 ;; => 166 | |
112 ;; | |
113 ;; (imap-mailbox-get 'uidvalidity) | |
114 ;; => "908992622" | |
115 ;; | |
116 ;; (imap-search "FLAGGED SINCE 18-DEC-98") | |
117 ;; => (235 236) | |
118 ;; | |
119 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822) | |
120 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...." | |
121 ;; | |
122 ;; Todo: | |
123 ;; | |
124 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow. | |
125 ;; o Don't use `read' at all (important places already fixed) | |
126 ;; o Accept list of articles instead of message set string in most | |
127 ;; imap-message-* functions. | |
128 ;; | |
129 ;; Revision history: | |
130 ;; | |
131 ;; - 19991218 added starttls/digest-md5 patch, | |
132 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp> | |
133 ;; NB! you need SLIM for starttls.el and digest-md5.el | |
134 ;; - 19991023 commited to pgnus | |
135 ;; | |
136 | |
137 ;;; Code: | |
138 | |
139 (eval-when-compile (require 'cl)) | |
140 (eval-and-compile | |
141 (autoload 'open-ssl-stream "ssl") | |
142 (autoload 'base64-decode-string "base64") | |
143 (autoload 'base64-encode-string "base64") | |
144 (autoload 'starttls-open-stream "starttls") | |
145 (autoload 'starttls-negotiate "starttls") | |
146 (autoload 'digest-md5-parse-digest-challenge "digest-md5") | |
147 (autoload 'digest-md5-digest-response "digest-md5") | |
148 (autoload 'digest-md5-digest-uri "digest-md5") | |
149 (autoload 'digest-md5-challenge "digest-md5") | |
150 (autoload 'rfc2104-hash "rfc2104") | |
151 (autoload 'md5 "md5") | |
152 (autoload 'utf7-encode "utf7") | |
153 (autoload 'utf7-decode "utf7") | |
154 (autoload 'format-spec "format-spec") | |
155 (autoload 'format-spec-make "format-spec")) | |
156 | |
157 ;; User variables. | |
158 | |
159 (defgroup imap nil | |
160 "Low-level IMAP issues." | |
161 :version "21.1" | |
162 :group 'mail) | |
163 | |
164 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s" | |
165 "imtest -kp %s %p") | |
166 "List of strings containing commands for Kerberos 4 authentication. | |
167 %s is replaced with server hostname, %p with port to connect to, and | |
168 %l with the value of `imap-default-user'. The program should accept | |
169 IMAP commands on stdin and return responses to stdout. Each entry in | |
170 the list is tried until a successful connection is made." | |
171 :group 'imap | |
172 :type '(repeat string)) | |
173 | |
174 (defcustom imap-gssapi-program '("imtest -m gssapi -u %l -p %p %s") | |
175 "List of strings containing commands for GSSAPI (krb5) authentication. | |
176 %s is replaced with server hostname, %p with port to connect to, and | |
177 %l with the value of `imap-default-user'. The program should accept | |
178 IMAP commands on stdin and return responses to stdout. Each entry in | |
179 the list is tried until a successful connection is made." | |
180 :group 'imap | |
181 :type '(repeat string)) | |
182 | |
183 (defcustom imap-ssl-program '("openssl s_client -ssl3 -connect %s:%p" | |
184 "openssl s_client -ssl2 -connect %s:%p" | |
185 "s_client -ssl3 -connect %s:%p" | |
186 "s_client -ssl2 -connect %s:%p") | |
187 "A string, or list of strings, containing commands for SSL connections. | |
188 Within a string, %s is replaced with the server address and %p with | |
189 port number on server. The program should accept IMAP commands on | |
190 stdin and return responses to stdout. Each entry in the list is tried | |
191 until a successful connection is made." | |
192 :group 'imap | |
193 :type '(choice string | |
194 (repeat string))) | |
195 | |
196 (defcustom imap-shell-program '("ssh %s imapd" | |
197 "rsh %s imapd" | |
198 "ssh %g ssh %s imapd" | |
199 "rsh %g rsh %s imapd") | |
200 "A list of strings, containing commands for IMAP connection. | |
201 Within a string, %s is replaced with the server address, %p with port | |
202 number on server, %g with `imap-shell-host', and %l with | |
203 `imap-default-user'. The program should read IMAP commands from stdin | |
204 and write IMAP response to stdout. Each entry in the list is tried | |
205 until a successful connection is made." | |
206 :group 'imap | |
207 :type '(repeat string)) | |
208 | |
209 (defvar imap-shell-host "gateway" | |
210 "Hostname of rlogin proxy.") | |
211 | |
212 (defvar imap-default-user (user-login-name) | |
213 "Default username to use.") | |
214 | |
215 (defvar imap-error nil | |
216 "Error codes from the last command.") | |
217 | |
218 ;; Various variables. | |
219 | |
220 (defvar imap-fetch-data-hook nil | |
221 "Hooks called after receiving each FETCH response.") | |
222 | |
223 (defvar imap-streams '(gssapi kerberos4 starttls ssl network shell) | |
224 "Priority of streams to consider when opening connection to server.") | |
225 | |
226 (defvar imap-stream-alist | |
227 '((gssapi imap-gssapi-stream-p imap-gssapi-open) | |
228 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open) | |
229 (ssl imap-ssl-p imap-ssl-open) | |
230 (network imap-network-p imap-network-open) | |
231 (shell imap-shell-p imap-shell-open) | |
232 (starttls imap-starttls-p imap-starttls-open)) | |
233 "Definition of network streams. | |
234 | |
235 (NAME CHECK OPEN) | |
236 | |
237 NAME names the stream, CHECK is a function returning non-nil if the | |
238 server support the stream and OPEN is a function for opening the | |
239 stream.") | |
240 | |
241 (defvar imap-authenticators '(gssapi | |
242 kerberos4 | |
243 digest-md5 | |
244 cram-md5 | |
245 login | |
246 anonymous) | |
247 "Priority of authenticators to consider when authenticating to server.") | |
248 | |
249 (defvar imap-authenticator-alist | |
250 '((gssapi imap-gssapi-auth-p imap-gssapi-auth) | |
251 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth) | |
252 (cram-md5 imap-cram-md5-p imap-cram-md5-auth) | |
253 (login imap-login-p imap-login-auth) | |
254 (anonymous imap-anonymous-p imap-anonymous-auth) | |
255 (digest-md5 imap-digest-md5-p imap-digest-md5-auth)) | |
256 "Definition of authenticators. | |
257 | |
258 (NAME CHECK AUTHENTICATE) | |
259 | |
260 NAME names the authenticator. CHECK is a function returning non-nil if | |
261 the server support the authenticator and AUTHENTICATE is a function | |
262 for doing the actuall authentification.") | |
263 | |
264 (defvar imap-use-utf7 t | |
265 "If non-nil, do utf7 encoding/decoding of mailbox names. | |
266 Since the UTF7 decoding currently only decodes into ISO-8859-1 | |
267 characters, you may disable this decoding if you need to access UTF7 | |
268 encoded mailboxes which doesn't translate into ISO-8859-1.") | |
269 | |
270 ;; Internal constants. Change theese and die. | |
271 | |
272 (defconst imap-default-port 143) | |
273 (defconst imap-default-ssl-port 993) | |
274 (defconst imap-default-stream 'network) | |
275 (defconst imap-coding-system-for-read 'binary) | |
276 (defconst imap-coding-system-for-write 'binary) | |
277 (defconst imap-local-variables '(imap-server | |
278 imap-port | |
279 imap-client-eol | |
280 imap-server-eol | |
281 imap-auth | |
282 imap-stream | |
283 imap-username | |
284 imap-password | |
285 imap-current-mailbox | |
286 imap-current-target-mailbox | |
287 imap-message-data | |
288 imap-capability | |
289 imap-namespace | |
290 imap-state | |
291 imap-reached-tag | |
292 imap-failed-tags | |
293 imap-tag | |
294 imap-process | |
295 imap-calculate-literal-size-first | |
296 imap-mailbox-data)) | |
297 | |
298 ;; Internal variables. | |
299 | |
300 (defvar imap-stream nil) | |
301 (defvar imap-auth nil) | |
302 (defvar imap-server nil) | |
303 (defvar imap-port nil) | |
304 (defvar imap-username nil) | |
305 (defvar imap-password nil) | |
306 (defvar imap-calculate-literal-size-first nil) | |
307 (defvar imap-state 'closed | |
308 "IMAP state. | |
309 Valid states are `closed', `initial', `nonauth', `auth', `selected' | |
310 and `examine'.") | |
311 | |
312 (defvar imap-server-eol "\r\n" | |
313 "The EOL string sent from the server.") | |
314 | |
315 (defvar imap-client-eol "\r\n" | |
316 "The EOL string we send to the server.") | |
317 | |
318 (defvar imap-current-mailbox nil | |
319 "Current mailbox name.") | |
320 | |
321 (defvar imap-current-target-mailbox nil | |
322 "Current target mailbox for COPY and APPEND commands.") | |
323 | |
324 (defvar imap-mailbox-data nil | |
325 "Obarray with mailbox data.") | |
326 | |
327 (defvar imap-mailbox-prime 997 | |
328 "Length of imap-mailbox-data.") | |
329 | |
330 (defvar imap-current-message nil | |
331 "Current message number.") | |
332 | |
333 (defvar imap-message-data nil | |
334 "Obarray with message data.") | |
335 | |
336 (defvar imap-message-prime 997 | |
337 "Length of imap-message-data.") | |
338 | |
339 (defvar imap-capability nil | |
340 "Capability for server.") | |
341 | |
342 (defvar imap-namespace nil | |
343 "Namespace for current server.") | |
344 | |
345 (defvar imap-reached-tag 0 | |
346 "Lower limit on command tags that have been parsed.") | |
347 | |
348 (defvar imap-failed-tags nil | |
349 "Alist of tags that failed. | |
350 Each element is a list with four elements; tag (a integer), response | |
351 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and | |
352 human readable response text (a string).") | |
353 | |
354 (defvar imap-tag 0 | |
355 "Command tag number.") | |
356 | |
357 (defvar imap-process nil | |
358 "Process.") | |
359 | |
360 (defvar imap-continuation nil | |
361 "Non-nil indicates that the server emitted a continuation request. | |
362 The actually value is really the text on the continuation line.") | |
363 | |
364 (defvar imap-log nil | |
365 "Name of buffer for imap session trace. | |
366 For example: (setq imap-log \"*imap-log*\")") | |
367 | |
368 (defvar imap-debug nil ;"*imap-debug*" | |
369 "Name of buffer for random debug spew. | |
370 For example: (setq imap-debug \"*imap-debug*\")") | |
371 | |
372 | |
373 ;; Utility functions: | |
374 | |
375 (defsubst imap-disable-multibyte () | |
376 "Enable multibyte in the current buffer." | |
377 (when (fboundp 'set-buffer-multibyte) | |
378 (set-buffer-multibyte nil))) | |
379 | |
380 (defun imap-read-passwd (prompt &rest args) | |
381 "Read a password using PROMPT. | |
382 If ARGS, PROMPT is used as an argument to `format'." | |
383 (let ((prompt (if args | |
384 (apply 'format prompt args) | |
385 prompt))) | |
386 (funcall (if (or (fboundp 'read-passwd) | |
387 (and (load "subr" t) | |
388 (fboundp 'read-passwd)) | |
389 (and (load "passwd" t) | |
390 (fboundp 'read-passwd))) | |
391 'read-passwd | |
392 (autoload 'ange-ftp-read-passwd "ange-ftp") | |
393 'ange-ftp-read-passwd) | |
394 prompt))) | |
395 | |
396 (defsubst imap-utf7-encode (string) | |
397 (if imap-use-utf7 | |
398 (and string | |
399 (condition-case () | |
400 (utf7-encode string t) | |
401 (error (message | |
402 "imap: Could not UTF7 encode `%s', using it unencoded..." | |
403 string) | |
404 string))) | |
405 string)) | |
406 | |
407 (defsubst imap-utf7-decode (string) | |
408 (if imap-use-utf7 | |
409 (and string | |
410 (condition-case () | |
411 (utf7-decode string t) | |
412 (error (message | |
413 "imap: Could not UTF7 decode `%s', using it undecoded..." | |
414 string) | |
415 string))) | |
416 string)) | |
417 | |
418 (defsubst imap-ok-p (status) | |
419 (if (eq status 'OK) | |
420 t | |
421 (setq imap-error status) | |
422 nil)) | |
423 | |
424 (defun imap-error-text (&optional buffer) | |
425 (with-current-buffer (or buffer (current-buffer)) | |
426 (nth 3 (car imap-failed-tags)))) | |
427 | |
428 | |
429 ;; Server functions; stream stuff: | |
430 | |
431 (defun imap-kerberos4-stream-p (buffer) | |
432 (imap-capability 'AUTH=KERBEROS_V4 buffer)) | |
433 | |
434 (defun imap-kerberos4-open (name buffer server port) | |
435 (let ((cmds imap-kerberos4-program) | |
436 cmd done) | |
437 (while (and (not done) (setq cmd (pop cmds))) | |
438 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd) | |
439 (erase-buffer) | |
440 (let* ((port (or port imap-default-port)) | |
441 (coding-system-for-read imap-coding-system-for-read) | |
442 (coding-system-for-write imap-coding-system-for-write) | |
443 (process (start-process | |
444 name buffer shell-file-name shell-command-switch | |
445 (format-spec | |
446 cmd | |
447 (format-spec-make | |
448 ?s server | |
449 ?p (number-to-string port) | |
450 ?l imap-default-user)))) | |
451 response) | |
452 (when process | |
453 (with-current-buffer buffer | |
454 (setq imap-client-eol "\n" | |
455 imap-calculate-literal-size-first t) | |
456 (while (and (memq (process-status process) '(open run)) | |
457 (goto-char (point-min)) | |
458 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities | |
459 (or (while (looking-at "^C:") | |
460 (forward-line)) | |
461 t) | |
462 ;; cyrus 1.6 imtest print "S: " before server greeting | |
463 (or (not (looking-at "S: ")) | |
464 (forward-char 3) | |
465 t) | |
466 (not (and (imap-parse-greeting) | |
467 ;; success in imtest < 1.6: | |
468 (or (re-search-forward | |
469 "^__\\(.*\\)__\n" nil t) | |
470 ;; success in imtest 1.6: | |
471 (re-search-forward | |
472 "^\\(Authenticat.*\\)" nil t)) | |
473 (setq response (match-string 1))))) | |
474 (accept-process-output process 1) | |
475 (sit-for 1)) | |
476 (and imap-log | |
477 (with-current-buffer (get-buffer-create imap-log) | |
478 (imap-disable-multibyte) | |
479 (buffer-disable-undo) | |
480 (goto-char (point-max)) | |
481 (insert-buffer-substring buffer))) | |
482 (erase-buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
483 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
484 (if response (concat "done, " response) "failed")) |
31717 | 485 (if (and response (let ((case-fold-search nil)) |
486 (not (string-match "failed" response)))) | |
487 (setq done process) | |
488 (if (memq (process-status process) '(open run)) | |
489 (imap-send-command-wait "LOGOUT")) | |
490 (delete-process process) | |
491 nil))))) | |
492 done)) | |
493 | |
494 (defun imap-gssapi-stream-p (buffer) | |
495 (imap-capability 'AUTH=GSSAPI buffer)) | |
496 | |
497 (defun imap-gssapi-open (name buffer server port) | |
498 (let ((cmds imap-gssapi-program) | |
499 cmd done) | |
500 (while (and (not done) (setq cmd (pop cmds))) | |
501 (message "Opening GSSAPI IMAP connection with `%s'..." cmd) | |
502 (let* ((port (or port imap-default-port)) | |
503 (coding-system-for-read imap-coding-system-for-read) | |
504 (coding-system-for-write imap-coding-system-for-write) | |
505 (process (start-process | |
506 name buffer shell-file-name shell-command-switch | |
507 (format-spec | |
508 cmd | |
509 (format-spec-make | |
510 ?s server | |
511 ?p (number-to-string port) | |
512 ?l imap-default-user)))) | |
513 response) | |
514 (when process | |
515 (with-current-buffer buffer | |
516 (setq imap-client-eol "\n") | |
517 (while (and (memq (process-status process) '(open run)) | |
518 (goto-char (point-min)) | |
519 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities | |
520 (or (while (looking-at "^C:") | |
521 (forward-line)) | |
522 t) | |
523 ;; cyrus 1.6 imtest print "S: " before server greeting | |
524 (or (not (looking-at "S: ")) | |
525 (forward-char 3) | |
526 t) | |
527 (not (and (imap-parse-greeting) | |
528 ;; success in imtest 1.6: | |
529 (re-search-forward | |
530 "^\\(Authenticat.*\\)" nil t) | |
531 (setq response (match-string 1))))) | |
532 (accept-process-output process 1) | |
533 (sit-for 1)) | |
534 (and imap-log | |
535 (with-current-buffer (get-buffer-create imap-log) | |
536 (imap-disable-multibyte) | |
537 (buffer-disable-undo) | |
538 (goto-char (point-max)) | |
539 (insert-buffer-substring buffer))) | |
540 (erase-buffer) | |
541 (message "GSSAPI IMAP connection: %s" (or response "failed")) | |
542 (if (and response (let ((case-fold-search nil)) | |
543 (not (string-match "failed" response)))) | |
544 (setq done process) | |
545 (if (memq (process-status process) '(open run)) | |
546 (imap-send-command-wait "LOGOUT")) | |
547 (delete-process process) | |
548 nil))))) | |
549 done)) | |
550 | |
551 (defun imap-ssl-p (buffer) | |
552 nil) | |
553 | |
554 (defun imap-ssl-open (name buffer server port) | |
555 "Open a SSL connection to server." | |
556 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program | |
557 (list imap-ssl-program))) | |
558 cmd done) | |
559 (while (and (not done) (setq cmd (pop cmds))) | |
560 (message "imap: Opening SSL connection with `%s'..." cmd) | |
561 (let* ((port (or port imap-default-ssl-port)) | |
562 (coding-system-for-read imap-coding-system-for-read) | |
563 (coding-system-for-write imap-coding-system-for-write) | |
564 (ssl-program-name shell-file-name) | |
565 (ssl-program-arguments | |
566 (list shell-command-switch | |
567 (format-spec cmd (format-spec-make | |
568 ?s server | |
569 ?p (number-to-string port))))) | |
570 process) | |
571 (when (setq process (ignore-errors (open-ssl-stream | |
572 name buffer server port))) | |
573 (with-current-buffer buffer | |
574 (goto-char (point-min)) | |
575 (while (and (memq (process-status process) '(open run)) | |
576 (goto-char (point-max)) | |
577 (forward-line -1) | |
578 (not (imap-parse-greeting))) | |
579 (accept-process-output process 1) | |
580 (sit-for 1)) | |
581 (and imap-log | |
582 (with-current-buffer (get-buffer-create imap-log) | |
583 (imap-disable-multibyte) | |
584 (buffer-disable-undo) | |
585 (goto-char (point-max)) | |
586 (insert-buffer-substring buffer))) | |
587 (erase-buffer) | |
588 (when (memq (process-status process) '(open run)) | |
589 (setq done process)))))) | |
590 (if done | |
591 (progn | |
592 (message "imap: Opening SSL connection with `%s'...done" cmd) | |
593 done) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
594 (message "imap: Opening SSL connection with `%s'...failed" cmd) |
31717 | 595 nil))) |
596 | |
597 (defun imap-network-p (buffer) | |
598 t) | |
599 | |
600 (defun imap-network-open (name buffer server port) | |
601 (let* ((port (or port imap-default-port)) | |
602 (coding-system-for-read imap-coding-system-for-read) | |
603 (coding-system-for-write imap-coding-system-for-write) | |
604 (process (open-network-stream name buffer server port))) | |
605 (when process | |
606 (while (and (memq (process-status process) '(open run)) | |
607 (goto-char (point-min)) | |
608 (not (imap-parse-greeting))) | |
609 (accept-process-output process 1) | |
610 (sit-for 1)) | |
611 (and imap-log | |
612 (with-current-buffer (get-buffer-create imap-log) | |
613 (imap-disable-multibyte) | |
614 (buffer-disable-undo) | |
615 (goto-char (point-max)) | |
616 (insert-buffer-substring buffer))) | |
617 (when (memq (process-status process) '(open run)) | |
618 process)))) | |
619 | |
620 (defun imap-shell-p (buffer) | |
621 nil) | |
622 | |
623 (defun imap-shell-open (name buffer server port) | |
624 (let ((cmds imap-shell-program) | |
625 cmd done) | |
626 (while (and (not done) (setq cmd (pop cmds))) | |
627 (message "imap: Opening IMAP connection with `%s'..." cmd) | |
628 (setq imap-client-eol "\n") | |
629 (let* ((port (or port imap-default-port)) | |
630 (coding-system-for-read imap-coding-system-for-read) | |
631 (coding-system-for-write imap-coding-system-for-write) | |
632 (process (start-process | |
633 name buffer shell-file-name shell-command-switch | |
634 (format-spec | |
635 cmd | |
636 (format-spec-make | |
637 ?s server | |
638 ?g imap-shell-host | |
639 ?p (number-to-string port) | |
640 ?l imap-default-user))))) | |
641 (when process | |
642 (while (and (memq (process-status process) '(open run)) | |
643 (goto-char (point-min)) | |
644 (not (imap-parse-greeting))) | |
645 (accept-process-output process 1) | |
646 (sit-for 1)) | |
647 (erase-buffer) | |
648 (and imap-log | |
649 (with-current-buffer (get-buffer-create imap-log) | |
650 (imap-disable-multibyte) | |
651 (buffer-disable-undo) | |
652 (goto-char (point-max)) | |
653 (insert-buffer-substring buffer))) | |
654 (when (memq (process-status process) '(open run)) | |
655 (setq done process))))) | |
656 (if done | |
657 (progn | |
658 (message "imap: Opening IMAP connection with `%s'...done" cmd) | |
659 done) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
660 (message "imap: Opening IMAP connection with `%s'...failed" cmd) |
31717 | 661 nil))) |
662 | |
663 (defun imap-starttls-p (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
664 (and (imap-capability 'STARTTLS buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
665 (condition-case () |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
666 (progn |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
667 (require 'starttls) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
668 (call-process "starttls")) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
669 (error nil)))) |
31717 | 670 |
671 (defun imap-starttls-open (name buffer server port) | |
672 (let* ((port (or port imap-default-port)) | |
673 (coding-system-for-read imap-coding-system-for-read) | |
674 (coding-system-for-write imap-coding-system-for-write) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
675 (process (starttls-open-stream name buffer server port)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
676 done) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
677 (message "imap: Connecting with STARTTLS...") |
31717 | 678 (when process |
679 (while (and (memq (process-status process) '(open run)) | |
680 (goto-char (point-min)) | |
681 (not (imap-parse-greeting))) | |
682 (accept-process-output process 1) | |
683 (sit-for 1)) | |
684 (and imap-log | |
685 (with-current-buffer (get-buffer-create imap-log) | |
686 (buffer-disable-undo) | |
687 (goto-char (point-max)) | |
688 (insert-buffer-substring buffer))) | |
689 (let ((imap-process process)) | |
690 (unwind-protect | |
691 (progn | |
692 (set-process-filter imap-process 'imap-arrival-filter) | |
693 (when (and (eq imap-stream 'starttls) | |
694 (imap-ok-p (imap-send-command-wait "STARTTLS"))) | |
695 (starttls-negotiate imap-process))) | |
696 (set-process-filter imap-process nil))) | |
697 (when (memq (process-status process) '(open run)) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
698 (setq done process))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
699 (if done |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
700 (progn |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
701 (message "imap: Connecting with STARTTLS...done") |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
702 done) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
703 (message "imap: Connecting with STARTTLS...failed") |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
704 nil))) |
31717 | 705 |
706 ;; Server functions; authenticator stuff: | |
707 | |
708 (defun imap-interactive-login (buffer loginfunc) | |
709 "Login to server in BUFFER. | |
710 LOGINFUNC is passed a username and a password, it should return t if | |
711 it where sucessful authenticating itself to the server, nil otherwise. | |
712 Returns t if login was successful, nil otherwise." | |
713 (with-current-buffer buffer | |
714 (make-variable-buffer-local 'imap-username) | |
715 (make-variable-buffer-local 'imap-password) | |
716 (let (user passwd ret) | |
717 ;; (condition-case () | |
718 (while (or (not user) (not passwd)) | |
719 (setq user (or imap-username | |
720 (read-from-minibuffer | |
721 (concat "IMAP username for " imap-server ": ") | |
722 (or user imap-default-user)))) | |
723 (setq passwd (or imap-password | |
724 (imap-read-passwd | |
725 (concat "IMAP password for " user "@" | |
726 imap-server ": ")))) | |
727 (when (and user passwd) | |
728 (if (funcall loginfunc user passwd) | |
729 (progn | |
730 (setq ret t | |
731 imap-username user) | |
732 (if (and (not imap-password) | |
733 (y-or-n-p "Store password for this session? ")) | |
734 (setq imap-password passwd))) | |
735 (message "Login failed...") | |
736 (setq passwd nil) | |
737 (sit-for 1)))) | |
738 ;; (quit (with-current-buffer buffer | |
739 ;; (setq user nil | |
740 ;; passwd nil))) | |
741 ;; (error (with-current-buffer buffer | |
742 ;; (setq user nil | |
743 ;; passwd nil)))) | |
744 ret))) | |
745 | |
746 (defun imap-gssapi-auth-p (buffer) | |
747 (imap-capability 'AUTH=GSSAPI buffer)) | |
748 | |
749 (defun imap-gssapi-auth (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
750 (message "imap: Authenticating using GSSAPI...%s" |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
751 (if (eq imap-stream 'gssapi) "done" "failed")) |
31717 | 752 (eq imap-stream 'gssapi)) |
753 | |
754 (defun imap-kerberos4-auth-p (buffer) | |
755 (imap-capability 'AUTH=KERBEROS_V4 buffer)) | |
756 | |
757 (defun imap-kerberos4-auth (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
758 (message "imap: Authenticating using Kerberos 4...%s" |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
759 (if (eq imap-stream 'kerberos4) "done" "failed")) |
31717 | 760 (eq imap-stream 'kerberos4)) |
761 | |
762 (defun imap-cram-md5-p (buffer) | |
763 (imap-capability 'AUTH=CRAM-MD5 buffer)) | |
764 | |
765 (defun imap-cram-md5-auth (buffer) | |
766 "Login to server using the AUTH CRAM-MD5 method." | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
767 (message "imap: Authenticating using CRAM-MD5...") |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
768 (let ((done (imap-interactive-login |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
769 buffer |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
770 (lambda (user passwd) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
771 (imap-ok-p |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
772 (imap-send-command-wait |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
773 (list |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
774 "AUTHENTICATE CRAM-MD5" |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
775 (lambda (challenge) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
776 (let* ((decoded (base64-decode-string challenge)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
777 (hash (rfc2104-hash 'md5 64 16 passwd decoded)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
778 (response (concat user " " hash)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
779 (encoded (base64-encode-string response))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
780 encoded))))))))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
781 (if done |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
782 (message "imap: Authenticating using CRAM-MD5...done") |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
783 (message "imap: Authenticating using CRAM-MD5...failed")))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
784 |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
785 |
31717 | 786 |
787 (defun imap-login-p (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
788 (and (not (imap-capability 'LOGINDISABLED buffer)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
789 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))) |
31717 | 790 |
791 (defun imap-login-auth (buffer) | |
792 "Login to server using the LOGIN command." | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
793 (message "imap: Plaintext authentication...") |
31717 | 794 (imap-interactive-login buffer |
795 (lambda (user passwd) | |
796 (imap-ok-p (imap-send-command-wait | |
797 (concat "LOGIN \"" user "\" \"" | |
798 passwd "\"")))))) | |
799 | |
800 (defun imap-anonymous-p (buffer) | |
801 t) | |
802 | |
803 (defun imap-anonymous-auth (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
804 (message "imap: Loging in anonymously...") |
31717 | 805 (with-current-buffer buffer |
806 (imap-ok-p (imap-send-command-wait | |
807 (concat "LOGIN anonymous \"" (concat (user-login-name) "@" | |
808 (system-name)) "\""))))) | |
809 | |
810 (defun imap-digest-md5-p (buffer) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
811 (and (imap-capability 'AUTH=DIGEST-MD5 buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
812 (condition-case () |
31717 | 813 (require 'digest-md5) |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
814 (error nil)))) |
31717 | 815 |
816 (defun imap-digest-md5-auth (buffer) | |
817 "Login to server using the AUTH DIGEST-MD5 method." | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
818 (message "imap: Authenticating using DIGEST-MD5...") |
31717 | 819 (imap-interactive-login |
820 buffer | |
821 (lambda (user passwd) | |
822 (let ((tag | |
823 (imap-send-command | |
824 (list | |
825 "AUTHENTICATE DIGEST-MD5" | |
826 (lambda (challenge) | |
827 (digest-md5-parse-digest-challenge | |
828 (base64-decode-string challenge)) | |
829 (let* ((digest-uri | |
830 (digest-md5-digest-uri | |
831 "imap" (digest-md5-challenge 'realm))) | |
832 (response | |
833 (digest-md5-digest-response | |
834 user passwd digest-uri))) | |
835 (base64-encode-string response 'no-line-break)))) | |
836 ))) | |
837 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
838 nil | |
839 (setq imap-continuation nil) | |
840 (imap-send-command-1 "") | |
841 (imap-ok-p (imap-wait-for-tag tag))))))) | |
842 | |
843 ;; Server functions: | |
844 | |
845 (defun imap-open-1 (buffer) | |
846 (with-current-buffer buffer | |
847 (erase-buffer) | |
848 (setq imap-current-mailbox nil | |
849 imap-current-message nil | |
850 imap-state 'initial | |
851 imap-process (condition-case () | |
852 (funcall (nth 2 (assq imap-stream | |
853 imap-stream-alist)) | |
854 "imap" buffer imap-server imap-port) | |
855 ((error quit) nil))) | |
856 (when imap-process | |
857 (set-process-filter imap-process 'imap-arrival-filter) | |
858 (set-process-sentinel imap-process 'imap-sentinel) | |
859 (while (and (eq imap-state 'initial) | |
860 (memq (process-status imap-process) '(open run))) | |
861 (message "Waiting for response from %s..." imap-server) | |
862 (accept-process-output imap-process 1)) | |
863 (message "Waiting for response from %s...done" imap-server) | |
864 (and (memq (process-status imap-process) '(open run)) | |
865 imap-process)))) | |
866 | |
867 (defun imap-open (server &optional port stream auth buffer) | |
868 "Open a IMAP connection to host SERVER at PORT returning a buffer. | |
869 If PORT is unspecified, a default value is used (143 except | |
870 for SSL which use 993). | |
871 STREAM indicates the stream to use, see `imap-streams' for available | |
872 streams. If nil, it choices the best stream the server is capable of. | |
873 AUTH indicates authenticator to use, see `imap-authenticators' for | |
874 available authenticators. If nil, it choices the best stream the | |
875 server is capable of. | |
876 BUFFER can be a buffer or a name of a buffer, which is created if | |
877 necessery. If nil, the buffer name is generated." | |
878 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0)))) | |
879 (with-current-buffer (get-buffer-create buffer) | |
880 (if (imap-opened buffer) | |
881 (imap-close buffer)) | |
882 (mapcar 'make-variable-buffer-local imap-local-variables) | |
883 (imap-disable-multibyte) | |
884 (buffer-disable-undo) | |
885 (setq imap-server (or server imap-server)) | |
886 (setq imap-port (or port imap-port)) | |
887 (setq imap-auth (or auth imap-auth)) | |
888 (setq imap-stream (or stream imap-stream)) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
889 (message "imap: Connecting to %s..." imap-server) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
890 (if (let ((imap-stream (or imap-stream imap-default-stream))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
891 (imap-open-1 buffer)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
892 ;; Choose stream. |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
893 (let (stream-changed) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
894 (message "imap: Connecting to %s...done" imap-server) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
895 (when (null imap-stream) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
896 (let ((streams imap-streams)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
897 (while (setq stream (pop streams)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
898 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
899 (setq stream-changed (not (eq (or imap-stream |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
900 imap-default-stream) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
901 stream)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
902 imap-stream stream |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
903 streams nil))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
904 (unless imap-stream |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
905 (error "Couldn't figure out a stream for server")))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
906 (when stream-changed |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
907 (message "imap: Reconnecting with stream `%s'..." imap-stream) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
908 (imap-close buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
909 (if (imap-open-1 buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
910 (message "imap: Reconnecting with stream `%s'...done" |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
911 imap-stream) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
912 (message "imap: Reconnecting with stream `%s'...failed" |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
913 imap-stream)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
914 (setq imap-capability nil)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
915 (if (imap-opened buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
916 ;; Choose authenticator |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
917 (when (and (null imap-auth) (not (eq imap-state 'auth))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
918 (let ((auths imap-authenticators)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
919 (while (setq auth (pop auths)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
920 (if (funcall (nth 1 (assq auth imap-authenticator-alist)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
921 buffer) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
922 (setq imap-auth auth |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
923 auths nil))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
924 (unless imap-auth |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
925 (error "Couldn't figure out authenticator for server")))))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
926 (message "imap: Connecting to %s...failed" imap-server)) |
31717 | 927 (when (imap-opened buffer) |
928 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)) | |
929 buffer))) | |
930 | |
931 (defun imap-opened (&optional buffer) | |
932 "Return non-nil if connection to imap server in BUFFER is open. | |
933 If BUFFER is nil then the current buffer is used." | |
934 (and (setq buffer (get-buffer (or buffer (current-buffer)))) | |
935 (buffer-live-p buffer) | |
936 (with-current-buffer buffer | |
937 (and imap-process | |
938 (memq (process-status imap-process) '(open run)))))) | |
939 | |
940 (defun imap-authenticate (&optional user passwd buffer) | |
941 "Authenticate to server in BUFFER, using current buffer if nil. | |
942 It uses the authenticator specified when opening the server. If the | |
943 authenticator requires username/passwords, they are queried from the | |
944 user and optionally stored in the buffer. If USER and/or PASSWD is | |
945 specified, the user will not be questioned and the username and/or | |
946 password is remembered in the buffer." | |
947 (with-current-buffer (or buffer (current-buffer)) | |
948 (if (not (eq imap-state 'nonauth)) | |
949 (or (eq imap-state 'auth) | |
950 (eq imap-state 'select) | |
951 (eq imap-state 'examine)) | |
952 (make-variable-buffer-local 'imap-username) | |
953 (make-variable-buffer-local 'imap-password) | |
954 (if user (setq imap-username user)) | |
955 (if passwd (setq imap-password passwd)) | |
956 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer) | |
957 (setq imap-state 'auth))))) | |
958 | |
959 (defun imap-close (&optional buffer) | |
960 "Close connection to server in BUFFER. | |
961 If BUFFER is nil, the current buffer is used." | |
962 (with-current-buffer (or buffer (current-buffer)) | |
963 (and (imap-opened) | |
964 (not (imap-ok-p (imap-send-command-wait "LOGOUT"))) | |
965 (message "Server %s didn't let me log out" imap-server)) | |
966 (when (and imap-process | |
967 (memq (process-status imap-process) '(open run))) | |
968 (delete-process imap-process)) | |
969 (setq imap-current-mailbox nil | |
970 imap-current-message nil | |
971 imap-process nil) | |
972 (erase-buffer) | |
973 t)) | |
974 | |
975 (defun imap-capability (&optional identifier buffer) | |
976 "Return a list of identifiers which server in BUFFER support. | |
977 If IDENTIFIER, return non-nil if it's among the servers capabilities. | |
978 If BUFFER is nil, the current buffer is assumed." | |
979 (with-current-buffer (or buffer (current-buffer)) | |
980 (unless imap-capability | |
981 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY")) | |
982 (setq imap-capability '(IMAP2)))) | |
983 (if identifier | |
984 (memq (intern (upcase (symbol-name identifier))) imap-capability) | |
985 imap-capability))) | |
986 | |
987 (defun imap-namespace (&optional buffer) | |
988 "Return a namespace hierarchy at server in BUFFER. | |
989 If BUFFER is nil, the current buffer is assumed." | |
990 (with-current-buffer (or buffer (current-buffer)) | |
991 (unless imap-namespace | |
992 (when (imap-capability 'NAMESPACE) | |
993 (imap-send-command-wait "NAMESPACE"))) | |
994 imap-namespace)) | |
995 | |
996 (defun imap-send-command-wait (command &optional buffer) | |
997 (imap-wait-for-tag (imap-send-command command buffer) buffer)) | |
998 | |
999 | |
1000 ;; Mailbox functions: | |
1001 | |
1002 (defun imap-mailbox-put (propname value &optional mailbox buffer) | |
1003 (with-current-buffer (or buffer (current-buffer)) | |
1004 (if imap-mailbox-data | |
1005 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data) | |
1006 propname value) | |
1007 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s" | |
1008 propname value mailbox (current-buffer))) | |
1009 t)) | |
1010 | |
1011 (defsubst imap-mailbox-get-1 (propname &optional mailbox) | |
1012 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data) | |
1013 propname)) | |
1014 | |
1015 (defun imap-mailbox-get (propname &optional mailbox buffer) | |
1016 (let ((mailbox (imap-utf7-encode mailbox))) | |
1017 (with-current-buffer (or buffer (current-buffer)) | |
1018 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox))))) | |
1019 | |
1020 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer) | |
1021 (with-current-buffer (or buffer (current-buffer)) | |
1022 (let (result) | |
1023 (mapatoms | |
1024 (lambda (s) | |
1025 (push (funcall func (if mailbox-decoder | |
1026 (funcall mailbox-decoder (symbol-name s)) | |
1027 (symbol-name s))) result)) | |
1028 imap-mailbox-data) | |
1029 result))) | |
1030 | |
1031 (defun imap-mailbox-map (func &optional buffer) | |
1032 "Map a function across each mailbox in `imap-mailbox-data', returning a list. | |
1033 Function should take a mailbox name (a string) as | |
1034 the only argument." | |
1035 (imap-mailbox-map-1 func 'imap-utf7-decode buffer)) | |
1036 | |
1037 (defun imap-current-mailbox (&optional buffer) | |
1038 (with-current-buffer (or buffer (current-buffer)) | |
1039 (imap-utf7-decode imap-current-mailbox))) | |
1040 | |
1041 (defun imap-current-mailbox-p-1 (mailbox &optional examine) | |
1042 (and (string= mailbox imap-current-mailbox) | |
1043 (or (and examine | |
1044 (eq imap-state 'examine)) | |
1045 (and (not examine) | |
1046 (eq imap-state 'selected))))) | |
1047 | |
1048 (defun imap-current-mailbox-p (mailbox &optional examine buffer) | |
1049 (with-current-buffer (or buffer (current-buffer)) | |
1050 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine))) | |
1051 | |
1052 (defun imap-mailbox-select-1 (mailbox &optional examine) | |
1053 "Select MAILBOX on server in BUFFER. | |
1054 If EXAMINE is non-nil, do a read-only select." | |
1055 (if (imap-current-mailbox-p-1 mailbox examine) | |
1056 imap-current-mailbox | |
1057 (setq imap-current-mailbox mailbox) | |
1058 (if (imap-ok-p (imap-send-command-wait | |
1059 (concat (if examine "EXAMINE" "SELECT") " \"" | |
1060 mailbox "\""))) | |
1061 (progn | |
1062 (setq imap-message-data (make-vector imap-message-prime 0) | |
1063 imap-state (if examine 'examine 'selected)) | |
1064 imap-current-mailbox) | |
1065 ;; Failed SELECT/EXAMINE unselects current mailbox | |
1066 (setq imap-current-mailbox nil)))) | |
1067 | |
1068 (defun imap-mailbox-select (mailbox &optional examine buffer) | |
1069 (with-current-buffer (or buffer (current-buffer)) | |
1070 (imap-utf7-decode | |
1071 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine)))) | |
1072 | |
1073 (defun imap-mailbox-examine-1 (mailbox &optional buffer) | |
1074 (with-current-buffer (or buffer (current-buffer)) | |
1075 (imap-mailbox-select-1 mailbox 'exmine))) | |
1076 | |
1077 (defun imap-mailbox-examine (mailbox &optional buffer) | |
1078 "Examine MAILBOX on server in BUFFER." | |
1079 (imap-mailbox-select mailbox 'exmine buffer)) | |
1080 | |
1081 (defun imap-mailbox-unselect (&optional buffer) | |
1082 "Close current folder in BUFFER, without expunging articles." | |
1083 (with-current-buffer (or buffer (current-buffer)) | |
1084 (when (or (eq imap-state 'auth) | |
1085 (and (imap-capability 'UNSELECT) | |
1086 (imap-ok-p (imap-send-command-wait "UNSELECT"))) | |
1087 (and (imap-ok-p | |
1088 (imap-send-command-wait (concat "EXAMINE \"" | |
1089 imap-current-mailbox | |
1090 "\""))) | |
1091 (imap-ok-p (imap-send-command-wait "CLOSE")))) | |
1092 (setq imap-current-mailbox nil | |
1093 imap-message-data nil | |
1094 imap-state 'auth) | |
1095 t))) | |
1096 | |
1097 (defun imap-mailbox-expunge (&optional buffer) | |
1098 "Expunge articles in current folder in BUFFER. | |
1099 If BUFFER is nil the current buffer is assumed." | |
1100 (with-current-buffer (or buffer (current-buffer)) | |
1101 (when (and imap-current-mailbox (not (eq imap-state 'examine))) | |
1102 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))) | |
1103 | |
1104 (defun imap-mailbox-close (&optional buffer) | |
1105 "Expunge articles and close current folder in BUFFER. | |
1106 If BUFFER is nil the current buffer is assumed." | |
1107 (with-current-buffer (or buffer (current-buffer)) | |
1108 (when (and imap-current-mailbox | |
1109 (imap-ok-p (imap-send-command-wait "CLOSE"))) | |
1110 (setq imap-current-mailbox nil | |
1111 imap-message-data nil | |
1112 imap-state 'auth) | |
1113 t))) | |
1114 | |
1115 (defun imap-mailbox-create-1 (mailbox) | |
1116 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\"")))) | |
1117 | |
1118 (defun imap-mailbox-create (mailbox &optional buffer) | |
1119 "Create MAILBOX on server in BUFFER. | |
1120 If BUFFER is nil the current buffer is assumed." | |
1121 (with-current-buffer (or buffer (current-buffer)) | |
1122 (imap-mailbox-create-1 (imap-utf7-encode mailbox)))) | |
1123 | |
1124 (defun imap-mailbox-delete (mailbox &optional buffer) | |
1125 "Delete MAILBOX on server in BUFFER. | |
1126 If BUFFER is nil the current buffer is assumed." | |
1127 (let ((mailbox (imap-utf7-encode mailbox))) | |
1128 (with-current-buffer (or buffer (current-buffer)) | |
1129 (imap-ok-p | |
1130 (imap-send-command-wait (list "DELETE \"" mailbox "\"")))))) | |
1131 | |
1132 (defun imap-mailbox-rename (oldname newname &optional buffer) | |
1133 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER. | |
1134 If BUFFER is nil the current buffer is assumed." | |
1135 (let ((oldname (imap-utf7-encode oldname)) | |
1136 (newname (imap-utf7-encode newname))) | |
1137 (with-current-buffer (or buffer (current-buffer)) | |
1138 (imap-ok-p | |
1139 (imap-send-command-wait (list "RENAME \"" oldname "\" " | |
1140 "\"" newname "\"")))))) | |
1141 | |
1142 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer) | |
1143 "Return a list of subscribed mailboxes on server in BUFFER. | |
1144 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is | |
1145 non-nil, a hierarchy delimiter is added to root. REFERENCE is a | |
1146 implementation-specific string that has to be passed to lsub command." | |
1147 (with-current-buffer (or buffer (current-buffer)) | |
1148 ;; Make sure we know the hierarchy separator for root's hierarchy | |
1149 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root))) | |
1150 (imap-send-command-wait (concat "LIST \"" reference "\" \"" | |
1151 (imap-utf7-encode root) "\""))) | |
1152 ;; clear list data (NB not delimiter and other stuff) | |
1153 (imap-mailbox-map-1 (lambda (mailbox) | |
1154 (imap-mailbox-put 'lsub nil mailbox))) | |
1155 (when (imap-ok-p | |
1156 (imap-send-command-wait | |
1157 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root) | |
1158 (and add-delimiter (imap-mailbox-get-1 'delimiter root)) | |
1159 "%\""))) | |
1160 (let (out) | |
1161 (imap-mailbox-map-1 (lambda (mailbox) | |
1162 (when (imap-mailbox-get-1 'lsub mailbox) | |
1163 (push (imap-utf7-decode mailbox) out)))) | |
1164 (nreverse out))))) | |
1165 | |
1166 (defun imap-mailbox-list (root &optional reference add-delimiter buffer) | |
1167 "Return a list of mailboxes matching ROOT on server in BUFFER. | |
1168 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to | |
1169 root. REFERENCE is a implementation-specific string that has to be | |
1170 passed to list command." | |
1171 (with-current-buffer (or buffer (current-buffer)) | |
1172 ;; Make sure we know the hierarchy separator for root's hierarchy | |
1173 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root))) | |
1174 (imap-send-command-wait (concat "LIST \"" reference "\" \"" | |
1175 (imap-utf7-encode root) "\""))) | |
1176 ;; clear list data (NB not delimiter and other stuff) | |
1177 (imap-mailbox-map-1 (lambda (mailbox) | |
1178 (imap-mailbox-put 'list nil mailbox))) | |
1179 (when (imap-ok-p | |
1180 (imap-send-command-wait | |
1181 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root) | |
1182 (and add-delimiter (imap-mailbox-get-1 'delimiter root)) | |
1183 "%\""))) | |
1184 (let (out) | |
1185 (imap-mailbox-map-1 (lambda (mailbox) | |
1186 (when (imap-mailbox-get-1 'list mailbox) | |
1187 (push (imap-utf7-decode mailbox) out)))) | |
1188 (nreverse out))))) | |
1189 | |
1190 (defun imap-mailbox-subscribe (mailbox &optional buffer) | |
1191 "Send the SUBSCRIBE command on the mailbox to server in BUFFER. | |
1192 Returns non-nil if successful." | |
1193 (with-current-buffer (or buffer (current-buffer)) | |
1194 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \"" | |
1195 (imap-utf7-encode mailbox) | |
1196 "\""))))) | |
1197 | |
1198 (defun imap-mailbox-unsubscribe (mailbox &optional buffer) | |
1199 "Send the SUBSCRIBE command on the mailbox to server in BUFFER. | |
1200 Returns non-nil if successful." | |
1201 (with-current-buffer (or buffer (current-buffer)) | |
1202 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE " | |
1203 (imap-utf7-encode mailbox) | |
1204 "\""))))) | |
1205 | |
1206 (defun imap-mailbox-status (mailbox items &optional buffer) | |
1207 "Get status items ITEM in MAILBOX from server in BUFFER. | |
1208 ITEMS can be a symbol or a list of symbols, valid symbols are one of | |
1209 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity | |
1210 or 'unseen. If ITEMS is a list of symbols, a list of values is | |
1211 returned, if ITEMS is a symbol only it's value is returned." | |
1212 (with-current-buffer (or buffer (current-buffer)) | |
1213 (when (imap-ok-p | |
1214 (imap-send-command-wait (list "STATUS \"" | |
1215 (imap-utf7-encode mailbox) | |
1216 "\" " | |
1217 (format "%s" | |
1218 (if (listp items) | |
1219 items | |
1220 (list items)))))) | |
1221 (if (listp items) | |
1222 (mapcar (lambda (item) | |
1223 (imap-mailbox-get item mailbox)) | |
1224 items) | |
1225 (imap-mailbox-get items mailbox))))) | |
1226 | |
1227 (defun imap-mailbox-acl-get (&optional mailbox buffer) | |
1228 "Get ACL on mailbox from server in BUFFER." | |
1229 (let ((mailbox (imap-utf7-encode mailbox))) | |
1230 (with-current-buffer (or buffer (current-buffer)) | |
1231 (when (imap-ok-p | |
1232 (imap-send-command-wait (list "GETACL \"" | |
1233 (or mailbox imap-current-mailbox) | |
1234 "\""))) | |
1235 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox)))))) | |
1236 | |
1237 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer) | |
1238 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER." | |
1239 (let ((mailbox (imap-utf7-encode mailbox))) | |
1240 (with-current-buffer (or buffer (current-buffer)) | |
1241 (imap-ok-p | |
1242 (imap-send-command-wait (list "SETACL \"" | |
1243 (or mailbox imap-current-mailbox) | |
1244 "\" " | |
1245 identifier | |
1246 " " | |
1247 rights)))))) | |
1248 | |
1249 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer) | |
1250 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER." | |
1251 (let ((mailbox (imap-utf7-encode mailbox))) | |
1252 (with-current-buffer (or buffer (current-buffer)) | |
1253 (imap-ok-p | |
1254 (imap-send-command-wait (list "DELETEACL \"" | |
1255 (or mailbox imap-current-mailbox) | |
1256 "\" " | |
1257 identifier)))))) | |
1258 | |
1259 | |
1260 ;; Message functions: | |
1261 | |
1262 (defun imap-current-message (&optional buffer) | |
1263 (with-current-buffer (or buffer (current-buffer)) | |
1264 imap-current-message)) | |
1265 | |
1266 (defun imap-list-to-message-set (list) | |
1267 (mapconcat (lambda (item) | |
1268 (number-to-string item)) | |
1269 (if (listp list) | |
1270 list | |
1271 (list list)) | |
1272 ",")) | |
1273 | |
1274 (defun imap-range-to-message-set (range) | |
1275 (mapconcat | |
1276 (lambda (item) | |
1277 (if (consp item) | |
1278 (format "%d:%d" | |
1279 (car item) (cdr item)) | |
1280 (format "%d" item))) | |
1281 (if (and (listp range) (not (listp (cdr range)))) | |
1282 (list range) ;; make (1 . 2) into ((1 . 2)) | |
1283 range) | |
1284 ",")) | |
1285 | |
1286 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer) | |
1287 (with-current-buffer (or buffer (current-buffer)) | |
1288 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ") | |
1289 (if (listp uids) | |
1290 (imap-list-to-message-set uids) | |
1291 uids) | |
1292 props)))) | |
1293 | |
1294 (defun imap-fetch (uids props &optional receive nouidfetch buffer) | |
1295 "Fetch properties PROPS from message set UIDS from server in BUFFER. | |
1296 UIDS can be a string, number or a list of numbers. If RECEIVE | |
1297 is non-nil return theese properties." | |
1298 (with-current-buffer (or buffer (current-buffer)) | |
1299 (when (imap-ok-p (imap-send-command-wait | |
1300 (format "%sFETCH %s %s" (if nouidfetch "" "UID ") | |
1301 (if (listp uids) | |
1302 (imap-list-to-message-set uids) | |
1303 uids) | |
1304 props))) | |
1305 (if (or (null receive) (stringp uids)) | |
1306 t | |
1307 (if (listp uids) | |
1308 (mapcar (lambda (uid) | |
1309 (if (listp receive) | |
1310 (mapcar (lambda (prop) | |
1311 (imap-message-get uid prop)) | |
1312 receive) | |
1313 (imap-message-get uid receive))) | |
1314 uids) | |
1315 (imap-message-get uids receive)))))) | |
1316 | |
1317 (defun imap-message-put (uid propname value &optional buffer) | |
1318 (with-current-buffer (or buffer (current-buffer)) | |
1319 (if imap-message-data | |
1320 (put (intern (number-to-string uid) imap-message-data) | |
1321 propname value) | |
1322 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s" | |
1323 uid propname value (current-buffer))) | |
1324 t)) | |
1325 | |
1326 (defun imap-message-get (uid propname &optional buffer) | |
1327 (with-current-buffer (or buffer (current-buffer)) | |
1328 (get (intern-soft (number-to-string uid) imap-message-data) | |
1329 propname))) | |
1330 | |
1331 (defun imap-message-map (func propname &optional buffer) | |
1332 "Map a function across each mailbox in `imap-message-data', returning a list." | |
1333 (with-current-buffer (or buffer (current-buffer)) | |
1334 (let (result) | |
1335 (mapatoms | |
1336 (lambda (s) | |
1337 (push (funcall func (get s 'UID) (get s propname)) result)) | |
1338 imap-message-data) | |
1339 result))) | |
1340 | |
1341 (defmacro imap-message-envelope-date (uid &optional buffer) | |
1342 `(with-current-buffer (or ,buffer (current-buffer)) | |
1343 (elt (imap-message-get ,uid 'ENVELOPE) 0))) | |
1344 | |
1345 (defmacro imap-message-envelope-subject (uid &optional buffer) | |
1346 `(with-current-buffer (or ,buffer (current-buffer)) | |
1347 (elt (imap-message-get ,uid 'ENVELOPE) 1))) | |
1348 | |
1349 (defmacro imap-message-envelope-from (uid &optional buffer) | |
1350 `(with-current-buffer (or ,buffer (current-buffer)) | |
1351 (elt (imap-message-get ,uid 'ENVELOPE) 2))) | |
1352 | |
1353 (defmacro imap-message-envelope-sender (uid &optional buffer) | |
1354 `(with-current-buffer (or ,buffer (current-buffer)) | |
1355 (elt (imap-message-get ,uid 'ENVELOPE) 3))) | |
1356 | |
1357 (defmacro imap-message-envelope-reply-to (uid &optional buffer) | |
1358 `(with-current-buffer (or ,buffer (current-buffer)) | |
1359 (elt (imap-message-get ,uid 'ENVELOPE) 4))) | |
1360 | |
1361 (defmacro imap-message-envelope-to (uid &optional buffer) | |
1362 `(with-current-buffer (or ,buffer (current-buffer)) | |
1363 (elt (imap-message-get ,uid 'ENVELOPE) 5))) | |
1364 | |
1365 (defmacro imap-message-envelope-cc (uid &optional buffer) | |
1366 `(with-current-buffer (or ,buffer (current-buffer)) | |
1367 (elt (imap-message-get ,uid 'ENVELOPE) 6))) | |
1368 | |
1369 (defmacro imap-message-envelope-bcc (uid &optional buffer) | |
1370 `(with-current-buffer (or ,buffer (current-buffer)) | |
1371 (elt (imap-message-get ,uid 'ENVELOPE) 7))) | |
1372 | |
1373 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer) | |
1374 `(with-current-buffer (or ,buffer (current-buffer)) | |
1375 (elt (imap-message-get ,uid 'ENVELOPE) 8))) | |
1376 | |
1377 (defmacro imap-message-envelope-message-id (uid &optional buffer) | |
1378 `(with-current-buffer (or ,buffer (current-buffer)) | |
1379 (elt (imap-message-get ,uid 'ENVELOPE) 9))) | |
1380 | |
1381 (defmacro imap-message-body (uid &optional buffer) | |
1382 `(with-current-buffer (or ,buffer (current-buffer)) | |
1383 (imap-message-get ,uid 'BODY))) | |
1384 | |
1385 (defun imap-search (predicate &optional buffer) | |
1386 (with-current-buffer (or buffer (current-buffer)) | |
1387 (imap-mailbox-put 'search 'dummy) | |
1388 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate))) | |
1389 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy) | |
1390 (error "Missing SEARCH response to a SEARCH command") | |
1391 (imap-mailbox-get-1 'search imap-current-mailbox))))) | |
1392 | |
1393 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer) | |
1394 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER." | |
1395 (with-current-buffer (or buffer (current-buffer)) | |
1396 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox)) | |
1397 (member flag (imap-mailbox-get 'permanentflags mailbox))))) | |
1398 | |
1399 (defun imap-message-flags-set (articles flags &optional silent buffer) | |
1400 (when (and articles flags) | |
1401 (with-current-buffer (or buffer (current-buffer)) | |
1402 (imap-ok-p (imap-send-command-wait | |
1403 (concat "UID STORE " articles | |
1404 " FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1405 | |
1406 (defun imap-message-flags-del (articles flags &optional silent buffer) | |
1407 (when (and articles flags) | |
1408 (with-current-buffer (or buffer (current-buffer)) | |
1409 (imap-ok-p (imap-send-command-wait | |
1410 (concat "UID STORE " articles | |
1411 " -FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1412 | |
1413 (defun imap-message-flags-add (articles flags &optional silent buffer) | |
1414 (when (and articles flags) | |
1415 (with-current-buffer (or buffer (current-buffer)) | |
1416 (imap-ok-p (imap-send-command-wait | |
1417 (concat "UID STORE " articles | |
1418 " +FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1419 | |
1420 (defun imap-message-copyuid-1 (mailbox) | |
1421 (if (imap-capability 'UIDPLUS) | |
1422 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox)) | |
1423 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox)))) | |
1424 (let ((old-mailbox imap-current-mailbox) | |
1425 (state imap-state) | |
1426 (imap-message-data (make-vector 2 0))) | |
1427 (when (imap-mailbox-examine-1 mailbox) | |
1428 (prog1 | |
1429 (and (imap-fetch "*" "UID") | |
1430 (list (imap-mailbox-get-1 'uidvalidity mailbox) | |
1431 (apply 'max (imap-message-map | |
1432 (lambda (uid prop) uid) 'UID)))) | |
1433 (if old-mailbox | |
1434 (imap-mailbox-select old-mailbox (eq state 'examine)) | |
1435 (imap-mailbox-unselect))))))) | |
1436 | |
1437 (defun imap-message-copyuid (mailbox &optional buffer) | |
1438 (with-current-buffer (or buffer (current-buffer)) | |
1439 (imap-message-copyuid-1 (imap-utf7-decode mailbox)))) | |
1440 | |
1441 (defun imap-message-copy (articles mailbox | |
1442 &optional dont-create no-copyuid buffer) | |
1443 "Copy ARTICLES (a string message set) to MAILBOX on server in | |
1444 BUFFER, creating mailbox if it doesn't exist. If dont-create is | |
1445 non-nil, it will not create a mailbox. On success, return a list with | |
1446 the UIDVALIDITY of the mailbox the article(s) was copied to as the | |
1447 first element, rest of list contain the saved articles' UIDs." | |
1448 (when articles | |
1449 (with-current-buffer (or buffer (current-buffer)) | |
1450 (let ((mailbox (imap-utf7-encode mailbox))) | |
1451 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\"")) | |
1452 (imap-current-target-mailbox mailbox)) | |
1453 (if (imap-ok-p (imap-send-command-wait cmd)) | |
1454 t | |
1455 (when (and (not dont-create) | |
1456 (imap-mailbox-get-1 'trycreate mailbox)) | |
1457 (imap-mailbox-create-1 mailbox) | |
1458 (imap-ok-p (imap-send-command-wait cmd))))) | |
1459 (or no-copyuid | |
1460 (imap-message-copyuid-1 mailbox))))))) | |
1461 | |
1462 (defun imap-message-appenduid-1 (mailbox) | |
1463 (if (imap-capability 'UIDPLUS) | |
1464 (imap-mailbox-get-1 'appenduid mailbox) | |
1465 (let ((old-mailbox imap-current-mailbox) | |
1466 (state imap-state) | |
1467 (imap-message-data (make-vector 2 0))) | |
1468 (when (imap-mailbox-examine-1 mailbox) | |
1469 (prog1 | |
1470 (and (imap-fetch "*" "UID") | |
1471 (list (imap-mailbox-get-1 'uidvalidity mailbox) | |
1472 (apply 'max (imap-message-map | |
1473 (lambda (uid prop) uid) 'UID)))) | |
1474 (if old-mailbox | |
1475 (imap-mailbox-select old-mailbox (eq state 'examine)) | |
1476 (imap-mailbox-unselect))))))) | |
1477 | |
1478 (defun imap-message-appenduid (mailbox &optional buffer) | |
1479 (with-current-buffer (or buffer (current-buffer)) | |
1480 (imap-message-appenduid-1 (imap-utf7-encode mailbox)))) | |
1481 | |
1482 (defun imap-message-append (mailbox article &optional flags date-time buffer) | |
1483 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER. | |
1484 FLAGS and DATE-TIME is currently not used. Return a cons holding | |
1485 uidvalidity of MAILBOX and UID the newly created article got, or nil | |
1486 on failure." | |
1487 (let ((mailbox (imap-utf7-encode mailbox))) | |
1488 (with-current-buffer (or buffer (current-buffer)) | |
1489 (and (let ((imap-current-target-mailbox mailbox)) | |
1490 (imap-ok-p | |
1491 (imap-send-command-wait | |
1492 (list "APPEND \"" mailbox "\" " article)))) | |
1493 (imap-message-appenduid-1 mailbox))))) | |
1494 | |
1495 (defun imap-body-lines (body) | |
1496 "Return number of lines in article by looking at the mime bodystructure BODY." | |
1497 (if (listp body) | |
1498 (if (stringp (car body)) | |
1499 (cond ((and (string= (upcase (car body)) "TEXT") | |
1500 (numberp (nth 7 body))) | |
1501 (nth 7 body)) | |
1502 ((and (string= (upcase (car body)) "MESSAGE") | |
1503 (numberp (nth 9 body))) | |
1504 (nth 9 body)) | |
1505 (t 0)) | |
1506 (apply '+ (mapcar 'imap-body-lines body))) | |
1507 0)) | |
1508 | |
1509 (defun imap-envelope-from (from) | |
1510 "Return a from string line." | |
1511 (and from | |
1512 (concat (aref from 0) | |
1513 (if (aref from 0) " <") | |
1514 (aref from 2) | |
1515 "@" | |
1516 (aref from 3) | |
1517 (if (aref from 0) ">")))) | |
1518 | |
1519 | |
1520 ;; Internal functions. | |
1521 | |
1522 (defun imap-send-command-1 (cmdstr) | |
1523 (setq cmdstr (concat cmdstr imap-client-eol)) | |
1524 (and imap-log | |
1525 (with-current-buffer (get-buffer-create imap-log) | |
1526 (imap-disable-multibyte) | |
1527 (buffer-disable-undo) | |
1528 (goto-char (point-max)) | |
1529 (insert cmdstr))) | |
1530 (process-send-string imap-process cmdstr)) | |
1531 | |
1532 (defun imap-send-command (command &optional buffer) | |
1533 (with-current-buffer (or buffer (current-buffer)) | |
1534 (if (not (listp command)) (setq command (list command))) | |
1535 (let ((tag (setq imap-tag (1+ imap-tag))) | |
1536 cmd cmdstr) | |
1537 (setq cmdstr (concat (number-to-string imap-tag) " ")) | |
1538 (while (setq cmd (pop command)) | |
1539 (cond ((stringp cmd) | |
1540 (setq cmdstr (concat cmdstr cmd))) | |
1541 ((bufferp cmd) | |
1542 (let ((eol imap-client-eol) | |
1543 (calcfirst imap-calculate-literal-size-first) | |
1544 size) | |
1545 (with-current-buffer cmd | |
1546 (if calcfirst | |
1547 (setq size (buffer-size))) | |
1548 (when (not (equal eol "\r\n")) | |
1549 ;; XXX modifies buffer! | |
1550 (goto-char (point-min)) | |
1551 (while (search-forward "\r\n" nil t) | |
1552 (replace-match eol))) | |
1553 (if (not calcfirst) | |
1554 (setq size (buffer-size)))) | |
1555 (setq cmdstr | |
1556 (concat cmdstr (format "{%d}" size)))) | |
1557 (unwind-protect | |
1558 (progn | |
1559 (imap-send-command-1 cmdstr) | |
1560 (setq cmdstr nil) | |
1561 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
1562 (setq command nil);; abort command if no cont-req | |
1563 (let ((process imap-process) | |
1564 (stream imap-stream) | |
1565 (eol imap-client-eol)) | |
1566 (with-current-buffer cmd | |
1567 (and imap-log | |
1568 (with-current-buffer (get-buffer-create | |
1569 imap-log) | |
1570 (imap-disable-multibyte) | |
1571 (buffer-disable-undo) | |
1572 (goto-char (point-max)) | |
1573 (insert-buffer-substring cmd))) | |
1574 (process-send-region process (point-min) | |
1575 (point-max))) | |
1576 (process-send-string process imap-client-eol)))) | |
1577 (setq imap-continuation nil))) | |
1578 ((functionp cmd) | |
1579 (imap-send-command-1 cmdstr) | |
1580 (setq cmdstr nil) | |
1581 (unwind-protect | |
1582 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
1583 (setq command nil);; abort command if no cont-req | |
1584 (setq command (cons (funcall cmd imap-continuation) | |
1585 command))) | |
1586 (setq imap-continuation nil))) | |
1587 (t | |
1588 (error "Unknown command type")))) | |
1589 (if cmdstr | |
1590 (imap-send-command-1 cmdstr)) | |
1591 tag))) | |
1592 | |
1593 (defun imap-wait-for-tag (tag &optional buffer) | |
1594 (with-current-buffer (or buffer (current-buffer)) | |
1595 (while (and (null imap-continuation) | |
1596 (< imap-reached-tag tag)) | |
1597 (or (and (not (memq (process-status imap-process) '(open run))) | |
1598 (sit-for 1)) | |
1599 (accept-process-output imap-process 1))) | |
1600 (or (assq tag imap-failed-tags) | |
1601 (if imap-continuation | |
1602 'INCOMPLETE | |
1603 'OK)))) | |
1604 | |
1605 (defun imap-sentinel (process string) | |
1606 (delete-process process)) | |
1607 | |
1608 (defun imap-find-next-line () | |
1609 "Return point at end of current line, taking into account literals. | |
1610 Return nil if no complete line has arrived." | |
1611 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}" | |
1612 imap-server-eol) | |
1613 nil t) | |
1614 (if (match-string 1) | |
1615 (if (< (point-max) (+ (point) (string-to-number (match-string 1)))) | |
1616 nil | |
1617 (goto-char (+ (point) (string-to-number (match-string 1)))) | |
1618 (imap-find-next-line)) | |
1619 (point)))) | |
1620 | |
1621 (defun imap-arrival-filter (proc string) | |
1622 "IMAP process filter." | |
1623 (with-current-buffer (process-buffer proc) | |
1624 (goto-char (point-max)) | |
1625 (insert string) | |
1626 (and imap-log | |
1627 (with-current-buffer (get-buffer-create imap-log) | |
1628 (imap-disable-multibyte) | |
1629 (buffer-disable-undo) | |
1630 (goto-char (point-max)) | |
1631 (insert string))) | |
1632 (let (end) | |
1633 (goto-char (point-min)) | |
1634 (while (setq end (imap-find-next-line)) | |
1635 (save-restriction | |
1636 (narrow-to-region (point-min) end) | |
1637 (delete-backward-char (length imap-server-eol)) | |
1638 (goto-char (point-min)) | |
1639 (unwind-protect | |
1640 (cond ((eq imap-state 'initial) | |
1641 (imap-parse-greeting)) | |
1642 ((or (eq imap-state 'auth) | |
1643 (eq imap-state 'nonauth) | |
1644 (eq imap-state 'selected) | |
1645 (eq imap-state 'examine)) | |
1646 (imap-parse-response)) | |
1647 (t | |
1648 (message "Unknown state %s in arrival filter" | |
1649 imap-state))) | |
1650 (delete-region (point-min) (point-max)))))))) | |
1651 | |
1652 | |
1653 ;; Imap parser. | |
1654 | |
1655 (defsubst imap-forward () | |
1656 (or (eobp) (forward-char))) | |
1657 | |
1658 ;; number = 1*DIGIT | |
1659 ;; ; Unsigned 32-bit integer | |
1660 ;; ; (0 <= n < 4,294,967,296) | |
1661 | |
1662 (defsubst imap-parse-number () | |
1663 (when (looking-at "[0-9]+") | |
1664 (prog1 | |
1665 (string-to-number (match-string 0)) | |
1666 (goto-char (match-end 0))))) | |
1667 | |
1668 ;; literal = "{" number "}" CRLF *CHAR8 | |
1669 ;; ; Number represents the number of CHAR8s | |
1670 | |
1671 (defsubst imap-parse-literal () | |
1672 (when (looking-at "{\\([0-9]+\\)}\r\n") | |
1673 (let ((pos (match-end 0)) | |
1674 (len (string-to-number (match-string 1)))) | |
1675 (if (< (point-max) (+ pos len)) | |
1676 nil | |
1677 (goto-char (+ pos len)) | |
1678 (buffer-substring pos (+ pos len)))))) | |
1679 | |
1680 ;; string = quoted / literal | |
1681 ;; | |
1682 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE | |
1683 ;; | |
1684 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> / | |
1685 ;; "\" quoted-specials | |
1686 ;; | |
1687 ;; quoted-specials = DQUOTE / "\" | |
1688 ;; | |
1689 ;; TEXT-CHAR = <any CHAR except CR and LF> | |
1690 | |
1691 (defsubst imap-parse-string () | |
1692 (cond ((eq (char-after) ?\") | |
1693 (forward-char 1) | |
1694 (let ((p (point)) (name "")) | |
1695 (skip-chars-forward "^\"\\\\") | |
1696 (setq name (buffer-substring p (point))) | |
1697 (while (eq (char-after) ?\\) | |
1698 (setq p (1+ (point))) | |
1699 (forward-char 2) | |
1700 (skip-chars-forward "^\"\\\\") | |
1701 (setq name (concat name (buffer-substring p (point))))) | |
1702 (forward-char 1) | |
1703 name)) | |
1704 ((eq (char-after) ?{) | |
1705 (imap-parse-literal)))) | |
1706 | |
1707 ;; nil = "NIL" | |
1708 | |
1709 (defsubst imap-parse-nil () | |
1710 (if (looking-at "NIL") | |
1711 (goto-char (match-end 0)))) | |
1712 | |
1713 ;; nstring = string / nil | |
1714 | |
1715 (defsubst imap-parse-nstring () | |
1716 (or (imap-parse-string) | |
1717 (and (imap-parse-nil) | |
1718 nil))) | |
1719 | |
1720 ;; astring = atom / string | |
1721 ;; | |
1722 ;; atom = 1*ATOM-CHAR | |
1723 ;; | |
1724 ;; ATOM-CHAR = <any CHAR except atom-specials> | |
1725 ;; | |
1726 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards / | |
1727 ;; quoted-specials | |
1728 ;; | |
1729 ;; list-wildcards = "%" / "*" | |
1730 ;; | |
1731 ;; quoted-specials = DQUOTE / "\" | |
1732 | |
1733 (defsubst imap-parse-astring () | |
1734 (or (imap-parse-string) | |
1735 (buffer-substring (point) | |
1736 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t) | |
1737 (goto-char (1- (match-end 0))) | |
1738 (end-of-line) | |
1739 (point))))) | |
1740 | |
1741 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP | |
1742 ;; addr-host ")" | |
1743 ;; | |
1744 ;; addr-adl = nstring | |
1745 ;; ; Holds route from [RFC-822] route-addr if | |
1746 ;; ; non-NIL | |
1747 ;; | |
1748 ;; addr-host = nstring | |
1749 ;; ; NIL indicates [RFC-822] group syntax. | |
1750 ;; ; Otherwise, holds [RFC-822] domain name | |
1751 ;; | |
1752 ;; addr-mailbox = nstring | |
1753 ;; ; NIL indicates end of [RFC-822] group; if | |
1754 ;; ; non-NIL and addr-host is NIL, holds | |
1755 ;; ; [RFC-822] group name. | |
1756 ;; ; Otherwise, holds [RFC-822] local-part | |
1757 ;; ; after removing [RFC-822] quoting | |
1758 ;; | |
1759 ;; addr-name = nstring | |
1760 ;; ; If non-NIL, holds phrase from [RFC-822] | |
1761 ;; ; mailbox after removing [RFC-822] quoting | |
1762 ;; | |
1763 | |
1764 (defsubst imap-parse-address () | |
1765 (let (address) | |
1766 (when (eq (char-after) ?\() | |
1767 (imap-forward) | |
1768 (setq address (vector (prog1 (imap-parse-nstring) | |
1769 (imap-forward)) | |
1770 (prog1 (imap-parse-nstring) | |
1771 (imap-forward)) | |
1772 (prog1 (imap-parse-nstring) | |
1773 (imap-forward)) | |
1774 (imap-parse-nstring))) | |
1775 (when (eq (char-after) ?\)) | |
1776 (imap-forward) | |
1777 address)))) | |
1778 | |
1779 ;; address-list = "(" 1*address ")" / nil | |
1780 ;; | |
1781 ;; nil = "NIL" | |
1782 | |
1783 (defsubst imap-parse-address-list () | |
1784 (if (eq (char-after) ?\() | |
1785 (let (address addresses) | |
1786 (imap-forward) | |
1787 (while (and (not (eq (char-after) ?\))) | |
1788 ;; next line for MS Exchange bug | |
1789 (progn (and (eq (char-after) ? ) (imap-forward)) t) | |
1790 (setq address (imap-parse-address))) | |
1791 (setq addresses (cons address addresses))) | |
1792 (when (eq (char-after) ?\)) | |
1793 (imap-forward) | |
1794 (nreverse addresses))) | |
1795 (assert (imap-parse-nil)))) | |
1796 | |
1797 ;; mailbox = "INBOX" / astring | |
1798 ;; ; INBOX is case-insensitive. All case variants of | |
1799 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX | |
1800 ;; ; not as an astring. An astring which consists of | |
1801 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X" | |
1802 ;; ; is considered to be INBOX and not an astring. | |
1803 ;; ; Refer to section 5.1 for further | |
1804 ;; ; semantic details of mailbox names. | |
1805 | |
1806 (defsubst imap-parse-mailbox () | |
1807 (let ((mailbox (imap-parse-astring))) | |
1808 (if (string-equal "INBOX" (upcase mailbox)) | |
1809 "INBOX" | |
1810 mailbox))) | |
1811 | |
1812 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF | |
1813 ;; | |
1814 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text | |
1815 ;; ; Authentication condition | |
1816 ;; | |
1817 ;; resp-cond-bye = "BYE" SP resp-text | |
1818 | |
1819 (defun imap-parse-greeting () | |
1820 "Parse a IMAP greeting." | |
1821 (cond ((looking-at "\\* OK ") | |
1822 (setq imap-state 'nonauth)) | |
1823 ((looking-at "\\* PREAUTH ") | |
1824 (setq imap-state 'auth)) | |
1825 ((looking-at "\\* BYE ") | |
1826 (setq imap-state 'closed)))) | |
1827 | |
1828 ;; response = *(continue-req / response-data) response-done | |
1829 ;; | |
1830 ;; continue-req = "+" SP (resp-text / base64) CRLF | |
1831 ;; | |
1832 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye / | |
1833 ;; mailbox-data / message-data / capability-data) CRLF | |
1834 ;; | |
1835 ;; response-done = response-tagged / response-fatal | |
1836 ;; | |
1837 ;; response-fatal = "*" SP resp-cond-bye CRLF | |
1838 ;; ; Server closes connection immediately | |
1839 ;; | |
1840 ;; response-tagged = tag SP resp-cond-state CRLF | |
1841 ;; | |
1842 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text | |
1843 ;; ; Status condition | |
1844 ;; | |
1845 ;; resp-cond-bye = "BYE" SP resp-text | |
1846 ;; | |
1847 ;; mailbox-data = "FLAGS" SP flag-list / | |
1848 ;; "LIST" SP mailbox-list / | |
1849 ;; "LSUB" SP mailbox-list / | |
1850 ;; "SEARCH" *(SP nz-number) / | |
1851 ;; "STATUS" SP mailbox SP "(" | |
1852 ;; [status-att SP number *(SP status-att SP number)] ")" / | |
1853 ;; number SP "EXISTS" / | |
1854 ;; number SP "RECENT" | |
1855 ;; | |
1856 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att)) | |
1857 ;; | |
1858 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1" | |
1859 ;; *(SP capability) | |
1860 ;; ; IMAP4rev1 servers which offer RFC 1730 | |
1861 ;; ; compatibility MUST list "IMAP4" as the first | |
1862 ;; ; capability. | |
1863 | |
1864 (defun imap-parse-response () | |
1865 "Parse a IMAP command response." | |
1866 (let (token) | |
1867 (case (setq token (read (current-buffer))) | |
1868 (+ (setq imap-continuation | |
1869 (or (buffer-substring (min (point-max) (1+ (point))) | |
1870 (point-max)) | |
1871 t))) | |
1872 (* (case (prog1 (setq token (read (current-buffer))) | |
1873 (imap-forward)) | |
1874 (OK (imap-parse-resp-text)) | |
1875 (NO (imap-parse-resp-text)) | |
1876 (BAD (imap-parse-resp-text)) | |
1877 (BYE (imap-parse-resp-text)) | |
1878 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list))) | |
1879 (LIST (imap-parse-data-list 'list)) | |
1880 (LSUB (imap-parse-data-list 'lsub)) | |
1881 (SEARCH (imap-mailbox-put | |
1882 'search | |
1883 (read (concat "(" (buffer-substring (point) (point-max)) ")")))) | |
1884 (STATUS (imap-parse-status)) | |
1885 (CAPABILITY (setq imap-capability | |
1886 (read (concat "(" (upcase (buffer-substring | |
1887 (point) (point-max))) | |
1888 ")")))) | |
1889 (ACL (imap-parse-acl)) | |
1890 (t (case (prog1 (read (current-buffer)) | |
1891 (imap-forward)) | |
1892 (EXISTS (imap-mailbox-put 'exists token)) | |
1893 (RECENT (imap-mailbox-put 'recent token)) | |
1894 (EXPUNGE t) | |
1895 (FETCH (imap-parse-fetch token)) | |
1896 (t (message "Garbage: %s" (buffer-string))))))) | |
1897 (t (let (status) | |
1898 (if (not (integerp token)) | |
1899 (message "Garbage: %s" (buffer-string)) | |
1900 (case (prog1 (setq status (read (current-buffer))) | |
1901 (imap-forward)) | |
1902 (OK (progn | |
1903 (setq imap-reached-tag (max imap-reached-tag token)) | |
1904 (imap-parse-resp-text))) | |
1905 (NO (progn | |
1906 (setq imap-reached-tag (max imap-reached-tag token)) | |
1907 (save-excursion | |
1908 (imap-parse-resp-text)) | |
1909 (let (code text) | |
1910 (when (eq (char-after) ?\[) | |
1911 (setq code (buffer-substring (point) | |
1912 (search-forward "]"))) | |
1913 (imap-forward)) | |
1914 (setq text (buffer-substring (point) (point-max))) | |
1915 (push (list token status code text) | |
1916 imap-failed-tags)))) | |
1917 (BAD (progn | |
1918 (setq imap-reached-tag (max imap-reached-tag token)) | |
1919 (save-excursion | |
1920 (imap-parse-resp-text)) | |
1921 (let (code text) | |
1922 (when (eq (char-after) ?\[) | |
1923 (setq code (buffer-substring (point) | |
1924 (search-forward "]"))) | |
1925 (imap-forward)) | |
1926 (setq text (buffer-substring (point) (point-max))) | |
1927 (push (list token status code text) imap-failed-tags) | |
1928 (error "Internal error, tag %s status %s code %s text %s" | |
1929 token status code text)))) | |
1930 (t (message "Garbage: %s" (buffer-string)))))))))) | |
1931 | |
1932 ;; resp-text = ["[" resp-text-code "]" SP] text | |
1933 ;; | |
1934 ;; text = 1*TEXT-CHAR | |
1935 ;; | |
1936 ;; TEXT-CHAR = <any CHAR except CR and LF> | |
1937 | |
1938 (defun imap-parse-resp-text () | |
1939 (imap-parse-resp-text-code)) | |
1940 | |
1941 ;; resp-text-code = "ALERT" / | |
1942 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] / | |
1943 ;; "NEWNAME" SP string SP string / | |
1944 ;; "PARSE" / | |
1945 ;; "PERMANENTFLAGS" SP "(" | |
1946 ;; [flag-perm *(SP flag-perm)] ")" / | |
1947 ;; "READ-ONLY" / | |
1948 ;; "READ-WRITE" / | |
1949 ;; "TRYCREATE" / | |
1950 ;; "UIDNEXT" SP nz-number / | |
1951 ;; "UIDVALIDITY" SP nz-number / | |
1952 ;; "UNSEEN" SP nz-number / | |
1953 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">] | |
1954 ;; | |
1955 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid | |
1956 ;; | |
1957 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set | |
1958 ;; | |
1959 ;; set = sequence-num / (sequence-num ":" sequence-num) / | |
1960 ;; (set "," set) | |
1961 ;; ; Identifies a set of messages. For message | |
1962 ;; ; sequence numbers, these are consecutive | |
1963 ;; ; numbers from 1 to the number of messages in | |
1964 ;; ; the mailbox | |
1965 ;; ; Comma delimits individual numbers, colon | |
1966 ;; ; delimits between two numbers inclusive. | |
1967 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13, | |
1968 ;; ; 14,15 for a mailbox with 15 messages. | |
1969 ;; | |
1970 ;; sequence-num = nz-number / "*" | |
1971 ;; ; * is the largest number in use. For message | |
1972 ;; ; sequence numbers, it is the number of messages | |
1973 ;; ; in the mailbox. For unique identifiers, it is | |
1974 ;; ; the unique identifier of the last message in | |
1975 ;; ; the mailbox. | |
1976 ;; | |
1977 ;; flag-perm = flag / "\*" | |
1978 ;; | |
1979 ;; flag = "\Answered" / "\Flagged" / "\Deleted" / | |
1980 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension | |
1981 ;; ; Does not include "\Recent" | |
1982 ;; | |
1983 ;; flag-extension = "\" atom | |
1984 ;; ; Future expansion. Client implementations | |
1985 ;; ; MUST accept flag-extension flags. Server | |
1986 ;; ; implementations MUST NOT generate | |
1987 ;; ; flag-extension flags except as defined by | |
1988 ;; ; future standard or standards-track | |
1989 ;; ; revisions of this specification. | |
1990 ;; | |
1991 ;; flag-keyword = atom | |
1992 ;; | |
1993 ;; resp-text-atom = 1*<any ATOM-CHAR except "]"> | |
1994 | |
1995 (defun imap-parse-resp-text-code () | |
1996 (when (eq (char-after) ?\[) | |
1997 (imap-forward) | |
1998 (cond ((search-forward "PERMANENTFLAGS " nil t) | |
1999 (imap-mailbox-put 'permanentflags (imap-parse-flag-list))) | |
2000 ((search-forward "UIDNEXT " nil t) | |
2001 (imap-mailbox-put 'uidnext (read (current-buffer)))) | |
2002 ((search-forward "UNSEEN " nil t) | |
2003 (imap-mailbox-put 'unseen (read (current-buffer)))) | |
2004 ((looking-at "UIDVALIDITY \\([0-9]+\\)") | |
2005 (imap-mailbox-put 'uidvalidity (match-string 1))) | |
2006 ((search-forward "READ-ONLY" nil t) | |
2007 (imap-mailbox-put 'read-only t)) | |
2008 ((search-forward "NEWNAME " nil t) | |
2009 (let (oldname newname) | |
2010 (setq oldname (imap-parse-string)) | |
2011 (imap-forward) | |
2012 (setq newname (imap-parse-string)) | |
2013 (imap-mailbox-put 'newname newname oldname))) | |
2014 ((search-forward "TRYCREATE" nil t) | |
2015 (imap-mailbox-put 'trycreate t imap-current-target-mailbox)) | |
2016 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)") | |
2017 (imap-mailbox-put 'appenduid | |
2018 (list (match-string 1) | |
2019 (string-to-number (match-string 2))) | |
2020 imap-current-target-mailbox)) | |
2021 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)") | |
2022 (imap-mailbox-put 'copyuid (list (match-string 1) | |
2023 (match-string 2) | |
2024 (match-string 3)) | |
2025 imap-current-target-mailbox)) | |
2026 ((search-forward "ALERT] " nil t) | |
2027 (message "Imap server %s information: %s" imap-server | |
2028 (buffer-substring (point) (point-max))))))) | |
2029 | |
2030 ;; mailbox-list = "(" [mbx-list-flags] ")" SP | |
2031 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox | |
2032 ;; | |
2033 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag | |
2034 ;; *(SP mbx-list-oflag) / | |
2035 ;; mbx-list-oflag *(SP mbx-list-oflag) | |
2036 ;; | |
2037 ;; mbx-list-oflag = "\Noinferiors" / flag-extension | |
2038 ;; ; Other flags; multiple possible per LIST response | |
2039 ;; | |
2040 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked" | |
2041 ;; ; Selectability flags; only one per LIST response | |
2042 ;; | |
2043 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> / | |
2044 ;; "\" quoted-specials | |
2045 ;; | |
2046 ;; quoted-specials = DQUOTE / "\" | |
2047 | |
2048 (defun imap-parse-data-list (type) | |
2049 (let (flags delimiter mailbox) | |
2050 (setq flags (imap-parse-flag-list)) | |
2051 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"") | |
2052 (setq delimiter (match-string 1)) | |
2053 (goto-char (1+ (match-end 0))) | |
2054 (when (setq mailbox (imap-parse-mailbox)) | |
2055 (imap-mailbox-put type t mailbox) | |
2056 (imap-mailbox-put 'list-flags flags mailbox) | |
2057 (imap-mailbox-put 'delimiter delimiter mailbox))))) | |
2058 | |
2059 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope / | |
2060 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" / | |
2061 ;; "INTERNALDATE" SPACE date_time / | |
2062 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring / | |
2063 ;; "RFC822.SIZE" SPACE number / | |
2064 ;; "BODY" ["STRUCTURE"] SPACE body / | |
2065 ;; "BODY" section ["<" number ">"] SPACE nstring / | |
2066 ;; "UID" SPACE uniqueid) ")" | |
2067 ;; | |
2068 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year | |
2069 ;; SPACE time SPACE zone <"> | |
2070 ;; | |
2071 ;; section ::= "[" [section_text / (nz_number *["." nz_number] | |
2072 ;; ["." (section_text / "MIME")])] "]" | |
2073 ;; | |
2074 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"] | |
2075 ;; SPACE header_list / "TEXT" | |
2076 ;; | |
2077 ;; header_fld_name ::= astring | |
2078 ;; | |
2079 ;; header_list ::= "(" 1#header_fld_name ")" | |
2080 | |
2081 (defsubst imap-parse-header-list () | |
2082 (when (eq (char-after) ?\() | |
2083 (let (strlist) | |
2084 (while (not (eq (char-after) ?\))) | |
2085 (imap-forward) | |
2086 (push (imap-parse-astring) strlist)) | |
2087 (imap-forward) | |
2088 (nreverse strlist)))) | |
2089 | |
2090 (defsubst imap-parse-fetch-body-section () | |
2091 (let ((section | |
2092 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t))))) | |
2093 (if (eq (char-before) ? ) | |
2094 (prog1 | |
2095 (mapconcat 'identity (cons section (imap-parse-header-list)) " ") | |
2096 (search-forward "]" nil t)) | |
2097 section))) | |
2098 | |
2099 (defun imap-parse-fetch (response) | |
2100 (when (eq (char-after) ?\() | |
2101 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text | |
2102 rfc822size body bodydetail bodystructure) | |
2103 (while (not (eq (char-after) ?\))) | |
2104 (imap-forward) | |
2105 (let ((token (read (current-buffer)))) | |
2106 (imap-forward) | |
2107 (cond ((eq token 'UID) | |
2108 (setq uid (ignore-errors (read (current-buffer))))) | |
2109 ((eq token 'FLAGS) | |
2110 (setq flags (imap-parse-flag-list))) | |
2111 ((eq token 'ENVELOPE) | |
2112 (setq envelope (imap-parse-envelope))) | |
2113 ((eq token 'INTERNALDATE) | |
2114 (setq internaldate (imap-parse-string))) | |
2115 ((eq token 'RFC822) | |
2116 (setq rfc822 (imap-parse-nstring))) | |
2117 ((eq token 'RFC822.HEADER) | |
2118 (setq rfc822header (imap-parse-nstring))) | |
2119 ((eq token 'RFC822.TEXT) | |
2120 (setq rfc822text (imap-parse-nstring))) | |
2121 ((eq token 'RFC822.SIZE) | |
2122 (setq rfc822size (read (current-buffer)))) | |
2123 ((eq token 'BODY) | |
2124 (if (eq (char-before) ?\[) | |
2125 (push (list | |
2126 (upcase (imap-parse-fetch-body-section)) | |
2127 (and (eq (char-after) ?<) | |
2128 (buffer-substring (1+ (point)) | |
2129 (search-forward ">" nil t))) | |
2130 (progn (imap-forward) | |
2131 (imap-parse-nstring))) | |
2132 bodydetail) | |
2133 (setq body (imap-parse-body)))) | |
2134 ((eq token 'BODYSTRUCTURE) | |
2135 (setq bodystructure (imap-parse-body)))))) | |
2136 (when uid | |
2137 (setq imap-current-message uid) | |
2138 (imap-message-put uid 'UID uid) | |
2139 (and flags (imap-message-put uid 'FLAGS flags)) | |
2140 (and envelope (imap-message-put uid 'ENVELOPE envelope)) | |
2141 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate)) | |
2142 (and rfc822 (imap-message-put uid 'RFC822 rfc822)) | |
2143 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header)) | |
2144 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text)) | |
2145 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size)) | |
2146 (and body (imap-message-put uid 'BODY body)) | |
2147 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail)) | |
2148 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure)) | |
2149 (run-hooks 'imap-fetch-data-hook))))) | |
2150 | |
2151 ;; mailbox-data = ... | |
2152 ;; "STATUS" SP mailbox SP "(" | |
2153 ;; [status-att SP number | |
2154 ;; *(SP status-att SP number)] ")" | |
2155 ;; ... | |
2156 ;; | |
2157 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" / | |
2158 ;; "UNSEEN" | |
2159 | |
2160 (defun imap-parse-status () | |
2161 (let ((mailbox (imap-parse-mailbox))) | |
2162 (when (and mailbox (search-forward "(" nil t)) | |
2163 (while (not (eq (char-after) ?\))) | |
2164 (let ((token (read (current-buffer)))) | |
2165 (cond ((eq token 'MESSAGES) | |
2166 (imap-mailbox-put 'messages (read (current-buffer)) mailbox)) | |
2167 ((eq token 'RECENT) | |
2168 (imap-mailbox-put 'recent (read (current-buffer)) mailbox)) | |
2169 ((eq token 'UIDNEXT) | |
2170 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox)) | |
2171 ((eq token 'UIDVALIDITY) | |
2172 (and (looking-at " \\([0-9]+\\)") | |
2173 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox) | |
2174 (goto-char (match-end 1)))) | |
2175 ((eq token 'UNSEEN) | |
2176 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox)) | |
2177 (t | |
2178 (message "Unknown status data %s in mailbox %s ignored" | |
2179 token mailbox)))))))) | |
2180 | |
2181 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE | |
2182 ;; rights) | |
2183 ;; | |
2184 ;; identifier ::= astring | |
2185 ;; | |
2186 ;; rights ::= astring | |
2187 | |
2188 (defun imap-parse-acl () | |
2189 (let ((mailbox (imap-parse-mailbox)) | |
2190 identifier rights acl) | |
2191 (while (eq (char-after) ?\ ) | |
2192 (imap-forward) | |
2193 (setq identifier (imap-parse-astring)) | |
2194 (imap-forward) | |
2195 (setq rights (imap-parse-astring)) | |
2196 (setq acl (append acl (list (cons identifier rights))))) | |
2197 (imap-mailbox-put 'acl acl mailbox))) | |
2198 | |
2199 ;; flag-list = "(" [flag *(SP flag)] ")" | |
2200 ;; | |
2201 ;; flag = "\Answered" / "\Flagged" / "\Deleted" / | |
2202 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension | |
2203 ;; ; Does not include "\Recent" | |
2204 ;; | |
2205 ;; flag-keyword = atom | |
2206 ;; | |
2207 ;; flag-extension = "\" atom | |
2208 ;; ; Future expansion. Client implementations | |
2209 ;; ; MUST accept flag-extension flags. Server | |
2210 ;; ; implementations MUST NOT generate | |
2211 ;; ; flag-extension flags except as defined by | |
2212 ;; ; future standard or standards-track | |
2213 ;; ; revisions of this specification. | |
2214 | |
2215 (defun imap-parse-flag-list () | |
2216 (let (flag-list start) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2217 (assert (eq (char-after) ?\()) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2218 (while (and (not (eq (char-after) ?\))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2219 (setq start (progn (imap-forward) (point))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2220 (> (skip-chars-forward "^ )" (gnus-point-at-eol)) 0)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2221 (push (buffer-substring start (point)) flag-list)) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2222 (assert (eq (char-after) ?\))) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2223 (imap-forward) |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2224 (nreverse flag-list))) |
31717 | 2225 |
2226 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP | |
2227 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP | |
2228 ;; env-in-reply-to SP env-message-id ")" | |
2229 ;; | |
2230 ;; env-bcc = "(" 1*address ")" / nil | |
2231 ;; | |
2232 ;; env-cc = "(" 1*address ")" / nil | |
2233 ;; | |
2234 ;; env-date = nstring | |
2235 ;; | |
2236 ;; env-from = "(" 1*address ")" / nil | |
2237 ;; | |
2238 ;; env-in-reply-to = nstring | |
2239 ;; | |
2240 ;; env-message-id = nstring | |
2241 ;; | |
2242 ;; env-reply-to = "(" 1*address ")" / nil | |
2243 ;; | |
2244 ;; env-sender = "(" 1*address ")" / nil | |
2245 ;; | |
2246 ;; env-subject = nstring | |
2247 ;; | |
2248 ;; env-to = "(" 1*address ")" / nil | |
2249 | |
2250 (defun imap-parse-envelope () | |
2251 (when (eq (char-after) ?\() | |
2252 (imap-forward) | |
2253 (vector (prog1 (imap-parse-nstring);; date | |
2254 (imap-forward)) | |
2255 (prog1 (imap-parse-nstring);; subject | |
2256 (imap-forward)) | |
2257 (prog1 (imap-parse-address-list);; from | |
2258 (imap-forward)) | |
2259 (prog1 (imap-parse-address-list);; sender | |
2260 (imap-forward)) | |
2261 (prog1 (imap-parse-address-list);; reply-to | |
2262 (imap-forward)) | |
2263 (prog1 (imap-parse-address-list);; to | |
2264 (imap-forward)) | |
2265 (prog1 (imap-parse-address-list);; cc | |
2266 (imap-forward)) | |
2267 (prog1 (imap-parse-address-list);; bcc | |
2268 (imap-forward)) | |
2269 (prog1 (imap-parse-nstring);; in-reply-to | |
2270 (imap-forward)) | |
2271 (prog1 (imap-parse-nstring);; message-id | |
2272 (imap-forward))))) | |
2273 | |
2274 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil | |
2275 | |
2276 (defsubst imap-parse-string-list () | |
2277 (cond ((eq (char-after) ?\();; body-fld-param | |
2278 (let (strlist str) | |
2279 (imap-forward) | |
2280 (while (setq str (imap-parse-string)) | |
2281 (push str strlist) | |
2282 ;; buggy stalker communigate pro 3.0 doesn't print SPC | |
2283 ;; between body-fld-param's sometimes | |
2284 (or (eq (char-after) ?\") | |
2285 (imap-forward))) | |
2286 (nreverse strlist))) | |
2287 ((imap-parse-nil) | |
2288 nil))) | |
2289 | |
2290 ;; body-extension = nstring / number / | |
2291 ;; "(" body-extension *(SP body-extension) ")" | |
2292 ;; ; Future expansion. Client implementations | |
2293 ;; ; MUST accept body-extension fields. Server | |
2294 ;; ; implementations MUST NOT generate | |
2295 ;; ; body-extension fields except as defined by | |
2296 ;; ; future standard or standards-track | |
2297 ;; ; revisions of this specification. | |
2298 | |
2299 (defun imap-parse-body-extension () | |
2300 (if (eq (char-after) ?\() | |
2301 (let (b-e) | |
2302 (imap-forward) | |
2303 (push (imap-parse-body-extension) b-e) | |
2304 (while (eq (char-after) ?\ ) | |
2305 (imap-forward) | |
2306 (push (imap-parse-body-extension) b-e)) | |
2307 (assert (eq (char-after) ?\))) | |
2308 (imap-forward) | |
2309 (nreverse b-e)) | |
2310 (or (imap-parse-number) | |
2311 (imap-parse-nstring)))) | |
2312 | |
2313 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang | |
2314 ;; *(SP body-extension)]] | |
2315 ;; ; MUST NOT be returned on non-extensible | |
2316 ;; ; "BODY" fetch | |
2317 ;; | |
2318 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang | |
2319 ;; *(SP body-extension)]] | |
2320 ;; ; MUST NOT be returned on non-extensible | |
2321 ;; ; "BODY" fetch | |
2322 | |
2323 (defsubst imap-parse-body-ext () | |
2324 (let (ext) | |
2325 (when (eq (char-after) ?\ );; body-fld-dsp | |
2326 (imap-forward) | |
2327 (let (dsp) | |
2328 (if (eq (char-after) ?\() | |
2329 (progn | |
2330 (imap-forward) | |
2331 (push (imap-parse-string) dsp) | |
2332 (imap-forward) | |
2333 (push (imap-parse-string-list) dsp) | |
2334 (imap-forward)) | |
2335 (assert (imap-parse-nil))) | |
2336 (push (nreverse dsp) ext)) | |
2337 (when (eq (char-after) ?\ );; body-fld-lang | |
2338 (imap-forward) | |
2339 (if (eq (char-after) ?\() | |
2340 (push (imap-parse-string-list) ext) | |
2341 (push (imap-parse-nstring) ext)) | |
2342 (while (eq (char-after) ?\ );; body-extension | |
2343 (imap-forward) | |
2344 (setq ext (append (imap-parse-body-extension) ext))))) | |
2345 ext)) | |
2346 | |
2347 ;; body = "(" body-type-1part / body-type-mpart ")" | |
2348 ;; | |
2349 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang | |
2350 ;; *(SP body-extension)]] | |
2351 ;; ; MUST NOT be returned on non-extensible | |
2352 ;; ; "BODY" fetch | |
2353 ;; | |
2354 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang | |
2355 ;; *(SP body-extension)]] | |
2356 ;; ; MUST NOT be returned on non-extensible | |
2357 ;; ; "BODY" fetch | |
2358 ;; | |
2359 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP | |
2360 ;; body-fld-enc SP body-fld-octets | |
2361 ;; | |
2362 ;; body-fld-desc = nstring | |
2363 ;; | |
2364 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil | |
2365 ;; | |
2366 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/ | |
2367 ;; "QUOTED-PRINTABLE") DQUOTE) / string | |
2368 ;; | |
2369 ;; body-fld-id = nstring | |
2370 ;; | |
2371 ;; body-fld-lang = nstring / "(" string *(SP string) ")" | |
2372 ;; | |
2373 ;; body-fld-lines = number | |
2374 ;; | |
2375 ;; body-fld-md5 = nstring | |
2376 ;; | |
2377 ;; body-fld-octets = number | |
2378 ;; | |
2379 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil | |
2380 ;; | |
2381 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text) | |
2382 ;; [SP body-ext-1part] | |
2383 ;; | |
2384 ;; body-type-basic = media-basic SP body-fields | |
2385 ;; ; MESSAGE subtype MUST NOT be "RFC822" | |
2386 ;; | |
2387 ;; body-type-msg = media-message SP body-fields SP envelope | |
2388 ;; SP body SP body-fld-lines | |
2389 ;; | |
2390 ;; body-type-text = media-text SP body-fields SP body-fld-lines | |
2391 ;; | |
2392 ;; body-type-mpart = 1*body SP media-subtype | |
2393 ;; [SP body-ext-mpart] | |
2394 ;; | |
2395 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" / | |
2396 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype | |
2397 ;; ; Defined in [MIME-IMT] | |
2398 ;; | |
2399 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE | |
2400 ;; ; Defined in [MIME-IMT] | |
2401 ;; | |
2402 ;; media-subtype = string | |
2403 ;; ; Defined in [MIME-IMT] | |
2404 ;; | |
2405 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype | |
2406 ;; ; Defined in [MIME-IMT] | |
2407 | |
2408 (defun imap-parse-body () | |
2409 (let (body) | |
2410 (when (eq (char-after) ?\() | |
2411 (imap-forward) | |
2412 (if (eq (char-after) ?\() | |
2413 (let (subbody) | |
2414 (while (and (eq (char-after) ?\() | |
2415 (setq subbody (imap-parse-body))) | |
2416 ;; buggy stalker communigate pro 3.0 insert a SPC between | |
2417 ;; parts in multiparts | |
2418 (when (and (eq (char-after) ?\ ) | |
2419 (eq (char-after (1+ (point))) ?\()) | |
2420 (imap-forward)) | |
2421 (push subbody body)) | |
2422 (imap-forward) | |
2423 (push (imap-parse-string) body);; media-subtype | |
2424 (when (eq (char-after) ?\ );; body-ext-mpart: | |
2425 (imap-forward) | |
2426 (if (eq (char-after) ?\();; body-fld-param | |
2427 (push (imap-parse-string-list) body) | |
2428 (push (and (imap-parse-nil) nil) body)) | |
2429 (setq body | |
2430 (append (imap-parse-body-ext) body)));; body-ext-... | |
2431 (assert (eq (char-after) ?\))) | |
2432 (imap-forward) | |
2433 (nreverse body)) | |
2434 | |
2435 (push (imap-parse-string) body);; media-type | |
2436 (imap-forward) | |
2437 (push (imap-parse-string) body);; media-subtype | |
2438 (imap-forward) | |
2439 ;; next line for Sun SIMS bug | |
2440 (and (eq (char-after) ? ) (imap-forward)) | |
2441 (if (eq (char-after) ?\();; body-fld-param | |
2442 (push (imap-parse-string-list) body) | |
2443 (push (and (imap-parse-nil) nil) body)) | |
2444 (imap-forward) | |
2445 (push (imap-parse-nstring) body);; body-fld-id | |
2446 (imap-forward) | |
2447 (push (imap-parse-nstring) body);; body-fld-desc | |
2448 (imap-forward) | |
32995
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2449 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2450 ;; nstring and return NIL instead of defaulting back to 7BIT |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2451 ;; as the standard says. |
3720ccaca161
2000-10-27 Simon Josefsson <simon@josefsson.org>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
2452 (push (or (imap-parse-nstring) "7BIT") body);; body-fld-enc |
31717 | 2453 (imap-forward) |
2454 (push (imap-parse-number) body);; body-fld-octets | |
2455 | |
2456 ;; ok, we're done parsing the required parts, what comes now is one | |
2457 ;; of three things: | |
2458 ;; | |
2459 ;; envelope (then we're parsing body-type-msg) | |
2460 ;; body-fld-lines (then we're parsing body-type-text) | |
2461 ;; body-ext-1part (then we're parsing body-type-basic) | |
2462 ;; | |
2463 ;; the problem is that the two first are in turn optionally followed | |
2464 ;; by the third. So we parse the first two here (if there are any)... | |
2465 | |
2466 (when (eq (char-after) ?\ ) | |
2467 (imap-forward) | |
2468 (let (lines) | |
2469 (cond ((eq (char-after) ?\();; body-type-msg: | |
2470 (push (imap-parse-envelope) body);; envelope | |
2471 (imap-forward) | |
2472 (push (imap-parse-body) body);; body | |
2473 ;; buggy stalker communigate pro 3.0 doesn't print | |
2474 ;; number of lines in message/rfc822 attachment | |
2475 (if (eq (char-after) ?\)) | |
2476 (push 0 body) | |
2477 (imap-forward) | |
2478 (push (imap-parse-number) body))) ;; body-fld-lines | |
2479 ((setq lines (imap-parse-number)) ;; body-type-text: | |
2480 (push lines body)) ;; body-fld-lines | |
2481 (t | |
2482 (backward-char))))) ;; no match... | |
2483 | |
2484 ;; ...and then parse the third one here... | |
2485 | |
2486 (when (eq (char-after) ?\ );; body-ext-1part: | |
2487 (imap-forward) | |
2488 (push (imap-parse-nstring) body);; body-fld-md5 | |
2489 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part.. | |
2490 | |
2491 (assert (eq (char-after) ?\))) | |
2492 (imap-forward) | |
2493 (nreverse body))))) | |
2494 | |
2495 (when imap-debug ; (untrace-all) | |
2496 (require 'trace) | |
2497 (buffer-disable-undo (get-buffer-create imap-debug)) | |
2498 (mapcar (lambda (f) (trace-function-background f imap-debug)) | |
2499 '( | |
2500 imap-read-passwd | |
2501 imap-utf7-encode | |
2502 imap-utf7-decode | |
2503 imap-error-text | |
2504 imap-kerberos4s-p | |
2505 imap-kerberos4-open | |
2506 imap-ssl-p | |
2507 imap-ssl-open | |
2508 imap-network-p | |
2509 imap-network-open | |
2510 imap-interactive-login | |
2511 imap-kerberos4a-p | |
2512 imap-kerberos4-auth | |
2513 imap-cram-md5-p | |
2514 imap-cram-md5-auth | |
2515 imap-login-p | |
2516 imap-login-auth | |
2517 imap-anonymous-p | |
2518 imap-anonymous-auth | |
2519 imap-open-1 | |
2520 imap-open | |
2521 imap-opened | |
2522 imap-authenticate | |
2523 imap-close | |
2524 imap-capability | |
2525 imap-namespace | |
2526 imap-send-command-wait | |
2527 imap-mailbox-put | |
2528 imap-mailbox-get | |
2529 imap-mailbox-map-1 | |
2530 imap-mailbox-map | |
2531 imap-current-mailbox | |
2532 imap-current-mailbox-p-1 | |
2533 imap-current-mailbox-p | |
2534 imap-mailbox-select-1 | |
2535 imap-mailbox-select | |
2536 imap-mailbox-examine-1 | |
2537 imap-mailbox-examine | |
2538 imap-mailbox-unselect | |
2539 imap-mailbox-expunge | |
2540 imap-mailbox-close | |
2541 imap-mailbox-create-1 | |
2542 imap-mailbox-create | |
2543 imap-mailbox-delete | |
2544 imap-mailbox-rename | |
2545 imap-mailbox-lsub | |
2546 imap-mailbox-list | |
2547 imap-mailbox-subscribe | |
2548 imap-mailbox-unsubscribe | |
2549 imap-mailbox-status | |
2550 imap-mailbox-acl-get | |
2551 imap-mailbox-acl-set | |
2552 imap-mailbox-acl-delete | |
2553 imap-current-message | |
2554 imap-list-to-message-set | |
2555 imap-fetch-asynch | |
2556 imap-fetch | |
2557 imap-message-put | |
2558 imap-message-get | |
2559 imap-message-map | |
2560 imap-search | |
2561 imap-message-flag-permanent-p | |
2562 imap-message-flags-set | |
2563 imap-message-flags-del | |
2564 imap-message-flags-add | |
2565 imap-message-copyuid-1 | |
2566 imap-message-copyuid | |
2567 imap-message-copy | |
2568 imap-message-appenduid-1 | |
2569 imap-message-appenduid | |
2570 imap-message-append | |
2571 imap-body-lines | |
2572 imap-envelope-from | |
2573 imap-send-command-1 | |
2574 imap-send-command | |
2575 imap-wait-for-tag | |
2576 imap-sentinel | |
2577 imap-find-next-line | |
2578 imap-arrival-filter | |
2579 imap-parse-greeting | |
2580 imap-parse-response | |
2581 imap-parse-resp-text | |
2582 imap-parse-resp-text-code | |
2583 imap-parse-data-list | |
2584 imap-parse-fetch | |
2585 imap-parse-status | |
2586 imap-parse-acl | |
2587 imap-parse-flag-list | |
2588 imap-parse-envelope | |
2589 imap-parse-body-extension | |
2590 imap-parse-body | |
2591 ))) | |
2592 | |
2593 (provide 'imap) | |
2594 | |
2595 ;;; imap.el ends here |