aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_calendar_ews.c
blob: 51761127eb626265d8b4c0eedaf6c5716a6a920f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2008 - 2009, Digium, Inc.
 *
 * Jan Kalab <pitlicek@gmail.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 * \brief Resource for handling MS Exchange Web Service calendars
 */

/*** MODULEINFO
	<depend>neon29</depend>
	<support_level>core</support_level>
***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <ne_request.h>
#include <ne_session.h>
#include <ne_uri.h>
#include <ne_socket.h>
#include <ne_auth.h>
#include <ne_xml.h>
#include <ne_xmlreq.h>
#include <ne_utils.h>
#include <ne_redirect.h>

#include "asterisk/module.h"
#include "asterisk/calendar.h"
#include "asterisk/lock.h"
#include "asterisk/config.h"
#include "asterisk/astobj2.h"

static void *ewscal_load_calendar(void *data);
static void *unref_ewscal(void *obj);
static int ewscal_write_event(struct ast_calendar_event *event);

static struct ast_calendar_tech ewscal_tech = {
	.type = "ews",
	.description = "MS Exchange Web Service calendars",
	.module = AST_MODULE,
	.load_calendar = ewscal_load_calendar,
	.unref_calendar = unref_ewscal,
	.write_event = ewscal_write_event,
};

enum xml_op {
	XML_OP_FIND = 100,
	XML_OP_GET,
	XML_OP_CREATE,
};

struct calendar_id {
	struct ast_str *id;
	AST_LIST_ENTRY(calendar_id) next;
};

struct xml_context {
	ne_xml_parser *parser;
	struct ast_str *cdata;
	struct ast_calendar_event *event;
	enum xml_op op;
	struct ewscal_pvt *pvt;
	AST_LIST_HEAD_NOLOCK(ids, calendar_id) ids;
};

/* Important states of XML parsing */
enum {
	XML_EVENT_NAME = 10,
	XML_EVENT_START,
	XML_EVENT_END,
	XML_EVENT_BUSY,
	XML_EVENT_ORGANIZER,
	XML_EVENT_LOCATION,
	XML_EVENT_ATTENDEE_LIST,
	XML_EVENT_ATTENDEE,
	XML_EVENT_MAILBOX,
	XML_EVENT_EMAIL_ADDRESS,
	XML_EVENT_CATEGORIES,
	XML_EVENT_CATEGORY,
	XML_EVENT_IMPORTANCE,
};

struct ewscal_pvt {
	AST_DECLARE_STRING_FIELDS(
		AST_STRING_FIELD(url);
		AST_STRING_FIELD(user);
		AST_STRING_FIELD(secret);
	);
	struct ast_calendar *owner;
	ne_uri uri;
	ne_session *session;
	struct ao2_container *events;
	unsigned int items;
};

static void ewscal_destructor(void *obj)
{
	struct ewscal_pvt *pvt = obj;

	ast_debug(1, "Destroying pvt for Exchange Web Service calendar %s\n", "pvt->owner->name");
	if (pvt->session) {
		ne_session_destroy(pvt->session);
	}
	ast_string_field_free_memory(pvt);

	ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);

	ao2_ref(pvt->events, -1);
}

static void *unref_ewscal(void *obj)
{
	struct ewscal_pvt *pvt = obj;

	ast_debug(5, "EWS: unref_ewscal()\n");
	ao2_ref(pvt, -1);
	return NULL;
}

static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
{
	struct ewscal_pvt *pvt = userdata;

	if (attempts > 1) {
		ast_log(LOG_WARNING, "Invalid username or password for Exchange Web Service calendar '%s'\n", pvt->owner->name);
		return -1;
	}

	ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
	ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);

	return 0;
}

static int ssl_verify(void *userdata, int failures, const ne_ssl_certificate *cert)
{
	struct ewscal_pvt *pvt = userdata;
	if (failures & NE_SSL_UNTRUSTED) {
		ast_log(LOG_WARNING, "Untrusted SSL certificate for calendar %s!\n", pvt->owner->name);
		return 0;
	}
	return 1;	/* NE_SSL_NOTYETVALID, NE_SSL_EXPIRED, NE_SSL_IDMISMATCH */
}

