]> code.delx.au - gnu-emacs-elpa/blob - packages/excorporate/excorporate.el
c32f7980e6de125aafe9f3ba935a69426d458308
[gnu-emacs-elpa] / packages / excorporate / excorporate.el
1 ;;; excorporate.el --- Exchange integration -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
6 ;; Maintainer: Thomas Fitzsimmons <fitzsim@fitzsim.org>
7 ;; Created: 2014-09-19
8 ;; Version: 0.7.1
9 ;; Keywords: calendar
10 ;; Homepage: https://www.fitzsim.org/blog/
11 ;; Package-Requires: ((emacs "24.1") (fsm "0.2") (soap-client "3.0.2") (url-http-ntlm "2.0.2"))
12
13 ;; This program is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Excorporate provides Exchange integration for Emacs.
29
30 ;; To create a connection to a web service:
31
32 ;; M-x excorporate
33
34 ;; Excorporate will prompt for an email address that it will use to
35 ;; automatically discover settings. Then it will connect to two or
36 ;; three separate hosts: the autodiscovery host, the web service host
37 ;; or load balancer, and the actual server if there is a load
38 ;; balancer. Therefore you may be prompted for your credentials two
39 ;; or three times.
40
41 ;; You should see a message indicating that the connection is ready
42 ;; either in the minibuffer or failing that in the *Messages* buffer.
43
44 ;; Finally, run M-x calendar, and press 'e' to show today's meetings.
45
46 ;; Please try autodiscovery first and report issues not yet listed
47 ;; below. When autodiscovery works it is very convenient; the goal is
48 ;; to make it work for as many users as possible.
49
50 ;; If autodiscovery fails, customize `excorporate-configuration' to
51 ;; skip autodiscovery.
52
53 ;; Autodiscovery will fail if:
54
55 ;; - Excorporate is accessing the server through a proxy (Emacs
56 ;; bug#10).
57
58 ;; - The server is not configured to support autodiscovery.
59
60 ;; - The email address is at a different domain than the server, e.g.,
61 ;; user@domain1.com, autodiscover.domain2.com.
62
63 ;; - Authentication is Kerberos/GSSAPI.
64
65 ;; Excorporate does know about the special case where the mail address
66 ;; is at a subdomain, e.g., user@sub.domain.com, and the server is at
67 ;; the main domain, e.g., autodiscover.domain.com. Autodiscovery will
68 ;; work in that case.
69
70 ;; Excorporate must be loaded before any other package that requires
71 ;; `soap-client'. The version of `soap-client' that Excorporate
72 ;; bundles is backward compatible.
73
74 ;; Acknowledgments:
75
76 ;; Alexandru Harsanyi <AlexHarsanyi@gmail.com> provided help and
77 ;; guidance on how to extend soap-client.el's WSDL and XSD handling,
78 ;; enabling support for the full Exchange Web Services API.
79
80 ;; Alex Luccisano <casual.lexicon@gmail.com> tested early versions of
81 ;; this library against a corporate installation of Exchange.
82
83 ;; Jon Miller <jonebird@gmail.com> tested against Exchange 2013. He
84 ;; also tracked down and reported a bad interaction with other
85 ;; packages that require soap-client.
86
87 ;; Nicolas Lamirault <nicolas.lamirault@gmail.com> tested the
88 ;; autodiscovery feature.
89
90 ;; Trey Jackson <bigfaceworm@gmail.com> confirmed autodiscovery worked
91 ;; for him.
92
93 ;; Joakim Verona <joakim@verona.se> tested autodiscovery in a
94 ;; Kerberos/GSSAPI environment.
95
96 ;; Wilfred Hughes <me@wilfred.me.uk> tested on Exchange 2007 and
97 ;; suggested documentation improvements.
98
99 ;;; Code:
100 \f
101 ;; Implementation-visible functions and variables.
102
103 ;; Add NTLM authorization scheme.
104 (require 'url-http-ntlm)
105 (require 'soap-client)
106 (require 'fsm)
107 (require 'excorporate-calendar)
108
109 (defconst exco--autodiscovery-templates
110 '("https://%s/autodiscover/autodiscover.svc"
111 "https://autodiscover.%s/autodiscover/autodiscover.svc")
112 "Autodiscovery URL templates.
113 URL templates to be formatted with a domain name, then searched
114 for autodiscovery files.")
115
116 (defvar exco--connections nil
117 "A hash table of finite state machines.
118 The key is the identifier passed to `exco-connect'. Each finite
119 state machine represents a service connection.")
120
121 (defvar exco--connection-identifiers nil
122 "An ordered list of connection identifiers.")
123
124 (defun exco--parse-xml-in-current-buffer ()
125 "Decode and parse the XML contents of the current buffer."
126 (let ((mime-part (mm-dissect-buffer t t)))
127 (unless mime-part
128 (error "Failed to decode response from server"))
129 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
130 (error "Server response is not an XML document"))
131 (with-temp-buffer
132 (mm-insert-part mime-part)
133 (prog1
134 (car (xml-parse-region (point-min) (point-max)))
135 (kill-buffer)
136 (mm-destroy-part mime-part)))))
137
138 (defun exco--bind-wsdl (wsdl service-url port-name target-namespace
139 binding-name)
140 "Create a WSDL binding.
141 Create a binding port for WSDL from SERVICE-URL, PORT-NAME,
142 TARGET-NAMESPACE and BINDING-NAME."
143 (let* ((namespace (soap-wsdl-find-namespace target-namespace wsdl))
144 (port (make-soap-port
145 :name port-name
146 :binding (cons target-namespace binding-name)
147 :service-url service-url)))
148 (soap-namespace-put port namespace)
149 (push port (soap-wsdl-ports wsdl))
150 (soap-resolve-references port wsdl)
151 wsdl))
152
153 (defun exco--handle-url-error (url status)
154 "Handle an error that occurred when retrieving URL.
155 The details of the error are in STATUS, in the same format as the
156 argument to a `url-retrieve' callback. Return non-nil to retry,
157 nil to continue."
158 (if (eq (cl-third (plist-get status :error)) 500)
159 ;; The server reported an internal server error. Try to recover
160 ;; by re-requesting the target URL and its most recent redirect.
161 ;; I'm not sure what conditions cause the server to get into
162 ;; this state -- it might be because the server has stale
163 ;; knowledge of old keepalive connections -- but this should
164 ;; recover it. We need to disable ntlm in
165 ;; url-registered-auth-schemes so that it doesn't prevent
166 ;; setting keepalives to nil.
167 (let ((url-registered-auth-schemes nil)
168 (url-http-attempt-keepalives nil)
169 (redirect (plist-get status :redirect)))
170 (fsm-debug-output "exco--fsm received 500 error for %s" url)
171 (url-debug 'excorporate "Attempting 500 recovery")
172 (ignore-errors
173 ;; Emacs's url-retrieve does not respect the values of
174 ;; url-http-attempt-keepalives and
175 ;; url-registered-auth-schemes in asynchronous contexts.
176 ;; Unless url.el is eventually changed to do so, the
177 ;; following requests must be synchronous so that they run
178 ;; entirely within url-http-attempt-keepalives's dynamic
179 ;; extent. These calls block the main event loop,
180 ;; unfortunately, but only in this rare error recovery
181 ;; scenario.
182 (url-retrieve-synchronously url)
183 (when redirect (url-retrieve-synchronously redirect)))
184 (url-debug 'excorporate "Done 500 recovery attempt")
185 ;; Retry.
186 t)
187 ;; We received some other error, which just
188 ;; means we should try the next URL.
189 (fsm-debug-output "exco--fsm didn't find %s" url)
190 ;; Don't retry.
191 nil))
192
193 (defun exco--retrieve-next-import (fsm state-data return-for next-state)
194 "Retrieve the next XML schema import.
195 FSM is the finite state machine, STATE-DATA is FSM's state data,
196 and RETURN-FOR is one of :enter or :event to indicate what return
197 type the calling function expects. NEXT-STATE is the next state
198 the FSM should transition to on success."
199 (let* ((url (plist-get state-data :service-url))
200 (xml (plist-get state-data :service-xml))
201 (wsdl (plist-get state-data :service-wsdl))
202 (imports (soap-wsdl-xmlschema-imports wsdl))
203 (next-state (if imports :parsing-service-wsdl next-state)))
204 (when imports
205 (let ((import-url (url-expand-file-name (pop imports) url)))
206 (let ((url-request-method "GET")
207 (url-package-name "soap-client.el")
208 (url-package-version "1.0")
209 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
210 (url-http-attempt-keepalives t))
211 (url-retrieve
212 import-url
213 (lambda (status)
214 (let ((data-buffer (current-buffer)))
215 (unwind-protect
216 (progn
217 (url-debug 'excorporate "Processing import %s" status)
218 (if (eq (car status) :error)
219 ;; There is an error. It may be recoverable
220 ;; if it's HTTP 500 (internal server error).
221 (if (and (exco--handle-url-error import-url status)
222 ;; Only retry once.
223 (not (plist-get state-data :retrying)))
224 ;; We should retry. Don't save the
225 ;; popped urls list to state-data, so
226 ;; that this :try-next-url will
227 ;; re-attempt to retrieve the same car as
228 ;; before. Set the retry flag.
229 (progn
230 (plist-put state-data :retrying t))
231 ;; Save the popped urls list so that the next url
232 ;; is attempted, and clear the retry flag.
233 (plist-put state-data :retrying nil)
234 (setf (soap-wsdl-xmlschema-imports wsdl) imports)
235 (plist-put state-data :failure-message
236 (format "Failed to retrieve %s"
237 import-url))
238 (fsm-send fsm :unrecoverable-error))
239 ;; Success, parse WSDL.
240 (plist-put state-data :retrying nil)
241 (setf (soap-wsdl-xmlschema-imports wsdl) imports)
242 (soap-with-local-xmlns xml
243 (soap-wsdl-add-namespace
244 (soap-parse-schema (soap-parse-server-response) wsdl)
245 wsdl))
246 (plist-put state-data :service-wsdl wsdl)))
247 (and (buffer-live-p data-buffer)
248 (kill-buffer data-buffer))))
249 (fsm-send fsm t))))))
250 (if (eq return-for :enter)
251 (list state-data nil)
252 (list next-state state-data nil))))
253
254 (define-state-machine exco--fsm :start
255 ((identifier)
256 "Start an Excorporate finite state machine."
257 (if (stringp identifier)
258 (let ((domain (cadr (split-string identifier "@"))))
259 (unless (and domain (not (equal domain "")))
260 (error "Invalid domain for address %s" identifier))
261 (list :retrieving-autodiscovery-xml
262 (list
263 ;; State machine data.
264 ;; Unique finite state machine identifier. Either mail-address
265 ;; or (mail-address . service-url). The latter allows multiple
266 ;; state machines to operate on the same service URL. Login
267 ;; credentials are handled separately by auth-source and url,
268 ;; so these should be the only two identifier types needed here.
269 :identifier identifier
270 ;; User data.
271 :mail-address identifier
272 ;; Error recovery data.
273 :retrying nil
274 ;; Autodiscovery data.
275 :autodiscovery-urls
276 (append (mapcar (lambda (template)
277 (format template domain))
278 exco--autodiscovery-templates)
279 ;; Handle the user@sub.domain.com =>
280 ;; autodiscover.domain.com case reported by a
281 ;; user. Only try one extra level.
282 (let ((domain-parts (split-string domain "\\.")))
283 (when (> (length domain-parts) 2)
284 (mapcar (lambda (template)
285 (format template
286 (mapconcat
287 'identity
288 (cdr domain-parts) ".")))
289 exco--autodiscovery-templates))))
290 ;; Service data.
291 :service-url nil
292 :service-xml nil
293 :service-wsdl nil
294 ;; State data.
295 :next-state-after-success nil
296 :failure-message nil
297 :server-version nil)
298 ;; No timeout.
299 nil))
300 ;; Go directly to :retrieving-service-xml, skipping autodiscovery.
301 (list :retrieving-service-xml
302 (list
303 :identifier identifier
304 :mail-address (car identifier)
305 :retrying nil
306 :autodiscovery-urls nil
307 ;; Use service-url field from identifier.
308 :service-url (cdr identifier)
309 :service-xml nil
310 :service-wsdl nil
311 :next-state-after-success nil
312 :failure-message nil
313 :server-version nil)
314 ;; No timeout.
315 nil))))
316
317 (define-state exco--fsm :retrieving-autodiscovery-xml
318 (fsm state-data event _callback)
319 (cl-case event
320 (:try-next-url
321 (let ((urls (plist-get state-data :autodiscovery-urls)))
322 (if urls
323 (let ((url (pop urls)))
324 (fsm-debug-output "exco--fsm will probe %s" url)
325 (condition-case nil
326 (url-retrieve
327 url
328 (lambda (status)
329 (let ((data-buffer (current-buffer)))
330 (unwind-protect
331 (progn
332 (url-debug 'excorporate
333 "Processing status: %s" status)
334 (if (eq (car status) :error)
335 (progn
336 (if (and
337 (exco--handle-url-error url status)
338 ;; Only retry once.
339 (not (plist-get state-data :retrying)))
340 ;; We should retry. Don't save the popped
341 ;; urls list to state-data, so that this
342 ;; :try-next-url will re-attempt to
343 ;; retrieve the same car as before. Set
344 ;; the retry flag.
345 (plist-put state-data :retrying t)
346 ;; Save the popped urls list so that the
347 ;; next url is attempted, and clear the
348 ;; retry flag.
349 (plist-put state-data :retrying nil)
350 (plist-put state-data
351 :autodiscovery-urls urls))
352 ;; Try next or retry.
353 (fsm-send fsm :try-next-url))
354 ;; Success, save URL and parse returned XML.
355 (message
356 "Excorporate: Found autodiscovery URL for %S: %s"
357 (plist-get state-data :identifier) url)
358 (plist-put state-data :retrying nil)
359 (plist-put state-data :service-url url)
360 (plist-put state-data :service-xml
361 (exco--parse-xml-in-current-buffer))
362 (fsm-send fsm :success))
363 (url-debug 'excorporate "Done processing status"))
364 (and (buffer-live-p data-buffer)
365 (kill-buffer data-buffer))))))
366 (error
367 (fsm-debug-output "exco--fsm connection refused for %s" url)
368 (plist-put state-data :retrying nil)
369 (plist-put state-data :autodiscovery-urls urls)
370 (fsm-send fsm :try-next-url)))
371 (list :retrieving-autodiscovery-xml state-data nil))
372 (plist-put state-data :failure-message
373 "Autodiscovery ran out of URLs to try")
374 (list :shutting-down-on-error state-data nil))))
375 (:success
376 (plist-put state-data :next-state-after-success :retrieving-service-xml)
377 (list :parsing-service-wsdl state-data nil))))
378
379 (define-enter-state exco--fsm :shutting-down-on-error
380 (_fsm state-data)
381 (let ((failure-message (plist-get state-data :failure-message)))
382 (exco-disconnect (plist-get state-data :identifier))
383 (message "Excorporate: %s" failure-message)
384 (url-debug 'excorporate "Failed: %s" failure-message)
385 (fsm-debug-output "exco--fsm failed: %s" failure-message))
386 (list state-data nil))
387
388 (define-state exco--fsm :shutting-down-on-error
389 (_fsm state-data _event _callback)
390 (list :shutting-down-on-error state-data nil))
391
392 (define-enter-state exco--fsm :retrieving-service-xml
393 (fsm state-data)
394 (when (stringp (plist-get state-data :identifier))
395 (let* ((xml (plist-get state-data :service-xml))
396 (unbound-wsdl (plist-get state-data :service-wsdl))
397 (wsdl
398 (progn
399 ;; Skip soap-parse-wsdl-phase-fetch-schema to avoid
400 ;; synchronous URL fetches.
401 (soap-parse-wsdl-phase-finish-parsing xml unbound-wsdl)
402 (exco--bind-wsdl
403 (soap-wsdl-resolve-references unbound-wsdl)
404 (plist-get state-data :service-url)
405 "AutodiscoverServicePort"
406 "http://schemas.microsoft.com/exchange/2010/Autodiscover"
407 "DefaultBinding_Autodiscover"))))
408 (soap-invoke-async
409 (lambda (response)
410 (let ((result-url
411 (exco-extract-value '(Response
412 UserResponses
413 UserResponse
414 UserSettings
415 UserSetting
416 Value)
417 response)))
418 (if result-url
419 (progn
420 (plist-put state-data :service-url result-url)
421 (message "Excorporate: Found service URL for %S: %s"
422 (plist-get state-data :identifier)
423 (plist-get state-data :service-url)))
424 ;; No result. Check for error.
425 (let ((error-message
426 (exco-extract-value '(Response
427 UserResponses
428 UserResponse
429 ErrorMessage)
430 response)))
431 (if error-message
432 (message "Excorporate: %s" error-message)
433 (message "Excorporate: Failed to find service URL"))))
434 (fsm-send fsm :retrieve-xml)))
435 nil
436 wsdl
437 "AutodiscoverServicePort"
438 "GetUserSettings"
439 `((RequestedServerVersion . "Exchange2010")
440 (Request
441 (Users
442 (User
443 (Mailbox . ,(plist-get state-data :mail-address))))
444 (RequestedSettings
445 (Setting . "InternalEwsUrl")))))))
446 (list state-data nil))
447
448 (define-state exco--fsm :retrieving-service-xml
449 (fsm state-data event _callback)
450 (cl-case event
451 (:unrecoverable-error
452 (list :shutting-down-on-error state-data nil))
453 (:retrieve-xml
454 (let* ((service-url (plist-get state-data :service-url))
455 (wsdl-url (replace-regexp-in-string "/[^/]*$" "/Services.wsdl"
456 service-url)))
457 (url-retrieve wsdl-url
458 (lambda (status)
459 (let ((data-buffer (current-buffer)))
460 (unwind-protect
461 (if (eq (car status) :error)
462 (progn
463 (plist-put state-data :failure-message
464 (format "Failed to retrieve %s"
465 wsdl-url))
466 (fsm-send fsm :unrecoverable-error))
467 (plist-put state-data
468 :service-xml
469 (exco--parse-xml-in-current-buffer))
470 (fsm-send fsm :success))
471 (and (buffer-live-p data-buffer)
472 (kill-buffer data-buffer)))))))
473 (list :retrieving-service-xml state-data nil))
474 (:success
475 (plist-put state-data :next-state-after-success :retrieving-data)
476 (list :parsing-service-wsdl state-data nil))))
477
478 (define-enter-state exco--fsm :parsing-service-wsdl
479 (fsm state-data)
480 (let* ((url (plist-get state-data :service-url))
481 (xml (plist-get state-data :service-xml))
482 (next-state (plist-get state-data :next-state-after-success))
483 (wsdl (soap-make-wsdl url)))
484 (soap-parse-wsdl-phase-validate-node xml)
485 ;; Skip soap-parse-wsdl-phase-fetch-imports to avoid synchronous
486 ;; fetches of import URLs.
487 (soap-parse-wsdl-phase-parse-schema xml wsdl)
488 (plist-put state-data :service-wsdl wsdl)
489 (exco--retrieve-next-import fsm state-data :enter next-state)))
490
491 (define-state exco--fsm :parsing-service-wsdl
492 (fsm state-data event _callback)
493 (if (eq event :unrecoverable-error)
494 (list :shutting-down-on-error state-data nil)
495 (let ((next-state (plist-get state-data :next-state-after-success)))
496 (exco--retrieve-next-import fsm state-data :event next-state))))
497
498 (defun exco--get-server-version (wsdl)
499 "Extract server version from WSDL."
500 (catch 'found
501 (dolist (attribute
502 (soap-xs-type-attributes
503 (soap-xs-element-type
504 (soap-wsdl-get
505 '("http://schemas.microsoft.com/exchange/services/2006/types"
506 . "RequestServerVersion")
507 wsdl 'soap-xs-element-p))))
508 (when (equal (soap-xs-attribute-name attribute) "Version")
509 (throw 'found (soap-xs-attribute-default attribute))))
510 (warn "Excorporate: Failed to determine server version")
511 nil))
512
513 (define-enter-state exco--fsm :retrieving-data
514 (_fsm state-data)
515 (let ((wsdl (plist-get state-data :service-wsdl))
516 (identifier (plist-get state-data :identifier)))
517 ;; Skip soap-parse-wsdl-phase-fetch-schema to avoid synchronous
518 ;; URL fetches.
519 (soap-parse-wsdl-phase-finish-parsing (plist-get state-data :service-xml)
520 wsdl)
521 (exco--bind-wsdl
522 (soap-wsdl-resolve-references wsdl)
523 (plist-get state-data :service-url)
524 "ExchangeServicePort"
525 "http://schemas.microsoft.com/exchange/services/2006/messages"
526 "ExchangeServiceBinding")
527 (plist-put state-data :server-version (exco--get-server-version wsdl))
528 (fsm-debug-output "exco--fsm %s server version is %s"
529 identifier (exco-server-version identifier))
530 (message "Excorporate: Connection %S is ready" identifier))
531 (list state-data nil))
532
533 (define-state exco--fsm :retrieving-data
534 (_fsm state-data event _callback)
535 (let* ((identifier (plist-get state-data :identifier))
536 (wsdl (plist-get state-data :service-wsdl))
537 (name (pop event))
538 (arguments (pop event))
539 (callback (pop event)))
540 (apply #'soap-invoke-async
541 (lambda (response)
542 (funcall callback identifier response))
543 nil
544 wsdl
545 "ExchangeServicePort"
546 name
547 arguments))
548 (list :retrieving-data state-data nil))
549
550 (defun exco--ensure-connection ()
551 "Ensure at least one connection exists or throw an error."
552 (unless exco--connection-identifiers
553 (error "Excorporate: No connections exist. Run M-x excorporate")))
554
555 (defmacro exco--with-fsm (identifier &rest body)
556 "With `fsm' set to IDENTIFIER, run BODY.
557 Run BODY with `fsm' set to the finite state machine specified by
558 IDENTIFIER."
559 (declare (indent 1) (debug t))
560 `(progn
561 (exco--ensure-connection)
562 (let ((fsm (gethash ,identifier exco--connections)))
563 (unless fsm
564 (error "Excorporate: Connection %S does not exist" ,identifier))
565 ,@body)))
566 \f
567 ;; Developer-visible functions and variables.
568
569 (defun exco-api-version ()
570 "Return the Excorporate API version.
571 Return a non-negative integer representing the current
572 Excorporate application programming interface version. Version 0
573 is subject to change."
574 0)
575
576 (defun exco-connect (identifier)
577 "Connect or reconnect to a web service.
578 IDENTIFIER is the mail address to use for autodiscovery or a
579 pair (mail-address . service-url)."
580 (if (stringp identifier)
581 (message "Excorporate: Starting autodiscovery for %S"
582 identifier))
583 (let ((fsm (start-exco--fsm identifier)))
584 (unless exco--connections
585 (setq exco--connections (make-hash-table :test 'equal)))
586 (when (gethash identifier exco--connections)
587 (exco-disconnect identifier))
588 (puthash identifier fsm exco--connections)
589 (push identifier exco--connection-identifiers)
590 (if (stringp identifier)
591 (fsm-send fsm :try-next-url)
592 (fsm-send fsm :retrieve-xml))
593 nil))
594
595 (defun exco-operate (identifier name arguments callback)
596 "Execute a service operation asynchronously.
597 IDENTIFIER is the connection identifier. Execute operation NAME
598 with ARGUMENTS then call CALLBACK with two arguments, IDENTIFIER
599 and the server's response."
600 (exco--with-fsm identifier
601 (fsm-send fsm (list name arguments callback)))
602 nil)
603
604 (defun exco-server-version (identifier)
605 "Return the server version for connection IDENTIFIER, as a string.
606 Examples are \"Exchange2010\", \"Exchange2010_SP1\",
607 \"Exchange2013\"."
608 (exco--with-fsm identifier
609 (plist-get (fsm-get-state-data fsm) :server-version)))
610
611 (defun exco-disconnect (identifier)
612 "Disconnect from a web service.
613 IDENTIFIER is the mail address used to look up the connection."
614 (exco--with-fsm identifier
615 (setq exco--connection-identifiers
616 (delete identifier exco--connection-identifiers))
617 (remhash identifier exco--connections))
618 nil)
619
620 (defun exco-extract-value (path result)
621 "Extract the value at PATH from RESULT.
622 PATH is an ordered list of node names."
623 (let ((values (nreverse (car result))))
624 (dolist (path-element path)
625 (setq values (assoc path-element values)))
626 (cdr values)))
627
628 (defun exco-calendar-item-iterate (response callback)
629 "Iterate through calendar items in RESPONSE, calling CALLBACK on each.
630 Returns a list of results from callback. CALLBACK takes arguments:
631 SUBJECT, a string, the subject of the meeting.
632 START, the start date and time in Emacs internal representation.
633 END, the start date and time in Emacs internal representation.
634 LOCATION, the location of the meeting.
635 MAIN-INVITEES, a list of strings representing required participants.
636 OPTIONAL-INVITEES, a list of strings representing optional participants."
637 (let ((result-list '()))
638 (dolist (calendar-item (exco-extract-value '(ResponseMessages
639 FindItemResponseMessage
640 RootFolder
641 Items)
642 response))
643 (let* ((subject (cdr (assoc 'Subject calendar-item)))
644 (start (cdr (assoc 'Start calendar-item)))
645 (start-internal (apply #'encode-time
646 (soap-decode-date-time
647 start 'dateTime)))
648 (end (cdr (assoc 'End calendar-item)))
649 (end-internal (apply #'encode-time
650 (soap-decode-date-time
651 end 'dateTime)))
652 (location (cdr (assoc 'Location calendar-item)))
653 (to-invitees (cdr (assoc 'DisplayTo calendar-item)))
654 (main-invitees (when to-invitees
655 (mapcar 'org-trim
656 (split-string to-invitees ";"))))
657 (cc-invitees (cdr (assoc 'DisplayCc calendar-item)))
658 (optional-invitees (when cc-invitees
659 (mapcar 'org-trim
660 (split-string cc-invitees ";")))))
661 (push (funcall callback subject start-internal end-internal
662 location main-invitees optional-invitees)
663 result-list)))
664 (nreverse result-list)))
665
666 ;; Date-time utility functions.
667 (defun exco-extend-timezone (date-time-string)
668 "Add a colon to the timezone in DATE-TIME-STRING.
669 DATE-TIME-STRING must be formatted as if returned by
670 `format-time-string' with FORMAT-STRING \"%FT%T%z\". Web
671 services require the ISO8601 extended format of timezone, which
672 includes the colon."
673 (concat
674 (substring date-time-string 0 22) ":" (substring date-time-string 22)))
675
676 (defun exco-format-date-time (time-internal)
677 "Convert TIME-INTERNAL to an XSD compatible date-time string."
678 (exco-extend-timezone
679 (format-time-string "%FT%T%z" time-internal)))
680
681 ;; Use month day year order to be compatible with
682 ;; calendar-cursor-to-date. I wish I could instead use the ISO 8601
683 ;; ordering, year month day.
684 (defun exco-get-meetings-for-day (identifier month day year callback)
685 "Return the meetings for the specified day.
686 IDENTIFIER is the connection identifier. MONTH, DAY and YEAR are
687 the meeting month, day and year. Call CALLBACK with two
688 arguments, IDENTIFIER and the server's response."
689 (let* ((start-of-day-time-internal
690 (apply #'encode-time `(0 0 0 ,day ,month ,year)))
691 (start-of-day-date-time
692 (exco-format-date-time start-of-day-time-internal))
693 (start-of-next-day-date-time
694 (exco-extend-timezone
695 (format-time-string "%FT00:00:00%z"
696 (time-add start-of-day-time-internal
697 (seconds-to-time 86400))))))
698 (exco-operate
699 identifier
700 "FindItem"
701 `(;; Main arguments.
702 ((Traversal . "Shallow")
703 (ItemShape
704 (BaseShape . "AllProperties"))
705 ;; To aid productivity, excorporate-calfw automatically prunes your
706 ;; meetings to a maximum of 100 per day.
707 (CalendarView (MaxEntriesReturned . "100")
708 (StartDate . ,start-of-day-date-time)
709 (EndDate . ,start-of-next-day-date-time))
710 (ParentFolderIds
711 (DistinguishedFolderId (Id . "calendar"))))
712 ;; Empty arguments.
713 ,@(let ((server-major-version
714 (string-to-number
715 (substring (exco-server-version identifier) 8 12))))
716 (cond
717 ((<= server-major-version 2007)
718 '(nil nil nil nil))
719 ((< server-major-version 2013)
720 '(nil nil nil nil nil))
721 (t
722 '(nil nil nil nil nil nil)))))
723 callback)))
724
725 (defun exco-connection-iterate (initialize-function
726 per-connection-function
727 per-connection-callback
728 finalize-function)
729 "Iterate Excorporate connections.
730 Call INITIALIZE-FUNCTION once before iterating.
731 Call PER-CONNECTION-FUNCTION for each connection.
732 Pass PER-CONNECTION-CALLBACK to PER-CONNECTION-FUNCTION.
733 Call FINALIZE-FUNCTION after all operations have responded."
734 (exco--ensure-connection)
735 (funcall initialize-function)
736 (let ((responses 0)
737 (connection-count (length exco--connection-identifiers)))
738 (dolist (identifier exco--connection-identifiers)
739 (funcall per-connection-function identifier
740 (lambda (&rest arguments)
741 (setq responses (1+ responses))
742 (apply per-connection-callback arguments)
743 (when (equal responses connection-count)
744 (funcall finalize-function)))))))
745 \f
746 ;; User-visible functions and variables.
747 (defgroup excorporate nil
748 "Exchange support."
749 :version "25.1"
750 :group 'comm
751 :group 'calendar)
752
753 ;; Name the excorporate-configuration variable vaguely. It is currently a
754 ;; MAIL-ADDRESS string, a pair (MAIL-ADDRESS . SERVICE-URL), or nil. In the
755 ;; future it could allow a list of strings and pairs.
756 (defcustom excorporate-configuration nil
757 "Excorporate configuration.
758 The mail address to use for autodiscovery."
759 :type '(choice
760 (const
761 :tag "Prompt for Exchange mail address to use for autodiscovery" nil)
762 (string :tag "Exchange mail address to use for autodiscovery")
763 (cons :tag "Skip autodiscovery"
764 (string :tag "Exchange mail address (e.g., hacker@gnu.org)")
765 (string :tag "Exchange Web Services URL\
766 (e.g., https://mail.gnu.org/ews/exchange.asmx)"))))
767
768 ;;;###autoload
769 (defun excorporate ()
770 "Start Excorporate.
771 Prompt for a mail address to use for autodiscovery, with an
772 initial suggestion of `user-mail-address'. However, if
773 `excorporate-configuration' is non-nil, `excorporate' will use
774 that without prompting."
775 (interactive)
776 (cond
777 ((eq excorporate-configuration nil)
778 (exco-connect (completing-read "Exchange mail address: "
779 (list user-mail-address)
780 nil nil user-mail-address)))
781 ((stringp excorporate-configuration)
782 (exco-connect excorporate-configuration))
783 ((null (consp (cdr excorporate-configuration)))
784 (exco-connect excorporate-configuration))
785 (t
786 (error "Excorporate: Invalid configuration"))))
787
788 (provide 'excorporate)
789
790 ;;; excorporate.el ends here