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