static time_t mstime_to_time_t(char *mstime)
{
	struct ast_tm tm;
	struct timeval tv;

	if (ast_strptime(mstime, "%FT%TZ", &tm)) {
		tv = ast_mktime(&tm, "UTC");
		return tv.tv_sec;
	}
	return 0;
}

static int startelm(void *userdata, int parent, const char *nspace, const char *name, const char **atts)
{
	struct xml_context *ctx = userdata;

	ast_debug(5, "EWS: XML: Start: %s\n", name);
	if (ctx->op == XML_OP_CREATE) {
		return NE_XML_DECLINE;
	}

	/* Nodes needed for traversing until CalendarItem is found */
	if (!strcmp(name, "Envelope") ||
		!strcmp(name, "Body") ||
		!strcmp(name, "FindItemResponse") ||
		!strcmp(name, "GetItemResponse") ||
		!strcmp(name, "CreateItemResponse") ||
		!strcmp(name, "ResponseMessages") ||
		!strcmp(name, "FindItemResponseMessage") || !strcmp(name, "GetItemResponseMessage") ||
		!strcmp(name, "Items")
	) {
		return 1;
	} else if (!strcmp(name, "RootFolder")) {
		/* Get number of events */
		unsigned int items;

		ast_debug(3, "EWS: XML: <RootFolder>\n");
		if (sscanf(ne_xml_get_attr(ctx->parser, atts, NULL, "TotalItemsInView"), "%u", &items) != 1) {
			/* Couldn't read enything */
			ne_xml_set_error(ctx->parser, "Could't read number of events.");
			return NE_XML_ABORT;
		}

		ast_debug(3, "EWS: %u calendar items to load\n", items);
		ctx->pvt->items = items;
		if (items < 1) {
			/* Stop processing XML if there are no events */
			ast_calendar_merge_events(ctx->pvt->owner, ctx->pvt->events);
			return NE_XML_DECLINE;
		}
		return 1;
	} else if (!strcmp(name, "CalendarItem")) {
		/* Event start */
		ast_debug(3, "EWS: XML: <CalendarItem>\n");
		if (!(ctx->pvt && ctx->pvt->owner)) {
			ast_log(LOG_ERROR, "Require a private structure with an owner\n");
			return NE_XML_ABORT;
		}

		ctx->event = ast_calendar_event_alloc(ctx->pvt->owner);
		if (!ctx->event) {
			ast_log(LOG_ERROR, "Could not allocate an event!\n");
			return NE_XML_ABORT;
		}

		ctx->cdata = ast_str_create(64);
		if (!ctx->cdata) {
			ast_log(LOG_ERROR, "Could not allocate CDATA!\n");
			return NE_XML_ABORT;
		}

		return 1;
	} else if (!strcmp(name, "ItemId")) {
		/* Event UID */
		if (ctx->op == XML_OP_FIND) {
			struct calendar_id *id;
			if (!(id = ast_calloc(1, sizeof(id)))) {
				return NE_XML_ABORT;
			}
			if (!(id->id = ast_str_create(256))) {
				ast_free(id);
				return NE_XML_ABORT;
			}
			ast_str_set(&id->id, 0, "%s", ne_xml_get_attr(ctx->parser, atts, NULL, "Id"));
			AST_LIST_INSERT_TAIL(&ctx->ids, id, next);
			ast_debug(3, "EWS_FIND: XML: UID: %s\n", ast_str_buffer(id->id));
		} else {
			ast_debug(3, "EWS_GET: XML: UID: %s\n", ne_xml_get_attr(ctx->parser, atts, NULL, "Id"));
			ast_string_field_set(ctx->event, uid, ne_xml_get_attr(ctx->parser, atts, NULL, "Id"));
		}
		return XML_EVENT_NAME;
	} else if (!strcmp(name, "Subject")) {
		/* Event name */
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_NAME;
	} else if (!strcmp(name, "Start")) {
		/* Event start time */
		return XML_EVENT_START;
	} else if (!strcmp(name, "End")) {
		/* Event end time */
		return XML_EVENT_END;
	} else if (!strcmp(name, "LegacyFreeBusyStatus")) {
		/* Event busy state */
		return XML_EVENT_BUSY;
	} else if (!strcmp(name, "Organizer") ||
			(parent == XML_EVENT_ORGANIZER && (!strcmp(name, "Mailbox") ||
			!strcmp(name, "Name")))) {
		/* Event organizer */
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_ORGANIZER;
	} else if (!strcmp(name, "Location")) {
		/* Event location */
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_LOCATION;
	} else if (!strcmp(name, "Categories")) {
		/* Event categories */
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_CATEGORIES;
	} else if (parent == XML_EVENT_CATEGORIES && !strcmp(name, "String")) {
		/* Event category */
		return XML_EVENT_CATEGORY;
	} else if (!strcmp(name, "Importance")) {
		/* Event importance (priority) */
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_IMPORTANCE;
	} else if (!strcmp(name, "RequiredAttendees") || !strcmp(name, "OptionalAttendees")) {
		return XML_EVENT_ATTENDEE_LIST;
	} else if (!strcmp(name, "Attendee") && parent == XML_EVENT_ATTENDEE_LIST) {
		return XML_EVENT_ATTENDEE;
	} else if (!strcmp(name, "Mailbox") && parent == XML_EVENT_ATTENDEE) {
		return XML_EVENT_MAILBOX;
	} else if (!strcmp(name, "EmailAddress") && parent == XML_EVENT_MAILBOX) {
		if (!ctx->cdata) {
			return NE_XML_ABORT;
		}
		ast_str_reset(ctx->cdata);
		return XML_EVENT_EMAIL_ADDRESS;
	}

	return NE_XML_DECLINE;
}

