res_calendar_caldav.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. /*! \file
  19. * \brief Resource for handling CalDAV calendars
  20. */
  21. /*** MODULEINFO
  22. <depend>neon</depend>
  23. <depend>ical</depend>
  24. <depend>libxml2</depend>
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <libical/ical.h>
  30. #include <ne_session.h>
  31. #include <ne_uri.h>
  32. #include <ne_request.h>
  33. #include <ne_auth.h>
  34. #include <ne_redirect.h>
  35. #include <libxml/xmlmemory.h>
  36. #include <libxml/parser.h>
  37. #include "asterisk/module.h"
  38. #include "asterisk/calendar.h"
  39. #include "asterisk/lock.h"
  40. #include "asterisk/config.h"
  41. #include "asterisk/astobj2.h"
  42. static void *caldav_load_calendar(void *data);
  43. static void *unref_caldav(void *obj);
  44. static int caldav_write_event(struct ast_calendar_event *event);
  45. static struct ast_calendar_tech caldav_tech = {
  46. .type = "caldav",
  47. .description = "CalDAV calendars",
  48. .module = AST_MODULE,
  49. .load_calendar = caldav_load_calendar,
  50. .unref_calendar = unref_caldav,
  51. .write_event = caldav_write_event,
  52. };
  53. struct caldav_pvt {
  54. AST_DECLARE_STRING_FIELDS(
  55. AST_STRING_FIELD(url);
  56. AST_STRING_FIELD(user);
  57. AST_STRING_FIELD(secret);
  58. );
  59. struct ast_calendar *owner;
  60. ne_uri uri;
  61. ne_session *session;
  62. struct ao2_container *events;
  63. };
  64. static void caldav_destructor(void *obj)
  65. {
  66. struct caldav_pvt *pvt = obj;
  67. ast_debug(1, "Destroying pvt for CalDAV calendar %s\n", pvt->owner->name);
  68. if (pvt->session) {
  69. ne_session_destroy(pvt->session);
  70. }
  71. ast_string_field_free_memory(pvt);
  72. ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  73. ao2_ref(pvt->events, -1);
  74. }
  75. static void *unref_caldav(void *obj)
  76. {
  77. struct caldav_pvt *pvt = obj;
  78. ao2_ref(pvt, -1);
  79. return NULL;
  80. }
  81. static int fetch_response_reader(void *data, const char *block, size_t len)
  82. {
  83. struct ast_str **response = data;
  84. unsigned char *tmp;
  85. if (!(tmp = ast_malloc(len + 1))) {
  86. return -1;
  87. }
  88. memcpy(tmp, block, len);
  89. tmp[len] = '\0';
  90. ast_str_append(response, 0, "%s", tmp);
  91. ast_free(tmp);
  92. return 0;
  93. }
  94. static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
  95. {
  96. struct caldav_pvt *pvt = userdata;
  97. if (attempts > 1) {
  98. ast_log(LOG_WARNING, "Invalid username or password for CalDAV calendar '%s'\n", pvt->owner->name);
  99. return -1;
  100. }
  101. ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
  102. ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
  103. return 0;
  104. }
  105. 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)
  106. {
  107. struct ast_str *response;
  108. ne_request *req;
  109. int ret;
  110. char buf[1000];
  111. if (!pvt) {
  112. ast_log(LOG_ERROR, "There is no private!\n");
  113. return NULL;
  114. }
  115. if (!(response = ast_str_create(512))) {
  116. ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
  117. return NULL;
  118. }
  119. snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : "");
  120. req = ne_request_create(pvt->session, method, buf);
  121. ne_add_response_body_reader(req, ne_accept_2xx, fetch_response_reader, &response);
  122. ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body));
  123. ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type);
  124. ret = ne_request_dispatch(req);
  125. ne_request_destroy(req);
  126. if (ret != NE_OK || !ast_str_strlen(response)) {
  127. if (ret != NE_OK) {
  128. 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));
  129. }
  130. ast_free(response);
  131. return NULL;
  132. }
  133. return response;
  134. }
  135. static int caldav_write_event(struct ast_calendar_event *event)
  136. {
  137. struct caldav_pvt *pvt;
  138. struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
  139. icalcomponent *calendar, *icalevent;
  140. icaltimezone *utc = icaltimezone_get_utc_timezone();
  141. int ret = -1;
  142. if (!event) {
  143. ast_log(LOG_WARNING, "No event passed!\n");
  144. return -1;
  145. }
  146. if (!(event->start && event->end)) {
  147. ast_log(LOG_WARNING, "The event must contain a start and an end\n");
  148. return -1;
  149. }
  150. if (!(body = ast_str_create(512)) ||
  151. !(subdir = ast_str_create(32))) {
  152. ast_log(LOG_ERROR, "Could not allocate memory for request!\n");
  153. goto write_cleanup;
  154. }
  155. pvt = event->owner->tech_pvt;
  156. if (ast_strlen_zero(event->uid)) {
  157. unsigned short val[8];
  158. int x;
  159. for (x = 0; x < 8; x++) {
  160. val[x] = ast_random();
  161. }
  162. ast_string_field_build(event, uid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
  163. }
  164. calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
  165. icalcomponent_add_property(calendar, icalproperty_new_version("2.0"));
  166. icalcomponent_add_property(calendar, icalproperty_new_prodid("-//Digium, Inc.//res_caldav//EN"));
  167. icalevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
  168. icalcomponent_add_property(icalevent, icalproperty_new_dtstamp(icaltime_current_time_with_zone(utc)));
  169. icalcomponent_add_property(icalevent, icalproperty_new_uid(event->uid));
  170. icalcomponent_add_property(icalevent, icalproperty_new_dtstart(icaltime_from_timet_with_zone(event->start, 0, utc)));
  171. icalcomponent_add_property(icalevent, icalproperty_new_dtend(icaltime_from_timet_with_zone(event->end, 0, utc)));
  172. if (!ast_strlen_zero(event->organizer)) {
  173. icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->organizer));
  174. }
  175. if (!ast_strlen_zero(event->summary)) {
  176. icalcomponent_add_property(icalevent, icalproperty_new_summary(event->summary));
  177. }
  178. if (!ast_strlen_zero(event->description)) {
  179. icalcomponent_add_property(icalevent, icalproperty_new_description(event->description));
  180. }
  181. if (!ast_strlen_zero(event->location)) {
  182. icalcomponent_add_property(icalevent, icalproperty_new_location(event->location));
  183. }
  184. if (!ast_strlen_zero(event->categories)) {
  185. icalcomponent_add_property(icalevent, icalproperty_new_categories(event->categories));
  186. }
  187. if (event->priority > 0) {
  188. icalcomponent_add_property(icalevent, icalproperty_new_priority(event->priority));
  189. }
  190. switch (event->busy_state) {
  191. case AST_CALENDAR_BS_BUSY:
  192. icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));
  193. break;
  194. case AST_CALENDAR_BS_BUSY_TENTATIVE:
  195. icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE));
  196. break;
  197. default:
  198. icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE));
  199. }
  200. icalcomponent_add_component(calendar, icalevent);
  201. ast_str_append(&body, 0, "%s", icalcomponent_as_ical_string(calendar));
  202. ast_str_set(&subdir, 0, "%s%s.ics", pvt->url[strlen(pvt->url) - 1] == '/' ? "" : "/", event->uid);
  203. response = caldav_request(pvt, "PUT", body, subdir, "text/calendar");
  204. ret = 0;
  205. write_cleanup:
  206. if (body) {
  207. ast_free(body);
  208. }
  209. if (response) {
  210. ast_free(response);
  211. }
  212. if (subdir) {
  213. ast_free(subdir);
  214. }
  215. return ret;
  216. }
  217. static struct ast_str *caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
  218. {
  219. struct ast_str *body, *response;
  220. icaltimezone *utc = icaltimezone_get_utc_timezone();
  221. icaltimetype start, end;
  222. const char *start_str, *end_str;
  223. if (!(body = ast_str_create(512))) {
  224. ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
  225. return NULL;
  226. }
  227. start = icaltime_from_timet_with_zone(start_time, 0, utc);
  228. end = icaltime_from_timet_with_zone(end_time, 0, utc);
  229. start_str = icaltime_as_ical_string(start);
  230. end_str = icaltime_as_ical_string(end);
  231. /* If I was really being efficient, I would store a collection of event URIs and etags,
  232. * first doing a query of just the etag and seeing if anything had changed. If it had,
  233. * then I would do a request for each of the events that had changed, and only bother
  234. * updating those. Oh well. */
  235. ast_str_append(&body, 0,
  236. "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
  237. "<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n"
  238. " <D:prop>\n"
  239. " <C:calendar-data>\n"
  240. " <C:expand start=\"%s\" end=\"%s\"/>\n"
  241. " </C:calendar-data>\n"
  242. " </D:prop>\n"
  243. " <C:filter>\n"
  244. " <C:comp-filter name=\"VCALENDAR\">\n"
  245. " <C:comp-filter name=\"VEVENT\">\n"
  246. " <C:time-range start=\"%s\" end=\"%s\"/>\n"
  247. " </C:comp-filter>\n"
  248. " </C:comp-filter>\n"
  249. " </C:filter>\n"
  250. "</C:calendar-query>\n", start_str, end_str, start_str, end_str);
  251. response = caldav_request(pvt, "REPORT", body, NULL, NULL);
  252. ast_free(body);
  253. return response;
  254. }
  255. static time_t icalfloat_to_timet(icaltimetype time)
  256. {
  257. struct ast_tm tm = {0,};
  258. struct timeval tv;
  259. tm.tm_mday = time.day;
  260. tm.tm_mon = time.month - 1;
  261. tm.tm_year = time.year - 1900;
  262. tm.tm_hour = time.hour;
  263. tm.tm_min = time.minute;
  264. tm.tm_sec = time.second;
  265. tm.tm_isdst = -1;
  266. tv = ast_mktime(&tm, NULL);
  267. return tv.tv_sec;
  268. }
  269. /* span->start & span->end may be dates or floating times which have no timezone,
  270. * which would mean that they should apply to the local timezone for all recepients.
  271. * For example, if a meeting was set for 1PM-2PM floating time, people in different time
  272. * zones would not be scheduled at the same local times. Dates are often treated as
  273. * floating times, so all day events will need to be converted--so we can trust the
  274. * span here, and instead will grab the start and end from the component, which will
  275. * allow us to test for floating times or dates.
  276. */
  277. static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)
  278. {
  279. struct caldav_pvt *pvt = data;
  280. struct ast_calendar_event *event;
  281. icaltimezone *utc = icaltimezone_get_utc_timezone();
  282. icaltimetype start, end, tmp;
  283. icalcomponent *valarm;
  284. icalproperty *prop;
  285. struct icaltriggertype trigger;
  286. if (!(pvt && pvt->owner)) {
  287. ast_log(LOG_ERROR, "Require a private structure with an owner\n");
  288. return;
  289. }
  290. if (!(event = ast_calendar_event_alloc(pvt->owner))) {
  291. ast_log(LOG_ERROR, "Could not allocate an event!\n");
  292. return;
  293. }
  294. start = icalcomponent_get_dtstart(comp);
  295. end = icalcomponent_get_dtend(comp);
  296. event->start = icaltime_get_tzid(start) ? span->start : icalfloat_to_timet(start);
  297. event->end = icaltime_get_tzid(end) ? span->end : icalfloat_to_timet(end);
  298. event->busy_state = span->is_busy ? AST_CALENDAR_BS_BUSY : AST_CALENDAR_BS_FREE;
  299. if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) {
  300. ast_string_field_set(event, summary, icalproperty_get_value_as_string(prop));
  301. }
  302. if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) {
  303. ast_string_field_set(event, description, icalproperty_get_value_as_string(prop));
  304. }
  305. if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) {
  306. ast_string_field_set(event, organizer, icalproperty_get_value_as_string(prop));
  307. }
  308. if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) {
  309. ast_string_field_set(event, location, icalproperty_get_value_as_string(prop));
  310. }
  311. if ((prop = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY))) {
  312. ast_string_field_set(event, categories, icalproperty_get_value_as_string(prop));
  313. }
  314. if ((prop = icalcomponent_get_first_property(comp, ICAL_PRIORITY_PROPERTY))) {
  315. event->priority = icalvalue_get_integer(icalproperty_get_value(prop));
  316. }
  317. if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) {
  318. ast_string_field_set(event, uid, icalproperty_get_value_as_string(prop));
  319. } else {
  320. ast_log(LOG_WARNING, "No UID found, but one is required. Generating, but updates may not be acurate\n");
  321. if (!ast_strlen_zero(event->summary)) {
  322. ast_string_field_set(event, uid, event->summary);
  323. } else {
  324. char tmp[100];
  325. snprintf(tmp, sizeof(tmp), "%lu", event->start);
  326. ast_string_field_set(event, uid, tmp);
  327. }
  328. }
  329. /* Get the attendees */
  330. for (prop = icalcomponent_get_first_property(comp, ICAL_ATTENDEE_PROPERTY);
  331. prop; prop = icalcomponent_get_next_property(comp, ICAL_ATTENDEE_PROPERTY)) {
  332. struct ast_calendar_attendee *attendee;
  333. const char *data;
  334. if (!(attendee = ast_calloc(1, sizeof(*attendee)))) {
  335. event = ast_calendar_unref_event(event);
  336. return;
  337. }
  338. data = icalproperty_get_attendee(prop);
  339. if (ast_strlen_zero(data)) {
  340. ast_free(attendee);
  341. continue;
  342. }
  343. attendee->data = ast_strdup(data);
  344. AST_LIST_INSERT_TAIL(&event->attendees, attendee, next);
  345. }
  346. /* Only set values for alarm based on VALARM. Can be overriden in main/calendar.c by autoreminder
  347. * therefore, go ahead and add events even if their is no VALARM or it is malformed
  348. * Currently we are only getting the first VALARM and are handling repitition in main/calendar.c from calendar.conf */
  349. if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
  350. ao2_link(pvt->events, event);
  351. event = ast_calendar_unref_event(event);
  352. return;
  353. }
  354. if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) {
  355. ast_log(LOG_WARNING, "VALARM has no TRIGGER, skipping!\n");
  356. ao2_link(pvt->events, event);
  357. event = ast_calendar_unref_event(event);
  358. return;
  359. }
  360. trigger = icalproperty_get_trigger(prop);
  361. if (icaltriggertype_is_null_trigger(trigger)) {
  362. ast_log(LOG_WARNING, "Bad TRIGGER for VALARM, skipping!\n");
  363. ao2_link(pvt->events, event);
  364. event = ast_calendar_unref_event(event);
  365. return;
  366. }
  367. if (!icaltime_is_null_time(trigger.time)) { /* This is an absolute time */
  368. tmp = icaltime_convert_to_zone(trigger.time, utc);
  369. event->alarm = icaltime_as_timet_with_zone(tmp, utc);
  370. } else { /* Offset from either dtstart or dtend */
  371. /* XXX Technically you can check RELATED to see if the event fires from the END of the event
  372. * But, I'm not sure I've ever seen anyone implement it in calendaring software, so I'm ignoring for now */
  373. tmp = icaltime_add(start, trigger.duration);
  374. event->alarm = icaltime_as_timet_with_zone(tmp, icaltime_get_timezone(start));
  375. }
  376. ao2_link(pvt->events, event);
  377. event = ast_calendar_unref_event(event);
  378. return;
  379. }
  380. struct xmlstate {
  381. int in_caldata;
  382. struct caldav_pvt *pvt;
  383. struct ast_str *cdata;
  384. time_t start;
  385. time_t end;
  386. };
  387. static void handle_start_element(void *data, const xmlChar *fullname, const xmlChar **atts)
  388. {
  389. struct xmlstate *state = data;
  390. if (!xmlStrcasecmp(fullname, BAD_CAST "C:calendar-data")) {
  391. state->in_caldata = 1;
  392. ast_str_reset(state->cdata);
  393. }
  394. }
  395. static void handle_end_element(void *data, const xmlChar *name)
  396. {
  397. struct xmlstate *state = data;
  398. struct icaltimetype start, end;
  399. icaltimezone *utc = icaltimezone_get_utc_timezone();
  400. icalcomponent *iter;
  401. icalcomponent *comp;
  402. if (xmlStrcasecmp(name, BAD_CAST "C:calendar-data")) {
  403. return;
  404. }
  405. state->in_caldata = 0;
  406. if (!(state->cdata && ast_str_strlen(state->cdata))) {
  407. return;
  408. }
  409. /* XXX Parse the calendar blurb for recurrence events in the time range,
  410. * create an event, and add it to pvt->events */
  411. start = icaltime_from_timet_with_zone(state->start, 0, utc);
  412. end = icaltime_from_timet_with_zone(state->end, 0, utc);
  413. comp = icalparser_parse_string(ast_str_buffer(state->cdata));
  414. for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT);
  415. iter;
  416. iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT))
  417. {
  418. icalcomponent_foreach_recurrence(iter, start, end, caldav_add_event, state->pvt);
  419. }
  420. icalcomponent_free(comp);
  421. }
  422. static void handle_characters(void *data, const xmlChar *ch, int len)
  423. {
  424. struct xmlstate *state = data;
  425. xmlChar *tmp;
  426. if (!state->in_caldata) {
  427. return;
  428. }
  429. tmp = xmlStrndup(ch, len);
  430. ast_str_append(&state->cdata, 0, "%s", (char *)tmp);
  431. xmlFree(tmp);
  432. }
  433. static int update_caldav(struct caldav_pvt *pvt)
  434. {
  435. struct timeval now = ast_tvnow();
  436. time_t start, end;
  437. struct ast_str *response;
  438. xmlSAXHandler saxHandler;
  439. struct xmlstate state = {
  440. .in_caldata = 0,
  441. .pvt = pvt
  442. };
  443. start = now.tv_sec;
  444. end = now.tv_sec + 60 * pvt->owner->timeframe;
  445. if (!(response = caldav_get_events_between(pvt, start, end))) {
  446. return -1;
  447. }
  448. if (!(state.cdata = ast_str_create(512))) {
  449. ast_free(response);
  450. return -1;
  451. }
  452. state.start = start;
  453. state.end = end;
  454. memset(&saxHandler, 0, sizeof(saxHandler));
  455. saxHandler.startElement = handle_start_element;
  456. saxHandler.endElement = handle_end_element;
  457. saxHandler.characters = handle_characters;
  458. xmlSAXUserParseMemory(&saxHandler, &state, ast_str_buffer(response), ast_str_strlen(response));
  459. ast_calendar_merge_events(pvt->owner, pvt->events);
  460. ast_free(response);
  461. ast_free(state.cdata);
  462. return 0;
  463. }
  464. static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
  465. {
  466. /* Verify all certs */
  467. return 0;
  468. }
  469. static void *caldav_load_calendar(void *void_data)
  470. {
  471. struct caldav_pvt *pvt;
  472. const struct ast_config *cfg;
  473. struct ast_variable *v;
  474. struct ast_calendar *cal = void_data;
  475. ast_mutex_t refreshlock;
  476. if (!(cal && (cfg = ast_calendar_config_acquire()))) {
  477. ast_log(LOG_ERROR, "You must enable calendar support for res_caldav to load\n");
  478. return NULL;
  479. }
  480. if (ao2_trylock(cal)) {
  481. if (cal->unloading) {
  482. ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
  483. } else {
  484. ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
  485. }
  486. ast_calendar_config_release();
  487. return NULL;
  488. }
  489. if (!(pvt = ao2_alloc(sizeof(*pvt), caldav_destructor))) {
  490. ast_log(LOG_ERROR, "Could not allocate caldav_pvt structure for calendar: %s\n", cal->name);
  491. ast_calendar_config_release();
  492. return NULL;
  493. }
  494. pvt->owner = cal;
  495. if (!(pvt->events = ast_calendar_event_container_alloc())) {
  496. ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
  497. pvt = unref_caldav(pvt);
  498. ao2_unlock(cal);
  499. ast_calendar_config_release();
  500. return NULL;
  501. }
  502. if (ast_string_field_init(pvt, 32)) {
  503. ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
  504. pvt = unref_caldav(pvt);
  505. ao2_unlock(cal);
  506. ast_calendar_config_release();
  507. return NULL;
  508. }
  509. for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
  510. if (!strcasecmp(v->name, "url")) {
  511. ast_string_field_set(pvt, url, v->value);
  512. } else if (!strcasecmp(v->name, "user")) {
  513. ast_string_field_set(pvt, user, v->value);
  514. } else if (!strcasecmp(v->name, "secret")) {
  515. ast_string_field_set(pvt, secret, v->value);
  516. }
  517. }
  518. ast_calendar_config_release();
  519. if (ast_strlen_zero(pvt->url)) {
  520. ast_log(LOG_WARNING, "No URL was specified for CalDAV calendar '%s' - skipping.\n", cal->name);
  521. pvt = unref_caldav(pvt);
  522. ao2_unlock(cal);
  523. return NULL;
  524. }
  525. if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
  526. ast_log(LOG_WARNING, "Could not parse url '%s' for CalDAV calendar '%s' - skipping.\n", pvt->url, cal->name);
  527. pvt = unref_caldav(pvt);
  528. ao2_unlock(cal);
  529. return NULL;
  530. }
  531. if (pvt->uri.scheme == NULL) {
  532. pvt->uri.scheme = "http";
  533. }
  534. if (pvt->uri.port == 0) {
  535. pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
  536. }
  537. pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
  538. ne_redirect_register(pvt->session);
  539. ne_set_server_auth(pvt->session, auth_credentials, pvt);
  540. if (!strcasecmp(pvt->uri.scheme, "https")) {
  541. ne_ssl_trust_default_ca(pvt->session);
  542. ne_ssl_set_verify(pvt->session, verify_cert, NULL);
  543. }
  544. cal->tech_pvt = pvt;
  545. ast_mutex_init(&refreshlock);
  546. /* Load it the first time */
  547. update_caldav(pvt);
  548. ao2_unlock(cal);
  549. /* The only writing from another thread will be if unload is true */
  550. for (;;) {
  551. struct timeval tv = ast_tvnow();
  552. struct timespec ts = {0,};
  553. ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);
  554. ast_mutex_lock(&refreshlock);
  555. while (!pvt->owner->unloading) {
  556. if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
  557. break;
  558. }
  559. }
  560. ast_mutex_unlock(&refreshlock);
  561. if (pvt->owner->unloading) {
  562. ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
  563. return NULL;
  564. }
  565. ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);
  566. update_caldav(pvt);
  567. }
  568. return NULL;
  569. }
  570. static int load_module(void)
  571. {
  572. ne_sock_init();
  573. if (ast_calendar_register(&caldav_tech)) {
  574. ne_sock_exit();
  575. return AST_MODULE_LOAD_DECLINE;
  576. }
  577. return AST_MODULE_LOAD_SUCCESS;
  578. }
  579. static int unload_module(void)
  580. {
  581. ast_calendar_unregister(&caldav_tech);
  582. ne_sock_exit();
  583. return 0;
  584. }
  585. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk CalDAV Calendar Integration",
  586. .load = load_module,
  587. .unload = unload_module,
  588. .load_pri = AST_MODPRI_DEVSTATE_PLUGIN,
  589. );