Skip to content

Instantly share code, notes, and snippets.

@xeijin
Created January 4, 2020 15:05
Show Gist options
  • Save xeijin/0acca2b22aee6ee25251d6cea05512c0 to your computer and use it in GitHub Desktop.
Save xeijin/0acca2b22aee6ee25251d6cea05512c0 to your computer and use it in GitHub Desktop.
(defun +propositum/pwsh-format-list-filter (proc output)
"transform the OUTPUT produced by powershell command PROC into an alist cons'd key-value pairs.
Note: the powershell command should its results through the Format-List cmdlet."
(let ;; split output on newlines to get individual entries
((entries (s-lines output))
(result))
;; map over each entry, splitting keys from values then transform resulting list into cons'd alist
(mapcar #'map-pairs
(dolist (e entries result)
(s-split-up-to " : " e 1)))))
(defcustom propositum-ews-dll-location nil
"(Windows) the full path to the EWS library: `Microsoft.Exchange.WebServices.dll'"
:type '(file :must-match t)
:group 'propositum)
(defcustom propositum-ews-config nil
"alist of email address and server (domain only) used to retrieve outlook data via Exchange Web Services (EWS)
this avoids the need to use EWS autodiscover, making data retrieval faster.
the first entry is always used as the default"
:type '(alist :tag "ews config"
:key-type (string :tag "email address")
:value-type (string :tag "ews server (domain only)"))
:group 'propositum)
(defun +propositum/get-ol-calendar-entries (start end &optional email)
"synchronously retrieves calendar entries between START and END for the mailbox EMAIL
START and END are date/timestamp strings (format is flexible as they will be parsed with powershell's [datetime]::Parse)"
(fset 'in-ews-cfg (lambda (e) (alist-get e propositum-ews-config)))
(let ((result)
(cmd-args (list)))
(pcase email
((pred in-ews-cfg)
(push (format "-EmailAddress %S -Server %S"
email
(alist-get email propositum-ews-config))
cmd-args))
(nil
(if propositum-ews-config
(let ((default-config (nth 0 propositum-ews-config)))
(push (format "-EmailAddress %S -Server %S"
(car default-config)
(cdr default-config)))
(user-error "an email address is required. Alternatively, M-x customize the variable 'propositum-ews-config'"))))
(_ ;; use EWS auto-discover if email is supplied but not in config
(push (format "-EmailAddress %S" email) cmd-args)))
(push (format "-StartDate ([datetime]::Parse(%S) -EndDate ([datetime]::Parse(%S)" start end) cmd-args)
(when propositum-ews-dll-location
(push (format "-WebServicesDLL %S" propositum-ews-dll-location)))
(push "-CalendarOnly -DestinationId \"EntryId\"" cmd-args)
(+propositum/pwsh-format-list-filter
nil
(xei/call-process "ews-get" cmd-args))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment