1 ;;; imap.el --- imap library
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <jas@pdc.kth.se>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; imap.el is a elisp library providing an interface for talking to
31 ;; imap.el is roughly divided in two parts, one that parses IMAP
32 ;; responses from the server and storing data into buffer-local
33 ;; variables, and one for utility functions which send commands to
34 ;; server, waits for an answer, and return information. The latter
35 ;; part is layered on top of the previous.
37 ;; The imap.el API consist of the following functions, other functions
38 ;; in this file should not be called directly and the result of doing
39 ;; so are at best undefined.
43 ;; imap-open, imap-opened, imap-authenticate, imap-close,
44 ;; imap-capability, imap-namespace, imap-error-text
48 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
49 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
50 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
51 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
52 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
53 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
54 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
58 ;; imap-fetch-asynch, imap-fetch,
59 ;; imap-current-message, imap-list-to-message-set,
60 ;; imap-message-get, imap-message-map
61 ;; imap-message-envelope-date, imap-message-envelope-subject,
62 ;; imap-message-envelope-from, imap-message-envelope-sender,
63 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
64 ;; imap-message-envelope-cc, imap-message-envelope-bcc
65 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
66 ;; imap-message-body, imap-message-flag-permanent-p
67 ;; imap-message-flags-set, imap-message-flags-del
68 ;; imap-message-flags-add, imap-message-copyuid
69 ;; imap-message-copy, imap-message-appenduid
70 ;; imap-message-append, imap-envelope-from
73 ;; It is my hope that these commands should be pretty self
74 ;; explanatory for someone that know IMAP. All functions have
75 ;; additional documentation on how to invoke them.
77 ;; imap.el support RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1), implemented
78 ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
79 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
80 ;; LOGINDISABLED) (with use of external library starttls.el and
81 ;; program starttls), and the GSSAPI / kerberos V4 sections of RFC1731
82 ;; (with use of external program `imtest'), RFC2971 (ID). It also
83 ;; takes advantage of the UNSELECT extension in Cyrus IMAPD.
85 ;; Without the work of John McClary Prevost and Jim Radford this library
86 ;; would not have seen the light of day. Many thanks.
88 ;; This is a transcript of short interactive session for demonstration
91 ;; (imap-open "my.mail.server")
92 ;; => " *imap* my.mail.server:0"
94 ;; The rest are invoked with current buffer as the buffer returned by
95 ;; `imap-open'. It is possible to do all without this, but it would
96 ;; look ugly here since `buffer' is always the last argument for all
97 ;; imap.el API functions.
99 ;; (imap-authenticate "myusername" "mypassword")
102 ;; (imap-mailbox-lsub "*")
103 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
105 ;; (imap-mailbox-list "INBOX.n%")
106 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
108 ;; (imap-mailbox-select "INBOX.nnimap")
111 ;; (imap-mailbox-get 'exists)
114 ;; (imap-mailbox-get 'uidvalidity)
117 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
120 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
121 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
125 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
126 ;; o Don't use `read' at all (important places already fixed)
127 ;; o Accept list of articles instead of message set string in most
128 ;; imap-message-* functions.
129 ;; o Send strings as literal if they contain, e.g., ".
133 ;; - 19991218 added starttls/digest-md5 patch,
134 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
135 ;; NB! you need SLIM for starttls.el and digest-md5.el
136 ;; - 19991023 commited to pgnus
141 (eval-when-compile (require 'cl))
143 (autoload 'starttls-open-stream "starttls")
144 (autoload 'starttls-negotiate "starttls")
145 (autoload 'sasl-find-mechanism "sasl")
146 (autoload 'digest-md5-parse-digest-challenge "digest-md5")
147 (autoload 'digest-md5-digest-response "digest-md5")
148 (autoload 'digest-md5-digest-uri "digest-md5")
149 (autoload 'digest-md5-challenge "digest-md5")
150 (autoload 'rfc2104-hash "rfc2104")
151 (autoload 'utf7-encode "utf7")
152 (autoload 'utf7-decode "utf7")
153 (autoload 'format-spec "format-spec")
154 (autoload 'format-spec-make "format-spec")
155 (autoload 'open-tls-stream "tls"))
160 "Low-level IMAP issues."
164 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
166 "List of strings containing commands for Kerberos 4 authentication.
167 %s is replaced with server hostname, %p with port to connect to, and
168 %l with the value of `imap-default-user'. The program should accept
169 IMAP commands on stdin and return responses to stdout. Each entry in
170 the list is tried until a successful connection is made."
172 :type '(repeat string))
174 (defcustom imap-gssapi-program (list
175 (concat "gsasl %s %p "
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 (defvar imap-logout-timeout nil
329 "Close server immediately if it can't logout in this number of seconds.
330 If it is nil, never close server until logout completes. Normally,
331 the value of this variable will be bound to a certain value to which
332 an application program that uses this module specifies on a per-server
335 ;; Internal constants. Change these and die.
337 (defconst imap-default-port 143)
338 (defconst imap-default-ssl-port 993)
339 (defconst imap-default-tls-port 993)
340 (defconst imap-default-stream 'network)
341 (defconst imap-coding-system-for-read 'binary)
342 (defconst imap-coding-system-for-write 'binary)
343 (defconst imap-local-variables '(imap-server
352 imap-current-target-mailbox
362 imap-calculate-literal-size-first
364 (defconst imap-log-buffer "*imap-log*")
365 (defconst imap-debug-buffer "*imap-debug*")
367 ;; Internal variables.
369 (defvar imap-stream nil)
370 (defvar imap-auth nil)
371 (defvar imap-server nil)
372 (defvar imap-port nil)
373 (defvar imap-username nil)
374 (defvar imap-password nil)
375 (defvar imap-calculate-literal-size-first nil)
376 (defvar imap-state 'closed
378 Valid states are `closed', `initial', `nonauth', `auth', `selected'
381 (defvar imap-server-eol "\r\n"
382 "The EOL string sent from the server.")
384 (defvar imap-client-eol "\r\n"
385 "The EOL string we send to the server.")
387 (defvar imap-current-mailbox nil
388 "Current mailbox name.")
390 (defvar imap-current-target-mailbox nil
391 "Current target mailbox for COPY and APPEND commands.")
393 (defvar imap-mailbox-data nil
394 "Obarray with mailbox data.")
396 (defvar imap-mailbox-prime 997
397 "Length of imap-mailbox-data.")
399 (defvar imap-current-message nil
400 "Current message number.")
402 (defvar imap-message-data nil
403 "Obarray with message data.")
405 (defvar imap-message-prime 997
406 "Length of imap-message-data.")
408 (defvar imap-capability nil
409 "Capability for server.")
415 (defvar imap-namespace nil
416 "Namespace for current server.")
418 (defvar imap-reached-tag 0
419 "Lower limit on command tags that have been parsed.")
421 (defvar imap-failed-tags nil
422 "Alist of tags that failed.
423 Each element is a list with four elements; tag (a integer), response
424 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
425 human readable response text (a string).")
428 "Command tag number.")
430 (defvar imap-process nil
433 (defvar imap-continuation nil
434 "Non-nil indicates that the server emitted a continuation request.
435 The actual value is really the text on the continuation line.")
437 (defvar imap-callbacks nil
438 "List of response tags and callbacks, on the form `(number . function)'.
439 The function should take two arguments, the first the IMAP tag and the
440 second the status (OK, NO, BAD etc) of the command.")
443 ;; Utility functions:
445 (defun imap-remassoc (key alist)
446 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
447 The modified LIST is returned. If the first member
448 of LIST has a car that is `equal' to KEY, there is no way to remove it
449 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
450 sure of changing the value of `foo'."
452 (if (equal key (caar alist))
454 (setcdr alist (imap-remassoc key (cdr alist)))
457 (defsubst imap-disable-multibyte ()
458 "Enable multibyte in the current buffer."
459 (when (fboundp 'set-buffer-multibyte)
460 (set-buffer-multibyte nil)))
462 (defsubst imap-utf7-encode (string)
466 (utf7-encode string t)
468 "imap: Could not UTF7 encode `%s', using it unencoded..."
473 (defsubst imap-utf7-decode (string)
477 (utf7-decode string t)
479 "imap: Could not UTF7 decode `%s', using it undecoded..."
484 (defsubst imap-ok-p (status)
487 (setq imap-error status)
490 (defun imap-error-text (&optional buffer)
491 (with-current-buffer (or buffer (current-buffer))
492 (nth 3 (car imap-failed-tags))))
495 ;; Server functions; stream stuff:
497 (defun imap-kerberos4-stream-p (buffer)
498 (imap-capability 'AUTH=KERBEROS_V4 buffer))
500 (defun imap-kerberos4-open (name buffer server port)
501 (let ((cmds imap-kerberos4-program)
503 (while (and (not done) (setq cmd (pop cmds)))
504 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
506 (let* ((port (or port imap-default-port))
507 (coding-system-for-read imap-coding-system-for-read)
508 (coding-system-for-write imap-coding-system-for-write)
509 (process-connection-type imap-process-connection-type)
510 (process (start-process
511 name buffer shell-file-name shell-command-switch
516 ?p (number-to-string port)
517 ?l imap-default-user))))
520 (with-current-buffer buffer
521 (setq imap-client-eol "\n"
522 imap-calculate-literal-size-first t)
523 (while (and (memq (process-status process) '(open run))
524 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
525 (goto-char (point-min))
526 ;; Athena IMTEST can output SSL verify errors
527 (or (while (looking-at "^verify error:num=")
530 (or (while (looking-at "^TLS connection established")
533 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
534 (or (while (looking-at "^C:")
537 ;; cyrus 1.6 imtest print "S: " before server greeting
538 (or (not (looking-at "S: "))
541 (not (and (imap-parse-greeting)
542 ;; success in imtest < 1.6:
543 (or (re-search-forward
544 "^__\\(.*\\)__\n" nil t)
545 ;; success in imtest 1.6:
547 "^\\(Authenticat.*\\)" nil t))
548 (setq response (match-string 1)))))
549 (accept-process-output process 1)
552 (with-current-buffer (get-buffer-create imap-log-buffer)
553 (imap-disable-multibyte)
554 (buffer-disable-undo)
555 (goto-char (point-max))
556 (insert-buffer-substring buffer)))
558 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
559 (if response (concat "done, " response) "failed"))
560 (if (and response (let ((case-fold-search nil))
561 (not (string-match "failed" response))))
563 (if (memq (process-status process) '(open run))
565 (delete-process process)
569 (defun imap-gssapi-stream-p (buffer)
570 (imap-capability 'AUTH=GSSAPI buffer))
572 (defun imap-gssapi-open (name buffer server port)
573 (let ((cmds imap-gssapi-program)
575 (while (and (not done) (setq cmd (pop cmds)))
576 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
578 (let* ((port (or port imap-default-port))
579 (coding-system-for-read imap-coding-system-for-read)
580 (coding-system-for-write imap-coding-system-for-write)
581 (process-connection-type imap-process-connection-type)
582 (process (start-process
583 name buffer shell-file-name shell-command-switch
588 ?p (number-to-string port)
589 ?l imap-default-user))))
592 (with-current-buffer buffer
593 (setq imap-client-eol "\n"
594 imap-calculate-literal-size-first t)
595 (while (and (memq (process-status process) '(open run))
596 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
597 (goto-char (point-min))
598 ;; Athena IMTEST can output SSL verify errors
599 (or (while (looking-at "^verify error:num=")
602 (or (while (looking-at "^TLS connection established")
605 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
606 (or (while (looking-at "^C:")
609 ;; cyrus 1.6 imtest print "S: " before server greeting
610 (or (not (looking-at "S: "))
613 ;; GNU SASL may print 'Trying ...' first.
614 (or (not (looking-at "Trying "))
617 (not (and (imap-parse-greeting)
618 ;; success in imtest 1.6:
620 (concat "^\\(\\(Authenticat.*\\)\\|\\("
621 "Client authentication "
624 (setq response (match-string 1)))))
625 (accept-process-output process 1)
628 (with-current-buffer (get-buffer-create imap-log-buffer)
629 (imap-disable-multibyte)
630 (buffer-disable-undo)
631 (goto-char (point-max))
632 (insert-buffer-substring buffer)))
634 (message "GSSAPI IMAP connection: %s" (or response "failed"))
635 (if (and response (let ((case-fold-search nil))
636 (not (string-match "failed" response))))
638 (if (memq (process-status process) '(open run))
640 (delete-process process)
644 (defun imap-ssl-p (buffer)
647 (defun imap-ssl-open (name buffer server port)
648 "Open a SSL connection to server."
649 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
650 (list imap-ssl-program)))
652 (while (and (not done) (setq cmd (pop cmds)))
653 (message "imap: Opening SSL connection with `%s'..." cmd)
655 (let* ((port (or port imap-default-ssl-port))
656 (coding-system-for-read imap-coding-system-for-read)
657 (coding-system-for-write imap-coding-system-for-write)
658 (process-connection-type imap-process-connection-type)
659 (set-process-query-on-exit-flag
660 (if (fboundp 'set-process-query-on-exit-flag)
661 'set-process-query-on-exit-flag
662 'process-kill-without-query))
665 (setq process (start-process
666 name buffer shell-file-name
671 ?p (number-to-string port)))))
672 (funcall set-process-query-on-exit-flag process nil)
674 (with-current-buffer buffer
675 (goto-char (point-min))
676 (while (and (memq (process-status process) '(open run))
677 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
678 (goto-char (point-max))
680 (not (imap-parse-greeting)))
681 (accept-process-output process 1)
684 (with-current-buffer (get-buffer-create imap-log-buffer)
685 (imap-disable-multibyte)
686 (buffer-disable-undo)
687 (goto-char (point-max))
688 (insert-buffer-substring buffer)))
690 (when (memq (process-status process) '(open run))
691 (setq done process))))))
694 (message "imap: Opening SSL connection with `%s'...done" cmd)
696 (message "imap: Opening SSL connection with `%s'...failed" cmd)
699 (defun imap-tls-p (buffer)
702 (defun imap-tls-open (name buffer server port)
703 (let* ((port (or port imap-default-tls-port))
704 (coding-system-for-read imap-coding-system-for-read)
705 (coding-system-for-write imap-coding-system-for-write)
706 (process (open-tls-stream name buffer server port)))
708 (while (and (memq (process-status process) '(open run))
709 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
710 (goto-char (point-max))
712 (not (imap-parse-greeting)))
713 (accept-process-output process 1)
716 (with-current-buffer (get-buffer-create imap-log-buffer)
717 (imap-disable-multibyte)
718 (buffer-disable-undo)
719 (goto-char (point-max))
720 (insert-buffer-substring buffer)))
721 (when (memq (process-status process) '(open run))
724 (defun imap-network-p (buffer)
727 (defun imap-network-open (name buffer server port)
728 (let* ((port (or port imap-default-port))
729 (coding-system-for-read imap-coding-system-for-read)
730 (coding-system-for-write imap-coding-system-for-write)
731 (process (open-network-stream name buffer server port)))
733 (while (and (memq (process-status process) '(open run))
734 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
735 (goto-char (point-min))
736 (not (imap-parse-greeting)))
737 (accept-process-output process 1)
740 (with-current-buffer (get-buffer-create imap-log-buffer)
741 (imap-disable-multibyte)
742 (buffer-disable-undo)
743 (goto-char (point-max))
744 (insert-buffer-substring buffer)))
745 (when (memq (process-status process) '(open run))
748 (defun imap-shell-p (buffer)
751 (defun imap-shell-open (name buffer server port)
752 (let ((cmds (if (listp imap-shell-program) imap-shell-program
753 (list imap-shell-program)))
755 (while (and (not done) (setq cmd (pop cmds)))
756 (message "imap: Opening IMAP connection with `%s'..." cmd)
757 (setq imap-client-eol "\n")
758 (let* ((port (or port imap-default-port))
759 (coding-system-for-read imap-coding-system-for-read)
760 (coding-system-for-write imap-coding-system-for-write)
761 (process (start-process
762 name buffer shell-file-name shell-command-switch
768 ?p (number-to-string port)
769 ?l imap-default-user)))))
771 (while (and (memq (process-status process) '(open run))
772 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
773 (goto-char (point-max))
775 (not (imap-parse-greeting)))
776 (accept-process-output process 1)
779 (with-current-buffer (get-buffer-create imap-log-buffer)
780 (imap-disable-multibyte)
781 (buffer-disable-undo)
782 (goto-char (point-max))
783 (insert-buffer-substring buffer)))
785 (when (memq (process-status process) '(open run))
786 (setq done process)))))
789 (message "imap: Opening IMAP connection with `%s'...done" cmd)
791 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
794 (defun imap-starttls-p (buffer)
795 (imap-capability 'STARTTLS buffer))
797 (defun imap-starttls-open (name buffer server port)
798 (let* ((port (or port imap-default-port))
799 (coding-system-for-read imap-coding-system-for-read)
800 (coding-system-for-write imap-coding-system-for-write)
801 (process (starttls-open-stream name buffer server port))
803 (message "imap: Connecting with STARTTLS...")
805 (while (and (memq (process-status process) '(open run))
806 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
807 (goto-char (point-max))
809 (not (imap-parse-greeting)))
810 (accept-process-output process 1)
812 (imap-send-command "STARTTLS")
813 (while (and (memq (process-status process) '(open run))
814 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
815 (goto-char (point-max))
817 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
818 (accept-process-output process 1)
821 (with-current-buffer (get-buffer-create imap-log-buffer)
822 (buffer-disable-undo)
823 (goto-char (point-max))
824 (insert-buffer-substring buffer)))
825 (when (and (setq tls-info (starttls-negotiate process))
826 (memq (process-status process) '(open run)))
827 (setq done process)))
828 (if (stringp tls-info)
829 (message "imap: STARTTLS info: %s" tls-info))
830 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
833 ;; Server functions; authenticator stuff:
835 (defun imap-interactive-login (buffer loginfunc)
836 "Login to server in BUFFER.
837 LOGINFUNC is passed a username and a password, it should return t if
838 it where successful authenticating itself to the server, nil otherwise.
839 Returns t if login was successful, nil otherwise."
840 (with-current-buffer buffer
841 (make-local-variable 'imap-username)
842 (make-local-variable 'imap-password)
843 (let (user passwd ret)
844 ;; (condition-case ()
845 (while (or (not user) (not passwd))
846 (setq user (or imap-username
847 (read-from-minibuffer
848 (concat "IMAP username for " imap-server
849 " (using stream `" (symbol-name imap-stream)
851 (or user imap-default-user))))
852 (setq passwd (or imap-password
854 (concat "IMAP password for " user "@"
855 imap-server " (using authenticator `"
856 (symbol-name imap-auth) "'): "))))
857 (when (and user passwd)
858 (if (funcall loginfunc user passwd)
862 (when (and (not imap-password)
863 (or imap-store-password
864 (y-or-n-p "Store password for this session? ")))
865 (setq imap-password passwd)))
866 (message "Login failed...")
868 (setq imap-password nil)
870 ;; (quit (with-current-buffer buffer
873 ;; (error (with-current-buffer buffer
878 (defun imap-gssapi-auth-p (buffer)
879 (eq imap-stream 'gssapi))
881 (defun imap-gssapi-auth (buffer)
882 (message "imap: Authenticating using GSSAPI...%s"
883 (if (eq imap-stream 'gssapi) "done" "failed"))
884 (eq imap-stream 'gssapi))
886 (defun imap-kerberos4-auth-p (buffer)
887 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
888 (eq imap-stream 'kerberos4)))
890 (defun imap-kerberos4-auth (buffer)
891 (message "imap: Authenticating using Kerberos 4...%s"
892 (if (eq imap-stream 'kerberos4) "done" "failed"))
893 (eq imap-stream 'kerberos4))
895 (defun imap-cram-md5-p (buffer)
896 (imap-capability 'AUTH=CRAM-MD5 buffer))
898 (defun imap-cram-md5-auth (buffer)
899 "Login to server using the AUTH CRAM-MD5 method."
900 (message "imap: Authenticating using CRAM-MD5...")
901 (let ((done (imap-interactive-login
903 (lambda (user passwd)
905 (imap-send-command-wait
907 "AUTHENTICATE CRAM-MD5"
909 (let* ((decoded (base64-decode-string challenge))
910 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
911 (response (concat user " " hash))
912 (encoded (base64-encode-string response)))
915 (message "imap: Authenticating using CRAM-MD5...done")
916 (message "imap: Authenticating using CRAM-MD5...failed"))))
918 (defun imap-login-p (buffer)
919 (and (not (imap-capability 'LOGINDISABLED buffer))
920 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
922 (defun imap-quote-specials (string)
925 (goto-char (point-min))
926 (while (re-search-forward "[\\\"]" nil t)
932 (defun imap-login-auth (buffer)
933 "Login to server using the LOGIN command."
934 (message "imap: Plaintext authentication...")
935 (imap-interactive-login buffer
936 (lambda (user passwd)
937 (imap-ok-p (imap-send-command-wait
939 (imap-quote-specials user)
941 (imap-quote-specials passwd)
944 (defun imap-anonymous-p (buffer)
947 (defun imap-anonymous-auth (buffer)
948 (message "imap: Logging in anonymously...")
949 (with-current-buffer buffer
950 (imap-ok-p (imap-send-command-wait
951 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
952 (system-name)) "\"")))))
954 ;;; Compiler directives.
956 (defvar imap-sasl-client)
957 (defvar imap-sasl-step)
959 (defun imap-sasl-make-mechanisms (buffer)
962 (let ((name (symbol-name sym)))
963 (if (and (> (length name) 5)
964 (string-equal "AUTH=" (substring name 0 5 )))
965 (setq mecs (cons (substring name 5) mecs)))))
966 (imap-capability nil buffer))
969 (defun imap-sasl-auth-p (buffer)
970 (and (condition-case ()
973 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
975 (defun imap-sasl-auth (buffer)
976 "Login to server using the SASL method."
977 (message "imap: Authenticating using SASL...")
978 (with-current-buffer buffer
979 (make-local-variable 'imap-username)
980 (make-local-variable 'imap-sasl-client)
981 (make-local-variable 'imap-sasl-step)
982 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
985 (setq user (or imap-username
986 (read-from-minibuffer
987 (concat "IMAP username for " imap-server " using SASL "
988 (sasl-mechanism-name mechanism) ": ")
989 (or user imap-default-user))))
991 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
992 imap-sasl-step (sasl-next-step imap-sasl-client nil))
993 (let ((tag (imap-send-command
994 (if (sasl-step-data imap-sasl-step)
995 (format "AUTHENTICATE %s %s"
996 (sasl-mechanism-name mechanism)
997 (sasl-step-data imap-sasl-step))
998 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
1000 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
1001 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
1002 (setq imap-continuation nil
1003 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
1004 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
1005 (base64-encode-string (sasl-step-data imap-sasl-step) t)
1007 (if (imap-ok-p (imap-wait-for-tag tag))
1008 (setq imap-username user
1010 (message "Login failed...")
1014 (defun imap-digest-md5-p (buffer)
1015 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
1017 (require 'digest-md5)
1020 (defun imap-digest-md5-auth (buffer)
1021 "Login to server using the AUTH DIGEST-MD5 method."
1022 (message "imap: Authenticating using DIGEST-MD5...")
1023 (imap-interactive-login
1025 (lambda (user passwd)
1029 "AUTHENTICATE DIGEST-MD5"
1031 (digest-md5-parse-digest-challenge
1032 (base64-decode-string challenge))
1034 (digest-md5-digest-uri
1035 "imap" (digest-md5-challenge 'realm)))
1037 (digest-md5-digest-response
1038 user passwd digest-uri)))
1039 (base64-encode-string response 'no-line-break))))
1041 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1043 (setq imap-continuation nil)
1044 (imap-send-command-1 "")
1045 (imap-ok-p (imap-wait-for-tag tag)))))))
1047 ;; Server functions:
1049 (defun imap-open-1 (buffer)
1050 (with-current-buffer buffer
1052 (setq imap-current-mailbox nil
1053 imap-current-message nil
1055 imap-process (condition-case ()
1056 (funcall (nth 2 (assq imap-stream
1058 "imap" buffer imap-server imap-port)
1059 ((error quit) nil)))
1061 (set-process-filter imap-process 'imap-arrival-filter)
1062 (set-process-sentinel imap-process 'imap-sentinel)
1063 (while (and (eq imap-state 'initial)
1064 (memq (process-status imap-process) '(open run)))
1065 (message "Waiting for response from %s..." imap-server)
1066 (accept-process-output imap-process 1))
1067 (message "Waiting for response from %s...done" imap-server)
1068 (and (memq (process-status imap-process) '(open run))
1071 (defun imap-open (server &optional port stream auth buffer)
1072 "Open a IMAP connection to host SERVER at PORT returning a buffer.
1073 If PORT is unspecified, a default value is used (143 except
1074 for SSL which use 993).
1075 STREAM indicates the stream to use, see `imap-streams' for available
1076 streams. If nil, it choices the best stream the server is capable of.
1077 AUTH indicates authenticator to use, see `imap-authenticators' for
1078 available authenticators. If nil, it choices the best stream the
1079 server is capable of.
1080 BUFFER can be a buffer or a name of a buffer, which is created if
1081 necessary. If nil, the buffer name is generated."
1082 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1083 (with-current-buffer (get-buffer-create buffer)
1084 (if (imap-opened buffer)
1085 (imap-close buffer))
1086 (mapc 'make-local-variable imap-local-variables)
1087 (imap-disable-multibyte)
1088 (buffer-disable-undo)
1089 (setq imap-server (or server imap-server))
1090 (setq imap-port (or port imap-port))
1091 (setq imap-auth (or auth imap-auth))
1092 (setq imap-stream (or stream imap-stream))
1093 (message "imap: Connecting to %s..." imap-server)
1094 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1095 (imap-open-1 buffer)))
1097 (message "imap: Connecting to %s...failed" imap-server)
1099 (when (null imap-stream)
1100 ;; Need to choose stream.
1101 (let ((streams imap-streams))
1102 (while (setq stream (pop streams))
1103 ;; OK to use this stream?
1104 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1106 (if (not (eq imap-default-stream stream))
1107 (with-current-buffer (get-buffer-create
1108 (generate-new-buffer-name " *temp*"))
1109 (mapc 'make-local-variable imap-local-variables)
1110 (imap-disable-multibyte)
1111 (buffer-disable-undo)
1112 (setq imap-server (or server imap-server))
1113 (setq imap-port (or port imap-port))
1114 (setq imap-auth (or auth imap-auth))
1115 (message "imap: Reconnecting with stream `%s'..." stream)
1116 (if (null (let ((imap-stream stream))
1117 (imap-open-1 (current-buffer))))
1119 (kill-buffer (current-buffer))
1121 "imap: Reconnecting with stream `%s'...failed"
1123 ;; We're done, kill the first connection
1125 (let ((name (if (stringp buffer)
1127 (buffer-name buffer))))
1128 (kill-buffer buffer)
1129 (rename-buffer name))
1130 (message "imap: Reconnecting with stream `%s'...done"
1132 (setq imap-stream stream)
1133 (setq imap-capability nil)
1134 (setq streams nil)))
1136 (message "imap: Connecting to %s...done" imap-server)
1137 (setq imap-stream stream)
1138 (setq imap-capability nil)
1139 (setq streams nil))))))
1140 (when (imap-opened buffer)
1141 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1145 (defun imap-opened (&optional buffer)
1146 "Return non-nil if connection to imap server in BUFFER is open.
1147 If BUFFER is nil then the current buffer is used."
1148 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1149 (buffer-live-p buffer)
1150 (with-current-buffer buffer
1152 (memq (process-status imap-process) '(open run))))))
1154 (defun imap-authenticate (&optional user passwd buffer)
1155 "Authenticate to server in BUFFER, using current buffer if nil.
1156 It uses the authenticator specified when opening the server. If the
1157 authenticator requires username/passwords, they are queried from the
1158 user and optionally stored in the buffer. If USER and/or PASSWD is
1159 specified, the user will not be questioned and the username and/or
1160 password is remembered in the buffer."
1161 (with-current-buffer (or buffer (current-buffer))
1162 (if (not (eq imap-state 'nonauth))
1163 (or (eq imap-state 'auth)
1164 (eq imap-state 'selected)
1165 (eq imap-state 'examine))
1166 (make-local-variable 'imap-username)
1167 (make-local-variable 'imap-password)
1168 (if user (setq imap-username user))
1169 (if passwd (setq imap-password passwd))
1171 (and (funcall (nth 2 (assq imap-auth
1172 imap-authenticator-alist)) buffer)
1173 (setq imap-state 'auth))
1174 ;; Choose authenticator.
1175 (let ((auths imap-authenticators)
1177 (while (setq auth (pop auths))
1178 ;; OK to use authenticator?
1179 (when (funcall (nth 1 (assq auth imap-authenticator-alist)) buffer)
1180 (message "imap: Authenticating to `%s' using `%s'..."
1182 (setq imap-auth auth)
1183 (if (funcall (nth 2 (assq auth imap-authenticator-alist)) buffer)
1185 (message "imap: Authenticating to `%s' using `%s'...done"
1188 (message "imap: Authenticating to `%s' using `%s'...failed"
1189 imap-server auth)))))
1192 (defun imap-close (&optional buffer)
1193 "Close connection to server in BUFFER.
1194 If BUFFER is nil, the current buffer is used."
1195 (with-current-buffer (or buffer (current-buffer))
1200 (when (and imap-process
1201 (memq (process-status imap-process) '(open run)))
1202 (delete-process imap-process))
1203 (setq imap-current-mailbox nil
1204 imap-current-message nil
1209 (defun imap-capability (&optional identifier buffer)
1210 "Return a list of identifiers which server in BUFFER support.
1211 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1212 If BUFFER is nil, the current buffer is assumed."
1213 (with-current-buffer (or buffer (current-buffer))
1214 (unless imap-capability
1215 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1216 (setq imap-capability '(IMAP2))))
1218 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1221 (defun imap-id (&optional list-of-values buffer)
1222 "Identify client to server in BUFFER, and return server identity.
1223 LIST-OF-VALUES is nil, or a plist with identifier and value
1224 strings to send to the server to identify the client.
1226 Return a list of identifiers which server in BUFFER support, or
1227 nil if it doesn't support ID or returns no information.
1229 If BUFFER is nil, the current buffer is assumed."
1230 (with-current-buffer (or buffer (current-buffer))
1231 (when (and (imap-capability 'ID)
1232 (imap-ok-p (imap-send-command-wait
1233 (if (null list-of-values)
1235 (concat "ID (" (mapconcat (lambda (el)
1236 (concat "\"" el "\""))
1241 (defun imap-namespace (&optional buffer)
1242 "Return a namespace hierarchy at server in BUFFER.
1243 If BUFFER is nil, the current buffer is assumed."
1244 (with-current-buffer (or buffer (current-buffer))
1245 (unless imap-namespace
1246 (when (imap-capability 'NAMESPACE)
1247 (imap-send-command-wait "NAMESPACE")))
1250 (defun imap-send-command-wait (command &optional buffer)
1251 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1253 (defun imap-logout (&optional buffer)
1254 (or buffer (setq buffer (current-buffer)))
1255 (if imap-logout-timeout
1256 (with-timeout (imap-logout-timeout
1258 (with-current-buffer buffer
1259 (delete-process imap-process))
1261 (imap-send-command "LOGOUT" buffer))
1262 (imap-send-command "LOGOUT" buffer)))
1264 (defun imap-logout-wait (&optional buffer)
1265 (or buffer (setq buffer (current-buffer)))
1266 (if imap-logout-timeout
1267 (with-timeout (imap-logout-timeout
1269 (with-current-buffer buffer
1270 (delete-process imap-process))
1272 (imap-send-command-wait "LOGOUT" buffer))
1273 (imap-send-command-wait "LOGOUT" buffer)))
1276 ;; Mailbox functions:
1278 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1279 (with-current-buffer (or buffer (current-buffer))
1280 (if imap-mailbox-data
1281 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1283 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1284 propname value mailbox (current-buffer)))
1287 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1288 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1291 (defun imap-mailbox-get (propname &optional mailbox buffer)
1292 (let ((mailbox (imap-utf7-encode mailbox)))
1293 (with-current-buffer (or buffer (current-buffer))
1294 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1296 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1297 (with-current-buffer (or buffer (current-buffer))
1301 (push (funcall func (if mailbox-decoder
1302 (funcall mailbox-decoder (symbol-name s))
1303 (symbol-name s))) result))
1307 (defun imap-mailbox-map (func &optional buffer)
1308 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1309 Function should take a mailbox name (a string) as
1311 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1313 (defun imap-current-mailbox (&optional buffer)
1314 (with-current-buffer (or buffer (current-buffer))
1315 (imap-utf7-decode imap-current-mailbox)))
1317 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1318 (and (string= mailbox imap-current-mailbox)
1320 (eq imap-state 'examine))
1322 (eq imap-state 'selected)))))
1324 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1325 (with-current-buffer (or buffer (current-buffer))
1326 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1328 (defun imap-mailbox-select-1 (mailbox &optional examine)
1329 "Select MAILBOX on server in BUFFER.
1330 If EXAMINE is non-nil, do a read-only select."
1331 (if (imap-current-mailbox-p-1 mailbox examine)
1332 imap-current-mailbox
1333 (setq imap-current-mailbox mailbox)
1334 (if (imap-ok-p (imap-send-command-wait
1335 (concat (if examine "EXAMINE" "SELECT") " \""
1338 (setq imap-message-data (make-vector imap-message-prime 0)
1339 imap-state (if examine 'examine 'selected))
1340 imap-current-mailbox)
1341 ;; Failed SELECT/EXAMINE unselects current mailbox
1342 (setq imap-current-mailbox nil))))
1344 (defun imap-mailbox-select (mailbox &optional examine buffer)
1345 (with-current-buffer (or buffer (current-buffer))
1347 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1349 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1350 (with-current-buffer (or buffer (current-buffer))
1351 (imap-mailbox-select-1 mailbox 'examine)))
1353 (defun imap-mailbox-examine (mailbox &optional buffer)
1354 "Examine MAILBOX on server in BUFFER."
1355 (imap-mailbox-select mailbox 'examine buffer))
1357 (defun imap-mailbox-unselect (&optional buffer)
1358 "Close current folder in BUFFER, without expunging articles."
1359 (with-current-buffer (or buffer (current-buffer))
1360 (when (or (eq imap-state 'auth)
1361 (and (imap-capability 'UNSELECT)
1362 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1364 (imap-send-command-wait (concat "EXAMINE \""
1365 imap-current-mailbox
1367 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1368 (setq imap-current-mailbox nil
1369 imap-message-data nil
1373 (defun imap-mailbox-expunge (&optional asynch buffer)
1374 "Expunge articles in current folder in BUFFER.
1375 If ASYNCH, do not wait for succesful completion of the command.
1376 If BUFFER is nil the current buffer is assumed."
1377 (with-current-buffer (or buffer (current-buffer))
1378 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1380 (imap-send-command "EXPUNGE")
1381 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1383 (defun imap-mailbox-close (&optional asynch buffer)
1384 "Expunge articles and close current folder in BUFFER.
1385 If ASYNCH, do not wait for succesful completion of the command.
1386 If BUFFER is nil the current buffer is assumed."
1387 (with-current-buffer (or buffer (current-buffer))
1388 (when imap-current-mailbox
1390 (imap-add-callback (imap-send-command "CLOSE")
1391 `(lambda (tag status)
1392 (message "IMAP mailbox `%s' closed... %s"
1393 imap-current-mailbox status)
1394 (when (eq ,imap-current-mailbox
1395 imap-current-mailbox)
1396 ;; Don't wipe out data if another mailbox
1398 (setq imap-current-mailbox nil
1399 imap-message-data nil
1400 imap-state 'auth))))
1401 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1402 (setq imap-current-mailbox nil
1403 imap-message-data nil
1407 (defun imap-mailbox-create-1 (mailbox)
1408 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1410 (defun imap-mailbox-create (mailbox &optional buffer)
1411 "Create MAILBOX on server in BUFFER.
1412 If BUFFER is nil the current buffer is assumed."
1413 (with-current-buffer (or buffer (current-buffer))
1414 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1416 (defun imap-mailbox-delete (mailbox &optional buffer)
1417 "Delete MAILBOX on server in BUFFER.
1418 If BUFFER is nil the current buffer is assumed."
1419 (let ((mailbox (imap-utf7-encode mailbox)))
1420 (with-current-buffer (or buffer (current-buffer))
1422 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1424 (defun imap-mailbox-rename (oldname newname &optional buffer)
1425 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1426 If BUFFER is nil the current buffer is assumed."
1427 (let ((oldname (imap-utf7-encode oldname))
1428 (newname (imap-utf7-encode newname)))
1429 (with-current-buffer (or buffer (current-buffer))
1431 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1432 "\"" newname "\""))))))
1434 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1435 "Return a list of subscribed mailboxes on server in BUFFER.
1436 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1437 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1438 implementation-specific string that has to be passed to lsub command."
1439 (with-current-buffer (or buffer (current-buffer))
1440 ;; Make sure we know the hierarchy separator for root's hierarchy
1441 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1442 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1443 (imap-utf7-encode root) "\"")))
1444 ;; clear list data (NB not delimiter and other stuff)
1445 (imap-mailbox-map-1 (lambda (mailbox)
1446 (imap-mailbox-put 'lsub nil mailbox)))
1448 (imap-send-command-wait
1449 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1450 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1453 (imap-mailbox-map-1 (lambda (mailbox)
1454 (when (imap-mailbox-get-1 'lsub mailbox)
1455 (push (imap-utf7-decode mailbox) out))))
1458 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1459 "Return a list of mailboxes matching ROOT on server in BUFFER.
1460 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1461 root. REFERENCE is a implementation-specific string that has to be
1462 passed to list command."
1463 (with-current-buffer (or buffer (current-buffer))
1464 ;; Make sure we know the hierarchy separator for root's hierarchy
1465 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1466 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1467 (imap-utf7-encode root) "\"")))
1468 ;; clear list data (NB not delimiter and other stuff)
1469 (imap-mailbox-map-1 (lambda (mailbox)
1470 (imap-mailbox-put 'list nil mailbox)))
1472 (imap-send-command-wait
1473 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1474 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1477 (imap-mailbox-map-1 (lambda (mailbox)
1478 (when (imap-mailbox-get-1 'list mailbox)
1479 (push (imap-utf7-decode mailbox) out))))
1482 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1483 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1484 Returns non-nil if successful."
1485 (with-current-buffer (or buffer (current-buffer))
1486 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1487 (imap-utf7-encode mailbox)
1490 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1491 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1492 Returns non-nil if successful."
1493 (with-current-buffer (or buffer (current-buffer))
1494 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1495 (imap-utf7-encode mailbox)
1498 (defun imap-mailbox-status (mailbox items &optional buffer)
1499 "Get status items ITEM in MAILBOX from server in BUFFER.
1500 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1501 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1502 or 'unseen. If ITEMS is a list of symbols, a list of values is
1503 returned, if ITEMS is a symbol only its value is returned."
1504 (with-current-buffer (or buffer (current-buffer))
1506 (imap-send-command-wait (list "STATUS \""
1507 (imap-utf7-encode mailbox)
1515 (mapcar (lambda (item)
1516 (imap-mailbox-get item mailbox))
1518 (imap-mailbox-get items mailbox)))))
1520 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1521 "Send status item request ITEM on MAILBOX to server in BUFFER.
1522 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1523 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1524 or 'unseen. The IMAP command tag is returned."
1525 (with-current-buffer (or buffer (current-buffer))
1526 (imap-send-command (list "STATUS \""
1527 (imap-utf7-encode mailbox)
1535 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1536 "Get ACL on mailbox from server in BUFFER."
1537 (let ((mailbox (imap-utf7-encode mailbox)))
1538 (with-current-buffer (or buffer (current-buffer))
1540 (imap-send-command-wait (list "GETACL \""
1541 (or mailbox imap-current-mailbox)
1543 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1545 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1546 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1547 (let ((mailbox (imap-utf7-encode mailbox)))
1548 (with-current-buffer (or buffer (current-buffer))
1550 (imap-send-command-wait (list "SETACL \""
1551 (or mailbox imap-current-mailbox)
1557 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1558 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1559 (let ((mailbox (imap-utf7-encode mailbox)))
1560 (with-current-buffer (or buffer (current-buffer))
1562 (imap-send-command-wait (list "DELETEACL \""
1563 (or mailbox imap-current-mailbox)
1568 ;; Message functions:
1570 (defun imap-current-message (&optional buffer)
1571 (with-current-buffer (or buffer (current-buffer))
1572 imap-current-message))
1574 (defun imap-list-to-message-set (list)
1575 (mapconcat (lambda (item)
1576 (number-to-string item))
1582 (defun imap-range-to-message-set (range)
1587 (car item) (cdr item))
1588 (format "%d" item)))
1589 (if (and (listp range) (not (listp (cdr range))))
1590 (list range) ;; make (1 . 2) into ((1 . 2))
1594 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1595 (with-current-buffer (or buffer (current-buffer))
1596 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1598 (imap-list-to-message-set uids)
1602 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1603 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1604 UIDS can be a string, number or a list of numbers. If RECEIVE
1605 is non-nil return these properties."
1606 (with-current-buffer (or buffer (current-buffer))
1607 (when (imap-ok-p (imap-send-command-wait
1608 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1610 (imap-list-to-message-set uids)
1613 (if (or (null receive) (stringp uids))
1616 (mapcar (lambda (uid)
1618 (mapcar (lambda (prop)
1619 (imap-message-get uid prop))
1621 (imap-message-get uid receive)))
1623 (imap-message-get uids receive))))))
1625 (defun imap-message-put (uid propname value &optional buffer)
1626 (with-current-buffer (or buffer (current-buffer))
1627 (if imap-message-data
1628 (put (intern (number-to-string uid) imap-message-data)
1630 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1631 uid propname value (current-buffer)))
1634 (defun imap-message-get (uid propname &optional buffer)
1635 (with-current-buffer (or buffer (current-buffer))
1636 (get (intern-soft (number-to-string uid) imap-message-data)
1639 (defun imap-message-map (func propname &optional buffer)
1640 "Map a function across each mailbox in `imap-message-data', returning a list."
1641 (with-current-buffer (or buffer (current-buffer))
1645 (push (funcall func (get s 'UID) (get s propname)) result))
1649 (defmacro imap-message-envelope-date (uid &optional buffer)
1650 `(with-current-buffer (or ,buffer (current-buffer))
1651 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1653 (defmacro imap-message-envelope-subject (uid &optional buffer)
1654 `(with-current-buffer (or ,buffer (current-buffer))
1655 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1657 (defmacro imap-message-envelope-from (uid &optional buffer)
1658 `(with-current-buffer (or ,buffer (current-buffer))
1659 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1661 (defmacro imap-message-envelope-sender (uid &optional buffer)
1662 `(with-current-buffer (or ,buffer (current-buffer))
1663 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1665 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1666 `(with-current-buffer (or ,buffer (current-buffer))
1667 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1669 (defmacro imap-message-envelope-to (uid &optional buffer)
1670 `(with-current-buffer (or ,buffer (current-buffer))
1671 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1673 (defmacro imap-message-envelope-cc (uid &optional buffer)
1674 `(with-current-buffer (or ,buffer (current-buffer))
1675 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1677 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1678 `(with-current-buffer (or ,buffer (current-buffer))
1679 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1681 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1682 `(with-current-buffer (or ,buffer (current-buffer))
1683 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1685 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1686 `(with-current-buffer (or ,buffer (current-buffer))
1687 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1689 (defmacro imap-message-body (uid &optional buffer)
1690 `(with-current-buffer (or ,buffer (current-buffer))
1691 (imap-message-get ,uid 'BODY)))
1693 (defun imap-search (predicate &optional buffer)
1694 (with-current-buffer (or buffer (current-buffer))
1695 (imap-mailbox-put 'search 'dummy)
1696 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1697 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1699 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1701 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1703 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1704 "Return t if FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1705 (with-current-buffer (or buffer (current-buffer))
1706 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1707 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1709 (defun imap-message-flags-set (articles flags &optional silent buffer)
1710 (when (and articles flags)
1711 (with-current-buffer (or buffer (current-buffer))
1712 (imap-ok-p (imap-send-command-wait
1713 (concat "UID STORE " articles
1714 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1716 (defun imap-message-flags-del (articles flags &optional silent buffer)
1717 (when (and articles flags)
1718 (with-current-buffer (or buffer (current-buffer))
1719 (imap-ok-p (imap-send-command-wait
1720 (concat "UID STORE " articles
1721 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1723 (defun imap-message-flags-add (articles flags &optional silent buffer)
1724 (when (and articles flags)
1725 (with-current-buffer (or buffer (current-buffer))
1726 (imap-ok-p (imap-send-command-wait
1727 (concat "UID STORE " articles
1728 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1730 (defun imap-message-copyuid-1 (mailbox)
1731 (if (imap-capability 'UIDPLUS)
1732 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1733 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1734 (let ((old-mailbox imap-current-mailbox)
1736 (imap-message-data (make-vector 2 0)))
1737 (when (imap-mailbox-examine-1 mailbox)
1739 (and (imap-fetch "*" "UID")
1740 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1741 (apply 'max (imap-message-map
1742 (lambda (uid prop) uid) 'UID))))
1744 (imap-mailbox-select old-mailbox (eq state 'examine))
1745 (imap-mailbox-unselect)))))))
1747 (defun imap-message-copyuid (mailbox &optional buffer)
1748 (with-current-buffer (or buffer (current-buffer))
1749 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1751 (defun imap-message-copy (articles mailbox
1752 &optional dont-create no-copyuid buffer)
1753 "Copy ARTICLES (a string message set) to MAILBOX on server in
1754 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1755 non-nil, it will not create a mailbox. On success, return a list with
1756 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1757 first element, rest of list contain the saved articles' UIDs."
1759 (with-current-buffer (or buffer (current-buffer))
1760 (let ((mailbox (imap-utf7-encode mailbox)))
1761 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1762 (imap-current-target-mailbox mailbox))
1763 (if (imap-ok-p (imap-send-command-wait cmd))
1765 (when (and (not dont-create)
1766 ;; removed because of buggy Oracle server
1767 ;; that doesn't send TRYCREATE tags (which
1768 ;; is a MUST according to specifications):
1769 ;;(imap-mailbox-get-1 'trycreate mailbox)
1770 (imap-mailbox-create-1 mailbox))
1771 (imap-ok-p (imap-send-command-wait cmd)))))
1773 (imap-message-copyuid-1 mailbox)))))))
1775 (defun imap-message-appenduid-1 (mailbox)
1776 (if (imap-capability 'UIDPLUS)
1777 (imap-mailbox-get-1 'appenduid mailbox)
1778 (let ((old-mailbox imap-current-mailbox)
1780 (imap-message-data (make-vector 2 0)))
1781 (when (imap-mailbox-examine-1 mailbox)
1783 (and (imap-fetch "*" "UID")
1784 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1785 (apply 'max (imap-message-map
1786 (lambda (uid prop) uid) 'UID))))
1788 (imap-mailbox-select old-mailbox (eq state 'examine))
1789 (imap-mailbox-unselect)))))))
1791 (defun imap-message-appenduid (mailbox &optional buffer)
1792 (with-current-buffer (or buffer (current-buffer))
1793 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1795 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1796 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1797 FLAGS and DATE-TIME is currently not used. Return a cons holding
1798 uidvalidity of MAILBOX and UID the newly created article got, or nil
1800 (let ((mailbox (imap-utf7-encode mailbox)))
1801 (with-current-buffer (or buffer (current-buffer))
1802 (and (let ((imap-current-target-mailbox mailbox))
1804 (imap-send-command-wait
1805 (list "APPEND \"" mailbox "\" " article))))
1806 (imap-message-appenduid-1 mailbox)))))
1808 (defun imap-body-lines (body)
1809 "Return number of lines in article by looking at the mime bodystructure BODY."
1811 (if (stringp (car body))
1812 (cond ((and (string= (upcase (car body)) "TEXT")
1813 (numberp (nth 7 body)))
1815 ((and (string= (upcase (car body)) "MESSAGE")
1816 (numberp (nth 9 body)))
1819 (apply '+ (mapcar 'imap-body-lines body)))
1822 (defun imap-envelope-from (from)
1823 "Return a from string line."
1825 (concat (aref from 0)
1826 (if (aref from 0) " <")
1830 (if (aref from 0) ">"))))
1833 ;; Internal functions.
1835 (defun imap-add-callback (tag func)
1836 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1838 (defun imap-send-command-1 (cmdstr)
1839 (setq cmdstr (concat cmdstr imap-client-eol))
1841 (with-current-buffer (get-buffer-create imap-log-buffer)
1842 (imap-disable-multibyte)
1843 (buffer-disable-undo)
1844 (goto-char (point-max))
1846 (process-send-string imap-process cmdstr))
1848 (defun imap-send-command (command &optional buffer)
1849 (with-current-buffer (or buffer (current-buffer))
1850 (if (not (listp command)) (setq command (list command)))
1851 (let ((tag (setq imap-tag (1+ imap-tag)))
1853 (setq cmdstr (concat (number-to-string imap-tag) " "))
1854 (while (setq cmd (pop command))
1855 (cond ((stringp cmd)
1856 (setq cmdstr (concat cmdstr cmd)))
1858 (let ((eol imap-client-eol)
1859 (calcfirst imap-calculate-literal-size-first)
1861 (with-current-buffer cmd
1863 (setq size (buffer-size)))
1864 (when (not (equal eol "\r\n"))
1865 ;; XXX modifies buffer!
1866 (goto-char (point-min))
1867 (while (search-forward "\r\n" nil t)
1868 (replace-match eol)))
1870 (setq size (buffer-size))))
1872 (concat cmdstr (format "{%d}" size))))
1875 (imap-send-command-1 cmdstr)
1877 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1878 (setq command nil) ;; abort command if no cont-req
1879 (let ((process imap-process)
1880 (stream imap-stream)
1881 (eol imap-client-eol))
1882 (with-current-buffer cmd
1884 (with-current-buffer (get-buffer-create
1886 (imap-disable-multibyte)
1887 (buffer-disable-undo)
1888 (goto-char (point-max))
1889 (insert-buffer-substring cmd)))
1890 (process-send-region process (point-min)
1892 (process-send-string process imap-client-eol))))
1893 (setq imap-continuation nil)))
1895 (imap-send-command-1 cmdstr)
1898 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1899 (setq command nil) ;; abort command if no cont-req
1900 (setq command (cons (funcall cmd imap-continuation)
1902 (setq imap-continuation nil)))
1904 (error "Unknown command type"))))
1906 (imap-send-command-1 cmdstr))
1909 (defun imap-wait-for-tag (tag &optional buffer)
1910 (with-current-buffer (or buffer (current-buffer))
1911 (let (imap-have-messaged)
1912 (while (and (null imap-continuation)
1913 (memq (process-status imap-process) '(open run))
1914 (< imap-reached-tag tag))
1915 (let ((len (/ (point-max) 1024))
1918 (setq imap-have-messaged t)
1919 (message "imap read: %dk" len))
1920 (accept-process-output imap-process
1921 (truncate imap-read-timeout)
1922 (truncate (* (- imap-read-timeout
1923 (truncate imap-read-timeout))
1925 ;; A process can die _before_ we have processed everything it
1926 ;; has to say. Moreover, this can happen in between the call to
1927 ;; accept-process-output and the call to process-status in an
1928 ;; iteration of the loop above.
1929 (when (and (null imap-continuation)
1930 (< imap-reached-tag tag))
1931 (accept-process-output imap-process 0 0))
1932 (when imap-have-messaged
1934 (and (memq (process-status imap-process) '(open run))
1935 (or (assq tag imap-failed-tags)
1936 (if imap-continuation
1940 (defun imap-sentinel (process string)
1941 (delete-process process))
1943 (defun imap-find-next-line ()
1944 "Return point at end of current line, taking into account literals.
1945 Return nil if no complete line has arrived."
1946 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1949 (if (match-string 1)
1950 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1952 (goto-char (+ (point) (string-to-number (match-string 1))))
1953 (imap-find-next-line))
1956 (defun imap-arrival-filter (proc string)
1957 "IMAP process filter."
1958 ;; Sometimes, we are called even though the process has died.
1959 ;; Better abstain from doing stuff in that case.
1960 (when (buffer-name (process-buffer proc))
1961 (with-current-buffer (process-buffer proc)
1962 (goto-char (point-max))
1965 (with-current-buffer (get-buffer-create imap-log-buffer)
1966 (imap-disable-multibyte)
1967 (buffer-disable-undo)
1968 (goto-char (point-max))
1971 (goto-char (point-min))
1972 (while (setq end (imap-find-next-line))
1974 (narrow-to-region (point-min) end)
1975 (delete-backward-char (length imap-server-eol))
1976 (goto-char (point-min))
1978 (cond ((eq imap-state 'initial)
1979 (imap-parse-greeting))
1980 ((or (eq imap-state 'auth)
1981 (eq imap-state 'nonauth)
1982 (eq imap-state 'selected)
1983 (eq imap-state 'examine))
1984 (imap-parse-response))
1986 (message "Unknown state %s in arrival filter"
1988 (delete-region (point-min) (point-max)))))))))
1993 (defsubst imap-forward ()
1994 (or (eobp) (forward-char)))
1997 ;; ; Unsigned 32-bit integer
1998 ;; ; (0 <= n < 4,294,967,296)
2000 (defsubst imap-parse-number ()
2001 (when (looking-at "[0-9]+")
2003 (string-to-number (match-string 0))
2004 (goto-char (match-end 0)))))
2006 ;; literal = "{" number "}" CRLF *CHAR8
2007 ;; ; Number represents the number of CHAR8s
2009 (defsubst imap-parse-literal ()
2010 (when (looking-at "{\\([0-9]+\\)}\r\n")
2011 (let ((pos (match-end 0))
2012 (len (string-to-number (match-string 1))))
2013 (if (< (point-max) (+ pos len))
2015 (goto-char (+ pos len))
2016 (buffer-substring pos (+ pos len))))))
2018 ;; string = quoted / literal
2020 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
2022 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2023 ;; "\" quoted-specials
2025 ;; quoted-specials = DQUOTE / "\"
2027 ;; TEXT-CHAR = <any CHAR except CR and LF>
2029 (defsubst imap-parse-string ()
2030 (cond ((eq (char-after) ?\")
2032 (let ((p (point)) (name ""))
2033 (skip-chars-forward "^\"\\\\")
2034 (setq name (buffer-substring p (point)))
2035 (while (eq (char-after) ?\\)
2036 (setq p (1+ (point)))
2038 (skip-chars-forward "^\"\\\\")
2039 (setq name (concat name (buffer-substring p (point)))))
2042 ((eq (char-after) ?{)
2043 (imap-parse-literal))))
2047 (defsubst imap-parse-nil ()
2048 (if (looking-at "NIL")
2049 (goto-char (match-end 0))))
2051 ;; nstring = string / nil
2053 (defsubst imap-parse-nstring ()
2054 (or (imap-parse-string)
2055 (and (imap-parse-nil)
2058 ;; astring = atom / string
2060 ;; atom = 1*ATOM-CHAR
2062 ;; ATOM-CHAR = <any CHAR except atom-specials>
2064 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2067 ;; list-wildcards = "%" / "*"
2069 ;; quoted-specials = DQUOTE / "\"
2071 (defsubst imap-parse-astring ()
2072 (or (imap-parse-string)
2073 (buffer-substring (point)
2074 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2075 (goto-char (1- (match-end 0)))
2079 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2082 ;; addr-adl = nstring
2083 ;; ; Holds route from [RFC-822] route-addr if
2086 ;; addr-host = nstring
2087 ;; ; nil indicates [RFC-822] group syntax.
2088 ;; ; Otherwise, holds [RFC-822] domain name
2090 ;; addr-mailbox = nstring
2091 ;; ; nil indicates end of [RFC-822] group; if
2092 ;; ; non-nil and addr-host is nil, holds
2093 ;; ; [RFC-822] group name.
2094 ;; ; Otherwise, holds [RFC-822] local-part
2095 ;; ; after removing [RFC-822] quoting
2097 ;; addr-name = nstring
2098 ;; ; If non-nil, holds phrase from [RFC-822]
2099 ;; ; mailbox after removing [RFC-822] quoting
2102 (defsubst imap-parse-address ()
2104 (when (eq (char-after) ?\()
2106 (setq address (vector (prog1 (imap-parse-nstring)
2108 (prog1 (imap-parse-nstring)
2110 (prog1 (imap-parse-nstring)
2112 (imap-parse-nstring)))
2113 (when (eq (char-after) ?\))
2117 ;; address-list = "(" 1*address ")" / nil
2121 (defsubst imap-parse-address-list ()
2122 (if (eq (char-after) ?\()
2123 (let (address addresses)
2125 (while (and (not (eq (char-after) ?\)))
2126 ;; next line for MS Exchange bug
2127 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2128 (setq address (imap-parse-address)))
2129 (setq addresses (cons address addresses)))
2130 (when (eq (char-after) ?\))
2132 (nreverse addresses)))
2133 ;; With assert, the code might not be eval'd.
2134 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2137 ;; mailbox = "INBOX" / astring
2138 ;; ; INBOX is case-insensitive. All case variants of
2139 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2140 ;; ; not as an astring. An astring which consists of
2141 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2142 ;; ; is considered to be INBOX and not an astring.
2143 ;; ; Refer to section 5.1 for further
2144 ;; ; semantic details of mailbox names.
2146 (defsubst imap-parse-mailbox ()
2147 (let ((mailbox (imap-parse-astring)))
2148 (if (string-equal "INBOX" (upcase mailbox))
2152 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2154 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2155 ;; ; Authentication condition
2157 ;; resp-cond-bye = "BYE" SP resp-text
2159 (defun imap-parse-greeting ()
2160 "Parse a IMAP greeting."
2161 (cond ((looking-at "\\* OK ")
2162 (setq imap-state 'nonauth))
2163 ((looking-at "\\* PREAUTH ")
2164 (setq imap-state 'auth))
2165 ((looking-at "\\* BYE ")
2166 (setq imap-state 'closed))))
2168 ;; response = *(continue-req / response-data) response-done
2170 ;; continue-req = "+" SP (resp-text / base64) CRLF
2172 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2173 ;; mailbox-data / message-data / capability-data) CRLF
2175 ;; response-done = response-tagged / response-fatal
2177 ;; response-fatal = "*" SP resp-cond-bye CRLF
2178 ;; ; Server closes connection immediately
2180 ;; response-tagged = tag SP resp-cond-state CRLF
2182 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2183 ;; ; Status condition
2185 ;; resp-cond-bye = "BYE" SP resp-text
2187 ;; mailbox-data = "FLAGS" SP flag-list /
2188 ;; "LIST" SP mailbox-list /
2189 ;; "LSUB" SP mailbox-list /
2190 ;; "SEARCH" *(SP nz-number) /
2191 ;; "STATUS" SP mailbox SP "("
2192 ;; [status-att SP number *(SP status-att SP number)] ")" /
2193 ;; number SP "EXISTS" /
2194 ;; number SP "RECENT"
2196 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2198 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2200 ;; ; IMAP4rev1 servers which offer RFC 1730
2201 ;; ; compatibility MUST list "IMAP4" as the first
2204 (defun imap-parse-response ()
2205 "Parse a IMAP command response."
2207 (case (setq token (read (current-buffer)))
2208 (+ (setq imap-continuation
2209 (or (buffer-substring (min (point-max) (1+ (point)))
2212 (* (case (prog1 (setq token (read (current-buffer)))
2214 (OK (imap-parse-resp-text))
2215 (NO (imap-parse-resp-text))
2216 (BAD (imap-parse-resp-text))
2217 (BYE (imap-parse-resp-text))
2218 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2219 (LIST (imap-parse-data-list 'list))
2220 (LSUB (imap-parse-data-list 'lsub))
2221 (SEARCH (imap-mailbox-put
2223 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2224 (STATUS (imap-parse-status))
2225 (CAPABILITY (setq imap-capability
2226 (read (concat "(" (upcase (buffer-substring
2227 (point) (point-max)))
2229 (ID (setq imap-id (read (buffer-substring (point)
2231 (ACL (imap-parse-acl))
2232 (t (case (prog1 (read (current-buffer))
2234 (EXISTS (imap-mailbox-put 'exists token))
2235 (RECENT (imap-mailbox-put 'recent token))
2237 (FETCH (imap-parse-fetch token))
2238 (t (message "Garbage: %s" (buffer-string)))))))
2240 (if (not (integerp token))
2241 (message "Garbage: %s" (buffer-string))
2242 (case (prog1 (setq status (read (current-buffer)))
2245 (setq imap-reached-tag (max imap-reached-tag token))
2246 (imap-parse-resp-text)))
2248 (setq imap-reached-tag (max imap-reached-tag token))
2250 (imap-parse-resp-text))
2252 (when (eq (char-after) ?\[)
2253 (setq code (buffer-substring (point)
2254 (search-forward "]")))
2256 (setq text (buffer-substring (point) (point-max)))
2257 (push (list token status code text)
2258 imap-failed-tags))))
2260 (setq imap-reached-tag (max imap-reached-tag token))
2262 (imap-parse-resp-text))
2264 (when (eq (char-after) ?\[)
2265 (setq code (buffer-substring (point)
2266 (search-forward "]")))
2268 (setq text (buffer-substring (point) (point-max)))
2269 (push (list token status code text) imap-failed-tags)
2270 (error "Internal error, tag %s status %s code %s text %s"
2271 token status code text))))
2272 (t (message "Garbage: %s" (buffer-string))))
2273 (when (assq token imap-callbacks)
2274 (funcall (cdr (assq token imap-callbacks)) token status)
2275 (setq imap-callbacks
2276 (imap-remassoc token imap-callbacks)))))))))
2278 ;; resp-text = ["[" resp-text-code "]" SP] text
2280 ;; text = 1*TEXT-CHAR
2282 ;; TEXT-CHAR = <any CHAR except CR and LF>
2284 (defun imap-parse-resp-text ()
2285 (imap-parse-resp-text-code))
2287 ;; resp-text-code = "ALERT" /
2288 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2289 ;; "NEWNAME" SP string SP string /
2291 ;; "PERMANENTFLAGS" SP "("
2292 ;; [flag-perm *(SP flag-perm)] ")" /
2296 ;; "UIDNEXT" SP nz-number /
2297 ;; "UIDVALIDITY" SP nz-number /
2298 ;; "UNSEEN" SP nz-number /
2299 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2301 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2303 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2305 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2307 ;; ; Identifies a set of messages. For message
2308 ;; ; sequence numbers, these are consecutive
2309 ;; ; numbers from 1 to the number of messages in
2311 ;; ; Comma delimits individual numbers, colon
2312 ;; ; delimits between two numbers inclusive.
2313 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2314 ;; ; 14,15 for a mailbox with 15 messages.
2316 ;; sequence-num = nz-number / "*"
2317 ;; ; * is the largest number in use. For message
2318 ;; ; sequence numbers, it is the number of messages
2319 ;; ; in the mailbox. For unique identifiers, it is
2320 ;; ; the unique identifier of the last message in
2323 ;; flag-perm = flag / "\*"
2325 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2326 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2327 ;; ; Does not include "\Recent"
2329 ;; flag-extension = "\" atom
2330 ;; ; Future expansion. Client implementations
2331 ;; ; MUST accept flag-extension flags. Server
2332 ;; ; implementations MUST NOT generate
2333 ;; ; flag-extension flags except as defined by
2334 ;; ; future standard or standards-track
2335 ;; ; revisions of this specification.
2337 ;; flag-keyword = atom
2339 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2341 (defun imap-parse-resp-text-code ()
2342 ;; xxx next line for stalker communigate pro 3.3.1 bug
2343 (when (looking-at " \\[")
2345 (when (eq (char-after) ?\[)
2347 (cond ((search-forward "PERMANENTFLAGS " nil t)
2348 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2349 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2350 (imap-mailbox-put 'uidnext (match-string 1)))
2351 ((search-forward "UNSEEN " nil t)
2352 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2353 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2354 (imap-mailbox-put 'uidvalidity (match-string 1)))
2355 ((search-forward "READ-ONLY" nil t)
2356 (imap-mailbox-put 'read-only t))
2357 ((search-forward "NEWNAME " nil t)
2358 (let (oldname newname)
2359 (setq oldname (imap-parse-string))
2361 (setq newname (imap-parse-string))
2362 (imap-mailbox-put 'newname newname oldname)))
2363 ((search-forward "TRYCREATE" nil t)
2364 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2365 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2366 (imap-mailbox-put 'appenduid
2367 (list (match-string 1)
2368 (string-to-number (match-string 2)))
2369 imap-current-target-mailbox))
2370 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2371 (imap-mailbox-put 'copyuid (list (match-string 1)
2374 imap-current-target-mailbox))
2375 ((search-forward "ALERT] " nil t)
2376 (message "Imap server %s information: %s" imap-server
2377 (buffer-substring (point) (point-max)))))))
2379 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2380 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2382 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2383 ;; *(SP mbx-list-oflag) /
2384 ;; mbx-list-oflag *(SP mbx-list-oflag)
2386 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2387 ;; ; Other flags; multiple possible per LIST response
2389 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2390 ;; ; Selectability flags; only one per LIST response
2392 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2393 ;; "\" quoted-specials
2395 ;; quoted-specials = DQUOTE / "\"
2397 (defun imap-parse-data-list (type)
2398 (let (flags delimiter mailbox)
2399 (setq flags (imap-parse-flag-list))
2400 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2401 (setq delimiter (match-string 1))
2402 (goto-char (1+ (match-end 0)))
2403 (when (setq mailbox (imap-parse-mailbox))
2404 (imap-mailbox-put type t mailbox)
2405 (imap-mailbox-put 'list-flags flags mailbox)
2406 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2408 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2409 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2410 ;; "INTERNALDATE" SPACE date_time /
2411 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2412 ;; "RFC822.SIZE" SPACE number /
2413 ;; "BODY" ["STRUCTURE"] SPACE body /
2414 ;; "BODY" section ["<" number ">"] SPACE nstring /
2415 ;; "UID" SPACE uniqueid) ")"
2417 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2418 ;; SPACE time SPACE zone <">
2420 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2421 ;; ["." (section_text / "MIME")])] "]"
2423 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2424 ;; SPACE header_list / "TEXT"
2426 ;; header_fld_name ::= astring
2428 ;; header_list ::= "(" 1#header_fld_name ")"
2430 (defsubst imap-parse-header-list ()
2431 (when (eq (char-after) ?\()
2433 (while (not (eq (char-after) ?\)))
2435 (push (imap-parse-astring) strlist))
2437 (nreverse strlist))))
2439 (defsubst imap-parse-fetch-body-section ()
2441 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2442 (if (eq (char-before) ? )
2444 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2445 (search-forward "]" nil t))
2448 (defun imap-parse-fetch (response)
2449 (when (eq (char-after) ?\()
2450 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2451 rfc822size body bodydetail bodystructure flags-empty)
2452 (while (not (eq (char-after) ?\)))
2454 (let ((token (read (current-buffer))))
2456 (cond ((eq token 'UID)
2457 (setq uid (condition-case ()
2458 (read (current-buffer))
2461 (setq flags (imap-parse-flag-list))
2463 (setq flags-empty 't)))
2464 ((eq token 'ENVELOPE)
2465 (setq envelope (imap-parse-envelope)))
2466 ((eq token 'INTERNALDATE)
2467 (setq internaldate (imap-parse-string)))
2469 (setq rfc822 (imap-parse-nstring)))
2470 ((eq token 'RFC822.HEADER)
2471 (setq rfc822header (imap-parse-nstring)))
2472 ((eq token 'RFC822.TEXT)
2473 (setq rfc822text (imap-parse-nstring)))
2474 ((eq token 'RFC822.SIZE)
2475 (setq rfc822size (read (current-buffer))))
2477 (if (eq (char-before) ?\[)
2479 (upcase (imap-parse-fetch-body-section))
2480 (and (eq (char-after) ?<)
2481 (buffer-substring (1+ (point))
2482 (search-forward ">" nil t)))
2483 (progn (imap-forward)
2484 (imap-parse-nstring)))
2486 (setq body (imap-parse-body))))
2487 ((eq token 'BODYSTRUCTURE)
2488 (setq bodystructure (imap-parse-body))))))
2490 (setq imap-current-message uid)
2491 (imap-message-put uid 'UID uid)
2492 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2493 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2494 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2495 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2496 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2497 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2498 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2499 (and body (imap-message-put uid 'BODY body))
2500 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2501 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2502 (run-hooks 'imap-fetch-data-hook)))))
2504 ;; mailbox-data = ...
2505 ;; "STATUS" SP mailbox SP "("
2506 ;; [status-att SP number
2507 ;; *(SP status-att SP number)] ")"
2510 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2513 (defun imap-parse-status ()
2514 (let ((mailbox (imap-parse-mailbox)))
2515 (if (eq (char-after) ? )
2517 (when (and mailbox (eq (char-after) ?\())
2518 (while (and (not (eq (char-after) ?\)))
2519 (or (forward-char) t)
2520 (looking-at "\\([A-Za-z]+\\) "))
2521 (let ((token (upcase (match-string 1))))
2522 (goto-char (match-end 0))
2523 (cond ((string= token "MESSAGES")
2524 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2525 ((string= token "RECENT")
2526 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2527 ((string= token "UIDNEXT")
2528 (and (looking-at "[0-9]+")
2529 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2530 (goto-char (match-end 0))))
2531 ((string= token "UIDVALIDITY")
2532 (and (looking-at "[0-9]+")
2533 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2534 (goto-char (match-end 0))))
2535 ((string= token "UNSEEN")
2536 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2538 (message "Unknown status data %s in mailbox %s ignored"
2540 (read (current-buffer)))))))))
2542 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2545 ;; identifier ::= astring
2547 ;; rights ::= astring
2549 (defun imap-parse-acl ()
2550 (let ((mailbox (imap-parse-mailbox))
2551 identifier rights acl)
2552 (while (eq (char-after) ?\ )
2554 (setq identifier (imap-parse-astring))
2556 (setq rights (imap-parse-astring))
2557 (setq acl (append acl (list (cons identifier rights)))))
2558 (imap-mailbox-put 'acl acl mailbox)))
2560 ;; flag-list = "(" [flag *(SP flag)] ")"
2562 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2563 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2564 ;; ; Does not include "\Recent"
2566 ;; flag-keyword = atom
2568 ;; flag-extension = "\" atom
2569 ;; ; Future expansion. Client implementations
2570 ;; ; MUST accept flag-extension flags. Server
2571 ;; ; implementations MUST NOT generate
2572 ;; ; flag-extension flags except as defined by
2573 ;; ; future standard or standards-track
2574 ;; ; revisions of this specification.
2576 (defun imap-parse-flag-list ()
2577 (let (flag-list start)
2578 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list")
2579 (while (and (not (eq (char-after) ?\)))
2582 ;; next line for Courier IMAP bug.
2583 (skip-chars-forward " ")
2585 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2586 (push (buffer-substring start (point)) flag-list))
2587 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list")
2589 (nreverse flag-list)))
2591 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2592 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2593 ;; env-in-reply-to SP env-message-id ")"
2595 ;; env-bcc = "(" 1*address ")" / nil
2597 ;; env-cc = "(" 1*address ")" / nil
2599 ;; env-date = nstring
2601 ;; env-from = "(" 1*address ")" / nil
2603 ;; env-in-reply-to = nstring
2605 ;; env-message-id = nstring
2607 ;; env-reply-to = "(" 1*address ")" / nil
2609 ;; env-sender = "(" 1*address ")" / nil
2611 ;; env-subject = nstring
2613 ;; env-to = "(" 1*address ")" / nil
2615 (defun imap-parse-envelope ()
2616 (when (eq (char-after) ?\()
2618 (vector (prog1 (imap-parse-nstring) ;; date
2620 (prog1 (imap-parse-nstring) ;; subject
2622 (prog1 (imap-parse-address-list) ;; from
2624 (prog1 (imap-parse-address-list) ;; sender
2626 (prog1 (imap-parse-address-list) ;; reply-to
2628 (prog1 (imap-parse-address-list) ;; to
2630 (prog1 (imap-parse-address-list) ;; cc
2632 (prog1 (imap-parse-address-list) ;; bcc
2634 (prog1 (imap-parse-nstring) ;; in-reply-to
2636 (prog1 (imap-parse-nstring) ;; message-id
2639 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2641 (defsubst imap-parse-string-list ()
2642 (cond ((eq (char-after) ?\() ;; body-fld-param
2645 (while (setq str (imap-parse-string))
2647 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2648 ;; between body-fld-param's sometimes
2649 (or (eq (char-after) ?\")
2651 (nreverse strlist)))
2655 ;; body-extension = nstring / number /
2656 ;; "(" body-extension *(SP body-extension) ")"
2657 ;; ; Future expansion. Client implementations
2658 ;; ; MUST accept body-extension fields. Server
2659 ;; ; implementations MUST NOT generate
2660 ;; ; body-extension fields except as defined by
2661 ;; ; future standard or standards-track
2662 ;; ; revisions of this specification.
2664 (defun imap-parse-body-extension ()
2665 (if (eq (char-after) ?\()
2668 (push (imap-parse-body-extension) b-e)
2669 (while (eq (char-after) ?\ )
2671 (push (imap-parse-body-extension) b-e))
2672 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2675 (or (imap-parse-number)
2676 (imap-parse-nstring))))
2678 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2679 ;; *(SP body-extension)]]
2680 ;; ; MUST NOT be returned on non-extensible
2683 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2684 ;; *(SP body-extension)]]
2685 ;; ; MUST NOT be returned on non-extensible
2688 (defsubst imap-parse-body-ext ()
2690 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2693 (if (eq (char-after) ?\()
2696 (push (imap-parse-string) dsp)
2698 (push (imap-parse-string-list) dsp)
2700 ;; With assert, the code might not be eval'd.
2701 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2703 (push (nreverse dsp) ext))
2704 (when (eq (char-after) ?\ ) ;; body-fld-lang
2706 (if (eq (char-after) ?\()
2707 (push (imap-parse-string-list) ext)
2708 (push (imap-parse-nstring) ext))
2709 (while (eq (char-after) ?\ ) ;; body-extension
2711 (setq ext (append (imap-parse-body-extension) ext)))))
2714 ;; body = "(" body-type-1part / body-type-mpart ")"
2716 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2717 ;; *(SP body-extension)]]
2718 ;; ; MUST NOT be returned on non-extensible
2721 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2722 ;; *(SP body-extension)]]
2723 ;; ; MUST NOT be returned on non-extensible
2726 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2727 ;; body-fld-enc SP body-fld-octets
2729 ;; body-fld-desc = nstring
2731 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2733 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2734 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2736 ;; body-fld-id = nstring
2738 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2740 ;; body-fld-lines = number
2742 ;; body-fld-md5 = nstring
2744 ;; body-fld-octets = number
2746 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2748 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2749 ;; [SP body-ext-1part]
2751 ;; body-type-basic = media-basic SP body-fields
2752 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2754 ;; body-type-msg = media-message SP body-fields SP envelope
2755 ;; SP body SP body-fld-lines
2757 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2759 ;; body-type-mpart = 1*body SP media-subtype
2760 ;; [SP body-ext-mpart]
2762 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2763 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2764 ;; ; Defined in [MIME-IMT]
2766 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2767 ;; ; Defined in [MIME-IMT]
2769 ;; media-subtype = string
2770 ;; ; Defined in [MIME-IMT]
2772 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2773 ;; ; Defined in [MIME-IMT]
2775 (defun imap-parse-body ()
2777 (when (eq (char-after) ?\()
2779 (if (eq (char-after) ?\()
2781 (while (and (eq (char-after) ?\()
2782 (setq subbody (imap-parse-body)))
2783 ;; buggy stalker communigate pro 3.0 insert a SPC between
2784 ;; parts in multiparts
2785 (when (and (eq (char-after) ?\ )
2786 (eq (char-after (1+ (point))) ?\())
2788 (push subbody body))
2790 (push (imap-parse-string) body) ;; media-subtype
2791 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2793 (if (eq (char-after) ?\() ;; body-fld-param
2794 (push (imap-parse-string-list) body)
2795 (push (and (imap-parse-nil) nil) body))
2797 (append (imap-parse-body-ext) body))) ;; body-ext-...
2798 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2802 (push (imap-parse-string) body) ;; media-type
2804 (push (imap-parse-string) body) ;; media-subtype
2806 ;; next line for Sun SIMS bug
2807 (and (eq (char-after) ? ) (imap-forward))
2808 (if (eq (char-after) ?\() ;; body-fld-param
2809 (push (imap-parse-string-list) body)
2810 (push (and (imap-parse-nil) nil) body))
2812 (push (imap-parse-nstring) body) ;; body-fld-id
2814 (push (imap-parse-nstring) body) ;; body-fld-desc
2816 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2817 ;; nstring and return nil instead of defaulting back to 7BIT
2818 ;; as the standard says.
2819 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2821 (push (imap-parse-number) body) ;; body-fld-octets
2823 ;; ok, we're done parsing the required parts, what comes now is one
2826 ;; envelope (then we're parsing body-type-msg)
2827 ;; body-fld-lines (then we're parsing body-type-text)
2828 ;; body-ext-1part (then we're parsing body-type-basic)
2830 ;; the problem is that the two first are in turn optionally followed
2831 ;; by the third. So we parse the first two here (if there are any)...
2833 (when (eq (char-after) ?\ )
2836 (cond ((eq (char-after) ?\() ;; body-type-msg:
2837 (push (imap-parse-envelope) body) ;; envelope
2839 (push (imap-parse-body) body) ;; body
2840 ;; buggy stalker communigate pro 3.0 doesn't print
2841 ;; number of lines in message/rfc822 attachment
2842 (if (eq (char-after) ?\))
2845 (push (imap-parse-number) body))) ;; body-fld-lines
2846 ((setq lines (imap-parse-number)) ;; body-type-text:
2847 (push lines body)) ;; body-fld-lines
2849 (backward-char))))) ;; no match...
2851 ;; ...and then parse the third one here...
2853 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2855 (push (imap-parse-nstring) body) ;; body-fld-md5
2856 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2858 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2862 (when imap-debug ; (untrace-all)
2864 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2865 (mapc (lambda (f) (trace-function-background f imap-debug-buffer))
2876 imap-interactive-login
2892 imap-send-command-wait
2897 imap-current-mailbox
2898 imap-current-mailbox-p-1
2899 imap-current-mailbox-p
2900 imap-mailbox-select-1
2902 imap-mailbox-examine-1
2903 imap-mailbox-examine
2904 imap-mailbox-unselect
2905 imap-mailbox-expunge
2907 imap-mailbox-create-1
2913 imap-mailbox-subscribe
2914 imap-mailbox-unsubscribe
2916 imap-mailbox-acl-get
2917 imap-mailbox-acl-set
2918 imap-mailbox-acl-delete
2919 imap-current-message
2920 imap-list-to-message-set
2927 imap-message-flag-permanent-p
2928 imap-message-flags-set
2929 imap-message-flags-del
2930 imap-message-flags-add
2931 imap-message-copyuid-1
2932 imap-message-copyuid
2934 imap-message-appenduid-1
2935 imap-message-appenduid
2947 imap-parse-resp-text
2948 imap-parse-resp-text-code
2949 imap-parse-data-list
2953 imap-parse-flag-list
2955 imap-parse-body-extension
2961 ;;; arch-tag: 27369ed6-33e4-482f-96f1-8bb906ba70f7
2962 ;;; imap.el ends here