Merge "res_pjsip: Add XML documentation for "use_callerid_contact""
[asterisk/asterisk.git] / res / res_calendar_caldav.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008 - 2009, Digium, Inc.
5  *
6  * Terry Wilson <twilson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * \brief Resource for handling CalDAV calendars
21  */
22
23 /*** MODULEINFO
24         <depend>res_calendar</depend>
25         <depend>neon</depend>
26         <depend>ical</depend>
27         <depend>libxml2</depend>
28         <support_level>extended</support_level>
29 ***/
30
31 #include "asterisk.h"
32
33 #include <libical/ical.h>
34 #include <ne_session.h>
35 #include <ne_uri.h>
36 #include <ne_request.h>
37 #include <ne_auth.h>
38 #include <ne_redirect.h>
39 #include <libxml/xmlmemory.h>
40 #include <libxml/parser.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/calendar.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/config.h"
47 #include "asterisk/astobj2.h"
48
49 static void *caldav_load_calendar(void *data);
50 static void *unref_caldav(void *obj);
51 static int caldav_write_event(struct ast_calendar_event *event);
52
53 static struct ast_calendar_tech caldav_tech = {
54         .type = "caldav",
55         .description = "CalDAV calendars",
56         .module = AST_MODULE,
57         .load_calendar = caldav_load_calendar,
58         .unref_calendar = unref_caldav,
59         .write_event = caldav_write_event,
60 };
61
62 struct caldav_pvt {
63         AST_DECLARE_STRING_FIELDS(
64                 AST_STRING_FIELD(url);
65                 AST_STRING_FIELD(user);
66                 AST_STRING_FIELD(secret);
67         );
68         struct ast_calendar *owner;
69         ne_uri uri;
70         ne_session *session;
71         struct ao2_container *events;
72 };
73
74 static void caldav_destructor(void *obj)
75 {
76         struct caldav_pvt *pvt = obj;
77
78         ast_debug(1, "Destroying pvt for CalDAV calendar %s\n", pvt->owner->name);
79         if (pvt->session) {
80                 ne_session_destroy(pvt->session);
81         }
82         ne_uri_free(&pvt->uri);
83         ast_string_field_free_memory(pvt);
84
85         ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
86
87         ao2_ref(pvt->events, -1);
88 }
89
90 static void *unref_caldav(void *obj)
91 {
92         struct caldav_pvt *pvt = obj;
93
94         ao2_ref(pvt, -1);
95         return NULL;
96 }
97
98 static int fetch_response_reader(void *data, const char *block, size_t len)
99 {
100         struct ast_str **response = data;
101         unsigned char *tmp;
102
103         if (!(tmp = ast_malloc(len + 1))) {
104                 return -1;
105         }
106         memcpy(tmp, block, len);
107         tmp[len] = '\0';
108         ast_str_append(response, 0, "%s", tmp);
109         ast_free(tmp);
110
111         return 0;
112 }
113
114 static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
115 {
116         struct caldav_pvt *pvt = userdata;
117
118         if (attempts > 1) {
119                 ast_log(LOG_WARNING, "Invalid username or password for CalDAV calendar '%s'\n", pvt->owner->name);
120                 return -1;
121         }
122
123         ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
124         ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
125
126         return 0;
127 }
128
129 static int debug_response_handler(void *userdata, ne_request *req, const ne_status *st)
130 {
131         if (st->code < 200 || st->code > 299) {
132                 ast_debug(1, "Unexpected response from server, %d: %s\n", st->code, st->reason_phrase);
133                 return 0;
134         }
135         return 1;
136 }
137
138 static struct ast_str *caldav_request(struct caldav_pvt *pvt, const char *method, struct ast_str *req_body, struct ast_str *subdir, const char *content_type)
139 {
140         struct ast_str *response;
141         ne_request *req;
142         int ret;
143         char buf[1000];
144
145         if (!pvt) {
146                 ast_log(LOG_ERROR, "There is no private!\n");
147                 return NULL;
148         }
149
150         if (!(response = ast_str_create(512))) {
151                 ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
152                 return NULL;
153         }
154
155         snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : "");
156
157         req = ne_request_create(pvt->session, method, buf);
158         ne_add_response_body_reader(req, debug_response_handler, fetch_response_reader, &response);
159         ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body));
160         ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type);
161         ne_add_request_header(req, "Depth", "1");
162
163         ret = ne_request_dispatch(req);
164         ne_request_destroy(req);
165
166         if (ret != NE_OK) {
167                 ast_log(LOG_WARNING, "Unknown response to CalDAV calendar %s, request %s to %s: %s\n", pvt->owner->name, method, buf, ne_get_error(pvt->session));
168                 ast_free(response);
169                 return NULL;
170         }
171
172         return response;
173 }
174
175 static int caldav_write_event(struct ast_calendar_event *event)
176 {
177         struct caldav_pvt *pvt;
178         struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
179         icalcomponent *calendar, *icalevent;
180         icaltimezone *utc = icaltimezone_get_utc_timezone();
181         int ret = -1;
182
183         if (!event) {
184                 ast_log(LOG_WARNING, "No event passed!\n");
185                 return -1;
186         }
187
188         if (!(event->start && event->end)) {
189                 ast_log(LOG_WARNING, "The event must contain a start and an end\n");
190                 return -1;
191         }
192         if (!(body = ast_str_create(512)) ||
193                 !(subdir = ast_str_create(32))) {
194                 ast_log(LOG_ERROR, "Could not allocate memory for request!\n");
195                 goto write_cleanup;
196         }
197
198         pvt = event->owner->tech_pvt;
199
200         if (ast_strlen_zero(event->uid)) {
201                 unsigned short val[8];
202                 int x;
203                 for (x = 0; x < 8; x++) {
204                         val[x] = ast_random();
205                 }
206                 ast_string_field_build(event, uid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
207                         (unsigned)val[0], (unsigned)val[1], (unsigned)val[2],
208                         (unsigned)val[3], (unsigned)val[4], (unsigned)val[5],
209                         (unsigned)val[6], (unsigned)val[7]);
210         }
211
212         calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
213         icalcomponent_add_property(calendar, icalproperty_new_version("2.0"));
214         icalcomponent_add_property(calendar, icalproperty_new_prodid("-//Digium, Inc.//res_caldav//EN"));
215
216         icalevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
217         icalcomponent_add_property(icalevent, icalproperty_new_dtstamp(icaltime_current_time_with_zone(utc)));
218         icalcomponent_add_property(icalevent, icalproperty_new_uid(event->uid));
219         icalcomponent_add_property(icalevent, icalproperty_new_dtstart(icaltime_from_timet_with_zone(event->start, 0, utc)));
220         icalcomponent_add_property(icalevent, icalproperty_new_dtend(icaltime_from_timet_with_zone(event->end, 0, utc)));
221         if (!ast_strlen_zero(event->organizer)) {
222                 icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->organizer));
223         }
224         if (!ast_strlen_zero(event->summary)) {
225                 icalcomponent_add_property(icalevent, icalproperty_new_summary(event->summary));
226         }
227         if (!ast_strlen_zero(event->description)) {
228                 icalcomponent_add_property(icalevent, icalproperty_new_description(event->description));
229         }
230         if (!ast_strlen_zero(event->location)) {
231                 icalcomponent_add_property(icalevent, icalproperty_new_location(event->location));
232         }
233         if (!ast_strlen_zero(event->categories)) {
234                 icalcomponent_add_property(icalevent, icalproperty_new_categories(event->categories));
235         }
236         if (event->priority > 0) {
237                 icalcomponent_add_property(icalevent, icalproperty_new_priority(event->priority));
238         }
239
240         switch (event->busy_state) {
241         case AST_CALENDAR_BS_BUSY:
242                 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));
243                 break;
244
245         case AST_CALENDAR_BS_BUSY_TENTATIVE:
246                 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE));
247                 break;
248
249         default:
250                 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE));
251         }
252
253         icalcomponent_add_component(calendar, icalevent);
254
255         ast_str_append(&body, 0, "%s", icalcomponent_as_ical_string(calendar));
256         ast_str_set(&subdir, 0, "%s%s.ics", pvt->url[strlen(pvt->url) - 1] == '/' ? "" : "/", event->uid);
257
258         if ((response = caldav_request(pvt, "PUT", body, subdir, "text/calendar"))) {
259                 ret = 0;
260         }
261
262 write_cleanup:
263         if (body) {
264                 ast_free(body);
265         }
266         if (response) {
267                 ast_free(response);
268         }
269         if (subdir) {
270                 ast_free(subdir);
271         }
272
273         return ret;
274 }
275
276 static struct ast_str *caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
277 {
278         struct ast_str *body, *response;
279         icaltimezone *utc = icaltimezone_get_utc_timezone();
280         icaltimetype start, end;
281         const char *start_str, *end_str;
282
283         if (!(body = ast_str_create(512))) {
284                 ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
285                 return NULL;
286         }
287
288         start = icaltime_from_timet_with_zone(start_time, 0, utc);
289         end = icaltime_from_timet_with_zone(end_time, 0, utc);
290         start_str = icaltime_as_ical_string(start);
291         end_str = icaltime_as_ical_string(end);
292
293         /* If I was really being efficient, I would store a collection of event URIs and etags,
294          * first doing a query of just the etag and seeing if anything had changed.  If it had,
295          * then I would do a request for each of the events that had changed, and only bother
296          * updating those.  Oh well. */
297         ast_str_append(&body, 0,
298                 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
299                 "<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n"
300                 "  <D:prop>\n"
301                 "    <C:calendar-data>\n"
302                 "      <C:expand start=\"%s\" end=\"%s\"/>\n"
303                 "    </C:calendar-data>\n"
304                 "  </D:prop>\n"
305                 "  <C:filter>\n"
306                 "    <C:comp-filter name=\"VCALENDAR\">\n"
307                 "      <C:comp-filter name=\"VEVENT\">\n"
308                 "        <C:time-range start=\"%s\" end=\"%s\"/>\n"
309                 "      </C:comp-filter>\n"
310                 "    </C:comp-filter>\n"
311                 "  </C:filter>\n"
312                 "</C:calendar-query>\n", start_str, end_str, start_str, end_str);
313
314         response = caldav_request(pvt, "REPORT", body, NULL, NULL);
315         ast_free(body);
316         if (response && !ast_str_strlen(response)) {
317                 ast_free(response);
318                 return NULL;
319         }
320
321         return response;
322 }
323
324 static time_t icalfloat_to_timet(icaltimetype time)
325 {
326         struct ast_tm tm = {0,};
327         struct timeval tv;
328
329         tm.tm_mday = time.day;
330         tm.tm_mon = time.month - 1;
331         tm.tm_year = time.year - 1900;
332         tm.tm_hour = time.hour;
333         tm.tm_min = time.minute;
334         tm.tm_sec = time.second;
335         tm.tm_isdst = -1;
336         tv = ast_mktime(&tm, NULL);
337
338         return tv.tv_sec;
339 }
340
341 /* span->start & span->end may be dates or floating times which have no timezone,
342  * which would mean that they should apply to the local timezone for all recepients.
343  * For example, if a meeting was set for 1PM-2PM floating time, people in different time
344  * zones would not be scheduled at the same local times.  Dates are often treated as
345  * floating times, so all day events will need to be converted--so we can trust the
346  * span here, and instead will grab the start and end from the component, which will
347  * allow us to test for floating times or dates.
348  */
349 static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)
350 {
351         struct caldav_pvt *pvt = data;
352         struct ast_calendar_event *event;
353         icaltimezone *utc = icaltimezone_get_utc_timezone();
354         icaltimetype start, end, tmp;
355         icalcomponent *valarm;
356         icalproperty *prop;
357         struct icaltriggertype trigger;
358
359         if (!(pvt && pvt->owner)) {
360                 ast_log(LOG_ERROR, "Require a private structure with an owner\n");
361                 return;
362         }
363
364         if (!(event = ast_calendar_event_alloc(pvt->owner))) {
365                 ast_log(LOG_ERROR, "Could not allocate an event!\n");
366                 return;
367         }
368
369         start = icalcomponent_get_dtstart(comp);
370         end = icalcomponent_get_dtend(comp);
371
372         event->start = icaltime_get_tzid(start) ? span->start : icalfloat_to_timet(start);
373         event->end = icaltime_get_tzid(end) ? span->end : icalfloat_to_timet(end);
374         event->busy_state = span->is_busy ? AST_CALENDAR_BS_BUSY : AST_CALENDAR_BS_FREE;
375
376         if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) {
377                 ast_string_field_set(event, summary, icalproperty_get_value_as_string(prop));
378         }
379
380         if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) {
381                 ast_string_field_set(event, description, icalproperty_get_value_as_string(prop));
382         }
383
384         if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) {
385                 ast_string_field_set(event, organizer, icalproperty_get_value_as_string(prop));
386         }
387
388         if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) {
389                 ast_string_field_set(event, location, icalproperty_get_value_as_string(prop));
390         }
391
392         if ((prop = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY))) {
393                 ast_string_field_set(event, categories, icalproperty_get_value_as_string(prop));
394         }
395
396         if ((prop = icalcomponent_get_first_property(comp, ICAL_PRIORITY_PROPERTY))) {
397                 event->priority = icalvalue_get_integer(icalproperty_get_value(prop));
398         }
399
400         if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) {
401                 ast_string_field_set(event, uid, icalproperty_get_value_as_string(prop));
402         } else {
403                 ast_log(LOG_WARNING, "No UID found, but one is required. Generating, but updates may not be acurate\n");
404                 if (!ast_strlen_zero(event->summary)) {
405                         ast_string_field_set(event, uid, event->summary);
406                 } else {
407                         char tmp[100];
408                         snprintf(tmp, sizeof(tmp), "%ld", event->start);
409                         ast_string_field_set(event, uid, tmp);
410                 }
411         }
412
413         /* Get the attendees */
414         for (prop = icalcomponent_get_first_property(comp, ICAL_ATTENDEE_PROPERTY);
415                         prop; prop = icalcomponent_get_next_property(comp, ICAL_ATTENDEE_PROPERTY)) {
416                 struct ast_calendar_attendee *attendee;
417                 const char *data;
418
419                 if (!(attendee = ast_calloc(1, sizeof(*attendee)))) {
420                         event = ast_calendar_unref_event(event);
421                         return;
422                 }
423                 data = icalproperty_get_attendee(prop);
424                 if (ast_strlen_zero(data)) {
425                         ast_free(attendee);
426                         continue;
427                 }
428                 attendee->data = ast_strdup(data);
429                 AST_LIST_INSERT_TAIL(&event->attendees, attendee, next);
430         }
431
432
433         /* Only set values for alarm based on VALARM.  Can be overriden in main/calendar.c by autoreminder
434          * therefore, go ahead and add events even if their is no VALARM or it is malformed
435          * Currently we are only getting the first VALARM and are handling repitition in main/calendar.c from calendar.conf */
436         if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
437                 ao2_link(pvt->events, event);
438                 event = ast_calendar_unref_event(event);
439                 return;
440         }
441
442         if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) {
443                 ast_log(LOG_WARNING, "VALARM has no TRIGGER, skipping!\n");
444                 ao2_link(pvt->events, event);
445                 event = ast_calendar_unref_event(event);
446                 return;
447         }
448
449         trigger = icalproperty_get_trigger(prop);
450
451         if (icaltriggertype_is_null_trigger(trigger)) {
452                 ast_log(LOG_WARNING, "Bad TRIGGER for VALARM, skipping!\n");
453                 ao2_link(pvt->events, event);
454                 event = ast_calendar_unref_event(event);
455                 return;
456         }
457
458         if (!icaltime_is_null_time(trigger.time)) { /* This is an absolute time */
459                 tmp = icaltime_convert_to_zone(trigger.time, utc);
460                 event->alarm = icaltime_as_timet_with_zone(tmp, utc);
461         } else { /* Offset from either dtstart or dtend */
462                 /* XXX Technically you can check RELATED to see if the event fires from the END of the event
463                  * But, I'm not sure I've ever seen anyone implement it in calendaring software, so I'm ignoring for now */
464                 tmp = icaltime_add(start, trigger.duration);
465                 event->alarm = icaltime_as_timet_with_zone(tmp, icaltime_get_timezone(start));
466         }
467
468         ao2_link(pvt->events, event);
469         event = ast_calendar_unref_event(event);
470
471         return;
472 }
473
474 struct xmlstate {
475         int in_caldata;
476         struct caldav_pvt *pvt;
477         struct ast_str *cdata;
478         time_t start;
479         time_t end;
480 };
481
482 static const xmlChar *caldav_node_localname = BAD_CAST "calendar-data";
483 static const xmlChar *caldav_node_nsuri     = BAD_CAST "urn:ietf:params:xml:ns:caldav";
484
485 static void handle_start_element(void *data,
486                                                                  const xmlChar *localname, const xmlChar *prefix, const xmlChar *uri,
487                                                                  int nb_namespaces, const xmlChar **namespaces,
488                                                                  int nb_attributes, int nb_defaulted, const xmlChar **attributes)
489 {
490         struct xmlstate *state = data;
491
492         if (xmlStrcmp(localname, caldav_node_localname) || xmlStrcmp(uri, caldav_node_nsuri)) {
493                 return;
494         }
495
496         state->in_caldata = 1;
497         ast_str_reset(state->cdata);
498 }
499
500 static void handle_end_element(void *data,
501                                                            const xmlChar *localname, const xmlChar *prefix, const xmlChar *uri)
502 {
503         struct xmlstate *state = data;
504         struct icaltimetype start, end;
505         icaltimezone *utc = icaltimezone_get_utc_timezone();
506         icalcomponent *iter;
507         icalcomponent *comp;
508
509         if (xmlStrcmp(localname, caldav_node_localname) || xmlStrcmp(uri, caldav_node_nsuri)) {
510                 return;
511         }
512
513         state->in_caldata = 0;
514         if (!(state->cdata && ast_str_strlen(state->cdata))) {
515                 return;
516         }
517         /* XXX Parse the calendar blurb for recurrence events in the time range,
518          * create an event, and add it to pvt->events */
519         start = icaltime_from_timet_with_zone(state->start, 0, utc);
520         end = icaltime_from_timet_with_zone(state->end, 0, utc);
521         comp = icalparser_parse_string(ast_str_buffer(state->cdata));
522
523         for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT);
524              iter;
525              iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT))
526         {
527                 icalcomponent_foreach_recurrence(iter, start, end, caldav_add_event, state->pvt);
528         }
529
530         icalcomponent_free(comp);
531 }
532
533 static void handle_characters(void *data, const xmlChar *ch, int len)
534 {
535         struct xmlstate *state = data;
536         xmlChar *tmp;
537
538         if (!state->in_caldata) {
539                 return;
540         }
541
542         tmp = xmlStrndup(ch, len);
543         ast_str_append(&state->cdata, 0, "%s", (char *)tmp);
544         xmlFree(tmp);
545 }
546
547 static int update_caldav(struct caldav_pvt *pvt)
548 {
549         struct timeval now = ast_tvnow();
550         time_t start, end;
551         struct ast_str *response;
552         xmlSAXHandler saxHandler;
553         struct xmlstate state = {
554                 .in_caldata = 0,
555                 .pvt = pvt
556         };
557
558         start = now.tv_sec;
559         end = now.tv_sec + 60 * pvt->owner->timeframe;
560         if (!(response = caldav_get_events_between(pvt, start, end))) {
561                 return -1;
562         }
563
564         if (!(state.cdata = ast_str_create(512))) {
565                 ast_free(response);
566                 return -1;
567         }
568
569         state.start = start;
570         state.end = end;
571
572         /*
573          * We want SAX2, so you assume that we want to call xmlSAXVersion() here, and
574          * that certainly seems like the right thing to do, but the default SAX
575          * handling functions assume that the 'data' pointer is going to be a
576          * xmlParserCtxtPtr, not a user data pointer, so we have to make sure that we
577          * are only calling the handlers that we control.
578          *
579          * So instead we hack things up a bit, clearing the struct and then assigning
580          * the magic number manually.
581          *
582          * There may be a cleaner way to do this, but frankly the libxml2 docs are
583          * pretty sparse.
584          */
585         memset(&saxHandler, 0, sizeof(saxHandler));
586         saxHandler.initialized = XML_SAX2_MAGIC;
587         saxHandler.startElementNs = handle_start_element;
588         saxHandler.endElementNs = handle_end_element;
589         saxHandler.characters = handle_characters;
590
591         xmlSAXUserParseMemory(&saxHandler, &state, ast_str_buffer(response), ast_str_strlen(response));
592
593         ast_calendar_merge_events(pvt->owner, pvt->events);
594
595         ast_free(response);
596         ast_free(state.cdata);
597
598         return 0;
599 }
600
601 static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
602 {
603         /* Verify all certs */
604         return 0;
605 }
606
607 static void *caldav_load_calendar(void *void_data)
608 {
609         struct caldav_pvt *pvt;
610         const struct ast_config *cfg;
611         struct ast_variable *v;
612         struct ast_calendar *cal = void_data;
613         ast_mutex_t refreshlock;
614
615         if (!(cal && (cfg = ast_calendar_config_acquire()))) {
616                 ast_log(LOG_ERROR, "You must enable calendar support for res_caldav to load\n");
617                 return NULL;
618         }
619
620         if (ao2_trylock(cal)) {
621                 if (cal->unloading) {
622                         ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
623                 } else {
624                         ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
625                 }
626                 ast_calendar_config_release();
627                 return NULL;
628         }
629
630         if (!(pvt = ao2_alloc(sizeof(*pvt), caldav_destructor))) {
631                 ast_log(LOG_ERROR, "Could not allocate caldav_pvt structure for calendar: %s\n", cal->name);
632                 ast_calendar_config_release();
633                 return NULL;
634         }
635
636         pvt->owner = cal;
637
638         if (!(pvt->events = ast_calendar_event_container_alloc())) {
639                 ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
640                 pvt = unref_caldav(pvt);
641                 ao2_unlock(cal);
642                 ast_calendar_config_release();
643                 return NULL;
644         }
645
646         if (ast_string_field_init(pvt, 32)) {
647                 ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
648                 pvt = unref_caldav(pvt);
649                 ao2_unlock(cal);
650                 ast_calendar_config_release();
651                 return NULL;
652         }
653
654         for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
655                 if (!strcasecmp(v->name, "url")) {
656                         ast_string_field_set(pvt, url, v->value);
657                 } else if (!strcasecmp(v->name, "user")) {
658                         ast_string_field_set(pvt, user, v->value);
659                 } else if (!strcasecmp(v->name, "secret")) {
660                         ast_string_field_set(pvt, secret, v->value);
661                 }
662         }
663
664         ast_calendar_config_release();
665
666         if (ast_strlen_zero(pvt->url)) {
667                 ast_log(LOG_WARNING, "No URL was specified for CalDAV calendar '%s' - skipping.\n", cal->name);
668                 pvt = unref_caldav(pvt);
669                 ao2_unlock(cal);
670                 return NULL;
671         }
672
673         if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
674                 ast_log(LOG_WARNING, "Could not parse url '%s' for CalDAV calendar '%s' - skipping.\n", pvt->url, cal->name);
675                 pvt = unref_caldav(pvt);
676                 ao2_unlock(cal);
677                 return NULL;
678         }
679
680         if (pvt->uri.scheme == NULL) {
681                 pvt->uri.scheme = "http";
682         }
683
684         if (pvt->uri.port == 0) {
685                 pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
686         }
687
688         pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
689         ne_redirect_register(pvt->session);
690         ne_set_server_auth(pvt->session, auth_credentials, pvt);
691         if (!strcasecmp(pvt->uri.scheme, "https")) {
692                 ne_ssl_trust_default_ca(pvt->session);
693                 ne_ssl_set_verify(pvt->session, verify_cert, NULL);
694         }
695
696         cal->tech_pvt = pvt;
697
698         ast_mutex_init(&refreshlock);
699
700         /* Load it the first time */
701         update_caldav(pvt);
702
703         ao2_unlock(cal);
704
705         /* The only writing from another thread will be if unload is true */
706         for (;;) {
707                 struct timeval tv = ast_tvnow();
708                 struct timespec ts = {0,};
709
710                 ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);
711
712                 ast_mutex_lock(&refreshlock);
713                 while (!pvt->owner->unloading) {
714                         if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
715                                 break;
716                         }
717                 }
718                 ast_mutex_unlock(&refreshlock);
719
720                 if (pvt->owner->unloading) {
721                         ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
722                         return NULL;
723                 }
724
725                 ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);
726
727                 update_caldav(pvt);
728         }
729
730         return NULL;
731 }
732
733 static int load_module(void)
734 {
735         ne_sock_init();
736         if (ast_calendar_register(&caldav_tech)) {
737                 ne_sock_exit();
738                 return AST_MODULE_LOAD_DECLINE;
739         }
740
741         return AST_MODULE_LOAD_SUCCESS;
742 }
743
744 static int unload_module(void)
745 {
746         ast_calendar_unregister(&caldav_tech);
747         ne_sock_exit();
748         return 0;
749 }
750
751 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk CalDAV Calendar Integration",
752         .support_level = AST_MODULE_SUPPORT_EXTENDED,
753         .load = load_module,
754         .unload = unload_module,
755         .load_pri = AST_MODPRI_DEVSTATE_PLUGIN,
756         .requires = "res_calendar",
757 );