res_calendar_exchange.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 MS Exchange calendars
  20. */
  21. /*** MODULEINFO
  22. <depend>neon</depend>
  23. <depend>ical</depend>
  24. <depend>iksemel</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 <iksemel.h>
  36. #include "asterisk/module.h"
  37. #include "asterisk/calendar.h"
  38. #include "asterisk/lock.h"
  39. #include "asterisk/config.h"
  40. #include "asterisk/astobj2.h"
  41. static void *exchangecal_load_calendar(void *data);
  42. static void *unref_exchangecal(void *obj);
  43. static int exchangecal_write_event(struct ast_calendar_event *event);
  44. static struct ast_calendar_tech exchangecal_tech = {
  45. .type = "exchange",
  46. .description = "MS Exchange calendars",
  47. .module = AST_MODULE,
  48. .load_calendar = exchangecal_load_calendar,
  49. .unref_calendar = unref_exchangecal,
  50. .write_event = exchangecal_write_event,
  51. };
  52. struct exchangecal_pvt {
  53. AST_DECLARE_STRING_FIELDS(
  54. AST_STRING_FIELD(url);
  55. AST_STRING_FIELD(user);
  56. AST_STRING_FIELD(secret);
  57. );
  58. struct ast_calendar *owner;
  59. ne_uri uri;
  60. ne_session *session;
  61. struct ao2_container *events;
  62. };
  63. struct xmlstate {
  64. char tag[80];
  65. int in_response;
  66. int in_propstat;
  67. int in_prop;
  68. void *ptr;
  69. struct exchangecal_pvt *pvt;
  70. };
  71. static int parse_tag(void *data, char *name, char **atts, int type)
  72. {
  73. struct xmlstate *state = data;
  74. char *tmp;
  75. if ((tmp = strchr(name, ':'))) {
  76. tmp++;
  77. } else {
  78. return IKS_HOOK;
  79. }
  80. ast_copy_string(state->tag, tmp, sizeof(state->tag));
  81. switch (type) {
  82. case IKS_OPEN:
  83. if (!strcasecmp(state->tag, "response")) {
  84. struct ast_calendar_event *event;
  85. state->in_response = 1;
  86. if (!(event = ast_calendar_event_alloc(state->pvt->owner))) {
  87. return IKS_NOMEM;
  88. }
  89. state->ptr = event;
  90. } else if (!strcasecmp(state->tag, "propstat")) {
  91. state->in_propstat = 1;
  92. } else if (!strcasecmp(state->tag, "prop")) {
  93. state->in_prop = 1;
  94. }
  95. break;
  96. case IKS_CLOSE:
  97. if (!strcasecmp(state->tag, "response")) {
  98. struct ao2_container *events = state->pvt->events;
  99. struct ast_calendar_event *event = state->ptr;
  100. state->in_response = 0;
  101. if (ast_strlen_zero(event->uid)) {
  102. ast_log(LOG_ERROR, "This event has no UID, something has gone wrong\n");
  103. event = ast_calendar_unref_event(event);
  104. return IKS_HOOK;
  105. }
  106. ao2_link(events, event);
  107. event = ast_calendar_unref_event(event);
  108. } else if (!strcasecmp(state->tag, "propstat")) {
  109. state->in_propstat = 0;
  110. } else if (!strcasecmp(state->tag, "prop")) {
  111. state->in_prop = 0;
  112. }
  113. break;
  114. default:
  115. return IKS_OK;
  116. }
  117. return IKS_OK;
  118. }
  119. static time_t mstime_to_time_t(char *mstime)
  120. {
  121. char *read, *write;
  122. icaltimetype tt;
  123. for (read = write = mstime; *read; read++) {
  124. if (*read == '.') {
  125. *write++ = 'Z';
  126. *write = '\0';
  127. break;
  128. }
  129. if (*read == '-' || *read == ':')
  130. continue;
  131. *write = *read;
  132. write++;
  133. }
  134. tt = icaltime_from_string(mstime);
  135. return icaltime_as_timet(tt);
  136. }
  137. static enum ast_calendar_busy_state msbusy_to_bs(const char *msbusy)
  138. {
  139. if (!strcasecmp(msbusy, "FREE")) {
  140. return AST_CALENDAR_BS_FREE;
  141. } else if (!strcasecmp(msbusy, "TENTATIVE")) {
  142. return AST_CALENDAR_BS_BUSY_TENTATIVE;
  143. } else {
  144. return AST_CALENDAR_BS_BUSY;
  145. }
  146. }
  147. static int parse_cdata(void *data, char *value, size_t len)
  148. {
  149. char *str;
  150. struct xmlstate *state = data;
  151. struct ast_calendar_event *event = state->ptr;
  152. str = ast_skip_blanks(value);
  153. if (str == value + len)
  154. return IKS_OK;
  155. if (!(str = ast_calloc(1, len + 1))) {
  156. return IKS_NOMEM;
  157. }
  158. memcpy(str, value, len);
  159. if (!(state->in_response && state->in_propstat && state->in_prop)) {
  160. ast_free(str);
  161. return IKS_OK;
  162. }
  163. /* We use ast_string_field_build here because libiksemel is parsing CDATA with &lt; as
  164. * new elements which is a bit odd and shouldn't happen */
  165. if (!strcasecmp(state->tag, "subject")) {
  166. ast_string_field_build(event, summary, "%s%s", event->summary, str);
  167. } else if (!strcasecmp(state->tag, "location")) {
  168. ast_string_field_build(event, location, "%s%s", event->location, str);
  169. } else if (!strcasecmp(state->tag, "uid")) {
  170. ast_string_field_build(event, uid, "%s%s", event->location, str);
  171. } else if (!strcasecmp(state->tag, "organizer")) {
  172. ast_string_field_build(event, organizer, "%s%s", event->organizer, str);
  173. } else if (!strcasecmp(state->tag, "textdescription")) {
  174. ast_string_field_build(event, description, "%s%s", event->description, str);
  175. } else if (!strcasecmp(state->tag, "dtstart")) {
  176. event->start = mstime_to_time_t(str);
  177. } else if (!strcasecmp(state->tag, "dtend")) {
  178. event->end = mstime_to_time_t(str);
  179. } else if (!strcasecmp(state->tag, "busystatus")) {
  180. event->busy_state = msbusy_to_bs(str);
  181. } else if (!strcasecmp(state->tag, "reminderoffset")) {
  182. /*XXX Currently we rely on event->start being set first which means we rely on the response order
  183. * which technically should be fine since the query returns in the order we ask for, but ... */
  184. event->alarm = event->start - atoi(str);
  185. }
  186. ast_free(str);
  187. return IKS_OK;
  188. }
  189. static void exchangecal_destructor(void *obj)
  190. {
  191. struct exchangecal_pvt *pvt = obj;
  192. ast_debug(1, "Destroying pvt for Exchange calendar %s\n", pvt->owner->name);
  193. if (pvt->session) {
  194. ne_session_destroy(pvt->session);
  195. }
  196. ast_string_field_free_memory(pvt);
  197. ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  198. ao2_ref(pvt->events, -1);
  199. }
  200. static void *unref_exchangecal(void *obj)
  201. {
  202. struct exchangecal_pvt *pvt = obj;
  203. ao2_ref(pvt, -1);
  204. return NULL;
  205. }
  206. /* It is very important to use the return value of this function as a realloc could occur */
  207. static struct ast_str *generate_exchange_uuid(struct ast_str *uid)
  208. {
  209. unsigned short val[8];
  210. int x;
  211. for (x = 0; x < 8; x++) {
  212. val[x] = ast_random();
  213. }
  214. ast_str_set(&uid, 0, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
  215. return uid;
  216. }
  217. static int is_valid_uuid(struct ast_str *uid)
  218. {
  219. int i;
  220. if (ast_str_strlen(uid) != 36) {
  221. return 0;
  222. }
  223. for (i = 0; i < ast_str_strlen(uid); i++) {
  224. if (i == 8 || i == 13 || i == 18 || i == 23) {
  225. if (ast_str_buffer(uid)[i] != '-') {
  226. return 0;
  227. }
  228. } else if (!((ast_str_buffer(uid)[i] > 47 && ast_str_buffer(uid)[i] < 58) || (ast_str_buffer(uid)[i] > 96 && ast_str_buffer(uid)[i] < 103))) {
  229. return 0;
  230. }
  231. }
  232. return 1;
  233. }
  234. static struct ast_str *xml_encode_str(struct ast_str *dst, const char *src)
  235. {
  236. const char *tmp;
  237. char buf[7];
  238. for (tmp = src; *tmp; tmp++) {
  239. switch (*tmp) {
  240. case '\"':
  241. strcpy(buf, "&quot;");
  242. break;
  243. case '\'':
  244. strcpy(buf, "&apos;");
  245. break;
  246. case '&':
  247. strcpy(buf, "&amp;");
  248. break;
  249. case '<':
  250. strcpy(buf, "&lt;");
  251. break;
  252. case '>':
  253. strcpy(buf, "&gt;");
  254. break;
  255. default:
  256. sprintf(buf, "%c", *tmp);
  257. }
  258. ast_str_append(&dst, 0, "%s", buf);
  259. }
  260. return dst;
  261. }
  262. static struct ast_str *epoch_to_exchange_time(struct ast_str *dst, time_t epoch)
  263. {
  264. icaltimezone *utc = icaltimezone_get_utc_timezone();
  265. icaltimetype tt = icaltime_from_timet_with_zone(epoch, 0, utc);
  266. char tmp[30];
  267. int i;
  268. ast_copy_string(tmp, icaltime_as_ical_string(tt), sizeof(tmp));
  269. for (i = 0; tmp[i]; i++) {
  270. ast_str_append(&dst, 0, "%c", tmp[i]);
  271. if (i == 3 || i == 5)
  272. ast_str_append(&dst, 0, "%c", '-');
  273. if (i == 10 || i == 12)
  274. ast_str_append(&dst, 0, "%c", ':');
  275. if (i == 14)
  276. ast_str_append(&dst, 0, "%s", ".000");
  277. }
  278. return dst;
  279. }
  280. static struct ast_str *bs_to_exchange_bs(struct ast_str *dst, enum ast_calendar_busy_state bs)
  281. {
  282. switch (bs) {
  283. case AST_CALENDAR_BS_BUSY:
  284. ast_str_set(&dst, 0, "%s", "BUSY");
  285. break;
  286. case AST_CALENDAR_BS_BUSY_TENTATIVE:
  287. ast_str_set(&dst, 0, "%s", "TENTATIVE");
  288. break;
  289. default:
  290. ast_str_set(&dst, 0, "%s", "FREE");
  291. }
  292. return dst;
  293. }
  294. static int fetch_response_reader(void *data, const char *block, size_t len)
  295. {
  296. struct ast_str **response = data;
  297. unsigned char *tmp;
  298. if (!(tmp = ast_malloc(len + 1))) {
  299. return -1;
  300. }
  301. memcpy(tmp, block, len);
  302. tmp[len] = '\0';
  303. ast_str_append(response, 0, "%s", tmp);
  304. ast_free(tmp);
  305. return 0;
  306. }
  307. static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
  308. {
  309. struct exchangecal_pvt *pvt = userdata;
  310. if (attempts > 1) {
  311. ast_log(LOG_WARNING, "Invalid username or password for Exchange calendar '%s'\n", pvt->owner->name);
  312. return -1;
  313. }
  314. ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
  315. ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
  316. return 0;
  317. }
  318. static struct ast_str *exchangecal_request(struct exchangecal_pvt *pvt, const char *method, struct ast_str *req_body, struct ast_str *subdir)
  319. {
  320. struct ast_str *response;
  321. ne_request *req;
  322. int ret;
  323. char buf[1000];
  324. if (!pvt) {
  325. ast_log(LOG_ERROR, "There is no private!\n");
  326. return NULL;
  327. }
  328. if (!(response = ast_str_create(512))) {
  329. ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
  330. return NULL;
  331. }
  332. snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : "");
  333. req = ne_request_create(pvt->session, method, buf);
  334. ne_add_response_body_reader(req, ne_accept_2xx, fetch_response_reader, &response);
  335. ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body));
  336. ne_add_request_header(req, "Content-type", "text/xml");
  337. ret = ne_request_dispatch(req);
  338. ne_request_destroy(req);
  339. if (ret != NE_OK || !ast_str_strlen(response)) {
  340. ast_log(LOG_WARNING, "Unknown response to CalDAV calendar %s, request %s to %s: %s\n", pvt->owner->name, method, pvt->url, ne_get_error(pvt->session));
  341. ast_free(response);
  342. return NULL;
  343. }
  344. return response;
  345. }
  346. static int exchangecal_write_event(struct ast_calendar_event *event)
  347. {
  348. struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
  349. struct ast_str *uid = NULL, *summary = NULL, *description = NULL, *organizer = NULL,
  350. *location = NULL, *start = NULL, *end = NULL, *busystate = NULL;
  351. int ret = -1;
  352. if (!event) {
  353. ast_log(LOG_WARNING, "No event passed!\n");
  354. return -1;
  355. }
  356. if (!(event->start && event->end)) {
  357. ast_log(LOG_WARNING, "The event must contain a start and an end\n");
  358. return -1;
  359. }
  360. if (!(body = ast_str_create(512)) ||
  361. !(subdir = ast_str_create(32))) {
  362. ast_log(LOG_ERROR, "Could not allocate memory for request!\n");
  363. goto write_cleanup;
  364. }
  365. if (!(uid = ast_str_create(32)) ||
  366. !(summary = ast_str_create(32)) ||
  367. !(description = ast_str_create(32)) ||
  368. !(organizer = ast_str_create(32)) ||
  369. !(location = ast_str_create(32)) ||
  370. !(start = ast_str_create(32)) ||
  371. !(end = ast_str_create(32)) ||
  372. !(busystate = ast_str_create(32))) {
  373. ast_log(LOG_ERROR, "Unable to allocate memory for request values\n");
  374. goto write_cleanup;
  375. }
  376. if (ast_strlen_zero(event->uid)) {
  377. uid = generate_exchange_uuid(uid);
  378. } else {
  379. ast_str_set(&uid, 36, "%s", event->uid);
  380. }
  381. if (!is_valid_uuid(uid)) {
  382. ast_log(LOG_WARNING, "An invalid uid was provided, you may leave this field blank to have one generated for you\n");
  383. goto write_cleanup;
  384. }
  385. summary = xml_encode_str(summary, event->summary);
  386. description = xml_encode_str(description, event->description);
  387. organizer = xml_encode_str(organizer, event->organizer);
  388. location = xml_encode_str(location, event->location);
  389. start = epoch_to_exchange_time(start, event->start);
  390. end = epoch_to_exchange_time(end, event->end);
  391. busystate = bs_to_exchange_bs(busystate, event->busy_state);
  392. ast_str_append(&body, 0,
  393. "<?xml version=\"1.0\"?>\n"
  394. "<a:propertyupdate\n"
  395. " xmlns:a=\"DAV:\"\n"
  396. " xmlns:e=\"http://schemas.microsoft.com/exchange/\"\n"
  397. " xmlns:mapi=\"http://schemas.microsoft.com/mapi/\"\n"
  398. " xmlns:mapit=\"http://schemas.microsoft.com/mapi/proptag/\"\n"
  399. " xmlns:x=\"xml:\" xmlns:cal=\"urn:schemas:calendar:\"\n"
  400. " xmlns:dt=\"uuid:%s/\"\n" /* uid */
  401. " xmlns:header=\"urn:schemas:mailheader:\"\n"
  402. " xmlns:mail=\"urn:schemas:httpmail:\"\n"
  403. ">\n"
  404. " <a:set>\n"
  405. " <a:prop>\n"
  406. " <a:contentclass>urn:content-classes:appointment</a:contentclass>\n"
  407. " <e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>\n"
  408. " <mail:subject>%s</mail:subject>\n" /* summary */
  409. " <mail:description>%s</mail:description>\n" /* description */
  410. " <header:to>%s</header:to>\n" /* organizer */
  411. " <cal:location>%s</cal:location>\n" /* location */
  412. " <cal:dtstart dt:dt=\"dateTime.tz\">%s</cal:dtstart>\n" /* start */
  413. " <cal:dtend dt:dt=\"dateTime.tz\">%s</cal:dtend>\n" /* end */
  414. " <cal:instancetype dt:dt=\"int\">0</cal:instancetype>\n"
  415. " <cal:busystatus>%s</cal:busystatus>\n" /* busy_state (BUSY, FREE, BUSY_TENTATIVE) */
  416. " <cal:meetingstatus>CONFIRMED</cal:meetingstatus>\n"
  417. " <cal:alldayevent dt:dt=\"boolean\">0</cal:alldayevent>\n" /* XXX need to add event support for all day events */
  418. " <cal:responserequested dt:dt=\"boolean\">0</cal:responserequested>\n"
  419. " <mapi:finvited dt:dt=\"boolean\">1</mapi:finvited>\n"
  420. " </a:prop>\n"
  421. " </a:set>\n"
  422. "</a:propertyupdate>\n",
  423. ast_str_buffer(uid), ast_str_buffer(summary), ast_str_buffer(description), ast_str_buffer(organizer), ast_str_buffer(location), ast_str_buffer(start), ast_str_buffer(end), ast_str_buffer(busystate));
  424. ast_verb(0, "\n\n%s\n\n", ast_str_buffer(body));
  425. ast_str_set(&subdir, 0, "/Calendar/%s.eml", ast_str_buffer(uid));
  426. response = exchangecal_request(event->owner->tech_pvt, "PROPPATCH", body, subdir);
  427. ret = 0;
  428. write_cleanup:
  429. if (uid) {
  430. ast_free(uid);
  431. }
  432. if (summary) {
  433. ast_free(summary);
  434. }
  435. if (description) {
  436. ast_free(description);
  437. }
  438. if (organizer) {
  439. ast_free(organizer);
  440. }
  441. if (location) {
  442. ast_free(location);
  443. }
  444. if (start) {
  445. ast_free(start);
  446. }
  447. if (end) {
  448. ast_free(end);
  449. }
  450. if (busystate) {
  451. ast_free(busystate);
  452. }
  453. if (body) {
  454. ast_free(body);
  455. }
  456. if (response) {
  457. ast_free(response);
  458. }
  459. if (subdir) {
  460. ast_free(subdir);
  461. }
  462. return ret;
  463. }
  464. static struct ast_str *exchangecal_get_events_between(struct exchangecal_pvt *pvt, time_t start_time, time_t end_time)
  465. {
  466. struct ast_str *body, *response;
  467. char start[80], end[80];
  468. struct timeval tv = {0,};
  469. struct ast_tm tm;
  470. tv.tv_sec = start_time;
  471. ast_localtime(&tv, &tm, "UTC");
  472. ast_strftime(start, sizeof(start), "%Y/%m/%d %T", &tm);
  473. tv.tv_sec = end_time;
  474. ast_localtime(&tv, &tm, "UTC");
  475. ast_strftime(end, sizeof(end), "%Y/%m/%d %T", &tm);
  476. if (!(body = ast_str_create(512))) {
  477. ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
  478. return NULL;
  479. }
  480. ast_str_append(&body, 0,
  481. "<?xml version=\"1.0\"?>\n"
  482. "<g:searchrequest xmlns:g=\"DAV:\">\n"
  483. " <g:sql> SELECT \"urn:schemas:calendar:location\", \"urn:schemas:httpmail:subject\",\n"
  484. " \"urn:schemas:calendar:dtstart\", \"urn:schemas:calendar:dtend\",\n"
  485. " \"urn:schemas:calendar:busystatus\", \"urn:schemas:calendar:instancetype\",\n"
  486. " \"urn:schemas:calendar:uid\", \"urn:schemas:httpmail:textdescription\",\n"
  487. " \"urn:schemas:calendar:organizer\", \"urn:schemas:calendar:reminderoffset\"\n"
  488. " FROM Scope('SHALLOW TRAVERSAL OF \"%s/Calendar\"')\n"
  489. " WHERE NOT \"urn:schemas:calendar:instancetype\" = 1\n"
  490. " AND \"DAV:contentclass\" = 'urn:content-classes:appointment'\n"
  491. " AND NOT (\"urn:schemas:calendar:dtend\" &lt; '%s'\n"
  492. " OR \"urn:schemas:calendar:dtstart\" &gt; '%s')\n"
  493. " ORDER BY \"urn:schemas:calendar:dtstart\" ASC\n"
  494. " </g:sql>\n"
  495. "</g:searchrequest>\n", pvt->url, start, end);
  496. ast_debug(5, "Request:\n%s\n", ast_str_buffer(body));
  497. response = exchangecal_request(pvt, "SEARCH", body, NULL);
  498. ast_debug(5, "Response:\n%s\n", ast_str_buffer(response));
  499. ast_free(body);
  500. return response;
  501. }
  502. static int update_exchangecal(struct exchangecal_pvt *pvt)
  503. {
  504. struct xmlstate state;
  505. struct timeval now = ast_tvnow();
  506. time_t start, end;
  507. struct ast_str *response;
  508. iksparser *p;
  509. state.pvt = pvt;
  510. start = now.tv_sec;
  511. end = now.tv_sec + 60 * pvt->owner->timeframe;
  512. if (!(response = exchangecal_get_events_between(pvt, start, end))) {
  513. return -1;
  514. }
  515. p = iks_sax_new(&state, parse_tag, parse_cdata);
  516. iks_parse(p, ast_str_buffer(response), ast_str_strlen(response), 1);
  517. ast_calendar_merge_events(pvt->owner, pvt->events);
  518. ast_free(response);
  519. return 0;
  520. }
  521. static void *exchangecal_load_calendar(void *void_data)
  522. {
  523. struct exchangecal_pvt *pvt;
  524. const struct ast_config *cfg;
  525. struct ast_variable *v;
  526. struct ast_calendar *cal = void_data;
  527. ast_mutex_t refreshlock;
  528. if (!(cal && (cfg = ast_calendar_config_acquire()))) {
  529. ast_log(LOG_ERROR, "You must enable calendar support for res_exchangecal to load\n");
  530. return NULL;
  531. }
  532. if (ao2_trylock(cal)) {
  533. if (cal->unloading) {
  534. ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
  535. } else {
  536. ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
  537. }
  538. ast_calendar_config_release();
  539. return NULL;
  540. }
  541. if (!(pvt = ao2_alloc(sizeof(*pvt), exchangecal_destructor))) {
  542. ast_log(LOG_ERROR, "Could not allocate exchangecal_pvt structure for calendar: %s\n", cal->name);
  543. ast_calendar_config_release();
  544. return NULL;
  545. }
  546. pvt->owner = cal;
  547. if (!(pvt->events = ast_calendar_event_container_alloc())) {
  548. ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
  549. pvt = unref_exchangecal(pvt);
  550. ao2_unlock(cal);
  551. ast_calendar_config_release();
  552. return NULL;
  553. }
  554. if (ast_string_field_init(pvt, 32)) {
  555. ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
  556. pvt = unref_exchangecal(pvt);
  557. ao2_unlock(cal);
  558. ast_calendar_config_release();
  559. return NULL;
  560. }
  561. for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
  562. if (!strcasecmp(v->name, "url")) {
  563. ast_string_field_set(pvt, url, v->value);
  564. } else if (!strcasecmp(v->name, "user")) {
  565. ast_string_field_set(pvt, user, v->value);
  566. } else if (!strcasecmp(v->name, "secret")) {
  567. ast_string_field_set(pvt, secret, v->value);
  568. }
  569. }
  570. ast_calendar_config_release();
  571. if (ast_strlen_zero(pvt->url)) {
  572. ast_log(LOG_WARNING, "No URL was specified for Exchange calendar '%s' - skipping.\n", cal->name);
  573. pvt = unref_exchangecal(pvt);
  574. ao2_unlock(cal);
  575. return NULL;
  576. }
  577. if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
  578. ast_log(LOG_WARNING, "Could not parse url '%s' for Exchange calendar '%s' - skipping.\n", pvt->url, cal->name);
  579. pvt = unref_exchangecal(pvt);
  580. ao2_unlock(cal);
  581. return NULL;
  582. }
  583. if (pvt->uri.scheme == NULL) {
  584. pvt->uri.scheme = "http";
  585. }
  586. if (pvt->uri.port == 0) {
  587. pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
  588. }
  589. pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
  590. ne_redirect_register(pvt->session);
  591. ne_set_server_auth(pvt->session, auth_credentials, pvt);
  592. if (!strcasecmp(pvt->uri.scheme, "https")) {
  593. ne_ssl_trust_default_ca(pvt->session);
  594. }
  595. cal->tech_pvt = pvt;
  596. ast_mutex_init(&refreshlock);
  597. /* Load it the first time */
  598. update_exchangecal(pvt);
  599. ao2_unlock(cal);
  600. /* The only writing from another thread will be if unload is true */
  601. for (;;) {
  602. struct timeval tv = ast_tvnow();
  603. struct timespec ts = {0,};
  604. ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);
  605. ast_mutex_lock(&refreshlock);
  606. while (!pvt->owner->unloading) {
  607. if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
  608. break;
  609. }
  610. }
  611. ast_mutex_unlock(&refreshlock);
  612. if (pvt->owner->unloading) {
  613. ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
  614. return NULL;
  615. }
  616. ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);
  617. update_exchangecal(pvt);
  618. }
  619. return NULL;
  620. }
  621. static int load_module(void)
  622. {
  623. ne_sock_init();
  624. if (ast_calendar_register(&exchangecal_tech)) {
  625. ne_sock_exit();
  626. return AST_MODULE_LOAD_DECLINE;
  627. }
  628. return AST_MODULE_LOAD_SUCCESS;
  629. }
  630. static int unload_module(void)
  631. {
  632. ast_calendar_unregister(&exchangecal_tech);
  633. ne_sock_exit();
  634. return 0;
  635. }
  636. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk MS Exchange Calendar Integration",
  637. .load = load_module,
  638. .unload = unload_module,
  639. .load_pri = AST_MODPRI_DEVSTATE_PLUGIN,
  640. );