Commit | Line | Data |
---|---|---|
b2108a36 TZ |
1 | ;;; imap.el --- imap library |
2 | ||
ba318903 | 3 | ;; Copyright (C) 1998-2014 Free Software Foundation, Inc. |
b2108a36 TZ |
4 | |
5 | ;; Author: Simon Josefsson <simon@josefsson.org> | |
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 3 of the License, or | |
13 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;; imap.el is an elisp library providing an interface for talking to | |
26 | ;; IMAP servers. | |
27 | ;; | |
28 | ;; imap.el is roughly divided in two parts, one that parses IMAP | |
29 | ;; responses from the server and storing data into buffer-local | |
30 | ;; variables, and one for utility functions which send commands to | |
31 | ;; server, waits for an answer, and return information. The latter | |
32 | ;; part is layered on top of the previous. | |
33 | ;; | |
34 | ;; The imap.el API consist of the following functions, other functions | |
35 | ;; in this file should not be called directly and the result of doing | |
36 | ;; so are at best undefined. | |
37 | ;; | |
38 | ;; Global commands: | |
39 | ;; | |
40 | ;; imap-open, imap-opened, imap-authenticate, imap-close, | |
41 | ;; imap-capability, imap-namespace, imap-error-text | |
42 | ;; | |
43 | ;; Mailbox commands: | |
44 | ;; | |
45 | ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox, | |
46 | ;; imap-current-mailbox-p, imap-search, imap-mailbox-select, | |
47 | ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge | |
48 | ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete | |
49 | ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list | |
50 | ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status | |
51 | ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete | |
52 | ;; | |
53 | ;; Message commands: | |
54 | ;; | |
55 | ;; imap-fetch-asynch, imap-fetch, | |
56 | ;; imap-current-message, imap-list-to-message-set, | |
57 | ;; imap-message-get, imap-message-map | |
58 | ;; imap-message-envelope-date, imap-message-envelope-subject, | |
59 | ;; imap-message-envelope-from, imap-message-envelope-sender, | |
60 | ;; imap-message-envelope-reply-to, imap-message-envelope-to, | |
61 | ;; imap-message-envelope-cc, imap-message-envelope-bcc | |
62 | ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id | |
63 | ;; imap-message-body, imap-message-flag-permanent-p | |
64 | ;; imap-message-flags-set, imap-message-flags-del | |
65 | ;; imap-message-flags-add, imap-message-copyuid | |
66 | ;; imap-message-copy, imap-message-appenduid | |
67 | ;; imap-message-append, imap-envelope-from | |
68 | ;; imap-body-lines | |
69 | ;; | |
70 | ;; It is my hope that these commands should be pretty self | |
41013cb4 | 71 | ;; explanatory for someone who knows IMAP. All functions have |
b2108a36 TZ |
72 | ;; additional documentation on how to invoke them. |
73 | ;; | |
74 | ;; imap.el supports RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1). The implemented | |
75 | ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342 | |
76 | ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS, | |
77 | ;; LOGINDISABLED) (with use of external library starttls.el and | |
78 | ;; program starttls), and the GSSAPI / Kerberos V4 sections of RFC1731 | |
79 | ;; (with use of external program `imtest'), and RFC2971 (ID). It also | |
80 | ;; takes advantage of the UNSELECT extension in Cyrus IMAPD. | |
81 | ;; | |
82 | ;; Without the work of John McClary Prevost and Jim Radford this library | |
83 | ;; would not have seen the light of day. Many thanks. | |
84 | ;; | |
85 | ;; This is a transcript of a short interactive session for demonstration | |
86 | ;; purposes. | |
87 | ;; | |
88 | ;; (imap-open "my.mail.server") | |
89 | ;; => " *imap* my.mail.server:0" | |
90 | ;; | |
91 | ;; The rest are invoked with current buffer as the buffer returned by | |
92 | ;; `imap-open'. It is possible to do it all without this, but it would | |
93 | ;; look ugly here since `buffer' is always the last argument for all | |
94 | ;; imap.el API functions. | |
95 | ;; | |
96 | ;; (imap-authenticate "myusername" "mypassword") | |
97 | ;; => auth | |
98 | ;; | |
99 | ;; (imap-mailbox-lsub "*") | |
100 | ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam") | |
101 | ;; | |
102 | ;; (imap-mailbox-list "INBOX.n%") | |
103 | ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq") | |
104 | ;; | |
105 | ;; (imap-mailbox-select "INBOX.nnimap") | |
106 | ;; => "INBOX.nnimap" | |
107 | ;; | |
108 | ;; (imap-mailbox-get 'exists) | |
109 | ;; => 166 | |
110 | ;; | |
111 | ;; (imap-mailbox-get 'uidvalidity) | |
112 | ;; => "908992622" | |
113 | ;; | |
114 | ;; (imap-search "FLAGGED SINCE 18-DEC-98") | |
115 | ;; => (235 236) | |
116 | ;; | |
117 | ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822) | |
118 | ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...." | |
119 | ;; | |
120 | ;; Todo: | |
121 | ;; | |
122 | ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow. | |
123 | ;; Use IEEE floats (which are effectively exact)? -- fx | |
124 | ;; o Don't use `read' at all (important places already fixed) | |
125 | ;; o Accept list of articles instead of message set string in most | |
126 | ;; imap-message-* functions. | |
127 | ;; o Send strings as literal if they contain, e.g., ". | |
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 committed to pgnus | |
135 | ;; | |
136 | ||
137 | ;;; Code: | |
138 | ||
139 | (eval-when-compile (require 'cl)) | |
140 | (eval-and-compile | |
141 | ;; For Emacs <22.2 and XEmacs. | |
6c42fc3e | 142 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))) |
b2108a36 TZ |
143 | (autoload 'starttls-open-stream "starttls") |
144 | (autoload 'starttls-negotiate "starttls") | |
145 | (autoload 'sasl-find-mechanism "sasl") | |
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 'utf7-encode "utf7") | |
152 | (autoload 'utf7-decode "utf7") | |
153 | (autoload 'format-spec "format-spec") | |
154 | (autoload 'format-spec-make "format-spec") | |
155 | (autoload 'open-tls-stream "tls")) | |
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 (list | |
175 | (concat "gsasl %s %p " | |
176 | "--mechanism GSSAPI " | |
177 | "--authentication-id %l") | |
178 | "imtest -m gssapi -u %l -p %p %s") | |
179 | "List of strings containing commands for GSSAPI (krb5) authentication. | |
180 | %s is replaced with server hostname, %p with port to connect to, and | |
181 | %l with the value of `imap-default-user'. The program should accept | |
182 | IMAP commands on stdin and return responses to stdout. Each entry in | |
183 | the list is tried until a successful connection is made." | |
184 | :group 'imap | |
185 | :type '(repeat string)) | |
186 | ||
187 | (defcustom imap-ssl-program '("openssl s_client -quiet -ssl3 -connect %s:%p" | |
188 | "openssl s_client -quiet -ssl2 -connect %s:%p" | |
189 | "s_client -quiet -ssl3 -connect %s:%p" | |
190 | "s_client -quiet -ssl2 -connect %s:%p") | |
191 | "A string, or list of strings, containing commands for SSL connections. | |
192 | Within a string, %s is replaced with the server address and %p with | |
193 | port number on server. The program should accept IMAP commands on | |
194 | stdin and return responses to stdout. Each entry in the list is tried | |
195 | until a successful connection is made." | |
196 | :group 'imap | |
197 | :type '(choice string | |
198 | (repeat string))) | |
199 | ||
200 | (defcustom imap-shell-program '("ssh %s imapd" | |
201 | "rsh %s imapd" | |
202 | "ssh %g ssh %s imapd" | |
203 | "rsh %g rsh %s imapd") | |
204 | "A list of strings, containing commands for IMAP connection. | |
205 | Within a string, %s is replaced with the server address, %p with port | |
206 | number on server, %g with `imap-shell-host', and %l with | |
207 | `imap-default-user'. The program should read IMAP commands from stdin | |
208 | and write IMAP response to stdout. Each entry in the list is tried | |
209 | until a successful connection is made." | |
210 | :group 'imap | |
211 | :type '(repeat string)) | |
212 | ||
213 | (defcustom imap-process-connection-type nil | |
fb7ada5f | 214 | "Value for `process-connection-type' to use for Kerberos4, GSSAPI, shell, and SSL. |
b2108a36 TZ |
215 | The `process-connection-type' variable controls the type of device |
216 | used to communicate with subprocesses. Values are nil to use a | |
217 | pipe, or t or `pty' to use a pty. The value has no effect if the | |
218 | system has no ptys or if all ptys are busy: then a pipe is used | |
219 | in any case. The value takes effect when an IMAP server is | |
220 | opened; changing it after that has no effect." | |
221 | :version "22.1" | |
222 | :group 'imap | |
223 | :type 'boolean) | |
224 | ||
225 | (defcustom imap-use-utf7 t | |
226 | "If non-nil, do utf7 encoding/decoding of mailbox names. | |
227 | Since the UTF7 decoding currently only decodes into ISO-8859-1 | |
228 | characters, you may disable this decoding if you need to access UTF7 | |
229 | encoded mailboxes which doesn't translate into ISO-8859-1." | |
230 | :group 'imap | |
231 | :type 'boolean) | |
232 | ||
233 | (defcustom imap-log nil | |
234 | "If non-nil, an imap session trace is placed in `imap-log-buffer'. | |
235 | Note that username, passwords and other privacy sensitive | |
236 | information (such as e-mail) may be stored in the buffer. | |
237 | It is not written to disk, however. Do not enable this | |
238 | variable unless you are comfortable with that. | |
239 | ||
240 | See also `imap-debug'." | |
241 | :group 'imap | |
242 | :type 'boolean) | |
243 | ||
244 | (defcustom imap-debug nil | |
245 | "If non-nil, trace imap- functions into `imap-debug-buffer'. | |
246 | Uses `trace-function-background', so you can turn it off with, | |
247 | say, `untrace-all'. | |
248 | ||
249 | Note that username, passwords and other privacy sensitive | |
250 | information (such as e-mail) may be stored in the buffer. | |
251 | It is not written to disk, however. Do not enable this | |
252 | variable unless you are comfortable with that. | |
253 | ||
254 | This variable only takes effect when loading the `imap' library. | |
255 | See also `imap-log'." | |
256 | :group 'imap | |
257 | :type 'boolean) | |
258 | ||
259 | (defcustom imap-shell-host "gateway" | |
260 | "Hostname of rlogin proxy." | |
261 | :group 'imap | |
262 | :type 'string) | |
263 | ||
264 | (defcustom imap-default-user (user-login-name) | |
265 | "Default username to use." | |
266 | :group 'imap | |
267 | :type 'string) | |
268 | ||
269 | (defcustom imap-read-timeout (if (string-match | |
270 | "windows-nt\\|os/2\\|cygwin" | |
271 | (symbol-name system-type)) | |
272 | 1.0 | |
273 | 0.1) | |
fb7ada5f | 274 | "How long to wait between checking for the end of output. |
b2108a36 TZ |
275 | Shorter values mean quicker response, but is more CPU intensive." |
276 | :type 'number | |
277 | :group 'imap) | |
278 | ||
279 | (defcustom imap-store-password nil | |
280 | "If non-nil, store session password without prompting." | |
281 | :group 'imap | |
282 | :type 'boolean) | |
283 | ||
284 | ;; Various variables. | |
285 | ||
286 | (defvar imap-fetch-data-hook nil | |
287 | "Hooks called after receiving each FETCH response.") | |
288 | ||
289 | (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell) | |
290 | "Priority of streams to consider when opening connection to server.") | |
291 | ||
292 | (defvar imap-stream-alist | |
293 | '((gssapi imap-gssapi-stream-p imap-gssapi-open) | |
294 | (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open) | |
295 | (tls imap-tls-p imap-tls-open) | |
296 | (ssl imap-ssl-p imap-ssl-open) | |
297 | (network imap-network-p imap-network-open) | |
298 | (shell imap-shell-p imap-shell-open) | |
299 | (starttls imap-starttls-p imap-starttls-open)) | |
300 | "Definition of network streams. | |
301 | ||
302 | \(NAME CHECK OPEN) | |
303 | ||
304 | NAME names the stream, CHECK is a function returning non-nil if the | |
305 | server support the stream and OPEN is a function for opening the | |
306 | stream.") | |
307 | ||
308 | (defvar imap-authenticators '(gssapi | |
309 | kerberos4 | |
310 | digest-md5 | |
311 | cram-md5 | |
312 | ;;sasl | |
313 | login | |
314 | anonymous) | |
315 | "Priority of authenticators to consider when authenticating to server.") | |
316 | ||
317 | (defvar imap-authenticator-alist | |
318 | '((gssapi imap-gssapi-auth-p imap-gssapi-auth) | |
319 | (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth) | |
320 | (sasl imap-sasl-auth-p imap-sasl-auth) | |
321 | (cram-md5 imap-cram-md5-p imap-cram-md5-auth) | |
322 | (login imap-login-p imap-login-auth) | |
323 | (anonymous imap-anonymous-p imap-anonymous-auth) | |
324 | (digest-md5 imap-digest-md5-p imap-digest-md5-auth)) | |
325 | "Definition of authenticators. | |
326 | ||
327 | \(NAME CHECK AUTHENTICATE) | |
328 | ||
329 | NAME names the authenticator. CHECK is a function returning non-nil if | |
330 | the server support the authenticator and AUTHENTICATE is a function | |
331 | for doing the actual authentication.") | |
332 | ||
333 | (defvar imap-error nil | |
334 | "Error codes from the last command.") | |
335 | ||
336 | (defvar imap-logout-timeout nil | |
337 | "Close server immediately if it can't logout in this number of seconds. | |
338 | If it is nil, never close server until logout completes. Normally, | |
339 | the value of this variable will be bound to a certain value to which | |
340 | an application program that uses this module specifies on a per-server | |
341 | basis.") | |
342 | ||
343 | ;; Internal constants. Change these and die. | |
344 | ||
345 | (defconst imap-default-port 143) | |
346 | (defconst imap-default-ssl-port 993) | |
347 | (defconst imap-default-tls-port 993) | |
348 | (defconst imap-default-stream 'network) | |
349 | (defconst imap-coding-system-for-read 'binary) | |
350 | (defconst imap-coding-system-for-write 'binary) | |
351 | (defconst imap-local-variables '(imap-server | |
352 | imap-port | |
353 | imap-client-eol | |
354 | imap-server-eol | |
355 | imap-auth | |
356 | imap-stream | |
357 | imap-username | |
358 | imap-password | |
359 | imap-current-mailbox | |
360 | imap-current-target-mailbox | |
361 | imap-message-data | |
362 | imap-capability | |
363 | imap-id | |
364 | imap-namespace | |
365 | imap-state | |
366 | imap-reached-tag | |
367 | imap-failed-tags | |
368 | imap-tag | |
369 | imap-process | |
370 | imap-calculate-literal-size-first | |
371 | imap-mailbox-data)) | |
372 | (defconst imap-log-buffer "*imap-log*") | |
373 | (defconst imap-debug-buffer "*imap-debug*") | |
374 | ||
375 | ;; Internal variables. | |
376 | ||
377 | (defvar imap-stream nil) | |
378 | (defvar imap-auth nil) | |
379 | (defvar imap-server nil) | |
380 | (defvar imap-port nil) | |
381 | (defvar imap-username nil) | |
382 | (defvar imap-password nil) | |
383 | (defvar imap-last-authenticator nil) | |
384 | (defvar imap-calculate-literal-size-first nil) | |
385 | (defvar imap-state 'closed | |
386 | "IMAP state. | |
387 | Valid states are `closed', `initial', `nonauth', `auth', `selected' | |
388 | and `examine'.") | |
389 | ||
390 | (defvar imap-server-eol "\r\n" | |
391 | "The EOL string sent from the server.") | |
392 | ||
393 | (defvar imap-client-eol "\r\n" | |
394 | "The EOL string we send to the server.") | |
395 | ||
396 | (defvar imap-current-mailbox nil | |
397 | "Current mailbox name.") | |
398 | ||
399 | (defvar imap-current-target-mailbox nil | |
400 | "Current target mailbox for COPY and APPEND commands.") | |
401 | ||
402 | (defvar imap-mailbox-data nil | |
403 | "Obarray with mailbox data.") | |
404 | ||
405 | (defvar imap-mailbox-prime 997 | |
406 | "Length of `imap-mailbox-data'.") | |
407 | ||
408 | (defvar imap-current-message nil | |
409 | "Current message number.") | |
410 | ||
411 | (defvar imap-message-data nil | |
412 | "Obarray with message data.") | |
413 | ||
414 | (defvar imap-message-prime 997 | |
415 | "Length of `imap-message-data'.") | |
416 | ||
417 | (defvar imap-capability nil | |
418 | "Capability for server.") | |
419 | ||
420 | (defvar imap-id nil | |
421 | "Identity of server. | |
422 | See RFC 2971.") | |
423 | ||
424 | (defvar imap-namespace nil | |
425 | "Namespace for current server.") | |
426 | ||
427 | (defvar imap-reached-tag 0 | |
428 | "Lower limit on command tags that have been parsed.") | |
429 | ||
430 | (defvar imap-failed-tags nil | |
431 | "Alist of tags that failed. | |
432 | Each element is a list with four elements; tag (a integer), response | |
433 | state (a symbol, `OK', `NO' or `BAD'), response code (a string), and | |
434 | human readable response text (a string).") | |
435 | ||
436 | (defvar imap-tag 0 | |
437 | "Command tag number.") | |
438 | ||
439 | (defvar imap-process nil | |
440 | "Process.") | |
441 | ||
442 | (defvar imap-continuation nil | |
443 | "Non-nil indicates that the server emitted a continuation request. | |
444 | The actual value is really the text on the continuation line.") | |
445 | ||
446 | (defvar imap-callbacks nil | |
447 | "List of response tags and callbacks, on the form `(number . function)'. | |
448 | The function should take two arguments, the first the IMAP tag and the | |
449 | second the status (OK, NO, BAD etc) of the command.") | |
450 | ||
451 | (defvar imap-enable-exchange-bug-workaround nil | |
452 | "Send FETCH UID commands as *:* instead of *. | |
453 | ||
454 | When non-nil, use an alternative UIDS form. Enabling appears to | |
455 | be required for some servers (e.g., Microsoft Exchange 2007) | |
456 | which otherwise would trigger a response 'BAD The specified | |
457 | message set is invalid.'. We don't unconditionally use this | |
458 | form, since this is said to be significantly inefficient. | |
459 | ||
460 | This variable is set to t automatically per server if the | |
461 | canonical form fails.") | |
462 | ||
463 | \f | |
464 | ;; Utility functions: | |
465 | ||
466 | (defun imap-remassoc (key alist) | |
467 | "Delete by side effect any elements of ALIST whose car is `equal' to KEY. | |
468 | The modified ALIST is returned. If the first member | |
469 | of ALIST has a car that is `equal' to KEY, there is no way to remove it | |
470 | by side effect; therefore, write `(setq foo (remassoc key foo))' to be | |
471 | sure of changing the value of `foo'." | |
472 | (when alist | |
473 | (if (equal key (caar alist)) | |
474 | (cdr alist) | |
475 | (setcdr alist (imap-remassoc key (cdr alist))) | |
476 | alist))) | |
477 | ||
478 | (defmacro imap-disable-multibyte () | |
479 | "Enable multibyte in the current buffer." | |
480 | (unless (featurep 'xemacs) | |
481 | '(set-buffer-multibyte nil))) | |
482 | ||
483 | (defsubst imap-utf7-encode (string) | |
484 | (if imap-use-utf7 | |
485 | (and string | |
486 | (condition-case () | |
487 | (utf7-encode string t) | |
488 | (error (message | |
489 | "imap: Could not UTF7 encode `%s', using it unencoded..." | |
490 | string) | |
491 | string))) | |
492 | string)) | |
493 | ||
494 | (defsubst imap-utf7-decode (string) | |
495 | (if imap-use-utf7 | |
496 | (and string | |
497 | (condition-case () | |
498 | (utf7-decode string t) | |
499 | (error (message | |
500 | "imap: Could not UTF7 decode `%s', using it undecoded..." | |
501 | string) | |
502 | string))) | |
503 | string)) | |
504 | ||
505 | (defsubst imap-ok-p (status) | |
506 | (if (eq status 'OK) | |
507 | t | |
508 | (setq imap-error status) | |
509 | nil)) | |
510 | ||
511 | (defun imap-error-text (&optional buffer) | |
512 | (with-current-buffer (or buffer (current-buffer)) | |
513 | (nth 3 (car imap-failed-tags)))) | |
514 | ||
515 | \f | |
516 | ;; Server functions; stream stuff: | |
517 | ||
518 | (defun imap-log (string-or-buffer) | |
519 | (when imap-log | |
520 | (with-current-buffer (get-buffer-create imap-log-buffer) | |
521 | (imap-disable-multibyte) | |
522 | (buffer-disable-undo) | |
523 | (goto-char (point-max)) | |
524 | (if (bufferp string-or-buffer) | |
525 | (insert-buffer-substring string-or-buffer) | |
526 | (insert string-or-buffer))))) | |
527 | ||
528 | (defun imap-kerberos4-stream-p (buffer) | |
529 | (imap-capability 'AUTH=KERBEROS_V4 buffer)) | |
530 | ||
531 | (defun imap-kerberos4-open (name buffer server port) | |
532 | (let ((cmds imap-kerberos4-program) | |
533 | cmd done) | |
534 | (while (and (not done) (setq cmd (pop cmds))) | |
535 | (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd) | |
536 | (erase-buffer) | |
537 | (let* ((port (or port imap-default-port)) | |
538 | (coding-system-for-read imap-coding-system-for-read) | |
539 | (coding-system-for-write imap-coding-system-for-write) | |
540 | (process-connection-type imap-process-connection-type) | |
541 | (process (start-process | |
542 | name buffer shell-file-name shell-command-switch | |
543 | (format-spec | |
544 | cmd | |
545 | (format-spec-make | |
546 | ?s server | |
547 | ?p (number-to-string port) | |
548 | ?l imap-default-user)))) | |
549 | response) | |
550 | (when process | |
551 | (with-current-buffer buffer | |
552 | (setq imap-client-eol "\n" | |
553 | imap-calculate-literal-size-first t) | |
554 | (while (and (memq (process-status process) '(open run)) | |
555 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
556 | (goto-char (point-min)) | |
557 | ;; Athena IMTEST can output SSL verify errors | |
558 | (or (while (looking-at "^verify error:num=") | |
559 | (forward-line)) | |
560 | t) | |
561 | (or (while (looking-at "^TLS connection established") | |
562 | (forward-line)) | |
563 | t) | |
564 | ;; cyrus 1.6.x (13? < x <= 22) queries capabilities | |
565 | (or (while (looking-at "^C:") | |
566 | (forward-line)) | |
567 | t) | |
568 | ;; cyrus 1.6 imtest print "S: " before server greeting | |
569 | (or (not (looking-at "S: ")) | |
570 | (forward-char 3) | |
571 | t) | |
572 | (not (and (imap-parse-greeting) | |
573 | ;; success in imtest < 1.6: | |
574 | (or (re-search-forward | |
575 | "^__\\(.*\\)__\n" nil t) | |
576 | ;; success in imtest 1.6: | |
577 | (re-search-forward | |
578 | "^\\(Authenticat.*\\)" nil t)) | |
579 | (setq response (match-string 1))))) | |
580 | (accept-process-output process 1) | |
581 | (sit-for 1)) | |
582 | (erase-buffer) | |
583 | (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd | |
584 | (if response (concat "done, " response) "failed")) | |
585 | (if (and response (let ((case-fold-search nil)) | |
586 | (not (string-match "failed" response)))) | |
587 | (setq done process) | |
588 | (if (memq (process-status process) '(open run)) | |
589 | (imap-logout)) | |
590 | (delete-process process) | |
591 | nil))))) | |
592 | done)) | |
593 | ||
594 | (defun imap-gssapi-stream-p (buffer) | |
595 | (imap-capability 'AUTH=GSSAPI buffer)) | |
596 | ||
597 | (defun imap-gssapi-open (name buffer server port) | |
598 | (let ((cmds imap-gssapi-program) | |
599 | cmd done) | |
600 | (while (and (not done) (setq cmd (pop cmds))) | |
601 | (message "Opening GSSAPI IMAP connection with `%s'..." cmd) | |
602 | (erase-buffer) | |
603 | (let* ((port (or port imap-default-port)) | |
604 | (coding-system-for-read imap-coding-system-for-read) | |
605 | (coding-system-for-write imap-coding-system-for-write) | |
606 | (process-connection-type imap-process-connection-type) | |
607 | (process (start-process | |
608 | name buffer shell-file-name shell-command-switch | |
609 | (format-spec | |
610 | cmd | |
611 | (format-spec-make | |
612 | ?s server | |
613 | ?p (number-to-string port) | |
614 | ?l imap-default-user)))) | |
615 | response) | |
616 | (when process | |
617 | (with-current-buffer buffer | |
618 | (setq imap-client-eol "\n" | |
619 | imap-calculate-literal-size-first t) | |
620 | (while (and (memq (process-status process) '(open run)) | |
621 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
622 | (goto-char (point-min)) | |
623 | ;; Athena IMTEST can output SSL verify errors | |
624 | (or (while (looking-at "^verify error:num=") | |
625 | (forward-line)) | |
626 | t) | |
627 | (or (while (looking-at "^TLS connection established") | |
628 | (forward-line)) | |
629 | t) | |
630 | ;; cyrus 1.6.x (13? < x <= 22) queries capabilities | |
631 | (or (while (looking-at "^C:") | |
632 | (forward-line)) | |
633 | t) | |
634 | ;; cyrus 1.6 imtest print "S: " before server greeting | |
635 | (or (not (looking-at "S: ")) | |
636 | (forward-char 3) | |
637 | t) | |
638 | ;; GNU SASL may print 'Trying ...' first. | |
639 | (or (not (looking-at "Trying ")) | |
640 | (forward-line) | |
641 | t) | |
642 | (not (and (imap-parse-greeting) | |
643 | ;; success in imtest 1.6: | |
644 | (re-search-forward | |
645 | (concat "^\\(\\(Authenticat.*\\)\\|\\(" | |
646 | "Client authentication " | |
647 | "finished.*\\)\\)") | |
648 | nil t) | |
649 | (setq response (match-string 1))))) | |
650 | (accept-process-output process 1) | |
651 | (sit-for 1)) | |
652 | (imap-log buffer) | |
653 | (erase-buffer) | |
654 | (message "GSSAPI IMAP connection: %s" (or response "failed")) | |
655 | (if (and response (let ((case-fold-search nil)) | |
656 | (not (string-match "failed" response)))) | |
657 | (setq done process) | |
658 | (if (memq (process-status process) '(open run)) | |
659 | (imap-logout)) | |
660 | (delete-process process) | |
661 | nil))))) | |
662 | done)) | |
663 | ||
6c42fc3e | 664 | (defun imap-ssl-p (_buffer) |
b2108a36 TZ |
665 | nil) |
666 | ||
667 | (defun imap-ssl-open (name buffer server port) | |
668 | "Open an SSL connection to SERVER." | |
669 | (let ((cmds (if (listp imap-ssl-program) imap-ssl-program | |
670 | (list imap-ssl-program))) | |
671 | cmd done) | |
672 | (while (and (not done) (setq cmd (pop cmds))) | |
673 | (message "imap: Opening SSL connection with `%s'..." cmd) | |
674 | (erase-buffer) | |
675 | (let* ((port (or port imap-default-ssl-port)) | |
676 | (coding-system-for-read imap-coding-system-for-read) | |
677 | (coding-system-for-write imap-coding-system-for-write) | |
678 | (process-connection-type imap-process-connection-type) | |
679 | (set-process-query-on-exit-flag | |
680 | (if (fboundp 'set-process-query-on-exit-flag) | |
681 | 'set-process-query-on-exit-flag | |
682 | 'process-kill-without-query)) | |
683 | process) | |
684 | (when (progn | |
685 | (setq process (start-process | |
686 | name buffer shell-file-name | |
687 | shell-command-switch | |
688 | (format-spec cmd | |
689 | (format-spec-make | |
690 | ?s server | |
691 | ?p (number-to-string port))))) | |
692 | (funcall set-process-query-on-exit-flag process nil) | |
693 | process) | |
694 | (with-current-buffer buffer | |
695 | (goto-char (point-min)) | |
696 | (while (and (memq (process-status process) '(open run)) | |
697 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
698 | (goto-char (point-max)) | |
699 | (forward-line -1) | |
700 | (not (imap-parse-greeting))) | |
701 | (accept-process-output process 1) | |
702 | (sit-for 1)) | |
703 | (imap-log buffer) | |
704 | (erase-buffer) | |
705 | (when (memq (process-status process) '(open run)) | |
706 | (setq done process)))))) | |
707 | (if done | |
708 | (progn | |
709 | (message "imap: Opening SSL connection with `%s'...done" cmd) | |
710 | done) | |
711 | (message "imap: Opening SSL connection with `%s'...failed" cmd) | |
712 | nil))) | |
713 | ||
6c42fc3e | 714 | (defun imap-tls-p (_buffer) |
b2108a36 TZ |
715 | nil) |
716 | ||
717 | (defun imap-tls-open (name buffer server port) | |
718 | (let* ((port (or port imap-default-tls-port)) | |
719 | (coding-system-for-read imap-coding-system-for-read) | |
720 | (coding-system-for-write imap-coding-system-for-write) | |
721 | (process (open-tls-stream name buffer server port))) | |
722 | (when process | |
723 | (while (and (memq (process-status process) '(open run)) | |
724 | ;; FIXME: Per the "blue moon" comment, the process/buffer | |
725 | ;; handling here, and elsewhere in functions which open | |
726 | ;; streams, looks confused. Obviously we can change buffers | |
727 | ;; if a different process handler kicks in from | |
728 | ;; `accept-process-output' or `sit-for' below, and TRT seems | |
729 | ;; to be to `save-buffer' around those calls. (I wonder why | |
730 | ;; `sit-for' is used with a non-zero wait.) -- fx | |
731 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
732 | (goto-char (point-max)) | |
733 | (forward-line -1) | |
734 | (not (imap-parse-greeting))) | |
735 | (accept-process-output process 1) | |
736 | (sit-for 1)) | |
737 | (imap-log buffer) | |
738 | (when (memq (process-status process) '(open run)) | |
739 | process)))) | |
740 | ||
6c42fc3e | 741 | (defun imap-network-p (_buffer) |
b2108a36 TZ |
742 | t) |
743 | ||
744 | (defun imap-network-open (name buffer server port) | |
745 | (let* ((port (or port imap-default-port)) | |
746 | (coding-system-for-read imap-coding-system-for-read) | |
747 | (coding-system-for-write imap-coding-system-for-write) | |
748 | (process (open-network-stream name buffer server port))) | |
749 | (when process | |
750 | (while (and (memq (process-status process) '(open run)) | |
751 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
752 | (goto-char (point-min)) | |
753 | (not (imap-parse-greeting))) | |
754 | (accept-process-output process 1) | |
755 | (sit-for 1)) | |
756 | (imap-log buffer) | |
757 | (when (memq (process-status process) '(open run)) | |
758 | process)))) | |
759 | ||
6c42fc3e | 760 | (defun imap-shell-p (_buffer) |
b2108a36 TZ |
761 | nil) |
762 | ||
763 | (defun imap-shell-open (name buffer server port) | |
764 | (let ((cmds (if (listp imap-shell-program) imap-shell-program | |
765 | (list imap-shell-program))) | |
766 | cmd done) | |
767 | (while (and (not done) (setq cmd (pop cmds))) | |
768 | (message "imap: Opening IMAP connection with `%s'..." cmd) | |
769 | (setq imap-client-eol "\n") | |
770 | (let* ((port (or port imap-default-port)) | |
771 | (coding-system-for-read imap-coding-system-for-read) | |
772 | (coding-system-for-write imap-coding-system-for-write) | |
b62f8267 | 773 | (process-connection-type imap-process-connection-type) |
b2108a36 TZ |
774 | (process (start-process |
775 | name buffer shell-file-name shell-command-switch | |
776 | (format-spec | |
777 | cmd | |
778 | (format-spec-make | |
779 | ?s server | |
780 | ?g imap-shell-host | |
781 | ?p (number-to-string port) | |
782 | ?l imap-default-user))))) | |
783 | (when process | |
784 | (while (and (memq (process-status process) '(open run)) | |
785 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
786 | (goto-char (point-max)) | |
787 | (forward-line -1) | |
788 | (not (imap-parse-greeting))) | |
789 | (accept-process-output process 1) | |
790 | (sit-for 1)) | |
791 | (imap-log buffer) | |
792 | (erase-buffer) | |
793 | (when (memq (process-status process) '(open run)) | |
794 | (setq done process))))) | |
795 | (if done | |
796 | (progn | |
797 | (message "imap: Opening IMAP connection with `%s'...done" cmd) | |
798 | done) | |
799 | (message "imap: Opening IMAP connection with `%s'...failed" cmd) | |
800 | nil))) | |
801 | ||
802 | (defun imap-starttls-p (buffer) | |
803 | (imap-capability 'STARTTLS buffer)) | |
804 | ||
805 | (defun imap-starttls-open (name buffer server port) | |
806 | (let* ((port (or port imap-default-port)) | |
807 | (coding-system-for-read imap-coding-system-for-read) | |
808 | (coding-system-for-write imap-coding-system-for-write) | |
809 | (process (starttls-open-stream name buffer server port)) | |
810 | done tls-info) | |
811 | (message "imap: Connecting with STARTTLS...") | |
812 | (when process | |
813 | (while (and (memq (process-status process) '(open run)) | |
814 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
815 | (goto-char (point-max)) | |
816 | (forward-line -1) | |
817 | (not (imap-parse-greeting))) | |
818 | (accept-process-output process 1) | |
819 | (sit-for 1)) | |
820 | (imap-send-command "STARTTLS") | |
821 | (while (and (memq (process-status process) '(open run)) | |
822 | (set-buffer buffer) ;; XXX "blue moon" nntp.el bug | |
823 | (goto-char (point-max)) | |
824 | (forward-line -1) | |
825 | (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t))) | |
826 | (accept-process-output process 1) | |
827 | (sit-for 1)) | |
828 | (imap-log buffer) | |
829 | (when (and (setq tls-info (starttls-negotiate process)) | |
830 | (memq (process-status process) '(open run))) | |
831 | (setq done process))) | |
832 | (if (stringp tls-info) | |
833 | (message "imap: STARTTLS info: %s" tls-info)) | |
834 | (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed")) | |
835 | done)) | |
836 | ||
837 | ;; Server functions; authenticator stuff: | |
838 | ||
839 | (defun imap-interactive-login (buffer loginfunc) | |
840 | "Login to server in BUFFER. | |
41013cb4 MK |
841 | Return t if login was successful, nil otherwise. |
842 | ||
843 | LOGINFUNC is passed a username and a password. It should return | |
844 | t if it successfully authenticates, nil otherwise." | |
b2108a36 TZ |
845 | (with-current-buffer buffer |
846 | (make-local-variable 'imap-username) | |
847 | (make-local-variable 'imap-password) | |
848 | (let (user passwd ret) | |
849 | ;; (condition-case () | |
850 | (while (or (not user) (not passwd)) | |
851 | (setq user (or imap-username | |
852 | (read-from-minibuffer | |
853 | (concat "imap: username for " imap-server | |
854 | " (using stream `" (symbol-name imap-stream) | |
855 | "'): ") | |
856 | (or user imap-default-user)))) | |
857 | (setq passwd (or imap-password | |
858 | (read-passwd | |
859 | (concat "imap: password for " user "@" | |
860 | imap-server " (using authenticator `" | |
861 | (symbol-name imap-auth) "'): ")))) | |
862 | (when (and user passwd) | |
863 | (if (funcall loginfunc user passwd) | |
864 | (progn | |
865 | (message "imap: Login successful...") | |
866 | (setq ret t | |
867 | imap-username user) | |
868 | (when (and (not imap-password) | |
869 | (or imap-store-password | |
870 | (y-or-n-p "imap: Store password for this IMAP session? "))) | |
871 | (setq imap-password passwd))) | |
872 | (message "imap: Login failed...") | |
873 | (setq passwd nil) | |
874 | (setq imap-password nil) | |
875 | (sit-for 1)))) | |
876 | ;; (quit (with-current-buffer buffer | |
877 | ;; (setq user nil | |
878 | ;; passwd nil))) | |
879 | ;; (error (with-current-buffer buffer | |
880 | ;; (setq user nil | |
881 | ;; passwd nil)))) | |
882 | ret))) | |
883 | ||
6c42fc3e | 884 | (defun imap-gssapi-auth-p (_buffer) |
b2108a36 TZ |
885 | (eq imap-stream 'gssapi)) |
886 | ||
6c42fc3e | 887 | (defun imap-gssapi-auth (_buffer) |
b2108a36 TZ |
888 | (message "imap: Authenticating using GSSAPI...%s" |
889 | (if (eq imap-stream 'gssapi) "done" "failed")) | |
890 | (eq imap-stream 'gssapi)) | |
891 | ||
892 | (defun imap-kerberos4-auth-p (buffer) | |
893 | (and (imap-capability 'AUTH=KERBEROS_V4 buffer) | |
894 | (eq imap-stream 'kerberos4))) | |
895 | ||
6c42fc3e | 896 | (defun imap-kerberos4-auth (_buffer) |
b2108a36 TZ |
897 | (message "imap: Authenticating using Kerberos 4...%s" |
898 | (if (eq imap-stream 'kerberos4) "done" "failed")) | |
899 | (eq imap-stream 'kerberos4)) | |
900 | ||
901 | (defun imap-cram-md5-p (buffer) | |
902 | (imap-capability 'AUTH=CRAM-MD5 buffer)) | |
903 | ||
904 | (defun imap-cram-md5-auth (buffer) | |
905 | "Login to server using the AUTH CRAM-MD5 method." | |
906 | (message "imap: Authenticating using CRAM-MD5...") | |
907 | (let ((done (imap-interactive-login | |
908 | buffer | |
909 | (lambda (user passwd) | |
910 | (imap-ok-p | |
911 | (imap-send-command-wait | |
912 | (list | |
913 | "AUTHENTICATE CRAM-MD5" | |
914 | (lambda (challenge) | |
915 | (let* ((decoded (base64-decode-string challenge)) | |
916 | (hash (rfc2104-hash 'md5 64 16 passwd decoded)) | |
917 | (response (concat user " " hash)) | |
918 | (encoded (base64-encode-string response))) | |
919 | encoded))))))))) | |
920 | (if done | |
921 | (message "imap: Authenticating using CRAM-MD5...done") | |
922 | (message "imap: Authenticating using CRAM-MD5...failed")))) | |
923 | ||
924 | (defun imap-login-p (buffer) | |
925 | (and (not (imap-capability 'LOGINDISABLED buffer)) | |
926 | (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))) | |
927 | ||
928 | (defun imap-quote-specials (string) | |
929 | (with-temp-buffer | |
930 | (insert string) | |
931 | (goto-char (point-min)) | |
932 | (while (re-search-forward "[\\\"]" nil t) | |
933 | (forward-char -1) | |
934 | (insert "\\") | |
935 | (forward-char 1)) | |
936 | (buffer-string))) | |
937 | ||
938 | (defun imap-login-auth (buffer) | |
939 | "Login to server using the LOGIN command." | |
940 | (message "imap: Plaintext authentication...") | |
941 | (imap-interactive-login buffer | |
942 | (lambda (user passwd) | |
943 | (imap-ok-p (imap-send-command-wait | |
944 | (concat "LOGIN \"" | |
945 | (imap-quote-specials user) | |
946 | "\" \"" | |
947 | (imap-quote-specials passwd) | |
948 | "\"")))))) | |
949 | ||
6c42fc3e | 950 | (defun imap-anonymous-p (_buffer) |
b2108a36 TZ |
951 | t) |
952 | ||
953 | (defun imap-anonymous-auth (buffer) | |
954 | (message "imap: Logging in anonymously...") | |
955 | (with-current-buffer buffer | |
956 | (imap-ok-p (imap-send-command-wait | |
957 | (concat "LOGIN anonymous \"" (concat (user-login-name) "@" | |
958 | (system-name)) "\""))))) | |
959 | ||
960 | ;;; Compiler directives. | |
961 | ||
962 | (defvar imap-sasl-client) | |
963 | (defvar imap-sasl-step) | |
964 | ||
965 | (defun imap-sasl-make-mechanisms (buffer) | |
966 | (let ((mecs '())) | |
967 | (mapc (lambda (sym) | |
968 | (let ((name (symbol-name sym))) | |
969 | (if (and (> (length name) 5) | |
970 | (string-equal "AUTH=" (substring name 0 5 ))) | |
971 | (setq mecs (cons (substring name 5) mecs))))) | |
972 | (imap-capability nil buffer)) | |
973 | mecs)) | |
974 | ||
975 | (declare-function sasl-find-mechanism "sasl" (mechanism)) | |
976 | (declare-function sasl-mechanism-name "sasl" (mechanism)) | |
977 | (declare-function sasl-make-client "sasl" (mechanism name service server)) | |
978 | (declare-function sasl-next-step "sasl" (client step)) | |
979 | (declare-function sasl-step-data "sasl" (step)) | |
980 | (declare-function sasl-step-set-data "sasl" (step data)) | |
981 | ||
982 | (defun imap-sasl-auth-p (buffer) | |
983 | (and (condition-case () | |
984 | (require 'sasl) | |
985 | (error nil)) | |
986 | (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))) | |
987 | ||
988 | (defun imap-sasl-auth (buffer) | |
989 | "Login to server using the SASL method." | |
990 | (message "imap: Authenticating using SASL...") | |
991 | (with-current-buffer buffer | |
992 | (make-local-variable 'imap-username) | |
993 | (make-local-variable 'imap-sasl-client) | |
994 | (make-local-variable 'imap-sasl-step) | |
995 | (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))) | |
996 | logged user) | |
997 | (while (not logged) | |
998 | (setq user (or imap-username | |
999 | (read-from-minibuffer | |
1000 | (concat "IMAP username for " imap-server " using SASL " | |
1001 | (sasl-mechanism-name mechanism) ": ") | |
1002 | (or user imap-default-user)))) | |
1003 | (when user | |
1004 | (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server) | |
1005 | imap-sasl-step (sasl-next-step imap-sasl-client nil)) | |
1006 | (let ((tag (imap-send-command | |
1007 | (if (sasl-step-data imap-sasl-step) | |
1008 | (format "AUTHENTICATE %s %s" | |
1009 | (sasl-mechanism-name mechanism) | |
1010 | (sasl-step-data imap-sasl-step)) | |
1011 | (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism))) | |
1012 | buffer))) | |
1013 | (while (eq (imap-wait-for-tag tag) 'INCOMPLETE) | |
1014 | (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation)) | |
1015 | (setq imap-continuation nil | |
1016 | imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step)) | |
1017 | (imap-send-command-1 (if (sasl-step-data imap-sasl-step) | |
1018 | (base64-encode-string (sasl-step-data imap-sasl-step) t) | |
1019 | ""))) | |
1020 | (if (imap-ok-p (imap-wait-for-tag tag)) | |
1021 | (setq imap-username user | |
1022 | logged t) | |
1023 | (message "Login failed...") | |
1024 | (sit-for 1))))) | |
1025 | logged))) | |
1026 | ||
1027 | (defun imap-digest-md5-p (buffer) | |
1028 | (and (imap-capability 'AUTH=DIGEST-MD5 buffer) | |
1029 | (condition-case () | |
1030 | (require 'digest-md5) | |
1031 | (error nil)))) | |
1032 | ||
1033 | (defun imap-digest-md5-auth (buffer) | |
1034 | "Login to server using the AUTH DIGEST-MD5 method." | |
1035 | (message "imap: Authenticating using DIGEST-MD5...") | |
1036 | (imap-interactive-login | |
1037 | buffer | |
1038 | (lambda (user passwd) | |
1039 | (let ((tag | |
1040 | (imap-send-command | |
1041 | (list | |
1042 | "AUTHENTICATE DIGEST-MD5" | |
1043 | (lambda (challenge) | |
1044 | (digest-md5-parse-digest-challenge | |
1045 | (base64-decode-string challenge)) | |
1046 | (let* ((digest-uri | |
1047 | (digest-md5-digest-uri | |
1048 | "imap" (digest-md5-challenge 'realm))) | |
1049 | (response | |
1050 | (digest-md5-digest-response | |
1051 | user passwd digest-uri))) | |
1052 | (base64-encode-string response 'no-line-break)))) | |
1053 | ))) | |
1054 | (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
1055 | nil | |
1056 | (setq imap-continuation nil) | |
1057 | (imap-send-command-1 "") | |
1058 | (imap-ok-p (imap-wait-for-tag tag))))))) | |
1059 | ||
1060 | ;; Server functions: | |
1061 | ||
1062 | (defun imap-open-1 (buffer) | |
1063 | (with-current-buffer buffer | |
1064 | (erase-buffer) | |
1065 | (setq imap-current-mailbox nil | |
1066 | imap-current-message nil | |
1067 | imap-state 'initial | |
1068 | imap-process (condition-case () | |
1069 | (funcall (nth 2 (assq imap-stream | |
1070 | imap-stream-alist)) | |
1071 | "imap" buffer imap-server imap-port) | |
1072 | ((error quit) nil))) | |
1073 | (when imap-process | |
1074 | (set-process-filter imap-process 'imap-arrival-filter) | |
1075 | (set-process-sentinel imap-process 'imap-sentinel) | |
1076 | (while (and (eq imap-state 'initial) | |
1077 | (memq (process-status imap-process) '(open run))) | |
1078 | (message "Waiting for response from %s..." imap-server) | |
1079 | (accept-process-output imap-process 1)) | |
1080 | (message "Waiting for response from %s...done" imap-server) | |
1081 | (and (memq (process-status imap-process) '(open run)) | |
1082 | imap-process)))) | |
1083 | ||
1084 | (defun imap-open (server &optional port stream auth buffer) | |
1085 | "Open an IMAP connection to host SERVER at PORT returning a buffer. | |
1086 | If PORT is unspecified, a default value is used (143 except | |
1087 | for SSL which use 993). | |
1088 | STREAM indicates the stream to use, see `imap-streams' for available | |
1089 | streams. If nil, it choices the best stream the server is capable of. | |
1090 | AUTH indicates authenticator to use, see `imap-authenticators' for | |
1091 | available authenticators. If nil, it choices the best stream the | |
1092 | server is capable of. | |
1093 | BUFFER can be a buffer or a name of a buffer, which is created if | |
1094 | necessary. If nil, the buffer name is generated." | |
1095 | (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0)))) | |
1096 | (with-current-buffer (get-buffer-create buffer) | |
1097 | (if (imap-opened buffer) | |
1098 | (imap-close buffer)) | |
1099 | (mapc 'make-local-variable imap-local-variables) | |
1100 | (imap-disable-multibyte) | |
1101 | (buffer-disable-undo) | |
1102 | (setq imap-server (or server imap-server)) | |
1103 | (setq imap-port (or port imap-port)) | |
1104 | (setq imap-auth (or auth imap-auth)) | |
1105 | (setq imap-stream (or stream imap-stream)) | |
1106 | (message "imap: Connecting to %s..." imap-server) | |
1107 | (if (null (let ((imap-stream (or imap-stream imap-default-stream))) | |
1108 | (imap-open-1 buffer))) | |
1109 | (progn | |
1110 | (message "imap: Connecting to %s...failed" imap-server) | |
1111 | nil) | |
1112 | (when (null imap-stream) | |
1113 | ;; Need to choose stream. | |
1114 | (let ((streams imap-streams)) | |
1115 | (while (setq stream (pop streams)) | |
1116 | ;; OK to use this stream? | |
1117 | (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer) | |
1118 | ;; Stream changed? | |
1119 | (if (not (eq imap-default-stream stream)) | |
1120 | (with-current-buffer (get-buffer-create | |
1121 | (generate-new-buffer-name " *temp*")) | |
1122 | (mapc 'make-local-variable imap-local-variables) | |
1123 | (imap-disable-multibyte) | |
1124 | (buffer-disable-undo) | |
1125 | (setq imap-server (or server imap-server)) | |
1126 | (setq imap-port (or port imap-port)) | |
1127 | (setq imap-auth (or auth imap-auth)) | |
1128 | (message "imap: Reconnecting with stream `%s'..." stream) | |
1129 | (if (null (let ((imap-stream stream)) | |
1130 | (imap-open-1 (current-buffer)))) | |
1131 | (progn | |
1132 | (kill-buffer (current-buffer)) | |
1133 | (message | |
1134 | "imap: Reconnecting with stream `%s'...failed" | |
1135 | stream)) | |
1136 | ;; We're done, kill the first connection | |
1137 | (imap-close buffer) | |
1138 | (let ((name (if (stringp buffer) | |
1139 | buffer | |
1140 | (buffer-name buffer)))) | |
1141 | (kill-buffer buffer) | |
1142 | (rename-buffer name) | |
1143 | ;; set the passed buffer to the current one, | |
1144 | ;; so that (imap-opened buffer) later will work | |
1145 | (setq buffer (current-buffer))) | |
1146 | (message "imap: Reconnecting with stream `%s'...done" | |
1147 | stream) | |
1148 | (setq imap-stream stream) | |
1149 | (setq imap-capability nil) | |
1150 | (setq streams nil))) | |
1151 | ;; We're done | |
1152 | (message "imap: Connecting to %s...done" imap-server) | |
1153 | (setq imap-stream stream) | |
1154 | (setq imap-capability nil) | |
1155 | (setq streams nil)))))) | |
1156 | (when (imap-opened buffer) | |
1157 | (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))) | |
1158 | ;; (debug "opened+state+auth+buffer" (imap-opened buffer) imap-state imap-auth buffer) | |
1159 | (when imap-stream | |
1160 | buffer)))) | |
1161 | ||
1162 | (defcustom imap-ping-server t | |
1163 | "If non-nil, check if IMAP is open. | |
1164 | See the function `imap-ping-server'." | |
1165 | :version "23.1" ;; No Gnus | |
1166 | :group 'imap | |
1167 | :type 'boolean) | |
1168 | ||
1169 | (defun imap-opened (&optional buffer) | |
1170 | "Return non-nil if connection to imap server in BUFFER is open. | |
1171 | If BUFFER is nil then the current buffer is used." | |
1172 | (and (setq buffer (get-buffer (or buffer (current-buffer)))) | |
1173 | (buffer-live-p buffer) | |
1174 | (with-current-buffer buffer | |
1175 | (and imap-process | |
1176 | (memq (process-status imap-process) '(open run)) | |
1177 | (if imap-ping-server | |
1178 | (imap-ping-server) | |
1179 | t))))) | |
1180 | ||
1181 | (defun imap-ping-server (&optional buffer) | |
1182 | "Ping the IMAP server in BUFFER with a \"NOOP\" command. | |
1183 | Return non-nil if the server responds, and nil if it does not | |
1184 | respond. If BUFFER is nil, the current buffer is used." | |
1185 | (condition-case () | |
1186 | (imap-ok-p (imap-send-command-wait "NOOP" buffer)) | |
1187 | (error nil))) | |
1188 | ||
1189 | (defun imap-authenticate (&optional user passwd buffer) | |
1190 | "Authenticate to server in BUFFER, using current buffer if nil. | |
41013cb4 MK |
1191 | It uses the authenticator specified when opening the server. |
1192 | ||
1193 | Optional arguments USER and PASSWD specify the username and | |
1194 | password to use if the authenticator requires a username and/or | |
1195 | password. If omitted or nil, the authenticator may query the | |
1196 | user for a username and/or password." | |
b2108a36 TZ |
1197 | (with-current-buffer (or buffer (current-buffer)) |
1198 | (if (not (eq imap-state 'nonauth)) | |
1199 | (or (eq imap-state 'auth) | |
1200 | (eq imap-state 'selected) | |
1201 | (eq imap-state 'examine)) | |
1202 | (make-local-variable 'imap-username) | |
1203 | (make-local-variable 'imap-password) | |
1204 | (make-local-variable 'imap-last-authenticator) | |
1205 | (when user (setq imap-username user)) | |
1206 | (when passwd (setq imap-password passwd)) | |
1207 | (if imap-auth | |
1208 | (and (setq imap-last-authenticator | |
1209 | (assq imap-auth imap-authenticator-alist)) | |
1210 | (funcall (nth 2 imap-last-authenticator) (current-buffer)) | |
1211 | (setq imap-state 'auth)) | |
1212 | ;; Choose authenticator. | |
1213 | (let ((auths imap-authenticators) | |
1214 | auth) | |
1215 | (while (setq auth (pop auths)) | |
1216 | ;; OK to use authenticator? | |
1217 | (setq imap-last-authenticator | |
1218 | (assq auth imap-authenticator-alist)) | |
1219 | (when (funcall (nth 1 imap-last-authenticator) (current-buffer)) | |
1220 | (message "imap: Authenticating to `%s' using `%s'..." | |
1221 | imap-server auth) | |
1222 | (setq imap-auth auth) | |
1223 | (if (funcall (nth 2 imap-last-authenticator) (current-buffer)) | |
1224 | (progn | |
1225 | (message "imap: Authenticating to `%s' using `%s'...done" | |
1226 | imap-server auth) | |
1227 | ;; set imap-state correctly on successful auth attempt | |
1228 | (setq imap-state 'auth) | |
1229 | ;; stop iterating through the authenticator list | |
1230 | (setq auths nil)) | |
1231 | (message "imap: Authenticating to `%s' using `%s'...failed" | |
1232 | imap-server auth))))) | |
1233 | imap-state)))) | |
1234 | ||
1235 | (defun imap-close (&optional buffer) | |
1236 | "Close connection to server in BUFFER. | |
1237 | If BUFFER is nil, the current buffer is used." | |
1238 | (with-current-buffer (or buffer (current-buffer)) | |
1239 | (when (imap-opened) | |
1240 | (condition-case nil | |
1241 | (imap-logout-wait) | |
1242 | (quit nil))) | |
1243 | (when (and imap-process | |
1244 | (memq (process-status imap-process) '(open run))) | |
1245 | (delete-process imap-process)) | |
1246 | (setq imap-current-mailbox nil | |
1247 | imap-current-message nil | |
1248 | imap-process nil) | |
1249 | (erase-buffer) | |
1250 | t)) | |
1251 | ||
1252 | (defun imap-capability (&optional identifier buffer) | |
1253 | "Return a list of identifiers which server in BUFFER support. | |
1254 | If IDENTIFIER, return non-nil if it's among the servers capabilities. | |
1255 | If BUFFER is nil, the current buffer is assumed." | |
1256 | (with-current-buffer (or buffer (current-buffer)) | |
1257 | (unless imap-capability | |
1258 | (unless (imap-ok-p (imap-send-command-wait "CAPABILITY")) | |
1259 | (setq imap-capability '(IMAP2)))) | |
1260 | (if identifier | |
1261 | (memq (intern (upcase (symbol-name identifier))) imap-capability) | |
1262 | imap-capability))) | |
1263 | ||
1264 | (defun imap-id (&optional list-of-values buffer) | |
1265 | "Identify client to server in BUFFER, and return server identity. | |
1266 | LIST-OF-VALUES is nil, or a plist with identifier and value | |
1267 | strings to send to the server to identify the client. | |
1268 | ||
1269 | Return a list of identifiers which server in BUFFER support, or | |
1270 | nil if it doesn't support ID or returns no information. | |
1271 | ||
1272 | If BUFFER is nil, the current buffer is assumed." | |
1273 | (with-current-buffer (or buffer (current-buffer)) | |
1274 | (when (and (imap-capability 'ID) | |
1275 | (imap-ok-p (imap-send-command-wait | |
1276 | (if (null list-of-values) | |
1277 | "ID NIL" | |
1278 | (concat "ID (" (mapconcat (lambda (el) | |
1279 | (concat "\"" el "\"")) | |
1280 | list-of-values | |
1281 | " ") ")"))))) | |
1282 | imap-id))) | |
1283 | ||
1284 | (defun imap-namespace (&optional buffer) | |
1285 | "Return a namespace hierarchy at server in BUFFER. | |
1286 | If BUFFER is nil, the current buffer is assumed." | |
1287 | (with-current-buffer (or buffer (current-buffer)) | |
1288 | (unless imap-namespace | |
1289 | (when (imap-capability 'NAMESPACE) | |
1290 | (imap-send-command-wait "NAMESPACE"))) | |
1291 | imap-namespace)) | |
1292 | ||
1293 | (defun imap-send-command-wait (command &optional buffer) | |
1294 | (imap-wait-for-tag (imap-send-command command buffer) buffer)) | |
1295 | ||
1296 | (defun imap-logout (&optional buffer) | |
1297 | (or buffer (setq buffer (current-buffer))) | |
1298 | (if imap-logout-timeout | |
1299 | (with-timeout (imap-logout-timeout | |
1300 | (condition-case nil | |
1301 | (with-current-buffer buffer | |
1302 | (delete-process imap-process)) | |
1303 | (error))) | |
1304 | (imap-send-command "LOGOUT" buffer)) | |
1305 | (imap-send-command "LOGOUT" buffer))) | |
1306 | ||
1307 | (defun imap-logout-wait (&optional buffer) | |
1308 | (or buffer (setq buffer (current-buffer))) | |
1309 | (if imap-logout-timeout | |
1310 | (with-timeout (imap-logout-timeout | |
1311 | (condition-case nil | |
1312 | (with-current-buffer buffer | |
1313 | (delete-process imap-process)) | |
1314 | (error))) | |
1315 | (imap-send-command-wait "LOGOUT" buffer)) | |
1316 | (imap-send-command-wait "LOGOUT" buffer))) | |
1317 | ||
1318 | \f | |
1319 | ;; Mailbox functions: | |
1320 | ||
1321 | (defun imap-mailbox-put (propname value &optional mailbox buffer) | |
1322 | (with-current-buffer (or buffer (current-buffer)) | |
1323 | (if imap-mailbox-data | |
1324 | (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data) | |
1325 | propname value) | |
1326 | (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s" | |
1327 | propname value mailbox (current-buffer))) | |
1328 | t)) | |
1329 | ||
1330 | (defsubst imap-mailbox-get-1 (propname &optional mailbox) | |
1331 | (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data) | |
1332 | propname)) | |
1333 | ||
1334 | (defun imap-mailbox-get (propname &optional mailbox buffer) | |
1335 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1336 | (with-current-buffer (or buffer (current-buffer)) | |
1337 | (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox))))) | |
1338 | ||
1339 | (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer) | |
1340 | (with-current-buffer (or buffer (current-buffer)) | |
1341 | (let (result) | |
1342 | (mapatoms | |
1343 | (lambda (s) | |
1344 | (push (funcall func (if mailbox-decoder | |
1345 | (funcall mailbox-decoder (symbol-name s)) | |
1346 | (symbol-name s))) result)) | |
1347 | imap-mailbox-data) | |
1348 | result))) | |
1349 | ||
1350 | (defun imap-mailbox-map (func &optional buffer) | |
1351 | "Map a function across each mailbox in `imap-mailbox-data', returning a list. | |
1352 | Function should take a mailbox name (a string) as | |
1353 | the only argument." | |
1354 | (imap-mailbox-map-1 func 'imap-utf7-decode buffer)) | |
1355 | ||
1356 | (defun imap-current-mailbox (&optional buffer) | |
1357 | (with-current-buffer (or buffer (current-buffer)) | |
1358 | (imap-utf7-decode imap-current-mailbox))) | |
1359 | ||
1360 | (defun imap-current-mailbox-p-1 (mailbox &optional examine) | |
1361 | (and (string= mailbox imap-current-mailbox) | |
1362 | (or (and examine | |
1363 | (eq imap-state 'examine)) | |
1364 | (and (not examine) | |
1365 | (eq imap-state 'selected))))) | |
1366 | ||
1367 | (defun imap-current-mailbox-p (mailbox &optional examine buffer) | |
1368 | (with-current-buffer (or buffer (current-buffer)) | |
1369 | (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine))) | |
1370 | ||
1371 | (defun imap-mailbox-select-1 (mailbox &optional examine) | |
1372 | "Select MAILBOX on server in BUFFER. | |
1373 | If EXAMINE is non-nil, do a read-only select." | |
1374 | (if (imap-current-mailbox-p-1 mailbox examine) | |
1375 | imap-current-mailbox | |
1376 | (setq imap-current-mailbox mailbox) | |
1377 | (if (imap-ok-p (imap-send-command-wait | |
1378 | (concat (if examine "EXAMINE" "SELECT") " \"" | |
1379 | mailbox "\""))) | |
1380 | (progn | |
1381 | (setq imap-message-data (make-vector imap-message-prime 0) | |
1382 | imap-state (if examine 'examine 'selected)) | |
1383 | imap-current-mailbox) | |
1384 | ;; Failed SELECT/EXAMINE unselects current mailbox | |
1385 | (setq imap-current-mailbox nil)))) | |
1386 | ||
1387 | (defun imap-mailbox-select (mailbox &optional examine buffer) | |
1388 | (with-current-buffer (or buffer (current-buffer)) | |
1389 | (imap-utf7-decode | |
1390 | (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine)))) | |
1391 | ||
1392 | (defun imap-mailbox-examine-1 (mailbox &optional buffer) | |
1393 | (with-current-buffer (or buffer (current-buffer)) | |
1394 | (imap-mailbox-select-1 mailbox 'examine))) | |
1395 | ||
1396 | (defun imap-mailbox-examine (mailbox &optional buffer) | |
1397 | "Examine MAILBOX on server in BUFFER." | |
1398 | (imap-mailbox-select mailbox 'examine buffer)) | |
1399 | ||
1400 | (defun imap-mailbox-unselect (&optional buffer) | |
1401 | "Close current folder in BUFFER, without expunging articles." | |
1402 | (with-current-buffer (or buffer (current-buffer)) | |
1403 | (when (or (eq imap-state 'auth) | |
1404 | (and (imap-capability 'UNSELECT) | |
1405 | (imap-ok-p (imap-send-command-wait "UNSELECT"))) | |
1406 | (and (imap-ok-p | |
1407 | (imap-send-command-wait (concat "EXAMINE \"" | |
1408 | imap-current-mailbox | |
1409 | "\""))) | |
1410 | (imap-ok-p (imap-send-command-wait "CLOSE")))) | |
1411 | (setq imap-current-mailbox nil | |
1412 | imap-message-data nil | |
1413 | imap-state 'auth) | |
1414 | t))) | |
1415 | ||
1416 | (defun imap-mailbox-expunge (&optional asynch buffer) | |
1417 | "Expunge articles in current folder in BUFFER. | |
1418 | If ASYNCH, do not wait for successful completion of the command. | |
1419 | If BUFFER is nil the current buffer is assumed." | |
1420 | (with-current-buffer (or buffer (current-buffer)) | |
1421 | (when (and imap-current-mailbox (not (eq imap-state 'examine))) | |
1422 | (if asynch | |
1423 | (imap-send-command "EXPUNGE") | |
1424 | (imap-ok-p (imap-send-command-wait "EXPUNGE")))))) | |
1425 | ||
1426 | (defun imap-mailbox-close (&optional asynch buffer) | |
1427 | "Expunge articles and close current folder in BUFFER. | |
1428 | If ASYNCH, do not wait for successful completion of the command. | |
1429 | If BUFFER is nil the current buffer is assumed." | |
1430 | (with-current-buffer (or buffer (current-buffer)) | |
1431 | (when imap-current-mailbox | |
1432 | (if asynch | |
1433 | (imap-add-callback (imap-send-command "CLOSE") | |
1434 | `(lambda (tag status) | |
1435 | (message "IMAP mailbox `%s' closed... %s" | |
1436 | imap-current-mailbox status) | |
1437 | (when (eq ,imap-current-mailbox | |
1438 | imap-current-mailbox) | |
1439 | ;; Don't wipe out data if another mailbox | |
1440 | ;; was selected... | |
1441 | (setq imap-current-mailbox nil | |
1442 | imap-message-data nil | |
1443 | imap-state 'auth)))) | |
1444 | (when (imap-ok-p (imap-send-command-wait "CLOSE")) | |
1445 | (setq imap-current-mailbox nil | |
1446 | imap-message-data nil | |
1447 | imap-state 'auth))) | |
1448 | t))) | |
1449 | ||
1450 | (defun imap-mailbox-create-1 (mailbox) | |
1451 | (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\"")))) | |
1452 | ||
1453 | (defun imap-mailbox-create (mailbox &optional buffer) | |
1454 | "Create MAILBOX on server in BUFFER. | |
1455 | If BUFFER is nil the current buffer is assumed." | |
1456 | (with-current-buffer (or buffer (current-buffer)) | |
1457 | (imap-mailbox-create-1 (imap-utf7-encode mailbox)))) | |
1458 | ||
1459 | (defun imap-mailbox-delete (mailbox &optional buffer) | |
1460 | "Delete MAILBOX on server in BUFFER. | |
1461 | If BUFFER is nil the current buffer is assumed." | |
1462 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1463 | (with-current-buffer (or buffer (current-buffer)) | |
1464 | (imap-ok-p | |
1465 | (imap-send-command-wait (list "DELETE \"" mailbox "\"")))))) | |
1466 | ||
1467 | (defun imap-mailbox-rename (oldname newname &optional buffer) | |
1468 | "Rename mailbox OLDNAME to NEWNAME on server in BUFFER. | |
1469 | If BUFFER is nil the current buffer is assumed." | |
1470 | (let ((oldname (imap-utf7-encode oldname)) | |
1471 | (newname (imap-utf7-encode newname))) | |
1472 | (with-current-buffer (or buffer (current-buffer)) | |
1473 | (imap-ok-p | |
1474 | (imap-send-command-wait (list "RENAME \"" oldname "\" " | |
1475 | "\"" newname "\"")))))) | |
1476 | ||
1477 | (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer) | |
1478 | "Return a list of subscribed mailboxes on server in BUFFER. | |
1479 | If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is | |
41013cb4 | 1480 | non-nil, a hierarchy delimiter is added to root. REFERENCE is an |
b2108a36 TZ |
1481 | implementation-specific string that has to be passed to lsub command." |
1482 | (with-current-buffer (or buffer (current-buffer)) | |
1483 | ;; Make sure we know the hierarchy separator for root's hierarchy | |
1484 | (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root))) | |
1485 | (imap-send-command-wait (concat "LIST \"" reference "\" \"" | |
1486 | (imap-utf7-encode root) "\""))) | |
1487 | ;; clear list data (NB not delimiter and other stuff) | |
1488 | (imap-mailbox-map-1 (lambda (mailbox) | |
1489 | (imap-mailbox-put 'lsub nil mailbox))) | |
1490 | (when (imap-ok-p | |
1491 | (imap-send-command-wait | |
1492 | (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root) | |
1493 | (and add-delimiter (imap-mailbox-get-1 'delimiter root)) | |
1494 | "%\""))) | |
1495 | (let (out) | |
1496 | (imap-mailbox-map-1 (lambda (mailbox) | |
1497 | (when (imap-mailbox-get-1 'lsub mailbox) | |
1498 | (push (imap-utf7-decode mailbox) out)))) | |
1499 | (nreverse out))))) | |
1500 | ||
1501 | (defun imap-mailbox-list (root &optional reference add-delimiter buffer) | |
1502 | "Return a list of mailboxes matching ROOT on server in BUFFER. | |
1503 | If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to | |
41013cb4 | 1504 | root. REFERENCE is an implementation-specific string that has to be |
b2108a36 TZ |
1505 | passed to list command." |
1506 | (with-current-buffer (or buffer (current-buffer)) | |
1507 | ;; Make sure we know the hierarchy separator for root's hierarchy | |
1508 | (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root))) | |
1509 | (imap-send-command-wait (concat "LIST \"" reference "\" \"" | |
1510 | (imap-utf7-encode root) "\""))) | |
1511 | ;; clear list data (NB not delimiter and other stuff) | |
1512 | (imap-mailbox-map-1 (lambda (mailbox) | |
1513 | (imap-mailbox-put 'list nil mailbox))) | |
1514 | (when (imap-ok-p | |
1515 | (imap-send-command-wait | |
1516 | (concat "LIST \"" reference "\" \"" (imap-utf7-encode root) | |
1517 | (and add-delimiter (imap-mailbox-get-1 'delimiter root)) | |
1518 | "%\""))) | |
1519 | (let (out) | |
1520 | (imap-mailbox-map-1 (lambda (mailbox) | |
1521 | (when (imap-mailbox-get-1 'list mailbox) | |
1522 | (push (imap-utf7-decode mailbox) out)))) | |
1523 | (nreverse out))))) | |
1524 | ||
1525 | (defun imap-mailbox-subscribe (mailbox &optional buffer) | |
1526 | "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER. | |
1527 | Returns non-nil if successful." | |
1528 | (with-current-buffer (or buffer (current-buffer)) | |
1529 | (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \"" | |
1530 | (imap-utf7-encode mailbox) | |
1531 | "\""))))) | |
1532 | ||
1533 | (defun imap-mailbox-unsubscribe (mailbox &optional buffer) | |
1534 | "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER. | |
1535 | Returns non-nil if successful." | |
1536 | (with-current-buffer (or buffer (current-buffer)) | |
1537 | (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE " | |
1538 | (imap-utf7-encode mailbox) | |
1539 | "\""))))) | |
1540 | ||
1541 | (defun imap-mailbox-status (mailbox items &optional buffer) | |
1542 | "Get status items ITEM in MAILBOX from server in BUFFER. | |
1543 | ITEMS can be a symbol or a list of symbols, valid symbols are one of | |
1544 | the STATUS data items -- i.e. `messages', `recent', `uidnext', `uidvalidity', | |
1545 | or `unseen'. If ITEMS is a list of symbols, a list of values is | |
1546 | returned, if ITEMS is a symbol only its value is returned." | |
1547 | (with-current-buffer (or buffer (current-buffer)) | |
1548 | (when (imap-ok-p | |
1549 | (imap-send-command-wait (list "STATUS \"" | |
1550 | (imap-utf7-encode mailbox) | |
1551 | "\" " | |
1552 | (upcase | |
1553 | (format "%s" | |
1554 | (if (listp items) | |
1555 | items | |
1556 | (list items))))))) | |
1557 | (if (listp items) | |
1558 | (mapcar (lambda (item) | |
1559 | (imap-mailbox-get item mailbox)) | |
1560 | items) | |
1561 | (imap-mailbox-get items mailbox))))) | |
1562 | ||
1563 | (defun imap-mailbox-status-asynch (mailbox items &optional buffer) | |
41013cb4 | 1564 | "Send status item requests ITEMS on MAILBOX to server in BUFFER. |
b2108a36 TZ |
1565 | ITEMS can be a symbol or a list of symbols, valid symbols are one of |
1566 | the STATUS data items -- i.e. 'messages, 'recent, 'uidnext, 'uidvalidity | |
1567 | or 'unseen. The IMAP command tag is returned." | |
1568 | (with-current-buffer (or buffer (current-buffer)) | |
1569 | (imap-send-command (list "STATUS \"" | |
1570 | (imap-utf7-encode mailbox) | |
1571 | "\" " | |
1572 | (upcase | |
1573 | (format "%s" | |
1574 | (if (listp items) | |
1575 | items | |
1576 | (list items)))))))) | |
1577 | ||
1578 | (defun imap-mailbox-acl-get (&optional mailbox buffer) | |
1579 | "Get ACL on MAILBOX from server in BUFFER." | |
1580 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1581 | (with-current-buffer (or buffer (current-buffer)) | |
1582 | (when (imap-ok-p | |
1583 | (imap-send-command-wait (list "GETACL \"" | |
1584 | (or mailbox imap-current-mailbox) | |
1585 | "\""))) | |
1586 | (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox)))))) | |
1587 | ||
1588 | (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer) | |
1589 | "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER." | |
1590 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1591 | (with-current-buffer (or buffer (current-buffer)) | |
1592 | (imap-ok-p | |
1593 | (imap-send-command-wait (list "SETACL \"" | |
1594 | (or mailbox imap-current-mailbox) | |
1595 | "\" " | |
1596 | identifier | |
1597 | " " | |
1598 | rights)))))) | |
1599 | ||
1600 | (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer) | |
41013cb4 | 1601 | "Remove <id,rights> pairs for IDENTIFIER from MAILBOX on server in BUFFER." |
b2108a36 TZ |
1602 | (let ((mailbox (imap-utf7-encode mailbox))) |
1603 | (with-current-buffer (or buffer (current-buffer)) | |
1604 | (imap-ok-p | |
1605 | (imap-send-command-wait (list "DELETEACL \"" | |
1606 | (or mailbox imap-current-mailbox) | |
1607 | "\" " | |
1608 | identifier)))))) | |
1609 | ||
1610 | \f | |
1611 | ;; Message functions: | |
1612 | ||
1613 | (defun imap-current-message (&optional buffer) | |
1614 | (with-current-buffer (or buffer (current-buffer)) | |
1615 | imap-current-message)) | |
1616 | ||
1617 | (defun imap-list-to-message-set (list) | |
1618 | (mapconcat (lambda (item) | |
1619 | (number-to-string item)) | |
1620 | (if (listp list) | |
1621 | list | |
1622 | (list list)) | |
1623 | ",")) | |
1624 | ||
1625 | (defun imap-range-to-message-set (range) | |
1626 | (mapconcat | |
1627 | (lambda (item) | |
1628 | (if (consp item) | |
1629 | (format "%d:%d" | |
1630 | (car item) (cdr item)) | |
1631 | (format "%d" item))) | |
1632 | (if (and (listp range) (not (listp (cdr range)))) | |
1633 | (list range) ;; make (1 . 2) into ((1 . 2)) | |
1634 | range) | |
1635 | ",")) | |
1636 | ||
1637 | (defun imap-fetch-asynch (uids props &optional nouidfetch buffer) | |
1638 | (with-current-buffer (or buffer (current-buffer)) | |
1639 | (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ") | |
1640 | (if (listp uids) | |
1641 | (imap-list-to-message-set uids) | |
1642 | uids) | |
1643 | props)))) | |
1644 | ||
1645 | (defun imap-fetch (uids props &optional receive nouidfetch buffer) | |
1646 | "Fetch properties PROPS from message set UIDS from server in BUFFER. | |
41013cb4 MK |
1647 | UIDS can be a string, number or a list of numbers. If RECEIVE is |
1648 | non-nil, return these properties." | |
b2108a36 TZ |
1649 | (with-current-buffer (or buffer (current-buffer)) |
1650 | (when (imap-ok-p (imap-send-command-wait | |
1651 | (format "%sFETCH %s %s" (if nouidfetch "" "UID ") | |
1652 | (if (listp uids) | |
1653 | (imap-list-to-message-set uids) | |
1654 | uids) | |
1655 | props))) | |
1656 | (if (or (null receive) (stringp uids)) | |
1657 | t | |
1658 | (if (listp uids) | |
1659 | (mapcar (lambda (uid) | |
1660 | (if (listp receive) | |
1661 | (mapcar (lambda (prop) | |
1662 | (imap-message-get uid prop)) | |
1663 | receive) | |
1664 | (imap-message-get uid receive))) | |
1665 | uids) | |
1666 | (imap-message-get uids receive)))))) | |
1667 | ||
1668 | (defun imap-message-put (uid propname value &optional buffer) | |
1669 | (with-current-buffer (or buffer (current-buffer)) | |
1670 | (if imap-message-data | |
1671 | (put (intern (number-to-string uid) imap-message-data) | |
1672 | propname value) | |
1673 | (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s" | |
1674 | uid propname value (current-buffer))) | |
1675 | t)) | |
1676 | ||
1677 | (defun imap-message-get (uid propname &optional buffer) | |
1678 | (with-current-buffer (or buffer (current-buffer)) | |
1679 | (get (intern-soft (number-to-string uid) imap-message-data) | |
1680 | propname))) | |
1681 | ||
1682 | (defun imap-message-map (func propname &optional buffer) | |
1683 | "Map a function across each message in `imap-message-data', returning a list." | |
1684 | (with-current-buffer (or buffer (current-buffer)) | |
1685 | (let (result) | |
1686 | (mapatoms | |
1687 | (lambda (s) | |
1688 | (push (funcall func (get s 'UID) (get s propname)) result)) | |
1689 | imap-message-data) | |
1690 | result))) | |
1691 | ||
1692 | (defmacro imap-message-envelope-date (uid &optional buffer) | |
1693 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1694 | (elt (imap-message-get ,uid 'ENVELOPE) 0))) | |
1695 | ||
1696 | (defmacro imap-message-envelope-subject (uid &optional buffer) | |
1697 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1698 | (elt (imap-message-get ,uid 'ENVELOPE) 1))) | |
1699 | ||
1700 | (defmacro imap-message-envelope-from (uid &optional buffer) | |
1701 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1702 | (elt (imap-message-get ,uid 'ENVELOPE) 2))) | |
1703 | ||
1704 | (defmacro imap-message-envelope-sender (uid &optional buffer) | |
1705 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1706 | (elt (imap-message-get ,uid 'ENVELOPE) 3))) | |
1707 | ||
1708 | (defmacro imap-message-envelope-reply-to (uid &optional buffer) | |
1709 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1710 | (elt (imap-message-get ,uid 'ENVELOPE) 4))) | |
1711 | ||
1712 | (defmacro imap-message-envelope-to (uid &optional buffer) | |
1713 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1714 | (elt (imap-message-get ,uid 'ENVELOPE) 5))) | |
1715 | ||
1716 | (defmacro imap-message-envelope-cc (uid &optional buffer) | |
1717 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1718 | (elt (imap-message-get ,uid 'ENVELOPE) 6))) | |
1719 | ||
1720 | (defmacro imap-message-envelope-bcc (uid &optional buffer) | |
1721 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1722 | (elt (imap-message-get ,uid 'ENVELOPE) 7))) | |
1723 | ||
1724 | (defmacro imap-message-envelope-in-reply-to (uid &optional buffer) | |
1725 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1726 | (elt (imap-message-get ,uid 'ENVELOPE) 8))) | |
1727 | ||
1728 | (defmacro imap-message-envelope-message-id (uid &optional buffer) | |
1729 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1730 | (elt (imap-message-get ,uid 'ENVELOPE) 9))) | |
1731 | ||
1732 | (defmacro imap-message-body (uid &optional buffer) | |
1733 | `(with-current-buffer (or ,buffer (current-buffer)) | |
1734 | (imap-message-get ,uid 'BODY))) | |
1735 | ||
1736 | ;; FIXME: Should this try to use CHARSET? -- fx | |
1737 | (defun imap-search (predicate &optional buffer) | |
1738 | (with-current-buffer (or buffer (current-buffer)) | |
1739 | (imap-mailbox-put 'search 'dummy) | |
1740 | (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate))) | |
1741 | (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy) | |
1742 | (progn | |
1743 | (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...") | |
1744 | nil) | |
1745 | (imap-mailbox-get-1 'search imap-current-mailbox))))) | |
1746 | ||
1747 | (defun imap-message-flag-permanent-p (flag &optional mailbox buffer) | |
41013cb4 MK |
1748 | "Return t if FLAG can be permanently saved on articles. |
1749 | MAILBOX specifies a mailbox on the server in BUFFER." | |
b2108a36 TZ |
1750 | (with-current-buffer (or buffer (current-buffer)) |
1751 | (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox)) | |
1752 | (member flag (imap-mailbox-get 'permanentflags mailbox))))) | |
1753 | ||
1754 | (defun imap-message-flags-set (articles flags &optional silent buffer) | |
1755 | (when (and articles flags) | |
1756 | (with-current-buffer (or buffer (current-buffer)) | |
1757 | (imap-ok-p (imap-send-command-wait | |
1758 | (concat "UID STORE " articles | |
1759 | " FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1760 | ||
1761 | (defun imap-message-flags-del (articles flags &optional silent buffer) | |
1762 | (when (and articles flags) | |
1763 | (with-current-buffer (or buffer (current-buffer)) | |
1764 | (imap-ok-p (imap-send-command-wait | |
1765 | (concat "UID STORE " articles | |
1766 | " -FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1767 | ||
1768 | (defun imap-message-flags-add (articles flags &optional silent buffer) | |
1769 | (when (and articles flags) | |
1770 | (with-current-buffer (or buffer (current-buffer)) | |
1771 | (imap-ok-p (imap-send-command-wait | |
1772 | (concat "UID STORE " articles | |
1773 | " +FLAGS" (if silent ".SILENT") " (" flags ")")))))) | |
1774 | ||
1775 | ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/65317/focus=65343 | |
1776 | ;; Signal an error if we'd get an integer overflow. | |
1777 | ;; | |
1778 | ;; FIXME: Identify relevant calls to `string-to-number' and replace them with | |
1779 | ;; `imap-string-to-integer'. | |
1780 | (defun imap-string-to-integer (string &optional base) | |
1781 | (let ((number (string-to-number string base))) | |
1782 | (if (> number most-positive-fixnum) | |
1783 | (error | |
1784 | (format "String %s cannot be converted to a Lisp integer" number)) | |
1785 | number))) | |
1786 | ||
1787 | (defun imap-fetch-safe (uids props &optional receive nouidfetch buffer) | |
1788 | "Like `imap-fetch', but DTRT with Exchange 2007 bug. | |
1789 | However, UIDS here is a cons, where the car is the canonical form | |
1790 | of the UIDS specification, and the cdr is the one which works with | |
1791 | Exchange 2007 or, potentially, other buggy servers. | |
1792 | See `imap-enable-exchange-bug-workaround'." | |
1793 | ;; The first time we get here for a given, we'll try the canonical | |
1794 | ;; form. If we get the known error from the buggy server, set the | |
1795 | ;; flag buffer-locally (to account for connections to multiple | |
1796 | ;; servers), then re-try with the alternative UIDS spec. We don't | |
1797 | ;; unconditionally use the alternative form, since the | |
1798 | ;; currently-used alternatives are seriously inefficient with some | |
1799 | ;; servers (although they are valid). | |
1800 | ;; | |
1801 | ;; FIXME: Maybe it would be cleaner to have a flag to not signal | |
1802 | ;; the error (which otherwise gives a message), and test | |
1803 | ;; `imap-failed-tags'. Also, Other IMAP clients use other forms of | |
1804 | ;; request which work with Exchange, e.g. Claws does "UID FETCH 1:* | |
1805 | ;; (UID)" rather than "FETCH UID 1,*". Is there a good reason not | |
1806 | ;; to do the same? | |
1807 | (condition-case data | |
1808 | ;; Binding `debug-on-error' allows us to get the error from | |
1809 | ;; `imap-parse-response' -- it's normally caught by Emacs around | |
1810 | ;; execution of a process filter. | |
1811 | (let ((debug-on-error t)) | |
1812 | (imap-fetch (if imap-enable-exchange-bug-workaround | |
1813 | (cdr uids) | |
1814 | (car uids)) | |
1815 | props receive nouidfetch buffer)) | |
1816 | (error | |
1817 | (if (and (not imap-enable-exchange-bug-workaround) | |
1818 | ;; This is the Exchange 2007 response. It may be more | |
1819 | ;; robust just to check for a BAD response to the | |
1820 | ;; attempted fetch. | |
1821 | (string-match "The specified message set is invalid" | |
1822 | (cadr data))) | |
1823 | (with-current-buffer (or buffer (current-buffer)) | |
1824 | (set (make-local-variable 'imap-enable-exchange-bug-workaround) | |
1825 | t) | |
1826 | (imap-fetch (cdr uids) props receive nouidfetch)) | |
1827 | (signal (car data) (cdr data)))))) | |
1828 | ||
1829 | (defun imap-message-copyuid-1 (mailbox) | |
1830 | (if (imap-capability 'UIDPLUS) | |
1831 | (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox)) | |
1832 | (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox)))) | |
1833 | (let ((old-mailbox imap-current-mailbox) | |
1834 | (state imap-state) | |
1835 | (imap-message-data (make-vector 2 0))) | |
1836 | (when (imap-mailbox-examine-1 mailbox) | |
1837 | (prog1 | |
1838 | (and (imap-fetch-safe '("*" . "*:*") "UID") | |
1839 | (list (imap-mailbox-get-1 'uidvalidity mailbox) | |
1840 | (apply 'max (imap-message-map | |
6c42fc3e | 1841 | (lambda (uid _prop) uid) 'UID)))) |
b2108a36 TZ |
1842 | (if old-mailbox |
1843 | (imap-mailbox-select old-mailbox (eq state 'examine)) | |
1844 | (imap-mailbox-unselect))))))) | |
1845 | ||
1846 | (defun imap-message-copyuid (mailbox &optional buffer) | |
1847 | (with-current-buffer (or buffer (current-buffer)) | |
1848 | (imap-message-copyuid-1 (imap-utf7-decode mailbox)))) | |
1849 | ||
1850 | (defun imap-message-copy (articles mailbox | |
1851 | &optional dont-create no-copyuid buffer) | |
1852 | "Copy ARTICLES to MAILBOX on server in BUFFER. | |
1853 | ARTICLES is a string message set. Create mailbox if it doesn't exist, | |
1854 | unless DONT-CREATE is non-nil. On success, return a list with | |
1855 | the UIDVALIDITY of the mailbox the article(s) was copied to as the | |
1856 | first element. The rest of list contains the saved articles' UIDs." | |
1857 | (when articles | |
1858 | (with-current-buffer (or buffer (current-buffer)) | |
1859 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1860 | (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\"")) | |
1861 | (imap-current-target-mailbox mailbox)) | |
1862 | (if (imap-ok-p (imap-send-command-wait cmd)) | |
1863 | t | |
1864 | (when (and (not dont-create) | |
1865 | ;; removed because of buggy Oracle server | |
1866 | ;; that doesn't send TRYCREATE tags (which | |
1867 | ;; is a MUST according to specifications): | |
1868 | ;;(imap-mailbox-get-1 'trycreate mailbox) | |
1869 | (imap-mailbox-create-1 mailbox)) | |
1870 | (imap-ok-p (imap-send-command-wait cmd))))) | |
1871 | (or no-copyuid | |
1872 | (imap-message-copyuid-1 mailbox))))))) | |
1873 | ||
1874 | ;; FIXME: Amalgamate with imap-message-copyuid-1, using an extra arg, since it | |
1875 | ;; shares most of the code? -- fx | |
1876 | (defun imap-message-appenduid-1 (mailbox) | |
1877 | (if (imap-capability 'UIDPLUS) | |
1878 | (imap-mailbox-get-1 'appenduid mailbox) | |
1879 | (let ((old-mailbox imap-current-mailbox) | |
1880 | (state imap-state) | |
1881 | (imap-message-data (make-vector 2 0))) | |
1882 | (when (imap-mailbox-examine-1 mailbox) | |
1883 | (prog1 | |
1884 | (and (imap-fetch-safe '("*" . "*:*") "UID") | |
1885 | (list (imap-mailbox-get-1 'uidvalidity mailbox) | |
1886 | (apply 'max (imap-message-map | |
6c42fc3e | 1887 | (lambda (uid _prop) uid) 'UID)))) |
b2108a36 TZ |
1888 | (if old-mailbox |
1889 | (imap-mailbox-select old-mailbox (eq state 'examine)) | |
1890 | (imap-mailbox-unselect))))))) | |
1891 | ||
1892 | (defun imap-message-appenduid (mailbox &optional buffer) | |
1893 | (with-current-buffer (or buffer (current-buffer)) | |
1894 | (imap-message-appenduid-1 (imap-utf7-encode mailbox)))) | |
1895 | ||
6c42fc3e | 1896 | (defun imap-message-append (mailbox article &optional _flags _date-time buffer) |
b2108a36 TZ |
1897 | "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER. |
1898 | FLAGS and DATE-TIME is currently not used. Return a cons holding | |
1899 | uidvalidity of MAILBOX and UID the newly created article got, or nil | |
1900 | on failure." | |
1901 | (let ((mailbox (imap-utf7-encode mailbox))) | |
1902 | (with-current-buffer (or buffer (current-buffer)) | |
1903 | (and (let ((imap-current-target-mailbox mailbox)) | |
1904 | (imap-ok-p | |
1905 | (imap-send-command-wait | |
1906 | (list "APPEND \"" mailbox "\" " article)))) | |
1907 | (imap-message-appenduid-1 mailbox))))) | |
1908 | ||
1909 | (defun imap-body-lines (body) | |
1910 | "Return number of lines in article by looking at the mime bodystructure BODY." | |
1911 | (if (listp body) | |
1912 | (if (stringp (car body)) | |
1913 | (cond ((and (string= (upcase (car body)) "TEXT") | |
1914 | (numberp (nth 7 body))) | |
1915 | (nth 7 body)) | |
1916 | ((and (string= (upcase (car body)) "MESSAGE") | |
1917 | (numberp (nth 9 body))) | |
1918 | (nth 9 body)) | |
1919 | (t 0)) | |
1920 | (apply '+ (mapcar 'imap-body-lines body))) | |
1921 | 0)) | |
1922 | ||
1923 | (defun imap-envelope-from (from) | |
41013cb4 | 1924 | "Return a FROM string line." |
b2108a36 TZ |
1925 | (and from |
1926 | (concat (aref from 0) | |
1927 | (if (aref from 0) " <") | |
1928 | (aref from 2) | |
1929 | "@" | |
1930 | (aref from 3) | |
1931 | (if (aref from 0) ">")))) | |
1932 | ||
1933 | \f | |
1934 | ;; Internal functions. | |
1935 | ||
1936 | (defun imap-add-callback (tag func) | |
1937 | (setq imap-callbacks (append (list (cons tag func)) imap-callbacks))) | |
1938 | ||
1939 | (defun imap-send-command-1 (cmdstr) | |
1940 | (setq cmdstr (concat cmdstr imap-client-eol)) | |
1941 | (imap-log cmdstr) | |
1942 | (process-send-string imap-process cmdstr)) | |
1943 | ||
1944 | (defun imap-send-command (command &optional buffer) | |
1945 | (with-current-buffer (or buffer (current-buffer)) | |
1946 | (if (not (listp command)) (setq command (list command))) | |
1947 | (let ((tag (setq imap-tag (1+ imap-tag))) | |
1948 | cmd cmdstr) | |
1949 | (setq cmdstr (concat (number-to-string imap-tag) " ")) | |
1950 | (while (setq cmd (pop command)) | |
1951 | (cond ((stringp cmd) | |
1952 | (setq cmdstr (concat cmdstr cmd))) | |
1953 | ((bufferp cmd) | |
1954 | (let ((eol imap-client-eol) | |
1955 | (calcfirst imap-calculate-literal-size-first) | |
1956 | size) | |
1957 | (with-current-buffer cmd | |
1958 | (if calcfirst | |
1959 | (setq size (buffer-size))) | |
1960 | (when (not (equal eol "\r\n")) | |
1961 | ;; XXX modifies buffer! | |
1962 | (goto-char (point-min)) | |
1963 | (while (search-forward "\r\n" nil t) | |
1964 | (replace-match eol))) | |
1965 | (if (not calcfirst) | |
1966 | (setq size (buffer-size)))) | |
1967 | (setq cmdstr | |
1968 | (concat cmdstr (format "{%d}" size)))) | |
1969 | (unwind-protect | |
1970 | (progn | |
1971 | (imap-send-command-1 cmdstr) | |
1972 | (setq cmdstr nil) | |
1973 | (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
1974 | (setq command nil) ;; abort command if no cont-req | |
1975 | (let ((process imap-process) | |
1976 | (stream imap-stream) | |
1977 | (eol imap-client-eol)) | |
1978 | (with-current-buffer cmd | |
1979 | (imap-log cmd) | |
1980 | (process-send-region process (point-min) | |
1981 | (point-max))) | |
1982 | (process-send-string process imap-client-eol)))) | |
1983 | (setq imap-continuation nil))) | |
1984 | ((functionp cmd) | |
1985 | (imap-send-command-1 cmdstr) | |
1986 | (setq cmdstr nil) | |
1987 | (unwind-protect | |
1988 | (setq command | |
1989 | (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE)) | |
1990 | nil ;; abort command if no cont-req | |
1991 | (cons (funcall cmd imap-continuation) | |
1992 | command))) | |
1993 | (setq imap-continuation nil))) | |
1994 | (t | |
1995 | (error "Unknown command type")))) | |
1996 | (if cmdstr | |
1997 | (imap-send-command-1 cmdstr)) | |
1998 | tag))) | |
1999 | ||
2000 | (defun imap-wait-for-tag (tag &optional buffer) | |
2001 | (with-current-buffer (or buffer (current-buffer)) | |
2002 | (let (imap-have-messaged) | |
2003 | (while (and (null imap-continuation) | |
2004 | (memq (process-status imap-process) '(open run)) | |
2005 | (< imap-reached-tag tag)) | |
2006 | (let ((len (/ (buffer-size) 1024)) | |
2007 | message-log-max) | |
2008 | (unless (< len 10) | |
2009 | (setq imap-have-messaged t) | |
2010 | (message "imap read: %dk" len)) | |
2011 | (accept-process-output imap-process | |
2012 | (truncate imap-read-timeout) | |
2013 | (truncate (* (- imap-read-timeout | |
2014 | (truncate imap-read-timeout)) | |
2015 | 1000))))) | |
2016 | ;; A process can die _before_ we have processed everything it | |
2017 | ;; has to say. Moreover, this can happen in between the call to | |
2018 | ;; accept-process-output and the call to process-status in an | |
2019 | ;; iteration of the loop above. | |
2020 | (when (and (null imap-continuation) | |
2021 | (< imap-reached-tag tag)) | |
2022 | (accept-process-output imap-process 0 0)) | |
2023 | (when imap-have-messaged | |
2024 | (message "")) | |
2025 | (and (memq (process-status imap-process) '(open run)) | |
2026 | (or (assq tag imap-failed-tags) | |
2027 | (if imap-continuation | |
2028 | 'INCOMPLETE | |
2029 | 'OK)))))) | |
2030 | ||
2031 | (defun imap-sentinel (process string) | |
2032 | (delete-process process)) | |
2033 | ||
2034 | (defun imap-find-next-line () | |
2035 | "Return point at end of current line, taking into account literals. | |
2036 | Return nil if no complete line has arrived." | |
2037 | (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}" | |
2038 | imap-server-eol) | |
2039 | nil t) | |
2040 | (if (match-string 1) | |
2041 | (if (< (point-max) (+ (point) (string-to-number (match-string 1)))) | |
2042 | nil | |
2043 | (goto-char (+ (point) (string-to-number (match-string 1)))) | |
2044 | (imap-find-next-line)) | |
2045 | (point)))) | |
2046 | ||
2047 | (defun imap-arrival-filter (proc string) | |
2048 | "IMAP process filter." | |
2049 | ;; Sometimes, we are called even though the process has died. | |
2050 | ;; Better abstain from doing stuff in that case. | |
2051 | (when (buffer-name (process-buffer proc)) | |
2052 | (with-current-buffer (process-buffer proc) | |
2053 | (goto-char (point-max)) | |
2054 | (insert string) | |
2055 | (imap-log string) | |
2056 | (let (end) | |
2057 | (goto-char (point-min)) | |
2058 | (while (setq end (imap-find-next-line)) | |
2059 | (save-restriction | |
2060 | (narrow-to-region (point-min) end) | |
2061 | (delete-char (- (length imap-server-eol))) | |
2062 | (goto-char (point-min)) | |
2063 | (unwind-protect | |
2064 | (cond ((eq imap-state 'initial) | |
2065 | (imap-parse-greeting)) | |
2066 | ((or (eq imap-state 'auth) | |
2067 | (eq imap-state 'nonauth) | |
2068 | (eq imap-state 'selected) | |
2069 | (eq imap-state 'examine)) | |
2070 | (imap-parse-response)) | |
2071 | (t | |
2072 | (message "Unknown state %s in arrival filter" | |
2073 | imap-state))) | |
2074 | (delete-region (point-min) (point-max))))))))) | |
2075 | ||
2076 | \f | |
2077 | ;; Imap parser. | |
2078 | ||
2079 | (defsubst imap-forward () | |
2080 | (or (eobp) (forward-char))) | |
2081 | ||
2082 | ;; number = 1*DIGIT | |
2083 | ;; ; Unsigned 32-bit integer | |
2084 | ;; ; (0 <= n < 4,294,967,296) | |
2085 | ||
2086 | (defsubst imap-parse-number () | |
2087 | (when (looking-at "[0-9]+") | |
2088 | (prog1 | |
2089 | (string-to-number (match-string 0)) | |
2090 | (goto-char (match-end 0))))) | |
2091 | ||
2092 | ;; literal = "{" number "}" CRLF *CHAR8 | |
2093 | ;; ; Number represents the number of CHAR8s | |
2094 | ||
2095 | (defsubst imap-parse-literal () | |
2096 | (when (looking-at "{\\([0-9]+\\)}\r\n") | |
2097 | (let ((pos (match-end 0)) | |
2098 | (len (string-to-number (match-string 1)))) | |
2099 | (if (< (point-max) (+ pos len)) | |
2100 | nil | |
2101 | (goto-char (+ pos len)) | |
2102 | (buffer-substring pos (+ pos len)))))) | |
2103 | ||
2104 | ;; string = quoted / literal | |
2105 | ;; | |
2106 | ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE | |
2107 | ;; | |
2108 | ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> / | |
2109 | ;; "\" quoted-specials | |
2110 | ;; | |
2111 | ;; quoted-specials = DQUOTE / "\" | |
2112 | ;; | |
2113 | ;; TEXT-CHAR = <any CHAR except CR and LF> | |
2114 | ||
2115 | (defsubst imap-parse-string () | |
2116 | (cond ((eq (char-after) ?\") | |
2117 | (forward-char 1) | |
2118 | (let ((p (point)) (name "")) | |
2119 | (skip-chars-forward "^\"\\\\") | |
2120 | (setq name (buffer-substring p (point))) | |
2121 | (while (eq (char-after) ?\\) | |
2122 | (setq p (1+ (point))) | |
2123 | (forward-char 2) | |
2124 | (skip-chars-forward "^\"\\\\") | |
2125 | (setq name (concat name (buffer-substring p (point))))) | |
2126 | (forward-char 1) | |
2127 | name)) | |
2128 | ((eq (char-after) ?{) | |
2129 | (imap-parse-literal)))) | |
2130 | ||
2131 | ;; nil = "NIL" | |
2132 | ||
2133 | (defsubst imap-parse-nil () | |
2134 | (if (looking-at "NIL") | |
2135 | (goto-char (match-end 0)))) | |
2136 | ||
2137 | ;; nstring = string / nil | |
2138 | ||
2139 | (defsubst imap-parse-nstring () | |
2140 | (or (imap-parse-string) | |
2141 | (and (imap-parse-nil) | |
2142 | nil))) | |
2143 | ||
2144 | ;; astring = atom / string | |
2145 | ;; | |
2146 | ;; atom = 1*ATOM-CHAR | |
2147 | ;; | |
2148 | ;; ATOM-CHAR = <any CHAR except atom-specials> | |
2149 | ;; | |
2150 | ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards / | |
2151 | ;; quoted-specials | |
2152 | ;; | |
2153 | ;; list-wildcards = "%" / "*" | |
2154 | ;; | |
2155 | ;; quoted-specials = DQUOTE / "\" | |
2156 | ||
2157 | (defsubst imap-parse-astring () | |
2158 | (or (imap-parse-string) | |
2159 | (buffer-substring (point) | |
2160 | (if (re-search-forward "[(){ \r\n%*\"\\]" nil t) | |
2161 | (goto-char (1- (match-end 0))) | |
2162 | (end-of-line) | |
2163 | (point))))) | |
2164 | ||
2165 | ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP | |
2166 | ;; addr-host ")" | |
2167 | ;; | |
2168 | ;; addr-adl = nstring | |
2169 | ;; ; Holds route from [RFC-822] route-addr if | |
2170 | ;; ; non-nil | |
2171 | ;; | |
2172 | ;; addr-host = nstring | |
2173 | ;; ; nil indicates [RFC-822] group syntax. | |
2174 | ;; ; Otherwise, holds [RFC-822] domain name | |
2175 | ;; | |
2176 | ;; addr-mailbox = nstring | |
2177 | ;; ; nil indicates end of [RFC-822] group; if | |
2178 | ;; ; non-nil and addr-host is nil, holds | |
2179 | ;; ; [RFC-822] group name. | |
2180 | ;; ; Otherwise, holds [RFC-822] local-part | |
2181 | ;; ; after removing [RFC-822] quoting | |
2182 | ;; | |
2183 | ;; addr-name = nstring | |
2184 | ;; ; If non-nil, holds phrase from [RFC-822] | |
2185 | ;; ; mailbox after removing [RFC-822] quoting | |
2186 | ;; | |
2187 | ||
2188 | (defsubst imap-parse-address () | |
2189 | (let (address) | |
2190 | (when (eq (char-after) ?\() | |
2191 | (imap-forward) | |
2192 | (setq address (vector (prog1 (imap-parse-nstring) | |
2193 | (imap-forward)) | |
2194 | (prog1 (imap-parse-nstring) | |
2195 | (imap-forward)) | |
2196 | (prog1 (imap-parse-nstring) | |
2197 | (imap-forward)) | |
2198 | (imap-parse-nstring))) | |
2199 | (when (eq (char-after) ?\)) | |
2200 | (imap-forward) | |
2201 | address)))) | |
2202 | ||
2203 | ;; address-list = "(" 1*address ")" / nil | |
2204 | ;; | |
2205 | ;; nil = "NIL" | |
2206 | ||
2207 | (defsubst imap-parse-address-list () | |
2208 | (if (eq (char-after) ?\() | |
2209 | (let (address addresses) | |
2210 | (imap-forward) | |
2211 | (while (and (not (eq (char-after) ?\))) | |
2212 | ;; next line for MS Exchange bug | |
2213 | (progn (and (eq (char-after) ? ) (imap-forward)) t) | |
2214 | (setq address (imap-parse-address))) | |
2215 | (setq addresses (cons address addresses))) | |
2216 | (when (eq (char-after) ?\)) | |
2217 | (imap-forward) | |
2218 | (nreverse addresses))) | |
2219 | ;; With assert, the code might not be eval'd. | |
2220 | ;; (assert (imap-parse-nil) t "In imap-parse-address-list") | |
2221 | (imap-parse-nil))) | |
2222 | ||
2223 | ;; mailbox = "INBOX" / astring | |
2224 | ;; ; INBOX is case-insensitive. All case variants of | |
2225 | ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX | |
2226 | ;; ; not as an astring. An astring which consists of | |
2227 | ;; ; the case-insensitive sequence "I" "N" "B" "O" "X" | |
2228 | ;; ; is considered to be INBOX and not an astring. | |
2229 | ;; ; Refer to section 5.1 for further | |
2230 | ;; ; semantic details of mailbox names. | |
2231 | ||
2232 | (defsubst imap-parse-mailbox () | |
2233 | (let ((mailbox (imap-parse-astring))) | |
2234 | (if (string-equal "INBOX" (upcase mailbox)) | |
2235 | "INBOX" | |
2236 | mailbox))) | |
2237 | ||
2238 | ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF | |
2239 | ;; | |
2240 | ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text | |
2241 | ;; ; Authentication condition | |
2242 | ;; | |
2243 | ;; resp-cond-bye = "BYE" SP resp-text | |
2244 | ||
2245 | (defun imap-parse-greeting () | |
2246 | "Parse an IMAP greeting." | |
2247 | (cond ((looking-at "\\* OK ") | |
2248 | (setq imap-state 'nonauth)) | |
2249 | ((looking-at "\\* PREAUTH ") | |
2250 | (setq imap-state 'auth)) | |
2251 | ((looking-at "\\* BYE ") | |
2252 | (setq imap-state 'closed)))) | |
2253 | ||
2254 | ;; response = *(continue-req / response-data) response-done | |
2255 | ;; | |
2256 | ;; continue-req = "+" SP (resp-text / base64) CRLF | |
2257 | ;; | |
2258 | ;; response-data = "*" SP (resp-cond-state / resp-cond-bye / | |
2259 | ;; mailbox-data / message-data / capability-data) CRLF | |
2260 | ;; | |
2261 | ;; response-done = response-tagged / response-fatal | |
2262 | ;; | |
2263 | ;; response-fatal = "*" SP resp-cond-bye CRLF | |
2264 | ;; ; Server closes connection immediately | |
2265 | ;; | |
2266 | ;; response-tagged = tag SP resp-cond-state CRLF | |
2267 | ;; | |
2268 | ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text | |
2269 | ;; ; Status condition | |
2270 | ;; | |
2271 | ;; resp-cond-bye = "BYE" SP resp-text | |
2272 | ;; | |
2273 | ;; mailbox-data = "FLAGS" SP flag-list / | |
2274 | ;; "LIST" SP mailbox-list / | |
2275 | ;; "LSUB" SP mailbox-list / | |
2276 | ;; "SEARCH" *(SP nz-number) / | |
2277 | ;; "STATUS" SP mailbox SP "(" | |
2278 | ;; [status-att SP number *(SP status-att SP number)] ")" / | |
2279 | ;; number SP "EXISTS" / | |
2280 | ;; number SP "RECENT" | |
2281 | ;; | |
2282 | ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att)) | |
2283 | ;; | |
2284 | ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1" | |
2285 | ;; *(SP capability) | |
2286 | ;; ; IMAP4rev1 servers which offer RFC 1730 | |
2287 | ;; ; compatibility MUST list "IMAP4" as the first | |
2288 | ;; ; capability. | |
2289 | ||
2290 | (defun imap-parse-response () | |
41013cb4 | 2291 | "Parse an IMAP command response." |
b2108a36 TZ |
2292 | (let (token) |
2293 | (case (setq token (read (current-buffer))) | |
2294 | (+ (setq imap-continuation | |
2295 | (or (buffer-substring (min (point-max) (1+ (point))) | |
2296 | (point-max)) | |
2297 | t))) | |
2298 | (* (case (prog1 (setq token (read (current-buffer))) | |
2299 | (imap-forward)) | |
2300 | (OK (imap-parse-resp-text)) | |
2301 | (NO (imap-parse-resp-text)) | |
2302 | (BAD (imap-parse-resp-text)) | |
2303 | (BYE (imap-parse-resp-text)) | |
2304 | (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list))) | |
2305 | (LIST (imap-parse-data-list 'list)) | |
2306 | (LSUB (imap-parse-data-list 'lsub)) | |
2307 | (SEARCH (imap-mailbox-put | |
2308 | 'search | |
2309 | (read (concat "(" (buffer-substring (point) (point-max)) ")")))) | |
2310 | (STATUS (imap-parse-status)) | |
2311 | (CAPABILITY (setq imap-capability | |
2312 | (read (concat "(" (upcase (buffer-substring | |
2313 | (point) (point-max))) | |
2314 | ")")))) | |
2315 | (ID (setq imap-id (read (buffer-substring (point) | |
2316 | (point-max))))) | |
2317 | (ACL (imap-parse-acl)) | |
2318 | (t (case (prog1 (read (current-buffer)) | |
2319 | (imap-forward)) | |
2320 | (EXISTS (imap-mailbox-put 'exists token)) | |
2321 | (RECENT (imap-mailbox-put 'recent token)) | |
2322 | (EXPUNGE t) | |
2323 | (FETCH (imap-parse-fetch token)) | |
2324 | (t (message "Garbage: %s" (buffer-string))))))) | |
2325 | (t (let (status) | |
2326 | (if (not (integerp token)) | |
2327 | (message "Garbage: %s" (buffer-string)) | |
2328 | (case (prog1 (setq status (read (current-buffer))) | |
2329 | (imap-forward)) | |
2330 | (OK (progn | |
2331 | (setq imap-reached-tag (max imap-reached-tag token)) | |
2332 | (imap-parse-resp-text))) | |
2333 | (NO (progn | |
2334 | (setq imap-reached-tag (max imap-reached-tag token)) | |
2335 | (save-excursion | |
2336 | (imap-parse-resp-text)) | |
2337 | (let (code text) | |
2338 | (when (eq (char-after) ?\[) | |
2339 | (setq code (buffer-substring (point) | |
2340 | (search-forward "]"))) | |
2341 | (imap-forward)) | |
2342 | (setq text (buffer-substring (point) (point-max))) | |
2343 | (push (list token status code text) | |
2344 | imap-failed-tags)))) | |
2345 | (BAD (progn | |
2346 | (setq imap-reached-tag (max imap-reached-tag token)) | |
2347 | (save-excursion | |
2348 | (imap-parse-resp-text)) | |
2349 | (let (code text) | |
2350 | (when (eq (char-after) ?\[) | |
2351 | (setq code (buffer-substring (point) | |
2352 | (search-forward "]"))) | |
2353 | (imap-forward)) | |
2354 | (setq text (buffer-substring (point) (point-max))) | |
2355 | (push (list token status code text) imap-failed-tags) | |
2356 | (error "Internal error, tag %s status %s code %s text %s" | |
2357 | token status code text)))) | |
2358 | (t (message "Garbage: %s" (buffer-string)))) | |
2359 | (when (assq token imap-callbacks) | |
2360 | (funcall (cdr (assq token imap-callbacks)) token status) | |
2361 | (setq imap-callbacks | |
2362 | (imap-remassoc token imap-callbacks))))))))) | |
2363 | ||
2364 | ;; resp-text = ["[" resp-text-code "]" SP] text | |
2365 | ;; | |
2366 | ;; text = 1*TEXT-CHAR | |
2367 | ;; | |
2368 | ;; TEXT-CHAR = <any CHAR except CR and LF> | |
2369 | ||
2370 | (defun imap-parse-resp-text () | |
2371 | (imap-parse-resp-text-code)) | |
2372 | ||
2373 | ;; resp-text-code = "ALERT" / | |
2374 | ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] / | |
2375 | ;; "NEWNAME" SP string SP string / | |
2376 | ;; "PARSE" / | |
2377 | ;; "PERMANENTFLAGS" SP "(" | |
2378 | ;; [flag-perm *(SP flag-perm)] ")" / | |
2379 | ;; "READ-ONLY" / | |
2380 | ;; "READ-WRITE" / | |
2381 | ;; "TRYCREATE" / | |
2382 | ;; "UIDNEXT" SP nz-number / | |
2383 | ;; "UIDVALIDITY" SP nz-number / | |
2384 | ;; "UNSEEN" SP nz-number / | |
2385 | ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">] | |
2386 | ;; | |
2387 | ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid | |
2388 | ;; | |
2389 | ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set | |
2390 | ;; | |
2391 | ;; set = sequence-num / (sequence-num ":" sequence-num) / | |
2392 | ;; (set "," set) | |
2393 | ;; ; Identifies a set of messages. For message | |
2394 | ;; ; sequence numbers, these are consecutive | |
2395 | ;; ; numbers from 1 to the number of messages in | |
2396 | ;; ; the mailbox | |
2397 | ;; ; Comma delimits individual numbers, colon | |
2398 | ;; ; delimits between two numbers inclusive. | |
2399 | ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13, | |
2400 | ;; ; 14,15 for a mailbox with 15 messages. | |
2401 | ;; | |
2402 | ;; sequence-num = nz-number / "*" | |
2403 | ;; ; * is the largest number in use. For message | |
2404 | ;; ; sequence numbers, it is the number of messages | |
2405 | ;; ; in the mailbox. For unique identifiers, it is | |
2406 | ;; ; the unique identifier of the last message in | |
2407 | ;; ; the mailbox. | |
2408 | ;; | |
2409 | ;; flag-perm = flag / "\*" | |
2410 | ;; | |
2411 | ;; flag = "\Answered" / "\Flagged" / "\Deleted" / | |
2412 | ;; "\Seen" / "\Draft" / flag-keyword / flag-extension | |
2413 | ;; ; Does not include "\Recent" | |
2414 | ;; | |
2415 | ;; flag-extension = "\" atom | |
2416 | ;; ; Future expansion. Client implementations | |
2417 | ;; ; MUST accept flag-extension flags. Server | |
2418 | ;; ; implementations MUST NOT generate | |
2419 | ;; ; flag-extension flags except as defined by | |
2420 | ;; ; future standard or standards-track | |
2421 | ;; ; revisions of this specification. | |
2422 | ;; | |
2423 | ;; flag-keyword = atom | |
2424 | ;; | |
2425 | ;; resp-text-atom = 1*<any ATOM-CHAR except "]"> | |
2426 | ||
2427 | (defun imap-parse-resp-text-code () | |
2428 | ;; xxx next line for stalker communigate pro 3.3.1 bug | |
2429 | (when (looking-at " \\[") | |
2430 | (imap-forward)) | |
2431 | (when (eq (char-after) ?\[) | |
2432 | (imap-forward) | |
2433 | (cond ((search-forward "PERMANENTFLAGS " nil t) | |
2434 | (imap-mailbox-put 'permanentflags (imap-parse-flag-list))) | |
2435 | ((search-forward "UIDNEXT \\([0-9]+\\)" nil t) | |
2436 | (imap-mailbox-put 'uidnext (match-string 1))) | |
2437 | ((search-forward "UNSEEN " nil t) | |
2438 | (imap-mailbox-put 'first-unseen (read (current-buffer)))) | |
2439 | ((looking-at "UIDVALIDITY \\([0-9]+\\)") | |
2440 | (imap-mailbox-put 'uidvalidity (match-string 1))) | |
2441 | ((search-forward "READ-ONLY" nil t) | |
2442 | (imap-mailbox-put 'read-only t)) | |
2443 | ((search-forward "NEWNAME " nil t) | |
2444 | (let (oldname newname) | |
2445 | (setq oldname (imap-parse-string)) | |
2446 | (imap-forward) | |
2447 | (setq newname (imap-parse-string)) | |
2448 | (imap-mailbox-put 'newname newname oldname))) | |
2449 | ((search-forward "TRYCREATE" nil t) | |
2450 | (imap-mailbox-put 'trycreate t imap-current-target-mailbox)) | |
2451 | ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)") | |
2452 | (imap-mailbox-put 'appenduid | |
2453 | (list (match-string 1) | |
2454 | (string-to-number (match-string 2))) | |
2455 | imap-current-target-mailbox)) | |
2456 | ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)") | |
2457 | (imap-mailbox-put 'copyuid (list (match-string 1) | |
2458 | (match-string 2) | |
2459 | (match-string 3)) | |
2460 | imap-current-target-mailbox)) | |
2461 | ((search-forward "ALERT] " nil t) | |
2462 | (message "Imap server %s information: %s" imap-server | |
2463 | (buffer-substring (point) (point-max))))))) | |
2464 | ||
2465 | ;; mailbox-list = "(" [mbx-list-flags] ")" SP | |
2466 | ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox | |
2467 | ;; | |
2468 | ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag | |
2469 | ;; *(SP mbx-list-oflag) / | |
2470 | ;; mbx-list-oflag *(SP mbx-list-oflag) | |
2471 | ;; | |
2472 | ;; mbx-list-oflag = "\Noinferiors" / flag-extension | |
2473 | ;; ; Other flags; multiple possible per LIST response | |
2474 | ;; | |
2475 | ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked" | |
2476 | ;; ; Selectability flags; only one per LIST response | |
2477 | ;; | |
2478 | ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> / | |
2479 | ;; "\" quoted-specials | |
2480 | ;; | |
2481 | ;; quoted-specials = DQUOTE / "\" | |
2482 | ||
2483 | (defun imap-parse-data-list (type) | |
2484 | (let (flags delimiter mailbox) | |
2485 | (setq flags (imap-parse-flag-list)) | |
2486 | (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"") | |
2487 | (setq delimiter (match-string 1)) | |
2488 | (goto-char (1+ (match-end 0))) | |
2489 | (when (setq mailbox (imap-parse-mailbox)) | |
2490 | (imap-mailbox-put type t mailbox) | |
2491 | (imap-mailbox-put 'list-flags flags mailbox) | |
2492 | (imap-mailbox-put 'delimiter delimiter mailbox))))) | |
2493 | ||
2494 | ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope / | |
2495 | ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" / | |
2496 | ;; "INTERNALDATE" SPACE date_time / | |
2497 | ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring / | |
2498 | ;; "RFC822.SIZE" SPACE number / | |
2499 | ;; "BODY" ["STRUCTURE"] SPACE body / | |
2500 | ;; "BODY" section ["<" number ">"] SPACE nstring / | |
2501 | ;; "UID" SPACE uniqueid) ")" | |
2502 | ;; | |
2503 | ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year | |
2504 | ;; SPACE time SPACE zone <"> | |
2505 | ;; | |
2506 | ;; section ::= "[" [section_text / (nz_number *["." nz_number] | |
2507 | ;; ["." (section_text / "MIME")])] "]" | |
2508 | ;; | |
2509 | ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"] | |
2510 | ;; SPACE header_list / "TEXT" | |
2511 | ;; | |
2512 | ;; header_fld_name ::= astring | |
2513 | ;; | |
2514 | ;; header_list ::= "(" 1#header_fld_name ")" | |
2515 | ||
2516 | (defsubst imap-parse-header-list () | |
2517 | (when (eq (char-after) ?\() | |
2518 | (let (strlist) | |
2519 | (while (not (eq (char-after) ?\))) | |
2520 | (imap-forward) | |
2521 | (push (imap-parse-astring) strlist)) | |
2522 | (imap-forward) | |
2523 | (nreverse strlist)))) | |
2524 | ||
2525 | (defsubst imap-parse-fetch-body-section () | |
2526 | (let ((section | |
2527 | (buffer-substring (point) (1- (re-search-forward "[] ]" nil t))))) | |
2528 | (if (eq (char-before) ? ) | |
2529 | (prog1 | |
2530 | (mapconcat 'identity (cons section (imap-parse-header-list)) " ") | |
2531 | (search-forward "]" nil t)) | |
2532 | section))) | |
2533 | ||
2534 | (defun imap-parse-fetch (response) | |
2535 | (when (eq (char-after) ?\() | |
2536 | (let (uid flags envelope internaldate rfc822 rfc822header rfc822text | |
2537 | rfc822size body bodydetail bodystructure flags-empty) | |
2538 | ;; Courier can insert spurious blank characters which will | |
2539 | ;; confuse `read', so skip past them. | |
2540 | (while (let ((moved (skip-chars-forward " \t"))) | |
2541 | (prog1 (not (eq (char-after) ?\))) | |
2542 | (unless (= moved 0) (backward-char)))) | |
2543 | (imap-forward) | |
2544 | (let ((token (read (current-buffer)))) | |
2545 | (imap-forward) | |
2546 | (cond ((eq token 'UID) | |
2547 | (setq uid (condition-case () | |
2548 | (read (current-buffer)) | |
2549 | (error)))) | |
2550 | ((eq token 'FLAGS) | |
2551 | (setq flags (imap-parse-flag-list)) | |
2552 | (if (not flags) | |
2553 | (setq flags-empty 't))) | |
2554 | ((eq token 'ENVELOPE) | |
2555 | (setq envelope (imap-parse-envelope))) | |
2556 | ((eq token 'INTERNALDATE) | |
2557 | (setq internaldate (imap-parse-string))) | |
2558 | ((eq token 'RFC822) | |
2559 | (setq rfc822 (imap-parse-nstring))) | |
2560 | ((eq token 'RFC822.HEADER) | |
2561 | (setq rfc822header (imap-parse-nstring))) | |
2562 | ((eq token 'RFC822.TEXT) | |
2563 | (setq rfc822text (imap-parse-nstring))) | |
2564 | ((eq token 'RFC822.SIZE) | |
2565 | (setq rfc822size (read (current-buffer)))) | |
2566 | ((eq token 'BODY) | |
2567 | (if (eq (char-before) ?\[) | |
2568 | (push (list | |
2569 | (upcase (imap-parse-fetch-body-section)) | |
2570 | (and (eq (char-after) ?<) | |
2571 | (buffer-substring (1+ (point)) | |
2572 | (search-forward ">" nil t))) | |
2573 | (progn (imap-forward) | |
2574 | (imap-parse-nstring))) | |
2575 | bodydetail) | |
2576 | (setq body (imap-parse-body)))) | |
2577 | ((eq token 'BODYSTRUCTURE) | |
2578 | (setq bodystructure (imap-parse-body)))))) | |
2579 | (when uid | |
2580 | (setq imap-current-message uid) | |
2581 | (imap-message-put uid 'UID uid) | |
2582 | (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags)) | |
2583 | (and envelope (imap-message-put uid 'ENVELOPE envelope)) | |
2584 | (and internaldate (imap-message-put uid 'INTERNALDATE internaldate)) | |
2585 | (and rfc822 (imap-message-put uid 'RFC822 rfc822)) | |
2586 | (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header)) | |
2587 | (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text)) | |
2588 | (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size)) | |
2589 | (and body (imap-message-put uid 'BODY body)) | |
2590 | (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail)) | |
2591 | (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure)) | |
2592 | (run-hooks 'imap-fetch-data-hook))))) | |
2593 | ||
2594 | ;; mailbox-data = ... | |
2595 | ;; "STATUS" SP mailbox SP "(" | |
2596 | ;; [status-att SP number | |
2597 | ;; *(SP status-att SP number)] ")" | |
2598 | ;; ... | |
2599 | ;; | |
2600 | ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" / | |
2601 | ;; "UNSEEN" | |
2602 | ||
2603 | (defun imap-parse-status () | |
2604 | (let ((mailbox (imap-parse-mailbox))) | |
2605 | (if (eq (char-after) ? ) | |
2606 | (forward-char)) | |
2607 | (when (and mailbox (eq (char-after) ?\()) | |
2608 | (while (and (not (eq (char-after) ?\))) | |
2609 | (or (forward-char) t) | |
2610 | (looking-at "\\([A-Za-z]+\\) ")) | |
2611 | (let ((token (upcase (match-string 1)))) | |
2612 | (goto-char (match-end 0)) | |
2613 | (cond ((string= token "MESSAGES") | |
2614 | (imap-mailbox-put 'messages (read (current-buffer)) mailbox)) | |
2615 | ((string= token "RECENT") | |
2616 | (imap-mailbox-put 'recent (read (current-buffer)) mailbox)) | |
2617 | ((string= token "UIDNEXT") | |
2618 | (and (looking-at "[0-9]+") | |
2619 | (imap-mailbox-put 'uidnext (match-string 0) mailbox) | |
2620 | (goto-char (match-end 0)))) | |
2621 | ((string= token "UIDVALIDITY") | |
2622 | (and (looking-at "[0-9]+") | |
2623 | (imap-mailbox-put 'uidvalidity (match-string 0) mailbox) | |
2624 | (goto-char (match-end 0)))) | |
2625 | ((string= token "UNSEEN") | |
2626 | (imap-mailbox-put 'unseen (read (current-buffer)) mailbox)) | |
2627 | (t | |
2628 | (message "Unknown status data %s in mailbox %s ignored" | |
2629 | token mailbox) | |
2630 | (read (current-buffer))))))))) | |
2631 | ||
2632 | ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE | |
2633 | ;; rights) | |
2634 | ;; | |
2635 | ;; identifier ::= astring | |
2636 | ;; | |
2637 | ;; rights ::= astring | |
2638 | ||
2639 | (defun imap-parse-acl () | |
2640 | (let ((mailbox (imap-parse-mailbox)) | |
2641 | identifier rights acl) | |
2642 | (while (eq (char-after) ?\ ) | |
2643 | (imap-forward) | |
2644 | (setq identifier (imap-parse-astring)) | |
2645 | (imap-forward) | |
2646 | (setq rights (imap-parse-astring)) | |
2647 | (setq acl (append acl (list (cons identifier rights))))) | |
2648 | (imap-mailbox-put 'acl acl mailbox))) | |
2649 | ||
2650 | ;; flag-list = "(" [flag *(SP flag)] ")" | |
2651 | ;; | |
2652 | ;; flag = "\Answered" / "\Flagged" / "\Deleted" / | |
2653 | ;; "\Seen" / "\Draft" / flag-keyword / flag-extension | |
2654 | ;; ; Does not include "\Recent" | |
2655 | ;; | |
2656 | ;; flag-keyword = atom | |
2657 | ;; | |
2658 | ;; flag-extension = "\" atom | |
2659 | ;; ; Future expansion. Client implementations | |
2660 | ;; ; MUST accept flag-extension flags. Server | |
2661 | ;; ; implementations MUST NOT generate | |
2662 | ;; ; flag-extension flags except as defined by | |
2663 | ;; ; future standard or standards-track | |
2664 | ;; ; revisions of this specification. | |
2665 | ||
2666 | (defun imap-parse-flag-list () | |
2667 | (let (flag-list start) | |
2668 | (assert (eq (char-after) ?\() nil "In imap-parse-flag-list 1") | |
2669 | (while (and (not (eq (char-after) ?\))) | |
2670 | (setq start (progn | |
2671 | (imap-forward) | |
2672 | ;; next line for Courier IMAP bug. | |
2673 | (skip-chars-forward " ") | |
2674 | (point))) | |
2675 | (> (skip-chars-forward "^ )" (point-at-eol)) 0)) | |
2676 | (push (buffer-substring start (point)) flag-list)) | |
2677 | (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list 2") | |
2678 | (imap-forward) | |
2679 | (nreverse flag-list))) | |
2680 | ||
2681 | ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP | |
2682 | ;; env-reply-to SP env-to SP env-cc SP env-bcc SP | |
2683 | ;; env-in-reply-to SP env-message-id ")" | |
2684 | ;; | |
2685 | ;; env-bcc = "(" 1*address ")" / nil | |
2686 | ;; | |
2687 | ;; env-cc = "(" 1*address ")" / nil | |
2688 | ;; | |
2689 | ;; env-date = nstring | |
2690 | ;; | |
2691 | ;; env-from = "(" 1*address ")" / nil | |
2692 | ;; | |
2693 | ;; env-in-reply-to = nstring | |
2694 | ;; | |
2695 | ;; env-message-id = nstring | |
2696 | ;; | |
2697 | ;; env-reply-to = "(" 1*address ")" / nil | |
2698 | ;; | |
2699 | ;; env-sender = "(" 1*address ")" / nil | |
2700 | ;; | |
2701 | ;; env-subject = nstring | |
2702 | ;; | |
2703 | ;; env-to = "(" 1*address ")" / nil | |
2704 | ||
2705 | (defun imap-parse-envelope () | |
2706 | (when (eq (char-after) ?\() | |
2707 | (imap-forward) | |
2708 | (vector (prog1 (imap-parse-nstring) ;; date | |
2709 | (imap-forward)) | |
2710 | (prog1 (imap-parse-nstring) ;; subject | |
2711 | (imap-forward)) | |
2712 | (prog1 (imap-parse-address-list) ;; from | |
2713 | (imap-forward)) | |
2714 | (prog1 (imap-parse-address-list) ;; sender | |
2715 | (imap-forward)) | |
2716 | (prog1 (imap-parse-address-list) ;; reply-to | |
2717 | (imap-forward)) | |
2718 | (prog1 (imap-parse-address-list) ;; to | |
2719 | (imap-forward)) | |
2720 | (prog1 (imap-parse-address-list) ;; cc | |
2721 | (imap-forward)) | |
2722 | (prog1 (imap-parse-address-list) ;; bcc | |
2723 | (imap-forward)) | |
2724 | (prog1 (imap-parse-nstring) ;; in-reply-to | |
2725 | (imap-forward)) | |
2726 | (prog1 (imap-parse-nstring) ;; message-id | |
2727 | (imap-forward))))) | |
2728 | ||
2729 | ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil | |
2730 | ||
2731 | (defsubst imap-parse-string-list () | |
2732 | (cond ((eq (char-after) ?\() ;; body-fld-param | |
2733 | (let (strlist str) | |
2734 | (imap-forward) | |
2735 | (while (setq str (imap-parse-string)) | |
2736 | (push str strlist) | |
2737 | ;; buggy stalker communigate pro 3.0 doesn't print SPC | |
2738 | ;; between body-fld-param's sometimes | |
2739 | (or (eq (char-after) ?\") | |
2740 | (imap-forward))) | |
2741 | (nreverse strlist))) | |
2742 | ((imap-parse-nil) | |
2743 | nil))) | |
2744 | ||
2745 | ;; body-extension = nstring / number / | |
2746 | ;; "(" body-extension *(SP body-extension) ")" | |
2747 | ;; ; Future expansion. Client implementations | |
2748 | ;; ; MUST accept body-extension fields. Server | |
2749 | ;; ; implementations MUST NOT generate | |
2750 | ;; ; body-extension fields except as defined by | |
2751 | ;; ; future standard or standards-track | |
2752 | ;; ; revisions of this specification. | |
2753 | ||
2754 | (defun imap-parse-body-extension () | |
2755 | (if (eq (char-after) ?\() | |
2756 | (let (b-e) | |
2757 | (imap-forward) | |
2758 | (push (imap-parse-body-extension) b-e) | |
2759 | (while (eq (char-after) ?\ ) | |
2760 | (imap-forward) | |
2761 | (push (imap-parse-body-extension) b-e)) | |
2762 | (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension") | |
2763 | (imap-forward) | |
2764 | (nreverse b-e)) | |
2765 | (or (imap-parse-number) | |
2766 | (imap-parse-nstring)))) | |
2767 | ||
2768 | ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang | |
2769 | ;; *(SP body-extension)]] | |
2770 | ;; ; MUST NOT be returned on non-extensible | |
2771 | ;; ; "BODY" fetch | |
2772 | ;; | |
2773 | ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang | |
2774 | ;; *(SP body-extension)]] | |
2775 | ;; ; MUST NOT be returned on non-extensible | |
2776 | ;; ; "BODY" fetch | |
2777 | ||
2778 | (defsubst imap-parse-body-ext () | |
2779 | (let (ext) | |
2780 | (when (eq (char-after) ?\ ) ;; body-fld-dsp | |
2781 | (imap-forward) | |
2782 | (let (dsp) | |
2783 | (if (eq (char-after) ?\() | |
2784 | (progn | |
2785 | (imap-forward) | |
2786 | (push (imap-parse-string) dsp) | |
2787 | (imap-forward) | |
2788 | (push (imap-parse-string-list) dsp) | |
2789 | (imap-forward)) | |
2790 | ;; With assert, the code might not be eval'd. | |
2791 | ;; (assert (imap-parse-nil) t "In imap-parse-body-ext") | |
2792 | (imap-parse-nil)) | |
2793 | (push (nreverse dsp) ext)) | |
2794 | (when (eq (char-after) ?\ ) ;; body-fld-lang | |
2795 | (imap-forward) | |
2796 | (if (eq (char-after) ?\() | |
2797 | (push (imap-parse-string-list) ext) | |
2798 | (push (imap-parse-nstring) ext)) | |
2799 | (while (eq (char-after) ?\ ) ;; body-extension | |
2800 | (imap-forward) | |
2801 | (setq ext (append (imap-parse-body-extension) ext))))) | |
2802 | ext)) | |
2803 | ||
2804 | ;; body = "(" body-type-1part / body-type-mpart ")" | |
2805 | ;; | |
2806 | ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang | |
2807 | ;; *(SP body-extension)]] | |
2808 | ;; ; MUST NOT be returned on non-extensible | |
2809 | ;; ; "BODY" fetch | |
2810 | ;; | |
2811 | ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang | |
2812 | ;; *(SP body-extension)]] | |
2813 | ;; ; MUST NOT be returned on non-extensible | |
2814 | ;; ; "BODY" fetch | |
2815 | ;; | |
2816 | ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP | |
2817 | ;; body-fld-enc SP body-fld-octets | |
2818 | ;; | |
2819 | ;; body-fld-desc = nstring | |
2820 | ;; | |
2821 | ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil | |
2822 | ;; | |
2823 | ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/ | |
2824 | ;; "QUOTED-PRINTABLE") DQUOTE) / string | |
2825 | ;; | |
2826 | ;; body-fld-id = nstring | |
2827 | ;; | |
2828 | ;; body-fld-lang = nstring / "(" string *(SP string) ")" | |
2829 | ;; | |
2830 | ;; body-fld-lines = number | |
2831 | ;; | |
2832 | ;; body-fld-md5 = nstring | |
2833 | ;; | |
2834 | ;; body-fld-octets = number | |
2835 | ;; | |
2836 | ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil | |
2837 | ;; | |
2838 | ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text) | |
2839 | ;; [SP body-ext-1part] | |
2840 | ;; | |
2841 | ;; body-type-basic = media-basic SP body-fields | |
2842 | ;; ; MESSAGE subtype MUST NOT be "RFC822" | |
2843 | ;; | |
2844 | ;; body-type-msg = media-message SP body-fields SP envelope | |
2845 | ;; SP body SP body-fld-lines | |
2846 | ;; | |
2847 | ;; body-type-text = media-text SP body-fields SP body-fld-lines | |
2848 | ;; | |
2849 | ;; body-type-mpart = 1*body SP media-subtype | |
2850 | ;; [SP body-ext-mpart] | |
2851 | ;; | |
2852 | ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" / | |
2853 | ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype | |
2854 | ;; ; Defined in [MIME-IMT] | |
2855 | ;; | |
2856 | ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE | |
2857 | ;; ; Defined in [MIME-IMT] | |
2858 | ;; | |
2859 | ;; media-subtype = string | |
2860 | ;; ; Defined in [MIME-IMT] | |
2861 | ;; | |
2862 | ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype | |
2863 | ;; ; Defined in [MIME-IMT] | |
2864 | ||
2865 | (defun imap-parse-body () | |
2866 | (let (body) | |
2867 | (when (eq (char-after) ?\() | |
2868 | (imap-forward) | |
2869 | (if (eq (char-after) ?\() | |
2870 | (let (subbody) | |
2871 | (while (and (eq (char-after) ?\() | |
2872 | (setq subbody (imap-parse-body))) | |
2873 | ;; buggy stalker communigate pro 3.0 inserts a SPC between | |
2874 | ;; parts in multiparts | |
2875 | (when (and (eq (char-after) ?\ ) | |
2876 | (eq (char-after (1+ (point))) ?\()) | |
2877 | (imap-forward)) | |
2878 | (push subbody body)) | |
2879 | (imap-forward) | |
2880 | (push (imap-parse-string) body) ;; media-subtype | |
2881 | (when (eq (char-after) ?\ ) ;; body-ext-mpart: | |
2882 | (imap-forward) | |
2883 | (if (eq (char-after) ?\() ;; body-fld-param | |
2884 | (push (imap-parse-string-list) body) | |
2885 | (push (and (imap-parse-nil) nil) body)) | |
2886 | (setq body | |
2887 | (append (imap-parse-body-ext) body))) ;; body-ext-... | |
2888 | (assert (eq (char-after) ?\)) nil "In imap-parse-body") | |
2889 | (imap-forward) | |
2890 | (nreverse body)) | |
2891 | ||
2892 | (push (imap-parse-string) body) ;; media-type | |
2893 | (imap-forward) | |
2894 | (push (imap-parse-string) body) ;; media-subtype | |
2895 | (imap-forward) | |
2896 | ;; next line for Sun SIMS bug | |
2897 | (and (eq (char-after) ? ) (imap-forward)) | |
2898 | (if (eq (char-after) ?\() ;; body-fld-param | |
2899 | (push (imap-parse-string-list) body) | |
2900 | (push (and (imap-parse-nil) nil) body)) | |
2901 | (imap-forward) | |
2902 | (push (imap-parse-nstring) body) ;; body-fld-id | |
2903 | (imap-forward) | |
2904 | (push (imap-parse-nstring) body) ;; body-fld-desc | |
2905 | (imap-forward) | |
2906 | ;; Next `or' for Sun SIMS bug. It regards body-fld-enc as a | |
2907 | ;; nstring and returns nil instead of defaulting back to 7BIT | |
2908 | ;; as the standard says. | |
2909 | ;; Exchange (2007, at least) does this as well. | |
2910 | (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc | |
2911 | (imap-forward) | |
2912 | ;; Exchange 2007 can return -1, contrary to the spec... | |
2913 | (if (eq (char-after) ?-) | |
2914 | (progn | |
2915 | (skip-chars-forward "-0-9") | |
2916 | (push nil body)) | |
2917 | (push (imap-parse-number) body)) ;; body-fld-octets | |
2918 | ||
2919 | ;; Ok, we're done parsing the required parts, what comes now is one of | |
2920 | ;; three things: | |
2921 | ;; | |
2922 | ;; envelope (then we're parsing body-type-msg) | |
2923 | ;; body-fld-lines (then we're parsing body-type-text) | |
2924 | ;; body-ext-1part (then we're parsing body-type-basic) | |
2925 | ;; | |
2926 | ;; The problem is that the two first are in turn optionally followed | |
2927 | ;; by the third. So we parse the first two here (if there are any)... | |
2928 | ||
2929 | (when (eq (char-after) ?\ ) | |
2930 | (imap-forward) | |
2931 | (let (lines) | |
2932 | (cond ((eq (char-after) ?\() ;; body-type-msg: | |
2933 | (push (imap-parse-envelope) body) ;; envelope | |
2934 | (imap-forward) | |
2935 | (push (imap-parse-body) body) ;; body | |
2936 | ;; buggy stalker communigate pro 3.0 doesn't print | |
2937 | ;; number of lines in message/rfc822 attachment | |
2938 | (if (eq (char-after) ?\)) | |
2939 | (push 0 body) | |
2940 | (imap-forward) | |
2941 | (push (imap-parse-number) body))) ;; body-fld-lines | |
2942 | ((setq lines (imap-parse-number)) ;; body-type-text: | |
2943 | (push lines body)) ;; body-fld-lines | |
2944 | (t | |
2945 | (backward-char))))) ;; no match... | |
2946 | ||
2947 | ;; ...and then parse the third one here... | |
2948 | ||
2949 | (when (eq (char-after) ?\ ) ;; body-ext-1part: | |
2950 | (imap-forward) | |
2951 | (push (imap-parse-nstring) body) ;; body-fld-md5 | |
2952 | (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part.. | |
2953 | ||
2954 | (assert (eq (char-after) ?\)) nil "In imap-parse-body 2") | |
2955 | (imap-forward) | |
2956 | (nreverse body))))) | |
2957 | ||
2958 | (when imap-debug ; (untrace-all) | |
2959 | (require 'trace) | |
2960 | (buffer-disable-undo (get-buffer-create imap-debug-buffer)) | |
2961 | (mapc (lambda (f) (trace-function-background f imap-debug-buffer)) | |
2962 | '( | |
2963 | imap-utf7-encode | |
2964 | imap-utf7-decode | |
2965 | imap-error-text | |
2966 | imap-kerberos4s-p | |
2967 | imap-kerberos4-open | |
2968 | imap-ssl-p | |
2969 | imap-ssl-open | |
2970 | imap-network-p | |
2971 | imap-network-open | |
2972 | imap-interactive-login | |
2973 | imap-kerberos4a-p | |
2974 | imap-kerberos4-auth | |
2975 | imap-cram-md5-p | |
2976 | imap-cram-md5-auth | |
2977 | imap-login-p | |
2978 | imap-login-auth | |
2979 | imap-anonymous-p | |
2980 | imap-anonymous-auth | |
2981 | imap-open-1 | |
2982 | imap-open | |
2983 | imap-opened | |
2984 | imap-ping-server | |
2985 | imap-authenticate | |
2986 | imap-close | |
2987 | imap-capability | |
2988 | imap-namespace | |
2989 | imap-send-command-wait | |
2990 | imap-mailbox-put | |
2991 | imap-mailbox-get | |
2992 | imap-mailbox-map-1 | |
2993 | imap-mailbox-map | |
2994 | imap-current-mailbox | |
2995 | imap-current-mailbox-p-1 | |
2996 | imap-current-mailbox-p | |
2997 | imap-mailbox-select-1 | |
2998 | imap-mailbox-select | |
2999 | imap-mailbox-examine-1 | |
3000 | imap-mailbox-examine | |
3001 | imap-mailbox-unselect | |
3002 | imap-mailbox-expunge | |
3003 | imap-mailbox-close | |
3004 | imap-mailbox-create-1 | |
3005 | imap-mailbox-create | |
3006 | imap-mailbox-delete | |
3007 | imap-mailbox-rename | |
3008 | imap-mailbox-lsub | |
3009 | imap-mailbox-list | |
3010 | imap-mailbox-subscribe | |
3011 | imap-mailbox-unsubscribe | |
3012 | imap-mailbox-status | |
3013 | imap-mailbox-acl-get | |
3014 | imap-mailbox-acl-set | |
3015 | imap-mailbox-acl-delete | |
3016 | imap-current-message | |
3017 | imap-list-to-message-set | |
3018 | imap-fetch-asynch | |
3019 | imap-fetch | |
3020 | imap-fetch-safe | |
3021 | imap-message-put | |
3022 | imap-message-get | |
3023 | imap-message-map | |
3024 | imap-search | |
3025 | imap-message-flag-permanent-p | |
3026 | imap-message-flags-set | |
3027 | imap-message-flags-del | |
3028 | imap-message-flags-add | |
3029 | imap-message-copyuid-1 | |
3030 | imap-message-copyuid | |
3031 | imap-message-copy | |
3032 | imap-message-appenduid-1 | |
3033 | imap-message-appenduid | |
3034 | imap-message-append | |
3035 | imap-body-lines | |
3036 | imap-envelope-from | |
3037 | imap-send-command-1 | |
3038 | imap-send-command | |
3039 | imap-wait-for-tag | |
3040 | imap-sentinel | |
3041 | imap-find-next-line | |
3042 | imap-arrival-filter | |
3043 | imap-parse-greeting | |
3044 | imap-parse-response | |
3045 | imap-parse-resp-text | |
3046 | imap-parse-resp-text-code | |
3047 | imap-parse-data-list | |
3048 | imap-parse-fetch | |
3049 | imap-parse-status | |
3050 | imap-parse-acl | |
3051 | imap-parse-flag-list | |
3052 | imap-parse-envelope | |
3053 | imap-parse-body-extension | |
3054 | imap-parse-body | |
3055 | ))) | |
3056 | ||
3057 | (provide 'imap) | |
3058 | ||
3059 | ;;; imap.el ends here |