res_calendar_exchange.c 20 KB

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