1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 theese 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.."
228 (defcustom imap-use-utf7 t
229 "If non-nil, do utf7 encoding/decoding of mailbox names.
230 Since the UTF7 decoding currently only decodes into ISO-8859-1
231 characters, you may disable this decoding if you need to access UTF7
232 encoded mailboxes which doesn't translate into ISO-8859-1."
236 (defcustom imap-log nil
237 "If non-nil, a imap session trace is placed in *imap-log* buffer."
241 (defcustom imap-debug nil
242 "If non-nil, random debug spews are placed in *imap-debug* buffer."
246 (defcustom imap-shell-host "gateway"
247 "Hostname of rlogin proxy."
251 (defcustom imap-default-user (user-login-name)
252 "Default username to use."
256 (defcustom imap-read-timeout (if (string-match
257 "windows-nt\\|os/2\\|emx\\|cygwin"
258 (symbol-name system-type))
261 "*How long to wait between checking for the end of output.
262 Shorter values mean quicker response, but is more CPU intensive."
266 (defcustom imap-store-password nil
267 "If non-nil, store session password without promting."
271 ;; Various variables.
273 (defvar imap-fetch-data-hook nil
274 "Hooks called after receiving each FETCH response.")
276 (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell)
277 "Priority of streams to consider when opening connection to server.")
279 (defvar imap-stream-alist
280 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
281 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
282 (tls imap-tls-p imap-tls-open)
283 (ssl imap-ssl-p imap-ssl-open)
284 (network imap-network-p imap-network-open)
285 (shell imap-shell-p imap-shell-open)
286 (starttls imap-starttls-p imap-starttls-open))
287 "Definition of network streams.
291 NAME names the stream, CHECK is a function returning non-nil if the
292 server support the stream and OPEN is a function for opening the
295 (defvar imap-authenticators '(gssapi
302 "Priority of authenticators to consider when authenticating to server.")
304 (defvar imap-authenticator-alist
305 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
306 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
307 (sasl imap-sasl-auth-p imap-sasl-auth)
308 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
309 (login imap-login-p imap-login-auth)
310 (anonymous imap-anonymous-p imap-anonymous-auth)
311 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
312 "Definition of authenticators.
314 \(NAME CHECK AUTHENTICATE)
316 NAME names the authenticator. CHECK is a function returning non-nil if
317 the server support the authenticator and AUTHENTICATE is a function
318 for doing the actual authentication.")
320 (defvar imap-error nil
321 "Error codes from the last command.")
323 ;; Internal constants. Change theese and die.
325 (defconst imap-default-port 143)
326 (defconst imap-default-ssl-port 993)
327 (defconst imap-default-tls-port 993)
328 (defconst imap-default-stream 'network)
329 (defconst imap-local-variables '(imap-server
338 imap-current-target-mailbox
348 imap-calculate-literal-size-first
350 (defconst imap-log-buffer "*imap-log*")
351 (defconst imap-debug-buffer "*imap-debug*")
353 ;; Internal variables.
355 (defvar imap-stream nil)
356 (defvar imap-auth nil)
357 (defvar imap-server nil)
358 (defvar imap-port nil)
359 (defvar imap-username nil)
360 (defvar imap-password nil)
361 (defvar imap-calculate-literal-size-first nil)
362 (defvar imap-state 'closed
364 Valid states are `closed', `initial', `nonauth', `auth', `selected'
367 (defvar imap-server-eol "\r\n"
368 "The EOL string sent from the server.")
370 (defvar imap-client-eol "\r\n"
371 "The EOL string we send to the server.")
373 (defvar imap-current-mailbox nil
374 "Current mailbox name.")
376 (defvar imap-current-target-mailbox nil
377 "Current target mailbox for COPY and APPEND commands.")
379 (defvar imap-mailbox-data nil
380 "Obarray with mailbox data.")
382 (defvar imap-mailbox-prime 997
383 "Length of imap-mailbox-data.")
385 (defvar imap-current-message nil
386 "Current message number.")
388 (defvar imap-message-data nil
389 "Obarray with message data.")
391 (defvar imap-message-prime 997
392 "Length of imap-message-data.")
394 (defvar imap-capability nil
395 "Capability for server.")
401 (defvar imap-namespace nil
402 "Namespace for current server.")
404 (defvar imap-reached-tag 0
405 "Lower limit on command tags that have been parsed.")
407 (defvar imap-failed-tags nil
408 "Alist of tags that failed.
409 Each element is a list with four elements; tag (a integer), response
410 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
411 human readable response text (a string).")
414 "Command tag number.")
416 (defvar imap-process nil
419 (defvar imap-continuation nil
420 "Non-nil indicates that the server emitted a continuation request.
421 The actual value is really the text on the continuation line.")
423 (defvar imap-callbacks nil
424 "List of response tags and callbacks, on the form `(number . function)'.
425 The function should take two arguments, the first the IMAP tag and the
426 second the status (OK, NO, BAD etc) of the command.")
429 ;; Utility functions:
431 (defun imap-remassoc (key alist)
432 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
433 The modified LIST is returned. If the first member
434 of LIST has a car that is `equal' to KEY, there is no way to remove it
435 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
436 sure of changing the value of `foo'."
438 (if (equal key (caar alist))
440 (setcdr alist (imap-remassoc key (cdr alist)))
443 (defmacro imap-disable-multibyte ()
444 "Enable multibyte in the current buffer."
445 '(set-buffer-multibyte nil))
447 (defsubst imap-utf7-encode (string)
451 (utf7-encode string t)
453 "imap: Could not UTF7 encode `%s', using it unencoded..."
458 (defsubst imap-utf7-decode (string)
462 (utf7-decode string t)
464 "imap: Could not UTF7 decode `%s', using it undecoded..."
469 (defsubst imap-ok-p (status)
472 (setq imap-error status)
475 (defun imap-error-text (&optional buffer)
476 (with-current-buffer (or buffer (current-buffer))
477 (nth 3 (car imap-failed-tags))))
480 ;; Server functions; stream stuff:
482 (defun imap-kerberos4-stream-p (buffer)
483 (imap-capability 'AUTH=KERBEROS_V4 buffer))
485 (defun imap-kerberos4-open (name buffer server port)
486 (let ((cmds imap-kerberos4-program)
488 (while (and (not done) (setq cmd (pop cmds)))
489 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
491 (let* ((port (or port imap-default-port))
492 (process-connection-type imap-process-connection-type)
493 (process (as-binary-process
495 name buffer shell-file-name shell-command-switch
500 ?p (number-to-string port)
501 ?l imap-default-user)))))
504 (with-current-buffer buffer
505 (setq imap-client-eol "\n"
506 imap-calculate-literal-size-first t)
507 (while (and (memq (process-status process) '(open run))
508 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
509 (goto-char (point-min))
510 ;; Athena IMTEST can output SSL verify errors
511 (or (while (looking-at "^verify error:num=")
514 (or (while (looking-at "^TLS connection established")
517 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
518 (or (while (looking-at "^C:")
521 ;; cyrus 1.6 imtest print "S: " before server greeting
522 (or (not (looking-at "S: "))
525 (not (and (imap-parse-greeting)
526 ;; success in imtest < 1.6:
527 (or (re-search-forward
528 "^__\\(.*\\)__\n" nil t)
529 ;; success in imtest 1.6:
531 "^\\(Authenticat.*\\)" nil t))
532 (setq response (match-string 1)))))
533 (accept-process-output process 1)
536 (with-current-buffer (get-buffer-create imap-log-buffer)
537 (imap-disable-multibyte)
538 (buffer-disable-undo)
539 (goto-char (point-max))
540 (insert-buffer-substring buffer)))
542 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
543 (if response (concat "done, " response) "failed"))
544 (if (and response (let ((case-fold-search nil))
545 (not (string-match "failed" response))))
547 (if (memq (process-status process) '(open run))
548 (imap-send-command "LOGOUT"))
549 (delete-process process)
553 (defun imap-gssapi-stream-p (buffer)
554 (imap-capability 'AUTH=GSSAPI buffer))
556 (defun imap-gssapi-open (name buffer server port)
557 (let ((cmds imap-gssapi-program)
559 (while (and (not done) (setq cmd (pop cmds)))
560 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
562 (let* ((port (or port imap-default-port))
563 (process-connection-type imap-process-connection-type)
564 (process (as-binary-process
566 name buffer shell-file-name shell-command-switch
571 ?p (number-to-string port)
572 ?l imap-default-user)))))
575 (with-current-buffer buffer
576 (setq imap-client-eol "\n"
577 imap-calculate-literal-size-first t)
578 (while (and (memq (process-status process) '(open run))
579 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
580 (goto-char (point-min))
581 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
582 (or (while (looking-at "^C:")
585 ;; cyrus 1.6 imtest print "S: " before server greeting
586 (or (not (looking-at "S: "))
589 (not (and (imap-parse-greeting)
590 ;; success in imtest 1.6:
592 (concat "^\\(\\(Authenticat.*\\)\\|\\("
593 "Client authentication "
596 (setq response (match-string 1)))))
597 (accept-process-output process 1)
600 (with-current-buffer (get-buffer-create imap-log-buffer)
601 (imap-disable-multibyte)
602 (buffer-disable-undo)
603 (goto-char (point-max))
604 (insert-buffer-substring buffer)))
606 (message "GSSAPI IMAP connection: %s" (or response "failed"))
607 (if (and response (let ((case-fold-search nil))
608 (not (string-match "failed" response))))
610 (if (memq (process-status process) '(open run))
611 (imap-send-command "LOGOUT"))
612 (delete-process process)
616 (defun imap-ssl-p (buffer)
619 (defun imap-ssl-open (name buffer server port)
620 "Open a SSL connection to server."
621 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
622 (list imap-ssl-program)))
624 (while (and (not done) (setq cmd (pop cmds)))
625 (message "imap: Opening SSL connection with `%s'..." cmd)
627 (let ((port (or port imap-default-ssl-port))
628 (process-connection-type nil)
631 (setq process (as-binary-process
633 name buffer shell-file-name
638 ?p (number-to-string port))))))
639 (process-kill-without-query process))
640 (with-current-buffer buffer
641 (goto-char (point-min))
642 (while (and (memq (process-status process) '(open run))
643 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
644 (goto-char (point-max))
646 (not (imap-parse-greeting)))
647 (accept-process-output process 1)
650 (with-current-buffer (get-buffer-create imap-log-buffer)
651 (imap-disable-multibyte)
652 (buffer-disable-undo)
653 (goto-char (point-max))
654 (insert-buffer-substring buffer)))
656 (when (memq (process-status process) '(open run))
657 (setq done process))))))
660 (message "imap: Opening SSL connection with `%s'...done" cmd)
662 (message "imap: Opening SSL connection with `%s'...failed" cmd)
665 (defun imap-tls-p (buffer)
668 (defun imap-tls-open (name buffer server port)
669 (let* ((port (or port imap-default-tls-port))
670 (process (open-tls-stream name buffer server port)))
672 (while (and (memq (process-status process) '(open run))
673 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
674 (goto-char (point-max))
676 (not (imap-parse-greeting)))
677 (accept-process-output process 1)
680 (with-current-buffer (get-buffer-create imap-log-buffer)
681 (imap-disable-multibyte)
682 (buffer-disable-undo)
683 (goto-char (point-max))
684 (insert-buffer-substring buffer)))
685 (when (memq (process-status process) '(open run))
688 (defun imap-network-p (buffer)
691 (defun imap-network-open (name buffer server port)
692 (let* ((port (or port imap-default-port))
693 (process (open-network-stream-as-binary name buffer server port)))
695 (while (and (memq (process-status process) '(open run))
696 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
697 (goto-char (point-min))
698 (not (imap-parse-greeting)))
699 (accept-process-output process 1)
702 (with-current-buffer (get-buffer-create imap-log-buffer)
703 (imap-disable-multibyte)
704 (buffer-disable-undo)
705 (goto-char (point-max))
706 (insert-buffer-substring buffer)))
707 (when (memq (process-status process) '(open run))
710 (defun imap-shell-p (buffer)
713 (defun imap-shell-open (name buffer server port)
714 (let ((cmds (if (listp imap-shell-program) imap-shell-program
715 (list imap-shell-program)))
717 (while (and (not done) (setq cmd (pop cmds)))
718 (message "imap: Opening IMAP connection with `%s'..." cmd)
719 (setq imap-client-eol "\n")
720 (let* ((port (or port imap-default-port))
721 (process (as-binary-process
723 name buffer shell-file-name shell-command-switch
729 ?p (number-to-string port)
730 ?l imap-default-user))))))
732 (while (and (memq (process-status process) '(open run))
733 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
734 (goto-char (point-max))
736 (not (imap-parse-greeting)))
737 (accept-process-output process 1)
740 (with-current-buffer (get-buffer-create imap-log-buffer)
741 (imap-disable-multibyte)
742 (buffer-disable-undo)
743 (goto-char (point-max))
744 (insert-buffer-substring buffer)))
746 (when (memq (process-status process) '(open run))
747 (setq done process)))))
750 (message "imap: Opening IMAP connection with `%s'...done" cmd)
752 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
755 (defun imap-starttls-p (buffer)
756 (imap-capability 'STARTTLS buffer))
758 (defun imap-starttls-open (name buffer server port)
759 (let* ((port (or port imap-default-port))
760 (process (as-binary-process
761 (starttls-open-stream name buffer server port)))
763 (message "imap: Connecting with STARTTLS...")
765 (while (and (memq (process-status process) '(open run))
766 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
767 (goto-char (point-max))
769 (not (imap-parse-greeting)))
770 (accept-process-output process 1)
772 (imap-send-command "STARTTLS")
773 (while (and (memq (process-status process) '(open run))
774 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
775 (goto-char (point-max))
777 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
778 (accept-process-output process 1)
781 (with-current-buffer (get-buffer-create imap-log-buffer)
782 (buffer-disable-undo)
783 (goto-char (point-max))
784 (insert-buffer-substring buffer)))
785 (when (and (setq tls-info (starttls-negotiate process))
786 (memq (process-status process) '(open run)))
787 (setq done process)))
788 (if (stringp tls-info)
789 (message "imap: STARTTLS info: %s" tls-info))
790 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
793 ;; Server functions; authenticator stuff:
795 (defun imap-interactive-login (buffer loginfunc)
796 "Login to server in BUFFER.
797 LOGINFUNC is passed a username and a password, it should return t if
798 it where successful authenticating itself to the server, nil otherwise.
799 Returns t if login was successful, nil otherwise."
800 (with-current-buffer buffer
801 (make-local-variable 'imap-username)
802 (make-local-variable 'imap-password)
803 (let (user passwd ret)
804 ;; (condition-case ()
805 (while (or (not user) (not passwd))
806 (setq user (or imap-username
807 (read-from-minibuffer
808 (concat "IMAP username for " imap-server
809 " (using stream `" (symbol-name imap-stream)
811 (or user imap-default-user))))
812 (setq passwd (or imap-password
814 (concat "IMAP password for " user "@"
815 imap-server " (using authenticator `"
816 (symbol-name imap-auth) "'): "))))
817 (when (and user passwd)
818 (if (funcall loginfunc user passwd)
822 (when (and (not imap-password)
823 (or imap-store-password
824 (y-or-n-p "Store password for this session? ")))
825 (setq imap-password passwd)))
826 (message "Login failed...")
828 (setq imap-password nil)
830 ;; (quit (with-current-buffer buffer
833 ;; (error (with-current-buffer buffer
838 (defun imap-gssapi-auth-p (buffer)
839 (eq imap-stream 'gssapi))
841 (defun imap-gssapi-auth (buffer)
842 (message "imap: Authenticating using GSSAPI...%s"
843 (if (eq imap-stream 'gssapi) "done" "failed"))
844 (eq imap-stream 'gssapi))
846 (defun imap-kerberos4-auth-p (buffer)
847 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
848 (eq imap-stream 'kerberos4)))
850 (defun imap-kerberos4-auth (buffer)
851 (message "imap: Authenticating using Kerberos 4...%s"
852 (if (eq imap-stream 'kerberos4) "done" "failed"))
853 (eq imap-stream 'kerberos4))
855 (defun imap-cram-md5-p (buffer)
856 (imap-capability 'AUTH=CRAM-MD5 buffer))
858 (defun imap-cram-md5-auth (buffer)
859 "Login to server using the AUTH CRAM-MD5 method."
860 (message "imap: Authenticating using CRAM-MD5...")
861 (let ((done (imap-interactive-login
863 (lambda (user passwd)
865 (imap-send-command-wait
867 "AUTHENTICATE CRAM-MD5"
869 (let* ((decoded (base64-decode-string challenge))
871 (if (and (featurep 'xemacs)
872 (>= (function-max-args 'md5) 4))
873 (lambda (object &optional start end)
874 (md5 object start end 'binary))
876 (hash (rfc2104-hash hash-function 64 16
878 (response (concat user " " hash))
879 (encoded (base64-encode-string response)))
882 (message "imap: Authenticating using CRAM-MD5...done")
883 (message "imap: Authenticating using CRAM-MD5...failed"))))
885 (defun imap-login-p (buffer)
886 (and (not (imap-capability 'LOGINDISABLED buffer))
887 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
889 (defun imap-login-auth (buffer)
890 "Login to server using the LOGIN command."
891 (message "imap: Plaintext authentication...")
892 (imap-interactive-login buffer
893 (lambda (user passwd)
894 (imap-ok-p (imap-send-command-wait
895 (concat "LOGIN \"" user "\" \""
898 (defun imap-anonymous-p (buffer)
901 (defun imap-anonymous-auth (buffer)
902 (message "imap: Logging in anonymously...")
903 (with-current-buffer buffer
904 (imap-ok-p (imap-send-command-wait
905 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
906 (system-name)) "\"")))))
908 (defun imap-sasl-make-mechanisms (buffer)
911 (let ((name (symbol-name sym)))
912 (if (and (> (length name) 5)
913 (string-equal "AUTH=" (substring name 0 5 )))
914 (setq mecs (cons (substring name 5) mecs)))))
915 (imap-capability nil buffer))
918 (defun imap-sasl-auth-p (buffer)
919 (and (condition-case ()
922 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
924 (defun imap-sasl-auth (buffer)
925 "Login to server using the SASL method."
926 (message "imap: Authenticating using SASL...")
927 (with-current-buffer buffer
928 (make-local-variable 'imap-username)
929 (make-local-variable 'imap-sasl-client)
930 (make-local-variable 'imap-sasl-step)
931 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
934 (setq user (or imap-username
935 (read-from-minibuffer
936 (concat "IMAP username for " imap-server " using SASL "
937 (sasl-mechanism-name mechanism) ": ")
938 (or user imap-default-user))))
940 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
941 imap-sasl-step (sasl-next-step imap-sasl-client nil))
942 (let ((tag (imap-send-command
943 (if (sasl-step-data imap-sasl-step)
944 (format "AUTHENTICATE %s %s"
945 (sasl-mechanism-name mechanism)
946 (sasl-step-data imap-sasl-step))
947 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
949 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
950 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
951 (setq imap-continuation nil
952 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
953 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
954 (base64-encode-string (sasl-step-data imap-sasl-step) t)
956 (if (imap-ok-p (imap-wait-for-tag tag))
957 (setq imap-username user
959 (message "Login failed...")
963 (defun imap-digest-md5-p (buffer)
964 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
966 (require 'digest-md5)
969 (defun imap-digest-md5-auth (buffer)
970 "Login to server using the AUTH DIGEST-MD5 method."
971 (message "imap: Authenticating using DIGEST-MD5...")
972 (imap-interactive-login
974 (lambda (user passwd)
978 "AUTHENTICATE DIGEST-MD5"
980 (digest-md5-parse-digest-challenge
981 (base64-decode-string challenge))
983 (digest-md5-digest-uri
984 "imap" (digest-md5-challenge 'realm)))
986 (digest-md5-digest-response
987 user passwd digest-uri)))
988 (base64-encode-string response 'no-line-break))))
990 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
992 (setq imap-continuation nil)
993 (imap-send-command-1 "")
994 (imap-ok-p (imap-wait-for-tag tag)))))))
998 (defun imap-open-1 (buffer)
999 (with-current-buffer buffer
1001 (setq imap-current-mailbox nil
1002 imap-current-message nil
1004 imap-process (condition-case ()
1005 (funcall (nth 2 (assq imap-stream
1007 "imap" buffer imap-server imap-port)
1008 ((error quit) nil)))
1010 (set-process-filter imap-process 'imap-arrival-filter)
1011 (set-process-sentinel imap-process 'imap-sentinel)
1012 (while (and (eq imap-state 'initial)
1013 (memq (process-status imap-process) '(open run)))
1014 (message "Waiting for response from %s..." imap-server)
1015 (accept-process-output imap-process 1))
1016 (message "Waiting for response from %s...done" imap-server)
1017 (and (memq (process-status imap-process) '(open run))
1020 (defun imap-open (server &optional port stream auth buffer)
1021 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1022 If PORT is unspecified, a default value is used (143 except
1023 for SSL which use 993).
1024 STREAM indicates the stream to use, see `imap-streams' for available
1025 streams. If nil, it choices the best stream the server is capable of.
1026 AUTH indicates authenticator to use, see `imap-authenticators' for
1027 available authenticators. If nil, it choices the best stream the
1028 server is capable of.
1029 BUFFER can be a buffer or a name of a buffer, which is created if
1030 necessary. If nil, the buffer name is generated."
1031 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1032 (with-current-buffer (get-buffer-create buffer)
1033 (if (imap-opened buffer)
1034 (imap-close buffer))
1035 (mapcar 'make-local-variable imap-local-variables)
1036 (imap-disable-multibyte)
1037 (buffer-disable-undo)
1038 (setq imap-server (or server imap-server))
1039 (setq imap-port (or port imap-port))
1040 (setq imap-auth (or auth imap-auth))
1041 (setq imap-stream (or stream imap-stream))
1042 (message "imap: Connecting to %s..." imap-server)
1043 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1044 (imap-open-1 buffer)))
1046 (message "imap: Connecting to %s...failed" imap-server)
1048 (when (null imap-stream)
1049 ;; Need to choose stream.
1050 (let ((streams imap-streams))
1051 (while (setq stream (pop streams))
1052 ;; OK to use this stream?
1053 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1055 (if (not (eq imap-default-stream stream))
1056 (with-current-buffer (get-buffer-create
1057 (generate-new-buffer-name " *temp*"))
1058 (mapcar 'make-local-variable imap-local-variables)
1059 (imap-disable-multibyte)
1060 (buffer-disable-undo)
1061 (setq imap-server (or server imap-server))
1062 (setq imap-port (or port imap-port))
1063 (setq imap-auth (or auth imap-auth))
1064 (message "imap: Reconnecting with stream `%s'..." stream)
1065 (if (null (let ((imap-stream stream))
1066 (imap-open-1 (current-buffer))))
1068 (kill-buffer (current-buffer))
1070 "imap: Reconnecting with stream `%s'...failed"
1072 ;; We're done, kill the first connection
1074 (kill-buffer buffer)
1075 (rename-buffer buffer)
1076 (message "imap: Reconnecting with stream `%s'...done"
1078 (setq imap-stream stream)
1079 (setq imap-capability nil)
1080 (setq streams nil)))
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)))
1091 (defun imap-opened (&optional buffer)
1092 "Return non-nil if connection to imap server in BUFFER is open.
1093 If BUFFER is nil then the current buffer is used."
1094 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1095 (buffer-live-p buffer)
1096 (with-current-buffer buffer
1098 (memq (process-status imap-process) '(open run))))))
1100 (defun imap-authenticate (&optional user passwd buffer)
1101 "Authenticate to server in BUFFER, using current buffer if nil.
1102 It uses the authenticator specified when opening the server. If the
1103 authenticator requires username/passwords, they are queried from the
1104 user and optionally stored in the buffer. If USER and/or PASSWD is
1105 specified, the user will not be questioned and the username and/or
1106 password is remembered in the buffer."
1107 (with-current-buffer (or buffer (current-buffer))
1108 (if (not (eq imap-state 'nonauth))
1109 (or (eq imap-state 'auth)
1110 (eq imap-state 'select)
1111 (eq imap-state 'examine))
1112 (make-local-variable 'imap-username)
1113 (make-local-variable 'imap-password)
1114 (if user (setq imap-username user))
1115 (if passwd (setq imap-password passwd))
1117 (and (funcall (nth 2 (assq imap-auth
1118 imap-authenticator-alist)) buffer)
1119 (setq imap-state 'auth))
1120 ;; Choose authenticator.
1121 (let ((auths imap-authenticators)
1123 (while (setq auth (pop auths))
1124 ;; OK to use authenticator?
1125 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1126 (message "imap: Authenticating to `%s' using `%s'..."
1128 (setq imap-auth auth)
1129 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1131 (message "imap: Authenticating to `%s' using `%s'...done"
1134 (message "imap: Authenticating to `%s' using `%s'...failed"
1135 imap-server auth)))))
1138 (defun imap-close (&optional buffer)
1139 "Close connection to server in BUFFER.
1140 If BUFFER is nil, the current buffer is used."
1141 (with-current-buffer (or buffer (current-buffer))
1144 (imap-send-command-wait "LOGOUT")
1146 (when (and imap-process
1147 (memq (process-status imap-process) '(open run)))
1148 (delete-process imap-process))
1149 (setq imap-current-mailbox nil
1150 imap-current-message nil
1155 (defun imap-capability (&optional identifier buffer)
1156 "Return a list of identifiers which server in BUFFER support.
1157 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1158 If BUFFER is nil, the current buffer is assumed."
1159 (with-current-buffer (or buffer (current-buffer))
1160 (unless imap-capability
1161 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1162 (setq imap-capability '(IMAP2))))
1164 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1167 (defun imap-id (&optional list-of-values buffer)
1168 "Identify client to server in BUFFER, and return server identity.
1169 LIST-OF-VALUES is nil, or a plist with identifier and value
1170 strings to send to the server to identify the client.
1172 Return a list of identifiers which server in BUFFER support, or
1173 nil if it doesn't support ID or returns no information.
1175 If BUFFER is nil, the current buffer is assumed."
1176 (with-current-buffer (or buffer (current-buffer))
1177 (when (and (imap-capability 'ID)
1178 (imap-ok-p (imap-send-command-wait
1179 (if (null list-of-values)
1181 (concat "ID (" (mapconcat (lambda (el)
1182 (concat "\"" el "\""))
1187 (defun imap-namespace (&optional buffer)
1188 "Return a namespace hierarchy at server in BUFFER.
1189 If BUFFER is nil, the current buffer is assumed."
1190 (with-current-buffer (or buffer (current-buffer))
1191 (unless imap-namespace
1192 (when (imap-capability 'NAMESPACE)
1193 (imap-send-command-wait "NAMESPACE")))
1196 (defun imap-send-command-wait (command &optional buffer)
1197 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1200 ;; Mailbox functions:
1202 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1203 (with-current-buffer (or buffer (current-buffer))
1204 (if imap-mailbox-data
1205 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1207 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1208 propname value mailbox (current-buffer)))
1211 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1212 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1215 (defun imap-mailbox-get (propname &optional mailbox buffer)
1216 (let ((mailbox (imap-utf7-encode mailbox)))
1217 (with-current-buffer (or buffer (current-buffer))
1218 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1220 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1221 (with-current-buffer (or buffer (current-buffer))
1225 (push (funcall func (if mailbox-decoder
1226 (funcall mailbox-decoder (symbol-name s))
1227 (symbol-name s))) result))
1231 (defun imap-mailbox-map (func &optional buffer)
1232 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1233 Function should take a mailbox name (a string) as
1235 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1237 (defun imap-current-mailbox (&optional buffer)
1238 (with-current-buffer (or buffer (current-buffer))
1239 (imap-utf7-decode imap-current-mailbox)))
1241 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1242 (and (string= mailbox imap-current-mailbox)
1244 (eq imap-state 'examine))
1246 (eq imap-state 'selected)))))
1248 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1249 (with-current-buffer (or buffer (current-buffer))
1250 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1252 (defun imap-mailbox-select-1 (mailbox &optional examine)
1253 "Select MAILBOX on server in BUFFER.
1254 If EXAMINE is non-nil, do a read-only select."
1255 (if (imap-current-mailbox-p-1 mailbox examine)
1256 imap-current-mailbox
1257 (setq imap-current-mailbox mailbox)
1258 (if (imap-ok-p (imap-send-command-wait
1259 (concat (if examine "EXAMINE" "SELECT") " \""
1262 (setq imap-message-data (make-vector imap-message-prime 0)
1263 imap-state (if examine 'examine 'selected))
1264 imap-current-mailbox)
1265 ;; Failed SELECT/EXAMINE unselects current mailbox
1266 (setq imap-current-mailbox nil))))
1268 (defun imap-mailbox-select (mailbox &optional examine buffer)
1269 (with-current-buffer (or buffer (current-buffer))
1271 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1273 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1274 (with-current-buffer (or buffer (current-buffer))
1275 (imap-mailbox-select-1 mailbox 'examine)))
1277 (defun imap-mailbox-examine (mailbox &optional buffer)
1278 "Examine MAILBOX on server in BUFFER."
1279 (imap-mailbox-select mailbox 'examine buffer))
1281 (defun imap-mailbox-unselect (&optional buffer)
1282 "Close current folder in BUFFER, without expunging articles."
1283 (with-current-buffer (or buffer (current-buffer))
1284 (when (or (eq imap-state 'auth)
1285 (and (imap-capability 'UNSELECT)
1286 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1288 (imap-send-command-wait (concat "EXAMINE \""
1289 imap-current-mailbox
1291 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1292 (setq imap-current-mailbox nil
1293 imap-message-data nil
1297 (defun imap-mailbox-expunge (&optional asynch buffer)
1298 "Expunge articles in current folder in BUFFER.
1299 If ASYNCH, do not wait for succesful completion of the command.
1300 If BUFFER is nil the current buffer is assumed."
1301 (with-current-buffer (or buffer (current-buffer))
1302 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1304 (imap-send-command "EXPUNGE")
1305 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1307 (defun imap-mailbox-close (&optional asynch buffer)
1308 "Expunge articles and close current folder in BUFFER.
1309 If ASYNCH, do not wait for succesful completion of the command.
1310 If BUFFER is nil the current buffer is assumed."
1311 (with-current-buffer (or buffer (current-buffer))
1312 (when imap-current-mailbox
1314 (imap-add-callback (imap-send-command "CLOSE")
1315 `(lambda (tag status)
1316 (message "IMAP mailbox `%s' closed... %s"
1317 imap-current-mailbox status)
1318 (when (eq ,imap-current-mailbox
1319 imap-current-mailbox)
1320 ;; Don't wipe out data if another mailbox
1322 (setq imap-current-mailbox nil
1323 imap-message-data nil
1324 imap-state 'auth))))
1325 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1326 (setq imap-current-mailbox nil
1327 imap-message-data nil
1331 (defun imap-mailbox-create-1 (mailbox)
1332 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1334 (defun imap-mailbox-create (mailbox &optional buffer)
1335 "Create MAILBOX on server in BUFFER.
1336 If BUFFER is nil the current buffer is assumed."
1337 (with-current-buffer (or buffer (current-buffer))
1338 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1340 (defun imap-mailbox-delete (mailbox &optional buffer)
1341 "Delete MAILBOX on server in BUFFER.
1342 If BUFFER is nil the current buffer is assumed."
1343 (let ((mailbox (imap-utf7-encode mailbox)))
1344 (with-current-buffer (or buffer (current-buffer))
1346 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1348 (defun imap-mailbox-rename (oldname newname &optional buffer)
1349 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1350 If BUFFER is nil the current buffer is assumed."
1351 (let ((oldname (imap-utf7-encode oldname))
1352 (newname (imap-utf7-encode newname)))
1353 (with-current-buffer (or buffer (current-buffer))
1355 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1356 "\"" newname "\""))))))
1358 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1359 "Return a list of subscribed mailboxes on server in BUFFER.
1360 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1361 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1362 implementation-specific string that has to be passed to lsub command."
1363 (with-current-buffer (or buffer (current-buffer))
1364 ;; Make sure we know the hierarchy separator for root's hierarchy
1365 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1366 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1367 (imap-utf7-encode root) "\"")))
1368 ;; clear list data (NB not delimiter and other stuff)
1369 (imap-mailbox-map-1 (lambda (mailbox)
1370 (imap-mailbox-put 'lsub nil mailbox)))
1372 (imap-send-command-wait
1373 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1374 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1377 (imap-mailbox-map-1 (lambda (mailbox)
1378 (when (imap-mailbox-get-1 'lsub mailbox)
1379 (push (imap-utf7-decode mailbox) out))))
1382 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1383 "Return a list of mailboxes matching ROOT on server in BUFFER.
1384 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1385 root. REFERENCE is a implementation-specific string that has to be
1386 passed to list command."
1387 (with-current-buffer (or buffer (current-buffer))
1388 ;; Make sure we know the hierarchy separator for root's hierarchy
1389 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1390 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1391 (imap-utf7-encode root) "\"")))
1392 ;; clear list data (NB not delimiter and other stuff)
1393 (imap-mailbox-map-1 (lambda (mailbox)
1394 (imap-mailbox-put 'list nil mailbox)))
1396 (imap-send-command-wait
1397 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1398 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1401 (imap-mailbox-map-1 (lambda (mailbox)
1402 (when (imap-mailbox-get-1 'list mailbox)
1403 (push (imap-utf7-decode mailbox) out))))
1406 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1407 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1408 Returns non-nil if successful."
1409 (with-current-buffer (or buffer (current-buffer))
1410 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1411 (imap-utf7-encode mailbox)
1414 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1415 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1416 Returns non-nil if successful."
1417 (with-current-buffer (or buffer (current-buffer))
1418 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1419 (imap-utf7-encode mailbox)
1422 (defun imap-mailbox-status (mailbox items &optional buffer)
1423 "Get status items ITEM in MAILBOX from server in BUFFER.
1424 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1425 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1426 or 'unseen. If ITEMS is a list of symbols, a list of values is
1427 returned, if ITEMS is a symbol only its value is returned."
1428 (with-current-buffer (or buffer (current-buffer))
1430 (imap-send-command-wait (list "STATUS \""
1431 (imap-utf7-encode mailbox)
1439 (mapcar (lambda (item)
1440 (imap-mailbox-get item mailbox))
1442 (imap-mailbox-get items mailbox)))))
1444 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1445 "Send status item request ITEM on MAILBOX to server in BUFFER.
1446 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1447 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1448 or 'unseen. The IMAP command tag is returned."
1449 (with-current-buffer (or buffer (current-buffer))
1450 (imap-send-command (list "STATUS \""
1451 (imap-utf7-encode mailbox)
1458 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1459 "Get ACL on mailbox from server in BUFFER."
1460 (let ((mailbox (imap-utf7-encode mailbox)))
1461 (with-current-buffer (or buffer (current-buffer))
1463 (imap-send-command-wait (list "GETACL \""
1464 (or mailbox imap-current-mailbox)
1466 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1468 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1469 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1470 (let ((mailbox (imap-utf7-encode mailbox)))
1471 (with-current-buffer (or buffer (current-buffer))
1473 (imap-send-command-wait (list "SETACL \""
1474 (or mailbox imap-current-mailbox)
1480 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1481 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1482 (let ((mailbox (imap-utf7-encode mailbox)))
1483 (with-current-buffer (or buffer (current-buffer))
1485 (imap-send-command-wait (list "DELETEACL \""
1486 (or mailbox imap-current-mailbox)
1491 ;; Message functions:
1493 (defun imap-current-message (&optional buffer)
1494 (with-current-buffer (or buffer (current-buffer))
1495 imap-current-message))
1497 (defun imap-list-to-message-set (list)
1498 (mapconcat (lambda (item)
1499 (number-to-string item))
1505 (defun imap-range-to-message-set (range)
1510 (car item) (cdr item))
1511 (format "%d" item)))
1512 (if (and (listp range) (not (listp (cdr range))))
1513 (list range) ;; make (1 . 2) into ((1 . 2))
1517 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1518 (with-current-buffer (or buffer (current-buffer))
1519 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1521 (imap-list-to-message-set uids)
1525 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1526 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1527 UIDS can be a string, number or a list of numbers. If RECEIVE
1528 is non-nil return theese properties."
1529 (with-current-buffer (or buffer (current-buffer))
1530 (when (imap-ok-p (imap-send-command-wait
1531 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1533 (imap-list-to-message-set uids)
1536 (if (or (null receive) (stringp uids))
1539 (mapcar (lambda (uid)
1541 (mapcar (lambda (prop)
1542 (imap-message-get uid prop))
1544 (imap-message-get uid receive)))
1546 (imap-message-get uids receive))))))
1548 (defun imap-message-put (uid propname value &optional buffer)
1549 (with-current-buffer (or buffer (current-buffer))
1550 (if imap-message-data
1551 (put (intern (number-to-string uid) imap-message-data)
1553 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1554 uid propname value (current-buffer)))
1557 (defun imap-message-get (uid propname &optional buffer)
1558 (with-current-buffer (or buffer (current-buffer))
1559 (get (intern-soft (number-to-string uid) imap-message-data)
1562 (defun imap-message-map (func propname &optional buffer)
1563 "Map a function across each mailbox in `imap-message-data', returning a list."
1564 (with-current-buffer (or buffer (current-buffer))
1568 (push (funcall func (get s 'UID) (get s propname)) result))
1572 (defmacro imap-message-envelope-date (uid &optional buffer)
1573 `(with-current-buffer (or ,buffer (current-buffer))
1574 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1576 (defmacro imap-message-envelope-subject (uid &optional buffer)
1577 `(with-current-buffer (or ,buffer (current-buffer))
1578 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1580 (defmacro imap-message-envelope-from (uid &optional buffer)
1581 `(with-current-buffer (or ,buffer (current-buffer))
1582 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1584 (defmacro imap-message-envelope-sender (uid &optional buffer)
1585 `(with-current-buffer (or ,buffer (current-buffer))
1586 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1588 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1589 `(with-current-buffer (or ,buffer (current-buffer))
1590 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1592 (defmacro imap-message-envelope-to (uid &optional buffer)
1593 `(with-current-buffer (or ,buffer (current-buffer))
1594 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1596 (defmacro imap-message-envelope-cc (uid &optional buffer)
1597 `(with-current-buffer (or ,buffer (current-buffer))
1598 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1600 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1601 `(with-current-buffer (or ,buffer (current-buffer))
1602 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1604 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1605 `(with-current-buffer (or ,buffer (current-buffer))
1606 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1608 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1609 `(with-current-buffer (or ,buffer (current-buffer))
1610 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1612 (defmacro imap-message-body (uid &optional buffer)
1613 `(with-current-buffer (or ,buffer (current-buffer))
1614 (imap-message-get ,uid 'BODY)))
1616 (defun imap-search (predicate &optional buffer)
1617 (with-current-buffer (or buffer (current-buffer))
1618 (imap-mailbox-put 'search 'dummy)
1619 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1620 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1622 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1624 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1626 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1627 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1628 (with-current-buffer (or buffer (current-buffer))
1629 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1630 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1632 (defun imap-message-flags-set (articles flags &optional silent buffer)
1633 (when (and articles flags)
1634 (with-current-buffer (or buffer (current-buffer))
1635 (imap-ok-p (imap-send-command-wait
1636 (concat "UID STORE " articles
1637 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1639 (defun imap-message-flags-del (articles flags &optional silent buffer)
1640 (when (and articles flags)
1641 (with-current-buffer (or buffer (current-buffer))
1642 (imap-ok-p (imap-send-command-wait
1643 (concat "UID STORE " articles
1644 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1646 (defun imap-message-flags-add (articles flags &optional silent buffer)
1647 (when (and articles flags)
1648 (with-current-buffer (or buffer (current-buffer))
1649 (imap-ok-p (imap-send-command-wait
1650 (concat "UID STORE " articles
1651 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1653 (defun imap-message-copyuid-1 (mailbox)
1654 (if (imap-capability 'UIDPLUS)
1655 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1656 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1657 (let ((old-mailbox imap-current-mailbox)
1659 (imap-message-data (make-vector 2 0)))
1660 (when (imap-mailbox-examine-1 mailbox)
1662 (and (imap-fetch "*" "UID")
1663 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1664 (apply 'max (imap-message-map
1665 (lambda (uid prop) uid) 'UID))))
1667 (imap-mailbox-select old-mailbox (eq state 'examine))
1668 (imap-mailbox-unselect)))))))
1670 (defun imap-message-copyuid (mailbox &optional buffer)
1671 (with-current-buffer (or buffer (current-buffer))
1672 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1674 (defun imap-message-copy (articles mailbox
1675 &optional dont-create no-copyuid buffer)
1676 "Copy ARTICLES (a string message set) to MAILBOX on server in
1677 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1678 non-nil, it will not create a mailbox. On success, return a list with
1679 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1680 first element, rest of list contain the saved articles' UIDs."
1682 (with-current-buffer (or buffer (current-buffer))
1683 (let ((mailbox (imap-utf7-encode mailbox)))
1684 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1685 (imap-current-target-mailbox mailbox))
1686 (if (imap-ok-p (imap-send-command-wait cmd))
1688 (when (and (not dont-create)
1689 ;; removed because of buggy Oracle server
1690 ;; that doesn't send TRYCREATE tags (which
1691 ;; is a MUST according to specifications):
1692 ;;(imap-mailbox-get-1 'trycreate mailbox)
1693 (imap-mailbox-create-1 mailbox))
1694 (imap-ok-p (imap-send-command-wait cmd)))))
1696 (imap-message-copyuid-1 mailbox)))))))
1698 (defun imap-message-appenduid-1 (mailbox)
1699 (if (imap-capability 'UIDPLUS)
1700 (imap-mailbox-get-1 'appenduid mailbox)
1701 (let ((old-mailbox imap-current-mailbox)
1703 (imap-message-data (make-vector 2 0)))
1704 (when (imap-mailbox-examine-1 mailbox)
1706 (and (imap-fetch "*" "UID")
1707 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1708 (apply 'max (imap-message-map
1709 (lambda (uid prop) uid) 'UID))))
1711 (imap-mailbox-select old-mailbox (eq state 'examine))
1712 (imap-mailbox-unselect)))))))
1714 (defun imap-message-appenduid (mailbox &optional buffer)
1715 (with-current-buffer (or buffer (current-buffer))
1716 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1718 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1719 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1720 FLAGS and DATE-TIME is currently not used. Return a cons holding
1721 uidvalidity of MAILBOX and UID the newly created article got, or nil
1723 (let ((mailbox (imap-utf7-encode mailbox)))
1724 (with-current-buffer (or buffer (current-buffer))
1725 (and (let ((imap-current-target-mailbox mailbox))
1727 (imap-send-command-wait
1728 (list "APPEND \"" mailbox "\" " article))))
1729 (imap-message-appenduid-1 mailbox)))))
1731 (defun imap-body-lines (body)
1732 "Return number of lines in article by looking at the mime bodystructure BODY."
1734 (if (stringp (car body))
1735 (cond ((and (string= (upcase (car body)) "TEXT")
1736 (numberp (nth 7 body)))
1738 ((and (string= (upcase (car body)) "MESSAGE")
1739 (numberp (nth 9 body)))
1742 (apply '+ (mapcar 'imap-body-lines body)))
1745 (defun imap-envelope-from (from)
1746 "Return a from string line."
1748 (concat (aref from 0)
1749 (if (aref from 0) " <")
1753 (if (aref from 0) ">"))))
1756 ;; Internal functions.
1758 (defun imap-add-callback (tag func)
1759 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1761 (defun imap-send-command-1 (cmdstr)
1762 (setq cmdstr (concat cmdstr imap-client-eol))
1764 (with-current-buffer (get-buffer-create imap-log-buffer)
1765 (imap-disable-multibyte)
1766 (buffer-disable-undo)
1767 (goto-char (point-max))
1769 (process-send-string imap-process cmdstr))
1771 (defun imap-send-command (command &optional buffer)
1772 (with-current-buffer (or buffer (current-buffer))
1773 (if (not (listp command)) (setq command (list command)))
1774 (let ((tag (setq imap-tag (1+ imap-tag)))
1776 (setq cmdstr (concat (number-to-string imap-tag) " "))
1777 (while (setq cmd (pop command))
1778 (cond ((stringp cmd)
1779 (setq cmdstr (concat cmdstr cmd)))
1781 (let ((eol imap-client-eol)
1782 (calcfirst imap-calculate-literal-size-first)
1784 (with-current-buffer cmd
1786 (setq size (buffer-size)))
1787 (when (not (equal eol "\r\n"))
1788 ;; XXX modifies buffer!
1789 (goto-char (point-min))
1790 (while (search-forward "\r\n" nil t)
1791 (replace-match eol)))
1793 (setq size (buffer-size))))
1795 (concat cmdstr (format "{%d}" size))))
1798 (imap-send-command-1 cmdstr)
1800 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1801 (setq command nil) ;; abort command if no cont-req
1802 (let ((process imap-process)
1803 (stream imap-stream)
1804 (eol imap-client-eol))
1805 (with-current-buffer cmd
1807 (with-current-buffer (get-buffer-create
1809 (imap-disable-multibyte)
1810 (buffer-disable-undo)
1811 (goto-char (point-max))
1812 (insert-buffer-substring cmd)))
1813 (process-send-region process (point-min)
1815 (process-send-string process imap-client-eol))))
1816 (setq imap-continuation nil)))
1818 (imap-send-command-1 cmdstr)
1821 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1822 (setq command nil) ;; abort command if no cont-req
1823 (setq command (cons (funcall cmd imap-continuation)
1825 (setq imap-continuation nil)))
1827 (error "Unknown command type"))))
1829 (imap-send-command-1 cmdstr))
1832 (defun imap-wait-for-tag (tag &optional buffer)
1833 (with-current-buffer (or buffer (current-buffer))
1834 (let (imap-have-messaged)
1835 (while (and (null imap-continuation)
1836 (memq (process-status imap-process) '(open run))
1837 (< imap-reached-tag tag))
1838 (let ((len (/ (point-max) 1024))
1841 (setq imap-have-messaged t)
1842 (message "imap read: %dk" len))
1843 (accept-process-output imap-process
1844 (truncate imap-read-timeout)
1845 (truncate (* (- imap-read-timeout
1846 (truncate imap-read-timeout))
1848 ;; A process can die _before_ we have processed everything it
1849 ;; has to say. Moreover, this can happen in between the call to
1850 ;; accept-process-output and the call to process-status in an
1851 ;; iteration of the loop above.
1852 (when (and (null imap-continuation)
1853 (< imap-reached-tag tag))
1854 (accept-process-output imap-process 0 0))
1855 (when imap-have-messaged
1857 (and (memq (process-status imap-process) '(open run))
1858 (or (assq tag imap-failed-tags)
1859 (if imap-continuation
1863 (defun imap-sentinel (process string)
1864 (delete-process process))
1866 (defun imap-find-next-line ()
1867 "Return point at end of current line, taking into account literals.
1868 Return nil if no complete line has arrived."
1869 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1872 (if (match-string 1)
1873 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1875 (goto-char (+ (point) (string-to-number (match-string 1))))
1876 (imap-find-next-line))
1879 (defun imap-arrival-filter (proc string)
1880 "IMAP process filter."
1881 ;; Sometimes, we are called even though the process has died.
1882 ;; Better abstain from doing stuff in that case.
1883 (when (buffer-name (process-buffer proc))
1884 (with-current-buffer (process-buffer proc)
1885 (goto-char (point-max))
1888 (with-current-buffer (get-buffer-create imap-log-buffer)
1889 (imap-disable-multibyte)
1890 (buffer-disable-undo)
1891 (goto-char (point-max))
1894 (goto-char (point-min))
1895 (while (setq end (imap-find-next-line))
1897 (narrow-to-region (point-min) end)
1898 (delete-backward-char (length imap-server-eol))
1899 (goto-char (point-min))
1901 (cond ((eq imap-state 'initial)
1902 (imap-parse-greeting))
1903 ((or (eq imap-state 'auth)
1904 (eq imap-state 'nonauth)
1905 (eq imap-state 'selected)
1906 (eq imap-state 'examine))
1907 (imap-parse-response))
1909 (message "Unknown state %s in arrival filter"
1911 (delete-region (point-min) (point-max)))))))))
1916 (defsubst imap-forward ()
1917 (or (eobp) (forward-char)))
1920 ;; ; Unsigned 32-bit integer
1921 ;; ; (0 <= n < 4,294,967,296)
1923 (defsubst imap-parse-number ()
1924 (when (looking-at "[0-9]+")
1926 (string-to-number (match-string 0))
1927 (goto-char (match-end 0)))))
1929 ;; literal = "{" number "}" CRLF *CHAR8
1930 ;; ; Number represents the number of CHAR8s
1932 (defsubst imap-parse-literal ()
1933 (when (looking-at "{\\([0-9]+\\)}\r\n")
1934 (let ((pos (match-end 0))
1935 (len (string-to-number (match-string 1))))
1936 (if (< (point-max) (+ pos len))
1938 (goto-char (+ pos len))
1939 (buffer-substring pos (+ pos len))))))
1941 ;; string = quoted / literal
1943 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1945 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1946 ;; "\" quoted-specials
1948 ;; quoted-specials = DQUOTE / "\"
1950 ;; TEXT-CHAR = <any CHAR except CR and LF>
1952 (defsubst imap-parse-string ()
1953 (cond ((eq (char-after) ?\")
1955 (let ((p (point)) (name ""))
1956 (skip-chars-forward "^\"\\\\")
1957 (setq name (buffer-substring p (point)))
1958 (while (eq (char-after) ?\\)
1959 (setq p (1+ (point)))
1961 (skip-chars-forward "^\"\\\\")
1962 (setq name (concat name (buffer-substring p (point)))))
1965 ((eq (char-after) ?{)
1966 (imap-parse-literal))))
1970 (defsubst imap-parse-nil ()
1971 (if (looking-at "NIL")
1972 (goto-char (match-end 0))))
1974 ;; nstring = string / nil
1976 (defsubst imap-parse-nstring ()
1977 (or (imap-parse-string)
1978 (and (imap-parse-nil)
1981 ;; astring = atom / string
1983 ;; atom = 1*ATOM-CHAR
1985 ;; ATOM-CHAR = <any CHAR except atom-specials>
1987 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1990 ;; list-wildcards = "%" / "*"
1992 ;; quoted-specials = DQUOTE / "\"
1994 (defsubst imap-parse-astring ()
1995 (or (imap-parse-string)
1996 (buffer-substring (point)
1997 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1998 (goto-char (1- (match-end 0)))
2002 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2005 ;; addr-adl = nstring
2006 ;; ; Holds route from [RFC-822] route-addr if
2009 ;; addr-host = nstring
2010 ;; ; nil indicates [RFC-822] group syntax.
2011 ;; ; Otherwise, holds [RFC-822] domain name
2013 ;; addr-mailbox = nstring
2014 ;; ; nil indicates end of [RFC-822] group; if
2015 ;; ; non-nil and addr-host is nil, holds
2016 ;; ; [RFC-822] group name.
2017 ;; ; Otherwise, holds [RFC-822] local-part
2018 ;; ; after removing [RFC-822] quoting
2020 ;; addr-name = nstring
2021 ;; ; If non-nil, holds phrase from [RFC-822]
2022 ;; ; mailbox after removing [RFC-822] quoting
2025 (defsubst imap-parse-address ()
2027 (when (eq (char-after) ?\()
2029 (setq address (vector (prog1 (imap-parse-nstring)
2031 (prog1 (imap-parse-nstring)
2033 (prog1 (imap-parse-nstring)
2035 (imap-parse-nstring)))
2036 (when (eq (char-after) ?\))
2040 ;; address-list = "(" 1*address ")" / nil
2044 (defsubst imap-parse-address-list ()
2045 (if (eq (char-after) ?\()
2046 (let (address addresses)
2048 (while (and (not (eq (char-after) ?\)))
2049 ;; next line for MS Exchange bug
2050 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2051 (setq address (imap-parse-address)))
2052 (setq addresses (cons address addresses)))
2053 (when (eq (char-after) ?\))
2055 (nreverse addresses)))
2056 (assert (imap-parse-nil) t "In imap-parse-address-list")))
2058 ;; mailbox = "INBOX" / astring
2059 ;; ; INBOX is case-insensitive. All case variants of
2060 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2061 ;; ; not as an astring. An astring which consists of
2062 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2063 ;; ; is considered to be INBOX and not an astring.
2064 ;; ; Refer to section 5.1 for further
2065 ;; ; semantic details of mailbox names.
2067 (defsubst imap-parse-mailbox ()
2068 (let ((mailbox (imap-parse-astring)))
2069 (if (string-equal "INBOX" (upcase mailbox))
2073 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2075 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2076 ;; ; Authentication condition
2078 ;; resp-cond-bye = "BYE" SP resp-text
2080 (defun imap-parse-greeting ()
2081 "Parse a IMAP greeting."
2082 (cond ((looking-at "\\* OK ")
2083 (setq imap-state 'nonauth))
2084 ((looking-at "\\* PREAUTH ")
2085 (setq imap-state 'auth))
2086 ((looking-at "\\* BYE ")
2087 (setq imap-state 'closed))))
2089 ;; response = *(continue-req / response-data) response-done
2091 ;; continue-req = "+" SP (resp-text / base64) CRLF
2093 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2094 ;; mailbox-data / message-data / capability-data) CRLF
2096 ;; response-done = response-tagged / response-fatal
2098 ;; response-fatal = "*" SP resp-cond-bye CRLF
2099 ;; ; Server closes connection immediately
2101 ;; response-tagged = tag SP resp-cond-state CRLF
2103 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2104 ;; ; Status condition
2106 ;; resp-cond-bye = "BYE" SP resp-text
2108 ;; mailbox-data = "FLAGS" SP flag-list /
2109 ;; "LIST" SP mailbox-list /
2110 ;; "LSUB" SP mailbox-list /
2111 ;; "SEARCH" *(SP nz-number) /
2112 ;; "STATUS" SP mailbox SP "("
2113 ;; [status-att SP number *(SP status-att SP number)] ")" /
2114 ;; number SP "EXISTS" /
2115 ;; number SP "RECENT"
2117 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2119 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2121 ;; ; IMAP4rev1 servers which offer RFC 1730
2122 ;; ; compatibility MUST list "IMAP4" as the first
2125 (defun imap-parse-response ()
2126 "Parse a IMAP command response."
2128 (case (setq token (read (current-buffer)))
2129 (+ (setq imap-continuation
2130 (or (buffer-substring (min (point-max) (1+ (point)))
2133 (* (case (prog1 (setq token (read (current-buffer)))
2135 (OK (imap-parse-resp-text))
2136 (NO (imap-parse-resp-text))
2137 (BAD (imap-parse-resp-text))
2138 (BYE (imap-parse-resp-text))
2139 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2140 (LIST (imap-parse-data-list 'list))
2141 (LSUB (imap-parse-data-list 'lsub))
2142 (SEARCH (imap-mailbox-put
2144 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2145 (STATUS (imap-parse-status))
2146 (CAPABILITY (setq imap-capability
2147 (read (concat "(" (upcase (buffer-substring
2148 (point) (point-max)))
2150 (ID (setq imap-id (read (buffer-substring (point)
2152 (ACL (imap-parse-acl))
2153 (t (case (prog1 (read (current-buffer))
2155 (EXISTS (imap-mailbox-put 'exists token))
2156 (RECENT (imap-mailbox-put 'recent token))
2158 (FETCH (imap-parse-fetch token))
2159 (t (message "Garbage: %s" (buffer-string)))))))
2161 (if (not (integerp token))
2162 (message "Garbage: %s" (buffer-string))
2163 (case (prog1 (setq status (read (current-buffer)))
2166 (setq imap-reached-tag (max imap-reached-tag token))
2167 (imap-parse-resp-text)))
2169 (setq imap-reached-tag (max imap-reached-tag token))
2171 (imap-parse-resp-text))
2173 (when (eq (char-after) ?\[)
2174 (setq code (buffer-substring (point)
2175 (search-forward "]")))
2177 (setq text (buffer-substring (point) (point-max)))
2178 (push (list token status code text)
2179 imap-failed-tags))))
2181 (setq imap-reached-tag (max imap-reached-tag token))
2183 (imap-parse-resp-text))
2185 (when (eq (char-after) ?\[)
2186 (setq code (buffer-substring (point)
2187 (search-forward "]")))
2189 (setq text (buffer-substring (point) (point-max)))
2190 (push (list token status code text) imap-failed-tags)
2191 (error "Internal error, tag %s status %s code %s text %s"
2192 token status code text))))
2193 (t (message "Garbage: %s" (buffer-string))))
2194 (when (assq token imap-callbacks)
2195 (funcall (cdr (assq token imap-callbacks)) token status)
2196 (setq imap-callbacks
2197 (imap-remassoc token imap-callbacks)))))))))
2199 ;; resp-text = ["[" resp-text-code "]" SP] text
2201 ;; text = 1*TEXT-CHAR
2203 ;; TEXT-CHAR = <any CHAR except CR and LF>
2205 (defun imap-parse-resp-text ()
2206 (imap-parse-resp-text-code))
2208 ;; resp-text-code = "ALERT" /
2209 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2210 ;; "NEWNAME" SP string SP string /
2212 ;; "PERMANENTFLAGS" SP "("
2213 ;; [flag-perm *(SP flag-perm)] ")" /
2217 ;; "UIDNEXT" SP nz-number /
2218 ;; "UIDVALIDITY" SP nz-number /
2219 ;; "UNSEEN" SP nz-number /
2220 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2222 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2224 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2226 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2228 ;; ; Identifies a set of messages. For message
2229 ;; ; sequence numbers, these are consecutive
2230 ;; ; numbers from 1 to the number of messages in
2232 ;; ; Comma delimits individual numbers, colon
2233 ;; ; delimits between two numbers inclusive.
2234 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2235 ;; ; 14,15 for a mailbox with 15 messages.
2237 ;; sequence-num = nz-number / "*"
2238 ;; ; * is the largest number in use. For message
2239 ;; ; sequence numbers, it is the number of messages
2240 ;; ; in the mailbox. For unique identifiers, it is
2241 ;; ; the unique identifier of the last message in
2244 ;; flag-perm = flag / "\*"
2246 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2247 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2248 ;; ; Does not include "\Recent"
2250 ;; flag-extension = "\" atom
2251 ;; ; Future expansion. Client implementations
2252 ;; ; MUST accept flag-extension flags. Server
2253 ;; ; implementations MUST NOT generate
2254 ;; ; flag-extension flags except as defined by
2255 ;; ; future standard or standards-track
2256 ;; ; revisions of this specification.
2258 ;; flag-keyword = atom
2260 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2262 (defun imap-parse-resp-text-code ()
2263 ;; xxx next line for stalker communigate pro 3.3.1 bug
2264 (when (looking-at " \\[")
2266 (when (eq (char-after) ?\[)
2268 (cond ((search-forward "PERMANENTFLAGS " nil t)
2269 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2270 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2271 (imap-mailbox-put 'uidnext (match-string 1)))
2272 ((search-forward "UNSEEN " nil t)
2273 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2274 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2275 (imap-mailbox-put 'uidvalidity (match-string 1)))
2276 ((search-forward "READ-ONLY" nil t)
2277 (imap-mailbox-put 'read-only t))
2278 ((search-forward "NEWNAME " nil t)
2279 (let (oldname newname)
2280 (setq oldname (imap-parse-string))
2282 (setq newname (imap-parse-string))
2283 (imap-mailbox-put 'newname newname oldname)))
2284 ((search-forward "TRYCREATE" nil t)
2285 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2286 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2287 (imap-mailbox-put 'appenduid
2288 (list (match-string 1)
2289 (string-to-number (match-string 2)))
2290 imap-current-target-mailbox))
2291 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2292 (imap-mailbox-put 'copyuid (list (match-string 1)
2295 imap-current-target-mailbox))
2296 ((search-forward "ALERT] " nil t)
2297 (message "Imap server %s information: %s" imap-server
2298 (buffer-substring (point) (point-max)))))))
2300 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2301 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2303 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2304 ;; *(SP mbx-list-oflag) /
2305 ;; mbx-list-oflag *(SP mbx-list-oflag)
2307 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2308 ;; ; Other flags; multiple possible per LIST response
2310 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2311 ;; ; Selectability flags; only one per LIST response
2313 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2314 ;; "\" quoted-specials
2316 ;; quoted-specials = DQUOTE / "\"
2318 (defun imap-parse-data-list (type)
2319 (let (flags delimiter mailbox)
2320 (setq flags (imap-parse-flag-list))
2321 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2322 (setq delimiter (match-string 1))
2323 (goto-char (1+ (match-end 0)))
2324 (when (setq mailbox (imap-parse-mailbox))
2325 (imap-mailbox-put type t mailbox)
2326 (imap-mailbox-put 'list-flags flags mailbox)
2327 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2329 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2330 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2331 ;; "INTERNALDATE" SPACE date_time /
2332 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2333 ;; "RFC822.SIZE" SPACE number /
2334 ;; "BODY" ["STRUCTURE"] SPACE body /
2335 ;; "BODY" section ["<" number ">"] SPACE nstring /
2336 ;; "UID" SPACE uniqueid) ")"
2338 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2339 ;; SPACE time SPACE zone <">
2341 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2342 ;; ["." (section_text / "MIME")])] "]"
2344 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2345 ;; SPACE header_list / "TEXT"
2347 ;; header_fld_name ::= astring
2349 ;; header_list ::= "(" 1#header_fld_name ")"
2351 (defsubst imap-parse-header-list ()
2352 (when (eq (char-after) ?\()
2354 (while (not (eq (char-after) ?\)))
2356 (push (imap-parse-astring) strlist))
2358 (nreverse strlist))))
2360 (defsubst imap-parse-fetch-body-section ()
2362 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2363 (if (eq (char-before) ? )
2365 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2366 (search-forward "]" nil t))
2369 (defun imap-parse-fetch (response)
2370 (when (eq (char-after) ?\()
2371 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2372 rfc822size body bodydetail bodystructure flags-empty)
2373 (while (not (eq (char-after) ?\)))
2375 (let ((token (read (current-buffer))))
2377 (cond ((eq token 'UID)
2378 (setq uid (condition-case ()
2379 (read (current-buffer))
2382 (setq flags (imap-parse-flag-list))
2384 (setq flags-empty 't)))
2385 ((eq token 'ENVELOPE)
2386 (setq envelope (imap-parse-envelope)))
2387 ((eq token 'INTERNALDATE)
2388 (setq internaldate (imap-parse-string)))
2390 (setq rfc822 (imap-parse-nstring)))
2391 ((eq token 'RFC822.HEADER)
2392 (setq rfc822header (imap-parse-nstring)))
2393 ((eq token 'RFC822.TEXT)
2394 (setq rfc822text (imap-parse-nstring)))
2395 ((eq token 'RFC822.SIZE)
2396 (setq rfc822size (read (current-buffer))))
2398 (if (eq (char-before) ?\[)
2400 (upcase (imap-parse-fetch-body-section))
2401 (and (eq (char-after) ?<)
2402 (buffer-substring (1+ (point))
2403 (search-forward ">" nil t)))
2404 (progn (imap-forward)
2405 (imap-parse-nstring)))
2407 (setq body (imap-parse-body))))
2408 ((eq token 'BODYSTRUCTURE)
2409 (setq bodystructure (imap-parse-body))))))
2411 (setq imap-current-message uid)
2412 (imap-message-put uid 'UID uid)
2413 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2414 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2415 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2416 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2417 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2418 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2419 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2420 (and body (imap-message-put uid 'BODY body))
2421 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2422 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2423 (run-hooks 'imap-fetch-data-hook)))))
2425 ;; mailbox-data = ...
2426 ;; "STATUS" SP mailbox SP "("
2427 ;; [status-att SP number
2428 ;; *(SP status-att SP number)] ")"
2431 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2434 (defun imap-parse-status ()
2435 (let ((mailbox (imap-parse-mailbox)))
2436 (if (eq (char-after) ? )
2438 (when (and mailbox (eq (char-after) ?\())
2439 (while (and (not (eq (char-after) ?\)))
2440 (or (forward-char) t)
2441 (looking-at "\\([A-Za-z]+\\) "))
2442 (let ((token (match-string 1)))
2443 (goto-char (match-end 0))
2444 (cond ((string= token "MESSAGES")
2445 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2446 ((string= token "RECENT")
2447 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2448 ((string= token "UIDNEXT")
2449 (and (looking-at "[0-9]+")
2450 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2451 (goto-char (match-end 0))))
2452 ((string= token "UIDVALIDITY")
2453 (and (looking-at "[0-9]+")
2454 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2455 (goto-char (match-end 0))))
2456 ((string= token "UNSEEN")
2457 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2459 (message "Unknown status data %s in mailbox %s ignored"
2461 (read (current-buffer)))))))))
2463 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2466 ;; identifier ::= astring
2468 ;; rights ::= astring
2470 (defun imap-parse-acl ()
2471 (let ((mailbox (imap-parse-mailbox))
2472 identifier rights acl)
2473 (while (eq (char-after) ?\ )
2475 (setq identifier (imap-parse-astring))
2477 (setq rights (imap-parse-astring))
2478 (setq acl (append acl (list (cons identifier rights)))))
2479 (imap-mailbox-put 'acl acl mailbox)))
2481 ;; flag-list = "(" [flag *(SP flag)] ")"
2483 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2484 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2485 ;; ; Does not include "\Recent"
2487 ;; flag-keyword = atom
2489 ;; flag-extension = "\" atom
2490 ;; ; Future expansion. Client implementations
2491 ;; ; MUST accept flag-extension flags. Server
2492 ;; ; implementations MUST NOT generate
2493 ;; ; flag-extension flags except as defined by
2494 ;; ; future standard or standards-track
2495 ;; ; revisions of this specification.
2497 (defun imap-parse-flag-list ()
2498 (let (flag-list start)
2499 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2500 (while (and (not (eq (char-after) ?\)))
2503 ;; next line for Courier IMAP bug.
2504 (skip-chars-forward " ")
2506 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2507 (push (buffer-substring start (point)) flag-list))
2508 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2510 (nreverse flag-list)))
2512 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2513 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2514 ;; env-in-reply-to SP env-message-id ")"
2516 ;; env-bcc = "(" 1*address ")" / nil
2518 ;; env-cc = "(" 1*address ")" / nil
2520 ;; env-date = nstring
2522 ;; env-from = "(" 1*address ")" / nil
2524 ;; env-in-reply-to = nstring
2526 ;; env-message-id = nstring
2528 ;; env-reply-to = "(" 1*address ")" / nil
2530 ;; env-sender = "(" 1*address ")" / nil
2532 ;; env-subject = nstring
2534 ;; env-to = "(" 1*address ")" / nil
2536 (defun imap-parse-envelope ()
2537 (when (eq (char-after) ?\()
2539 (vector (prog1 (imap-parse-nstring) ;; date
2541 (prog1 (imap-parse-nstring) ;; subject
2543 (prog1 (imap-parse-address-list) ;; from
2545 (prog1 (imap-parse-address-list) ;; sender
2547 (prog1 (imap-parse-address-list) ;; reply-to
2549 (prog1 (imap-parse-address-list) ;; to
2551 (prog1 (imap-parse-address-list) ;; cc
2553 (prog1 (imap-parse-address-list) ;; bcc
2555 (prog1 (imap-parse-nstring) ;; in-reply-to
2557 (prog1 (imap-parse-nstring) ;; message-id
2560 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2562 (defsubst imap-parse-string-list ()
2563 (cond ((eq (char-after) ?\() ;; body-fld-param
2566 (while (setq str (imap-parse-string))
2568 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2569 ;; between body-fld-param's sometimes
2570 (or (eq (char-after) ?\")
2572 (nreverse strlist)))
2576 ;; body-extension = nstring / number /
2577 ;; "(" body-extension *(SP body-extension) ")"
2578 ;; ; Future expansion. Client implementations
2579 ;; ; MUST accept body-extension fields. Server
2580 ;; ; implementations MUST NOT generate
2581 ;; ; body-extension fields except as defined by
2582 ;; ; future standard or standards-track
2583 ;; ; revisions of this specification.
2585 (defun imap-parse-body-extension ()
2586 (if (eq (char-after) ?\()
2589 (push (imap-parse-body-extension) b-e)
2590 (while (eq (char-after) ?\ )
2592 (push (imap-parse-body-extension) b-e))
2593 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2596 (or (imap-parse-number)
2597 (imap-parse-nstring))))
2599 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2600 ;; *(SP body-extension)]]
2601 ;; ; MUST NOT be returned on non-extensible
2604 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2605 ;; *(SP body-extension)]]
2606 ;; ; MUST NOT be returned on non-extensible
2609 (defsubst imap-parse-body-ext ()
2611 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2614 (if (eq (char-after) ?\()
2617 (push (imap-parse-string) dsp)
2619 (push (imap-parse-string-list) dsp)
2621 (assert (imap-parse-nil) t "In imap-parse-body-ext"))
2622 (push (nreverse dsp) ext))
2623 (when (eq (char-after) ?\ ) ;; body-fld-lang
2625 (if (eq (char-after) ?\()
2626 (push (imap-parse-string-list) ext)
2627 (push (imap-parse-nstring) ext))
2628 (while (eq (char-after) ?\ ) ;; body-extension
2630 (setq ext (append (imap-parse-body-extension) ext)))))
2633 ;; body = "(" body-type-1part / body-type-mpart ")"
2635 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2636 ;; *(SP body-extension)]]
2637 ;; ; MUST NOT be returned on non-extensible
2640 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2641 ;; *(SP body-extension)]]
2642 ;; ; MUST NOT be returned on non-extensible
2645 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2646 ;; body-fld-enc SP body-fld-octets
2648 ;; body-fld-desc = nstring
2650 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2652 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2653 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2655 ;; body-fld-id = nstring
2657 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2659 ;; body-fld-lines = number
2661 ;; body-fld-md5 = nstring
2663 ;; body-fld-octets = number
2665 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2667 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2668 ;; [SP body-ext-1part]
2670 ;; body-type-basic = media-basic SP body-fields
2671 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2673 ;; body-type-msg = media-message SP body-fields SP envelope
2674 ;; SP body SP body-fld-lines
2676 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2678 ;; body-type-mpart = 1*body SP media-subtype
2679 ;; [SP body-ext-mpart]
2681 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2682 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2683 ;; ; Defined in [MIME-IMT]
2685 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2686 ;; ; Defined in [MIME-IMT]
2688 ;; media-subtype = string
2689 ;; ; Defined in [MIME-IMT]
2691 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2692 ;; ; Defined in [MIME-IMT]
2694 (defun imap-parse-body ()
2696 (when (eq (char-after) ?\()
2698 (if (eq (char-after) ?\()
2700 (while (and (eq (char-after) ?\()
2701 (setq subbody (imap-parse-body)))
2702 ;; buggy stalker communigate pro 3.0 insert a SPC between
2703 ;; parts in multiparts
2704 (when (and (eq (char-after) ?\ )
2705 (eq (char-after (1+ (point))) ?\())
2707 (push subbody body))
2709 (push (imap-parse-string) body) ;; media-subtype
2710 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2712 (if (eq (char-after) ?\() ;; body-fld-param
2713 (push (imap-parse-string-list) body)
2714 (push (and (imap-parse-nil) nil) body))
2716 (append (imap-parse-body-ext) body))) ;; body-ext-...
2717 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2721 (push (imap-parse-string) body) ;; media-type
2723 (push (imap-parse-string) body) ;; media-subtype
2725 ;; next line for Sun SIMS bug
2726 (and (eq (char-after) ? ) (imap-forward))
2727 (if (eq (char-after) ?\() ;; body-fld-param
2728 (push (imap-parse-string-list) body)
2729 (push (and (imap-parse-nil) nil) body))
2731 (push (imap-parse-nstring) body) ;; body-fld-id
2733 (push (imap-parse-nstring) body) ;; body-fld-desc
2735 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2736 ;; nstring and return nil instead of defaulting back to 7BIT
2737 ;; as the standard says.
2738 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2740 (push (imap-parse-number) body) ;; body-fld-octets
2742 ;; ok, we're done parsing the required parts, what comes now is one
2745 ;; envelope (then we're parsing body-type-msg)
2746 ;; body-fld-lines (then we're parsing body-type-text)
2747 ;; body-ext-1part (then we're parsing body-type-basic)
2749 ;; the problem is that the two first are in turn optionally followed
2750 ;; by the third. So we parse the first two here (if there are any)...
2752 (when (eq (char-after) ?\ )
2755 (cond ((eq (char-after) ?\() ;; body-type-msg:
2756 (push (imap-parse-envelope) body) ;; envelope
2758 (push (imap-parse-body) body) ;; body
2759 ;; buggy stalker communigate pro 3.0 doesn't print
2760 ;; number of lines in message/rfc822 attachment
2761 (if (eq (char-after) ?\))
2764 (push (imap-parse-number) body))) ;; body-fld-lines
2765 ((setq lines (imap-parse-number)) ;; body-type-text:
2766 (push lines body)) ;; body-fld-lines
2768 (backward-char))))) ;; no match...
2770 ;; ...and then parse the third one here...
2772 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2774 (push (imap-parse-nstring) body) ;; body-fld-md5
2775 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2777 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2781 (when imap-debug ; (untrace-all)
2783 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2784 (mapcar (lambda (f) (trace-function-background f imap-debug-buffer))
2795 imap-interactive-login
2811 imap-send-command-wait
2816 imap-current-mailbox
2817 imap-current-mailbox-p-1
2818 imap-current-mailbox-p
2819 imap-mailbox-select-1
2821 imap-mailbox-examine-1
2822 imap-mailbox-examine
2823 imap-mailbox-unselect
2824 imap-mailbox-expunge
2826 imap-mailbox-create-1
2832 imap-mailbox-subscribe
2833 imap-mailbox-unsubscribe
2835 imap-mailbox-acl-get
2836 imap-mailbox-acl-set
2837 imap-mailbox-acl-delete
2838 imap-current-message
2839 imap-list-to-message-set
2846 imap-message-flag-permanent-p
2847 imap-message-flags-set
2848 imap-message-flags-del
2849 imap-message-flags-add
2850 imap-message-copyuid-1
2851 imap-message-copyuid
2853 imap-message-appenduid-1
2854 imap-message-appenduid
2866 imap-parse-resp-text
2867 imap-parse-resp-text-code
2868 imap-parse-data-list
2872 imap-parse-flag-list
2874 imap-parse-body-extension
2880 ;;; imap.el ends here