1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; imap.el is a elisp library providing an interface for talking to
30 ;; imap.el is roughly divided in two parts, one that parses IMAP
31 ;; responses from the server and storing data into buffer-local
32 ;; variables, and one for utility functions which send commands to
33 ;; server, waits for an answer, and return information. The latter
34 ;; part is layered on top of the previous.
36 ;; The imap.el API consist of the following functions, other functions
37 ;; in this file should not be called directly and the result of doing
38 ;; so are at best undefined.
42 ;; imap-open, imap-opened, imap-authenticate, imap-close,
43 ;; imap-capability, imap-namespace, imap-error-text
47 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
48 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
49 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
50 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
51 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
52 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
53 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
57 ;; imap-fetch-asynch, imap-fetch,
58 ;; imap-current-message, imap-list-to-message-set,
59 ;; imap-message-get, imap-message-map
60 ;; imap-message-envelope-date, imap-message-envelope-subject,
61 ;; imap-message-envelope-from, imap-message-envelope-sender,
62 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
63 ;; imap-message-envelope-cc, imap-message-envelope-bcc
64 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
65 ;; imap-message-body, imap-message-flag-permanent-p
66 ;; imap-message-flags-set, imap-message-flags-del
67 ;; imap-message-flags-add, imap-message-copyuid
68 ;; imap-message-copy, imap-message-appenduid
69 ;; imap-message-append, imap-envelope-from
72 ;; It is my hope that these commands should be pretty self
73 ;; explanatory for someone that know IMAP. All functions have
74 ;; additional documentation on how to invoke them.
76 ;; imap.el support RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1), implemented
77 ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
78 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
79 ;; LOGINDISABLED) (with use of external library starttls.el and
80 ;; program starttls), and the GSSAPI / kerberos V4 sections of RFC1731
81 ;; (with use of external program `imtest'), RFC2971 (ID). It also
82 ;; take advantage the UNSELECT extension in Cyrus IMAPD.
84 ;; Without the work of John McClary Prevost and Jim Radford this library
85 ;; would not have seen the light of day. Many thanks.
87 ;; This is a transcript of short interactive session for demonstration
90 ;; (imap-open "my.mail.server")
91 ;; => " *imap* my.mail.server:0"
93 ;; The rest are invoked with current buffer as the buffer returned by
94 ;; `imap-open'. It is possible to do all without this, but it would
95 ;; look ugly here since `buffer' is always the last argument for all
96 ;; imap.el API functions.
98 ;; (imap-authenticate "myusername" "mypassword")
101 ;; (imap-mailbox-lsub "*")
102 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
104 ;; (imap-mailbox-list "INBOX.n%")
105 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
107 ;; (imap-mailbox-select "INBOX.nnimap")
110 ;; (imap-mailbox-get 'exists)
113 ;; (imap-mailbox-get 'uidvalidity)
116 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
119 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
120 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
124 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
125 ;; o Don't use `read' at all (important places already fixed)
126 ;; o Accept list of articles instead of message set string in most
127 ;; imap-message-* functions.
128 ;; o Send strings as literal if they contain, e.g., ".
132 ;; - 19991218 added starttls/digest-md5 patch,
133 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
134 ;; NB! you need SLIM for starttls.el and digest-md5.el
135 ;; - 19991023 commited to pgnus
140 (eval-when-compile (require 'cl))
142 (autoload 'starttls-open-stream "starttls")
143 (autoload 'starttls-negotiate "starttls")
144 (autoload 'sasl-find-mechanism "sasl")
145 (autoload 'digest-md5-parse-digest-challenge "digest-md5")
146 (autoload 'digest-md5-digest-response "digest-md5")
147 (autoload 'digest-md5-digest-uri "digest-md5")
148 (autoload 'digest-md5-challenge "digest-md5")
149 (autoload 'rfc2104-hash "rfc2104")
150 (autoload 'utf7-encode "utf7")
151 (autoload 'utf7-decode "utf7")
152 (autoload 'format-spec "format-spec")
153 (autoload 'format-spec-make "format-spec")
154 (autoload 'open-tls-stream "tls"))
159 "Low-level IMAP issues."
163 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
165 "List of strings containing commands for Kerberos 4 authentication.
166 %s is replaced with server hostname, %p with port to connect to, and
167 %l with the value of `imap-default-user'. The program should accept
168 IMAP commands on stdin and return responses to stdout. Each entry in
169 the list is tried until a successful connection is made."
171 :type '(repeat string))
173 (defcustom imap-gssapi-program (list
174 (concat "gsasl --client --connect %s:%p "
175 "--imap --application-data "
176 "--mechanism GSSAPI "
177 "--authentication-id %l")
178 "imtest -m gssapi -u %l -p %p %s")
179 "List of strings containing commands for GSSAPI (krb5) authentication.
180 %s is replaced with server hostname, %p with port to connect to, and
181 %l with the value of `imap-default-user'. The program should accept
182 IMAP commands on stdin and return responses to stdout. Each entry in
183 the list is tried until a successful connection is made."
185 :type '(repeat string))
187 (defcustom imap-ssl-program '("openssl s_client -quiet -ssl3 -connect %s:%p"
188 "openssl s_client -quiet -ssl2 -connect %s:%p"
189 "s_client -quiet -ssl3 -connect %s:%p"
190 "s_client -quiet -ssl2 -connect %s:%p")
191 "A string, or list of strings, containing commands for SSL connections.
192 Within a string, %s is replaced with the server address and %p with
193 port number on server. The program should accept IMAP commands on
194 stdin and return responses to stdout. Each entry in the list is tried
195 until a successful connection is made."
197 :type '(choice string
200 (defcustom imap-shell-program '("ssh %s imapd"
202 "ssh %g ssh %s imapd"
203 "rsh %g rsh %s imapd")
204 "A list of strings, containing commands for IMAP connection.
205 Within a string, %s is replaced with the server address, %p with port
206 number on server, %g with `imap-shell-host', and %l with
207 `imap-default-user'. The program should read IMAP commands from stdin
208 and write IMAP response to stdout. Each entry in the list is tried
209 until a successful connection is made."
211 :type '(repeat string))
213 (defcustom imap-process-connection-type nil
214 "*Value for `process-connection-type' to use for Kerberos4, GSSAPI and SSL.
215 The `process-connection-type' variable control type of device
216 used to communicate with subprocesses. Values are nil to use a
217 pipe, or t or `pty' to use a pty. The value has no effect if the
218 system has no ptys or if all ptys are busy: then a pipe is used
219 in any case. The value takes effect when a IMAP server is
220 opened, changing it after that has no effect."
225 (defcustom imap-use-utf7 t
226 "If non-nil, do utf7 encoding/decoding of mailbox names.
227 Since the UTF7 decoding currently only decodes into ISO-8859-1
228 characters, you may disable this decoding if you need to access UTF7
229 encoded mailboxes which doesn't translate into ISO-8859-1."
233 (defcustom imap-log nil
234 "If non-nil, a imap session trace is placed in *imap-log* buffer.
235 Note that username, passwords and other privacy sensitive
236 information (such as e-mail) may be stored in the *imap-log*
237 buffer. It is not written to disk, however. Do not enable this
238 variable unless you are comfortable with that."
242 (defcustom imap-debug nil
243 "If non-nil, random debug spews are placed in *imap-debug* buffer.
244 Note that username, passwords and other privacy sensitive
245 information (such as e-mail) may be stored in the *imap-debug*
246 buffer. It is not written to disk, however. Do not enable this
247 variable unless you are comfortable with that."
251 (defcustom imap-shell-host "gateway"
252 "Hostname of rlogin proxy."
256 (defcustom imap-default-user (user-login-name)
257 "Default username to use."
261 (defcustom imap-read-timeout (if (string-match
262 "windows-nt\\|os/2\\|emx\\|cygwin"
263 (symbol-name system-type))
266 "*How long to wait between checking for the end of output.
267 Shorter values mean quicker response, but is more CPU intensive."
271 (defcustom imap-store-password nil
272 "If non-nil, store session password without promting."
276 ;; Various variables.
278 (defvar imap-fetch-data-hook nil
279 "Hooks called after receiving each FETCH response.")
281 (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell)
282 "Priority of streams to consider when opening connection to server.")
284 (defvar imap-stream-alist
285 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
286 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
287 (tls imap-tls-p imap-tls-open)
288 (ssl imap-ssl-p imap-ssl-open)
289 (network imap-network-p imap-network-open)
290 (shell imap-shell-p imap-shell-open)
291 (starttls imap-starttls-p imap-starttls-open))
292 "Definition of network streams.
296 NAME names the stream, CHECK is a function returning non-nil if the
297 server support the stream and OPEN is a function for opening the
300 (defvar imap-authenticators '(gssapi
307 "Priority of authenticators to consider when authenticating to server.")
309 (defvar imap-authenticator-alist
310 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
311 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
312 (sasl imap-sasl-auth-p imap-sasl-auth)
313 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
314 (login imap-login-p imap-login-auth)
315 (anonymous imap-anonymous-p imap-anonymous-auth)
316 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
317 "Definition of authenticators.
319 \(NAME CHECK AUTHENTICATE)
321 NAME names the authenticator. CHECK is a function returning non-nil if
322 the server support the authenticator and AUTHENTICATE is a function
323 for doing the actual authentication.")
325 (defvar imap-error nil
326 "Error codes from the last command.")
328 ;; Internal constants. Change these and die.
330 (defconst imap-default-port 143)
331 (defconst imap-default-ssl-port 993)
332 (defconst imap-default-tls-port 993)
333 (defconst imap-default-stream 'network)
334 (defconst imap-coding-system-for-read 'binary)
335 (defconst imap-coding-system-for-write 'binary)
336 (defconst imap-local-variables '(imap-server
345 imap-current-target-mailbox
355 imap-calculate-literal-size-first
357 (defconst imap-log-buffer "*imap-log*")
358 (defconst imap-debug-buffer "*imap-debug*")
360 ;; Internal variables.
362 (defvar imap-stream nil)
363 (defvar imap-auth nil)
364 (defvar imap-server nil)
365 (defvar imap-port nil)
366 (defvar imap-username nil)
367 (defvar imap-password nil)
368 (defvar imap-calculate-literal-size-first nil)
369 (defvar imap-state 'closed
371 Valid states are `closed', `initial', `nonauth', `auth', `selected'
374 (defvar imap-server-eol "\r\n"
375 "The EOL string sent from the server.")
377 (defvar imap-client-eol "\r\n"
378 "The EOL string we send to the server.")
380 (defvar imap-current-mailbox nil
381 "Current mailbox name.")
383 (defvar imap-current-target-mailbox nil
384 "Current target mailbox for COPY and APPEND commands.")
386 (defvar imap-mailbox-data nil
387 "Obarray with mailbox data.")
389 (defvar imap-mailbox-prime 997
390 "Length of imap-mailbox-data.")
392 (defvar imap-current-message nil
393 "Current message number.")
395 (defvar imap-message-data nil
396 "Obarray with message data.")
398 (defvar imap-message-prime 997
399 "Length of imap-message-data.")
401 (defvar imap-capability nil
402 "Capability for server.")
408 (defvar imap-namespace nil
409 "Namespace for current server.")
411 (defvar imap-reached-tag 0
412 "Lower limit on command tags that have been parsed.")
414 (defvar imap-failed-tags nil
415 "Alist of tags that failed.
416 Each element is a list with four elements; tag (a integer), response
417 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
418 human readable response text (a string).")
421 "Command tag number.")
423 (defvar imap-process nil
426 (defvar imap-continuation nil
427 "Non-nil indicates that the server emitted a continuation request.
428 The actual value is really the text on the continuation line.")
430 (defvar imap-callbacks nil
431 "List of response tags and callbacks, on the form `(number . function)'.
432 The function should take two arguments, the first the IMAP tag and the
433 second the status (OK, NO, BAD etc) of the command.")
436 ;; Utility functions:
438 (defun imap-remassoc (key alist)
439 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
440 The modified LIST is returned. If the first member
441 of LIST has a car that is `equal' to KEY, there is no way to remove it
442 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
443 sure of changing the value of `foo'."
445 (if (equal key (caar alist))
447 (setcdr alist (imap-remassoc key (cdr alist)))
450 (defsubst imap-disable-multibyte ()
451 "Enable multibyte in the current buffer."
452 (when (fboundp 'set-buffer-multibyte)
453 (set-buffer-multibyte nil)))
455 (defsubst imap-utf7-encode (string)
459 (utf7-encode string t)
461 "imap: Could not UTF7 encode `%s', using it unencoded..."
466 (defsubst imap-utf7-decode (string)
470 (utf7-decode string t)
472 "imap: Could not UTF7 decode `%s', using it undecoded..."
477 (defsubst imap-ok-p (status)
480 (setq imap-error status)
483 (defun imap-error-text (&optional buffer)
484 (with-current-buffer (or buffer (current-buffer))
485 (nth 3 (car imap-failed-tags))))
488 ;; Server functions; stream stuff:
490 (defun imap-kerberos4-stream-p (buffer)
491 (imap-capability 'AUTH=KERBEROS_V4 buffer))
493 (defun imap-kerberos4-open (name buffer server port)
494 (let ((cmds imap-kerberos4-program)
496 (while (and (not done) (setq cmd (pop cmds)))
497 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
499 (let* ((port (or port imap-default-port))
500 (coding-system-for-read imap-coding-system-for-read)
501 (coding-system-for-write imap-coding-system-for-write)
502 (process-connection-type imap-process-connection-type)
503 (process (start-process
504 name buffer shell-file-name shell-command-switch
509 ?p (number-to-string port)
510 ?l imap-default-user))))
513 (with-current-buffer buffer
514 (setq imap-client-eol "\n"
515 imap-calculate-literal-size-first t)
516 (while (and (memq (process-status process) '(open run))
517 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
518 (goto-char (point-min))
519 ;; Athena IMTEST can output SSL verify errors
520 (or (while (looking-at "^verify error:num=")
523 (or (while (looking-at "^TLS connection established")
526 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
527 (or (while (looking-at "^C:")
530 ;; cyrus 1.6 imtest print "S: " before server greeting
531 (or (not (looking-at "S: "))
534 (not (and (imap-parse-greeting)
535 ;; success in imtest < 1.6:
536 (or (re-search-forward
537 "^__\\(.*\\)__\n" nil t)
538 ;; success in imtest 1.6:
540 "^\\(Authenticat.*\\)" nil t))
541 (setq response (match-string 1)))))
542 (accept-process-output process 1)
545 (with-current-buffer (get-buffer-create imap-log-buffer)
546 (imap-disable-multibyte)
547 (buffer-disable-undo)
548 (goto-char (point-max))
549 (insert-buffer-substring buffer)))
551 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
552 (if response (concat "done, " response) "failed"))
553 (if (and response (let ((case-fold-search nil))
554 (not (string-match "failed" response))))
556 (if (memq (process-status process) '(open run))
557 (imap-send-command "LOGOUT"))
558 (delete-process process)
562 (defun imap-gssapi-stream-p (buffer)
563 (imap-capability 'AUTH=GSSAPI buffer))
565 (defun imap-gssapi-open (name buffer server port)
566 (let ((cmds imap-gssapi-program)
568 (while (and (not done) (setq cmd (pop cmds)))
569 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
571 (let* ((port (or port imap-default-port))
572 (coding-system-for-read imap-coding-system-for-read)
573 (coding-system-for-write imap-coding-system-for-write)
574 (process-connection-type imap-process-connection-type)
575 (process (start-process
576 name buffer shell-file-name shell-command-switch
581 ?p (number-to-string port)
582 ?l imap-default-user))))
585 (with-current-buffer buffer
586 (setq imap-client-eol "\n"
587 imap-calculate-literal-size-first t)
588 (while (and (memq (process-status process) '(open run))
589 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
590 (goto-char (point-min))
591 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
592 (or (while (looking-at "^C:")
595 ;; cyrus 1.6 imtest print "S: " before server greeting
596 (or (not (looking-at "S: "))
599 (not (and (imap-parse-greeting)
600 ;; success in imtest 1.6:
602 (concat "^\\(\\(Authenticat.*\\)\\|\\("
603 "Client authentication "
606 (setq response (match-string 1)))))
607 (accept-process-output process 1)
610 (with-current-buffer (get-buffer-create imap-log-buffer)
611 (imap-disable-multibyte)
612 (buffer-disable-undo)
613 (goto-char (point-max))
614 (insert-buffer-substring buffer)))
616 (message "GSSAPI IMAP connection: %s" (or response "failed"))
617 (if (and response (let ((case-fold-search nil))
618 (not (string-match "failed" response))))
620 (if (memq (process-status process) '(open run))
621 (imap-send-command "LOGOUT"))
622 (delete-process process)
626 (defun imap-ssl-p (buffer)
629 (defun imap-ssl-open (name buffer server port)
630 "Open a SSL connection to server."
631 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
632 (list imap-ssl-program)))
634 (while (and (not done) (setq cmd (pop cmds)))
635 (message "imap: Opening SSL connection with `%s'..." cmd)
637 (let* ((port (or port imap-default-ssl-port))
638 (coding-system-for-read imap-coding-system-for-read)
639 (coding-system-for-write imap-coding-system-for-write)
640 (process-connection-type imap-process-connection-type)
643 (setq process (start-process
644 name buffer shell-file-name
649 ?p (number-to-string port)))))
650 (process-kill-without-query process)
652 (with-current-buffer buffer
653 (goto-char (point-min))
654 (while (and (memq (process-status process) '(open run))
655 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
656 (goto-char (point-max))
658 (not (imap-parse-greeting)))
659 (accept-process-output process 1)
662 (with-current-buffer (get-buffer-create imap-log-buffer)
663 (imap-disable-multibyte)
664 (buffer-disable-undo)
665 (goto-char (point-max))
666 (insert-buffer-substring buffer)))
668 (when (memq (process-status process) '(open run))
669 (setq done process))))))
672 (message "imap: Opening SSL connection with `%s'...done" cmd)
674 (message "imap: Opening SSL connection with `%s'...failed" cmd)
677 (defun imap-tls-p (buffer)
680 (defun imap-tls-open (name buffer server port)
681 (let* ((port (or port imap-default-tls-port))
682 (coding-system-for-read imap-coding-system-for-read)
683 (coding-system-for-write imap-coding-system-for-write)
684 (process (open-tls-stream name buffer server port)))
686 (while (and (memq (process-status process) '(open run))
687 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
688 (goto-char (point-max))
690 (not (imap-parse-greeting)))
691 (accept-process-output process 1)
694 (with-current-buffer (get-buffer-create imap-log-buffer)
695 (imap-disable-multibyte)
696 (buffer-disable-undo)
697 (goto-char (point-max))
698 (insert-buffer-substring buffer)))
699 (when (memq (process-status process) '(open run))
702 (defun imap-network-p (buffer)
705 (defun imap-network-open (name buffer server port)
706 (let* ((port (or port imap-default-port))
707 (coding-system-for-read imap-coding-system-for-read)
708 (coding-system-for-write imap-coding-system-for-write)
709 (process (open-network-stream name buffer server port)))
711 (while (and (memq (process-status process) '(open run))
712 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
713 (goto-char (point-min))
714 (not (imap-parse-greeting)))
715 (accept-process-output process 1)
718 (with-current-buffer (get-buffer-create imap-log-buffer)
719 (imap-disable-multibyte)
720 (buffer-disable-undo)
721 (goto-char (point-max))
722 (insert-buffer-substring buffer)))
723 (when (memq (process-status process) '(open run))
726 (defun imap-shell-p (buffer)
729 (defun imap-shell-open (name buffer server port)
730 (let ((cmds (if (listp imap-shell-program) imap-shell-program
731 (list imap-shell-program)))
733 (while (and (not done) (setq cmd (pop cmds)))
734 (message "imap: Opening IMAP connection with `%s'..." cmd)
735 (setq imap-client-eol "\n")
736 (let* ((port (or port imap-default-port))
737 (coding-system-for-read imap-coding-system-for-read)
738 (coding-system-for-write imap-coding-system-for-write)
739 (process (start-process
740 name buffer shell-file-name shell-command-switch
746 ?p (number-to-string port)
747 ?l imap-default-user)))))
749 (while (and (memq (process-status process) '(open run))
750 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
751 (goto-char (point-max))
753 (not (imap-parse-greeting)))
754 (accept-process-output process 1)
757 (with-current-buffer (get-buffer-create imap-log-buffer)
758 (imap-disable-multibyte)
759 (buffer-disable-undo)
760 (goto-char (point-max))
761 (insert-buffer-substring buffer)))
763 (when (memq (process-status process) '(open run))
764 (setq done process)))))
767 (message "imap: Opening IMAP connection with `%s'...done" cmd)
769 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
772 (defun imap-starttls-p (buffer)
773 (imap-capability 'STARTTLS buffer))
775 (defun imap-starttls-open (name buffer server port)
776 (let* ((port (or port imap-default-port))
777 (coding-system-for-read imap-coding-system-for-read)
778 (coding-system-for-write imap-coding-system-for-write)
779 (process (starttls-open-stream name buffer server port))
781 (message "imap: Connecting with STARTTLS...")
783 (while (and (memq (process-status process) '(open run))
784 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
785 (goto-char (point-max))
787 (not (imap-parse-greeting)))
788 (accept-process-output process 1)
790 (imap-send-command "STARTTLS")
791 (while (and (memq (process-status process) '(open run))
792 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
793 (goto-char (point-max))
795 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
796 (accept-process-output process 1)
799 (with-current-buffer (get-buffer-create imap-log-buffer)
800 (buffer-disable-undo)
801 (goto-char (point-max))
802 (insert-buffer-substring buffer)))
803 (when (and (setq tls-info (starttls-negotiate process))
804 (memq (process-status process) '(open run)))
805 (setq done process)))
806 (if (stringp tls-info)
807 (message "imap: STARTTLS info: %s" tls-info))
808 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
811 ;; Server functions; authenticator stuff:
813 (defun imap-interactive-login (buffer loginfunc)
814 "Login to server in BUFFER.
815 LOGINFUNC is passed a username and a password, it should return t if
816 it where successful authenticating itself to the server, nil otherwise.
817 Returns t if login was successful, nil otherwise."
818 (with-current-buffer buffer
819 (make-local-variable 'imap-username)
820 (make-local-variable 'imap-password)
821 (let (user passwd ret)
822 ;; (condition-case ()
823 (while (or (not user) (not passwd))
824 (setq user (or imap-username
825 (read-from-minibuffer
826 (concat "IMAP username for " imap-server
827 " (using stream `" (symbol-name imap-stream)
829 (or user imap-default-user))))
830 (setq passwd (or imap-password
832 (concat "IMAP password for " user "@"
833 imap-server " (using authenticator `"
834 (symbol-name imap-auth) "'): "))))
835 (when (and user passwd)
836 (if (funcall loginfunc user passwd)
840 (when (and (not imap-password)
841 (or imap-store-password
842 (y-or-n-p "Store password for this session? ")))
843 (setq imap-password passwd)))
844 (message "Login failed...")
846 (setq imap-password nil)
848 ;; (quit (with-current-buffer buffer
851 ;; (error (with-current-buffer buffer
856 (defun imap-gssapi-auth-p (buffer)
857 (eq imap-stream 'gssapi))
859 (defun imap-gssapi-auth (buffer)
860 (message "imap: Authenticating using GSSAPI...%s"
861 (if (eq imap-stream 'gssapi) "done" "failed"))
862 (eq imap-stream 'gssapi))
864 (defun imap-kerberos4-auth-p (buffer)
865 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
866 (eq imap-stream 'kerberos4)))
868 (defun imap-kerberos4-auth (buffer)
869 (message "imap: Authenticating using Kerberos 4...%s"
870 (if (eq imap-stream 'kerberos4) "done" "failed"))
871 (eq imap-stream 'kerberos4))
873 (defun imap-cram-md5-p (buffer)
874 (imap-capability 'AUTH=CRAM-MD5 buffer))
876 (defun imap-cram-md5-auth (buffer)
877 "Login to server using the AUTH CRAM-MD5 method."
878 (message "imap: Authenticating using CRAM-MD5...")
879 (let ((done (imap-interactive-login
881 (lambda (user passwd)
883 (imap-send-command-wait
885 "AUTHENTICATE CRAM-MD5"
887 (let* ((decoded (base64-decode-string challenge))
888 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
889 (response (concat user " " hash))
890 (encoded (base64-encode-string response)))
893 (message "imap: Authenticating using CRAM-MD5...done")
894 (message "imap: Authenticating using CRAM-MD5...failed"))))
896 (defun imap-login-p (buffer)
897 (and (not (imap-capability 'LOGINDISABLED buffer))
898 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
900 (defun imap-login-auth (buffer)
901 "Login to server using the LOGIN command."
902 (message "imap: Plaintext authentication...")
903 (imap-interactive-login buffer
904 (lambda (user passwd)
905 (imap-ok-p (imap-send-command-wait
906 (concat "LOGIN \"" user "\" \""
909 (defun imap-anonymous-p (buffer)
912 (defun imap-anonymous-auth (buffer)
913 (message "imap: Logging in anonymously...")
914 (with-current-buffer buffer
915 (imap-ok-p (imap-send-command-wait
916 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
917 (system-name)) "\"")))))
919 ;;; Compiler directives.
921 (defvar imap-sasl-client)
922 (defvar imap-sasl-step)
924 (defun imap-sasl-make-mechanisms (buffer)
927 (let ((name (symbol-name sym)))
928 (if (and (> (length name) 5)
929 (string-equal "AUTH=" (substring name 0 5 )))
930 (setq mecs (cons (substring name 5) mecs)))))
931 (imap-capability nil buffer))
934 (defun imap-sasl-auth-p (buffer)
935 (and (condition-case ()
938 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
940 (defun imap-sasl-auth (buffer)
941 "Login to server using the SASL method."
942 (message "imap: Authenticating using SASL...")
943 (with-current-buffer buffer
944 (make-local-variable 'imap-username)
945 (make-local-variable 'imap-sasl-client)
946 (make-local-variable 'imap-sasl-step)
947 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
950 (setq user (or imap-username
951 (read-from-minibuffer
952 (concat "IMAP username for " imap-server " using SASL "
953 (sasl-mechanism-name mechanism) ": ")
954 (or user imap-default-user))))
956 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
957 imap-sasl-step (sasl-next-step imap-sasl-client nil))
958 (let ((tag (imap-send-command
959 (if (sasl-step-data imap-sasl-step)
960 (format "AUTHENTICATE %s %s"
961 (sasl-mechanism-name mechanism)
962 (sasl-step-data imap-sasl-step))
963 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
965 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
966 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
967 (setq imap-continuation nil
968 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
969 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
970 (base64-encode-string (sasl-step-data imap-sasl-step) t)
972 (if (imap-ok-p (imap-wait-for-tag tag))
973 (setq imap-username user
975 (message "Login failed...")
979 (defun imap-digest-md5-p (buffer)
980 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
982 (require 'digest-md5)
985 (defun imap-digest-md5-auth (buffer)
986 "Login to server using the AUTH DIGEST-MD5 method."
987 (message "imap: Authenticating using DIGEST-MD5...")
988 (imap-interactive-login
990 (lambda (user passwd)
994 "AUTHENTICATE DIGEST-MD5"
996 (digest-md5-parse-digest-challenge
997 (base64-decode-string challenge))
999 (digest-md5-digest-uri
1000 "imap" (digest-md5-challenge 'realm)))
1002 (digest-md5-digest-response
1003 user passwd digest-uri)))
1004 (base64-encode-string response 'no-line-break))))
1006 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1008 (setq imap-continuation nil)
1009 (imap-send-command-1 "")
1010 (imap-ok-p (imap-wait-for-tag tag)))))))
1012 ;; Server functions:
1014 (defun imap-open-1 (buffer)
1015 (with-current-buffer buffer
1017 (setq imap-current-mailbox nil
1018 imap-current-message nil
1020 imap-process (condition-case ()
1021 (funcall (nth 2 (assq imap-stream
1023 "imap" buffer imap-server imap-port)
1024 ((error quit) nil)))
1026 (set-process-filter imap-process 'imap-arrival-filter)
1027 (set-process-sentinel imap-process 'imap-sentinel)
1028 (while (and (eq imap-state 'initial)
1029 (memq (process-status imap-process) '(open run)))
1030 (message "Waiting for response from %s..." imap-server)
1031 (accept-process-output imap-process 1))
1032 (message "Waiting for response from %s...done" imap-server)
1033 (and (memq (process-status imap-process) '(open run))
1036 (defun imap-open (server &optional port stream auth buffer)
1037 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1038 If PORT is unspecified, a default value is used (143 except
1039 for SSL which use 993).
1040 STREAM indicates the stream to use, see `imap-streams' for available
1041 streams. If nil, it choices the best stream the server is capable of.
1042 AUTH indicates authenticator to use, see `imap-authenticators' for
1043 available authenticators. If nil, it choices the best stream the
1044 server is capable of.
1045 BUFFER can be a buffer or a name of a buffer, which is created if
1046 necessary. If nil, the buffer name is generated."
1047 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1048 (with-current-buffer (get-buffer-create buffer)
1049 (if (imap-opened buffer)
1050 (imap-close buffer))
1051 (mapcar 'make-local-variable imap-local-variables)
1052 (imap-disable-multibyte)
1053 (buffer-disable-undo)
1054 (setq imap-server (or server imap-server))
1055 (setq imap-port (or port imap-port))
1056 (setq imap-auth (or auth imap-auth))
1057 (setq imap-stream (or stream imap-stream))
1058 (message "imap: Connecting to %s..." imap-server)
1059 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1060 (imap-open-1 buffer)))
1062 (message "imap: Connecting to %s...failed" imap-server)
1064 (when (null imap-stream)
1065 ;; Need to choose stream.
1066 (let ((streams imap-streams))
1067 (while (setq stream (pop streams))
1068 ;; OK to use this stream?
1069 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1071 (if (not (eq imap-default-stream stream))
1072 (with-current-buffer (get-buffer-create
1073 (generate-new-buffer-name " *temp*"))
1074 (mapcar 'make-local-variable imap-local-variables)
1075 (imap-disable-multibyte)
1076 (buffer-disable-undo)
1077 (setq imap-server (or server imap-server))
1078 (setq imap-port (or port imap-port))
1079 (setq imap-auth (or auth imap-auth))
1080 (message "imap: Reconnecting with stream `%s'..." stream)
1081 (if (null (let ((imap-stream stream))
1082 (imap-open-1 (current-buffer))))
1084 (kill-buffer (current-buffer))
1086 "imap: Reconnecting with stream `%s'...failed"
1088 ;; We're done, kill the first connection
1090 (kill-buffer buffer)
1091 (rename-buffer buffer)
1092 (message "imap: Reconnecting with stream `%s'...done"
1094 (setq imap-stream stream)
1095 (setq imap-capability nil)
1096 (setq streams nil)))
1098 (message "imap: Connecting to %s...done" imap-server)
1099 (setq imap-stream stream)
1100 (setq imap-capability nil)
1101 (setq streams nil))))))
1102 (when (imap-opened buffer)
1103 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1107 (defun imap-opened (&optional buffer)
1108 "Return non-nil if connection to imap server in BUFFER is open.
1109 If BUFFER is nil then the current buffer is used."
1110 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1111 (buffer-live-p buffer)
1112 (with-current-buffer buffer
1114 (memq (process-status imap-process) '(open run))))))
1116 (defun imap-authenticate (&optional user passwd buffer)
1117 "Authenticate to server in BUFFER, using current buffer if nil.
1118 It uses the authenticator specified when opening the server. If the
1119 authenticator requires username/passwords, they are queried from the
1120 user and optionally stored in the buffer. If USER and/or PASSWD is
1121 specified, the user will not be questioned and the username and/or
1122 password is remembered in the buffer."
1123 (with-current-buffer (or buffer (current-buffer))
1124 (if (not (eq imap-state 'nonauth))
1125 (or (eq imap-state 'auth)
1126 (eq imap-state 'select)
1127 (eq imap-state 'examine))
1128 (make-local-variable 'imap-username)
1129 (make-local-variable 'imap-password)
1130 (if user (setq imap-username user))
1131 (if passwd (setq imap-password passwd))
1133 (and (funcall (nth 2 (assq imap-auth
1134 imap-authenticator-alist)) buffer)
1135 (setq imap-state 'auth))
1136 ;; Choose authenticator.
1137 (let ((auths imap-authenticators)
1139 (while (setq auth (pop auths))
1140 ;; OK to use authenticator?
1141 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1142 (message "imap: Authenticating to `%s' using `%s'..."
1144 (setq imap-auth auth)
1145 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1147 (message "imap: Authenticating to `%s' using `%s'...done"
1150 (message "imap: Authenticating to `%s' using `%s'...failed"
1151 imap-server auth)))))
1154 (defun imap-close (&optional buffer)
1155 "Close connection to server in BUFFER.
1156 If BUFFER is nil, the current buffer is used."
1157 (with-current-buffer (or buffer (current-buffer))
1160 (imap-send-command-wait "LOGOUT")
1162 (when (and imap-process
1163 (memq (process-status imap-process) '(open run)))
1164 (delete-process imap-process))
1165 (setq imap-current-mailbox nil
1166 imap-current-message nil
1171 (defun imap-capability (&optional identifier buffer)
1172 "Return a list of identifiers which server in BUFFER support.
1173 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1174 If BUFFER is nil, the current buffer is assumed."
1175 (with-current-buffer (or buffer (current-buffer))
1176 (unless imap-capability
1177 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1178 (setq imap-capability '(IMAP2))))
1180 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1183 (defun imap-id (&optional list-of-values buffer)
1184 "Identify client to server in BUFFER, and return server identity.
1185 LIST-OF-VALUES is nil, or a plist with identifier and value
1186 strings to send to the server to identify the client.
1188 Return a list of identifiers which server in BUFFER support, or
1189 nil if it doesn't support ID or returns no information.
1191 If BUFFER is nil, the current buffer is assumed."
1192 (with-current-buffer (or buffer (current-buffer))
1193 (when (and (imap-capability 'ID)
1194 (imap-ok-p (imap-send-command-wait
1195 (if (null list-of-values)
1197 (concat "ID (" (mapconcat (lambda (el)
1198 (concat "\"" el "\""))
1203 (defun imap-namespace (&optional buffer)
1204 "Return a namespace hierarchy at server in BUFFER.
1205 If BUFFER is nil, the current buffer is assumed."
1206 (with-current-buffer (or buffer (current-buffer))
1207 (unless imap-namespace
1208 (when (imap-capability 'NAMESPACE)
1209 (imap-send-command-wait "NAMESPACE")))
1212 (defun imap-send-command-wait (command &optional buffer)
1213 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1216 ;; Mailbox functions:
1218 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1219 (with-current-buffer (or buffer (current-buffer))
1220 (if imap-mailbox-data
1221 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1223 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1224 propname value mailbox (current-buffer)))
1227 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1228 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1231 (defun imap-mailbox-get (propname &optional mailbox buffer)
1232 (let ((mailbox (imap-utf7-encode mailbox)))
1233 (with-current-buffer (or buffer (current-buffer))
1234 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1236 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1237 (with-current-buffer (or buffer (current-buffer))
1241 (push (funcall func (if mailbox-decoder
1242 (funcall mailbox-decoder (symbol-name s))
1243 (symbol-name s))) result))
1247 (defun imap-mailbox-map (func &optional buffer)
1248 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1249 Function should take a mailbox name (a string) as
1251 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1253 (defun imap-current-mailbox (&optional buffer)
1254 (with-current-buffer (or buffer (current-buffer))
1255 (imap-utf7-decode imap-current-mailbox)))
1257 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1258 (and (string= mailbox imap-current-mailbox)
1260 (eq imap-state 'examine))
1262 (eq imap-state 'selected)))))
1264 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1265 (with-current-buffer (or buffer (current-buffer))
1266 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1268 (defun imap-mailbox-select-1 (mailbox &optional examine)
1269 "Select MAILBOX on server in BUFFER.
1270 If EXAMINE is non-nil, do a read-only select."
1271 (if (imap-current-mailbox-p-1 mailbox examine)
1272 imap-current-mailbox
1273 (setq imap-current-mailbox mailbox)
1274 (if (imap-ok-p (imap-send-command-wait
1275 (concat (if examine "EXAMINE" "SELECT") " \""
1278 (setq imap-message-data (make-vector imap-message-prime 0)
1279 imap-state (if examine 'examine 'selected))
1280 imap-current-mailbox)
1281 ;; Failed SELECT/EXAMINE unselects current mailbox
1282 (setq imap-current-mailbox nil))))
1284 (defun imap-mailbox-select (mailbox &optional examine buffer)
1285 (with-current-buffer (or buffer (current-buffer))
1287 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1289 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1290 (with-current-buffer (or buffer (current-buffer))
1291 (imap-mailbox-select-1 mailbox 'examine)))
1293 (defun imap-mailbox-examine (mailbox &optional buffer)
1294 "Examine MAILBOX on server in BUFFER."
1295 (imap-mailbox-select mailbox 'examine buffer))
1297 (defun imap-mailbox-unselect (&optional buffer)
1298 "Close current folder in BUFFER, without expunging articles."
1299 (with-current-buffer (or buffer (current-buffer))
1300 (when (or (eq imap-state 'auth)
1301 (and (imap-capability 'UNSELECT)
1302 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1304 (imap-send-command-wait (concat "EXAMINE \""
1305 imap-current-mailbox
1307 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1308 (setq imap-current-mailbox nil
1309 imap-message-data nil
1313 (defun imap-mailbox-expunge (&optional asynch buffer)
1314 "Expunge articles in 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 (and imap-current-mailbox (not (eq imap-state 'examine)))
1320 (imap-send-command "EXPUNGE")
1321 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1323 (defun imap-mailbox-close (&optional asynch buffer)
1324 "Expunge articles and close current folder in BUFFER.
1325 If ASYNCH, do not wait for succesful completion of the command.
1326 If BUFFER is nil the current buffer is assumed."
1327 (with-current-buffer (or buffer (current-buffer))
1328 (when imap-current-mailbox
1330 (imap-add-callback (imap-send-command "CLOSE")
1331 `(lambda (tag status)
1332 (message "IMAP mailbox `%s' closed... %s"
1333 imap-current-mailbox status)
1334 (when (eq ,imap-current-mailbox
1335 imap-current-mailbox)
1336 ;; Don't wipe out data if another mailbox
1338 (setq imap-current-mailbox nil
1339 imap-message-data nil
1340 imap-state 'auth))))
1341 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1342 (setq imap-current-mailbox nil
1343 imap-message-data nil
1347 (defun imap-mailbox-create-1 (mailbox)
1348 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1350 (defun imap-mailbox-create (mailbox &optional buffer)
1351 "Create MAILBOX on server in BUFFER.
1352 If BUFFER is nil the current buffer is assumed."
1353 (with-current-buffer (or buffer (current-buffer))
1354 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1356 (defun imap-mailbox-delete (mailbox &optional buffer)
1357 "Delete MAILBOX on server in BUFFER.
1358 If BUFFER is nil the current buffer is assumed."
1359 (let ((mailbox (imap-utf7-encode mailbox)))
1360 (with-current-buffer (or buffer (current-buffer))
1362 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1364 (defun imap-mailbox-rename (oldname newname &optional buffer)
1365 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1366 If BUFFER is nil the current buffer is assumed."
1367 (let ((oldname (imap-utf7-encode oldname))
1368 (newname (imap-utf7-encode newname)))
1369 (with-current-buffer (or buffer (current-buffer))
1371 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1372 "\"" newname "\""))))))
1374 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1375 "Return a list of subscribed mailboxes on server in BUFFER.
1376 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1377 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1378 implementation-specific string that has to be passed to lsub command."
1379 (with-current-buffer (or buffer (current-buffer))
1380 ;; Make sure we know the hierarchy separator for root's hierarchy
1381 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1382 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1383 (imap-utf7-encode root) "\"")))
1384 ;; clear list data (NB not delimiter and other stuff)
1385 (imap-mailbox-map-1 (lambda (mailbox)
1386 (imap-mailbox-put 'lsub nil mailbox)))
1388 (imap-send-command-wait
1389 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1390 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1393 (imap-mailbox-map-1 (lambda (mailbox)
1394 (when (imap-mailbox-get-1 'lsub mailbox)
1395 (push (imap-utf7-decode mailbox) out))))
1398 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1399 "Return a list of mailboxes matching ROOT on server in BUFFER.
1400 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1401 root. REFERENCE is a implementation-specific string that has to be
1402 passed to list command."
1403 (with-current-buffer (or buffer (current-buffer))
1404 ;; Make sure we know the hierarchy separator for root's hierarchy
1405 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1406 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1407 (imap-utf7-encode root) "\"")))
1408 ;; clear list data (NB not delimiter and other stuff)
1409 (imap-mailbox-map-1 (lambda (mailbox)
1410 (imap-mailbox-put 'list nil mailbox)))
1412 (imap-send-command-wait
1413 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1414 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1417 (imap-mailbox-map-1 (lambda (mailbox)
1418 (when (imap-mailbox-get-1 'list mailbox)
1419 (push (imap-utf7-decode mailbox) out))))
1422 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1423 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1424 Returns non-nil if successful."
1425 (with-current-buffer (or buffer (current-buffer))
1426 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1427 (imap-utf7-encode mailbox)
1430 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1431 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1432 Returns non-nil if successful."
1433 (with-current-buffer (or buffer (current-buffer))
1434 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1435 (imap-utf7-encode mailbox)
1438 (defun imap-mailbox-status (mailbox items &optional buffer)
1439 "Get status items ITEM in MAILBOX from server in BUFFER.
1440 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1441 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1442 or 'unseen. If ITEMS is a list of symbols, a list of values is
1443 returned, if ITEMS is a symbol only its value is returned."
1444 (with-current-buffer (or buffer (current-buffer))
1446 (imap-send-command-wait (list "STATUS \""
1447 (imap-utf7-encode mailbox)
1455 (mapcar (lambda (item)
1456 (imap-mailbox-get item mailbox))
1458 (imap-mailbox-get items mailbox)))))
1460 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1461 "Send status item request ITEM on MAILBOX to server in BUFFER.
1462 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1463 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1464 or 'unseen. The IMAP command tag is returned."
1465 (with-current-buffer (or buffer (current-buffer))
1466 (imap-send-command (list "STATUS \""
1467 (imap-utf7-encode mailbox)
1474 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1475 "Get ACL on 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 "GETACL \""
1480 (or mailbox imap-current-mailbox)
1482 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1484 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1485 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1486 (let ((mailbox (imap-utf7-encode mailbox)))
1487 (with-current-buffer (or buffer (current-buffer))
1489 (imap-send-command-wait (list "SETACL \""
1490 (or mailbox imap-current-mailbox)
1496 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1497 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1498 (let ((mailbox (imap-utf7-encode mailbox)))
1499 (with-current-buffer (or buffer (current-buffer))
1501 (imap-send-command-wait (list "DELETEACL \""
1502 (or mailbox imap-current-mailbox)
1507 ;; Message functions:
1509 (defun imap-current-message (&optional buffer)
1510 (with-current-buffer (or buffer (current-buffer))
1511 imap-current-message))
1513 (defun imap-list-to-message-set (list)
1514 (mapconcat (lambda (item)
1515 (number-to-string item))
1521 (defun imap-range-to-message-set (range)
1526 (car item) (cdr item))
1527 (format "%d" item)))
1528 (if (and (listp range) (not (listp (cdr range))))
1529 (list range) ;; make (1 . 2) into ((1 . 2))
1533 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1534 (with-current-buffer (or buffer (current-buffer))
1535 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1537 (imap-list-to-message-set uids)
1541 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1542 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1543 UIDS can be a string, number or a list of numbers. If RECEIVE
1544 is non-nil return these properties."
1545 (with-current-buffer (or buffer (current-buffer))
1546 (when (imap-ok-p (imap-send-command-wait
1547 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1549 (imap-list-to-message-set uids)
1552 (if (or (null receive) (stringp uids))
1555 (mapcar (lambda (uid)
1557 (mapcar (lambda (prop)
1558 (imap-message-get uid prop))
1560 (imap-message-get uid receive)))
1562 (imap-message-get uids receive))))))
1564 (defun imap-message-put (uid propname value &optional buffer)
1565 (with-current-buffer (or buffer (current-buffer))
1566 (if imap-message-data
1567 (put (intern (number-to-string uid) imap-message-data)
1569 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1570 uid propname value (current-buffer)))
1573 (defun imap-message-get (uid propname &optional buffer)
1574 (with-current-buffer (or buffer (current-buffer))
1575 (get (intern-soft (number-to-string uid) imap-message-data)
1578 (defun imap-message-map (func propname &optional buffer)
1579 "Map a function across each mailbox in `imap-message-data', returning a list."
1580 (with-current-buffer (or buffer (current-buffer))
1584 (push (funcall func (get s 'UID) (get s propname)) result))
1588 (defmacro imap-message-envelope-date (uid &optional buffer)
1589 `(with-current-buffer (or ,buffer (current-buffer))
1590 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1592 (defmacro imap-message-envelope-subject (uid &optional buffer)
1593 `(with-current-buffer (or ,buffer (current-buffer))
1594 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1596 (defmacro imap-message-envelope-from (uid &optional buffer)
1597 `(with-current-buffer (or ,buffer (current-buffer))
1598 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1600 (defmacro imap-message-envelope-sender (uid &optional buffer)
1601 `(with-current-buffer (or ,buffer (current-buffer))
1602 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1604 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1605 `(with-current-buffer (or ,buffer (current-buffer))
1606 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1608 (defmacro imap-message-envelope-to (uid &optional buffer)
1609 `(with-current-buffer (or ,buffer (current-buffer))
1610 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1612 (defmacro imap-message-envelope-cc (uid &optional buffer)
1613 `(with-current-buffer (or ,buffer (current-buffer))
1614 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1616 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1617 `(with-current-buffer (or ,buffer (current-buffer))
1618 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1620 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1621 `(with-current-buffer (or ,buffer (current-buffer))
1622 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1624 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1625 `(with-current-buffer (or ,buffer (current-buffer))
1626 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1628 (defmacro imap-message-body (uid &optional buffer)
1629 `(with-current-buffer (or ,buffer (current-buffer))
1630 (imap-message-get ,uid 'BODY)))
1632 (defun imap-search (predicate &optional buffer)
1633 (with-current-buffer (or buffer (current-buffer))
1634 (imap-mailbox-put 'search 'dummy)
1635 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1636 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1638 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1640 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1642 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1643 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1644 (with-current-buffer (or buffer (current-buffer))
1645 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1646 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1648 (defun imap-message-flags-set (articles flags &optional silent buffer)
1649 (when (and articles flags)
1650 (with-current-buffer (or buffer (current-buffer))
1651 (imap-ok-p (imap-send-command-wait
1652 (concat "UID STORE " articles
1653 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1655 (defun imap-message-flags-del (articles flags &optional silent buffer)
1656 (when (and articles flags)
1657 (with-current-buffer (or buffer (current-buffer))
1658 (imap-ok-p (imap-send-command-wait
1659 (concat "UID STORE " articles
1660 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1662 (defun imap-message-flags-add (articles flags &optional silent buffer)
1663 (when (and articles flags)
1664 (with-current-buffer (or buffer (current-buffer))
1665 (imap-ok-p (imap-send-command-wait
1666 (concat "UID STORE " articles
1667 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1669 (defun imap-message-copyuid-1 (mailbox)
1670 (if (imap-capability 'UIDPLUS)
1671 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1672 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1673 (let ((old-mailbox imap-current-mailbox)
1675 (imap-message-data (make-vector 2 0)))
1676 (when (imap-mailbox-examine-1 mailbox)
1678 (and (imap-fetch "*" "UID")
1679 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1680 (apply 'max (imap-message-map
1681 (lambda (uid prop) uid) 'UID))))
1683 (imap-mailbox-select old-mailbox (eq state 'examine))
1684 (imap-mailbox-unselect)))))))
1686 (defun imap-message-copyuid (mailbox &optional buffer)
1687 (with-current-buffer (or buffer (current-buffer))
1688 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1690 (defun imap-message-copy (articles mailbox
1691 &optional dont-create no-copyuid buffer)
1692 "Copy ARTICLES (a string message set) to MAILBOX on server in
1693 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1694 non-nil, it will not create a mailbox. On success, return a list with
1695 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1696 first element, rest of list contain the saved articles' UIDs."
1698 (with-current-buffer (or buffer (current-buffer))
1699 (let ((mailbox (imap-utf7-encode mailbox)))
1700 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1701 (imap-current-target-mailbox mailbox))
1702 (if (imap-ok-p (imap-send-command-wait cmd))
1704 (when (and (not dont-create)
1705 ;; removed because of buggy Oracle server
1706 ;; that doesn't send TRYCREATE tags (which
1707 ;; is a MUST according to specifications):
1708 ;;(imap-mailbox-get-1 'trycreate mailbox)
1709 (imap-mailbox-create-1 mailbox))
1710 (imap-ok-p (imap-send-command-wait cmd)))))
1712 (imap-message-copyuid-1 mailbox)))))))
1714 (defun imap-message-appenduid-1 (mailbox)
1715 (if (imap-capability 'UIDPLUS)
1716 (imap-mailbox-get-1 'appenduid mailbox)
1717 (let ((old-mailbox imap-current-mailbox)
1719 (imap-message-data (make-vector 2 0)))
1720 (when (imap-mailbox-examine-1 mailbox)
1722 (and (imap-fetch "*" "UID")
1723 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1724 (apply 'max (imap-message-map
1725 (lambda (uid prop) uid) 'UID))))
1727 (imap-mailbox-select old-mailbox (eq state 'examine))
1728 (imap-mailbox-unselect)))))))
1730 (defun imap-message-appenduid (mailbox &optional buffer)
1731 (with-current-buffer (or buffer (current-buffer))
1732 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1734 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1735 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1736 FLAGS and DATE-TIME is currently not used. Return a cons holding
1737 uidvalidity of MAILBOX and UID the newly created article got, or nil
1739 (let ((mailbox (imap-utf7-encode mailbox)))
1740 (with-current-buffer (or buffer (current-buffer))
1741 (and (let ((imap-current-target-mailbox mailbox))
1743 (imap-send-command-wait
1744 (list "APPEND \"" mailbox "\" " article))))
1745 (imap-message-appenduid-1 mailbox)))))
1747 (defun imap-body-lines (body)
1748 "Return number of lines in article by looking at the mime bodystructure BODY."
1750 (if (stringp (car body))
1751 (cond ((and (string= (upcase (car body)) "TEXT")
1752 (numberp (nth 7 body)))
1754 ((and (string= (upcase (car body)) "MESSAGE")
1755 (numberp (nth 9 body)))
1758 (apply '+ (mapcar 'imap-body-lines body)))
1761 (defun imap-envelope-from (from)
1762 "Return a from string line."
1764 (concat (aref from 0)
1765 (if (aref from 0) " <")
1769 (if (aref from 0) ">"))))
1772 ;; Internal functions.
1774 (defun imap-add-callback (tag func)
1775 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1777 (defun imap-send-command-1 (cmdstr)
1778 (setq cmdstr (concat cmdstr imap-client-eol))
1780 (with-current-buffer (get-buffer-create imap-log-buffer)
1781 (imap-disable-multibyte)
1782 (buffer-disable-undo)
1783 (goto-char (point-max))
1785 (process-send-string imap-process cmdstr))
1787 (defun imap-send-command (command &optional buffer)
1788 (with-current-buffer (or buffer (current-buffer))
1789 (if (not (listp command)) (setq command (list command)))
1790 (let ((tag (setq imap-tag (1+ imap-tag)))
1792 (setq cmdstr (concat (number-to-string imap-tag) " "))
1793 (while (setq cmd (pop command))
1794 (cond ((stringp cmd)
1795 (setq cmdstr (concat cmdstr cmd)))
1797 (let ((eol imap-client-eol)
1798 (calcfirst imap-calculate-literal-size-first)
1800 (with-current-buffer cmd
1802 (setq size (buffer-size)))
1803 (when (not (equal eol "\r\n"))
1804 ;; XXX modifies buffer!
1805 (goto-char (point-min))
1806 (while (search-forward "\r\n" nil t)
1807 (replace-match eol)))
1809 (setq size (buffer-size))))
1811 (concat cmdstr (format "{%d}" size))))
1814 (imap-send-command-1 cmdstr)
1816 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1817 (setq command nil) ;; abort command if no cont-req
1818 (let ((process imap-process)
1819 (stream imap-stream)
1820 (eol imap-client-eol))
1821 (with-current-buffer cmd
1823 (with-current-buffer (get-buffer-create
1825 (imap-disable-multibyte)
1826 (buffer-disable-undo)
1827 (goto-char (point-max))
1828 (insert-buffer-substring cmd)))
1829 (process-send-region process (point-min)
1831 (process-send-string process imap-client-eol))))
1832 (setq imap-continuation nil)))
1834 (imap-send-command-1 cmdstr)
1837 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1838 (setq command nil) ;; abort command if no cont-req
1839 (setq command (cons (funcall cmd imap-continuation)
1841 (setq imap-continuation nil)))
1843 (error "Unknown command type"))))
1845 (imap-send-command-1 cmdstr))
1848 (defun imap-wait-for-tag (tag &optional buffer)
1849 (with-current-buffer (or buffer (current-buffer))
1850 (let (imap-have-messaged)
1851 (while (and (null imap-continuation)
1852 (memq (process-status imap-process) '(open run))
1853 (< imap-reached-tag tag))
1854 (let ((len (/ (point-max) 1024))
1857 (setq imap-have-messaged t)
1858 (message "imap read: %dk" len))
1859 (accept-process-output imap-process
1860 (truncate imap-read-timeout)
1861 (truncate (* (- imap-read-timeout
1862 (truncate imap-read-timeout))
1864 ;; A process can die _before_ we have processed everything it
1865 ;; has to say. Moreover, this can happen in between the call to
1866 ;; accept-process-output and the call to process-status in an
1867 ;; iteration of the loop above.
1868 (when (and (null imap-continuation)
1869 (< imap-reached-tag tag))
1870 (accept-process-output imap-process 0 0))
1871 (when imap-have-messaged
1873 (and (memq (process-status imap-process) '(open run))
1874 (or (assq tag imap-failed-tags)
1875 (if imap-continuation
1879 (defun imap-sentinel (process string)
1880 (delete-process process))
1882 (defun imap-find-next-line ()
1883 "Return point at end of current line, taking into account literals.
1884 Return nil if no complete line has arrived."
1885 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1888 (if (match-string 1)
1889 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1891 (goto-char (+ (point) (string-to-number (match-string 1))))
1892 (imap-find-next-line))
1895 (defun imap-arrival-filter (proc string)
1896 "IMAP process filter."
1897 ;; Sometimes, we are called even though the process has died.
1898 ;; Better abstain from doing stuff in that case.
1899 (when (buffer-name (process-buffer proc))
1900 (with-current-buffer (process-buffer proc)
1901 (goto-char (point-max))
1904 (with-current-buffer (get-buffer-create imap-log-buffer)
1905 (imap-disable-multibyte)
1906 (buffer-disable-undo)
1907 (goto-char (point-max))
1910 (goto-char (point-min))
1911 (while (setq end (imap-find-next-line))
1913 (narrow-to-region (point-min) end)
1914 (delete-backward-char (length imap-server-eol))
1915 (goto-char (point-min))
1917 (cond ((eq imap-state 'initial)
1918 (imap-parse-greeting))
1919 ((or (eq imap-state 'auth)
1920 (eq imap-state 'nonauth)
1921 (eq imap-state 'selected)
1922 (eq imap-state 'examine))
1923 (imap-parse-response))
1925 (message "Unknown state %s in arrival filter"
1927 (delete-region (point-min) (point-max)))))))))
1932 (defsubst imap-forward ()
1933 (or (eobp) (forward-char)))
1936 ;; ; Unsigned 32-bit integer
1937 ;; ; (0 <= n < 4,294,967,296)
1939 (defsubst imap-parse-number ()
1940 (when (looking-at "[0-9]+")
1942 (string-to-number (match-string 0))
1943 (goto-char (match-end 0)))))
1945 ;; literal = "{" number "}" CRLF *CHAR8
1946 ;; ; Number represents the number of CHAR8s
1948 (defsubst imap-parse-literal ()
1949 (when (looking-at "{\\([0-9]+\\)}\r\n")
1950 (let ((pos (match-end 0))
1951 (len (string-to-number (match-string 1))))
1952 (if (< (point-max) (+ pos len))
1954 (goto-char (+ pos len))
1955 (buffer-substring pos (+ pos len))))))
1957 ;; string = quoted / literal
1959 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1961 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1962 ;; "\" quoted-specials
1964 ;; quoted-specials = DQUOTE / "\"
1966 ;; TEXT-CHAR = <any CHAR except CR and LF>
1968 (defsubst imap-parse-string ()
1969 (cond ((eq (char-after) ?\")
1971 (let ((p (point)) (name ""))
1972 (skip-chars-forward "^\"\\\\")
1973 (setq name (buffer-substring p (point)))
1974 (while (eq (char-after) ?\\)
1975 (setq p (1+ (point)))
1977 (skip-chars-forward "^\"\\\\")
1978 (setq name (concat name (buffer-substring p (point)))))
1981 ((eq (char-after) ?{)
1982 (imap-parse-literal))))
1986 (defsubst imap-parse-nil ()
1987 (if (looking-at "NIL")
1988 (goto-char (match-end 0))))
1990 ;; nstring = string / nil
1992 (defsubst imap-parse-nstring ()
1993 (or (imap-parse-string)
1994 (and (imap-parse-nil)
1997 ;; astring = atom / string
1999 ;; atom = 1*ATOM-CHAR
2001 ;; ATOM-CHAR = <any CHAR except atom-specials>
2003 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2006 ;; list-wildcards = "%" / "*"
2008 ;; quoted-specials = DQUOTE / "\"
2010 (defsubst imap-parse-astring ()
2011 (or (imap-parse-string)
2012 (buffer-substring (point)
2013 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2014 (goto-char (1- (match-end 0)))
2018 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2021 ;; addr-adl = nstring
2022 ;; ; Holds route from [RFC-822] route-addr if
2025 ;; addr-host = nstring
2026 ;; ; nil indicates [RFC-822] group syntax.
2027 ;; ; Otherwise, holds [RFC-822] domain name
2029 ;; addr-mailbox = nstring
2030 ;; ; nil indicates end of [RFC-822] group; if
2031 ;; ; non-nil and addr-host is nil, holds
2032 ;; ; [RFC-822] group name.
2033 ;; ; Otherwise, holds [RFC-822] local-part
2034 ;; ; after removing [RFC-822] quoting
2036 ;; addr-name = nstring
2037 ;; ; If non-nil, holds phrase from [RFC-822]
2038 ;; ; mailbox after removing [RFC-822] quoting
2041 (defsubst imap-parse-address ()
2043 (when (eq (char-after) ?\()
2045 (setq address (vector (prog1 (imap-parse-nstring)
2047 (prog1 (imap-parse-nstring)
2049 (prog1 (imap-parse-nstring)
2051 (imap-parse-nstring)))
2052 (when (eq (char-after) ?\))
2056 ;; address-list = "(" 1*address ")" / nil
2060 (defsubst imap-parse-address-list ()
2061 (if (eq (char-after) ?\()
2062 (let (address addresses)
2064 (while (and (not (eq (char-after) ?\)))
2065 ;; next line for MS Exchange bug
2066 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2067 (setq address (imap-parse-address)))
2068 (setq addresses (cons address addresses)))
2069 (when (eq (char-after) ?\))
2071 (nreverse addresses)))
2072 ;; With assert, the code might not be eval'd.
2073 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2076 ;; mailbox = "INBOX" / astring
2077 ;; ; INBOX is case-insensitive. All case variants of
2078 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2079 ;; ; not as an astring. An astring which consists of
2080 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2081 ;; ; is considered to be INBOX and not an astring.
2082 ;; ; Refer to section 5.1 for further
2083 ;; ; semantic details of mailbox names.
2085 (defsubst imap-parse-mailbox ()
2086 (let ((mailbox (imap-parse-astring)))
2087 (if (string-equal "INBOX" (upcase mailbox))
2091 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2093 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2094 ;; ; Authentication condition
2096 ;; resp-cond-bye = "BYE" SP resp-text
2098 (defun imap-parse-greeting ()
2099 "Parse a IMAP greeting."
2100 (cond ((looking-at "\\* OK ")
2101 (setq imap-state 'nonauth))
2102 ((looking-at "\\* PREAUTH ")
2103 (setq imap-state 'auth))
2104 ((looking-at "\\* BYE ")
2105 (setq imap-state 'closed))))
2107 ;; response = *(continue-req / response-data) response-done
2109 ;; continue-req = "+" SP (resp-text / base64) CRLF
2111 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2112 ;; mailbox-data / message-data / capability-data) CRLF
2114 ;; response-done = response-tagged / response-fatal
2116 ;; response-fatal = "*" SP resp-cond-bye CRLF
2117 ;; ; Server closes connection immediately
2119 ;; response-tagged = tag SP resp-cond-state CRLF
2121 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2122 ;; ; Status condition
2124 ;; resp-cond-bye = "BYE" SP resp-text
2126 ;; mailbox-data = "FLAGS" SP flag-list /
2127 ;; "LIST" SP mailbox-list /
2128 ;; "LSUB" SP mailbox-list /
2129 ;; "SEARCH" *(SP nz-number) /
2130 ;; "STATUS" SP mailbox SP "("
2131 ;; [status-att SP number *(SP status-att SP number)] ")" /
2132 ;; number SP "EXISTS" /
2133 ;; number SP "RECENT"
2135 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2137 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2139 ;; ; IMAP4rev1 servers which offer RFC 1730
2140 ;; ; compatibility MUST list "IMAP4" as the first
2143 (defun imap-parse-response ()
2144 "Parse a IMAP command response."
2146 (case (setq token (read (current-buffer)))
2147 (+ (setq imap-continuation
2148 (or (buffer-substring (min (point-max) (1+ (point)))
2151 (* (case (prog1 (setq token (read (current-buffer)))
2153 (OK (imap-parse-resp-text))
2154 (NO (imap-parse-resp-text))
2155 (BAD (imap-parse-resp-text))
2156 (BYE (imap-parse-resp-text))
2157 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2158 (LIST (imap-parse-data-list 'list))
2159 (LSUB (imap-parse-data-list 'lsub))
2160 (SEARCH (imap-mailbox-put
2162 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2163 (STATUS (imap-parse-status))
2164 (CAPABILITY (setq imap-capability
2165 (read (concat "(" (upcase (buffer-substring
2166 (point) (point-max)))
2168 (ID (setq imap-id (read (buffer-substring (point)
2170 (ACL (imap-parse-acl))
2171 (t (case (prog1 (read (current-buffer))
2173 (EXISTS (imap-mailbox-put 'exists token))
2174 (RECENT (imap-mailbox-put 'recent token))
2176 (FETCH (imap-parse-fetch token))
2177 (t (message "Garbage: %s" (buffer-string)))))))
2179 (if (not (integerp token))
2180 (message "Garbage: %s" (buffer-string))
2181 (case (prog1 (setq status (read (current-buffer)))
2184 (setq imap-reached-tag (max imap-reached-tag token))
2185 (imap-parse-resp-text)))
2187 (setq imap-reached-tag (max imap-reached-tag token))
2189 (imap-parse-resp-text))
2191 (when (eq (char-after) ?\[)
2192 (setq code (buffer-substring (point)
2193 (search-forward "]")))
2195 (setq text (buffer-substring (point) (point-max)))
2196 (push (list token status code text)
2197 imap-failed-tags))))
2199 (setq imap-reached-tag (max imap-reached-tag token))
2201 (imap-parse-resp-text))
2203 (when (eq (char-after) ?\[)
2204 (setq code (buffer-substring (point)
2205 (search-forward "]")))
2207 (setq text (buffer-substring (point) (point-max)))
2208 (push (list token status code text) imap-failed-tags)
2209 (error "Internal error, tag %s status %s code %s text %s"
2210 token status code text))))
2211 (t (message "Garbage: %s" (buffer-string))))
2212 (when (assq token imap-callbacks)
2213 (funcall (cdr (assq token imap-callbacks)) token status)
2214 (setq imap-callbacks
2215 (imap-remassoc token imap-callbacks)))))))))
2217 ;; resp-text = ["[" resp-text-code "]" SP] text
2219 ;; text = 1*TEXT-CHAR
2221 ;; TEXT-CHAR = <any CHAR except CR and LF>
2223 (defun imap-parse-resp-text ()
2224 (imap-parse-resp-text-code))
2226 ;; resp-text-code = "ALERT" /
2227 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2228 ;; "NEWNAME" SP string SP string /
2230 ;; "PERMANENTFLAGS" SP "("
2231 ;; [flag-perm *(SP flag-perm)] ")" /
2235 ;; "UIDNEXT" SP nz-number /
2236 ;; "UIDVALIDITY" SP nz-number /
2237 ;; "UNSEEN" SP nz-number /
2238 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2240 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2242 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2244 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2246 ;; ; Identifies a set of messages. For message
2247 ;; ; sequence numbers, these are consecutive
2248 ;; ; numbers from 1 to the number of messages in
2250 ;; ; Comma delimits individual numbers, colon
2251 ;; ; delimits between two numbers inclusive.
2252 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2253 ;; ; 14,15 for a mailbox with 15 messages.
2255 ;; sequence-num = nz-number / "*"
2256 ;; ; * is the largest number in use. For message
2257 ;; ; sequence numbers, it is the number of messages
2258 ;; ; in the mailbox. For unique identifiers, it is
2259 ;; ; the unique identifier of the last message in
2262 ;; flag-perm = flag / "\*"
2264 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2265 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2266 ;; ; Does not include "\Recent"
2268 ;; flag-extension = "\" atom
2269 ;; ; Future expansion. Client implementations
2270 ;; ; MUST accept flag-extension flags. Server
2271 ;; ; implementations MUST NOT generate
2272 ;; ; flag-extension flags except as defined by
2273 ;; ; future standard or standards-track
2274 ;; ; revisions of this specification.
2276 ;; flag-keyword = atom
2278 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2280 (defun imap-parse-resp-text-code ()
2281 ;; xxx next line for stalker communigate pro 3.3.1 bug
2282 (when (looking-at " \\[")
2284 (when (eq (char-after) ?\[)
2286 (cond ((search-forward "PERMANENTFLAGS " nil t)
2287 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2288 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2289 (imap-mailbox-put 'uidnext (match-string 1)))
2290 ((search-forward "UNSEEN " nil t)
2291 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2292 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2293 (imap-mailbox-put 'uidvalidity (match-string 1)))
2294 ((search-forward "READ-ONLY" nil t)
2295 (imap-mailbox-put 'read-only t))
2296 ((search-forward "NEWNAME " nil t)
2297 (let (oldname newname)
2298 (setq oldname (imap-parse-string))
2300 (setq newname (imap-parse-string))
2301 (imap-mailbox-put 'newname newname oldname)))
2302 ((search-forward "TRYCREATE" nil t)
2303 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2304 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2305 (imap-mailbox-put 'appenduid
2306 (list (match-string 1)
2307 (string-to-number (match-string 2)))
2308 imap-current-target-mailbox))
2309 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2310 (imap-mailbox-put 'copyuid (list (match-string 1)
2313 imap-current-target-mailbox))
2314 ((search-forward "ALERT] " nil t)
2315 (message "Imap server %s information: %s" imap-server
2316 (buffer-substring (point) (point-max)))))))
2318 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2319 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2321 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2322 ;; *(SP mbx-list-oflag) /
2323 ;; mbx-list-oflag *(SP mbx-list-oflag)
2325 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2326 ;; ; Other flags; multiple possible per LIST response
2328 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2329 ;; ; Selectability flags; only one per LIST response
2331 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2332 ;; "\" quoted-specials
2334 ;; quoted-specials = DQUOTE / "\"
2336 (defun imap-parse-data-list (type)
2337 (let (flags delimiter mailbox)
2338 (setq flags (imap-parse-flag-list))
2339 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2340 (setq delimiter (match-string 1))
2341 (goto-char (1+ (match-end 0)))
2342 (when (setq mailbox (imap-parse-mailbox))
2343 (imap-mailbox-put type t mailbox)
2344 (imap-mailbox-put 'list-flags flags mailbox)
2345 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2347 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2348 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2349 ;; "INTERNALDATE" SPACE date_time /
2350 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2351 ;; "RFC822.SIZE" SPACE number /
2352 ;; "BODY" ["STRUCTURE"] SPACE body /
2353 ;; "BODY" section ["<" number ">"] SPACE nstring /
2354 ;; "UID" SPACE uniqueid) ")"
2356 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2357 ;; SPACE time SPACE zone <">
2359 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2360 ;; ["." (section_text / "MIME")])] "]"
2362 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2363 ;; SPACE header_list / "TEXT"
2365 ;; header_fld_name ::= astring
2367 ;; header_list ::= "(" 1#header_fld_name ")"
2369 (defsubst imap-parse-header-list ()
2370 (when (eq (char-after) ?\()
2372 (while (not (eq (char-after) ?\)))
2374 (push (imap-parse-astring) strlist))
2376 (nreverse strlist))))
2378 (defsubst imap-parse-fetch-body-section ()
2380 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2381 (if (eq (char-before) ? )
2383 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2384 (search-forward "]" nil t))
2387 (defun imap-parse-fetch (response)
2388 (when (eq (char-after) ?\()
2389 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2390 rfc822size body bodydetail bodystructure flags-empty)
2391 (while (not (eq (char-after) ?\)))
2393 (let ((token (read (current-buffer))))
2395 (cond ((eq token 'UID)
2396 (setq uid (condition-case ()
2397 (read (current-buffer))
2400 (setq flags (imap-parse-flag-list))
2402 (setq flags-empty 't)))
2403 ((eq token 'ENVELOPE)
2404 (setq envelope (imap-parse-envelope)))
2405 ((eq token 'INTERNALDATE)
2406 (setq internaldate (imap-parse-string)))
2408 (setq rfc822 (imap-parse-nstring)))
2409 ((eq token 'RFC822.HEADER)
2410 (setq rfc822header (imap-parse-nstring)))
2411 ((eq token 'RFC822.TEXT)
2412 (setq rfc822text (imap-parse-nstring)))
2413 ((eq token 'RFC822.SIZE)
2414 (setq rfc822size (read (current-buffer))))
2416 (if (eq (char-before) ?\[)
2418 (upcase (imap-parse-fetch-body-section))
2419 (and (eq (char-after) ?<)
2420 (buffer-substring (1+ (point))
2421 (search-forward ">" nil t)))
2422 (progn (imap-forward)
2423 (imap-parse-nstring)))
2425 (setq body (imap-parse-body))))
2426 ((eq token 'BODYSTRUCTURE)
2427 (setq bodystructure (imap-parse-body))))))
2429 (setq imap-current-message uid)
2430 (imap-message-put uid 'UID uid)
2431 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2432 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2433 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2434 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2435 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2436 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2437 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2438 (and body (imap-message-put uid 'BODY body))
2439 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2440 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2441 (run-hooks 'imap-fetch-data-hook)))))
2443 ;; mailbox-data = ...
2444 ;; "STATUS" SP mailbox SP "("
2445 ;; [status-att SP number
2446 ;; *(SP status-att SP number)] ")"
2449 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2452 (defun imap-parse-status ()
2453 (let ((mailbox (imap-parse-mailbox)))
2454 (if (eq (char-after) ? )
2456 (when (and mailbox (eq (char-after) ?\())
2457 (while (and (not (eq (char-after) ?\)))
2458 (or (forward-char) t)
2459 (looking-at "\\([A-Za-z]+\\) "))
2460 (let ((token (match-string 1)))
2461 (goto-char (match-end 0))
2462 (cond ((string= token "MESSAGES")
2463 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2464 ((string= token "RECENT")
2465 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2466 ((string= token "UIDNEXT")
2467 (and (looking-at "[0-9]+")
2468 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2469 (goto-char (match-end 0))))
2470 ((string= token "UIDVALIDITY")
2471 (and (looking-at "[0-9]+")
2472 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2473 (goto-char (match-end 0))))
2474 ((string= token "UNSEEN")
2475 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2477 (message "Unknown status data %s in mailbox %s ignored"
2479 (read (current-buffer)))))))))
2481 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2484 ;; identifier ::= astring
2486 ;; rights ::= astring
2488 (defun imap-parse-acl ()
2489 (let ((mailbox (imap-parse-mailbox))
2490 identifier rights acl)
2491 (while (eq (char-after) ?\ )
2493 (setq identifier (imap-parse-astring))
2495 (setq rights (imap-parse-astring))
2496 (setq acl (append acl (list (cons identifier rights)))))
2497 (imap-mailbox-put 'acl acl mailbox)))
2499 ;; flag-list = "(" [flag *(SP flag)] ")"
2501 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2502 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2503 ;; ; Does not include "\Recent"
2505 ;; flag-keyword = atom
2507 ;; flag-extension = "\" atom
2508 ;; ; Future expansion. Client implementations
2509 ;; ; MUST accept flag-extension flags. Server
2510 ;; ; implementations MUST NOT generate
2511 ;; ; flag-extension flags except as defined by
2512 ;; ; future standard or standards-track
2513 ;; ; revisions of this specification.
2515 (defun imap-parse-flag-list ()
2516 (let (flag-list start)
2517 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2518 (while (and (not (eq (char-after) ?\)))
2521 ;; next line for Courier IMAP bug.
2522 (skip-chars-forward " ")
2524 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2525 (push (buffer-substring start (point)) flag-list))
2526 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2528 (nreverse flag-list)))
2530 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2531 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2532 ;; env-in-reply-to SP env-message-id ")"
2534 ;; env-bcc = "(" 1*address ")" / nil
2536 ;; env-cc = "(" 1*address ")" / nil
2538 ;; env-date = nstring
2540 ;; env-from = "(" 1*address ")" / nil
2542 ;; env-in-reply-to = nstring
2544 ;; env-message-id = nstring
2546 ;; env-reply-to = "(" 1*address ")" / nil
2548 ;; env-sender = "(" 1*address ")" / nil
2550 ;; env-subject = nstring
2552 ;; env-to = "(" 1*address ")" / nil
2554 (defun imap-parse-envelope ()
2555 (when (eq (char-after) ?\()
2557 (vector (prog1 (imap-parse-nstring) ;; date
2559 (prog1 (imap-parse-nstring) ;; subject
2561 (prog1 (imap-parse-address-list) ;; from
2563 (prog1 (imap-parse-address-list) ;; sender
2565 (prog1 (imap-parse-address-list) ;; reply-to
2567 (prog1 (imap-parse-address-list) ;; to
2569 (prog1 (imap-parse-address-list) ;; cc
2571 (prog1 (imap-parse-address-list) ;; bcc
2573 (prog1 (imap-parse-nstring) ;; in-reply-to
2575 (prog1 (imap-parse-nstring) ;; message-id
2578 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2580 (defsubst imap-parse-string-list ()
2581 (cond ((eq (char-after) ?\() ;; body-fld-param
2584 (while (setq str (imap-parse-string))
2586 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2587 ;; between body-fld-param's sometimes
2588 (or (eq (char-after) ?\")
2590 (nreverse strlist)))
2594 ;; body-extension = nstring / number /
2595 ;; "(" body-extension *(SP body-extension) ")"
2596 ;; ; Future expansion. Client implementations
2597 ;; ; MUST accept body-extension fields. Server
2598 ;; ; implementations MUST NOT generate
2599 ;; ; body-extension fields except as defined by
2600 ;; ; future standard or standards-track
2601 ;; ; revisions of this specification.
2603 (defun imap-parse-body-extension ()
2604 (if (eq (char-after) ?\()
2607 (push (imap-parse-body-extension) b-e)
2608 (while (eq (char-after) ?\ )
2610 (push (imap-parse-body-extension) b-e))
2611 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2614 (or (imap-parse-number)
2615 (imap-parse-nstring))))
2617 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2618 ;; *(SP body-extension)]]
2619 ;; ; MUST NOT be returned on non-extensible
2622 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2623 ;; *(SP body-extension)]]
2624 ;; ; MUST NOT be returned on non-extensible
2627 (defsubst imap-parse-body-ext ()
2629 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2632 (if (eq (char-after) ?\()
2635 (push (imap-parse-string) dsp)
2637 (push (imap-parse-string-list) dsp)
2639 ;; With assert, the code might not be eval'd.
2640 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2642 (push (nreverse dsp) ext))
2643 (when (eq (char-after) ?\ ) ;; body-fld-lang
2645 (if (eq (char-after) ?\()
2646 (push (imap-parse-string-list) ext)
2647 (push (imap-parse-nstring) ext))
2648 (while (eq (char-after) ?\ ) ;; body-extension
2650 (setq ext (append (imap-parse-body-extension) ext)))))
2653 ;; body = "(" body-type-1part / body-type-mpart ")"
2655 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2656 ;; *(SP body-extension)]]
2657 ;; ; MUST NOT be returned on non-extensible
2660 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2661 ;; *(SP body-extension)]]
2662 ;; ; MUST NOT be returned on non-extensible
2665 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2666 ;; body-fld-enc SP body-fld-octets
2668 ;; body-fld-desc = nstring
2670 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2672 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2673 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2675 ;; body-fld-id = nstring
2677 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2679 ;; body-fld-lines = number
2681 ;; body-fld-md5 = nstring
2683 ;; body-fld-octets = number
2685 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2687 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2688 ;; [SP body-ext-1part]
2690 ;; body-type-basic = media-basic SP body-fields
2691 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2693 ;; body-type-msg = media-message SP body-fields SP envelope
2694 ;; SP body SP body-fld-lines
2696 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2698 ;; body-type-mpart = 1*body SP media-subtype
2699 ;; [SP body-ext-mpart]
2701 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2702 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2703 ;; ; Defined in [MIME-IMT]
2705 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2706 ;; ; Defined in [MIME-IMT]
2708 ;; media-subtype = string
2709 ;; ; Defined in [MIME-IMT]
2711 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2712 ;; ; Defined in [MIME-IMT]
2714 (defun imap-parse-body ()
2716 (when (eq (char-after) ?\()
2718 (if (eq (char-after) ?\()
2720 (while (and (eq (char-after) ?\()
2721 (setq subbody (imap-parse-body)))
2722 ;; buggy stalker communigate pro 3.0 insert a SPC between
2723 ;; parts in multiparts
2724 (when (and (eq (char-after) ?\ )
2725 (eq (char-after (1+ (point))) ?\())
2727 (push subbody body))
2729 (push (imap-parse-string) body) ;; media-subtype
2730 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2732 (if (eq (char-after) ?\() ;; body-fld-param
2733 (push (imap-parse-string-list) body)
2734 (push (and (imap-parse-nil) nil) body))
2736 (append (imap-parse-body-ext) body))) ;; body-ext-...
2737 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2741 (push (imap-parse-string) body) ;; media-type
2743 (push (imap-parse-string) body) ;; media-subtype
2745 ;; next line for Sun SIMS bug
2746 (and (eq (char-after) ? ) (imap-forward))
2747 (if (eq (char-after) ?\() ;; body-fld-param
2748 (push (imap-parse-string-list) body)
2749 (push (and (imap-parse-nil) nil) body))
2751 (push (imap-parse-nstring) body) ;; body-fld-id
2753 (push (imap-parse-nstring) body) ;; body-fld-desc
2755 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2756 ;; nstring and return nil instead of defaulting back to 7BIT
2757 ;; as the standard says.
2758 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2760 (push (imap-parse-number) body) ;; body-fld-octets
2762 ;; ok, we're done parsing the required parts, what comes now is one
2765 ;; envelope (then we're parsing body-type-msg)
2766 ;; body-fld-lines (then we're parsing body-type-text)
2767 ;; body-ext-1part (then we're parsing body-type-basic)
2769 ;; the problem is that the two first are in turn optionally followed
2770 ;; by the third. So we parse the first two here (if there are any)...
2772 (when (eq (char-after) ?\ )
2775 (cond ((eq (char-after) ?\() ;; body-type-msg:
2776 (push (imap-parse-envelope) body) ;; envelope
2778 (push (imap-parse-body) body) ;; body
2779 ;; buggy stalker communigate pro 3.0 doesn't print
2780 ;; number of lines in message/rfc822 attachment
2781 (if (eq (char-after) ?\))
2784 (push (imap-parse-number) body))) ;; body-fld-lines
2785 ((setq lines (imap-parse-number)) ;; body-type-text:
2786 (push lines body)) ;; body-fld-lines
2788 (backward-char))))) ;; no match...
2790 ;; ...and then parse the third one here...
2792 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2794 (push (imap-parse-nstring) body) ;; body-fld-md5
2795 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2797 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2801 (when imap-debug ; (untrace-all)
2803 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2804 (mapcar (lambda (f) (trace-function-background f imap-debug-buffer))
2815 imap-interactive-login
2831 imap-send-command-wait
2836 imap-current-mailbox
2837 imap-current-mailbox-p-1
2838 imap-current-mailbox-p
2839 imap-mailbox-select-1
2841 imap-mailbox-examine-1
2842 imap-mailbox-examine
2843 imap-mailbox-unselect
2844 imap-mailbox-expunge
2846 imap-mailbox-create-1
2852 imap-mailbox-subscribe
2853 imap-mailbox-unsubscribe
2855 imap-mailbox-acl-get
2856 imap-mailbox-acl-set
2857 imap-mailbox-acl-delete
2858 imap-current-message
2859 imap-list-to-message-set
2866 imap-message-flag-permanent-p
2867 imap-message-flags-set
2868 imap-message-flags-del
2869 imap-message-flags-add
2870 imap-message-copyuid-1
2871 imap-message-copyuid
2873 imap-message-appenduid-1
2874 imap-message-appenduid
2886 imap-parse-resp-text
2887 imap-parse-resp-text-code
2888 imap-parse-data-list
2892 imap-parse-flag-list
2894 imap-parse-body-extension
2900 ;;; arch-tag: 27369ed6-33e4-482f-96f1-8bb906ba70f7
2901 ;;; imap.el ends here