Fix permissions handling (CVE-2010-0825).
[emacs.git] / lisp / net / imap.el
bloba47822533e568840a292fc8fe95d8840eae33106
1 ;;; imap.el --- imap library
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; imap.el is an elisp library providing an interface for talking to
27 ;; IMAP servers.
29 ;; imap.el is roughly divided in two parts, one that parses IMAP
30 ;; responses from the server and storing data into buffer-local
31 ;; variables, and one for utility functions which send commands to
32 ;; server, waits for an answer, and return information. The latter
33 ;; part is layered on top of the previous.
35 ;; The imap.el API consist of the following functions, other functions
36 ;; in this file should not be called directly and the result of doing
37 ;; so are at best undefined.
39 ;; Global commands:
41 ;; imap-open, imap-opened, imap-authenticate, imap-close,
42 ;; imap-capability, imap-namespace, imap-error-text
44 ;; Mailbox commands:
46 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
47 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
48 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
49 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
50 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
51 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
52 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
54 ;; Message commands:
56 ;; imap-fetch-asynch, imap-fetch,
57 ;; imap-current-message, imap-list-to-message-set,
58 ;; imap-message-get, imap-message-map
59 ;; imap-message-envelope-date, imap-message-envelope-subject,
60 ;; imap-message-envelope-from, imap-message-envelope-sender,
61 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
62 ;; imap-message-envelope-cc, imap-message-envelope-bcc
63 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
64 ;; imap-message-body, imap-message-flag-permanent-p
65 ;; imap-message-flags-set, imap-message-flags-del
66 ;; imap-message-flags-add, imap-message-copyuid
67 ;; imap-message-copy, imap-message-appenduid
68 ;; imap-message-append, imap-envelope-from
69 ;; imap-body-lines
71 ;; It is my hope that these commands should be pretty self
72 ;; explanatory for someone that know IMAP. All functions have
73 ;; additional documentation on how to invoke them.
75 ;; imap.el supports RFC1730/2060/RFC3501 (IMAP4/IMAP4rev1). The implemented
76 ;; IMAP extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
77 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
78 ;; LOGINDISABLED) (with use of external library starttls.el and
79 ;; program starttls), and the GSSAPI / Kerberos V4 sections of RFC1731
80 ;; (with use of external program `imtest'), and RFC2971 (ID). It also
81 ;; takes advantage of the UNSELECT extension in Cyrus IMAPD.
83 ;; Without the work of John McClary Prevost and Jim Radford this library
84 ;; would not have seen the light of day. Many thanks.
86 ;; This is a transcript of a short interactive session for demonstration
87 ;; purposes.
89 ;; (imap-open "my.mail.server")
90 ;; => " *imap* my.mail.server:0"
92 ;; The rest are invoked with current buffer as the buffer returned by
93 ;; `imap-open'. It is possible to do it all without this, but it would
94 ;; look ugly here since `buffer' is always the last argument for all
95 ;; imap.el API functions.
97 ;; (imap-authenticate "myusername" "mypassword")
98 ;; => auth
100 ;; (imap-mailbox-lsub "*")
101 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
103 ;; (imap-mailbox-list "INBOX.n%")
104 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
106 ;; (imap-mailbox-select "INBOX.nnimap")
107 ;; => "INBOX.nnimap"
109 ;; (imap-mailbox-get 'exists)
110 ;; => 166
112 ;; (imap-mailbox-get 'uidvalidity)
113 ;; => "908992622"
115 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
116 ;; => (235 236)
118 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
119 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
121 ;; Todo:
123 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
124 ;; Use IEEE floats (which are effectively exact)? -- fx
125 ;; o Don't use `read' at all (important places already fixed)
126 ;; o Accept list of articles instead of message set string in most
127 ;; imap-message-* functions.
128 ;; o Send strings as literal if they contain, e.g., ".
130 ;; Revision history:
132 ;; - 19991218 added starttls/digest-md5 patch,
133 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
134 ;; NB! you need SLIM for starttls.el and digest-md5.el
135 ;; - 19991023 committed to pgnus
138 ;;; Code:
140 (eval-when-compile (require 'cl))
141 (eval-and-compile
142 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
143 (autoload 'starttls-open-stream "starttls")
144 (autoload 'starttls-negotiate "starttls")
145 (autoload 'sasl-find-mechanism "sasl")
146 (autoload 'digest-md5-parse-digest-challenge "digest-md5")
147 (autoload 'digest-md5-digest-response "digest-md5")
148 (autoload 'digest-md5-digest-uri "digest-md5")
149 (autoload 'digest-md5-challenge "digest-md5")
150 (autoload 'rfc2104-hash "rfc2104")
151 (autoload 'utf7-encode "utf7")
152 (autoload 'utf7-decode "utf7")
153 (autoload 'format-spec "format-spec")
154 (autoload 'format-spec-make "format-spec")
155 (autoload 'open-tls-stream "tls"))
157 ;; User variables.
159 (defgroup imap nil
160 "Low-level IMAP issues."
161 :version "21.1"
162 :group 'mail)
164 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
165 "imtest -kp %s %p")
166 "List of strings containing commands for Kerberos 4 authentication.
167 %s is replaced with server hostname, %p with port to connect to, and
168 %l with the value of `imap-default-user'. The program should accept
169 IMAP commands on stdin and return responses to stdout. Each entry in
170 the list is tried until a successful connection is made."
171 :group 'imap
172 :type '(repeat string))
174 (defcustom imap-gssapi-program (list
175 (concat "gsasl %s %p "
176 "--mechanism GSSAPI "
177 "--authentication-id %l")
178 "imtest -m gssapi -u %l -p %p %s")
179 "List of strings containing commands for GSSAPI (krb5) authentication.
180 %s is replaced with server hostname, %p with port to connect to, and
181 %l with the value of `imap-default-user'. The program should accept
182 IMAP commands on stdin and return responses to stdout. Each entry in
183 the list is tried until a successful connection is made."
184 :group 'imap
185 :type '(repeat string))
187 (defcustom imap-ssl-program '("openssl s_client -quiet -ssl3 -connect %s:%p"
188 "openssl s_client -quiet -ssl2 -connect %s:%p"
189 "s_client -quiet -ssl3 -connect %s:%p"
190 "s_client -quiet -ssl2 -connect %s:%p")
191 "A string, or list of strings, containing commands for SSL connections.
192 Within a string, %s is replaced with the server address and %p with
193 port number on server. The program should accept IMAP commands on
194 stdin and return responses to stdout. Each entry in the list is tried
195 until a successful connection is made."
196 :group 'imap
197 :type '(choice string
198 (repeat string)))
200 (defcustom imap-shell-program '("ssh %s imapd"
201 "rsh %s imapd"
202 "ssh %g ssh %s imapd"
203 "rsh %g rsh %s imapd")
204 "A list of strings, containing commands for IMAP connection.
205 Within a string, %s is replaced with the server address, %p with port
206 number on server, %g with `imap-shell-host', and %l with
207 `imap-default-user'. The program should read IMAP commands from stdin
208 and write IMAP response to stdout. Each entry in the list is tried
209 until a successful connection is made."
210 :group 'imap
211 :type '(repeat string))
213 (defcustom imap-process-connection-type nil
214 "*Value for `process-connection-type' to use for Kerberos4, GSSAPI and SSL.
215 The `process-connection-type' variable controls the type of device
216 used to communicate with subprocesses. Values are nil to use a
217 pipe, or t or `pty' to use a pty. The value has no effect if the
218 system has no ptys or if all ptys are busy: then a pipe is used
219 in any case. The value takes effect when an IMAP server is
220 opened; changing it after that has no effect."
221 :version "22.1"
222 :group 'imap
223 :type 'boolean)
225 (defcustom imap-use-utf7 t
226 "If non-nil, do utf7 encoding/decoding of mailbox names.
227 Since the UTF7 decoding currently only decodes into ISO-8859-1
228 characters, you may disable this decoding if you need to access UTF7
229 encoded mailboxes which doesn't translate into ISO-8859-1."
230 :group 'imap
231 :type 'boolean)
233 (defcustom imap-log nil
234 "If non-nil, an imap session trace is placed in `imap-log-buffer'.
235 Note that username, passwords and other privacy sensitive
236 information (such as e-mail) may be stored in the buffer.
237 It is not written to disk, however. Do not enable this
238 variable unless you are comfortable with that.
240 See also `imap-debug'."
241 :group 'imap
242 :type 'boolean)
244 (defcustom imap-debug nil
245 "If non-nil, trace imap- functions into `imap-debug-buffer'.
246 Uses `trace-function-background', so you can turn it off with,
247 say, `untrace-all'.
249 Note that username, passwords and other privacy sensitive
250 information (such as e-mail) may be stored in the buffer.
251 It is not written to disk, however. Do not enable this
252 variable unless you are comfortable with that.
254 This variable only takes effect when loading the `imap' library.
255 See also `imap-log'."
256 :group 'imap
257 :type 'boolean)
259 (defcustom imap-shell-host "gateway"
260 "Hostname of rlogin proxy."
261 :group 'imap
262 :type 'string)
264 (defcustom imap-default-user (user-login-name)
265 "Default username to use."
266 :group 'imap
267 :type 'string)
269 (defcustom imap-read-timeout (if (string-match
270 "windows-nt\\|os/2\\|emx\\|cygwin"
271 (symbol-name system-type))
273 0.1)
274 "*How long to wait between checking for the end of output.
275 Shorter values mean quicker response, but is more CPU intensive."
276 :type 'number
277 :group 'imap)
279 (defcustom imap-store-password nil
280 "If non-nil, store session password without prompting."
281 :group 'imap
282 :type 'boolean)
284 ;; Various variables.
286 (defvar imap-fetch-data-hook nil
287 "Hooks called after receiving each FETCH response.")
289 (defvar imap-streams '(gssapi kerberos4 starttls tls ssl network shell)
290 "Priority of streams to consider when opening connection to server.")
292 (defvar imap-stream-alist
293 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
294 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
295 (tls imap-tls-p imap-tls-open)
296 (ssl imap-ssl-p imap-ssl-open)
297 (network imap-network-p imap-network-open)
298 (shell imap-shell-p imap-shell-open)
299 (starttls imap-starttls-p imap-starttls-open))
300 "Definition of network streams.
302 \(NAME CHECK OPEN)
304 NAME names the stream, CHECK is a function returning non-nil if the
305 server support the stream and OPEN is a function for opening the
306 stream.")
308 (defvar imap-authenticators '(gssapi
309 kerberos4
310 digest-md5
311 cram-md5
312 ;;sasl
313 login
314 anonymous)
315 "Priority of authenticators to consider when authenticating to server.")
317 (defvar imap-authenticator-alist
318 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
319 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
320 (sasl imap-sasl-auth-p imap-sasl-auth)
321 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
322 (login imap-login-p imap-login-auth)
323 (anonymous imap-anonymous-p imap-anonymous-auth)
324 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
325 "Definition of authenticators.
327 \(NAME CHECK AUTHENTICATE)
329 NAME names the authenticator. CHECK is a function returning non-nil if
330 the server support the authenticator and AUTHENTICATE is a function
331 for doing the actual authentication.")
333 (defvar imap-error nil
334 "Error codes from the last command.")
336 (defvar imap-logout-timeout nil
337 "Close server immediately if it can't logout in this number of seconds.
338 If it is nil, never close server until logout completes. Normally,
339 the value of this variable will be bound to a certain value to which
340 an application program that uses this module specifies on a per-server
341 basis.")
343 ;; Internal constants. Change these and die.
345 (defconst imap-default-port 143)
346 (defconst imap-default-ssl-port 993)
347 (defconst imap-default-tls-port 993)
348 (defconst imap-default-stream 'network)
349 (defconst imap-coding-system-for-read 'binary)
350 (defconst imap-coding-system-for-write 'binary)
351 (defconst imap-local-variables '(imap-server
352 imap-port
353 imap-client-eol
354 imap-server-eol
355 imap-auth
356 imap-stream
357 imap-username
358 imap-password
359 imap-current-mailbox
360 imap-current-target-mailbox
361 imap-message-data
362 imap-capability
363 imap-id
364 imap-namespace
365 imap-state
366 imap-reached-tag
367 imap-failed-tags
368 imap-tag
369 imap-process
370 imap-calculate-literal-size-first
371 imap-mailbox-data))
372 (defconst imap-log-buffer "*imap-log*")
373 (defconst imap-debug-buffer "*imap-debug*")
375 ;; Internal variables.
377 (defvar imap-stream nil)
378 (defvar imap-auth nil)
379 (defvar imap-server nil)
380 (defvar imap-port nil)
381 (defvar imap-username nil)
382 (defvar imap-password nil)
383 (defvar imap-last-authenticator nil)
384 (defvar imap-calculate-literal-size-first nil)
385 (defvar imap-state 'closed
386 "IMAP state.
387 Valid states are `closed', `initial', `nonauth', `auth', `selected'
388 and `examine'.")
390 (defvar imap-server-eol "\r\n"
391 "The EOL string sent from the server.")
393 (defvar imap-client-eol "\r\n"
394 "The EOL string we send to the server.")
396 (defvar imap-current-mailbox nil
397 "Current mailbox name.")
399 (defvar imap-current-target-mailbox nil
400 "Current target mailbox for COPY and APPEND commands.")
402 (defvar imap-mailbox-data nil
403 "Obarray with mailbox data.")
405 (defvar imap-mailbox-prime 997
406 "Length of `imap-mailbox-data'.")
408 (defvar imap-current-message nil
409 "Current message number.")
411 (defvar imap-message-data nil
412 "Obarray with message data.")
414 (defvar imap-message-prime 997
415 "Length of `imap-message-data'.")
417 (defvar imap-capability nil
418 "Capability for server.")
420 (defvar imap-id nil
421 "Identity of server.
422 See RFC 2971.")
424 (defvar imap-namespace nil
425 "Namespace for current server.")
427 (defvar imap-reached-tag 0
428 "Lower limit on command tags that have been parsed.")
430 (defvar imap-failed-tags nil
431 "Alist of tags that failed.
432 Each element is a list with four elements; tag (a integer), response
433 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
434 human readable response text (a string).")
436 (defvar imap-tag 0
437 "Command tag number.")
439 (defvar imap-process nil
440 "Process.")
442 (defvar imap-continuation nil
443 "Non-nil indicates that the server emitted a continuation request.
444 The actual value is really the text on the continuation line.")
446 (defvar imap-callbacks nil
447 "List of response tags and callbacks, on the form `(number . function)'.
448 The function should take two arguments, the first the IMAP tag and the
449 second the status (OK, NO, BAD etc) of the command.")
451 (defvar imap-enable-exchange-bug-workaround nil
452 "Send FETCH UID commands as *:* instead of *.
454 When non-nil, use an alternative UIDS form. Enabling appears to
455 be required for some servers (e.g., Microsoft Exchange 2007)
456 which otherwise would trigger a response 'BAD The specified
457 message set is invalid.'. We don't unconditionally use this
458 form, since this is said to be significantly inefficient.
460 This variable is set to t automatically per server if the
461 canonical form fails.")
464 ;; Utility functions:
466 (defun imap-remassoc (key alist)
467 "Delete by side effect any elements of ALIST whose car is `equal' to KEY.
468 The modified ALIST is returned. If the first member
469 of ALIST has a car that is `equal' to KEY, there is no way to remove it
470 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
471 sure of changing the value of `foo'."
472 (when alist
473 (if (equal key (caar alist))
474 (cdr alist)
475 (setcdr alist (imap-remassoc key (cdr alist)))
476 alist)))
478 (defsubst imap-disable-multibyte ()
479 "Enable multibyte in the current buffer."
480 (when (fboundp 'set-buffer-multibyte)
481 (set-buffer-multibyte nil)))
483 (defsubst imap-utf7-encode (string)
484 (if imap-use-utf7
485 (and string
486 (condition-case ()
487 (utf7-encode string t)
488 (error (message
489 "imap: Could not UTF7 encode `%s', using it unencoded..."
490 string)
491 string)))
492 string))
494 (defsubst imap-utf7-decode (string)
495 (if imap-use-utf7
496 (and string
497 (condition-case ()
498 (utf7-decode string t)
499 (error (message
500 "imap: Could not UTF7 decode `%s', using it undecoded..."
501 string)
502 string)))
503 string))
505 (defsubst imap-ok-p (status)
506 (if (eq status 'OK)
508 (setq imap-error status)
509 nil))
511 (defun imap-error-text (&optional buffer)
512 (with-current-buffer (or buffer (current-buffer))
513 (nth 3 (car imap-failed-tags))))
516 ;; Server functions; stream stuff:
518 (defun imap-kerberos4-stream-p (buffer)
519 (imap-capability 'AUTH=KERBEROS_V4 buffer))
521 (defun imap-kerberos4-open (name buffer server port)
522 (let ((cmds imap-kerberos4-program)
523 cmd done)
524 (while (and (not done) (setq cmd (pop cmds)))
525 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
526 (erase-buffer)
527 (let* ((port (or port imap-default-port))
528 (coding-system-for-read imap-coding-system-for-read)
529 (coding-system-for-write imap-coding-system-for-write)
530 (process-connection-type imap-process-connection-type)
531 (process (start-process
532 name buffer shell-file-name shell-command-switch
533 (format-spec
535 (format-spec-make
536 ?s server
537 ?p (number-to-string port)
538 ?l imap-default-user))))
539 response)
540 (when process
541 (with-current-buffer buffer
542 (setq imap-client-eol "\n"
543 imap-calculate-literal-size-first t)
544 (while (and (memq (process-status process) '(open run))
545 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
546 (goto-char (point-min))
547 ;; Athena IMTEST can output SSL verify errors
548 (or (while (looking-at "^verify error:num=")
549 (forward-line))
551 (or (while (looking-at "^TLS connection established")
552 (forward-line))
554 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
555 (or (while (looking-at "^C:")
556 (forward-line))
558 ;; cyrus 1.6 imtest print "S: " before server greeting
559 (or (not (looking-at "S: "))
560 (forward-char 3)
562 (not (and (imap-parse-greeting)
563 ;; success in imtest < 1.6:
564 (or (re-search-forward
565 "^__\\(.*\\)__\n" nil t)
566 ;; success in imtest 1.6:
567 (re-search-forward
568 "^\\(Authenticat.*\\)" nil t))
569 (setq response (match-string 1)))))
570 (accept-process-output process 1)
571 (sit-for 1))
572 (and imap-log
573 (with-current-buffer (get-buffer-create imap-log-buffer)
574 (imap-disable-multibyte)
575 (buffer-disable-undo)
576 (goto-char (point-max))
577 (insert-buffer-substring buffer)))
578 (erase-buffer)
579 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
580 (if response (concat "done, " response) "failed"))
581 (if (and response (let ((case-fold-search nil))
582 (not (string-match "failed" response))))
583 (setq done process)
584 (if (memq (process-status process) '(open run))
585 (imap-logout))
586 (delete-process process)
587 nil)))))
588 done))
590 (defun imap-gssapi-stream-p (buffer)
591 (imap-capability 'AUTH=GSSAPI buffer))
593 (defun imap-gssapi-open (name buffer server port)
594 (let ((cmds imap-gssapi-program)
595 cmd done)
596 (while (and (not done) (setq cmd (pop cmds)))
597 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
598 (erase-buffer)
599 (let* ((port (or port imap-default-port))
600 (coding-system-for-read imap-coding-system-for-read)
601 (coding-system-for-write imap-coding-system-for-write)
602 (process-connection-type imap-process-connection-type)
603 (process (start-process
604 name buffer shell-file-name shell-command-switch
605 (format-spec
607 (format-spec-make
608 ?s server
609 ?p (number-to-string port)
610 ?l imap-default-user))))
611 response)
612 (when process
613 (with-current-buffer buffer
614 (setq imap-client-eol "\n"
615 imap-calculate-literal-size-first t)
616 (while (and (memq (process-status process) '(open run))
617 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
618 (goto-char (point-min))
619 ;; Athena IMTEST can output SSL verify errors
620 (or (while (looking-at "^verify error:num=")
621 (forward-line))
623 (or (while (looking-at "^TLS connection established")
624 (forward-line))
626 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
627 (or (while (looking-at "^C:")
628 (forward-line))
630 ;; cyrus 1.6 imtest print "S: " before server greeting
631 (or (not (looking-at "S: "))
632 (forward-char 3)
634 ;; GNU SASL may print 'Trying ...' first.
635 (or (not (looking-at "Trying "))
636 (forward-line)
638 (not (and (imap-parse-greeting)
639 ;; success in imtest 1.6:
640 (re-search-forward
641 (concat "^\\(\\(Authenticat.*\\)\\|\\("
642 "Client authentication "
643 "finished.*\\)\\)")
644 nil t)
645 (setq response (match-string 1)))))
646 (accept-process-output process 1)
647 (sit-for 1))
648 (and imap-log
649 (with-current-buffer (get-buffer-create imap-log-buffer)
650 (imap-disable-multibyte)
651 (buffer-disable-undo)
652 (goto-char (point-max))
653 (insert-buffer-substring buffer)))
654 (erase-buffer)
655 (message "GSSAPI IMAP connection: %s" (or response "failed"))
656 (if (and response (let ((case-fold-search nil))
657 (not (string-match "failed" response))))
658 (setq done process)
659 (if (memq (process-status process) '(open run))
660 (imap-logout))
661 (delete-process process)
662 nil)))))
663 done))
665 (defun imap-ssl-p (buffer)
666 nil)
668 (defun imap-ssl-open (name buffer server port)
669 "Open an SSL connection to SERVER."
670 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
671 (list imap-ssl-program)))
672 cmd done)
673 (while (and (not done) (setq cmd (pop cmds)))
674 (message "imap: Opening SSL connection with `%s'..." cmd)
675 (erase-buffer)
676 (let* ((port (or port imap-default-ssl-port))
677 (coding-system-for-read imap-coding-system-for-read)
678 (coding-system-for-write imap-coding-system-for-write)
679 (process-connection-type imap-process-connection-type)
680 (set-process-query-on-exit-flag
681 (if (fboundp 'set-process-query-on-exit-flag)
682 'set-process-query-on-exit-flag
683 'process-kill-without-query))
684 process)
685 (when (progn
686 (setq process (start-process
687 name buffer shell-file-name
688 shell-command-switch
689 (format-spec cmd
690 (format-spec-make
691 ?s server
692 ?p (number-to-string port)))))
693 (funcall set-process-query-on-exit-flag process nil)
694 process)
695 (with-current-buffer buffer
696 (goto-char (point-min))
697 (while (and (memq (process-status process) '(open run))
698 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
699 (goto-char (point-max))
700 (forward-line -1)
701 (not (imap-parse-greeting)))
702 (accept-process-output process 1)
703 (sit-for 1))
704 (and imap-log
705 (with-current-buffer (get-buffer-create imap-log-buffer)
706 (imap-disable-multibyte)
707 (buffer-disable-undo)
708 (goto-char (point-max))
709 (insert-buffer-substring buffer)))
710 (erase-buffer)
711 (when (memq (process-status process) '(open run))
712 (setq done process))))))
713 (if done
714 (progn
715 (message "imap: Opening SSL connection with `%s'...done" cmd)
716 done)
717 (message "imap: Opening SSL connection with `%s'...failed" cmd)
718 nil)))
720 (defun imap-tls-p (buffer)
721 nil)
723 (defun imap-tls-open (name buffer server port)
724 (let* ((port (or port imap-default-tls-port))
725 (coding-system-for-read imap-coding-system-for-read)
726 (coding-system-for-write imap-coding-system-for-write)
727 (process (open-tls-stream name buffer server port)))
728 (when process
729 (while (and (memq (process-status process) '(open run))
730 ;; FIXME: Per the "blue moon" comment, the process/buffer
731 ;; handling here, and elsewhere in functions which open
732 ;; streams, looks confused. Obviously we can change buffers
733 ;; if a different process handler kicks in from
734 ;; `accept-process-output' or `sit-for' below, and TRT seems
735 ;; to be to `save-buffer' around those calls. (I wonder why
736 ;; `sit-for' is used with a non-zero wait.) -- fx
737 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
738 (goto-char (point-max))
739 (forward-line -1)
740 (not (imap-parse-greeting)))
741 (accept-process-output process 1)
742 (sit-for 1))
743 (and imap-log
744 (with-current-buffer (get-buffer-create imap-log-buffer)
745 (imap-disable-multibyte)
746 (buffer-disable-undo)
747 (goto-char (point-max))
748 (insert-buffer-substring buffer)))
749 (when (memq (process-status process) '(open run))
750 process))))
752 (defun imap-network-p (buffer)
755 (defun imap-network-open (name buffer server port)
756 (let* ((port (or port imap-default-port))
757 (coding-system-for-read imap-coding-system-for-read)
758 (coding-system-for-write imap-coding-system-for-write)
759 (process (open-network-stream name buffer server port)))
760 (when process
761 (while (and (memq (process-status process) '(open run))
762 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
763 (goto-char (point-min))
764 (not (imap-parse-greeting)))
765 (accept-process-output process 1)
766 (sit-for 1))
767 (and imap-log
768 (with-current-buffer (get-buffer-create imap-log-buffer)
769 (imap-disable-multibyte)
770 (buffer-disable-undo)
771 (goto-char (point-max))
772 (insert-buffer-substring buffer)))
773 (when (memq (process-status process) '(open run))
774 process))))
776 (defun imap-shell-p (buffer)
777 nil)
779 (defun imap-shell-open (name buffer server port)
780 (let ((cmds (if (listp imap-shell-program) imap-shell-program
781 (list imap-shell-program)))
782 cmd done)
783 (while (and (not done) (setq cmd (pop cmds)))
784 (message "imap: Opening IMAP connection with `%s'..." cmd)
785 (setq imap-client-eol "\n")
786 (let* ((port (or port imap-default-port))
787 (coding-system-for-read imap-coding-system-for-read)
788 (coding-system-for-write imap-coding-system-for-write)
789 (process (start-process
790 name buffer shell-file-name shell-command-switch
791 (format-spec
793 (format-spec-make
794 ?s server
795 ?g imap-shell-host
796 ?p (number-to-string port)
797 ?l imap-default-user)))))
798 (when process
799 (while (and (memq (process-status process) '(open run))
800 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
801 (goto-char (point-max))
802 (forward-line -1)
803 (not (imap-parse-greeting)))
804 (accept-process-output process 1)
805 (sit-for 1))
806 (and imap-log
807 (with-current-buffer (get-buffer-create imap-log-buffer)
808 (imap-disable-multibyte)
809 (buffer-disable-undo)
810 (goto-char (point-max))
811 (insert-buffer-substring buffer)))
812 (erase-buffer)
813 (when (memq (process-status process) '(open run))
814 (setq done process)))))
815 (if done
816 (progn
817 (message "imap: Opening IMAP connection with `%s'...done" cmd)
818 done)
819 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
820 nil)))
822 (defun imap-starttls-p (buffer)
823 (imap-capability 'STARTTLS buffer))
825 (defun imap-starttls-open (name buffer server port)
826 (let* ((port (or port imap-default-port))
827 (coding-system-for-read imap-coding-system-for-read)
828 (coding-system-for-write imap-coding-system-for-write)
829 (process (starttls-open-stream name buffer server port))
830 done tls-info)
831 (message "imap: Connecting with STARTTLS...")
832 (when process
833 (while (and (memq (process-status process) '(open run))
834 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
835 (goto-char (point-max))
836 (forward-line -1)
837 (not (imap-parse-greeting)))
838 (accept-process-output process 1)
839 (sit-for 1))
840 (imap-send-command "STARTTLS")
841 (while (and (memq (process-status process) '(open run))
842 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
843 (goto-char (point-max))
844 (forward-line -1)
845 (not (re-search-forward "[0-9]+ OK.*\r?\n" nil t)))
846 (accept-process-output process 1)
847 (sit-for 1))
848 (and imap-log
849 (with-current-buffer (get-buffer-create imap-log-buffer)
850 (buffer-disable-undo)
851 (goto-char (point-max))
852 (insert-buffer-substring buffer)))
853 (when (and (setq tls-info (starttls-negotiate process))
854 (memq (process-status process) '(open run)))
855 (setq done process)))
856 (if (stringp tls-info)
857 (message "imap: STARTTLS info: %s" tls-info))
858 (message "imap: Connecting with STARTTLS...%s" (if done "done" "failed"))
859 done))
861 ;; Server functions; authenticator stuff:
863 (defun imap-interactive-login (buffer loginfunc)
864 "Login to server in BUFFER.
865 LOGINFUNC is passed a username and a password, it should return t if
866 it where successful authenticating itself to the server, nil otherwise.
867 Returns t if login was successful, nil otherwise."
868 (with-current-buffer buffer
869 (make-local-variable 'imap-username)
870 (make-local-variable 'imap-password)
871 (let (user passwd ret)
872 ;; (condition-case ()
873 (while (or (not user) (not passwd))
874 (setq user (or imap-username
875 (read-from-minibuffer
876 (concat "imap: username for " imap-server
877 " (using stream `" (symbol-name imap-stream)
878 "'): ")
879 (or user imap-default-user))))
880 (setq passwd (or imap-password
881 (read-passwd
882 (concat "imap: password for " user "@"
883 imap-server " (using authenticator `"
884 (symbol-name imap-auth) "'): "))))
885 (when (and user passwd)
886 (if (funcall loginfunc user passwd)
887 (progn
888 (message "imap: Login successful...")
889 (setq ret t
890 imap-username user)
891 (when (and (not imap-password)
892 (or imap-store-password
893 (y-or-n-p "imap: Store password for this IMAP session? ")))
894 (setq imap-password passwd)))
895 (message "imap: Login failed...")
896 (setq passwd nil)
897 (setq imap-password nil)
898 (sit-for 1))))
899 ;; (quit (with-current-buffer buffer
900 ;; (setq user nil
901 ;; passwd nil)))
902 ;; (error (with-current-buffer buffer
903 ;; (setq user nil
904 ;; passwd nil))))
905 ret)))
907 (defun imap-gssapi-auth-p (buffer)
908 (eq imap-stream 'gssapi))
910 (defun imap-gssapi-auth (buffer)
911 (message "imap: Authenticating using GSSAPI...%s"
912 (if (eq imap-stream 'gssapi) "done" "failed"))
913 (eq imap-stream 'gssapi))
915 (defun imap-kerberos4-auth-p (buffer)
916 (and (imap-capability 'AUTH=KERBEROS_V4 buffer)
917 (eq imap-stream 'kerberos4)))
919 (defun imap-kerberos4-auth (buffer)
920 (message "imap: Authenticating using Kerberos 4...%s"
921 (if (eq imap-stream 'kerberos4) "done" "failed"))
922 (eq imap-stream 'kerberos4))
924 (defun imap-cram-md5-p (buffer)
925 (imap-capability 'AUTH=CRAM-MD5 buffer))
927 (defun imap-cram-md5-auth (buffer)
928 "Login to server using the AUTH CRAM-MD5 method."
929 (message "imap: Authenticating using CRAM-MD5...")
930 (let ((done (imap-interactive-login
931 buffer
932 (lambda (user passwd)
933 (imap-ok-p
934 (imap-send-command-wait
935 (list
936 "AUTHENTICATE CRAM-MD5"
937 (lambda (challenge)
938 (let* ((decoded (base64-decode-string challenge))
939 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
940 (response (concat user " " hash))
941 (encoded (base64-encode-string response)))
942 encoded)))))))))
943 (if done
944 (message "imap: Authenticating using CRAM-MD5...done")
945 (message "imap: Authenticating using CRAM-MD5...failed"))))
947 (defun imap-login-p (buffer)
948 (and (not (imap-capability 'LOGINDISABLED buffer))
949 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
951 (defun imap-quote-specials (string)
952 (with-temp-buffer
953 (insert string)
954 (goto-char (point-min))
955 (while (re-search-forward "[\\\"]" nil t)
956 (forward-char -1)
957 (insert "\\")
958 (forward-char 1))
959 (buffer-string)))
961 (defun imap-login-auth (buffer)
962 "Login to server using the LOGIN command."
963 (message "imap: Plaintext authentication...")
964 (imap-interactive-login buffer
965 (lambda (user passwd)
966 (imap-ok-p (imap-send-command-wait
967 (concat "LOGIN \""
968 (imap-quote-specials user)
969 "\" \""
970 (imap-quote-specials passwd)
971 "\""))))))
973 (defun imap-anonymous-p (buffer)
976 (defun imap-anonymous-auth (buffer)
977 (message "imap: Logging in anonymously...")
978 (with-current-buffer buffer
979 (imap-ok-p (imap-send-command-wait
980 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
981 (system-name)) "\"")))))
983 ;;; Compiler directives.
985 (defvar imap-sasl-client)
986 (defvar imap-sasl-step)
988 (defun imap-sasl-make-mechanisms (buffer)
989 (let ((mecs '()))
990 (mapc (lambda (sym)
991 (let ((name (symbol-name sym)))
992 (if (and (> (length name) 5)
993 (string-equal "AUTH=" (substring name 0 5 )))
994 (setq mecs (cons (substring name 5) mecs)))))
995 (imap-capability nil buffer))
996 mecs))
998 (declare-function sasl-find-mechanism "sasl" (mechanism))
999 (declare-function sasl-mechanism-name "sasl" (mechanism))
1000 (declare-function sasl-make-client "sasl" (mechanism name service server))
1001 (declare-function sasl-next-step "sasl" (client step))
1002 (declare-function sasl-step-data "sasl" (step))
1003 (declare-function sasl-step-set-data "sasl" (step data))
1005 (defun imap-sasl-auth-p (buffer)
1006 (and (condition-case ()
1007 (require 'sasl)
1008 (error nil))
1009 (sasl-find-mechanism (imap-sasl-make-mechanisms buffer))))
1011 (defun imap-sasl-auth (buffer)
1012 "Login to server using the SASL method."
1013 (message "imap: Authenticating using SASL...")
1014 (with-current-buffer buffer
1015 (make-local-variable 'imap-username)
1016 (make-local-variable 'imap-sasl-client)
1017 (make-local-variable 'imap-sasl-step)
1018 (let ((mechanism (sasl-find-mechanism (imap-sasl-make-mechanisms buffer)))
1019 logged user)
1020 (while (not logged)
1021 (setq user (or imap-username
1022 (read-from-minibuffer
1023 (concat "IMAP username for " imap-server " using SASL "
1024 (sasl-mechanism-name mechanism) ": ")
1025 (or user imap-default-user))))
1026 (when user
1027 (setq imap-sasl-client (sasl-make-client mechanism user "imap2" imap-server)
1028 imap-sasl-step (sasl-next-step imap-sasl-client nil))
1029 (let ((tag (imap-send-command
1030 (if (sasl-step-data imap-sasl-step)
1031 (format "AUTHENTICATE %s %s"
1032 (sasl-mechanism-name mechanism)
1033 (sasl-step-data imap-sasl-step))
1034 (format "AUTHENTICATE %s" (sasl-mechanism-name mechanism)))
1035 buffer)))
1036 (while (eq (imap-wait-for-tag tag) 'INCOMPLETE)
1037 (sasl-step-set-data imap-sasl-step (base64-decode-string imap-continuation))
1038 (setq imap-continuation nil
1039 imap-sasl-step (sasl-next-step imap-sasl-client imap-sasl-step))
1040 (imap-send-command-1 (if (sasl-step-data imap-sasl-step)
1041 (base64-encode-string (sasl-step-data imap-sasl-step) t)
1042 "")))
1043 (if (imap-ok-p (imap-wait-for-tag tag))
1044 (setq imap-username user
1045 logged t)
1046 (message "Login failed...")
1047 (sit-for 1)))))
1048 logged)))
1050 (defun imap-digest-md5-p (buffer)
1051 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
1052 (condition-case ()
1053 (require 'digest-md5)
1054 (error nil))))
1056 (defun imap-digest-md5-auth (buffer)
1057 "Login to server using the AUTH DIGEST-MD5 method."
1058 (message "imap: Authenticating using DIGEST-MD5...")
1059 (imap-interactive-login
1060 buffer
1061 (lambda (user passwd)
1062 (let ((tag
1063 (imap-send-command
1064 (list
1065 "AUTHENTICATE DIGEST-MD5"
1066 (lambda (challenge)
1067 (digest-md5-parse-digest-challenge
1068 (base64-decode-string challenge))
1069 (let* ((digest-uri
1070 (digest-md5-digest-uri
1071 "imap" (digest-md5-challenge 'realm)))
1072 (response
1073 (digest-md5-digest-response
1074 user passwd digest-uri)))
1075 (base64-encode-string response 'no-line-break))))
1077 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1079 (setq imap-continuation nil)
1080 (imap-send-command-1 "")
1081 (imap-ok-p (imap-wait-for-tag tag)))))))
1083 ;; Server functions:
1085 (defun imap-open-1 (buffer)
1086 (with-current-buffer buffer
1087 (erase-buffer)
1088 (setq imap-current-mailbox nil
1089 imap-current-message nil
1090 imap-state 'initial
1091 imap-process (condition-case ()
1092 (funcall (nth 2 (assq imap-stream
1093 imap-stream-alist))
1094 "imap" buffer imap-server imap-port)
1095 ((error quit) nil)))
1096 (when imap-process
1097 (set-process-filter imap-process 'imap-arrival-filter)
1098 (set-process-sentinel imap-process 'imap-sentinel)
1099 (while (and (eq imap-state 'initial)
1100 (memq (process-status imap-process) '(open run)))
1101 (message "Waiting for response from %s..." imap-server)
1102 (accept-process-output imap-process 1))
1103 (message "Waiting for response from %s...done" imap-server)
1104 (and (memq (process-status imap-process) '(open run))
1105 imap-process))))
1107 (defun imap-open (server &optional port stream auth buffer)
1108 "Open an IMAP connection to host SERVER at PORT returning a buffer.
1109 If PORT is unspecified, a default value is used (143 except
1110 for SSL which use 993).
1111 STREAM indicates the stream to use, see `imap-streams' for available
1112 streams. If nil, it choices the best stream the server is capable of.
1113 AUTH indicates authenticator to use, see `imap-authenticators' for
1114 available authenticators. If nil, it choices the best stream the
1115 server is capable of.
1116 BUFFER can be a buffer or a name of a buffer, which is created if
1117 necessary. If nil, the buffer name is generated."
1118 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
1119 (with-current-buffer (get-buffer-create buffer)
1120 (if (imap-opened buffer)
1121 (imap-close buffer))
1122 (mapc 'make-local-variable imap-local-variables)
1123 (imap-disable-multibyte)
1124 (buffer-disable-undo)
1125 (setq imap-server (or server imap-server))
1126 (setq imap-port (or port imap-port))
1127 (setq imap-auth (or auth imap-auth))
1128 (setq imap-stream (or stream imap-stream))
1129 (message "imap: Connecting to %s..." imap-server)
1130 (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
1131 (imap-open-1 buffer)))
1132 (progn
1133 (message "imap: Connecting to %s...failed" imap-server)
1134 nil)
1135 (when (null imap-stream)
1136 ;; Need to choose stream.
1137 (let ((streams imap-streams))
1138 (while (setq stream (pop streams))
1139 ;; OK to use this stream?
1140 (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
1141 ;; Stream changed?
1142 (if (not (eq imap-default-stream stream))
1143 (with-current-buffer (get-buffer-create
1144 (generate-new-buffer-name " *temp*"))
1145 (mapc 'make-local-variable imap-local-variables)
1146 (imap-disable-multibyte)
1147 (buffer-disable-undo)
1148 (setq imap-server (or server imap-server))
1149 (setq imap-port (or port imap-port))
1150 (setq imap-auth (or auth imap-auth))
1151 (message "imap: Reconnecting with stream `%s'..." stream)
1152 (if (null (let ((imap-stream stream))
1153 (imap-open-1 (current-buffer))))
1154 (progn
1155 (kill-buffer (current-buffer))
1156 (message
1157 "imap: Reconnecting with stream `%s'...failed"
1158 stream))
1159 ;; We're done, kill the first connection
1160 (imap-close buffer)
1161 (let ((name (if (stringp buffer)
1162 buffer
1163 (buffer-name buffer))))
1164 (kill-buffer buffer)
1165 (rename-buffer name)
1166 ;; set the passed buffer to the current one,
1167 ;; so that (imap-opened buffer) later will work
1168 (setq buffer (current-buffer)))
1169 (message "imap: Reconnecting with stream `%s'...done"
1170 stream)
1171 (setq imap-stream stream)
1172 (setq imap-capability nil)
1173 (setq streams nil)))
1174 ;; We're done
1175 (message "imap: Connecting to %s...done" imap-server)
1176 (setq imap-stream stream)
1177 (setq imap-capability nil)
1178 (setq streams nil))))))
1179 (when (imap-opened buffer)
1180 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0)))
1181 ;; (debug "opened+state+auth+buffer" (imap-opened buffer) imap-state imap-auth buffer)
1182 (when imap-stream
1183 buffer))))
1185 (defcustom imap-ping-server t
1186 "If non-nil, check if IMAP is open.
1187 See the function `imap-ping-server'."
1188 :version "23.1" ;; No Gnus
1189 :group 'imap
1190 :type 'boolean)
1192 (defun imap-opened (&optional buffer)
1193 "Return non-nil if connection to imap server in BUFFER is open.
1194 If BUFFER is nil then the current buffer is used."
1195 (and (setq buffer (get-buffer (or buffer (current-buffer))))
1196 (buffer-live-p buffer)
1197 (with-current-buffer buffer
1198 (and imap-process
1199 (memq (process-status imap-process) '(open run))
1200 (if imap-ping-server
1201 (imap-ping-server)
1202 t)))))
1204 (defun imap-ping-server (&optional buffer)
1205 "Ping the IMAP server in BUFFER with a \"NOOP\" command.
1206 Return non-nil if the server responds, and nil if it does not
1207 respond. If BUFFER is nil, the current buffer is used."
1208 (condition-case ()
1209 (imap-ok-p (imap-send-command-wait "NOOP" buffer))
1210 (error nil)))
1212 (defun imap-authenticate (&optional user passwd buffer)
1213 "Authenticate to server in BUFFER, using current buffer if nil.
1214 It uses the authenticator specified when opening the server. If the
1215 authenticator requires username/passwords, they are queried from the
1216 user and optionally stored in the buffer. If USER and/or PASSWD is
1217 specified, the user will not be questioned and the username and/or
1218 password is remembered in the buffer."
1219 (with-current-buffer (or buffer (current-buffer))
1220 (if (not (eq imap-state 'nonauth))
1221 (or (eq imap-state 'auth)
1222 (eq imap-state 'selected)
1223 (eq imap-state 'examine))
1224 (make-local-variable 'imap-username)
1225 (make-local-variable 'imap-password)
1226 (make-local-variable 'imap-last-authenticator)
1227 (when user (setq imap-username user))
1228 (when passwd (setq imap-password passwd))
1229 (if imap-auth
1230 (and (setq imap-last-authenticator
1231 (assq imap-auth imap-authenticator-alist))
1232 (funcall (nth 2 imap-last-authenticator) (current-buffer))
1233 (setq imap-state 'auth))
1234 ;; Choose authenticator.
1235 (let ((auths imap-authenticators)
1236 auth)
1237 (while (setq auth (pop auths))
1238 ;; OK to use authenticator?
1239 (setq imap-last-authenticator
1240 (assq auth imap-authenticator-alist))
1241 (when (funcall (nth 1 imap-last-authenticator) (current-buffer))
1242 (message "imap: Authenticating to `%s' using `%s'..."
1243 imap-server auth)
1244 (setq imap-auth auth)
1245 (if (funcall (nth 2 imap-last-authenticator) (current-buffer))
1246 (progn
1247 (message "imap: Authenticating to `%s' using `%s'...done"
1248 imap-server auth)
1249 ;; set imap-state correctly on successful auth attempt
1250 (setq imap-state 'auth)
1251 ;; stop iterating through the authenticator list
1252 (setq auths nil))
1253 (message "imap: Authenticating to `%s' using `%s'...failed"
1254 imap-server auth)))))
1255 imap-state))))
1257 (defun imap-close (&optional buffer)
1258 "Close connection to server in BUFFER.
1259 If BUFFER is nil, the current buffer is used."
1260 (with-current-buffer (or buffer (current-buffer))
1261 (when (imap-opened)
1262 (condition-case nil
1263 (imap-logout-wait)
1264 (quit nil)))
1265 (when (and imap-process
1266 (memq (process-status imap-process) '(open run)))
1267 (delete-process imap-process))
1268 (setq imap-current-mailbox nil
1269 imap-current-message nil
1270 imap-process nil)
1271 (erase-buffer)
1274 (defun imap-capability (&optional identifier buffer)
1275 "Return a list of identifiers which server in BUFFER support.
1276 If IDENTIFIER, return non-nil if it's among the servers capabilities.
1277 If BUFFER is nil, the current buffer is assumed."
1278 (with-current-buffer (or buffer (current-buffer))
1279 (unless imap-capability
1280 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
1281 (setq imap-capability '(IMAP2))))
1282 (if identifier
1283 (memq (intern (upcase (symbol-name identifier))) imap-capability)
1284 imap-capability)))
1286 (defun imap-id (&optional list-of-values buffer)
1287 "Identify client to server in BUFFER, and return server identity.
1288 LIST-OF-VALUES is nil, or a plist with identifier and value
1289 strings to send to the server to identify the client.
1291 Return a list of identifiers which server in BUFFER support, or
1292 nil if it doesn't support ID or returns no information.
1294 If BUFFER is nil, the current buffer is assumed."
1295 (with-current-buffer (or buffer (current-buffer))
1296 (when (and (imap-capability 'ID)
1297 (imap-ok-p (imap-send-command-wait
1298 (if (null list-of-values)
1299 "ID NIL"
1300 (concat "ID (" (mapconcat (lambda (el)
1301 (concat "\"" el "\""))
1302 list-of-values
1303 " ") ")")))))
1304 imap-id)))
1306 (defun imap-namespace (&optional buffer)
1307 "Return a namespace hierarchy at server in BUFFER.
1308 If BUFFER is nil, the current buffer is assumed."
1309 (with-current-buffer (or buffer (current-buffer))
1310 (unless imap-namespace
1311 (when (imap-capability 'NAMESPACE)
1312 (imap-send-command-wait "NAMESPACE")))
1313 imap-namespace))
1315 (defun imap-send-command-wait (command &optional buffer)
1316 (imap-wait-for-tag (imap-send-command command buffer) buffer))
1318 (defun imap-logout (&optional buffer)
1319 (or buffer (setq buffer (current-buffer)))
1320 (if imap-logout-timeout
1321 (with-timeout (imap-logout-timeout
1322 (condition-case nil
1323 (with-current-buffer buffer
1324 (delete-process imap-process))
1325 (error)))
1326 (imap-send-command "LOGOUT" buffer))
1327 (imap-send-command "LOGOUT" buffer)))
1329 (defun imap-logout-wait (&optional buffer)
1330 (or buffer (setq buffer (current-buffer)))
1331 (if imap-logout-timeout
1332 (with-timeout (imap-logout-timeout
1333 (condition-case nil
1334 (with-current-buffer buffer
1335 (delete-process imap-process))
1336 (error)))
1337 (imap-send-command-wait "LOGOUT" buffer))
1338 (imap-send-command-wait "LOGOUT" buffer)))
1341 ;; Mailbox functions:
1343 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1344 (with-current-buffer (or buffer (current-buffer))
1345 (if imap-mailbox-data
1346 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1347 propname value)
1348 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1349 propname value mailbox (current-buffer)))
1352 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1353 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1354 propname))
1356 (defun imap-mailbox-get (propname &optional mailbox buffer)
1357 (let ((mailbox (imap-utf7-encode mailbox)))
1358 (with-current-buffer (or buffer (current-buffer))
1359 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1361 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1362 (with-current-buffer (or buffer (current-buffer))
1363 (let (result)
1364 (mapatoms
1365 (lambda (s)
1366 (push (funcall func (if mailbox-decoder
1367 (funcall mailbox-decoder (symbol-name s))
1368 (symbol-name s))) result))
1369 imap-mailbox-data)
1370 result)))
1372 (defun imap-mailbox-map (func &optional buffer)
1373 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1374 Function should take a mailbox name (a string) as
1375 the only argument."
1376 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1378 (defun imap-current-mailbox (&optional buffer)
1379 (with-current-buffer (or buffer (current-buffer))
1380 (imap-utf7-decode imap-current-mailbox)))
1382 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1383 (and (string= mailbox imap-current-mailbox)
1384 (or (and examine
1385 (eq imap-state 'examine))
1386 (and (not examine)
1387 (eq imap-state 'selected)))))
1389 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1390 (with-current-buffer (or buffer (current-buffer))
1391 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1393 (defun imap-mailbox-select-1 (mailbox &optional examine)
1394 "Select MAILBOX on server in BUFFER.
1395 If EXAMINE is non-nil, do a read-only select."
1396 (if (imap-current-mailbox-p-1 mailbox examine)
1397 imap-current-mailbox
1398 (setq imap-current-mailbox mailbox)
1399 (if (imap-ok-p (imap-send-command-wait
1400 (concat (if examine "EXAMINE" "SELECT") " \""
1401 mailbox "\"")))
1402 (progn
1403 (setq imap-message-data (make-vector imap-message-prime 0)
1404 imap-state (if examine 'examine 'selected))
1405 imap-current-mailbox)
1406 ;; Failed SELECT/EXAMINE unselects current mailbox
1407 (setq imap-current-mailbox nil))))
1409 (defun imap-mailbox-select (mailbox &optional examine buffer)
1410 (with-current-buffer (or buffer (current-buffer))
1411 (imap-utf7-decode
1412 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1414 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1415 (with-current-buffer (or buffer (current-buffer))
1416 (imap-mailbox-select-1 mailbox 'examine)))
1418 (defun imap-mailbox-examine (mailbox &optional buffer)
1419 "Examine MAILBOX on server in BUFFER."
1420 (imap-mailbox-select mailbox 'examine buffer))
1422 (defun imap-mailbox-unselect (&optional buffer)
1423 "Close current folder in BUFFER, without expunging articles."
1424 (with-current-buffer (or buffer (current-buffer))
1425 (when (or (eq imap-state 'auth)
1426 (and (imap-capability 'UNSELECT)
1427 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1428 (and (imap-ok-p
1429 (imap-send-command-wait (concat "EXAMINE \""
1430 imap-current-mailbox
1431 "\"")))
1432 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1433 (setq imap-current-mailbox nil
1434 imap-message-data nil
1435 imap-state 'auth)
1436 t)))
1438 (defun imap-mailbox-expunge (&optional asynch buffer)
1439 "Expunge articles in current folder in BUFFER.
1440 If ASYNCH, do not wait for successful completion of the command.
1441 If BUFFER is nil the current buffer is assumed."
1442 (with-current-buffer (or buffer (current-buffer))
1443 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1444 (if asynch
1445 (imap-send-command "EXPUNGE")
1446 (imap-ok-p (imap-send-command-wait "EXPUNGE"))))))
1448 (defun imap-mailbox-close (&optional asynch buffer)
1449 "Expunge articles and close current folder in BUFFER.
1450 If ASYNCH, do not wait for successful completion of the command.
1451 If BUFFER is nil the current buffer is assumed."
1452 (with-current-buffer (or buffer (current-buffer))
1453 (when imap-current-mailbox
1454 (if asynch
1455 (imap-add-callback (imap-send-command "CLOSE")
1456 `(lambda (tag status)
1457 (message "IMAP mailbox `%s' closed... %s"
1458 imap-current-mailbox status)
1459 (when (eq ,imap-current-mailbox
1460 imap-current-mailbox)
1461 ;; Don't wipe out data if another mailbox
1462 ;; was selected...
1463 (setq imap-current-mailbox nil
1464 imap-message-data nil
1465 imap-state 'auth))))
1466 (when (imap-ok-p (imap-send-command-wait "CLOSE"))
1467 (setq imap-current-mailbox nil
1468 imap-message-data nil
1469 imap-state 'auth)))
1470 t)))
1472 (defun imap-mailbox-create-1 (mailbox)
1473 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1475 (defun imap-mailbox-create (mailbox &optional buffer)
1476 "Create MAILBOX on server in BUFFER.
1477 If BUFFER is nil the current buffer is assumed."
1478 (with-current-buffer (or buffer (current-buffer))
1479 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1481 (defun imap-mailbox-delete (mailbox &optional buffer)
1482 "Delete MAILBOX on server in BUFFER.
1483 If BUFFER is nil the current buffer is assumed."
1484 (let ((mailbox (imap-utf7-encode mailbox)))
1485 (with-current-buffer (or buffer (current-buffer))
1486 (imap-ok-p
1487 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1489 (defun imap-mailbox-rename (oldname newname &optional buffer)
1490 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1491 If BUFFER is nil the current buffer is assumed."
1492 (let ((oldname (imap-utf7-encode oldname))
1493 (newname (imap-utf7-encode newname)))
1494 (with-current-buffer (or buffer (current-buffer))
1495 (imap-ok-p
1496 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1497 "\"" newname "\""))))))
1499 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1500 "Return a list of subscribed mailboxes on server in BUFFER.
1501 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1502 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1503 implementation-specific string that has to be passed to lsub command."
1504 (with-current-buffer (or buffer (current-buffer))
1505 ;; Make sure we know the hierarchy separator for root's hierarchy
1506 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1507 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1508 (imap-utf7-encode root) "\"")))
1509 ;; clear list data (NB not delimiter and other stuff)
1510 (imap-mailbox-map-1 (lambda (mailbox)
1511 (imap-mailbox-put 'lsub nil mailbox)))
1512 (when (imap-ok-p
1513 (imap-send-command-wait
1514 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1515 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1516 "%\"")))
1517 (let (out)
1518 (imap-mailbox-map-1 (lambda (mailbox)
1519 (when (imap-mailbox-get-1 'lsub mailbox)
1520 (push (imap-utf7-decode mailbox) out))))
1521 (nreverse out)))))
1523 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1524 "Return a list of mailboxes matching ROOT on server in BUFFER.
1525 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1526 root. REFERENCE is a implementation-specific string that has to be
1527 passed to list command."
1528 (with-current-buffer (or buffer (current-buffer))
1529 ;; Make sure we know the hierarchy separator for root's hierarchy
1530 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1531 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1532 (imap-utf7-encode root) "\"")))
1533 ;; clear list data (NB not delimiter and other stuff)
1534 (imap-mailbox-map-1 (lambda (mailbox)
1535 (imap-mailbox-put 'list nil mailbox)))
1536 (when (imap-ok-p
1537 (imap-send-command-wait
1538 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1539 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1540 "%\"")))
1541 (let (out)
1542 (imap-mailbox-map-1 (lambda (mailbox)
1543 (when (imap-mailbox-get-1 'list mailbox)
1544 (push (imap-utf7-decode mailbox) out))))
1545 (nreverse out)))))
1547 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1548 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1549 Returns non-nil if successful."
1550 (with-current-buffer (or buffer (current-buffer))
1551 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1552 (imap-utf7-encode mailbox)
1553 "\"")))))
1555 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1556 "Send the SUBSCRIBE command on the MAILBOX to server in BUFFER.
1557 Returns non-nil if successful."
1558 (with-current-buffer (or buffer (current-buffer))
1559 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1560 (imap-utf7-encode mailbox)
1561 "\"")))))
1563 (defun imap-mailbox-status (mailbox items &optional buffer)
1564 "Get status items ITEM in MAILBOX from server in BUFFER.
1565 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1566 the STATUS data items -- i.e. `messages', `recent', `uidnext', `uidvalidity',
1567 or `unseen'. If ITEMS is a list of symbols, a list of values is
1568 returned, if ITEMS is a symbol only its value is returned."
1569 (with-current-buffer (or buffer (current-buffer))
1570 (when (imap-ok-p
1571 (imap-send-command-wait (list "STATUS \""
1572 (imap-utf7-encode mailbox)
1573 "\" "
1574 (upcase
1575 (format "%s"
1576 (if (listp items)
1577 items
1578 (list items)))))))
1579 (if (listp items)
1580 (mapcar (lambda (item)
1581 (imap-mailbox-get item mailbox))
1582 items)
1583 (imap-mailbox-get items mailbox)))))
1585 (defun imap-mailbox-status-asynch (mailbox items &optional buffer)
1586 "Send status item request ITEM on MAILBOX to server in BUFFER.
1587 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1588 the STATUS data items -- i.e. 'messages, 'recent, 'uidnext, 'uidvalidity
1589 or 'unseen. The IMAP command tag is returned."
1590 (with-current-buffer (or buffer (current-buffer))
1591 (imap-send-command (list "STATUS \""
1592 (imap-utf7-encode mailbox)
1593 "\" "
1594 (upcase
1595 (format "%s"
1596 (if (listp items)
1597 items
1598 (list items))))))))
1600 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1601 "Get ACL on MAILBOX from server in BUFFER."
1602 (let ((mailbox (imap-utf7-encode mailbox)))
1603 (with-current-buffer (or buffer (current-buffer))
1604 (when (imap-ok-p
1605 (imap-send-command-wait (list "GETACL \""
1606 (or mailbox imap-current-mailbox)
1607 "\"")))
1608 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1610 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1611 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1612 (let ((mailbox (imap-utf7-encode mailbox)))
1613 (with-current-buffer (or buffer (current-buffer))
1614 (imap-ok-p
1615 (imap-send-command-wait (list "SETACL \""
1616 (or mailbox imap-current-mailbox)
1617 "\" "
1618 identifier
1620 rights))))))
1622 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1623 "Remove any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1624 (let ((mailbox (imap-utf7-encode mailbox)))
1625 (with-current-buffer (or buffer (current-buffer))
1626 (imap-ok-p
1627 (imap-send-command-wait (list "DELETEACL \""
1628 (or mailbox imap-current-mailbox)
1629 "\" "
1630 identifier))))))
1633 ;; Message functions:
1635 (defun imap-current-message (&optional buffer)
1636 (with-current-buffer (or buffer (current-buffer))
1637 imap-current-message))
1639 (defun imap-list-to-message-set (list)
1640 (mapconcat (lambda (item)
1641 (number-to-string item))
1642 (if (listp list)
1643 list
1644 (list list))
1645 ","))
1647 (defun imap-range-to-message-set (range)
1648 (mapconcat
1649 (lambda (item)
1650 (if (consp item)
1651 (format "%d:%d"
1652 (car item) (cdr item))
1653 (format "%d" item)))
1654 (if (and (listp range) (not (listp (cdr range))))
1655 (list range) ;; make (1 . 2) into ((1 . 2))
1656 range)
1657 ","))
1659 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1660 (with-current-buffer (or buffer (current-buffer))
1661 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1662 (if (listp uids)
1663 (imap-list-to-message-set uids)
1664 uids)
1665 props))))
1667 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1668 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1669 UIDS can be a string, number or a list of numbers. If RECEIVE
1670 is non-nil return these properties."
1671 (with-current-buffer (or buffer (current-buffer))
1672 (when (imap-ok-p (imap-send-command-wait
1673 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1674 (if (listp uids)
1675 (imap-list-to-message-set uids)
1676 uids)
1677 props)))
1678 (if (or (null receive) (stringp uids))
1680 (if (listp uids)
1681 (mapcar (lambda (uid)
1682 (if (listp receive)
1683 (mapcar (lambda (prop)
1684 (imap-message-get uid prop))
1685 receive)
1686 (imap-message-get uid receive)))
1687 uids)
1688 (imap-message-get uids receive))))))
1690 (defun imap-message-put (uid propname value &optional buffer)
1691 (with-current-buffer (or buffer (current-buffer))
1692 (if imap-message-data
1693 (put (intern (number-to-string uid) imap-message-data)
1694 propname value)
1695 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1696 uid propname value (current-buffer)))
1699 (defun imap-message-get (uid propname &optional buffer)
1700 (with-current-buffer (or buffer (current-buffer))
1701 (get (intern-soft (number-to-string uid) imap-message-data)
1702 propname)))
1704 (defun imap-message-map (func propname &optional buffer)
1705 "Map a function across each message in `imap-message-data', returning a list."
1706 (with-current-buffer (or buffer (current-buffer))
1707 (let (result)
1708 (mapatoms
1709 (lambda (s)
1710 (push (funcall func (get s 'UID) (get s propname)) result))
1711 imap-message-data)
1712 result)))
1714 (defmacro imap-message-envelope-date (uid &optional buffer)
1715 `(with-current-buffer (or ,buffer (current-buffer))
1716 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1718 (defmacro imap-message-envelope-subject (uid &optional buffer)
1719 `(with-current-buffer (or ,buffer (current-buffer))
1720 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1722 (defmacro imap-message-envelope-from (uid &optional buffer)
1723 `(with-current-buffer (or ,buffer (current-buffer))
1724 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1726 (defmacro imap-message-envelope-sender (uid &optional buffer)
1727 `(with-current-buffer (or ,buffer (current-buffer))
1728 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1730 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1731 `(with-current-buffer (or ,buffer (current-buffer))
1732 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1734 (defmacro imap-message-envelope-to (uid &optional buffer)
1735 `(with-current-buffer (or ,buffer (current-buffer))
1736 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1738 (defmacro imap-message-envelope-cc (uid &optional buffer)
1739 `(with-current-buffer (or ,buffer (current-buffer))
1740 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1742 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1743 `(with-current-buffer (or ,buffer (current-buffer))
1744 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1746 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1747 `(with-current-buffer (or ,buffer (current-buffer))
1748 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1750 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1751 `(with-current-buffer (or ,buffer (current-buffer))
1752 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1754 (defmacro imap-message-body (uid &optional buffer)
1755 `(with-current-buffer (or ,buffer (current-buffer))
1756 (imap-message-get ,uid 'BODY)))
1758 ;; FIXME: Should this try to use CHARSET? -- fx
1759 (defun imap-search (predicate &optional buffer)
1760 (with-current-buffer (or buffer (current-buffer))
1761 (imap-mailbox-put 'search 'dummy)
1762 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1763 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1764 (progn
1765 (message "Missing SEARCH response to a SEARCH command (server not RFC compliant)...")
1766 nil)
1767 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1769 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1770 "Return t if FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1771 (with-current-buffer (or buffer (current-buffer))
1772 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1773 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1775 (defun imap-message-flags-set (articles flags &optional silent buffer)
1776 (when (and articles flags)
1777 (with-current-buffer (or buffer (current-buffer))
1778 (imap-ok-p (imap-send-command-wait
1779 (concat "UID STORE " articles
1780 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1782 (defun imap-message-flags-del (articles flags &optional silent buffer)
1783 (when (and articles flags)
1784 (with-current-buffer (or buffer (current-buffer))
1785 (imap-ok-p (imap-send-command-wait
1786 (concat "UID STORE " articles
1787 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1789 (defun imap-message-flags-add (articles flags &optional silent buffer)
1790 (when (and articles flags)
1791 (with-current-buffer (or buffer (current-buffer))
1792 (imap-ok-p (imap-send-command-wait
1793 (concat "UID STORE " articles
1794 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1796 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/65317/focus=65343
1797 ;; Signal an error if we'd get an integer overflow.
1799 ;; FIXME: Identify relevant calls to `string-to-number' and replace them with
1800 ;; `imap-string-to-integer'.
1801 (defun imap-string-to-integer (string &optional base)
1802 (let ((number (string-to-number string base)))
1803 (if (> number most-positive-fixnum)
1804 (error
1805 (format "String %s cannot be converted to a Lisp integer" number))
1806 number)))
1808 (defun imap-fetch-safe (uids props &optional receive nouidfetch buffer)
1809 "Like `imap-fetch', but DTRT with Exchange 2007 bug.
1810 However, UIDS here is a cons, where the car is the canonical form
1811 of the UIDS specification, and the cdr is the one which works with
1812 Exchange 2007 or, potentially, other buggy servers.
1813 See `imap-enable-exchange-bug-workaround'."
1814 ;; The first time we get here for a given, we'll try the canonical
1815 ;; form. If we get the known error from the buggy server, set the
1816 ;; flag buffer-locally (to account for connections to multiple
1817 ;; servers), then re-try with the alternative UIDS spec. We don't
1818 ;; unconditionally use the alternative form, since the
1819 ;; currently-used alternatives are seriously inefficient with some
1820 ;; servers (although they are valid).
1822 ;; FIXME: Maybe it would be cleaner to have a flag to not signal
1823 ;; the error (which otherwise gives a message), and test
1824 ;; `imap-failed-tags'. Also, Other IMAP clients use other forms of
1825 ;; request which work with Exchange, e.g. Claws does "UID FETCH 1:*
1826 ;; (UID)" rather than "FETCH UID 1,*". Is there a good reason not
1827 ;; to do the same?
1828 (condition-case data
1829 ;; Binding `debug-on-error' allows us to get the error from
1830 ;; `imap-parse-response' -- it's normally caught by Emacs around
1831 ;; execution of a process filter.
1832 (let ((debug-on-error t))
1833 (imap-fetch (if imap-enable-exchange-bug-workaround
1834 (cdr uids)
1835 (car uids))
1836 props receive nouidfetch buffer))
1837 (error
1838 (if (and (not imap-enable-exchange-bug-workaround)
1839 ;; This is the Exchange 2007 response. It may be more
1840 ;; robust just to check for a BAD response to the
1841 ;; attempted fetch.
1842 (string-match "The specified message set is invalid"
1843 (cadr data)))
1844 (with-current-buffer (or buffer (current-buffer))
1845 (set (make-local-variable 'imap-enable-exchange-bug-workaround)
1847 (imap-fetch (cdr uids) props receive nouidfetch))
1848 (signal (car data) (cdr data))))))
1850 (defun imap-message-copyuid-1 (mailbox)
1851 (if (imap-capability 'UIDPLUS)
1852 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1853 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1854 (let ((old-mailbox imap-current-mailbox)
1855 (state imap-state)
1856 (imap-message-data (make-vector 2 0)))
1857 (when (imap-mailbox-examine-1 mailbox)
1858 (prog1
1859 (and (imap-fetch-safe '("*" . "*:*") "UID")
1860 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1861 (apply 'max (imap-message-map
1862 (lambda (uid prop) uid) 'UID))))
1863 (if old-mailbox
1864 (imap-mailbox-select old-mailbox (eq state 'examine))
1865 (imap-mailbox-unselect)))))))
1867 (defun imap-message-copyuid (mailbox &optional buffer)
1868 (with-current-buffer (or buffer (current-buffer))
1869 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1871 (defun imap-message-copy (articles mailbox
1872 &optional dont-create no-copyuid buffer)
1873 "Copy ARTICLES to MAILBOX on server in BUFFER.
1874 ARTICLES is a string message set. Create mailbox if it doesn't exist,
1875 unless DONT-CREATE is non-nil. On success, return a list with
1876 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1877 first element. The rest of list contains the saved articles' UIDs."
1878 (when articles
1879 (with-current-buffer (or buffer (current-buffer))
1880 (let ((mailbox (imap-utf7-encode mailbox)))
1881 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1882 (imap-current-target-mailbox mailbox))
1883 (if (imap-ok-p (imap-send-command-wait cmd))
1885 (when (and (not dont-create)
1886 ;; removed because of buggy Oracle server
1887 ;; that doesn't send TRYCREATE tags (which
1888 ;; is a MUST according to specifications):
1889 ;;(imap-mailbox-get-1 'trycreate mailbox)
1890 (imap-mailbox-create-1 mailbox))
1891 (imap-ok-p (imap-send-command-wait cmd)))))
1892 (or no-copyuid
1893 (imap-message-copyuid-1 mailbox)))))))
1895 ;; FIXME: Amalgamate with imap-message-copyuid-1, using an extra arg, since it
1896 ;; shares most of the code? -- fx
1897 (defun imap-message-appenduid-1 (mailbox)
1898 (if (imap-capability 'UIDPLUS)
1899 (imap-mailbox-get-1 'appenduid mailbox)
1900 (let ((old-mailbox imap-current-mailbox)
1901 (state imap-state)
1902 (imap-message-data (make-vector 2 0)))
1903 (when (imap-mailbox-examine-1 mailbox)
1904 (prog1
1905 (and (imap-fetch-safe '("*" . "*:*") "UID")
1906 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1907 (apply 'max (imap-message-map
1908 (lambda (uid prop) uid) 'UID))))
1909 (if old-mailbox
1910 (imap-mailbox-select old-mailbox (eq state 'examine))
1911 (imap-mailbox-unselect)))))))
1913 (defun imap-message-appenduid (mailbox &optional buffer)
1914 (with-current-buffer (or buffer (current-buffer))
1915 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1917 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1918 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1919 FLAGS and DATE-TIME is currently not used. Return a cons holding
1920 uidvalidity of MAILBOX and UID the newly created article got, or nil
1921 on failure."
1922 (let ((mailbox (imap-utf7-encode mailbox)))
1923 (with-current-buffer (or buffer (current-buffer))
1924 (and (let ((imap-current-target-mailbox mailbox))
1925 (imap-ok-p
1926 (imap-send-command-wait
1927 (list "APPEND \"" mailbox "\" " article))))
1928 (imap-message-appenduid-1 mailbox)))))
1930 (defun imap-body-lines (body)
1931 "Return number of lines in article by looking at the mime bodystructure BODY."
1932 (if (listp body)
1933 (if (stringp (car body))
1934 (cond ((and (string= (upcase (car body)) "TEXT")
1935 (numberp (nth 7 body)))
1936 (nth 7 body))
1937 ((and (string= (upcase (car body)) "MESSAGE")
1938 (numberp (nth 9 body)))
1939 (nth 9 body))
1940 (t 0))
1941 (apply '+ (mapcar 'imap-body-lines body)))
1944 (defun imap-envelope-from (from)
1945 "Return a from string line."
1946 (and from
1947 (concat (aref from 0)
1948 (if (aref from 0) " <")
1949 (aref from 2)
1951 (aref from 3)
1952 (if (aref from 0) ">"))))
1955 ;; Internal functions.
1957 (defun imap-add-callback (tag func)
1958 (setq imap-callbacks (append (list (cons tag func)) imap-callbacks)))
1960 (defun imap-send-command-1 (cmdstr)
1961 (setq cmdstr (concat cmdstr imap-client-eol))
1962 (and imap-log
1963 (with-current-buffer (get-buffer-create imap-log-buffer)
1964 (imap-disable-multibyte)
1965 (buffer-disable-undo)
1966 (goto-char (point-max))
1967 (insert cmdstr)))
1968 (process-send-string imap-process cmdstr))
1970 (defun imap-send-command (command &optional buffer)
1971 (with-current-buffer (or buffer (current-buffer))
1972 (if (not (listp command)) (setq command (list command)))
1973 (let ((tag (setq imap-tag (1+ imap-tag)))
1974 cmd cmdstr)
1975 (setq cmdstr (concat (number-to-string imap-tag) " "))
1976 (while (setq cmd (pop command))
1977 (cond ((stringp cmd)
1978 (setq cmdstr (concat cmdstr cmd)))
1979 ((bufferp cmd)
1980 (let ((eol imap-client-eol)
1981 (calcfirst imap-calculate-literal-size-first)
1982 size)
1983 (with-current-buffer cmd
1984 (if calcfirst
1985 (setq size (buffer-size)))
1986 (when (not (equal eol "\r\n"))
1987 ;; XXX modifies buffer!
1988 (goto-char (point-min))
1989 (while (search-forward "\r\n" nil t)
1990 (replace-match eol)))
1991 (if (not calcfirst)
1992 (setq size (buffer-size))))
1993 (setq cmdstr
1994 (concat cmdstr (format "{%d}" size))))
1995 (unwind-protect
1996 (progn
1997 (imap-send-command-1 cmdstr)
1998 (setq cmdstr nil)
1999 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
2000 (setq command nil) ;; abort command if no cont-req
2001 (let ((process imap-process)
2002 (stream imap-stream)
2003 (eol imap-client-eol))
2004 (with-current-buffer cmd
2005 (and imap-log
2006 (with-current-buffer (get-buffer-create
2007 imap-log-buffer)
2008 (imap-disable-multibyte)
2009 (buffer-disable-undo)
2010 (goto-char (point-max))
2011 (insert-buffer-substring cmd)))
2012 (process-send-region process (point-min)
2013 (point-max)))
2014 (process-send-string process imap-client-eol))))
2015 (setq imap-continuation nil)))
2016 ((functionp cmd)
2017 (imap-send-command-1 cmdstr)
2018 (setq cmdstr nil)
2019 (unwind-protect
2020 (setq command
2021 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
2022 nil ;; abort command if no cont-req
2023 (cons (funcall cmd imap-continuation)
2024 command)))
2025 (setq imap-continuation nil)))
2027 (error "Unknown command type"))))
2028 (if cmdstr
2029 (imap-send-command-1 cmdstr))
2030 tag)))
2032 (defun imap-wait-for-tag (tag &optional buffer)
2033 (with-current-buffer (or buffer (current-buffer))
2034 (let (imap-have-messaged)
2035 (while (and (null imap-continuation)
2036 (memq (process-status imap-process) '(open run))
2037 (< imap-reached-tag tag))
2038 (let ((len (/ (buffer-size) 1024))
2039 message-log-max)
2040 (unless (< len 10)
2041 (setq imap-have-messaged t)
2042 (message "imap read: %dk" len))
2043 (accept-process-output imap-process
2044 (truncate imap-read-timeout)
2045 (truncate (* (- imap-read-timeout
2046 (truncate imap-read-timeout))
2047 1000)))))
2048 ;; A process can die _before_ we have processed everything it
2049 ;; has to say. Moreover, this can happen in between the call to
2050 ;; accept-process-output and the call to process-status in an
2051 ;; iteration of the loop above.
2052 (when (and (null imap-continuation)
2053 (< imap-reached-tag tag))
2054 (accept-process-output imap-process 0 0))
2055 (when imap-have-messaged
2056 (message ""))
2057 (and (memq (process-status imap-process) '(open run))
2058 (or (assq tag imap-failed-tags)
2059 (if imap-continuation
2060 'INCOMPLETE
2061 'OK))))))
2063 (defun imap-sentinel (process string)
2064 (delete-process process))
2066 (defun imap-find-next-line ()
2067 "Return point at end of current line, taking into account literals.
2068 Return nil if no complete line has arrived."
2069 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
2070 imap-server-eol)
2071 nil t)
2072 (if (match-string 1)
2073 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
2075 (goto-char (+ (point) (string-to-number (match-string 1))))
2076 (imap-find-next-line))
2077 (point))))
2079 (defun imap-arrival-filter (proc string)
2080 "IMAP process filter."
2081 ;; Sometimes, we are called even though the process has died.
2082 ;; Better abstain from doing stuff in that case.
2083 (when (buffer-name (process-buffer proc))
2084 (with-current-buffer (process-buffer proc)
2085 (goto-char (point-max))
2086 (insert string)
2087 (and imap-log
2088 (with-current-buffer (get-buffer-create imap-log-buffer)
2089 (imap-disable-multibyte)
2090 (buffer-disable-undo)
2091 (goto-char (point-max))
2092 (insert string)))
2093 (let (end)
2094 (goto-char (point-min))
2095 (while (setq end (imap-find-next-line))
2096 (save-restriction
2097 (narrow-to-region (point-min) end)
2098 (delete-backward-char (length imap-server-eol))
2099 (goto-char (point-min))
2100 (unwind-protect
2101 (cond ((eq imap-state 'initial)
2102 (imap-parse-greeting))
2103 ((or (eq imap-state 'auth)
2104 (eq imap-state 'nonauth)
2105 (eq imap-state 'selected)
2106 (eq imap-state 'examine))
2107 (imap-parse-response))
2109 (message "Unknown state %s in arrival filter"
2110 imap-state)))
2111 (delete-region (point-min) (point-max)))))))))
2114 ;; Imap parser.
2116 (defsubst imap-forward ()
2117 (or (eobp) (forward-char)))
2119 ;; number = 1*DIGIT
2120 ;; ; Unsigned 32-bit integer
2121 ;; ; (0 <= n < 4,294,967,296)
2123 (defsubst imap-parse-number ()
2124 (when (looking-at "[0-9]+")
2125 (prog1
2126 (string-to-number (match-string 0))
2127 (goto-char (match-end 0)))))
2129 ;; literal = "{" number "}" CRLF *CHAR8
2130 ;; ; Number represents the number of CHAR8s
2132 (defsubst imap-parse-literal ()
2133 (when (looking-at "{\\([0-9]+\\)}\r\n")
2134 (let ((pos (match-end 0))
2135 (len (string-to-number (match-string 1))))
2136 (if (< (point-max) (+ pos len))
2138 (goto-char (+ pos len))
2139 (buffer-substring pos (+ pos len))))))
2141 ;; string = quoted / literal
2143 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
2145 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2146 ;; "\" quoted-specials
2148 ;; quoted-specials = DQUOTE / "\"
2150 ;; TEXT-CHAR = <any CHAR except CR and LF>
2152 (defsubst imap-parse-string ()
2153 (cond ((eq (char-after) ?\")
2154 (forward-char 1)
2155 (let ((p (point)) (name ""))
2156 (skip-chars-forward "^\"\\\\")
2157 (setq name (buffer-substring p (point)))
2158 (while (eq (char-after) ?\\)
2159 (setq p (1+ (point)))
2160 (forward-char 2)
2161 (skip-chars-forward "^\"\\\\")
2162 (setq name (concat name (buffer-substring p (point)))))
2163 (forward-char 1)
2164 name))
2165 ((eq (char-after) ?{)
2166 (imap-parse-literal))))
2168 ;; nil = "NIL"
2170 (defsubst imap-parse-nil ()
2171 (if (looking-at "NIL")
2172 (goto-char (match-end 0))))
2174 ;; nstring = string / nil
2176 (defsubst imap-parse-nstring ()
2177 (or (imap-parse-string)
2178 (and (imap-parse-nil)
2179 nil)))
2181 ;; astring = atom / string
2183 ;; atom = 1*ATOM-CHAR
2185 ;; ATOM-CHAR = <any CHAR except atom-specials>
2187 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
2188 ;; quoted-specials
2190 ;; list-wildcards = "%" / "*"
2192 ;; quoted-specials = DQUOTE / "\"
2194 (defsubst imap-parse-astring ()
2195 (or (imap-parse-string)
2196 (buffer-substring (point)
2197 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
2198 (goto-char (1- (match-end 0)))
2199 (end-of-line)
2200 (point)))))
2202 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
2203 ;; addr-host ")"
2205 ;; addr-adl = nstring
2206 ;; ; Holds route from [RFC-822] route-addr if
2207 ;; ; non-nil
2209 ;; addr-host = nstring
2210 ;; ; nil indicates [RFC-822] group syntax.
2211 ;; ; Otherwise, holds [RFC-822] domain name
2213 ;; addr-mailbox = nstring
2214 ;; ; nil indicates end of [RFC-822] group; if
2215 ;; ; non-nil and addr-host is nil, holds
2216 ;; ; [RFC-822] group name.
2217 ;; ; Otherwise, holds [RFC-822] local-part
2218 ;; ; after removing [RFC-822] quoting
2220 ;; addr-name = nstring
2221 ;; ; If non-nil, holds phrase from [RFC-822]
2222 ;; ; mailbox after removing [RFC-822] quoting
2225 (defsubst imap-parse-address ()
2226 (let (address)
2227 (when (eq (char-after) ?\()
2228 (imap-forward)
2229 (setq address (vector (prog1 (imap-parse-nstring)
2230 (imap-forward))
2231 (prog1 (imap-parse-nstring)
2232 (imap-forward))
2233 (prog1 (imap-parse-nstring)
2234 (imap-forward))
2235 (imap-parse-nstring)))
2236 (when (eq (char-after) ?\))
2237 (imap-forward)
2238 address))))
2240 ;; address-list = "(" 1*address ")" / nil
2242 ;; nil = "NIL"
2244 (defsubst imap-parse-address-list ()
2245 (if (eq (char-after) ?\()
2246 (let (address addresses)
2247 (imap-forward)
2248 (while (and (not (eq (char-after) ?\)))
2249 ;; next line for MS Exchange bug
2250 (progn (and (eq (char-after) ? ) (imap-forward)) t)
2251 (setq address (imap-parse-address)))
2252 (setq addresses (cons address addresses)))
2253 (when (eq (char-after) ?\))
2254 (imap-forward)
2255 (nreverse addresses)))
2256 ;; With assert, the code might not be eval'd.
2257 ;; (assert (imap-parse-nil) t "In imap-parse-address-list")
2258 (imap-parse-nil)))
2260 ;; mailbox = "INBOX" / astring
2261 ;; ; INBOX is case-insensitive. All case variants of
2262 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
2263 ;; ; not as an astring. An astring which consists of
2264 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
2265 ;; ; is considered to be INBOX and not an astring.
2266 ;; ; Refer to section 5.1 for further
2267 ;; ; semantic details of mailbox names.
2269 (defsubst imap-parse-mailbox ()
2270 (let ((mailbox (imap-parse-astring)))
2271 (if (string-equal "INBOX" (upcase mailbox))
2272 "INBOX"
2273 mailbox)))
2275 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
2277 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
2278 ;; ; Authentication condition
2280 ;; resp-cond-bye = "BYE" SP resp-text
2282 (defun imap-parse-greeting ()
2283 "Parse an IMAP greeting."
2284 (cond ((looking-at "\\* OK ")
2285 (setq imap-state 'nonauth))
2286 ((looking-at "\\* PREAUTH ")
2287 (setq imap-state 'auth))
2288 ((looking-at "\\* BYE ")
2289 (setq imap-state 'closed))))
2291 ;; response = *(continue-req / response-data) response-done
2293 ;; continue-req = "+" SP (resp-text / base64) CRLF
2295 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
2296 ;; mailbox-data / message-data / capability-data) CRLF
2298 ;; response-done = response-tagged / response-fatal
2300 ;; response-fatal = "*" SP resp-cond-bye CRLF
2301 ;; ; Server closes connection immediately
2303 ;; response-tagged = tag SP resp-cond-state CRLF
2305 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
2306 ;; ; Status condition
2308 ;; resp-cond-bye = "BYE" SP resp-text
2310 ;; mailbox-data = "FLAGS" SP flag-list /
2311 ;; "LIST" SP mailbox-list /
2312 ;; "LSUB" SP mailbox-list /
2313 ;; "SEARCH" *(SP nz-number) /
2314 ;; "STATUS" SP mailbox SP "("
2315 ;; [status-att SP number *(SP status-att SP number)] ")" /
2316 ;; number SP "EXISTS" /
2317 ;; number SP "RECENT"
2319 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
2321 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
2322 ;; *(SP capability)
2323 ;; ; IMAP4rev1 servers which offer RFC 1730
2324 ;; ; compatibility MUST list "IMAP4" as the first
2325 ;; ; capability.
2327 (defun imap-parse-response ()
2328 "Parse a IMAP command response."
2329 (let (token)
2330 (case (setq token (read (current-buffer)))
2331 (+ (setq imap-continuation
2332 (or (buffer-substring (min (point-max) (1+ (point)))
2333 (point-max))
2334 t)))
2335 (* (case (prog1 (setq token (read (current-buffer)))
2336 (imap-forward))
2337 (OK (imap-parse-resp-text))
2338 (NO (imap-parse-resp-text))
2339 (BAD (imap-parse-resp-text))
2340 (BYE (imap-parse-resp-text))
2341 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
2342 (LIST (imap-parse-data-list 'list))
2343 (LSUB (imap-parse-data-list 'lsub))
2344 (SEARCH (imap-mailbox-put
2345 'search
2346 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
2347 (STATUS (imap-parse-status))
2348 (CAPABILITY (setq imap-capability
2349 (read (concat "(" (upcase (buffer-substring
2350 (point) (point-max)))
2351 ")"))))
2352 (ID (setq imap-id (read (buffer-substring (point)
2353 (point-max)))))
2354 (ACL (imap-parse-acl))
2355 (t (case (prog1 (read (current-buffer))
2356 (imap-forward))
2357 (EXISTS (imap-mailbox-put 'exists token))
2358 (RECENT (imap-mailbox-put 'recent token))
2359 (EXPUNGE t)
2360 (FETCH (imap-parse-fetch token))
2361 (t (message "Garbage: %s" (buffer-string)))))))
2362 (t (let (status)
2363 (if (not (integerp token))
2364 (message "Garbage: %s" (buffer-string))
2365 (case (prog1 (setq status (read (current-buffer)))
2366 (imap-forward))
2367 (OK (progn
2368 (setq imap-reached-tag (max imap-reached-tag token))
2369 (imap-parse-resp-text)))
2370 (NO (progn
2371 (setq imap-reached-tag (max imap-reached-tag token))
2372 (save-excursion
2373 (imap-parse-resp-text))
2374 (let (code text)
2375 (when (eq (char-after) ?\[)
2376 (setq code (buffer-substring (point)
2377 (search-forward "]")))
2378 (imap-forward))
2379 (setq text (buffer-substring (point) (point-max)))
2380 (push (list token status code text)
2381 imap-failed-tags))))
2382 (BAD (progn
2383 (setq imap-reached-tag (max imap-reached-tag token))
2384 (save-excursion
2385 (imap-parse-resp-text))
2386 (let (code text)
2387 (when (eq (char-after) ?\[)
2388 (setq code (buffer-substring (point)
2389 (search-forward "]")))
2390 (imap-forward))
2391 (setq text (buffer-substring (point) (point-max)))
2392 (push (list token status code text) imap-failed-tags)
2393 (error "Internal error, tag %s status %s code %s text %s"
2394 token status code text))))
2395 (t (message "Garbage: %s" (buffer-string))))
2396 (when (assq token imap-callbacks)
2397 (funcall (cdr (assq token imap-callbacks)) token status)
2398 (setq imap-callbacks
2399 (imap-remassoc token imap-callbacks)))))))))
2401 ;; resp-text = ["[" resp-text-code "]" SP] text
2403 ;; text = 1*TEXT-CHAR
2405 ;; TEXT-CHAR = <any CHAR except CR and LF>
2407 (defun imap-parse-resp-text ()
2408 (imap-parse-resp-text-code))
2410 ;; resp-text-code = "ALERT" /
2411 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
2412 ;; "NEWNAME" SP string SP string /
2413 ;; "PARSE" /
2414 ;; "PERMANENTFLAGS" SP "("
2415 ;; [flag-perm *(SP flag-perm)] ")" /
2416 ;; "READ-ONLY" /
2417 ;; "READ-WRITE" /
2418 ;; "TRYCREATE" /
2419 ;; "UIDNEXT" SP nz-number /
2420 ;; "UIDVALIDITY" SP nz-number /
2421 ;; "UNSEEN" SP nz-number /
2422 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
2424 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
2426 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
2428 ;; set = sequence-num / (sequence-num ":" sequence-num) /
2429 ;; (set "," set)
2430 ;; ; Identifies a set of messages. For message
2431 ;; ; sequence numbers, these are consecutive
2432 ;; ; numbers from 1 to the number of messages in
2433 ;; ; the mailbox
2434 ;; ; Comma delimits individual numbers, colon
2435 ;; ; delimits between two numbers inclusive.
2436 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
2437 ;; ; 14,15 for a mailbox with 15 messages.
2439 ;; sequence-num = nz-number / "*"
2440 ;; ; * is the largest number in use. For message
2441 ;; ; sequence numbers, it is the number of messages
2442 ;; ; in the mailbox. For unique identifiers, it is
2443 ;; ; the unique identifier of the last message in
2444 ;; ; the mailbox.
2446 ;; flag-perm = flag / "\*"
2448 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2449 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2450 ;; ; Does not include "\Recent"
2452 ;; flag-extension = "\" atom
2453 ;; ; Future expansion. Client implementations
2454 ;; ; MUST accept flag-extension flags. Server
2455 ;; ; implementations MUST NOT generate
2456 ;; ; flag-extension flags except as defined by
2457 ;; ; future standard or standards-track
2458 ;; ; revisions of this specification.
2460 ;; flag-keyword = atom
2462 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
2464 (defun imap-parse-resp-text-code ()
2465 ;; xxx next line for stalker communigate pro 3.3.1 bug
2466 (when (looking-at " \\[")
2467 (imap-forward))
2468 (when (eq (char-after) ?\[)
2469 (imap-forward)
2470 (cond ((search-forward "PERMANENTFLAGS " nil t)
2471 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2472 ((search-forward "UIDNEXT \\([0-9]+\\)" nil t)
2473 (imap-mailbox-put 'uidnext (match-string 1)))
2474 ((search-forward "UNSEEN " nil t)
2475 (imap-mailbox-put 'first-unseen (read (current-buffer))))
2476 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2477 (imap-mailbox-put 'uidvalidity (match-string 1)))
2478 ((search-forward "READ-ONLY" nil t)
2479 (imap-mailbox-put 'read-only t))
2480 ((search-forward "NEWNAME " nil t)
2481 (let (oldname newname)
2482 (setq oldname (imap-parse-string))
2483 (imap-forward)
2484 (setq newname (imap-parse-string))
2485 (imap-mailbox-put 'newname newname oldname)))
2486 ((search-forward "TRYCREATE" nil t)
2487 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2488 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2489 (imap-mailbox-put 'appenduid
2490 (list (match-string 1)
2491 (string-to-number (match-string 2)))
2492 imap-current-target-mailbox))
2493 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2494 (imap-mailbox-put 'copyuid (list (match-string 1)
2495 (match-string 2)
2496 (match-string 3))
2497 imap-current-target-mailbox))
2498 ((search-forward "ALERT] " nil t)
2499 (message "Imap server %s information: %s" imap-server
2500 (buffer-substring (point) (point-max)))))))
2502 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2503 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2505 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2506 ;; *(SP mbx-list-oflag) /
2507 ;; mbx-list-oflag *(SP mbx-list-oflag)
2509 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2510 ;; ; Other flags; multiple possible per LIST response
2512 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2513 ;; ; Selectability flags; only one per LIST response
2515 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2516 ;; "\" quoted-specials
2518 ;; quoted-specials = DQUOTE / "\"
2520 (defun imap-parse-data-list (type)
2521 (let (flags delimiter mailbox)
2522 (setq flags (imap-parse-flag-list))
2523 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2524 (setq delimiter (match-string 1))
2525 (goto-char (1+ (match-end 0)))
2526 (when (setq mailbox (imap-parse-mailbox))
2527 (imap-mailbox-put type t mailbox)
2528 (imap-mailbox-put 'list-flags flags mailbox)
2529 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2531 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2532 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2533 ;; "INTERNALDATE" SPACE date_time /
2534 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2535 ;; "RFC822.SIZE" SPACE number /
2536 ;; "BODY" ["STRUCTURE"] SPACE body /
2537 ;; "BODY" section ["<" number ">"] SPACE nstring /
2538 ;; "UID" SPACE uniqueid) ")"
2540 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2541 ;; SPACE time SPACE zone <">
2543 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2544 ;; ["." (section_text / "MIME")])] "]"
2546 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2547 ;; SPACE header_list / "TEXT"
2549 ;; header_fld_name ::= astring
2551 ;; header_list ::= "(" 1#header_fld_name ")"
2553 (defsubst imap-parse-header-list ()
2554 (when (eq (char-after) ?\()
2555 (let (strlist)
2556 (while (not (eq (char-after) ?\)))
2557 (imap-forward)
2558 (push (imap-parse-astring) strlist))
2559 (imap-forward)
2560 (nreverse strlist))))
2562 (defsubst imap-parse-fetch-body-section ()
2563 (let ((section
2564 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2565 (if (eq (char-before) ? )
2566 (prog1
2567 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2568 (search-forward "]" nil t))
2569 section)))
2571 (defun imap-parse-fetch (response)
2572 (when (eq (char-after) ?\()
2573 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2574 rfc822size body bodydetail bodystructure flags-empty)
2575 ;; Courier can insert spurious blank characters which will
2576 ;; confuse `read', so skip past them.
2577 (while (let ((moved (skip-chars-forward " \t")))
2578 (prog1 (not (eq (char-after) ?\)))
2579 (unless (= moved 0) (backward-char))))
2580 (imap-forward)
2581 (let ((token (read (current-buffer))))
2582 (imap-forward)
2583 (cond ((eq token 'UID)
2584 (setq uid (condition-case ()
2585 (read (current-buffer))
2586 (error))))
2587 ((eq token 'FLAGS)
2588 (setq flags (imap-parse-flag-list))
2589 (if (not flags)
2590 (setq flags-empty 't)))
2591 ((eq token 'ENVELOPE)
2592 (setq envelope (imap-parse-envelope)))
2593 ((eq token 'INTERNALDATE)
2594 (setq internaldate (imap-parse-string)))
2595 ((eq token 'RFC822)
2596 (setq rfc822 (imap-parse-nstring)))
2597 ((eq token 'RFC822.HEADER)
2598 (setq rfc822header (imap-parse-nstring)))
2599 ((eq token 'RFC822.TEXT)
2600 (setq rfc822text (imap-parse-nstring)))
2601 ((eq token 'RFC822.SIZE)
2602 (setq rfc822size (read (current-buffer))))
2603 ((eq token 'BODY)
2604 (if (eq (char-before) ?\[)
2605 (push (list
2606 (upcase (imap-parse-fetch-body-section))
2607 (and (eq (char-after) ?<)
2608 (buffer-substring (1+ (point))
2609 (search-forward ">" nil t)))
2610 (progn (imap-forward)
2611 (imap-parse-nstring)))
2612 bodydetail)
2613 (setq body (imap-parse-body))))
2614 ((eq token 'BODYSTRUCTURE)
2615 (setq bodystructure (imap-parse-body))))))
2616 (when uid
2617 (setq imap-current-message uid)
2618 (imap-message-put uid 'UID uid)
2619 (and (or flags flags-empty) (imap-message-put uid 'FLAGS flags))
2620 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2621 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2622 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2623 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2624 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2625 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2626 (and body (imap-message-put uid 'BODY body))
2627 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2628 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2629 (run-hooks 'imap-fetch-data-hook)))))
2631 ;; mailbox-data = ...
2632 ;; "STATUS" SP mailbox SP "("
2633 ;; [status-att SP number
2634 ;; *(SP status-att SP number)] ")"
2635 ;; ...
2637 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2638 ;; "UNSEEN"
2640 (defun imap-parse-status ()
2641 (let ((mailbox (imap-parse-mailbox)))
2642 (if (eq (char-after) ? )
2643 (forward-char))
2644 (when (and mailbox (eq (char-after) ?\())
2645 (while (and (not (eq (char-after) ?\)))
2646 (or (forward-char) t)
2647 (looking-at "\\([A-Za-z]+\\) "))
2648 (let ((token (upcase (match-string 1))))
2649 (goto-char (match-end 0))
2650 (cond ((string= token "MESSAGES")
2651 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2652 ((string= token "RECENT")
2653 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2654 ((string= token "UIDNEXT")
2655 (and (looking-at "[0-9]+")
2656 (imap-mailbox-put 'uidnext (match-string 0) mailbox)
2657 (goto-char (match-end 0))))
2658 ((string= token "UIDVALIDITY")
2659 (and (looking-at "[0-9]+")
2660 (imap-mailbox-put 'uidvalidity (match-string 0) mailbox)
2661 (goto-char (match-end 0))))
2662 ((string= token "UNSEEN")
2663 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2665 (message "Unknown status data %s in mailbox %s ignored"
2666 token mailbox)
2667 (read (current-buffer)))))))))
2669 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2670 ;; rights)
2672 ;; identifier ::= astring
2674 ;; rights ::= astring
2676 (defun imap-parse-acl ()
2677 (let ((mailbox (imap-parse-mailbox))
2678 identifier rights acl)
2679 (while (eq (char-after) ?\ )
2680 (imap-forward)
2681 (setq identifier (imap-parse-astring))
2682 (imap-forward)
2683 (setq rights (imap-parse-astring))
2684 (setq acl (append acl (list (cons identifier rights)))))
2685 (imap-mailbox-put 'acl acl mailbox)))
2687 ;; flag-list = "(" [flag *(SP flag)] ")"
2689 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2690 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2691 ;; ; Does not include "\Recent"
2693 ;; flag-keyword = atom
2695 ;; flag-extension = "\" atom
2696 ;; ; Future expansion. Client implementations
2697 ;; ; MUST accept flag-extension flags. Server
2698 ;; ; implementations MUST NOT generate
2699 ;; ; flag-extension flags except as defined by
2700 ;; ; future standard or standards-track
2701 ;; ; revisions of this specification.
2703 (defun imap-parse-flag-list ()
2704 (let (flag-list start)
2705 (assert (eq (char-after) ?\() nil "In imap-parse-flag-list 1")
2706 (while (and (not (eq (char-after) ?\)))
2707 (setq start (progn
2708 (imap-forward)
2709 ;; next line for Courier IMAP bug.
2710 (skip-chars-forward " ")
2711 (point)))
2712 (> (skip-chars-forward "^ )" (point-at-eol)) 0))
2713 (push (buffer-substring start (point)) flag-list))
2714 (assert (eq (char-after) ?\)) nil "In imap-parse-flag-list 2")
2715 (imap-forward)
2716 (nreverse flag-list)))
2718 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2719 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2720 ;; env-in-reply-to SP env-message-id ")"
2722 ;; env-bcc = "(" 1*address ")" / nil
2724 ;; env-cc = "(" 1*address ")" / nil
2726 ;; env-date = nstring
2728 ;; env-from = "(" 1*address ")" / nil
2730 ;; env-in-reply-to = nstring
2732 ;; env-message-id = nstring
2734 ;; env-reply-to = "(" 1*address ")" / nil
2736 ;; env-sender = "(" 1*address ")" / nil
2738 ;; env-subject = nstring
2740 ;; env-to = "(" 1*address ")" / nil
2742 (defun imap-parse-envelope ()
2743 (when (eq (char-after) ?\()
2744 (imap-forward)
2745 (vector (prog1 (imap-parse-nstring) ;; date
2746 (imap-forward))
2747 (prog1 (imap-parse-nstring) ;; subject
2748 (imap-forward))
2749 (prog1 (imap-parse-address-list) ;; from
2750 (imap-forward))
2751 (prog1 (imap-parse-address-list) ;; sender
2752 (imap-forward))
2753 (prog1 (imap-parse-address-list) ;; reply-to
2754 (imap-forward))
2755 (prog1 (imap-parse-address-list) ;; to
2756 (imap-forward))
2757 (prog1 (imap-parse-address-list) ;; cc
2758 (imap-forward))
2759 (prog1 (imap-parse-address-list) ;; bcc
2760 (imap-forward))
2761 (prog1 (imap-parse-nstring) ;; in-reply-to
2762 (imap-forward))
2763 (prog1 (imap-parse-nstring) ;; message-id
2764 (imap-forward)))))
2766 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2768 (defsubst imap-parse-string-list ()
2769 (cond ((eq (char-after) ?\() ;; body-fld-param
2770 (let (strlist str)
2771 (imap-forward)
2772 (while (setq str (imap-parse-string))
2773 (push str strlist)
2774 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2775 ;; between body-fld-param's sometimes
2776 (or (eq (char-after) ?\")
2777 (imap-forward)))
2778 (nreverse strlist)))
2779 ((imap-parse-nil)
2780 nil)))
2782 ;; body-extension = nstring / number /
2783 ;; "(" body-extension *(SP body-extension) ")"
2784 ;; ; Future expansion. Client implementations
2785 ;; ; MUST accept body-extension fields. Server
2786 ;; ; implementations MUST NOT generate
2787 ;; ; body-extension fields except as defined by
2788 ;; ; future standard or standards-track
2789 ;; ; revisions of this specification.
2791 (defun imap-parse-body-extension ()
2792 (if (eq (char-after) ?\()
2793 (let (b-e)
2794 (imap-forward)
2795 (push (imap-parse-body-extension) b-e)
2796 (while (eq (char-after) ?\ )
2797 (imap-forward)
2798 (push (imap-parse-body-extension) b-e))
2799 (assert (eq (char-after) ?\)) nil "In imap-parse-body-extension")
2800 (imap-forward)
2801 (nreverse b-e))
2802 (or (imap-parse-number)
2803 (imap-parse-nstring))))
2805 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2806 ;; *(SP body-extension)]]
2807 ;; ; MUST NOT be returned on non-extensible
2808 ;; ; "BODY" fetch
2810 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2811 ;; *(SP body-extension)]]
2812 ;; ; MUST NOT be returned on non-extensible
2813 ;; ; "BODY" fetch
2815 (defsubst imap-parse-body-ext ()
2816 (let (ext)
2817 (when (eq (char-after) ?\ ) ;; body-fld-dsp
2818 (imap-forward)
2819 (let (dsp)
2820 (if (eq (char-after) ?\()
2821 (progn
2822 (imap-forward)
2823 (push (imap-parse-string) dsp)
2824 (imap-forward)
2825 (push (imap-parse-string-list) dsp)
2826 (imap-forward))
2827 ;; With assert, the code might not be eval'd.
2828 ;; (assert (imap-parse-nil) t "In imap-parse-body-ext")
2829 (imap-parse-nil))
2830 (push (nreverse dsp) ext))
2831 (when (eq (char-after) ?\ ) ;; body-fld-lang
2832 (imap-forward)
2833 (if (eq (char-after) ?\()
2834 (push (imap-parse-string-list) ext)
2835 (push (imap-parse-nstring) ext))
2836 (while (eq (char-after) ?\ ) ;; body-extension
2837 (imap-forward)
2838 (setq ext (append (imap-parse-body-extension) ext)))))
2839 ext))
2841 ;; body = "(" body-type-1part / body-type-mpart ")"
2843 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2844 ;; *(SP body-extension)]]
2845 ;; ; MUST NOT be returned on non-extensible
2846 ;; ; "BODY" fetch
2848 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2849 ;; *(SP body-extension)]]
2850 ;; ; MUST NOT be returned on non-extensible
2851 ;; ; "BODY" fetch
2853 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2854 ;; body-fld-enc SP body-fld-octets
2856 ;; body-fld-desc = nstring
2858 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2860 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2861 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2863 ;; body-fld-id = nstring
2865 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2867 ;; body-fld-lines = number
2869 ;; body-fld-md5 = nstring
2871 ;; body-fld-octets = number
2873 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2875 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2876 ;; [SP body-ext-1part]
2878 ;; body-type-basic = media-basic SP body-fields
2879 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2881 ;; body-type-msg = media-message SP body-fields SP envelope
2882 ;; SP body SP body-fld-lines
2884 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2886 ;; body-type-mpart = 1*body SP media-subtype
2887 ;; [SP body-ext-mpart]
2889 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2890 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2891 ;; ; Defined in [MIME-IMT]
2893 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2894 ;; ; Defined in [MIME-IMT]
2896 ;; media-subtype = string
2897 ;; ; Defined in [MIME-IMT]
2899 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2900 ;; ; Defined in [MIME-IMT]
2902 (defun imap-parse-body ()
2903 (let (body)
2904 (when (eq (char-after) ?\()
2905 (imap-forward)
2906 (if (eq (char-after) ?\()
2907 (let (subbody)
2908 (while (and (eq (char-after) ?\()
2909 (setq subbody (imap-parse-body)))
2910 ;; buggy stalker communigate pro 3.0 inserts a SPC between
2911 ;; parts in multiparts
2912 (when (and (eq (char-after) ?\ )
2913 (eq (char-after (1+ (point))) ?\())
2914 (imap-forward))
2915 (push subbody body))
2916 (imap-forward)
2917 (push (imap-parse-string) body) ;; media-subtype
2918 (when (eq (char-after) ?\ ) ;; body-ext-mpart:
2919 (imap-forward)
2920 (if (eq (char-after) ?\() ;; body-fld-param
2921 (push (imap-parse-string-list) body)
2922 (push (and (imap-parse-nil) nil) body))
2923 (setq body
2924 (append (imap-parse-body-ext) body))) ;; body-ext-...
2925 (assert (eq (char-after) ?\)) nil "In imap-parse-body")
2926 (imap-forward)
2927 (nreverse body))
2929 (push (imap-parse-string) body) ;; media-type
2930 (imap-forward)
2931 (push (imap-parse-string) body) ;; media-subtype
2932 (imap-forward)
2933 ;; next line for Sun SIMS bug
2934 (and (eq (char-after) ? ) (imap-forward))
2935 (if (eq (char-after) ?\() ;; body-fld-param
2936 (push (imap-parse-string-list) body)
2937 (push (and (imap-parse-nil) nil) body))
2938 (imap-forward)
2939 (push (imap-parse-nstring) body) ;; body-fld-id
2940 (imap-forward)
2941 (push (imap-parse-nstring) body) ;; body-fld-desc
2942 (imap-forward)
2943 ;; Next `or' for Sun SIMS bug. It regards body-fld-enc as a
2944 ;; nstring and returns nil instead of defaulting back to 7BIT
2945 ;; as the standard says.
2946 ;; Exchange (2007, at least) does this as well.
2947 (push (or (imap-parse-nstring) "7BIT") body) ;; body-fld-enc
2948 (imap-forward)
2949 ;; Exchange 2007 can return -1, contrary to the spec...
2950 (if (eq (char-after) ?-)
2951 (progn
2952 (skip-chars-forward "-0-9")
2953 (push nil body))
2954 (push (imap-parse-number) body)) ;; body-fld-octets
2956 ;; Ok, we're done parsing the required parts, what comes now is one of
2957 ;; three things:
2959 ;; envelope (then we're parsing body-type-msg)
2960 ;; body-fld-lines (then we're parsing body-type-text)
2961 ;; body-ext-1part (then we're parsing body-type-basic)
2963 ;; The problem is that the two first are in turn optionally followed
2964 ;; by the third. So we parse the first two here (if there are any)...
2966 (when (eq (char-after) ?\ )
2967 (imap-forward)
2968 (let (lines)
2969 (cond ((eq (char-after) ?\() ;; body-type-msg:
2970 (push (imap-parse-envelope) body) ;; envelope
2971 (imap-forward)
2972 (push (imap-parse-body) body) ;; body
2973 ;; buggy stalker communigate pro 3.0 doesn't print
2974 ;; number of lines in message/rfc822 attachment
2975 (if (eq (char-after) ?\))
2976 (push 0 body)
2977 (imap-forward)
2978 (push (imap-parse-number) body))) ;; body-fld-lines
2979 ((setq lines (imap-parse-number)) ;; body-type-text:
2980 (push lines body)) ;; body-fld-lines
2982 (backward-char))))) ;; no match...
2984 ;; ...and then parse the third one here...
2986 (when (eq (char-after) ?\ ) ;; body-ext-1part:
2987 (imap-forward)
2988 (push (imap-parse-nstring) body) ;; body-fld-md5
2989 (setq body (append (imap-parse-body-ext) body))) ;; body-ext-1part..
2991 (assert (eq (char-after) ?\)) nil "In imap-parse-body 2")
2992 (imap-forward)
2993 (nreverse body)))))
2995 (when imap-debug ; (untrace-all)
2996 (require 'trace)
2997 (buffer-disable-undo (get-buffer-create imap-debug-buffer))
2998 (mapc (lambda (f) (trace-function-background f imap-debug-buffer))
3000 imap-utf7-encode
3001 imap-utf7-decode
3002 imap-error-text
3003 imap-kerberos4s-p
3004 imap-kerberos4-open
3005 imap-ssl-p
3006 imap-ssl-open
3007 imap-network-p
3008 imap-network-open
3009 imap-interactive-login
3010 imap-kerberos4a-p
3011 imap-kerberos4-auth
3012 imap-cram-md5-p
3013 imap-cram-md5-auth
3014 imap-login-p
3015 imap-login-auth
3016 imap-anonymous-p
3017 imap-anonymous-auth
3018 imap-open-1
3019 imap-open
3020 imap-opened
3021 imap-ping-server
3022 imap-authenticate
3023 imap-close
3024 imap-capability
3025 imap-namespace
3026 imap-send-command-wait
3027 imap-mailbox-put
3028 imap-mailbox-get
3029 imap-mailbox-map-1
3030 imap-mailbox-map
3031 imap-current-mailbox
3032 imap-current-mailbox-p-1
3033 imap-current-mailbox-p
3034 imap-mailbox-select-1
3035 imap-mailbox-select
3036 imap-mailbox-examine-1
3037 imap-mailbox-examine
3038 imap-mailbox-unselect
3039 imap-mailbox-expunge
3040 imap-mailbox-close
3041 imap-mailbox-create-1
3042 imap-mailbox-create
3043 imap-mailbox-delete
3044 imap-mailbox-rename
3045 imap-mailbox-lsub
3046 imap-mailbox-list
3047 imap-mailbox-subscribe
3048 imap-mailbox-unsubscribe
3049 imap-mailbox-status
3050 imap-mailbox-acl-get
3051 imap-mailbox-acl-set
3052 imap-mailbox-acl-delete
3053 imap-current-message
3054 imap-list-to-message-set
3055 imap-fetch-asynch
3056 imap-fetch
3057 imap-fetch-safe
3058 imap-message-put
3059 imap-message-get
3060 imap-message-map
3061 imap-search
3062 imap-message-flag-permanent-p
3063 imap-message-flags-set
3064 imap-message-flags-del
3065 imap-message-flags-add
3066 imap-message-copyuid-1
3067 imap-message-copyuid
3068 imap-message-copy
3069 imap-message-appenduid-1
3070 imap-message-appenduid
3071 imap-message-append
3072 imap-body-lines
3073 imap-envelope-from
3074 imap-send-command-1
3075 imap-send-command
3076 imap-wait-for-tag
3077 imap-sentinel
3078 imap-find-next-line
3079 imap-arrival-filter
3080 imap-parse-greeting
3081 imap-parse-response
3082 imap-parse-resp-text
3083 imap-parse-resp-text-code
3084 imap-parse-data-list
3085 imap-parse-fetch
3086 imap-parse-status
3087 imap-parse-acl
3088 imap-parse-flag-list
3089 imap-parse-envelope
3090 imap-parse-body-extension
3091 imap-parse-body
3094 (provide 'imap)
3096 ;; arch-tag: 27369ed6-33e4-482f-96f1-8bb906ba70f7
3097 ;;; imap.el ends here