static int cdata(void *userdata, int state, const char *cdata, size_t len)
{
	struct xml_context *ctx = userdata;
	char data[len + 1];

	/* !!! DON'T USE AST_STRING_FIELD FUNCTIONS HERE, JUST COLLECT CTX->CDATA !!! */
	if (state < XML_EVENT_NAME || ctx->op == XML_OP_CREATE) {
		return 0;
	}

	if (!ctx->event) {
		ast_log(LOG_ERROR, "Parsing event data, but event object does not exist!\n");
		return 1;
	}

	if (!ctx->cdata) {
		ast_log(LOG_ERROR, "String for storing CDATA is unitialized!\n");
		return 1;
	}

	ast_copy_string(data, cdata, len + 1);

	switch (state) {
	case XML_EVENT_START:
		ctx->event->start = mstime_to_time_t(data);
		break;
	case XML_EVENT_END:
		ctx->event->end = mstime_to_time_t(data);
		break;
	case XML_EVENT_BUSY:
		if (!strcmp(data, "Busy") || !strcmp(data, "OOF")) {
			ast_debug(3, "EWS: XML: Busy: yes\n");
			ctx->event->busy_state = AST_CALENDAR_BS_BUSY;
		}
		else if (!strcmp(data, "Tentative")) {
			ast_debug(3, "EWS: XML: Busy: tentative\n");
			ctx->event->busy_state = AST_CALENDAR_BS_BUSY_TENTATIVE;
		}
		else {
			ast_debug(3, "EWS: XML: Busy: no\n");
			ctx->event->busy_state = AST_CALENDAR_BS_FREE;
		}
		break;
	case XML_EVENT_CATEGORY:
		if (ast_str_strlen(ctx->cdata) == 0) {
			ast_str_set(&ctx->cdata, 0, "%s", data);
		} else {
			ast_str_append(&ctx->cdata, 0, ",%s", data);
		}
		break;
	default:
		ast_str_append(&ctx->cdata, 0, "%s", data);
	}

	ast_debug(5, "EWS: XML: CDATA: %s\n", ast_str_buffer(ctx->cdata));

	return 0;
}

