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 imap-process-connection-type)
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 ;;; Compiler directives.
910 (defvar imap-sasl-client)
911 (defvar imap-sasl-step)
913 (defun imap-sasl-make-mechanisms (buffer)
916 (let ((name (symbol-name sym)))
917 (if (and (> (length name) 5)
918 (string-equal "AUTH=" (substring name 0 5 )))
919 (setq mecs (cons (substring name 5) mecs)))))
920 (imap-capability nil buffer))
923 (defun imap-sasl-auth-p (buffer)
924 (and (condition-case ()
927 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
929 (defun imap-sasl-auth (buffer)
930 "Login to server using the SASL method."
931 (message "imap: Authenticating using SASL...")
932 (with-current-buffer buffer
933 (make-local-variable 'imap-username)
934 (make-local-variable 'imap-sasl-client)
935 (make-local-variable 'imap-sasl-step)
936 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
939 (setq user (or imap-username
940 (read-from-minibuffer
941 (concat "IMAP username for " imap-server " using SASL "
942 (sasl-mechanism-name mechanism) ": ")
943 (or user imap-default-user))))
945 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
946 imap-sasl-step (sasl-next-step imap-sasl-client nil))
947 (let ((tag (imap-send-command
948 (if (sasl-step-data imap-sasl-step)
949 (format "AUTHENTICATE %s %s"
950 (sasl-mechanism-name mechanism)
951 (sasl-step-data imap-sasl-step))
952 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
954 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
955 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
956 (setq imap-continuation nil
957 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
958 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
959 (base64-encode-string (sasl-step-data imap-sasl-step) t)
961 (if (imap-ok-p (imap-wait-for-tag tag))
962 (setq imap-username user
964 (message "Login failed...")
968 (defun imap-digest-md5-p (buffer)
969 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
971 (require 'digest-md5)
974 (defun imap-digest-md5-auth (buffer)
975 "Login to server using the AUTH DIGEST-MD5 method."
976 (message "imap: Authenticating using DIGEST-MD5...")
977 (imap-interactive-login
979 (lambda (user passwd)
983 "AUTHENTICATE DIGEST-MD5"
985 (digest-md5-parse-digest-challenge
986 (base64-decode-string challenge))
988 (digest-md5-digest-uri
989 "imap" (digest-md5-challenge 'realm)))
991 (digest-md5-digest-response
992 user passwd digest-uri)))
993 (base64-encode-string response 'no-line-break))))
995 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
997 (setq imap-continuation nil)
998 (imap-send-command-1 "")
999 (imap-ok-p (imap-wait-for-tag tag)))))))
1001 ;; Server functions:
1003 (defun imap-open-1 (buffer)
1004 (with-current-buffer buffer
1006 (setq imap-current-mailbox nil
1007 imap-current-message nil
1009 imap-process (condition-case ()
1010 (funcall (nth 2 (assq imap-stream
1012 "imap" buffer imap-server imap-port)
1013 ((error quit) nil)))
1015 (set-process-filter imap-process 'imap-arrival-filter)
1016 (set-process-sentinel imap-process 'imap-sentinel)
1017 (while (and (eq imap-state 'initial)
1018 (memq (process-status imap-process) '(open run)))
1019 (message "Waiting for response from %s..." imap-server)
1020 (accept-process-output imap-process 1))
1021 (message "Waiting for response from %s...done" imap-server)
1022 (and (memq (process-status imap-process) '(open run))
1025 (defun imap-open (server &optional port stream auth buffer)
1026 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1027 If PORT is unspecified, a default value is used (143 except
1028 for SSL which use 993).
1029 STREAM indicates the stream to use, see `imap-streams' for available
1030 streams. If nil, it choices the best stream the server is capable of.
1031 AUTH indicates authenticator to use, see `imap-authenticators' for
1032 available authenticators. If nil, it choices the best stream the
1033 server is capable of.
1034 BUFFER can be a buffer or a name of a buffer, which is created if
1035 necessary. If nil, the buffer name is generated."
1036 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1037 (with-current-buffer (get-buffer-create buffer)
1038 (if (imap-opened buffer)
1039 (imap-close buffer))
1040 (mapcar 'make-local-variable imap-local-variables)
1041 (imap-disable-multibyte)
1042 (buffer-disable-undo)
1043 (setq imap-server (or server imap-server))
1044 (setq imap-port (or port imap-port))
1045 (setq imap-auth (or auth imap-auth))
1046 (setq imap-stream (or stream imap-stream))
1047 (message "imap: Connecting to %s..." imap-server)
1048 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1049 (imap-open-1 buffer)))
1051 (message "imap: Connecting to %s...failed" imap-server)
1053 (when (null imap-stream)
1054 ;; Need to choose stream.
1055 (let ((streams imap-streams))
1056 (while (setq stream (pop streams))
1057 ;; OK to use this stream?
1058 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1060 (if (not (eq imap-default-stream stream))
1061 (with-current-buffer (get-buffer-create
1062 (generate-new-buffer-name " *temp*"))
1063 (mapcar 'make-local-variable imap-local-variables)
1064 (imap-disable-multibyte)
1065 (buffer-disable-undo)
1066 (setq imap-server (or server imap-server))
1067 (setq imap-port (or port imap-port))
1068 (setq imap-auth (or auth imap-auth))
1069 (message "imap: Reconnecting with stream `%s'..." stream)
1070 (if (null (let ((imap-stream stream))
1071 (imap-open-1 (current-buffer))))
1073 (kill-buffer (current-buffer))
1075 "imap: Reconnecting with stream `%s'...failed"
1077 ;; We're done, kill the first connection
1079 (kill-buffer buffer)
1080 (rename-buffer buffer)
1081 (message "imap: Reconnecting with stream `%s'...done"
1083 (setq imap-stream stream)
1084 (setq imap-capability nil)
1085 (setq streams nil)))
1087 (message "imap: Connecting to %s...done" imap-server)
1088 (setq imap-stream stream)
1089 (setq imap-capability nil)
1090 (setq streams nil))))))
1091 (when (imap-opened buffer)
1092 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1096 (defun imap-opened (&optional buffer)
1097 "Return non-nil if connection to imap server in BUFFER is open.
1098 If BUFFER is nil then the current buffer is used."
1099 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1100 (buffer-live-p buffer)
1101 (with-current-buffer buffer
1103 (memq (process-status imap-process) '(open run))))))
1105 (defun imap-authenticate (&optional user passwd buffer)
1106 "Authenticate to server in BUFFER, using current buffer if nil.
1107 It uses the authenticator specified when opening the server. If the
1108 authenticator requires username/passwords, they are queried from the
1109 user and optionally stored in the buffer. If USER and/or PASSWD is
1110 specified, the user will not be questioned and the username and/or
1111 password is remembered in the buffer."
1112 (with-current-buffer (or buffer (current-buffer))
1113 (if (not (eq imap-state 'nonauth))
1114 (or (eq imap-state 'auth)
1115 (eq imap-state 'select)
1116 (eq imap-state 'examine))
1117 (make-local-variable 'imap-username)
1118 (make-local-variable 'imap-password)
1119 (if user (setq imap-username user))
1120 (if passwd (setq imap-password passwd))
1122 (and (funcall (nth 2 (assq imap-auth
1123 imap-authenticator-alist)) buffer)
1124 (setq imap-state 'auth))
1125 ;; Choose authenticator.
1126 (let ((auths imap-authenticators)
1128 (while (setq auth (pop auths))
1129 ;; OK to use authenticator?
1130 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1131 (message "imap: Authenticating to `%s' using `%s'..."
1133 (setq imap-auth auth)
1134 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1136 (message "imap: Authenticating to `%s' using `%s'...done"
1139 (message "imap: Authenticating to `%s' using `%s'...failed"
1140 imap-server auth)))))
1143 (defun imap-close (&optional buffer)
1144 "Close connection to server in BUFFER.
1145 If BUFFER is nil, the current buffer is used."
1146 (with-current-buffer (or buffer (current-buffer))
1149 (imap-send-command-wait "LOGOUT")
1151 (when (and imap-process
1152 (memq (process-status imap-process) '(open run)))
1153 (delete-process imap-process))
1154 (setq imap-current-mailbox nil
1155 imap-current-message nil
1160 (defun imap-capability (&optional identifier buffer)
1161 "Return a list of identifiers which server in BUFFER support.
1162 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1163 If BUFFER is nil, the current buffer is assumed."
1164 (with-current-buffer (or buffer (current-buffer))
1165 (unless imap-capability
1166 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1167 (setq imap-capability '(IMAP2))))
1169 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1172 (defun imap-id (&optional list-of-values buffer)
1173 "Identify client to server in BUFFER, and return server identity.
1174 LIST-OF-VALUES is nil, or a plist with identifier and value
1175 strings to send to the server to identify the client.
1177 Return a list of identifiers which server in BUFFER support, or
1178 nil if it doesn't support ID or returns no information.
1180 If BUFFER is nil, the current buffer is assumed."
1181 (with-current-buffer (or buffer (current-buffer))
1182 (when (and (imap-capability 'ID)
1183 (imap-ok-p (imap-send-command-wait
1184 (if (null list-of-values)
1186 (concat "ID (" (mapconcat (lambda (el)
1187 (concat "\"" el "\""))
1192 (defun imap-namespace (&optional buffer)
1193 "Return a namespace hierarchy at server in BUFFER.
1194 If BUFFER is nil, the current buffer is assumed."
1195 (with-current-buffer (or buffer (current-buffer))
1196 (unless imap-namespace
1197 (when (imap-capability 'NAMESPACE)
1198 (imap-send-command-wait "NAMESPACE")))
1201 (defun imap-send-command-wait (command &optional buffer)
1202 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1205 ;; Mailbox functions:
1207 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1208 (with-current-buffer (or buffer (current-buffer))
1209 (if imap-mailbox-data
1210 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1212 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1213 propname value mailbox (current-buffer)))
1216 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1217 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1220 (defun imap-mailbox-get (propname &optional mailbox buffer)
1221 (let ((mailbox (imap-utf7-encode mailbox)))
1222 (with-current-buffer (or buffer (current-buffer))
1223 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1225 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1226 (with-current-buffer (or buffer (current-buffer))
1230 (push (funcall func (if mailbox-decoder
1231 (funcall mailbox-decoder (symbol-name s))
1232 (symbol-name s))) result))
1236 (defun imap-mailbox-map (func &optional buffer)
1237 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1238 Function should take a mailbox name (a string) as
1240 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1242 (defun imap-current-mailbox (&optional buffer)
1243 (with-current-buffer (or buffer (current-buffer))
1244 (imap-utf7-decode imap-current-mailbox)))
1246 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1247 (and (string= mailbox imap-current-mailbox)
1249 (eq imap-state 'examine))
1251 (eq imap-state 'selected)))))
1253 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1254 (with-current-buffer (or buffer (current-buffer))
1255 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1257 (defun imap-mailbox-select-1 (mailbox &optional examine)
1258 "Select MAILBOX on server in BUFFER.
1259 If EXAMINE is non-nil, do a read-only select."
1260 (if (imap-current-mailbox-p-1 mailbox examine)
1261 imap-current-mailbox
1262 (setq imap-current-mailbox mailbox)
1263 (if (imap-ok-p (imap-send-command-wait
1264 (concat (if examine "EXAMINE" "SELECT") " \""
1267 (setq imap-message-data (make-vector imap-message-prime 0)
1268 imap-state (if examine 'examine 'selected))
1269 imap-current-mailbox)
1270 ;; Failed SELECT/EXAMINE unselects current mailbox
1271 (setq imap-current-mailbox nil))))
1273 (defun imap-mailbox-select (mailbox &optional examine buffer)
1274 (with-current-buffer (or buffer (current-buffer))
1276 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1278 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1279 (with-current-buffer (or buffer (current-buffer))
1280 (imap-mailbox-select-1 mailbox 'examine)))
1282 (defun imap-mailbox-examine (mailbox &optional buffer)
1283 "Examine MAILBOX on server in BUFFER."
1284 (imap-mailbox-select mailbox 'examine buffer))
1286 (defun imap-mailbox-unselect (&optional buffer)
1287 "Close current folder in BUFFER, without expunging articles."
1288 (with-current-buffer (or buffer (current-buffer))
1289 (when (or (eq imap-state 'auth)
1290 (and (imap-capability 'UNSELECT)
1291 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1293 (imap-send-command-wait (concat "EXAMINE \""
1294 imap-current-mailbox
1296 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1297 (setq imap-current-mailbox nil
1298 imap-message-data nil
1302 (defun imap-mailbox-expunge (&optional asynch buffer)
1303 "Expunge articles in current folder in BUFFER.
1304 If ASYNCH, do not wait for succesful completion of the command.
1305 If BUFFER is nil the current buffer is assumed."
1306 (with-current-buffer (or buffer (current-buffer))
1307 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1309 (imap-send-command "EXPUNGE")
1310 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1312 (defun imap-mailbox-close (&optional asynch buffer)
1313 "Expunge articles and close current folder in BUFFER.
1314 If ASYNCH, do not wait for succesful completion of the command.
1315 If BUFFER is nil the current buffer is assumed."
1316 (with-current-buffer (or buffer (current-buffer))
1317 (when imap-current-mailbox
1319 (imap-add-callback (imap-send-command "CLOSE")
1320 `(lambda (tag status)
1321 (message "IMAP mailbox `%s' closed... %s"
1322 imap-current-mailbox status)
1323 (when (eq ,imap-current-mailbox
1324 imap-current-mailbox)
1325 ;; Don't wipe out data if another mailbox
1327 (setq imap-current-mailbox nil
1328 imap-message-data nil
1329 imap-state 'auth))))
1330 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1331 (setq imap-current-mailbox nil
1332 imap-message-data nil
1336 (defun imap-mailbox-create-1 (mailbox)
1337 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1339 (defun imap-mailbox-create (mailbox &optional buffer)
1340 "Create MAILBOX on server in BUFFER.
1341 If BUFFER is nil the current buffer is assumed."
1342 (with-current-buffer (or buffer (current-buffer))
1343 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1345 (defun imap-mailbox-delete (mailbox &optional buffer)
1346 "Delete MAILBOX on server in BUFFER.
1347 If BUFFER is nil the current buffer is assumed."
1348 (let ((mailbox (imap-utf7-encode mailbox)))
1349 (with-current-buffer (or buffer (current-buffer))
1351 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1353 (defun imap-mailbox-rename (oldname newname &optional buffer)
1354 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1355 If BUFFER is nil the current buffer is assumed."
1356 (let ((oldname (imap-utf7-encode oldname))
1357 (newname (imap-utf7-encode newname)))
1358 (with-current-buffer (or buffer (current-buffer))
1360 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1361 "\"" newname "\""))))))
1363 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1364 "Return a list of subscribed mailboxes on server in BUFFER.
1365 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1366 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1367 implementation-specific string that has to be passed to lsub command."
1368 (with-current-buffer (or buffer (current-buffer))
1369 ;; Make sure we know the hierarchy separator for root's hierarchy
1370 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1371 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1372 (imap-utf7-encode root) "\"")))
1373 ;; clear list data (NB not delimiter and other stuff)
1374 (imap-mailbox-map-1 (lambda (mailbox)
1375 (imap-mailbox-put 'lsub nil mailbox)))
1377 (imap-send-command-wait
1378 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1379 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1382 (imap-mailbox-map-1 (lambda (mailbox)
1383 (when (imap-mailbox-get-1 'lsub mailbox)
1384 (push (imap-utf7-decode mailbox) out))))
1387 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1388 "Return a list of mailboxes matching ROOT on server in BUFFER.
1389 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1390 root. REFERENCE is a implementation-specific string that has to be
1391 passed to list command."
1392 (with-current-buffer (or buffer (current-buffer))
1393 ;; Make sure we know the hierarchy separator for root's hierarchy
1394 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1395 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1396 (imap-utf7-encode root) "\"")))
1397 ;; clear list data (NB not delimiter and other stuff)
1398 (imap-mailbox-map-1 (lambda (mailbox)
1399 (imap-mailbox-put 'list nil mailbox)))
1401 (imap-send-command-wait
1402 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1403 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1406 (imap-mailbox-map-1 (lambda (mailbox)
1407 (when (imap-mailbox-get-1 'list mailbox)
1408 (push (imap-utf7-decode mailbox) out))))
1411 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1412 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1413 Returns non-nil if successful."
1414 (with-current-buffer (or buffer (current-buffer))
1415 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1416 (imap-utf7-encode mailbox)
1419 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1420 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1421 Returns non-nil if successful."
1422 (with-current-buffer (or buffer (current-buffer))
1423 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1424 (imap-utf7-encode mailbox)
1427 (defun imap-mailbox-status (mailbox items &optional buffer)
1428 "Get status items ITEM in MAILBOX from server in BUFFER.
1429 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1430 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1431 or 'unseen. If ITEMS is a list of symbols, a list of values is
1432 returned, if ITEMS is a symbol only its value is returned."
1433 (with-current-buffer (or buffer (current-buffer))
1435 (imap-send-command-wait (list "STATUS \""
1436 (imap-utf7-encode mailbox)
1444 (mapcar (lambda (item)
1445 (imap-mailbox-get item mailbox))
1447 (imap-mailbox-get items mailbox)))))
1449 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1450 "Send status item request ITEM on MAILBOX to server in BUFFER.
1451 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1452 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1453 or 'unseen. The IMAP command tag is returned."
1454 (with-current-buffer (or buffer (current-buffer))
1455 (imap-send-command (list "STATUS \""
1456 (imap-utf7-encode mailbox)
1463 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1464 "Get ACL on mailbox from server in BUFFER."
1465 (let ((mailbox (imap-utf7-encode mailbox)))
1466 (with-current-buffer (or buffer (current-buffer))
1468 (imap-send-command-wait (list "GETACL \""
1469 (or mailbox imap-current-mailbox)
1471 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1473 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1474 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1475 (let ((mailbox (imap-utf7-encode mailbox)))
1476 (with-current-buffer (or buffer (current-buffer))
1478 (imap-send-command-wait (list "SETACL \""
1479 (or mailbox imap-current-mailbox)
1485 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1486 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1487 (let ((mailbox (imap-utf7-encode mailbox)))
1488 (with-current-buffer (or buffer (current-buffer))
1490 (imap-send-command-wait (list "DELETEACL \""
1491 (or mailbox imap-current-mailbox)
1496 ;; Message functions:
1498 (defun imap-current-message (&optional buffer)
1499 (with-current-buffer (or buffer (current-buffer))
1500 imap-current-message))
1502 (defun imap-list-to-message-set (list)
1503 (mapconcat (lambda (item)
1504 (number-to-string item))
1510 (defun imap-range-to-message-set (range)
1515 (car item) (cdr item))
1516 (format "%d" item)))
1517 (if (and (listp range) (not (listp (cdr range))))
1518 (list range) ;; make (1 . 2) into ((1 . 2))
1522 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1523 (with-current-buffer (or buffer (current-buffer))
1524 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1526 (imap-list-to-message-set uids)
1530 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1531 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1532 UIDS can be a string, number or a list of numbers. If RECEIVE
1533 is non-nil return theese properties."
1534 (with-current-buffer (or buffer (current-buffer))
1535 (when (imap-ok-p (imap-send-command-wait
1536 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1538 (imap-list-to-message-set uids)
1541 (if (or (null receive) (stringp uids))
1544 (mapcar (lambda (uid)
1546 (mapcar (lambda (prop)
1547 (imap-message-get uid prop))
1549 (imap-message-get uid receive)))
1551 (imap-message-get uids receive))))))
1553 (defun imap-message-put (uid propname value &optional buffer)
1554 (with-current-buffer (or buffer (current-buffer))
1555 (if imap-message-data
1556 (put (intern (number-to-string uid) imap-message-data)
1558 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1559 uid propname value (current-buffer)))
1562 (defun imap-message-get (uid propname &optional buffer)
1563 (with-current-buffer (or buffer (current-buffer))
1564 (get (intern-soft (number-to-string uid) imap-message-data)
1567 (defun imap-message-map (func propname &optional buffer)
1568 "Map a function across each mailbox in `imap-message-data', returning a list."
1569 (with-current-buffer (or buffer (current-buffer))
1573 (push (funcall func (get s 'UID) (get s propname)) result))
1577 (defmacro imap-message-envelope-date (uid &optional buffer)
1578 `(with-current-buffer (or ,buffer (current-buffer))
1579 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1581 (defmacro imap-message-envelope-subject (uid &optional buffer)
1582 `(with-current-buffer (or ,buffer (current-buffer))
1583 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1585 (defmacro imap-message-envelope-from (uid &optional buffer)
1586 `(with-current-buffer (or ,buffer (current-buffer))
1587 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1589 (defmacro imap-message-envelope-sender (uid &optional buffer)
1590 `(with-current-buffer (or ,buffer (current-buffer))
1591 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1593 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1594 `(with-current-buffer (or ,buffer (current-buffer))
1595 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1597 (defmacro imap-message-envelope-to (uid &optional buffer)
1598 `(with-current-buffer (or ,buffer (current-buffer))
1599 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1601 (defmacro imap-message-envelope-cc (uid &optional buffer)
1602 `(with-current-buffer (or ,buffer (current-buffer))
1603 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1605 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1606 `(with-current-buffer (or ,buffer (current-buffer))
1607 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1609 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1610 `(with-current-buffer (or ,buffer (current-buffer))
1611 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1613 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1614 `(with-current-buffer (or ,buffer (current-buffer))
1615 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1617 (defmacro imap-message-body (uid &optional buffer)
1618 `(with-current-buffer (or ,buffer (current-buffer))
1619 (imap-message-get ,uid 'BODY)))
1621 (defun imap-search (predicate &optional buffer)
1622 (with-current-buffer (or buffer (current-buffer))
1623 (imap-mailbox-put 'search 'dummy)
1624 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1625 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1627 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1629 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1631 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1632 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1633 (with-current-buffer (or buffer (current-buffer))
1634 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1635 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1637 (defun imap-message-flags-set (articles flags &optional silent buffer)
1638 (when (and articles flags)
1639 (with-current-buffer (or buffer (current-buffer))
1640 (imap-ok-p (imap-send-command-wait
1641 (concat "UID STORE " articles
1642 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1644 (defun imap-message-flags-del (articles flags &optional silent buffer)
1645 (when (and articles flags)
1646 (with-current-buffer (or buffer (current-buffer))
1647 (imap-ok-p (imap-send-command-wait
1648 (concat "UID STORE " articles
1649 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1651 (defun imap-message-flags-add (articles flags &optional silent buffer)
1652 (when (and articles flags)
1653 (with-current-buffer (or buffer (current-buffer))
1654 (imap-ok-p (imap-send-command-wait
1655 (concat "UID STORE " articles
1656 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1658 (defun imap-message-copyuid-1 (mailbox)
1659 (if (imap-capability 'UIDPLUS)
1660 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1661 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1662 (let ((old-mailbox imap-current-mailbox)
1664 (imap-message-data (make-vector 2 0)))
1665 (when (imap-mailbox-examine-1 mailbox)
1667 (and (imap-fetch "*" "UID")
1668 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1669 (apply 'max (imap-message-map
1670 (lambda (uid prop) uid) 'UID))))
1672 (imap-mailbox-select old-mailbox (eq state 'examine))
1673 (imap-mailbox-unselect)))))))
1675 (defun imap-message-copyuid (mailbox &optional buffer)
1676 (with-current-buffer (or buffer (current-buffer))
1677 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1679 (defun imap-message-copy (articles mailbox
1680 &optional dont-create no-copyuid buffer)
1681 "Copy ARTICLES (a string message set) to MAILBOX on server in
1682 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1683 non-nil, it will not create a mailbox. On success, return a list with
1684 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1685 first element, rest of list contain the saved articles' UIDs."
1687 (with-current-buffer (or buffer (current-buffer))
1688 (let ((mailbox (imap-utf7-encode mailbox)))
1689 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1690 (imap-current-target-mailbox mailbox))
1691 (if (imap-ok-p (imap-send-command-wait cmd))
1693 (when (and (not dont-create)
1694 ;; removed because of buggy Oracle server
1695 ;; that doesn't send TRYCREATE tags (which
1696 ;; is a MUST according to specifications):
1697 ;;(imap-mailbox-get-1 'trycreate mailbox)
1698 (imap-mailbox-create-1 mailbox))
1699 (imap-ok-p (imap-send-command-wait cmd)))))
1701 (imap-message-copyuid-1 mailbox)))))))
1703 (defun imap-message-appenduid-1 (mailbox)
1704 (if (imap-capability 'UIDPLUS)
1705 (imap-mailbox-get-1 'appenduid mailbox)
1706 (let ((old-mailbox imap-current-mailbox)
1708 (imap-message-data (make-vector 2 0)))
1709 (when (imap-mailbox-examine-1 mailbox)
1711 (and (imap-fetch "*" "UID")
1712 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1713 (apply 'max (imap-message-map
1714 (lambda (uid prop) uid) 'UID))))
1716 (imap-mailbox-select old-mailbox (eq state 'examine))
1717 (imap-mailbox-unselect)))))))
1719 (defun imap-message-appenduid (mailbox &optional buffer)
1720 (with-current-buffer (or buffer (current-buffer))
1721 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1723 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1724 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1725 FLAGS and DATE-TIME is currently not used. Return a cons holding
1726 uidvalidity of MAILBOX and UID the newly created article got, or nil
1728 (let ((mailbox (imap-utf7-encode mailbox)))
1729 (with-current-buffer (or buffer (current-buffer))
1730 (and (let ((imap-current-target-mailbox mailbox))
1732 (imap-send-command-wait
1733 (list "APPEND \"" mailbox "\" " article))))
1734 (imap-message-appenduid-1 mailbox)))))
1736 (defun imap-body-lines (body)
1737 "Return number of lines in article by looking at the mime bodystructure BODY."
1739 (if (stringp (car body))
1740 (cond ((and (string= (upcase (car body)) "TEXT")
1741 (numberp (nth 7 body)))
1743 ((and (string= (upcase (car body)) "MESSAGE")
1744 (numberp (nth 9 body)))
1747 (apply '+ (mapcar 'imap-body-lines body)))
1750 (defun imap-envelope-from (from)
1751 "Return a from string line."
1753 (concat (aref from 0)
1754 (if (aref from 0) " <")
1758 (if (aref from 0) ">"))))
1761 ;; Internal functions.
1763 (defun imap-add-callback (tag func)
1764 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1766 (defun imap-send-command-1 (cmdstr)
1767 (setq cmdstr (concat cmdstr imap-client-eol))
1769 (with-current-buffer (get-buffer-create imap-log-buffer)
1770 (imap-disable-multibyte)
1771 (buffer-disable-undo)
1772 (goto-char (point-max))
1774 (process-send-string imap-process cmdstr))
1776 (defun imap-send-command (command &optional buffer)
1777 (with-current-buffer (or buffer (current-buffer))
1778 (if (not (listp command)) (setq command (list command)))
1779 (let ((tag (setq imap-tag (1+ imap-tag)))
1781 (setq cmdstr (concat (number-to-string imap-tag) " "))
1782 (while (setq cmd (pop command))
1783 (cond ((stringp cmd)
1784 (setq cmdstr (concat cmdstr cmd)))
1786 (let ((eol imap-client-eol)
1787 (calcfirst imap-calculate-literal-size-first)
1789 (with-current-buffer cmd
1791 (setq size (buffer-size)))
1792 (when (not (equal eol "\r\n"))
1793 ;; XXX modifies buffer!
1794 (goto-char (point-min))
1795 (while (search-forward "\r\n" nil t)
1796 (replace-match eol)))
1798 (setq size (buffer-size))))
1800 (concat cmdstr (format "{%d}" size))))
1803 (imap-send-command-1 cmdstr)
1805 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1806 (setq command nil) ;; abort command if no cont-req
1807 (let ((process imap-process)
1808 (stream imap-stream)
1809 (eol imap-client-eol))
1810 (with-current-buffer cmd
1812 (with-current-buffer (get-buffer-create
1814 (imap-disable-multibyte)
1815 (buffer-disable-undo)
1816 (goto-char (point-max))
1817 (insert-buffer-substring cmd)))
1818 (process-send-region process (point-min)
1820 (process-send-string process imap-client-eol))))
1821 (setq imap-continuation nil)))
1823 (imap-send-command-1 cmdstr)
1826 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1827 (setq command nil) ;; abort command if no cont-req
1828 (setq command (cons (funcall cmd imap-continuation)
1830 (setq imap-continuation nil)))
1832 (error "Unknown command type"))))
1834 (imap-send-command-1 cmdstr))
1837 (defun imap-wait-for-tag (tag &optional buffer)
1838 (with-current-buffer (or buffer (current-buffer))
1839 (let (imap-have-messaged)
1840 (while (and (null imap-continuation)
1841 (memq (process-status imap-process) '(open run))
1842 (< imap-reached-tag tag))
1843 (let ((len (/ (point-max) 1024))
1846 (setq imap-have-messaged t)
1847 (message "imap read: %dk" len))
1848 (accept-process-output imap-process
1849 (truncate imap-read-timeout)
1850 (truncate (* (- imap-read-timeout
1851 (truncate imap-read-timeout))
1853 ;; A process can die _before_ we have processed everything it
1854 ;; has to say. Moreover, this can happen in between the call to
1855 ;; accept-process-output and the call to process-status in an
1856 ;; iteration of the loop above.
1857 (when (and (null imap-continuation)
1858 (< imap-reached-tag tag))
1859 (accept-process-output imap-process 0 0))
1860 (when imap-have-messaged
1862 (and (memq (process-status imap-process) '(open run))
1863 (or (assq tag imap-failed-tags)
1864 (if imap-continuation
1868 (defun imap-sentinel (process string)
1869 (delete-process process))
1871 (defun imap-find-next-line ()
1872 "Return point at end of current line, taking into account literals.
1873 Return nil if no complete line has arrived."
1874 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1877 (if (match-string 1)
1878 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1880 (goto-char (+ (point) (string-to-number (match-string 1))))
1881 (imap-find-next-line))
1884 (defun imap-arrival-filter (proc string)
1885 "IMAP process filter."
1886 ;; Sometimes, we are called even though the process has died.
1887 ;; Better abstain from doing stuff in that case.
1888 (when (buffer-name (process-buffer proc))
1889 (with-current-buffer (process-buffer proc)
1890 (goto-char (point-max))
1893 (with-current-buffer (get-buffer-create imap-log-buffer)
1894 (imap-disable-multibyte)
1895 (buffer-disable-undo)
1896 (goto-char (point-max))
1899 (goto-char (point-min))
1900 (while (setq end (imap-find-next-line))
1902 (narrow-to-region (point-min) end)
1903 (delete-backward-char (length imap-server-eol))
1904 (goto-char (point-min))
1906 (cond ((eq imap-state 'initial)
1907 (imap-parse-greeting))
1908 ((or (eq imap-state 'auth)
1909 (eq imap-state 'nonauth)
1910 (eq imap-state 'selected)
1911 (eq imap-state 'examine))
1912 (imap-parse-response))
1914 (message "Unknown state %s in arrival filter"
1916 (delete-region (point-min) (point-max)))))))))
1921 (defsubst imap-forward ()
1922 (or (eobp) (forward-char)))
1925 ;; ; Unsigned 32-bit integer
1926 ;; ; (0 <= n < 4,294,967,296)
1928 (defsubst imap-parse-number ()
1929 (when (looking-at "[0-9]+")
1931 (string-to-number (match-string 0))
1932 (goto-char (match-end 0)))))
1934 ;; literal = "{" number "}" CRLF *CHAR8
1935 ;; ; Number represents the number of CHAR8s
1937 (defsubst imap-parse-literal ()
1938 (when (looking-at "{\\([0-9]+\\)}\r\n")
1939 (let ((pos (match-end 0))
1940 (len (string-to-number (match-string 1))))
1941 (if (< (point-max) (+ pos len))
1943 (goto-char (+ pos len))
1944 (buffer-substring pos (+ pos len))))))
1946 ;; string = quoted / literal
1948 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1950 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1951 ;; "\" quoted-specials
1953 ;; quoted-specials = DQUOTE / "\"
1955 ;; TEXT-CHAR = <any CHAR except CR and LF>
1957 (defsubst imap-parse-string ()
1958 (cond ((eq (char-after) ?\")
1960 (let ((p (point)) (name ""))
1961 (skip-chars-forward "^\"\\\\")
1962 (setq name (buffer-substring p (point)))
1963 (while (eq (char-after) ?\\)
1964 (setq p (1+ (point)))
1966 (skip-chars-forward "^\"\\\\")
1967 (setq name (concat name (buffer-substring p (point)))))
1970 ((eq (char-after) ?{)
1971 (imap-parse-literal))))
1975 (defsubst imap-parse-nil ()
1976 (if (looking-at "NIL")
1977 (goto-char (match-end 0))))
1979 ;; nstring = string / nil
1981 (defsubst imap-parse-nstring ()
1982 (or (imap-parse-string)
1983 (and (imap-parse-nil)
1986 ;; astring = atom / string
1988 ;; atom = 1*ATOM-CHAR
1990 ;; ATOM-CHAR = <any CHAR except atom-specials>
1992 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1995 ;; list-wildcards = "%" / "*"
1997 ;; quoted-specials = DQUOTE / "\"
1999 (defsubst imap-parse-astring ()
2000 (or (imap-parse-string)
2001 (buffer-substring (point)
2002 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2003 (goto-char (1- (match-end 0)))
2007 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2010 ;; addr-adl = nstring
2011 ;; ; Holds route from [RFC-822] route-addr if
2014 ;; addr-host = nstring
2015 ;; ; nil indicates [RFC-822] group syntax.
2016 ;; ; Otherwise, holds [RFC-822] domain name
2018 ;; addr-mailbox = nstring
2019 ;; ; nil indicates end of [RFC-822] group; if
2020 ;; ; non-nil and addr-host is nil, holds
2021 ;; ; [RFC-822] group name.
2022 ;; ; Otherwise, holds [RFC-822] local-part
2023 ;; ; after removing [RFC-822] quoting
2025 ;; addr-name = nstring
2026 ;; ; If non-nil, holds phrase from [RFC-822]
2027 ;; ; mailbox after removing [RFC-822] quoting
2030 (defsubst imap-parse-address ()
2032 (when (eq (char-after) ?\()
2034 (setq address (vector (prog1 (imap-parse-nstring)
2036 (prog1 (imap-parse-nstring)
2038 (prog1 (imap-parse-nstring)
2040 (imap-parse-nstring)))
2041 (when (eq (char-after) ?\))
2045 ;; address-list = "(" 1*address ")" / nil
2049 (defsubst imap-parse-address-list ()
2050 (if (eq (char-after) ?\()
2051 (let (address addresses)
2053 (while (and (not (eq (char-after) ?\)))
2054 ;; next line for MS Exchange bug
2055 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2056 (setq address (imap-parse-address)))
2057 (setq addresses (cons address addresses)))
2058 (when (eq (char-after) ?\))
2060 (nreverse addresses)))
2061 ;; With assert, the code might not be eval'd.
2062 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2065 ;; mailbox = "INBOX" / astring
2066 ;; ; INBOX is case-insensitive. All case variants of
2067 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2068 ;; ; not as an astring. An astring which consists of
2069 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2070 ;; ; is considered to be INBOX and not an astring.
2071 ;; ; Refer to section 5.1 for further
2072 ;; ; semantic details of mailbox names.
2074 (defsubst imap-parse-mailbox ()
2075 (let ((mailbox (imap-parse-astring)))
2076 (if (string-equal "INBOX" (upcase mailbox))
2080 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2082 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2083 ;; ; Authentication condition
2085 ;; resp-cond-bye = "BYE" SP resp-text
2087 (defun imap-parse-greeting ()
2088 "Parse a IMAP greeting."
2089 (cond ((looking-at "\\* OK ")
2090 (setq imap-state 'nonauth))
2091 ((looking-at "\\* PREAUTH ")
2092 (setq imap-state 'auth))
2093 ((looking-at "\\* BYE ")
2094 (setq imap-state 'closed))))
2096 ;; response = *(continue-req / response-data) response-done
2098 ;; continue-req = "+" SP (resp-text / base64) CRLF
2100 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2101 ;; mailbox-data / message-data / capability-data) CRLF
2103 ;; response-done = response-tagged / response-fatal
2105 ;; response-fatal = "*" SP resp-cond-bye CRLF
2106 ;; ; Server closes connection immediately
2108 ;; response-tagged = tag SP resp-cond-state CRLF
2110 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2111 ;; ; Status condition
2113 ;; resp-cond-bye = "BYE" SP resp-text
2115 ;; mailbox-data = "FLAGS" SP flag-list /
2116 ;; "LIST" SP mailbox-list /
2117 ;; "LSUB" SP mailbox-list /
2118 ;; "SEARCH" *(SP nz-number) /
2119 ;; "STATUS" SP mailbox SP "("
2120 ;; [status-att SP number *(SP status-att SP number)] ")" /
2121 ;; number SP "EXISTS" /
2122 ;; number SP "RECENT"
2124 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2126 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2128 ;; ; IMAP4rev1 servers which offer RFC 1730
2129 ;; ; compatibility MUST list "IMAP4" as the first
2132 (defun imap-parse-response ()
2133 "Parse a IMAP command response."
2135 (case (setq token (read (current-buffer)))
2136 (+ (setq imap-continuation
2137 (or (buffer-substring (min (point-max) (1+ (point)))
2140 (* (case (prog1 (setq token (read (current-buffer)))
2142 (OK (imap-parse-resp-text))
2143 (NO (imap-parse-resp-text))
2144 (BAD (imap-parse-resp-text))
2145 (BYE (imap-parse-resp-text))
2146 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2147 (LIST (imap-parse-data-list 'list))
2148 (LSUB (imap-parse-data-list 'lsub))
2149 (SEARCH (imap-mailbox-put
2151 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2152 (STATUS (imap-parse-status))
2153 (CAPABILITY (setq imap-capability
2154 (read (concat "(" (upcase (buffer-substring
2155 (point) (point-max)))
2157 (ID (setq imap-id (read (buffer-substring (point)
2159 (ACL (imap-parse-acl))
2160 (t (case (prog1 (read (current-buffer))
2162 (EXISTS (imap-mailbox-put 'exists token))
2163 (RECENT (imap-mailbox-put 'recent token))
2165 (FETCH (imap-parse-fetch token))
2166 (t (message "Garbage: %s" (buffer-string)))))))
2168 (if (not (integerp token))
2169 (message "Garbage: %s" (buffer-string))
2170 (case (prog1 (setq status (read (current-buffer)))
2173 (setq imap-reached-tag (max imap-reached-tag token))
2174 (imap-parse-resp-text)))
2176 (setq imap-reached-tag (max imap-reached-tag token))
2178 (imap-parse-resp-text))
2180 (when (eq (char-after) ?\[)
2181 (setq code (buffer-substring (point)
2182 (search-forward "]")))
2184 (setq text (buffer-substring (point) (point-max)))
2185 (push (list token status code text)
2186 imap-failed-tags))))
2188 (setq imap-reached-tag (max imap-reached-tag token))
2190 (imap-parse-resp-text))
2192 (when (eq (char-after) ?\[)
2193 (setq code (buffer-substring (point)
2194 (search-forward "]")))
2196 (setq text (buffer-substring (point) (point-max)))
2197 (push (list token status code text) imap-failed-tags)
2198 (error "Internal error, tag %s status %s code %s text %s"
2199 token status code text))))
2200 (t (message "Garbage: %s" (buffer-string))))
2201 (when (assq token imap-callbacks)
2202 (funcall (cdr (assq token imap-callbacks)) token status)
2203 (setq imap-callbacks
2204 (imap-remassoc token imap-callbacks)))))))))
2206 ;; resp-text = ["[" resp-text-code "]" SP] text
2208 ;; text = 1*TEXT-CHAR
2210 ;; TEXT-CHAR = <any CHAR except CR and LF>
2212 (defun imap-parse-resp-text ()
2213 (imap-parse-resp-text-code))
2215 ;; resp-text-code = "ALERT" /
2216 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2217 ;; "NEWNAME" SP string SP string /
2219 ;; "PERMANENTFLAGS" SP "("
2220 ;; [flag-perm *(SP flag-perm)] ")" /
2224 ;; "UIDNEXT" SP nz-number /
2225 ;; "UIDVALIDITY" SP nz-number /
2226 ;; "UNSEEN" SP nz-number /
2227 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2229 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2231 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2233 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2235 ;; ; Identifies a set of messages. For message
2236 ;; ; sequence numbers, these are consecutive
2237 ;; ; numbers from 1 to the number of messages in
2239 ;; ; Comma delimits individual numbers, colon
2240 ;; ; delimits between two numbers inclusive.
2241 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2242 ;; ; 14,15 for a mailbox with 15 messages.
2244 ;; sequence-num = nz-number / "*"
2245 ;; ; * is the largest number in use. For message
2246 ;; ; sequence numbers, it is the number of messages
2247 ;; ; in the mailbox. For unique identifiers, it is
2248 ;; ; the unique identifier of the last message in
2251 ;; flag-perm = flag / "\*"
2253 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2254 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2255 ;; ; Does not include "\Recent"
2257 ;; flag-extension = "\" atom
2258 ;; ; Future expansion. Client implementations
2259 ;; ; MUST accept flag-extension flags. Server
2260 ;; ; implementations MUST NOT generate
2261 ;; ; flag-extension flags except as defined by
2262 ;; ; future standard or standards-track
2263 ;; ; revisions of this specification.
2265 ;; flag-keyword = atom
2267 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2269 (defun imap-parse-resp-text-code ()
2270 ;; xxx next line for stalker communigate pro 3.3.1 bug
2271 (when (looking-at " \\[")
2273 (when (eq (char-after) ?\[)
2275 (cond ((search-forward "PERMANENTFLAGS " nil t)
2276 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2277 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2278 (imap-mailbox-put 'uidnext (match-string 1)))
2279 ((search-forward "UNSEEN " nil t)
2280 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2281 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2282 (imap-mailbox-put 'uidvalidity (match-string 1)))
2283 ((search-forward "READ-ONLY" nil t)
2284 (imap-mailbox-put 'read-only t))
2285 ((search-forward "NEWNAME " nil t)
2286 (let (oldname newname)
2287 (setq oldname (imap-parse-string))
2289 (setq newname (imap-parse-string))
2290 (imap-mailbox-put 'newname newname oldname)))
2291 ((search-forward "TRYCREATE" nil t)
2292 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2293 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2294 (imap-mailbox-put 'appenduid
2295 (list (match-string 1)
2296 (string-to-number (match-string 2)))
2297 imap-current-target-mailbox))
2298 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2299 (imap-mailbox-put 'copyuid (list (match-string 1)
2302 imap-current-target-mailbox))
2303 ((search-forward "ALERT] " nil t)
2304 (message "Imap server %s information: %s" imap-server
2305 (buffer-substring (point) (point-max)))))))
2307 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2308 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2310 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2311 ;; *(SP mbx-list-oflag) /
2312 ;; mbx-list-oflag *(SP mbx-list-oflag)
2314 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2315 ;; ; Other flags; multiple possible per LIST response
2317 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2318 ;; ; Selectability flags; only one per LIST response
2320 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2321 ;; "\" quoted-specials
2323 ;; quoted-specials = DQUOTE / "\"
2325 (defun imap-parse-data-list (type)
2326 (let (flags delimiter mailbox)
2327 (setq flags (imap-parse-flag-list))
2328 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2329 (setq delimiter (match-string 1))
2330 (goto-char (1+ (match-end 0)))
2331 (when (setq mailbox (imap-parse-mailbox))
2332 (imap-mailbox-put type t mailbox)
2333 (imap-mailbox-put 'list-flags flags mailbox)
2334 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2336 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2337 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2338 ;; "INTERNALDATE" SPACE date_time /
2339 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2340 ;; "RFC822.SIZE" SPACE number /
2341 ;; "BODY" ["STRUCTURE"] SPACE body /
2342 ;; "BODY" section ["<" number ">"] SPACE nstring /
2343 ;; "UID" SPACE uniqueid) ")"
2345 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2346 ;; SPACE time SPACE zone <">
2348 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2349 ;; ["." (section_text / "MIME")])] "]"
2351 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2352 ;; SPACE header_list / "TEXT"
2354 ;; header_fld_name ::= astring
2356 ;; header_list ::= "(" 1#header_fld_name ")"
2358 (defsubst imap-parse-header-list ()
2359 (when (eq (char-after) ?\()
2361 (while (not (eq (char-after) ?\)))
2363 (push (imap-parse-astring) strlist))
2365 (nreverse strlist))))
2367 (defsubst imap-parse-fetch-body-section ()
2369 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2370 (if (eq (char-before) ? )
2372 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2373 (search-forward "]" nil t))
2376 (defun imap-parse-fetch (response)
2377 (when (eq (char-after) ?\()
2378 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2379 rfc822size body bodydetail bodystructure flags-empty)
2380 (while (not (eq (char-after) ?\)))
2382 (let ((token (read (current-buffer))))
2384 (cond ((eq token 'UID)
2385 (setq uid (condition-case ()
2386 (read (current-buffer))
2389 (setq flags (imap-parse-flag-list))
2391 (setq flags-empty 't)))
2392 ((eq token 'ENVELOPE)
2393 (setq envelope (imap-parse-envelope)))
2394 ((eq token 'INTERNALDATE)
2395 (setq internaldate (imap-parse-string)))
2397 (setq rfc822 (imap-parse-nstring)))
2398 ((eq token 'RFC822.HEADER)
2399 (setq rfc822header (imap-parse-nstring)))
2400 ((eq token 'RFC822.TEXT)
2401 (setq rfc822text (imap-parse-nstring)))
2402 ((eq token 'RFC822.SIZE)
2403 (setq rfc822size (read (current-buffer))))
2405 (if (eq (char-before) ?\[)
2407 (upcase (imap-parse-fetch-body-section))
2408 (and (eq (char-after) ?<)
2409 (buffer-substring (1+ (point))
2410 (search-forward ">" nil t)))
2411 (progn (imap-forward)
2412 (imap-parse-nstring)))
2414 (setq body (imap-parse-body))))
2415 ((eq token 'BODYSTRUCTURE)
2416 (setq bodystructure (imap-parse-body))))))
2418 (setq imap-current-message uid)
2419 (imap-message-put uid 'UID uid)
2420 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2421 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2422 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2423 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2424 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2425 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2426 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2427 (and body (imap-message-put uid 'BODY body))
2428 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2429 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2430 (run-hooks 'imap-fetch-data-hook)))))
2432 ;; mailbox-data = ...
2433 ;; "STATUS" SP mailbox SP "("
2434 ;; [status-att SP number
2435 ;; *(SP status-att SP number)] ")"
2438 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2441 (defun imap-parse-status ()
2442 (let ((mailbox (imap-parse-mailbox)))
2443 (if (eq (char-after) ? )
2445 (when (and mailbox (eq (char-after) ?\())
2446 (while (and (not (eq (char-after) ?\)))
2447 (or (forward-char) t)
2448 (looking-at "\\([A-Za-z]+\\) "))
2449 (let ((token (match-string 1)))
2450 (goto-char (match-end 0))
2451 (cond ((string= token "MESSAGES")
2452 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2453 ((string= token "RECENT")
2454 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2455 ((string= token "UIDNEXT")
2456 (and (looking-at "[0-9]+")
2457 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2458 (goto-char (match-end 0))))
2459 ((string= token "UIDVALIDITY")
2460 (and (looking-at "[0-9]+")
2461 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2462 (goto-char (match-end 0))))
2463 ((string= token "UNSEEN")
2464 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2466 (message "Unknown status data %s in mailbox %s ignored"
2468 (read (current-buffer)))))))))
2470 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2473 ;; identifier ::= astring
2475 ;; rights ::= astring
2477 (defun imap-parse-acl ()
2478 (let ((mailbox (imap-parse-mailbox))
2479 identifier rights acl)
2480 (while (eq (char-after) ?\ )
2482 (setq identifier (imap-parse-astring))
2484 (setq rights (imap-parse-astring))
2485 (setq acl (append acl (list (cons identifier rights)))))
2486 (imap-mailbox-put 'acl acl mailbox)))
2488 ;; flag-list = "(" [flag *(SP flag)] ")"
2490 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2491 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2492 ;; ; Does not include "\Recent"
2494 ;; flag-keyword = atom
2496 ;; flag-extension = "\" atom
2497 ;; ; Future expansion. Client implementations
2498 ;; ; MUST accept flag-extension flags. Server
2499 ;; ; implementations MUST NOT generate
2500 ;; ; flag-extension flags except as defined by
2501 ;; ; future standard or standards-track
2502 ;; ; revisions of this specification.
2504 (defun imap-parse-flag-list ()
2505 (let (flag-list start)
2506 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2507 (while (and (not (eq (char-after) ?\)))
2510 ;; next line for Courier IMAP bug.
2511 (skip-chars-forward " ")
2513 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2514 (push (buffer-substring start (point)) flag-list))
2515 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2517 (nreverse flag-list)))
2519 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2520 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2521 ;; env-in-reply-to SP env-message-id ")"
2523 ;; env-bcc = "(" 1*address ")" / nil
2525 ;; env-cc = "(" 1*address ")" / nil
2527 ;; env-date = nstring
2529 ;; env-from = "(" 1*address ")" / nil
2531 ;; env-in-reply-to = nstring
2533 ;; env-message-id = nstring
2535 ;; env-reply-to = "(" 1*address ")" / nil
2537 ;; env-sender = "(" 1*address ")" / nil
2539 ;; env-subject = nstring
2541 ;; env-to = "(" 1*address ")" / nil
2543 (defun imap-parse-envelope ()
2544 (when (eq (char-after) ?\()
2546 (vector (prog1 (imap-parse-nstring) ;; date
2548 (prog1 (imap-parse-nstring) ;; subject
2550 (prog1 (imap-parse-address-list) ;; from
2552 (prog1 (imap-parse-address-list) ;; sender
2554 (prog1 (imap-parse-address-list) ;; reply-to
2556 (prog1 (imap-parse-address-list) ;; to
2558 (prog1 (imap-parse-address-list) ;; cc
2560 (prog1 (imap-parse-address-list) ;; bcc
2562 (prog1 (imap-parse-nstring) ;; in-reply-to
2564 (prog1 (imap-parse-nstring) ;; message-id
2567 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2569 (defsubst imap-parse-string-list ()
2570 (cond ((eq (char-after) ?\() ;; body-fld-param
2573 (while (setq str (imap-parse-string))
2575 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2576 ;; between body-fld-param's sometimes
2577 (or (eq (char-after) ?\")
2579 (nreverse strlist)))
2583 ;; body-extension = nstring / number /
2584 ;; "(" body-extension *(SP body-extension) ")"
2585 ;; ; Future expansion. Client implementations
2586 ;; ; MUST accept body-extension fields. Server
2587 ;; ; implementations MUST NOT generate
2588 ;; ; body-extension fields except as defined by
2589 ;; ; future standard or standards-track
2590 ;; ; revisions of this specification.
2592 (defun imap-parse-body-extension ()
2593 (if (eq (char-after) ?\()
2596 (push (imap-parse-body-extension) b-e)
2597 (while (eq (char-after) ?\ )
2599 (push (imap-parse-body-extension) b-e))
2600 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2603 (or (imap-parse-number)
2604 (imap-parse-nstring))))
2606 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2607 ;; *(SP body-extension)]]
2608 ;; ; MUST NOT be returned on non-extensible
2611 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2612 ;; *(SP body-extension)]]
2613 ;; ; MUST NOT be returned on non-extensible
2616 (defsubst imap-parse-body-ext ()
2618 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2621 (if (eq (char-after) ?\()
2624 (push (imap-parse-string) dsp)
2626 (push (imap-parse-string-list) dsp)
2628 ;; With assert, the code might not be eval'd.
2629 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2631 (push (nreverse dsp) ext))
2632 (when (eq (char-after) ?\ ) ;; body-fld-lang
2634 (if (eq (char-after) ?\()
2635 (push (imap-parse-string-list) ext)
2636 (push (imap-parse-nstring) ext))
2637 (while (eq (char-after) ?\ ) ;; body-extension
2639 (setq ext (append (imap-parse-body-extension) ext)))))
2642 ;; body = "(" body-type-1part / body-type-mpart ")"
2644 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2645 ;; *(SP body-extension)]]
2646 ;; ; MUST NOT be returned on non-extensible
2649 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2650 ;; *(SP body-extension)]]
2651 ;; ; MUST NOT be returned on non-extensible
2654 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2655 ;; body-fld-enc SP body-fld-octets
2657 ;; body-fld-desc = nstring
2659 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2661 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2662 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2664 ;; body-fld-id = nstring
2666 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2668 ;; body-fld-lines = number
2670 ;; body-fld-md5 = nstring
2672 ;; body-fld-octets = number
2674 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2676 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2677 ;; [SP body-ext-1part]
2679 ;; body-type-basic = media-basic SP body-fields
2680 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2682 ;; body-type-msg = media-message SP body-fields SP envelope
2683 ;; SP body SP body-fld-lines
2685 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2687 ;; body-type-mpart = 1*body SP media-subtype
2688 ;; [SP body-ext-mpart]
2690 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2691 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2692 ;; ; Defined in [MIME-IMT]
2694 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2695 ;; ; Defined in [MIME-IMT]
2697 ;; media-subtype = string
2698 ;; ; Defined in [MIME-IMT]
2700 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2701 ;; ; Defined in [MIME-IMT]
2703 (defun imap-parse-body ()
2705 (when (eq (char-after) ?\()
2707 (if (eq (char-after) ?\()
2709 (while (and (eq (char-after) ?\()
2710 (setq subbody (imap-parse-body)))
2711 ;; buggy stalker communigate pro 3.0 insert a SPC between
2712 ;; parts in multiparts
2713 (when (and (eq (char-after) ?\ )
2714 (eq (char-after (1+ (point))) ?\())
2716 (push subbody body))
2718 (push (imap-parse-string) body) ;; media-subtype
2719 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2721 (if (eq (char-after) ?\() ;; body-fld-param
2722 (push (imap-parse-string-list) body)
2723 (push (and (imap-parse-nil) nil) body))
2725 (append (imap-parse-body-ext) body))) ;; body-ext-...
2726 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2730 (push (imap-parse-string) body) ;; media-type
2732 (push (imap-parse-string) body) ;; media-subtype
2734 ;; next line for Sun SIMS bug
2735 (and (eq (char-after) ? ) (imap-forward))
2736 (if (eq (char-after) ?\() ;; body-fld-param
2737 (push (imap-parse-string-list) body)
2738 (push (and (imap-parse-nil) nil) body))
2740 (push (imap-parse-nstring) body) ;; body-fld-id
2742 (push (imap-parse-nstring) body) ;; body-fld-desc
2744 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2745 ;; nstring and return nil instead of defaulting back to 7BIT
2746 ;; as the standard says.
2747 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2749 (push (imap-parse-number) body) ;; body-fld-octets
2751 ;; ok, we're done parsing the required parts, what comes now is one
2754 ;; envelope (then we're parsing body-type-msg)
2755 ;; body-fld-lines (then we're parsing body-type-text)
2756 ;; body-ext-1part (then we're parsing body-type-basic)
2758 ;; the problem is that the two first are in turn optionally followed
2759 ;; by the third. So we parse the first two here (if there are any)...
2761 (when (eq (char-after) ?\ )
2764 (cond ((eq (char-after) ?\() ;; body-type-msg:
2765 (push (imap-parse-envelope) body) ;; envelope
2767 (push (imap-parse-body) body) ;; body
2768 ;; buggy stalker communigate pro 3.0 doesn't print
2769 ;; number of lines in message/rfc822 attachment
2770 (if (eq (char-after) ?\))
2773 (push (imap-parse-number) body))) ;; body-fld-lines
2774 ((setq lines (imap-parse-number)) ;; body-type-text:
2775 (push lines body)) ;; body-fld-lines
2777 (backward-char))))) ;; no match...
2779 ;; ...and then parse the third one here...
2781 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2783 (push (imap-parse-nstring) body) ;; body-fld-md5
2784 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2786 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2790 (when imap-debug ; (untrace-all)
2792 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2793 (mapcar (lambda (f) (trace-function-background f imap-debug-buffer))
2804 imap-interactive-login
2820 imap-send-command-wait
2825 imap-current-mailbox
2826 imap-current-mailbox-p-1
2827 imap-current-mailbox-p
2828 imap-mailbox-select-1
2830 imap-mailbox-examine-1
2831 imap-mailbox-examine
2832 imap-mailbox-unselect
2833 imap-mailbox-expunge
2835 imap-mailbox-create-1
2841 imap-mailbox-subscribe
2842 imap-mailbox-unsubscribe
2844 imap-mailbox-acl-get
2845 imap-mailbox-acl-set
2846 imap-mailbox-acl-delete
2847 imap-current-message
2848 imap-list-to-message-set
2855 imap-message-flag-permanent-p
2856 imap-message-flags-set
2857 imap-message-flags-del
2858 imap-message-flags-add
2859 imap-message-copyuid-1
2860 imap-message-copyuid
2862 imap-message-appenduid-1
2863 imap-message-appenduid
2875 imap-parse-resp-text
2876 imap-parse-resp-text-code
2877 imap-parse-data-list
2881 imap-parse-flag-list
2883 imap-parse-body-extension
2889 ;;; imap.el ends here