1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; imap.el is a elisp library providing an interface for talking to
29 ;; imap.el is roughly divided in two parts, one that parses IMAP
30 ;; responses from the server and storing data into buffer-local
31 ;; variables, and one for utility functions which send commands to
32 ;; server, waits for an answer, and return information. The latter
33 ;; part is layered on top of the previous.
35 ;; The imap.el API consist of the following functions, other functions
36 ;; in this file should not be called directly and the result of doing
37 ;; so are at best undefined.
41 ;; imap-open, imap-opened, imap-authenticate, imap-close,
42 ;; imap-capability, imap-namespace, imap-error-text
46 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
47 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
48 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
49 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
50 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
51 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
52 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
56 ;; imap-fetch-asynch, imap-fetch,
57 ;; imap-current-message, imap-list-to-message-set,
58 ;; imap-message-get, imap-message-map
59 ;; imap-message-envelope-date, imap-message-envelope-subject,
60 ;; imap-message-envelope-from, imap-message-envelope-sender,
61 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
62 ;; imap-message-envelope-cc, imap-message-envelope-bcc
63 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
64 ;; imap-message-body, imap-message-flag-permanent-p
65 ;; imap-message-flags-set, imap-message-flags-del
66 ;; imap-message-flags-add, imap-message-copyuid
67 ;; imap-message-copy, imap-message-appenduid
68 ;; imap-message-append, imap-envelope-from
71 ;; It is my hope that theese commands should be pretty self
72 ;; explanatory for someone that know IMAP. All functions have
73 ;; additional documentation on how to invoke them.
75 ;; imap.el support RFC1730/2060 (IMAP4/IMAP4rev1), implemented IMAP
76 ;; extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
77 ;; (NAMESPACE), RFC2359 (UIDPLUS), and the kerberos V4 part of RFC1731
78 ;; (with use of external program `imtest'). It also take advantage
79 ;; the UNSELECT extension in Cyrus IMAPD.
81 ;; Without the work of John McClary Prevost and Jim Radford this library
82 ;; would not have seen the light of day. Many thanks.
84 ;; This is a transcript of short interactive session for demonstration
87 ;; (imap-open "my.mail.server")
88 ;; => " *imap* my.mail.server:0"
90 ;; The rest are invoked with current buffer as the buffer returned by
91 ;; `imap-open'. It is possible to do all without this, but it would
92 ;; look ugly here since `buffer' is always the last argument for all
93 ;; imap.el API functions.
95 ;; (imap-authenticate "myusername" "mypassword")
98 ;; (imap-mailbox-lsub "*")
99 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
101 ;; (imap-mailbox-list "INBOX.n%")
102 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
104 ;; (imap-mailbox-select "INBOX.nnimap")
107 ;; (imap-mailbox-get 'exists)
110 ;; (imap-mailbox-get 'uidvalidity)
113 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
116 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
117 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
121 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
122 ;; o Don't use `read' at all (important places already fixed)
123 ;; o Accept list of articles instead of message set string in most
124 ;; imap-message-* functions.
125 ;; o Cyrus IMAPd 1.6.x `imtest' support in the imtest wrapper
126 ;; o Format-spec'ify the ssl horror
130 ;; - this is unreleased software
137 (autoload 'open-ssl-stream "ssl")
138 (autoload 'base64-decode-string "base64")
139 (autoload 'rfc2104-hash "rfc2104")
140 (autoload 'md5 "md5")
141 (autoload 'utf7-encode "utf7")
142 (autoload 'utf7-decode "utf7")
143 (autoload 'format-spec "format-spec")
144 (autoload 'format-spec-make "format-spec"))
148 (defvar imap-imtest-program "imtest -kp %s %p"
149 "How to call program for Kerberos 4 authentication.
150 %s is replaced with server and %p with port to connect to. The
151 program should accept IMAP commands on stdin and return responses to
154 (defvar imap-ssl-program 'auto
155 "Program to use for SSL connections. It is called like this
157 `imap-ssl-program' `imap-ssl-arguments' -ssl2 -connect host:port
159 where -ssl2 can also be -ssl3 to indicate which ssl version to use. It
160 should accept IMAP commands on stdin and return responses to stdout.
162 For SSLeay set this to \"s_client\" and `imap-ssl-arguments' to nil,
163 for OpenSSL set this to \"openssl\" and `imap-ssl-arguments' to
166 If 'auto it tries s_client first and then openssl.")
168 (defvar imap-ssl-arguments nil
169 "Arguments to pass to `imap-ssl-program'.
171 For SSLeay set this to nil, for OpenSSL to \"s_client\".
173 If `imap-ssl-program' is 'auto this variable has no effect.")
175 (defvar imap-default-user (user-login-name)
176 "Default username to use.")
178 (defvar imap-error nil
179 "Error codes from the last command.")
181 ;; Various variables.
183 (defvar imap-fetch-data-hook nil
184 "Hooks called after receiving each FETCH response.")
186 (defvar imap-streams '(kerberos4 ssl network)
187 "Priority of streams to consider when opening connection to
190 (defvar imap-stream-alist
191 '((kerberos4 imap-kerberos4s-p imap-kerberos4-open)
192 (ssl imap-ssl-p imap-ssl-open)
193 (network imap-network-p imap-network-open))
194 "Definition of network streams.
198 NAME names the stream, CHECK is a function returning non-nil if the
199 server support the stream and OPEN is a function for opening the
202 (defvar imap-authenticators '(kerberos4 cram-md5 login anonymous)
203 "Priority of authenticators to consider when authenticating to
206 (defvar imap-authenticator-alist
207 '((kerberos4 imap-kerberos4a-p imap-kerberos4-auth)
208 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
209 (login imap-login-p imap-login-auth)
210 (anonymous imap-anonymous-p imap-anonymous-auth))
211 "Definition of authenticators.
213 (NAME CHECK AUTHENTICATE)
215 NAME names the authenticator. CHECK is a function returning non-nil if
216 the server support the authenticator and AUTHENTICATE is a function
217 for doing the actuall authentification.")
219 (defvar imap-utf7-p nil
220 "If non-nil, do utf7 encoding/decoding of mailbox names.
221 Since the UTF7 decoding currently only decodes into ISO-8859-1
222 characters, you may disable this decoding if you need to access UTF7
223 encoded mailboxes which doesn't translate into ISO-8859-1.")
225 ;; Internal constants. Change theese and die.
227 (defconst imap-default-port 143)
228 (defconst imap-default-ssl-port 993)
229 (defconst imap-default-stream 'network)
230 (defconst imap-coding-system-for-read 'binary)
231 (defconst imap-coding-system-for-write 'binary)
232 (defconst imap-local-variables '(imap-server
241 imap-current-target-mailbox
252 ;; Internal variables.
254 (defvar imap-stream nil)
255 (defvar imap-auth nil)
256 (defvar imap-server nil)
257 (defvar imap-port nil)
258 (defvar imap-username nil)
259 (defvar imap-password nil)
260 (defvar imap-state 'closed
261 "IMAP state. Valid states are `closed', `initial', `nonauth',
262 `auth', `selected' and `examine'.")
264 (defvar imap-server-eol "\r\n"
265 "The EOL string sent from the server.")
267 (defvar imap-client-eol "\r\n"
268 "The EOL string we send to the server.")
270 (defvar imap-current-mailbox nil
271 "Current mailbox name.")
273 (defvar imap-current-target-mailbox nil
274 "Current target mailbox for COPY and APPEND commands.")
276 (defvar imap-mailbox-data nil
277 "Obarray with mailbox data.")
279 (defvar imap-mailbox-prime 997
280 "Length of imap-mailbox-data.")
282 (defvar imap-current-message nil
283 "Current message number.")
285 (defvar imap-message-data nil
286 "Obarray with message data.")
288 (defvar imap-message-prime 997
289 "Length of imap-message-data.")
291 (defvar imap-capability nil
292 "Capability for server.")
294 (defvar imap-namespace nil
295 "Namespace for current server.")
297 (defvar imap-reached-tag 0
298 "Lower limit on command tags that have been parsed.")
300 (defvar imap-failed-tags nil
301 "Alist of tags that failed. Each element is a list with four
302 elements; tag (a integer), response state (a symbol, `OK', `NO' or
303 `BAD'), response code (a string), and human readable response text (a
307 "Command tag number.")
309 (defvar imap-process nil
312 (defvar imap-continuation nil
313 "Non-nil indicates that the server emitted a continuation request. The
314 actually value is really the text on the continuation line.")
316 (defvar imap-log "*imap-log*"
317 "Imap session trace.")
319 (defvar imap-debug nil;"*imap-debug*"
320 "Random debug spew.")
323 ;; Utility functions:
325 (defsubst imap-disable-multibyte ()
326 "Enable multibyte in the current buffer."
327 (when (fboundp 'set-buffer-multibyte)
328 (set-buffer-multibyte nil)))
330 (defun imap-read-passwd (prompt &rest args)
331 "Read a password using PROMPT. If ARGS, PROMPT is used as an
332 argument to `format'."
333 (let ((prompt (if args
334 (apply 'format prompt args)
336 (funcall (if (or (fboundp 'read-passwd)
338 (fboundp 'read-passwd))
339 (and (load "passwd" t)
340 (fboundp 'read-passwd)))
342 (autoload 'ange-ftp-read-passwd "ange-ftp")
343 'ange-ftp-read-passwd)
346 (defsubst imap-utf7-encode (string)
350 (utf7-encode string t)
352 "imap: Could not UTF7 encode `%s', using it unencoded..."
357 (defsubst imap-utf7-decode (string)
361 (utf7-decode string t)
363 "imap: Could not UTF7 decode `%s', using it undecoded..."
368 (defsubst imap-ok-p (status)
371 (setq imap-error status)
374 (defun imap-error-text (&optional buffer)
375 (with-current-buffer (or buffer (current-buffer))
376 (nth 3 (car imap-failed-tags))))
379 ;; Server functions; stream stuff:
381 (defun imap-kerberos4s-p (buffer)
382 (imap-capability 'AUTH=KERBEROS_V4 buffer))
384 (defun imap-kerberos4-open (name buffer server port)
385 (message "Opening Kerberized IMAP connection...")
386 (let* ((port (or port imap-default-port))
387 (coding-system-for-read imap-coding-system-for-read)
388 (coding-system-for-write imap-coding-system-for-write)
389 (process (start-process
390 name buffer shell-file-name shell-command-switch
393 (format-spec-make ?s server ?p (number-to-string port))))))
395 (with-current-buffer buffer
396 (setq imap-client-eol "\n")
397 ;; Result of authentication is a string: __Full privacy protection__
398 (while (and (memq (process-status process) '(open run))
399 (goto-char (point-min))
400 (not (and (imap-parse-greeting)
401 (re-search-forward "__\\(.*\\)__\n" nil t))))
402 (accept-process-output process 1)
405 (with-current-buffer (get-buffer-create imap-log)
406 (imap-disable-multibyte)
407 (buffer-disable-undo)
408 (goto-char (point-max))
409 (insert-buffer-substring buffer)))
410 (let ((response (match-string 1)))
412 (message "Kerberized IMAP connection: %s" response)
413 (if (and response (let ((case-fold-search nil))
414 (not (string-match "failed" response))))
416 (if (memq (process-status process) '(open run))
417 (imap-send-command-wait "LOGOUT"))
418 (delete-process process)
421 (defun imap-ssl-p (buffer)
424 (defun imap-ssl-open-2 (name buffer server port &optional extra-ssl-args)
425 (let* ((port (or port imap-default-ssl-port))
426 (coding-system-for-read imap-coding-system-for-read)
427 (coding-system-for-write imap-coding-system-for-write)
428 (ssl-program-name imap-ssl-program)
429 (ssl-program-arguments (append imap-ssl-arguments extra-ssl-args
431 (format "%s:%d" server port))))
432 (process (ignore-errors (open-ssl-stream name buffer server port))))
434 (with-current-buffer buffer
435 (goto-char (point-min))
436 (while (and (memq (process-status process) '(open run))
437 (goto-char (point-max))
439 (not (imap-parse-greeting)))
440 (accept-process-output process 1)
443 (with-current-buffer (get-buffer-create imap-log)
444 (imap-disable-multibyte)
445 (buffer-disable-undo)
446 (goto-char (point-max))
447 (insert-buffer-substring buffer)))
449 (when (memq (process-status process) '(open run))
452 (defun imap-ssl-open-1 (name buffer server port &optional extra-ssl-args)
453 (or (and (eq imap-ssl-program 'auto)
454 (let ((imap-ssl-program "s_client")
455 (imap-ssl-arguments nil))
456 (message "imap: Opening IMAP connection with %s %s..."
457 imap-ssl-program (car-safe extra-ssl-args))
458 (imap-ssl-open-2 name buffer server port extra-ssl-args)))
459 (and (eq imap-ssl-program 'auto)
460 (let ((imap-ssl-program "openssl")
461 (imap-ssl-arguments '("s_client")))
462 (message "imap: Opening IMAP connection with %s %s..."
463 imap-ssl-program (car-safe extra-ssl-args))
464 (imap-ssl-open-2 name buffer server port extra-ssl-args)))
465 (and (not (eq imap-ssl-program 'auto))
466 (progn (message "imap: Opening IMAP connection with %s %s..."
467 imap-ssl-program (car-safe extra-ssl-args))
468 (imap-ssl-open-2 name buffer server port extra-ssl-args)))))
470 (defun imap-ssl-open (name buffer server port)
471 (or (imap-ssl-open-1 name buffer server port '("-ssl3"))
472 (imap-ssl-open-1 name buffer server port '("-ssl2"))))
474 (defun imap-network-p (buffer)
477 (defun imap-network-open (name buffer server port)
478 (let* ((port (or port imap-default-port))
479 (coding-system-for-read imap-coding-system-for-read)
480 (coding-system-for-write imap-coding-system-for-write)
481 (process (open-network-stream name buffer server port)))
483 (while (and (memq (process-status process) '(open run))
484 (goto-char (point-min))
485 (not (imap-parse-greeting)))
486 (accept-process-output process 1)
489 (with-current-buffer (get-buffer-create imap-log)
490 (imap-disable-multibyte)
491 (buffer-disable-undo)
492 (goto-char (point-max))
493 (insert-buffer-substring buffer)))
494 (when (memq (process-status process) '(open run))
497 ;; Server functions; authenticator stuff:
499 (defun imap-interactive-login (buffer loginfunc)
500 "Login to server in BUFFER. LOGINFUNC is passed a username and a
501 password, it should return t if it where sucessful authenticating
502 itself to the server, nil otherwise. Returns t if login was
503 successful, nil otherwise."
504 (with-current-buffer buffer
505 (make-variable-buffer-local 'imap-username)
506 (make-variable-buffer-local 'imap-password)
507 (let (user passwd ret)
508 ;; (condition-case ()
509 (while (or (not user) (not passwd))
510 (setq user (or imap-username
511 (read-from-minibuffer
512 (concat "IMAP username for " imap-server ": ")
513 (or user imap-default-user))))
514 (setq passwd (or imap-password
516 (concat "IMAP password for " user "@"
518 (when (and user passwd)
519 (if (funcall loginfunc user passwd)
523 (if (and (not imap-password)
524 (y-or-n-p "Store password for this session? "))
525 (setq imap-password passwd)))
526 (message "Login failed...")
529 ;; (quit (with-current-buffer buffer
532 ;; (error (with-current-buffer buffer
537 (defun imap-kerberos4a-p (buffer)
538 (imap-capability 'AUTH=KERBEROS_V4 buffer))
540 (defun imap-kerberos4-auth (buffer)
541 (eq imap-stream 'kerberos4))
543 (defun imap-cram-md5-p (buffer)
544 (imap-capability 'AUTH=CRAM-MD5 buffer))
546 (defun imap-cram-md5-auth (buffer)
547 "Login to server using the AUTH CRAM-MD5 method."
548 (imap-interactive-login
550 (lambda (user passwd)
552 (imap-send-command-wait
554 "AUTHENTICATE CRAM-MD5"
556 (let* ((decoded (base64-decode-string challenge))
557 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
558 (response (concat user " " hash))
559 (encoded (base64-encode-string response)))
562 (defun imap-login-p (buffer)
563 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))
565 (defun imap-login-auth (buffer)
566 "Login to server using the LOGIN command."
567 (imap-interactive-login buffer
568 (lambda (user passwd)
569 (imap-ok-p (imap-send-command-wait
570 (concat "LOGIN \"" user "\" \""
573 (defun imap-anonymous-p (buffer)
576 (defun imap-anonymous-auth (buffer)
577 (with-current-buffer buffer
578 (imap-ok-p (imap-send-command-wait
579 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
580 (system-name)) "\"")))))
584 (defun imap-open-1 (buffer)
585 (with-current-buffer buffer
587 (setq imap-current-mailbox nil
588 imap-current-message nil
590 imap-process (condition-case ()
591 (funcall (nth 2 (assq imap-stream
593 "imap" buffer imap-server imap-port)
596 (set-process-filter imap-process 'imap-arrival-filter)
597 (set-process-sentinel imap-process 'imap-sentinel)
598 (while (and (eq imap-state 'initial)
599 (memq (process-status imap-process) '(open run)))
600 (message "Waiting for response from %s..." imap-server)
601 (accept-process-output imap-process 1))
602 (message "Waiting for response from %s...done" imap-server)
603 (and (memq (process-status imap-process) '(open run))
606 (defun imap-open (server &optional port stream auth buffer)
607 "Open a IMAP connection to host SERVER at PORT returning a
608 buffer. If PORT is unspecified, a default value is used (143 except
609 for SSL which use 993).
610 STREAM indicates the stream to use, see `imap-streams' for available
611 streams. If nil, it choices the best stream the server is capable of.
612 AUTH indicates authenticator to use, see `imap-authenticators' for
613 available authenticators. If nil, it choices the best stream the
614 server is capable of.
615 BUFFER can be a buffer or a name of a buffer, which is created if
616 necessery. If nil, the buffer name is generated."
617 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
618 (with-current-buffer (get-buffer-create buffer)
619 (if (imap-opened buffer)
621 (mapc 'make-variable-buffer-local imap-local-variables)
622 (imap-disable-multibyte)
623 (buffer-disable-undo)
624 (setq imap-server (or server imap-server))
625 (setq imap-port (or port imap-port))
626 (setq imap-auth (or auth imap-auth))
627 (setq imap-stream (or stream imap-stream))
628 (when (let ((imap-stream (or imap-stream imap-default-stream)))
629 (imap-open-1 buffer))
631 (let (stream-changed)
632 (when (null imap-stream)
633 (let ((streams imap-streams))
634 (while (setq stream (pop streams))
635 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
636 (setq stream-changed (not (eq (or imap-stream
642 (error "Couldn't figure out a stream for server"))))
644 (message "Reconnecting with %s..." imap-stream)
647 (setq imap-capability nil)))
648 (if (imap-opened buffer)
649 ;; Choose authenticator
650 (when (null imap-auth)
651 (let ((auths imap-authenticators))
652 (while (setq auth (pop auths))
653 (if (funcall (nth 1 (assq auth imap-authenticator-alist))
658 (error "Couldn't figure out authenticator for server"))))))
659 (when (imap-opened buffer)
660 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))
663 (defun imap-opened (&optional buffer)
664 "Return non-nil if connection to imap server in BUFFER is open. If
665 BUFFER is nil then the current buffer is used."
666 (and (setq buffer (get-buffer (or buffer (current-buffer))))
667 (buffer-live-p buffer)
668 (with-current-buffer buffer
670 (memq (process-status imap-process) '(open run))))))
672 (defun imap-authenticate (&optional user passwd buffer)
673 "Authenticate to server in BUFFER, using current buffer if nil. It
674 uses the authenticator specified when opening the server. If the
675 authenticator requires username/passwords, they are queried from the
676 user and optionally stored in the buffer. If USER and/or PASSWD is
677 specified, the user will not be questioned and the username and/or
678 password is remembered in the buffer."
679 (with-current-buffer (or buffer (current-buffer))
680 (when (eq imap-state 'nonauth)
681 (make-variable-buffer-local 'imap-username)
682 (make-variable-buffer-local 'imap-password)
683 (if user (setq imap-username user))
684 (if passwd (setq imap-password passwd))
685 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer)
686 (setq imap-state 'auth)))))
688 (defun imap-close (&optional buffer)
689 "Close connection to server in BUFFER. If BUFFER is nil, the current
691 (with-current-buffer (or buffer (current-buffer))
693 (not (imap-ok-p (imap-send-command-wait "LOGOUT")))
694 (message "Server %s didn't let me log out" imap-server))
695 (when (and imap-process
696 (memq (process-status imap-process) '(open run)))
697 (delete-process imap-process))
698 (setq imap-current-mailbox nil
699 imap-current-message nil
704 (defun imap-capability (&optional identifier buffer)
705 "Return a list of identifiers which server in BUFFER support. If
706 IDENTIFIER, return non-nil if it's among the servers capabilities. If
707 BUFFER is nil, the current buffer is assumed."
708 (with-current-buffer (or buffer (current-buffer))
709 (unless imap-capability
710 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
711 (setq imap-capability '(IMAP2))))
713 (memq (intern (upcase (symbol-name identifier))) imap-capability)
716 (defun imap-namespace (&optional buffer)
717 "Return a namespace hierarchy at server in BUFFER. If BUFFER is nil,
718 the current buffer is assumed."
719 (with-current-buffer (or buffer (current-buffer))
720 (unless imap-namespace
721 (when (imap-capability 'NAMESPACE)
722 (imap-send-command-wait "NAMESPACE")))
725 (defun imap-send-command-wait (command &optional buffer)
726 (imap-wait-for-tag (imap-send-command command buffer) buffer))
729 ;; Mailbox functions:
731 (defun imap-mailbox-put (propname value &optional mailbox buffer)
732 (with-current-buffer (or buffer (current-buffer))
733 (if imap-mailbox-data
734 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
736 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
737 propname value mailbox (current-buffer)))
740 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
741 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
744 (defun imap-mailbox-get (propname &optional mailbox buffer)
745 (let ((mailbox (imap-utf7-encode mailbox)))
746 (with-current-buffer (or buffer (current-buffer))
747 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
749 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
750 (with-current-buffer (or buffer (current-buffer))
754 (push (funcall func (if mailbox-decoder
755 (funcall mailbox-decoder (symbol-name s))
756 (symbol-name s))) result))
760 (defun imap-mailbox-map (func &optional buffer)
761 "Map a function across each mailbox in `imap-mailbox-data',
762 returning a list. Function should take a mailbox name (a string) as
764 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
766 (defun imap-current-mailbox (&optional buffer)
767 (with-current-buffer (or buffer (current-buffer))
768 (imap-utf7-decode imap-current-mailbox)))
770 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
771 (and (string= mailbox imap-current-mailbox)
773 (eq imap-state 'examine))
775 (eq imap-state 'selected)))))
777 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
778 (with-current-buffer (or buffer (current-buffer))
779 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
781 (defun imap-mailbox-select-1 (mailbox &optional examine)
782 "Select MAILBOX on server in BUFFER. If EXAMINE is non-nil, do a
784 (if (imap-current-mailbox-p-1 mailbox examine)
786 (setq imap-current-mailbox mailbox)
787 (if (imap-ok-p (imap-send-command-wait
788 (concat (if examine "EXAMINE" "SELECT") " \""
791 (setq imap-message-data (make-vector imap-message-prime 0)
792 imap-state (if examine 'examine 'selected))
793 imap-current-mailbox)
794 ;; Failed SELECT/EXAMINE unselects current mailbox
795 (setq imap-current-mailbox nil))))
797 (defun imap-mailbox-select (mailbox &optional examine buffer)
798 (with-current-buffer (or buffer (current-buffer))
800 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
802 (defun imap-mailbox-examine (mailbox &optional buffer)
803 "Examine MAILBOX on server in BUFFER"
804 (imap-mailbox-select mailbox 'exmine buffer))
806 (defun imap-mailbox-unselect (&optional buffer)
807 "Close current folder in BUFFER, without expunging articles."
808 (with-current-buffer (or buffer (current-buffer))
809 (when (or (eq imap-state 'auth)
810 (and (imap-capability 'UNSELECT)
811 (imap-ok-p (imap-send-command-wait "UNSELECT")))
813 (imap-send-command-wait (concat "EXAMINE \""
816 (imap-ok-p (imap-send-command-wait "CLOSE"))))
817 (setq imap-current-mailbox nil
818 imap-message-data nil
822 (defun imap-mailbox-expunge (&optional buffer)
823 "Expunge articles in current folder in BUFFER. If BUFFER is
824 nil the current buffer is assumed."
825 (with-current-buffer (or buffer (current-buffer))
826 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
827 (imap-ok-p (imap-send-command-wait "EXPUNGE")))))
829 (defun imap-mailbox-close (&optional buffer)
830 "Expunge articles and close current folder in BUFFER. If BUFFER is
831 nil the current buffer is assumed."
832 (with-current-buffer (or buffer (current-buffer))
833 (when (and imap-current-mailbox
834 (imap-ok-p (imap-send-command-wait "CLOSE")))
835 (setq imap-current-mailbox nil
836 imap-message-data nil
840 (defun imap-mailbox-create-1 (mailbox)
841 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
843 (defun imap-mailbox-create (mailbox &optional buffer)
844 "Create MAILBOX on server in BUFFER. If BUFFER is nil the current
846 (with-current-buffer (or buffer (current-buffer))
847 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
849 (defun imap-mailbox-delete (mailbox &optional buffer)
850 "Delete MAILBOX on server in BUFFER. If BUFFER is nil the current
852 (let ((mailbox (imap-utf7-encode mailbox)))
853 (with-current-buffer (or buffer (current-buffer))
855 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
857 (defun imap-mailbox-rename (oldname newname &optional buffer)
858 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER. If BUFFER is
859 nil the current buffer is assumed."
860 (let ((oldname (imap-utf7-encode oldname))
861 (newname (imap-utf7-encode newname)))
862 (with-current-buffer (or buffer (current-buffer))
864 (imap-send-command-wait (list "RENAME \"" oldname "\" "
865 "\"" newname "\""))))))
867 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
868 "Return a list of subscribed mailboxes on server in BUFFER.
869 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
870 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
871 implementation-specific string that has to be passed to lsub command."
872 (with-current-buffer (or buffer (current-buffer))
873 ;; Make sure we know the hierarchy separator for root's hierarchy
874 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
875 (imap-send-command-wait (concat "LIST \"" reference "\" \""
876 (imap-utf7-encode root) "\"")))
877 ;; clear list data (NB not delimiter and other stuff)
878 (imap-mailbox-map-1 (lambda (mailbox)
879 (imap-mailbox-put 'lsub nil mailbox)))
881 (imap-send-command-wait
882 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
883 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
886 (imap-mailbox-map-1 (lambda (mailbox)
887 (when (imap-mailbox-get-1 'lsub mailbox)
888 (push (imap-utf7-decode mailbox) out))))
891 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
892 "Return a list of mailboxes matching ROOT on server in BUFFER.
893 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
894 root. REFERENCE is a implementation-specific string that has to be
895 passed to list command."
896 (with-current-buffer (or buffer (current-buffer))
897 ;; Make sure we know the hierarchy separator for root's hierarchy
898 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
899 (imap-send-command-wait (concat "LIST \"" reference "\" \""
900 (imap-utf7-encode root) "\"")))
901 ;; clear list data (NB not delimiter and other stuff)
902 (imap-mailbox-map-1 (lambda (mailbox)
903 (imap-mailbox-put 'list nil mailbox)))
905 (imap-send-command-wait
906 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
907 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
910 (imap-mailbox-map-1 (lambda (mailbox)
911 (when (imap-mailbox-get-1 'list mailbox)
912 (push (imap-utf7-decode mailbox) out))))
915 (defun imap-mailbox-subscribe (mailbox &optional buffer)
916 "Send the SUBSCRIBE command on the mailbox to server in
917 BUFFER. Returns non-nil if successful."
918 (with-current-buffer (or buffer (current-buffer))
919 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
920 (imap-utf7-encode mailbox)
923 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
924 "Send the SUBSCRIBE command on the mailbox to server in
925 BUFFER. Returns non-nil if successful."
926 (with-current-buffer (or buffer (current-buffer))
927 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
928 (imap-utf7-encode mailbox)
931 (defun imap-mailbox-status (mailbox items &optional buffer)
932 "Get status items ITEM in MAILBOX from server in BUFFER. ITEMS can
933 be a symbol or a list of symbols, valid symbols are one of the STATUS
934 data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity or
935 'unseen. If ITEMS is a list of symbols, a list of values is returned,
936 if ITEMS is a symbol only it's value is returned."
937 (with-current-buffer (or buffer (current-buffer))
939 (imap-send-command-wait (list "STATUS \""
940 (imap-utf7-encode mailbox)
947 (mapcar (lambda (item)
948 (imap-mailbox-get-1 item mailbox))
950 (imap-mailbox-get-1 items mailbox)))))
952 (defun imap-mailbox-acl-get (&optional mailbox buffer)
953 "Get ACL on mailbox from server in BUFFER."
954 (let ((mailbox (imap-utf7-encode mailbox)))
955 (with-current-buffer (or buffer (current-buffer))
957 (imap-send-command-wait (list "GETACL \""
958 (or mailbox imap-current-mailbox)
960 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
962 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
963 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in
965 (let ((mailbox (imap-utf7-encode mailbox)))
966 (with-current-buffer (or buffer (current-buffer))
968 (imap-send-command-wait (list "SETACL \""
969 (or mailbox imap-current-mailbox)
975 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
976 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from
978 (let ((mailbox (imap-utf7-encode mailbox)))
979 (with-current-buffer (or buffer (current-buffer))
981 (imap-send-command-wait (list "DELETEACL \""
982 (or mailbox imap-current-mailbox)
987 ;; Message functions:
989 (defun imap-current-message (&optional buffer)
990 (with-current-buffer (or buffer (current-buffer))
991 imap-current-message))
993 (defun imap-list-to-message-set (list)
994 (mapconcat (lambda (item)
995 (number-to-string item))
1001 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1002 (with-current-buffer (or buffer (current-buffer))
1003 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1005 (imap-list-to-message-set uids)
1009 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1010 "Fetch properties PROPS from message set UIDS from server in
1011 BUFFER. UIDS can be a string, number or a list of numbers. If RECEIVE
1012 is non-nil return theese properties."
1013 (with-current-buffer (or buffer (current-buffer))
1014 (when (imap-ok-p (imap-send-command-wait
1015 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1017 (imap-list-to-message-set uids)
1020 (if (or (null receive) (stringp uids))
1023 (mapcar (lambda (uid)
1025 (mapcar (lambda (prop)
1026 (imap-message-get uid prop))
1028 (imap-message-get uid receive)))
1030 (imap-message-get uids receive))))))
1032 (defun imap-message-put (uid propname value &optional buffer)
1033 (with-current-buffer (or buffer (current-buffer))
1034 (if imap-message-data
1035 (put (intern (number-to-string uid) imap-message-data)
1037 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1038 uid propname value (current-buffer)))
1041 (defun imap-message-get (uid propname &optional buffer)
1042 (with-current-buffer (or buffer (current-buffer))
1043 (get (intern-soft (number-to-string uid) imap-message-data)
1046 (defun imap-message-map (func propname &optional buffer)
1047 "Map a function across each mailbox in `imap-message-data',
1049 (with-current-buffer (or buffer (current-buffer))
1053 (push (funcall func (get s 'UID) (get s propname)) result))
1057 (defmacro imap-message-envelope-date (uid &optional buffer)
1058 `(with-current-buffer (or ,buffer (current-buffer))
1059 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1061 (defmacro imap-message-envelope-subject (uid &optional buffer)
1062 `(with-current-buffer (or ,buffer (current-buffer))
1063 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1065 (defmacro imap-message-envelope-from (uid &optional buffer)
1066 `(with-current-buffer (or ,buffer (current-buffer))
1067 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1069 (defmacro imap-message-envelope-sender (uid &optional buffer)
1070 `(with-current-buffer (or ,buffer (current-buffer))
1071 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1073 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1074 `(with-current-buffer (or ,buffer (current-buffer))
1075 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1077 (defmacro imap-message-envelope-to (uid &optional buffer)
1078 `(with-current-buffer (or ,buffer (current-buffer))
1079 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1081 (defmacro imap-message-envelope-cc (uid &optional buffer)
1082 `(with-current-buffer (or ,buffer (current-buffer))
1083 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1085 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1086 `(with-current-buffer (or ,buffer (current-buffer))
1087 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1089 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1090 `(with-current-buffer (or ,buffer (current-buffer))
1091 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1093 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1094 `(with-current-buffer (or ,buffer (current-buffer))
1095 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1097 (defmacro imap-message-body (uid &optional buffer)
1098 `(with-current-buffer (or ,buffer (current-buffer))
1099 (imap-message-get ,uid 'BODY)))
1101 (defun imap-search (predicate &optional buffer)
1102 (with-current-buffer (or buffer (current-buffer))
1103 (imap-mailbox-put 'search 'dummy)
1104 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1105 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1106 (error "Missing SEARCH response to a SEARCH command")
1107 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1109 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1110 "Return t iff FLAG can be permanently (between IMAP sessions) saved
1111 on articles, in MAILBOX on server in BUFFER."
1112 (with-current-buffer (or buffer (current-buffer))
1113 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1114 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1116 (defun imap-message-flags-set (articles flags &optional silent buffer)
1117 (when (and articles flags)
1118 (with-current-buffer (or buffer (current-buffer))
1119 (imap-ok-p (imap-send-command-wait
1120 (concat "UID STORE " articles
1121 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1123 (defun imap-message-flags-del (articles flags &optional silent buffer)
1124 (when (and articles flags)
1125 (with-current-buffer (or buffer (current-buffer))
1126 (imap-ok-p (imap-send-command-wait
1127 (concat "UID STORE " articles
1128 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1130 (defun imap-message-flags-add (articles flags &optional silent buffer)
1131 (when (and articles flags)
1132 (with-current-buffer (or buffer (current-buffer))
1133 (imap-ok-p (imap-send-command-wait
1134 (concat "UID STORE " articles
1135 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1137 (defun imap-message-copyuid-1 (mailbox)
1138 (if (imap-capability 'UIDPLUS)
1139 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1140 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1141 (let ((old-mailbox imap-current-mailbox)
1143 (imap-message-data (make-vector 2 0)))
1144 (when (imap-mailbox-examine mailbox)
1146 (and (imap-fetch "*" "UID")
1147 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1148 (apply 'max (imap-message-map
1149 (lambda (uid prop) uid) 'UID))))
1151 (imap-mailbox-select old-mailbox (eq state 'examine))
1152 (imap-mailbox-unselect)))))))
1154 (defun imap-message-copyuid (mailbox &optional buffer)
1155 (with-current-buffer (or buffer (current-buffer))
1156 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1158 (defun imap-message-copy (articles mailbox
1159 &optional dont-create no-copyuid buffer)
1160 "Copy ARTICLES (a string message set) to MAILBOX on server in
1161 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1162 non-nil, it will not create a mailbox. On success, return a list with
1163 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1164 first element, rest of list contain the saved articles' UIDs."
1166 (with-current-buffer (or buffer (current-buffer))
1167 (let ((mailbox (imap-utf7-encode mailbox)))
1168 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1169 (imap-current-target-mailbox mailbox))
1170 (if (imap-ok-p (imap-send-command-wait cmd))
1172 (when (and (not dont-create)
1173 (imap-mailbox-get-1 'trycreate mailbox))
1174 (imap-mailbox-create-1 mailbox)
1175 (imap-ok-p (imap-send-command-wait cmd)))))
1177 (imap-message-copyuid-1 mailbox)))))))
1179 (defun imap-message-appenduid-1 (mailbox)
1180 (if (imap-capability 'UIDPLUS)
1181 (imap-mailbox-get-1 'appenduid mailbox)
1182 (let ((old-mailbox imap-current-mailbox)
1184 (imap-message-data (make-vector 2 0)))
1185 (when (imap-mailbox-examine mailbox)
1187 (and (imap-fetch "*" "UID")
1188 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1189 (apply 'max (imap-message-map
1190 (lambda (uid prop) uid) 'UID))))
1192 (imap-mailbox-select old-mailbox (eq state 'examine))
1193 (imap-mailbox-unselect)))))))
1195 (defun imap-message-appenduid (mailbox &optional buffer)
1196 (with-current-buffer (or buffer (current-buffer))
1197 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1199 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1200 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER. FLAGS and
1201 DATE-TIME is currently not used. Return a cons holding uidvalidity of
1202 MAILBOX and UID the newly created article got, or nil on failure."
1203 (let ((mailbox (imap-utf7-encode mailbox)))
1204 (with-current-buffer (or buffer (current-buffer))
1205 (and (let ((imap-current-target-mailbox mailbox))
1207 (imap-send-command-wait
1208 (list "APPEND \"" mailbox "\" " article))))
1209 (imap-message-appenduid-1 mailbox)))))
1211 (defun imap-body-lines (body)
1212 "Return number of lines in article by looking at the mime bodystructure
1215 (if (stringp (car body))
1216 (cond ((and (string= (car body) "TEXT")
1217 (numberp (nth 7 body)))
1219 ((and (string= (car body) "MESSAGE")
1220 (numberp (nth 9 body)))
1223 (apply '+ (mapcar 'imap-body-lines body)))
1226 (defun imap-envelope-from (from)
1227 "Return a from string line."
1229 (concat (aref from 0)
1230 (if (aref from 0) " <")
1234 (if (aref from 0) ">"))))
1237 ;; Internal functions.
1239 (defun imap-send-command-1 (cmdstr)
1240 (setq cmdstr (concat cmdstr imap-client-eol))
1242 (with-current-buffer (get-buffer-create imap-log)
1243 (imap-disable-multibyte)
1244 (buffer-disable-undo)
1245 (goto-char (point-max))
1247 (process-send-string imap-process cmdstr))
1249 (defun imap-send-command (command &optional buffer)
1250 (with-current-buffer (or buffer (current-buffer))
1251 (if (not (listp command)) (setq command (list command)))
1252 (let ((tag (setq imap-tag (1+ imap-tag)))
1254 (setq cmdstr (concat (number-to-string imap-tag) " "))
1255 (while (setq cmd (pop command))
1256 (cond ((stringp cmd)
1257 (setq cmdstr (concat cmdstr cmd)))
1260 (concat cmdstr (format "{%d}" (with-current-buffer cmd
1264 (imap-send-command-1 cmdstr)
1266 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1267 (setq command nil) ;; abort command if no cont-req
1268 (let ((process imap-process)
1269 (stream imap-stream))
1270 (with-current-buffer cmd
1271 (when (eq stream 'kerberos4)
1272 ;; XXX modifies buffer!
1273 (goto-char (point-min))
1274 (while (search-forward "\r\n" nil t)
1275 (replace-match "\n")))
1277 (with-current-buffer (get-buffer-create
1279 (imap-disable-multibyte)
1280 (buffer-disable-undo)
1281 (goto-char (point-max))
1282 (insert-buffer-substring cmd)))
1283 (process-send-region process (point-min)
1285 (process-send-string process imap-client-eol))))
1286 (setq imap-continuation nil)))
1288 (imap-send-command-1 cmdstr)
1291 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1292 (setq command nil) ;; abort command if no cont-req
1293 (setq command (cons (funcall cmd imap-continuation)
1295 (setq imap-continuation nil)))
1297 (error "Unknown command type"))))
1299 (imap-send-command-1 cmdstr))
1302 (defun imap-wait-for-tag (tag &optional buffer)
1303 (with-current-buffer (or buffer (current-buffer))
1304 (while (and (null imap-continuation)
1305 (< imap-reached-tag tag))
1306 (or (and (not (memq (process-status imap-process) '(open run)))
1308 (accept-process-output imap-process 1)))
1309 (or (assq tag imap-failed-tags)
1310 (if imap-continuation
1314 (defun imap-sentinel (process string)
1315 (delete-process process))
1317 (defun imap-find-next-line ()
1318 "Return point at end of current line, taking into account
1319 literals. Return nil if no complete line has arrived."
1320 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1323 (if (match-string 1)
1324 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1326 (goto-char (+ (point) (string-to-number (match-string 1))))
1327 (imap-find-next-line))
1330 (defun imap-arrival-filter (proc string)
1331 "IMAP process filter."
1332 (with-current-buffer (process-buffer proc)
1333 (goto-char (point-max))
1336 (with-current-buffer (get-buffer-create imap-log)
1337 (imap-disable-multibyte)
1338 (buffer-disable-undo)
1339 (goto-char (point-max))
1342 (goto-char (point-min))
1343 (while (setq end (imap-find-next-line))
1345 (narrow-to-region (point-min) end)
1346 (delete-backward-char (length imap-server-eol))
1347 (goto-char (point-min))
1349 (cond ((eq imap-state 'initial)
1350 (imap-parse-greeting))
1351 ((or (eq imap-state 'auth)
1352 (eq imap-state 'nonauth)
1353 (eq imap-state 'selected)
1354 (eq imap-state 'examine))
1355 (imap-parse-response))
1357 (message "Unknown state %s in arrival filter"
1359 (delete-region (point-min) (point-max))))))))
1364 (defsubst imap-forward ()
1365 (or (eobp) (forward-char)))
1368 ;; ; Unsigned 32-bit integer
1369 ;; ; (0 <= n < 4,294,967,296)
1371 (defsubst imap-parse-number ()
1372 (when (looking-at "[0-9]+")
1374 (string-to-number (match-string 0))
1375 (goto-char (match-end 0)))))
1377 ;; literal = "{" number "}" CRLF *CHAR8
1378 ;; ; Number represents the number of CHAR8s
1380 (defsubst imap-parse-literal ()
1381 (when (looking-at "{\\([0-9]+\\)}\r\n")
1382 (let ((pos (match-end 0))
1383 (len (string-to-number (match-string 1))))
1384 (if (< (point-max) (+ pos len))
1386 (goto-char (+ pos len))
1387 (buffer-substring-no-properties pos (+ pos len))))))
1389 ;; string = quoted / literal
1391 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1393 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1394 ;; "\" quoted-specials
1396 ;; quoted-specials = DQUOTE / "\"
1398 ;; TEXT-CHAR = <any CHAR except CR and LF>
1400 (defsubst imap-parse-string ()
1401 (let (strstart strend)
1402 (cond ((and (eq (char-after) ?\")
1403 (setq strstart (point))
1404 (setq strend (search-forward "\"" nil t 2)))
1405 (buffer-substring-no-properties (1+ strstart) (1- strend)))
1406 ((eq (char-after) ?{)
1407 (imap-parse-literal)))))
1411 (defsubst imap-parse-nil ()
1412 (if (looking-at "NIL")
1413 (goto-char (match-end 0))))
1415 ;; nstring = string / nil
1417 (defsubst imap-parse-nstring ()
1418 (or (imap-parse-string)
1419 (and (imap-parse-nil)
1422 ;; astring = atom / string
1424 ;; atom = 1*ATOM-CHAR
1426 ;; ATOM-CHAR = <any CHAR except atom-specials>
1428 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1431 ;; list-wildcards = "%" / "*"
1433 ;; quoted-specials = DQUOTE / "\"
1435 (defsubst imap-parse-astring ()
1436 (or (imap-parse-string)
1437 (buffer-substring (point)
1438 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1439 (goto-char (1- (match-end 0)))
1443 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
1446 ;; addr-adl = nstring
1447 ;; ; Holds route from [RFC-822] route-addr if
1450 ;; addr-host = nstring
1451 ;; ; NIL indicates [RFC-822] group syntax.
1452 ;; ; Otherwise, holds [RFC-822] domain name
1454 ;; addr-mailbox = nstring
1455 ;; ; NIL indicates end of [RFC-822] group; if
1456 ;; ; non-NIL and addr-host is NIL, holds
1457 ;; ; [RFC-822] group name.
1458 ;; ; Otherwise, holds [RFC-822] local-part
1459 ;; ; after removing [RFC-822] quoting
1461 ;; addr-name = nstring
1462 ;; ; If non-NIL, holds phrase from [RFC-822]
1463 ;; ; mailbox after removing [RFC-822] quoting
1466 (defsubst imap-parse-address ()
1468 (when (eq (char-after) ?\()
1470 (setq address (vector (prog1 (imap-parse-nstring)
1472 (prog1 (imap-parse-nstring)
1474 (prog1 (imap-parse-nstring)
1476 (imap-parse-nstring)))
1477 (when (eq (char-after) ?\))
1481 ;; address-list = "(" 1*address ")" / nil
1485 (defsubst imap-parse-address-list ()
1486 (if (eq (char-after) ?\()
1487 (let (address addresses)
1489 (while (and (not (eq (char-after) ?\)))
1490 ;; next line for MS Exchange bug
1491 (progn (and (eq (char-after) ? ) (imap-forward)) t)
1492 (setq address (imap-parse-address)))
1493 (setq addresses (cons address addresses)))
1494 (when (eq (char-after) ?\))
1496 (nreverse addresses)))
1497 (assert (imap-parse-nil))))
1499 ;; mailbox = "INBOX" / astring
1500 ;; ; INBOX is case-insensitive. All case variants of
1501 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
1502 ;; ; not as an astring. An astring which consists of
1503 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
1504 ;; ; is considered to be INBOX and not an astring.
1505 ;; ; Refer to section 5.1 for further
1506 ;; ; semantic details of mailbox names.
1508 (defsubst imap-parse-mailbox ()
1509 (let ((mailbox (imap-parse-astring)))
1510 (if (string-equal "INBOX" (upcase mailbox))
1514 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
1516 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
1517 ;; ; Authentication condition
1519 ;; resp-cond-bye = "BYE" SP resp-text
1521 (defun imap-parse-greeting ()
1522 "Parse a IMAP greeting."
1523 (cond ((looking-at "\\* OK ")
1524 (setq imap-state 'nonauth))
1525 ((looking-at "\\* PREAUTH ")
1526 (setq imap-state 'auth))
1527 ((looking-at "\\* BYE ")
1528 (setq imap-state 'closed))))
1530 ;; response = *(continue-req / response-data) response-done
1532 ;; continue-req = "+" SP (resp-text / base64) CRLF
1534 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
1535 ;; mailbox-data / message-data / capability-data) CRLF
1537 ;; response-done = response-tagged / response-fatal
1539 ;; response-fatal = "*" SP resp-cond-bye CRLF
1540 ;; ; Server closes connection immediately
1542 ;; response-tagged = tag SP resp-cond-state CRLF
1544 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
1545 ;; ; Status condition
1547 ;; resp-cond-bye = "BYE" SP resp-text
1549 ;; mailbox-data = "FLAGS" SP flag-list /
1550 ;; "LIST" SP mailbox-list /
1551 ;; "LSUB" SP mailbox-list /
1552 ;; "SEARCH" *(SP nz-number) /
1553 ;; "STATUS" SP mailbox SP "("
1554 ;; [status-att SP number *(SP status-att SP number)] ")" /
1555 ;; number SP "EXISTS" /
1556 ;; number SP "RECENT"
1558 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
1560 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
1562 ;; ; IMAP4rev1 servers which offer RFC 1730
1563 ;; ; compatibility MUST list "IMAP4" as the first
1566 (defun imap-parse-response ()
1567 "Parse a IMAP command response."
1569 (case (setq token (read (current-buffer)))
1570 (+ (setq imap-continuation
1571 (or (buffer-substring (min (point-max) (1+ (point)))
1574 (* (case (prog1 (setq token (read (current-buffer)))
1576 (OK (imap-parse-resp-text))
1577 (NO (imap-parse-resp-text))
1578 (BAD (imap-parse-resp-text))
1579 (BYE (imap-parse-resp-text))
1580 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
1581 (LIST (imap-parse-data-list 'list))
1582 (LSUB (imap-parse-data-list 'lsub))
1583 (SEARCH (imap-mailbox-put
1585 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
1586 (STATUS (imap-parse-status))
1587 (CAPABILITY (setq imap-capability
1588 (read (concat "(" (upcase (buffer-substring
1589 (point) (point-max)))
1591 (ACL (imap-parse-acl))
1592 (t (case (prog1 (read (current-buffer))
1594 (EXISTS (imap-mailbox-put 'exists token))
1595 (RECENT (imap-mailbox-put 'recent token))
1597 (FETCH (imap-parse-fetch token))
1598 (t (message "Garbage: %s" (buffer-string)))))))
1600 (if (not (integerp token))
1601 (message "Garbage: %s" (buffer-string))
1602 (case (prog1 (setq status (read (current-buffer)))
1605 (setq imap-reached-tag (max imap-reached-tag token))
1606 (imap-parse-resp-text)))
1608 (setq imap-reached-tag (max imap-reached-tag token))
1610 (imap-parse-resp-text))
1612 (when (eq (char-after) ?\[)
1613 (setq code (buffer-substring (point)
1614 (search-forward "]")))
1616 (setq text (buffer-substring (point) (point-max)))
1617 (push (list token status code text)
1618 imap-failed-tags))))
1620 (setq imap-reached-tag (max imap-reached-tag token))
1622 (imap-parse-resp-text))
1624 (when (eq (char-after) ?\[)
1625 (setq code (buffer-substring (point)
1626 (search-forward "]")))
1628 (setq text (buffer-substring (point) (point-max)))
1629 (push (list token status code text) imap-failed-tags)
1630 (error "Internal error, tag %s status %s code %s text %s"
1631 token status code text))))
1632 (t (message "Garbage: %s" (buffer-string))))))))))
1634 ;; resp-text = ["[" resp-text-code "]" SP] text
1636 ;; text = 1*TEXT-CHAR
1638 ;; TEXT-CHAR = <any CHAR except CR and LF>
1640 (defun imap-parse-resp-text ()
1641 (imap-parse-resp-text-code))
1643 ;; resp-text-code = "ALERT" /
1644 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
1645 ;; "NEWNAME" SP string SP string /
1647 ;; "PERMANENTFLAGS" SP "("
1648 ;; [flag-perm *(SP flag-perm)] ")" /
1652 ;; "UIDNEXT" SP nz-number /
1653 ;; "UIDVALIDITY" SP nz-number /
1654 ;; "UNSEEN" SP nz-number /
1655 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
1657 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
1659 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
1661 ;; set = sequence-num / (sequence-num ":" sequence-num) /
1663 ;; ; Identifies a set of messages. For message
1664 ;; ; sequence numbers, these are consecutive
1665 ;; ; numbers from 1 to the number of messages in
1667 ;; ; Comma delimits individual numbers, colon
1668 ;; ; delimits between two numbers inclusive.
1669 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
1670 ;; ; 14,15 for a mailbox with 15 messages.
1672 ;; sequence-num = nz-number / "*"
1673 ;; ; * is the largest number in use. For message
1674 ;; ; sequence numbers, it is the number of messages
1675 ;; ; in the mailbox. For unique identifiers, it is
1676 ;; ; the unique identifier of the last message in
1679 ;; flag-perm = flag / "\*"
1681 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1682 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1683 ;; ; Does not include "\Recent"
1685 ;; flag-extension = "\" atom
1686 ;; ; Future expansion. Client implementations
1687 ;; ; MUST accept flag-extension flags. Server
1688 ;; ; implementations MUST NOT generate
1689 ;; ; flag-extension flags except as defined by
1690 ;; ; future standard or standards-track
1691 ;; ; revisions of this specification.
1693 ;; flag-keyword = atom
1695 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
1697 (defun imap-parse-resp-text-code ()
1698 (when (eq (char-after) ?\[)
1700 (cond ((search-forward "PERMANENTFLAGS " nil t)
1701 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
1702 ((search-forward "UIDNEXT " nil t)
1703 (imap-mailbox-put 'uidnext (read (current-buffer))))
1704 ((search-forward "UNSEEN " nil t)
1705 (imap-mailbox-put 'unseen (read (current-buffer))))
1706 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
1707 (imap-mailbox-put 'uidvalidity (match-string 1)))
1708 ((search-forward "READ-ONLY" nil t)
1709 (imap-mailbox-put 'read-only t))
1710 ((search-forward "NEWNAME " nil t)
1711 (let (oldname newname)
1712 (setq oldname (imap-parse-string))
1714 (setq newname (imap-parse-string))
1715 (imap-mailbox-put 'newname newname oldname)))
1716 ((search-forward "TRYCREATE" nil t)
1717 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
1718 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
1719 (imap-mailbox-put 'appenduid
1720 (list (match-string 1)
1721 (string-to-number (match-string 2)))
1722 imap-current-target-mailbox))
1723 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
1724 (imap-mailbox-put 'copyuid (list (match-string 1)
1727 imap-current-target-mailbox))
1728 ((search-forward "ALERT] " nil t)
1729 (message "Imap server %s information: %s" imap-server
1730 (buffer-substring (point) (point-max)))))))
1732 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
1733 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
1735 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
1736 ;; *(SP mbx-list-oflag) /
1737 ;; mbx-list-oflag *(SP mbx-list-oflag)
1739 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
1740 ;; ; Other flags; multiple possible per LIST response
1742 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
1743 ;; ; Selectability flags; only one per LIST response
1745 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1746 ;; "\" quoted-specials
1748 ;; quoted-specials = DQUOTE / "\"
1750 (defun imap-parse-data-list (type)
1751 (let (flags delimiter mailbox)
1752 (setq flags (imap-parse-flag-list))
1753 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
1754 (setq delimiter (match-string 1))
1755 (goto-char (1+ (match-end 0)))
1756 (when (setq mailbox (imap-parse-mailbox))
1757 (imap-mailbox-put type t mailbox)
1758 (imap-mailbox-put 'list-flags flags mailbox)
1759 (imap-mailbox-put 'delimiter delimiter mailbox)))))
1761 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
1762 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
1763 ;; "INTERNALDATE" SPACE date_time /
1764 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
1765 ;; "RFC822.SIZE" SPACE number /
1766 ;; "BODY" ["STRUCTURE"] SPACE body /
1767 ;; "BODY" section ["<" number ">"] SPACE nstring /
1768 ;; "UID" SPACE uniqueid) ")"
1770 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
1771 ;; SPACE time SPACE zone <">
1773 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
1774 ;; ["." (section_text / "MIME")])] "]"
1776 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
1777 ;; SPACE header_list / "TEXT"
1779 ;; header_fld_name ::= astring
1781 ;; header_list ::= "(" 1#header_fld_name ")"
1783 (defsubst imap-parse-header-list ()
1784 (when (eq (char-after) ?\()
1786 (while (not (eq (char-after) ?\)))
1788 (push (imap-parse-astring) strlist))
1790 (nreverse strlist))))
1792 (defsubst imap-parse-fetch-body-section ()
1794 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
1795 (if (eq (char-before) ? )
1797 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
1798 (search-forward "]" nil t))
1801 (defun imap-parse-fetch (response)
1802 (when (eq (char-after) ?\()
1803 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
1804 rfc822size body bodydetail bodystructure)
1805 (while (not (eq (char-after) ?\)))
1807 (let ((token (read (current-buffer))))
1809 (cond ((eq token 'UID)
1810 (setq uid (ignore-errors (read (current-buffer)))))
1812 (setq flags (imap-parse-flag-list)))
1813 ((eq token 'ENVELOPE)
1814 (setq envelope (imap-parse-envelope)))
1815 ((eq token 'INTERNALDATE)
1816 (setq internaldate (imap-parse-string)))
1818 (setq rfc822 (imap-parse-nstring)))
1819 ((eq token 'RFC822.HEADER)
1820 (setq rfc822header (imap-parse-nstring)))
1821 ((eq token 'RFC822.TEXT)
1822 (setq rfc822text (imap-parse-nstring)))
1823 ((eq token 'RFC822.SIZE)
1824 (setq rfc822size (read (current-buffer))))
1826 (if (eq (char-before) ?\[)
1828 (upcase (imap-parse-fetch-body-section))
1829 (and (eq (char-after) ?<)
1830 (buffer-substring (1+ (point))
1831 (search-forward ">" nil t)))
1832 (progn (imap-forward)
1833 (imap-parse-nstring)))
1835 (setq body (imap-parse-body))))
1836 ((eq token 'BODYSTRUCTURE)
1837 (setq bodystructure (imap-parse-body))))))
1839 (setq imap-current-message uid)
1840 (imap-message-put uid 'UID uid)
1841 (and flags (imap-message-put uid 'FLAGS flags))
1842 (and envelope (imap-message-put uid 'ENVELOPE envelope))
1843 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
1844 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
1845 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
1846 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
1847 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
1848 (and body (imap-message-put uid 'BODY body))
1849 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
1850 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
1851 (run-hooks 'imap-fetch-data-hook)))))
1853 ;; mailbox-data = ...
1854 ;; "STATUS" SP mailbox SP "("
1855 ;; [status-att SP number
1856 ;; *(SP status-att SP number)] ")"
1859 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
1862 (defun imap-parse-status ()
1863 (let ((mailbox (imap-parse-mailbox)))
1864 (when (and mailbox (search-forward "(" nil t))
1865 (while (not (eq (char-after) ?\)))
1866 (let ((token (read (current-buffer))))
1867 (cond ((eq token 'MESSAGES)
1868 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
1870 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
1871 ((eq token 'UIDNEXT)
1872 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox))
1873 ((eq token 'UIDVALIDITY)
1874 (and (looking-at " \\([0-9]+\\)")
1875 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox)
1876 (goto-char (match-end 1))))
1878 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
1880 (message "Unknown status data %s in mailbox %s ignored"
1881 token mailbox))))))))
1883 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
1886 ;; identifier ::= astring
1888 ;; rights ::= astring
1890 (defun imap-parse-acl ()
1891 (let ((mailbox (imap-parse-mailbox))
1892 identifier rights acl)
1893 (while (eq (char-after) ?\ )
1895 (setq identifier (imap-parse-astring))
1897 (setq rights (imap-parse-astring))
1898 (setq acl (append acl (list (cons identifier rights)))))
1899 (imap-mailbox-put 'acl acl mailbox)))
1901 ;; flag-list = "(" [flag *(SP flag)] ")"
1903 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1904 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1905 ;; ; Does not include "\Recent"
1907 ;; flag-keyword = atom
1909 ;; flag-extension = "\" atom
1910 ;; ; Future expansion. Client implementations
1911 ;; ; MUST accept flag-extension flags. Server
1912 ;; ; implementations MUST NOT generate
1913 ;; ; flag-extension flags except as defined by
1914 ;; ; future standard or standards-track
1915 ;; ; revisions of this specification.
1917 (defun imap-parse-flag-list ()
1918 (let ((str (buffer-substring-no-properties
1919 (point) (search-forward ")" nil t)))
1921 (while (setq pos (string-match "\\\\" str (and pos (+ 2 pos))))
1922 (setq str (replace-match "\\\\" nil t str)))
1923 (mapcar 'symbol-name (read str))))
1925 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
1926 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
1927 ;; env-in-reply-to SP env-message-id ")"
1929 ;; env-bcc = "(" 1*address ")" / nil
1931 ;; env-cc = "(" 1*address ")" / nil
1933 ;; env-date = nstring
1935 ;; env-from = "(" 1*address ")" / nil
1937 ;; env-in-reply-to = nstring
1939 ;; env-message-id = nstring
1941 ;; env-reply-to = "(" 1*address ")" / nil
1943 ;; env-sender = "(" 1*address ")" / nil
1945 ;; env-subject = nstring
1947 ;; env-to = "(" 1*address ")" / nil
1949 (defun imap-parse-envelope ()
1950 (when (eq (char-after) ?\()
1952 (vector (prog1 (imap-parse-nstring) ;; date
1954 (prog1 (imap-parse-nstring) ;; subject
1956 (prog1 (imap-parse-address-list) ;; from
1958 (prog1 (imap-parse-address-list) ;; sender
1960 (prog1 (imap-parse-address-list) ;; reply-to
1962 (prog1 (imap-parse-address-list) ;; to
1964 (prog1 (imap-parse-address-list) ;; cc
1966 (prog1 (imap-parse-address-list) ;; bcc
1968 (prog1 (imap-parse-nstring) ;; in-reply-to
1970 (prog1 (imap-parse-nstring) ;; message-id
1973 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
1975 (defsubst imap-parse-string-list ()
1976 (cond ((eq (char-after) ?\() ;; body-fld-param
1979 (while (setq str (imap-parse-string))
1982 (nreverse strlist)))
1986 ;; body-extension = nstring / number /
1987 ;; "(" body-extension *(SP body-extension) ")"
1988 ;; ; Future expansion. Client implementations
1989 ;; ; MUST accept body-extension fields. Server
1990 ;; ; implementations MUST NOT generate
1991 ;; ; body-extension fields except as defined by
1992 ;; ; future standard or standards-track
1993 ;; ; revisions of this specification.
1995 (defun imap-parse-body-extension ()
1996 (if (eq (char-after) ?\()
1999 (push (imap-parse-body-extension) b-e)
2000 (while (eq (char-after) ?\ )
2002 (push (imap-parse-body-extension) b-e))
2003 (assert (eq (char-after) ?\)))
2006 (or (imap-parse-number)
2007 (imap-parse-nstring))))
2009 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2010 ;; *(SP body-extension)]]
2011 ;; ; MUST NOT be returned on non-extensible
2014 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2015 ;; *(SP body-extension)]]
2016 ;; ; MUST NOT be returned on non-extensible
2019 (defsubst imap-parse-body-ext ()
2021 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2024 (if (eq (char-after) ?\()
2027 (push (imap-parse-string) dsp)
2029 (push (imap-parse-string-list) dsp)
2031 (assert (imap-parse-nil)))
2032 (push (nreverse dsp) ext))
2033 (when (eq (char-after) ?\ ) ;; body-fld-lang
2035 (if (eq (char-after) ?\()
2036 (push (imap-parse-string-list) ext)
2037 (push (imap-parse-nstring) ext))
2038 (while (eq (char-after) ?\ ) ;; body-extension
2040 (setq ext (append (imap-parse-body-extension) ext)))))
2043 ;; body = "(" body-type-1part / body-type-mpart ")"
2045 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2046 ;; *(SP body-extension)]]
2047 ;; ; MUST NOT be returned on non-extensible
2050 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2051 ;; *(SP body-extension)]]
2052 ;; ; MUST NOT be returned on non-extensible
2055 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2056 ;; body-fld-enc SP body-fld-octets
2058 ;; body-fld-desc = nstring
2060 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2062 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2063 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2065 ;; body-fld-id = nstring
2067 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2069 ;; body-fld-lines = number
2071 ;; body-fld-md5 = nstring
2073 ;; body-fld-octets = number
2075 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2077 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2078 ;; [SP body-ext-1part]
2080 ;; body-type-basic = media-basic SP body-fields
2081 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2083 ;; body-type-msg = media-message SP body-fields SP envelope
2084 ;; SP body SP body-fld-lines
2086 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2088 ;; body-type-mpart = 1*body SP media-subtype
2089 ;; [SP body-ext-mpart]
2091 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2092 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2093 ;; ; Defined in [MIME-IMT]
2095 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2096 ;; ; Defined in [MIME-IMT]
2098 ;; media-subtype = string
2099 ;; ; Defined in [MIME-IMT]
2101 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2102 ;; ; Defined in [MIME-IMT]
2104 (defun imap-parse-body ()
2106 (when (eq (char-after) ?\()
2108 (if (eq (char-after) ?\()
2110 (while (and (eq (char-after) ?\()
2111 (setq subbody (imap-parse-body)))
2112 (push subbody body))
2114 (push (imap-parse-string) body) ;; media-subtype
2115 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2117 (if (eq (char-after) ?\() ;; body-fld-param
2118 (push (imap-parse-string-list) body)
2119 (push (and (imap-parse-nil) nil) body))
2121 (append (imap-parse-body-ext) body))) ;; body-ext-...
2122 (assert (eq (char-after) ?\)))
2126 (push (imap-parse-string) body) ;; media-type
2128 (push (imap-parse-string) body) ;; media-subtype
2130 ;; next line for Sun SIMS bug
2131 (and (eq (char-after) ? ) (imap-forward))
2132 (if (eq (char-after) ?\() ;; body-fld-param
2133 (push (imap-parse-string-list) body)
2134 (push (and (imap-parse-nil) nil) body))
2136 (push (imap-parse-nstring) body) ;; body-fld-id
2138 (push (imap-parse-nstring) body) ;; body-fld-desc
2140 (push (imap-parse-string) body) ;; body-fld-enc
2142 (push (imap-parse-number) body) ;; body-fld-octets
2144 ;; ok, we're done parsing the required parts, what comes now is one
2147 ;; envelope (then we're parsing body-type-msg)
2148 ;; body-fld-lines (then we're parsing body-type-text)
2149 ;; body-ext-1part (then we're parsing body-type-basic)
2151 ;; the problem is that the two first are in turn optionally followed
2152 ;; by the third. So we parse the first two here (if there are any)...
2154 (when (eq (char-after) ?\ )
2157 (cond ((eq (char-after) ?\() ;; body-type-msg:
2158 (push (imap-parse-envelope) body) ;; envelope
2160 (push (imap-parse-body) body) ;; body
2162 (push (imap-parse-number) body)) ;; body-fld-lines
2163 ((setq lines (imap-parse-number)) ;; body-type-text:
2164 (push lines body)) ;; body-fld-lines
2166 (backward-char))))) ;; no match...
2168 ;; ...and then parse the third one here...
2170 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2172 (push (imap-parse-nstring) body) ;; body-fld-md5
2173 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part..
2175 (assert (eq (char-after) ?\)))
2179 (when imap-debug ; (untrace-all)
2181 (buffer-disable-undo (get-buffer-create imap-debug))
2182 (mapc (lambda (f) (trace-function-background f imap-debug))
2196 imap-interactive-login
2212 imap-send-command-wait
2217 imap-current-mailbox
2218 imap-current-mailbox-p-1
2219 imap-current-mailbox-p
2220 imap-mailbox-select-1
2222 imap-mailbox-examine
2223 imap-mailbox-unselect
2224 imap-mailbox-expunge
2226 imap-mailbox-create-1
2232 imap-mailbox-subscribe
2233 imap-mailbox-unsubscribe
2235 imap-mailbox-acl-get
2236 imap-mailbox-acl-set
2237 imap-mailbox-acl-delete
2238 imap-current-message
2239 imap-list-to-message-set
2246 imap-message-flag-permanent-p
2247 imap-message-flags-set
2248 imap-message-flags-del
2249 imap-message-flags-add
2250 imap-message-copyuid-1
2251 imap-message-copyuid
2253 imap-message-appenduid-1
2254 imap-message-appenduid
2266 imap-parse-resp-text
2267 imap-parse-resp-text-code
2268 imap-parse-data-list
2272 imap-parse-flag-list
2274 imap-parse-body-extension
2280 ;;; imap.el ends here