static int endelm(void *userdata, int state, const char *nspace, const char *name)
{
	struct xml_context *ctx = userdata;

	ast_debug(5, "EWS: XML: End:   %s\n", name);
	if (ctx->op == XML_OP_FIND || ctx->op == XML_OP_CREATE) {
		return NE_XML_DECLINE;
	}

	if (!strcmp(name, "Subject")) {
		/* Event name end*/
		ast_string_field_set(ctx->event, summary, ast_str_buffer(ctx->cdata));
		ast_debug(3, "EWS: XML: Summary: %s\n", ctx->event->summary);
		ast_str_reset(ctx->cdata);
	} else if (!strcmp(name, "Organizer")) {
		/* Event organizer end */
		ast_string_field_set(ctx->event, organizer, ast_str_buffer(ctx->cdata));
		ast_debug(3, "EWS: XML: Organizer: %s\n", ctx->event->organizer);
		ast_str_reset(ctx->cdata);
	} else if (!strcmp(name, "Location")) {
		/* Event location end */
		ast_string_field_set(ctx->event, location, ast_str_buffer(ctx->cdata));
		ast_debug(3, "EWS: XML: Location: %s\n", ctx->event->location);
		ast_str_reset(ctx->cdata);
	} else if (!strcmp(name, "Categories")) {
		/* Event categories end */
		ast_string_field_set(ctx->event, categories, ast_str_buffer(ctx->cdata));
		ast_debug(3, "EWS: XML: Categories: %s\n", ctx->event->categories);
		ast_str_reset(ctx->cdata);
	} else if (!strcmp(name, "Importance")) {
		/* Event importance end */
		if (!strcmp(ast_str_buffer(ctx->cdata), "Low")) {
			ctx->event->priority = 9;
		} else if (!strcmp(ast_str_buffer(ctx->cdata), "Normal")) {
			ctx->event->priority = 5;
		} else if (!strcmp(ast_str_buffer(ctx->cdata), "High")) {
			ctx->event->priority = 1;
		}
		ast_debug(3, "EWS: XML: Importance: %s (%d)\n", ast_str_buffer(ctx->cdata), ctx->event->priority);
		ast_str_reset(ctx->cdata);
	} else if (state == XML_EVENT_EMAIL_ADDRESS) {
		struct ast_calendar_attendee *attendee;

		if (!(attendee = ast_calloc(1, sizeof(*attendee)))) {
			ctx->event = ast_calendar_unref_event(ctx->event);
			return  1;
		}

		if (ast_str_strlen(ctx->cdata)) {
			attendee->data = ast_strdup(ast_str_buffer(ctx->cdata));
			AST_LIST_INSERT_TAIL(&ctx->event->attendees, attendee, next);
		}
		ast_debug(3, "EWS: XML: attendee address '%s'\n", ast_str_buffer(ctx->cdata));
		ast_str_reset(ctx->cdata);
	} else if (!strcmp(name, "CalendarItem")) {
		/* Event end */
		ast_debug(3, "EWS: XML: </CalendarItem>\n");
		ast_free(ctx->cdata);
		if (ctx->event) {
			ao2_link(ctx->pvt->events, ctx->event);
			ctx->event = ast_calendar_unref_event(ctx->event);
		} else {
			ast_log(LOG_ERROR, "Event data ended in XML, but event object does not exist!\n");
			return 1;
		}
	} else if (!strcmp(name, "Envelope")) {
		/* Events end */
		ast_debug(3, "EWS: XML: %d of %d event(s) has been parsed…\n", ao2_container_count(ctx->pvt->events), ctx->pvt->items);
		if (ao2_container_count(ctx->pvt->events) >= ctx->pvt->items) {
			ast_debug(3, "EWS: XML: All events has been parsed, merging…\n");
			ast_calendar_merge_events(ctx->pvt->owner, ctx->pvt->events);
		}
	}

	return 0;
}

static const char *mstime(time_t t, char *buf, size_t buflen)
{
	struct timeval tv = {
		.tv_sec = t,
	};
	struct ast_tm tm;

	ast_localtime(&tv, &tm, "utc");
	ast_strftime(buf, buflen, "%FT%TZ", &tm);

	return S_OR(buf, "");
}

static const char *msstatus(enum ast_calendar_busy_state state)
{
	switch (state) {
	case AST_CALENDAR_BS_BUSY_TENTATIVE:
		return "Tentative";
	case AST_CALENDAR_BS_BUSY:
		return "Busy";
	case AST_CALENDAR_BS_FREE:
		return "Free";
	default:
		return "";
	}
}

static const char *get_soap_action(enum xml_op op)
{
	switch (op) {
	case XML_OP_FIND:
		return "\"http://schemas.microsoft.com/exchange/services/2006/messages/FindItem\"";
	case XML_OP_GET:
		return "\"http://schemas.microsoft.com/exchange/services/2006/messages/GetItem\"";
	case XML_OP_CREATE:
		return "\"http://schemas.microsoft.com/exchange/services/2006/messages/CreateItem\"";
	}

	return "";
}

