1 ;;; excorporate.el --- Exchange integration -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 ;; Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
6 ;; Maintainer: Thomas Fitzsimmons <fitzsim@fitzsim.org>
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"))
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.
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.
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/>.
28 ;; Excorporate provides Exchange integration for Emacs.
30 ;; To create a connection to a web service:
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
41 ;; You should see a message indicating that the connection is ready
42 ;; either in the minibuffer or failing that in the *Messages* buffer.
44 ;; Finally, run M-x calendar, and press 'e' to show today's meetings.
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.
50 ;; If autodiscovery fails, customize `excorporate-configuration' to
51 ;; skip autodiscovery.
53 ;; Autodiscovery will fail if:
55 ;; - Excorporate is accessing the server through a proxy (Emacs
58 ;; - The server is not configured to support autodiscovery.
60 ;; - The email address is at a different domain than the server, e.g.,
61 ;; user@domain1.com, autodiscover.domain2.com.
63 ;; - Authentication is Kerberos/GSSAPI.
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
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.
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.
80 ;; Alex Luccisano <casual.lexicon@gmail.com> tested early versions of
81 ;; this library against a corporate installation of Exchange.
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.
87 ;; Nicolas Lamirault <nicolas.lamirault@gmail.com> tested the
88 ;; autodiscovery feature.
90 ;; Trey Jackson <bigfaceworm@gmail.com> confirmed autodiscovery worked
93 ;; Joakim Verona <joakim@verona.se> tested autodiscovery in a
94 ;; Kerberos/GSSAPI environment.
96 ;; Wilfred Hughes <me@wilfred.me.uk> tested on Exchange 2007 and
97 ;; suggested documentation improvements.
101 ;; Implementation-visible functions and variables.
103 ;; Add NTLM authorization scheme.
104 (require 'url-http-ntlm)
105 (require 'soap-client)
107 (require 'excorporate-calendar)
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.")
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.")
121 (defvar exco--connection-identifiers nil
122 "An ordered list of connection identifiers.")
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)))
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"))
132 (mm-insert-part mime-part)
134 (car (xml-parse-region (point-min) (point-max)))
136 (mm-destroy-part mime-part)))))
138 (defun exco--bind-wsdl (wsdl service-url port-name target-namespace
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
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)
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,
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")
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
182 (url-retrieve-synchronously url)
183 (when redirect (url-retrieve-synchronously redirect)))
184 (url-debug 'excorporate "Done 500 recovery attempt")
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)
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)))
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))
214 (let ((data-buffer (current-buffer)))
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)
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.
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"
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)
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))))
254 (define-state-machine exco--fsm :start
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
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
271 :mail-address identifier
272 ;; Error recovery data.
274 ;; Autodiscovery data.
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)
288 (cdr domain-parts) ".")))
289 exco--autodiscovery-templates))))
295 :next-state-after-success nil
300 ;; Go directly to :retrieving-service-xml, skipping autodiscovery.
301 (list :retrieving-service-xml
303 :identifier identifier
304 :mail-address (car identifier)
306 :autodiscovery-urls nil
307 ;; Use service-url field from identifier.
308 :service-url (cdr identifier)
311 :next-state-after-success nil
317 (define-state exco--fsm :retrieving-autodiscovery-xml
318 (fsm state-data event _callback)
321 (let ((urls (plist-get state-data :autodiscovery-urls)))
323 (let ((url (pop urls)))
324 (fsm-debug-output "exco--fsm will probe %s" url)
329 (let ((data-buffer (current-buffer)))
332 (url-debug 'excorporate
333 "Processing status: %s" status)
334 (if (eq (car status) :error)
337 (exco--handle-url-error url status)
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
345 (plist-put state-data :retrying t)
346 ;; Save the popped urls list so that the
347 ;; next url is attempted, and clear the
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.
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))))))
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))))
376 (plist-put state-data :next-state-after-success :retrieving-service-xml)
377 (list :parsing-service-wsdl state-data nil))))
379 (define-enter-state exco--fsm :shutting-down-on-error
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))
388 (define-state exco--fsm :shutting-down-on-error
389 (_fsm state-data _event _callback)
390 (list :shutting-down-on-error state-data nil))
392 (define-enter-state exco--fsm :retrieving-service-xml
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))
399 ;; Skip soap-parse-wsdl-phase-fetch-schema to avoid
400 ;; synchronous URL fetches.
401 (soap-parse-wsdl-phase-finish-parsing xml unbound-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"))))
411 (exco-extract-value '(Response
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.
426 (exco-extract-value '(Response
432 (message "Excorporate: %s" error-message)
433 (message "Excorporate: Failed to find service URL"))))
434 (fsm-send fsm :retrieve-xml)))
437 "AutodiscoverServicePort"
439 `((RequestedServerVersion . "Exchange2010")
443 (Mailbox . ,(plist-get state-data :mail-address))))
445 (Setting . "InternalEwsUrl")))))))
446 (list state-data nil))
448 (define-state exco--fsm :retrieving-service-xml
449 (fsm state-data event _callback)
451 (:unrecoverable-error
452 (list :shutting-down-on-error state-data nil))
454 (let ((service-url (plist-get state-data :service-url)))
455 (url-retrieve (concat service-url "?wsdl")
457 (let ((data-buffer (current-buffer)))
459 (if (eq (car status) :error)
461 (plist-put state-data :failure-message
462 (format "Failed to retrieve %s"
464 (fsm-send fsm :unrecoverable-error))
465 (plist-put state-data
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))
473 (plist-put state-data :next-state-after-success :retrieving-data)
474 (list :parsing-service-wsdl state-data nil))))
476 (define-enter-state exco--fsm :parsing-service-wsdl
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)))
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))))
496 (defun exco--get-server-version (wsdl)
497 "Extract server version from WSDL."
500 (soap-xs-type-attributes
501 (soap-xs-element-type
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")
511 (define-enter-state exco--fsm :retrieving-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
517 (soap-parse-wsdl-phase-finish-parsing (plist-get state-data :service-xml)
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))
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))
536 (arguments (pop event))
537 (callback (pop event)))
538 (apply #'soap-invoke-async
540 (funcall callback identifier response))
543 "ExchangeServicePort"
546 (list :retrieving-data state-data nil))
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")))
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
557 (declare (indent 1) (debug t))
559 (exco--ensure-connection)
560 (let ((fsm (gethash ,identifier exco--connections)))
562 (error "Excorporate: Connection %S does not exist" ,identifier))
565 ;; Developer-visible functions and variables.
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."
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"
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))
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)))
602 (defun exco-server-version (identifier)
603 "Return the server version for connection IDENTIFIER, as a string.
604 Examples are \"Exchange2010\", \"Exchange2010_SP1\",
606 (exco--with-fsm identifier
607 (plist-get (fsm-get-state-data fsm) :server-version)))
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))
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)))
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
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
646 (end (cdr (assoc 'End calendar-item)))
647 (end-internal (apply #'encode-time
648 (soap-decode-date-time
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
656 (split-string cc-invitees ";")))))
657 (push (funcall callback subject start-internal end-internal
658 location main-invitees optional-invitees)
660 (nreverse result-list)))
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
670 (substring date-time-string 0 22) ":" (substring date-time-string 22)))
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)))
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))))))
698 ((Traversal . "Shallow")
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))
707 (DistinguishedFolderId (Id . "calendar"))))
709 ,@(let ((server-major-version
711 (substring (exco-server-version identifier) 8 12))))
713 ((<= server-major-version 2007)
715 ((< server-major-version 2013)
716 '(nil nil nil nil nil))
718 '(nil nil nil nil nil nil)))))
721 (defun exco-connection-iterate (initialize-function
722 per-connection-function
723 per-connection-callback
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)
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)))))))
742 ;; User-visible functions and variables.
743 (defgroup excorporate nil
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."
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)"))))
765 (defun 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."
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))
782 (error "Excorporate: Invalid configuration"))))
784 (provide 'excorporate)
786 ;;; excorporate.el ends here