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"))
159 "Low-level IMAP issues."
162 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
164 "List of strings containing commands for Kerberos 4 authentication.
165 %s is replaced with server hostname, %p with port to connect to, and
166 %l with the value of `imap-default-user'. The program should accept
167 IMAP commands on stdin and return responses to stdout. Each entry in
168 the list is tried until a successful connection is made."
170 :type '(repeat string))
172 (defcustom imap-gssapi-program '("imtest -m gssapi -u %l -p %p %s")
173 "List of strings containing commands for GSSAPI (krb5) authentication.
174 %s is replaced with server hostname, %p with port to connect to, and
175 %l with the value of `imap-default-user'. The program should accept
176 IMAP commands on stdin and return responses to stdout. Each entry in
177 the list is tried until a successful connection is made."
179 :type '(repeat string))
181 (defcustom imap-ssl-program '("openssl s_client -ssl3 -connect %s:%p"
182 "openssl s_client -ssl2 -connect %s:%p"
183 "s_client -ssl3 -connect %s:%p"
184 "s_client -ssl2 -connect %s:%p")
185 "A string, or list of strings, containing commands for SSL connections.
186 Within a string, %s is replaced with the server address and %p with
187 port number on server. The program should accept IMAP commands on
188 stdin and return responses to stdout. Each entry in the list is tried
189 until a successful connection is made."
191 :type '(choice string
194 (defcustom imap-shell-program '("ssh %s imapd"
196 "ssh %g ssh %s imapd"
197 "rsh %g rsh %s imapd")
198 "A list of strings, containing commands for IMAP connection.
199 Within a string, %s is replaced with the server address, %p with port
200 number on server, %g with `imap-shell-host', and %l with
201 `imap-default-user'. The program should read IMAP commands from stdin
202 and write IMAP response to stdout. Each entry in the list is tried
203 until a successful connection is made."
205 :type '(repeat string))
207 (defvar imap-shell-host "gateway"
208 "Hostname of rlogin proxy.")
210 (defvar imap-default-user (user-login-name)
211 "Default username to use.")
213 (defvar imap-error nil
214 "Error codes from the last command.")
216 ;; Various variables.
218 (defvar imap-fetch-data-hook nil
219 "Hooks called after receiving each FETCH response.")
221 (defvar imap-streams '(gssapi kerberos4 starttls ssl network shell)
222 "Priority of streams to consider when opening connection to server.")
224 (defvar imap-stream-alist
225 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
226 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
227 (ssl imap-ssl-p imap-ssl-open)
228 (network imap-network-p imap-network-open)
229 (shell imap-shell-p imap-shell-open)
230 (starttls imap-starttls-p imap-starttls-open))
231 "Definition of network streams.
235 NAME names the stream, CHECK is a function returning non-nil if the
236 server support the stream and OPEN is a function for opening the
239 (defvar imap-authenticators '(gssapi
245 "Priority of authenticators to consider when authenticating to server.")
247 (defvar imap-authenticator-alist
248 '((gssapi imap-gssapi-auth-p imap-gssapia-auth)
249 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
250 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
251 (login imap-login-p imap-login-auth)
252 (anonymous imap-anonymous-p imap-anonymous-auth)
253 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
254 "Definition of authenticators.
256 \(NAME CHECK AUTHENTICATE)
258 NAME names the authenticator. CHECK is a function returning non-nil if
259 the server support the authenticator and AUTHENTICATE is a function
260 for doing the actuall authentification.")
262 (defvar imap-use-utf7 t
263 "If non-nil, do utf7 encoding/decoding of mailbox names.
264 Since the UTF7 decoding currently only decodes into ISO-8859-1
265 characters, you may disable this decoding if you need to access UTF7
266 encoded mailboxes which doesn't translate into ISO-8859-1.")
268 ;; Internal constants. Change theese and die.
270 (defconst imap-default-port 143)
271 (defconst imap-default-ssl-port 993)
272 (defconst imap-default-stream 'network)
273 (defconst imap-local-variables '(imap-server
282 imap-current-target-mailbox
293 ;; Internal variables.
295 (defvar imap-stream nil)
296 (defvar imap-auth nil)
297 (defvar imap-server nil)
298 (defvar imap-port nil)
299 (defvar imap-username nil)
300 (defvar imap-password nil)
301 (defvar imap-state 'closed
303 Valid states are `closed', `initial', `nonauth', `auth', `selected'
306 (defvar imap-server-eol "\r\n"
307 "The EOL string sent from the server.")
309 (defvar imap-client-eol "\r\n"
310 "The EOL string we send to the server.")
312 (defvar imap-current-mailbox nil
313 "Current mailbox name.")
315 (defvar imap-current-target-mailbox nil
316 "Current target mailbox for COPY and APPEND commands.")
318 (defvar imap-mailbox-data nil
319 "Obarray with mailbox data.")
321 (defvar imap-mailbox-prime 997
322 "Length of imap-mailbox-data.")
324 (defvar imap-current-message nil
325 "Current message number.")
327 (defvar imap-message-data nil
328 "Obarray with message data.")
330 (defvar imap-message-prime 997
331 "Length of imap-message-data.")
333 (defvar imap-capability nil
334 "Capability for server.")
336 (defvar imap-namespace nil
337 "Namespace for current server.")
339 (defvar imap-reached-tag 0
340 "Lower limit on command tags that have been parsed.")
342 (defvar imap-failed-tags nil
343 "Alist of tags that failed.
344 Each element is a list with four elements; tag (a integer), response
345 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
346 human readable response text (a string).")
349 "Command tag number.")
351 (defvar imap-process nil
354 (defvar imap-continuation nil
355 "Non-nil indicates that the server emitted a continuation request.
356 The actually value is really the text on the continuation line.")
359 "Imap session trace.")
361 (defvar imap-debug nil ;"*imap-debug*"
362 "Random debug spew.")
365 ;; Utility functions:
367 (defun imap-read-passwd (prompt &rest args)
368 "Read a password using PROMPT.
369 If ARGS, PROMPT is used as an argument to `format'."
370 (let ((prompt (if args
371 (apply 'format prompt args)
373 (funcall (if (or (fboundp 'read-passwd)
375 (fboundp 'read-passwd))
376 (and (load "passwd" t)
377 (fboundp 'read-passwd)))
379 (autoload 'ange-ftp-read-passwd "ange-ftp")
380 'ange-ftp-read-passwd)
383 (defsubst imap-utf7-encode (string)
387 (utf7-encode string t)
389 "imap: Could not UTF7 encode `%s', using it unencoded..."
394 (defsubst imap-utf7-decode (string)
398 (utf7-decode string t)
400 "imap: Could not UTF7 decode `%s', using it undecoded..."
405 (defsubst imap-ok-p (status)
408 (setq imap-error status)
411 (defun imap-error-text (&optional buffer)
412 (with-current-buffer (or buffer (current-buffer))
413 (nth 3 (car imap-failed-tags))))
416 ;; Server functions; stream stuff:
418 (defun imap-kerberos4-stream-p (buffer)
419 (imap-capability 'AUTH=KERBEROS_V4 buffer))
421 (defun imap-kerberos4-open (name buffer server port)
422 (let ((cmds imap-kerberos4-program)
424 (while (and (not done) (setq cmd (pop cmds)))
425 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
426 (let* ((port (or port imap-default-port))
427 (process (as-binary-process
429 name buffer shell-file-name shell-command-switch
434 ?p (number-to-string port)
435 ?l imap-default-user)))))
438 (with-current-buffer buffer
439 (setq imap-client-eol "\n")
440 (while (and (memq (process-status process) '(open run))
441 (goto-char (point-min))
442 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
443 (or (while (looking-at "^C:")
446 ;; cyrus 1.6 imtest print "S: " before server greeting
447 (or (not (looking-at "S: "))
450 (not (and (imap-parse-greeting)
451 ;; success in imtest < 1.6:
452 (or (re-search-forward
453 "^__\\(.*\\)__\n" nil t)
454 ;; success in imtest 1.6:
456 "^\\(Authenticat.*\\)" nil t))
457 (setq response (match-string 1)))))
458 (accept-process-output process 1)
461 (with-current-buffer (get-buffer-create imap-log)
462 (buffer-disable-undo)
463 (goto-char (point-max))
464 (insert-buffer-substring buffer)))
466 (message "Kerberos 4 IMAP connection: %s" (or response "failed"))
467 (if (and response (let ((case-fold-search nil))
468 (not (string-match "failed" response))))
470 (if (memq (process-status process) '(open run))
471 (imap-send-command-wait "LOGOUT"))
472 (delete-process process)
476 (defun imap-gssapi-stream-p (buffer)
477 (imap-capability 'AUTH=GSSAPI buffer))
479 (defun imap-gssapi-open (name buffer server port)
480 (let ((cmds imap-gssapi-program)
482 (while (and (not done) (setq cmd (pop cmds)))
483 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
484 (let* ((port (or port imap-default-port))
485 (process (as-binary-process
487 name buffer shell-file-name shell-command-switch
492 ?p (number-to-string port)
493 ?l imap-default-user)))))
496 (with-current-buffer buffer
497 (setq imap-client-eol "\n")
498 (while (and (memq (process-status process) '(open run))
499 (goto-char (point-min))
500 ;; cyrus 1.6 imtest print "S: " before server greeting
501 (or (not (looking-at "S: "))
504 (not (and (imap-parse-greeting)
505 ;; success in imtest 1.6:
507 "^\\(Authenticat.*\\)" nil t)
508 (setq response (match-string 1)))))
509 (accept-process-output process 1)
512 (with-current-buffer (get-buffer-create imap-log)
513 (buffer-disable-undo)
514 (goto-char (point-max))
515 (insert-buffer-substring buffer)))
517 (message "GSSAPI IMAP connection: %s" (or response "failed"))
518 (if (and response (let ((case-fold-search nil))
519 (not (string-match "failed" response))))
521 (if (memq (process-status process) '(open run))
522 (imap-send-command-wait "LOGOUT"))
523 (delete-process process)
527 (defun imap-ssl-p (buffer)
530 (defun imap-ssl-open (name buffer server port)
531 "Open a SSL connection to server."
532 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
533 (list imap-ssl-program)))
535 (while (and (not done) (setq cmd (pop cmds)))
536 (message "imap: Opening SSL connection with `%s'..." cmd)
537 (let* ((port (or port imap-default-ssl-port))
538 (ssl-program-name shell-file-name)
539 (ssl-program-arguments
540 (list shell-command-switch
541 (format-spec cmd (format-spec-make
543 ?p (number-to-string port)))))
547 (cond ((eq system-type 'windows-nt)
548 (let (selective-display
549 (coding-system-for-write 'binary)
550 (coding-system-for-read 'raw-text-dos)
551 (output-coding-system 'binary)
552 (input-coding-system 'raw-text-dos))
553 (open-ssl-stream name buffer server port)))
556 (open-ssl-stream name buffer server port))))))
557 (with-current-buffer buffer
558 (goto-char (point-min))
559 (while (and (memq (process-status process) '(open run))
560 (goto-char (point-max))
562 (not (imap-parse-greeting)))
563 (accept-process-output process 1)
566 (with-current-buffer (get-buffer-create imap-log)
567 (buffer-disable-undo)
568 (goto-char (point-max))
569 (insert-buffer-substring buffer)))
571 (when (memq (process-status process) '(open run))
572 (setq done process))))))
575 (message "imap: Opening SSL connection with `%s'...done" cmd)
577 (message "imap: Failed opening SSL connection")
580 (defun imap-network-p (buffer)
583 (defun imap-network-open (name buffer server port)
584 (let* ((port (or port imap-default-port))
585 (process (open-network-stream-as-binary name buffer server port)))
587 (while (and (memq (process-status process) '(open run))
588 (goto-char (point-min))
589 (not (imap-parse-greeting)))
590 (accept-process-output process 1)
593 (with-current-buffer (get-buffer-create imap-log)
594 (buffer-disable-undo)
595 (goto-char (point-max))
596 (insert-buffer-substring buffer)))
597 (when (memq (process-status process) '(open run))
600 (defun imap-shell-p (buffer)
603 (defun imap-shell-open (name buffer server port)
604 (let ((cmds imap-shell-program)
606 (while (and (not done) (setq cmd (pop cmds)))
607 (message "imap: Opening IMAP connection with `%s'..." cmd)
608 (setq imap-client-eol "\n")
609 (let* ((port (or port imap-default-port))
610 (process (as-binary-process
612 name buffer shell-file-name shell-command-switch
618 ?p (number-to-string port)
619 ?l imap-default-user))))))
621 (while (and (memq (process-status process) '(open run))
622 (goto-char (point-min))
623 (not (imap-parse-greeting)))
624 (accept-process-output process 1)
628 (with-current-buffer (get-buffer-create imap-log)
629 (buffer-disable-undo)
630 (goto-char (point-max))
631 (insert-buffer-substring buffer)))
632 (when (memq (process-status process) '(open run))
633 (setq done process)))))
636 (message "imap: Opening IMAP connection with `%s'...done" cmd)
638 (message "imap: Failed opening IMAP connection")
641 (defun imap-starttls-p (buffer)
642 (and (condition-case ()
645 (imap-capability 'STARTTLS buffer)))
647 (defun imap-starttls-open (name buffer server port)
648 (let* ((port (or port imap-default-port))
649 (process (as-binary-process
650 (starttls-open-stream name buffer server port))))
652 (while (and (memq (process-status process) '(open run))
653 (goto-char (point-min))
654 (not (imap-parse-greeting)))
655 (accept-process-output process 1)
658 (with-current-buffer (get-buffer-create imap-log)
659 (buffer-disable-undo)
660 (goto-char (point-max))
661 (insert-buffer-substring buffer)))
662 (let ((imap-process process))
665 (set-process-filter imap-process 'imap-arrival-filter)
666 (when (and (eq imap-stream 'starttls)
667 (imap-ok-p (imap-send-command-wait "STARTTLS")))
668 (starttls-negotiate imap-process)))
669 (set-process-filter imap-process nil)))
670 (when (memq (process-status process) '(open run))
673 ;; Server functions; authenticator stuff:
675 (defun imap-interactive-login (buffer loginfunc)
676 "Login to server in BUFFER.
677 LOGINFUNC is passed a username and a password, it should return t if
678 it where sucessful authenticating itself to the server, nil otherwise.
679 Returns t if login was successful, nil otherwise."
680 (with-current-buffer buffer
681 (make-variable-buffer-local 'imap-username)
682 (make-variable-buffer-local 'imap-password)
683 (let (user passwd ret)
684 ;; (condition-case ()
685 (while (or (not user) (not passwd))
686 (setq user (or imap-username
687 (read-from-minibuffer
688 (concat "IMAP username for " imap-server ": ")
689 (or user imap-default-user))))
690 (setq passwd (or imap-password
692 (concat "IMAP password for " user "@"
694 (when (and user passwd)
695 (if (funcall loginfunc user passwd)
699 (if (and (not imap-password)
700 (y-or-n-p "Store password for this session? "))
701 (setq imap-password passwd)))
702 (message "Login failed...")
705 ;; (quit (with-current-buffer buffer
708 ;; (error (with-current-buffer buffer
713 (defun imap-gssapi-auth-p (buffer)
714 (imap-capability 'AUTH=GSSAPI buffer))
716 (defun imap-gssapi-auth (buffer)
717 (eq imap-stream 'gssapi))
719 (defun imap-kerberos4-auth-p (buffer)
720 (imap-capability 'AUTH=KERBEROS_V4 buffer))
722 (defun imap-kerberos4-auth (buffer)
723 (eq imap-stream 'kerberos4))
725 (defun imap-cram-md5-p (buffer)
726 (imap-capability 'AUTH=CRAM-MD5 buffer))
728 (defun imap-cram-md5-auth (buffer)
729 "Login to server using the AUTH CRAM-MD5 method."
730 (imap-interactive-login
732 (lambda (user passwd)
734 (imap-send-command-wait
736 "AUTHENTICATE CRAM-MD5"
738 (let* ((decoded (base64-decode-string challenge))
739 (hash-function (if (and (featurep 'xemacs)
740 (>= (function-max-args 'md5) 4))
741 (lambda (object &optional start end)
742 (md5 object start end 'binary))
744 (hash (rfc2104-hash hash-function 64 16 passwd decoded))
745 (response (concat user " " hash))
746 (encoded (base64-encode-string response)))
749 (defun imap-login-p (buffer)
750 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer)))
752 (defun imap-login-auth (buffer)
753 "Login to server using the LOGIN command."
754 (imap-interactive-login buffer
755 (lambda (user passwd)
756 (imap-ok-p (imap-send-command-wait
757 (concat "LOGIN \"" user "\" \""
760 (defun imap-anonymous-p (buffer)
763 (defun imap-anonymous-auth (buffer)
764 (with-current-buffer buffer
765 (imap-ok-p (imap-send-command-wait
766 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
767 (system-name)) "\"")))))
769 (defun imap-digest-md5-p (buffer)
770 (and (condition-case ()
771 (require 'digest-md5)
773 (imap-capability 'AUTH=DIGEST-MD5 buffer)))
775 (defun imap-digest-md5-auth (buffer)
776 "Login to server using the AUTH DIGEST-MD5 method."
777 (imap-interactive-login
779 (lambda (user passwd)
783 "AUTHENTICATE DIGEST-MD5"
785 (base64-encode-string
786 (sasl-digest-md5-digest-response
787 (base64-decode-string challenge)
788 user passwd "imap" imap-server)
790 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
792 (setq imap-continuation nil)
793 (imap-send-command-1 "")
794 (imap-ok-p (imap-wait-for-tag tag)))))))
798 (defun imap-open-1 (buffer)
799 (with-current-buffer buffer
801 (setq imap-current-mailbox nil
802 imap-current-message nil
804 imap-process (condition-case ()
805 (funcall (nth 2 (assq imap-stream
807 "imap" buffer imap-server imap-port)
810 (set-process-filter imap-process 'imap-arrival-filter)
811 (set-process-sentinel imap-process 'imap-sentinel)
812 (while (and (eq imap-state 'initial)
813 (memq (process-status imap-process) '(open run)))
814 (message "Waiting for response from %s..." imap-server)
815 (accept-process-output imap-process 1))
816 (message "Waiting for response from %s...done" imap-server)
817 (and (memq (process-status imap-process) '(open run))
820 (defun imap-open (server &optional port stream auth buffer)
821 "Open a IMAP connection to host SERVER at PORT returning a buffer.
822 If PORT is unspecified, a default value is used (143 except
823 for SSL which use 993).
824 STREAM indicates the stream to use, see `imap-streams' for available
825 streams. If nil, it choices the best stream the server is capable of.
826 AUTH indicates authenticator to use, see `imap-authenticators' for
827 available authenticators. If nil, it choices the best stream the
828 server is capable of.
829 BUFFER can be a buffer or a name of a buffer, which is created if
830 necessery. If nil, the buffer name is generated."
831 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
832 (with-current-buffer (get-buffer-create buffer)
833 (if (imap-opened buffer)
835 (mapcar 'make-variable-buffer-local imap-local-variables)
836 (set-buffer-multibyte nil)
837 (buffer-disable-undo)
838 (setq imap-server (or server imap-server))
839 (setq imap-port (or port imap-port))
840 (setq imap-auth (or auth imap-auth))
841 (setq imap-stream (or stream imap-stream))
842 (when (let ((imap-stream (or imap-stream imap-default-stream)))
843 (imap-open-1 buffer))
845 (let (stream-changed)
846 (when (null imap-stream)
847 (let ((streams imap-streams))
848 (while (setq stream (pop streams))
849 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
850 (setq stream-changed (not (eq (or imap-stream
856 (error "Couldn't figure out a stream for server"))))
858 (message "Reconnecting with %s..." imap-stream)
861 (setq imap-capability nil)))
862 (if (imap-opened buffer)
863 ;; Choose authenticator
864 (when (and (null imap-auth) (not (eq imap-state 'auth)))
865 (let ((auths imap-authenticators))
866 (while (setq auth (pop auths))
867 (if (funcall (nth 1 (assq auth imap-authenticator-alist))
872 (error "Couldn't figure out authenticator for server"))))))
873 (when (imap-opened buffer)
874 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))
877 (defun imap-opened (&optional buffer)
878 "Return non-nil if connection to imap server in BUFFER is open.
879 If BUFFER is nil then the current buffer is used."
880 (and (setq buffer (get-buffer (or buffer (current-buffer))))
881 (buffer-live-p buffer)
882 (with-current-buffer buffer
884 (memq (process-status imap-process) '(open run))))))
886 (defun imap-authenticate (&optional user passwd buffer)
887 "Authenticate to server in BUFFER, using current buffer if nil.
888 It uses the authenticator specified when opening the server. If the
889 authenticator requires username/passwords, they are queried from the
890 user and optionally stored in the buffer. If USER and/or PASSWD is
891 specified, the user will not be questioned and the username and/or
892 password is remembered in the buffer."
893 (with-current-buffer (or buffer (current-buffer))
894 (if (not (eq imap-state 'nonauth))
895 (or (eq imap-state 'auth)
896 (eq imap-state 'select)
897 (eq imap-state 'examine))
898 (make-variable-buffer-local 'imap-username)
899 (make-variable-buffer-local 'imap-password)
900 (if user (setq imap-username user))
901 (if passwd (setq imap-password passwd))
902 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer)
903 (setq imap-state 'auth)))))
905 (defun imap-close (&optional buffer)
906 "Close connection to server in BUFFER.
907 If BUFFER is nil, the current buffer is used."
908 (with-current-buffer (or buffer (current-buffer))
910 (not (imap-ok-p (imap-send-command-wait "LOGOUT")))
911 (message "Server %s didn't let me log out" imap-server))
912 (when (and imap-process
913 (memq (process-status imap-process) '(open run)))
914 (delete-process imap-process))
915 (setq imap-current-mailbox nil
916 imap-current-message nil
921 (defun imap-capability (&optional identifier buffer)
922 "Return a list of identifiers which server in BUFFER support.
923 If IDENTIFIER, return non-nil if it's among the servers capabilities.
924 If BUFFER is nil, the current buffer is assumed."
925 (with-current-buffer (or buffer (current-buffer))
926 (unless imap-capability
927 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
928 (setq imap-capability '(IMAP2))))
930 (memq (intern (upcase (symbol-name identifier))) imap-capability)
933 (defun imap-namespace (&optional buffer)
934 "Return a namespace hierarchy at server in BUFFER.
935 If BUFFER is nil, the current buffer is assumed."
936 (with-current-buffer (or buffer (current-buffer))
937 (unless imap-namespace
938 (when (imap-capability 'NAMESPACE)
939 (imap-send-command-wait "NAMESPACE")))
942 (defun imap-send-command-wait (command &optional buffer)
943 (imap-wait-for-tag (imap-send-command command buffer) buffer))
946 ;; Mailbox functions:
948 (defun imap-mailbox-put (propname value &optional mailbox buffer)
949 (with-current-buffer (or buffer (current-buffer))
950 (if imap-mailbox-data
951 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
953 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
954 propname value mailbox (current-buffer)))
957 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
958 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
961 (defun imap-mailbox-get (propname &optional mailbox buffer)
962 (let ((mailbox (imap-utf7-encode mailbox)))
963 (with-current-buffer (or buffer (current-buffer))
964 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
966 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
967 (with-current-buffer (or buffer (current-buffer))
971 (push (funcall func (if mailbox-decoder
972 (funcall mailbox-decoder (symbol-name s))
973 (symbol-name s))) result))
977 (defun imap-mailbox-map (func &optional buffer)
978 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
979 Function should take a mailbox name (a string) as
981 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
983 (defun imap-current-mailbox (&optional buffer)
984 (with-current-buffer (or buffer (current-buffer))
985 (imap-utf7-decode imap-current-mailbox)))
987 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
988 (and (string= mailbox imap-current-mailbox)
990 (eq imap-state 'examine))
992 (eq imap-state 'selected)))))
994 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
995 (with-current-buffer (or buffer (current-buffer))
996 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
998 (defun imap-mailbox-select-1 (mailbox &optional examine)
999 "Select MAILBOX on server in BUFFER.
1000 If EXAMINE is non-nil, do a read-only select."
1001 (if (imap-current-mailbox-p-1 mailbox examine)
1002 imap-current-mailbox
1003 (setq imap-current-mailbox mailbox)
1004 (if (imap-ok-p (imap-send-command-wait
1005 (concat (if examine "EXAMINE" "SELECT") " \""
1008 (setq imap-message-data (make-vector imap-message-prime 0)
1009 imap-state (if examine 'examine 'selected))
1010 imap-current-mailbox)
1011 ;; Failed SELECT/EXAMINE unselects current mailbox
1012 (setq imap-current-mailbox nil))))
1014 (defun imap-mailbox-select (mailbox &optional examine buffer)
1015 (with-current-buffer (or buffer (current-buffer))
1017 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1019 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1020 (with-current-buffer (or buffer (current-buffer))
1021 (imap-mailbox-select-1 mailbox 'exmine)))
1023 (defun imap-mailbox-examine (mailbox &optional buffer)
1024 "Examine MAILBOX on server in BUFFER."
1025 (imap-mailbox-select mailbox 'exmine buffer))
1027 (defun imap-mailbox-unselect (&optional buffer)
1028 "Close current folder in BUFFER, without expunging articles."
1029 (with-current-buffer (or buffer (current-buffer))
1030 (when (or (eq imap-state 'auth)
1031 (and (imap-capability 'UNSELECT)
1032 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1034 (imap-send-command-wait (concat "EXAMINE \""
1035 imap-current-mailbox
1037 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1038 (setq imap-current-mailbox nil
1039 imap-message-data nil
1043 (defun imap-mailbox-expunge (&optional buffer)
1044 "Expunge articles in current folder in BUFFER.
1045 If BUFFER is nil the current buffer is assumed."
1046 (with-current-buffer (or buffer (current-buffer))
1047 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1048 (imap-ok-p (imap-send-command-wait "EXPUNGE")))))
1050 (defun imap-mailbox-close (&optional buffer)
1051 "Expunge articles and close current folder in BUFFER.
1052 If BUFFER is nil the current buffer is assumed."
1053 (with-current-buffer (or buffer (current-buffer))
1054 (when (and imap-current-mailbox
1055 (imap-ok-p (imap-send-command-wait "CLOSE")))
1056 (setq imap-current-mailbox nil
1057 imap-message-data nil
1061 (defun imap-mailbox-create-1 (mailbox)
1062 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1064 (defun imap-mailbox-create (mailbox &optional buffer)
1065 "Create MAILBOX on server in BUFFER.
1066 If BUFFER is nil the current buffer is assumed."
1067 (with-current-buffer (or buffer (current-buffer))
1068 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1070 (defun imap-mailbox-delete (mailbox &optional buffer)
1071 "Delete MAILBOX on server in BUFFER.
1072 If BUFFER is nil the current buffer is assumed."
1073 (let ((mailbox (imap-utf7-encode mailbox)))
1074 (with-current-buffer (or buffer (current-buffer))
1076 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1078 (defun imap-mailbox-rename (oldname newname &optional buffer)
1079 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1080 If BUFFER is nil the current buffer is assumed."
1081 (let ((oldname (imap-utf7-encode oldname))
1082 (newname (imap-utf7-encode newname)))
1083 (with-current-buffer (or buffer (current-buffer))
1085 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1086 "\"" newname "\""))))))
1088 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1089 "Return a list of subscribed mailboxes on server in BUFFER.
1090 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1091 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1092 implementation-specific string that has to be passed to lsub command."
1093 (with-current-buffer (or buffer (current-buffer))
1094 ;; Make sure we know the hierarchy separator for root's hierarchy
1095 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1096 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1097 (imap-utf7-encode root) "\"")))
1098 ;; clear list data (NB not delimiter and other stuff)
1099 (imap-mailbox-map-1 (lambda (mailbox)
1100 (imap-mailbox-put 'lsub nil mailbox)))
1102 (imap-send-command-wait
1103 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1104 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1107 (imap-mailbox-map-1 (lambda (mailbox)
1108 (when (imap-mailbox-get-1 'lsub mailbox)
1109 (push (imap-utf7-decode mailbox) out))))
1112 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1113 "Return a list of mailboxes matching ROOT on server in BUFFER.
1114 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1115 root. REFERENCE is a implementation-specific string that has to be
1116 passed to list command."
1117 (with-current-buffer (or buffer (current-buffer))
1118 ;; Make sure we know the hierarchy separator for root's hierarchy
1119 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1120 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1121 (imap-utf7-encode root) "\"")))
1122 ;; clear list data (NB not delimiter and other stuff)
1123 (imap-mailbox-map-1 (lambda (mailbox)
1124 (imap-mailbox-put 'list nil mailbox)))
1126 (imap-send-command-wait
1127 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1128 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1131 (imap-mailbox-map-1 (lambda (mailbox)
1132 (when (imap-mailbox-get-1 'list mailbox)
1133 (push (imap-utf7-decode mailbox) out))))
1136 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1137 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1138 Returns non-nil if successful."
1139 (with-current-buffer (or buffer (current-buffer))
1140 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1141 (imap-utf7-encode mailbox)
1144 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1145 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1146 Returns non-nil if successful."
1147 (with-current-buffer (or buffer (current-buffer))
1148 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1149 (imap-utf7-encode mailbox)
1152 (defun imap-mailbox-status (mailbox items &optional buffer)
1153 "Get status items ITEM in MAILBOX from server in BUFFER.
1154 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1155 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1156 or 'unseen. If ITEMS is a list of symbols, a list of values is
1157 returned, if ITEMS is a symbol only it's value is returned."
1158 (with-current-buffer (or buffer (current-buffer))
1160 (imap-send-command-wait (list "STATUS \""
1161 (imap-utf7-encode mailbox)
1168 (mapcar (lambda (item)
1169 (imap-mailbox-get item mailbox))
1171 (imap-mailbox-get items mailbox)))))
1173 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1174 "Get ACL on mailbox from server in BUFFER."
1175 (let ((mailbox (imap-utf7-encode mailbox)))
1176 (with-current-buffer (or buffer (current-buffer))
1178 (imap-send-command-wait (list "GETACL \""
1179 (or mailbox imap-current-mailbox)
1181 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1183 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1184 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1185 (let ((mailbox (imap-utf7-encode mailbox)))
1186 (with-current-buffer (or buffer (current-buffer))
1188 (imap-send-command-wait (list "SETACL \""
1189 (or mailbox imap-current-mailbox)
1195 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1196 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1197 (let ((mailbox (imap-utf7-encode mailbox)))
1198 (with-current-buffer (or buffer (current-buffer))
1200 (imap-send-command-wait (list "DELETEACL \""
1201 (or mailbox imap-current-mailbox)
1206 ;; Message functions:
1208 (defun imap-current-message (&optional buffer)
1209 (with-current-buffer (or buffer (current-buffer))
1210 imap-current-message))
1212 (defun imap-list-to-message-set (list)
1213 (mapconcat (lambda (item)
1214 (number-to-string item))
1220 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1221 (with-current-buffer (or buffer (current-buffer))
1222 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1224 (imap-list-to-message-set uids)
1228 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1229 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1230 UIDS can be a string, number or a list of numbers. If RECEIVE
1231 is non-nil return theese properties."
1232 (with-current-buffer (or buffer (current-buffer))
1233 (when (imap-ok-p (imap-send-command-wait
1234 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1236 (imap-list-to-message-set uids)
1239 (if (or (null receive) (stringp uids))
1242 (mapcar (lambda (uid)
1244 (mapcar (lambda (prop)
1245 (imap-message-get uid prop))
1247 (imap-message-get uid receive)))
1249 (imap-message-get uids receive))))))
1251 (defun imap-message-put (uid propname value &optional buffer)
1252 (with-current-buffer (or buffer (current-buffer))
1253 (if imap-message-data
1254 (put (intern (number-to-string uid) imap-message-data)
1256 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1257 uid propname value (current-buffer)))
1260 (defun imap-message-get (uid propname &optional buffer)
1261 (with-current-buffer (or buffer (current-buffer))
1262 (get (intern-soft (number-to-string uid) imap-message-data)
1265 (defun imap-message-map (func propname &optional buffer)
1266 "Map a function across each mailbox in `imap-message-data', returning a list."
1267 (with-current-buffer (or buffer (current-buffer))
1271 (push (funcall func (get s 'UID) (get s propname)) result))
1275 (defmacro imap-message-envelope-date (uid &optional buffer)
1276 `(with-current-buffer (or ,buffer (current-buffer))
1277 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1279 (defmacro imap-message-envelope-subject (uid &optional buffer)
1280 `(with-current-buffer (or ,buffer (current-buffer))
1281 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1283 (defmacro imap-message-envelope-from (uid &optional buffer)
1284 `(with-current-buffer (or ,buffer (current-buffer))
1285 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1287 (defmacro imap-message-envelope-sender (uid &optional buffer)
1288 `(with-current-buffer (or ,buffer (current-buffer))
1289 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1291 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1292 `(with-current-buffer (or ,buffer (current-buffer))
1293 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1295 (defmacro imap-message-envelope-to (uid &optional buffer)
1296 `(with-current-buffer (or ,buffer (current-buffer))
1297 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1299 (defmacro imap-message-envelope-cc (uid &optional buffer)
1300 `(with-current-buffer (or ,buffer (current-buffer))
1301 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1303 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1304 `(with-current-buffer (or ,buffer (current-buffer))
1305 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1307 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1308 `(with-current-buffer (or ,buffer (current-buffer))
1309 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1311 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1312 `(with-current-buffer (or ,buffer (current-buffer))
1313 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1315 (defmacro imap-message-body (uid &optional buffer)
1316 `(with-current-buffer (or ,buffer (current-buffer))
1317 (imap-message-get ,uid 'BODY)))
1319 (defun imap-search (predicate &optional buffer)
1320 (with-current-buffer (or buffer (current-buffer))
1321 (imap-mailbox-put 'search 'dummy)
1322 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1323 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1324 (error "Missing SEARCH response to a SEARCH command")
1325 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1327 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1328 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1329 (with-current-buffer (or buffer (current-buffer))
1330 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1331 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1333 (defun imap-message-flags-set (articles flags &optional silent buffer)
1334 (when (and articles flags)
1335 (with-current-buffer (or buffer (current-buffer))
1336 (imap-ok-p (imap-send-command-wait
1337 (concat "UID STORE " articles
1338 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1340 (defun imap-message-flags-del (articles flags &optional silent buffer)
1341 (when (and articles flags)
1342 (with-current-buffer (or buffer (current-buffer))
1343 (imap-ok-p (imap-send-command-wait
1344 (concat "UID STORE " articles
1345 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1347 (defun imap-message-flags-add (articles flags &optional silent buffer)
1348 (when (and articles flags)
1349 (with-current-buffer (or buffer (current-buffer))
1350 (imap-ok-p (imap-send-command-wait
1351 (concat "UID STORE " articles
1352 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1354 (defun imap-message-copyuid-1 (mailbox)
1355 (if (imap-capability 'UIDPLUS)
1356 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1357 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1358 (let ((old-mailbox imap-current-mailbox)
1360 (imap-message-data (make-vector 2 0)))
1361 (when (imap-mailbox-examine-1 mailbox)
1363 (and (imap-fetch "*" "UID")
1364 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1365 (apply 'max (imap-message-map
1366 (lambda (uid prop) uid) 'UID))))
1368 (imap-mailbox-select old-mailbox (eq state 'examine))
1369 (imap-mailbox-unselect)))))))
1371 (defun imap-message-copyuid (mailbox &optional buffer)
1372 (with-current-buffer (or buffer (current-buffer))
1373 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1375 (defun imap-message-copy (articles mailbox
1376 &optional dont-create no-copyuid buffer)
1377 "Copy ARTICLES (a string message set) to MAILBOX on server in
1378 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1379 non-nil, it will not create a mailbox. On success, return a list with
1380 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1381 first element, rest of list contain the saved articles' UIDs."
1383 (with-current-buffer (or buffer (current-buffer))
1384 (let ((mailbox (imap-utf7-encode mailbox)))
1385 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1386 (imap-current-target-mailbox mailbox))
1387 (if (imap-ok-p (imap-send-command-wait cmd))
1389 (when (and (not dont-create)
1390 (imap-mailbox-get-1 'trycreate mailbox))
1391 (imap-mailbox-create-1 mailbox)
1392 (imap-ok-p (imap-send-command-wait cmd)))))
1394 (imap-message-copyuid-1 mailbox)))))))
1396 (defun imap-message-appenduid-1 (mailbox)
1397 (if (imap-capability 'UIDPLUS)
1398 (imap-mailbox-get-1 'appenduid mailbox)
1399 (let ((old-mailbox imap-current-mailbox)
1401 (imap-message-data (make-vector 2 0)))
1402 (when (imap-mailbox-examine-1 mailbox)
1404 (and (imap-fetch "*" "UID")
1405 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1406 (apply 'max (imap-message-map
1407 (lambda (uid prop) uid) 'UID))))
1409 (imap-mailbox-select old-mailbox (eq state 'examine))
1410 (imap-mailbox-unselect)))))))
1412 (defun imap-message-appenduid (mailbox &optional buffer)
1413 (with-current-buffer (or buffer (current-buffer))
1414 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1416 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1417 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1418 FLAGS and DATE-TIME is currently not used. Return a cons holding
1419 uidvalidity of MAILBOX and UID the newly created article got, or nil
1421 (let ((mailbox (imap-utf7-encode mailbox)))
1422 (with-current-buffer (or buffer (current-buffer))
1423 (and (let ((imap-current-target-mailbox mailbox))
1425 (imap-send-command-wait
1426 (list "APPEND \"" mailbox "\" " article))))
1427 (imap-message-appenduid-1 mailbox)))))
1429 (defun imap-body-lines (body)
1430 "Return number of lines in article by looking at the mime bodystructure BODY."
1432 (if (stringp (car body))
1433 ;; upcase for bug in courier imap server
1434 (cond ((and (string= (upcase (car body)) "TEXT")
1435 (numberp (nth 7 body)))
1437 ((and (string= (upcase (car body)) "MESSAGE")
1438 (numberp (nth 9 body)))
1441 (apply '+ (mapcar 'imap-body-lines body)))
1444 (defun imap-envelope-from (from)
1445 "Return a from string line."
1447 (concat (aref from 0)
1448 (if (aref from 0) " <")
1452 (if (aref from 0) ">"))))
1455 ;; Internal functions.
1457 (defun imap-send-command-1 (cmdstr)
1458 (setq cmdstr (concat cmdstr imap-client-eol))
1460 (with-current-buffer (get-buffer-create imap-log)
1461 (buffer-disable-undo)
1462 (goto-char (point-max))
1464 (process-send-string imap-process cmdstr))
1466 (defun imap-send-command (command &optional buffer)
1467 (with-current-buffer (or buffer (current-buffer))
1468 (if (not (listp command)) (setq command (list command)))
1469 (let ((tag (setq imap-tag (1+ imap-tag)))
1471 (setq cmdstr (concat (number-to-string imap-tag) " "))
1472 (while (setq cmd (pop command))
1473 (cond ((stringp cmd)
1474 (setq cmdstr (concat cmdstr cmd)))
1477 (concat cmdstr (format "{%d}" (with-current-buffer cmd
1481 (imap-send-command-1 cmdstr)
1483 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1484 (setq command nil);; abort command if no cont-req
1485 (let ((process imap-process)
1486 (stream imap-stream)
1487 (eol imap-client-eol))
1488 (with-current-buffer cmd
1489 (when (not (equal eol "\r\n"))
1490 ;; XXX modifies buffer!
1491 (goto-char (point-min))
1492 (while (search-forward "\r\n" nil t)
1493 (replace-match eol)))
1495 (with-current-buffer (get-buffer-create
1497 (buffer-disable-undo)
1498 (goto-char (point-max))
1499 (insert-buffer-substring cmd)))
1500 (process-send-region process (point-min)
1502 (process-send-string process imap-client-eol))))
1503 (setq imap-continuation nil)))
1505 (imap-send-command-1 cmdstr)
1508 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1509 (setq command nil);; abort command if no cont-req
1510 (setq command (cons (funcall cmd imap-continuation)
1512 (setq imap-continuation nil)))
1514 (error "Unknown command type"))))
1516 (imap-send-command-1 cmdstr))
1519 (defun imap-wait-for-tag (tag &optional buffer)
1520 (with-current-buffer (or buffer (current-buffer))
1521 (while (and (null imap-continuation)
1522 (< imap-reached-tag tag))
1523 (or (and (not (memq (process-status imap-process) '(open run)))
1525 (accept-process-output imap-process 1)))
1526 (or (assq tag imap-failed-tags)
1527 (if imap-continuation
1531 (defun imap-sentinel (process string)
1532 (delete-process process))
1534 (defun imap-find-next-line ()
1535 "Return point at end of current line, taking into account literals.
1536 Return nil if no complete line has arrived."
1537 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1540 (if (match-string 1)
1541 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1543 (goto-char (+ (point) (string-to-number (match-string 1))))
1544 (imap-find-next-line))
1547 (defun imap-arrival-filter (proc string)
1548 "IMAP process filter."
1549 (with-current-buffer (process-buffer proc)
1550 (goto-char (point-max))
1553 (with-current-buffer (get-buffer-create imap-log)
1554 (buffer-disable-undo)
1555 (goto-char (point-max))
1558 (goto-char (point-min))
1559 (while (setq end (imap-find-next-line))
1561 (narrow-to-region (point-min) end)
1562 (delete-backward-char (length imap-server-eol))
1563 (goto-char (point-min))
1565 (cond ((eq imap-state 'initial)
1566 (imap-parse-greeting))
1567 ((or (eq imap-state 'auth)
1568 (eq imap-state 'nonauth)
1569 (eq imap-state 'selected)
1570 (eq imap-state 'examine))
1571 (imap-parse-response))
1573 (message "Unknown state %s in arrival filter"
1575 (delete-region (point-min) (point-max))))))))
1580 (defsubst imap-forward ()
1581 (or (eobp) (forward-char)))
1584 ;; ; Unsigned 32-bit integer
1585 ;; ; (0 <= n < 4,294,967,296)
1587 (defsubst imap-parse-number ()
1588 (when (looking-at "[0-9]+")
1590 (string-to-number (match-string 0))
1591 (goto-char (match-end 0)))))
1593 ;; literal = "{" number "}" CRLF *CHAR8
1594 ;; ; Number represents the number of CHAR8s
1596 (defsubst imap-parse-literal ()
1597 (when (looking-at "{\\([0-9]+\\)}\r\n")
1598 (let ((pos (match-end 0))
1599 (len (string-to-number (match-string 1))))
1600 (if (< (point-max) (+ pos len))
1602 (goto-char (+ pos len))
1603 (buffer-substring pos (+ pos len))))))
1605 ;; string = quoted / literal
1607 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1609 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1610 ;; "\" quoted-specials
1612 ;; quoted-specials = DQUOTE / "\"
1614 ;; TEXT-CHAR = <any CHAR except CR and LF>
1616 (defsubst imap-parse-string ()
1617 (cond ((eq (char-after) ?\")
1619 (let ((p (point)) (name ""))
1620 (skip-chars-forward "^\"\\\\")
1621 (setq name (buffer-substring p (point)))
1622 (while (eq (char-after) ?\\)
1623 (setq p (1+ (point)))
1625 (skip-chars-forward "^\"\\\\")
1626 (setq name (concat name (buffer-substring p (point)))))
1629 ((eq (char-after) ?{)
1630 (imap-parse-literal))))
1634 (defsubst imap-parse-nil ()
1635 (if (looking-at "NIL")
1636 (goto-char (match-end 0))))
1638 ;; nstring = string / nil
1640 (defsubst imap-parse-nstring ()
1641 (or (imap-parse-string)
1642 (and (imap-parse-nil)
1645 ;; astring = atom / string
1647 ;; atom = 1*ATOM-CHAR
1649 ;; ATOM-CHAR = <any CHAR except atom-specials>
1651 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1654 ;; list-wildcards = "%" / "*"
1656 ;; quoted-specials = DQUOTE / "\"
1658 (defsubst imap-parse-astring ()
1659 (or (imap-parse-string)
1660 (buffer-substring (point)
1661 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1662 (goto-char (1- (match-end 0)))
1666 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
1669 ;; addr-adl = nstring
1670 ;; ; Holds route from [RFC-822] route-addr if
1673 ;; addr-host = nstring
1674 ;; ; NIL indicates [RFC-822] group syntax.
1675 ;; ; Otherwise, holds [RFC-822] domain name
1677 ;; addr-mailbox = nstring
1678 ;; ; NIL indicates end of [RFC-822] group; if
1679 ;; ; non-NIL and addr-host is NIL, holds
1680 ;; ; [RFC-822] group name.
1681 ;; ; Otherwise, holds [RFC-822] local-part
1682 ;; ; after removing [RFC-822] quoting
1684 ;; addr-name = nstring
1685 ;; ; If non-NIL, holds phrase from [RFC-822]
1686 ;; ; mailbox after removing [RFC-822] quoting
1689 (defsubst imap-parse-address ()
1691 (when (eq (char-after) ?\()
1693 (setq address (vector (prog1 (imap-parse-nstring)
1695 (prog1 (imap-parse-nstring)
1697 (prog1 (imap-parse-nstring)
1699 (imap-parse-nstring)))
1700 (when (eq (char-after) ?\))
1704 ;; address-list = "(" 1*address ")" / nil
1708 (defsubst imap-parse-address-list ()
1709 (if (eq (char-after) ?\()
1710 (let (address addresses)
1712 (while (and (not (eq (char-after) ?\)))
1713 ;; next line for MS Exchange bug
1714 (progn (and (eq (char-after) ? ) (imap-forward)) t)
1715 (setq address (imap-parse-address)))
1716 (setq addresses (cons address addresses)))
1717 (when (eq (char-after) ?\))
1719 (nreverse addresses)))
1720 (assert (imap-parse-nil))))
1722 ;; mailbox = "INBOX" / astring
1723 ;; ; INBOX is case-insensitive. All case variants of
1724 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
1725 ;; ; not as an astring. An astring which consists of
1726 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
1727 ;; ; is considered to be INBOX and not an astring.
1728 ;; ; Refer to section 5.1 for further
1729 ;; ; semantic details of mailbox names.
1731 (defsubst imap-parse-mailbox ()
1732 (let ((mailbox (imap-parse-astring)))
1733 (if (string-equal "INBOX" (upcase mailbox))
1737 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
1739 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
1740 ;; ; Authentication condition
1742 ;; resp-cond-bye = "BYE" SP resp-text
1744 (defun imap-parse-greeting ()
1745 "Parse a IMAP greeting."
1746 (cond ((looking-at "\\* OK ")
1747 (setq imap-state 'nonauth))
1748 ((looking-at "\\* PREAUTH ")
1749 (setq imap-state 'auth))
1750 ((looking-at "\\* BYE ")
1751 (setq imap-state 'closed))))
1753 ;; response = *(continue-req / response-data) response-done
1755 ;; continue-req = "+" SP (resp-text / base64) CRLF
1757 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
1758 ;; mailbox-data / message-data / capability-data) CRLF
1760 ;; response-done = response-tagged / response-fatal
1762 ;; response-fatal = "*" SP resp-cond-bye CRLF
1763 ;; ; Server closes connection immediately
1765 ;; response-tagged = tag SP resp-cond-state CRLF
1767 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
1768 ;; ; Status condition
1770 ;; resp-cond-bye = "BYE" SP resp-text
1772 ;; mailbox-data = "FLAGS" SP flag-list /
1773 ;; "LIST" SP mailbox-list /
1774 ;; "LSUB" SP mailbox-list /
1775 ;; "SEARCH" *(SP nz-number) /
1776 ;; "STATUS" SP mailbox SP "("
1777 ;; [status-att SP number *(SP status-att SP number)] ")" /
1778 ;; number SP "EXISTS" /
1779 ;; number SP "RECENT"
1781 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
1783 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
1785 ;; ; IMAP4rev1 servers which offer RFC 1730
1786 ;; ; compatibility MUST list "IMAP4" as the first
1789 (defun imap-parse-response ()
1790 "Parse a IMAP command response."
1792 (case (setq token (read (current-buffer)))
1793 (+ (setq imap-continuation
1794 (or (buffer-substring (min (point-max) (1+ (point)))
1797 (* (case (prog1 (setq token (read (current-buffer)))
1799 (OK (imap-parse-resp-text))
1800 (NO (imap-parse-resp-text))
1801 (BAD (imap-parse-resp-text))
1802 (BYE (imap-parse-resp-text))
1803 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
1804 (LIST (imap-parse-data-list 'list))
1805 (LSUB (imap-parse-data-list 'lsub))
1806 (SEARCH (imap-mailbox-put
1808 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
1809 (STATUS (imap-parse-status))
1810 (CAPABILITY (setq imap-capability
1811 (read (concat "(" (upcase (buffer-substring
1812 (point) (point-max)))
1814 (ACL (imap-parse-acl))
1815 (t (case (prog1 (read (current-buffer))
1817 (EXISTS (imap-mailbox-put 'exists token))
1818 (RECENT (imap-mailbox-put 'recent token))
1820 (FETCH (imap-parse-fetch token))
1821 (t (message "Garbage: %s" (buffer-string)))))))
1823 (if (not (integerp token))
1824 (message "Garbage: %s" (buffer-string))
1825 (case (prog1 (setq status (read (current-buffer)))
1828 (setq imap-reached-tag (max imap-reached-tag token))
1829 (imap-parse-resp-text)))
1831 (setq imap-reached-tag (max imap-reached-tag token))
1833 (imap-parse-resp-text))
1835 (when (eq (char-after) ?\[)
1836 (setq code (buffer-substring (point)
1837 (search-forward "]")))
1839 (setq text (buffer-substring (point) (point-max)))
1840 (push (list token status code text)
1841 imap-failed-tags))))
1843 (setq imap-reached-tag (max imap-reached-tag token))
1845 (imap-parse-resp-text))
1847 (when (eq (char-after) ?\[)
1848 (setq code (buffer-substring (point)
1849 (search-forward "]")))
1851 (setq text (buffer-substring (point) (point-max)))
1852 (push (list token status code text) imap-failed-tags)
1853 (error "Internal error, tag %s status %s code %s text %s"
1854 token status code text))))
1855 (t (message "Garbage: %s" (buffer-string))))))))))
1857 ;; resp-text = ["[" resp-text-code "]" SP] text
1859 ;; text = 1*TEXT-CHAR
1861 ;; TEXT-CHAR = <any CHAR except CR and LF>
1863 (defun imap-parse-resp-text ()
1864 (imap-parse-resp-text-code))
1866 ;; resp-text-code = "ALERT" /
1867 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
1868 ;; "NEWNAME" SP string SP string /
1870 ;; "PERMANENTFLAGS" SP "("
1871 ;; [flag-perm *(SP flag-perm)] ")" /
1875 ;; "UIDNEXT" SP nz-number /
1876 ;; "UIDVALIDITY" SP nz-number /
1877 ;; "UNSEEN" SP nz-number /
1878 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
1880 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
1882 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
1884 ;; set = sequence-num / (sequence-num ":" sequence-num) /
1886 ;; ; Identifies a set of messages. For message
1887 ;; ; sequence numbers, these are consecutive
1888 ;; ; numbers from 1 to the number of messages in
1890 ;; ; Comma delimits individual numbers, colon
1891 ;; ; delimits between two numbers inclusive.
1892 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
1893 ;; ; 14,15 for a mailbox with 15 messages.
1895 ;; sequence-num = nz-number / "*"
1896 ;; ; * is the largest number in use. For message
1897 ;; ; sequence numbers, it is the number of messages
1898 ;; ; in the mailbox. For unique identifiers, it is
1899 ;; ; the unique identifier of the last message in
1902 ;; flag-perm = flag / "\*"
1904 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1905 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1906 ;; ; Does not include "\Recent"
1908 ;; flag-extension = "\" atom
1909 ;; ; Future expansion. Client implementations
1910 ;; ; MUST accept flag-extension flags. Server
1911 ;; ; implementations MUST NOT generate
1912 ;; ; flag-extension flags except as defined by
1913 ;; ; future standard or standards-track
1914 ;; ; revisions of this specification.
1916 ;; flag-keyword = atom
1918 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
1920 (defun imap-parse-resp-text-code ()
1921 (when (eq (char-after) ?\[)
1923 (cond ((search-forward "PERMANENTFLAGS " nil t)
1924 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
1925 ((search-forward "UIDNEXT " nil t)
1926 (imap-mailbox-put 'uidnext (read (current-buffer))))
1927 ((search-forward "UNSEEN " nil t)
1928 (imap-mailbox-put 'unseen (read (current-buffer))))
1929 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
1930 (imap-mailbox-put 'uidvalidity (match-string 1)))
1931 ((search-forward "READ-ONLY" nil t)
1932 (imap-mailbox-put 'read-only t))
1933 ((search-forward "NEWNAME " nil t)
1934 (let (oldname newname)
1935 (setq oldname (imap-parse-string))
1937 (setq newname (imap-parse-string))
1938 (imap-mailbox-put 'newname newname oldname)))
1939 ((search-forward "TRYCREATE" nil t)
1940 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
1941 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
1942 (imap-mailbox-put 'appenduid
1943 (list (match-string 1)
1944 (string-to-number (match-string 2)))
1945 imap-current-target-mailbox))
1946 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
1947 (imap-mailbox-put 'copyuid (list (match-string 1)
1950 imap-current-target-mailbox))
1951 ((search-forward "ALERT] " nil t)
1952 (message "Imap server %s information: %s" imap-server
1953 (buffer-substring (point) (point-max)))))))
1955 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
1956 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
1958 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
1959 ;; *(SP mbx-list-oflag) /
1960 ;; mbx-list-oflag *(SP mbx-list-oflag)
1962 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
1963 ;; ; Other flags; multiple possible per LIST response
1965 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
1966 ;; ; Selectability flags; only one per LIST response
1968 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1969 ;; "\" quoted-specials
1971 ;; quoted-specials = DQUOTE / "\"
1973 (defun imap-parse-data-list (type)
1974 (let (flags delimiter mailbox)
1975 (setq flags (imap-parse-flag-list))
1976 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
1977 (setq delimiter (match-string 1))
1978 (goto-char (1+ (match-end 0)))
1979 (when (setq mailbox (imap-parse-mailbox))
1980 (imap-mailbox-put type t mailbox)
1981 (imap-mailbox-put 'list-flags flags mailbox)
1982 (imap-mailbox-put 'delimiter delimiter mailbox)))))
1984 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
1985 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
1986 ;; "INTERNALDATE" SPACE date_time /
1987 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
1988 ;; "RFC822.SIZE" SPACE number /
1989 ;; "BODY" ["STRUCTURE"] SPACE body /
1990 ;; "BODY" section ["<" number ">"] SPACE nstring /
1991 ;; "UID" SPACE uniqueid) ")"
1993 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
1994 ;; SPACE time SPACE zone <">
1996 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
1997 ;; ["." (section_text / "MIME")])] "]"
1999 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2000 ;; SPACE header_list / "TEXT"
2002 ;; header_fld_name ::= astring
2004 ;; header_list ::= "(" 1#header_fld_name ")"
2006 (defsubst imap-parse-header-list ()
2007 (when (eq (char-after) ?\()
2009 (while (not (eq (char-after) ?\)))
2011 (push (imap-parse-astring) strlist))
2013 (nreverse strlist))))
2015 (defsubst imap-parse-fetch-body-section ()
2017 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2018 (if (eq (char-before) ? )
2020 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2021 (search-forward "]" nil t))
2024 (defun imap-parse-fetch (response)
2025 (when (eq (char-after) ?\()
2026 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2027 rfc822size body bodydetail bodystructure)
2028 (while (not (eq (char-after) ?\)))
2030 (let ((token (read (current-buffer))))
2032 (cond ((eq token 'UID)
2033 (setq uid (ignore-errors (read (current-buffer)))))
2035 (setq flags (imap-parse-flag-list)))
2036 ((eq token 'ENVELOPE)
2037 (setq envelope (imap-parse-envelope)))
2038 ((eq token 'INTERNALDATE)
2039 (setq internaldate (imap-parse-string)))
2041 (setq rfc822 (imap-parse-nstring)))
2042 ((eq token 'RFC822.HEADER)
2043 (setq rfc822header (imap-parse-nstring)))
2044 ((eq token 'RFC822.TEXT)
2045 (setq rfc822text (imap-parse-nstring)))
2046 ((eq token 'RFC822.SIZE)
2047 (setq rfc822size (read (current-buffer))))
2049 (if (eq (char-before) ?\[)
2051 (upcase (imap-parse-fetch-body-section))
2052 (and (eq (char-after) ?<)
2053 (buffer-substring (1+ (point))
2054 (search-forward ">" nil t)))
2055 (progn (imap-forward)
2056 (imap-parse-nstring)))
2058 (setq body (imap-parse-body))))
2059 ((eq token 'BODYSTRUCTURE)
2060 (setq bodystructure (imap-parse-body))))))
2062 (setq imap-current-message uid)
2063 (imap-message-put uid 'UID uid)
2064 (and flags (imap-message-put uid 'FLAGS flags))
2065 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2066 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2067 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2068 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2069 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2070 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2071 (and body (imap-message-put uid 'BODY body))
2072 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2073 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2074 (run-hooks 'imap-fetch-data-hook)))))
2076 ;; mailbox-data = ...
2077 ;; "STATUS" SP mailbox SP "("
2078 ;; [status-att SP number
2079 ;; *(SP status-att SP number)] ")"
2082 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2085 (defun imap-parse-status ()
2086 (let ((mailbox (imap-parse-mailbox)))
2087 (when (and mailbox (search-forward "(" nil t))
2088 (while (not (eq (char-after) ?\)))
2089 (let ((token (read (current-buffer))))
2090 (cond ((eq token 'MESSAGES)
2091 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2093 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2094 ((eq token 'UIDNEXT)
2095 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox))
2096 ((eq token 'UIDVALIDITY)
2097 (and (looking-at " \\([0-9]+\\)")
2098 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox)
2099 (goto-char (match-end 1))))
2101 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2103 (message "Unknown status data %s in mailbox %s ignored"
2104 token mailbox))))))))
2106 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2109 ;; identifier ::= astring
2111 ;; rights ::= astring
2113 (defun imap-parse-acl ()
2114 (let ((mailbox (imap-parse-mailbox))
2115 identifier rights acl)
2116 (while (eq (char-after) ?\ )
2118 (setq identifier (imap-parse-astring))
2120 (setq rights (imap-parse-astring))
2121 (setq acl (append acl (list (cons identifier rights)))))
2122 (imap-mailbox-put 'acl acl mailbox)))
2124 ;; flag-list = "(" [flag *(SP flag)] ")"
2126 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2127 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2128 ;; ; Does not include "\Recent"
2130 ;; flag-keyword = atom
2132 ;; flag-extension = "\" atom
2133 ;; ; Future expansion. Client implementations
2134 ;; ; MUST accept flag-extension flags. Server
2135 ;; ; implementations MUST NOT generate
2136 ;; ; flag-extension flags except as defined by
2137 ;; ; future standard or standards-track
2138 ;; ; revisions of this specification.
2140 (defun imap-parse-flag-list ()
2141 (let ((str (buffer-substring (point) (search-forward ")" nil t)))
2143 (while (setq pos (string-match "\\\\" str (and pos (+ 2 pos))))
2144 (setq str (replace-match "\\\\" nil t str)))
2145 (mapcar 'symbol-name (read str))))
2147 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2148 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2149 ;; env-in-reply-to SP env-message-id ")"
2151 ;; env-bcc = "(" 1*address ")" / nil
2153 ;; env-cc = "(" 1*address ")" / nil
2155 ;; env-date = nstring
2157 ;; env-from = "(" 1*address ")" / nil
2159 ;; env-in-reply-to = nstring
2161 ;; env-message-id = nstring
2163 ;; env-reply-to = "(" 1*address ")" / nil
2165 ;; env-sender = "(" 1*address ")" / nil
2167 ;; env-subject = nstring
2169 ;; env-to = "(" 1*address ")" / nil
2171 (defun imap-parse-envelope ()
2172 (when (eq (char-after) ?\()
2174 (vector (prog1 (imap-parse-nstring);; date
2176 (prog1 (imap-parse-nstring);; subject
2178 (prog1 (imap-parse-address-list);; from
2180 (prog1 (imap-parse-address-list);; sender
2182 (prog1 (imap-parse-address-list);; reply-to
2184 (prog1 (imap-parse-address-list);; to
2186 (prog1 (imap-parse-address-list);; cc
2188 (prog1 (imap-parse-address-list);; bcc
2190 (prog1 (imap-parse-nstring);; in-reply-to
2192 (prog1 (imap-parse-nstring);; message-id
2195 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2197 (defsubst imap-parse-string-list ()
2198 (cond ((eq (char-after) ?\();; body-fld-param
2201 (while (setq str (imap-parse-string))
2204 (nreverse strlist)))
2208 ;; body-extension = nstring / number /
2209 ;; "(" body-extension *(SP body-extension) ")"
2210 ;; ; Future expansion. Client implementations
2211 ;; ; MUST accept body-extension fields. Server
2212 ;; ; implementations MUST NOT generate
2213 ;; ; body-extension fields except as defined by
2214 ;; ; future standard or standards-track
2215 ;; ; revisions of this specification.
2217 (defun imap-parse-body-extension ()
2218 (if (eq (char-after) ?\()
2221 (push (imap-parse-body-extension) b-e)
2222 (while (eq (char-after) ?\ )
2224 (push (imap-parse-body-extension) b-e))
2225 (assert (eq (char-after) ?\)))
2228 (or (imap-parse-number)
2229 (imap-parse-nstring))))
2231 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2232 ;; *(SP body-extension)]]
2233 ;; ; MUST NOT be returned on non-extensible
2236 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2237 ;; *(SP body-extension)]]
2238 ;; ; MUST NOT be returned on non-extensible
2241 (defsubst imap-parse-body-ext ()
2243 (when (eq (char-after) ?\ );; body-fld-dsp
2246 (if (eq (char-after) ?\()
2249 (push (imap-parse-string) dsp)
2251 (push (imap-parse-string-list) dsp)
2253 (assert (imap-parse-nil)))
2254 (push (nreverse dsp) ext))
2255 (when (eq (char-after) ?\ );; body-fld-lang
2257 (if (eq (char-after) ?\()
2258 (push (imap-parse-string-list) ext)
2259 (push (imap-parse-nstring) ext))
2260 (while (eq (char-after) ?\ );; body-extension
2262 (setq ext (append (imap-parse-body-extension) ext)))))
2265 ;; body = "(" body-type-1part / body-type-mpart ")"
2267 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2268 ;; *(SP body-extension)]]
2269 ;; ; MUST NOT be returned on non-extensible
2272 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2273 ;; *(SP body-extension)]]
2274 ;; ; MUST NOT be returned on non-extensible
2277 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2278 ;; body-fld-enc SP body-fld-octets
2280 ;; body-fld-desc = nstring
2282 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2284 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2285 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2287 ;; body-fld-id = nstring
2289 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2291 ;; body-fld-lines = number
2293 ;; body-fld-md5 = nstring
2295 ;; body-fld-octets = number
2297 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2299 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2300 ;; [SP body-ext-1part]
2302 ;; body-type-basic = media-basic SP body-fields
2303 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2305 ;; body-type-msg = media-message SP body-fields SP envelope
2306 ;; SP body SP body-fld-lines
2308 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2310 ;; body-type-mpart = 1*body SP media-subtype
2311 ;; [SP body-ext-mpart]
2313 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2314 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2315 ;; ; Defined in [MIME-IMT]
2317 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2318 ;; ; Defined in [MIME-IMT]
2320 ;; media-subtype = string
2321 ;; ; Defined in [MIME-IMT]
2323 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2324 ;; ; Defined in [MIME-IMT]
2326 (defun imap-parse-body ()
2328 (when (eq (char-after) ?\()
2330 (if (eq (char-after) ?\()
2332 (while (and (eq (char-after) ?\()
2333 (setq subbody (imap-parse-body)))
2334 (push subbody body))
2336 (push (imap-parse-string) body);; media-subtype
2337 (when (eq (char-after) ?\ );; body-ext-mpart:
2339 (if (eq (char-after) ?\();; body-fld-param
2340 (push (imap-parse-string-list) body)
2341 (push (and (imap-parse-nil) nil) body))
2343 (append (imap-parse-body-ext) body)));; body-ext-...
2344 (assert (eq (char-after) ?\)))
2348 (push (imap-parse-string) body);; media-type
2350 (push (imap-parse-string) body);; media-subtype
2352 ;; next line for Sun SIMS bug
2353 (and (eq (char-after) ? ) (imap-forward))
2354 (if (eq (char-after) ?\();; body-fld-param
2355 (push (imap-parse-string-list) body)
2356 (push (and (imap-parse-nil) nil) body))
2358 (push (imap-parse-nstring) body);; body-fld-id
2360 (push (imap-parse-nstring) body);; body-fld-desc
2362 (push (imap-parse-string) body);; body-fld-enc
2364 (push (imap-parse-number) body);; body-fld-octets
2366 ;; ok, we're done parsing the required parts, what comes now is one
2369 ;; envelope (then we're parsing body-type-msg)
2370 ;; body-fld-lines (then we're parsing body-type-text)
2371 ;; body-ext-1part (then we're parsing body-type-basic)
2373 ;; the problem is that the two first are in turn optionally followed
2374 ;; by the third. So we parse the first two here (if there are any)...
2376 (when (eq (char-after) ?\ )
2379 (cond ((eq (char-after) ?\();; body-type-msg:
2380 (push (imap-parse-envelope) body);; envelope
2382 (push (imap-parse-body) body);; body
2384 (push (imap-parse-number) body));; body-fld-lines
2385 ((setq lines (imap-parse-number));; body-type-text:
2386 (push lines body));; body-fld-lines
2388 (backward-char)))));; no match...
2390 ;; ...and then parse the third one here...
2392 (when (eq (char-after) ?\ );; body-ext-1part:
2394 (push (imap-parse-nstring) body);; body-fld-md5
2395 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part..
2397 (assert (eq (char-after) ?\)))
2401 (when imap-debug ; (untrace-all)
2403 (buffer-disable-undo (get-buffer-create imap-debug))
2404 (mapcar (lambda (f) (trace-function-background f imap-debug))
2416 imap-interactive-login
2432 imap-send-command-wait
2437 imap-current-mailbox
2438 imap-current-mailbox-p-1
2439 imap-current-mailbox-p
2440 imap-mailbox-select-1
2442 imap-mailbox-examine-1
2443 imap-mailbox-examine
2444 imap-mailbox-unselect
2445 imap-mailbox-expunge
2447 imap-mailbox-create-1
2453 imap-mailbox-subscribe
2454 imap-mailbox-unsubscribe
2456 imap-mailbox-acl-get
2457 imap-mailbox-acl-set
2458 imap-mailbox-acl-delete
2459 imap-current-message
2460 imap-list-to-message-set
2467 imap-message-flag-permanent-p
2468 imap-message-flags-set
2469 imap-message-flags-del
2470 imap-message-flags-add
2471 imap-message-copyuid-1
2472 imap-message-copyuid
2474 imap-message-appenduid-1
2475 imap-message-appenduid
2487 imap-parse-resp-text
2488 imap-parse-resp-text-code
2489 imap-parse-data-list
2493 imap-parse-flag-list
2495 imap-parse-body-extension
2501 ;;; imap.el ends here