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