static int send_ews_request_and_parse(struct ast_str *request, struct xml_context *ctx)
{
	int ret;
	ne_request *req;
	ne_xml_parser *parser;

	ast_debug(3, "EWS: HTTP request...\n");
	if (!(ctx && ctx->pvt)) {
		ast_log(LOG_ERROR, "There is no private!\n");
		return -1;
	}

	if (!ast_str_strlen(request)) {
		ast_log(LOG_ERROR, "No request to send!\n");
		return -1;
	}

	ast_debug(3, "%s\n", ast_str_buffer(request));

	/* Prepare HTTP POST request */
	req = ne_request_create(ctx->pvt->session, "POST", ctx->pvt->uri.path);
	ne_set_request_flag(req, NE_REQFLAG_IDEMPOTENT, 0);

	/* Set headers--should be application/soap+xml, but MS… :/ */
	ne_add_request_header(req, "Content-Type", "text/xml; charset=utf-8");
	ne_add_request_header(req, "SOAPAction", get_soap_action(ctx->op));

	/* Set body to SOAP request */
	ne_set_request_body_buffer(req, ast_str_buffer(request), ast_str_strlen(request));

	/* Prepare XML parser */
	parser = ne_xml_create();
	ctx->parser = parser;
	ne_xml_push_handler(parser, startelm, cdata, endelm, ctx);	/* Callbacks */

	/* Dispatch request and parse response as XML */
	ret = ne_xml_dispatch_request(req, parser);
	if (ret != NE_OK) { /* Error handling */
		ast_log(LOG_WARNING, "Unable to communicate with Exchange Web Service at '%s': %s\n", ctx->pvt->url, ne_get_error(ctx->pvt->session));
		ne_request_destroy(req);
		ne_xml_destroy(parser);
		return -1;
	}

	/* Cleanup */
	ne_request_destroy(req);
	ne_xml_destroy(parser);

	return 0;
}

static int ewscal_write_event(struct ast_calendar_event *event)
{
	struct ast_str *request;
	struct ewscal_pvt *pvt = event->owner->tech_pvt;
	char start[21], end[21];
	struct xml_context ctx = {
		.op = XML_OP_CREATE,
		.pvt = pvt,
	};
	int ret;
	char *category, *categories;

	if (!pvt) {
		return -1;
	}

	if (!(request = ast_str_create(1024))) {
		return -1;
	}

	ast_str_set(&request, 0,
		"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
			"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
			"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
			"xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
			"<soap:Body>"
			"<CreateItem xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\" "
				"xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" "
				"SendMeetingInvitations=\"SendToNone\" >"
				"<SavedItemFolderId>"
					"<t:DistinguishedFolderId Id=\"calendar\"/>"
				"</SavedItemFolderId>"
				"<Items>"
					"<t:CalendarItem xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
						"<Subject>%s</Subject>"
						"<Body BodyType=\"Text\">%s</Body>"
						"<ReminderIsSet>false</ReminderIsSet>"
						"<Start>%s</Start>"
						"<End>%s</End>"
						"<IsAllDayEvent>false</IsAllDayEvent>"
						"<LegacyFreeBusyStatus>%s</LegacyFreeBusyStatus>"
						"<Location>%s</Location>",
		event->summary,
		event->description,
		mstime(event->start, start, sizeof(start)),
		mstime(event->end, end, sizeof(end)),
		msstatus(event->busy_state),
		event->location
	);
	/* Event priority */
	switch (event->priority) {
	case 1:
	case 2:
	case 3:
	case 4:
		ast_str_append(&request, 0, "<Importance>High</Importance>");
		break;
	case 5:
		ast_str_append(&request, 0, "<Importance>Normal</Importance>");
		break;
	case 6:
	case 7:
	case 8:
	case 9:
		ast_str_append(&request, 0, "<Importance>Low</Importance>");
		break;
	}
	/* Event categories*/
	if (strlen(event->categories) > 0) {
		ast_str_append(&request, 0, "<Categories>");
		categories = ast_strdupa(event->categories);	/* Duplicate string, since strsep() is destructive */
		category = strsep(&categories, ",");
		while (category != NULL) {
			ast_str_append(&request, 0, "<String>%s</String>", category);
			category = strsep(&categories, ",");
		}
		ast_str_append(&request, 0, "</Categories>");
	}
	/* Finish request */
	ast_str_append(&request, 0, "</t:CalendarItem></Items></CreateItem></soap:Body></soap:Envelope>");

	ret = send_ews_request_and_parse(request, &ctx);

	ast_free(request);

	return ret;
}

