1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; imap.el is a elisp library providing an interface for talking to
30 ;; imap.el is roughly divided in two parts, one that parses IMAP
31 ;; responses from the server and storing data into buffer-local
32 ;; variables, and one for utility functions which send commands to
33 ;; server, waits for an answer, and return information. The latter
34 ;; part is layered on top of the previous.
36 ;; The imap.el API consist of the following functions, other functions
37 ;; in this file should not be called directly and the result of doing
38 ;; so are at best undefined.
42 ;; imap-open, imap-opened, imap-authenticate, imap-close,
43 ;; imap-capability, imap-namespace, imap-error-text
47 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
48 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
49 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
50 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
51 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
52 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
53 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
57 ;; imap-fetch-asynch, imap-fetch,
58 ;; imap-current-message, imap-list-to-message-set,
59 ;; imap-message-get, imap-message-map
60 ;; imap-message-envelope-date, imap-message-envelope-subject,
61 ;; imap-message-envelope-from, imap-message-envelope-sender,
62 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
63 ;; imap-message-envelope-cc, imap-message-envelope-bcc
64 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
65 ;; imap-message-body, imap-message-flag-permanent-p
66 ;; imap-message-flags-set, imap-message-flags-del
67 ;; imap-message-flags-add, imap-message-copyuid
68 ;; imap-message-copy, imap-message-appenduid
69 ;; imap-message-append, imap-envelope-from
72 ;; It is my hope that these commands should be pretty self
73 ;; explanatory for someone that know IMAP. All functions have
74 ;; additional documentation on how to invoke them.
76 ;; imap.el support RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1), implemented
77 ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
78 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
79 ;; LOGINDISABLED) (with use of external library starttls.el and
80 ;; program starttls), and the GSSAPI / kerberos V4 sections of RFC1731
81 ;; (with use of external program `imtest'), RFC2971 (ID). It also
82 ;; take advantage the UNSELECT extension in Cyrus IMAPD.
84 ;; Without the work of John McClary Prevost and Jim Radford this library
85 ;; would not have seen the light of day. Many thanks.
87 ;; This is a transcript of short interactive session for demonstration
90 ;; (imap-open "my.mail.server")
91 ;; => " *imap* my.mail.server:0"
93 ;; The rest are invoked with current buffer as the buffer returned by
94 ;; `imap-open'. It is possible to do all without this, but it would
95 ;; look ugly here since `buffer' is always the last argument for all
96 ;; imap.el API functions.
98 ;; (imap-authenticate "myusername" "mypassword")
101 ;; (imap-mailbox-lsub "*")
102 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
104 ;; (imap-mailbox-list "INBOX.n%")
105 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
107 ;; (imap-mailbox-select "INBOX.nnimap")
110 ;; (imap-mailbox-get 'exists)
113 ;; (imap-mailbox-get 'uidvalidity)
116 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
119 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
120 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
124 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
125 ;; o Don't use `read' at all (important places already fixed)
126 ;; o Accept list of articles instead of message set string in most
127 ;; imap-message-* functions.
128 ;; o Send strings as literal if they contain, e.g., ".
132 ;; - 19991218 added starttls/digest-md5 patch,
133 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
134 ;; NB! you need SLIM for starttls.el and digest-md5.el
135 ;; - 19991023 commited to pgnus
140 (eval-when-compile (require 'cl))
141 (eval-when-compile (require 'static))
146 (autoload 'starttls-open-stream "starttls")
147 (autoload 'starttls-negotiate "starttls")
148 (autoload 'sasl-find-mechanism "sasl")
149 (autoload 'digest-md5-parse-digest-challenge "digest-md5")
150 (autoload 'digest-md5-digest-response "digest-md5")
151 (autoload 'digest-md5-digest-uri "digest-md5")
152 (autoload 'digest-md5-challenge "digest-md5")
153 (autoload 'rfc2104-hash "rfc2104")
154 (autoload 'utf7-encode "utf7")
155 (autoload 'utf7-decode "utf7")
156 (autoload 'format-spec "format-spec")
157 (autoload 'format-spec-make "format-spec")
158 (autoload 'open-tls-stream "tls"))
163 "Low-level IMAP issues."
167 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
169 "List of strings containing commands for Kerberos 4 authentication.
170 %s is replaced with server hostname, %p with port to connect to, and
171 %l with the value of `imap-default-user'. The program should accept
172 IMAP commands on stdin and return responses to stdout. Each entry in
173 the list is tried until a successful connection is made."
175 :type '(repeat string))
177 (defcustom imap-gssapi-program (list
178 (concat "gsasl --client --connect %s:%p "
179 "--imap --application-data "
180 "--mechanism GSSAPI "
181 "--authentication-id %l")
182 "imtest -m gssapi -u %l -p %p %s")
183 "List of strings containing commands for GSSAPI (krb5) authentication.
184 %s is replaced with server hostname, %p with port to connect to, and
185 %l with the value of `imap-default-user'. The program should accept
186 IMAP commands on stdin and return responses to stdout. Each entry in
187 the list is tried until a successful connection is made."
189 :type '(repeat string))
191 (defcustom imap-ssl-program '("openssl s_client -quiet -ssl3 -connect %s:%p"
192 "openssl s_client -quiet -ssl2 -connect %s:%p"
193 "s_client -quiet -ssl3 -connect %s:%p"
194 "s_client -quiet -ssl2 -connect %s:%p")
195 "A string, or list of strings, containing commands for SSL connections.
196 Within a string, %s is replaced with the server address and %p with
197 port number on server. The program should accept IMAP commands on
198 stdin and return responses to stdout. Each entry in the list is tried
199 until a successful connection is made."
201 :type '(choice string
204 (defcustom imap-shell-program '("ssh %s imapd"
206 "ssh %g ssh %s imapd"
207 "rsh %g rsh %s imapd")
208 "A list of strings, containing commands for IMAP connection.
209 Within a string, %s is replaced with the server address, %p with port
210 number on server, %g with `imap-shell-host', and %l with
211 `imap-default-user'. The program should read IMAP commands from stdin
212 and write IMAP response to stdout. Each entry in the list is tried
213 until a successful connection is made."
215 :type '(repeat string))
217 (defcustom imap-process-connection-type nil
218 "*Value for `process-connection-type' to use for Kerberos4, GSSAPI and SSL.
219 The `process-connection-type' variable control type of device
220 used to communicate with subprocesses. Values are nil to use a
221 pipe, or t or `pty' to use a pty. The value has no effect if the
222 system has no ptys or if all ptys are busy: then a pipe is used
223 in any case. The value takes effect when a IMAP server is
224 opened, changing it after that has no effect."
229 (defcustom imap-use-utf7 t
230 "If non-nil, do utf7 encoding/decoding of mailbox names.
231 Since the UTF7 decoding currently only decodes into ISO-8859-1
232 characters, you may disable this decoding if you need to access UTF7
233 encoded mailboxes which doesn't translate into ISO-8859-1."
237 (defcustom imap-log nil
238 "If non-nil, a imap session trace is placed in *imap-log* buffer.
239 Note that username, passwords and other privacy sensitive
240 information (such as e-mail) may be stored in the *imap-log*
241 buffer. It is not written to disk, however. Do not enable this
242 variable unless you are comfortable with that."
246 (defcustom imap-debug nil
247 "If non-nil, random debug spews are placed in *imap-debug* buffer.
248 Note that username, passwords and other privacy sensitive
249 information (such as e-mail) may be stored in the *imap-debug*
250 buffer. It is not written to disk, however. Do not enable this
251 variable unless you are comfortable with that."
255 (defcustom imap-shell-host "gateway"
256 "Hostname of rlogin proxy."
260 (defcustom imap-default-user (user-login-name)
261 "Default username to use."
265 (defcustom imap-read-timeout (if (string-match
266 "windows-nt\\|os/2\\|emx\\|cygwin"
267 (symbol-name system-type))
270 "*How long to wait between checking for the end of output.
271 Shorter values mean quicker response, but is more CPU intensive."
275 (defcustom imap-store-password nil
276 "If non-nil, store session password without promting."
280 ;; Various variables.
282 (defvar imap-fetch-data-hook nil
283 "Hooks called after receiving each FETCH response.")
285 (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell)
286 "Priority of streams to consider when opening connection to server.")
288 (defvar imap-stream-alist
289 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
290 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
291 (tls imap-tls-p imap-tls-open)
292 (ssl imap-ssl-p imap-ssl-open)
293 (network imap-network-p imap-network-open)
294 (shell imap-shell-p imap-shell-open)
295 (starttls imap-starttls-p imap-starttls-open))
296 "Definition of network streams.
300 NAME names the stream, CHECK is a function returning non-nil if the
301 server support the stream and OPEN is a function for opening the
304 (defvar imap-authenticators '(gssapi
311 "Priority of authenticators to consider when authenticating to server.")
313 (defvar imap-authenticator-alist
314 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
315 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
316 (sasl imap-sasl-auth-p imap-sasl-auth)
317 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
318 (login imap-login-p imap-login-auth)
319 (anonymous imap-anonymous-p imap-anonymous-auth)
320 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
321 "Definition of authenticators.
323 \(NAME CHECK AUTHENTICATE)
325 NAME names the authenticator. CHECK is a function returning non-nil if
326 the server support the authenticator and AUTHENTICATE is a function
327 for doing the actual authentication.")
329 (defvar imap-error nil
330 "Error codes from the last command.")
332 ;; Internal constants. Change these and die.
334 (defconst imap-default-port 143)
335 (defconst imap-default-ssl-port 993)
336 (defconst imap-default-tls-port 993)
337 (defconst imap-default-stream 'network)
338 (defconst imap-local-variables '(imap-server
347 imap-current-target-mailbox
357 imap-calculate-literal-size-first
359 (defconst imap-log-buffer "*imap-log*")
360 (defconst imap-debug-buffer "*imap-debug*")
362 ;; Internal variables.
364 (defvar imap-stream nil)
365 (defvar imap-auth nil)
366 (defvar imap-server nil)
367 (defvar imap-port nil)
368 (defvar imap-username nil)
369 (defvar imap-password nil)
370 (defvar imap-calculate-literal-size-first nil)
371 (defvar imap-state 'closed
373 Valid states are `closed', `initial', `nonauth', `auth', `selected'
376 (defvar imap-server-eol "\r\n"
377 "The EOL string sent from the server.")
379 (defvar imap-client-eol "\r\n"
380 "The EOL string we send to the server.")
382 (defvar imap-current-mailbox nil
383 "Current mailbox name.")
385 (defvar imap-current-target-mailbox nil
386 "Current target mailbox for COPY and APPEND commands.")
388 (defvar imap-mailbox-data nil
389 "Obarray with mailbox data.")
391 (defvar imap-mailbox-prime 997
392 "Length of imap-mailbox-data.")
394 (defvar imap-current-message nil
395 "Current message number.")
397 (defvar imap-message-data nil
398 "Obarray with message data.")
400 (defvar imap-message-prime 997
401 "Length of imap-message-data.")
403 (defvar imap-capability nil
404 "Capability for server.")
410 (defvar imap-namespace nil
411 "Namespace for current server.")
413 (defvar imap-reached-tag 0
414 "Lower limit on command tags that have been parsed.")
416 (defvar imap-failed-tags nil
417 "Alist of tags that failed.
418 Each element is a list with four elements; tag (a integer), response
419 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
420 human readable response text (a string).")
423 "Command tag number.")
425 (defvar imap-process nil
428 (defvar imap-continuation nil
429 "Non-nil indicates that the server emitted a continuation request.
430 The actual value is really the text on the continuation line.")
432 (defvar imap-callbacks nil
433 "List of response tags and callbacks, on the form `(number . function)'.
434 The function should take two arguments, the first the IMAP tag and the
435 second the status (OK, NO, BAD etc) of the command.")
438 ;; Utility functions:
440 (defun imap-remassoc (key alist)
441 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
442 The modified LIST is returned. If the first member
443 of LIST has a car that is `equal' to KEY, there is no way to remove it
444 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
445 sure of changing the value of `foo'."
447 (if (equal key (caar alist))
449 (setcdr alist (imap-remassoc key (cdr alist)))
452 (defmacro imap-disable-multibyte ()
453 "Enable multibyte in the current buffer."
454 '(set-buffer-multibyte nil))
456 (defsubst imap-utf7-encode (string)
460 (utf7-encode string t)
462 "imap: Could not UTF7 encode `%s', using it unencoded..."
467 (defsubst imap-utf7-decode (string)
471 (utf7-decode string t)
473 "imap: Could not UTF7 decode `%s', using it undecoded..."
478 (defsubst imap-ok-p (status)
481 (setq imap-error status)
484 (defun imap-error-text (&optional buffer)
485 (with-current-buffer (or buffer (current-buffer))
486 (nth 3 (car imap-failed-tags))))
489 ;; Server functions; stream stuff:
491 (defun imap-kerberos4-stream-p (buffer)
492 (imap-capability 'AUTH=KERBEROS_V4 buffer))
494 (defun imap-kerberos4-open (name buffer server port)
495 (let ((cmds imap-kerberos4-program)
497 (while (and (not done) (setq cmd (pop cmds)))
498 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
500 (let* ((port (or port imap-default-port))
501 (process-connection-type imap-process-connection-type)
502 (process (as-binary-process
504 name buffer shell-file-name shell-command-switch
509 ?p (number-to-string port)
510 ?l imap-default-user)))))
513 (with-current-buffer buffer
514 (setq imap-client-eol "\n"
515 imap-calculate-literal-size-first t)
516 (while (and (memq (process-status process) '(open run))
517 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
518 (goto-char (point-min))
519 ;; Athena IMTEST can output SSL verify errors
520 (or (while (looking-at "^verify error:num=")
523 (or (while (looking-at "^TLS connection established")
526 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
527 (or (while (looking-at "^C:")
530 ;; cyrus 1.6 imtest print "S: " before server greeting
531 (or (not (looking-at "S: "))
534 (not (and (imap-parse-greeting)
535 ;; success in imtest < 1.6:
536 (or (re-search-forward
537 "^__\\(.*\\)__\n" nil t)
538 ;; success in imtest 1.6:
540 "^\\(Authenticat.*\\)" nil t))
541 (setq response (match-string 1)))))
542 (accept-process-output process 1)
545 (with-current-buffer (get-buffer-create imap-log-buffer)
546 (imap-disable-multibyte)
547 (buffer-disable-undo)
548 (goto-char (point-max))
549 (insert-buffer-substring buffer)))
551 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
552 (if response (concat "done, " response) "failed"))
553 (if (and response (let ((case-fold-search nil))
554 (not (string-match "failed" response))))
556 (if (memq (process-status process) '(open run))
557 (imap-send-command "LOGOUT"))
558 (delete-process process)
562 (defun imap-gssapi-stream-p (buffer)
563 (imap-capability 'AUTH=GSSAPI buffer))
565 (defun imap-gssapi-open (name buffer server port)
566 (let ((cmds imap-gssapi-program)
568 (while (and (not done) (setq cmd (pop cmds)))
569 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
571 (let* ((port (or port imap-default-port))
572 (process-connection-type imap-process-connection-type)
573 (process (as-binary-process
575 name buffer shell-file-name shell-command-switch
580 ?p (number-to-string port)
581 ?l imap-default-user)))))
584 (with-current-buffer buffer
585 (setq imap-client-eol "\n"
586 imap-calculate-literal-size-first t)
587 (while (and (memq (process-status process) '(open run))
588 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
589 (goto-char (point-min))
590 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
591 (or (while (looking-at "^C:")
594 ;; cyrus 1.6 imtest print "S: " before server greeting
595 (or (not (looking-at "S: "))
598 (not (and (imap-parse-greeting)
599 ;; success in imtest 1.6:
601 (concat "^\\(\\(Authenticat.*\\)\\|\\("
602 "Client authentication "
605 (setq response (match-string 1)))))
606 (accept-process-output process 1)
609 (with-current-buffer (get-buffer-create imap-log-buffer)
610 (imap-disable-multibyte)
611 (buffer-disable-undo)
612 (goto-char (point-max))
613 (insert-buffer-substring buffer)))
615 (message "GSSAPI IMAP connection: %s" (or response "failed"))
616 (if (and response (let ((case-fold-search nil))
617 (not (string-match "failed" response))))
619 (if (memq (process-status process) '(open run))
620 (imap-send-command "LOGOUT"))
621 (delete-process process)
625 (defun imap-ssl-p (buffer)
628 (defun imap-ssl-open (name buffer server port)
629 "Open a SSL connection to server."
630 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
631 (list imap-ssl-program)))
633 (while (and (not done) (setq cmd (pop cmds)))
634 (message "imap: Opening SSL connection with `%s'..." cmd)
636 (let ((port (or port imap-default-ssl-port))
637 (process-connection-type imap-process-connection-type)
638 (set-process-query-on-exit-flag
639 (if (fboundp 'set-process-query-on-exit-flag)
640 'set-process-query-on-exit-flag
641 'process-kill-without-query))
644 (setq process (as-binary-process
646 name buffer shell-file-name
651 ?p (number-to-string port))))))
652 (funcall set-process-query-on-exit-flag process nil))
653 (with-current-buffer buffer
654 (goto-char (point-min))
655 (while (and (memq (process-status process) '(open run))
656 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
657 (goto-char (point-max))
659 (not (imap-parse-greeting)))
660 (accept-process-output process 1)
663 (with-current-buffer (get-buffer-create imap-log-buffer)
664 (imap-disable-multibyte)
665 (buffer-disable-undo)
666 (goto-char (point-max))
667 (insert-buffer-substring buffer)))
669 (when (memq (process-status process) '(open run))
670 (setq done process))))))
673 (message "imap: Opening SSL connection with `%s'...done" cmd)
675 (message "imap: Opening SSL connection with `%s'...failed" cmd)
678 (defun imap-tls-p (buffer)
681 (defun imap-tls-open (name buffer server port)
682 (let* ((port (or port imap-default-tls-port))
683 (process (open-tls-stream name buffer server port)))
685 (while (and (memq (process-status process) '(open run))
686 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
687 (goto-char (point-max))
689 (not (imap-parse-greeting)))
690 (accept-process-output process 1)
693 (with-current-buffer (get-buffer-create imap-log-buffer)
694 (imap-disable-multibyte)
695 (buffer-disable-undo)
696 (goto-char (point-max))
697 (insert-buffer-substring buffer)))
698 (when (memq (process-status process) '(open run))
701 (defun imap-network-p (buffer)
704 (defun imap-network-open (name buffer server port)
705 (let* ((port (or port imap-default-port))
706 (process (open-network-stream-as-binary name buffer server port)))
708 (while (and (memq (process-status process) '(open run))
709 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
710 (goto-char (point-min))
711 (not (imap-parse-greeting)))
712 (accept-process-output process 1)
715 (with-current-buffer (get-buffer-create imap-log-buffer)
716 (imap-disable-multibyte)
717 (buffer-disable-undo)
718 (goto-char (point-max))
719 (insert-buffer-substring buffer)))
720 (when (memq (process-status process) '(open run))
723 (defun imap-shell-p (buffer)
726 (defun imap-shell-open (name buffer server port)
727 (let ((cmds (if (listp imap-shell-program) imap-shell-program
728 (list imap-shell-program)))
730 (while (and (not done) (setq cmd (pop cmds)))
731 (message "imap: Opening IMAP connection with `%s'..." cmd)
732 (setq imap-client-eol "\n")
733 (let* ((port (or port imap-default-port))
734 (process (as-binary-process
736 name buffer shell-file-name shell-command-switch
742 ?p (number-to-string port)
743 ?l imap-default-user))))))
745 (while (and (memq (process-status process) '(open run))
746 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
747 (goto-char (point-max))
749 (not (imap-parse-greeting)))
750 (accept-process-output process 1)
753 (with-current-buffer (get-buffer-create imap-log-buffer)
754 (imap-disable-multibyte)
755 (buffer-disable-undo)
756 (goto-char (point-max))
757 (insert-buffer-substring buffer)))
759 (when (memq (process-status process) '(open run))
760 (setq done process)))))
763 (message "imap: Opening IMAP connection with `%s'...done" cmd)
765 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
768 (defun imap-starttls-p (buffer)
769 (imap-capability 'STARTTLS buffer))
771 (defun imap-starttls-open (name buffer server port)
772 (let* ((port (or port imap-default-port))
773 (process (as-binary-process
774 (starttls-open-stream name buffer server port)))
776 (message "imap: Connecting with STARTTLS...")
778 (while (and (memq (process-status process) '(open run))
779 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
780 (goto-char (point-max))
782 (not (imap-parse-greeting)))
783 (accept-process-output process 1)
785 (imap-send-command "STARTTLS")
786 (while (and (memq (process-status process) '(open run))
787 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
788 (goto-char (point-max))
790 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
791 (accept-process-output process 1)
794 (with-current-buffer (get-buffer-create imap-log-buffer)
795 (buffer-disable-undo)
796 (goto-char (point-max))
797 (insert-buffer-substring buffer)))
798 (when (and (setq tls-info (starttls-negotiate process))
799 (memq (process-status process) '(open run)))
800 (setq done process)))
801 (if (stringp tls-info)
802 (message "imap: STARTTLS info: %s" tls-info))
803 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
806 ;; Server functions; authenticator stuff:
808 (defun imap-interactive-login (buffer loginfunc)
809 "Login to server in BUFFER.
810 LOGINFUNC is passed a username and a password, it should return t if
811 it where successful authenticating itself to the server, nil otherwise.
812 Returns t if login was successful, nil otherwise."
813 (with-current-buffer buffer
814 (make-local-variable 'imap-username)
815 (make-local-variable 'imap-password)
816 (let (user passwd ret)
817 ;; (condition-case ()
818 (while (or (not user) (not passwd))
819 (setq user (or imap-username
820 (read-from-minibuffer
821 (concat "IMAP username for " imap-server
822 " (using stream `" (symbol-name imap-stream)
824 (or user imap-default-user))))
825 (setq passwd (or imap-password
827 (concat "IMAP password for " user "@"
828 imap-server " (using authenticator `"
829 (symbol-name imap-auth) "'): "))))
830 (when (and user passwd)
831 (if (funcall loginfunc user passwd)
835 (when (and (not imap-password)
836 (or imap-store-password
837 (y-or-n-p "Store password for this session? ")))
838 (setq imap-password passwd)))
839 (message "Login failed...")
841 (setq imap-password nil)
843 ;; (quit (with-current-buffer buffer
846 ;; (error (with-current-buffer buffer
851 (defun imap-gssapi-auth-p (buffer)
852 (eq imap-stream 'gssapi))
854 (defun imap-gssapi-auth (buffer)
855 (message "imap: Authenticating using GSSAPI...%s"
856 (if (eq imap-stream 'gssapi) "done" "failed"))
857 (eq imap-stream 'gssapi))
859 (defun imap-kerberos4-auth-p (buffer)
860 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
861 (eq imap-stream 'kerberos4)))
863 (defun imap-kerberos4-auth (buffer)
864 (message "imap: Authenticating using Kerberos 4...%s"
865 (if (eq imap-stream 'kerberos4) "done" "failed"))
866 (eq imap-stream 'kerberos4))
868 (defun imap-cram-md5-p (buffer)
869 (imap-capability 'AUTH=CRAM-MD5 buffer))
871 (defun imap-cram-md5-auth (buffer)
872 "Login to server using the AUTH CRAM-MD5 method."
873 (message "imap: Authenticating using CRAM-MD5...")
874 (let ((done (imap-interactive-login
876 (lambda (user passwd)
878 (imap-send-command-wait
880 "AUTHENTICATE CRAM-MD5"
882 (let* ((decoded (base64-decode-string challenge))
884 (if (and (featurep 'xemacs)
885 (>= (function-max-args 'md5) 4))
886 (lambda (object &optional start end)
887 (md5 object start end 'binary))
889 (hash (rfc2104-hash hash-function 64 16
891 (response (concat user " " hash))
892 (encoded (base64-encode-string response)))
895 (message "imap: Authenticating using CRAM-MD5...done")
896 (message "imap: Authenticating using CRAM-MD5...failed"))))
898 (defun imap-login-p (buffer)
899 (and (not (imap-capability 'LOGINDISABLED buffer))
900 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
902 (defun imap-login-auth (buffer)
903 "Login to server using the LOGIN command."
904 (message "imap: Plaintext authentication...")
905 (imap-interactive-login buffer
906 (lambda (user passwd)
907 (imap-ok-p (imap-send-command-wait
908 (concat "LOGIN \"" user "\" \""
911 (defun imap-anonymous-p (buffer)
914 (defun imap-anonymous-auth (buffer)
915 (message "imap: Logging in anonymously...")
916 (with-current-buffer buffer
917 (imap-ok-p (imap-send-command-wait
918 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
919 (system-name)) "\"")))))
921 ;;; Compiler directives.
923 (defvar imap-sasl-client)
924 (defvar imap-sasl-step)
926 (defun imap-sasl-make-mechanisms (buffer)
929 (let ((name (symbol-name sym)))
930 (if (and (> (length name) 5)
931 (string-equal "AUTH=" (substring name 0 5 )))
932 (setq mecs (cons (substring name 5) mecs)))))
933 (imap-capability nil buffer))
936 (defun imap-sasl-auth-p (buffer)
937 (and (condition-case ()
940 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
942 (defun imap-sasl-auth (buffer)
943 "Login to server using the SASL method."
944 (message "imap: Authenticating using SASL...")
945 (with-current-buffer buffer
946 (make-local-variable 'imap-username)
947 (make-local-variable 'imap-sasl-client)
948 (make-local-variable 'imap-sasl-step)
949 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
952 (setq user (or imap-username
953 (read-from-minibuffer
954 (concat "IMAP username for " imap-server " using SASL "
955 (sasl-mechanism-name mechanism) ": ")
956 (or user imap-default-user))))
958 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
959 imap-sasl-step (sasl-next-step imap-sasl-client nil))
960 (let ((tag (imap-send-command
961 (if (sasl-step-data imap-sasl-step)
962 (format "AUTHENTICATE %s %s"
963 (sasl-mechanism-name mechanism)
964 (sasl-step-data imap-sasl-step))
965 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
967 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
968 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
969 (setq imap-continuation nil
970 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
971 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
972 (base64-encode-string (sasl-step-data imap-sasl-step) t)
974 (if (imap-ok-p (imap-wait-for-tag tag))
975 (setq imap-username user
977 (message "Login failed...")
981 (defun imap-digest-md5-p (buffer)
982 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
984 (require 'digest-md5)
987 (defun imap-digest-md5-auth (buffer)
988 "Login to server using the AUTH DIGEST-MD5 method."
989 (message "imap: Authenticating using DIGEST-MD5...")
990 (imap-interactive-login
992 (lambda (user passwd)
996 "AUTHENTICATE DIGEST-MD5"
998 (digest-md5-parse-digest-challenge
999 (base64-decode-string challenge))
1001 (digest-md5-digest-uri
1002 "imap" (digest-md5-challenge 'realm)))
1004 (digest-md5-digest-response
1005 user passwd digest-uri)))
1006 (base64-encode-string response 'no-line-break))))
1008 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1010 (setq imap-continuation nil)
1011 (imap-send-command-1 "")
1012 (imap-ok-p (imap-wait-for-tag tag)))))))
1014 ;; Server functions:
1016 (defun imap-open-1 (buffer)
1017 (with-current-buffer buffer
1019 (setq imap-current-mailbox nil
1020 imap-current-message nil
1022 imap-process (condition-case ()
1023 (funcall (nth 2 (assq imap-stream
1025 "imap" buffer imap-server imap-port)
1026 ((error quit) nil)))
1028 (set-process-filter imap-process 'imap-arrival-filter)
1029 (set-process-sentinel imap-process 'imap-sentinel)
1030 (while (and (eq imap-state 'initial)
1031 (memq (process-status imap-process) '(open run)))
1032 (message "Waiting for response from %s..." imap-server)
1033 (accept-process-output imap-process 1))
1034 (message "Waiting for response from %s...done" imap-server)
1035 (and (memq (process-status imap-process) '(open run))
1038 (defun imap-open (server &optional port stream auth buffer)
1039 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1040 If PORT is unspecified, a default value is used (143 except
1041 for SSL which use 993).
1042 STREAM indicates the stream to use, see `imap-streams' for available
1043 streams. If nil, it choices the best stream the server is capable of.
1044 AUTH indicates authenticator to use, see `imap-authenticators' for
1045 available authenticators. If nil, it choices the best stream the
1046 server is capable of.
1047 BUFFER can be a buffer or a name of a buffer, which is created if
1048 necessary. If nil, the buffer name is generated."
1049 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1050 (with-current-buffer (get-buffer-create buffer)
1051 (if (imap-opened buffer)
1052 (imap-close buffer))
1053 (mapcar 'make-local-variable imap-local-variables)
1054 (imap-disable-multibyte)
1055 (buffer-disable-undo)
1056 (setq imap-server (or server imap-server))
1057 (setq imap-port (or port imap-port))
1058 (setq imap-auth (or auth imap-auth))
1059 (setq imap-stream (or stream imap-stream))
1060 (message "imap: Connecting to %s..." imap-server)
1061 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1062 (imap-open-1 buffer)))
1064 (message "imap: Connecting to %s...failed" imap-server)
1066 (when (null imap-stream)
1067 ;; Need to choose stream.
1068 (let ((streams imap-streams))
1069 (while (setq stream (pop streams))
1070 ;; OK to use this stream?
1071 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1073 (if (not (eq imap-default-stream stream))
1074 (with-current-buffer (get-buffer-create
1075 (generate-new-buffer-name " *temp*"))
1076 (mapcar 'make-local-variable imap-local-variables)
1077 (imap-disable-multibyte)
1078 (buffer-disable-undo)
1079 (setq imap-server (or server imap-server))
1080 (setq imap-port (or port imap-port))
1081 (setq imap-auth (or auth imap-auth))
1082 (message "imap: Reconnecting with stream `%s'..." stream)
1083 (if (null (let ((imap-stream stream))
1084 (imap-open-1 (current-buffer))))
1086 (kill-buffer (current-buffer))
1088 "imap: Reconnecting with stream `%s'...failed"
1090 ;; We're done, kill the first connection
1092 (kill-buffer buffer)
1093 (rename-buffer buffer)
1094 (message "imap: Reconnecting with stream `%s'...done"
1096 (setq imap-stream stream)
1097 (setq imap-capability nil)
1098 (setq streams nil)))
1100 (message "imap: Connecting to %s...done" imap-server)
1101 (setq imap-stream stream)
1102 (setq imap-capability nil)
1103 (setq streams nil))))))
1104 (when (imap-opened buffer)
1105 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1109 (defun imap-opened (&optional buffer)
1110 "Return non-nil if connection to imap server in BUFFER is open.
1111 If BUFFER is nil then the current buffer is used."
1112 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1113 (buffer-live-p buffer)
1114 (with-current-buffer buffer
1116 (memq (process-status imap-process) '(open run))))))
1118 (defun imap-authenticate (&optional user passwd buffer)
1119 "Authenticate to server in BUFFER, using current buffer if nil.
1120 It uses the authenticator specified when opening the server. If the
1121 authenticator requires username/passwords, they are queried from the
1122 user and optionally stored in the buffer. If USER and/or PASSWD is
1123 specified, the user will not be questioned and the username and/or
1124 password is remembered in the buffer."
1125 (with-current-buffer (or buffer (current-buffer))
1126 (if (not (eq imap-state 'nonauth))
1127 (or (eq imap-state 'auth)
1128 (eq imap-state 'selected)
1129 (eq imap-state 'examine))
1130 (make-local-variable 'imap-username)
1131 (make-local-variable 'imap-password)
1132 (if user (setq imap-username user))
1133 (if passwd (setq imap-password passwd))
1135 (and (funcall (nth 2 (assq imap-auth
1136 imap-authenticator-alist)) buffer)
1137 (setq imap-state 'auth))
1138 ;; Choose authenticator.
1139 (let ((auths imap-authenticators)
1141 (while (setq auth (pop auths))
1142 ;; OK to use authenticator?
1143 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1144 (message "imap: Authenticating to `%s' using `%s'..."
1146 (setq imap-auth auth)
1147 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1149 (message "imap: Authenticating to `%s' using `%s'...done"
1152 (message "imap: Authenticating to `%s' using `%s'...failed"
1153 imap-server auth)))))
1156 (defun imap-close (&optional buffer)
1157 "Close connection to server in BUFFER.
1158 If BUFFER is nil, the current buffer is used."
1159 (with-current-buffer (or buffer (current-buffer))
1162 (imap-send-command-wait "LOGOUT")
1164 (when (and imap-process
1165 (memq (process-status imap-process) '(open run)))
1166 (delete-process imap-process))
1167 (setq imap-current-mailbox nil
1168 imap-current-message nil
1173 (defun imap-capability (&optional identifier buffer)
1174 "Return a list of identifiers which server in BUFFER support.
1175 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1176 If BUFFER is nil, the current buffer is assumed."
1177 (with-current-buffer (or buffer (current-buffer))
1178 (unless imap-capability
1179 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1180 (setq imap-capability '(IMAP2))))
1182 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1185 (defun imap-id (&optional list-of-values buffer)
1186 "Identify client to server in BUFFER, and return server identity.
1187 LIST-OF-VALUES is nil, or a plist with identifier and value
1188 strings to send to the server to identify the client.
1190 Return a list of identifiers which server in BUFFER support, or
1191 nil if it doesn't support ID or returns no information.
1193 If BUFFER is nil, the current buffer is assumed."
1194 (with-current-buffer (or buffer (current-buffer))
1195 (when (and (imap-capability 'ID)
1196 (imap-ok-p (imap-send-command-wait
1197 (if (null list-of-values)
1199 (concat "ID (" (mapconcat (lambda (el)
1200 (concat "\"" el "\""))
1205 (defun imap-namespace (&optional buffer)
1206 "Return a namespace hierarchy at server in BUFFER.
1207 If BUFFER is nil, the current buffer is assumed."
1208 (with-current-buffer (or buffer (current-buffer))
1209 (unless imap-namespace
1210 (when (imap-capability 'NAMESPACE)
1211 (imap-send-command-wait "NAMESPACE")))
1214 (defun imap-send-command-wait (command &optional buffer)
1215 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1218 ;; Mailbox functions:
1220 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1221 (with-current-buffer (or buffer (current-buffer))
1222 (if imap-mailbox-data
1223 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1225 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1226 propname value mailbox (current-buffer)))
1229 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1230 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1233 (defun imap-mailbox-get (propname &optional mailbox buffer)
1234 (let ((mailbox (imap-utf7-encode mailbox)))
1235 (with-current-buffer (or buffer (current-buffer))
1236 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1238 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1239 (with-current-buffer (or buffer (current-buffer))
1243 (push (funcall func (if mailbox-decoder
1244 (funcall mailbox-decoder (symbol-name s))
1245 (symbol-name s))) result))
1249 (defun imap-mailbox-map (func &optional buffer)
1250 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1251 Function should take a mailbox name (a string) as
1253 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1255 (defun imap-current-mailbox (&optional buffer)
1256 (with-current-buffer (or buffer (current-buffer))
1257 (imap-utf7-decode imap-current-mailbox)))
1259 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1260 (and (string= mailbox imap-current-mailbox)
1262 (eq imap-state 'examine))
1264 (eq imap-state 'selected)))))
1266 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1267 (with-current-buffer (or buffer (current-buffer))
1268 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1270 (defun imap-mailbox-select-1 (mailbox &optional examine)
1271 "Select MAILBOX on server in BUFFER.
1272 If EXAMINE is non-nil, do a read-only select."
1273 (if (imap-current-mailbox-p-1 mailbox examine)
1274 imap-current-mailbox
1275 (setq imap-current-mailbox mailbox)
1276 (if (imap-ok-p (imap-send-command-wait
1277 (concat (if examine "EXAMINE" "SELECT") " \""
1280 (setq imap-message-data (make-vector imap-message-prime 0)
1281 imap-state (if examine 'examine 'selected))
1282 imap-current-mailbox)
1283 ;; Failed SELECT/EXAMINE unselects current mailbox
1284 (setq imap-current-mailbox nil))))
1286 (defun imap-mailbox-select (mailbox &optional examine buffer)
1287 (with-current-buffer (or buffer (current-buffer))
1289 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1291 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1292 (with-current-buffer (or buffer (current-buffer))
1293 (imap-mailbox-select-1 mailbox 'examine)))
1295 (defun imap-mailbox-examine (mailbox &optional buffer)
1296 "Examine MAILBOX on server in BUFFER."
1297 (imap-mailbox-select mailbox 'examine buffer))
1299 (defun imap-mailbox-unselect (&optional buffer)
1300 "Close current folder in BUFFER, without expunging articles."
1301 (with-current-buffer (or buffer (current-buffer))
1302 (when (or (eq imap-state 'auth)
1303 (and (imap-capability 'UNSELECT)
1304 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1306 (imap-send-command-wait (concat "EXAMINE \""
1307 imap-current-mailbox
1309 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1310 (setq imap-current-mailbox nil
1311 imap-message-data nil
1315 (defun imap-mailbox-expunge (&optional asynch buffer)
1316 "Expunge articles in current folder in BUFFER.
1317 If ASYNCH, do not wait for succesful completion of the command.
1318 If BUFFER is nil the current buffer is assumed."
1319 (with-current-buffer (or buffer (current-buffer))
1320 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1322 (imap-send-command "EXPUNGE")
1323 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1325 (defun imap-mailbox-close (&optional asynch buffer)
1326 "Expunge articles and close current folder in BUFFER.
1327 If ASYNCH, do not wait for succesful completion of the command.
1328 If BUFFER is nil the current buffer is assumed."
1329 (with-current-buffer (or buffer (current-buffer))
1330 (when imap-current-mailbox
1332 (imap-add-callback (imap-send-command "CLOSE")
1333 `(lambda (tag status)
1334 (message "IMAP mailbox `%s' closed... %s"
1335 imap-current-mailbox status)
1336 (when (eq ,imap-current-mailbox
1337 imap-current-mailbox)
1338 ;; Don't wipe out data if another mailbox
1340 (setq imap-current-mailbox nil
1341 imap-message-data nil
1342 imap-state 'auth))))
1343 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1344 (setq imap-current-mailbox nil
1345 imap-message-data nil
1349 (defun imap-mailbox-create-1 (mailbox)
1350 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1352 (defun imap-mailbox-create (mailbox &optional buffer)
1353 "Create MAILBOX on server in BUFFER.
1354 If BUFFER is nil the current buffer is assumed."
1355 (with-current-buffer (or buffer (current-buffer))
1356 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1358 (defun imap-mailbox-delete (mailbox &optional buffer)
1359 "Delete MAILBOX on server in BUFFER.
1360 If BUFFER is nil the current buffer is assumed."
1361 (let ((mailbox (imap-utf7-encode mailbox)))
1362 (with-current-buffer (or buffer (current-buffer))
1364 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1366 (defun imap-mailbox-rename (oldname newname &optional buffer)
1367 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1368 If BUFFER is nil the current buffer is assumed."
1369 (let ((oldname (imap-utf7-encode oldname))
1370 (newname (imap-utf7-encode newname)))
1371 (with-current-buffer (or buffer (current-buffer))
1373 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1374 "\"" newname "\""))))))
1376 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1377 "Return a list of subscribed mailboxes on server in BUFFER.
1378 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1379 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1380 implementation-specific string that has to be passed to lsub command."
1381 (with-current-buffer (or buffer (current-buffer))
1382 ;; Make sure we know the hierarchy separator for root's hierarchy
1383 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1384 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1385 (imap-utf7-encode root) "\"")))
1386 ;; clear list data (NB not delimiter and other stuff)
1387 (imap-mailbox-map-1 (lambda (mailbox)
1388 (imap-mailbox-put 'lsub nil mailbox)))
1390 (imap-send-command-wait
1391 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1392 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1395 (imap-mailbox-map-1 (lambda (mailbox)
1396 (when (imap-mailbox-get-1 'lsub mailbox)
1397 (push (imap-utf7-decode mailbox) out))))
1400 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1401 "Return a list of mailboxes matching ROOT on server in BUFFER.
1402 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1403 root. REFERENCE is a implementation-specific string that has to be
1404 passed to list command."
1405 (with-current-buffer (or buffer (current-buffer))
1406 ;; Make sure we know the hierarchy separator for root's hierarchy
1407 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1408 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1409 (imap-utf7-encode root) "\"")))
1410 ;; clear list data (NB not delimiter and other stuff)
1411 (imap-mailbox-map-1 (lambda (mailbox)
1412 (imap-mailbox-put 'list nil mailbox)))
1414 (imap-send-command-wait
1415 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1416 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1419 (imap-mailbox-map-1 (lambda (mailbox)
1420 (when (imap-mailbox-get-1 'list mailbox)
1421 (push (imap-utf7-decode mailbox) out))))
1424 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1425 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1426 Returns non-nil if successful."
1427 (with-current-buffer (or buffer (current-buffer))
1428 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1429 (imap-utf7-encode mailbox)
1432 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1433 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1434 Returns non-nil if successful."
1435 (with-current-buffer (or buffer (current-buffer))
1436 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1437 (imap-utf7-encode mailbox)
1440 (defun imap-mailbox-status (mailbox items &optional buffer)
1441 "Get status items ITEM in MAILBOX from server in BUFFER.
1442 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1443 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1444 or 'unseen. If ITEMS is a list of symbols, a list of values is
1445 returned, if ITEMS is a symbol only its value is returned."
1446 (with-current-buffer (or buffer (current-buffer))
1448 (imap-send-command-wait (list "STATUS \""
1449 (imap-utf7-encode mailbox)
1457 (mapcar (lambda (item)
1458 (imap-mailbox-get item mailbox))
1460 (imap-mailbox-get items mailbox)))))
1462 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1463 "Send status item request ITEM on MAILBOX to server in BUFFER.
1464 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1465 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1466 or 'unseen. The IMAP command tag is returned."
1467 (with-current-buffer (or buffer (current-buffer))
1468 (imap-send-command (list "STATUS \""
1469 (imap-utf7-encode mailbox)
1476 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1477 "Get ACL on mailbox from server in BUFFER."
1478 (let ((mailbox (imap-utf7-encode mailbox)))
1479 (with-current-buffer (or buffer (current-buffer))
1481 (imap-send-command-wait (list "GETACL \""
1482 (or mailbox imap-current-mailbox)
1484 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1486 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1487 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1488 (let ((mailbox (imap-utf7-encode mailbox)))
1489 (with-current-buffer (or buffer (current-buffer))
1491 (imap-send-command-wait (list "SETACL \""
1492 (or mailbox imap-current-mailbox)
1498 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1499 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1500 (let ((mailbox (imap-utf7-encode mailbox)))
1501 (with-current-buffer (or buffer (current-buffer))
1503 (imap-send-command-wait (list "DELETEACL \""
1504 (or mailbox imap-current-mailbox)
1509 ;; Message functions:
1511 (defun imap-current-message (&optional buffer)
1512 (with-current-buffer (or buffer (current-buffer))
1513 imap-current-message))
1515 (defun imap-list-to-message-set (list)
1516 (mapconcat (lambda (item)
1517 (number-to-string item))
1523 (defun imap-range-to-message-set (range)
1528 (car item) (cdr item))
1529 (format "%d" item)))
1530 (if (and (listp range) (not (listp (cdr range))))
1531 (list range) ;; make (1 . 2) into ((1 . 2))
1535 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1536 (with-current-buffer (or buffer (current-buffer))
1537 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1539 (imap-list-to-message-set uids)
1543 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1544 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1545 UIDS can be a string, number or a list of numbers. If RECEIVE
1546 is non-nil return these properties."
1547 (with-current-buffer (or buffer (current-buffer))
1548 (when (imap-ok-p (imap-send-command-wait
1549 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1551 (imap-list-to-message-set uids)
1554 (if (or (null receive) (stringp uids))
1557 (mapcar (lambda (uid)
1559 (mapcar (lambda (prop)
1560 (imap-message-get uid prop))
1562 (imap-message-get uid receive)))
1564 (imap-message-get uids receive))))))
1566 (defun imap-message-put (uid propname value &optional buffer)
1567 (with-current-buffer (or buffer (current-buffer))
1568 (if imap-message-data
1569 (put (intern (number-to-string uid) imap-message-data)
1571 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1572 uid propname value (current-buffer)))
1575 (defun imap-message-get (uid propname &optional buffer)
1576 (with-current-buffer (or buffer (current-buffer))
1577 (get (intern-soft (number-to-string uid) imap-message-data)
1580 (defun imap-message-map (func propname &optional buffer)
1581 "Map a function across each mailbox in `imap-message-data', returning a list."
1582 (with-current-buffer (or buffer (current-buffer))
1586 (push (funcall func (get s 'UID) (get s propname)) result))
1590 (defmacro imap-message-envelope-date (uid &optional buffer)
1591 `(with-current-buffer (or ,buffer (current-buffer))
1592 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1594 (defmacro imap-message-envelope-subject (uid &optional buffer)
1595 `(with-current-buffer (or ,buffer (current-buffer))
1596 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1598 (defmacro imap-message-envelope-from (uid &optional buffer)
1599 `(with-current-buffer (or ,buffer (current-buffer))
1600 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1602 (defmacro imap-message-envelope-sender (uid &optional buffer)
1603 `(with-current-buffer (or ,buffer (current-buffer))
1604 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1606 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1607 `(with-current-buffer (or ,buffer (current-buffer))
1608 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1610 (defmacro imap-message-envelope-to (uid &optional buffer)
1611 `(with-current-buffer (or ,buffer (current-buffer))
1612 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1614 (defmacro imap-message-envelope-cc (uid &optional buffer)
1615 `(with-current-buffer (or ,buffer (current-buffer))
1616 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1618 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1619 `(with-current-buffer (or ,buffer (current-buffer))
1620 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1622 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1623 `(with-current-buffer (or ,buffer (current-buffer))
1624 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1626 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1627 `(with-current-buffer (or ,buffer (current-buffer))
1628 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1630 (defmacro imap-message-body (uid &optional buffer)
1631 `(with-current-buffer (or ,buffer (current-buffer))
1632 (imap-message-get ,uid 'BODY)))
1634 (defun imap-search (predicate &optional buffer)
1635 (with-current-buffer (or buffer (current-buffer))
1636 (imap-mailbox-put 'search 'dummy)
1637 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1638 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1640 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1642 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1644 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1645 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1646 (with-current-buffer (or buffer (current-buffer))
1647 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1648 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1650 (defun imap-message-flags-set (articles flags &optional silent buffer)
1651 (when (and articles flags)
1652 (with-current-buffer (or buffer (current-buffer))
1653 (imap-ok-p (imap-send-command-wait
1654 (concat "UID STORE " articles
1655 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1657 (defun imap-message-flags-del (articles flags &optional silent buffer)
1658 (when (and articles flags)
1659 (with-current-buffer (or buffer (current-buffer))
1660 (imap-ok-p (imap-send-command-wait
1661 (concat "UID STORE " articles
1662 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1664 (defun imap-message-flags-add (articles flags &optional silent buffer)
1665 (when (and articles flags)
1666 (with-current-buffer (or buffer (current-buffer))
1667 (imap-ok-p (imap-send-command-wait
1668 (concat "UID STORE " articles
1669 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1671 (defun imap-message-copyuid-1 (mailbox)
1672 (if (imap-capability 'UIDPLUS)
1673 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1674 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1675 (let ((old-mailbox imap-current-mailbox)
1677 (imap-message-data (make-vector 2 0)))
1678 (when (imap-mailbox-examine-1 mailbox)
1680 (and (imap-fetch "*" "UID")
1681 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1682 (apply 'max (imap-message-map
1683 (lambda (uid prop) uid) 'UID))))
1685 (imap-mailbox-select old-mailbox (eq state 'examine))
1686 (imap-mailbox-unselect)))))))
1688 (defun imap-message-copyuid (mailbox &optional buffer)
1689 (with-current-buffer (or buffer (current-buffer))
1690 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1692 (defun imap-message-copy (articles mailbox
1693 &optional dont-create no-copyuid buffer)
1694 "Copy ARTICLES (a string message set) to MAILBOX on server in
1695 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1696 non-nil, it will not create a mailbox. On success, return a list with
1697 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1698 first element, rest of list contain the saved articles' UIDs."
1700 (with-current-buffer (or buffer (current-buffer))
1701 (let ((mailbox (imap-utf7-encode mailbox)))
1702 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1703 (imap-current-target-mailbox mailbox))
1704 (if (imap-ok-p (imap-send-command-wait cmd))
1706 (when (and (not dont-create)
1707 ;; removed because of buggy Oracle server
1708 ;; that doesn't send TRYCREATE tags (which
1709 ;; is a MUST according to specifications):
1710 ;;(imap-mailbox-get-1 'trycreate mailbox)
1711 (imap-mailbox-create-1 mailbox))
1712 (imap-ok-p (imap-send-command-wait cmd)))))
1714 (imap-message-copyuid-1 mailbox)))))))
1716 (defun imap-message-appenduid-1 (mailbox)
1717 (if (imap-capability 'UIDPLUS)
1718 (imap-mailbox-get-1 'appenduid mailbox)
1719 (let ((old-mailbox imap-current-mailbox)
1721 (imap-message-data (make-vector 2 0)))
1722 (when (imap-mailbox-examine-1 mailbox)
1724 (and (imap-fetch "*" "UID")
1725 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1726 (apply 'max (imap-message-map
1727 (lambda (uid prop) uid) 'UID))))
1729 (imap-mailbox-select old-mailbox (eq state 'examine))
1730 (imap-mailbox-unselect)))))))
1732 (defun imap-message-appenduid (mailbox &optional buffer)
1733 (with-current-buffer (or buffer (current-buffer))
1734 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1736 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1737 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1738 FLAGS and DATE-TIME is currently not used. Return a cons holding
1739 uidvalidity of MAILBOX and UID the newly created article got, or nil
1741 (let ((mailbox (imap-utf7-encode mailbox)))
1742 (with-current-buffer (or buffer (current-buffer))
1743 (and (let ((imap-current-target-mailbox mailbox))
1745 (imap-send-command-wait
1746 (list "APPEND \"" mailbox "\" " article))))
1747 (imap-message-appenduid-1 mailbox)))))
1749 (defun imap-body-lines (body)
1750 "Return number of lines in article by looking at the mime bodystructure BODY."
1752 (if (stringp (car body))
1753 (cond ((and (string= (upcase (car body)) "TEXT")
1754 (numberp (nth 7 body)))
1756 ((and (string= (upcase (car body)) "MESSAGE")
1757 (numberp (nth 9 body)))
1760 (apply '+ (mapcar 'imap-body-lines body)))
1763 (defun imap-envelope-from (from)
1764 "Return a from string line."
1766 (concat (aref from 0)
1767 (if (aref from 0) " <")
1771 (if (aref from 0) ">"))))
1774 ;; Internal functions.
1776 (defun imap-add-callback (tag func)
1777 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1779 (defun imap-send-command-1 (cmdstr)
1780 (setq cmdstr (concat cmdstr imap-client-eol))
1782 (with-current-buffer (get-buffer-create imap-log-buffer)
1783 (imap-disable-multibyte)
1784 (buffer-disable-undo)
1785 (goto-char (point-max))
1787 (process-send-string imap-process cmdstr))
1789 (defun imap-send-command (command &optional buffer)
1790 (with-current-buffer (or buffer (current-buffer))
1791 (if (not (listp command)) (setq command (list command)))
1792 (let ((tag (setq imap-tag (1+ imap-tag)))
1794 (setq cmdstr (concat (number-to-string imap-tag) " "))
1795 (while (setq cmd (pop command))
1796 (cond ((stringp cmd)
1797 (setq cmdstr (concat cmdstr cmd)))
1799 (let ((eol imap-client-eol)
1800 (calcfirst imap-calculate-literal-size-first)
1802 (with-current-buffer cmd
1804 (setq size (buffer-size)))
1805 (when (not (equal eol "\r\n"))
1806 ;; XXX modifies buffer!
1807 (goto-char (point-min))
1808 (while (search-forward "\r\n" nil t)
1809 (replace-match eol)))
1811 (setq size (buffer-size))))
1813 (concat cmdstr (format "{%d}" size))))
1816 (imap-send-command-1 cmdstr)
1818 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1819 (setq command nil) ;; abort command if no cont-req
1820 (let ((process imap-process)
1821 (stream imap-stream)
1822 (eol imap-client-eol))
1823 (with-current-buffer cmd
1825 (with-current-buffer (get-buffer-create
1827 (imap-disable-multibyte)
1828 (buffer-disable-undo)
1829 (goto-char (point-max))
1830 (insert-buffer-substring cmd)))
1831 (process-send-region process (point-min)
1833 (process-send-string process imap-client-eol))))
1834 (setq imap-continuation nil)))
1836 (imap-send-command-1 cmdstr)
1839 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1840 (setq command nil) ;; abort command if no cont-req
1841 (setq command (cons (funcall cmd imap-continuation)
1843 (setq imap-continuation nil)))
1845 (error "Unknown command type"))))
1847 (imap-send-command-1 cmdstr))
1850 (defun imap-wait-for-tag (tag &optional buffer)
1851 (with-current-buffer (or buffer (current-buffer))
1852 (let (imap-have-messaged)
1853 (while (and (null imap-continuation)
1854 (memq (process-status imap-process) '(open run))
1855 (< imap-reached-tag tag))
1856 (let ((len (/ (point-max) 1024))
1859 (setq imap-have-messaged t)
1860 (message "imap read: %dk" len))
1861 (accept-process-output imap-process
1862 (truncate imap-read-timeout)
1863 (truncate (* (- imap-read-timeout
1864 (truncate imap-read-timeout))
1866 ;; A process can die _before_ we have processed everything it
1867 ;; has to say. Moreover, this can happen in between the call to
1868 ;; accept-process-output and the call to process-status in an
1869 ;; iteration of the loop above.
1870 (when (and (null imap-continuation)
1871 (< imap-reached-tag tag))
1872 (accept-process-output imap-process 0 0))
1873 (when imap-have-messaged
1875 (and (memq (process-status imap-process) '(open run))
1876 (or (assq tag imap-failed-tags)
1877 (if imap-continuation
1881 (defun imap-sentinel (process string)
1882 (delete-process process))
1884 (defun imap-find-next-line ()
1885 "Return point at end of current line, taking into account literals.
1886 Return nil if no complete line has arrived."
1887 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1890 (if (match-string 1)
1891 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1893 (goto-char (+ (point) (string-to-number (match-string 1))))
1894 (imap-find-next-line))
1897 (defun imap-arrival-filter (proc string)
1898 "IMAP process filter."
1899 ;; Sometimes, we are called even though the process has died.
1900 ;; Better abstain from doing stuff in that case.
1901 (when (buffer-name (process-buffer proc))
1902 (with-current-buffer (process-buffer proc)
1903 (goto-char (point-max))
1906 (with-current-buffer (get-buffer-create imap-log-buffer)
1907 (imap-disable-multibyte)
1908 (buffer-disable-undo)
1909 (goto-char (point-max))
1912 (goto-char (point-min))
1913 (while (setq end (imap-find-next-line))
1915 (narrow-to-region (point-min) end)
1916 (delete-backward-char (length imap-server-eol))
1917 (goto-char (point-min))
1919 (cond ((eq imap-state 'initial)
1920 (imap-parse-greeting))
1921 ((or (eq imap-state 'auth)
1922 (eq imap-state 'nonauth)
1923 (eq imap-state 'selected)
1924 (eq imap-state 'examine))
1925 (imap-parse-response))
1927 (message "Unknown state %s in arrival filter"
1929 (delete-region (point-min) (point-max)))))))))
1934 (defsubst imap-forward ()
1935 (or (eobp) (forward-char)))
1938 ;; ; Unsigned 32-bit integer
1939 ;; ; (0 <= n < 4,294,967,296)
1941 (defsubst imap-parse-number ()
1942 (when (looking-at "[0-9]+")
1944 (string-to-number (match-string 0))
1945 (goto-char (match-end 0)))))
1947 ;; literal = "{" number "}" CRLF *CHAR8
1948 ;; ; Number represents the number of CHAR8s
1950 (defsubst imap-parse-literal ()
1951 (when (looking-at "{\\([0-9]+\\)}\r\n")
1952 (let ((pos (match-end 0))
1953 (len (string-to-number (match-string 1))))
1954 (if (< (point-max) (+ pos len))
1956 (goto-char (+ pos len))
1957 (buffer-substring pos (+ pos len))))))
1959 ;; string = quoted / literal
1961 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1963 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1964 ;; "\" quoted-specials
1966 ;; quoted-specials = DQUOTE / "\"
1968 ;; TEXT-CHAR = <any CHAR except CR and LF>
1970 (defsubst imap-parse-string ()
1971 (cond ((eq (char-after) ?\")
1973 (let ((p (point)) (name ""))
1974 (skip-chars-forward "^\"\\\\")
1975 (setq name (buffer-substring p (point)))
1976 (while (eq (char-after) ?\\)
1977 (setq p (1+ (point)))
1979 (skip-chars-forward "^\"\\\\")
1980 (setq name (concat name (buffer-substring p (point)))))
1983 ((eq (char-after) ?{)
1984 (imap-parse-literal))))
1988 (defsubst imap-parse-nil ()
1989 (if (looking-at "NIL")
1990 (goto-char (match-end 0))))
1992 ;; nstring = string / nil
1994 (defsubst imap-parse-nstring ()
1995 (or (imap-parse-string)
1996 (and (imap-parse-nil)
1999 ;; astring = atom / string
2001 ;; atom = 1*ATOM-CHAR
2003 ;; ATOM-CHAR = <any CHAR except atom-specials>
2005 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2008 ;; list-wildcards = "%" / "*"
2010 ;; quoted-specials = DQUOTE / "\"
2012 (defsubst imap-parse-astring ()
2013 (or (imap-parse-string)
2014 (buffer-substring (point)
2015 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2016 (goto-char (1- (match-end 0)))
2020 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2023 ;; addr-adl = nstring
2024 ;; ; Holds route from [RFC-822] route-addr if
2027 ;; addr-host = nstring
2028 ;; ; nil indicates [RFC-822] group syntax.
2029 ;; ; Otherwise, holds [RFC-822] domain name
2031 ;; addr-mailbox = nstring
2032 ;; ; nil indicates end of [RFC-822] group; if
2033 ;; ; non-nil and addr-host is nil, holds
2034 ;; ; [RFC-822] group name.
2035 ;; ; Otherwise, holds [RFC-822] local-part
2036 ;; ; after removing [RFC-822] quoting
2038 ;; addr-name = nstring
2039 ;; ; If non-nil, holds phrase from [RFC-822]
2040 ;; ; mailbox after removing [RFC-822] quoting
2043 (defsubst imap-parse-address ()
2045 (when (eq (char-after) ?\()
2047 (setq address (vector (prog1 (imap-parse-nstring)
2049 (prog1 (imap-parse-nstring)
2051 (prog1 (imap-parse-nstring)
2053 (imap-parse-nstring)))
2054 (when (eq (char-after) ?\))
2058 ;; address-list = "(" 1*address ")" / nil
2062 (defsubst imap-parse-address-list ()
2063 (if (eq (char-after) ?\()
2064 (let (address addresses)
2066 (while (and (not (eq (char-after) ?\)))
2067 ;; next line for MS Exchange bug
2068 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2069 (setq address (imap-parse-address)))
2070 (setq addresses (cons address addresses)))
2071 (when (eq (char-after) ?\))
2073 (nreverse addresses)))
2074 ;; With assert, the code might not be eval'd.
2075 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2078 ;; mailbox = "INBOX" / astring
2079 ;; ; INBOX is case-insensitive. All case variants of
2080 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2081 ;; ; not as an astring. An astring which consists of
2082 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2083 ;; ; is considered to be INBOX and not an astring.
2084 ;; ; Refer to section 5.1 for further
2085 ;; ; semantic details of mailbox names.
2087 (defsubst imap-parse-mailbox ()
2088 (let ((mailbox (imap-parse-astring)))
2089 (if (string-equal "INBOX" (upcase mailbox))
2093 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2095 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2096 ;; ; Authentication condition
2098 ;; resp-cond-bye = "BYE" SP resp-text
2100 (defun imap-parse-greeting ()
2101 "Parse a IMAP greeting."
2102 (cond ((looking-at "\\* OK ")
2103 (setq imap-state 'nonauth))
2104 ((looking-at "\\* PREAUTH ")
2105 (setq imap-state 'auth))
2106 ((looking-at "\\* BYE ")
2107 (setq imap-state 'closed))))
2109 ;; response = *(continue-req / response-data) response-done
2111 ;; continue-req = "+" SP (resp-text / base64) CRLF
2113 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2114 ;; mailbox-data / message-data / capability-data) CRLF
2116 ;; response-done = response-tagged / response-fatal
2118 ;; response-fatal = "*" SP resp-cond-bye CRLF
2119 ;; ; Server closes connection immediately
2121 ;; response-tagged = tag SP resp-cond-state CRLF
2123 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2124 ;; ; Status condition
2126 ;; resp-cond-bye = "BYE" SP resp-text
2128 ;; mailbox-data = "FLAGS" SP flag-list /
2129 ;; "LIST" SP mailbox-list /
2130 ;; "LSUB" SP mailbox-list /
2131 ;; "SEARCH" *(SP nz-number) /
2132 ;; "STATUS" SP mailbox SP "("
2133 ;; [status-att SP number *(SP status-att SP number)] ")" /
2134 ;; number SP "EXISTS" /
2135 ;; number SP "RECENT"
2137 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2139 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2141 ;; ; IMAP4rev1 servers which offer RFC 1730
2142 ;; ; compatibility MUST list "IMAP4" as the first
2145 (defun imap-parse-response ()
2146 "Parse a IMAP command response."
2148 (case (setq token (read (current-buffer)))
2149 (+ (setq imap-continuation
2150 (or (buffer-substring (min (point-max) (1+ (point)))
2153 (* (case (prog1 (setq token (read (current-buffer)))
2155 (OK (imap-parse-resp-text))
2156 (NO (imap-parse-resp-text))
2157 (BAD (imap-parse-resp-text))
2158 (BYE (imap-parse-resp-text))
2159 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2160 (LIST (imap-parse-data-list 'list))
2161 (LSUB (imap-parse-data-list 'lsub))
2162 (SEARCH (imap-mailbox-put
2164 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2165 (STATUS (imap-parse-status))
2166 (CAPABILITY (setq imap-capability
2167 (read (concat "(" (upcase (buffer-substring
2168 (point) (point-max)))
2170 (ID (setq imap-id (read (buffer-substring (point)
2172 (ACL (imap-parse-acl))
2173 (t (case (prog1 (read (current-buffer))
2175 (EXISTS (imap-mailbox-put 'exists token))
2176 (RECENT (imap-mailbox-put 'recent token))
2178 (FETCH (imap-parse-fetch token))
2179 (t (message "Garbage: %s" (buffer-string)))))))
2181 (if (not (integerp token))
2182 (message "Garbage: %s" (buffer-string))
2183 (case (prog1 (setq status (read (current-buffer)))
2186 (setq imap-reached-tag (max imap-reached-tag token))
2187 (imap-parse-resp-text)))
2189 (setq imap-reached-tag (max imap-reached-tag token))
2191 (imap-parse-resp-text))
2193 (when (eq (char-after) ?\[)
2194 (setq code (buffer-substring (point)
2195 (search-forward "]")))
2197 (setq text (buffer-substring (point) (point-max)))
2198 (push (list token status code text)
2199 imap-failed-tags))))
2201 (setq imap-reached-tag (max imap-reached-tag token))
2203 (imap-parse-resp-text))
2205 (when (eq (char-after) ?\[)
2206 (setq code (buffer-substring (point)
2207 (search-forward "]")))
2209 (setq text (buffer-substring (point) (point-max)))
2210 (push (list token status code text) imap-failed-tags)
2211 (error "Internal error, tag %s status %s code %s text %s"
2212 token status code text))))
2213 (t (message "Garbage: %s" (buffer-string))))
2214 (when (assq token imap-callbacks)
2215 (funcall (cdr (assq token imap-callbacks)) token status)
2216 (setq imap-callbacks
2217 (imap-remassoc token imap-callbacks)))))))))
2219 ;; resp-text = ["[" resp-text-code "]" SP] text
2221 ;; text = 1*TEXT-CHAR
2223 ;; TEXT-CHAR = <any CHAR except CR and LF>
2225 (defun imap-parse-resp-text ()
2226 (imap-parse-resp-text-code))
2228 ;; resp-text-code = "ALERT" /
2229 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2230 ;; "NEWNAME" SP string SP string /
2232 ;; "PERMANENTFLAGS" SP "("
2233 ;; [flag-perm *(SP flag-perm)] ")" /
2237 ;; "UIDNEXT" SP nz-number /
2238 ;; "UIDVALIDITY" SP nz-number /
2239 ;; "UNSEEN" SP nz-number /
2240 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2242 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2244 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2246 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2248 ;; ; Identifies a set of messages. For message
2249 ;; ; sequence numbers, these are consecutive
2250 ;; ; numbers from 1 to the number of messages in
2252 ;; ; Comma delimits individual numbers, colon
2253 ;; ; delimits between two numbers inclusive.
2254 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2255 ;; ; 14,15 for a mailbox with 15 messages.
2257 ;; sequence-num = nz-number / "*"
2258 ;; ; * is the largest number in use. For message
2259 ;; ; sequence numbers, it is the number of messages
2260 ;; ; in the mailbox. For unique identifiers, it is
2261 ;; ; the unique identifier of the last message in
2264 ;; flag-perm = flag / "\*"
2266 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2267 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2268 ;; ; Does not include "\Recent"
2270 ;; flag-extension = "\" atom
2271 ;; ; Future expansion. Client implementations
2272 ;; ; MUST accept flag-extension flags. Server
2273 ;; ; implementations MUST NOT generate
2274 ;; ; flag-extension flags except as defined by
2275 ;; ; future standard or standards-track
2276 ;; ; revisions of this specification.
2278 ;; flag-keyword = atom
2280 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2282 (defun imap-parse-resp-text-code ()
2283 ;; xxx next line for stalker communigate pro 3.3.1 bug
2284 (when (looking-at " \\[")
2286 (when (eq (char-after) ?\[)
2288 (cond ((search-forward "PERMANENTFLAGS " nil t)
2289 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2290 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2291 (imap-mailbox-put 'uidnext (match-string 1)))
2292 ((search-forward "UNSEEN " nil t)
2293 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2294 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2295 (imap-mailbox-put 'uidvalidity (match-string 1)))
2296 ((search-forward "READ-ONLY" nil t)
2297 (imap-mailbox-put 'read-only t))
2298 ((search-forward "NEWNAME " nil t)
2299 (let (oldname newname)
2300 (setq oldname (imap-parse-string))
2302 (setq newname (imap-parse-string))
2303 (imap-mailbox-put 'newname newname oldname)))
2304 ((search-forward "TRYCREATE" nil t)
2305 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2306 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2307 (imap-mailbox-put 'appenduid
2308 (list (match-string 1)
2309 (string-to-number (match-string 2)))
2310 imap-current-target-mailbox))
2311 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2312 (imap-mailbox-put 'copyuid (list (match-string 1)
2315 imap-current-target-mailbox))
2316 ((search-forward "ALERT] " nil t)
2317 (message "Imap server %s information: %s" imap-server
2318 (buffer-substring (point) (point-max)))))))
2320 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2321 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2323 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2324 ;; *(SP mbx-list-oflag) /
2325 ;; mbx-list-oflag *(SP mbx-list-oflag)
2327 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2328 ;; ; Other flags; multiple possible per LIST response
2330 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2331 ;; ; Selectability flags; only one per LIST response
2333 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2334 ;; "\" quoted-specials
2336 ;; quoted-specials = DQUOTE / "\"
2338 (defun imap-parse-data-list (type)
2339 (let (flags delimiter mailbox)
2340 (setq flags (imap-parse-flag-list))
2341 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2342 (setq delimiter (match-string 1))
2343 (goto-char (1+ (match-end 0)))
2344 (when (setq mailbox (imap-parse-mailbox))
2345 (imap-mailbox-put type t mailbox)
2346 (imap-mailbox-put 'list-flags flags mailbox)
2347 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2349 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2350 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2351 ;; "INTERNALDATE" SPACE date_time /
2352 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2353 ;; "RFC822.SIZE" SPACE number /
2354 ;; "BODY" ["STRUCTURE"] SPACE body /
2355 ;; "BODY" section ["<" number ">"] SPACE nstring /
2356 ;; "UID" SPACE uniqueid) ")"
2358 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2359 ;; SPACE time SPACE zone <">
2361 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2362 ;; ["." (section_text / "MIME")])] "]"
2364 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2365 ;; SPACE header_list / "TEXT"
2367 ;; header_fld_name ::= astring
2369 ;; header_list ::= "(" 1#header_fld_name ")"
2371 (defsubst imap-parse-header-list ()
2372 (when (eq (char-after) ?\()
2374 (while (not (eq (char-after) ?\)))
2376 (push (imap-parse-astring) strlist))
2378 (nreverse strlist))))
2380 (defsubst imap-parse-fetch-body-section ()
2382 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2383 (if (eq (char-before) ? )
2385 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2386 (search-forward "]" nil t))
2389 (defun imap-parse-fetch (response)
2390 (when (eq (char-after) ?\()
2391 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2392 rfc822size body bodydetail bodystructure flags-empty)
2393 (while (not (eq (char-after) ?\)))
2395 (let ((token (read (current-buffer))))
2397 (cond ((eq token 'UID)
2398 (setq uid (condition-case ()
2399 (read (current-buffer))
2402 (setq flags (imap-parse-flag-list))
2404 (setq flags-empty 't)))
2405 ((eq token 'ENVELOPE)
2406 (setq envelope (imap-parse-envelope)))
2407 ((eq token 'INTERNALDATE)
2408 (setq internaldate (imap-parse-string)))
2410 (setq rfc822 (imap-parse-nstring)))
2411 ((eq token 'RFC822.HEADER)
2412 (setq rfc822header (imap-parse-nstring)))
2413 ((eq token 'RFC822.TEXT)
2414 (setq rfc822text (imap-parse-nstring)))
2415 ((eq token 'RFC822.SIZE)
2416 (setq rfc822size (read (current-buffer))))
2418 (if (eq (char-before) ?\[)
2420 (upcase (imap-parse-fetch-body-section))
2421 (and (eq (char-after) ?<)
2422 (buffer-substring (1+ (point))
2423 (search-forward ">" nil t)))
2424 (progn (imap-forward)
2425 (imap-parse-nstring)))
2427 (setq body (imap-parse-body))))
2428 ((eq token 'BODYSTRUCTURE)
2429 (setq bodystructure (imap-parse-body))))))
2431 (setq imap-current-message uid)
2432 (imap-message-put uid 'UID uid)
2433 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2434 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2435 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2436 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2437 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2438 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2439 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2440 (and body (imap-message-put uid 'BODY body))
2441 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2442 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2443 (run-hooks 'imap-fetch-data-hook)))))
2445 ;; mailbox-data = ...
2446 ;; "STATUS" SP mailbox SP "("
2447 ;; [status-att SP number
2448 ;; *(SP status-att SP number)] ")"
2451 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2454 (defun imap-parse-status ()
2455 (let ((mailbox (imap-parse-mailbox)))
2456 (if (eq (char-after) ? )
2458 (when (and mailbox (eq (char-after) ?\())
2459 (while (and (not (eq (char-after) ?\)))
2460 (or (forward-char) t)
2461 (looking-at "\\([A-Za-z]+\\) "))
2462 (let ((token (match-string 1)))
2463 (goto-char (match-end 0))
2464 (cond ((string= token "MESSAGES")
2465 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2466 ((string= token "RECENT")
2467 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2468 ((string= token "UIDNEXT")
2469 (and (looking-at "[0-9]+")
2470 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2471 (goto-char (match-end 0))))
2472 ((string= token "UIDVALIDITY")
2473 (and (looking-at "[0-9]+")
2474 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2475 (goto-char (match-end 0))))
2476 ((string= token "UNSEEN")
2477 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2479 (message "Unknown status data %s in mailbox %s ignored"
2481 (read (current-buffer)))))))))
2483 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2486 ;; identifier ::= astring
2488 ;; rights ::= astring
2490 (defun imap-parse-acl ()
2491 (let ((mailbox (imap-parse-mailbox))
2492 identifier rights acl)
2493 (while (eq (char-after) ?\ )
2495 (setq identifier (imap-parse-astring))
2497 (setq rights (imap-parse-astring))
2498 (setq acl (append acl (list (cons identifier rights)))))
2499 (imap-mailbox-put 'acl acl mailbox)))
2501 ;; flag-list = "(" [flag *(SP flag)] ")"
2503 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2504 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2505 ;; ; Does not include "\Recent"
2507 ;; flag-keyword = atom
2509 ;; flag-extension = "\" atom
2510 ;; ; Future expansion. Client implementations
2511 ;; ; MUST accept flag-extension flags. Server
2512 ;; ; implementations MUST NOT generate
2513 ;; ; flag-extension flags except as defined by
2514 ;; ; future standard or standards-track
2515 ;; ; revisions of this specification.
2517 (defun imap-parse-flag-list ()
2518 (let (flag-list start)
2519 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2520 (while (and (not (eq (char-after) ?\)))
2523 ;; next line for Courier IMAP bug.
2524 (skip-chars-forward " ")
2526 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2527 (push (buffer-substring start (point)) flag-list))
2528 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2530 (nreverse flag-list)))
2532 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2533 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2534 ;; env-in-reply-to SP env-message-id ")"
2536 ;; env-bcc = "(" 1*address ")" / nil
2538 ;; env-cc = "(" 1*address ")" / nil
2540 ;; env-date = nstring
2542 ;; env-from = "(" 1*address ")" / nil
2544 ;; env-in-reply-to = nstring
2546 ;; env-message-id = nstring
2548 ;; env-reply-to = "(" 1*address ")" / nil
2550 ;; env-sender = "(" 1*address ")" / nil
2552 ;; env-subject = nstring
2554 ;; env-to = "(" 1*address ")" / nil
2556 (defun imap-parse-envelope ()
2557 (when (eq (char-after) ?\()
2559 (vector (prog1 (imap-parse-nstring) ;; date
2561 (prog1 (imap-parse-nstring) ;; subject
2563 (prog1 (imap-parse-address-list) ;; from
2565 (prog1 (imap-parse-address-list) ;; sender
2567 (prog1 (imap-parse-address-list) ;; reply-to
2569 (prog1 (imap-parse-address-list) ;; to
2571 (prog1 (imap-parse-address-list) ;; cc
2573 (prog1 (imap-parse-address-list) ;; bcc
2575 (prog1 (imap-parse-nstring) ;; in-reply-to
2577 (prog1 (imap-parse-nstring) ;; message-id
2580 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2582 (defsubst imap-parse-string-list ()
2583 (cond ((eq (char-after) ?\() ;; body-fld-param
2586 (while (setq str (imap-parse-string))
2588 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2589 ;; between body-fld-param's sometimes
2590 (or (eq (char-after) ?\")
2592 (nreverse strlist)))
2596 ;; body-extension = nstring / number /
2597 ;; "(" body-extension *(SP body-extension) ")"
2598 ;; ; Future expansion. Client implementations
2599 ;; ; MUST accept body-extension fields. Server
2600 ;; ; implementations MUST NOT generate
2601 ;; ; body-extension fields except as defined by
2602 ;; ; future standard or standards-track
2603 ;; ; revisions of this specification.
2605 (defun imap-parse-body-extension ()
2606 (if (eq (char-after) ?\()
2609 (push (imap-parse-body-extension) b-e)
2610 (while (eq (char-after) ?\ )
2612 (push (imap-parse-body-extension) b-e))
2613 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2616 (or (imap-parse-number)
2617 (imap-parse-nstring))))
2619 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2620 ;; *(SP body-extension)]]
2621 ;; ; MUST NOT be returned on non-extensible
2624 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2625 ;; *(SP body-extension)]]
2626 ;; ; MUST NOT be returned on non-extensible
2629 (defsubst imap-parse-body-ext ()
2631 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2634 (if (eq (char-after) ?\()
2637 (push (imap-parse-string) dsp)
2639 (push (imap-parse-string-list) dsp)
2641 ;; With assert, the code might not be eval'd.
2642 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2644 (push (nreverse dsp) ext))
2645 (when (eq (char-after) ?\ ) ;; body-fld-lang
2647 (if (eq (char-after) ?\()
2648 (push (imap-parse-string-list) ext)
2649 (push (imap-parse-nstring) ext))
2650 (while (eq (char-after) ?\ ) ;; body-extension
2652 (setq ext (append (imap-parse-body-extension) ext)))))
2655 ;; body = "(" body-type-1part / body-type-mpart ")"
2657 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2658 ;; *(SP body-extension)]]
2659 ;; ; MUST NOT be returned on non-extensible
2662 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2663 ;; *(SP body-extension)]]
2664 ;; ; MUST NOT be returned on non-extensible
2667 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2668 ;; body-fld-enc SP body-fld-octets
2670 ;; body-fld-desc = nstring
2672 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2674 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2675 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2677 ;; body-fld-id = nstring
2679 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2681 ;; body-fld-lines = number
2683 ;; body-fld-md5 = nstring
2685 ;; body-fld-octets = number
2687 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2689 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2690 ;; [SP body-ext-1part]
2692 ;; body-type-basic = media-basic SP body-fields
2693 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2695 ;; body-type-msg = media-message SP body-fields SP envelope
2696 ;; SP body SP body-fld-lines
2698 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2700 ;; body-type-mpart = 1*body SP media-subtype
2701 ;; [SP body-ext-mpart]
2703 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2704 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2705 ;; ; Defined in [MIME-IMT]
2707 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2708 ;; ; Defined in [MIME-IMT]
2710 ;; media-subtype = string
2711 ;; ; Defined in [MIME-IMT]
2713 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2714 ;; ; Defined in [MIME-IMT]
2716 (defun imap-parse-body ()
2718 (when (eq (char-after) ?\()
2720 (if (eq (char-after) ?\()
2722 (while (and (eq (char-after) ?\()
2723 (setq subbody (imap-parse-body)))
2724 ;; buggy stalker communigate pro 3.0 insert a SPC between
2725 ;; parts in multiparts
2726 (when (and (eq (char-after) ?\ )
2727 (eq (char-after (1+ (point))) ?\())
2729 (push subbody body))
2731 (push (imap-parse-string) body) ;; media-subtype
2732 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2734 (if (eq (char-after) ?\() ;; body-fld-param
2735 (push (imap-parse-string-list) body)
2736 (push (and (imap-parse-nil) nil) body))
2738 (append (imap-parse-body-ext) body))) ;; body-ext-...
2739 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2743 (push (imap-parse-string) body) ;; media-type
2745 (push (imap-parse-string) body) ;; media-subtype
2747 ;; next line for Sun SIMS bug
2748 (and (eq (char-after) ? ) (imap-forward))
2749 (if (eq (char-after) ?\() ;; body-fld-param
2750 (push (imap-parse-string-list) body)
2751 (push (and (imap-parse-nil) nil) body))
2753 (push (imap-parse-nstring) body) ;; body-fld-id
2755 (push (imap-parse-nstring) body) ;; body-fld-desc
2757 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2758 ;; nstring and return nil instead of defaulting back to 7BIT
2759 ;; as the standard says.
2760 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2762 (push (imap-parse-number) body) ;; body-fld-octets
2764 ;; ok, we're done parsing the required parts, what comes now is one
2767 ;; envelope (then we're parsing body-type-msg)
2768 ;; body-fld-lines (then we're parsing body-type-text)
2769 ;; body-ext-1part (then we're parsing body-type-basic)
2771 ;; the problem is that the two first are in turn optionally followed
2772 ;; by the third. So we parse the first two here (if there are any)...
2774 (when (eq (char-after) ?\ )
2777 (cond ((eq (char-after) ?\() ;; body-type-msg:
2778 (push (imap-parse-envelope) body) ;; envelope
2780 (push (imap-parse-body) body) ;; body
2781 ;; buggy stalker communigate pro 3.0 doesn't print
2782 ;; number of lines in message/rfc822 attachment
2783 (if (eq (char-after) ?\))
2786 (push (imap-parse-number) body))) ;; body-fld-lines
2787 ((setq lines (imap-parse-number)) ;; body-type-text:
2788 (push lines body)) ;; body-fld-lines
2790 (backward-char))))) ;; no match...
2792 ;; ...and then parse the third one here...
2794 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2796 (push (imap-parse-nstring) body) ;; body-fld-md5
2797 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2799 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2803 (when imap-debug ; (untrace-all)
2805 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2806 (mapcar (lambda (f) (trace-function-background f imap-debug-buffer))
2817 imap-interactive-login
2833 imap-send-command-wait
2838 imap-current-mailbox
2839 imap-current-mailbox-p-1
2840 imap-current-mailbox-p
2841 imap-mailbox-select-1
2843 imap-mailbox-examine-1
2844 imap-mailbox-examine
2845 imap-mailbox-unselect
2846 imap-mailbox-expunge
2848 imap-mailbox-create-1
2854 imap-mailbox-subscribe
2855 imap-mailbox-unsubscribe
2857 imap-mailbox-acl-get
2858 imap-mailbox-acl-set
2859 imap-mailbox-acl-delete
2860 imap-current-message
2861 imap-list-to-message-set
2868 imap-message-flag-permanent-p
2869 imap-message-flags-set
2870 imap-message-flags-del
2871 imap-message-flags-add
2872 imap-message-copyuid-1
2873 imap-message-copyuid
2875 imap-message-appenduid-1
2876 imap-message-appenduid
2888 imap-parse-resp-text
2889 imap-parse-resp-text-code
2890 imap-parse-data-list
2894 imap-parse-flag-list
2896 imap-parse-body-extension
2902 ;;; imap.el ends here