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."
229 (defcustom imap-use-utf7 t
230 "If non-nil, do utf7 encoding/decoding of mailbox names.
231 Since the UTF7 decoding currently only decodes into ISO-8859-1
232 characters, you may disable this decoding if you need to access UTF7
233 encoded mailboxes which doesn't translate into ISO-8859-1."
237 (defcustom imap-log nil
238 "If non-nil, a imap session trace is placed in *imap-log* buffer."
242 (defcustom imap-debug nil
243 "If non-nil, random debug spews are placed in *imap-debug* buffer."
247 (defcustom imap-shell-host "gateway"
248 "Hostname of rlogin proxy."
252 (defcustom imap-default-user (user-login-name)
253 "Default username to use."
257 (defcustom imap-read-timeout (if (string-match
258 "windows-nt\\|os/2\\|emx\\|cygwin"
259 (symbol-name system-type))
262 "*How long to wait between checking for the end of output.
263 Shorter values mean quicker response, but is more CPU intensive."
267 (defcustom imap-store-password nil
268 "If non-nil, store session password without promting."
272 ;; Various variables.
274 (defvar imap-fetch-data-hook nil
275 "Hooks called after receiving each FETCH response.")
277 (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell)
278 "Priority of streams to consider when opening connection to server.")
280 (defvar imap-stream-alist
281 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
282 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
283 (tls imap-tls-p imap-tls-open)
284 (ssl imap-ssl-p imap-ssl-open)
285 (network imap-network-p imap-network-open)
286 (shell imap-shell-p imap-shell-open)
287 (starttls imap-starttls-p imap-starttls-open))
288 "Definition of network streams.
292 NAME names the stream, CHECK is a function returning non-nil if the
293 server support the stream and OPEN is a function for opening the
296 (defvar imap-authenticators '(gssapi
303 "Priority of authenticators to consider when authenticating to server.")
305 (defvar imap-authenticator-alist
306 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
307 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
308 (sasl imap-sasl-auth-p imap-sasl-auth)
309 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
310 (login imap-login-p imap-login-auth)
311 (anonymous imap-anonymous-p imap-anonymous-auth)
312 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
313 "Definition of authenticators.
315 \(NAME CHECK AUTHENTICATE)
317 NAME names the authenticator. CHECK is a function returning non-nil if
318 the server support the authenticator and AUTHENTICATE is a function
319 for doing the actual authentication.")
321 (defvar imap-error nil
322 "Error codes from the last command.")
324 ;; Internal constants. Change theese and die.
326 (defconst imap-default-port 143)
327 (defconst imap-default-ssl-port 993)
328 (defconst imap-default-tls-port 993)
329 (defconst imap-default-stream 'network)
330 (defconst imap-local-variables '(imap-server
339 imap-current-target-mailbox
349 imap-calculate-literal-size-first
351 (defconst imap-log-buffer "*imap-log*")
352 (defconst imap-debug-buffer "*imap-debug*")
354 ;; Internal variables.
356 (defvar imap-stream nil)
357 (defvar imap-auth nil)
358 (defvar imap-server nil)
359 (defvar imap-port nil)
360 (defvar imap-username nil)
361 (defvar imap-password nil)
362 (defvar imap-calculate-literal-size-first nil)
363 (defvar imap-state 'closed
365 Valid states are `closed', `initial', `nonauth', `auth', `selected'
368 (defvar imap-server-eol "\r\n"
369 "The EOL string sent from the server.")
371 (defvar imap-client-eol "\r\n"
372 "The EOL string we send to the server.")
374 (defvar imap-current-mailbox nil
375 "Current mailbox name.")
377 (defvar imap-current-target-mailbox nil
378 "Current target mailbox for COPY and APPEND commands.")
380 (defvar imap-mailbox-data nil
381 "Obarray with mailbox data.")
383 (defvar imap-mailbox-prime 997
384 "Length of imap-mailbox-data.")
386 (defvar imap-current-message nil
387 "Current message number.")
389 (defvar imap-message-data nil
390 "Obarray with message data.")
392 (defvar imap-message-prime 997
393 "Length of imap-message-data.")
395 (defvar imap-capability nil
396 "Capability for server.")
402 (defvar imap-namespace nil
403 "Namespace for current server.")
405 (defvar imap-reached-tag 0
406 "Lower limit on command tags that have been parsed.")
408 (defvar imap-failed-tags nil
409 "Alist of tags that failed.
410 Each element is a list with four elements; tag (a integer), response
411 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
412 human readable response text (a string).")
415 "Command tag number.")
417 (defvar imap-process nil
420 (defvar imap-continuation nil
421 "Non-nil indicates that the server emitted a continuation request.
422 The actual value is really the text on the continuation line.")
424 (defvar imap-callbacks nil
425 "List of response tags and callbacks, on the form `(number . function)'.
426 The function should take two arguments, the first the IMAP tag and the
427 second the status (OK, NO, BAD etc) of the command.")
430 ;; Utility functions:
432 (defun imap-remassoc (key alist)
433 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
434 The modified LIST is returned. If the first member
435 of LIST has a car that is `equal' to KEY, there is no way to remove it
436 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
437 sure of changing the value of `foo'."
439 (if (equal key (caar alist))
441 (setcdr alist (imap-remassoc key (cdr alist)))
444 (defmacro imap-disable-multibyte ()
445 "Enable multibyte in the current buffer."
446 '(set-buffer-multibyte nil))
448 (defsubst imap-utf7-encode (string)
452 (utf7-encode string t)
454 "imap: Could not UTF7 encode `%s', using it unencoded..."
459 (defsubst imap-utf7-decode (string)
463 (utf7-decode string t)
465 "imap: Could not UTF7 decode `%s', using it undecoded..."
470 (defsubst imap-ok-p (status)
473 (setq imap-error status)
476 (defun imap-error-text (&optional buffer)
477 (with-current-buffer (or buffer (current-buffer))
478 (nth 3 (car imap-failed-tags))))
481 ;; Server functions; stream stuff:
483 (defun imap-kerberos4-stream-p (buffer)
484 (imap-capability 'AUTH=KERBEROS_V4 buffer))
486 (defun imap-kerberos4-open (name buffer server port)
487 (let ((cmds imap-kerberos4-program)
489 (while (and (not done) (setq cmd (pop cmds)))
490 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
492 (let* ((port (or port imap-default-port))
493 (process-connection-type imap-process-connection-type)
494 (process (as-binary-process
496 name buffer shell-file-name shell-command-switch
501 ?p (number-to-string port)
502 ?l imap-default-user)))))
505 (with-current-buffer buffer
506 (setq imap-client-eol "\n"
507 imap-calculate-literal-size-first t)
508 (while (and (memq (process-status process) '(open run))
509 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
510 (goto-char (point-min))
511 ;; Athena IMTEST can output SSL verify errors
512 (or (while (looking-at "^verify error:num=")
515 (or (while (looking-at "^TLS connection established")
518 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
519 (or (while (looking-at "^C:")
522 ;; cyrus 1.6 imtest print "S: " before server greeting
523 (or (not (looking-at "S: "))
526 (not (and (imap-parse-greeting)
527 ;; success in imtest < 1.6:
528 (or (re-search-forward
529 "^__\\(.*\\)__\n" nil t)
530 ;; success in imtest 1.6:
532 "^\\(Authenticat.*\\)" nil t))
533 (setq response (match-string 1)))))
534 (accept-process-output process 1)
537 (with-current-buffer (get-buffer-create imap-log-buffer)
538 (imap-disable-multibyte)
539 (buffer-disable-undo)
540 (goto-char (point-max))
541 (insert-buffer-substring buffer)))
543 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
544 (if response (concat "done, " response) "failed"))
545 (if (and response (let ((case-fold-search nil))
546 (not (string-match "failed" response))))
548 (if (memq (process-status process) '(open run))
549 (imap-send-command "LOGOUT"))
550 (delete-process process)
554 (defun imap-gssapi-stream-p (buffer)
555 (imap-capability 'AUTH=GSSAPI buffer))
557 (defun imap-gssapi-open (name buffer server port)
558 (let ((cmds imap-gssapi-program)
560 (while (and (not done) (setq cmd (pop cmds)))
561 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
563 (let* ((port (or port imap-default-port))
564 (process-connection-type imap-process-connection-type)
565 (process (as-binary-process
567 name buffer shell-file-name shell-command-switch
572 ?p (number-to-string port)
573 ?l imap-default-user)))))
576 (with-current-buffer buffer
577 (setq imap-client-eol "\n"
578 imap-calculate-literal-size-first t)
579 (while (and (memq (process-status process) '(open run))
580 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
581 (goto-char (point-min))
582 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
583 (or (while (looking-at "^C:")
586 ;; cyrus 1.6 imtest print "S: " before server greeting
587 (or (not (looking-at "S: "))
590 (not (and (imap-parse-greeting)
591 ;; success in imtest 1.6:
593 (concat "^\\(\\(Authenticat.*\\)\\|\\("
594 "Client authentication "
597 (setq response (match-string 1)))))
598 (accept-process-output process 1)
601 (with-current-buffer (get-buffer-create imap-log-buffer)
602 (imap-disable-multibyte)
603 (buffer-disable-undo)
604 (goto-char (point-max))
605 (insert-buffer-substring buffer)))
607 (message "GSSAPI IMAP connection: %s" (or response "failed"))
608 (if (and response (let ((case-fold-search nil))
609 (not (string-match "failed" response))))
611 (if (memq (process-status process) '(open run))
612 (imap-send-command "LOGOUT"))
613 (delete-process process)
617 (defun imap-ssl-p (buffer)
620 (defun imap-ssl-open (name buffer server port)
621 "Open a SSL connection to server."
622 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
623 (list imap-ssl-program)))
625 (while (and (not done) (setq cmd (pop cmds)))
626 (message "imap: Opening SSL connection with `%s'..." cmd)
628 (let ((port (or port imap-default-ssl-port))
629 (process-connection-type imap-process-connection-type)
632 (setq process (as-binary-process
634 name buffer shell-file-name
639 ?p (number-to-string port))))))
640 (process-kill-without-query process))
641 (with-current-buffer buffer
642 (goto-char (point-min))
643 (while (and (memq (process-status process) '(open run))
644 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
645 (goto-char (point-max))
647 (not (imap-parse-greeting)))
648 (accept-process-output process 1)
651 (with-current-buffer (get-buffer-create imap-log-buffer)
652 (imap-disable-multibyte)
653 (buffer-disable-undo)
654 (goto-char (point-max))
655 (insert-buffer-substring buffer)))
657 (when (memq (process-status process) '(open run))
658 (setq done process))))))
661 (message "imap: Opening SSL connection with `%s'...done" cmd)
663 (message "imap: Opening SSL connection with `%s'...failed" cmd)
666 (defun imap-tls-p (buffer)
669 (defun imap-tls-open (name buffer server port)
670 (let* ((port (or port imap-default-tls-port))
671 (process (open-tls-stream name buffer server port)))
673 (while (and (memq (process-status process) '(open run))
674 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
675 (goto-char (point-max))
677 (not (imap-parse-greeting)))
678 (accept-process-output process 1)
681 (with-current-buffer (get-buffer-create imap-log-buffer)
682 (imap-disable-multibyte)
683 (buffer-disable-undo)
684 (goto-char (point-max))
685 (insert-buffer-substring buffer)))
686 (when (memq (process-status process) '(open run))
689 (defun imap-network-p (buffer)
692 (defun imap-network-open (name buffer server port)
693 (let* ((port (or port imap-default-port))
694 (process (open-network-stream-as-binary name buffer server port)))
696 (while (and (memq (process-status process) '(open run))
697 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
698 (goto-char (point-min))
699 (not (imap-parse-greeting)))
700 (accept-process-output process 1)
703 (with-current-buffer (get-buffer-create imap-log-buffer)
704 (imap-disable-multibyte)
705 (buffer-disable-undo)
706 (goto-char (point-max))
707 (insert-buffer-substring buffer)))
708 (when (memq (process-status process) '(open run))
711 (defun imap-shell-p (buffer)
714 (defun imap-shell-open (name buffer server port)
715 (let ((cmds (if (listp imap-shell-program) imap-shell-program
716 (list imap-shell-program)))
718 (while (and (not done) (setq cmd (pop cmds)))
719 (message "imap: Opening IMAP connection with `%s'..." cmd)
720 (setq imap-client-eol "\n")
721 (let* ((port (or port imap-default-port))
722 (process (as-binary-process
724 name buffer shell-file-name shell-command-switch
730 ?p (number-to-string port)
731 ?l imap-default-user))))))
733 (while (and (memq (process-status process) '(open run))
734 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
735 (goto-char (point-max))
737 (not (imap-parse-greeting)))
738 (accept-process-output process 1)
741 (with-current-buffer (get-buffer-create imap-log-buffer)
742 (imap-disable-multibyte)
743 (buffer-disable-undo)
744 (goto-char (point-max))
745 (insert-buffer-substring buffer)))
747 (when (memq (process-status process) '(open run))
748 (setq done process)))))
751 (message "imap: Opening IMAP connection with `%s'...done" cmd)
753 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
756 (defun imap-starttls-p (buffer)
757 (imap-capability 'STARTTLS buffer))
759 (defun imap-starttls-open (name buffer server port)
760 (let* ((port (or port imap-default-port))
761 (process (as-binary-process
762 (starttls-open-stream name buffer server port)))
764 (message "imap: Connecting with STARTTLS...")
766 (while (and (memq (process-status process) '(open run))
767 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
768 (goto-char (point-max))
770 (not (imap-parse-greeting)))
771 (accept-process-output process 1)
773 (imap-send-command "STARTTLS")
774 (while (and (memq (process-status process) '(open run))
775 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
776 (goto-char (point-max))
778 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
779 (accept-process-output process 1)
782 (with-current-buffer (get-buffer-create imap-log-buffer)
783 (buffer-disable-undo)
784 (goto-char (point-max))
785 (insert-buffer-substring buffer)))
786 (when (and (setq tls-info (starttls-negotiate process))
787 (memq (process-status process) '(open run)))
788 (setq done process)))
789 (if (stringp tls-info)
790 (message "imap: STARTTLS info: %s" tls-info))
791 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
794 ;; Server functions; authenticator stuff:
796 (defun imap-interactive-login (buffer loginfunc)
797 "Login to server in BUFFER.
798 LOGINFUNC is passed a username and a password, it should return t if
799 it where successful authenticating itself to the server, nil otherwise.
800 Returns t if login was successful, nil otherwise."
801 (with-current-buffer buffer
802 (make-local-variable 'imap-username)
803 (make-local-variable 'imap-password)
804 (let (user passwd ret)
805 ;; (condition-case ()
806 (while (or (not user) (not passwd))
807 (setq user (or imap-username
808 (read-from-minibuffer
809 (concat "IMAP username for " imap-server
810 " (using stream `" (symbol-name imap-stream)
812 (or user imap-default-user))))
813 (setq passwd (or imap-password
815 (concat "IMAP password for " user "@"
816 imap-server " (using authenticator `"
817 (symbol-name imap-auth) "'): "))))
818 (when (and user passwd)
819 (if (funcall loginfunc user passwd)
823 (when (and (not imap-password)
824 (or imap-store-password
825 (y-or-n-p "Store password for this session? ")))
826 (setq imap-password passwd)))
827 (message "Login failed...")
829 (setq imap-password nil)
831 ;; (quit (with-current-buffer buffer
834 ;; (error (with-current-buffer buffer
839 (defun imap-gssapi-auth-p (buffer)
840 (eq imap-stream 'gssapi))
842 (defun imap-gssapi-auth (buffer)
843 (message "imap: Authenticating using GSSAPI...%s"
844 (if (eq imap-stream 'gssapi) "done" "failed"))
845 (eq imap-stream 'gssapi))
847 (defun imap-kerberos4-auth-p (buffer)
848 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
849 (eq imap-stream 'kerberos4)))
851 (defun imap-kerberos4-auth (buffer)
852 (message "imap: Authenticating using Kerberos 4...%s"
853 (if (eq imap-stream 'kerberos4) "done" "failed"))
854 (eq imap-stream 'kerberos4))
856 (defun imap-cram-md5-p (buffer)
857 (imap-capability 'AUTH=CRAM-MD5 buffer))
859 (defun imap-cram-md5-auth (buffer)
860 "Login to server using the AUTH CRAM-MD5 method."
861 (message "imap: Authenticating using CRAM-MD5...")
862 (let ((done (imap-interactive-login
864 (lambda (user passwd)
866 (imap-send-command-wait
868 "AUTHENTICATE CRAM-MD5"
870 (let* ((decoded (base64-decode-string challenge))
872 (if (and (featurep 'xemacs)
873 (>= (function-max-args 'md5) 4))
874 (lambda (object &optional start end)
875 (md5 object start end 'binary))
877 (hash (rfc2104-hash hash-function 64 16
879 (response (concat user " " hash))
880 (encoded (base64-encode-string response)))
883 (message "imap: Authenticating using CRAM-MD5...done")
884 (message "imap: Authenticating using CRAM-MD5...failed"))))
886 (defun imap-login-p (buffer)
887 (and (not (imap-capability 'LOGINDISABLED buffer))
888 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
890 (defun imap-login-auth (buffer)
891 "Login to server using the LOGIN command."
892 (message "imap: Plaintext authentication...")
893 (imap-interactive-login buffer
894 (lambda (user passwd)
895 (imap-ok-p (imap-send-command-wait
896 (concat "LOGIN \"" user "\" \""
899 (defun imap-anonymous-p (buffer)
902 (defun imap-anonymous-auth (buffer)
903 (message "imap: Logging in anonymously...")
904 (with-current-buffer buffer
905 (imap-ok-p (imap-send-command-wait
906 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
907 (system-name)) "\"")))))
909 ;;; Compiler directives.
911 (defvar imap-sasl-client)
912 (defvar imap-sasl-step)
914 (defun imap-sasl-make-mechanisms (buffer)
917 (let ((name (symbol-name sym)))
918 (if (and (> (length name) 5)
919 (string-equal "AUTH=" (substring name 0 5 )))
920 (setq mecs (cons (substring name 5) mecs)))))
921 (imap-capability nil buffer))
924 (defun imap-sasl-auth-p (buffer)
925 (and (condition-case ()
928 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
930 (defun imap-sasl-auth (buffer)
931 "Login to server using the SASL method."
932 (message "imap: Authenticating using SASL...")
933 (with-current-buffer buffer
934 (make-local-variable 'imap-username)
935 (make-local-variable 'imap-sasl-client)
936 (make-local-variable 'imap-sasl-step)
937 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
940 (setq user (or imap-username
941 (read-from-minibuffer
942 (concat "IMAP username for " imap-server " using SASL "
943 (sasl-mechanism-name mechanism) ": ")
944 (or user imap-default-user))))
946 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
947 imap-sasl-step (sasl-next-step imap-sasl-client nil))
948 (let ((tag (imap-send-command
949 (if (sasl-step-data imap-sasl-step)
950 (format "AUTHENTICATE %s %s"
951 (sasl-mechanism-name mechanism)
952 (sasl-step-data imap-sasl-step))
953 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
955 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
956 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
957 (setq imap-continuation nil
958 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
959 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
960 (base64-encode-string (sasl-step-data imap-sasl-step) t)
962 (if (imap-ok-p (imap-wait-for-tag tag))
963 (setq imap-username user
965 (message "Login failed...")
969 (defun imap-digest-md5-p (buffer)
970 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
972 (require 'digest-md5)
975 (defun imap-digest-md5-auth (buffer)
976 "Login to server using the AUTH DIGEST-MD5 method."
977 (message "imap: Authenticating using DIGEST-MD5...")
978 (imap-interactive-login
980 (lambda (user passwd)
984 "AUTHENTICATE DIGEST-MD5"
986 (digest-md5-parse-digest-challenge
987 (base64-decode-string challenge))
989 (digest-md5-digest-uri
990 "imap" (digest-md5-challenge 'realm)))
992 (digest-md5-digest-response
993 user passwd digest-uri)))
994 (base64-encode-string response 'no-line-break))))
996 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
998 (setq imap-continuation nil)
999 (imap-send-command-1 "")
1000 (imap-ok-p (imap-wait-for-tag tag)))))))
1002 ;; Server functions:
1004 (defun imap-open-1 (buffer)
1005 (with-current-buffer buffer
1007 (setq imap-current-mailbox nil
1008 imap-current-message nil
1010 imap-process (condition-case ()
1011 (funcall (nth 2 (assq imap-stream
1013 "imap" buffer imap-server imap-port)
1014 ((error quit) nil)))
1016 (set-process-filter imap-process 'imap-arrival-filter)
1017 (set-process-sentinel imap-process 'imap-sentinel)
1018 (while (and (eq imap-state 'initial)
1019 (memq (process-status imap-process) '(open run)))
1020 (message "Waiting for response from %s..." imap-server)
1021 (accept-process-output imap-process 1))
1022 (message "Waiting for response from %s...done" imap-server)
1023 (and (memq (process-status imap-process) '(open run))
1026 (defun imap-open (server &optional port stream auth buffer)
1027 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1028 If PORT is unspecified, a default value is used (143 except
1029 for SSL which use 993).
1030 STREAM indicates the stream to use, see `imap-streams' for available
1031 streams. If nil, it choices the best stream the server is capable of.
1032 AUTH indicates authenticator to use, see `imap-authenticators' for
1033 available authenticators. If nil, it choices the best stream the
1034 server is capable of.
1035 BUFFER can be a buffer or a name of a buffer, which is created if
1036 necessary. If nil, the buffer name is generated."
1037 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1038 (with-current-buffer (get-buffer-create buffer)
1039 (if (imap-opened buffer)
1040 (imap-close buffer))
1041 (mapcar 'make-local-variable imap-local-variables)
1042 (imap-disable-multibyte)
1043 (buffer-disable-undo)
1044 (setq imap-server (or server imap-server))
1045 (setq imap-port (or port imap-port))
1046 (setq imap-auth (or auth imap-auth))
1047 (setq imap-stream (or stream imap-stream))
1048 (message "imap: Connecting to %s..." imap-server)
1049 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1050 (imap-open-1 buffer)))
1052 (message "imap: Connecting to %s...failed" imap-server)
1054 (when (null imap-stream)
1055 ;; Need to choose stream.
1056 (let ((streams imap-streams))
1057 (while (setq stream (pop streams))
1058 ;; OK to use this stream?
1059 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1061 (if (not (eq imap-default-stream stream))
1062 (with-current-buffer (get-buffer-create
1063 (generate-new-buffer-name " *temp*"))
1064 (mapcar 'make-local-variable imap-local-variables)
1065 (imap-disable-multibyte)
1066 (buffer-disable-undo)
1067 (setq imap-server (or server imap-server))
1068 (setq imap-port (or port imap-port))
1069 (setq imap-auth (or auth imap-auth))
1070 (message "imap: Reconnecting with stream `%s'..." stream)
1071 (if (null (let ((imap-stream stream))
1072 (imap-open-1 (current-buffer))))
1074 (kill-buffer (current-buffer))
1076 "imap: Reconnecting with stream `%s'...failed"
1078 ;; We're done, kill the first connection
1080 (kill-buffer buffer)
1081 (rename-buffer buffer)
1082 (message "imap: Reconnecting with stream `%s'...done"
1084 (setq imap-stream stream)
1085 (setq imap-capability nil)
1086 (setq streams nil)))
1088 (message "imap: Connecting to %s...done" imap-server)
1089 (setq imap-stream stream)
1090 (setq imap-capability nil)
1091 (setq streams nil))))))
1092 (when (imap-opened buffer)
1093 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1097 (defun imap-opened (&optional buffer)
1098 "Return non-nil if connection to imap server in BUFFER is open.
1099 If BUFFER is nil then the current buffer is used."
1100 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1101 (buffer-live-p buffer)
1102 (with-current-buffer buffer
1104 (memq (process-status imap-process) '(open run))))))
1106 (defun imap-authenticate (&optional user passwd buffer)
1107 "Authenticate to server in BUFFER, using current buffer if nil.
1108 It uses the authenticator specified when opening the server. If the
1109 authenticator requires username/passwords, they are queried from the
1110 user and optionally stored in the buffer. If USER and/or PASSWD is
1111 specified, the user will not be questioned and the username and/or
1112 password is remembered in the buffer."
1113 (with-current-buffer (or buffer (current-buffer))
1114 (if (not (eq imap-state 'nonauth))
1115 (or (eq imap-state 'auth)
1116 (eq imap-state 'select)
1117 (eq imap-state 'examine))
1118 (make-local-variable 'imap-username)
1119 (make-local-variable 'imap-password)
1120 (if user (setq imap-username user))
1121 (if passwd (setq imap-password passwd))
1123 (and (funcall (nth 2 (assq imap-auth
1124 imap-authenticator-alist)) buffer)
1125 (setq imap-state 'auth))
1126 ;; Choose authenticator.
1127 (let ((auths imap-authenticators)
1129 (while (setq auth (pop auths))
1130 ;; OK to use authenticator?
1131 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1132 (message "imap: Authenticating to `%s' using `%s'..."
1134 (setq imap-auth auth)
1135 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1137 (message "imap: Authenticating to `%s' using `%s'...done"
1140 (message "imap: Authenticating to `%s' using `%s'...failed"
1141 imap-server auth)))))
1144 (defun imap-close (&optional buffer)
1145 "Close connection to server in BUFFER.
1146 If BUFFER is nil, the current buffer is used."
1147 (with-current-buffer (or buffer (current-buffer))
1150 (imap-send-command-wait "LOGOUT")
1152 (when (and imap-process
1153 (memq (process-status imap-process) '(open run)))
1154 (delete-process imap-process))
1155 (setq imap-current-mailbox nil
1156 imap-current-message nil
1161 (defun imap-capability (&optional identifier buffer)
1162 "Return a list of identifiers which server in BUFFER support.
1163 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1164 If BUFFER is nil, the current buffer is assumed."
1165 (with-current-buffer (or buffer (current-buffer))
1166 (unless imap-capability
1167 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1168 (setq imap-capability '(IMAP2))))
1170 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1173 (defun imap-id (&optional list-of-values buffer)
1174 "Identify client to server in BUFFER, and return server identity.
1175 LIST-OF-VALUES is nil, or a plist with identifier and value
1176 strings to send to the server to identify the client.
1178 Return a list of identifiers which server in BUFFER support, or
1179 nil if it doesn't support ID or returns no information.
1181 If BUFFER is nil, the current buffer is assumed."
1182 (with-current-buffer (or buffer (current-buffer))
1183 (when (and (imap-capability 'ID)
1184 (imap-ok-p (imap-send-command-wait
1185 (if (null list-of-values)
1187 (concat "ID (" (mapconcat (lambda (el)
1188 (concat "\"" el "\""))
1193 (defun imap-namespace (&optional buffer)
1194 "Return a namespace hierarchy at server in BUFFER.
1195 If BUFFER is nil, the current buffer is assumed."
1196 (with-current-buffer (or buffer (current-buffer))
1197 (unless imap-namespace
1198 (when (imap-capability 'NAMESPACE)
1199 (imap-send-command-wait "NAMESPACE")))
1202 (defun imap-send-command-wait (command &optional buffer)
1203 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1206 ;; Mailbox functions:
1208 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1209 (with-current-buffer (or buffer (current-buffer))
1210 (if imap-mailbox-data
1211 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1213 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1214 propname value mailbox (current-buffer)))
1217 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1218 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1221 (defun imap-mailbox-get (propname &optional mailbox buffer)
1222 (let ((mailbox (imap-utf7-encode mailbox)))
1223 (with-current-buffer (or buffer (current-buffer))
1224 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1226 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1227 (with-current-buffer (or buffer (current-buffer))
1231 (push (funcall func (if mailbox-decoder
1232 (funcall mailbox-decoder (symbol-name s))
1233 (symbol-name s))) result))
1237 (defun imap-mailbox-map (func &optional buffer)
1238 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1239 Function should take a mailbox name (a string) as
1241 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1243 (defun imap-current-mailbox (&optional buffer)
1244 (with-current-buffer (or buffer (current-buffer))
1245 (imap-utf7-decode imap-current-mailbox)))
1247 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1248 (and (string= mailbox imap-current-mailbox)
1250 (eq imap-state 'examine))
1252 (eq imap-state 'selected)))))
1254 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1255 (with-current-buffer (or buffer (current-buffer))
1256 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1258 (defun imap-mailbox-select-1 (mailbox &optional examine)
1259 "Select MAILBOX on server in BUFFER.
1260 If EXAMINE is non-nil, do a read-only select."
1261 (if (imap-current-mailbox-p-1 mailbox examine)
1262 imap-current-mailbox
1263 (setq imap-current-mailbox mailbox)
1264 (if (imap-ok-p (imap-send-command-wait
1265 (concat (if examine "EXAMINE" "SELECT") " \""
1268 (setq imap-message-data (make-vector imap-message-prime 0)
1269 imap-state (if examine 'examine 'selected))
1270 imap-current-mailbox)
1271 ;; Failed SELECT/EXAMINE unselects current mailbox
1272 (setq imap-current-mailbox nil))))
1274 (defun imap-mailbox-select (mailbox &optional examine buffer)
1275 (with-current-buffer (or buffer (current-buffer))
1277 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1279 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1280 (with-current-buffer (or buffer (current-buffer))
1281 (imap-mailbox-select-1 mailbox 'examine)))
1283 (defun imap-mailbox-examine (mailbox &optional buffer)
1284 "Examine MAILBOX on server in BUFFER."
1285 (imap-mailbox-select mailbox 'examine buffer))
1287 (defun imap-mailbox-unselect (&optional buffer)
1288 "Close current folder in BUFFER, without expunging articles."
1289 (with-current-buffer (or buffer (current-buffer))
1290 (when (or (eq imap-state 'auth)
1291 (and (imap-capability 'UNSELECT)
1292 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1294 (imap-send-command-wait (concat "EXAMINE \""
1295 imap-current-mailbox
1297 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1298 (setq imap-current-mailbox nil
1299 imap-message-data nil
1303 (defun imap-mailbox-expunge (&optional asynch buffer)
1304 "Expunge articles in current folder in BUFFER.
1305 If ASYNCH, do not wait for succesful completion of the command.
1306 If BUFFER is nil the current buffer is assumed."
1307 (with-current-buffer (or buffer (current-buffer))
1308 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1310 (imap-send-command "EXPUNGE")
1311 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1313 (defun imap-mailbox-close (&optional asynch buffer)
1314 "Expunge articles and close current folder in BUFFER.
1315 If ASYNCH, do not wait for succesful completion of the command.
1316 If BUFFER is nil the current buffer is assumed."
1317 (with-current-buffer (or buffer (current-buffer))
1318 (when imap-current-mailbox
1320 (imap-add-callback (imap-send-command "CLOSE")
1321 `(lambda (tag status)
1322 (message "IMAP mailbox `%s' closed... %s"
1323 imap-current-mailbox status)
1324 (when (eq ,imap-current-mailbox
1325 imap-current-mailbox)
1326 ;; Don't wipe out data if another mailbox
1328 (setq imap-current-mailbox nil
1329 imap-message-data nil
1330 imap-state 'auth))))
1331 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1332 (setq imap-current-mailbox nil
1333 imap-message-data nil
1337 (defun imap-mailbox-create-1 (mailbox)
1338 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1340 (defun imap-mailbox-create (mailbox &optional buffer)
1341 "Create MAILBOX on server in BUFFER.
1342 If BUFFER is nil the current buffer is assumed."
1343 (with-current-buffer (or buffer (current-buffer))
1344 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1346 (defun imap-mailbox-delete (mailbox &optional buffer)
1347 "Delete MAILBOX on server in BUFFER.
1348 If BUFFER is nil the current buffer is assumed."
1349 (let ((mailbox (imap-utf7-encode mailbox)))
1350 (with-current-buffer (or buffer (current-buffer))
1352 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1354 (defun imap-mailbox-rename (oldname newname &optional buffer)
1355 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1356 If BUFFER is nil the current buffer is assumed."
1357 (let ((oldname (imap-utf7-encode oldname))
1358 (newname (imap-utf7-encode newname)))
1359 (with-current-buffer (or buffer (current-buffer))
1361 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1362 "\"" newname "\""))))))
1364 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1365 "Return a list of subscribed mailboxes on server in BUFFER.
1366 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1367 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1368 implementation-specific string that has to be passed to lsub command."
1369 (with-current-buffer (or buffer (current-buffer))
1370 ;; Make sure we know the hierarchy separator for root's hierarchy
1371 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1372 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1373 (imap-utf7-encode root) "\"")))
1374 ;; clear list data (NB not delimiter and other stuff)
1375 (imap-mailbox-map-1 (lambda (mailbox)
1376 (imap-mailbox-put 'lsub nil mailbox)))
1378 (imap-send-command-wait
1379 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1380 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1383 (imap-mailbox-map-1 (lambda (mailbox)
1384 (when (imap-mailbox-get-1 'lsub mailbox)
1385 (push (imap-utf7-decode mailbox) out))))
1388 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1389 "Return a list of mailboxes matching ROOT on server in BUFFER.
1390 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1391 root. REFERENCE is a implementation-specific string that has to be
1392 passed to list command."
1393 (with-current-buffer (or buffer (current-buffer))
1394 ;; Make sure we know the hierarchy separator for root's hierarchy
1395 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1396 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1397 (imap-utf7-encode root) "\"")))
1398 ;; clear list data (NB not delimiter and other stuff)
1399 (imap-mailbox-map-1 (lambda (mailbox)
1400 (imap-mailbox-put 'list nil mailbox)))
1402 (imap-send-command-wait
1403 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1404 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1407 (imap-mailbox-map-1 (lambda (mailbox)
1408 (when (imap-mailbox-get-1 'list mailbox)
1409 (push (imap-utf7-decode mailbox) out))))
1412 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1413 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1414 Returns non-nil if successful."
1415 (with-current-buffer (or buffer (current-buffer))
1416 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1417 (imap-utf7-encode mailbox)
1420 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1421 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1422 Returns non-nil if successful."
1423 (with-current-buffer (or buffer (current-buffer))
1424 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1425 (imap-utf7-encode mailbox)
1428 (defun imap-mailbox-status (mailbox items &optional buffer)
1429 "Get status items ITEM in MAILBOX from server in BUFFER.
1430 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1431 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1432 or 'unseen. If ITEMS is a list of symbols, a list of values is
1433 returned, if ITEMS is a symbol only its value is returned."
1434 (with-current-buffer (or buffer (current-buffer))
1436 (imap-send-command-wait (list "STATUS \""
1437 (imap-utf7-encode mailbox)
1445 (mapcar (lambda (item)
1446 (imap-mailbox-get item mailbox))
1448 (imap-mailbox-get items mailbox)))))
1450 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1451 "Send status item request ITEM on MAILBOX to server in BUFFER.
1452 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1453 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1454 or 'unseen. The IMAP command tag is returned."
1455 (with-current-buffer (or buffer (current-buffer))
1456 (imap-send-command (list "STATUS \""
1457 (imap-utf7-encode mailbox)
1464 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1465 "Get ACL on mailbox from server in BUFFER."
1466 (let ((mailbox (imap-utf7-encode mailbox)))
1467 (with-current-buffer (or buffer (current-buffer))
1469 (imap-send-command-wait (list "GETACL \""
1470 (or mailbox imap-current-mailbox)
1472 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1474 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1475 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1476 (let ((mailbox (imap-utf7-encode mailbox)))
1477 (with-current-buffer (or buffer (current-buffer))
1479 (imap-send-command-wait (list "SETACL \""
1480 (or mailbox imap-current-mailbox)
1486 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1487 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1488 (let ((mailbox (imap-utf7-encode mailbox)))
1489 (with-current-buffer (or buffer (current-buffer))
1491 (imap-send-command-wait (list "DELETEACL \""
1492 (or mailbox imap-current-mailbox)
1497 ;; Message functions:
1499 (defun imap-current-message (&optional buffer)
1500 (with-current-buffer (or buffer (current-buffer))
1501 imap-current-message))
1503 (defun imap-list-to-message-set (list)
1504 (mapconcat (lambda (item)
1505 (number-to-string item))
1511 (defun imap-range-to-message-set (range)
1516 (car item) (cdr item))
1517 (format "%d" item)))
1518 (if (and (listp range) (not (listp (cdr range))))
1519 (list range) ;; make (1 . 2) into ((1 . 2))
1523 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1524 (with-current-buffer (or buffer (current-buffer))
1525 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1527 (imap-list-to-message-set uids)
1531 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1532 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1533 UIDS can be a string, number or a list of numbers. If RECEIVE
1534 is non-nil return theese properties."
1535 (with-current-buffer (or buffer (current-buffer))
1536 (when (imap-ok-p (imap-send-command-wait
1537 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1539 (imap-list-to-message-set uids)
1542 (if (or (null receive) (stringp uids))
1545 (mapcar (lambda (uid)
1547 (mapcar (lambda (prop)
1548 (imap-message-get uid prop))
1550 (imap-message-get uid receive)))
1552 (imap-message-get uids receive))))))
1554 (defun imap-message-put (uid propname value &optional buffer)
1555 (with-current-buffer (or buffer (current-buffer))
1556 (if imap-message-data
1557 (put (intern (number-to-string uid) imap-message-data)
1559 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1560 uid propname value (current-buffer)))
1563 (defun imap-message-get (uid propname &optional buffer)
1564 (with-current-buffer (or buffer (current-buffer))
1565 (get (intern-soft (number-to-string uid) imap-message-data)
1568 (defun imap-message-map (func propname &optional buffer)
1569 "Map a function across each mailbox in `imap-message-data', returning a list."
1570 (with-current-buffer (or buffer (current-buffer))
1574 (push (funcall func (get s 'UID) (get s propname)) result))
1578 (defmacro imap-message-envelope-date (uid &optional buffer)
1579 `(with-current-buffer (or ,buffer (current-buffer))
1580 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1582 (defmacro imap-message-envelope-subject (uid &optional buffer)
1583 `(with-current-buffer (or ,buffer (current-buffer))
1584 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1586 (defmacro imap-message-envelope-from (uid &optional buffer)
1587 `(with-current-buffer (or ,buffer (current-buffer))
1588 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1590 (defmacro imap-message-envelope-sender (uid &optional buffer)
1591 `(with-current-buffer (or ,buffer (current-buffer))
1592 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1594 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1595 `(with-current-buffer (or ,buffer (current-buffer))
1596 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1598 (defmacro imap-message-envelope-to (uid &optional buffer)
1599 `(with-current-buffer (or ,buffer (current-buffer))
1600 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1602 (defmacro imap-message-envelope-cc (uid &optional buffer)
1603 `(with-current-buffer (or ,buffer (current-buffer))
1604 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1606 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1607 `(with-current-buffer (or ,buffer (current-buffer))
1608 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1610 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1611 `(with-current-buffer (or ,buffer (current-buffer))
1612 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1614 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1615 `(with-current-buffer (or ,buffer (current-buffer))
1616 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1618 (defmacro imap-message-body (uid &optional buffer)
1619 `(with-current-buffer (or ,buffer (current-buffer))
1620 (imap-message-get ,uid 'BODY)))
1622 (defun imap-search (predicate &optional buffer)
1623 (with-current-buffer (or buffer (current-buffer))
1624 (imap-mailbox-put 'search 'dummy)
1625 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1626 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1628 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1630 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1632 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1633 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1634 (with-current-buffer (or buffer (current-buffer))
1635 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1636 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1638 (defun imap-message-flags-set (articles flags &optional silent buffer)
1639 (when (and articles flags)
1640 (with-current-buffer (or buffer (current-buffer))
1641 (imap-ok-p (imap-send-command-wait
1642 (concat "UID STORE " articles
1643 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1645 (defun imap-message-flags-del (articles flags &optional silent buffer)
1646 (when (and articles flags)
1647 (with-current-buffer (or buffer (current-buffer))
1648 (imap-ok-p (imap-send-command-wait
1649 (concat "UID STORE " articles
1650 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1652 (defun imap-message-flags-add (articles flags &optional silent buffer)
1653 (when (and articles flags)
1654 (with-current-buffer (or buffer (current-buffer))
1655 (imap-ok-p (imap-send-command-wait
1656 (concat "UID STORE " articles
1657 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1659 (defun imap-message-copyuid-1 (mailbox)
1660 (if (imap-capability 'UIDPLUS)
1661 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1662 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1663 (let ((old-mailbox imap-current-mailbox)
1665 (imap-message-data (make-vector 2 0)))
1666 (when (imap-mailbox-examine-1 mailbox)
1668 (and (imap-fetch "*" "UID")
1669 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1670 (apply 'max (imap-message-map
1671 (lambda (uid prop) uid) 'UID))))
1673 (imap-mailbox-select old-mailbox (eq state 'examine))
1674 (imap-mailbox-unselect)))))))
1676 (defun imap-message-copyuid (mailbox &optional buffer)
1677 (with-current-buffer (or buffer (current-buffer))
1678 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1680 (defun imap-message-copy (articles mailbox
1681 &optional dont-create no-copyuid buffer)
1682 "Copy ARTICLES (a string message set) to MAILBOX on server in
1683 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1684 non-nil, it will not create a mailbox. On success, return a list with
1685 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1686 first element, rest of list contain the saved articles' UIDs."
1688 (with-current-buffer (or buffer (current-buffer))
1689 (let ((mailbox (imap-utf7-encode mailbox)))
1690 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1691 (imap-current-target-mailbox mailbox))
1692 (if (imap-ok-p (imap-send-command-wait cmd))
1694 (when (and (not dont-create)
1695 ;; removed because of buggy Oracle server
1696 ;; that doesn't send TRYCREATE tags (which
1697 ;; is a MUST according to specifications):
1698 ;;(imap-mailbox-get-1 'trycreate mailbox)
1699 (imap-mailbox-create-1 mailbox))
1700 (imap-ok-p (imap-send-command-wait cmd)))))
1702 (imap-message-copyuid-1 mailbox)))))))
1704 (defun imap-message-appenduid-1 (mailbox)
1705 (if (imap-capability 'UIDPLUS)
1706 (imap-mailbox-get-1 'appenduid mailbox)
1707 (let ((old-mailbox imap-current-mailbox)
1709 (imap-message-data (make-vector 2 0)))
1710 (when (imap-mailbox-examine-1 mailbox)
1712 (and (imap-fetch "*" "UID")
1713 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1714 (apply 'max (imap-message-map
1715 (lambda (uid prop) uid) 'UID))))
1717 (imap-mailbox-select old-mailbox (eq state 'examine))
1718 (imap-mailbox-unselect)))))))
1720 (defun imap-message-appenduid (mailbox &optional buffer)
1721 (with-current-buffer (or buffer (current-buffer))
1722 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1724 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1725 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1726 FLAGS and DATE-TIME is currently not used. Return a cons holding
1727 uidvalidity of MAILBOX and UID the newly created article got, or nil
1729 (let ((mailbox (imap-utf7-encode mailbox)))
1730 (with-current-buffer (or buffer (current-buffer))
1731 (and (let ((imap-current-target-mailbox mailbox))
1733 (imap-send-command-wait
1734 (list "APPEND \"" mailbox "\" " article))))
1735 (imap-message-appenduid-1 mailbox)))))
1737 (defun imap-body-lines (body)
1738 "Return number of lines in article by looking at the mime bodystructure BODY."
1740 (if (stringp (car body))
1741 (cond ((and (string= (upcase (car body)) "TEXT")
1742 (numberp (nth 7 body)))
1744 ((and (string= (upcase (car body)) "MESSAGE")
1745 (numberp (nth 9 body)))
1748 (apply '+ (mapcar 'imap-body-lines body)))
1751 (defun imap-envelope-from (from)
1752 "Return a from string line."
1754 (concat (aref from 0)
1755 (if (aref from 0) " <")
1759 (if (aref from 0) ">"))))
1762 ;; Internal functions.
1764 (defun imap-add-callback (tag func)
1765 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1767 (defun imap-send-command-1 (cmdstr)
1768 (setq cmdstr (concat cmdstr imap-client-eol))
1770 (with-current-buffer (get-buffer-create imap-log-buffer)
1771 (imap-disable-multibyte)
1772 (buffer-disable-undo)
1773 (goto-char (point-max))
1775 (process-send-string imap-process cmdstr))
1777 (defun imap-send-command (command &optional buffer)
1778 (with-current-buffer (or buffer (current-buffer))
1779 (if (not (listp command)) (setq command (list command)))
1780 (let ((tag (setq imap-tag (1+ imap-tag)))
1782 (setq cmdstr (concat (number-to-string imap-tag) " "))
1783 (while (setq cmd (pop command))
1784 (cond ((stringp cmd)
1785 (setq cmdstr (concat cmdstr cmd)))
1787 (let ((eol imap-client-eol)
1788 (calcfirst imap-calculate-literal-size-first)
1790 (with-current-buffer cmd
1792 (setq size (buffer-size)))
1793 (when (not (equal eol "\r\n"))
1794 ;; XXX modifies buffer!
1795 (goto-char (point-min))
1796 (while (search-forward "\r\n" nil t)
1797 (replace-match eol)))
1799 (setq size (buffer-size))))
1801 (concat cmdstr (format "{%d}" size))))
1804 (imap-send-command-1 cmdstr)
1806 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1807 (setq command nil) ;; abort command if no cont-req
1808 (let ((process imap-process)
1809 (stream imap-stream)
1810 (eol imap-client-eol))
1811 (with-current-buffer cmd
1813 (with-current-buffer (get-buffer-create
1815 (imap-disable-multibyte)
1816 (buffer-disable-undo)
1817 (goto-char (point-max))
1818 (insert-buffer-substring cmd)))
1819 (process-send-region process (point-min)
1821 (process-send-string process imap-client-eol))))
1822 (setq imap-continuation nil)))
1824 (imap-send-command-1 cmdstr)
1827 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1828 (setq command nil) ;; abort command if no cont-req
1829 (setq command (cons (funcall cmd imap-continuation)
1831 (setq imap-continuation nil)))
1833 (error "Unknown command type"))))
1835 (imap-send-command-1 cmdstr))
1838 (defun imap-wait-for-tag (tag &optional buffer)
1839 (with-current-buffer (or buffer (current-buffer))
1840 (let (imap-have-messaged)
1841 (while (and (null imap-continuation)
1842 (memq (process-status imap-process) '(open run))
1843 (< imap-reached-tag tag))
1844 (let ((len (/ (point-max) 1024))
1847 (setq imap-have-messaged t)
1848 (message "imap read: %dk" len))
1849 (accept-process-output imap-process
1850 (truncate imap-read-timeout)
1851 (truncate (* (- imap-read-timeout
1852 (truncate imap-read-timeout))
1854 ;; A process can die _before_ we have processed everything it
1855 ;; has to say. Moreover, this can happen in between the call to
1856 ;; accept-process-output and the call to process-status in an
1857 ;; iteration of the loop above.
1858 (when (and (null imap-continuation)
1859 (< imap-reached-tag tag))
1860 (accept-process-output imap-process 0 0))
1861 (when imap-have-messaged
1863 (and (memq (process-status imap-process) '(open run))
1864 (or (assq tag imap-failed-tags)
1865 (if imap-continuation
1869 (defun imap-sentinel (process string)
1870 (delete-process process))
1872 (defun imap-find-next-line ()
1873 "Return point at end of current line, taking into account literals.
1874 Return nil if no complete line has arrived."
1875 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1878 (if (match-string 1)
1879 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1881 (goto-char (+ (point) (string-to-number (match-string 1))))
1882 (imap-find-next-line))
1885 (defun imap-arrival-filter (proc string)
1886 "IMAP process filter."
1887 ;; Sometimes, we are called even though the process has died.
1888 ;; Better abstain from doing stuff in that case.
1889 (when (buffer-name (process-buffer proc))
1890 (with-current-buffer (process-buffer proc)
1891 (goto-char (point-max))
1894 (with-current-buffer (get-buffer-create imap-log-buffer)
1895 (imap-disable-multibyte)
1896 (buffer-disable-undo)
1897 (goto-char (point-max))
1900 (goto-char (point-min))
1901 (while (setq end (imap-find-next-line))
1903 (narrow-to-region (point-min) end)
1904 (delete-backward-char (length imap-server-eol))
1905 (goto-char (point-min))
1907 (cond ((eq imap-state 'initial)
1908 (imap-parse-greeting))
1909 ((or (eq imap-state 'auth)
1910 (eq imap-state 'nonauth)
1911 (eq imap-state 'selected)
1912 (eq imap-state 'examine))
1913 (imap-parse-response))
1915 (message "Unknown state %s in arrival filter"
1917 (delete-region (point-min) (point-max)))))))))
1922 (defsubst imap-forward ()
1923 (or (eobp) (forward-char)))
1926 ;; ; Unsigned 32-bit integer
1927 ;; ; (0 <= n < 4,294,967,296)
1929 (defsubst imap-parse-number ()
1930 (when (looking-at "[0-9]+")
1932 (string-to-number (match-string 0))
1933 (goto-char (match-end 0)))))
1935 ;; literal = "{" number "}" CRLF *CHAR8
1936 ;; ; Number represents the number of CHAR8s
1938 (defsubst imap-parse-literal ()
1939 (when (looking-at "{\\([0-9]+\\)}\r\n")
1940 (let ((pos (match-end 0))
1941 (len (string-to-number (match-string 1))))
1942 (if (< (point-max) (+ pos len))
1944 (goto-char (+ pos len))
1945 (buffer-substring pos (+ pos len))))))
1947 ;; string = quoted / literal
1949 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1951 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1952 ;; "\" quoted-specials
1954 ;; quoted-specials = DQUOTE / "\"
1956 ;; TEXT-CHAR = <any CHAR except CR and LF>
1958 (defsubst imap-parse-string ()
1959 (cond ((eq (char-after) ?\")
1961 (let ((p (point)) (name ""))
1962 (skip-chars-forward "^\"\\\\")
1963 (setq name (buffer-substring p (point)))
1964 (while (eq (char-after) ?\\)
1965 (setq p (1+ (point)))
1967 (skip-chars-forward "^\"\\\\")
1968 (setq name (concat name (buffer-substring p (point)))))
1971 ((eq (char-after) ?{)
1972 (imap-parse-literal))))
1976 (defsubst imap-parse-nil ()
1977 (if (looking-at "NIL")
1978 (goto-char (match-end 0))))
1980 ;; nstring = string / nil
1982 (defsubst imap-parse-nstring ()
1983 (or (imap-parse-string)
1984 (and (imap-parse-nil)
1987 ;; astring = atom / string
1989 ;; atom = 1*ATOM-CHAR
1991 ;; ATOM-CHAR = <any CHAR except atom-specials>
1993 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1996 ;; list-wildcards = "%" / "*"
1998 ;; quoted-specials = DQUOTE / "\"
2000 (defsubst imap-parse-astring ()
2001 (or (imap-parse-string)
2002 (buffer-substring (point)
2003 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2004 (goto-char (1- (match-end 0)))
2008 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2011 ;; addr-adl = nstring
2012 ;; ; Holds route from [RFC-822] route-addr if
2015 ;; addr-host = nstring
2016 ;; ; nil indicates [RFC-822] group syntax.
2017 ;; ; Otherwise, holds [RFC-822] domain name
2019 ;; addr-mailbox = nstring
2020 ;; ; nil indicates end of [RFC-822] group; if
2021 ;; ; non-nil and addr-host is nil, holds
2022 ;; ; [RFC-822] group name.
2023 ;; ; Otherwise, holds [RFC-822] local-part
2024 ;; ; after removing [RFC-822] quoting
2026 ;; addr-name = nstring
2027 ;; ; If non-nil, holds phrase from [RFC-822]
2028 ;; ; mailbox after removing [RFC-822] quoting
2031 (defsubst imap-parse-address ()
2033 (when (eq (char-after) ?\()
2035 (setq address (vector (prog1 (imap-parse-nstring)
2037 (prog1 (imap-parse-nstring)
2039 (prog1 (imap-parse-nstring)
2041 (imap-parse-nstring)))
2042 (when (eq (char-after) ?\))
2046 ;; address-list = "(" 1*address ")" / nil
2050 (defsubst imap-parse-address-list ()
2051 (if (eq (char-after) ?\()
2052 (let (address addresses)
2054 (while (and (not (eq (char-after) ?\)))
2055 ;; next line for MS Exchange bug
2056 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2057 (setq address (imap-parse-address)))
2058 (setq addresses (cons address addresses)))
2059 (when (eq (char-after) ?\))
2061 (nreverse addresses)))
2062 ;; With assert, the code might not be eval'd.
2063 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2066 ;; mailbox = "INBOX" / astring
2067 ;; ; INBOX is case-insensitive. All case variants of
2068 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2069 ;; ; not as an astring. An astring which consists of
2070 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2071 ;; ; is considered to be INBOX and not an astring.
2072 ;; ; Refer to section 5.1 for further
2073 ;; ; semantic details of mailbox names.
2075 (defsubst imap-parse-mailbox ()
2076 (let ((mailbox (imap-parse-astring)))
2077 (if (string-equal "INBOX" (upcase mailbox))
2081 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2083 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2084 ;; ; Authentication condition
2086 ;; resp-cond-bye = "BYE" SP resp-text
2088 (defun imap-parse-greeting ()
2089 "Parse a IMAP greeting."
2090 (cond ((looking-at "\\* OK ")
2091 (setq imap-state 'nonauth))
2092 ((looking-at "\\* PREAUTH ")
2093 (setq imap-state 'auth))
2094 ((looking-at "\\* BYE ")
2095 (setq imap-state 'closed))))
2097 ;; response = *(continue-req / response-data) response-done
2099 ;; continue-req = "+" SP (resp-text / base64) CRLF
2101 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2102 ;; mailbox-data / message-data / capability-data) CRLF
2104 ;; response-done = response-tagged / response-fatal
2106 ;; response-fatal = "*" SP resp-cond-bye CRLF
2107 ;; ; Server closes connection immediately
2109 ;; response-tagged = tag SP resp-cond-state CRLF
2111 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2112 ;; ; Status condition
2114 ;; resp-cond-bye = "BYE" SP resp-text
2116 ;; mailbox-data = "FLAGS" SP flag-list /
2117 ;; "LIST" SP mailbox-list /
2118 ;; "LSUB" SP mailbox-list /
2119 ;; "SEARCH" *(SP nz-number) /
2120 ;; "STATUS" SP mailbox SP "("
2121 ;; [status-att SP number *(SP status-att SP number)] ")" /
2122 ;; number SP "EXISTS" /
2123 ;; number SP "RECENT"
2125 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2127 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2129 ;; ; IMAP4rev1 servers which offer RFC 1730
2130 ;; ; compatibility MUST list "IMAP4" as the first
2133 (defun imap-parse-response ()
2134 "Parse a IMAP command response."
2136 (case (setq token (read (current-buffer)))
2137 (+ (setq imap-continuation
2138 (or (buffer-substring (min (point-max) (1+ (point)))
2141 (* (case (prog1 (setq token (read (current-buffer)))
2143 (OK (imap-parse-resp-text))
2144 (NO (imap-parse-resp-text))
2145 (BAD (imap-parse-resp-text))
2146 (BYE (imap-parse-resp-text))
2147 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2148 (LIST (imap-parse-data-list 'list))
2149 (LSUB (imap-parse-data-list 'lsub))
2150 (SEARCH (imap-mailbox-put
2152 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2153 (STATUS (imap-parse-status))
2154 (CAPABILITY (setq imap-capability
2155 (read (concat "(" (upcase (buffer-substring
2156 (point) (point-max)))
2158 (ID (setq imap-id (read (buffer-substring (point)
2160 (ACL (imap-parse-acl))
2161 (t (case (prog1 (read (current-buffer))
2163 (EXISTS (imap-mailbox-put 'exists token))
2164 (RECENT (imap-mailbox-put 'recent token))
2166 (FETCH (imap-parse-fetch token))
2167 (t (message "Garbage: %s" (buffer-string)))))))
2169 (if (not (integerp token))
2170 (message "Garbage: %s" (buffer-string))
2171 (case (prog1 (setq status (read (current-buffer)))
2174 (setq imap-reached-tag (max imap-reached-tag token))
2175 (imap-parse-resp-text)))
2177 (setq imap-reached-tag (max imap-reached-tag token))
2179 (imap-parse-resp-text))
2181 (when (eq (char-after) ?\[)
2182 (setq code (buffer-substring (point)
2183 (search-forward "]")))
2185 (setq text (buffer-substring (point) (point-max)))
2186 (push (list token status code text)
2187 imap-failed-tags))))
2189 (setq imap-reached-tag (max imap-reached-tag token))
2191 (imap-parse-resp-text))
2193 (when (eq (char-after) ?\[)
2194 (setq code (buffer-substring (point)
2195 (search-forward "]")))
2197 (setq text (buffer-substring (point) (point-max)))
2198 (push (list token status code text) imap-failed-tags)
2199 (error "Internal error, tag %s status %s code %s text %s"
2200 token status code text))))
2201 (t (message "Garbage: %s" (buffer-string))))
2202 (when (assq token imap-callbacks)
2203 (funcall (cdr (assq token imap-callbacks)) token status)
2204 (setq imap-callbacks
2205 (imap-remassoc token imap-callbacks)))))))))
2207 ;; resp-text = ["[" resp-text-code "]" SP] text
2209 ;; text = 1*TEXT-CHAR
2211 ;; TEXT-CHAR = <any CHAR except CR and LF>
2213 (defun imap-parse-resp-text ()
2214 (imap-parse-resp-text-code))
2216 ;; resp-text-code = "ALERT" /
2217 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2218 ;; "NEWNAME" SP string SP string /
2220 ;; "PERMANENTFLAGS" SP "("
2221 ;; [flag-perm *(SP flag-perm)] ")" /
2225 ;; "UIDNEXT" SP nz-number /
2226 ;; "UIDVALIDITY" SP nz-number /
2227 ;; "UNSEEN" SP nz-number /
2228 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2230 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2232 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2234 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2236 ;; ; Identifies a set of messages. For message
2237 ;; ; sequence numbers, these are consecutive
2238 ;; ; numbers from 1 to the number of messages in
2240 ;; ; Comma delimits individual numbers, colon
2241 ;; ; delimits between two numbers inclusive.
2242 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2243 ;; ; 14,15 for a mailbox with 15 messages.
2245 ;; sequence-num = nz-number / "*"
2246 ;; ; * is the largest number in use. For message
2247 ;; ; sequence numbers, it is the number of messages
2248 ;; ; in the mailbox. For unique identifiers, it is
2249 ;; ; the unique identifier of the last message in
2252 ;; flag-perm = flag / "\*"
2254 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2255 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2256 ;; ; Does not include "\Recent"
2258 ;; flag-extension = "\" atom
2259 ;; ; Future expansion. Client implementations
2260 ;; ; MUST accept flag-extension flags. Server
2261 ;; ; implementations MUST NOT generate
2262 ;; ; flag-extension flags except as defined by
2263 ;; ; future standard or standards-track
2264 ;; ; revisions of this specification.
2266 ;; flag-keyword = atom
2268 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2270 (defun imap-parse-resp-text-code ()
2271 ;; xxx next line for stalker communigate pro 3.3.1 bug
2272 (when (looking-at " \\[")
2274 (when (eq (char-after) ?\[)
2276 (cond ((search-forward "PERMANENTFLAGS " nil t)
2277 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2278 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2279 (imap-mailbox-put 'uidnext (match-string 1)))
2280 ((search-forward "UNSEEN " nil t)
2281 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2282 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2283 (imap-mailbox-put 'uidvalidity (match-string 1)))
2284 ((search-forward "READ-ONLY" nil t)
2285 (imap-mailbox-put 'read-only t))
2286 ((search-forward "NEWNAME " nil t)
2287 (let (oldname newname)
2288 (setq oldname (imap-parse-string))
2290 (setq newname (imap-parse-string))
2291 (imap-mailbox-put 'newname newname oldname)))
2292 ((search-forward "TRYCREATE" nil t)
2293 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2294 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2295 (imap-mailbox-put 'appenduid
2296 (list (match-string 1)
2297 (string-to-number (match-string 2)))
2298 imap-current-target-mailbox))
2299 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2300 (imap-mailbox-put 'copyuid (list (match-string 1)
2303 imap-current-target-mailbox))
2304 ((search-forward "ALERT] " nil t)
2305 (message "Imap server %s information: %s" imap-server
2306 (buffer-substring (point) (point-max)))))))
2308 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2309 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2311 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2312 ;; *(SP mbx-list-oflag) /
2313 ;; mbx-list-oflag *(SP mbx-list-oflag)
2315 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2316 ;; ; Other flags; multiple possible per LIST response
2318 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2319 ;; ; Selectability flags; only one per LIST response
2321 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2322 ;; "\" quoted-specials
2324 ;; quoted-specials = DQUOTE / "\"
2326 (defun imap-parse-data-list (type)
2327 (let (flags delimiter mailbox)
2328 (setq flags (imap-parse-flag-list))
2329 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2330 (setq delimiter (match-string 1))
2331 (goto-char (1+ (match-end 0)))
2332 (when (setq mailbox (imap-parse-mailbox))
2333 (imap-mailbox-put type t mailbox)
2334 (imap-mailbox-put 'list-flags flags mailbox)
2335 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2337 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2338 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2339 ;; "INTERNALDATE" SPACE date_time /
2340 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2341 ;; "RFC822.SIZE" SPACE number /
2342 ;; "BODY" ["STRUCTURE"] SPACE body /
2343 ;; "BODY" section ["<" number ">"] SPACE nstring /
2344 ;; "UID" SPACE uniqueid) ")"
2346 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2347 ;; SPACE time SPACE zone <">
2349 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2350 ;; ["." (section_text / "MIME")])] "]"
2352 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2353 ;; SPACE header_list / "TEXT"
2355 ;; header_fld_name ::= astring
2357 ;; header_list ::= "(" 1#header_fld_name ")"
2359 (defsubst imap-parse-header-list ()
2360 (when (eq (char-after) ?\()
2362 (while (not (eq (char-after) ?\)))
2364 (push (imap-parse-astring) strlist))
2366 (nreverse strlist))))
2368 (defsubst imap-parse-fetch-body-section ()
2370 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2371 (if (eq (char-before) ? )
2373 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2374 (search-forward "]" nil t))
2377 (defun imap-parse-fetch (response)
2378 (when (eq (char-after) ?\()
2379 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2380 rfc822size body bodydetail bodystructure flags-empty)
2381 (while (not (eq (char-after) ?\)))
2383 (let ((token (read (current-buffer))))
2385 (cond ((eq token 'UID)
2386 (setq uid (condition-case ()
2387 (read (current-buffer))
2390 (setq flags (imap-parse-flag-list))
2392 (setq flags-empty 't)))
2393 ((eq token 'ENVELOPE)
2394 (setq envelope (imap-parse-envelope)))
2395 ((eq token 'INTERNALDATE)
2396 (setq internaldate (imap-parse-string)))
2398 (setq rfc822 (imap-parse-nstring)))
2399 ((eq token 'RFC822.HEADER)
2400 (setq rfc822header (imap-parse-nstring)))
2401 ((eq token 'RFC822.TEXT)
2402 (setq rfc822text (imap-parse-nstring)))
2403 ((eq token 'RFC822.SIZE)
2404 (setq rfc822size (read (current-buffer))))
2406 (if (eq (char-before) ?\[)
2408 (upcase (imap-parse-fetch-body-section))
2409 (and (eq (char-after) ?<)
2410 (buffer-substring (1+ (point))
2411 (search-forward ">" nil t)))
2412 (progn (imap-forward)
2413 (imap-parse-nstring)))
2415 (setq body (imap-parse-body))))
2416 ((eq token 'BODYSTRUCTURE)
2417 (setq bodystructure (imap-parse-body))))))
2419 (setq imap-current-message uid)
2420 (imap-message-put uid 'UID uid)
2421 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2422 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2423 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2424 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2425 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2426 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2427 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2428 (and body (imap-message-put uid 'BODY body))
2429 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2430 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2431 (run-hooks 'imap-fetch-data-hook)))))
2433 ;; mailbox-data = ...
2434 ;; "STATUS" SP mailbox SP "("
2435 ;; [status-att SP number
2436 ;; *(SP status-att SP number)] ")"
2439 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2442 (defun imap-parse-status ()
2443 (let ((mailbox (imap-parse-mailbox)))
2444 (if (eq (char-after) ? )
2446 (when (and mailbox (eq (char-after) ?\())
2447 (while (and (not (eq (char-after) ?\)))
2448 (or (forward-char) t)
2449 (looking-at "\\([A-Za-z]+\\) "))
2450 (let ((token (match-string 1)))
2451 (goto-char (match-end 0))
2452 (cond ((string= token "MESSAGES")
2453 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2454 ((string= token "RECENT")
2455 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2456 ((string= token "UIDNEXT")
2457 (and (looking-at "[0-9]+")
2458 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2459 (goto-char (match-end 0))))
2460 ((string= token "UIDVALIDITY")
2461 (and (looking-at "[0-9]+")
2462 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2463 (goto-char (match-end 0))))
2464 ((string= token "UNSEEN")
2465 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2467 (message "Unknown status data %s in mailbox %s ignored"
2469 (read (current-buffer)))))))))
2471 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2474 ;; identifier ::= astring
2476 ;; rights ::= astring
2478 (defun imap-parse-acl ()
2479 (let ((mailbox (imap-parse-mailbox))
2480 identifier rights acl)
2481 (while (eq (char-after) ?\ )
2483 (setq identifier (imap-parse-astring))
2485 (setq rights (imap-parse-astring))
2486 (setq acl (append acl (list (cons identifier rights)))))
2487 (imap-mailbox-put 'acl acl mailbox)))
2489 ;; flag-list = "(" [flag *(SP flag)] ")"
2491 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2492 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2493 ;; ; Does not include "\Recent"
2495 ;; flag-keyword = atom
2497 ;; flag-extension = "\" atom
2498 ;; ; Future expansion. Client implementations
2499 ;; ; MUST accept flag-extension flags. Server
2500 ;; ; implementations MUST NOT generate
2501 ;; ; flag-extension flags except as defined by
2502 ;; ; future standard or standards-track
2503 ;; ; revisions of this specification.
2505 (defun imap-parse-flag-list ()
2506 (let (flag-list start)
2507 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2508 (while (and (not (eq (char-after) ?\)))
2511 ;; next line for Courier IMAP bug.
2512 (skip-chars-forward " ")
2514 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2515 (push (buffer-substring start (point)) flag-list))
2516 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2518 (nreverse flag-list)))
2520 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2521 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2522 ;; env-in-reply-to SP env-message-id ")"
2524 ;; env-bcc = "(" 1*address ")" / nil
2526 ;; env-cc = "(" 1*address ")" / nil
2528 ;; env-date = nstring
2530 ;; env-from = "(" 1*address ")" / nil
2532 ;; env-in-reply-to = nstring
2534 ;; env-message-id = nstring
2536 ;; env-reply-to = "(" 1*address ")" / nil
2538 ;; env-sender = "(" 1*address ")" / nil
2540 ;; env-subject = nstring
2542 ;; env-to = "(" 1*address ")" / nil
2544 (defun imap-parse-envelope ()
2545 (when (eq (char-after) ?\()
2547 (vector (prog1 (imap-parse-nstring) ;; date
2549 (prog1 (imap-parse-nstring) ;; subject
2551 (prog1 (imap-parse-address-list) ;; from
2553 (prog1 (imap-parse-address-list) ;; sender
2555 (prog1 (imap-parse-address-list) ;; reply-to
2557 (prog1 (imap-parse-address-list) ;; to
2559 (prog1 (imap-parse-address-list) ;; cc
2561 (prog1 (imap-parse-address-list) ;; bcc
2563 (prog1 (imap-parse-nstring) ;; in-reply-to
2565 (prog1 (imap-parse-nstring) ;; message-id
2568 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2570 (defsubst imap-parse-string-list ()
2571 (cond ((eq (char-after) ?\() ;; body-fld-param
2574 (while (setq str (imap-parse-string))
2576 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2577 ;; between body-fld-param's sometimes
2578 (or (eq (char-after) ?\")
2580 (nreverse strlist)))
2584 ;; body-extension = nstring / number /
2585 ;; "(" body-extension *(SP body-extension) ")"
2586 ;; ; Future expansion. Client implementations
2587 ;; ; MUST accept body-extension fields. Server
2588 ;; ; implementations MUST NOT generate
2589 ;; ; body-extension fields except as defined by
2590 ;; ; future standard or standards-track
2591 ;; ; revisions of this specification.
2593 (defun imap-parse-body-extension ()
2594 (if (eq (char-after) ?\()
2597 (push (imap-parse-body-extension) b-e)
2598 (while (eq (char-after) ?\ )
2600 (push (imap-parse-body-extension) b-e))
2601 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2604 (or (imap-parse-number)
2605 (imap-parse-nstring))))
2607 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2608 ;; *(SP body-extension)]]
2609 ;; ; MUST NOT be returned on non-extensible
2612 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2613 ;; *(SP body-extension)]]
2614 ;; ; MUST NOT be returned on non-extensible
2617 (defsubst imap-parse-body-ext ()
2619 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2622 (if (eq (char-after) ?\()
2625 (push (imap-parse-string) dsp)
2627 (push (imap-parse-string-list) dsp)
2629 ;; With assert, the code might not be eval'd.
2630 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2632 (push (nreverse dsp) ext))
2633 (when (eq (char-after) ?\ ) ;; body-fld-lang
2635 (if (eq (char-after) ?\()
2636 (push (imap-parse-string-list) ext)
2637 (push (imap-parse-nstring) ext))
2638 (while (eq (char-after) ?\ ) ;; body-extension
2640 (setq ext (append (imap-parse-body-extension) ext)))))
2643 ;; body = "(" body-type-1part / body-type-mpart ")"
2645 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2646 ;; *(SP body-extension)]]
2647 ;; ; MUST NOT be returned on non-extensible
2650 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2651 ;; *(SP body-extension)]]
2652 ;; ; MUST NOT be returned on non-extensible
2655 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2656 ;; body-fld-enc SP body-fld-octets
2658 ;; body-fld-desc = nstring
2660 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2662 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2663 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2665 ;; body-fld-id = nstring
2667 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2669 ;; body-fld-lines = number
2671 ;; body-fld-md5 = nstring
2673 ;; body-fld-octets = number
2675 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2677 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2678 ;; [SP body-ext-1part]
2680 ;; body-type-basic = media-basic SP body-fields
2681 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2683 ;; body-type-msg = media-message SP body-fields SP envelope
2684 ;; SP body SP body-fld-lines
2686 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2688 ;; body-type-mpart = 1*body SP media-subtype
2689 ;; [SP body-ext-mpart]
2691 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2692 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2693 ;; ; Defined in [MIME-IMT]
2695 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2696 ;; ; Defined in [MIME-IMT]
2698 ;; media-subtype = string
2699 ;; ; Defined in [MIME-IMT]
2701 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2702 ;; ; Defined in [MIME-IMT]
2704 (defun imap-parse-body ()
2706 (when (eq (char-after) ?\()
2708 (if (eq (char-after) ?\()
2710 (while (and (eq (char-after) ?\()
2711 (setq subbody (imap-parse-body)))
2712 ;; buggy stalker communigate pro 3.0 insert a SPC between
2713 ;; parts in multiparts
2714 (when (and (eq (char-after) ?\ )
2715 (eq (char-after (1+ (point))) ?\())
2717 (push subbody body))
2719 (push (imap-parse-string) body) ;; media-subtype
2720 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2722 (if (eq (char-after) ?\() ;; body-fld-param
2723 (push (imap-parse-string-list) body)
2724 (push (and (imap-parse-nil) nil) body))
2726 (append (imap-parse-body-ext) body))) ;; body-ext-...
2727 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2731 (push (imap-parse-string) body) ;; media-type
2733 (push (imap-parse-string) body) ;; media-subtype
2735 ;; next line for Sun SIMS bug
2736 (and (eq (char-after) ? ) (imap-forward))
2737 (if (eq (char-after) ?\() ;; body-fld-param
2738 (push (imap-parse-string-list) body)
2739 (push (and (imap-parse-nil) nil) body))
2741 (push (imap-parse-nstring) body) ;; body-fld-id
2743 (push (imap-parse-nstring) body) ;; body-fld-desc
2745 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2746 ;; nstring and return nil instead of defaulting back to 7BIT
2747 ;; as the standard says.
2748 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2750 (push (imap-parse-number) body) ;; body-fld-octets
2752 ;; ok, we're done parsing the required parts, what comes now is one
2755 ;; envelope (then we're parsing body-type-msg)
2756 ;; body-fld-lines (then we're parsing body-type-text)
2757 ;; body-ext-1part (then we're parsing body-type-basic)
2759 ;; the problem is that the two first are in turn optionally followed
2760 ;; by the third. So we parse the first two here (if there are any)...
2762 (when (eq (char-after) ?\ )
2765 (cond ((eq (char-after) ?\() ;; body-type-msg:
2766 (push (imap-parse-envelope) body) ;; envelope
2768 (push (imap-parse-body) body) ;; body
2769 ;; buggy stalker communigate pro 3.0 doesn't print
2770 ;; number of lines in message/rfc822 attachment
2771 (if (eq (char-after) ?\))
2774 (push (imap-parse-number) body))) ;; body-fld-lines
2775 ((setq lines (imap-parse-number)) ;; body-type-text:
2776 (push lines body)) ;; body-fld-lines
2778 (backward-char))))) ;; no match...
2780 ;; ...and then parse the third one here...
2782 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2784 (push (imap-parse-nstring) body) ;; body-fld-md5
2785 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2787 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2791 (when imap-debug ; (untrace-all)
2793 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2794 (mapcar (lambda (f) (trace-function-background f imap-debug-buffer))
2805 imap-interactive-login
2821 imap-send-command-wait
2826 imap-current-mailbox
2827 imap-current-mailbox-p-1
2828 imap-current-mailbox-p
2829 imap-mailbox-select-1
2831 imap-mailbox-examine-1
2832 imap-mailbox-examine
2833 imap-mailbox-unselect
2834 imap-mailbox-expunge
2836 imap-mailbox-create-1
2842 imap-mailbox-subscribe
2843 imap-mailbox-unsubscribe
2845 imap-mailbox-acl-get
2846 imap-mailbox-acl-set
2847 imap-mailbox-acl-delete
2848 imap-current-message
2849 imap-list-to-message-set
2856 imap-message-flag-permanent-p
2857 imap-message-flags-set
2858 imap-message-flags-del
2859 imap-message-flags-add
2860 imap-message-copyuid-1
2861 imap-message-copyuid
2863 imap-message-appenduid-1
2864 imap-message-appenduid
2876 imap-parse-resp-text
2877 imap-parse-resp-text-code
2878 imap-parse-data-list
2882 imap-parse-flag-list
2884 imap-parse-body-extension
2890 ;;; imap.el ends here