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