res_calendar_caldav.c 21 KB

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