1 ;;; imap.el --- imap library
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; imap.el is an elisp library providing an interface for talking to
28 ;; imap.el is roughly divided in two parts, one that parses IMAP
29 ;; responses from the server and storing data into buffer-local
30 ;; variables, and one for utility functions which send commands to
31 ;; server, waits for an answer, and return information. The latter
32 ;; part is layered on top of the previous.
34 ;; The imap.el API consist of the following functions, other functions
35 ;; in this file should not be called directly and the result of doing
36 ;; so are at best undefined.
40 ;; imap-open, imap-opened, imap-authenticate, imap-close,
41 ;; imap-capability, imap-namespace, imap-error-text
45 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
46 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
47 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
48 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
49 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
50 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
51 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
55 ;; imap-fetch-asynch, imap-fetch,
56 ;; imap-current-message, imap-list-to-message-set,
57 ;; imap-message-get, imap-message-map
58 ;; imap-message-envelope-date, imap-message-envelope-subject,
59 ;; imap-message-envelope-from, imap-message-envelope-sender,
60 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
61 ;; imap-message-envelope-cc, imap-message-envelope-bcc
62 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
63 ;; imap-message-body, imap-message-flag-permanent-p
64 ;; imap-message-flags-set, imap-message-flags-del
65 ;; imap-message-flags-add, imap-message-copyuid
66 ;; imap-message-copy, imap-message-appenduid
67 ;; imap-message-append, imap-envelope-from
70 ;; It is my hope that these commands should be pretty self
71 ;; explanatory for someone who knows IMAP. All functions have
72 ;; additional documentation on how to invoke them.
74 ;; imap.el supports RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1). The implemented
75 ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
76 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
77 ;; LOGINDISABLED), and the GSSAPI / Kerberos V4 sections of RFC1731
78 ;; (with use of external program `imtest'), and RFC2971 (ID). It also
79 ;; takes advantage of the UNSELECT extension in Cyrus IMAPD.
81 ;; Without the work of John McClary Prevost and Jim Radford this library
82 ;; would not have seen the light of day. Many thanks.
84 ;; This is a transcript of a short interactive session for demonstration
87 ;; (imap-open "my.mail.server")
88 ;; => " *imap* my.mail.server:0"
90 ;; The rest are invoked with current buffer as the buffer returned by
91 ;; `imap-open'. It is possible to do it all without this, but it would
92 ;; look ugly here since `buffer' is always the last argument for all
93 ;; imap.el API functions.
95 ;; (imap-authenticate "myusername" "mypassword")
98 ;; (imap-mailbox-lsub "*")
99 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
101 ;; (imap-mailbox-list "INBOX.n%")
102 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
104 ;; (imap-mailbox-select "INBOX.nnimap")
107 ;; (imap-mailbox-get 'exists)
110 ;; (imap-mailbox-get 'uidvalidity)
113 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
116 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
117 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
121 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
122 ;; Use IEEE floats (which are effectively exact)? -- fx
123 ;; o Don't use `read' at all (important places already fixed)
124 ;; o Accept list of articles instead of message set string in most
125 ;; imap-message-* functions.
126 ;; o Send strings as literal if they contain, e.g., ".
130 ;; - 19991218 added starttls/digest-md5 patch,
131 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
132 ;; NB! you need SLIM for starttls.el and digest-md5.el
133 ;; - 19991023 committed to pgnus
138 (eval-when-compile (require 'cl
))
140 ;; For Emacs <22.2 and XEmacs.
141 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest _r
)))
142 (autoload 'sasl-find-mechanism
"sasl")
143 (autoload 'digest-md5-parse-digest-challenge
"digest-md5")
144 (autoload 'digest-md5-digest-response
"digest-md5")
145 (autoload 'digest-md5-digest-uri
"digest-md5")
146 (autoload 'digest-md5-challenge
"digest-md5")
147 (autoload 'rfc2104-hash
"rfc2104")
148 (autoload 'utf7-encode
"utf7")
149 (autoload 'utf7-decode
"utf7")
150 (autoload 'format-spec
"format-spec")
151 (autoload 'format-spec-make
"format-spec"))
156 "Low-level IMAP issues."
160 (defcustom imap-kerberos4-program
'("imtest -m kerberos_v4 -u %l -p %p %s"
162 "List of strings containing commands for Kerberos 4 authentication.
163 %s is replaced with server hostname, %p with port to connect to, and
164 %l with the value of `imap-default-user'. The program should accept
165 IMAP commands on stdin and return responses to stdout. Each entry in
166 the list is tried until a successful connection is made."
168 :type
'(repeat string
))
170 (defcustom imap-gssapi-program
(list
171 (concat "gsasl %s %p "
172 "--mechanism GSSAPI "
173 "--authentication-id %l")
174 "imtest -m gssapi -u %l -p %p %s")
175 "List of strings containing commands for GSSAPI (krb5) authentication.
176 %s is replaced with server hostname, %p with port to connect to, and
177 %l with the value of `imap-default-user'. The program should accept
178 IMAP commands on stdin and return responses to stdout. Each entry in
179 the list is tried until a successful connection is made."
181 :type
'(repeat string
))
183 (defcustom imap-shell-program
'("ssh %s imapd"
185 "ssh %g ssh %s imapd"
186 "rsh %g rsh %s imapd")
187 "A list of strings, containing commands for IMAP connection.
188 Within a string, %s is replaced with the server address, %p with port
189 number on server, %g with `imap-shell-host', and %l with
190 `imap-default-user'. The program should read IMAP commands from stdin
191 and write IMAP response to stdout. Each entry in the list is tried
192 until a successful connection is made."
194 :type
'(repeat string
))
196 (defcustom imap-process-connection-type nil
197 "Value for `process-connection-type' to use for Kerberos4, GSSAPI, shell, and SSL.
198 The `process-connection-type' variable controls the type of device
199 used to communicate with subprocesses. Values are nil to use a
200 pipe, or t or `pty' to use a pty. The value has no effect if the
201 system has no ptys or if all ptys are busy: then a pipe is used
202 in any case. The value takes effect when an IMAP server is
203 opened; changing it after that has no effect."
208 (defcustom imap-use-utf7 t
209 "If non-nil, do utf7 encoding/decoding of mailbox names.
210 Since the UTF7 decoding currently only decodes into ISO-8859-1
211 characters, you may disable this decoding if you need to access UTF7
212 encoded mailboxes which doesn't translate into ISO-8859-1."
216 (defcustom imap-log nil
217 "If non-nil, an imap session trace is placed in `imap-log-buffer'.
218 Note that username, passwords and other privacy sensitive
219 information (such as e-mail) may be stored in the buffer.
220 It is not written to disk, however. Do not enable this
221 variable unless you are comfortable with that.
223 See also `imap-debug'."
227 (defcustom imap-debug nil
228 "If non-nil, trace imap- functions into `imap-debug-buffer'.
229 Uses `trace-function-background', so you can turn it off with,
232 Note that username, passwords and other privacy sensitive
233 information (such as e-mail) may be stored in the buffer.
234 It is not written to disk, however. Do not enable this
235 variable unless you are comfortable with that.
237 This variable only takes effect when loading the `imap' library.
238 See also `imap-log'."
242 (defcustom imap-shell-host
"gateway"
243 "Hostname of rlogin proxy."
247 (defcustom imap-default-user
(user-login-name)
248 "Default username to use."
252 (defcustom imap-read-timeout
(if (string-match
253 "windows-nt\\|os/2\\|cygwin"
254 (symbol-name system-type
))
257 "How long to wait between checking for the end of output.
258 Shorter values mean quicker response, but is more CPU intensive."
262 (defcustom imap-store-password nil
263 "If non-nil, store session password without prompting."
267 ;; Various variables.
269 (defvar imap-fetch-data-hook nil
270 "Hooks called after receiving each FETCH response.")
272 (defvar imap-streams
'(gssapi kerberos4 starttls tls ssl network shell
)
273 "Priority of streams to consider when opening connection to server.")
275 (defvar imap-stream-alist
276 '((gssapi imap-gssapi-stream-p imap-gssapi-open
)
277 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open
)
278 (tls imap-tls-p imap-tls-open
)
279 (ssl imap-tls-p imap-tls-open
)
280 (network imap-network-p imap-network-open
)
281 (shell imap-shell-p imap-shell-open
)
282 (starttls imap-starttls-p imap-starttls-open
))
283 "Definition of network streams.
287 NAME names the stream, CHECK is a function returning non-nil if the
288 server support the stream and OPEN is a function for opening the
291 (defvar imap-authenticators
'(gssapi
298 "Priority of authenticators to consider when authenticating to server.")
300 (defvar imap-authenticator-alist
301 '((gssapi imap-gssapi-auth-p imap-gssapi-auth
)
302 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth
)
303 (sasl imap-sasl-auth-p imap-sasl-auth
)
304 (cram-md5 imap-cram-md5-p imap-cram-md5-auth
)
305 (login imap-login-p imap-login-auth
)
306 (anonymous imap-anonymous-p imap-anonymous-auth
)
307 (digest-md5 imap-digest-md5-p imap-digest-md5-auth
))
308 "Definition of authenticators.
310 \(NAME CHECK AUTHENTICATE)
312 NAME names the authenticator. CHECK is a function returning non-nil if
313 the server support the authenticator and AUTHENTICATE is a function
314 for doing the actual authentication.")
316 (defvar imap-error nil
317 "Error codes from the last command.")
319 (defvar imap-logout-timeout nil
320 "Close server immediately if it can't logout in this number of seconds.
321 If it is nil, never close server until logout completes. Normally,
322 the value of this variable will be bound to a certain value to which
323 an application program that uses this module specifies on a per-server
326 ;; Internal constants. Change these and die.
328 (defconst imap-default-port
143)
329 (defconst imap-default-ssl-port
993)
330 (defconst imap-default-tls-port
993)
331 (defconst imap-default-stream
'network
)
332 (defconst imap-coding-system-for-read
'binary
)
333 (defconst imap-coding-system-for-write
'binary
)
334 (defconst imap-local-variables
'(imap-server
343 imap-current-target-mailbox
353 imap-calculate-literal-size-first
355 (defconst imap-log-buffer
"*imap-log*")
356 (defconst imap-debug-buffer
"*imap-debug*")
358 ;; Internal variables.
360 (defvar imap-stream nil
)
361 (defvar imap-auth nil
)
362 (defvar imap-server nil
)
363 (defvar imap-port nil
)
364 (defvar imap-username nil
)
365 (defvar imap-password nil
)
366 (defvar imap-last-authenticator nil
)
367 (defvar imap-calculate-literal-size-first nil
)
368 (defvar imap-state
'closed
370 Valid states are `closed', `initial', `nonauth', `auth', `selected'
373 (defvar imap-server-eol
"\r\n"
374 "The EOL string sent from the server.")
376 (defvar imap-client-eol
"\r\n"
377 "The EOL string we send to the server.")
379 (defvar imap-current-mailbox nil
380 "Current mailbox name.")
382 (defvar imap-current-target-mailbox nil
383 "Current target mailbox for COPY and APPEND commands.")
385 (defvar imap-mailbox-data nil
386 "Obarray with mailbox data.")
388 (defvar imap-mailbox-prime
997
389 "Length of `imap-mailbox-data'.")
391 (defvar imap-current-message nil
392 "Current message number.")
394 (defvar imap-message-data nil
395 "Obarray with message data.")
397 (defvar imap-message-prime
997
398 "Length of `imap-message-data'.")
400 (defvar imap-capability nil
401 "Capability for server.")
407 (defvar imap-namespace nil
408 "Namespace for current server.")
410 (defvar imap-reached-tag
0
411 "Lower limit on command tags that have been parsed.")
413 (defvar imap-failed-tags nil
414 "Alist of tags that failed.
415 Each element is a list with four elements; tag (a integer), response
416 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
417 human readable response text (a string).")
420 "Command tag number.")
422 (defvar imap-process nil
425 (defvar imap-continuation nil
426 "Non-nil indicates that the server emitted a continuation request.
427 The actual value is really the text on the continuation line.")
429 (defvar imap-callbacks nil
430 "List of response tags and callbacks, on the form `(number . function)'.
431 The function should take two arguments, the first the IMAP tag and the
432 second the status (OK, NO, BAD etc) of the command.")
434 (defvar imap-enable-exchange-bug-workaround nil
435 "Send FETCH UID commands as *:* instead of *.
437 When non-nil, use an alternative UIDS form. Enabling appears to
438 be required for some servers (e.g., Microsoft Exchange 2007)
439 which otherwise would trigger a response `BAD The specified
440 message set is invalid.'. We don't unconditionally use this
441 form, since this is said to be significantly inefficient.
443 This variable is set to t automatically per server if the
444 canonical form fails.")
447 ;; Utility functions:
449 (defun imap-remassoc (key alist
)
450 "Delete by side effect any elements of ALIST whose car is `equal' to KEY.
451 The modified ALIST is returned. If the first member
452 of ALIST has a car that is `equal' to KEY, there is no way to remove it
453 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
454 sure of changing the value of `foo'."
456 (if (equal key
(caar alist
))
458 (setcdr alist
(imap-remassoc key
(cdr alist
)))
461 (defmacro imap-disable-multibyte
()
462 "Enable multibyte in the current buffer."
463 (unless (featurep 'xemacs
)
464 '(set-buffer-multibyte nil
)))
466 (defsubst imap-utf7-encode
(string)
470 (utf7-encode string t
)
472 "imap: Could not UTF7 encode `%s', using it unencoded..."
477 (defsubst imap-utf7-decode
(string)
481 (utf7-decode string t
)
483 "imap: Could not UTF7 decode `%s', using it undecoded..."
488 (defsubst imap-ok-p
(status)
491 (setq imap-error status
)
494 (defun imap-error-text (&optional buffer
)
495 (with-current-buffer (or buffer
(current-buffer))
496 (nth 3 (car imap-failed-tags
))))
499 ;; Server functions; stream stuff:
501 (defun imap-log (string-or-buffer)
503 (with-current-buffer (get-buffer-create imap-log-buffer
)
504 (imap-disable-multibyte)
505 (buffer-disable-undo)
506 (goto-char (point-max))
507 (if (bufferp string-or-buffer
)
508 (insert-buffer-substring string-or-buffer
)
509 (insert string-or-buffer
)))))
511 (defun imap-kerberos4-stream-p (buffer)
512 (imap-capability 'AUTH
=KERBEROS_V4 buffer
))
514 (defun imap-kerberos4-open (name buffer server port
)
515 (let ((cmds imap-kerberos4-program
)
517 (while (and (not done
) (setq cmd
(pop cmds
)))
518 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd
)
520 (let* ((port (or port imap-default-port
))
521 (coding-system-for-read imap-coding-system-for-read
)
522 (coding-system-for-write imap-coding-system-for-write
)
523 (process-connection-type imap-process-connection-type
)
524 (process (start-process
525 name buffer shell-file-name shell-command-switch
530 ?p
(number-to-string port
)
531 ?l imap-default-user
))))
534 (with-current-buffer buffer
535 (setq imap-client-eol
"\n"
536 imap-calculate-literal-size-first t
)
537 (while (and (memq (process-status process
) '(open run
))
538 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
539 (goto-char (point-min))
540 ;; Athena IMTEST can output SSL verify errors
541 (or (while (looking-at "^verify error:num=")
544 (or (while (looking-at "^TLS connection established")
547 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
548 (or (while (looking-at "^C:")
551 ;; cyrus 1.6 imtest print "S: " before server greeting
552 (or (not (looking-at "S: "))
555 (not (and (imap-parse-greeting)
556 ;; success in imtest < 1.6:
557 (or (re-search-forward
558 "^__\\(.*\\)__\n" nil t
)
559 ;; success in imtest 1.6:
561 "^\\(Authenticat.*\\)" nil t
))
562 (setq response
(match-string 1)))))
563 (accept-process-output process
1)
566 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
567 (if response
(concat "done, " response
) "failed"))
568 (if (and response
(let ((case-fold-search nil
))
569 (not (string-match "failed" response
))))
571 (if (memq (process-status process
) '(open run
))
573 (delete-process process
)
577 (defun imap-gssapi-stream-p (buffer)
578 (imap-capability 'AUTH
=GSSAPI buffer
))
580 (defun imap-gssapi-open (name buffer server port
)
581 (let ((cmds imap-gssapi-program
)
583 (while (and (not done
) (setq cmd
(pop cmds
)))
584 (message "Opening GSSAPI IMAP connection with `%s'..." cmd
)
586 (let* ((port (or port imap-default-port
))
587 (coding-system-for-read imap-coding-system-for-read
)
588 (coding-system-for-write imap-coding-system-for-write
)
589 (process-connection-type imap-process-connection-type
)
590 (process (start-process
591 name buffer shell-file-name shell-command-switch
596 ?p
(number-to-string port
)
597 ?l imap-default-user
))))
600 (with-current-buffer buffer
601 (setq imap-client-eol
"\n"
602 imap-calculate-literal-size-first t
)
603 (while (and (memq (process-status process
) '(open run
))
604 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
605 (goto-char (point-min))
606 ;; Athena IMTEST can output SSL verify errors
607 (or (while (looking-at "^verify error:num=")
610 (or (while (looking-at "^TLS connection established")
613 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
614 (or (while (looking-at "^C:")
617 ;; cyrus 1.6 imtest print "S: " before server greeting
618 (or (not (looking-at "S: "))
621 ;; GNU SASL may print 'Trying ...' first.
622 (or (not (looking-at "Trying "))
625 (not (and (imap-parse-greeting)
626 ;; success in imtest 1.6:
628 (concat "^\\(\\(Authenticat.*\\)\\|\\("
629 "Client authentication "
632 (setq response
(match-string 1)))))
633 (accept-process-output process
1)
637 (message "GSSAPI IMAP connection: %s" (or response
"failed"))
638 (if (and response
(let ((case-fold-search nil
))
639 (not (string-match "failed" response
))))
641 (if (memq (process-status process
) '(open run
))
643 (delete-process process
)
647 (defun imap-tls-p (_buffer)
650 (defun imap-tls-open (name buffer server port
)
651 (let* ((port (or port imap-default-tls-port
))
652 (coding-system-for-read imap-coding-system-for-read
)
653 (coding-system-for-write imap-coding-system-for-write
)
654 (process (open-network-stream name buffer server port
657 (while (and (memq (process-status process
) '(open run
))
658 ;; FIXME: Per the "blue moon" comment, the process/buffer
659 ;; handling here, and elsewhere in functions which open
660 ;; streams, looks confused. Obviously we can change buffers
661 ;; if a different process handler kicks in from
662 ;; `accept-process-output' or `sit-for' below, and TRT seems
663 ;; to be to `save-buffer' around those calls. (I wonder why
664 ;; `sit-for' is used with a non-zero wait.) -- fx
665 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
666 (goto-char (point-max))
668 (not (imap-parse-greeting)))
669 (accept-process-output process
1)
672 (when (memq (process-status process
) '(open run
))
675 (defun imap-network-p (_buffer)
678 (defun imap-network-open (name buffer server port
)
679 (let* ((port (or port imap-default-port
))
680 (coding-system-for-read imap-coding-system-for-read
)
681 (coding-system-for-write imap-coding-system-for-write
)
682 (process (open-network-stream name buffer server port
)))
684 (while (and (memq (process-status process
) '(open run
))
685 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
686 (goto-char (point-min))
687 (not (imap-parse-greeting)))
688 (accept-process-output process
1)
691 (when (memq (process-status process
) '(open run
))
694 (defun imap-shell-p (_buffer)
697 (defun imap-shell-open (name buffer server port
)
698 (let ((cmds (if (listp imap-shell-program
) imap-shell-program
699 (list imap-shell-program
)))
701 (while (and (not done
) (setq cmd
(pop cmds
)))
702 (message "imap: Opening IMAP connection with `%s'..." cmd
)
703 (setq imap-client-eol
"\n")
704 (let* ((port (or port imap-default-port
))
705 (coding-system-for-read imap-coding-system-for-read
)
706 (coding-system-for-write imap-coding-system-for-write
)
707 (process-connection-type imap-process-connection-type
)
708 (process (start-process
709 name buffer shell-file-name shell-command-switch
715 ?p
(number-to-string port
)
716 ?l imap-default-user
)))))
718 (while (and (memq (process-status process
) '(open run
))
719 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
720 (goto-char (point-max))
722 (not (imap-parse-greeting)))
723 (accept-process-output process
1)
727 (when (memq (process-status process
) '(open run
))
728 (setq done process
)))))
731 (message "imap: Opening IMAP connection with `%s'...done" cmd
)
733 (message "imap: Opening IMAP connection with `%s'...failed" cmd
)
736 (defun imap-starttls-p (buffer)
737 (imap-capability 'STARTTLS buffer
))
739 (defun imap-starttls-open (name buffer server port
)
740 (message "imap: Connecting with STARTTLS...")
741 (let* ((port (or port imap-default-port
))
742 (coding-system-for-read imap-coding-system-for-read
)
743 (coding-system-for-write imap-coding-system-for-write
)
744 (process (open-network-stream
745 name buffer server port
747 :capability-command
"1 CAPABILITY\r\n"
748 :always-query-capabilities t
749 :end-of-command
"\r\n"
752 #'(lambda (capabilities)
753 (when (string-match-p "STARTTLS" capabilities
)
758 (when (memq (process-status process
) '(open run
))
760 (with-current-buffer buffer
761 (goto-char (point-min))
762 (imap-parse-greeting))))
763 (message "imap: Connecting with STARTTLS...%s" (if done
"done" "failed"))
766 ;; Server functions; authenticator stuff:
768 (defun imap-interactive-login (buffer loginfunc
)
769 "Login to server in BUFFER.
770 Return t if login was successful, nil otherwise.
772 LOGINFUNC is passed a username and a password. It should return
773 t if it successfully authenticates, nil otherwise."
774 (with-current-buffer buffer
775 (make-local-variable 'imap-username
)
776 (make-local-variable 'imap-password
)
777 (let (user passwd ret
)
778 ;; (condition-case ()
779 (while (or (not user
) (not passwd
))
780 (setq user
(or imap-username
781 (read-from-minibuffer
783 "imap: username for %s (using stream `%s'): "
784 imap-server imap-stream
)
785 (or user imap-default-user
))))
790 "imap: password for %s@%s (using authenticator `%s'): "
791 user imap-server imap-auth
))))
792 (when (and user passwd
)
793 (if (funcall loginfunc user passwd
)
795 (message "imap: Login successful...")
798 (when (and (not imap-password
)
799 (or imap-store-password
800 (y-or-n-p "imap: Store password for this IMAP session? ")))
801 (setq imap-password passwd
)))
802 (message "imap: Login failed...")
804 (setq imap-password nil
)
806 ;; (quit (with-current-buffer buffer
809 ;; (error (with-current-buffer buffer
814 (defun imap-gssapi-auth-p (_buffer)
815 (eq imap-stream
'gssapi
))
817 (defun imap-gssapi-auth (_buffer)
818 (message "imap: Authenticating using GSSAPI...%s"
819 (if (eq imap-stream
'gssapi
) "done" "failed"))
820 (eq imap-stream
'gssapi
))
822 (defun imap-kerberos4-auth-p (buffer)
823 (and (imap-capability 'AUTH
=KERBEROS_V4 buffer
)
824 (eq imap-stream
'kerberos4
)))
826 (defun imap-kerberos4-auth (_buffer)
827 (message "imap: Authenticating using Kerberos 4...%s"
828 (if (eq imap-stream
'kerberos4
) "done" "failed"))
829 (eq imap-stream
'kerberos4
))
831 (defun imap-cram-md5-p (buffer)
832 (imap-capability 'AUTH
=CRAM-MD5 buffer
))
834 (defun imap-cram-md5-auth (buffer)
835 "Login to server using the AUTH CRAM-MD5 method."
836 (message "imap: Authenticating using CRAM-MD5...")
837 (let ((done (imap-interactive-login
839 (lambda (user passwd
)
841 (imap-send-command-wait
843 "AUTHENTICATE CRAM-MD5"
845 (let* ((decoded (base64-decode-string challenge
))
846 (hash (rfc2104-hash 'md5
64 16 passwd decoded
))
847 (response (concat user
" " hash
))
848 (encoded (base64-encode-string response
)))
851 (message "imap: Authenticating using CRAM-MD5...done")
852 (message "imap: Authenticating using CRAM-MD5...failed"))))
854 (defun imap-login-p (buffer)
855 (and (not (imap-capability 'LOGINDISABLED buffer
))
856 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer
))))
858 (defun imap-quote-specials (string)
861 (goto-char (point-min))
862 (while (re-search-forward "[\\\"]" nil t
)
868 (defun imap-login-auth (buffer)
869 "Login to server using the LOGIN command."
870 (message "imap: Plaintext authentication...")
871 (imap-interactive-login buffer
872 (lambda (user passwd
)
873 (imap-ok-p (imap-send-command-wait
875 (imap-quote-specials user
)
877 (imap-quote-specials passwd
)
880 (defun imap-anonymous-p (_buffer)
883 (defun imap-anonymous-auth (buffer)
884 (message "imap: Logging in anonymously...")
885 (with-current-buffer buffer
886 (imap-ok-p (imap-send-command-wait
887 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
888 (system-name)) "\"")))))
890 ;;; Compiler directives.
892 (defvar imap-sasl-client
)
893 (defvar imap-sasl-step
)
895 (defun imap-sasl-make-mechanisms (buffer)
898 (let ((name (symbol-name sym
)))
899 (if (and (> (length name
) 5)
900 (string-equal "AUTH=" (substring name
0 5 )))
901 (setq mecs
(cons (substring name
5) mecs
)))))
902 (imap-capability nil buffer
))
905 (declare-function sasl-find-mechanism
"sasl" (mechanism))
906 (declare-function sasl-mechanism-name
"sasl" (mechanism))
907 (declare-function sasl-make-client
"sasl" (mechanism name service server
))
908 (declare-function sasl-next-step
"sasl" (client step
))
909 (declare-function sasl-step-data
"sasl" (step))
910 (declare-function sasl-step-set-data
"sasl" (step data
))
912 (defun imap-sasl-auth-p (buffer)
913 (and (condition-case ()
916 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer
))))
918 (defun imap-sasl-auth (buffer)
919 "Login to server using the SASL method."
920 (message "imap: Authenticating using SASL...")
921 (with-current-buffer buffer
922 (make-local-variable 'imap-username
)
923 (make-local-variable 'imap-sasl-client
)
924 (make-local-variable 'imap-sasl-step
)
925 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer
)))
928 (setq user
(or imap-username
929 (read-from-minibuffer
930 (concat "IMAP username for " imap-server
" using SASL "
931 (sasl-mechanism-name mechanism
) ": ")
932 (or user imap-default-user
))))
934 (setq imap-sasl-client
(sasl-make-client mechanism user
"imap2" imap-server
)
935 imap-sasl-step
(sasl-next-step imap-sasl-client nil
))
936 (let ((tag (imap-send-command
937 (if (sasl-step-data imap-sasl-step
)
938 (format "AUTHENTICATE %s %s"
939 (sasl-mechanism-name mechanism
)
940 (sasl-step-data imap-sasl-step
))
941 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism
)))
943 (while (eq (imap-wait-for-tag tag
) 'INCOMPLETE
)
944 (sasl-step-set-data imap-sasl-step
(base64-decode-string imap-continuation
))
945 (setq imap-continuation nil
946 imap-sasl-step
(sasl-next-step imap-sasl-client imap-sasl-step
))
947 (imap-send-command-1 (if (sasl-step-data imap-sasl-step
)
948 (base64-encode-string (sasl-step-data imap-sasl-step
) t
)
950 (if (imap-ok-p (imap-wait-for-tag tag
))
951 (setq imap-username user
953 (message "Login failed...")
957 (defun imap-digest-md5-p (buffer)
958 (and (imap-capability 'AUTH
=DIGEST-MD5 buffer
)
960 (require 'digest-md5
)
963 (defun imap-digest-md5-auth (buffer)
964 "Login to server using the AUTH DIGEST-MD5 method."
965 (message "imap: Authenticating using DIGEST-MD5...")
966 (imap-interactive-login
968 (lambda (user passwd
)
972 "AUTHENTICATE DIGEST-MD5"
974 (digest-md5-parse-digest-challenge
975 (base64-decode-string challenge
))
977 (digest-md5-digest-uri
978 "imap" (digest-md5-challenge 'realm
)))
980 (digest-md5-digest-response
981 user passwd digest-uri
)))
982 (base64-encode-string response
'no-line-break
))))
984 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
986 (setq imap-continuation nil
)
987 (imap-send-command-1 "")
988 (imap-ok-p (imap-wait-for-tag tag
)))))))
992 (defun imap-open-1 (buffer)
993 (with-current-buffer buffer
995 (setq imap-current-mailbox nil
996 imap-current-message nil
998 imap-process
(condition-case ()
999 (funcall (nth 2 (assq imap-stream
1001 "imap" buffer imap-server imap-port
)
1002 ((error quit
) nil
)))
1004 (set-process-filter imap-process
'imap-arrival-filter
)
1005 (set-process-sentinel imap-process
'imap-sentinel
)
1006 (while (and (eq imap-state
'initial
)
1007 (memq (process-status imap-process
) '(open run
)))
1008 (message "Waiting for response from %s..." imap-server
)
1009 (accept-process-output imap-process
1))
1010 (message "Waiting for response from %s...done" imap-server
)
1011 (and (memq (process-status imap-process
) '(open run
))
1014 (defun imap-open (server &optional port stream auth buffer
)
1015 "Open an IMAP connection to host SERVER at PORT returning a buffer.
1016 If PORT is unspecified, a default value is used (143 except
1017 for SSL which use 993).
1018 STREAM indicates the stream to use, see `imap-streams' for available
1019 streams. If nil, it choices the best stream the server is capable of.
1020 AUTH indicates authenticator to use, see `imap-authenticators' for
1021 available authenticators. If nil, it choices the best stream the
1022 server is capable of.
1023 BUFFER can be a buffer or a name of a buffer, which is created if
1024 necessary. If nil, the buffer name is generated."
1025 (setq buffer
(or buffer
(format " *imap* %s:%d" server
(or port
0))))
1026 (with-current-buffer (get-buffer-create buffer
)
1027 (if (imap-opened buffer
)
1028 (imap-close buffer
))
1029 (mapc 'make-local-variable imap-local-variables
)
1030 (imap-disable-multibyte)
1031 (buffer-disable-undo)
1032 (setq imap-server
(or server imap-server
))
1033 (setq imap-port
(or port imap-port
))
1034 (setq imap-auth
(or auth imap-auth
))
1035 (setq imap-stream
(or stream imap-stream
))
1036 (message "imap: Connecting to %s..." imap-server
)
1037 (if (null (let ((imap-stream (or imap-stream imap-default-stream
)))
1038 (imap-open-1 buffer
)))
1040 (message "imap: Connecting to %s...failed" imap-server
)
1042 (when (null imap-stream
)
1043 ;; Need to choose stream.
1044 (let ((streams imap-streams
))
1045 (while (setq stream
(pop streams
))
1046 ;; OK to use this stream?
1047 (when (funcall (nth 1 (assq stream imap-stream-alist
)) buffer
)
1049 (if (not (eq imap-default-stream stream
))
1050 (with-current-buffer (get-buffer-create
1051 (generate-new-buffer-name " *temp*"))
1052 (mapc 'make-local-variable imap-local-variables
)
1053 (imap-disable-multibyte)
1054 (buffer-disable-undo)
1055 (setq imap-server
(or server imap-server
))
1056 (setq imap-port
(or port imap-port
))
1057 (setq imap-auth
(or auth imap-auth
))
1058 (message "imap: Reconnecting with stream `%s'..." stream
)
1059 (if (null (let ((imap-stream stream
))
1060 (imap-open-1 (current-buffer))))
1062 (kill-buffer (current-buffer))
1064 "imap: Reconnecting with stream `%s'...failed"
1066 ;; We're done, kill the first connection
1068 (let ((name (if (stringp buffer
)
1070 (buffer-name buffer
))))
1071 (kill-buffer buffer
)
1072 (rename-buffer name
)
1073 ;; set the passed buffer to the current one,
1074 ;; so that (imap-opened buffer) later will work
1075 (setq buffer
(current-buffer)))
1076 (message "imap: Reconnecting with stream `%s'...done"
1078 (setq imap-stream stream
)
1079 (setq imap-capability nil
)
1080 (setq streams nil
)))
1082 (message "imap: Connecting to %s...done" imap-server
)
1083 (setq imap-stream stream
)
1084 (setq imap-capability nil
)
1085 (setq streams nil
))))))
1086 (when (imap-opened buffer
)
1087 (setq imap-mailbox-data
(make-vector imap-mailbox-prime
0)))
1088 ;; (debug "opened+state+auth+buffer" (imap-opened buffer) imap-state imap-auth buffer)
1092 (defcustom imap-ping-server t
1093 "If non-nil, check if IMAP is open.
1094 See the function `imap-ping-server'."
1095 :version
"23.1" ;; No Gnus
1099 (defun imap-opened (&optional buffer
)
1100 "Return non-nil if connection to imap server in BUFFER is open.
1101 If BUFFER is nil then the current buffer is used."
1102 (and (setq buffer
(get-buffer (or buffer
(current-buffer))))
1103 (buffer-live-p buffer
)
1104 (with-current-buffer buffer
1106 (memq (process-status imap-process
) '(open run
))
1107 (if imap-ping-server
1111 (defun imap-ping-server (&optional buffer
)
1112 "Ping the IMAP server in BUFFER with a \"NOOP\" command.
1113 Return non-nil if the server responds, and nil if it does not
1114 respond. If BUFFER is nil, the current buffer is used."
1116 (imap-ok-p (imap-send-command-wait "NOOP" buffer
))
1119 (defun imap-authenticate (&optional user passwd buffer
)
1120 "Authenticate to server in BUFFER, using current buffer if nil.
1121 It uses the authenticator specified when opening the server.
1123 Optional arguments USER and PASSWD specify the username and
1124 password to use if the authenticator requires a username and/or
1125 password. If omitted or nil, the authenticator may query the
1126 user for a username and/or password."
1127 (with-current-buffer (or buffer
(current-buffer))
1128 (if (not (eq imap-state
'nonauth
))
1129 (or (eq imap-state
'auth
)
1130 (eq imap-state
'selected
)
1131 (eq imap-state
'examine
))
1132 (make-local-variable 'imap-username
)
1133 (make-local-variable 'imap-password
)
1134 (make-local-variable 'imap-last-authenticator
)
1135 (when user
(setq imap-username user
))
1136 (when passwd
(setq imap-password passwd
))
1138 (and (setq imap-last-authenticator
1139 (assq imap-auth imap-authenticator-alist
))
1140 (funcall (nth 2 imap-last-authenticator
) (current-buffer))
1141 (setq imap-state
'auth
))
1142 ;; Choose authenticator.
1143 (let ((auths imap-authenticators
)
1145 (while (setq auth
(pop auths
))
1146 ;; OK to use authenticator?
1147 (setq imap-last-authenticator
1148 (assq auth imap-authenticator-alist
))
1149 (when (funcall (nth 1 imap-last-authenticator
) (current-buffer))
1150 (message "imap: Authenticating to `%s' using `%s'..."
1152 (setq imap-auth auth
)
1153 (if (funcall (nth 2 imap-last-authenticator
) (current-buffer))
1155 (message "imap: Authenticating to `%s' using `%s'...done"
1157 ;; set imap-state correctly on successful auth attempt
1158 (setq imap-state
'auth
)
1159 ;; stop iterating through the authenticator list
1161 (message "imap: Authenticating to `%s' using `%s'...failed"
1162 imap-server auth
)))))
1165 (defun imap-close (&optional buffer
)
1166 "Close connection to server in BUFFER.
1167 If BUFFER is nil, the current buffer is used."
1168 (with-current-buffer (or buffer
(current-buffer))
1173 (when (and imap-process
1174 (memq (process-status imap-process
) '(open run
)))
1175 (delete-process imap-process
))
1176 (setq imap-current-mailbox nil
1177 imap-current-message nil
1182 (defun imap-capability (&optional identifier buffer
)
1183 "Return a list of identifiers which server in BUFFER support.
1184 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1185 If BUFFER is nil, the current buffer is assumed."
1186 (with-current-buffer (or buffer
(current-buffer))
1187 (unless imap-capability
1188 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1189 (setq imap-capability
'(IMAP2))))
1191 (memq (intern (upcase (symbol-name identifier
))) imap-capability
)
1194 (defun imap-id (&optional list-of-values buffer
)
1195 "Identify client to server in BUFFER, and return server identity.
1196 LIST-OF-VALUES is nil, or a plist with identifier and value
1197 strings to send to the server to identify the client.
1199 Return a list of identifiers which server in BUFFER support, or
1200 nil if it doesn't support ID or returns no information.
1202 If BUFFER is nil, the current buffer is assumed."
1203 (with-current-buffer (or buffer
(current-buffer))
1204 (when (and (imap-capability 'ID
)
1205 (imap-ok-p (imap-send-command-wait
1206 (if (null list-of-values
)
1208 (concat "ID (" (mapconcat (lambda (el)
1209 (concat "\"" el
"\""))
1214 (defun imap-namespace (&optional buffer
)
1215 "Return a namespace hierarchy at server in BUFFER.
1216 If BUFFER is nil, the current buffer is assumed."
1217 (with-current-buffer (or buffer
(current-buffer))
1218 (unless imap-namespace
1219 (when (imap-capability 'NAMESPACE
)
1220 (imap-send-command-wait "NAMESPACE")))
1223 (defun imap-send-command-wait (command &optional buffer
)
1224 (imap-wait-for-tag (imap-send-command command buffer
) buffer
))
1226 (defun imap-logout (&optional buffer
)
1227 (or buffer
(setq buffer
(current-buffer)))
1228 (if imap-logout-timeout
1229 (with-timeout (imap-logout-timeout
1231 (with-current-buffer buffer
1232 (delete-process imap-process
))
1234 (imap-send-command "LOGOUT" buffer
))
1235 (imap-send-command "LOGOUT" buffer
)))
1237 (defun imap-logout-wait (&optional buffer
)
1238 (or buffer
(setq buffer
(current-buffer)))
1239 (if imap-logout-timeout
1240 (with-timeout (imap-logout-timeout
1242 (with-current-buffer buffer
1243 (delete-process imap-process
))
1245 (imap-send-command-wait "LOGOUT" buffer
))
1246 (imap-send-command-wait "LOGOUT" buffer
)))
1249 ;; Mailbox functions:
1251 (defun imap-mailbox-put (propname value
&optional mailbox buffer
)
1252 (with-current-buffer (or buffer
(current-buffer))
1253 (if imap-mailbox-data
1254 (put (intern (or mailbox imap-current-mailbox
) imap-mailbox-data
)
1256 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1257 propname value mailbox
(current-buffer)))
1260 (defsubst imap-mailbox-get-1
(propname &optional mailbox
)
1261 (get (intern-soft (or mailbox imap-current-mailbox
) imap-mailbox-data
)
1264 (defun imap-mailbox-get (propname &optional mailbox buffer
)
1265 (let ((mailbox (imap-utf7-encode mailbox
)))
1266 (with-current-buffer (or buffer
(current-buffer))
1267 (imap-mailbox-get-1 propname
(or mailbox imap-current-mailbox
)))))
1269 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer
)
1270 (with-current-buffer (or buffer
(current-buffer))
1274 (push (funcall func
(if mailbox-decoder
1275 (funcall mailbox-decoder
(symbol-name s
))
1276 (symbol-name s
))) result
))
1280 (defun imap-mailbox-map (func &optional buffer
)
1281 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1282 Function should take a mailbox name (a string) as
1284 (imap-mailbox-map-1 func
'imap-utf7-decode buffer
))
1286 (defun imap-current-mailbox (&optional buffer
)
1287 (with-current-buffer (or buffer
(current-buffer))
1288 (imap-utf7-decode imap-current-mailbox
)))
1290 (defun imap-current-mailbox-p-1 (mailbox &optional examine
)
1291 (and (string= mailbox imap-current-mailbox
)
1293 (eq imap-state
'examine
))
1295 (eq imap-state
'selected
)))))
1297 (defun imap-current-mailbox-p (mailbox &optional examine buffer
)
1298 (with-current-buffer (or buffer
(current-buffer))
1299 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox
) examine
)))
1301 (defun imap-mailbox-select-1 (mailbox &optional examine
)
1302 "Select MAILBOX on server in BUFFER.
1303 If EXAMINE is non-nil, do a read-only select."
1304 (if (imap-current-mailbox-p-1 mailbox examine
)
1305 imap-current-mailbox
1306 (setq imap-current-mailbox mailbox
)
1307 (if (imap-ok-p (imap-send-command-wait
1308 (concat (if examine
"EXAMINE" "SELECT") " \""
1311 (setq imap-message-data
(make-vector imap-message-prime
0)
1312 imap-state
(if examine
'examine
'selected
))
1313 imap-current-mailbox
)
1314 ;; Failed SELECT/EXAMINE unselects current mailbox
1315 (setq imap-current-mailbox nil
))))
1317 (defun imap-mailbox-select (mailbox &optional examine buffer
)
1318 (with-current-buffer (or buffer
(current-buffer))
1320 (imap-mailbox-select-1 (imap-utf7-encode mailbox
) examine
))))
1322 (defun imap-mailbox-examine-1 (mailbox &optional buffer
)
1323 (with-current-buffer (or buffer
(current-buffer))
1324 (imap-mailbox-select-1 mailbox
'examine
)))
1326 (defun imap-mailbox-examine (mailbox &optional buffer
)
1327 "Examine MAILBOX on server in BUFFER."
1328 (imap-mailbox-select mailbox
'examine buffer
))
1330 (defun imap-mailbox-unselect (&optional buffer
)
1331 "Close current folder in BUFFER, without expunging articles."
1332 (with-current-buffer (or buffer
(current-buffer))
1333 (when (or (eq imap-state
'auth
)
1334 (and (imap-capability 'UNSELECT
)
1335 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1337 (imap-send-command-wait (concat "EXAMINE \""
1338 imap-current-mailbox
1340 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1341 (setq imap-current-mailbox nil
1342 imap-message-data nil
1346 (defun imap-mailbox-expunge (&optional asynch buffer
)
1347 "Expunge articles in current folder in BUFFER.
1348 If ASYNCH, do not wait for successful completion of the command.
1349 If BUFFER is nil the current buffer is assumed."
1350 (with-current-buffer (or buffer
(current-buffer))
1351 (when (and imap-current-mailbox
(not (eq imap-state
'examine
)))
1353 (imap-send-command "EXPUNGE")
1354 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1356 (defun imap-mailbox-close (&optional asynch buffer
)
1357 "Expunge articles and close current folder in BUFFER.
1358 If ASYNCH, do not wait for successful completion of the command.
1359 If BUFFER is nil the current buffer is assumed."
1360 (with-current-buffer (or buffer
(current-buffer))
1361 (when imap-current-mailbox
1363 (imap-add-callback (imap-send-command "CLOSE")
1364 `(lambda (tag status
)
1365 (message "IMAP mailbox `%s' closed... %s"
1366 imap-current-mailbox status
)
1367 (when (eq ,imap-current-mailbox
1368 imap-current-mailbox
)
1369 ;; Don't wipe out data if another mailbox
1371 (setq imap-current-mailbox nil
1372 imap-message-data nil
1373 imap-state
'auth
))))
1374 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1375 (setq imap-current-mailbox nil
1376 imap-message-data nil
1380 (defun imap-mailbox-create-1 (mailbox)
1381 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox
"\""))))
1383 (defun imap-mailbox-create (mailbox &optional buffer
)
1384 "Create MAILBOX on server in BUFFER.
1385 If BUFFER is nil the current buffer is assumed."
1386 (with-current-buffer (or buffer
(current-buffer))
1387 (imap-mailbox-create-1 (imap-utf7-encode mailbox
))))
1389 (defun imap-mailbox-delete (mailbox &optional buffer
)
1390 "Delete MAILBOX on server in BUFFER.
1391 If BUFFER is nil the current buffer is assumed."
1392 (let ((mailbox (imap-utf7-encode mailbox
)))
1393 (with-current-buffer (or buffer
(current-buffer))
1395 (imap-send-command-wait (list "DELETE \"" mailbox
"\""))))))
1397 (defun imap-mailbox-rename (oldname newname
&optional buffer
)
1398 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1399 If BUFFER is nil the current buffer is assumed."
1400 (let ((oldname (imap-utf7-encode oldname
))
1401 (newname (imap-utf7-encode newname
)))
1402 (with-current-buffer (or buffer
(current-buffer))
1404 (imap-send-command-wait (list "RENAME \"" oldname
"\" "
1405 "\"" newname
"\""))))))
1407 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer
)
1408 "Return a list of subscribed mailboxes on server in BUFFER.
1409 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1410 non-nil, a hierarchy delimiter is added to root. REFERENCE is an
1411 implementation-specific string that has to be passed to lsub command."
1412 (with-current-buffer (or buffer
(current-buffer))
1413 ;; Make sure we know the hierarchy separator for root's hierarchy
1414 (when (and add-delimiter
(null (imap-mailbox-get-1 'delimiter root
)))
1415 (imap-send-command-wait (concat "LIST \"" reference
"\" \""
1416 (imap-utf7-encode root
) "\"")))
1417 ;; clear list data (NB not delimiter and other stuff)
1418 (imap-mailbox-map-1 (lambda (mailbox)
1419 (imap-mailbox-put 'lsub nil mailbox
)))
1421 (imap-send-command-wait
1422 (concat "LSUB \"" reference
"\" \"" (imap-utf7-encode root
)
1423 (and add-delimiter
(imap-mailbox-get-1 'delimiter root
))
1426 (imap-mailbox-map-1 (lambda (mailbox)
1427 (when (imap-mailbox-get-1 'lsub mailbox
)
1428 (push (imap-utf7-decode mailbox
) out
))))
1431 (defun imap-mailbox-list (root &optional reference add-delimiter buffer
)
1432 "Return a list of mailboxes matching ROOT on server in BUFFER.
1433 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1434 root. REFERENCE is an implementation-specific string that has to be
1435 passed to list command."
1436 (with-current-buffer (or buffer
(current-buffer))
1437 ;; Make sure we know the hierarchy separator for root's hierarchy
1438 (when (and add-delimiter
(null (imap-mailbox-get-1 'delimiter root
)))
1439 (imap-send-command-wait (concat "LIST \"" reference
"\" \""
1440 (imap-utf7-encode root
) "\"")))
1441 ;; clear list data (NB not delimiter and other stuff)
1442 (imap-mailbox-map-1 (lambda (mailbox)
1443 (imap-mailbox-put 'list nil mailbox
)))
1445 (imap-send-command-wait
1446 (concat "LIST \"" reference
"\" \"" (imap-utf7-encode root
)
1447 (and add-delimiter
(imap-mailbox-get-1 'delimiter root
))
1450 (imap-mailbox-map-1 (lambda (mailbox)
1451 (when (imap-mailbox-get-1 'list mailbox
)
1452 (push (imap-utf7-decode mailbox
) out
))))
1455 (defun imap-mailbox-subscribe (mailbox &optional buffer
)
1456 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1457 Returns non-nil if successful."
1458 (with-current-buffer (or buffer
(current-buffer))
1459 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1460 (imap-utf7-encode mailbox
)
1463 (defun imap-mailbox-unsubscribe (mailbox &optional buffer
)
1464 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1465 Returns non-nil if successful."
1466 (with-current-buffer (or buffer
(current-buffer))
1467 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1468 (imap-utf7-encode mailbox
)
1471 (defun imap-mailbox-status (mailbox items
&optional buffer
)
1472 "Get status items ITEM in MAILBOX from server in BUFFER.
1473 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1474 the STATUS data items -- i.e. `messages', `recent', `uidnext', `uidvalidity',
1475 or `unseen'. If ITEMS is a list of symbols, a list of values is
1476 returned, if ITEMS is a symbol only its value is returned."
1477 (with-current-buffer (or buffer
(current-buffer))
1479 (imap-send-command-wait (list "STATUS \""
1480 (imap-utf7-encode mailbox
)
1488 (mapcar (lambda (item)
1489 (imap-mailbox-get item mailbox
))
1491 (imap-mailbox-get items mailbox
)))))
1493 (defun imap-mailbox-status-asynch (mailbox items
&optional buffer
)
1494 "Send status item requests ITEMS on MAILBOX to server in BUFFER.
1495 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1496 the STATUS data items -- i.e., `messages', `recent', `uidnext', `uidvalidity'
1497 or `unseen'. The IMAP command tag is returned."
1498 (with-current-buffer (or buffer
(current-buffer))
1499 (imap-send-command (list "STATUS \""
1500 (imap-utf7-encode mailbox
)
1508 (defun imap-mailbox-acl-get (&optional mailbox buffer
)
1509 "Get ACL on MAILBOX from server in BUFFER."
1510 (let ((mailbox (imap-utf7-encode mailbox
)))
1511 (with-current-buffer (or buffer
(current-buffer))
1513 (imap-send-command-wait (list "GETACL \""
1514 (or mailbox imap-current-mailbox
)
1516 (imap-mailbox-get-1 'acl
(or mailbox imap-current-mailbox
))))))
1518 (defun imap-mailbox-acl-set (identifier rights
&optional mailbox buffer
)
1519 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1520 (let ((mailbox (imap-utf7-encode mailbox
)))
1521 (with-current-buffer (or buffer
(current-buffer))
1523 (imap-send-command-wait (list "SETACL \""
1524 (or mailbox imap-current-mailbox
)
1530 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer
)
1531 "Remove <id,rights> pairs for IDENTIFIER from MAILBOX on server in BUFFER."
1532 (let ((mailbox (imap-utf7-encode mailbox
)))
1533 (with-current-buffer (or buffer
(current-buffer))
1535 (imap-send-command-wait (list "DELETEACL \""
1536 (or mailbox imap-current-mailbox
)
1541 ;; Message functions:
1543 (defun imap-current-message (&optional buffer
)
1544 (with-current-buffer (or buffer
(current-buffer))
1545 imap-current-message
))
1547 (defun imap-list-to-message-set (list)
1548 (mapconcat (lambda (item)
1549 (number-to-string item
))
1555 (defun imap-range-to-message-set (range)
1560 (car item
) (cdr item
))
1561 (format "%d" item
)))
1562 (if (and (listp range
) (not (listp (cdr range
))))
1563 (list range
) ;; make (1 . 2) into ((1 . 2))
1567 (defun imap-fetch-asynch (uids props
&optional nouidfetch buffer
)
1568 (with-current-buffer (or buffer
(current-buffer))
1569 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch
"" "UID ")
1571 (imap-list-to-message-set uids
)
1575 (defun imap-fetch (uids props
&optional receive nouidfetch buffer
)
1576 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1577 UIDS can be a string, number or a list of numbers. If RECEIVE is
1578 non-nil, return these properties."
1579 (with-current-buffer (or buffer
(current-buffer))
1580 (when (imap-ok-p (imap-send-command-wait
1581 (format "%sFETCH %s %s" (if nouidfetch
"" "UID ")
1583 (imap-list-to-message-set uids
)
1586 (if (or (null receive
) (stringp uids
))
1589 (mapcar (lambda (uid)
1591 (mapcar (lambda (prop)
1592 (imap-message-get uid prop
))
1594 (imap-message-get uid receive
)))
1596 (imap-message-get uids receive
))))))
1598 (defun imap-message-put (uid propname value
&optional buffer
)
1599 (with-current-buffer (or buffer
(current-buffer))
1600 (if imap-message-data
1601 (put (intern (number-to-string uid
) imap-message-data
)
1603 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1604 uid propname value
(current-buffer)))
1607 (defun imap-message-get (uid propname
&optional buffer
)
1608 (with-current-buffer (or buffer
(current-buffer))
1609 (get (intern-soft (number-to-string uid
) imap-message-data
)
1612 (defun imap-message-map (func propname
&optional buffer
)
1613 "Map a function across each message in `imap-message-data', returning a list."
1614 (with-current-buffer (or buffer
(current-buffer))
1618 (push (funcall func
(get s
'UID
) (get s propname
)) result
))
1622 (defmacro imap-message-envelope-date
(uid &optional buffer
)
1623 `(with-current-buffer (or ,buffer
(current-buffer))
1624 (elt (imap-message-get ,uid
'ENVELOPE
) 0)))
1626 (defmacro imap-message-envelope-subject
(uid &optional buffer
)
1627 `(with-current-buffer (or ,buffer
(current-buffer))
1628 (elt (imap-message-get ,uid
'ENVELOPE
) 1)))
1630 (defmacro imap-message-envelope-from
(uid &optional buffer
)
1631 `(with-current-buffer (or ,buffer
(current-buffer))
1632 (elt (imap-message-get ,uid
'ENVELOPE
) 2)))
1634 (defmacro imap-message-envelope-sender
(uid &optional buffer
)
1635 `(with-current-buffer (or ,buffer
(current-buffer))
1636 (elt (imap-message-get ,uid
'ENVELOPE
) 3)))
1638 (defmacro imap-message-envelope-reply-to
(uid &optional buffer
)
1639 `(with-current-buffer (or ,buffer
(current-buffer))
1640 (elt (imap-message-get ,uid
'ENVELOPE
) 4)))
1642 (defmacro imap-message-envelope-to
(uid &optional buffer
)
1643 `(with-current-buffer (or ,buffer
(current-buffer))
1644 (elt (imap-message-get ,uid
'ENVELOPE
) 5)))
1646 (defmacro imap-message-envelope-cc
(uid &optional buffer
)
1647 `(with-current-buffer (or ,buffer
(current-buffer))
1648 (elt (imap-message-get ,uid
'ENVELOPE
) 6)))
1650 (defmacro imap-message-envelope-bcc
(uid &optional buffer
)
1651 `(with-current-buffer (or ,buffer
(current-buffer))
1652 (elt (imap-message-get ,uid
'ENVELOPE
) 7)))
1654 (defmacro imap-message-envelope-in-reply-to
(uid &optional buffer
)
1655 `(with-current-buffer (or ,buffer
(current-buffer))
1656 (elt (imap-message-get ,uid
'ENVELOPE
) 8)))
1658 (defmacro imap-message-envelope-message-id
(uid &optional buffer
)
1659 `(with-current-buffer (or ,buffer
(current-buffer))
1660 (elt (imap-message-get ,uid
'ENVELOPE
) 9)))
1662 (defmacro imap-message-body
(uid &optional buffer
)
1663 `(with-current-buffer (or ,buffer
(current-buffer))
1664 (imap-message-get ,uid
'BODY
)))
1666 ;; FIXME: Should this try to use CHARSET? -- fx
1667 (defun imap-search (predicate &optional buffer
)
1668 (with-current-buffer (or buffer
(current-buffer))
1669 (imap-mailbox-put 'search
'dummy
)
1670 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate
)))
1671 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox
) 'dummy
)
1673 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1675 (imap-mailbox-get-1 'search imap-current-mailbox
)))))
1677 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer
)
1678 "Return t if FLAG can be permanently saved on articles.
1679 MAILBOX specifies a mailbox on the server in BUFFER."
1680 (with-current-buffer (or buffer
(current-buffer))
1681 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox
))
1682 (member flag
(imap-mailbox-get 'permanentflags mailbox
)))))
1684 (defun imap-message-flags-set (articles flags
&optional silent buffer
)
1685 (when (and articles flags
)
1686 (with-current-buffer (or buffer
(current-buffer))
1687 (imap-ok-p (imap-send-command-wait
1688 (concat "UID STORE " articles
1689 " FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1691 (defun imap-message-flags-del (articles flags
&optional silent buffer
)
1692 (when (and articles flags
)
1693 (with-current-buffer (or buffer
(current-buffer))
1694 (imap-ok-p (imap-send-command-wait
1695 (concat "UID STORE " articles
1696 " -FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1698 (defun imap-message-flags-add (articles flags
&optional silent buffer
)
1699 (when (and articles flags
)
1700 (with-current-buffer (or buffer
(current-buffer))
1701 (imap-ok-p (imap-send-command-wait
1702 (concat "UID STORE " articles
1703 " +FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1705 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/65317/focus=65343
1706 ;; Signal an error if we'd get an integer overflow.
1708 ;; FIXME: Identify relevant calls to `string-to-number' and replace them with
1709 ;; `imap-string-to-integer'.
1710 (defun imap-string-to-integer (string &optional base
)
1711 (let ((number (string-to-number string base
)))
1712 (if (> number most-positive-fixnum
)
1714 (format "String %s cannot be converted to a Lisp integer" number
))
1717 (defun imap-fetch-safe (uids props
&optional receive nouidfetch buffer
)
1718 "Like `imap-fetch', but DTRT with Exchange 2007 bug.
1719 However, UIDS here is a cons, where the car is the canonical form
1720 of the UIDS specification, and the cdr is the one which works with
1721 Exchange 2007 or, potentially, other buggy servers.
1722 See `imap-enable-exchange-bug-workaround'."
1723 ;; The first time we get here for a given, we'll try the canonical
1724 ;; form. If we get the known error from the buggy server, set the
1725 ;; flag buffer-locally (to account for connections to multiple
1726 ;; servers), then re-try with the alternative UIDS spec. We don't
1727 ;; unconditionally use the alternative form, since the
1728 ;; currently-used alternatives are seriously inefficient with some
1729 ;; servers (although they are valid).
1731 ;; FIXME: Maybe it would be cleaner to have a flag to not signal
1732 ;; the error (which otherwise gives a message), and test
1733 ;; `imap-failed-tags'. Also, Other IMAP clients use other forms of
1734 ;; request which work with Exchange, e.g. Claws does "UID FETCH 1:*
1735 ;; (UID)" rather than "FETCH UID 1,*". Is there a good reason not
1737 (condition-case data
1738 ;; Binding `debug-on-error' allows us to get the error from
1739 ;; `imap-parse-response' -- it's normally caught by Emacs around
1740 ;; execution of a process filter.
1741 (let ((debug-on-error t
))
1742 (imap-fetch (if imap-enable-exchange-bug-workaround
1745 props receive nouidfetch buffer
))
1747 (if (and (not imap-enable-exchange-bug-workaround
)
1748 ;; This is the Exchange 2007 response. It may be more
1749 ;; robust just to check for a BAD response to the
1751 (string-match "The specified message set is invalid"
1753 (with-current-buffer (or buffer
(current-buffer))
1754 (set (make-local-variable 'imap-enable-exchange-bug-workaround
)
1756 (imap-fetch (cdr uids
) props receive nouidfetch
))
1757 (signal (car data
) (cdr data
))))))
1759 (defun imap-message-copyuid-1 (mailbox)
1760 (if (imap-capability 'UIDPLUS
)
1761 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox
))
1762 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox
))))
1763 (let ((old-mailbox imap-current-mailbox
)
1765 (imap-message-data (make-vector 2 0)))
1766 (when (imap-mailbox-examine-1 mailbox
)
1768 (and (imap-fetch-safe '("*" .
"*:*") "UID")
1769 (list (imap-mailbox-get-1 'uidvalidity mailbox
)
1770 (apply 'max
(imap-message-map
1771 (lambda (uid _prop
) uid
) 'UID
))))
1773 (imap-mailbox-select old-mailbox
(eq state
'examine
))
1774 (imap-mailbox-unselect)))))))
1776 (defun imap-message-copyuid (mailbox &optional buffer
)
1777 (with-current-buffer (or buffer
(current-buffer))
1778 (imap-message-copyuid-1 (imap-utf7-decode mailbox
))))
1780 (defun imap-message-copy (articles mailbox
1781 &optional dont-create no-copyuid buffer
)
1782 "Copy ARTICLES to MAILBOX on server in BUFFER.
1783 ARTICLES is a string message set. Create mailbox if it doesn't exist,
1784 unless DONT-CREATE is non-nil. On success, return a list with
1785 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1786 first element. The rest of list contains the saved articles' UIDs."
1788 (with-current-buffer (or buffer
(current-buffer))
1789 (let ((mailbox (imap-utf7-encode mailbox
)))
1790 (if (let ((cmd (concat "UID COPY " articles
" \"" mailbox
"\""))
1791 (imap-current-target-mailbox mailbox
))
1792 (if (imap-ok-p (imap-send-command-wait cmd
))
1794 (when (and (not dont-create
)
1795 ;; removed because of buggy Oracle server
1796 ;; that doesn't send TRYCREATE tags (which
1797 ;; is a MUST according to specifications):
1798 ;;(imap-mailbox-get-1 'trycreate mailbox)
1799 (imap-mailbox-create-1 mailbox
))
1800 (imap-ok-p (imap-send-command-wait cmd
)))))
1802 (imap-message-copyuid-1 mailbox
)))))))
1804 ;; FIXME: Amalgamate with imap-message-copyuid-1, using an extra arg, since it
1805 ;; shares most of the code? -- fx
1806 (defun imap-message-appenduid-1 (mailbox)
1807 (if (imap-capability 'UIDPLUS
)
1808 (imap-mailbox-get-1 'appenduid mailbox
)
1809 (let ((old-mailbox imap-current-mailbox
)
1811 (imap-message-data (make-vector 2 0)))
1812 (when (imap-mailbox-examine-1 mailbox
)
1814 (and (imap-fetch-safe '("*" .
"*:*") "UID")
1815 (list (imap-mailbox-get-1 'uidvalidity mailbox
)
1816 (apply 'max
(imap-message-map
1817 (lambda (uid _prop
) uid
) 'UID
))))
1819 (imap-mailbox-select old-mailbox
(eq state
'examine
))
1820 (imap-mailbox-unselect)))))))
1822 (defun imap-message-appenduid (mailbox &optional buffer
)
1823 (with-current-buffer (or buffer
(current-buffer))
1824 (imap-message-appenduid-1 (imap-utf7-encode mailbox
))))
1826 (defun imap-message-append (mailbox article
&optional _flags _date-time buffer
)
1827 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1828 FLAGS and DATE-TIME is currently not used. Return a cons holding
1829 uidvalidity of MAILBOX and UID the newly created article got, or nil
1831 (let ((mailbox (imap-utf7-encode mailbox
)))
1832 (with-current-buffer (or buffer
(current-buffer))
1833 (and (let ((imap-current-target-mailbox mailbox
))
1835 (imap-send-command-wait
1836 (list "APPEND \"" mailbox
"\" " article
))))
1837 (imap-message-appenduid-1 mailbox
)))))
1839 (defun imap-body-lines (body)
1840 "Return number of lines in article by looking at the mime bodystructure BODY."
1842 (if (stringp (car body
))
1843 (cond ((and (string= (upcase (car body
)) "TEXT")
1844 (numberp (nth 7 body
)))
1846 ((and (string= (upcase (car body
)) "MESSAGE")
1847 (numberp (nth 9 body
)))
1850 (apply '+ (mapcar 'imap-body-lines body
)))
1853 (defun imap-envelope-from (from)
1854 "Return a FROM string line."
1856 (concat (aref from
0)
1857 (if (aref from
0) " <")
1861 (if (aref from
0) ">"))))
1864 ;; Internal functions.
1866 (defun imap-add-callback (tag func
)
1867 (setq imap-callbacks
(append (list (cons tag func
)) imap-callbacks
)))
1869 (defun imap-send-command-1 (cmdstr)
1870 (setq cmdstr
(concat cmdstr imap-client-eol
))
1872 (process-send-string imap-process cmdstr
))
1874 (defun imap-send-command (command &optional buffer
)
1875 (with-current-buffer (or buffer
(current-buffer))
1876 (if (not (listp command
)) (setq command
(list command
)))
1877 (let ((tag (setq imap-tag
(1+ imap-tag
)))
1879 (setq cmdstr
(concat (number-to-string imap-tag
) " "))
1880 (while (setq cmd
(pop command
))
1881 (cond ((stringp cmd
)
1882 (setq cmdstr
(concat cmdstr cmd
)))
1884 (let ((eol imap-client-eol
)
1885 (calcfirst imap-calculate-literal-size-first
)
1887 (with-current-buffer cmd
1889 (setq size
(buffer-size)))
1890 (when (not (equal eol
"\r\n"))
1891 ;; XXX modifies buffer!
1892 (goto-char (point-min))
1893 (while (search-forward "\r\n" nil t
)
1894 (replace-match eol
)))
1896 (setq size
(buffer-size))))
1898 (concat cmdstr
(format "{%d}" size
))))
1901 (imap-send-command-1 cmdstr
)
1903 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
1904 (setq command nil
) ;; abort command if no cont-req
1905 (let ((process imap-process
)
1906 (stream imap-stream
)
1907 (eol imap-client-eol
))
1908 (with-current-buffer cmd
1910 (process-send-region process
(point-min)
1912 (process-send-string process imap-client-eol
))))
1913 (setq imap-continuation nil
)))
1915 (imap-send-command-1 cmdstr
)
1919 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
1920 nil
;; abort command if no cont-req
1921 (cons (funcall cmd imap-continuation
)
1923 (setq imap-continuation nil
)))
1925 (error "Unknown command type"))))
1927 (imap-send-command-1 cmdstr
))
1930 (defun imap-wait-for-tag (tag &optional buffer
)
1931 (with-current-buffer (or buffer
(current-buffer))
1932 (let (imap-have-messaged)
1933 (while (and (null imap-continuation
)
1934 (memq (process-status imap-process
) '(open run
))
1935 (< imap-reached-tag tag
))
1936 (let ((len (/ (buffer-size) 1024))
1939 (setq imap-have-messaged t
)
1940 (message "imap read: %dk" len
))
1941 (accept-process-output imap-process
1942 (truncate imap-read-timeout
)
1943 (truncate (* (- imap-read-timeout
1944 (truncate imap-read-timeout
))
1946 ;; A process can die _before_ we have processed everything it
1947 ;; has to say. Moreover, this can happen in between the call to
1948 ;; accept-process-output and the call to process-status in an
1949 ;; iteration of the loop above.
1950 (when (and (null imap-continuation
)
1951 (< imap-reached-tag tag
))
1952 (accept-process-output imap-process
0 0))
1953 (when imap-have-messaged
1955 (and (memq (process-status imap-process
) '(open run
))
1956 (or (assq tag imap-failed-tags
)
1957 (if imap-continuation
1961 (defun imap-sentinel (process string
)
1962 (delete-process process
))
1964 (defun imap-find-next-line ()
1965 "Return point at end of current line, taking into account literals.
1966 Return nil if no complete line has arrived."
1967 (when (re-search-forward (concat imap-server-eol
"\\|{\\([0-9]+\\)}"
1970 (if (match-string 1)
1971 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1973 (goto-char (+ (point) (string-to-number (match-string 1))))
1974 (imap-find-next-line))
1977 (defun imap-arrival-filter (proc string
)
1978 "IMAP process filter."
1979 ;; Sometimes, we are called even though the process has died.
1980 ;; Better abstain from doing stuff in that case.
1981 (when (buffer-name (process-buffer proc
))
1982 (with-current-buffer (process-buffer proc
)
1983 (goto-char (point-max))
1987 (goto-char (point-min))
1988 (while (setq end
(imap-find-next-line))
1990 (narrow-to-region (point-min) end
)
1991 (delete-char (- (length imap-server-eol
)))
1992 (goto-char (point-min))
1994 (cond ((eq imap-state
'initial
)
1995 (imap-parse-greeting))
1996 ((or (eq imap-state
'auth
)
1997 (eq imap-state
'nonauth
)
1998 (eq imap-state
'selected
)
1999 (eq imap-state
'examine
))
2000 (imap-parse-response))
2002 (message "Unknown state %s in arrival filter"
2004 (delete-region (point-min) (point-max)))))))))
2009 (defsubst imap-forward
()
2010 (or (eobp) (forward-char)))
2013 ;; ; Unsigned 32-bit integer
2014 ;; ; (0 <= n < 4,294,967,296)
2016 (defsubst imap-parse-number
()
2017 (when (looking-at "[0-9]+")
2019 (string-to-number (match-string 0))
2020 (goto-char (match-end 0)))))
2022 ;; literal = "{" number "}" CRLF *CHAR8
2023 ;; ; Number represents the number of CHAR8s
2025 (defsubst imap-parse-literal
()
2026 (when (looking-at "{\\([0-9]+\\)}\r\n")
2027 (let ((pos (match-end 0))
2028 (len (string-to-number (match-string 1))))
2029 (if (< (point-max) (+ pos len
))
2031 (goto-char (+ pos len
))
2032 (buffer-substring pos
(+ pos len
))))))
2034 ;; string = quoted / literal
2036 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
2038 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2039 ;; "\" quoted-specials
2041 ;; quoted-specials = DQUOTE / "\"
2043 ;; TEXT-CHAR = <any CHAR except CR and LF>
2045 (defsubst imap-parse-string
()
2046 (cond ((eq (char-after) ?
\")
2048 (let ((p (point)) (name ""))
2049 (skip-chars-forward "^\"\\\\")
2050 (setq name
(buffer-substring p
(point)))
2051 (while (eq (char-after) ?
\\)
2052 (setq p
(1+ (point)))
2054 (skip-chars-forward "^\"\\\\")
2055 (setq name
(concat name
(buffer-substring p
(point)))))
2058 ((eq (char-after) ?
{)
2059 (imap-parse-literal))))
2063 (defsubst imap-parse-nil
()
2064 (if (looking-at "NIL")
2065 (goto-char (match-end 0))))
2067 ;; nstring = string / nil
2069 (defsubst imap-parse-nstring
()
2070 (or (imap-parse-string)
2071 (and (imap-parse-nil)
2074 ;; astring = atom / string
2076 ;; atom = 1*ATOM-CHAR
2078 ;; ATOM-CHAR = <any CHAR except atom-specials>
2080 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2083 ;; list-wildcards = "%" / "*"
2085 ;; quoted-specials = DQUOTE / "\"
2087 (defsubst imap-parse-astring
()
2088 (or (imap-parse-string)
2089 (buffer-substring (point)
2090 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t
)
2091 (goto-char (1- (match-end 0)))
2095 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2098 ;; addr-adl = nstring
2099 ;; ; Holds route from [RFC-822] route-addr if
2102 ;; addr-host = nstring
2103 ;; ; nil indicates [RFC-822] group syntax.
2104 ;; ; Otherwise, holds [RFC-822] domain name
2106 ;; addr-mailbox = nstring
2107 ;; ; nil indicates end of [RFC-822] group; if
2108 ;; ; non-nil and addr-host is nil, holds
2109 ;; ; [RFC-822] group name.
2110 ;; ; Otherwise, holds [RFC-822] local-part
2111 ;; ; after removing [RFC-822] quoting
2113 ;; addr-name = nstring
2114 ;; ; If non-nil, holds phrase from [RFC-822]
2115 ;; ; mailbox after removing [RFC-822] quoting
2118 (defsubst imap-parse-address
()
2120 (when (eq (char-after) ?\
()
2122 (setq address
(vector (prog1 (imap-parse-nstring)
2124 (prog1 (imap-parse-nstring)
2126 (prog1 (imap-parse-nstring)
2128 (imap-parse-nstring)))
2129 (when (eq (char-after) ?\
))
2133 ;; address-list = "(" 1*address ")" / nil
2137 (defsubst imap-parse-address-list
()
2138 (if (eq (char-after) ?\
()
2139 (let (address addresses
)
2141 (while (and (not (eq (char-after) ?\
)))
2142 ;; next line for MS Exchange bug
2143 (progn (and (eq (char-after) ?
) (imap-forward)) t
)
2144 (setq address
(imap-parse-address)))
2145 (setq addresses
(cons address addresses
)))
2146 (when (eq (char-after) ?\
))
2148 (nreverse addresses
)))
2149 ;; With assert, the code might not be eval'd.
2150 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2153 ;; mailbox = "INBOX" / astring
2154 ;; ; INBOX is case-insensitive. All case variants of
2155 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2156 ;; ; not as an astring. An astring which consists of
2157 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2158 ;; ; is considered to be INBOX and not an astring.
2159 ;; ; Refer to section 5.1 for further
2160 ;; ; semantic details of mailbox names.
2162 (defsubst imap-parse-mailbox
()
2163 (let ((mailbox (imap-parse-astring)))
2164 (if (string-equal "INBOX" (upcase mailbox
))
2168 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2170 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2171 ;; ; Authentication condition
2173 ;; resp-cond-bye = "BYE" SP resp-text
2175 (defun imap-parse-greeting ()
2176 "Parse an IMAP greeting."
2177 (cond ((looking-at "\\* OK ")
2178 (setq imap-state
'nonauth
))
2179 ((looking-at "\\* PREAUTH ")
2180 (setq imap-state
'auth
))
2181 ((looking-at "\\* BYE ")
2182 (setq imap-state
'closed
))))
2184 ;; response = *(continue-req / response-data) response-done
2186 ;; continue-req = "+" SP (resp-text / base64) CRLF
2188 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2189 ;; mailbox-data / message-data / capability-data) CRLF
2191 ;; response-done = response-tagged / response-fatal
2193 ;; response-fatal = "*" SP resp-cond-bye CRLF
2194 ;; ; Server closes connection immediately
2196 ;; response-tagged = tag SP resp-cond-state CRLF
2198 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2199 ;; ; Status condition
2201 ;; resp-cond-bye = "BYE" SP resp-text
2203 ;; mailbox-data = "FLAGS" SP flag-list /
2204 ;; "LIST" SP mailbox-list /
2205 ;; "LSUB" SP mailbox-list /
2206 ;; "SEARCH" *(SP nz-number) /
2207 ;; "STATUS" SP mailbox SP "("
2208 ;; [status-att SP number *(SP status-att SP number)] ")" /
2209 ;; number SP "EXISTS" /
2210 ;; number SP "RECENT"
2212 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2214 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2216 ;; ; IMAP4rev1 servers which offer RFC 1730
2217 ;; ; compatibility MUST list "IMAP4" as the first
2220 (defun imap-parse-response ()
2221 "Parse an IMAP command response."
2223 (case (setq token
(read (current-buffer)))
2224 (+ (setq imap-continuation
2225 (or (buffer-substring (min (point-max) (1+ (point)))
2228 (* (case (prog1 (setq token
(read (current-buffer)))
2230 (OK (imap-parse-resp-text))
2231 (NO (imap-parse-resp-text))
2232 (BAD (imap-parse-resp-text))
2233 (BYE (imap-parse-resp-text))
2234 (FLAGS (imap-mailbox-put 'flags
(imap-parse-flag-list)))
2235 (LIST (imap-parse-data-list 'list
))
2236 (LSUB (imap-parse-data-list 'lsub
))
2237 (SEARCH (imap-mailbox-put
2239 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2240 (STATUS (imap-parse-status))
2241 (CAPABILITY (setq imap-capability
2242 (read (concat "(" (upcase (buffer-substring
2243 (point) (point-max)))
2245 (ID (setq imap-id
(read (buffer-substring (point)
2247 (ACL (imap-parse-acl))
2248 (t (case (prog1 (read (current-buffer))
2250 (EXISTS (imap-mailbox-put 'exists token
))
2251 (RECENT (imap-mailbox-put 'recent token
))
2253 (FETCH (imap-parse-fetch token
))
2254 (t (message "Garbage: %s" (buffer-string)))))))
2256 (if (not (integerp token
))
2257 (message "Garbage: %s" (buffer-string))
2258 (case (prog1 (setq status
(read (current-buffer)))
2261 (setq imap-reached-tag
(max imap-reached-tag token
))
2262 (imap-parse-resp-text)))
2264 (setq imap-reached-tag
(max imap-reached-tag token
))
2266 (imap-parse-resp-text))
2268 (when (eq (char-after) ?\
[)
2269 (setq code
(buffer-substring (point)
2270 (search-forward "]")))
2272 (setq text
(buffer-substring (point) (point-max)))
2273 (push (list token status code text
)
2274 imap-failed-tags
))))
2276 (setq imap-reached-tag
(max imap-reached-tag token
))
2278 (imap-parse-resp-text))
2280 (when (eq (char-after) ?\
[)
2281 (setq code
(buffer-substring (point)
2282 (search-forward "]")))
2284 (setq text
(buffer-substring (point) (point-max)))
2285 (push (list token status code text
) imap-failed-tags
)
2286 (error "Internal error, tag %s status %s code %s text %s"
2287 token status code text
))))
2288 (t (message "Garbage: %s" (buffer-string))))
2289 (when (assq token imap-callbacks
)
2290 (funcall (cdr (assq token imap-callbacks
)) token status
)
2291 (setq imap-callbacks
2292 (imap-remassoc token imap-callbacks
)))))))))
2294 ;; resp-text = ["[" resp-text-code "]" SP] text
2296 ;; text = 1*TEXT-CHAR
2298 ;; TEXT-CHAR = <any CHAR except CR and LF>
2300 (defun imap-parse-resp-text ()
2301 (imap-parse-resp-text-code))
2303 ;; resp-text-code = "ALERT" /
2304 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2305 ;; "NEWNAME" SP string SP string /
2307 ;; "PERMANENTFLAGS" SP "("
2308 ;; [flag-perm *(SP flag-perm)] ")" /
2312 ;; "UIDNEXT" SP nz-number /
2313 ;; "UIDVALIDITY" SP nz-number /
2314 ;; "UNSEEN" SP nz-number /
2315 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2317 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2319 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2321 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2323 ;; ; Identifies a set of messages. For message
2324 ;; ; sequence numbers, these are consecutive
2325 ;; ; numbers from 1 to the number of messages in
2327 ;; ; Comma delimits individual numbers, colon
2328 ;; ; delimits between two numbers inclusive.
2329 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2330 ;; ; 14,15 for a mailbox with 15 messages.
2332 ;; sequence-num = nz-number / "*"
2333 ;; ; * is the largest number in use. For message
2334 ;; ; sequence numbers, it is the number of messages
2335 ;; ; in the mailbox. For unique identifiers, it is
2336 ;; ; the unique identifier of the last message in
2339 ;; flag-perm = flag / "\*"
2341 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2342 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2343 ;; ; Does not include "\Recent"
2345 ;; flag-extension = "\" atom
2346 ;; ; Future expansion. Client implementations
2347 ;; ; MUST accept flag-extension flags. Server
2348 ;; ; implementations MUST NOT generate
2349 ;; ; flag-extension flags except as defined by
2350 ;; ; future standard or standards-track
2351 ;; ; revisions of this specification.
2353 ;; flag-keyword = atom
2355 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2357 (defun imap-parse-resp-text-code ()
2358 ;; xxx next line for stalker communigate pro 3.3.1 bug
2359 (when (looking-at " \\[")
2361 (when (eq (char-after) ?\
[)
2363 (cond ((search-forward "PERMANENTFLAGS " nil t
)
2364 (imap-mailbox-put 'permanentflags
(imap-parse-flag-list)))
2365 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t
)
2366 (imap-mailbox-put 'uidnext
(match-string 1)))
2367 ((search-forward "UNSEEN " nil t
)
2368 (imap-mailbox-put 'first-unseen
(read (current-buffer))))
2369 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2370 (imap-mailbox-put 'uidvalidity
(match-string 1)))
2371 ((search-forward "READ-ONLY" nil t
)
2372 (imap-mailbox-put 'read-only t
))
2373 ((search-forward "NEWNAME " nil t
)
2374 (let (oldname newname
)
2375 (setq oldname
(imap-parse-string))
2377 (setq newname
(imap-parse-string))
2378 (imap-mailbox-put 'newname newname oldname
)))
2379 ((search-forward "TRYCREATE" nil t
)
2380 (imap-mailbox-put 'trycreate t imap-current-target-mailbox
))
2381 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2382 (imap-mailbox-put 'appenduid
2383 (list (match-string 1)
2384 (string-to-number (match-string 2)))
2385 imap-current-target-mailbox
))
2386 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2387 (imap-mailbox-put 'copyuid
(list (match-string 1)
2390 imap-current-target-mailbox
))
2391 ((search-forward "ALERT] " nil t
)
2392 (message "Imap server %s information: %s" imap-server
2393 (buffer-substring (point) (point-max)))))))
2395 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2396 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2398 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2399 ;; *(SP mbx-list-oflag) /
2400 ;; mbx-list-oflag *(SP mbx-list-oflag)
2402 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2403 ;; ; Other flags; multiple possible per LIST response
2405 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2406 ;; ; Selectability flags; only one per LIST response
2408 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2409 ;; "\" quoted-specials
2411 ;; quoted-specials = DQUOTE / "\"
2413 (defun imap-parse-data-list (type)
2414 (let (flags delimiter mailbox
)
2415 (setq flags
(imap-parse-flag-list))
2416 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2417 (setq delimiter
(match-string 1))
2418 (goto-char (1+ (match-end 0)))
2419 (when (setq mailbox
(imap-parse-mailbox))
2420 (imap-mailbox-put type t mailbox
)
2421 (imap-mailbox-put 'list-flags flags mailbox
)
2422 (imap-mailbox-put 'delimiter delimiter mailbox
)))))
2424 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2425 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2426 ;; "INTERNALDATE" SPACE date_time /
2427 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2428 ;; "RFC822.SIZE" SPACE number /
2429 ;; "BODY" ["STRUCTURE"] SPACE body /
2430 ;; "BODY" section ["<" number ">"] SPACE nstring /
2431 ;; "UID" SPACE uniqueid) ")"
2433 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2434 ;; SPACE time SPACE zone <">
2436 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2437 ;; ["." (section_text / "MIME")])] "]"
2439 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2440 ;; SPACE header_list / "TEXT"
2442 ;; header_fld_name ::= astring
2444 ;; header_list ::= "(" 1#header_fld_name ")"
2446 (defsubst imap-parse-header-list
()
2447 (when (eq (char-after) ?\
()
2449 (while (not (eq (char-after) ?\
)))
2451 (push (imap-parse-astring) strlist
))
2453 (nreverse strlist
))))
2455 (defsubst imap-parse-fetch-body-section
()
2457 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t
)))))
2458 (if (eq (char-before) ?
)
2460 (mapconcat 'identity
(cons section
(imap-parse-header-list)) " ")
2461 (search-forward "]" nil t
))
2464 (defun imap-parse-fetch (response)
2465 (when (eq (char-after) ?\
()
2466 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2467 rfc822size body bodydetail bodystructure flags-empty
)
2468 ;; Courier can insert spurious blank characters which will
2469 ;; confuse `read', so skip past them.
2470 (while (let ((moved (skip-chars-forward " \t")))
2471 (prog1 (not (eq (char-after) ?\
)))
2472 (unless (= moved
0) (backward-char))))
2474 (let ((token (read (current-buffer))))
2476 (cond ((eq token
'UID
)
2477 (setq uid
(condition-case ()
2478 (read (current-buffer))
2481 (setq flags
(imap-parse-flag-list))
2483 (setq flags-empty
't
)))
2484 ((eq token
'ENVELOPE
)
2485 (setq envelope
(imap-parse-envelope)))
2486 ((eq token
'INTERNALDATE
)
2487 (setq internaldate
(imap-parse-string)))
2489 (setq rfc822
(imap-parse-nstring)))
2490 ((eq token
'RFC822.HEADER
)
2491 (setq rfc822header
(imap-parse-nstring)))
2492 ((eq token
'RFC822.TEXT
)
2493 (setq rfc822text
(imap-parse-nstring)))
2494 ((eq token
'RFC822.SIZE
)
2495 (setq rfc822size
(read (current-buffer))))
2497 (if (eq (char-before) ?\
[)
2499 (upcase (imap-parse-fetch-body-section))
2500 (and (eq (char-after) ?
<)
2501 (buffer-substring (1+ (point))
2502 (search-forward ">" nil t
)))
2503 (progn (imap-forward)
2504 (imap-parse-nstring)))
2506 (setq body
(imap-parse-body))))
2507 ((eq token
'BODYSTRUCTURE
)
2508 (setq bodystructure
(imap-parse-body))))))
2510 (setq imap-current-message uid
)
2511 (imap-message-put uid
'UID uid
)
2512 (and (or flags flags-empty
) (imap-message-put uid
'FLAGS flags
))
2513 (and envelope
(imap-message-put uid
'ENVELOPE envelope
))
2514 (and internaldate
(imap-message-put uid
'INTERNALDATE internaldate
))
2515 (and rfc822
(imap-message-put uid
'RFC822 rfc822
))
2516 (and rfc822header
(imap-message-put uid
'RFC822.HEADER rfc822header
))
2517 (and rfc822text
(imap-message-put uid
'RFC822.TEXT rfc822text
))
2518 (and rfc822size
(imap-message-put uid
'RFC822.SIZE rfc822size
))
2519 (and body
(imap-message-put uid
'BODY body
))
2520 (and bodydetail
(imap-message-put uid
'BODYDETAIL bodydetail
))
2521 (and bodystructure
(imap-message-put uid
'BODYSTRUCTURE bodystructure
))
2522 (run-hooks 'imap-fetch-data-hook
)))))
2524 ;; mailbox-data = ...
2525 ;; "STATUS" SP mailbox SP "("
2526 ;; [status-att SP number
2527 ;; *(SP status-att SP number)] ")"
2530 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2533 (defun imap-parse-status ()
2534 (let ((mailbox (imap-parse-mailbox)))
2535 (if (eq (char-after) ?
)
2537 (when (and mailbox
(eq (char-after) ?\
())
2538 (while (and (not (eq (char-after) ?\
)))
2539 (or (forward-char) t
)
2540 (looking-at "\\([A-Za-z]+\\) "))
2541 (let ((token (upcase (match-string 1))))
2542 (goto-char (match-end 0))
2543 (cond ((string= token
"MESSAGES")
2544 (imap-mailbox-put 'messages
(read (current-buffer)) mailbox
))
2545 ((string= token
"RECENT")
2546 (imap-mailbox-put 'recent
(read (current-buffer)) mailbox
))
2547 ((string= token
"UIDNEXT")
2548 (and (looking-at "[0-9]+")
2549 (imap-mailbox-put 'uidnext
(match-string 0) mailbox
)
2550 (goto-char (match-end 0))))
2551 ((string= token
"UIDVALIDITY")
2552 (and (looking-at "[0-9]+")
2553 (imap-mailbox-put 'uidvalidity
(match-string 0) mailbox
)
2554 (goto-char (match-end 0))))
2555 ((string= token
"UNSEEN")
2556 (imap-mailbox-put 'unseen
(read (current-buffer)) mailbox
))
2558 (message "Unknown status data %s in mailbox %s ignored"
2560 (read (current-buffer)))))))))
2562 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2565 ;; identifier ::= astring
2567 ;; rights ::= astring
2569 (defun imap-parse-acl ()
2570 (let ((mailbox (imap-parse-mailbox))
2571 identifier rights acl
)
2572 (while (eq (char-after) ?\
)
2574 (setq identifier
(imap-parse-astring))
2576 (setq rights
(imap-parse-astring))
2577 (setq acl
(append acl
(list (cons identifier rights
)))))
2578 (imap-mailbox-put 'acl acl mailbox
)))
2580 ;; flag-list = "(" [flag *(SP flag)] ")"
2582 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2583 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2584 ;; ; Does not include "\Recent"
2586 ;; flag-keyword = atom
2588 ;; flag-extension = "\" atom
2589 ;; ; Future expansion. Client implementations
2590 ;; ; MUST accept flag-extension flags. Server
2591 ;; ; implementations MUST NOT generate
2592 ;; ; flag-extension flags except as defined by
2593 ;; ; future standard or standards-track
2594 ;; ; revisions of this specification.
2596 (defun imap-parse-flag-list ()
2597 (let (flag-list start
)
2598 (assert (eq (char-after) ?\
() nil
"In imap-parse-flag-list 1")
2599 (while (and (not (eq (char-after) ?\
)))
2602 ;; next line for Courier IMAP bug.
2603 (skip-chars-forward " ")
2605 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2606 (push (buffer-substring start
(point)) flag-list
))
2607 (assert (eq (char-after) ?\
)) nil
"In imap-parse-flag-list 2")
2609 (nreverse flag-list
)))
2611 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2612 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2613 ;; env-in-reply-to SP env-message-id ")"
2615 ;; env-bcc = "(" 1*address ")" / nil
2617 ;; env-cc = "(" 1*address ")" / nil
2619 ;; env-date = nstring
2621 ;; env-from = "(" 1*address ")" / nil
2623 ;; env-in-reply-to = nstring
2625 ;; env-message-id = nstring
2627 ;; env-reply-to = "(" 1*address ")" / nil
2629 ;; env-sender = "(" 1*address ")" / nil
2631 ;; env-subject = nstring
2633 ;; env-to = "(" 1*address ")" / nil
2635 (defun imap-parse-envelope ()
2636 (when (eq (char-after) ?\
()
2638 (vector (prog1 (imap-parse-nstring) ;; date
2640 (prog1 (imap-parse-nstring) ;; subject
2642 (prog1 (imap-parse-address-list) ;; from
2644 (prog1 (imap-parse-address-list) ;; sender
2646 (prog1 (imap-parse-address-list) ;; reply-to
2648 (prog1 (imap-parse-address-list) ;; to
2650 (prog1 (imap-parse-address-list) ;; cc
2652 (prog1 (imap-parse-address-list) ;; bcc
2654 (prog1 (imap-parse-nstring) ;; in-reply-to
2656 (prog1 (imap-parse-nstring) ;; message-id
2659 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2661 (defsubst imap-parse-string-list
()
2662 (cond ((eq (char-after) ?\
() ;; body-fld-param
2665 (while (setq str
(imap-parse-string))
2667 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2668 ;; between body-fld-param's sometimes
2669 (or (eq (char-after) ?
\")
2671 (nreverse strlist
)))
2675 ;; body-extension = nstring / number /
2676 ;; "(" body-extension *(SP body-extension) ")"
2677 ;; ; Future expansion. Client implementations
2678 ;; ; MUST accept body-extension fields. Server
2679 ;; ; implementations MUST NOT generate
2680 ;; ; body-extension fields except as defined by
2681 ;; ; future standard or standards-track
2682 ;; ; revisions of this specification.
2684 (defun imap-parse-body-extension ()
2685 (if (eq (char-after) ?\
()
2688 (push (imap-parse-body-extension) b-e
)
2689 (while (eq (char-after) ?\
)
2691 (push (imap-parse-body-extension) b-e
))
2692 (assert (eq (char-after) ?\
)) nil
"In imap-parse-body-extension")
2695 (or (imap-parse-number)
2696 (imap-parse-nstring))))
2698 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2699 ;; *(SP body-extension)]]
2700 ;; ; MUST NOT be returned on non-extensible
2703 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2704 ;; *(SP body-extension)]]
2705 ;; ; MUST NOT be returned on non-extensible
2708 (defsubst imap-parse-body-ext
()
2710 (when (eq (char-after) ?\
) ;; body-fld-dsp
2713 (if (eq (char-after) ?\
()
2716 (push (imap-parse-string) dsp
)
2718 (push (imap-parse-string-list) dsp
)
2720 ;; With assert, the code might not be eval'd.
2721 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2723 (push (nreverse dsp
) ext
))
2724 (when (eq (char-after) ?\
) ;; body-fld-lang
2726 (if (eq (char-after) ?\
()
2727 (push (imap-parse-string-list) ext
)
2728 (push (imap-parse-nstring) ext
))
2729 (while (eq (char-after) ?\
) ;; body-extension
2731 (setq ext
(append (imap-parse-body-extension) ext
)))))
2734 ;; body = "(" body-type-1part / body-type-mpart ")"
2736 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2737 ;; *(SP body-extension)]]
2738 ;; ; MUST NOT be returned on non-extensible
2741 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2742 ;; *(SP body-extension)]]
2743 ;; ; MUST NOT be returned on non-extensible
2746 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2747 ;; body-fld-enc SP body-fld-octets
2749 ;; body-fld-desc = nstring
2751 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2753 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2754 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2756 ;; body-fld-id = nstring
2758 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2760 ;; body-fld-lines = number
2762 ;; body-fld-md5 = nstring
2764 ;; body-fld-octets = number
2766 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2768 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2769 ;; [SP body-ext-1part]
2771 ;; body-type-basic = media-basic SP body-fields
2772 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2774 ;; body-type-msg = media-message SP body-fields SP envelope
2775 ;; SP body SP body-fld-lines
2777 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2779 ;; body-type-mpart = 1*body SP media-subtype
2780 ;; [SP body-ext-mpart]
2782 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2783 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2784 ;; ; Defined in [MIME-IMT]
2786 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2787 ;; ; Defined in [MIME-IMT]
2789 ;; media-subtype = string
2790 ;; ; Defined in [MIME-IMT]
2792 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2793 ;; ; Defined in [MIME-IMT]
2795 (defun imap-parse-body ()
2797 (when (eq (char-after) ?\
()
2799 (if (eq (char-after) ?\
()
2801 (while (and (eq (char-after) ?\
()
2802 (setq subbody
(imap-parse-body)))
2803 ;; buggy stalker communigate pro 3.0 inserts a SPC between
2804 ;; parts in multiparts
2805 (when (and (eq (char-after) ?\
)
2806 (eq (char-after (1+ (point))) ?\
())
2808 (push subbody body
))
2810 (push (imap-parse-string) body
) ;; media-subtype
2811 (when (eq (char-after) ?\
) ;; body-ext-mpart:
2813 (if (eq (char-after) ?\
() ;; body-fld-param
2814 (push (imap-parse-string-list) body
)
2815 (push (and (imap-parse-nil) nil
) body
))
2817 (append (imap-parse-body-ext) body
))) ;; body-ext-...
2818 (assert (eq (char-after) ?\
)) nil
"In imap-parse-body")
2822 (push (imap-parse-string) body
) ;; media-type
2824 (push (imap-parse-string) body
) ;; media-subtype
2826 ;; next line for Sun SIMS bug
2827 (and (eq (char-after) ?
) (imap-forward))
2828 (if (eq (char-after) ?\
() ;; body-fld-param
2829 (push (imap-parse-string-list) body
)
2830 (push (and (imap-parse-nil) nil
) body
))
2832 (push (imap-parse-nstring) body
) ;; body-fld-id
2834 (push (imap-parse-nstring) body
) ;; body-fld-desc
2836 ;; Next `or' for Sun SIMS bug. It regards body-fld-enc as a
2837 ;; nstring and returns nil instead of defaulting back to 7BIT
2838 ;; as the standard says.
2839 ;; Exchange (2007, at least) does this as well.
2840 (push (or (imap-parse-nstring) "7BIT") body
) ;; body-fld-enc
2842 ;; Exchange 2007 can return -1, contrary to the spec...
2843 (if (eq (char-after) ?-
)
2845 (skip-chars-forward "-0-9")
2847 (push (imap-parse-number) body
)) ;; body-fld-octets
2849 ;; Ok, we're done parsing the required parts, what comes now is one of
2852 ;; envelope (then we're parsing body-type-msg)
2853 ;; body-fld-lines (then we're parsing body-type-text)
2854 ;; body-ext-1part (then we're parsing body-type-basic)
2856 ;; The problem is that the two first are in turn optionally followed
2857 ;; by the third. So we parse the first two here (if there are any)...
2859 (when (eq (char-after) ?\
)
2862 (cond ((eq (char-after) ?\
() ;; body-type-msg:
2863 (push (imap-parse-envelope) body
) ;; envelope
2865 (push (imap-parse-body) body
) ;; body
2866 ;; buggy stalker communigate pro 3.0 doesn't print
2867 ;; number of lines in message/rfc822 attachment
2868 (if (eq (char-after) ?\
))
2871 (push (imap-parse-number) body
))) ;; body-fld-lines
2872 ((setq lines
(imap-parse-number)) ;; body-type-text:
2873 (push lines body
)) ;; body-fld-lines
2875 (backward-char))))) ;; no match...
2877 ;; ...and then parse the third one here...
2879 (when (eq (char-after) ?\
) ;; body-ext-1part:
2881 (push (imap-parse-nstring) body
) ;; body-fld-md5
2882 (setq body
(append (imap-parse-body-ext) body
))) ;; body-ext-1part..
2884 (assert (eq (char-after) ?\
)) nil
"In imap-parse-body 2")
2888 (when imap-debug
; (untrace-all)
2890 (buffer-disable-undo (get-buffer-create imap-debug-buffer
))
2891 (mapc (lambda (f) (trace-function-background f imap-debug-buffer
))
2900 imap-interactive-login
2917 imap-send-command-wait
2922 imap-current-mailbox
2923 imap-current-mailbox-p-1
2924 imap-current-mailbox-p
2925 imap-mailbox-select-1
2927 imap-mailbox-examine-1
2928 imap-mailbox-examine
2929 imap-mailbox-unselect
2930 imap-mailbox-expunge
2932 imap-mailbox-create-1
2938 imap-mailbox-subscribe
2939 imap-mailbox-unsubscribe
2941 imap-mailbox-acl-get
2942 imap-mailbox-acl-set
2943 imap-mailbox-acl-delete
2944 imap-current-message
2945 imap-list-to-message-set
2953 imap-message-flag-permanent-p
2954 imap-message-flags-set
2955 imap-message-flags-del
2956 imap-message-flags-add
2957 imap-message-copyuid-1
2958 imap-message-copyuid
2960 imap-message-appenduid-1
2961 imap-message-appenduid
2973 imap-parse-resp-text
2974 imap-parse-resp-text-code
2975 imap-parse-data-list
2979 imap-parse-flag-list
2981 imap-parse-body-extension
2987 ;;; imap.el ends here