1 ;;; imap.el --- imap library
2 ;; Copyright (C) 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; imap.el is a elisp library providing an interface for talking to
30 ;; imap.el is roughly divided in two parts, one that parses IMAP
31 ;; responses from the server and storing data into buffer-local
32 ;; variables, and one for utility functions which send commands to
33 ;; server, waits for an answer, and return information. The latter
34 ;; part is layered on top of the previous.
36 ;; The imap.el API consist of the following functions, other functions
37 ;; in this file should not be called directly and the result of doing
38 ;; so are at best undefined.
42 ;; imap-open, imap-opened, imap-authenticate, imap-close,
43 ;; imap-capability, imap-namespace, imap-error-text
47 ;; imap-mailbox-get, imap-mailbox-map, imap-current-mailbox,
48 ;; imap-current-mailbox-p, imap-search, imap-mailbox-select,
49 ;; imap-mailbox-examine, imap-mailbox-unselect, imap-mailbox-expunge
50 ;; imap-mailbox-close, imap-mailbox-create, imap-mailbox-delete
51 ;; imap-mailbox-rename, imap-mailbox-lsub, imap-mailbox-list
52 ;; imap-mailbox-subscribe, imap-mailbox-unsubscribe, imap-mailbox-status
53 ;; imap-mailbox-acl-get, imap-mailbox-acl-set, imap-mailbox-acl-delete
57 ;; imap-fetch-asynch, imap-fetch,
58 ;; imap-current-message, imap-list-to-message-set,
59 ;; imap-message-get, imap-message-map
60 ;; imap-message-envelope-date, imap-message-envelope-subject,
61 ;; imap-message-envelope-from, imap-message-envelope-sender,
62 ;; imap-message-envelope-reply-to, imap-message-envelope-to,
63 ;; imap-message-envelope-cc, imap-message-envelope-bcc
64 ;; imap-message-envelope-in-reply-to, imap-message-envelope-message-id
65 ;; imap-message-body, imap-message-flag-permanent-p
66 ;; imap-message-flags-set, imap-message-flags-del
67 ;; imap-message-flags-add, imap-message-copyuid
68 ;; imap-message-copy, imap-message-appenduid
69 ;; imap-message-append, imap-envelope-from
72 ;; It is my hope that theese commands should be pretty self
73 ;; explanatory for someone that know IMAP. All functions have
74 ;; additional documentation on how to invoke them.
76 ;; imap.el support RFC1730/2060 (IMAP4/IMAP4rev1), implemented IMAP
77 ;; extensions are RFC2195 (CRAM-MD5), RFC2086 (ACL), RFC2342
78 ;; (NAMESPACE), RFC2359 (UIDPLUS), the IMAP-part of RFC2595 (STARTTLS,
79 ;; LOGINDISABLED) (with use of external library starttls.el and
80 ;; program starttls) and the GSSAPI / kerberos V4 sections of RFC1731
81 ;; (with use of external program `imtest'). It also take advantage
82 ;; the UNSELECT extension in Cyrus IMAPD.
84 ;; Without the work of John McClary Prevost and Jim Radford this library
85 ;; would not have seen the light of day. Many thanks.
87 ;; This is a transcript of short interactive session for demonstration
90 ;; (imap-open "my.mail.server")
91 ;; => " *imap* my.mail.server:0"
93 ;; The rest are invoked with current buffer as the buffer returned by
94 ;; `imap-open'. It is possible to do all without this, but it would
95 ;; look ugly here since `buffer' is always the last argument for all
96 ;; imap.el API functions.
98 ;; (imap-authenticate "myusername" "mypassword")
101 ;; (imap-mailbox-lsub "*")
102 ;; => ("INBOX.sentmail" "INBOX.private" "INBOX.draft" "INBOX.spam")
104 ;; (imap-mailbox-list "INBOX.n%")
105 ;; => ("INBOX.namedroppers" "INBOX.nnimap" "INBOX.ntbugtraq")
107 ;; (imap-mailbox-select "INBOX.nnimap")
110 ;; (imap-mailbox-get 'exists)
113 ;; (imap-mailbox-get 'uidvalidity)
116 ;; (imap-search "FLAGGED SINCE 18-DEC-98")
119 ;; (imap-fetch 235 "RFC822.PEEK" 'RFC822)
120 ;; => "X-Sieve: cmu-sieve 1.3^M\nX-Username: <jas@pdc.kth.se>^M\r...."
124 ;; o Parse UIDs as strings? We need to overcome the 28 bit limit somehow.
125 ;; o Don't use `read' at all (important places already fixed)
126 ;; o Accept list of articles instead of message set string in most
127 ;; imap-message-* functions.
131 ;; - 19991218 added starttls/digest-md5 patch,
132 ;; by Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
133 ;; NB! you need SLIM for starttls.el and digest-md5.el
134 ;; - 19991023 commited to pgnus
139 (eval-when-compile (require 'cl))
140 (eval-when-compile (require 'static))
145 (autoload 'open-ssl-stream "ssl")
146 (autoload 'starttls-open-stream "starttls")
147 (autoload 'starttls-negotiate "starttls")
148 (autoload 'rfc2104-hash "rfc2104")
149 (autoload 'md5 "md5")
150 (autoload 'utf7-encode "utf7")
151 (autoload 'utf7-decode "utf7")
152 (autoload 'format-spec "format-spec")
153 (autoload 'format-spec-make "format-spec")
154 ;; Avoid use gnus-point-at-eol so we're independent of Gnus. These
155 ;; days we have point-at-eol anyhow.
156 (if (fboundp 'point-at-eol)
157 (defalias 'imap-point-at-eol 'point-at-eol)
158 (defun imap-point-at-eol ()
162 (autoload 'sasl-digest-md5-digest-response "sasl"))
167 "Low-level IMAP issues."
171 (defcustom imap-kerberos4-program '("imtest -m kerberos_v4 -u %l -p %p %s"
173 "List of strings containing commands for Kerberos 4 authentication.
174 %s is replaced with server hostname, %p with port to connect to, and
175 %l with the value of `imap-default-user'. The program should accept
176 IMAP commands on stdin and return responses to stdout. Each entry in
177 the list is tried until a successful connection is made."
179 :type '(repeat string))
181 (defcustom imap-gssapi-program '("imtest -m gssapi -u %l -p %p %s")
182 "List of strings containing commands for GSSAPI (krb5) authentication.
183 %s is replaced with server hostname, %p with port to connect to, and
184 %l with the value of `imap-default-user'. The program should accept
185 IMAP commands on stdin and return responses to stdout. Each entry in
186 the list is tried until a successful connection is made."
188 :type '(repeat string))
190 (defcustom imap-ssl-program '("openssl s_client -ssl3 -connect %s:%p"
191 "openssl s_client -ssl2 -connect %s:%p"
192 "s_client -ssl3 -connect %s:%p"
193 "s_client -ssl2 -connect %s:%p")
194 "A string, or list of strings, containing commands for SSL connections.
195 Within a string, %s is replaced with the server address and %p with
196 port number on server. The program should accept IMAP commands on
197 stdin and return responses to stdout. Each entry in the list is tried
198 until a successful connection is made."
200 :type '(choice string
203 (defcustom imap-shell-program '("ssh %s imapd"
205 "ssh %g ssh %s imapd"
206 "rsh %g rsh %s imapd")
207 "A list of strings, containing commands for IMAP connection.
208 Within a string, %s is replaced with the server address, %p with port
209 number on server, %g with `imap-shell-host', and %l with
210 `imap-default-user'. The program should read IMAP commands from stdin
211 and write IMAP response to stdout. Each entry in the list is tried
212 until a successful connection is made."
214 :type '(repeat string))
216 (defvar imap-shell-host "gateway"
217 "Hostname of rlogin proxy.")
219 (defvar imap-default-user (user-login-name)
220 "Default username to use.")
222 (defvar imap-error nil
223 "Error codes from the last command.")
225 ;; Various variables.
227 (defvar imap-fetch-data-hook nil
228 "Hooks called after receiving each FETCH response.")
230 (defvar imap-streams '(gssapi kerberos4 starttls ssl network shell)
231 "Priority of streams to consider when opening connection to server.")
233 (defvar imap-stream-alist
234 '((gssapi imap-gssapi-stream-p imap-gssapi-open)
235 (kerberos4 imap-kerberos4-stream-p imap-kerberos4-open)
236 (ssl imap-ssl-p imap-ssl-open)
237 (network imap-network-p imap-network-open)
238 (shell imap-shell-p imap-shell-open)
239 (starttls imap-starttls-p imap-starttls-open))
240 "Definition of network streams.
244 NAME names the stream, CHECK is a function returning non-nil if the
245 server support the stream and OPEN is a function for opening the
248 (defvar imap-authenticators '(gssapi
254 "Priority of authenticators to consider when authenticating to server.")
256 (defvar imap-authenticator-alist
257 '((gssapi imap-gssapi-auth-p imap-gssapi-auth)
258 (kerberos4 imap-kerberos4-auth-p imap-kerberos4-auth)
259 (cram-md5 imap-cram-md5-p imap-cram-md5-auth)
260 (login imap-login-p imap-login-auth)
261 (anonymous imap-anonymous-p imap-anonymous-auth)
262 (digest-md5 imap-digest-md5-p imap-digest-md5-auth))
263 "Definition of authenticators.
265 \(NAME CHECK AUTHENTICATE)
267 NAME names the authenticator. CHECK is a function returning non-nil if
268 the server support the authenticator and AUTHENTICATE is a function
269 for doing the actuall authentification.")
271 (defvar imap-use-utf7 t
272 "If non-nil, do utf7 encoding/decoding of mailbox names.
273 Since the UTF7 decoding currently only decodes into ISO-8859-1
274 characters, you may disable this decoding if you need to access UTF7
275 encoded mailboxes which doesn't translate into ISO-8859-1.")
277 ;; Internal constants. Change theese and die.
279 (defconst imap-default-port 143)
280 (defconst imap-default-ssl-port 993)
281 (defconst imap-default-stream 'network)
282 (defconst imap-local-variables '(imap-server
291 imap-current-target-mailbox
300 imap-calculate-literal-size-first
303 ;; Internal variables.
305 (defvar imap-stream nil)
306 (defvar imap-auth nil)
307 (defvar imap-server nil)
308 (defvar imap-port nil)
309 (defvar imap-username nil)
310 (defvar imap-password nil)
311 (defvar imap-calculate-literal-size-first nil)
312 (defvar imap-state 'closed
314 Valid states are `closed', `initial', `nonauth', `auth', `selected'
317 (defvar imap-server-eol "\r\n"
318 "The EOL string sent from the server.")
320 (defvar imap-client-eol "\r\n"
321 "The EOL string we send to the server.")
323 (defvar imap-current-mailbox nil
324 "Current mailbox name.")
326 (defvar imap-current-target-mailbox nil
327 "Current target mailbox for COPY and APPEND commands.")
329 (defvar imap-mailbox-data nil
330 "Obarray with mailbox data.")
332 (defvar imap-mailbox-prime 997
333 "Length of imap-mailbox-data.")
335 (defvar imap-current-message nil
336 "Current message number.")
338 (defvar imap-message-data nil
339 "Obarray with message data.")
341 (defvar imap-message-prime 997
342 "Length of imap-message-data.")
344 (defvar imap-capability nil
345 "Capability for server.")
347 (defvar imap-namespace nil
348 "Namespace for current server.")
350 (defvar imap-reached-tag 0
351 "Lower limit on command tags that have been parsed.")
353 (defvar imap-failed-tags nil
354 "Alist of tags that failed.
355 Each element is a list with four elements; tag (a integer), response
356 state (a symbol, `OK', `NO' or `BAD'), response code (a string), and
357 human readable response text (a string).")
360 "Command tag number.")
362 (defvar imap-process nil
365 (defvar imap-continuation nil
366 "Non-nil indicates that the server emitted a continuation request.
367 The actually value is really the text on the continuation line.")
370 "Name of buffer for imap session trace.
371 For example: (setq imap-log \"*imap-log*\")")
373 (defvar imap-debug nil ;"*imap-debug*"
374 "Name of buffer for random debug spew.
375 For example: (setq imap-debug \"*imap-debug*\")")
378 ;; Utility functions:
380 (defun imap-read-passwd (prompt &rest args)
381 "Read a password using PROMPT.
382 If ARGS, PROMPT is used as an argument to `format'."
383 (let ((prompt (if args
384 (apply 'format prompt args)
386 (funcall (if (or (fboundp 'read-passwd)
388 (fboundp 'read-passwd))
389 (and (load "passwd" t)
390 (fboundp 'read-passwd)))
392 (autoload 'ange-ftp-read-passwd "ange-ftp")
393 'ange-ftp-read-passwd)
396 (defsubst imap-utf7-encode (string)
400 (utf7-encode string t)
402 "imap: Could not UTF7 encode `%s', using it unencoded..."
407 (defsubst imap-utf7-decode (string)
411 (utf7-decode string t)
413 "imap: Could not UTF7 decode `%s', using it undecoded..."
418 (defsubst imap-ok-p (status)
421 (setq imap-error status)
424 (defun imap-error-text (&optional buffer)
425 (with-current-buffer (or buffer (current-buffer))
426 (nth 3 (car imap-failed-tags))))
429 ;; Server functions; stream stuff:
431 (defun imap-kerberos4-stream-p (buffer)
432 (imap-capability 'AUTH=KERBEROS_V4 buffer))
434 (defun imap-kerberos4-open (name buffer server port)
435 (let ((cmds imap-kerberos4-program)
437 (while (and (not done) (setq cmd (pop cmds)))
438 (message "Opening Kerberos 4 IMAP connection with `%s'..." cmd)
440 (let* ((port (or port imap-default-port))
441 (process (as-binary-process
443 name buffer shell-file-name shell-command-switch
448 ?p (number-to-string port)
449 ?l imap-default-user)))))
452 (with-current-buffer buffer
453 (setq imap-client-eol "\n"
454 imap-calculate-literal-size-first t)
455 (while (and (memq (process-status process) '(open run))
456 (goto-char (point-min))
457 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
458 (or (while (looking-at "^C:")
461 ;; cyrus 1.6 imtest print "S: " before server greeting
462 (or (not (looking-at "S: "))
465 (not (and (imap-parse-greeting)
466 ;; success in imtest < 1.6:
467 (or (re-search-forward
468 "^__\\(.*\\)__\n" nil t)
469 ;; success in imtest 1.6:
471 "^\\(Authenticat.*\\)" nil t))
472 (setq response (match-string 1)))))
473 (accept-process-output process 1)
476 (with-current-buffer (get-buffer-create imap-log)
477 (buffer-disable-undo)
478 (goto-char (point-max))
479 (insert-buffer-substring buffer)))
481 (message "Opening Kerberos 4 IMAP connection with `%s'...%s" cmd
482 (if response (concat "done, " response) "failed"))
483 (if (and response (let ((case-fold-search nil))
484 (not (string-match "failed" response))))
486 (if (memq (process-status process) '(open run))
487 (imap-send-command-wait "LOGOUT"))
488 (delete-process process)
492 (defun imap-gssapi-stream-p (buffer)
493 (imap-capability 'AUTH=GSSAPI buffer))
495 (defun imap-gssapi-open (name buffer server port)
496 (let ((cmds imap-gssapi-program)
498 (while (and (not done) (setq cmd (pop cmds)))
499 (message "Opening GSSAPI IMAP connection with `%s'..." cmd)
500 (let* ((port (or port imap-default-port))
501 (process (as-binary-process
503 name buffer shell-file-name shell-command-switch
508 ?p (number-to-string port)
509 ?l imap-default-user)))))
512 (with-current-buffer buffer
513 (setq imap-client-eol "\n"
514 imap-calculate-literal-size-first t)
515 (while (and (memq (process-status process) '(open run))
516 (goto-char (point-min))
517 ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
518 (or (while (looking-at "^C:")
521 ;; cyrus 1.6 imtest print "S: " before server greeting
522 (or (not (looking-at "S: "))
525 (not (and (imap-parse-greeting)
526 ;; success in imtest 1.6:
528 "^\\(Authenticat.*\\)" nil t)
529 (setq response (match-string 1)))))
530 (accept-process-output process 1)
533 (with-current-buffer (get-buffer-create imap-log)
534 (buffer-disable-undo)
535 (goto-char (point-max))
536 (insert-buffer-substring buffer)))
538 (message "GSSAPI IMAP connection: %s" (or response "failed"))
539 (if (and response (let ((case-fold-search nil))
540 (not (string-match "failed" response))))
542 (if (memq (process-status process) '(open run))
543 (imap-send-command-wait "LOGOUT"))
544 (delete-process process)
548 (defun imap-ssl-p (buffer)
551 (defun imap-ssl-open (name buffer server port)
552 "Open a SSL connection to server."
553 (let ((cmds (if (listp imap-ssl-program) imap-ssl-program
554 (list imap-ssl-program)))
556 (while (and (not done) (setq cmd (pop cmds)))
557 (message "imap: Opening SSL connection with `%s'..." cmd)
558 (let* ((port (or port imap-default-ssl-port))
559 (ssl-program-name shell-file-name)
560 (ssl-program-arguments
561 (list shell-command-switch
562 (format-spec cmd (format-spec-make
564 ?p (number-to-string port)))))
568 (cond ((eq system-type 'windows-nt)
569 (let (selective-display
570 (coding-system-for-write 'binary)
571 (coding-system-for-read 'raw-text-dos)
572 (output-coding-system 'binary)
573 (input-coding-system 'raw-text-dos))
574 (open-ssl-stream name buffer server port)))
577 (open-ssl-stream name buffer server port))))))
578 (with-current-buffer buffer
579 (goto-char (point-min))
580 (while (and (memq (process-status process) '(open run))
581 (goto-char (point-max))
583 (not (imap-parse-greeting)))
584 (accept-process-output process 1)
587 (with-current-buffer (get-buffer-create imap-log)
588 (buffer-disable-undo)
589 (goto-char (point-max))
590 (insert-buffer-substring buffer)))
592 (when (memq (process-status process) '(open run))
593 (setq done process))))))
596 (message "imap: Opening SSL connection with `%s'...done" cmd)
598 (message "imap: Opening SSL connection with `%s'...failed" cmd)
601 (defun imap-network-p (buffer)
604 (defun imap-network-open (name buffer server port)
605 (let* ((port (or port imap-default-port))
606 (process (open-network-stream-as-binary name buffer server port)))
608 (while (and (memq (process-status process) '(open run))
609 (goto-char (point-min))
610 (not (imap-parse-greeting)))
611 (accept-process-output process 1)
614 (with-current-buffer (get-buffer-create imap-log)
615 (buffer-disable-undo)
616 (goto-char (point-max))
617 (insert-buffer-substring buffer)))
618 (when (memq (process-status process) '(open run))
621 (defun imap-shell-p (buffer)
624 (defun imap-shell-open (name buffer server port)
625 (let ((cmds imap-shell-program)
627 (while (and (not done) (setq cmd (pop cmds)))
628 (message "imap: Opening IMAP connection with `%s'..." cmd)
629 (setq imap-client-eol "\n")
630 (let* ((port (or port imap-default-port))
631 (process (as-binary-process
633 name buffer shell-file-name shell-command-switch
639 ?p (number-to-string port)
640 ?l imap-default-user))))))
642 (while (and (memq (process-status process) '(open run))
643 (goto-char (point-min))
644 (not (imap-parse-greeting)))
645 (accept-process-output process 1)
648 (with-current-buffer (get-buffer-create imap-log)
649 (buffer-disable-undo)
650 (goto-char (point-max))
651 (insert-buffer-substring buffer)))
653 (when (memq (process-status process) '(open run))
654 (setq done process)))))
657 (message "imap: Opening IMAP connection with `%s'...done" cmd)
659 (message "imap: Opening IMAP connection with `%s'...failed" cmd)
662 (defun imap-starttls-p (buffer)
663 (and (imap-capability 'STARTTLS buffer)
667 (call-process "starttls"))
670 (defun imap-starttls-open (name buffer server port)
671 (let* ((port (or port imap-default-port))
672 (process (as-binary-process
673 (starttls-open-stream name buffer server port)))
675 (message "imap: Connecting with STARTTLS...")
677 (while (and (memq (process-status process) '(open run))
678 (goto-char (point-min))
679 (not (imap-parse-greeting)))
680 (accept-process-output process 1)
683 (with-current-buffer (get-buffer-create imap-log)
684 (buffer-disable-undo)
685 (goto-char (point-max))
686 (insert-buffer-substring buffer)))
687 (let ((imap-process process))
690 (set-process-filter imap-process 'imap-arrival-filter)
691 (when (and (eq imap-stream 'starttls)
692 (imap-ok-p (imap-send-command-wait "STARTTLS")))
693 (starttls-negotiate imap-process)))
694 (set-process-filter imap-process nil)))
695 (when (memq (process-status process) '(open run))
696 (setq done process)))
699 (message "imap: Connecting with STARTTLS...done")
701 (message "imap: Connecting with STARTTLS...failed")
704 ;; Server functions; authenticator stuff:
706 (defun imap-interactive-login (buffer loginfunc)
707 "Login to server in BUFFER.
708 LOGINFUNC is passed a username and a password, it should return t if
709 it where sucessful authenticating itself to the server, nil otherwise.
710 Returns t if login was successful, nil otherwise."
711 (with-current-buffer buffer
712 (make-variable-buffer-local 'imap-username)
713 (make-variable-buffer-local 'imap-password)
714 (let (user passwd ret)
715 ;; (condition-case ()
716 (while (or (not user) (not passwd))
717 (setq user (or imap-username
718 (read-from-minibuffer
719 (concat "IMAP username for " imap-server ": ")
720 (or user imap-default-user))))
721 (setq passwd (or imap-password
723 (concat "IMAP password for " user "@"
725 (when (and user passwd)
726 (if (funcall loginfunc user passwd)
730 (if (and (not imap-password)
731 (y-or-n-p "Store password for this session? "))
732 (setq imap-password passwd)))
733 (message "Login failed...")
736 ;; (quit (with-current-buffer buffer
739 ;; (error (with-current-buffer buffer
744 (defun imap-gssapi-auth-p (buffer)
745 (imap-capability 'AUTH=GSSAPI buffer))
747 (defun imap-gssapi-auth (buffer)
748 (message "imap: Authenticating using GSSAPI...%s"
749 (if (eq imap-stream 'gssapi) "done" "failed"))
750 (eq imap-stream 'gssapi))
752 (defun imap-kerberos4-auth-p (buffer)
753 (imap-capability 'AUTH=KERBEROS_V4 buffer))
755 (defun imap-kerberos4-auth (buffer)
756 (message "imap: Authenticating using Kerberos 4...%s"
757 (if (eq imap-stream 'kerberos4) "done" "failed"))
758 (eq imap-stream 'kerberos4))
760 (defun imap-cram-md5-p (buffer)
761 (imap-capability 'AUTH=CRAM-MD5 buffer))
763 (defun imap-cram-md5-auth (buffer)
764 "Login to server using the AUTH CRAM-MD5 method."
765 (message "imap: Authenticating using CRAM-MD5...")
766 (let ((done (imap-interactive-login
768 (lambda (user passwd)
770 (imap-send-command-wait
772 "AUTHENTICATE CRAM-MD5"
774 (let* ((decoded (base64-decode-string challenge))
776 (if (and (featurep 'xemacs)
777 (>= (function-max-args 'md5) 4))
778 (lambda (object &optional start end)
779 (md5 object start end 'binary))
781 (hash (rfc2104-hash hash-function 64 16
783 (response (concat user " " hash))
784 (encoded (base64-encode-string response)))
787 (message "imap: Authenticating using CRAM-MD5...done")
788 (message "imap: Authenticating using CRAM-MD5...failed"))))
790 (defun imap-login-p (buffer)
791 (and (not (imap-capability 'LOGINDISABLED buffer))
792 (not (imap-capability 'X-LOGIN-CMD-DISABLED buffer))))
794 (defun imap-login-auth (buffer)
795 "Login to server using the LOGIN command."
796 (message "imap: Plaintext authentication...")
797 (imap-interactive-login buffer
798 (lambda (user passwd)
799 (imap-ok-p (imap-send-command-wait
800 (concat "LOGIN \"" user "\" \""
803 (defun imap-anonymous-p (buffer)
806 (defun imap-anonymous-auth (buffer)
807 (message "imap: Loging in anonymously...")
808 (with-current-buffer buffer
809 (imap-ok-p (imap-send-command-wait
810 (concat "LOGIN anonymous \"" (concat (user-login-name) "@"
811 (system-name)) "\"")))))
813 (defun imap-digest-md5-p (buffer)
814 (and (imap-capability 'AUTH=DIGEST-MD5 buffer)
816 (require 'digest-md5)
819 (defun imap-digest-md5-auth (buffer)
820 "Login to server using the AUTH DIGEST-MD5 method."
821 (message "imap: Authenticating using DIGEST-MD5...")
822 (imap-interactive-login
824 (lambda (user passwd)
828 "AUTHENTICATE DIGEST-MD5"
830 (base64-encode-string
831 (sasl-digest-md5-digest-response
832 (base64-decode-string challenge)
833 user passwd "imap" imap-server)
835 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
837 (setq imap-continuation nil)
838 (imap-send-command-1 "")
839 (imap-ok-p (imap-wait-for-tag tag)))))))
843 (defun imap-open-1 (buffer)
844 (with-current-buffer buffer
846 (setq imap-current-mailbox nil
847 imap-current-message nil
849 imap-process (condition-case ()
850 (funcall (nth 2 (assq imap-stream
852 "imap" buffer imap-server imap-port)
855 (set-process-filter imap-process 'imap-arrival-filter)
856 (set-process-sentinel imap-process 'imap-sentinel)
857 (while (and (eq imap-state 'initial)
858 (memq (process-status imap-process) '(open run)))
859 (message "Waiting for response from %s..." imap-server)
860 (accept-process-output imap-process 1))
861 (message "Waiting for response from %s...done" imap-server)
862 (and (memq (process-status imap-process) '(open run))
865 (defun imap-open (server &optional port stream auth buffer)
866 "Open a IMAP connection to host SERVER at PORT returning a buffer.
867 If PORT is unspecified, a default value is used (143 except
868 for SSL which use 993).
869 STREAM indicates the stream to use, see `imap-streams' for available
870 streams. If nil, it choices the best stream the server is capable of.
871 AUTH indicates authenticator to use, see `imap-authenticators' for
872 available authenticators. If nil, it choices the best stream the
873 server is capable of.
874 BUFFER can be a buffer or a name of a buffer, which is created if
875 necessery. If nil, the buffer name is generated."
876 (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
877 (with-current-buffer (get-buffer-create buffer)
878 (if (imap-opened buffer)
880 (mapcar 'make-variable-buffer-local imap-local-variables)
881 (set-buffer-multibyte nil)
882 (buffer-disable-undo)
883 (setq imap-server (or server imap-server))
884 (setq imap-port (or port imap-port))
885 (setq imap-auth (or auth imap-auth))
886 (setq imap-stream (or stream imap-stream))
887 (message "imap: Connecting to %s..." imap-server)
888 (if (let ((imap-stream (or imap-stream imap-default-stream)))
889 (imap-open-1 buffer))
891 (let (stream-changed)
892 (message "imap: Connecting to %s...done" imap-server)
893 (when (null imap-stream)
894 (let ((streams imap-streams))
895 (while (setq stream (pop streams))
896 (if (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
897 (setq stream-changed (not (eq (or imap-stream
903 (error "Couldn't figure out a stream for server"))))
905 (message "imap: Reconnecting with stream `%s'..." imap-stream)
907 (if (imap-open-1 buffer)
908 (message "imap: Reconnecting with stream `%s'...done"
910 (message "imap: Reconnecting with stream `%s'...failed"
912 (setq imap-capability nil))
913 (if (imap-opened buffer)
914 ;; Choose authenticator
915 (when (and (null imap-auth) (not (eq imap-state 'auth)))
916 (let ((auths imap-authenticators))
917 (while (setq auth (pop auths))
918 (if (funcall (nth 1 (assq auth imap-authenticator-alist))
923 (error "Couldn't figure out authenticator for server"))))))
924 (message "imap: Connecting to %s...failed" imap-server))
925 (when (imap-opened buffer)
926 (setq imap-mailbox-data (make-vector imap-mailbox-prime 0))
929 (defun imap-opened (&optional buffer)
930 "Return non-nil if connection to imap server in BUFFER is open.
931 If BUFFER is nil then the current buffer is used."
932 (and (setq buffer (get-buffer (or buffer (current-buffer))))
933 (buffer-live-p buffer)
934 (with-current-buffer buffer
936 (memq (process-status imap-process) '(open run))))))
938 (defun imap-authenticate (&optional user passwd buffer)
939 "Authenticate to server in BUFFER, using current buffer if nil.
940 It uses the authenticator specified when opening the server. If the
941 authenticator requires username/passwords, they are queried from the
942 user and optionally stored in the buffer. If USER and/or PASSWD is
943 specified, the user will not be questioned and the username and/or
944 password is remembered in the buffer."
945 (with-current-buffer (or buffer (current-buffer))
946 (if (not (eq imap-state 'nonauth))
947 (or (eq imap-state 'auth)
948 (eq imap-state 'select)
949 (eq imap-state 'examine))
950 (make-variable-buffer-local 'imap-username)
951 (make-variable-buffer-local 'imap-password)
952 (if user (setq imap-username user))
953 (if passwd (setq imap-password passwd))
954 (if (funcall (nth 2 (assq imap-auth imap-authenticator-alist)) buffer)
955 (setq imap-state 'auth)))))
957 (defun imap-close (&optional buffer)
958 "Close connection to server in BUFFER.
959 If BUFFER is nil, the current buffer is used."
960 (with-current-buffer (or buffer (current-buffer))
962 (not (imap-ok-p (imap-send-command-wait "LOGOUT")))
963 (message "Server %s didn't let me log out" imap-server))
964 (when (and imap-process
965 (memq (process-status imap-process) '(open run)))
966 (delete-process imap-process))
967 (setq imap-current-mailbox nil
968 imap-current-message nil
973 (defun imap-capability (&optional identifier buffer)
974 "Return a list of identifiers which server in BUFFER support.
975 If IDENTIFIER, return non-nil if it's among the servers capabilities.
976 If BUFFER is nil, the current buffer is assumed."
977 (with-current-buffer (or buffer (current-buffer))
978 (unless imap-capability
979 (unless (imap-ok-p (imap-send-command-wait "CAPABILITY"))
980 (setq imap-capability '(IMAP2))))
982 (memq (intern (upcase (symbol-name identifier))) imap-capability)
985 (defun imap-namespace (&optional buffer)
986 "Return a namespace hierarchy at server in BUFFER.
987 If BUFFER is nil, the current buffer is assumed."
988 (with-current-buffer (or buffer (current-buffer))
989 (unless imap-namespace
990 (when (imap-capability 'NAMESPACE)
991 (imap-send-command-wait "NAMESPACE")))
994 (defun imap-send-command-wait (command &optional buffer)
995 (imap-wait-for-tag (imap-send-command command buffer) buffer))
998 ;; Mailbox functions:
1000 (defun imap-mailbox-put (propname value &optional mailbox buffer)
1001 (with-current-buffer (or buffer (current-buffer))
1002 (if imap-mailbox-data
1003 (put (intern (or mailbox imap-current-mailbox) imap-mailbox-data)
1005 (error "Imap-mailbox-data is nil, prop %s value %s mailbox %s buffer %s"
1006 propname value mailbox (current-buffer)))
1009 (defsubst imap-mailbox-get-1 (propname &optional mailbox)
1010 (get (intern-soft (or mailbox imap-current-mailbox) imap-mailbox-data)
1013 (defun imap-mailbox-get (propname &optional mailbox buffer)
1014 (let ((mailbox (imap-utf7-encode mailbox)))
1015 (with-current-buffer (or buffer (current-buffer))
1016 (imap-mailbox-get-1 propname (or mailbox imap-current-mailbox)))))
1018 (defun imap-mailbox-map-1 (func &optional mailbox-decoder buffer)
1019 (with-current-buffer (or buffer (current-buffer))
1023 (push (funcall func (if mailbox-decoder
1024 (funcall mailbox-decoder (symbol-name s))
1025 (symbol-name s))) result))
1029 (defun imap-mailbox-map (func &optional buffer)
1030 "Map a function across each mailbox in `imap-mailbox-data', returning a list.
1031 Function should take a mailbox name (a string) as
1033 (imap-mailbox-map-1 func 'imap-utf7-decode buffer))
1035 (defun imap-current-mailbox (&optional buffer)
1036 (with-current-buffer (or buffer (current-buffer))
1037 (imap-utf7-decode imap-current-mailbox)))
1039 (defun imap-current-mailbox-p-1 (mailbox &optional examine)
1040 (and (string= mailbox imap-current-mailbox)
1042 (eq imap-state 'examine))
1044 (eq imap-state 'selected)))))
1046 (defun imap-current-mailbox-p (mailbox &optional examine buffer)
1047 (with-current-buffer (or buffer (current-buffer))
1048 (imap-current-mailbox-p-1 (imap-utf7-encode mailbox) examine)))
1050 (defun imap-mailbox-select-1 (mailbox &optional examine)
1051 "Select MAILBOX on server in BUFFER.
1052 If EXAMINE is non-nil, do a read-only select."
1053 (if (imap-current-mailbox-p-1 mailbox examine)
1054 imap-current-mailbox
1055 (setq imap-current-mailbox mailbox)
1056 (if (imap-ok-p (imap-send-command-wait
1057 (concat (if examine "EXAMINE" "SELECT") " \""
1060 (setq imap-message-data (make-vector imap-message-prime 0)
1061 imap-state (if examine 'examine 'selected))
1062 imap-current-mailbox)
1063 ;; Failed SELECT/EXAMINE unselects current mailbox
1064 (setq imap-current-mailbox nil))))
1066 (defun imap-mailbox-select (mailbox &optional examine buffer)
1067 (with-current-buffer (or buffer (current-buffer))
1069 (imap-mailbox-select-1 (imap-utf7-encode mailbox) examine))))
1071 (defun imap-mailbox-examine-1 (mailbox &optional buffer)
1072 (with-current-buffer (or buffer (current-buffer))
1073 (imap-mailbox-select-1 mailbox 'exmine)))
1075 (defun imap-mailbox-examine (mailbox &optional buffer)
1076 "Examine MAILBOX on server in BUFFER."
1077 (imap-mailbox-select mailbox 'exmine buffer))
1079 (defun imap-mailbox-unselect (&optional buffer)
1080 "Close current folder in BUFFER, without expunging articles."
1081 (with-current-buffer (or buffer (current-buffer))
1082 (when (or (eq imap-state 'auth)
1083 (and (imap-capability 'UNSELECT)
1084 (imap-ok-p (imap-send-command-wait "UNSELECT")))
1086 (imap-send-command-wait (concat "EXAMINE \""
1087 imap-current-mailbox
1089 (imap-ok-p (imap-send-command-wait "CLOSE"))))
1090 (setq imap-current-mailbox nil
1091 imap-message-data nil
1095 (defun imap-mailbox-expunge (&optional buffer)
1096 "Expunge articles in current folder in BUFFER.
1097 If BUFFER is nil the current buffer is assumed."
1098 (with-current-buffer (or buffer (current-buffer))
1099 (when (and imap-current-mailbox (not (eq imap-state 'examine)))
1100 (imap-ok-p (imap-send-command-wait "EXPUNGE")))))
1102 (defun imap-mailbox-close (&optional buffer)
1103 "Expunge articles and close current folder in BUFFER.
1104 If BUFFER is nil the current buffer is assumed."
1105 (with-current-buffer (or buffer (current-buffer))
1106 (when (and imap-current-mailbox
1107 (imap-ok-p (imap-send-command-wait "CLOSE")))
1108 (setq imap-current-mailbox nil
1109 imap-message-data nil
1113 (defun imap-mailbox-create-1 (mailbox)
1114 (imap-ok-p (imap-send-command-wait (list "CREATE \"" mailbox "\""))))
1116 (defun imap-mailbox-create (mailbox &optional buffer)
1117 "Create MAILBOX on server in BUFFER.
1118 If BUFFER is nil the current buffer is assumed."
1119 (with-current-buffer (or buffer (current-buffer))
1120 (imap-mailbox-create-1 (imap-utf7-encode mailbox))))
1122 (defun imap-mailbox-delete (mailbox &optional buffer)
1123 "Delete MAILBOX on server in BUFFER.
1124 If BUFFER is nil the current buffer is assumed."
1125 (let ((mailbox (imap-utf7-encode mailbox)))
1126 (with-current-buffer (or buffer (current-buffer))
1128 (imap-send-command-wait (list "DELETE \"" mailbox "\""))))))
1130 (defun imap-mailbox-rename (oldname newname &optional buffer)
1131 "Rename mailbox OLDNAME to NEWNAME on server in BUFFER.
1132 If BUFFER is nil the current buffer is assumed."
1133 (let ((oldname (imap-utf7-encode oldname))
1134 (newname (imap-utf7-encode newname)))
1135 (with-current-buffer (or buffer (current-buffer))
1137 (imap-send-command-wait (list "RENAME \"" oldname "\" "
1138 "\"" newname "\""))))))
1140 (defun imap-mailbox-lsub (&optional root reference add-delimiter buffer)
1141 "Return a list of subscribed mailboxes on server in BUFFER.
1142 If ROOT is non-nil, only list matching mailboxes. If ADD-DELIMITER is
1143 non-nil, a hierarchy delimiter is added to root. REFERENCE is a
1144 implementation-specific string that has to be passed to lsub command."
1145 (with-current-buffer (or buffer (current-buffer))
1146 ;; Make sure we know the hierarchy separator for root's hierarchy
1147 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1148 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1149 (imap-utf7-encode root) "\"")))
1150 ;; clear list data (NB not delimiter and other stuff)
1151 (imap-mailbox-map-1 (lambda (mailbox)
1152 (imap-mailbox-put 'lsub nil mailbox)))
1154 (imap-send-command-wait
1155 (concat "LSUB \"" reference "\" \"" (imap-utf7-encode root)
1156 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1159 (imap-mailbox-map-1 (lambda (mailbox)
1160 (when (imap-mailbox-get-1 'lsub mailbox)
1161 (push (imap-utf7-decode mailbox) out))))
1164 (defun imap-mailbox-list (root &optional reference add-delimiter buffer)
1165 "Return a list of mailboxes matching ROOT on server in BUFFER.
1166 If ADD-DELIMITER is non-nil, a hierarchy delimiter is added to
1167 root. REFERENCE is a implementation-specific string that has to be
1168 passed to list command."
1169 (with-current-buffer (or buffer (current-buffer))
1170 ;; Make sure we know the hierarchy separator for root's hierarchy
1171 (when (and add-delimiter (null (imap-mailbox-get-1 'delimiter root)))
1172 (imap-send-command-wait (concat "LIST \"" reference "\" \""
1173 (imap-utf7-encode root) "\"")))
1174 ;; clear list data (NB not delimiter and other stuff)
1175 (imap-mailbox-map-1 (lambda (mailbox)
1176 (imap-mailbox-put 'list nil mailbox)))
1178 (imap-send-command-wait
1179 (concat "LIST \"" reference "\" \"" (imap-utf7-encode root)
1180 (and add-delimiter (imap-mailbox-get-1 'delimiter root))
1183 (imap-mailbox-map-1 (lambda (mailbox)
1184 (when (imap-mailbox-get-1 'list mailbox)
1185 (push (imap-utf7-decode mailbox) out))))
1188 (defun imap-mailbox-subscribe (mailbox &optional buffer)
1189 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1190 Returns non-nil if successful."
1191 (with-current-buffer (or buffer (current-buffer))
1192 (imap-ok-p (imap-send-command-wait (concat "SUBSCRIBE \""
1193 (imap-utf7-encode mailbox)
1196 (defun imap-mailbox-unsubscribe (mailbox &optional buffer)
1197 "Send the SUBSCRIBE command on the mailbox to server in BUFFER.
1198 Returns non-nil if successful."
1199 (with-current-buffer (or buffer (current-buffer))
1200 (imap-ok-p (imap-send-command-wait (concat "UNSUBSCRIBE "
1201 (imap-utf7-encode mailbox)
1204 (defun imap-mailbox-status (mailbox items &optional buffer)
1205 "Get status items ITEM in MAILBOX from server in BUFFER.
1206 ITEMS can be a symbol or a list of symbols, valid symbols are one of
1207 the STATUS data items -- ie 'messages, 'recent, 'uidnext, 'uidvalidity
1208 or 'unseen. If ITEMS is a list of symbols, a list of values is
1209 returned, if ITEMS is a symbol only it's value is returned."
1210 (with-current-buffer (or buffer (current-buffer))
1212 (imap-send-command-wait (list "STATUS \""
1213 (imap-utf7-encode mailbox)
1220 (mapcar (lambda (item)
1221 (imap-mailbox-get item mailbox))
1223 (imap-mailbox-get items mailbox)))))
1225 (defun imap-mailbox-acl-get (&optional mailbox buffer)
1226 "Get ACL on mailbox from server in BUFFER."
1227 (let ((mailbox (imap-utf7-encode mailbox)))
1228 (with-current-buffer (or buffer (current-buffer))
1230 (imap-send-command-wait (list "GETACL \""
1231 (or mailbox imap-current-mailbox)
1233 (imap-mailbox-get-1 'acl (or mailbox imap-current-mailbox))))))
1235 (defun imap-mailbox-acl-set (identifier rights &optional mailbox buffer)
1236 "Change/set ACL for IDENTIFIER to RIGHTS in MAILBOX from server in BUFFER."
1237 (let ((mailbox (imap-utf7-encode mailbox)))
1238 (with-current-buffer (or buffer (current-buffer))
1240 (imap-send-command-wait (list "SETACL \""
1241 (or mailbox imap-current-mailbox)
1247 (defun imap-mailbox-acl-delete (identifier &optional mailbox buffer)
1248 "Removes any <identifier,rights> pair for IDENTIFIER in MAILBOX from server in BUFFER."
1249 (let ((mailbox (imap-utf7-encode mailbox)))
1250 (with-current-buffer (or buffer (current-buffer))
1252 (imap-send-command-wait (list "DELETEACL \""
1253 (or mailbox imap-current-mailbox)
1258 ;; Message functions:
1260 (defun imap-current-message (&optional buffer)
1261 (with-current-buffer (or buffer (current-buffer))
1262 imap-current-message))
1264 (defun imap-list-to-message-set (list)
1265 (mapconcat (lambda (item)
1266 (number-to-string item))
1272 (defun imap-range-to-message-set (range)
1277 (car item) (cdr item))
1278 (format "%d" item)))
1279 (if (and (listp range) (not (listp (cdr range))))
1280 (list range) ;; make (1 . 2) into ((1 . 2))
1284 (defun imap-fetch-asynch (uids props &optional nouidfetch buffer)
1285 (with-current-buffer (or buffer (current-buffer))
1286 (imap-send-command (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1288 (imap-list-to-message-set uids)
1292 (defun imap-fetch (uids props &optional receive nouidfetch buffer)
1293 "Fetch properties PROPS from message set UIDS from server in BUFFER.
1294 UIDS can be a string, number or a list of numbers. If RECEIVE
1295 is non-nil return theese properties."
1296 (with-current-buffer (or buffer (current-buffer))
1297 (when (imap-ok-p (imap-send-command-wait
1298 (format "%sFETCH %s %s" (if nouidfetch "" "UID ")
1300 (imap-list-to-message-set uids)
1303 (if (or (null receive) (stringp uids))
1306 (mapcar (lambda (uid)
1308 (mapcar (lambda (prop)
1309 (imap-message-get uid prop))
1311 (imap-message-get uid receive)))
1313 (imap-message-get uids receive))))))
1315 (defun imap-message-put (uid propname value &optional buffer)
1316 (with-current-buffer (or buffer (current-buffer))
1317 (if imap-message-data
1318 (put (intern (number-to-string uid) imap-message-data)
1320 (error "Imap-message-data is nil, uid %s prop %s value %s buffer %s"
1321 uid propname value (current-buffer)))
1324 (defun imap-message-get (uid propname &optional buffer)
1325 (with-current-buffer (or buffer (current-buffer))
1326 (get (intern-soft (number-to-string uid) imap-message-data)
1329 (defun imap-message-map (func propname &optional buffer)
1330 "Map a function across each mailbox in `imap-message-data', returning a list."
1331 (with-current-buffer (or buffer (current-buffer))
1335 (push (funcall func (get s 'UID) (get s propname)) result))
1339 (defmacro imap-message-envelope-date (uid &optional buffer)
1340 `(with-current-buffer (or ,buffer (current-buffer))
1341 (elt (imap-message-get ,uid 'ENVELOPE) 0)))
1343 (defmacro imap-message-envelope-subject (uid &optional buffer)
1344 `(with-current-buffer (or ,buffer (current-buffer))
1345 (elt (imap-message-get ,uid 'ENVELOPE) 1)))
1347 (defmacro imap-message-envelope-from (uid &optional buffer)
1348 `(with-current-buffer (or ,buffer (current-buffer))
1349 (elt (imap-message-get ,uid 'ENVELOPE) 2)))
1351 (defmacro imap-message-envelope-sender (uid &optional buffer)
1352 `(with-current-buffer (or ,buffer (current-buffer))
1353 (elt (imap-message-get ,uid 'ENVELOPE) 3)))
1355 (defmacro imap-message-envelope-reply-to (uid &optional buffer)
1356 `(with-current-buffer (or ,buffer (current-buffer))
1357 (elt (imap-message-get ,uid 'ENVELOPE) 4)))
1359 (defmacro imap-message-envelope-to (uid &optional buffer)
1360 `(with-current-buffer (or ,buffer (current-buffer))
1361 (elt (imap-message-get ,uid 'ENVELOPE) 5)))
1363 (defmacro imap-message-envelope-cc (uid &optional buffer)
1364 `(with-current-buffer (or ,buffer (current-buffer))
1365 (elt (imap-message-get ,uid 'ENVELOPE) 6)))
1367 (defmacro imap-message-envelope-bcc (uid &optional buffer)
1368 `(with-current-buffer (or ,buffer (current-buffer))
1369 (elt (imap-message-get ,uid 'ENVELOPE) 7)))
1371 (defmacro imap-message-envelope-in-reply-to (uid &optional buffer)
1372 `(with-current-buffer (or ,buffer (current-buffer))
1373 (elt (imap-message-get ,uid 'ENVELOPE) 8)))
1375 (defmacro imap-message-envelope-message-id (uid &optional buffer)
1376 `(with-current-buffer (or ,buffer (current-buffer))
1377 (elt (imap-message-get ,uid 'ENVELOPE) 9)))
1379 (defmacro imap-message-body (uid &optional buffer)
1380 `(with-current-buffer (or ,buffer (current-buffer))
1381 (imap-message-get ,uid 'BODY)))
1383 (defun imap-search (predicate &optional buffer)
1384 (with-current-buffer (or buffer (current-buffer))
1385 (imap-mailbox-put 'search 'dummy)
1386 (when (imap-ok-p (imap-send-command-wait (concat "UID SEARCH " predicate)))
1387 (if (eq (imap-mailbox-get-1 'search imap-current-mailbox) 'dummy)
1388 (error "Missing SEARCH response to a SEARCH command")
1389 (imap-mailbox-get-1 'search imap-current-mailbox)))))
1391 (defun imap-message-flag-permanent-p (flag &optional mailbox buffer)
1392 "Return t iff FLAG can be permanently (between IMAP sessions) saved on articles, in MAILBOX on server in BUFFER."
1393 (with-current-buffer (or buffer (current-buffer))
1394 (or (member "\\*" (imap-mailbox-get 'permanentflags mailbox))
1395 (member flag (imap-mailbox-get 'permanentflags mailbox)))))
1397 (defun imap-message-flags-set (articles flags &optional silent buffer)
1398 (when (and articles flags)
1399 (with-current-buffer (or buffer (current-buffer))
1400 (imap-ok-p (imap-send-command-wait
1401 (concat "UID STORE " articles
1402 " FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1404 (defun imap-message-flags-del (articles flags &optional silent buffer)
1405 (when (and articles flags)
1406 (with-current-buffer (or buffer (current-buffer))
1407 (imap-ok-p (imap-send-command-wait
1408 (concat "UID STORE " articles
1409 " -FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1411 (defun imap-message-flags-add (articles flags &optional silent buffer)
1412 (when (and articles flags)
1413 (with-current-buffer (or buffer (current-buffer))
1414 (imap-ok-p (imap-send-command-wait
1415 (concat "UID STORE " articles
1416 " +FLAGS" (if silent ".SILENT") " (" flags ")"))))))
1418 (defun imap-message-copyuid-1 (mailbox)
1419 (if (imap-capability 'UIDPLUS)
1420 (list (nth 0 (imap-mailbox-get-1 'copyuid mailbox))
1421 (string-to-number (nth 2 (imap-mailbox-get-1 'copyuid mailbox))))
1422 (let ((old-mailbox imap-current-mailbox)
1424 (imap-message-data (make-vector 2 0)))
1425 (when (imap-mailbox-examine-1 mailbox)
1427 (and (imap-fetch "*" "UID")
1428 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1429 (apply 'max (imap-message-map
1430 (lambda (uid prop) uid) 'UID))))
1432 (imap-mailbox-select old-mailbox (eq state 'examine))
1433 (imap-mailbox-unselect)))))))
1435 (defun imap-message-copyuid (mailbox &optional buffer)
1436 (with-current-buffer (or buffer (current-buffer))
1437 (imap-message-copyuid-1 (imap-utf7-decode mailbox))))
1439 (defun imap-message-copy (articles mailbox
1440 &optional dont-create no-copyuid buffer)
1441 "Copy ARTICLES (a string message set) to MAILBOX on server in
1442 BUFFER, creating mailbox if it doesn't exist. If dont-create is
1443 non-nil, it will not create a mailbox. On success, return a list with
1444 the UIDVALIDITY of the mailbox the article(s) was copied to as the
1445 first element, rest of list contain the saved articles' UIDs."
1447 (with-current-buffer (or buffer (current-buffer))
1448 (let ((mailbox (imap-utf7-encode mailbox)))
1449 (if (let ((cmd (concat "UID COPY " articles " \"" mailbox "\""))
1450 (imap-current-target-mailbox mailbox))
1451 (if (imap-ok-p (imap-send-command-wait cmd))
1453 (when (and (not dont-create)
1454 ;; removed because of buggy Oracle server
1455 ;; that doesn't send TRYCREATE tags (which
1456 ;; is a MUST according to specifications):
1457 ;;(imap-mailbox-get-1 'trycreate mailbox)
1458 (imap-mailbox-create-1 mailbox))
1459 (imap-ok-p (imap-send-command-wait cmd)))))
1461 (imap-message-copyuid-1 mailbox)))))))
1463 (defun imap-message-appenduid-1 (mailbox)
1464 (if (imap-capability 'UIDPLUS)
1465 (imap-mailbox-get-1 'appenduid mailbox)
1466 (let ((old-mailbox imap-current-mailbox)
1468 (imap-message-data (make-vector 2 0)))
1469 (when (imap-mailbox-examine-1 mailbox)
1471 (and (imap-fetch "*" "UID")
1472 (list (imap-mailbox-get-1 'uidvalidity mailbox)
1473 (apply 'max (imap-message-map
1474 (lambda (uid prop) uid) 'UID))))
1476 (imap-mailbox-select old-mailbox (eq state 'examine))
1477 (imap-mailbox-unselect)))))))
1479 (defun imap-message-appenduid (mailbox &optional buffer)
1480 (with-current-buffer (or buffer (current-buffer))
1481 (imap-message-appenduid-1 (imap-utf7-encode mailbox))))
1483 (defun imap-message-append (mailbox article &optional flags date-time buffer)
1484 "Append ARTICLE (a buffer) to MAILBOX on server in BUFFER.
1485 FLAGS and DATE-TIME is currently not used. Return a cons holding
1486 uidvalidity of MAILBOX and UID the newly created article got, or nil
1488 (let ((mailbox (imap-utf7-encode mailbox)))
1489 (with-current-buffer (or buffer (current-buffer))
1490 (and (let ((imap-current-target-mailbox mailbox))
1492 (imap-send-command-wait
1493 (list "APPEND \"" mailbox "\" " article))))
1494 (imap-message-appenduid-1 mailbox)))))
1496 (defun imap-body-lines (body)
1497 "Return number of lines in article by looking at the mime bodystructure BODY."
1499 (if (stringp (car body))
1500 (cond ((and (string= (upcase (car body)) "TEXT")
1501 (numberp (nth 7 body)))
1503 ((and (string= (upcase (car body)) "MESSAGE")
1504 (numberp (nth 9 body)))
1507 (apply '+ (mapcar 'imap-body-lines body)))
1510 (defun imap-envelope-from (from)
1511 "Return a from string line."
1513 (concat (aref from 0)
1514 (if (aref from 0) " <")
1518 (if (aref from 0) ">"))))
1521 ;; Internal functions.
1523 (defun imap-send-command-1 (cmdstr)
1524 (setq cmdstr (concat cmdstr imap-client-eol))
1526 (with-current-buffer (get-buffer-create imap-log)
1527 (buffer-disable-undo)
1528 (goto-char (point-max))
1530 (process-send-string imap-process cmdstr))
1532 (defun imap-send-command (command &optional buffer)
1533 (with-current-buffer (or buffer (current-buffer))
1534 (if (not (listp command)) (setq command (list command)))
1535 (let ((tag (setq imap-tag (1+ imap-tag)))
1537 (setq cmdstr (concat (number-to-string imap-tag) " "))
1538 (while (setq cmd (pop command))
1539 (cond ((stringp cmd)
1540 (setq cmdstr (concat cmdstr cmd)))
1542 (let ((eol imap-client-eol)
1543 (calcfirst imap-calculate-literal-size-first)
1545 (with-current-buffer cmd
1547 (setq size (buffer-size)))
1548 (when (not (equal eol "\r\n"))
1549 ;; XXX modifies buffer!
1550 (goto-char (point-min))
1551 (while (search-forward "\r\n" nil t)
1552 (replace-match eol)))
1554 (setq size (buffer-size))))
1556 (concat cmdstr (format "{%d}" size))))
1559 (imap-send-command-1 cmdstr)
1561 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1562 (setq command nil);; abort command if no cont-req
1563 (let ((process imap-process)
1564 (stream imap-stream)
1565 (eol imap-client-eol))
1566 (with-current-buffer cmd
1568 (with-current-buffer (get-buffer-create
1570 (buffer-disable-undo)
1571 (goto-char (point-max))
1572 (insert-buffer-substring cmd)))
1573 (process-send-region process (point-min)
1575 (process-send-string process imap-client-eol))))
1576 (setq imap-continuation nil)))
1578 (imap-send-command-1 cmdstr)
1581 (if (not (eq (imap-wait-for-tag tag) 'INCOMPLETE))
1582 (setq command nil);; abort command if no cont-req
1583 (setq command (cons (funcall cmd imap-continuation)
1585 (setq imap-continuation nil)))
1587 (error "Unknown command type"))))
1589 (imap-send-command-1 cmdstr))
1592 (defun imap-wait-for-tag (tag &optional buffer)
1593 (with-current-buffer (or buffer (current-buffer))
1594 (while (and (null imap-continuation)
1595 (< imap-reached-tag tag))
1596 (or (and (not (memq (process-status imap-process) '(open run)))
1598 (let ((len (/ (point-max) 1024))
1601 (message "imap read: %dk" len))
1602 (accept-process-output imap-process 1))))
1604 (or (assq tag imap-failed-tags)
1605 (if imap-continuation
1609 (defun imap-sentinel (process string)
1610 (delete-process process))
1612 (defun imap-find-next-line ()
1613 "Return point at end of current line, taking into account literals.
1614 Return nil if no complete line has arrived."
1615 (when (re-search-forward (concat imap-server-eol "\\|{\\([0-9]+\\)}"
1618 (if (match-string 1)
1619 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1621 (goto-char (+ (point) (string-to-number (match-string 1))))
1622 (imap-find-next-line))
1625 (defun imap-arrival-filter (proc string)
1626 "IMAP process filter."
1627 (with-current-buffer (process-buffer proc)
1628 (goto-char (point-max))
1631 (with-current-buffer (get-buffer-create imap-log)
1632 (buffer-disable-undo)
1633 (goto-char (point-max))
1636 (goto-char (point-min))
1637 (while (setq end (imap-find-next-line))
1639 (narrow-to-region (point-min) end)
1640 (delete-backward-char (length imap-server-eol))
1641 (goto-char (point-min))
1643 (cond ((eq imap-state 'initial)
1644 (imap-parse-greeting))
1645 ((or (eq imap-state 'auth)
1646 (eq imap-state 'nonauth)
1647 (eq imap-state 'selected)
1648 (eq imap-state 'examine))
1649 (imap-parse-response))
1651 (message "Unknown state %s in arrival filter"
1653 (delete-region (point-min) (point-max))))))))
1658 (defsubst imap-forward ()
1659 (or (eobp) (forward-char)))
1662 ;; ; Unsigned 32-bit integer
1663 ;; ; (0 <= n < 4,294,967,296)
1665 (defsubst imap-parse-number ()
1666 (when (looking-at "[0-9]+")
1668 (string-to-number (match-string 0))
1669 (goto-char (match-end 0)))))
1671 ;; literal = "{" number "}" CRLF *CHAR8
1672 ;; ; Number represents the number of CHAR8s
1674 (defsubst imap-parse-literal ()
1675 (when (looking-at "{\\([0-9]+\\)}\r\n")
1676 (let ((pos (match-end 0))
1677 (len (string-to-number (match-string 1))))
1678 (if (< (point-max) (+ pos len))
1680 (goto-char (+ pos len))
1681 (buffer-substring pos (+ pos len))))))
1683 ;; string = quoted / literal
1685 ;; quoted = DQUOTE *QUOTED-CHAR DQUOTE
1687 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
1688 ;; "\" quoted-specials
1690 ;; quoted-specials = DQUOTE / "\"
1692 ;; TEXT-CHAR = <any CHAR except CR and LF>
1694 (defsubst imap-parse-string ()
1695 (cond ((eq (char-after) ?\")
1697 (let ((p (point)) (name ""))
1698 (skip-chars-forward "^\"\\\\")
1699 (setq name (buffer-substring p (point)))
1700 (while (eq (char-after) ?\\)
1701 (setq p (1+ (point)))
1703 (skip-chars-forward "^\"\\\\")
1704 (setq name (concat name (buffer-substring p (point)))))
1707 ((eq (char-after) ?{)
1708 (imap-parse-literal))))
1712 (defsubst imap-parse-nil ()
1713 (if (looking-at "NIL")
1714 (goto-char (match-end 0))))
1716 ;; nstring = string / nil
1718 (defsubst imap-parse-nstring ()
1719 (or (imap-parse-string)
1720 (and (imap-parse-nil)
1723 ;; astring = atom / string
1725 ;; atom = 1*ATOM-CHAR
1727 ;; ATOM-CHAR = <any CHAR except atom-specials>
1729 ;; atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
1732 ;; list-wildcards = "%" / "*"
1734 ;; quoted-specials = DQUOTE / "\"
1736 (defsubst imap-parse-astring ()
1737 (or (imap-parse-string)
1738 (buffer-substring (point)
1739 (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1740 (goto-char (1- (match-end 0)))
1744 ;; address = "(" addr-name SP addr-adl SP addr-mailbox SP
1747 ;; addr-adl = nstring
1748 ;; ; Holds route from [RFC-822] route-addr if
1751 ;; addr-host = nstring
1752 ;; ; NIL indicates [RFC-822] group syntax.
1753 ;; ; Otherwise, holds [RFC-822] domain name
1755 ;; addr-mailbox = nstring
1756 ;; ; NIL indicates end of [RFC-822] group; if
1757 ;; ; non-NIL and addr-host is NIL, holds
1758 ;; ; [RFC-822] group name.
1759 ;; ; Otherwise, holds [RFC-822] local-part
1760 ;; ; after removing [RFC-822] quoting
1762 ;; addr-name = nstring
1763 ;; ; If non-NIL, holds phrase from [RFC-822]
1764 ;; ; mailbox after removing [RFC-822] quoting
1767 (defsubst imap-parse-address ()
1769 (when (eq (char-after) ?\()
1771 (setq address (vector (prog1 (imap-parse-nstring)
1773 (prog1 (imap-parse-nstring)
1775 (prog1 (imap-parse-nstring)
1777 (imap-parse-nstring)))
1778 (when (eq (char-after) ?\))
1782 ;; address-list = "(" 1*address ")" / nil
1786 (defsubst imap-parse-address-list ()
1787 (if (eq (char-after) ?\()
1788 (let (address addresses)
1790 (while (and (not (eq (char-after) ?\)))
1791 ;; next line for MS Exchange bug
1792 (progn (and (eq (char-after) ? ) (imap-forward)) t)
1793 (setq address (imap-parse-address)))
1794 (setq addresses (cons address addresses)))
1795 (when (eq (char-after) ?\))
1797 (nreverse addresses)))
1798 (assert (imap-parse-nil))))
1800 ;; mailbox = "INBOX" / astring
1801 ;; ; INBOX is case-insensitive. All case variants of
1802 ;; ; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX
1803 ;; ; not as an astring. An astring which consists of
1804 ;; ; the case-insensitive sequence "I" "N" "B" "O" "X"
1805 ;; ; is considered to be INBOX and not an astring.
1806 ;; ; Refer to section 5.1 for further
1807 ;; ; semantic details of mailbox names.
1809 (defsubst imap-parse-mailbox ()
1810 (let ((mailbox (imap-parse-astring)))
1811 (if (string-equal "INBOX" (upcase mailbox))
1815 ;; greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
1817 ;; resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
1818 ;; ; Authentication condition
1820 ;; resp-cond-bye = "BYE" SP resp-text
1822 (defun imap-parse-greeting ()
1823 "Parse a IMAP greeting."
1824 (cond ((looking-at "\\* OK ")
1825 (setq imap-state 'nonauth))
1826 ((looking-at "\\* PREAUTH ")
1827 (setq imap-state 'auth))
1828 ((looking-at "\\* BYE ")
1829 (setq imap-state 'closed))))
1831 ;; response = *(continue-req / response-data) response-done
1833 ;; continue-req = "+" SP (resp-text / base64) CRLF
1835 ;; response-data = "*" SP (resp-cond-state / resp-cond-bye /
1836 ;; mailbox-data / message-data / capability-data) CRLF
1838 ;; response-done = response-tagged / response-fatal
1840 ;; response-fatal = "*" SP resp-cond-bye CRLF
1841 ;; ; Server closes connection immediately
1843 ;; response-tagged = tag SP resp-cond-state CRLF
1845 ;; resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
1846 ;; ; Status condition
1848 ;; resp-cond-bye = "BYE" SP resp-text
1850 ;; mailbox-data = "FLAGS" SP flag-list /
1851 ;; "LIST" SP mailbox-list /
1852 ;; "LSUB" SP mailbox-list /
1853 ;; "SEARCH" *(SP nz-number) /
1854 ;; "STATUS" SP mailbox SP "("
1855 ;; [status-att SP number *(SP status-att SP number)] ")" /
1856 ;; number SP "EXISTS" /
1857 ;; number SP "RECENT"
1859 ;; message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
1861 ;; capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
1863 ;; ; IMAP4rev1 servers which offer RFC 1730
1864 ;; ; compatibility MUST list "IMAP4" as the first
1867 (defun imap-parse-response ()
1868 "Parse a IMAP command response."
1870 (case (setq token (read (current-buffer)))
1871 (+ (setq imap-continuation
1872 (or (buffer-substring (min (point-max) (1+ (point)))
1875 (* (case (prog1 (setq token (read (current-buffer)))
1877 (OK (imap-parse-resp-text))
1878 (NO (imap-parse-resp-text))
1879 (BAD (imap-parse-resp-text))
1880 (BYE (imap-parse-resp-text))
1881 (FLAGS (imap-mailbox-put 'flags (imap-parse-flag-list)))
1882 (LIST (imap-parse-data-list 'list))
1883 (LSUB (imap-parse-data-list 'lsub))
1884 (SEARCH (imap-mailbox-put
1886 (read (concat "(" (buffer-substring (point) (point-max)) ")"))))
1887 (STATUS (imap-parse-status))
1888 (CAPABILITY (setq imap-capability
1889 (read (concat "(" (upcase (buffer-substring
1890 (point) (point-max)))
1892 (ACL (imap-parse-acl))
1893 (t (case (prog1 (read (current-buffer))
1895 (EXISTS (imap-mailbox-put 'exists token))
1896 (RECENT (imap-mailbox-put 'recent token))
1898 (FETCH (imap-parse-fetch token))
1899 (t (message "Garbage: %s" (buffer-string)))))))
1901 (if (not (integerp token))
1902 (message "Garbage: %s" (buffer-string))
1903 (case (prog1 (setq status (read (current-buffer)))
1906 (setq imap-reached-tag (max imap-reached-tag token))
1907 (imap-parse-resp-text)))
1909 (setq imap-reached-tag (max imap-reached-tag token))
1911 (imap-parse-resp-text))
1913 (when (eq (char-after) ?\[)
1914 (setq code (buffer-substring (point)
1915 (search-forward "]")))
1917 (setq text (buffer-substring (point) (point-max)))
1918 (push (list token status code text)
1919 imap-failed-tags))))
1921 (setq imap-reached-tag (max imap-reached-tag token))
1923 (imap-parse-resp-text))
1925 (when (eq (char-after) ?\[)
1926 (setq code (buffer-substring (point)
1927 (search-forward "]")))
1929 (setq text (buffer-substring (point) (point-max)))
1930 (push (list token status code text) imap-failed-tags)
1931 (error "Internal error, tag %s status %s code %s text %s"
1932 token status code text))))
1933 (t (message "Garbage: %s" (buffer-string))))))))))
1935 ;; resp-text = ["[" resp-text-code "]" SP] text
1937 ;; text = 1*TEXT-CHAR
1939 ;; TEXT-CHAR = <any CHAR except CR and LF>
1941 (defun imap-parse-resp-text ()
1942 (imap-parse-resp-text-code))
1944 ;; resp-text-code = "ALERT" /
1945 ;; "BADCHARSET [SP "(" astring *(SP astring) ")" ] /
1946 ;; "NEWNAME" SP string SP string /
1948 ;; "PERMANENTFLAGS" SP "("
1949 ;; [flag-perm *(SP flag-perm)] ")" /
1953 ;; "UIDNEXT" SP nz-number /
1954 ;; "UIDVALIDITY" SP nz-number /
1955 ;; "UNSEEN" SP nz-number /
1956 ;; resp-text-atom [SP 1*<any TEXT-CHAR except "]">]
1958 ;; resp_code_apnd = "APPENDUID" SPACE nz_number SPACE uniqueid
1960 ;; resp_code_copy = "COPYUID" SPACE nz_number SPACE set SPACE set
1962 ;; set = sequence-num / (sequence-num ":" sequence-num) /
1964 ;; ; Identifies a set of messages. For message
1965 ;; ; sequence numbers, these are consecutive
1966 ;; ; numbers from 1 to the number of messages in
1968 ;; ; Comma delimits individual numbers, colon
1969 ;; ; delimits between two numbers inclusive.
1970 ;; ; Example: 2,4:7,9,12:* is 2,4,5,6,7,9,12,13,
1971 ;; ; 14,15 for a mailbox with 15 messages.
1973 ;; sequence-num = nz-number / "*"
1974 ;; ; * is the largest number in use. For message
1975 ;; ; sequence numbers, it is the number of messages
1976 ;; ; in the mailbox. For unique identifiers, it is
1977 ;; ; the unique identifier of the last message in
1980 ;; flag-perm = flag / "\*"
1982 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
1983 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
1984 ;; ; Does not include "\Recent"
1986 ;; flag-extension = "\" atom
1987 ;; ; Future expansion. Client implementations
1988 ;; ; MUST accept flag-extension flags. Server
1989 ;; ; implementations MUST NOT generate
1990 ;; ; flag-extension flags except as defined by
1991 ;; ; future standard or standards-track
1992 ;; ; revisions of this specification.
1994 ;; flag-keyword = atom
1996 ;; resp-text-atom = 1*<any ATOM-CHAR except "]">
1998 (defun imap-parse-resp-text-code ()
1999 ;; xxx next line for stalker communigate pro 3.3.1 bug
2000 (when (looking-at " \\[")
2002 (when (eq (char-after) ?\[)
2004 (cond ((search-forward "PERMANENTFLAGS " nil t)
2005 (imap-mailbox-put 'permanentflags (imap-parse-flag-list)))
2006 ((search-forward "UIDNEXT " nil t)
2007 (imap-mailbox-put 'uidnext (read (current-buffer))))
2008 ((search-forward "UNSEEN " nil t)
2009 (imap-mailbox-put 'unseen (read (current-buffer))))
2010 ((looking-at "UIDVALIDITY \\([0-9]+\\)")
2011 (imap-mailbox-put 'uidvalidity (match-string 1)))
2012 ((search-forward "READ-ONLY" nil t)
2013 (imap-mailbox-put 'read-only t))
2014 ((search-forward "NEWNAME " nil t)
2015 (let (oldname newname)
2016 (setq oldname (imap-parse-string))
2018 (setq newname (imap-parse-string))
2019 (imap-mailbox-put 'newname newname oldname)))
2020 ((search-forward "TRYCREATE" nil t)
2021 (imap-mailbox-put 'trycreate t imap-current-target-mailbox))
2022 ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
2023 (imap-mailbox-put 'appenduid
2024 (list (match-string 1)
2025 (string-to-number (match-string 2)))
2026 imap-current-target-mailbox))
2027 ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
2028 (imap-mailbox-put 'copyuid (list (match-string 1)
2031 imap-current-target-mailbox))
2032 ((search-forward "ALERT] " nil t)
2033 (message "Imap server %s information: %s" imap-server
2034 (buffer-substring (point) (point-max)))))))
2036 ;; mailbox-list = "(" [mbx-list-flags] ")" SP
2037 ;; (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
2039 ;; mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
2040 ;; *(SP mbx-list-oflag) /
2041 ;; mbx-list-oflag *(SP mbx-list-oflag)
2043 ;; mbx-list-oflag = "\Noinferiors" / flag-extension
2044 ;; ; Other flags; multiple possible per LIST response
2046 ;; mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
2047 ;; ; Selectability flags; only one per LIST response
2049 ;; QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
2050 ;; "\" quoted-specials
2052 ;; quoted-specials = DQUOTE / "\"
2054 (defun imap-parse-data-list (type)
2055 (let (flags delimiter mailbox)
2056 (setq flags (imap-parse-flag-list))
2057 (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
2058 (setq delimiter (match-string 1))
2059 (goto-char (1+ (match-end 0)))
2060 (when (setq mailbox (imap-parse-mailbox))
2061 (imap-mailbox-put type t mailbox)
2062 (imap-mailbox-put 'list-flags flags mailbox)
2063 (imap-mailbox-put 'delimiter delimiter mailbox)))))
2065 ;; msg_att ::= "(" 1#("ENVELOPE" SPACE envelope /
2066 ;; "FLAGS" SPACE "(" #(flag / "\Recent") ")" /
2067 ;; "INTERNALDATE" SPACE date_time /
2068 ;; "RFC822" [".HEADER" / ".TEXT"] SPACE nstring /
2069 ;; "RFC822.SIZE" SPACE number /
2070 ;; "BODY" ["STRUCTURE"] SPACE body /
2071 ;; "BODY" section ["<" number ">"] SPACE nstring /
2072 ;; "UID" SPACE uniqueid) ")"
2074 ;; date_time ::= <"> date_day_fixed "-" date_month "-" date_year
2075 ;; SPACE time SPACE zone <">
2077 ;; section ::= "[" [section_text / (nz_number *["." nz_number]
2078 ;; ["." (section_text / "MIME")])] "]"
2080 ;; section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"]
2081 ;; SPACE header_list / "TEXT"
2083 ;; header_fld_name ::= astring
2085 ;; header_list ::= "(" 1#header_fld_name ")"
2087 (defsubst imap-parse-header-list ()
2088 (when (eq (char-after) ?\()
2090 (while (not (eq (char-after) ?\)))
2092 (push (imap-parse-astring) strlist))
2094 (nreverse strlist))))
2096 (defsubst imap-parse-fetch-body-section ()
2098 (buffer-substring (point) (1- (re-search-forward "[] ]" nil t)))))
2099 (if (eq (char-before) ? )
2101 (mapconcat 'identity (cons section (imap-parse-header-list)) " ")
2102 (search-forward "]" nil t))
2105 (defun imap-parse-fetch (response)
2106 (when (eq (char-after) ?\()
2107 (let (uid flags envelope internaldate rfc822 rfc822header rfc822text
2108 rfc822size body bodydetail bodystructure)
2109 (while (not (eq (char-after) ?\)))
2111 (let ((token (read (current-buffer))))
2113 (cond ((eq token 'UID)
2114 (setq uid (ignore-errors (read (current-buffer)))))
2116 (setq flags (imap-parse-flag-list)))
2117 ((eq token 'ENVELOPE)
2118 (setq envelope (imap-parse-envelope)))
2119 ((eq token 'INTERNALDATE)
2120 (setq internaldate (imap-parse-string)))
2122 (setq rfc822 (imap-parse-nstring)))
2123 ((eq token 'RFC822.HEADER)
2124 (setq rfc822header (imap-parse-nstring)))
2125 ((eq token 'RFC822.TEXT)
2126 (setq rfc822text (imap-parse-nstring)))
2127 ((eq token 'RFC822.SIZE)
2128 (setq rfc822size (read (current-buffer))))
2130 (if (eq (char-before) ?\[)
2132 (upcase (imap-parse-fetch-body-section))
2133 (and (eq (char-after) ?<)
2134 (buffer-substring (1+ (point))
2135 (search-forward ">" nil t)))
2136 (progn (imap-forward)
2137 (imap-parse-nstring)))
2139 (setq body (imap-parse-body))))
2140 ((eq token 'BODYSTRUCTURE)
2141 (setq bodystructure (imap-parse-body))))))
2143 (setq imap-current-message uid)
2144 (imap-message-put uid 'UID uid)
2145 (and flags (imap-message-put uid 'FLAGS flags))
2146 (and envelope (imap-message-put uid 'ENVELOPE envelope))
2147 (and internaldate (imap-message-put uid 'INTERNALDATE internaldate))
2148 (and rfc822 (imap-message-put uid 'RFC822 rfc822))
2149 (and rfc822header (imap-message-put uid 'RFC822.HEADER rfc822header))
2150 (and rfc822text (imap-message-put uid 'RFC822.TEXT rfc822text))
2151 (and rfc822size (imap-message-put uid 'RFC822.SIZE rfc822size))
2152 (and body (imap-message-put uid 'BODY body))
2153 (and bodydetail (imap-message-put uid 'BODYDETAIL bodydetail))
2154 (and bodystructure (imap-message-put uid 'BODYSTRUCTURE bodystructure))
2155 (run-hooks 'imap-fetch-data-hook)))))
2157 ;; mailbox-data = ...
2158 ;; "STATUS" SP mailbox SP "("
2159 ;; [status-att SP number
2160 ;; *(SP status-att SP number)] ")"
2163 ;; status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
2166 (defun imap-parse-status ()
2167 (let ((mailbox (imap-parse-mailbox)))
2168 (when (and mailbox (search-forward "(" nil t))
2169 (while (not (eq (char-after) ?\)))
2170 (let ((token (read (current-buffer))))
2171 (cond ((eq token 'MESSAGES)
2172 (imap-mailbox-put 'messages (read (current-buffer)) mailbox))
2174 (imap-mailbox-put 'recent (read (current-buffer)) mailbox))
2175 ((eq token 'UIDNEXT)
2176 (imap-mailbox-put 'uidnext (read (current-buffer)) mailbox))
2177 ((eq token 'UIDVALIDITY)
2178 (and (looking-at " \\([0-9]+\\)")
2179 (imap-mailbox-put 'uidvalidity (match-string 1) mailbox)
2180 (goto-char (match-end 1))))
2182 (imap-mailbox-put 'unseen (read (current-buffer)) mailbox))
2184 (message "Unknown status data %s in mailbox %s ignored"
2185 token mailbox))))))))
2187 ;; acl_data ::= "ACL" SPACE mailbox *(SPACE identifier SPACE
2190 ;; identifier ::= astring
2192 ;; rights ::= astring
2194 (defun imap-parse-acl ()
2195 (let ((mailbox (imap-parse-mailbox))
2196 identifier rights acl)
2197 (while (eq (char-after) ?\ )
2199 (setq identifier (imap-parse-astring))
2201 (setq rights (imap-parse-astring))
2202 (setq acl (append acl (list (cons identifier rights)))))
2203 (imap-mailbox-put 'acl acl mailbox)))
2205 ;; flag-list = "(" [flag *(SP flag)] ")"
2207 ;; flag = "\Answered" / "\Flagged" / "\Deleted" /
2208 ;; "\Seen" / "\Draft" / flag-keyword / flag-extension
2209 ;; ; Does not include "\Recent"
2211 ;; flag-keyword = atom
2213 ;; flag-extension = "\" atom
2214 ;; ; Future expansion. Client implementations
2215 ;; ; MUST accept flag-extension flags. Server
2216 ;; ; implementations MUST NOT generate
2217 ;; ; flag-extension flags except as defined by
2218 ;; ; future standard or standards-track
2219 ;; ; revisions of this specification.
2221 (defun imap-parse-flag-list ()
2222 (let (flag-list start)
2223 (assert (eq (char-after) ?\())
2224 (while (and (not (eq (char-after) ?\)))
2225 (setq start (progn (imap-forward) (point)))
2226 (> (skip-chars-forward "^ )" (imap-point-at-eol)) 0))
2227 (push (buffer-substring start (point)) flag-list))
2228 (assert (eq (char-after) ?\)))
2230 (nreverse flag-list)))
2232 ;; envelope = "(" env-date SP env-subject SP env-from SP env-sender SP
2233 ;; env-reply-to SP env-to SP env-cc SP env-bcc SP
2234 ;; env-in-reply-to SP env-message-id ")"
2236 ;; env-bcc = "(" 1*address ")" / nil
2238 ;; env-cc = "(" 1*address ")" / nil
2240 ;; env-date = nstring
2242 ;; env-from = "(" 1*address ")" / nil
2244 ;; env-in-reply-to = nstring
2246 ;; env-message-id = nstring
2248 ;; env-reply-to = "(" 1*address ")" / nil
2250 ;; env-sender = "(" 1*address ")" / nil
2252 ;; env-subject = nstring
2254 ;; env-to = "(" 1*address ")" / nil
2256 (defun imap-parse-envelope ()
2257 (when (eq (char-after) ?\()
2259 (vector (prog1 (imap-parse-nstring);; date
2261 (prog1 (imap-parse-nstring);; subject
2263 (prog1 (imap-parse-address-list);; from
2265 (prog1 (imap-parse-address-list);; sender
2267 (prog1 (imap-parse-address-list);; reply-to
2269 (prog1 (imap-parse-address-list);; to
2271 (prog1 (imap-parse-address-list);; cc
2273 (prog1 (imap-parse-address-list);; bcc
2275 (prog1 (imap-parse-nstring);; in-reply-to
2277 (prog1 (imap-parse-nstring);; message-id
2280 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2282 (defsubst imap-parse-string-list ()
2283 (cond ((eq (char-after) ?\();; body-fld-param
2286 (while (setq str (imap-parse-string))
2288 ;; buggy stalker communigate pro 3.0 doesn't print SPC
2289 ;; between body-fld-param's sometimes
2290 (or (eq (char-after) ?\")
2292 (nreverse strlist)))
2296 ;; body-extension = nstring / number /
2297 ;; "(" body-extension *(SP body-extension) ")"
2298 ;; ; Future expansion. Client implementations
2299 ;; ; MUST accept body-extension fields. Server
2300 ;; ; implementations MUST NOT generate
2301 ;; ; body-extension fields except as defined by
2302 ;; ; future standard or standards-track
2303 ;; ; revisions of this specification.
2305 (defun imap-parse-body-extension ()
2306 (if (eq (char-after) ?\()
2309 (push (imap-parse-body-extension) b-e)
2310 (while (eq (char-after) ?\ )
2312 (push (imap-parse-body-extension) b-e))
2313 (assert (eq (char-after) ?\)))
2316 (or (imap-parse-number)
2317 (imap-parse-nstring))))
2319 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2320 ;; *(SP body-extension)]]
2321 ;; ; MUST NOT be returned on non-extensible
2324 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2325 ;; *(SP body-extension)]]
2326 ;; ; MUST NOT be returned on non-extensible
2329 (defsubst imap-parse-body-ext ()
2331 (when (eq (char-after) ?\ );; body-fld-dsp
2334 (if (eq (char-after) ?\()
2337 (push (imap-parse-string) dsp)
2339 (push (imap-parse-string-list) dsp)
2341 (assert (imap-parse-nil)))
2342 (push (nreverse dsp) ext))
2343 (when (eq (char-after) ?\ );; body-fld-lang
2345 (if (eq (char-after) ?\()
2346 (push (imap-parse-string-list) ext)
2347 (push (imap-parse-nstring) ext))
2348 (while (eq (char-after) ?\ );; body-extension
2350 (setq ext (append (imap-parse-body-extension) ext)))))
2353 ;; body = "(" body-type-1part / body-type-mpart ")"
2355 ;; body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
2356 ;; *(SP body-extension)]]
2357 ;; ; MUST NOT be returned on non-extensible
2360 ;; body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
2361 ;; *(SP body-extension)]]
2362 ;; ; MUST NOT be returned on non-extensible
2365 ;; body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
2366 ;; body-fld-enc SP body-fld-octets
2368 ;; body-fld-desc = nstring
2370 ;; body-fld-dsp = "(" string SP body-fld-param ")" / nil
2372 ;; body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
2373 ;; "QUOTED-PRINTABLE") DQUOTE) / string
2375 ;; body-fld-id = nstring
2377 ;; body-fld-lang = nstring / "(" string *(SP string) ")"
2379 ;; body-fld-lines = number
2381 ;; body-fld-md5 = nstring
2383 ;; body-fld-octets = number
2385 ;; body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
2387 ;; body-type-1part = (body-type-basic / body-type-msg / body-type-text)
2388 ;; [SP body-ext-1part]
2390 ;; body-type-basic = media-basic SP body-fields
2391 ;; ; MESSAGE subtype MUST NOT be "RFC822"
2393 ;; body-type-msg = media-message SP body-fields SP envelope
2394 ;; SP body SP body-fld-lines
2396 ;; body-type-text = media-text SP body-fields SP body-fld-lines
2398 ;; body-type-mpart = 1*body SP media-subtype
2399 ;; [SP body-ext-mpart]
2401 ;; media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
2402 ;; "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
2403 ;; ; Defined in [MIME-IMT]
2405 ;; media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
2406 ;; ; Defined in [MIME-IMT]
2408 ;; media-subtype = string
2409 ;; ; Defined in [MIME-IMT]
2411 ;; media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
2412 ;; ; Defined in [MIME-IMT]
2414 (defun imap-parse-body ()
2416 (when (eq (char-after) ?\()
2418 (if (eq (char-after) ?\()
2420 (while (and (eq (char-after) ?\()
2421 (setq subbody (imap-parse-body)))
2422 ;; buggy stalker communigate pro 3.0 insert a SPC between
2423 ;; parts in multiparts
2424 (when (and (eq (char-after) ?\ )
2425 (eq (char-after (1+ (point))) ?\())
2427 (push subbody body))
2429 (push (imap-parse-string) body);; media-subtype
2430 (when (eq (char-after) ?\ );; body-ext-mpart:
2432 (if (eq (char-after) ?\();; body-fld-param
2433 (push (imap-parse-string-list) body)
2434 (push (and (imap-parse-nil) nil) body))
2436 (append (imap-parse-body-ext) body)));; body-ext-...
2437 (assert (eq (char-after) ?\)))
2441 (push (imap-parse-string) body);; media-type
2443 (push (imap-parse-string) body);; media-subtype
2445 ;; next line for Sun SIMS bug
2446 (and (eq (char-after) ? ) (imap-forward))
2447 (if (eq (char-after) ?\();; body-fld-param
2448 (push (imap-parse-string-list) body)
2449 (push (and (imap-parse-nil) nil) body))
2451 (push (imap-parse-nstring) body);; body-fld-id
2453 (push (imap-parse-nstring) body);; body-fld-desc
2455 ;; next `or' for Sun SIMS bug, it regard body-fld-enc as a
2456 ;; nstring and return NIL instead of defaulting back to 7BIT
2457 ;; as the standard says.
2458 (push (or (imap-parse-nstring) "7BIT") body);; body-fld-enc
2460 (push (imap-parse-number) body);; body-fld-octets
2462 ;; ok, we're done parsing the required parts, what comes now is one
2465 ;; envelope (then we're parsing body-type-msg)
2466 ;; body-fld-lines (then we're parsing body-type-text)
2467 ;; body-ext-1part (then we're parsing body-type-basic)
2469 ;; the problem is that the two first are in turn optionally followed
2470 ;; by the third. So we parse the first two here (if there are any)...
2472 (when (eq (char-after) ?\ )
2475 (cond ((eq (char-after) ?\();; body-type-msg:
2476 (push (imap-parse-envelope) body);; envelope
2478 (push (imap-parse-body) body);; body
2479 ;; buggy stalker communigate pro 3.0 doesn't print
2480 ;; number of lines in message/rfc822 attachment
2481 (if (eq (char-after) ?\))
2484 (push (imap-parse-number) body))) ;; body-fld-lines
2485 ((setq lines (imap-parse-number)) ;; body-type-text:
2486 (push lines body)) ;; body-fld-lines
2488 (backward-char))))) ;; no match...
2490 ;; ...and then parse the third one here...
2492 (when (eq (char-after) ?\ );; body-ext-1part:
2494 (push (imap-parse-nstring) body);; body-fld-md5
2495 (setq body (append (imap-parse-body-ext) body)));; body-ext-1part..
2497 (assert (eq (char-after) ?\)))
2501 (when imap-debug ; (untrace-all)
2503 (buffer-disable-undo (get-buffer-create imap-debug))
2504 (mapcar (lambda (f) (trace-function-background f imap-debug))
2516 imap-interactive-login
2532 imap-send-command-wait
2537 imap-current-mailbox
2538 imap-current-mailbox-p-1
2539 imap-current-mailbox-p
2540 imap-mailbox-select-1
2542 imap-mailbox-examine-1
2543 imap-mailbox-examine
2544 imap-mailbox-unselect
2545 imap-mailbox-expunge
2547 imap-mailbox-create-1
2553 imap-mailbox-subscribe
2554 imap-mailbox-unsubscribe
2556 imap-mailbox-acl-get
2557 imap-mailbox-acl-set
2558 imap-mailbox-acl-delete
2559 imap-current-message
2560 imap-list-to-message-set
2567 imap-message-flag-permanent-p
2568 imap-message-flags-set
2569 imap-message-flags-del
2570 imap-message-flags-add
2571 imap-message-copyuid-1
2572 imap-message-copyuid
2574 imap-message-appenduid-1
2575 imap-message-appenduid
2587 imap-parse-resp-text
2588 imap-parse-resp-text-code
2589 imap-parse-data-list
2593 imap-parse-flag-list
2595 imap-parse-body-extension
2601 ;;; imap.el ends here