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
129 ;; - 19991218 added starttls/digest-md5 patch,
130 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
131 ;; NB! you need SLIM for starttls.el and digest-md5.el
132 ;; - 19991023 commited to pgnus
137 (eval-when-compile (require 'cl))
139 (autoload 'open-ssl-stream "ssl")
140 (autoload 'base64-decode-string "base64")
141 (autoload 'base64-encode-string "base64")
142 (autoload 'starttls-open-stream "starttls")
143 (autoload 'starttls-negotiate "starttls")
144 (autoload 'digest-md5-parse-digest-challenge "digest-md5")
145 (autoload 'digest-md5-digest-response "digest-md5")
146 (autoload 'digest-md5-digest-uri "digest-md5")
147 (autoload 'digest-md5-challenge "digest-md5")
148 (autoload 'rfc2104-hash "rfc2104")
149 (autoload 'md5 "md5")
150 (autoload 'utf7-encode "utf7")
151 (autoload 'utf7-decode "utf7")
152 (autoload 'format-spec "format-spec")
153 (autoload 'format-spec-make "format-spec"))
157 (defvar imap-imtest-program "imtest -kp %s %p"
158 "How to call program for Kerberos 4 authentication.
159 %s is replaced with server and %p with port to connect to. The
160 program should accept IMAP commands on stdin and return responses to
163 (defvar imap-ssl-program '("openssl s_client -ssl3 -connect %s:%p"
164 "openssl s_client -ssl2 -connect %s:%p"
165 "s_client -ssl3 -connect %s:%p"
166 "s_client -ssl2 -connect %s:%p")
167 "A string, or list of strings, containing commands for SSL connections.
168 Within a string, %s is replaced with the server address and %p with
169 port number on server. The program should accept IMAP commands on
170 stdin and return responses to stdout.")
172 (defvar imap-default-user (user-login-name)
173 "Default username to use.")
175 (defvar imap-error nil
176 "Error codes from the last command.")
178 ;; Various variables.
180 (defvar imap-fetch-data-hook nil
181 "Hooks called after receiving each FETCH response.")
183 (defvar imap-streams '(kerberos4 starttls ssl network)
184 "Priority of streams to consider when opening connection to server.")
186 (defvar imap-stream-alist
187 '((kerberos4 imap-kerberos4s-p imap-kerberos4-open)
188 (ssl imap-ssl-p imap-ssl-open)
189 (network imap-network-p imap-network-open)
190 (starttls imap-starttls-p imap-starttls-open))
191 "Definition of network streams.
195 NAME names the stream, CHECK is a function returning non-nil if the
196 server support the stream and OPEN is a function for opening the
199 (defvar imap-authenticators '(kerberos4 digest-md5 cram-md5 login anonymous)
200 "Priority of authenticators to consider when authenticating to server.")
202 (defvar imap-authenticator-alist
203 '((kerberos4 imap-kerberos4a-p imap-kerberos4-auth)
204 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
205 (login imap-login-p imap-login-auth)
206 (anonymous imap-anonymous-p imap-anonymous-auth)
207 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
208 "Definition of authenticators.
210 (NAME CHECK AUTHENTICATE)
212 NAME names the authenticator. CHECK is a function returning non-nil if
213 the server support the authenticator and AUTHENTICATE is a function
214 for doing the actuall authentification.")
216 (defvar imap-use-utf7 t
217 "If non-nil, do utf7 encoding/decoding of mailbox names.
218 Since the UTF7 decoding currently only decodes into ISO-8859-1
219 characters, you may disable this decoding if you need to access UTF7
220 encoded mailboxes which doesn't translate into ISO-8859-1.")
222 ;; Internal constants. Change theese and die.
224 (defconst imap-default-port 143)
225 (defconst imap-default-ssl-port 993)
226 (defconst imap-default-stream 'network)
227 (defconst imap-coding-system-for-read 'binary)
228 (defconst imap-coding-system-for-write 'binary)
229 (defconst imap-local-variables '(imap-server
238 imap-current-target-mailbox
249 ;; Internal variables.
251 (defvar imap-stream nil)
252 (defvar imap-auth nil)
253 (defvar imap-server nil)
254 (defvar imap-port nil)
255 (defvar imap-username nil)
256 (defvar imap-password nil)
257 (defvar imap-state 'closed
259 Valid states are `closed', `initial', `nonauth', `auth', `selected'
262 (defvar imap-server-eol "\r\n"
263 "The EOL string sent from the server.")
265 (defvar imap-client-eol "\r\n"
266 "The EOL string we send to the server.")
268 (defvar imap-current-mailbox nil
269 "Current mailbox name.")
271 (defvar imap-current-target-mailbox nil
272 "Current target mailbox for COPY and APPEND commands.")
274 (defvar imap-mailbox-data nil
275 "Obarray with mailbox data.")
277 (defvar imap-mailbox-prime 997
278 "Length of imap-mailbox-data.")
280 (defvar imap-current-message nil
281 "Current message number.")
283 (defvar imap-message-data nil
284 "Obarray with message data.")
286 (defvar imap-message-prime 997
287 "Length of imap-message-data.")
289 (defvar imap-capability nil
290 "Capability for server.")
292 (defvar imap-namespace nil
293 "Namespace for current server.")
295 (defvar imap-reached-tag 0
296 "Lower limit on command tags that have been parsed.")
298 (defvar imap-failed-tags nil
299 "Alist of tags that failed.
300 Each element is a list with four elements; tag (a integer), response
301 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
302 human readable response text (a string).")
305 "Command tag number.")
307 (defvar imap-process nil
310 (defvar imap-continuation nil
311 "Non-nil indicates that the server emitted a continuation request.
312 The actually value is really the text on the continuation line.")
315 "Imap session trace.")
317 (defvar imap-debug nil ;"*imap-debug*"
318 "Random debug spew.")
321 ;; Utility functions:
323 (defsubst imap-disable-multibyte ()
324 "Enable multibyte in the current buffer."
325 (when (fboundp 'set-buffer-multibyte)
326 (set-buffer-multibyte nil)))
328 (defun imap-read-passwd (prompt &rest args)
329 "Read a password using PROMPT.
330 If ARGS, PROMPT is used as an argument to `format'."
331 (let ((prompt (if args
332 (apply 'format prompt args)
334 (funcall (if (or (fboundp 'read-passwd)
336 (fboundp 'read-passwd))
337 (and (load "passwd" t)
338 (fboundp 'read-passwd)))
340 (autoload 'ange-ftp-read-passwd "ange-ftp")
341 'ange-ftp-read-passwd)
344 (defsubst imap-utf7-encode (string)
348 (utf7-encode string t)
350 "imap: Could not UTF7 encode `%s', using it unencoded..."
355 (defsubst imap-utf7-decode (string)
359 (utf7-decode string t)
361 "imap: Could not UTF7 decode `%s', using it undecoded..."
366 (defsubst imap-ok-p (status)
369 (setq imap-error status)
372 (defun imap-error-text (&optional buffer)
373 (with-current-buffer (or buffer (current-buffer))
374 (nth 3 (car imap-failed-tags))))
377 ;; Server functions; stream stuff:
379 (defun imap-kerberos4s-p (buffer)
380 (imap-capability 'AUTH=KERBEROS_V4 buffer))
382 (defun imap-kerberos4-open (name buffer server port)
383 (message "Opening Kerberized IMAP connection...")
384 (let* ((port (or port imap-default-port))
385 (coding-system-for-read imap-coding-system-for-read)
386 (coding-system-for-write imap-coding-system-for-write)
387 (process (start-process
388 name buffer shell-file-name shell-command-switch
391 (format-spec-make ?s server ?p (number-to-string port))))))
393 (with-current-buffer buffer
394 (setq imap-client-eol "\n")
395 ;; Result of authentication is a string: __Full privacy protection__
396 (while (and (memq (process-status process) '(open run))
397 (goto-char (point-min))
398 (not (and (imap-parse-greeting)
399 (re-search-forward "__\\(.*\\)__\n" nil t))))
400 (accept-process-output process 1)
403 (with-current-buffer (get-buffer-create imap-log)
404 (imap-disable-multibyte)
405 (buffer-disable-undo)
406 (goto-char (point-max))
407 (insert-buffer-substring buffer)))
408 (let ((response (match-string 1)))
410 (message "Kerberized IMAP connection: %s" response)
411 (if (and response (let ((case-fold-search nil))
412 (not (string-match "failed" response))))
414 (if (memq (process-status process) '(open run))
415 (imap-send-command-wait "LOGOUT"))
416 (delete-process process)
419 (defun imap-ssl-p (buffer)
422 (defun imap-ssl-open (name buffer server port)
423 "Open a SSL connection to server."
424 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
425 (list imap-ssl-program)))
427 (while (and (not done) (setq cmd (pop cmds)))
428 (message "imap: Opening SSL connection with `%s'..." cmd)
429 (let* ((port (or port imap-default-ssl-port))
430 (coding-system-for-read imap-coding-system-for-read)
431 (coding-system-for-write imap-coding-system-for-write)
432 (ssl-program-name shell-file-name)
433 (ssl-program-arguments
434 (list shell-command-switch
435 (format-spec cmd (format-spec-make
437 ?p (number-to-string port)))))
439 (when (setq process (ignore-errors (open-ssl-stream
440 name buffer server port)))
441 (with-current-buffer buffer
442 (goto-char (point-min))
443 (while (and (memq (process-status process) '(open run))
444 (goto-char (point-max))
446 (not (imap-parse-greeting)))
447 (accept-process-output process 1)
450 (with-current-buffer (get-buffer-create imap-log)
451 (imap-disable-multibyte)
452 (buffer-disable-undo)
453 (goto-char (point-max))
454 (insert-buffer-substring buffer)))
456 (when (memq (process-status process) '(open run))
457 (setq done process))))))
460 (message "imap: Opening SSL connection with `%s'...done" cmd)
462 (message "imap: Failed opening SSL connection")
465 (defun imap-network-p (buffer)
468 (defun imap-network-open (name buffer server port)
469 (let* ((port (or port imap-default-port))
470 (coding-system-for-read imap-coding-system-for-read)
471 (coding-system-for-write imap-coding-system-for-write)
472 (process (open-network-stream name buffer server port)))
474 (while (and (memq (process-status process) '(open run))
475 (goto-char (point-min))
476 (not (imap-parse-greeting)))
477 (accept-process-output process 1)
480 (with-current-buffer (get-buffer-create imap-log)
481 (imap-disable-multibyte)
482 (buffer-disable-undo)
483 (goto-char (point-max))
484 (insert-buffer-substring buffer)))
485 (when (memq (process-status process) '(open run))
488 (defun imap-starttls-p (buffer)
489 (and (condition-case ()
492 (imap-capability 'STARTTLS buffer)))
494 (defun imap-starttls-open (name buffer server port)
495 (let* ((port (or port imap-default-port))
496 (coding-system-for-read imap-coding-system-for-read)
497 (coding-system-for-write imap-coding-system-for-write)
498 (process (starttls-open-stream name buffer server port)))
500 (while (and (memq (process-status process) '(open run))
501 (goto-char (point-min))
502 (not (imap-parse-greeting)))
503 (accept-process-output process 1)
506 (with-current-buffer (get-buffer-create imap-log)
507 (buffer-disable-undo)
508 (goto-char (point-max))
509 (insert-buffer-substring buffer)))
510 (let ((imap-process process))
513 (set-process-filter imap-process 'imap-arrival-filter)
514 (when (and (eq imap-stream 'starttls)
515 (imap-ok-p (imap-send-command-wait "STARTTLS")))
516 (starttls-negotiate imap-process)))
517 (set-process-filter imap-process nil)))
518 (when (memq (process-status process) '(open run))
521 ;; Server functions; authenticator stuff:
523 (defun imap-interactive-login (buffer loginfunc)
524 "Login to server in BUFFER.
525 LOGINFUNC is passed a username and a password, it should return t if
526 it where sucessful authenticating itself to the server, nil otherwise.
527 Returns t if login was successful, nil otherwise."
528 (with-current-buffer buffer
529 (make-variable-buffer-local 'imap-username)
530 (make-variable-buffer-local 'imap-password)
531 (let (user passwd ret)
532 ;; (condition-case ()
533 (while (or (not user) (not passwd))
534 (setq user (or imap-username
535 (read-from-minibuffer
536 (concat "IMAP username for " imap-server ": ")
537 (or user imap-default-user))))
538 (setq passwd (or imap-password
540 (concat "IMAP password for " user "@"
542 (when (and user passwd)
543 (if (funcall loginfunc user passwd)
547 (if (and (not imap-password)
548 (y-or-n-p "Store password for this session? "))
549 (setq imap-password passwd)))
550 (message "Login failed...")
553 ;; (quit (with-current-buffer buffer
556 ;; (error (with-current-buffer buffer
561 (defun imap-kerberos4a-p (buffer)
562 (imap-capability 'AUTH=KERBEROS_V4 buffer))
564 (defun imap-kerberos4-auth (buffer)
565 (eq imap-stream 'kerberos4))
567 (defun imap-cram-md5-p (buffer)
568 (imap-capability 'AUTH=CRAM-MD5 buffer))
570 (defun imap-cram-md5-auth (buffer)
571 "Login to server using the AUTH CRAM-MD5 method."
572 (imap-interactive-login
574 (lambda (user passwd)
576 (imap-send-command-wait
578 "AUTHENTICATE CRAM-MD5"
580 (let* ((decoded (base64-decode-string challenge))
581 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
582 (response (concat user " " hash))
583 (encoded (base64-encode-string response)))
586 (defun imap-login-p (buffer)
587 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))
589 (defun imap-login-auth (buffer)
590 "Login to server using the LOGIN command."
591 (imap-interactive-login buffer
592 (lambda (user passwd)
593 (imap-ok-p (imap-send-command-wait
594 (concat "LOGIN \"" user "\" \""
597 (defun imap-anonymous-p (buffer)
600 (defun imap-anonymous-auth (buffer)
601 (with-current-buffer buffer
602 (imap-ok-p (imap-send-command-wait
603 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
604 (system-name)) "\"")))))
606 (defun imap-digest-md5-p (buffer)
607 (and (condition-case ()
608 (require 'digest-md5)
610 (imap-capability 'AUTH=DIGEST-MD5 buffer)))
612 (defun imap-digest-md5-auth (buffer)
613 "Login to server using the AUTH DIGEST-MD5 method."
614 (imap-interactive-login
616 (lambda (user passwd)
620 "AUTHENTICATE DIGEST-MD5"
622 (digest-md5-parse-digest-challenge
623 (base64-decode-string challenge))
625 (digest-md5-digest-uri
626 "imap" (digest-md5-challenge 'realm)))
628 (digest-md5-digest-response
629 user passwd digest-uri)))
630 (base64-encode-string response 'no-line-break))))
632 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
634 (setq imap-continuation nil)
635 (imap-send-command-1 "")
636 (imap-ok-p (imap-wait-for-tag tag)))))))
640 (defun imap-open-1 (buffer)
641 (with-current-buffer buffer
643 (setq imap-current-mailbox nil
644 imap-current-message nil
646 imap-process (condition-case ()
647 (funcall (nth 2 (assq imap-stream
649 "imap" buffer imap-server imap-port)
652 (set-process-filter imap-process 'imap-arrival-filter)
653 (set-process-sentinel imap-process 'imap-sentinel)
654 (while (and (eq imap-state 'initial)
655 (memq (process-status imap-process) '(open run)))
656 (message "Waiting for response from %s..." imap-server)
657 (accept-process-output imap-process 1))
658 (message "Waiting for response from %s...done" imap-server)
659 (and (memq (process-status imap-process) '(open run))
662 (defun imap-open (server &optional port stream auth buffer)
663 "Open a IMAP connection to host SERVER at PORT returning a buffer.
664 If PORT is unspecified, a default value is used (143 except
665 for SSL which use 993).
666 STREAM indicates the stream to use, see `imap-streams' for available
667 streams. If nil, it choices the best stream the server is capable of.
668 AUTH indicates authenticator to use, see `imap-authenticators' for
669 available authenticators. If nil, it choices the best stream the
670 server is capable of.
671 BUFFER can be a buffer or a name of a buffer, which is created if
672 necessery. If nil, the buffer name is generated."
673 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
674 (with-current-buffer (get-buffer-create buffer)
675 (if (imap-opened buffer)
677 (mapcar 'make-variable-buffer-local imap-local-variables)
678 (imap-disable-multibyte)
679 (buffer-disable-undo)
680 (setq imap-server (or server imap-server))
681 (setq imap-port (or port imap-port))
682 (setq imap-auth (or auth imap-auth))
683 (setq imap-stream (or stream imap-stream))
684 (when (let ((imap-stream (or imap-stream imap-default-stream)))
685 (imap-open-1 buffer))
687 (let (stream-changed)
688 (when (null imap-stream)
689 (let ((streams imap-streams))
690 (while (setq stream (pop streams))
691 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
692 (setq stream-changed (not (eq (or imap-stream
698 (error "Couldn't figure out a stream for server"))))
700 (message "Reconnecting with %s..." imap-stream)
703 (setq imap-capability nil)))
704 (if (imap-opened buffer)
705 ;; Choose authenticator
706 (when (null imap-auth)
707 (let ((auths imap-authenticators))
708 (while (setq auth (pop auths))
709 (if (funcall (nth 1 (assq auth imap-authenticator-alist))
714 (error "Couldn't figure out authenticator for server"))))))
715 (when (imap-opened buffer)
716 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))
719 (defun imap-opened (&optional buffer)
720 "Return non-nil if connection to imap server in BUFFER is open.
721 If BUFFER is nil then the current buffer is used."
722 (and (setq buffer (get-buffer (or buffer (current-buffer))))
723 (buffer-live-p buffer)
724 (with-current-buffer buffer
726 (memq (process-status imap-process) '(open run))))))
728 (defun imap-authenticate (&optional user passwd buffer)
729 "Authenticate to server in BUFFER, using current buffer if nil.
730 It uses the authenticator specified when opening the server. If the
731 authenticator requires username/passwords, they are queried from the
732 user and optionally stored in the buffer. If USER and/or PASSWD is
733 specified, the user will not be questioned and the username and/or
734 password is remembered in the buffer."
735 (with-current-buffer (or buffer (current-buffer))
736 (when (eq imap-state 'nonauth)
737 (make-variable-buffer-local 'imap-username)
738 (make-variable-buffer-local 'imap-password)
739 (if user (setq imap-username user))
740 (if passwd (setq imap-password passwd))
741 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer)
742 (setq imap-state 'auth)))))
744 (defun imap-close (&optional buffer)
745 "Close connection to server in BUFFER.
746 If BUFFER is nil, the current buffer is used."
747 (with-current-buffer (or buffer (current-buffer))
749 (not (imap-ok-p (imap-send-command-wait "LOGOUT")))
750 (message "Server %s didn't let me log out" imap-server))
751 (when (and imap-process
752 (memq (process-status imap-process) '(open run)))
753 (delete-process imap-process))
754 (setq imap-current-mailbox nil
755 imap-current-message nil
760 (defun imap-capability (&optional identifier buffer)
761 "Return a list of identifiers which server in BUFFER support.
762 If IDENTIFIER, return non-nil if it's among the servers capabilities.
763 If BUFFER is nil, the current buffer is assumed."
764 (with-current-buffer (or buffer (current-buffer))
765 (unless imap-capability
766 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
767 (setq imap-capability '(IMAP2))))
769 (memq (intern (upcase (symbol-name identifier))) imap-capability)
772 (defun imap-namespace (&optional buffer)
773 "Return a namespace hierarchy at server in BUFFER.
774 If BUFFER is nil, the current buffer is assumed."
775 (with-current-buffer (or buffer (current-buffer))
776 (unless imap-namespace
777 (when (imap-capability 'NAMESPACE)
778 (imap-send-command-wait "NAMESPACE")))
781 (defun imap-send-command-wait (command &optional buffer)
782 (imap-wait-for-tag (imap-send-command command buffer) buffer))
785 ;; Mailbox functions:
787 (defun imap-mailbox-put (propname value &optional mailbox buffer)
788 (with-current-buffer (or buffer (current-buffer))
789 (if imap-mailbox-data
790 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
792 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
793 propname value mailbox (current-buffer)))
796 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
797 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
800 (defun imap-mailbox-get (propname &optional mailbox buffer)
801 (let ((mailbox (imap-utf7-encode mailbox)))
802 (with-current-buffer (or buffer (current-buffer))
803 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
805 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
806 (with-current-buffer (or buffer (current-buffer))
810 (push (funcall func (if mailbox-decoder
811 (funcall mailbox-decoder (symbol-name s))
812 (symbol-name s))) result))
816 (defun imap-mailbox-map (func &optional buffer)
817 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
818 Function should take a mailbox name (a string) as
820 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
822 (defun imap-current-mailbox (&optional buffer)
823 (with-current-buffer (or buffer (current-buffer))
824 (imap-utf7-decode imap-current-mailbox)))
826 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
827 (and (string= mailbox imap-current-mailbox)
829 (eq imap-state 'examine))
831 (eq imap-state 'selected)))))
833 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
834 (with-current-buffer (or buffer (current-buffer))
835 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
837 (defun imap-mailbox-select-1 (mailbox &optional examine)
838 "Select MAILBOX on server in BUFFER.
839 If EXAMINE is non-nil, do a read-only select."
840 (if (imap-current-mailbox-p-1 mailbox examine)
842 (setq imap-current-mailbox mailbox)
843 (if (imap-ok-p (imap-send-command-wait
844 (concat (if examine "EXAMINE" "SELECT") " \""
847 (setq imap-message-data (make-vector imap-message-prime 0)
848 imap-state (if examine 'examine 'selected))
849 imap-current-mailbox)
850 ;; Failed SELECT/EXAMINE unselects current mailbox
851 (setq imap-current-mailbox nil))))
853 (defun imap-mailbox-select (mailbox &optional examine buffer)
854 (with-current-buffer (or buffer (current-buffer))
856 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
858 (defun imap-mailbox-examine (mailbox &optional buffer)
859 "Examine MAILBOX on server in BUFFER."
860 (imap-mailbox-select mailbox 'exmine buffer))
862 (defun imap-mailbox-unselect (&optional buffer)
863 "Close current folder in BUFFER, without expunging articles."
864 (with-current-buffer (or buffer (current-buffer))
865 (when (or (eq imap-state 'auth)
866 (and (imap-capability 'UNSELECT)
867 (imap-ok-p (imap-send-command-wait "UNSELECT")))
869 (imap-send-command-wait (concat "EXAMINE \""
872 (imap-ok-p (imap-send-command-wait "CLOSE"))))
873 (setq imap-current-mailbox nil
874 imap-message-data nil
878 (defun imap-mailbox-expunge (&optional buffer)
879 "Expunge articles in current folder in BUFFER.
880 If BUFFER is nil the current buffer is assumed."
881 (with-current-buffer (or buffer (current-buffer))
882 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
883 (imap-ok-p (imap-send-command-wait "EXPUNGE")))))
885 (defun imap-mailbox-close (&optional buffer)
886 "Expunge articles and close current folder in BUFFER.
887 If BUFFER is nil the current buffer is assumed."
888 (with-current-buffer (or buffer (current-buffer))
889 (when (and imap-current-mailbox
890 (imap-ok-p (imap-send-command-wait "CLOSE")))
891 (setq imap-current-mailbox nil
892 imap-message-data nil
896 (defun imap-mailbox-create-1 (mailbox)
897 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
899 (defun imap-mailbox-create (mailbox &optional buffer)
900 "Create MAILBOX on server in BUFFER.
901 If BUFFER is nil the current buffer is assumed."
902 (with-current-buffer (or buffer (current-buffer))
903 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
905 (defun imap-mailbox-delete (mailbox &optional buffer)
906 "Delete MAILBOX on server in BUFFER.
907 If BUFFER is nil the current buffer is assumed."
908 (let ((mailbox (imap-utf7-encode mailbox)))
909 (with-current-buffer (or buffer (current-buffer))
911 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
913 (defun imap-mailbox-rename (oldname newname &optional buffer)
914 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
915 If BUFFER is nil the current buffer is assumed."
916 (let ((oldname (imap-utf7-encode oldname))
917 (newname (imap-utf7-encode newname)))
918 (with-current-buffer (or buffer (current-buffer))
920 (imap-send-command-wait (list "RENAME \"" oldname "\" "
921 "\"" newname "\""))))))
923 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
924 "Return a list of subscribed mailboxes on server in BUFFER.
925 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
926 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
927 implementation-specific string that has to be passed to lsub command."
928 (with-current-buffer (or buffer (current-buffer))
929 ;; Make sure we know the hierarchy separator for root's hierarchy
930 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
931 (imap-send-command-wait (concat "LIST \"" reference "\" \""
932 (imap-utf7-encode root) "\"")))
933 ;; clear list data (NB not delimiter and other stuff)
934 (imap-mailbox-map-1 (lambda (mailbox)
935 (imap-mailbox-put 'lsub nil mailbox)))
937 (imap-send-command-wait
938 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
939 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
942 (imap-mailbox-map-1 (lambda (mailbox)
943 (when (imap-mailbox-get-1 'lsub mailbox)
944 (push (imap-utf7-decode mailbox) out))))
947 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
948 "Return a list of mailboxes matching ROOT on server in BUFFER.
949 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
950 root. REFERENCE is a implementation-specific string that has to be
951 passed to list command."
952 (with-current-buffer (or buffer (current-buffer))
953 ;; Make sure we know the hierarchy separator for root's hierarchy
954 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
955 (imap-send-command-wait (concat "LIST \"" reference "\" \""
956 (imap-utf7-encode root) "\"")))
957 ;; clear list data (NB not delimiter and other stuff)
958 (imap-mailbox-map-1 (lambda (mailbox)
959 (imap-mailbox-put 'list nil mailbox)))
961 (imap-send-command-wait
962 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
963 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
966 (imap-mailbox-map-1 (lambda (mailbox)
967 (when (imap-mailbox-get-1 'list mailbox)
968 (push (imap-utf7-decode mailbox) out))))
971 (defun imap-mailbox-subscribe (mailbox &optional buffer)
972 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
973 Returns non-nil if successful."
974 (with-current-buffer (or buffer (current-buffer))
975 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
976 (imap-utf7-encode mailbox)
979 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
980 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
981 Returns non-nil if successful."
982 (with-current-buffer (or buffer (current-buffer))
983 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
984 (imap-utf7-encode mailbox)
987 (defun imap-mailbox-status (mailbox items &optional buffer)
988 "Get status items ITEM in MAILBOX from server in BUFFER.
989 ITEMS can be a symbol or a list of symbols, valid symbols are one of
990 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
991 or 'unseen. If ITEMS is a list of symbols, a list of values is
992 returned, if ITEMS is a symbol only it's value is returned."
993 (with-current-buffer (or buffer (current-buffer))
995 (imap-send-command-wait (list "STATUS \""
996 (imap-utf7-encode mailbox)
1003 (mapcar (lambda (item)
1004 (imap-mailbox-get-1 item mailbox))
1006 (imap-mailbox-get-1 items mailbox)))))
1008 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1009 "Get ACL on mailbox from server in BUFFER."
1010 (let ((mailbox (imap-utf7-encode mailbox)))
1011 (with-current-buffer (or buffer (current-buffer))
1013 (imap-send-command-wait (list "GETACL \""
1014 (or mailbox imap-current-mailbox)
1016 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1018 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1019 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1020 (let ((mailbox (imap-utf7-encode mailbox)))
1021 (with-current-buffer (or buffer (current-buffer))
1023 (imap-send-command-wait (list "SETACL \""
1024 (or mailbox imap-current-mailbox)
1030 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1031 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1032 (let ((mailbox (imap-utf7-encode mailbox)))
1033 (with-current-buffer (or buffer (current-buffer))
1035 (imap-send-command-wait (list "DELETEACL \""
1036 (or mailbox imap-current-mailbox)
1041 ;; Message functions:
1043 (defun imap-current-message (&optional buffer)
1044 (with-current-buffer (or buffer (current-buffer))
1045 imap-current-message))
1047 (defun imap-list-to-message-set (list)
1048 (mapconcat (lambda (item)
1049 (number-to-string item))
1055 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1056 (with-current-buffer (or buffer (current-buffer))
1057 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1059 (imap-list-to-message-set uids)
1063 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1064 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1065 UIDS can be a string, number or a list of numbers. If RECEIVE
1066 is non-nil return theese properties."
1067 (with-current-buffer (or buffer (current-buffer))
1068 (when (imap-ok-p (imap-send-command-wait
1069 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1071 (imap-list-to-message-set uids)
1074 (if (or (null receive) (stringp uids))
1077 (mapcar (lambda (uid)
1079 (mapcar (lambda (prop)
1080 (imap-message-get uid prop))
1082 (imap-message-get uid receive)))
1084 (imap-message-get uids receive))))))
1086 (defun imap-message-put (uid propname value &optional buffer)
1087 (with-current-buffer (or buffer (current-buffer))
1088 (if imap-message-data
1089 (put (intern (number-to-string uid) imap-message-data)
1091 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1092 uid propname value (current-buffer)))
1095 (defun imap-message-get (uid propname &optional buffer)
1096 (with-current-buffer (or buffer (current-buffer))
1097 (get (intern-soft (number-to-string uid) imap-message-data)
1100 (defun imap-message-map (func propname &optional buffer)
1101 "Map a function across each mailbox in `imap-message-data', returning a list."
1102 (with-current-buffer (or buffer (current-buffer))
1106 (push (funcall func (get s 'UID) (get s propname)) result))
1110 (defmacro imap-message-envelope-date (uid &optional buffer)
1111 `(with-current-buffer (or ,buffer (current-buffer))
1112 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1114 (defmacro imap-message-envelope-subject (uid &optional buffer)
1115 `(with-current-buffer (or ,buffer (current-buffer))
1116 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1118 (defmacro imap-message-envelope-from (uid &optional buffer)
1119 `(with-current-buffer (or ,buffer (current-buffer))
1120 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1122 (defmacro imap-message-envelope-sender (uid &optional buffer)
1123 `(with-current-buffer (or ,buffer (current-buffer))
1124 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1126 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1127 `(with-current-buffer (or ,buffer (current-buffer))
1128 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1130 (defmacro imap-message-envelope-to (uid &optional buffer)
1131 `(with-current-buffer (or ,buffer (current-buffer))
1132 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1134 (defmacro imap-message-envelope-cc (uid &optional buffer)
1135 `(with-current-buffer (or ,buffer (current-buffer))
1136 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1138 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1139 `(with-current-buffer (or ,buffer (current-buffer))
1140 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1142 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1143 `(with-current-buffer (or ,buffer (current-buffer))
1144 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1146 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1147 `(with-current-buffer (or ,buffer (current-buffer))
1148 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1150 (defmacro imap-message-body (uid &optional buffer)
1151 `(with-current-buffer (or ,buffer (current-buffer))
1152 (imap-message-get ,uid 'BODY)))
1154 (defun imap-search (predicate &optional buffer)
1155 (with-current-buffer (or buffer (current-buffer))
1156 (imap-mailbox-put 'search 'dummy)
1157 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1158 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1159 (error "Missing SEARCH response to a SEARCH command")
1160 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1162 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1163 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1164 (with-current-buffer (or buffer (current-buffer))
1165 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1166 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1168 (defun imap-message-flags-set (articles flags &optional silent buffer)
1169 (when (and articles flags)
1170 (with-current-buffer (or buffer (current-buffer))
1171 (imap-ok-p (imap-send-command-wait
1172 (concat "UID STORE " articles
1173 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1175 (defun imap-message-flags-del (articles flags &optional silent buffer)
1176 (when (and articles flags)
1177 (with-current-buffer (or buffer (current-buffer))
1178 (imap-ok-p (imap-send-command-wait
1179 (concat "UID STORE " articles
1180 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1182 (defun imap-message-flags-add (articles flags &optional silent buffer)
1183 (when (and articles flags)
1184 (with-current-buffer (or buffer (current-buffer))
1185 (imap-ok-p (imap-send-command-wait
1186 (concat "UID STORE " articles
1187 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1189 (defun imap-message-copyuid-1 (mailbox)
1190 (if (imap-capability 'UIDPLUS)
1191 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1192 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1193 (let ((old-mailbox imap-current-mailbox)
1195 (imap-message-data (make-vector 2 0)))
1196 (when (imap-mailbox-examine mailbox)
1198 (and (imap-fetch "*" "UID")
1199 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1200 (apply 'max (imap-message-map
1201 (lambda (uid prop) uid) 'UID))))
1203 (imap-mailbox-select old-mailbox (eq state 'examine))
1204 (imap-mailbox-unselect)))))))
1206 (defun imap-message-copyuid (mailbox &optional buffer)
1207 (with-current-buffer (or buffer (current-buffer))
1208 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1210 (defun imap-message-copy (articles mailbox
1211 &optional dont-create no-copyuid buffer)
1212 "Copy ARTICLES (a string message set) to MAILBOX on server in
1213 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1214 non-nil, it will not create a mailbox. On success, return a list with
1215 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1216 first element, rest of list contain the saved articles' UIDs."
1218 (with-current-buffer (or buffer (current-buffer))
1219 (let ((mailbox (imap-utf7-encode mailbox)))
1220 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1221 (imap-current-target-mailbox mailbox))
1222 (if (imap-ok-p (imap-send-command-wait cmd))
1224 (when (and (not dont-create)
1225 (imap-mailbox-get-1 'trycreate mailbox))
1226 (imap-mailbox-create-1 mailbox)
1227 (imap-ok-p (imap-send-command-wait cmd)))))
1229 (imap-message-copyuid-1 mailbox)))))))
1231 (defun imap-message-appenduid-1 (mailbox)
1232 (if (imap-capability 'UIDPLUS)
1233 (imap-mailbox-get-1 'appenduid mailbox)
1234 (let ((old-mailbox imap-current-mailbox)
1236 (imap-message-data (make-vector 2 0)))
1237 (when (imap-mailbox-examine mailbox)
1239 (and (imap-fetch "*" "UID")
1240 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1241 (apply 'max (imap-message-map
1242 (lambda (uid prop) uid) 'UID))))
1244 (imap-mailbox-select old-mailbox (eq state 'examine))
1245 (imap-mailbox-unselect)))))))
1247 (defun imap-message-appenduid (mailbox &optional buffer)
1248 (with-current-buffer (or buffer (current-buffer))
1249 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1251 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1252 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1253 FLAGS and DATE-TIME is currently not used. Return a cons holding
1254 uidvalidity of MAILBOX and UID the newly created article got, or nil
1256 (let ((mailbox (imap-utf7-encode mailbox)))
1257 (with-current-buffer (or buffer (current-buffer))
1258 (and (let ((imap-current-target-mailbox mailbox))
1260 (imap-send-command-wait
1261 (list "APPEND \"" mailbox "\" " article))))
1262 (imap-message-appenduid-1 mailbox)))))
1264 (defun imap-body-lines (body)
1265 "Return number of lines in article by looking at the mime bodystructure BODY."
1267 (if (stringp (car body))
1268 (cond ((and (string= (car body) "TEXT")
1269 (numberp (nth 7 body)))
1271 ((and (string= (car body) "MESSAGE")
1272 (numberp (nth 9 body)))
1275 (apply '+ (mapcar 'imap-body-lines body)))
1278 (defun imap-envelope-from (from)
1279 "Return a from string line."
1281 (concat (aref from 0)
1282 (if (aref from 0) " <")
1286 (if (aref from 0) ">"))))
1289 ;; Internal functions.
1291 (defun imap-send-command-1 (cmdstr)
1292 (setq cmdstr (concat cmdstr imap-client-eol))
1294 (with-current-buffer (get-buffer-create imap-log)
1295 (imap-disable-multibyte)
1296 (buffer-disable-undo)
1297 (goto-char (point-max))
1299 (process-send-string imap-process cmdstr))
1301 (defun imap-send-command (command &optional buffer)
1302 (with-current-buffer (or buffer (current-buffer))
1303 (if (not (listp command)) (setq command (list command)))
1304 (let ((tag (setq imap-tag (1+ imap-tag)))
1306 (setq cmdstr (concat (number-to-string imap-tag) " "))
1307 (while (setq cmd (pop command))
1308 (cond ((stringp cmd)
1309 (setq cmdstr (concat cmdstr cmd)))
1312 (concat cmdstr (format "{%d}" (with-current-buffer cmd
1316 (imap-send-command-1 cmdstr)
1318 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1319 (setq command nil);; abort command if no cont-req
1320 (let ((process imap-process)
1321 (stream imap-stream))
1322 (with-current-buffer cmd
1323 (when (eq stream 'kerberos4)
1324 ;; XXX modifies buffer!
1325 (goto-char (point-min))
1326 (while (search-forward "\r\n" nil t)
1327 (replace-match "\n")))
1329 (with-current-buffer (get-buffer-create
1331 (imap-disable-multibyte)
1332 (buffer-disable-undo)
1333 (goto-char (point-max))
1334 (insert-buffer-substring cmd)))
1335 (process-send-region process (point-min)
1337 (process-send-string process imap-client-eol))))
1338 (setq imap-continuation nil)))
1340 (imap-send-command-1 cmdstr)
1343 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1344 (setq command nil);; abort command if no cont-req
1345 (setq command (cons (funcall cmd imap-continuation)
1347 (setq imap-continuation nil)))
1349 (error "Unknown command type"))))
1351 (imap-send-command-1 cmdstr))
1354 (defun imap-wait-for-tag (tag &optional buffer)
1355 (with-current-buffer (or buffer (current-buffer))
1356 (while (and (null imap-continuation)
1357 (< imap-reached-tag tag))
1358 (or (and (not (memq (process-status imap-process) '(open run)))
1360 (accept-process-output imap-process 1)))
1361 (or (assq tag imap-failed-tags)
1362 (if imap-continuation
1366 (defun imap-sentinel (process string)
1367 (delete-process process))
1369 (defun imap-find-next-line ()
1370 "Return point at end of current line, taking into account literals.
1371 Return nil if no complete line has arrived."
1372 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1375 (if (match-string 1)
1376 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1378 (goto-char (+ (point) (string-to-number (match-string 1))))
1379 (imap-find-next-line))
1382 (defun imap-arrival-filter (proc string)
1383 "IMAP process filter."
1384 (with-current-buffer (process-buffer proc)
1385 (goto-char (point-max))
1388 (with-current-buffer (get-buffer-create imap-log)
1389 (imap-disable-multibyte)
1390 (buffer-disable-undo)
1391 (goto-char (point-max))
1394 (goto-char (point-min))
1395 (while (setq end (imap-find-next-line))
1397 (narrow-to-region (point-min) end)
1398 (delete-backward-char (length imap-server-eol))
1399 (goto-char (point-min))
1401 (cond ((eq imap-state 'initial)
1402 (imap-parse-greeting))
1403 ((or (eq imap-state 'auth)
1404 (eq imap-state 'nonauth)
1405 (eq imap-state 'selected)
1406 (eq imap-state 'examine))
1407 (imap-parse-response))
1409 (message "Unknown state %s in arrival filter"
1411 (delete-region (point-min) (point-max))))))))
1416 (defsubst imap-forward ()
1417 (or (eobp) (forward-char)))
1420 ;; ; Unsigned 32-bit integer
1421 ;; ; (0 <= n < 4,294,967,296)
1423 (defsubst imap-parse-number ()
1424 (when (looking-at "[0-9]+")
1426 (string-to-number (match-string 0))
1427 (goto-char (match-end 0)))))
1429 ;; literal = "{" number "}" CRLF *CHAR8
1430 ;; ; Number represents the number of CHAR8s
1432 (defsubst imap-parse-literal ()
1433 (when (looking-at "{\\([0-9]+\\)}\r\n")
1434 (let ((pos (match-end 0))
1435 (len (string-to-number (match-string 1))))
1436 (if (< (point-max) (+ pos len))
1438 (goto-char (+ pos len))
1439 (buffer-substring-no-properties pos (+ pos len))))))
1441 ;; string = quoted / literal
1443 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1445 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1446 ;; "\" quoted-specials
1448 ;; quoted-specials = DQUOTE / "\"
1450 ;; TEXT-CHAR = <any CHAR except CR and LF>
1452 (defsubst imap-parse-string ()
1453 (let (strstart strend)
1454 (cond ((and (eq (char-after) ?\")
1455 (setq strstart (point))
1456 (setq strend (search-forward "\"" nil t 2)))
1457 (buffer-substring-no-properties (1+ strstart) (1- strend)))
1458 ((eq (char-after) ?{)
1459 (imap-parse-literal)))))
1463 (defsubst imap-parse-nil ()
1464 (if (looking-at "NIL")
1465 (goto-char (match-end 0))))
1467 ;; nstring = string / nil
1469 (defsubst imap-parse-nstring ()
1470 (or (imap-parse-string)
1471 (and (imap-parse-nil)
1474 ;; astring = atom / string
1476 ;; atom = 1*ATOM-CHAR
1478 ;; ATOM-CHAR = <any CHAR except atom-specials>
1480 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1483 ;; list-wildcards = "%" / "*"
1485 ;; quoted-specials = DQUOTE / "\"
1487 (defsubst imap-parse-astring ()
1488 (or (imap-parse-string)
1489 (buffer-substring (point)
1490 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1491 (goto-char (1- (match-end 0)))
1495 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
1498 ;; addr-adl = nstring
1499 ;; ; Holds route from [RFC-822] route-addr if
1502 ;; addr-host = nstring
1503 ;; ; NIL indicates [RFC-822] group syntax.
1504 ;; ; Otherwise, holds [RFC-822] domain name
1506 ;; addr-mailbox = nstring
1507 ;; ; NIL indicates end of [RFC-822] group; if
1508 ;; ; non-NIL and addr-host is NIL, holds
1509 ;; ; [RFC-822] group name.
1510 ;; ; Otherwise, holds [RFC-822] local-part
1511 ;; ; after removing [RFC-822] quoting
1513 ;; addr-name = nstring
1514 ;; ; If non-NIL, holds phrase from [RFC-822]
1515 ;; ; mailbox after removing [RFC-822] quoting
1518 (defsubst imap-parse-address ()
1520 (when (eq (char-after) ?\()
1522 (setq address (vector (prog1 (imap-parse-nstring)
1524 (prog1 (imap-parse-nstring)
1526 (prog1 (imap-parse-nstring)
1528 (imap-parse-nstring)))
1529 (when (eq (char-after) ?\))
1533 ;; address-list = "(" 1*address ")" / nil
1537 (defsubst imap-parse-address-list ()
1538 (if (eq (char-after) ?\()
1539 (let (address addresses)
1541 (while (and (not (eq (char-after) ?\)))
1542 ;; next line for MS Exchange bug
1543 (progn (and (eq (char-after) ? ) (imap-forward)) t)
1544 (setq address (imap-parse-address)))
1545 (setq addresses (cons address addresses)))
1546 (when (eq (char-after) ?\))
1548 (nreverse addresses)))
1549 (assert (imap-parse-nil))))
1551 ;; mailbox = "INBOX" / astring
1552 ;; ; INBOX is case-insensitive. All case variants of
1553 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
1554 ;; ; not as an astring. An astring which consists of
1555 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
1556 ;; ; is considered to be INBOX and not an astring.
1557 ;; ; Refer to section 5.1 for further
1558 ;; ; semantic details of mailbox names.
1560 (defsubst imap-parse-mailbox ()
1561 (let ((mailbox (imap-parse-astring)))
1562 (if (string-equal "INBOX" (upcase mailbox))
1566 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
1568 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
1569 ;; ; Authentication condition
1571 ;; resp-cond-bye = "BYE" SP resp-text
1573 (defun imap-parse-greeting ()
1574 "Parse a IMAP greeting."
1575 (cond ((looking-at "\\* OK ")
1576 (setq imap-state 'nonauth))
1577 ((looking-at "\\* PREAUTH ")
1578 (setq imap-state 'auth))
1579 ((looking-at "\\* BYE ")
1580 (setq imap-state 'closed))))
1582 ;; response = *(continue-req / response-data) response-done
1584 ;; continue-req = "+" SP (resp-text / base64) CRLF
1586 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
1587 ;; mailbox-data / message-data / capability-data) CRLF
1589 ;; response-done = response-tagged / response-fatal
1591 ;; response-fatal = "*" SP resp-cond-bye CRLF
1592 ;; ; Server closes connection immediately
1594 ;; response-tagged = tag SP resp-cond-state CRLF
1596 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
1597 ;; ; Status condition
1599 ;; resp-cond-bye = "BYE" SP resp-text
1601 ;; mailbox-data = "FLAGS" SP flag-list /
1602 ;; "LIST" SP mailbox-list /
1603 ;; "LSUB" SP mailbox-list /
1604 ;; "SEARCH" *(SP nz-number) /
1605 ;; "STATUS" SP mailbox SP "("
1606 ;; [status-att SP number *(SP status-att SP number)] ")" /
1607 ;; number SP "EXISTS" /
1608 ;; number SP "RECENT"
1610 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
1612 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
1614 ;; ; IMAP4rev1 servers which offer RFC 1730
1615 ;; ; compatibility MUST list "IMAP4" as the first
1618 (defun imap-parse-response ()
1619 "Parse a IMAP command response."
1621 (case (setq token (read (current-buffer)))
1622 (+ (setq imap-continuation
1623 (or (buffer-substring (min (point-max) (1+ (point)))
1626 (* (case (prog1 (setq token (read (current-buffer)))
1628 (OK (imap-parse-resp-text))
1629 (NO (imap-parse-resp-text))
1630 (BAD (imap-parse-resp-text))
1631 (BYE (imap-parse-resp-text))
1632 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
1633 (LIST (imap-parse-data-list 'list))
1634 (LSUB (imap-parse-data-list 'lsub))
1635 (SEARCH (imap-mailbox-put
1637 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
1638 (STATUS (imap-parse-status))
1639 (CAPABILITY (setq imap-capability
1640 (read (concat "(" (upcase (buffer-substring
1641 (point) (point-max)))
1643 (ACL (imap-parse-acl))
1644 (t (case (prog1 (read (current-buffer))
1646 (EXISTS (imap-mailbox-put 'exists token))
1647 (RECENT (imap-mailbox-put 'recent token))
1649 (FETCH (imap-parse-fetch token))
1650 (t (message "Garbage: %s" (buffer-string)))))))
1652 (if (not (integerp token))
1653 (message "Garbage: %s" (buffer-string))
1654 (case (prog1 (setq status (read (current-buffer)))
1657 (setq imap-reached-tag (max imap-reached-tag token))
1658 (imap-parse-resp-text)))
1660 (setq imap-reached-tag (max imap-reached-tag token))
1662 (imap-parse-resp-text))
1664 (when (eq (char-after) ?\[)
1665 (setq code (buffer-substring (point)
1666 (search-forward "]")))
1668 (setq text (buffer-substring (point) (point-max)))
1669 (push (list token status code text)
1670 imap-failed-tags))))
1672 (setq imap-reached-tag (max imap-reached-tag token))
1674 (imap-parse-resp-text))
1676 (when (eq (char-after) ?\[)
1677 (setq code (buffer-substring (point)
1678 (search-forward "]")))
1680 (setq text (buffer-substring (point) (point-max)))
1681 (push (list token status code text) imap-failed-tags)
1682 (error "Internal error, tag %s status %s code %s text %s"
1683 token status code text))))
1684 (t (message "Garbage: %s" (buffer-string))))))))))
1686 ;; resp-text = ["[" resp-text-code "]" SP] text
1688 ;; text = 1*TEXT-CHAR
1690 ;; TEXT-CHAR = <any CHAR except CR and LF>
1692 (defun imap-parse-resp-text ()
1693 (imap-parse-resp-text-code))
1695 ;; resp-text-code = "ALERT" /
1696 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
1697 ;; "NEWNAME" SP string SP string /
1699 ;; "PERMANENTFLAGS" SP "("
1700 ;; [flag-perm *(SP flag-perm)] ")" /
1704 ;; "UIDNEXT" SP nz-number /
1705 ;; "UIDVALIDITY" SP nz-number /
1706 ;; "UNSEEN" SP nz-number /
1707 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
1709 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
1711 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
1713 ;; set = sequence-num / (sequence-num ":" sequence-num) /
1715 ;; ; Identifies a set of messages. For message
1716 ;; ; sequence numbers, these are consecutive
1717 ;; ; numbers from 1 to the number of messages in
1719 ;; ; Comma delimits individual numbers, colon
1720 ;; ; delimits between two numbers inclusive.
1721 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
1722 ;; ; 14,15 for a mailbox with 15 messages.
1724 ;; sequence-num = nz-number / "*"
1725 ;; ; * is the largest number in use. For message
1726 ;; ; sequence numbers, it is the number of messages
1727 ;; ; in the mailbox. For unique identifiers, it is
1728 ;; ; the unique identifier of the last message in
1731 ;; flag-perm = flag / "\*"
1733 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1734 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1735 ;; ; Does not include "\Recent"
1737 ;; flag-extension = "\" atom
1738 ;; ; Future expansion. Client implementations
1739 ;; ; MUST accept flag-extension flags. Server
1740 ;; ; implementations MUST NOT generate
1741 ;; ; flag-extension flags except as defined by
1742 ;; ; future standard or standards-track
1743 ;; ; revisions of this specification.
1745 ;; flag-keyword = atom
1747 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
1749 (defun imap-parse-resp-text-code ()
1750 (when (eq (char-after) ?\[)
1752 (cond ((search-forward "PERMANENTFLAGS " nil t)
1753 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
1754 ((search-forward "UIDNEXT " nil t)
1755 (imap-mailbox-put 'uidnext (read (current-buffer))))
1756 ((search-forward "UNSEEN " nil t)
1757 (imap-mailbox-put 'unseen (read (current-buffer))))
1758 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
1759 (imap-mailbox-put 'uidvalidity (match-string 1)))
1760 ((search-forward "READ-ONLY" nil t)
1761 (imap-mailbox-put 'read-only t))
1762 ((search-forward "NEWNAME " nil t)
1763 (let (oldname newname)
1764 (setq oldname (imap-parse-string))
1766 (setq newname (imap-parse-string))
1767 (imap-mailbox-put 'newname newname oldname)))
1768 ((search-forward "TRYCREATE" nil t)
1769 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
1770 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
1771 (imap-mailbox-put 'appenduid
1772 (list (match-string 1)
1773 (string-to-number (match-string 2)))
1774 imap-current-target-mailbox))
1775 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
1776 (imap-mailbox-put 'copyuid (list (match-string 1)
1779 imap-current-target-mailbox))
1780 ((search-forward "ALERT] " nil t)
1781 (message "Imap server %s information: %s" imap-server
1782 (buffer-substring (point) (point-max)))))))
1784 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
1785 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
1787 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
1788 ;; *(SP mbx-list-oflag) /
1789 ;; mbx-list-oflag *(SP mbx-list-oflag)
1791 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
1792 ;; ; Other flags; multiple possible per LIST response
1794 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
1795 ;; ; Selectability flags; only one per LIST response
1797 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1798 ;; "\" quoted-specials
1800 ;; quoted-specials = DQUOTE / "\"
1802 (defun imap-parse-data-list (type)
1803 (let (flags delimiter mailbox)
1804 (setq flags (imap-parse-flag-list))
1805 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
1806 (setq delimiter (match-string 1))
1807 (goto-char (1+ (match-end 0)))
1808 (when (setq mailbox (imap-parse-mailbox))
1809 (imap-mailbox-put type t mailbox)
1810 (imap-mailbox-put 'list-flags flags mailbox)
1811 (imap-mailbox-put 'delimiter delimiter mailbox)))))
1813 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
1814 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
1815 ;; "INTERNALDATE" SPACE date_time /
1816 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
1817 ;; "RFC822.SIZE" SPACE number /
1818 ;; "BODY" ["STRUCTURE"] SPACE body /
1819 ;; "BODY" section ["<" number ">"] SPACE nstring /
1820 ;; "UID" SPACE uniqueid) ")"
1822 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
1823 ;; SPACE time SPACE zone <">
1825 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
1826 ;; ["." (section_text / "MIME")])] "]"
1828 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
1829 ;; SPACE header_list / "TEXT"
1831 ;; header_fld_name ::= astring
1833 ;; header_list ::= "(" 1#header_fld_name ")"
1835 (defsubst imap-parse-header-list ()
1836 (when (eq (char-after) ?\()
1838 (while (not (eq (char-after) ?\)))
1840 (push (imap-parse-astring) strlist))
1842 (nreverse strlist))))
1844 (defsubst imap-parse-fetch-body-section ()
1846 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
1847 (if (eq (char-before) ? )
1849 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
1850 (search-forward "]" nil t))
1853 (defun imap-parse-fetch (response)
1854 (when (eq (char-after) ?\()
1855 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
1856 rfc822size body bodydetail bodystructure)
1857 (while (not (eq (char-after) ?\)))
1859 (let ((token (read (current-buffer))))
1861 (cond ((eq token 'UID)
1862 (setq uid (ignore-errors (read (current-buffer)))))
1864 (setq flags (imap-parse-flag-list)))
1865 ((eq token 'ENVELOPE)
1866 (setq envelope (imap-parse-envelope)))
1867 ((eq token 'INTERNALDATE)
1868 (setq internaldate (imap-parse-string)))
1870 (setq rfc822 (imap-parse-nstring)))
1871 ((eq token 'RFC822.HEADER)
1872 (setq rfc822header (imap-parse-nstring)))
1873 ((eq token 'RFC822.TEXT)
1874 (setq rfc822text (imap-parse-nstring)))
1875 ((eq token 'RFC822.SIZE)
1876 (setq rfc822size (read (current-buffer))))
1878 (if (eq (char-before) ?\[)
1880 (upcase (imap-parse-fetch-body-section))
1881 (and (eq (char-after) ?<)
1882 (buffer-substring (1+ (point))
1883 (search-forward ">" nil t)))
1884 (progn (imap-forward)
1885 (imap-parse-nstring)))
1887 (setq body (imap-parse-body))))
1888 ((eq token 'BODYSTRUCTURE)
1889 (setq bodystructure (imap-parse-body))))))
1891 (setq imap-current-message uid)
1892 (imap-message-put uid 'UID uid)
1893 (and flags (imap-message-put uid 'FLAGS flags))
1894 (and envelope (imap-message-put uid 'ENVELOPE envelope))
1895 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
1896 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
1897 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
1898 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
1899 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
1900 (and body (imap-message-put uid 'BODY body))
1901 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
1902 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
1903 (run-hooks 'imap-fetch-data-hook)))))
1905 ;; mailbox-data = ...
1906 ;; "STATUS" SP mailbox SP "("
1907 ;; [status-att SP number
1908 ;; *(SP status-att SP number)] ")"
1911 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
1914 (defun imap-parse-status ()
1915 (let ((mailbox (imap-parse-mailbox)))
1916 (when (and mailbox (search-forward "(" nil t))
1917 (while (not (eq (char-after) ?\)))
1918 (let ((token (read (current-buffer))))
1919 (cond ((eq token 'MESSAGES)
1920 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
1922 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
1923 ((eq token 'UIDNEXT)
1924 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox))
1925 ((eq token 'UIDVALIDITY)
1926 (and (looking-at " \\([0-9]+\\)")
1927 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox)
1928 (goto-char (match-end 1))))
1930 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
1932 (message "Unknown status data %s in mailbox %s ignored"
1933 token mailbox))))))))
1935 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
1938 ;; identifier ::= astring
1940 ;; rights ::= astring
1942 (defun imap-parse-acl ()
1943 (let ((mailbox (imap-parse-mailbox))
1944 identifier rights acl)
1945 (while (eq (char-after) ?\ )
1947 (setq identifier (imap-parse-astring))
1949 (setq rights (imap-parse-astring))
1950 (setq acl (append acl (list (cons identifier rights)))))
1951 (imap-mailbox-put 'acl acl mailbox)))
1953 ;; flag-list = "(" [flag *(SP flag)] ")"
1955 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1956 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1957 ;; ; Does not include "\Recent"
1959 ;; flag-keyword = atom
1961 ;; flag-extension = "\" atom
1962 ;; ; Future expansion. Client implementations
1963 ;; ; MUST accept flag-extension flags. Server
1964 ;; ; implementations MUST NOT generate
1965 ;; ; flag-extension flags except as defined by
1966 ;; ; future standard or standards-track
1967 ;; ; revisions of this specification.
1969 (defun imap-parse-flag-list ()
1970 (let ((str (buffer-substring-no-properties
1971 (point) (search-forward ")" nil t)))
1973 (while (setq pos (string-match "\\\\" str (and pos (+ 2 pos))))
1974 (setq str (replace-match "\\\\" nil t str)))
1975 (mapcar 'symbol-name (read str))))
1977 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
1978 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
1979 ;; env-in-reply-to SP env-message-id ")"
1981 ;; env-bcc = "(" 1*address ")" / nil
1983 ;; env-cc = "(" 1*address ")" / nil
1985 ;; env-date = nstring
1987 ;; env-from = "(" 1*address ")" / nil
1989 ;; env-in-reply-to = nstring
1991 ;; env-message-id = nstring
1993 ;; env-reply-to = "(" 1*address ")" / nil
1995 ;; env-sender = "(" 1*address ")" / nil
1997 ;; env-subject = nstring
1999 ;; env-to = "(" 1*address ")" / nil
2001 (defun imap-parse-envelope ()
2002 (when (eq (char-after) ?\()
2004 (vector (prog1 (imap-parse-nstring);; date
2006 (prog1 (imap-parse-nstring);; subject
2008 (prog1 (imap-parse-address-list);; from
2010 (prog1 (imap-parse-address-list);; sender
2012 (prog1 (imap-parse-address-list);; reply-to
2014 (prog1 (imap-parse-address-list);; to
2016 (prog1 (imap-parse-address-list);; cc
2018 (prog1 (imap-parse-address-list);; bcc
2020 (prog1 (imap-parse-nstring);; in-reply-to
2022 (prog1 (imap-parse-nstring);; message-id
2025 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2027 (defsubst imap-parse-string-list ()
2028 (cond ((eq (char-after) ?\();; body-fld-param
2031 (while (setq str (imap-parse-string))
2034 (nreverse strlist)))
2038 ;; body-extension = nstring / number /
2039 ;; "(" body-extension *(SP body-extension) ")"
2040 ;; ; Future expansion. Client implementations
2041 ;; ; MUST accept body-extension fields. Server
2042 ;; ; implementations MUST NOT generate
2043 ;; ; body-extension fields except as defined by
2044 ;; ; future standard or standards-track
2045 ;; ; revisions of this specification.
2047 (defun imap-parse-body-extension ()
2048 (if (eq (char-after) ?\()
2051 (push (imap-parse-body-extension) b-e)
2052 (while (eq (char-after) ?\ )
2054 (push (imap-parse-body-extension) b-e))
2055 (assert (eq (char-after) ?\)))
2058 (or (imap-parse-number)
2059 (imap-parse-nstring))))
2061 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2062 ;; *(SP body-extension)]]
2063 ;; ; MUST NOT be returned on non-extensible
2066 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2067 ;; *(SP body-extension)]]
2068 ;; ; MUST NOT be returned on non-extensible
2071 (defsubst imap-parse-body-ext ()
2073 (when (eq (char-after) ?\ );; body-fld-dsp
2076 (if (eq (char-after) ?\()
2079 (push (imap-parse-string) dsp)
2081 (push (imap-parse-string-list) dsp)
2083 (assert (imap-parse-nil)))
2084 (push (nreverse dsp) ext))
2085 (when (eq (char-after) ?\ );; body-fld-lang
2087 (if (eq (char-after) ?\()
2088 (push (imap-parse-string-list) ext)
2089 (push (imap-parse-nstring) ext))
2090 (while (eq (char-after) ?\ );; body-extension
2092 (setq ext (append (imap-parse-body-extension) ext)))))
2095 ;; body = "(" body-type-1part / body-type-mpart ")"
2097 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2098 ;; *(SP body-extension)]]
2099 ;; ; MUST NOT be returned on non-extensible
2102 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2103 ;; *(SP body-extension)]]
2104 ;; ; MUST NOT be returned on non-extensible
2107 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2108 ;; body-fld-enc SP body-fld-octets
2110 ;; body-fld-desc = nstring
2112 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2114 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2115 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2117 ;; body-fld-id = nstring
2119 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2121 ;; body-fld-lines = number
2123 ;; body-fld-md5 = nstring
2125 ;; body-fld-octets = number
2127 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2129 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2130 ;; [SP body-ext-1part]
2132 ;; body-type-basic = media-basic SP body-fields
2133 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2135 ;; body-type-msg = media-message SP body-fields SP envelope
2136 ;; SP body SP body-fld-lines
2138 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2140 ;; body-type-mpart = 1*body SP media-subtype
2141 ;; [SP body-ext-mpart]
2143 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2144 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2145 ;; ; Defined in [MIME-IMT]
2147 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2148 ;; ; Defined in [MIME-IMT]
2150 ;; media-subtype = string
2151 ;; ; Defined in [MIME-IMT]
2153 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2154 ;; ; Defined in [MIME-IMT]
2156 (defun imap-parse-body ()
2158 (when (eq (char-after) ?\()
2160 (if (eq (char-after) ?\()
2162 (while (and (eq (char-after) ?\()
2163 (setq subbody (imap-parse-body)))
2164 (push subbody body))
2166 (push (imap-parse-string) body);; media-subtype
2167 (when (eq (char-after) ?\ );; body-ext-mpart:
2169 (if (eq (char-after) ?\();; body-fld-param
2170 (push (imap-parse-string-list) body)
2171 (push (and (imap-parse-nil) nil) body))
2173 (append (imap-parse-body-ext) body)));; body-ext-...
2174 (assert (eq (char-after) ?\)))
2178 (push (imap-parse-string) body);; media-type
2180 (push (imap-parse-string) body);; media-subtype
2182 ;; next line for Sun SIMS bug
2183 (and (eq (char-after) ? ) (imap-forward))
2184 (if (eq (char-after) ?\();; body-fld-param
2185 (push (imap-parse-string-list) body)
2186 (push (and (imap-parse-nil) nil) body))
2188 (push (imap-parse-nstring) body);; body-fld-id
2190 (push (imap-parse-nstring) body);; body-fld-desc
2192 (push (imap-parse-string) body);; body-fld-enc
2194 (push (imap-parse-number) body);; body-fld-octets
2196 ;; ok, we're done parsing the required parts, what comes now is one
2199 ;; envelope (then we're parsing body-type-msg)
2200 ;; body-fld-lines (then we're parsing body-type-text)
2201 ;; body-ext-1part (then we're parsing body-type-basic)
2203 ;; the problem is that the two first are in turn optionally followed
2204 ;; by the third. So we parse the first two here (if there are any)...
2206 (when (eq (char-after) ?\ )
2209 (cond ((eq (char-after) ?\();; body-type-msg:
2210 (push (imap-parse-envelope) body);; envelope
2212 (push (imap-parse-body) body);; body
2214 (push (imap-parse-number) body));; body-fld-lines
2215 ((setq lines (imap-parse-number));; body-type-text:
2216 (push lines body));; body-fld-lines
2218 (backward-char)))));; no match...
2220 ;; ...and then parse the third one here...
2222 (when (eq (char-after) ?\ );; body-ext-1part:
2224 (push (imap-parse-nstring) body);; body-fld-md5
2225 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part..
2227 (assert (eq (char-after) ?\)))
2231 (when imap-debug ; (untrace-all)
2233 (buffer-disable-undo (get-buffer-create imap-debug))
2234 (mapcar (lambda (f) (trace-function-background f imap-debug))
2246 imap-interactive-login
2262 imap-send-command-wait
2267 imap-current-mailbox
2268 imap-current-mailbox-p-1
2269 imap-current-mailbox-p
2270 imap-mailbox-select-1
2272 imap-mailbox-examine
2273 imap-mailbox-unselect
2274 imap-mailbox-expunge
2276 imap-mailbox-create-1
2282 imap-mailbox-subscribe
2283 imap-mailbox-unsubscribe
2285 imap-mailbox-acl-get
2286 imap-mailbox-acl-set
2287 imap-mailbox-acl-delete
2288 imap-current-message
2289 imap-list-to-message-set
2296 imap-message-flag-permanent-p
2297 imap-message-flags-set
2298 imap-message-flags-del
2299 imap-message-flags-add
2300 imap-message-copyuid-1
2301 imap-message-copyuid
2303 imap-message-appenduid-1
2304 imap-message-appenduid
2316 imap-parse-resp-text
2317 imap-parse-resp-text-code
2318 imap-parse-data-list
2322 imap-parse-flag-list
2324 imap-parse-body-extension
2330 ;;; imap.el ends here