1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; imap.el is a elisp library providing an interface for talking to
30 ;; imap.el is roughly divided in two parts, one that parses IMAP
31 ;; responses from the server and storing data into buffer-local
32 ;; variables, and one for utility functions which send commands to
33 ;; server, waits for an answer, and return information. The latter
34 ;; part is layered on top of the previous.
36 ;; The imap.el API consist of the following functions, other functions
37 ;; in this file should not be called directly and the result of doing
38 ;; so are at best undefined.
42 ;; imap-open, imap-opened, imap-authenticate, imap-close,
43 ;; imap-capability, imap-namespace, imap-error-text
47 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
48 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
49 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
50 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
51 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
52 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
53 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
57 ;; imap-fetch-asynch, imap-fetch,
58 ;; imap-current-message, imap-list-to-message-set,
59 ;; imap-message-get, imap-message-map
60 ;; imap-message-envelope-date, imap-message-envelope-subject,
61 ;; imap-message-envelope-from, imap-message-envelope-sender,
62 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
63 ;; imap-message-envelope-cc, imap-message-envelope-bcc
64 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
65 ;; imap-message-body, imap-message-flag-permanent-p
66 ;; imap-message-flags-set, imap-message-flags-del
67 ;; imap-message-flags-add, imap-message-copyuid
68 ;; imap-message-copy, imap-message-appenduid
69 ;; imap-message-append, imap-envelope-from
72 ;; It is my hope that theese commands should be pretty self
73 ;; explanatory for someone that know IMAP. All functions have
74 ;; additional documentation on how to invoke them.
76 ;; imap.el support RFC1730/2060 (IMAP4/IMAP4rev1), implemented IMAP
77 ;; extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
78 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS)
79 ;; (with use of external library starttls.el and program starttls) and
80 ;; the GSSAPI / kerberos V4 sections of RFC1731 (with use of external
81 ;; program `imtest'). It also take advantage the UNSELECT extension
84 ;; Without the work of John McClary Prevost and Jim Radford this library
85 ;; would not have seen the light of day. Many thanks.
87 ;; This is a transcript of short interactive session for demonstration
90 ;; (imap-open "my.mail.server")
91 ;; => " *imap* my.mail.server:0"
93 ;; The rest are invoked with current buffer as the buffer returned by
94 ;; `imap-open'. It is possible to do all without this, but it would
95 ;; look ugly here since `buffer' is always the last argument for all
96 ;; imap.el API functions.
98 ;; (imap-authenticate "myusername" "mypassword")
101 ;; (imap-mailbox-lsub "*")
102 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
104 ;; (imap-mailbox-list "INBOX.n%")
105 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
107 ;; (imap-mailbox-select "INBOX.nnimap")
110 ;; (imap-mailbox-get 'exists)
113 ;; (imap-mailbox-get 'uidvalidity)
116 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
119 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
120 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
124 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
125 ;; o Don't use `read' at all (important places already fixed)
126 ;; o Accept list of articles instead of message set string in most
127 ;; imap-message-* functions.
131 ;; - 19991218 added starttls/digest-md5 patch,
132 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
133 ;; NB! you need SLIM for starttls.el and digest-md5.el
134 ;; - 19991023 commited to pgnus
139 (eval-when-compile (require 'cl))
140 (eval-when-compile (require 'static))
143 (autoload 'open-ssl-stream "ssl")
144 (autoload 'base64-decode-string "base64")
145 (autoload 'base64-encode-string "base64")
146 (autoload 'starttls-open-stream "starttls")
147 (autoload 'starttls-negotiate "starttls")
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")
154 (autoload 'sasl-digest-md5-digest-response "sasl"))
158 (defvar imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
160 "List of strings containing commands for Kerberos 4 authentication.
161 %s is replaced with server hostname, %p with port to connect to, and
162 %l with the value of `imap-default-user'. The program should accept
163 IMAP commands on stdin and return responses to stdout.")
165 (defvar imap-gssapi-program '("imtest -m gssapi -u %l -p %p %s")
166 "List of strings containing commands for GSSAPI (krb5) authentication.
167 %s is replaced with server hostname, %p with port to connect to, and
168 %l with the value of `imap-default-user'. The program should accept
169 IMAP commands on stdin and return responses to stdout.")
171 (defvar imap-ssl-program '("openssl s_client -ssl3 -connect %s:%p"
172 "openssl s_client -ssl2 -connect %s:%p"
173 "s_client -ssl3 -connect %s:%p"
174 "s_client -ssl2 -connect %s:%p")
175 "A string, or list of strings, containing commands for SSL connections.
176 Within a string, %s is replaced with the server address and %p with
177 port number on server. The program should accept IMAP commands on
178 stdin and return responses to stdout.")
180 (defvar imap-default-user (user-login-name)
181 "Default username to use.")
183 (defvar imap-error nil
184 "Error codes from the last command.")
186 ;; Various variables.
188 (defvar imap-fetch-data-hook nil
189 "Hooks called after receiving each FETCH response.")
191 (defvar imap-streams '(gssapi kerberos4 starttls ssl network)
192 "Priority of streams to consider when opening connection to server.")
194 (defvar imap-stream-alist
195 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
196 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
197 (ssl imap-ssl-p imap-ssl-open)
198 (network imap-network-p imap-network-open)
199 (starttls imap-starttls-p imap-starttls-open))
200 "Definition of network streams.
204 NAME names the stream, CHECK is a function returning non-nil if the
205 server support the stream and OPEN is a function for opening the
208 (defvar imap-authenticators '(gssapi
214 "Priority of authenticators to consider when authenticating to server.")
216 (defvar imap-authenticator-alist
217 '((gssapi imap-gssapi-auth-p imap-gssapia-auth)
218 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
219 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
220 (login imap-login-p imap-login-auth)
221 (anonymous imap-anonymous-p imap-anonymous-auth)
222 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
223 "Definition of authenticators.
225 \(NAME CHECK AUTHENTICATE)
227 NAME names the authenticator. CHECK is a function returning non-nil if
228 the server support the authenticator and AUTHENTICATE is a function
229 for doing the actuall authentification.")
231 (defvar imap-use-utf7 t
232 "If non-nil, do utf7 encoding/decoding of mailbox names.
233 Since the UTF7 decoding currently only decodes into ISO-8859-1
234 characters, you may disable this decoding if you need to access UTF7
235 encoded mailboxes which doesn't translate into ISO-8859-1.")
237 ;; Internal constants. Change theese and die.
239 (defconst imap-default-port 143)
240 (defconst imap-default-ssl-port 993)
241 (defconst imap-default-stream 'network)
242 (defconst imap-local-variables '(imap-server
251 imap-current-target-mailbox
262 ;; Internal variables.
264 (defvar imap-stream nil)
265 (defvar imap-auth nil)
266 (defvar imap-server nil)
267 (defvar imap-port nil)
268 (defvar imap-username nil)
269 (defvar imap-password nil)
270 (defvar imap-state 'closed
272 Valid states are `closed', `initial', `nonauth', `auth', `selected'
275 (defvar imap-server-eol "\r\n"
276 "The EOL string sent from the server.")
278 (defvar imap-client-eol "\r\n"
279 "The EOL string we send to the server.")
281 (defvar imap-current-mailbox nil
282 "Current mailbox name.")
284 (defvar imap-current-target-mailbox nil
285 "Current target mailbox for COPY and APPEND commands.")
287 (defvar imap-mailbox-data nil
288 "Obarray with mailbox data.")
290 (defvar imap-mailbox-prime 997
291 "Length of imap-mailbox-data.")
293 (defvar imap-current-message nil
294 "Current message number.")
296 (defvar imap-message-data nil
297 "Obarray with message data.")
299 (defvar imap-message-prime 997
300 "Length of imap-message-data.")
302 (defvar imap-capability nil
303 "Capability for server.")
305 (defvar imap-namespace nil
306 "Namespace for current server.")
308 (defvar imap-reached-tag 0
309 "Lower limit on command tags that have been parsed.")
311 (defvar imap-failed-tags nil
312 "Alist of tags that failed.
313 Each element is a list with four elements; tag (a integer), response
314 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
315 human readable response text (a string).")
318 "Command tag number.")
320 (defvar imap-process nil
323 (defvar imap-continuation nil
324 "Non-nil indicates that the server emitted a continuation request.
325 The actually value is really the text on the continuation line.")
328 "Imap session trace.")
330 (defvar imap-debug nil ;"*imap-debug*"
331 "Random debug spew.")
334 ;; Utility functions:
336 (defun imap-read-passwd (prompt &rest args)
337 "Read a password using PROMPT.
338 If ARGS, PROMPT is used as an argument to `format'."
339 (let ((prompt (if args
340 (apply 'format prompt args)
342 (funcall (if (or (fboundp 'read-passwd)
344 (fboundp 'read-passwd))
345 (and (load "passwd" t)
346 (fboundp 'read-passwd)))
348 (autoload 'ange-ftp-read-passwd "ange-ftp")
349 'ange-ftp-read-passwd)
352 (defsubst imap-utf7-encode (string)
356 (utf7-encode string t)
358 "imap: Could not UTF7 encode `%s', using it unencoded..."
363 (defsubst imap-utf7-decode (string)
367 (utf7-decode string t)
369 "imap: Could not UTF7 decode `%s', using it undecoded..."
374 (defsubst imap-ok-p (status)
377 (setq imap-error status)
380 (defun imap-error-text (&optional buffer)
381 (with-current-buffer (or buffer (current-buffer))
382 (nth 3 (car imap-failed-tags))))
385 ;; Server functions; stream stuff:
387 (defun imap-kerberos4-stream-p (buffer)
388 (imap-capability 'AUTH=KERBEROS_V4 buffer))
390 (defun imap-kerberos4-open (name buffer server port)
391 (let ((cmds imap-kerberos4-program)
393 (while (and (not done) (setq cmd (pop cmds)))
394 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
395 (let* ((port (or port imap-default-port))
396 (process (as-binary-process
398 name buffer shell-file-name shell-command-switch
403 ?p (number-to-string port)
404 ?l imap-default-user)))))
407 (with-current-buffer buffer
408 (setq imap-client-eol "\n")
409 (while (and (memq (process-status process) '(open run))
410 (goto-char (point-min))
411 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
412 (or (while (looking-at "^C:")
415 ;; cyrus 1.6 imtest print "S: " before server greeting
416 (or (not (looking-at "S: "))
419 (not (and (imap-parse-greeting)
420 ;; success in imtest < 1.6:
421 (or (re-search-forward
422 "^__\\(.*\\)__\n" nil t)
423 ;; success in imtest 1.6:
425 "^\\(Authenticat.*\\)" nil t))
426 (setq response (match-string 1)))))
427 (accept-process-output process 1)
430 (with-current-buffer (get-buffer-create imap-log)
431 (buffer-disable-undo)
432 (goto-char (point-max))
433 (insert-buffer-substring buffer)))
435 (message "Kerberos 4 IMAP connection: %s" (or response "failed"))
436 (if (and response (let ((case-fold-search nil))
437 (not (string-match "failed" response))))
439 (if (memq (process-status process) '(open run))
440 (imap-send-command-wait "LOGOUT"))
441 (delete-process process)
445 (defun imap-gssapi-stream-p (buffer)
446 (imap-capability 'AUTH=GSSAPI buffer))
448 (defun imap-gssapi-open (name buffer server port)
449 (let ((cmds imap-gssapi-program)
451 (while (and (not done) (setq cmd (pop cmds)))
452 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
453 (let* ((port (or port imap-default-port))
454 (process (as-binary-process
456 name buffer shell-file-name shell-command-switch
461 ?p (number-to-string port)
462 ?l imap-default-user)))))
465 (with-current-buffer buffer
466 (setq imap-client-eol "\n")
467 (while (and (memq (process-status process) '(open run))
468 (goto-char (point-min))
469 ;; cyrus 1.6 imtest print "S: " before server greeting
470 (or (not (looking-at "S: "))
473 (not (and (imap-parse-greeting)
474 ;; success in imtest 1.6:
476 "^\\(Authenticat.*\\)" nil t)
477 (setq response (match-string 1)))))
478 (accept-process-output process 1)
481 (with-current-buffer (get-buffer-create imap-log)
482 (buffer-disable-undo)
483 (goto-char (point-max))
484 (insert-buffer-substring buffer)))
486 (message "GSSAPI IMAP connection: %s" (or response "failed"))
487 (if (and response (let ((case-fold-search nil))
488 (not (string-match "failed" response))))
490 (if (memq (process-status process) '(open run))
491 (imap-send-command-wait "LOGOUT"))
492 (delete-process process)
496 (defun imap-ssl-p (buffer)
499 (defun imap-ssl-open (name buffer server port)
500 "Open a SSL connection to server."
501 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
502 (list imap-ssl-program)))
504 (while (and (not done) (setq cmd (pop cmds)))
505 (message "imap: Opening SSL connection with `%s'..." cmd)
506 (let* ((port (or port imap-default-ssl-port))
507 (ssl-program-name shell-file-name)
508 (ssl-program-arguments
509 (list shell-command-switch
510 (format-spec cmd (format-spec-make
512 ?p (number-to-string port)))))
516 (cond ((eq system-type 'windows-nt)
517 (let (selective-display
518 (coding-system-for-write 'binary)
519 (coding-system-for-read 'raw-text-dos)
520 (output-coding-system 'binary)
521 (input-coding-system 'raw-text-dos))
522 (open-ssl-stream name buffer server port)))
525 (open-ssl-stream name buffer server port))))))
526 (with-current-buffer buffer
527 (goto-char (point-min))
528 (while (and (memq (process-status process) '(open run))
529 (goto-char (point-max))
531 (not (imap-parse-greeting)))
532 (accept-process-output process 1)
535 (with-current-buffer (get-buffer-create imap-log)
536 (buffer-disable-undo)
537 (goto-char (point-max))
538 (insert-buffer-substring buffer)))
540 (when (memq (process-status process) '(open run))
541 (setq done process))))))
544 (message "imap: Opening SSL connection with `%s'...done" cmd)
546 (message "imap: Failed opening SSL connection")
549 (defun imap-network-p (buffer)
552 (defun imap-network-open (name buffer server port)
553 (let* ((port (or port imap-default-port))
554 (process (open-network-stream-as-binary name buffer server port)))
556 (while (and (memq (process-status process) '(open run))
557 (goto-char (point-min))
558 (not (imap-parse-greeting)))
559 (accept-process-output process 1)
562 (with-current-buffer (get-buffer-create imap-log)
563 (buffer-disable-undo)
564 (goto-char (point-max))
565 (insert-buffer-substring buffer)))
566 (when (memq (process-status process) '(open run))
569 (defun imap-starttls-p (buffer)
570 (and (condition-case ()
573 (imap-capability 'STARTTLS buffer)))
575 (defun imap-starttls-open (name buffer server port)
576 (let* ((port (or port imap-default-port))
577 (process (as-binary-process
578 (starttls-open-stream name buffer server port))))
580 (while (and (memq (process-status process) '(open run))
581 (goto-char (point-min))
582 (not (imap-parse-greeting)))
583 (accept-process-output process 1)
586 (with-current-buffer (get-buffer-create imap-log)
587 (buffer-disable-undo)
588 (goto-char (point-max))
589 (insert-buffer-substring buffer)))
590 (let ((imap-process process))
593 (set-process-filter imap-process 'imap-arrival-filter)
594 (when (and (eq imap-stream 'starttls)
595 (imap-ok-p (imap-send-command-wait "STARTTLS")))
596 (starttls-negotiate imap-process)))
597 (set-process-filter imap-process nil)))
598 (when (memq (process-status process) '(open run))
601 ;; Server functions; authenticator stuff:
603 (defun imap-interactive-login (buffer loginfunc)
604 "Login to server in BUFFER.
605 LOGINFUNC is passed a username and a password, it should return t if
606 it where sucessful authenticating itself to the server, nil otherwise.
607 Returns t if login was successful, nil otherwise."
608 (with-current-buffer buffer
609 (make-variable-buffer-local 'imap-username)
610 (make-variable-buffer-local 'imap-password)
611 (let (user passwd ret)
612 ;; (condition-case ()
613 (while (or (not user) (not passwd))
614 (setq user (or imap-username
615 (read-from-minibuffer
616 (concat "IMAP username for " imap-server ": ")
617 (or user imap-default-user))))
618 (setq passwd (or imap-password
620 (concat "IMAP password for " user "@"
622 (when (and user passwd)
623 (if (funcall loginfunc user passwd)
627 (if (and (not imap-password)
628 (y-or-n-p "Store password for this session? "))
629 (setq imap-password passwd)))
630 (message "Login failed...")
633 ;; (quit (with-current-buffer buffer
636 ;; (error (with-current-buffer buffer
641 (defun imap-gssapi-auth-p (buffer)
642 (imap-capability 'AUTH=GSSAPI buffer))
644 (defun imap-gssapi-auth (buffer)
645 (eq imap-stream 'gssapi))
647 (defun imap-kerberos4-auth-p (buffer)
648 (imap-capability 'AUTH=KERBEROS_V4 buffer))
650 (defun imap-kerberos4-auth (buffer)
651 (eq imap-stream 'kerberos4))
653 (defun imap-cram-md5-p (buffer)
654 (imap-capability 'AUTH=CRAM-MD5 buffer))
656 (defun imap-cram-md5-auth (buffer)
657 "Login to server using the AUTH CRAM-MD5 method."
658 (imap-interactive-login
660 (lambda (user passwd)
662 (imap-send-command-wait
664 "AUTHENTICATE CRAM-MD5"
666 (let* ((decoded (base64-decode-string challenge))
667 (hash-function (if (and (featurep 'xemacs)
668 (>= (function-max-args 'md5) 4))
669 (lambda (object &optional start end)
670 (md5 object start end 'binary))
672 (hash (rfc2104-hash hash-function 64 16 passwd decoded))
673 (response (concat user " " hash))
674 (encoded (base64-encode-string response)))
677 (defun imap-login-p (buffer)
678 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))
680 (defun imap-login-auth (buffer)
681 "Login to server using the LOGIN command."
682 (imap-interactive-login buffer
683 (lambda (user passwd)
684 (imap-ok-p (imap-send-command-wait
685 (concat "LOGIN \"" user "\" \""
688 (defun imap-anonymous-p (buffer)
691 (defun imap-anonymous-auth (buffer)
692 (with-current-buffer buffer
693 (imap-ok-p (imap-send-command-wait
694 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
695 (system-name)) "\"")))))
697 (defun imap-digest-md5-p (buffer)
698 (and (condition-case ()
699 (require 'digest-md5)
701 (imap-capability 'AUTH=DIGEST-MD5 buffer)))
703 (defun imap-digest-md5-auth (buffer)
704 "Login to server using the AUTH DIGEST-MD5 method."
705 (imap-interactive-login
707 (lambda (user passwd)
711 "AUTHENTICATE DIGEST-MD5"
713 (base64-encode-string
714 (sasl-digest-md5-digest-response
715 (base64-decode-string challenge)
716 user passwd "imap" imap-server)
718 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
720 (setq imap-continuation nil)
721 (imap-send-command-1 "")
722 (imap-ok-p (imap-wait-for-tag tag)))))))
726 (defun imap-open-1 (buffer)
727 (with-current-buffer buffer
729 (setq imap-current-mailbox nil
730 imap-current-message nil
732 imap-process (condition-case ()
733 (funcall (nth 2 (assq imap-stream
735 "imap" buffer imap-server imap-port)
738 (set-process-filter imap-process 'imap-arrival-filter)
739 (set-process-sentinel imap-process 'imap-sentinel)
740 (while (and (eq imap-state 'initial)
741 (memq (process-status imap-process) '(open run)))
742 (message "Waiting for response from %s..." imap-server)
743 (accept-process-output imap-process 1))
744 (message "Waiting for response from %s...done" imap-server)
745 (and (memq (process-status imap-process) '(open run))
748 (defun imap-open (server &optional port stream auth buffer)
749 "Open a IMAP connection to host SERVER at PORT returning a buffer.
750 If PORT is unspecified, a default value is used (143 except
751 for SSL which use 993).
752 STREAM indicates the stream to use, see `imap-streams' for available
753 streams. If nil, it choices the best stream the server is capable of.
754 AUTH indicates authenticator to use, see `imap-authenticators' for
755 available authenticators. If nil, it choices the best stream the
756 server is capable of.
757 BUFFER can be a buffer or a name of a buffer, which is created if
758 necessery. If nil, the buffer name is generated."
759 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
760 (with-current-buffer (get-buffer-create buffer)
761 (if (imap-opened buffer)
763 (mapcar 'make-variable-buffer-local imap-local-variables)
764 (set-buffer-multibyte nil)
765 (buffer-disable-undo)
766 (setq imap-server (or server imap-server))
767 (setq imap-port (or port imap-port))
768 (setq imap-auth (or auth imap-auth))
769 (setq imap-stream (or stream imap-stream))
770 (when (let ((imap-stream (or imap-stream imap-default-stream)))
771 (imap-open-1 buffer))
773 (let (stream-changed)
774 (when (null imap-stream)
775 (let ((streams imap-streams))
776 (while (setq stream (pop streams))
777 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
778 (setq stream-changed (not (eq (or imap-stream
784 (error "Couldn't figure out a stream for server"))))
786 (message "Reconnecting with %s..." imap-stream)
789 (setq imap-capability nil)))
790 (if (imap-opened buffer)
791 ;; Choose authenticator
792 (when (null imap-auth)
793 (let ((auths imap-authenticators))
794 (while (setq auth (pop auths))
795 (if (funcall (nth 1 (assq auth imap-authenticator-alist))
800 (error "Couldn't figure out authenticator for server"))))))
801 (when (imap-opened buffer)
802 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))
805 (defun imap-opened (&optional buffer)
806 "Return non-nil if connection to imap server in BUFFER is open.
807 If BUFFER is nil then the current buffer is used."
808 (and (setq buffer (get-buffer (or buffer (current-buffer))))
809 (buffer-live-p buffer)
810 (with-current-buffer buffer
812 (memq (process-status imap-process) '(open run))))))
814 (defun imap-authenticate (&optional user passwd buffer)
815 "Authenticate to server in BUFFER, using current buffer if nil.
816 It uses the authenticator specified when opening the server. If the
817 authenticator requires username/passwords, they are queried from the
818 user and optionally stored in the buffer. If USER and/or PASSWD is
819 specified, the user will not be questioned and the username and/or
820 password is remembered in the buffer."
821 (with-current-buffer (or buffer (current-buffer))
822 (when (eq imap-state 'nonauth)
823 (make-variable-buffer-local 'imap-username)
824 (make-variable-buffer-local 'imap-password)
825 (if user (setq imap-username user))
826 (if passwd (setq imap-password passwd))
827 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer)
828 (setq imap-state 'auth)))))
830 (defun imap-close (&optional buffer)
831 "Close connection to server in BUFFER.
832 If BUFFER is nil, the current buffer is used."
833 (with-current-buffer (or buffer (current-buffer))
835 (not (imap-ok-p (imap-send-command-wait "LOGOUT")))
836 (message "Server %s didn't let me log out" imap-server))
837 (when (and imap-process
838 (memq (process-status imap-process) '(open run)))
839 (delete-process imap-process))
840 (setq imap-current-mailbox nil
841 imap-current-message nil
846 (defun imap-capability (&optional identifier buffer)
847 "Return a list of identifiers which server in BUFFER support.
848 If IDENTIFIER, return non-nil if it's among the servers capabilities.
849 If BUFFER is nil, the current buffer is assumed."
850 (with-current-buffer (or buffer (current-buffer))
851 (unless imap-capability
852 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
853 (setq imap-capability '(IMAP2))))
855 (memq (intern (upcase (symbol-name identifier))) imap-capability)
858 (defun imap-namespace (&optional buffer)
859 "Return a namespace hierarchy at server in BUFFER.
860 If BUFFER is nil, the current buffer is assumed."
861 (with-current-buffer (or buffer (current-buffer))
862 (unless imap-namespace
863 (when (imap-capability 'NAMESPACE)
864 (imap-send-command-wait "NAMESPACE")))
867 (defun imap-send-command-wait (command &optional buffer)
868 (imap-wait-for-tag (imap-send-command command buffer) buffer))
871 ;; Mailbox functions:
873 (defun imap-mailbox-put (propname value &optional mailbox buffer)
874 (with-current-buffer (or buffer (current-buffer))
875 (if imap-mailbox-data
876 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
878 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
879 propname value mailbox (current-buffer)))
882 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
883 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
886 (defun imap-mailbox-get (propname &optional mailbox buffer)
887 (let ((mailbox (imap-utf7-encode mailbox)))
888 (with-current-buffer (or buffer (current-buffer))
889 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
891 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
892 (with-current-buffer (or buffer (current-buffer))
896 (push (funcall func (if mailbox-decoder
897 (funcall mailbox-decoder (symbol-name s))
898 (symbol-name s))) result))
902 (defun imap-mailbox-map (func &optional buffer)
903 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
904 Function should take a mailbox name (a string) as
906 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
908 (defun imap-current-mailbox (&optional buffer)
909 (with-current-buffer (or buffer (current-buffer))
910 (imap-utf7-decode imap-current-mailbox)))
912 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
913 (and (string= mailbox imap-current-mailbox)
915 (eq imap-state 'examine))
917 (eq imap-state 'selected)))))
919 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
920 (with-current-buffer (or buffer (current-buffer))
921 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
923 (defun imap-mailbox-select-1 (mailbox &optional examine)
924 "Select MAILBOX on server in BUFFER.
925 If EXAMINE is non-nil, do a read-only select."
926 (if (imap-current-mailbox-p-1 mailbox examine)
928 (setq imap-current-mailbox mailbox)
929 (if (imap-ok-p (imap-send-command-wait
930 (concat (if examine "EXAMINE" "SELECT") " \""
933 (setq imap-message-data (make-vector imap-message-prime 0)
934 imap-state (if examine 'examine 'selected))
935 imap-current-mailbox)
936 ;; Failed SELECT/EXAMINE unselects current mailbox
937 (setq imap-current-mailbox nil))))
939 (defun imap-mailbox-select (mailbox &optional examine buffer)
940 (with-current-buffer (or buffer (current-buffer))
942 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
944 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
945 (with-current-buffer (or buffer (current-buffer))
946 (imap-mailbox-select-1 mailbox 'exmine)))
948 (defun imap-mailbox-examine (mailbox &optional buffer)
949 "Examine MAILBOX on server in BUFFER."
950 (imap-mailbox-select mailbox 'exmine buffer))
952 (defun imap-mailbox-unselect (&optional buffer)
953 "Close current folder in BUFFER, without expunging articles."
954 (with-current-buffer (or buffer (current-buffer))
955 (when (or (eq imap-state 'auth)
956 (and (imap-capability 'UNSELECT)
957 (imap-ok-p (imap-send-command-wait "UNSELECT")))
959 (imap-send-command-wait (concat "EXAMINE \""
962 (imap-ok-p (imap-send-command-wait "CLOSE"))))
963 (setq imap-current-mailbox nil
964 imap-message-data nil
968 (defun imap-mailbox-expunge (&optional buffer)
969 "Expunge articles in current folder in BUFFER.
970 If BUFFER is nil the current buffer is assumed."
971 (with-current-buffer (or buffer (current-buffer))
972 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
973 (imap-ok-p (imap-send-command-wait "EXPUNGE")))))
975 (defun imap-mailbox-close (&optional buffer)
976 "Expunge articles and close current folder in BUFFER.
977 If BUFFER is nil the current buffer is assumed."
978 (with-current-buffer (or buffer (current-buffer))
979 (when (and imap-current-mailbox
980 (imap-ok-p (imap-send-command-wait "CLOSE")))
981 (setq imap-current-mailbox nil
982 imap-message-data nil
986 (defun imap-mailbox-create-1 (mailbox)
987 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
989 (defun imap-mailbox-create (mailbox &optional buffer)
990 "Create MAILBOX on server in BUFFER.
991 If BUFFER is nil the current buffer is assumed."
992 (with-current-buffer (or buffer (current-buffer))
993 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
995 (defun imap-mailbox-delete (mailbox &optional buffer)
996 "Delete MAILBOX on server in BUFFER.
997 If BUFFER is nil the current buffer is assumed."
998 (let ((mailbox (imap-utf7-encode mailbox)))
999 (with-current-buffer (or buffer (current-buffer))
1001 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1003 (defun imap-mailbox-rename (oldname newname &optional buffer)
1004 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1005 If BUFFER is nil the current buffer is assumed."
1006 (let ((oldname (imap-utf7-encode oldname))
1007 (newname (imap-utf7-encode newname)))
1008 (with-current-buffer (or buffer (current-buffer))
1010 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1011 "\"" newname "\""))))))
1013 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1014 "Return a list of subscribed mailboxes on server in BUFFER.
1015 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1016 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1017 implementation-specific string that has to be passed to lsub command."
1018 (with-current-buffer (or buffer (current-buffer))
1019 ;; Make sure we know the hierarchy separator for root's hierarchy
1020 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1021 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1022 (imap-utf7-encode root) "\"")))
1023 ;; clear list data (NB not delimiter and other stuff)
1024 (imap-mailbox-map-1 (lambda (mailbox)
1025 (imap-mailbox-put 'lsub nil mailbox)))
1027 (imap-send-command-wait
1028 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1029 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1032 (imap-mailbox-map-1 (lambda (mailbox)
1033 (when (imap-mailbox-get-1 'lsub mailbox)
1034 (push (imap-utf7-decode mailbox) out))))
1037 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1038 "Return a list of mailboxes matching ROOT on server in BUFFER.
1039 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1040 root. REFERENCE is a implementation-specific string that has to be
1041 passed to list command."
1042 (with-current-buffer (or buffer (current-buffer))
1043 ;; Make sure we know the hierarchy separator for root's hierarchy
1044 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1045 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1046 (imap-utf7-encode root) "\"")))
1047 ;; clear list data (NB not delimiter and other stuff)
1048 (imap-mailbox-map-1 (lambda (mailbox)
1049 (imap-mailbox-put 'list nil mailbox)))
1051 (imap-send-command-wait
1052 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1053 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1056 (imap-mailbox-map-1 (lambda (mailbox)
1057 (when (imap-mailbox-get-1 'list mailbox)
1058 (push (imap-utf7-decode mailbox) out))))
1061 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1062 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1063 Returns non-nil if successful."
1064 (with-current-buffer (or buffer (current-buffer))
1065 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1066 (imap-utf7-encode mailbox)
1069 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1070 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1071 Returns non-nil if successful."
1072 (with-current-buffer (or buffer (current-buffer))
1073 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1074 (imap-utf7-encode mailbox)
1077 (defun imap-mailbox-status (mailbox items &optional buffer)
1078 "Get status items ITEM in MAILBOX from server in BUFFER.
1079 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1080 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1081 or 'unseen. If ITEMS is a list of symbols, a list of values is
1082 returned, if ITEMS is a symbol only it's value is returned."
1083 (with-current-buffer (or buffer (current-buffer))
1085 (imap-send-command-wait (list "STATUS \""
1086 (imap-utf7-encode mailbox)
1093 (mapcar (lambda (item)
1094 (imap-mailbox-get item mailbox))
1096 (imap-mailbox-get items mailbox)))))
1098 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1099 "Get ACL on mailbox from server in BUFFER."
1100 (let ((mailbox (imap-utf7-encode mailbox)))
1101 (with-current-buffer (or buffer (current-buffer))
1103 (imap-send-command-wait (list "GETACL \""
1104 (or mailbox imap-current-mailbox)
1106 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1108 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1109 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1110 (let ((mailbox (imap-utf7-encode mailbox)))
1111 (with-current-buffer (or buffer (current-buffer))
1113 (imap-send-command-wait (list "SETACL \""
1114 (or mailbox imap-current-mailbox)
1120 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1121 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1122 (let ((mailbox (imap-utf7-encode mailbox)))
1123 (with-current-buffer (or buffer (current-buffer))
1125 (imap-send-command-wait (list "DELETEACL \""
1126 (or mailbox imap-current-mailbox)
1131 ;; Message functions:
1133 (defun imap-current-message (&optional buffer)
1134 (with-current-buffer (or buffer (current-buffer))
1135 imap-current-message))
1137 (defun imap-list-to-message-set (list)
1138 (mapconcat (lambda (item)
1139 (number-to-string item))
1145 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1146 (with-current-buffer (or buffer (current-buffer))
1147 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1149 (imap-list-to-message-set uids)
1153 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1154 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1155 UIDS can be a string, number or a list of numbers. If RECEIVE
1156 is non-nil return theese properties."
1157 (with-current-buffer (or buffer (current-buffer))
1158 (when (imap-ok-p (imap-send-command-wait
1159 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1161 (imap-list-to-message-set uids)
1164 (if (or (null receive) (stringp uids))
1167 (mapcar (lambda (uid)
1169 (mapcar (lambda (prop)
1170 (imap-message-get uid prop))
1172 (imap-message-get uid receive)))
1174 (imap-message-get uids receive))))))
1176 (defun imap-message-put (uid propname value &optional buffer)
1177 (with-current-buffer (or buffer (current-buffer))
1178 (if imap-message-data
1179 (put (intern (number-to-string uid) imap-message-data)
1181 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1182 uid propname value (current-buffer)))
1185 (defun imap-message-get (uid propname &optional buffer)
1186 (with-current-buffer (or buffer (current-buffer))
1187 (get (intern-soft (number-to-string uid) imap-message-data)
1190 (defun imap-message-map (func propname &optional buffer)
1191 "Map a function across each mailbox in `imap-message-data', returning a list."
1192 (with-current-buffer (or buffer (current-buffer))
1196 (push (funcall func (get s 'UID) (get s propname)) result))
1200 (defmacro imap-message-envelope-date (uid &optional buffer)
1201 `(with-current-buffer (or ,buffer (current-buffer))
1202 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1204 (defmacro imap-message-envelope-subject (uid &optional buffer)
1205 `(with-current-buffer (or ,buffer (current-buffer))
1206 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1208 (defmacro imap-message-envelope-from (uid &optional buffer)
1209 `(with-current-buffer (or ,buffer (current-buffer))
1210 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1212 (defmacro imap-message-envelope-sender (uid &optional buffer)
1213 `(with-current-buffer (or ,buffer (current-buffer))
1214 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1216 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1217 `(with-current-buffer (or ,buffer (current-buffer))
1218 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1220 (defmacro imap-message-envelope-to (uid &optional buffer)
1221 `(with-current-buffer (or ,buffer (current-buffer))
1222 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1224 (defmacro imap-message-envelope-cc (uid &optional buffer)
1225 `(with-current-buffer (or ,buffer (current-buffer))
1226 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1228 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1229 `(with-current-buffer (or ,buffer (current-buffer))
1230 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1232 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1233 `(with-current-buffer (or ,buffer (current-buffer))
1234 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1236 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1237 `(with-current-buffer (or ,buffer (current-buffer))
1238 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1240 (defmacro imap-message-body (uid &optional buffer)
1241 `(with-current-buffer (or ,buffer (current-buffer))
1242 (imap-message-get ,uid 'BODY)))
1244 (defun imap-search (predicate &optional buffer)
1245 (with-current-buffer (or buffer (current-buffer))
1246 (imap-mailbox-put 'search 'dummy)
1247 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1248 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1249 (error "Missing SEARCH response to a SEARCH command")
1250 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1252 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1253 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1254 (with-current-buffer (or buffer (current-buffer))
1255 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1256 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1258 (defun imap-message-flags-set (articles flags &optional silent buffer)
1259 (when (and articles flags)
1260 (with-current-buffer (or buffer (current-buffer))
1261 (imap-ok-p (imap-send-command-wait
1262 (concat "UID STORE " articles
1263 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1265 (defun imap-message-flags-del (articles flags &optional silent buffer)
1266 (when (and articles flags)
1267 (with-current-buffer (or buffer (current-buffer))
1268 (imap-ok-p (imap-send-command-wait
1269 (concat "UID STORE " articles
1270 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1272 (defun imap-message-flags-add (articles flags &optional silent buffer)
1273 (when (and articles flags)
1274 (with-current-buffer (or buffer (current-buffer))
1275 (imap-ok-p (imap-send-command-wait
1276 (concat "UID STORE " articles
1277 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1279 (defun imap-message-copyuid-1 (mailbox)
1280 (if (imap-capability 'UIDPLUS)
1281 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1282 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1283 (let ((old-mailbox imap-current-mailbox)
1285 (imap-message-data (make-vector 2 0)))
1286 (when (imap-mailbox-examine-1 mailbox)
1288 (and (imap-fetch "*" "UID")
1289 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1290 (apply 'max (imap-message-map
1291 (lambda (uid prop) uid) 'UID))))
1293 (imap-mailbox-select old-mailbox (eq state 'examine))
1294 (imap-mailbox-unselect)))))))
1296 (defun imap-message-copyuid (mailbox &optional buffer)
1297 (with-current-buffer (or buffer (current-buffer))
1298 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1300 (defun imap-message-copy (articles mailbox
1301 &optional dont-create no-copyuid buffer)
1302 "Copy ARTICLES (a string message set) to MAILBOX on server in
1303 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1304 non-nil, it will not create a mailbox. On success, return a list with
1305 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1306 first element, rest of list contain the saved articles' UIDs."
1308 (with-current-buffer (or buffer (current-buffer))
1309 (let ((mailbox (imap-utf7-encode mailbox)))
1310 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1311 (imap-current-target-mailbox mailbox))
1312 (if (imap-ok-p (imap-send-command-wait cmd))
1314 (when (and (not dont-create)
1315 (imap-mailbox-get-1 'trycreate mailbox))
1316 (imap-mailbox-create-1 mailbox)
1317 (imap-ok-p (imap-send-command-wait cmd)))))
1319 (imap-message-copyuid-1 mailbox)))))))
1321 (defun imap-message-appenduid-1 (mailbox)
1322 (if (imap-capability 'UIDPLUS)
1323 (imap-mailbox-get-1 'appenduid mailbox)
1324 (let ((old-mailbox imap-current-mailbox)
1326 (imap-message-data (make-vector 2 0)))
1327 (when (imap-mailbox-examine-1 mailbox)
1329 (and (imap-fetch "*" "UID")
1330 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1331 (apply 'max (imap-message-map
1332 (lambda (uid prop) uid) 'UID))))
1334 (imap-mailbox-select old-mailbox (eq state 'examine))
1335 (imap-mailbox-unselect)))))))
1337 (defun imap-message-appenduid (mailbox &optional buffer)
1338 (with-current-buffer (or buffer (current-buffer))
1339 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1341 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1342 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1343 FLAGS and DATE-TIME is currently not used. Return a cons holding
1344 uidvalidity of MAILBOX and UID the newly created article got, or nil
1346 (let ((mailbox (imap-utf7-encode mailbox)))
1347 (with-current-buffer (or buffer (current-buffer))
1348 (and (let ((imap-current-target-mailbox mailbox))
1350 (imap-send-command-wait
1351 (list "APPEND \"" mailbox "\" " article))))
1352 (imap-message-appenduid-1 mailbox)))))
1354 (defun imap-body-lines (body)
1355 "Return number of lines in article by looking at the mime bodystructure BODY."
1357 (if (stringp (car body))
1358 ;; upcase for bug in courier imap server
1359 (cond ((and (string= (upcase (car body)) "TEXT")
1360 (numberp (nth 7 body)))
1362 ((and (string= (upcase (car body)) "MESSAGE")
1363 (numberp (nth 9 body)))
1366 (apply '+ (mapcar 'imap-body-lines body)))
1369 (defun imap-envelope-from (from)
1370 "Return a from string line."
1372 (concat (aref from 0)
1373 (if (aref from 0) " <")
1377 (if (aref from 0) ">"))))
1380 ;; Internal functions.
1382 (defun imap-send-command-1 (cmdstr)
1383 (setq cmdstr (concat cmdstr imap-client-eol))
1385 (with-current-buffer (get-buffer-create imap-log)
1386 (buffer-disable-undo)
1387 (goto-char (point-max))
1389 (process-send-string imap-process cmdstr))
1391 (defun imap-send-command (command &optional buffer)
1392 (with-current-buffer (or buffer (current-buffer))
1393 (if (not (listp command)) (setq command (list command)))
1394 (let ((tag (setq imap-tag (1+ imap-tag)))
1396 (setq cmdstr (concat (number-to-string imap-tag) " "))
1397 (while (setq cmd (pop command))
1398 (cond ((stringp cmd)
1399 (setq cmdstr (concat cmdstr cmd)))
1402 (concat cmdstr (format "{%d}" (with-current-buffer cmd
1406 (imap-send-command-1 cmdstr)
1408 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1409 (setq command nil);; abort command if no cont-req
1410 (let ((process imap-process)
1411 (stream imap-stream)
1412 (eol imap-client-eol))
1413 (with-current-buffer cmd
1414 (when (not (equal eol "\r\n"))
1415 ;; XXX modifies buffer!
1416 (goto-char (point-min))
1417 (while (search-forward "\r\n" nil t)
1418 (replace-match eol)))
1420 (with-current-buffer (get-buffer-create
1422 (buffer-disable-undo)
1423 (goto-char (point-max))
1424 (insert-buffer-substring cmd)))
1425 (process-send-region process (point-min)
1427 (process-send-string process imap-client-eol))))
1428 (setq imap-continuation nil)))
1430 (imap-send-command-1 cmdstr)
1433 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1434 (setq command nil);; abort command if no cont-req
1435 (setq command (cons (funcall cmd imap-continuation)
1437 (setq imap-continuation nil)))
1439 (error "Unknown command type"))))
1441 (imap-send-command-1 cmdstr))
1444 (defun imap-wait-for-tag (tag &optional buffer)
1445 (with-current-buffer (or buffer (current-buffer))
1446 (while (and (null imap-continuation)
1447 (< imap-reached-tag tag))
1448 (or (and (not (memq (process-status imap-process) '(open run)))
1450 (accept-process-output imap-process 1)))
1451 (or (assq tag imap-failed-tags)
1452 (if imap-continuation
1456 (defun imap-sentinel (process string)
1457 (delete-process process))
1459 (defun imap-find-next-line ()
1460 "Return point at end of current line, taking into account literals.
1461 Return nil if no complete line has arrived."
1462 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1465 (if (match-string 1)
1466 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1468 (goto-char (+ (point) (string-to-number (match-string 1))))
1469 (imap-find-next-line))
1472 (defun imap-arrival-filter (proc string)
1473 "IMAP process filter."
1474 (with-current-buffer (process-buffer proc)
1475 (goto-char (point-max))
1478 (with-current-buffer (get-buffer-create imap-log)
1479 (buffer-disable-undo)
1480 (goto-char (point-max))
1483 (goto-char (point-min))
1484 (while (setq end (imap-find-next-line))
1486 (narrow-to-region (point-min) end)
1487 (delete-backward-char (length imap-server-eol))
1488 (goto-char (point-min))
1490 (cond ((eq imap-state 'initial)
1491 (imap-parse-greeting))
1492 ((or (eq imap-state 'auth)
1493 (eq imap-state 'nonauth)
1494 (eq imap-state 'selected)
1495 (eq imap-state 'examine))
1496 (imap-parse-response))
1498 (message "Unknown state %s in arrival filter"
1500 (delete-region (point-min) (point-max))))))))
1505 (defsubst imap-forward ()
1506 (or (eobp) (forward-char)))
1509 ;; ; Unsigned 32-bit integer
1510 ;; ; (0 <= n < 4,294,967,296)
1512 (defsubst imap-parse-number ()
1513 (when (looking-at "[0-9]+")
1515 (string-to-number (match-string 0))
1516 (goto-char (match-end 0)))))
1518 ;; literal = "{" number "}" CRLF *CHAR8
1519 ;; ; Number represents the number of CHAR8s
1521 (defsubst imap-parse-literal ()
1522 (when (looking-at "{\\([0-9]+\\)}\r\n")
1523 (let ((pos (match-end 0))
1524 (len (string-to-number (match-string 1))))
1525 (if (< (point-max) (+ pos len))
1527 (goto-char (+ pos len))
1528 (buffer-substring pos (+ pos len))))))
1530 ;; string = quoted / literal
1532 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1534 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1535 ;; "\" quoted-specials
1537 ;; quoted-specials = DQUOTE / "\"
1539 ;; TEXT-CHAR = <any CHAR except CR and LF>
1541 (defsubst imap-parse-string ()
1542 (cond ((eq (char-after) ?\")
1544 (let ((p (point)) (name ""))
1545 (skip-chars-forward "^\"\\\\")
1546 (setq name (buffer-substring p (point)))
1547 (while (eq (char-after) ?\\)
1548 (setq p (1+ (point)))
1550 (skip-chars-forward "^\"\\\\")
1551 (setq name (concat name (buffer-substring p (point)))))
1554 ((eq (char-after) ?{)
1555 (imap-parse-literal))))
1559 (defsubst imap-parse-nil ()
1560 (if (looking-at "NIL")
1561 (goto-char (match-end 0))))
1563 ;; nstring = string / nil
1565 (defsubst imap-parse-nstring ()
1566 (or (imap-parse-string)
1567 (and (imap-parse-nil)
1570 ;; astring = atom / string
1572 ;; atom = 1*ATOM-CHAR
1574 ;; ATOM-CHAR = <any CHAR except atom-specials>
1576 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1579 ;; list-wildcards = "%" / "*"
1581 ;; quoted-specials = DQUOTE / "\"
1583 (defsubst imap-parse-astring ()
1584 (or (imap-parse-string)
1585 (buffer-substring (point)
1586 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1587 (goto-char (1- (match-end 0)))
1591 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
1594 ;; addr-adl = nstring
1595 ;; ; Holds route from [RFC-822] route-addr if
1598 ;; addr-host = nstring
1599 ;; ; NIL indicates [RFC-822] group syntax.
1600 ;; ; Otherwise, holds [RFC-822] domain name
1602 ;; addr-mailbox = nstring
1603 ;; ; NIL indicates end of [RFC-822] group; if
1604 ;; ; non-NIL and addr-host is NIL, holds
1605 ;; ; [RFC-822] group name.
1606 ;; ; Otherwise, holds [RFC-822] local-part
1607 ;; ; after removing [RFC-822] quoting
1609 ;; addr-name = nstring
1610 ;; ; If non-NIL, holds phrase from [RFC-822]
1611 ;; ; mailbox after removing [RFC-822] quoting
1614 (defsubst imap-parse-address ()
1616 (when (eq (char-after) ?\()
1618 (setq address (vector (prog1 (imap-parse-nstring)
1620 (prog1 (imap-parse-nstring)
1622 (prog1 (imap-parse-nstring)
1624 (imap-parse-nstring)))
1625 (when (eq (char-after) ?\))
1629 ;; address-list = "(" 1*address ")" / nil
1633 (defsubst imap-parse-address-list ()
1634 (if (eq (char-after) ?\()
1635 (let (address addresses)
1637 (while (and (not (eq (char-after) ?\)))
1638 ;; next line for MS Exchange bug
1639 (progn (and (eq (char-after) ? ) (imap-forward)) t)
1640 (setq address (imap-parse-address)))
1641 (setq addresses (cons address addresses)))
1642 (when (eq (char-after) ?\))
1644 (nreverse addresses)))
1645 (assert (imap-parse-nil))))
1647 ;; mailbox = "INBOX" / astring
1648 ;; ; INBOX is case-insensitive. All case variants of
1649 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
1650 ;; ; not as an astring. An astring which consists of
1651 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
1652 ;; ; is considered to be INBOX and not an astring.
1653 ;; ; Refer to section 5.1 for further
1654 ;; ; semantic details of mailbox names.
1656 (defsubst imap-parse-mailbox ()
1657 (let ((mailbox (imap-parse-astring)))
1658 (if (string-equal "INBOX" (upcase mailbox))
1662 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
1664 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
1665 ;; ; Authentication condition
1667 ;; resp-cond-bye = "BYE" SP resp-text
1669 (defun imap-parse-greeting ()
1670 "Parse a IMAP greeting."
1671 (cond ((looking-at "\\* OK ")
1672 (setq imap-state 'nonauth))
1673 ((looking-at "\\* PREAUTH ")
1674 (setq imap-state 'auth))
1675 ((looking-at "\\* BYE ")
1676 (setq imap-state 'closed))))
1678 ;; response = *(continue-req / response-data) response-done
1680 ;; continue-req = "+" SP (resp-text / base64) CRLF
1682 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
1683 ;; mailbox-data / message-data / capability-data) CRLF
1685 ;; response-done = response-tagged / response-fatal
1687 ;; response-fatal = "*" SP resp-cond-bye CRLF
1688 ;; ; Server closes connection immediately
1690 ;; response-tagged = tag SP resp-cond-state CRLF
1692 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
1693 ;; ; Status condition
1695 ;; resp-cond-bye = "BYE" SP resp-text
1697 ;; mailbox-data = "FLAGS" SP flag-list /
1698 ;; "LIST" SP mailbox-list /
1699 ;; "LSUB" SP mailbox-list /
1700 ;; "SEARCH" *(SP nz-number) /
1701 ;; "STATUS" SP mailbox SP "("
1702 ;; [status-att SP number *(SP status-att SP number)] ")" /
1703 ;; number SP "EXISTS" /
1704 ;; number SP "RECENT"
1706 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
1708 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
1710 ;; ; IMAP4rev1 servers which offer RFC 1730
1711 ;; ; compatibility MUST list "IMAP4" as the first
1714 (defun imap-parse-response ()
1715 "Parse a IMAP command response."
1717 (case (setq token (read (current-buffer)))
1718 (+ (setq imap-continuation
1719 (or (buffer-substring (min (point-max) (1+ (point)))
1722 (* (case (prog1 (setq token (read (current-buffer)))
1724 (OK (imap-parse-resp-text))
1725 (NO (imap-parse-resp-text))
1726 (BAD (imap-parse-resp-text))
1727 (BYE (imap-parse-resp-text))
1728 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
1729 (LIST (imap-parse-data-list 'list))
1730 (LSUB (imap-parse-data-list 'lsub))
1731 (SEARCH (imap-mailbox-put
1733 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
1734 (STATUS (imap-parse-status))
1735 (CAPABILITY (setq imap-capability
1736 (read (concat "(" (upcase (buffer-substring
1737 (point) (point-max)))
1739 (ACL (imap-parse-acl))
1740 (t (case (prog1 (read (current-buffer))
1742 (EXISTS (imap-mailbox-put 'exists token))
1743 (RECENT (imap-mailbox-put 'recent token))
1745 (FETCH (imap-parse-fetch token))
1746 (t (message "Garbage: %s" (buffer-string)))))))
1748 (if (not (integerp token))
1749 (message "Garbage: %s" (buffer-string))
1750 (case (prog1 (setq status (read (current-buffer)))
1753 (setq imap-reached-tag (max imap-reached-tag token))
1754 (imap-parse-resp-text)))
1756 (setq imap-reached-tag (max imap-reached-tag token))
1758 (imap-parse-resp-text))
1760 (when (eq (char-after) ?\[)
1761 (setq code (buffer-substring (point)
1762 (search-forward "]")))
1764 (setq text (buffer-substring (point) (point-max)))
1765 (push (list token status code text)
1766 imap-failed-tags))))
1768 (setq imap-reached-tag (max imap-reached-tag token))
1770 (imap-parse-resp-text))
1772 (when (eq (char-after) ?\[)
1773 (setq code (buffer-substring (point)
1774 (search-forward "]")))
1776 (setq text (buffer-substring (point) (point-max)))
1777 (push (list token status code text) imap-failed-tags)
1778 (error "Internal error, tag %s status %s code %s text %s"
1779 token status code text))))
1780 (t (message "Garbage: %s" (buffer-string))))))))))
1782 ;; resp-text = ["[" resp-text-code "]" SP] text
1784 ;; text = 1*TEXT-CHAR
1786 ;; TEXT-CHAR = <any CHAR except CR and LF>
1788 (defun imap-parse-resp-text ()
1789 (imap-parse-resp-text-code))
1791 ;; resp-text-code = "ALERT" /
1792 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
1793 ;; "NEWNAME" SP string SP string /
1795 ;; "PERMANENTFLAGS" SP "("
1796 ;; [flag-perm *(SP flag-perm)] ")" /
1800 ;; "UIDNEXT" SP nz-number /
1801 ;; "UIDVALIDITY" SP nz-number /
1802 ;; "UNSEEN" SP nz-number /
1803 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
1805 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
1807 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
1809 ;; set = sequence-num / (sequence-num ":" sequence-num) /
1811 ;; ; Identifies a set of messages. For message
1812 ;; ; sequence numbers, these are consecutive
1813 ;; ; numbers from 1 to the number of messages in
1815 ;; ; Comma delimits individual numbers, colon
1816 ;; ; delimits between two numbers inclusive.
1817 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
1818 ;; ; 14,15 for a mailbox with 15 messages.
1820 ;; sequence-num = nz-number / "*"
1821 ;; ; * is the largest number in use. For message
1822 ;; ; sequence numbers, it is the number of messages
1823 ;; ; in the mailbox. For unique identifiers, it is
1824 ;; ; the unique identifier of the last message in
1827 ;; flag-perm = flag / "\*"
1829 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1830 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1831 ;; ; Does not include "\Recent"
1833 ;; flag-extension = "\" atom
1834 ;; ; Future expansion. Client implementations
1835 ;; ; MUST accept flag-extension flags. Server
1836 ;; ; implementations MUST NOT generate
1837 ;; ; flag-extension flags except as defined by
1838 ;; ; future standard or standards-track
1839 ;; ; revisions of this specification.
1841 ;; flag-keyword = atom
1843 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
1845 (defun imap-parse-resp-text-code ()
1846 (when (eq (char-after) ?\[)
1848 (cond ((search-forward "PERMANENTFLAGS " nil t)
1849 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
1850 ((search-forward "UIDNEXT " nil t)
1851 (imap-mailbox-put 'uidnext (read (current-buffer))))
1852 ((search-forward "UNSEEN " nil t)
1853 (imap-mailbox-put 'unseen (read (current-buffer))))
1854 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
1855 (imap-mailbox-put 'uidvalidity (match-string 1)))
1856 ((search-forward "READ-ONLY" nil t)
1857 (imap-mailbox-put 'read-only t))
1858 ((search-forward "NEWNAME " nil t)
1859 (let (oldname newname)
1860 (setq oldname (imap-parse-string))
1862 (setq newname (imap-parse-string))
1863 (imap-mailbox-put 'newname newname oldname)))
1864 ((search-forward "TRYCREATE" nil t)
1865 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
1866 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
1867 (imap-mailbox-put 'appenduid
1868 (list (match-string 1)
1869 (string-to-number (match-string 2)))
1870 imap-current-target-mailbox))
1871 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
1872 (imap-mailbox-put 'copyuid (list (match-string 1)
1875 imap-current-target-mailbox))
1876 ((search-forward "ALERT] " nil t)
1877 (message "Imap server %s information: %s" imap-server
1878 (buffer-substring (point) (point-max)))))))
1880 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
1881 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
1883 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
1884 ;; *(SP mbx-list-oflag) /
1885 ;; mbx-list-oflag *(SP mbx-list-oflag)
1887 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
1888 ;; ; Other flags; multiple possible per LIST response
1890 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
1891 ;; ; Selectability flags; only one per LIST response
1893 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1894 ;; "\" quoted-specials
1896 ;; quoted-specials = DQUOTE / "\"
1898 (defun imap-parse-data-list (type)
1899 (let (flags delimiter mailbox)
1900 (setq flags (imap-parse-flag-list))
1901 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
1902 (setq delimiter (match-string 1))
1903 (goto-char (1+ (match-end 0)))
1904 (when (setq mailbox (imap-parse-mailbox))
1905 (imap-mailbox-put type t mailbox)
1906 (imap-mailbox-put 'list-flags flags mailbox)
1907 (imap-mailbox-put 'delimiter delimiter mailbox)))))
1909 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
1910 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
1911 ;; "INTERNALDATE" SPACE date_time /
1912 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
1913 ;; "RFC822.SIZE" SPACE number /
1914 ;; "BODY" ["STRUCTURE"] SPACE body /
1915 ;; "BODY" section ["<" number ">"] SPACE nstring /
1916 ;; "UID" SPACE uniqueid) ")"
1918 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
1919 ;; SPACE time SPACE zone <">
1921 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
1922 ;; ["." (section_text / "MIME")])] "]"
1924 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
1925 ;; SPACE header_list / "TEXT"
1927 ;; header_fld_name ::= astring
1929 ;; header_list ::= "(" 1#header_fld_name ")"
1931 (defsubst imap-parse-header-list ()
1932 (when (eq (char-after) ?\()
1934 (while (not (eq (char-after) ?\)))
1936 (push (imap-parse-astring) strlist))
1938 (nreverse strlist))))
1940 (defsubst imap-parse-fetch-body-section ()
1942 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
1943 (if (eq (char-before) ? )
1945 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
1946 (search-forward "]" nil t))
1949 (defun imap-parse-fetch (response)
1950 (when (eq (char-after) ?\()
1951 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
1952 rfc822size body bodydetail bodystructure)
1953 (while (not (eq (char-after) ?\)))
1955 (let ((token (read (current-buffer))))
1957 (cond ((eq token 'UID)
1958 (setq uid (ignore-errors (read (current-buffer)))))
1960 (setq flags (imap-parse-flag-list)))
1961 ((eq token 'ENVELOPE)
1962 (setq envelope (imap-parse-envelope)))
1963 ((eq token 'INTERNALDATE)
1964 (setq internaldate (imap-parse-string)))
1966 (setq rfc822 (imap-parse-nstring)))
1967 ((eq token 'RFC822.HEADER)
1968 (setq rfc822header (imap-parse-nstring)))
1969 ((eq token 'RFC822.TEXT)
1970 (setq rfc822text (imap-parse-nstring)))
1971 ((eq token 'RFC822.SIZE)
1972 (setq rfc822size (read (current-buffer))))
1974 (if (eq (char-before) ?\[)
1976 (upcase (imap-parse-fetch-body-section))
1977 (and (eq (char-after) ?<)
1978 (buffer-substring (1+ (point))
1979 (search-forward ">" nil t)))
1980 (progn (imap-forward)
1981 (imap-parse-nstring)))
1983 (setq body (imap-parse-body))))
1984 ((eq token 'BODYSTRUCTURE)
1985 (setq bodystructure (imap-parse-body))))))
1987 (setq imap-current-message uid)
1988 (imap-message-put uid 'UID uid)
1989 (and flags (imap-message-put uid 'FLAGS flags))
1990 (and envelope (imap-message-put uid 'ENVELOPE envelope))
1991 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
1992 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
1993 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
1994 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
1995 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
1996 (and body (imap-message-put uid 'BODY body))
1997 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
1998 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
1999 (run-hooks 'imap-fetch-data-hook)))))
2001 ;; mailbox-data = ...
2002 ;; "STATUS" SP mailbox SP "("
2003 ;; [status-att SP number
2004 ;; *(SP status-att SP number)] ")"
2007 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2010 (defun imap-parse-status ()
2011 (let ((mailbox (imap-parse-mailbox)))
2012 (when (and mailbox (search-forward "(" nil t))
2013 (while (not (eq (char-after) ?\)))
2014 (let ((token (read (current-buffer))))
2015 (cond ((eq token 'MESSAGES)
2016 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2018 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2019 ((eq token 'UIDNEXT)
2020 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox))
2021 ((eq token 'UIDVALIDITY)
2022 (and (looking-at " \\([0-9]+\\)")
2023 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox)
2024 (goto-char (match-end 1))))
2026 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2028 (message "Unknown status data %s in mailbox %s ignored"
2029 token mailbox))))))))
2031 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2034 ;; identifier ::= astring
2036 ;; rights ::= astring
2038 (defun imap-parse-acl ()
2039 (let ((mailbox (imap-parse-mailbox))
2040 identifier rights acl)
2041 (while (eq (char-after) ?\ )
2043 (setq identifier (imap-parse-astring))
2045 (setq rights (imap-parse-astring))
2046 (setq acl (append acl (list (cons identifier rights)))))
2047 (imap-mailbox-put 'acl acl mailbox)))
2049 ;; flag-list = "(" [flag *(SP flag)] ")"
2051 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2052 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2053 ;; ; Does not include "\Recent"
2055 ;; flag-keyword = atom
2057 ;; flag-extension = "\" atom
2058 ;; ; Future expansion. Client implementations
2059 ;; ; MUST accept flag-extension flags. Server
2060 ;; ; implementations MUST NOT generate
2061 ;; ; flag-extension flags except as defined by
2062 ;; ; future standard or standards-track
2063 ;; ; revisions of this specification.
2065 (defun imap-parse-flag-list ()
2066 (let ((str (buffer-substring (point) (search-forward ")" nil t)))
2068 (while (setq pos (string-match "\\\\" str (and pos (+ 2 pos))))
2069 (setq str (replace-match "\\\\" nil t str)))
2070 (mapcar 'symbol-name (read str))))
2072 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2073 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2074 ;; env-in-reply-to SP env-message-id ")"
2076 ;; env-bcc = "(" 1*address ")" / nil
2078 ;; env-cc = "(" 1*address ")" / nil
2080 ;; env-date = nstring
2082 ;; env-from = "(" 1*address ")" / nil
2084 ;; env-in-reply-to = nstring
2086 ;; env-message-id = nstring
2088 ;; env-reply-to = "(" 1*address ")" / nil
2090 ;; env-sender = "(" 1*address ")" / nil
2092 ;; env-subject = nstring
2094 ;; env-to = "(" 1*address ")" / nil
2096 (defun imap-parse-envelope ()
2097 (when (eq (char-after) ?\()
2099 (vector (prog1 (imap-parse-nstring);; date
2101 (prog1 (imap-parse-nstring);; subject
2103 (prog1 (imap-parse-address-list);; from
2105 (prog1 (imap-parse-address-list);; sender
2107 (prog1 (imap-parse-address-list);; reply-to
2109 (prog1 (imap-parse-address-list);; to
2111 (prog1 (imap-parse-address-list);; cc
2113 (prog1 (imap-parse-address-list);; bcc
2115 (prog1 (imap-parse-nstring);; in-reply-to
2117 (prog1 (imap-parse-nstring);; message-id
2120 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2122 (defsubst imap-parse-string-list ()
2123 (cond ((eq (char-after) ?\();; body-fld-param
2126 (while (setq str (imap-parse-string))
2129 (nreverse strlist)))
2133 ;; body-extension = nstring / number /
2134 ;; "(" body-extension *(SP body-extension) ")"
2135 ;; ; Future expansion. Client implementations
2136 ;; ; MUST accept body-extension fields. Server
2137 ;; ; implementations MUST NOT generate
2138 ;; ; body-extension fields except as defined by
2139 ;; ; future standard or standards-track
2140 ;; ; revisions of this specification.
2142 (defun imap-parse-body-extension ()
2143 (if (eq (char-after) ?\()
2146 (push (imap-parse-body-extension) b-e)
2147 (while (eq (char-after) ?\ )
2149 (push (imap-parse-body-extension) b-e))
2150 (assert (eq (char-after) ?\)))
2153 (or (imap-parse-number)
2154 (imap-parse-nstring))))
2156 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2157 ;; *(SP body-extension)]]
2158 ;; ; MUST NOT be returned on non-extensible
2161 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2162 ;; *(SP body-extension)]]
2163 ;; ; MUST NOT be returned on non-extensible
2166 (defsubst imap-parse-body-ext ()
2168 (when (eq (char-after) ?\ );; body-fld-dsp
2171 (if (eq (char-after) ?\()
2174 (push (imap-parse-string) dsp)
2176 (push (imap-parse-string-list) dsp)
2178 (assert (imap-parse-nil)))
2179 (push (nreverse dsp) ext))
2180 (when (eq (char-after) ?\ );; body-fld-lang
2182 (if (eq (char-after) ?\()
2183 (push (imap-parse-string-list) ext)
2184 (push (imap-parse-nstring) ext))
2185 (while (eq (char-after) ?\ );; body-extension
2187 (setq ext (append (imap-parse-body-extension) ext)))))
2190 ;; body = "(" body-type-1part / body-type-mpart ")"
2192 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2193 ;; *(SP body-extension)]]
2194 ;; ; MUST NOT be returned on non-extensible
2197 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2198 ;; *(SP body-extension)]]
2199 ;; ; MUST NOT be returned on non-extensible
2202 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2203 ;; body-fld-enc SP body-fld-octets
2205 ;; body-fld-desc = nstring
2207 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2209 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2210 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2212 ;; body-fld-id = nstring
2214 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2216 ;; body-fld-lines = number
2218 ;; body-fld-md5 = nstring
2220 ;; body-fld-octets = number
2222 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2224 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2225 ;; [SP body-ext-1part]
2227 ;; body-type-basic = media-basic SP body-fields
2228 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2230 ;; body-type-msg = media-message SP body-fields SP envelope
2231 ;; SP body SP body-fld-lines
2233 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2235 ;; body-type-mpart = 1*body SP media-subtype
2236 ;; [SP body-ext-mpart]
2238 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2239 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2240 ;; ; Defined in [MIME-IMT]
2242 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2243 ;; ; Defined in [MIME-IMT]
2245 ;; media-subtype = string
2246 ;; ; Defined in [MIME-IMT]
2248 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2249 ;; ; Defined in [MIME-IMT]
2251 (defun imap-parse-body ()
2253 (when (eq (char-after) ?\()
2255 (if (eq (char-after) ?\()
2257 (while (and (eq (char-after) ?\()
2258 (setq subbody (imap-parse-body)))
2259 (push subbody body))
2261 (push (imap-parse-string) body);; media-subtype
2262 (when (eq (char-after) ?\ );; body-ext-mpart:
2264 (if (eq (char-after) ?\();; body-fld-param
2265 (push (imap-parse-string-list) body)
2266 (push (and (imap-parse-nil) nil) body))
2268 (append (imap-parse-body-ext) body)));; body-ext-...
2269 (assert (eq (char-after) ?\)))
2273 (push (imap-parse-string) body);; media-type
2275 (push (imap-parse-string) body);; media-subtype
2277 ;; next line for Sun SIMS bug
2278 (and (eq (char-after) ? ) (imap-forward))
2279 (if (eq (char-after) ?\();; body-fld-param
2280 (push (imap-parse-string-list) body)
2281 (push (and (imap-parse-nil) nil) body))
2283 (push (imap-parse-nstring) body);; body-fld-id
2285 (push (imap-parse-nstring) body);; body-fld-desc
2287 (push (imap-parse-string) body);; body-fld-enc
2289 (push (imap-parse-number) body);; body-fld-octets
2291 ;; ok, we're done parsing the required parts, what comes now is one
2294 ;; envelope (then we're parsing body-type-msg)
2295 ;; body-fld-lines (then we're parsing body-type-text)
2296 ;; body-ext-1part (then we're parsing body-type-basic)
2298 ;; the problem is that the two first are in turn optionally followed
2299 ;; by the third. So we parse the first two here (if there are any)...
2301 (when (eq (char-after) ?\ )
2304 (cond ((eq (char-after) ?\();; body-type-msg:
2305 (push (imap-parse-envelope) body);; envelope
2307 (push (imap-parse-body) body);; body
2309 (push (imap-parse-number) body));; body-fld-lines
2310 ((setq lines (imap-parse-number));; body-type-text:
2311 (push lines body));; body-fld-lines
2313 (backward-char)))));; no match...
2315 ;; ...and then parse the third one here...
2317 (when (eq (char-after) ?\ );; body-ext-1part:
2319 (push (imap-parse-nstring) body);; body-fld-md5
2320 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part..
2322 (assert (eq (char-after) ?\)))
2326 (when imap-debug ; (untrace-all)
2328 (buffer-disable-undo (get-buffer-create imap-debug))
2329 (mapcar (lambda (f) (trace-function-background f imap-debug))
2341 imap-interactive-login
2357 imap-send-command-wait
2362 imap-current-mailbox
2363 imap-current-mailbox-p-1
2364 imap-current-mailbox-p
2365 imap-mailbox-select-1
2367 imap-mailbox-examine-1
2368 imap-mailbox-examine
2369 imap-mailbox-unselect
2370 imap-mailbox-expunge
2372 imap-mailbox-create-1
2378 imap-mailbox-subscribe
2379 imap-mailbox-unsubscribe
2381 imap-mailbox-acl-get
2382 imap-mailbox-acl-set
2383 imap-mailbox-acl-delete
2384 imap-current-message
2385 imap-list-to-message-set
2392 imap-message-flag-permanent-p
2393 imap-message-flags-set
2394 imap-message-flags-del
2395 imap-message-flags-add
2396 imap-message-copyuid-1
2397 imap-message-copyuid
2399 imap-message-appenduid-1
2400 imap-message-appenduid
2412 imap-parse-resp-text
2413 imap-parse-resp-text-code
2414 imap-parse-data-list
2418 imap-parse-flag-list
2420 imap-parse-body-extension
2426 ;;; imap.el ends here