static struct calendar_id *get_ewscal_ids_for(struct ewscal_pvt *pvt)
{
	char start[21], end[21];
	struct ast_tm tm;
	struct timeval tv;
	struct ast_str *request;
	struct xml_context ctx = {
		.op = XML_OP_FIND,
		.pvt = pvt,
	};

	ast_debug(5, "EWS: get_ewscal_ids_for()\n");

	if (!pvt) {
		ast_log(LOG_ERROR, "There is no private!\n");
		return NULL;
	}

	/* Prepare timeframe strings */
	tv = ast_tvnow();
	ast_localtime(&tv, &tm, "UTC");
	ast_strftime(start, sizeof(start), "%FT%TZ", &tm);
	tv.tv_sec += 60 * pvt->owner->timeframe;
	ast_localtime(&tv, &tm, "UTC");
	ast_strftime(end, sizeof(end), "%FT%TZ", &tm);

	/* Prepare SOAP request */
	if (!(request = ast_str_create(512))) {
		return NULL;
	}

	ast_str_set(&request, 0,
		"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
		"xmlns:ns1=\"http://schemas.microsoft.com/exchange/services/2006/types\" "
		"xmlns:ns2=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"
			"<SOAP-ENV:Body>"
				"<ns2:FindItem Traversal=\"Shallow\">"
					"<ns2:ItemShape>"
						"<ns1:BaseShape>IdOnly</ns1:BaseShape>"
					"</ns2:ItemShape>"
					"<ns2:CalendarView StartDate=\"%s\" EndDate=\"%s\"/>"	/* Timeframe */
					"<ns2:ParentFolderIds>"
						"<ns1:DistinguishedFolderId Id=\"calendar\"/>"
					"</ns2:ParentFolderIds>"
				"</ns2:FindItem>"
			"</SOAP-ENV:Body>"
		"</SOAP-ENV:Envelope>",
		start, end	/* Timeframe */
	);

	AST_LIST_HEAD_INIT_NOLOCK(&ctx.ids);

	/* Dispatch request and parse response as XML */
	if (send_ews_request_and_parse(request, &ctx)) {
		ast_free(request);
		return NULL;
	}

	/* Cleanup */
	ast_free(request);

	return AST_LIST_FIRST(&ctx.ids);
}

static int parse_ewscal_id(struct ewscal_pvt *pvt, const char *id) {
	struct ast_str *request;
	struct xml_context ctx = {
		.pvt = pvt,
		.op = XML_OP_GET,
	};

	if (!(request = ast_str_create(512))) {
		return -1;
	}

	ast_str_set(&request, 0,
		"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
		"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
		"xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
		"<soap:Body>"
			"<GetItem xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"
				"<ItemShape>"
					"<t:BaseShape>AllProperties</t:BaseShape>"
				"</ItemShape>"
				"<ItemIds>"
					"<t:ItemId Id=\"%s\"/>"
				"</ItemIds>"
			"</GetItem>"
		"</soap:Body>"
		"</soap:Envelope>", id
	);

	if (send_ews_request_and_parse(request, &ctx)) {
		ast_free(request);
		return -1;
	}

	ast_free(request);

	return 0;
}

static int update_ewscal(struct ewscal_pvt *pvt)
{
	struct calendar_id *id_head;
	struct calendar_id *iter;

	if (!(id_head = get_ewscal_ids_for(pvt))) {
		return 0;
	}

	for (iter = id_head; iter; iter = AST_LIST_NEXT(iter, next)) {
		parse_ewscal_id(pvt, ast_str_buffer(iter->id));
		ast_free(iter->id);
		ast_free(iter);
	}

	return 0;
}

