1 ;;; imap.el --- imap library -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998-2018 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 <https://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-lib
))
139 (require 'format-spec
)
142 ;; Hmm... digest-md5 is not part of Emacs.
143 ;; FIXME: Should/can we use sasl-digest.el instead?
144 (declare-function digest-md5-parse-digest-challenge
"ext:digest-md5")
145 (declare-function digest-md5-digest-response
"ext:digest-md5")
146 (declare-function digest-md5-digest-uri
"ext:digest-md5")
147 (declare-function digest-md5-challenge
"ext:digest-md5")
152 "Low-level IMAP issues."
156 (defcustom imap-kerberos4-program
'("imtest -m kerberos_v4 -u %l -p %p %s"
158 "List of strings containing commands for Kerberos 4 authentication.
159 %s is replaced with server hostname, %p with port to connect to, and
160 %l with the value of `imap-default-user'. The program should accept
161 IMAP commands on stdin and return responses to stdout. Each entry in
162 the list is tried until a successful connection is made."
164 :type
'(repeat string
))
166 (defcustom imap-gssapi-program
(list
167 (concat "gsasl %s %p "
168 "--mechanism GSSAPI "
169 "--authentication-id %l")
170 "imtest -m gssapi -u %l -p %p %s")
171 "List of strings containing commands for GSSAPI (krb5) authentication.
172 %s is replaced with server hostname, %p with port to connect to, and
173 %l with the value of `imap-default-user'. The program should accept
174 IMAP commands on stdin and return responses to stdout. Each entry in
175 the list is tried until a successful connection is made."
177 :type
'(repeat string
))
179 (defcustom imap-shell-program
'("ssh %s imapd"
181 "ssh %g ssh %s imapd"
182 "rsh %g rsh %s imapd")
183 "A list of strings, containing commands for IMAP connection.
184 Within a string, %s is replaced with the server address, %p with port
185 number on server, %g with `imap-shell-host', and %l with
186 `imap-default-user'. The program should read IMAP commands from stdin
187 and write IMAP response to stdout. Each entry in the list is tried
188 until a successful connection is made."
190 :type
'(repeat string
))
192 (defcustom imap-process-connection-type nil
193 "Value for `process-connection-type' to use for Kerberos4, GSSAPI, shell, and SSL.
194 The `process-connection-type' variable controls the type of device
195 used to communicate with subprocesses. Values are nil to use a
196 pipe, or t or `pty' to use a pty. The value has no effect if the
197 system has no ptys or if all ptys are busy: then a pipe is used
198 in any case. The value takes effect when an IMAP server is
199 opened; changing it after that has no effect."
204 (defcustom imap-use-utf7 t
205 "If non-nil, do utf7 encoding/decoding of mailbox names.
206 Since the UTF7 decoding currently only decodes into ISO-8859-1
207 characters, you may disable this decoding if you need to access UTF7
208 encoded mailboxes which doesn't translate into ISO-8859-1."
212 (defcustom imap-log nil
213 "If non-nil, an imap session trace is placed in `imap-log-buffer'.
214 Note that username, passwords and other privacy sensitive
215 information (such as e-mail) may be stored in the buffer.
216 It is not written to disk, however. Do not enable this
217 variable unless you are comfortable with that.
219 See also `imap-debug'."
223 (defcustom imap-debug nil
224 "If non-nil, trace imap- functions into `imap-debug-buffer'.
225 Uses `trace-function-background', so you can turn it off with,
228 Note that username, passwords and other privacy sensitive
229 information (such as e-mail) may be stored in the buffer.
230 It is not written to disk, however. Do not enable this
231 variable unless you are comfortable with that.
233 This variable only takes effect when loading the `imap' library.
234 See also `imap-log'."
238 (defcustom imap-shell-host
"gateway"
239 "Hostname of rlogin proxy."
243 (defcustom imap-default-user
(user-login-name)
244 "Default username to use."
248 (defcustom imap-read-timeout
(if (memq system-type
'(windows-nt cygwin
))
251 "How long to wait between checking for the end of output.
252 Shorter values mean quicker response, but is more CPU intensive."
256 (defcustom imap-store-password nil
257 "If non-nil, store session password without prompting."
261 ;; Various variables.
263 (defvar imap-fetch-data-hook nil
264 "Hooks called after receiving each FETCH response.")
266 (defvar imap-streams
'(gssapi kerberos4 starttls tls ssl network shell
)
267 "Priority of streams to consider when opening connection to server.")
269 (defvar imap-stream-alist
270 '((gssapi imap-gssapi-stream-p imap-gssapi-open
)
271 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open
)
272 (tls imap-tls-p imap-tls-open
)
273 (ssl imap-tls-p imap-tls-open
)
274 (network imap-network-p imap-network-open
)
275 (shell imap-shell-p imap-shell-open
)
276 (starttls imap-starttls-p imap-starttls-open
))
277 "Definition of network streams.
281 NAME names the stream, CHECK is a function returning non-nil if the
282 server support the stream and OPEN is a function for opening the
285 (defvar imap-authenticators
'(gssapi
292 "Priority of authenticators to consider when authenticating to server.")
294 (defvar imap-authenticator-alist
295 '((gssapi imap-gssapi-auth-p imap-gssapi-auth
)
296 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth
)
297 (sasl imap-sasl-auth-p imap-sasl-auth
)
298 (cram-md5 imap-cram-md5-p imap-cram-md5-auth
)
299 (login imap-login-p imap-login-auth
)
300 (anonymous imap-anonymous-p imap-anonymous-auth
)
301 (digest-md5 imap-digest-md5-p imap-digest-md5-auth
))
302 "Definition of authenticators.
304 \(NAME CHECK AUTHENTICATE)
306 NAME names the authenticator. CHECK is a function returning non-nil if
307 the server support the authenticator and AUTHENTICATE is a function
308 for doing the actual authentication.")
310 (defvar imap-error nil
311 "Error codes from the last command.")
313 (defvar imap-logout-timeout nil
314 "Close server immediately if it can't logout in this number of seconds.
315 If it is nil, never close server until logout completes. Normally,
316 the value of this variable will be bound to a certain value to which
317 an application program that uses this module specifies on a per-server
320 ;; Internal constants. Change these and die.
322 (defconst imap-default-port
143)
323 (defconst imap-default-ssl-port
993)
324 (defconst imap-default-tls-port
993)
325 (defconst imap-default-stream
'network
)
326 (defconst imap-coding-system-for-read
'binary
)
327 (defconst imap-coding-system-for-write
'binary
)
328 (defconst imap-local-variables
'(imap-server
337 imap-current-target-mailbox
347 imap-calculate-literal-size-first
349 (defconst imap-log-buffer
"*imap-log*")
350 (defconst imap-debug-buffer
"*imap-debug*")
352 ;; Internal variables.
354 (defvar imap-stream nil
)
355 (defvar imap-auth nil
)
356 (defvar imap-server nil
)
357 (defvar imap-port nil
)
358 (defvar imap-username nil
)
359 (defvar imap-password nil
)
360 (defvar imap-last-authenticator nil
)
361 (defvar imap-calculate-literal-size-first nil
)
362 (defvar imap-state
'closed
364 Valid states are `closed', `initial', `nonauth', `auth', `selected'
367 (defvar imap-server-eol
"\r\n"
368 "The EOL string sent from the server.")
370 (defvar imap-client-eol
"\r\n"
371 "The EOL string we send to the server.")
373 (defvar imap-current-mailbox nil
374 "Current mailbox name.")
376 (defvar imap-current-target-mailbox nil
377 "Current target mailbox for COPY and APPEND commands.")
379 (defvar imap-mailbox-data nil
380 "Obarray with mailbox data.")
382 (defvar imap-mailbox-prime
997
383 "Length of `imap-mailbox-data'.")
385 (defvar imap-current-message nil
386 "Current message number.")
388 (defvar imap-message-data nil
389 "Obarray with message data.")
391 (defvar imap-message-prime
997
392 "Length of `imap-message-data'.")
394 (defvar imap-capability nil
395 "Capability for server.")
401 (defvar imap-namespace nil
402 "Namespace for current server.")
404 (defvar imap-reached-tag
0
405 "Lower limit on command tags that have been parsed.")
407 (defvar imap-failed-tags nil
408 "Alist of tags that failed.
409 Each element is a list with four elements; tag (a integer), response
410 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
411 human readable response text (a string).")
414 "Command tag number.")
416 (defvar imap-process nil
419 (defvar imap-continuation nil
420 "Non-nil indicates that the server emitted a continuation request.
421 The actual value is really the text on the continuation line.")
423 (defvar imap-callbacks nil
424 "List of response tags and callbacks, on the form `(number . function)'.
425 The function should take two arguments, the first the IMAP tag and the
426 second the status (OK, NO, BAD etc) of the command.")
428 (defvar imap-enable-exchange-bug-workaround nil
429 "Send FETCH UID commands as *:* instead of *.
431 When non-nil, use an alternative UIDS form. Enabling appears to
432 be required for some servers (e.g., Microsoft Exchange 2007)
433 which otherwise would trigger a response `BAD The specified
434 message set is invalid.'. We don't unconditionally use this
435 form, since this is said to be significantly inefficient.
437 This variable is set to t automatically per server if the
438 canonical form fails.")
441 ;; Utility functions:
443 (defun imap-remassoc (key alist
)
444 "Delete by side effect any elements of ALIST whose car is `equal' to KEY.
445 The modified ALIST is returned. If the first member
446 of ALIST has a car that is `equal' to KEY, there is no way to remove it
447 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
448 sure of changing the value of `foo'."
450 (if (equal key
(caar alist
))
452 (setcdr alist
(imap-remassoc key
(cdr alist
)))
455 (defmacro imap-disable-multibyte
()
456 "Enable multibyte in the current buffer."
457 (unless (featurep 'xemacs
)
458 '(set-buffer-multibyte nil
)))
460 (defsubst imap-utf7-encode
(string)
464 (utf7-encode string t
)
466 "imap: Could not UTF7 encode `%s', using it unencoded..."
471 (defsubst imap-utf7-decode
(string)
475 (utf7-decode string t
)
477 "imap: Could not UTF7 decode `%s', using it undecoded..."
482 (defsubst imap-ok-p
(status)
485 (setq imap-error status
)
488 (defun imap-error-text (&optional buffer
)
489 (with-current-buffer (or buffer
(current-buffer))
490 (nth 3 (car imap-failed-tags
))))
493 ;; Server functions; stream stuff:
495 (defun imap-log (string-or-buffer)
497 (with-current-buffer (get-buffer-create imap-log-buffer
)
498 (imap-disable-multibyte)
499 (buffer-disable-undo)
500 (goto-char (point-max))
501 (if (bufferp string-or-buffer
)
502 (insert-buffer-substring string-or-buffer
)
503 (insert string-or-buffer
)))))
505 (defun imap-kerberos4-stream-p (buffer)
506 (imap-capability 'AUTH
=KERBEROS_V4 buffer
))
508 (defun imap-kerberos4-open (name buffer server port
)
509 (let ((cmds imap-kerberos4-program
)
511 (while (and (not done
) (setq cmd
(pop cmds
)))
512 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd
)
514 (let* ((port (or port imap-default-port
))
515 (coding-system-for-read imap-coding-system-for-read
)
516 (coding-system-for-write imap-coding-system-for-write
)
517 (process-connection-type imap-process-connection-type
)
518 (process (start-process
519 name buffer shell-file-name shell-command-switch
524 ?p
(number-to-string port
)
525 ?l imap-default-user
))))
528 (with-current-buffer buffer
529 (setq imap-client-eol
"\n"
530 imap-calculate-literal-size-first t
)
531 (while (and (memq (process-status process
) '(open run
))
532 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
533 (goto-char (point-min))
534 ;; Athena IMTEST can output SSL verify errors
535 (or (while (looking-at "^verify error:num=")
538 (or (while (looking-at "^TLS connection established")
541 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
542 (or (while (looking-at "^C:")
545 ;; cyrus 1.6 imtest print "S: " before server greeting
546 (or (not (looking-at "S: "))
549 (not (and (imap-parse-greeting)
550 ;; success in imtest < 1.6:
551 (or (re-search-forward
552 "^__\\(.*\\)__\n" nil t
)
553 ;; success in imtest 1.6:
555 "^\\(Authenticat.*\\)" nil t
))
556 (setq response
(match-string 1)))))
557 (accept-process-output process
1)
560 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
561 (if response
(concat "done, " response
) "failed"))
562 (if (and response
(let ((case-fold-search nil
))
563 (not (string-match "failed" response
))))
565 (if (memq (process-status process
) '(open run
))
567 (delete-process process
)
571 (defun imap-gssapi-stream-p (buffer)
572 (imap-capability 'AUTH
=GSSAPI buffer
))
574 (defun imap-gssapi-open (name buffer server port
)
575 (let ((cmds imap-gssapi-program
)
577 (while (and (not done
) (setq cmd
(pop cmds
)))
578 (message "Opening GSSAPI IMAP connection with `%s'..." cmd
)
580 (let* ((port (or port imap-default-port
))
581 (coding-system-for-read imap-coding-system-for-read
)
582 (coding-system-for-write imap-coding-system-for-write
)
583 (process-connection-type imap-process-connection-type
)
584 (process (start-process
585 name buffer shell-file-name shell-command-switch
590 ?p
(number-to-string port
)
591 ?l imap-default-user
))))
594 (with-current-buffer buffer
595 (setq imap-client-eol
"\n"
596 imap-calculate-literal-size-first t
)
597 (while (and (memq (process-status process
) '(open run
))
598 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
599 (goto-char (point-min))
600 ;; Athena IMTEST can output SSL verify errors
601 (or (while (looking-at "^verify error:num=")
604 (or (while (looking-at "^TLS connection established")
607 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
608 (or (while (looking-at "^C:")
611 ;; cyrus 1.6 imtest print "S: " before server greeting
612 (or (not (looking-at "S: "))
615 ;; GNU SASL may print 'Trying ...' first.
616 (or (not (looking-at "Trying "))
619 (not (and (imap-parse-greeting)
620 ;; success in imtest 1.6:
622 (concat "^\\(\\(Authenticat.*\\)\\|\\("
623 "Client authentication "
626 (setq response
(match-string 1)))))
627 (accept-process-output process
1)
631 (message "GSSAPI IMAP connection: %s" (or response
"failed"))
632 (if (and response
(let ((case-fold-search nil
))
633 (not (string-match "failed" response
))))
635 (if (memq (process-status process
) '(open run
))
637 (delete-process process
)
641 (defun imap-tls-p (_buffer)
644 (defun imap-tls-open (name buffer server port
)
645 (let* ((port (or port imap-default-tls-port
))
646 (coding-system-for-read imap-coding-system-for-read
)
647 (coding-system-for-write imap-coding-system-for-write
)
648 (process (open-network-stream name buffer server port
651 (while (and (memq (process-status process
) '(open run
))
652 ;; FIXME: Per the "blue moon" comment, the process/buffer
653 ;; handling here, and elsewhere in functions which open
654 ;; streams, looks confused. Obviously we can change buffers
655 ;; if a different process handler kicks in from
656 ;; `accept-process-output' or `sit-for' below, and TRT seems
657 ;; to be to `save-buffer' around those calls. (I wonder why
658 ;; `sit-for' is used with a non-zero wait.) -- fx
659 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
660 (goto-char (point-max))
662 (not (imap-parse-greeting)))
663 (accept-process-output process
1)
666 (when (memq (process-status process
) '(open run
))
669 (defun imap-network-p (_buffer)
672 (defun imap-network-open (name buffer server port
)
673 (let* ((port (or port imap-default-port
))
674 (coding-system-for-read imap-coding-system-for-read
)
675 (coding-system-for-write imap-coding-system-for-write
)
676 (process (open-network-stream name buffer server port
)))
678 (while (and (memq (process-status process
) '(open run
))
679 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
680 (goto-char (point-min))
681 (not (imap-parse-greeting)))
682 (accept-process-output process
1)
685 (when (memq (process-status process
) '(open run
))
688 (defun imap-shell-p (_buffer)
691 (defun imap-shell-open (name buffer server port
)
692 (let ((cmds (if (listp imap-shell-program
) imap-shell-program
693 (list imap-shell-program
)))
695 (while (and (not done
) (setq cmd
(pop cmds
)))
696 (message "imap: Opening IMAP connection with `%s'..." cmd
)
697 (setq imap-client-eol
"\n")
698 (let* ((port (or port imap-default-port
))
699 (coding-system-for-read imap-coding-system-for-read
)
700 (coding-system-for-write imap-coding-system-for-write
)
701 (process-connection-type imap-process-connection-type
)
702 (process (start-process
703 name buffer shell-file-name shell-command-switch
709 ?p
(number-to-string port
)
710 ?l imap-default-user
)))))
712 (while (and (memq (process-status process
) '(open run
))
713 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
714 (goto-char (point-max))
716 (not (imap-parse-greeting)))
717 (accept-process-output process
1)
721 (when (memq (process-status process
) '(open run
))
722 (setq done process
)))))
725 (message "imap: Opening IMAP connection with `%s'...done" cmd
)
727 (message "imap: Opening IMAP connection with `%s'...failed" cmd
)
730 (defun imap-starttls-p (buffer)
731 (imap-capability 'STARTTLS buffer
))
733 (defun imap-starttls-open (name buffer server port
)
734 (message "imap: Connecting with STARTTLS...")
735 (let* ((port (or port imap-default-port
))
736 (coding-system-for-read imap-coding-system-for-read
)
737 (coding-system-for-write imap-coding-system-for-write
)
738 (process (open-network-stream
739 name buffer server port
741 :capability-command
"1 CAPABILITY\r\n"
742 :always-query-capabilities t
743 :end-of-command
"\r\n"
746 #'(lambda (capabilities)
747 (when (string-match-p "STARTTLS" capabilities
)
752 (when (memq (process-status process
) '(open run
))
754 (with-current-buffer buffer
755 (goto-char (point-min))
756 (imap-parse-greeting))))
757 (message "imap: Connecting with STARTTLS...%s" (if done
"done" "failed"))
760 ;; Server functions; authenticator stuff:
762 (defun imap-interactive-login (buffer loginfunc
)
763 "Login to server in BUFFER.
764 Return t if login was successful, nil otherwise.
766 LOGINFUNC is passed a username and a password. It should return
767 t if it successfully authenticates, nil otherwise."
768 (with-current-buffer buffer
769 (make-local-variable 'imap-username
)
770 (make-local-variable 'imap-password
)
771 (let (user passwd ret
)
772 ;; (condition-case ()
773 (while (or (not user
) (not passwd
))
774 (setq user
(or imap-username
775 (read-from-minibuffer
777 "imap: username for %s (using stream `%s'): "
778 imap-server imap-stream
)
779 (or user imap-default-user
))))
784 "imap: password for %s@%s (using authenticator `%s'): "
785 user imap-server imap-auth
))))
786 (when (and user passwd
)
787 (if (funcall loginfunc user passwd
)
789 (message "imap: Login successful...")
792 (when (and (not imap-password
)
793 (or imap-store-password
794 (y-or-n-p "imap: Store password for this IMAP session? ")))
795 (setq imap-password passwd
)))
796 (message "imap: Login failed...")
798 (setq imap-password nil
)
800 ;; (quit (with-current-buffer buffer
803 ;; (error (with-current-buffer buffer
808 (defun imap-gssapi-auth-p (_buffer)
809 (eq imap-stream
'gssapi
))
811 (defun imap-gssapi-auth (_buffer)
812 (message "imap: Authenticating using GSSAPI...%s"
813 (if (eq imap-stream
'gssapi
) "done" "failed"))
814 (eq imap-stream
'gssapi
))
816 (defun imap-kerberos4-auth-p (buffer)
817 (and (imap-capability 'AUTH
=KERBEROS_V4 buffer
)
818 (eq imap-stream
'kerberos4
)))
820 (defun imap-kerberos4-auth (_buffer)
821 (message "imap: Authenticating using Kerberos 4...%s"
822 (if (eq imap-stream
'kerberos4
) "done" "failed"))
823 (eq imap-stream
'kerberos4
))
825 (defun imap-cram-md5-p (buffer)
826 (imap-capability 'AUTH
=CRAM-MD5 buffer
))
828 (defun imap-cram-md5-auth (buffer)
829 "Login to server using the AUTH CRAM-MD5 method."
830 (message "imap: Authenticating using CRAM-MD5...")
831 (let ((done (imap-interactive-login
833 (lambda (user passwd
)
835 (imap-send-command-wait
837 "AUTHENTICATE CRAM-MD5"
839 (let* ((decoded (base64-decode-string challenge
))
840 (hash (rfc2104-hash 'md5
64 16 passwd decoded
))
841 (response (concat user
" " hash
))
842 (encoded (base64-encode-string response
)))
845 (message "imap: Authenticating using CRAM-MD5...done")
846 (message "imap: Authenticating using CRAM-MD5...failed"))))
848 (defun imap-login-p (buffer)
849 (and (not (imap-capability 'LOGINDISABLED buffer
))
850 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer
))))
852 (defun imap-quote-specials (string)
855 (goto-char (point-min))
856 (while (re-search-forward "[\\\"]" nil t
)
862 (defun imap-login-auth (buffer)
863 "Login to server using the LOGIN command."
864 (message "imap: Plaintext authentication...")
865 (imap-interactive-login buffer
866 (lambda (user passwd
)
867 (imap-ok-p (imap-send-command-wait
869 (imap-quote-specials user
)
871 (imap-quote-specials passwd
)
874 (defun imap-anonymous-p (_buffer)
877 (defun imap-anonymous-auth (buffer)
878 (message "imap: Logging in anonymously...")
879 (with-current-buffer buffer
880 (imap-ok-p (imap-send-command-wait
881 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
882 (system-name)) "\"")))))
884 ;;; Compiler directives.
886 (defvar imap-sasl-client
)
887 (defvar imap-sasl-step
)
889 (defun imap-sasl-make-mechanisms (buffer)
892 (let ((name (symbol-name sym
)))
893 (if (and (> (length name
) 5)
894 (string-equal "AUTH=" (substring name
0 5 )))
895 (setq mecs
(cons (substring name
5) mecs
)))))
896 (imap-capability nil buffer
))
899 (declare-function sasl-find-mechanism
"sasl" (mechanism))
900 (declare-function sasl-mechanism-name
"sasl" (mechanism))
901 (declare-function sasl-make-client
"sasl" (mechanism name service server
))
902 (declare-function sasl-next-step
"sasl" (client step
))
903 (declare-function sasl-step-data
"sasl" (step))
904 (declare-function sasl-step-set-data
"sasl" (step data
))
906 (defun imap-sasl-auth-p (buffer)
907 (and (condition-case ()
910 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer
))))
912 (defun imap-sasl-auth (buffer)
913 "Login to server using the SASL method."
914 (message "imap: Authenticating using SASL...")
915 (with-current-buffer buffer
916 (make-local-variable 'imap-username
)
917 (make-local-variable 'imap-sasl-client
)
918 (make-local-variable 'imap-sasl-step
)
919 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer
)))
922 (setq user
(or imap-username
923 (read-from-minibuffer
924 (concat "IMAP username for " imap-server
" using SASL "
925 (sasl-mechanism-name mechanism
) ": ")
926 (or user imap-default-user
))))
928 (setq imap-sasl-client
(sasl-make-client mechanism user
"imap2" imap-server
)
929 imap-sasl-step
(sasl-next-step imap-sasl-client nil
))
930 (let ((tag (imap-send-command
931 (if (sasl-step-data imap-sasl-step
)
932 (format "AUTHENTICATE %s %s"
933 (sasl-mechanism-name mechanism
)
934 (sasl-step-data imap-sasl-step
))
935 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism
)))
937 (while (eq (imap-wait-for-tag tag
) 'INCOMPLETE
)
938 (sasl-step-set-data imap-sasl-step
(base64-decode-string imap-continuation
))
939 (setq imap-continuation nil
940 imap-sasl-step
(sasl-next-step imap-sasl-client imap-sasl-step
))
941 (imap-send-command-1 (if (sasl-step-data imap-sasl-step
)
942 (base64-encode-string (sasl-step-data imap-sasl-step
) t
)
944 (if (imap-ok-p (imap-wait-for-tag tag
))
945 (setq imap-username user
947 (message "Login failed...")
951 (defun imap-digest-md5-p (buffer)
952 (and (imap-capability 'AUTH
=DIGEST-MD5 buffer
)
954 (require 'digest-md5
)
957 (defun imap-digest-md5-auth (buffer)
958 "Login to server using the AUTH DIGEST-MD5 method."
959 (message "imap: Authenticating using DIGEST-MD5...")
960 (imap-interactive-login
962 (lambda (user passwd
)
966 "AUTHENTICATE DIGEST-MD5"
968 (digest-md5-parse-digest-challenge
969 (base64-decode-string challenge
))
971 (digest-md5-digest-uri
972 "imap" (digest-md5-challenge 'realm
)))
974 (digest-md5-digest-response
975 user passwd digest-uri
)))
976 (base64-encode-string response
'no-line-break
))))
978 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
980 (setq imap-continuation nil
)
981 (imap-send-command-1 "")
982 (imap-ok-p (imap-wait-for-tag tag
)))))))
986 (defun imap-open-1 (buffer)
987 (with-current-buffer buffer
989 (setq imap-current-mailbox nil
990 imap-current-message nil
992 imap-process
(condition-case ()
993 (funcall (nth 2 (assq imap-stream
995 "imap" buffer imap-server imap-port
)
998 (set-process-filter imap-process
'imap-arrival-filter
)
999 (set-process-sentinel imap-process
'imap-sentinel
)
1000 (while (and (eq imap-state
'initial
)
1001 (memq (process-status imap-process
) '(open run
)))
1002 (message "Waiting for response from %s..." imap-server
)
1003 (accept-process-output imap-process
1))
1004 (message "Waiting for response from %s...done" imap-server
)
1005 (and (memq (process-status imap-process
) '(open run
))
1008 (defun imap-open (server &optional port stream auth buffer
)
1009 "Open an IMAP connection to host SERVER at PORT returning a buffer.
1010 If PORT is unspecified, a default value is used (143 except
1011 for SSL which use 993).
1012 STREAM indicates the stream to use, see `imap-streams' for available
1013 streams. If nil, it choices the best stream the server is capable of.
1014 AUTH indicates authenticator to use, see `imap-authenticators' for
1015 available authenticators. If nil, it choices the best stream the
1016 server is capable of.
1017 BUFFER can be a buffer or a name of a buffer, which is created if
1018 necessary. If nil, the buffer name is generated."
1019 (setq buffer
(or buffer
(format " *imap* %s:%d" server
(or port
0))))
1020 (with-current-buffer (get-buffer-create buffer
)
1021 (if (imap-opened buffer
)
1022 (imap-close buffer
))
1023 (mapc 'make-local-variable imap-local-variables
)
1024 (imap-disable-multibyte)
1025 (buffer-disable-undo)
1026 (setq imap-server
(or server imap-server
))
1027 (setq imap-port
(or port imap-port
))
1028 (setq imap-auth
(or auth imap-auth
))
1029 (setq imap-stream
(or stream imap-stream
))
1030 (message "imap: Connecting to %s..." imap-server
)
1031 (if (null (let ((imap-stream (or imap-stream imap-default-stream
)))
1032 (imap-open-1 buffer
)))
1034 (message "imap: Connecting to %s...failed" imap-server
)
1036 (when (null imap-stream
)
1037 ;; Need to choose stream.
1038 (let ((streams imap-streams
))
1039 (while (setq stream
(pop streams
))
1040 ;; OK to use this stream?
1041 (when (funcall (nth 1 (assq stream imap-stream-alist
)) buffer
)
1043 (if (not (eq imap-default-stream stream
))
1044 (with-current-buffer (get-buffer-create
1045 (generate-new-buffer-name " *temp*"))
1046 (mapc 'make-local-variable imap-local-variables
)
1047 (imap-disable-multibyte)
1048 (buffer-disable-undo)
1049 (setq imap-server
(or server imap-server
))
1050 (setq imap-port
(or port imap-port
))
1051 (setq imap-auth
(or auth imap-auth
))
1052 (message "imap: Reconnecting with stream `%s'..." stream
)
1053 (if (null (let ((imap-stream stream
))
1054 (imap-open-1 (current-buffer))))
1056 (kill-buffer (current-buffer))
1058 "imap: Reconnecting with stream `%s'...failed"
1060 ;; We're done, kill the first connection
1062 (let ((name (if (stringp buffer
)
1064 (buffer-name buffer
))))
1065 (kill-buffer buffer
)
1066 (rename-buffer name
)
1067 ;; set the passed buffer to the current one,
1068 ;; so that (imap-opened buffer) later will work
1069 (setq buffer
(current-buffer)))
1070 (message "imap: Reconnecting with stream `%s'...done"
1072 (setq imap-stream stream
)
1073 (setq imap-capability nil
)
1074 (setq streams nil
)))
1076 (message "imap: Connecting to %s...done" imap-server
)
1077 (setq imap-stream stream
)
1078 (setq imap-capability nil
)
1079 (setq streams nil
))))))
1080 (when (imap-opened buffer
)
1081 (setq imap-mailbox-data
(make-vector imap-mailbox-prime
0)))
1082 ;; (debug "opened+state+auth+buffer" (imap-opened buffer) imap-state imap-auth buffer)
1086 (defcustom imap-ping-server t
1087 "If non-nil, check if IMAP is open.
1088 See the function `imap-ping-server'."
1089 :version
"23.1" ;; No Gnus
1093 (defun imap-opened (&optional buffer
)
1094 "Return non-nil if connection to imap server in BUFFER is open.
1095 If BUFFER is nil then the current buffer is used."
1096 (and (setq buffer
(get-buffer (or buffer
(current-buffer))))
1097 (buffer-live-p buffer
)
1098 (with-current-buffer buffer
1100 (memq (process-status imap-process
) '(open run
))
1101 (if imap-ping-server
1105 (defun imap-ping-server (&optional buffer
)
1106 "Ping the IMAP server in BUFFER with a \"NOOP\" command.
1107 Return non-nil if the server responds, and nil if it does not
1108 respond. If BUFFER is nil, the current buffer is used."
1110 (imap-ok-p (imap-send-command-wait "NOOP" buffer
))
1113 (defun imap-authenticate (&optional user passwd buffer
)
1114 "Authenticate to server in BUFFER, using current buffer if nil.
1115 It uses the authenticator specified when opening the server.
1117 Optional arguments USER and PASSWD specify the username and
1118 password to use if the authenticator requires a username and/or
1119 password. If omitted or nil, the authenticator may query the
1120 user for a username and/or password."
1121 (with-current-buffer (or buffer
(current-buffer))
1122 (if (not (eq imap-state
'nonauth
))
1123 (or (eq imap-state
'auth
)
1124 (eq imap-state
'selected
)
1125 (eq imap-state
'examine
))
1126 (make-local-variable 'imap-username
)
1127 (make-local-variable 'imap-password
)
1128 (make-local-variable 'imap-last-authenticator
)
1129 (when user
(setq imap-username user
))
1130 (when passwd
(setq imap-password passwd
))
1132 (and (setq imap-last-authenticator
1133 (assq imap-auth imap-authenticator-alist
))
1134 (funcall (nth 2 imap-last-authenticator
) (current-buffer))
1135 (setq imap-state
'auth
))
1136 ;; Choose authenticator.
1137 (let ((auths imap-authenticators
)
1139 (while (setq auth
(pop auths
))
1140 ;; OK to use authenticator?
1141 (setq imap-last-authenticator
1142 (assq auth imap-authenticator-alist
))
1143 (when (funcall (nth 1 imap-last-authenticator
) (current-buffer))
1144 (message "imap: Authenticating to `%s' using `%s'..."
1146 (setq imap-auth auth
)
1147 (if (funcall (nth 2 imap-last-authenticator
) (current-buffer))
1149 (message "imap: Authenticating to `%s' using `%s'...done"
1151 ;; set imap-state correctly on successful auth attempt
1152 (setq imap-state
'auth
)
1153 ;; stop iterating through the authenticator list
1155 (message "imap: Authenticating to `%s' using `%s'...failed"
1156 imap-server auth
)))))
1159 (defun imap-close (&optional buffer
)
1160 "Close connection to server in BUFFER.
1161 If BUFFER is nil, the current buffer is used."
1162 (with-current-buffer (or buffer
(current-buffer))
1167 (when (and imap-process
1168 (memq (process-status imap-process
) '(open run
)))
1169 (delete-process imap-process
))
1170 (setq imap-current-mailbox nil
1171 imap-current-message nil
1176 (defun imap-capability (&optional identifier buffer
)
1177 "Return a list of identifiers which server in BUFFER support.
1178 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1179 If BUFFER is nil, the current buffer is assumed."
1180 (with-current-buffer (or buffer
(current-buffer))
1181 (unless imap-capability
1182 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1183 (setq imap-capability
'(IMAP2))))
1185 (memq (intern (upcase (symbol-name identifier
))) imap-capability
)
1188 (defun imap-id (&optional list-of-values buffer
)
1189 "Identify client to server in BUFFER, and return server identity.
1190 LIST-OF-VALUES is nil, or a plist with identifier and value
1191 strings to send to the server to identify the client.
1193 Return a list of identifiers which server in BUFFER support, or
1194 nil if it doesn't support ID or returns no information.
1196 If BUFFER is nil, the current buffer is assumed."
1197 (with-current-buffer (or buffer
(current-buffer))
1198 (when (and (imap-capability 'ID
)
1199 (imap-ok-p (imap-send-command-wait
1200 (if (null list-of-values
)
1202 (concat "ID (" (mapconcat (lambda (el)
1203 (concat "\"" el
"\""))
1208 (defun imap-namespace (&optional buffer
)
1209 "Return a namespace hierarchy at server in BUFFER.
1210 If BUFFER is nil, the current buffer is assumed."
1211 (with-current-buffer (or buffer
(current-buffer))
1212 (unless imap-namespace
1213 (when (imap-capability 'NAMESPACE
)
1214 (imap-send-command-wait "NAMESPACE")))
1217 (defun imap-send-command-wait (command &optional buffer
)
1218 (imap-wait-for-tag (imap-send-command command buffer
) buffer
))
1220 (defun imap-logout (&optional buffer
)
1221 (or buffer
(setq buffer
(current-buffer)))
1222 (if imap-logout-timeout
1223 (with-timeout (imap-logout-timeout
1225 (with-current-buffer buffer
1226 (delete-process imap-process
))
1228 (imap-send-command "LOGOUT" buffer
))
1229 (imap-send-command "LOGOUT" buffer
)))
1231 (defun imap-logout-wait (&optional buffer
)
1232 (or buffer
(setq buffer
(current-buffer)))
1233 (if imap-logout-timeout
1234 (with-timeout (imap-logout-timeout
1236 (with-current-buffer buffer
1237 (delete-process imap-process
))
1239 (imap-send-command-wait "LOGOUT" buffer
))
1240 (imap-send-command-wait "LOGOUT" buffer
)))
1243 ;; Mailbox functions:
1245 (defun imap-mailbox-put (propname value
&optional mailbox buffer
)
1246 (with-current-buffer (or buffer
(current-buffer))
1247 (if imap-mailbox-data
1248 (put (intern (or mailbox imap-current-mailbox
) imap-mailbox-data
)
1250 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1251 propname value mailbox
(current-buffer)))
1254 (defsubst imap-mailbox-get-1
(propname &optional mailbox
)
1255 (get (intern-soft (or mailbox imap-current-mailbox
) imap-mailbox-data
)
1258 (defun imap-mailbox-get (propname &optional mailbox buffer
)
1259 (let ((mailbox (imap-utf7-encode mailbox
)))
1260 (with-current-buffer (or buffer
(current-buffer))
1261 (imap-mailbox-get-1 propname
(or mailbox imap-current-mailbox
)))))
1263 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer
)
1264 (with-current-buffer (or buffer
(current-buffer))
1268 (push (funcall func
(if mailbox-decoder
1269 (funcall mailbox-decoder
(symbol-name s
))
1270 (symbol-name s
))) result
))
1274 (defun imap-mailbox-map (func &optional buffer
)
1275 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1276 Function should take a mailbox name (a string) as
1278 (imap-mailbox-map-1 func
'imap-utf7-decode buffer
))
1280 (defun imap-current-mailbox (&optional buffer
)
1281 (with-current-buffer (or buffer
(current-buffer))
1282 (imap-utf7-decode imap-current-mailbox
)))
1284 (defun imap-current-mailbox-p-1 (mailbox &optional examine
)
1285 (and (string= mailbox imap-current-mailbox
)
1287 (eq imap-state
'examine
))
1289 (eq imap-state
'selected
)))))
1291 (defun imap-current-mailbox-p (mailbox &optional examine buffer
)
1292 (with-current-buffer (or buffer
(current-buffer))
1293 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox
) examine
)))
1295 (defun imap-mailbox-select-1 (mailbox &optional examine
)
1296 "Select MAILBOX on server in BUFFER.
1297 If EXAMINE is non-nil, do a read-only select."
1298 (if (imap-current-mailbox-p-1 mailbox examine
)
1299 imap-current-mailbox
1300 (setq imap-current-mailbox mailbox
)
1301 (if (imap-ok-p (imap-send-command-wait
1302 (concat (if examine
"EXAMINE" "SELECT") " \""
1305 (setq imap-message-data
(make-vector imap-message-prime
0)
1306 imap-state
(if examine
'examine
'selected
))
1307 imap-current-mailbox
)
1308 ;; Failed SELECT/EXAMINE unselects current mailbox
1309 (setq imap-current-mailbox nil
))))
1311 (defun imap-mailbox-select (mailbox &optional examine buffer
)
1312 (with-current-buffer (or buffer
(current-buffer))
1314 (imap-mailbox-select-1 (imap-utf7-encode mailbox
) examine
))))
1316 (defun imap-mailbox-examine-1 (mailbox &optional buffer
)
1317 (with-current-buffer (or buffer
(current-buffer))
1318 (imap-mailbox-select-1 mailbox
'examine
)))
1320 (defun imap-mailbox-examine (mailbox &optional buffer
)
1321 "Examine MAILBOX on server in BUFFER."
1322 (imap-mailbox-select mailbox
'examine buffer
))
1324 (defun imap-mailbox-unselect (&optional buffer
)
1325 "Close current folder in BUFFER, without expunging articles."
1326 (with-current-buffer (or buffer
(current-buffer))
1327 (when (or (eq imap-state
'auth
)
1328 (and (imap-capability 'UNSELECT
)
1329 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1331 (imap-send-command-wait (concat "EXAMINE \""
1332 imap-current-mailbox
1334 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1335 (setq imap-current-mailbox nil
1336 imap-message-data nil
1340 (defun imap-mailbox-expunge (&optional asynch buffer
)
1341 "Expunge articles in current folder in BUFFER.
1342 If ASYNCH, do not wait for successful completion of the command.
1343 If BUFFER is nil the current buffer is assumed."
1344 (with-current-buffer (or buffer
(current-buffer))
1345 (when (and imap-current-mailbox
(not (eq imap-state
'examine
)))
1347 (imap-send-command "EXPUNGE")
1348 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1350 (defun imap-mailbox-close (&optional asynch buffer
)
1351 "Expunge articles and close current folder in BUFFER.
1352 If ASYNCH, do not wait for successful completion of the command.
1353 If BUFFER is nil the current buffer is assumed."
1354 (with-current-buffer (or buffer
(current-buffer))
1355 (when imap-current-mailbox
1357 (imap-add-callback (imap-send-command "CLOSE")
1358 `(lambda (tag status
)
1359 (message "IMAP mailbox `%s' closed... %s"
1360 imap-current-mailbox status
)
1361 (when (eq ,imap-current-mailbox
1362 imap-current-mailbox
)
1363 ;; Don't wipe out data if another mailbox
1365 (setq imap-current-mailbox nil
1366 imap-message-data nil
1367 imap-state
'auth
))))
1368 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1369 (setq imap-current-mailbox nil
1370 imap-message-data nil
1374 (defun imap-mailbox-create-1 (mailbox)
1375 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox
"\""))))
1377 (defun imap-mailbox-create (mailbox &optional buffer
)
1378 "Create MAILBOX on server in BUFFER.
1379 If BUFFER is nil the current buffer is assumed."
1380 (with-current-buffer (or buffer
(current-buffer))
1381 (imap-mailbox-create-1 (imap-utf7-encode mailbox
))))
1383 (defun imap-mailbox-delete (mailbox &optional buffer
)
1384 "Delete MAILBOX on server in BUFFER.
1385 If BUFFER is nil the current buffer is assumed."
1386 (let ((mailbox (imap-utf7-encode mailbox
)))
1387 (with-current-buffer (or buffer
(current-buffer))
1389 (imap-send-command-wait (list "DELETE \"" mailbox
"\""))))))
1391 (defun imap-mailbox-rename (oldname newname
&optional buffer
)
1392 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1393 If BUFFER is nil the current buffer is assumed."
1394 (let ((oldname (imap-utf7-encode oldname
))
1395 (newname (imap-utf7-encode newname
)))
1396 (with-current-buffer (or buffer
(current-buffer))
1398 (imap-send-command-wait (list "RENAME \"" oldname
"\" "
1399 "\"" newname
"\""))))))
1401 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer
)
1402 "Return a list of subscribed mailboxes on server in BUFFER.
1403 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1404 non-nil, a hierarchy delimiter is added to root. REFERENCE is an
1405 implementation-specific string that has to be passed to lsub command."
1406 (with-current-buffer (or buffer
(current-buffer))
1407 ;; Make sure we know the hierarchy separator for root's hierarchy
1408 (when (and add-delimiter
(null (imap-mailbox-get-1 'delimiter root
)))
1409 (imap-send-command-wait (concat "LIST \"" reference
"\" \""
1410 (imap-utf7-encode root
) "\"")))
1411 ;; clear list data (NB not delimiter and other stuff)
1412 (imap-mailbox-map-1 (lambda (mailbox)
1413 (imap-mailbox-put 'lsub nil mailbox
)))
1415 (imap-send-command-wait
1416 (concat "LSUB \"" reference
"\" \"" (imap-utf7-encode root
)
1417 (and add-delimiter
(imap-mailbox-get-1 'delimiter root
))
1420 (imap-mailbox-map-1 (lambda (mailbox)
1421 (when (imap-mailbox-get-1 'lsub mailbox
)
1422 (push (imap-utf7-decode mailbox
) out
))))
1425 (defun imap-mailbox-list (root &optional reference add-delimiter buffer
)
1426 "Return a list of mailboxes matching ROOT on server in BUFFER.
1427 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1428 root. REFERENCE is an implementation-specific string that has to be
1429 passed to list command."
1430 (with-current-buffer (or buffer
(current-buffer))
1431 ;; Make sure we know the hierarchy separator for root's hierarchy
1432 (when (and add-delimiter
(null (imap-mailbox-get-1 'delimiter root
)))
1433 (imap-send-command-wait (concat "LIST \"" reference
"\" \""
1434 (imap-utf7-encode root
) "\"")))
1435 ;; clear list data (NB not delimiter and other stuff)
1436 (imap-mailbox-map-1 (lambda (mailbox)
1437 (imap-mailbox-put 'list nil mailbox
)))
1439 (imap-send-command-wait
1440 (concat "LIST \"" reference
"\" \"" (imap-utf7-encode root
)
1441 (and add-delimiter
(imap-mailbox-get-1 'delimiter root
))
1444 (imap-mailbox-map-1 (lambda (mailbox)
1445 (when (imap-mailbox-get-1 'list mailbox
)
1446 (push (imap-utf7-decode mailbox
) out
))))
1449 (defun imap-mailbox-subscribe (mailbox &optional buffer
)
1450 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1451 Returns non-nil if successful."
1452 (with-current-buffer (or buffer
(current-buffer))
1453 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1454 (imap-utf7-encode mailbox
)
1457 (defun imap-mailbox-unsubscribe (mailbox &optional buffer
)
1458 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1459 Returns non-nil if successful."
1460 (with-current-buffer (or buffer
(current-buffer))
1461 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1462 (imap-utf7-encode mailbox
)
1465 (defun imap-mailbox-status (mailbox items
&optional buffer
)
1466 "Get status items ITEM in MAILBOX from server in BUFFER.
1467 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1468 the STATUS data items -- i.e. `messages', `recent', `uidnext', `uidvalidity',
1469 or `unseen'. If ITEMS is a list of symbols, a list of values is
1470 returned, if ITEMS is a symbol only its value is returned."
1471 (with-current-buffer (or buffer
(current-buffer))
1473 (imap-send-command-wait (list "STATUS \""
1474 (imap-utf7-encode mailbox
)
1482 (mapcar (lambda (item)
1483 (imap-mailbox-get item mailbox
))
1485 (imap-mailbox-get items mailbox
)))))
1487 (defun imap-mailbox-status-asynch (mailbox items
&optional buffer
)
1488 "Send status item requests ITEMS on MAILBOX to server in BUFFER.
1489 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1490 the STATUS data items -- i.e., `messages', `recent', `uidnext', `uidvalidity'
1491 or `unseen'. The IMAP command tag is returned."
1492 (with-current-buffer (or buffer
(current-buffer))
1493 (imap-send-command (list "STATUS \""
1494 (imap-utf7-encode mailbox
)
1502 (defun imap-mailbox-acl-get (&optional mailbox buffer
)
1503 "Get ACL on MAILBOX from server in BUFFER."
1504 (let ((mailbox (imap-utf7-encode mailbox
)))
1505 (with-current-buffer (or buffer
(current-buffer))
1507 (imap-send-command-wait (list "GETACL \""
1508 (or mailbox imap-current-mailbox
)
1510 (imap-mailbox-get-1 'acl
(or mailbox imap-current-mailbox
))))))
1512 (defun imap-mailbox-acl-set (identifier rights
&optional mailbox buffer
)
1513 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1514 (let ((mailbox (imap-utf7-encode mailbox
)))
1515 (with-current-buffer (or buffer
(current-buffer))
1517 (imap-send-command-wait (list "SETACL \""
1518 (or mailbox imap-current-mailbox
)
1524 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer
)
1525 "Remove <id,rights> pairs for IDENTIFIER from MAILBOX on server in BUFFER."
1526 (let ((mailbox (imap-utf7-encode mailbox
)))
1527 (with-current-buffer (or buffer
(current-buffer))
1529 (imap-send-command-wait (list "DELETEACL \""
1530 (or mailbox imap-current-mailbox
)
1535 ;; Message functions:
1537 (defun imap-current-message (&optional buffer
)
1538 (with-current-buffer (or buffer
(current-buffer))
1539 imap-current-message
))
1541 (defun imap-list-to-message-set (list)
1542 (mapconcat (lambda (item)
1543 (number-to-string item
))
1549 (defun imap-range-to-message-set (range)
1554 (car item
) (cdr item
))
1555 (format "%d" item
)))
1556 (if (and (listp range
) (not (listp (cdr range
))))
1557 (list range
) ;; make (1 . 2) into ((1 . 2))
1561 (defun imap-fetch-asynch (uids props
&optional nouidfetch buffer
)
1562 (with-current-buffer (or buffer
(current-buffer))
1563 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch
"" "UID ")
1565 (imap-list-to-message-set uids
)
1569 (defun imap-fetch (uids props
&optional receive nouidfetch buffer
)
1570 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1571 UIDS can be a string, number or a list of numbers. If RECEIVE is
1572 non-nil, return these properties."
1573 (with-current-buffer (or buffer
(current-buffer))
1574 (when (imap-ok-p (imap-send-command-wait
1575 (format "%sFETCH %s %s" (if nouidfetch
"" "UID ")
1577 (imap-list-to-message-set uids
)
1580 (if (or (null receive
) (stringp uids
))
1583 (mapcar (lambda (uid)
1585 (mapcar (lambda (prop)
1586 (imap-message-get uid prop
))
1588 (imap-message-get uid receive
)))
1590 (imap-message-get uids receive
))))))
1592 (defun imap-message-put (uid propname value
&optional buffer
)
1593 (with-current-buffer (or buffer
(current-buffer))
1594 (if imap-message-data
1595 (put (intern (number-to-string uid
) imap-message-data
)
1597 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1598 uid propname value
(current-buffer)))
1601 (defun imap-message-get (uid propname
&optional buffer
)
1602 (with-current-buffer (or buffer
(current-buffer))
1603 (get (intern-soft (number-to-string uid
) imap-message-data
)
1606 (defun imap-message-map (func propname
&optional buffer
)
1607 "Map a function across each message in `imap-message-data', returning a list."
1608 (with-current-buffer (or buffer
(current-buffer))
1612 (push (funcall func
(get s
'UID
) (get s propname
)) result
))
1616 (defmacro imap-message-envelope-date
(uid &optional buffer
)
1617 `(with-current-buffer (or ,buffer
(current-buffer))
1618 (elt (imap-message-get ,uid
'ENVELOPE
) 0)))
1620 (defmacro imap-message-envelope-subject
(uid &optional buffer
)
1621 `(with-current-buffer (or ,buffer
(current-buffer))
1622 (elt (imap-message-get ,uid
'ENVELOPE
) 1)))
1624 (defmacro imap-message-envelope-from
(uid &optional buffer
)
1625 `(with-current-buffer (or ,buffer
(current-buffer))
1626 (elt (imap-message-get ,uid
'ENVELOPE
) 2)))
1628 (defmacro imap-message-envelope-sender
(uid &optional buffer
)
1629 `(with-current-buffer (or ,buffer
(current-buffer))
1630 (elt (imap-message-get ,uid
'ENVELOPE
) 3)))
1632 (defmacro imap-message-envelope-reply-to
(uid &optional buffer
)
1633 `(with-current-buffer (or ,buffer
(current-buffer))
1634 (elt (imap-message-get ,uid
'ENVELOPE
) 4)))
1636 (defmacro imap-message-envelope-to
(uid &optional buffer
)
1637 `(with-current-buffer (or ,buffer
(current-buffer))
1638 (elt (imap-message-get ,uid
'ENVELOPE
) 5)))
1640 (defmacro imap-message-envelope-cc
(uid &optional buffer
)
1641 `(with-current-buffer (or ,buffer
(current-buffer))
1642 (elt (imap-message-get ,uid
'ENVELOPE
) 6)))
1644 (defmacro imap-message-envelope-bcc
(uid &optional buffer
)
1645 `(with-current-buffer (or ,buffer
(current-buffer))
1646 (elt (imap-message-get ,uid
'ENVELOPE
) 7)))
1648 (defmacro imap-message-envelope-in-reply-to
(uid &optional buffer
)
1649 `(with-current-buffer (or ,buffer
(current-buffer))
1650 (elt (imap-message-get ,uid
'ENVELOPE
) 8)))
1652 (defmacro imap-message-envelope-message-id
(uid &optional buffer
)
1653 `(with-current-buffer (or ,buffer
(current-buffer))
1654 (elt (imap-message-get ,uid
'ENVELOPE
) 9)))
1656 (defmacro imap-message-body
(uid &optional buffer
)
1657 `(with-current-buffer (or ,buffer
(current-buffer))
1658 (imap-message-get ,uid
'BODY
)))
1660 ;; FIXME: Should this try to use CHARSET? -- fx
1661 (defun imap-search (predicate &optional buffer
)
1662 (with-current-buffer (or buffer
(current-buffer))
1663 (imap-mailbox-put 'search
'dummy
)
1664 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate
)))
1665 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox
) 'dummy
)
1667 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1669 (imap-mailbox-get-1 'search imap-current-mailbox
)))))
1671 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer
)
1672 "Return t if FLAG can be permanently saved on articles.
1673 MAILBOX specifies a mailbox on the server in BUFFER."
1674 (with-current-buffer (or buffer
(current-buffer))
1675 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox
))
1676 (member flag
(imap-mailbox-get 'permanentflags mailbox
)))))
1678 (defun imap-message-flags-set (articles flags
&optional silent buffer
)
1679 (when (and articles flags
)
1680 (with-current-buffer (or buffer
(current-buffer))
1681 (imap-ok-p (imap-send-command-wait
1682 (concat "UID STORE " articles
1683 " FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1685 (defun imap-message-flags-del (articles flags
&optional silent buffer
)
1686 (when (and articles flags
)
1687 (with-current-buffer (or buffer
(current-buffer))
1688 (imap-ok-p (imap-send-command-wait
1689 (concat "UID STORE " articles
1690 " -FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1692 (defun imap-message-flags-add (articles flags
&optional silent buffer
)
1693 (when (and articles flags
)
1694 (with-current-buffer (or buffer
(current-buffer))
1695 (imap-ok-p (imap-send-command-wait
1696 (concat "UID STORE " articles
1697 " +FLAGS" (if silent
".SILENT") " (" flags
")"))))))
1699 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/65317/focus=65343
1700 ;; Signal an error if we'd get an integer overflow.
1702 ;; FIXME: Identify relevant calls to `string-to-number' and replace them with
1703 ;; `imap-string-to-integer'.
1704 (defun imap-string-to-integer (string &optional base
)
1705 (let ((number (string-to-number string base
)))
1706 (if (> number most-positive-fixnum
)
1708 (format "String %s cannot be converted to a Lisp integer" number
))
1711 (defun imap-fetch-safe (uids props
&optional receive nouidfetch buffer
)
1712 "Like `imap-fetch', but DTRT with Exchange 2007 bug.
1713 However, UIDS here is a cons, where the car is the canonical form
1714 of the UIDS specification, and the cdr is the one which works with
1715 Exchange 2007 or, potentially, other buggy servers.
1716 See `imap-enable-exchange-bug-workaround'."
1717 ;; The first time we get here for a given, we'll try the canonical
1718 ;; form. If we get the known error from the buggy server, set the
1719 ;; flag buffer-locally (to account for connections to multiple
1720 ;; servers), then re-try with the alternative UIDS spec. We don't
1721 ;; unconditionally use the alternative form, since the
1722 ;; currently-used alternatives are seriously inefficient with some
1723 ;; servers (although they are valid).
1725 ;; FIXME: Maybe it would be cleaner to have a flag to not signal
1726 ;; the error (which otherwise gives a message), and test
1727 ;; `imap-failed-tags'. Also, Other IMAP clients use other forms of
1728 ;; request which work with Exchange, e.g. Claws does "UID FETCH 1:*
1729 ;; (UID)" rather than "FETCH UID 1,*". Is there a good reason not
1731 (condition-case data
1732 ;; Binding `debug-on-error' allows us to get the error from
1733 ;; `imap-parse-response' -- it's normally caught by Emacs around
1734 ;; execution of a process filter.
1735 (let ((debug-on-error t
))
1736 (imap-fetch (if imap-enable-exchange-bug-workaround
1739 props receive nouidfetch buffer
))
1741 (if (and (not imap-enable-exchange-bug-workaround
)
1742 ;; This is the Exchange 2007 response. It may be more
1743 ;; robust just to check for a BAD response to the
1745 (string-match "The specified message set is invalid"
1747 (with-current-buffer (or buffer
(current-buffer))
1748 (set (make-local-variable 'imap-enable-exchange-bug-workaround
)
1750 (imap-fetch (cdr uids
) props receive nouidfetch
))
1751 (signal (car data
) (cdr data
))))))
1753 (defun imap-message-copyuid-1 (mailbox)
1754 (if (imap-capability 'UIDPLUS
)
1755 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox
))
1756 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox
))))
1757 (let ((old-mailbox imap-current-mailbox
)
1759 (imap-message-data (make-vector 2 0)))
1760 (when (imap-mailbox-examine-1 mailbox
)
1762 (and (imap-fetch-safe '("*" .
"*:*") "UID")
1763 (list (imap-mailbox-get-1 'uidvalidity mailbox
)
1764 (apply 'max
(imap-message-map
1765 (lambda (uid _prop
) uid
) 'UID
))))
1767 (imap-mailbox-select old-mailbox
(eq state
'examine
))
1768 (imap-mailbox-unselect)))))))
1770 (defun imap-message-copyuid (mailbox &optional buffer
)
1771 (with-current-buffer (or buffer
(current-buffer))
1772 (imap-message-copyuid-1 (imap-utf7-decode mailbox
))))
1774 (defun imap-message-copy (articles mailbox
1775 &optional dont-create no-copyuid buffer
)
1776 "Copy ARTICLES to MAILBOX on server in BUFFER.
1777 ARTICLES is a string message set. Create mailbox if it doesn't exist,
1778 unless DONT-CREATE is non-nil. On success, return a list with
1779 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1780 first element. The rest of list contains the saved articles' UIDs."
1782 (with-current-buffer (or buffer
(current-buffer))
1783 (let ((mailbox (imap-utf7-encode mailbox
)))
1784 (if (let ((cmd (concat "UID COPY " articles
" \"" mailbox
"\""))
1785 (imap-current-target-mailbox mailbox
))
1786 (if (imap-ok-p (imap-send-command-wait cmd
))
1788 (when (and (not dont-create
)
1789 ;; removed because of buggy Oracle server
1790 ;; that doesn't send TRYCREATE tags (which
1791 ;; is a MUST according to specifications):
1792 ;;(imap-mailbox-get-1 'trycreate mailbox)
1793 (imap-mailbox-create-1 mailbox
))
1794 (imap-ok-p (imap-send-command-wait cmd
)))))
1796 (imap-message-copyuid-1 mailbox
)))))))
1798 ;; FIXME: Amalgamate with imap-message-copyuid-1, using an extra arg, since it
1799 ;; shares most of the code? -- fx
1800 (defun imap-message-appenduid-1 (mailbox)
1801 (if (imap-capability 'UIDPLUS
)
1802 (imap-mailbox-get-1 'appenduid mailbox
)
1803 (let ((old-mailbox imap-current-mailbox
)
1805 (imap-message-data (make-vector 2 0)))
1806 (when (imap-mailbox-examine-1 mailbox
)
1808 (and (imap-fetch-safe '("*" .
"*:*") "UID")
1809 (list (imap-mailbox-get-1 'uidvalidity mailbox
)
1810 (apply 'max
(imap-message-map
1811 (lambda (uid _prop
) uid
) 'UID
))))
1813 (imap-mailbox-select old-mailbox
(eq state
'examine
))
1814 (imap-mailbox-unselect)))))))
1816 (defun imap-message-appenduid (mailbox &optional buffer
)
1817 (with-current-buffer (or buffer
(current-buffer))
1818 (imap-message-appenduid-1 (imap-utf7-encode mailbox
))))
1820 (defun imap-message-append (mailbox article
&optional _flags _date-time buffer
)
1821 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1822 FLAGS and DATE-TIME is currently not used. Return a cons holding
1823 uidvalidity of MAILBOX and UID the newly created article got, or nil
1825 (let ((mailbox (imap-utf7-encode mailbox
)))
1826 (with-current-buffer (or buffer
(current-buffer))
1827 (and (let ((imap-current-target-mailbox mailbox
))
1829 (imap-send-command-wait
1830 (list "APPEND \"" mailbox
"\" " article
))))
1831 (imap-message-appenduid-1 mailbox
)))))
1833 (defun imap-body-lines (body)
1834 "Return number of lines in article by looking at the mime bodystructure BODY."
1836 (if (stringp (car body
))
1837 (cond ((and (string= (upcase (car body
)) "TEXT")
1838 (numberp (nth 7 body
)))
1840 ((and (string= (upcase (car body
)) "MESSAGE")
1841 (numberp (nth 9 body
)))
1844 (apply '+ (mapcar 'imap-body-lines body
)))
1847 (defun imap-envelope-from (from)
1848 "Return a FROM string line."
1850 (concat (aref from
0)
1851 (if (aref from
0) " <")
1855 (if (aref from
0) ">"))))
1858 ;; Internal functions.
1860 (defun imap-add-callback (tag func
)
1861 (setq imap-callbacks
(append (list (cons tag func
)) imap-callbacks
)))
1863 (defun imap-send-command-1 (cmdstr)
1864 (setq cmdstr
(concat cmdstr imap-client-eol
))
1866 (process-send-string imap-process cmdstr
))
1868 (defun imap-send-command (command &optional buffer
)
1869 (with-current-buffer (or buffer
(current-buffer))
1870 (if (not (listp command
)) (setq command
(list command
)))
1871 (let ((tag (setq imap-tag
(1+ imap-tag
)))
1873 (setq cmdstr
(concat (number-to-string imap-tag
) " "))
1874 (while (setq cmd
(pop command
))
1875 (cond ((stringp cmd
)
1876 (setq cmdstr
(concat cmdstr cmd
)))
1878 (let ((eol imap-client-eol
)
1879 (calcfirst imap-calculate-literal-size-first
)
1881 (with-current-buffer cmd
1883 (setq size
(buffer-size)))
1884 (when (not (equal eol
"\r\n"))
1885 ;; XXX modifies buffer!
1886 (goto-char (point-min))
1887 (while (search-forward "\r\n" nil t
)
1888 (replace-match eol
)))
1890 (setq size
(buffer-size))))
1892 (concat cmdstr
(format "{%d}" size
))))
1895 (imap-send-command-1 cmdstr
)
1897 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
1898 (setq command nil
) ;; abort command if no cont-req
1899 (let ((process imap-process
))
1900 (with-current-buffer cmd
1902 (process-send-region process
(point-min)
1904 (process-send-string process imap-client-eol
))))
1905 (setq imap-continuation nil
)))
1907 (imap-send-command-1 cmdstr
)
1911 (if (not (eq (imap-wait-for-tag tag
) 'INCOMPLETE
))
1912 nil
;; abort command if no cont-req
1913 (cons (funcall cmd imap-continuation
)
1915 (setq imap-continuation nil
)))
1917 (error "Unknown command type"))))
1919 (imap-send-command-1 cmdstr
))
1922 (defun imap-wait-for-tag (tag &optional buffer
)
1923 (with-current-buffer (or buffer
(current-buffer))
1924 (let (imap-have-messaged)
1925 (while (and (null imap-continuation
)
1926 (memq (process-status imap-process
) '(open run
))
1927 (< imap-reached-tag tag
))
1928 (let ((len (/ (buffer-size) 1024))
1931 (setq imap-have-messaged t
)
1932 (message "imap read: %dk" len
))
1933 (accept-process-output imap-process
1934 (truncate imap-read-timeout
)
1935 (truncate (* (- imap-read-timeout
1936 (truncate imap-read-timeout
))
1938 ;; A process can die _before_ we have processed everything it
1939 ;; has to say. Moreover, this can happen in between the call to
1940 ;; accept-process-output and the call to process-status in an
1941 ;; iteration of the loop above.
1942 (when (and (null imap-continuation
)
1943 (< imap-reached-tag tag
))
1944 (accept-process-output imap-process
0 0))
1945 (when imap-have-messaged
1947 (and (memq (process-status imap-process
) '(open run
))
1948 (or (assq tag imap-failed-tags
)
1949 (if imap-continuation
1953 (defun imap-sentinel (process _string
)
1954 (delete-process process
))
1956 (defun imap-find-next-line ()
1957 "Return point at end of current line, taking into account literals.
1958 Return nil if no complete line has arrived."
1959 (when (re-search-forward (concat imap-server-eol
"\\|{\\([0-9]+\\)}"
1962 (if (match-string 1)
1963 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1965 (goto-char (+ (point) (string-to-number (match-string 1))))
1966 (imap-find-next-line))
1969 (defun imap-arrival-filter (proc string
)
1970 "IMAP process filter."
1971 ;; Sometimes, we are called even though the process has died.
1972 ;; Better abstain from doing stuff in that case.
1973 (when (buffer-name (process-buffer proc
))
1974 (with-current-buffer (process-buffer proc
)
1975 (goto-char (point-max))
1979 (goto-char (point-min))
1980 (while (setq end
(imap-find-next-line))
1982 (narrow-to-region (point-min) end
)
1983 (delete-char (- (length imap-server-eol
)))
1984 (goto-char (point-min))
1986 (cond ((eq imap-state
'initial
)
1987 (imap-parse-greeting))
1988 ((or (eq imap-state
'auth
)
1989 (eq imap-state
'nonauth
)
1990 (eq imap-state
'selected
)
1991 (eq imap-state
'examine
))
1992 (imap-parse-response))
1994 (message "Unknown state %s in arrival filter"
1996 (delete-region (point-min) (point-max)))))))))
2001 (defsubst imap-forward
()
2002 (or (eobp) (forward-char)))
2005 ;; ; Unsigned 32-bit integer
2006 ;; ; (0 <= n < 4,294,967,296)
2008 (defsubst imap-parse-number
()
2009 (when (looking-at "[0-9]+")
2011 (string-to-number (match-string 0))
2012 (goto-char (match-end 0)))))
2014 ;; literal = "{" number "}" CRLF *CHAR8
2015 ;; ; Number represents the number of CHAR8s
2017 (defsubst imap-parse-literal
()
2018 (when (looking-at "{\\([0-9]+\\)}\r\n")
2019 (let ((pos (match-end 0))
2020 (len (string-to-number (match-string 1))))
2021 (if (< (point-max) (+ pos len
))
2023 (goto-char (+ pos len
))
2024 (buffer-substring pos
(+ pos len
))))))
2026 ;; string = quoted / literal
2028 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
2030 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2031 ;; "\" quoted-specials
2033 ;; quoted-specials = DQUOTE / "\"
2035 ;; TEXT-CHAR = <any CHAR except CR and LF>
2037 (defsubst imap-parse-string
()
2038 (cond ((eq (char-after) ?
\")
2040 (let ((p (point)) (name ""))
2041 (skip-chars-forward "^\"\\\\")
2042 (setq name
(buffer-substring p
(point)))
2043 (while (eq (char-after) ?
\\)
2044 (setq p
(1+ (point)))
2046 (skip-chars-forward "^\"\\\\")
2047 (setq name
(concat name
(buffer-substring p
(point)))))
2050 ((eq (char-after) ?
{)
2051 (imap-parse-literal))))
2055 (defsubst imap-parse-nil
()
2056 (if (looking-at "NIL")
2057 (goto-char (match-end 0))))
2059 ;; nstring = string / nil
2061 (defsubst imap-parse-nstring
()
2062 (or (imap-parse-string)
2063 (and (imap-parse-nil)
2066 ;; astring = atom / string
2068 ;; atom = 1*ATOM-CHAR
2070 ;; ATOM-CHAR = <any CHAR except atom-specials>
2072 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2075 ;; list-wildcards = "%" / "*"
2077 ;; quoted-specials = DQUOTE / "\"
2079 (defsubst imap-parse-astring
()
2080 (or (imap-parse-string)
2081 (buffer-substring (point)
2082 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t
)
2083 (goto-char (1- (match-end 0)))
2087 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2090 ;; addr-adl = nstring
2091 ;; ; Holds route from [RFC-822] route-addr if
2094 ;; addr-host = nstring
2095 ;; ; nil indicates [RFC-822] group syntax.
2096 ;; ; Otherwise, holds [RFC-822] domain name
2098 ;; addr-mailbox = nstring
2099 ;; ; nil indicates end of [RFC-822] group; if
2100 ;; ; non-nil and addr-host is nil, holds
2101 ;; ; [RFC-822] group name.
2102 ;; ; Otherwise, holds [RFC-822] local-part
2103 ;; ; after removing [RFC-822] quoting
2105 ;; addr-name = nstring
2106 ;; ; If non-nil, holds phrase from [RFC-822]
2107 ;; ; mailbox after removing [RFC-822] quoting
2110 (defsubst imap-parse-address
()
2112 (when (eq (char-after) ?\
()
2114 (setq address
(vector (prog1 (imap-parse-nstring)
2116 (prog1 (imap-parse-nstring)
2118 (prog1 (imap-parse-nstring)
2120 (imap-parse-nstring)))
2121 (when (eq (char-after) ?\
))
2125 ;; address-list = "(" 1*address ")" / nil
2129 (defsubst imap-parse-address-list
()
2130 (if (eq (char-after) ?\
()
2131 (let (address addresses
)
2133 (while (and (not (eq (char-after) ?\
)))
2134 ;; next line for MS Exchange bug
2135 (progn (and (eq (char-after) ?
) (imap-forward)) t
)
2136 (setq address
(imap-parse-address)))
2137 (setq addresses
(cons address addresses
)))
2138 (when (eq (char-after) ?\
))
2140 (nreverse addresses
)))
2141 ;; With assert, the code might not be eval'd.
2142 ;; (cl-assert (imap-parse-nil) t "In imap-parse-address-list")
2145 ;; mailbox = "INBOX" / astring
2146 ;; ; INBOX is case-insensitive. All case variants of
2147 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2148 ;; ; not as an astring. An astring which consists of
2149 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2150 ;; ; is considered to be INBOX and not an astring.
2151 ;; ; Refer to section 5.1 for further
2152 ;; ; semantic details of mailbox names.
2154 (defsubst imap-parse-mailbox
()
2155 (let ((mailbox (imap-parse-astring)))
2156 (if (string-equal "INBOX" (upcase mailbox
))
2160 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2162 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2163 ;; ; Authentication condition
2165 ;; resp-cond-bye = "BYE" SP resp-text
2167 (defun imap-parse-greeting ()
2168 "Parse an IMAP greeting."
2169 (cond ((looking-at "\\* OK ")
2170 (setq imap-state
'nonauth
))
2171 ((looking-at "\\* PREAUTH ")
2172 (setq imap-state
'auth
))
2173 ((looking-at "\\* BYE ")
2174 (setq imap-state
'closed
))))
2176 ;; response = *(continue-req / response-data) response-done
2178 ;; continue-req = "+" SP (resp-text / base64) CRLF
2180 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2181 ;; mailbox-data / message-data / capability-data) CRLF
2183 ;; response-done = response-tagged / response-fatal
2185 ;; response-fatal = "*" SP resp-cond-bye CRLF
2186 ;; ; Server closes connection immediately
2188 ;; response-tagged = tag SP resp-cond-state CRLF
2190 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2191 ;; ; Status condition
2193 ;; resp-cond-bye = "BYE" SP resp-text
2195 ;; mailbox-data = "FLAGS" SP flag-list /
2196 ;; "LIST" SP mailbox-list /
2197 ;; "LSUB" SP mailbox-list /
2198 ;; "SEARCH" *(SP nz-number) /
2199 ;; "STATUS" SP mailbox SP "("
2200 ;; [status-att SP number *(SP status-att SP number)] ")" /
2201 ;; number SP "EXISTS" /
2202 ;; number SP "RECENT"
2204 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2206 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2208 ;; ; IMAP4rev1 servers which offer RFC 1730
2209 ;; ; compatibility MUST list "IMAP4" as the first
2212 (defun imap-parse-response ()
2213 "Parse an IMAP command response."
2215 (pcase (setq token
(read (current-buffer)))
2216 ('+ (setq imap-continuation
2217 (or (buffer-substring (min (point-max) (1+ (point)))
2220 ('* (pcase (prog1 (setq token
(read (current-buffer)))
2222 ('OK
(imap-parse-resp-text))
2223 ('NO
(imap-parse-resp-text))
2224 ('BAD
(imap-parse-resp-text))
2225 ('BYE
(imap-parse-resp-text))
2226 ('FLAGS
(imap-mailbox-put 'flags
(imap-parse-flag-list)))
2227 ('LIST
(imap-parse-data-list 'list
))
2228 ('LSUB
(imap-parse-data-list 'lsub
))
2229 ('SEARCH
(imap-mailbox-put
2231 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2232 ('STATUS
(imap-parse-status))
2233 ('CAPABILITY
(setq imap-capability
2234 (read (concat "(" (upcase (buffer-substring
2235 (point) (point-max)))
2237 ('ID
(setq imap-id
(read (buffer-substring (point)
2239 ('ACL
(imap-parse-acl))
2240 (_ (pcase (prog1 (read (current-buffer))
2242 ('EXISTS
(imap-mailbox-put 'exists token
))
2243 ('RECENT
(imap-mailbox-put 'recent token
))
2245 ('FETCH
(imap-parse-fetch))
2246 (_ (message "Garbage: %s" (buffer-string)))))))
2248 (if (not (integerp token
))
2249 (message "Garbage: %s" (buffer-string))
2250 (pcase (prog1 (setq status
(read (current-buffer)))
2253 (setq imap-reached-tag
(max imap-reached-tag token
))
2254 (imap-parse-resp-text)))
2256 (setq imap-reached-tag
(max imap-reached-tag token
))
2258 (imap-parse-resp-text))
2260 (when (eq (char-after) ?\
[)
2261 (setq code
(buffer-substring (point)
2262 (search-forward "]")))
2264 (setq text
(buffer-substring (point) (point-max)))
2265 (push (list token status code text
)
2266 imap-failed-tags
))))
2268 (setq imap-reached-tag
(max imap-reached-tag token
))
2270 (imap-parse-resp-text))
2272 (when (eq (char-after) ?\
[)
2273 (setq code
(buffer-substring (point)
2274 (search-forward "]")))
2276 (setq text
(buffer-substring (point) (point-max)))
2277 (push (list token status code text
) imap-failed-tags
)
2278 (error "Internal error, tag %s status %s code %s text %s"
2279 token status code text
))))
2280 (_ (message "Garbage: %s" (buffer-string))))
2281 (when (assq token imap-callbacks
)
2282 (funcall (cdr (assq token imap-callbacks
)) token status
)
2283 (setq imap-callbacks
2284 (imap-remassoc token imap-callbacks
)))))))))
2286 ;; resp-text = ["[" resp-text-code "]" SP] text
2288 ;; text = 1*TEXT-CHAR
2290 ;; TEXT-CHAR = <any CHAR except CR and LF>
2292 (defun imap-parse-resp-text ()
2293 (imap-parse-resp-text-code))
2295 ;; resp-text-code = "ALERT" /
2296 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2297 ;; "NEWNAME" SP string SP string /
2299 ;; "PERMANENTFLAGS" SP "("
2300 ;; [flag-perm *(SP flag-perm)] ")" /
2304 ;; "UIDNEXT" SP nz-number /
2305 ;; "UIDVALIDITY" SP nz-number /
2306 ;; "UNSEEN" SP nz-number /
2307 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2309 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2311 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2313 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2315 ;; ; Identifies a set of messages. For message
2316 ;; ; sequence numbers, these are consecutive
2317 ;; ; numbers from 1 to the number of messages in
2319 ;; ; Comma delimits individual numbers, colon
2320 ;; ; delimits between two numbers inclusive.
2321 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2322 ;; ; 14,15 for a mailbox with 15 messages.
2324 ;; sequence-num = nz-number / "*"
2325 ;; ; * is the largest number in use. For message
2326 ;; ; sequence numbers, it is the number of messages
2327 ;; ; in the mailbox. For unique identifiers, it is
2328 ;; ; the unique identifier of the last message in
2331 ;; flag-perm = flag / "\*"
2333 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2334 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2335 ;; ; Does not include "\Recent"
2337 ;; flag-extension = "\" atom
2338 ;; ; Future expansion. Client implementations
2339 ;; ; MUST accept flag-extension flags. Server
2340 ;; ; implementations MUST NOT generate
2341 ;; ; flag-extension flags except as defined by
2342 ;; ; future standard or standards-track
2343 ;; ; revisions of this specification.
2345 ;; flag-keyword = atom
2347 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2349 (defun imap-parse-resp-text-code ()
2350 ;; xxx next line for stalker communigate pro 3.3.1 bug
2351 (when (looking-at " \\[")
2353 (when (eq (char-after) ?\
[)
2355 (cond ((search-forward "PERMANENTFLAGS " nil t
)
2356 (imap-mailbox-put 'permanentflags
(imap-parse-flag-list)))
2357 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t
)
2358 (imap-mailbox-put 'uidnext
(match-string 1)))
2359 ((search-forward "UNSEEN " nil t
)
2360 (imap-mailbox-put 'first-unseen
(read (current-buffer))))
2361 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2362 (imap-mailbox-put 'uidvalidity
(match-string 1)))
2363 ((search-forward "READ-ONLY" nil t
)
2364 (imap-mailbox-put 'read-only t
))
2365 ((search-forward "NEWNAME " nil t
)
2366 (let (oldname newname
)
2367 (setq oldname
(imap-parse-string))
2369 (setq newname
(imap-parse-string))
2370 (imap-mailbox-put 'newname newname oldname
)))
2371 ((search-forward "TRYCREATE" nil t
)
2372 (imap-mailbox-put 'trycreate t imap-current-target-mailbox
))
2373 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2374 (imap-mailbox-put 'appenduid
2375 (list (match-string 1)
2376 (string-to-number (match-string 2)))
2377 imap-current-target-mailbox
))
2378 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2379 (imap-mailbox-put 'copyuid
(list (match-string 1)
2382 imap-current-target-mailbox
))
2383 ((search-forward "ALERT] " nil t
)
2384 (message "Imap server %s information: %s" imap-server
2385 (buffer-substring (point) (point-max)))))))
2387 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2388 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2390 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2391 ;; *(SP mbx-list-oflag) /
2392 ;; mbx-list-oflag *(SP mbx-list-oflag)
2394 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2395 ;; ; Other flags; multiple possible per LIST response
2397 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2398 ;; ; Selectability flags; only one per LIST response
2400 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2401 ;; "\" quoted-specials
2403 ;; quoted-specials = DQUOTE / "\"
2405 (defun imap-parse-data-list (type)
2406 (let (flags delimiter mailbox
)
2407 (setq flags
(imap-parse-flag-list))
2408 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2409 (setq delimiter
(match-string 1))
2410 (goto-char (1+ (match-end 0)))
2411 (when (setq mailbox
(imap-parse-mailbox))
2412 (imap-mailbox-put type t mailbox
)
2413 (imap-mailbox-put 'list-flags flags mailbox
)
2414 (imap-mailbox-put 'delimiter delimiter mailbox
)))))
2416 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2417 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2418 ;; "INTERNALDATE" SPACE date_time /
2419 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2420 ;; "RFC822.SIZE" SPACE number /
2421 ;; "BODY" ["STRUCTURE"] SPACE body /
2422 ;; "BODY" section ["<" number ">"] SPACE nstring /
2423 ;; "UID" SPACE uniqueid) ")"
2425 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2426 ;; SPACE time SPACE zone <">
2428 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2429 ;; ["." (section_text / "MIME")])] "]"
2431 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2432 ;; SPACE header_list / "TEXT"
2434 ;; header_fld_name ::= astring
2436 ;; header_list ::= "(" 1#header_fld_name ")"
2438 (defsubst imap-parse-header-list
()
2439 (when (eq (char-after) ?\
()
2441 (while (not (eq (char-after) ?\
)))
2443 (push (imap-parse-astring) strlist
))
2445 (nreverse strlist
))))
2447 (defsubst imap-parse-fetch-body-section
()
2449 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t
)))))
2450 (if (eq (char-before) ?
)
2452 (mapconcat 'identity
(cons section
(imap-parse-header-list)) " ")
2453 (search-forward "]" nil t
))
2456 (defun imap-parse-fetch ()
2457 (when (eq (char-after) ?\
()
2458 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2459 rfc822size body bodydetail bodystructure flags-empty
)
2460 ;; Courier can insert spurious blank characters which will
2461 ;; confuse `read', so skip past them.
2462 (while (let ((moved (skip-chars-forward " \t")))
2463 (prog1 (not (eq (char-after) ?\
)))
2464 (unless (= moved
0) (backward-char))))
2466 (let ((token (read (current-buffer))))
2468 (cond ((eq token
'UID
)
2469 (setq uid
(condition-case ()
2470 (read (current-buffer))
2473 (setq flags
(imap-parse-flag-list))
2475 (setq flags-empty
't
)))
2476 ((eq token
'ENVELOPE
)
2477 (setq envelope
(imap-parse-envelope)))
2478 ((eq token
'INTERNALDATE
)
2479 (setq internaldate
(imap-parse-string)))
2481 (setq rfc822
(imap-parse-nstring)))
2482 ((eq token
'RFC822.HEADER
)
2483 (setq rfc822header
(imap-parse-nstring)))
2484 ((eq token
'RFC822.TEXT
)
2485 (setq rfc822text
(imap-parse-nstring)))
2486 ((eq token
'RFC822.SIZE
)
2487 (setq rfc822size
(read (current-buffer))))
2489 (if (eq (char-before) ?\
[)
2491 (upcase (imap-parse-fetch-body-section))
2492 (and (eq (char-after) ?
<)
2493 (buffer-substring (1+ (point))
2494 (search-forward ">" nil t
)))
2495 (progn (imap-forward)
2496 (imap-parse-nstring)))
2498 (setq body
(imap-parse-body))))
2499 ((eq token
'BODYSTRUCTURE
)
2500 (setq bodystructure
(imap-parse-body))))))
2502 (setq imap-current-message uid
)
2503 (imap-message-put uid
'UID uid
)
2504 (and (or flags flags-empty
) (imap-message-put uid
'FLAGS flags
))
2505 (and envelope
(imap-message-put uid
'ENVELOPE envelope
))
2506 (and internaldate
(imap-message-put uid
'INTERNALDATE internaldate
))
2507 (and rfc822
(imap-message-put uid
'RFC822 rfc822
))
2508 (and rfc822header
(imap-message-put uid
'RFC822.HEADER rfc822header
))
2509 (and rfc822text
(imap-message-put uid
'RFC822.TEXT rfc822text
))
2510 (and rfc822size
(imap-message-put uid
'RFC822.SIZE rfc822size
))
2511 (and body
(imap-message-put uid
'BODY body
))
2512 (and bodydetail
(imap-message-put uid
'BODYDETAIL bodydetail
))
2513 (and bodystructure
(imap-message-put uid
'BODYSTRUCTURE bodystructure
))
2514 (run-hooks 'imap-fetch-data-hook
)))))
2516 ;; mailbox-data = ...
2517 ;; "STATUS" SP mailbox SP "("
2518 ;; [status-att SP number
2519 ;; *(SP status-att SP number)] ")"
2522 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2525 (defun imap-parse-status ()
2526 (let ((mailbox (imap-parse-mailbox)))
2527 (if (eq (char-after) ?
)
2529 (when (and mailbox
(eq (char-after) ?\
())
2530 (while (and (not (eq (char-after) ?\
)))
2531 (or (forward-char) t
)
2532 (looking-at "\\([A-Za-z]+\\) "))
2533 (let ((token (upcase (match-string 1))))
2534 (goto-char (match-end 0))
2535 (cond ((string= token
"MESSAGES")
2536 (imap-mailbox-put 'messages
(read (current-buffer)) mailbox
))
2537 ((string= token
"RECENT")
2538 (imap-mailbox-put 'recent
(read (current-buffer)) mailbox
))
2539 ((string= token
"UIDNEXT")
2540 (and (looking-at "[0-9]+")
2541 (imap-mailbox-put 'uidnext
(match-string 0) mailbox
)
2542 (goto-char (match-end 0))))
2543 ((string= token
"UIDVALIDITY")
2544 (and (looking-at "[0-9]+")
2545 (imap-mailbox-put 'uidvalidity
(match-string 0) mailbox
)
2546 (goto-char (match-end 0))))
2547 ((string= token
"UNSEEN")
2548 (imap-mailbox-put 'unseen
(read (current-buffer)) mailbox
))
2550 (message "Unknown status data %s in mailbox %s ignored"
2552 (read (current-buffer)))))))))
2554 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2557 ;; identifier ::= astring
2559 ;; rights ::= astring
2561 (defun imap-parse-acl ()
2562 (let ((mailbox (imap-parse-mailbox))
2563 identifier rights acl
)
2564 (while (eq (char-after) ?\
)
2566 (setq identifier
(imap-parse-astring))
2568 (setq rights
(imap-parse-astring))
2569 (setq acl
(append acl
(list (cons identifier rights
)))))
2570 (imap-mailbox-put 'acl acl mailbox
)))
2572 ;; flag-list = "(" [flag *(SP flag)] ")"
2574 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2575 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2576 ;; ; Does not include "\Recent"
2578 ;; flag-keyword = atom
2580 ;; flag-extension = "\" atom
2581 ;; ; Future expansion. Client implementations
2582 ;; ; MUST accept flag-extension flags. Server
2583 ;; ; implementations MUST NOT generate
2584 ;; ; flag-extension flags except as defined by
2585 ;; ; future standard or standards-track
2586 ;; ; revisions of this specification.
2588 (defun imap-parse-flag-list ()
2589 (let (flag-list start
)
2590 (cl-assert (eq (char-after) ?\
() nil
"In imap-parse-flag-list 1")
2591 (while (and (not (eq (char-after) ?\
)))
2594 ;; next line for Courier IMAP bug.
2595 (skip-chars-forward " ")
2597 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2598 (push (buffer-substring start
(point)) flag-list
))
2599 (cl-assert (eq (char-after) ?\
)) nil
"In imap-parse-flag-list 2")
2601 (nreverse flag-list
)))
2603 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2604 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2605 ;; env-in-reply-to SP env-message-id ")"
2607 ;; env-bcc = "(" 1*address ")" / nil
2609 ;; env-cc = "(" 1*address ")" / nil
2611 ;; env-date = nstring
2613 ;; env-from = "(" 1*address ")" / nil
2615 ;; env-in-reply-to = nstring
2617 ;; env-message-id = nstring
2619 ;; env-reply-to = "(" 1*address ")" / nil
2621 ;; env-sender = "(" 1*address ")" / nil
2623 ;; env-subject = nstring
2625 ;; env-to = "(" 1*address ")" / nil
2627 (defun imap-parse-envelope ()
2628 (when (eq (char-after) ?\
()
2630 (vector (prog1 (imap-parse-nstring) ;; date
2632 (prog1 (imap-parse-nstring) ;; subject
2634 (prog1 (imap-parse-address-list) ;; from
2636 (prog1 (imap-parse-address-list) ;; sender
2638 (prog1 (imap-parse-address-list) ;; reply-to
2640 (prog1 (imap-parse-address-list) ;; to
2642 (prog1 (imap-parse-address-list) ;; cc
2644 (prog1 (imap-parse-address-list) ;; bcc
2646 (prog1 (imap-parse-nstring) ;; in-reply-to
2648 (prog1 (imap-parse-nstring) ;; message-id
2651 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2653 (defsubst imap-parse-string-list
()
2654 (cond ((eq (char-after) ?\
() ;; body-fld-param
2657 (while (setq str
(imap-parse-string))
2659 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2660 ;; between body-fld-param's sometimes
2661 (or (eq (char-after) ?
\")
2663 (nreverse strlist
)))
2667 ;; body-extension = nstring / number /
2668 ;; "(" body-extension *(SP body-extension) ")"
2669 ;; ; Future expansion. Client implementations
2670 ;; ; MUST accept body-extension fields. Server
2671 ;; ; implementations MUST NOT generate
2672 ;; ; body-extension fields except as defined by
2673 ;; ; future standard or standards-track
2674 ;; ; revisions of this specification.
2676 (defun imap-parse-body-extension ()
2677 (if (eq (char-after) ?\
()
2680 (push (imap-parse-body-extension) b-e
)
2681 (while (eq (char-after) ?\
)
2683 (push (imap-parse-body-extension) b-e
))
2684 (cl-assert (eq (char-after) ?\
)) nil
"In imap-parse-body-extension")
2687 (or (imap-parse-number)
2688 (imap-parse-nstring))))
2690 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2691 ;; *(SP body-extension)]]
2692 ;; ; MUST NOT be returned on non-extensible
2695 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2696 ;; *(SP body-extension)]]
2697 ;; ; MUST NOT be returned on non-extensible
2700 (defsubst imap-parse-body-ext
()
2702 (when (eq (char-after) ?\
) ;; body-fld-dsp
2705 (if (eq (char-after) ?\
()
2708 (push (imap-parse-string) dsp
)
2710 (push (imap-parse-string-list) dsp
)
2712 ;; With assert, the code might not be eval'd.
2713 ;; (cl-assert (imap-parse-nil) t "In imap-parse-body-ext")
2715 (push (nreverse dsp
) ext
))
2716 (when (eq (char-after) ?\
) ;; body-fld-lang
2718 (if (eq (char-after) ?\
()
2719 (push (imap-parse-string-list) ext
)
2720 (push (imap-parse-nstring) ext
))
2721 (while (eq (char-after) ?\
) ;; body-extension
2723 (setq ext
(append (imap-parse-body-extension) ext
)))))
2726 ;; body = "(" body-type-1part / body-type-mpart ")"
2728 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2729 ;; *(SP body-extension)]]
2730 ;; ; MUST NOT be returned on non-extensible
2733 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2734 ;; *(SP body-extension)]]
2735 ;; ; MUST NOT be returned on non-extensible
2738 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2739 ;; body-fld-enc SP body-fld-octets
2741 ;; body-fld-desc = nstring
2743 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2745 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2746 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2748 ;; body-fld-id = nstring
2750 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2752 ;; body-fld-lines = number
2754 ;; body-fld-md5 = nstring
2756 ;; body-fld-octets = number
2758 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2760 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2761 ;; [SP body-ext-1part]
2763 ;; body-type-basic = media-basic SP body-fields
2764 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2766 ;; body-type-msg = media-message SP body-fields SP envelope
2767 ;; SP body SP body-fld-lines
2769 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2771 ;; body-type-mpart = 1*body SP media-subtype
2772 ;; [SP body-ext-mpart]
2774 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2775 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2776 ;; ; Defined in [MIME-IMT]
2778 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2779 ;; ; Defined in [MIME-IMT]
2781 ;; media-subtype = string
2782 ;; ; Defined in [MIME-IMT]
2784 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2785 ;; ; Defined in [MIME-IMT]
2787 (defun imap-parse-body ()
2789 (when (eq (char-after) ?\
()
2791 (if (eq (char-after) ?\
()
2793 (while (and (eq (char-after) ?\
()
2794 (setq subbody
(imap-parse-body)))
2795 ;; buggy stalker communigate pro 3.0 inserts a SPC between
2796 ;; parts in multiparts
2797 (when (and (eq (char-after) ?\
)
2798 (eq (char-after (1+ (point))) ?\
())
2800 (push subbody body
))
2802 (push (imap-parse-string) body
) ;; media-subtype
2803 (when (eq (char-after) ?\
) ;; body-ext-mpart:
2805 (if (eq (char-after) ?\
() ;; body-fld-param
2806 (push (imap-parse-string-list) body
)
2807 (push (and (imap-parse-nil) nil
) body
))
2809 (append (imap-parse-body-ext) body
))) ;; body-ext-...
2810 (cl-assert (eq (char-after) ?\
)) nil
"In imap-parse-body")
2814 (push (imap-parse-string) body
) ;; media-type
2816 (push (imap-parse-string) body
) ;; media-subtype
2818 ;; next line for Sun SIMS bug
2819 (and (eq (char-after) ?
) (imap-forward))
2820 (if (eq (char-after) ?\
() ;; body-fld-param
2821 (push (imap-parse-string-list) body
)
2822 (push (and (imap-parse-nil) nil
) body
))
2824 (push (imap-parse-nstring) body
) ;; body-fld-id
2826 (push (imap-parse-nstring) body
) ;; body-fld-desc
2828 ;; Next `or' for Sun SIMS bug. It regards body-fld-enc as a
2829 ;; nstring and returns nil instead of defaulting back to 7BIT
2830 ;; as the standard says.
2831 ;; Exchange (2007, at least) does this as well.
2832 (push (or (imap-parse-nstring) "7BIT") body
) ;; body-fld-enc
2834 ;; Exchange 2007 can return -1, contrary to the spec...
2835 (if (eq (char-after) ?-
)
2837 (skip-chars-forward "-0-9")
2839 (push (imap-parse-number) body
)) ;; body-fld-octets
2841 ;; Ok, we're done parsing the required parts, what comes now is one of
2844 ;; envelope (then we're parsing body-type-msg)
2845 ;; body-fld-lines (then we're parsing body-type-text)
2846 ;; body-ext-1part (then we're parsing body-type-basic)
2848 ;; The problem is that the two first are in turn optionally followed
2849 ;; by the third. So we parse the first two here (if there are any)...
2851 (when (eq (char-after) ?\
)
2854 (cond ((eq (char-after) ?\
() ;; body-type-msg:
2855 (push (imap-parse-envelope) body
) ;; envelope
2857 (push (imap-parse-body) body
) ;; body
2858 ;; buggy stalker communigate pro 3.0 doesn't print
2859 ;; number of lines in message/rfc822 attachment
2860 (if (eq (char-after) ?\
))
2863 (push (imap-parse-number) body
))) ;; body-fld-lines
2864 ((setq lines
(imap-parse-number)) ;; body-type-text:
2865 (push lines body
)) ;; body-fld-lines
2867 (backward-char))))) ;; no match...
2869 ;; ...and then parse the third one here...
2871 (when (eq (char-after) ?\
) ;; body-ext-1part:
2873 (push (imap-parse-nstring) body
) ;; body-fld-md5
2874 (setq body
(append (imap-parse-body-ext) body
))) ;; body-ext-1part..
2876 (cl-assert (eq (char-after) ?\
)) nil
"In imap-parse-body 2")
2880 (when imap-debug
; (untrace-all)
2882 (buffer-disable-undo (get-buffer-create imap-debug-buffer
))
2883 (mapc (lambda (f) (trace-function-background f imap-debug-buffer
))
2892 imap-interactive-login
2909 imap-send-command-wait
2914 imap-current-mailbox
2915 imap-current-mailbox-p-1
2916 imap-current-mailbox-p
2917 imap-mailbox-select-1
2919 imap-mailbox-examine-1
2920 imap-mailbox-examine
2921 imap-mailbox-unselect
2922 imap-mailbox-expunge
2924 imap-mailbox-create-1
2930 imap-mailbox-subscribe
2931 imap-mailbox-unsubscribe
2933 imap-mailbox-acl-get
2934 imap-mailbox-acl-set
2935 imap-mailbox-acl-delete
2936 imap-current-message
2937 imap-list-to-message-set
2945 imap-message-flag-permanent-p
2946 imap-message-flags-set
2947 imap-message-flags-del
2948 imap-message-flags-add
2949 imap-message-copyuid-1
2950 imap-message-copyuid
2952 imap-message-appenduid-1
2953 imap-message-appenduid
2965 imap-parse-resp-text
2966 imap-parse-resp-text-code
2967 imap-parse-data-list
2971 imap-parse-flag-list
2973 imap-parse-body-extension
2979 ;;; imap.el ends here