]> code.delx.au - gnu-emacs-elpa/blob - packages/excorporate/excorporate.el
Update packages/yasnippet by subtree-merging from its github-based upstream
[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 (url-retrieve (concat service-url "?wsdl")
456 (lambda (status)
457 (let ((data-buffer (current-buffer)))
458 (unwind-protect
459 (if (eq (car status) :error)
460 (progn
461 (plist-put state-data :failure-message
462 (format "Failed to retrieve %s"
463 service-url))
464 (fsm-send fsm :unrecoverable-error))
465 (plist-put state-data
466 :service-xml
467 (exco--parse-xml-in-current-buffer))
468 (fsm-send fsm :success))
469 (and (buffer-live-p data-buffer)
470 (kill-buffer data-buffer)))))))
471 (list :retrieving-service-xml state-data nil))
472 (:success
473 (plist-put state-data :next-state-after-success :retrieving-data)
474 (list :parsing-service-wsdl state-data nil))))
475
476 (define-enter-state exco--fsm :parsing-service-wsdl
477 (fsm state-data)
478 (let* ((url (plist-get state-data :service-url))
479 (xml (plist-get state-data :service-xml))
480 (next-state (plist-get state-data :next-state-after-success))
481 (wsdl (soap-make-wsdl url)))
482 (soap-parse-wsdl-phase-validate-node xml)
483 ;; Skip soap-parse-wsdl-phase-fetch-imports to avoid synchronous
484 ;; fetches of import URLs.
485 (soap-parse-wsdl-phase-parse-schema xml wsdl)
486 (plist-put state-data :service-wsdl wsdl)
487 (exco--retrieve-next-import fsm state-data :enter next-state)))
488
489 (define-state exco--fsm :parsing-service-wsdl
490 (fsm state-data event _callback)
491 (if (eq event :unrecoverable-error)
492 (list :shutting-down-on-error state-data nil)
493 (let ((next-state (plist-get state-data :next-state-after-success)))
494 (exco--retrieve-next-import fsm state-data :event next-state))))
495
496 (defun exco--get-server-version (wsdl)
497 "Extract server version from WSDL."
498 (catch 'found
499 (dolist (attribute
500 (soap-xs-type-attributes
501 (soap-xs-element-type
502 (soap-wsdl-get
503 '("http://schemas.microsoft.com/exchange/services/2006/types"
504 . "RequestServerVersion")
505 wsdl 'soap-xs-element-p))))
506 (when (equal (soap-xs-attribute-name attribute) "Version")
507 (throw 'found (soap-xs-attribute-default attribute))))
508 (warn "Excorporate: Failed to determine server version")
509 nil))
510
511 (define-enter-state exco--fsm :retrieving-data
512 (_fsm state-data)
513 (let ((wsdl (plist-get state-data :service-wsdl))
514 (identifier (plist-get state-data :identifier)))
515 ;; Skip soap-parse-wsdl-phase-fetch-schema to avoid synchronous
516 ;; URL fetches.
517 (soap-parse-wsdl-phase-finish-parsing (plist-get state-data :service-xml)
518 wsdl)
519 (exco--bind-wsdl
520 (soap-wsdl-resolve-references wsdl)
521 (plist-get state-data :service-url)
522 "ExchangeServicePort"
523 "http://schemas.microsoft.com/exchange/services/2006/messages"
524 "ExchangeServiceBinding")
525 (plist-put state-data :server-version (exco--get-server-version wsdl))
526 (fsm-debug-output "exco--fsm %s server version is %s"
527 identifier (exco-server-version identifier))
528 (message "Excorporate: Connection %S is ready" identifier))
529 (list state-data nil))
530
531 (define-state exco--fsm :retrieving-data
532 (_fsm state-data event _callback)
533 (let* ((identifier (plist-get state-data :identifier))
534 (wsdl (plist-get state-data :service-wsdl))
535 (name (pop event))
536 (arguments (pop event))
537 (callback (pop event)))
538 (apply #'soap-invoke-async
539 (lambda (response)
540 (funcall callback identifier response))
541 nil
542 wsdl
543 "ExchangeServicePort"
544 name
545 arguments))
546 (list :retrieving-data state-data nil))
547
548 (defun exco--ensure-connection ()
549 "Ensure at least one connection exists or throw an error."
550 (unless exco--connection-identifiers
551 (error "Excorporate: No connections exist. Run M-x excorporate")))
552
553 (defmacro exco--with-fsm (identifier &rest body)
554 "With `fsm' set to IDENTIFIER, run BODY.
555 Run BODY with `fsm' set to the finite state machine specified by
556 IDENTIFIER."
557 (declare (indent 1) (debug t))
558 `(progn
559 (exco--ensure-connection)
560 (let ((fsm (gethash ,identifier exco--connections)))
561 (unless fsm
562 (error "Excorporate: Connection %S does not exist" ,identifier))
563 ,@body)))
564 \f
565 ;; Developer-visible functions and variables.
566
567 (defun exco-api-version ()
568 "Return the Excorporate API version.
569 Return a non-negative integer representing the current
570 Excorporate application programming interface version. Version 0
571 is subject to change."
572 0)
573
574 (defun exco-connect (identifier)
575 "Connect or reconnect to a web service.
576 IDENTIFIER is the mail address to use for autodiscovery or a
577 pair (mail-address . service-url)."
578 (if (stringp identifier)
579 (message "Excorporate: Starting autodiscovery for %S"
580 identifier))
581 (let ((fsm (start-exco--fsm identifier)))
582 (unless exco--connections
583 (setq exco--connections (make-hash-table :test 'equal)))
584 (when (gethash identifier exco--connections)
585 (exco-disconnect identifier))
586 (puthash identifier fsm exco--connections)
587 (push identifier exco--connection-identifiers)
588 (if (stringp identifier)
589 (fsm-send fsm :try-next-url)
590 (fsm-send fsm :retrieve-xml))
591 nil))
592
593 (defun exco-operate (identifier name arguments callback)
594 "Execute a service operation asynchronously.
595 IDENTIFIER is the connection identifier. Execute operation NAME
596 with ARGUMENTS then call CALLBACK with two arguments, IDENTIFIER
597 and the server's response."
598 (exco--with-fsm identifier
599 (fsm-send fsm (list name arguments callback)))
600 nil)
601
602 (defun exco-server-version (identifier)
603 "Return the server version for connection IDENTIFIER, as a string.
604 Examples are \"Exchange2010\", \"Exchange2010_SP1\",
605 \"Exchange2013\"."
606 (exco--with-fsm identifier
607 (plist-get (fsm-get-state-data fsm) :server-version)))
608
609 (defun exco-disconnect (identifier)
610 "Disconnect from a web service.
611 IDENTIFIER is the mail address used to look up the connection."
612 (exco--with-fsm identifier
613 (setq exco--connection-identifiers
614 (delete identifier exco--connection-identifiers))
615 (remhash identifier exco--connections))
616 nil)
617
618 (defun exco-extract-value (path result)
619 "Extract the value at PATH from RESULT.
620 PATH is an ordered list of node names."
621 (let ((values (nreverse (car result))))
622 (dolist (path-element path)
623 (setq values (assoc path-element values)))
624 (cdr values)))
625
626 (defun exco-calendar-item-iterate (response callback)
627 "Iterate through calendar items in RESPONSE, calling CALLBACK on each.
628 Returns a list of results from callback. CALLBACK takes arguments:
629 SUBJECT, a string, the subject of the meeting.
630 START, the start date and time in Emacs internal representation.
631 END, the start date and time in Emacs internal representation.
632 LOCATION, the location of the meeting.
633 MAIN-INVITEES, a list of strings representing required participants.
634 OPTIONAL-INVITEES, a list of strings representing optional participants."
635 (let ((result-list '()))
636 (dolist (calendar-item (exco-extract-value '(ResponseMessages
637 FindItemResponseMessage
638 RootFolder
639 Items)
640 response))
641 (let* ((subject (cdr (assoc 'Subject calendar-item)))
642 (start (cdr (assoc 'Start calendar-item)))
643 (start-internal (apply #'encode-time
644 (soap-decode-date-time
645 start 'dateTime)))
646 (end (cdr (assoc 'End calendar-item)))
647 (end-internal (apply #'encode-time
648 (soap-decode-date-time
649 end 'dateTime)))
650 (location (cdr (assoc 'Location calendar-item)))
651 (to-invitees (cdr (assoc 'DisplayTo calendar-item)))
652 (main-invitees (mapcar 'org-trim (split-string to-invitees ";")))
653 (cc-invitees (cdr (assoc 'DisplayCc calendar-item)))
654 (optional-invitees (when cc-invitees
655 (mapcar 'org-trim
656 (split-string cc-invitees ";")))))
657 (push (funcall callback subject start-internal end-internal
658 location main-invitees optional-invitees)
659 result-list)))
660 (nreverse result-list)))
661
662 ;; Date-time utility functions.
663 (defun exco-extend-timezone (date-time-string)
664 "Add a colon to the timezone in DATE-TIME-STRING.
665 DATE-TIME-STRING must be formatted as if returned by
666 `format-time-string' with FORMAT-STRING \"%FT%T%z\". Web
667 services require the ISO8601 extended format of timezone, which
668 includes the colon."
669 (concat
670 (substring date-time-string 0 22) ":" (substring date-time-string 22)))
671
672 (defun exco-format-date-time (time-internal)
673 "Convert TIME-INTERNAL to an XSD compatible date-time string."
674 (exco-extend-timezone
675 (format-time-string "%FT%T%z" time-internal)))
676
677 ;; Use month day year order to be compatible with
678 ;; calendar-cursor-to-date. I wish I could instead use the ISO 8601
679 ;; ordering, year month day.
680 (defun exco-get-meetings-for-day (identifier month day year callback)
681 "Return the meetings for the specified day.
682 IDENTIFIER is the connection identifier. MONTH, DAY and YEAR are
683 the meeting month, day and year. Call CALLBACK with two
684 arguments, IDENTIFIER and the server's response."
685 (let* ((start-of-day-time-internal
686 (apply #'encode-time `(0 0 0 ,day ,month ,year)))
687 (start-of-day-date-time
688 (exco-format-date-time start-of-day-time-internal))
689 (start-of-next-day-date-time
690 (exco-extend-timezone
691 (format-time-string "%FT00:00:00%z"
692 (time-add start-of-day-time-internal
693 (seconds-to-time 86400))))))
694 (exco-operate
695 identifier
696 "FindItem"
697 `(;; Main arguments.
698 ((Traversal . "Shallow")
699 (ItemShape
700 (BaseShape . "AllProperties"))
701 ;; To aid productivity, excorporate-calfw automatically prunes your
702 ;; meetings to a maximum of 100 per day.
703 (CalendarView (MaxEntriesReturned . "100")
704 (StartDate . ,start-of-day-date-time)
705 (EndDate . ,start-of-next-day-date-time))
706 (ParentFolderIds
707 (DistinguishedFolderId (Id . "calendar"))))
708 ;; Empty arguments.
709 ,@(let ((server-major-version
710 (string-to-number
711 (substring (exco-server-version identifier) 8 12))))
712 (cond
713 ((<= server-major-version 2007)
714 '(nil nil nil nil))
715 ((< server-major-version 2013)
716 '(nil nil nil nil nil))
717 (t
718 '(nil nil nil nil nil nil)))))
719 callback)))
720
721 (defun exco-connection-iterate (initialize-function
722 per-connection-function
723 per-connection-callback
724 finalize-function)
725 "Iterate Excorporate connections.
726 Call INITIALIZE-FUNCTION once before iterating.
727 Call PER-CONNECTION-FUNCTION for each connection.
728 Pass PER-CONNECTION-CALLBACK to PER-CONNECTION-FUNCTION.
729 Call FINALIZE-FUNCTION after all operations have responded."
730 (exco--ensure-connection)
731 (funcall initialize-function)
732 (let ((responses 0)
733 (connection-count (length exco--connection-identifiers)))
734 (dolist (identifier exco--connection-identifiers)
735 (funcall per-connection-function identifier
736 (lambda (&rest arguments)
737 (setq responses (1+ responses))
738 (apply per-connection-callback arguments)
739 (when (equal responses connection-count)
740 (funcall finalize-function)))))))
741 \f
742 ;; User-visible functions and variables.
743 (defgroup excorporate nil
744 "Exchange support."
745 :version "25.1"
746 :group 'comm
747 :group 'calendar)
748
749 ;; Name the excorporate-configuration variable vaguely. It is currently a
750 ;; MAIL-ADDRESS string, a pair (MAIL-ADDRESS . SERVICE-URL), or nil. In the
751 ;; future it could allow a list of strings and pairs.
752 (defcustom excorporate-configuration nil
753 "Excorporate configuration.
754 The mail address to use for autodiscovery."
755 :type '(choice
756 (const
757 :tag "Prompt for Exchange mail address to use for autodiscovery" nil)
758 (string :tag "Exchange mail address to use for autodiscovery")
759 (cons :tag "Skip autodiscovery"
760 (string :tag "Exchange mail address (e.g., hacker@gnu.org)")
761 (string :tag "Exchange Web Services URL\
762 (e.g., https://mail.gnu.org/ews/exchange.asmx)"))))
763
764 ;;;###autoload
765 (defun excorporate ()
766 "Start Excorporate.
767 Prompt for a mail address to use for autodiscovery, with an
768 initial suggestion of `user-mail-address'. However, if
769 `excorporate-configuration' is non-nil, `excorporate' will use
770 that without prompting."
771 (interactive)
772 (cond
773 ((eq excorporate-configuration nil)
774 (exco-connect (completing-read "Exchange mail address: "
775 (list user-mail-address)
776 nil nil user-mail-address)))
777 ((stringp excorporate-configuration)
778 (exco-connect excorporate-configuration))
779 ((null (consp (cdr excorporate-configuration)))
780 (exco-connect excorporate-configuration))
781 (t
782 (error "Excorporate: Invalid configuration"))))
783
784 (provide 'excorporate)
785
786 ;;; excorporate.el ends here