static void *ewscal_load_calendar(void *void_data)
{
	struct ewscal_pvt *pvt;
	const struct ast_config *cfg;
	struct ast_variable *v;
	struct ast_calendar *cal = void_data;
	ast_mutex_t refreshlock;

	ast_debug(5, "EWS: ewscal_load_calendar()\n");

	if (!(cal && (cfg = ast_calendar_config_acquire()))) {
		ast_log(LOG_ERROR, "You must enable calendar support for res_ewscal to load\n");
		return NULL;
	}

	if (ao2_trylock(cal)) {
		if (cal->unloading) {
			ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
		} else {
			ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
		}
		ast_calendar_config_release();
		return NULL;
	}

	if (!(pvt = ao2_alloc(sizeof(*pvt), ewscal_destructor))) {
		ast_log(LOG_ERROR, "Could not allocate ewscal_pvt structure for calendar: %s\n", cal->name);
		ast_calendar_config_release();
		return NULL;
	}

	pvt->owner = cal;

	if (!(pvt->events = ast_calendar_event_container_alloc())) {
		ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
		pvt = unref_ewscal(pvt);
		ao2_unlock(cal);
		ast_calendar_config_release();
		return NULL;
	}

	if (ast_string_field_init(pvt, 32)) {
		ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
		pvt = unref_ewscal(pvt);
		ao2_unlock(cal);
		ast_calendar_config_release();
		return NULL;
	}

	for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
		if (!strcasecmp(v->name, "url")) {
			ast_string_field_set(pvt, url, v->value);
		} else if (!strcasecmp(v->name, "user")) {
			ast_string_field_set(pvt, user, v->value);
		} else if (!strcasecmp(v->name, "secret")) {
			ast_string_field_set(pvt, secret, v->value);
		}
	}

	ast_calendar_config_release();

	if (ast_strlen_zero(pvt->url)) {
		ast_log(LOG_WARNING, "No URL was specified for Exchange Web Service calendar '%s' - skipping.\n", cal->name);
		pvt = unref_ewscal(pvt);
		ao2_unlock(cal);
		return NULL;
	}

	if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
		ast_log(LOG_WARNING, "Could not parse url '%s' for Exchange Web Service calendar '%s' - skipping.\n", pvt->url, cal->name);
		pvt = unref_ewscal(pvt);
		ao2_unlock(cal);
		return NULL;
	}

	if (pvt->uri.scheme == NULL) {
		pvt->uri.scheme = "http";
	}

	if (pvt->uri.port == 0) {
		pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
	}

	ast_debug(3, "ne_uri.scheme	= %s\n", pvt->uri.scheme);
	ast_debug(3, "ne_uri.host	= %s\n", pvt->uri.host);
	ast_debug(3, "ne_uri.port	= %u\n", pvt->uri.port);
	ast_debug(3, "ne_uri.path	= %s\n", pvt->uri.path);
	ast_debug(3, "user		= %s\n", pvt->user);
	ast_debug(3, "secret		= %s\n", pvt->secret);

	pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
	ne_redirect_register(pvt->session);
	ne_set_server_auth(pvt->session, auth_credentials, pvt);
	ne_set_useragent(pvt->session, "Asterisk");

	if (!strcasecmp(pvt->uri.scheme, "https")) {
		ne_ssl_trust_default_ca(pvt->session);
		ne_ssl_set_verify(pvt->session, ssl_verify, pvt);
	}

	cal->tech_pvt = pvt;

	ast_mutex_init(&refreshlock);

	/* Load it the first time */
	update_ewscal(pvt);

	ao2_unlock(cal);

	/* The only writing from another thread will be if unload is true */
	for (;;) {
		struct timeval tv = ast_tvnow();
		struct timespec ts = {0,};

		ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);

		ast_mutex_lock(&refreshlock);
		while (!pvt->owner->unloading) {
			if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
				break;
			}
		}
		ast_mutex_unlock(&refreshlock);

		if (pvt->owner->unloading) {
			ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
			return NULL;
		}

		ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);

		update_ewscal(pvt);
	}

	return NULL;
}

static int load_module(void)
{
	/* Actualy, 0.29.1 is required (because of NTLM authentication), but this
	 * function does not support matching patch version. */
	if (ne_version_match(0, 29)) {
		ast_log(LOG_ERROR, "Exchange Web Service calendar module require neon >= 0.29.1, but %s is installed.\n", ne_version_string());
		return AST_MODULE_LOAD_DECLINE;
	}

	if (ast_calendar_register(&ewscal_tech) && (ne_sock_init() == 0)) {
		return AST_MODULE_LOAD_DECLINE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	ne_sock_exit();
	ast_calendar_unregister(&ewscal_tech);

	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk MS Exchange Web Service Calendar Integration",
	.load = load_module,
	.unload = unload_module,
	.load_pri = AST_MODPRI_DEVSTATE_PLUGIN,
);