| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include <libical/ical.h> |
| 34 |
|
| 35 |
#if ICAL_MAJOR_VERSION >= 4 |
| 36 |
#define ICAL_SPAN_CONST const |
| 37 |
#else |
| 38 |
#define ICAL_SPAN_CONST |
| 39 |
#endif |
| 40 |
#include <ne_session.h> |
| 41 |
#include <ne_uri.h> |
| 42 |
#include <ne_request.h> |
| 43 |
#include <ne_auth.h> |
| 44 |
#include <ne_redirect.h> |
| 45 |
#include <libxml/xmlreader.h> |
| 46 |
|
| 47 |
#include "asterisk/module.h" |
| 48 |
#include "asterisk/channel.h" |
| 49 |
#include "asterisk/calendar.h" |
| 50 |
#include "asterisk/lock.h" |
| 51 |
#include "asterisk/config.h" |
| 52 |
#include "asterisk/astobj2.h" |
| 53 |
|
| 54 |
static void *caldav_load_calendar(void *data); |
| 55 |
static void *unref_caldav(void *obj); |
| 56 |
static int caldav_write_event(struct ast_calendar_event *event); |
| 57 |
|
| 58 |
static struct ast_calendar_tech caldav_tech = { |
| 59 |
.type = "caldav", |
| 60 |
.description = "CalDAV calendars", |
| 61 |
.module = AST_MODULE, |
| 62 |
.load_calendar = caldav_load_calendar, |
| 63 |
.unref_calendar = unref_caldav, |
| 64 |
.write_event = caldav_write_event, |
| 65 |
}; |
| 66 |
|
| 67 |
struct caldav_pvt { |
| 68 |
AST_DECLARE_STRING_FIELDS( |
| 69 |
AST_STRING_FIELD(url); |
| 70 |
AST_STRING_FIELD(user); |
| 71 |
AST_STRING_FIELD(secret); |
| 72 |
); |
| 73 |
struct ast_calendar *owner; |
| 74 |
ne_uri uri; |
| 75 |
ne_session *session; |
| 76 |
struct ao2_container *events; |
| 77 |
}; |
| 78 |
|
| 79 |
static void caldav_destructor(void *obj) |
| 80 |
{ |
| 81 |
struct caldav_pvt *pvt = obj; |
| 82 |
|
| 83 |
ast_debug(1, "Destroying pvt for CalDAV calendar %s\n", pvt->owner->name); |
| 84 |
if (pvt->session) { |
| 85 |
ne_session_destroy(pvt->session); |
| 86 |
} |
| 87 |
ne_uri_free(&pvt->uri); |
| 88 |
ast_string_field_free_memory(pvt); |
| 89 |
|
| 90 |
ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL); |
| 91 |
|
| 92 |
ao2_ref(pvt->events, -1); |
| 93 |
} |
| 94 |
|
| 95 |
static void *unref_caldav(void *obj) |
| 96 |
{ |
| 97 |
struct caldav_pvt *pvt = obj; |
| 98 |
|
| 99 |
ao2_ref(pvt, -1); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
static int fetch_response_reader(void *data, const char *block, size_t len) |
| 104 |
{ |
| 105 |
struct ast_str **response = data; |
| 106 |
unsigned char *tmp; |
| 107 |
|
| 108 |
if (!(tmp = ast_malloc(len + 1))) { |
| 109 |
return -1; |
| 110 |
} |
| 111 |
memcpy(tmp, block, len); |
| 112 |
tmp[len] = '\0'; |
| 113 |
ast_str_append(response, 0, "%s", tmp); |
| 114 |
ast_free(tmp); |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret) |
| 120 |
{ |
| 121 |
struct caldav_pvt *pvt = userdata; |
| 122 |
|
| 123 |
if (attempts > 1) { |
| 124 |
ast_log(LOG_WARNING, "Invalid username or password for CalDAV calendar '%s'\n", pvt->owner->name); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
|
| 128 |
ne_strnzcpy(username, pvt->user, NE_ABUFSIZ); |
| 129 |
ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ); |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static int debug_response_handler(void *userdata, ne_request *req, const ne_status *st) |
| 135 |
{ |
| 136 |
if (st->code < 200 || st->code > 299) { |
| 137 |
if (st->code == 401) { |
| 138 |
ast_debug(1, "Got a 401 from the server but we expect this to happen when authenticating, %d: %s\n", st->code, st->reason_phrase); |
| 139 |
} else { |
| 140 |
ast_debug(1, "Unexpected response from server, %d: %s\n", st->code, st->reason_phrase); |
| 141 |
} |
| 142 |
return 0; |
| 143 |
} |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
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) |
| 148 |
{ |
| 149 |
struct ast_str *response; |
| 150 |
ne_request *req; |
| 151 |
int ret; |
| 152 |
char buf[1000]; |
| 153 |
|
| 154 |
if (!pvt) { |
| 155 |
ast_log(LOG_ERROR, "There is no private!\n"); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
if (!(response = ast_str_create(512))) { |
| 160 |
ast_log(LOG_ERROR, "Could not allocate memory for response.\n"); |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : ""); |
| 165 |
|
| 166 |
req = ne_request_create(pvt->session, method, buf); |
| 167 |
ne_add_response_body_reader(req, debug_response_handler, fetch_response_reader, &response); |
| 168 |
ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body)); |
| 169 |
ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type); |
| 170 |
ne_add_request_header(req, "Depth", "1"); |
| 171 |
|
| 172 |
ret = ne_request_dispatch(req); |
| 173 |
ne_request_destroy(req); |
| 174 |
|
| 175 |
if (ret != NE_OK) { |
| 176 |
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)); |
| 177 |
ast_free(response); |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
return response; |
| 182 |
} |
| 183 |
|
| 184 |
static int caldav_write_event(struct ast_calendar_event *event) |
| 185 |
{ |
| 186 |
struct caldav_pvt *pvt; |
| 187 |
struct ast_str *body = NULL, *response = NULL, *subdir = NULL; |
| 188 |
icalcomponent *calendar, *icalevent; |
| 189 |
icaltimezone *utc = icaltimezone_get_utc_timezone(); |
| 190 |
int ret = -1; |
| 191 |
|
| 192 |
if (!event) { |
| 193 |
ast_log(LOG_WARNING, "No event passed!\n"); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
if (!(event->start && event->end)) { |
| 198 |
ast_log(LOG_WARNING, "The event must contain a start and an end\n"); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
if (!(body = ast_str_create(512)) || |
| 202 |
!(subdir = ast_str_create(32))) { |
| 203 |
ast_log(LOG_ERROR, "Could not allocate memory for request!\n"); |
| 204 |
goto write_cleanup; |
| 205 |
} |
| 206 |
|
| 207 |
pvt = event->owner->tech_pvt; |
| 208 |
|
| 209 |
if (ast_strlen_zero(event->uid)) { |
| 210 |
unsigned short val[8]; |
| 211 |
int x; |
| 212 |
for (x = 0; x < 8; x++) { |
| 213 |
val[x] = ast_random(); |
| 214 |
} |
| 215 |
ast_string_field_build(event, uid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", |
| 216 |
(unsigned)val[0], (unsigned)val[1], (unsigned)val[2], |
| 217 |
(unsigned)val[3], (unsigned)val[4], (unsigned)val[5], |
| 218 |
(unsigned)val[6], (unsigned)val[7]); |
| 219 |
} |
| 220 |
|
| 221 |
calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT); |
| 222 |
icalcomponent_add_property(calendar, icalproperty_new_version("2.0")); |
| 223 |
icalcomponent_add_property(calendar, icalproperty_new_prodid("-//Digium, Inc.//res_caldav//EN")); |
| 224 |
|
| 225 |
icalevent = icalcomponent_new(ICAL_VEVENT_COMPONENT); |
| 226 |
icalcomponent_add_property(icalevent, icalproperty_new_dtstamp(icaltime_current_time_with_zone(utc))); |
| 227 |
icalcomponent_add_property(icalevent, icalproperty_new_uid(event->uid)); |
| 228 |
icalcomponent_add_property(icalevent, icalproperty_new_dtstart(icaltime_from_timet_with_zone(event->start, 0, utc))); |
| 229 |
icalcomponent_add_property(icalevent, icalproperty_new_dtend(icaltime_from_timet_with_zone(event->end, 0, utc))); |
| 230 |
if (!ast_strlen_zero(event->organizer)) { |
| 231 |
icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->organizer)); |
| 232 |
} |
| 233 |
if (!ast_strlen_zero(event->summary)) { |
| 234 |
icalcomponent_add_property(icalevent, icalproperty_new_summary(event->summary)); |
| 235 |
} |
| 236 |
if (!ast_strlen_zero(event->description)) { |
| 237 |
icalcomponent_add_property(icalevent, icalproperty_new_description(event->description)); |
| 238 |
} |
| 239 |
if (!ast_strlen_zero(event->location)) { |
| 240 |
icalcomponent_add_property(icalevent, icalproperty_new_location(event->location)); |
| 241 |
} |
| 242 |
if (!ast_strlen_zero(event->categories)) { |
| 243 |
icalcomponent_add_property(icalevent, icalproperty_new_categories(event->categories)); |
| 244 |
} |
| 245 |
if (event->priority > 0) { |
| 246 |
icalcomponent_add_property(icalevent, icalproperty_new_priority(event->priority)); |
| 247 |
} |
| 248 |
|
| 249 |
switch (event->busy_state) { |
| 250 |
case AST_CALENDAR_BS_BUSY: |
| 251 |
icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED)); |
| 252 |
break; |
| 253 |
|
| 254 |
case AST_CALENDAR_BS_BUSY_TENTATIVE: |
| 255 |
icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE)); |
| 256 |
break; |
| 257 |
|
| 258 |
default: |
| 259 |
icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE)); |
| 260 |
} |
| 261 |
|
| 262 |
icalcomponent_add_component(calendar, icalevent); |
| 263 |
|
| 264 |
ast_str_append(&body, 0, "%s", icalcomponent_as_ical_string(calendar)); |
| 265 |
ast_str_set(&subdir, 0, "%s%s.ics", pvt->url[strlen(pvt->url) - 1] == '/' ? "" : "/", event->uid); |
| 266 |
|
| 267 |
if ((response = caldav_request(pvt, "PUT", body, subdir, "text/calendar"))) { |
| 268 |
ret = 0; |
| 269 |
} |
| 270 |
|
| 271 |
write_cleanup: |
| 272 |
if (body) { |
| 273 |
ast_free(body); |
| 274 |
} |
| 275 |
if (response) { |
| 276 |
ast_free(response); |
| 277 |
} |
| 278 |
if (subdir) { |
| 279 |
ast_free(subdir); |
| 280 |
} |
| 281 |
|
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
static struct ast_str *caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time) |
| 286 |
{ |
| 287 |
struct ast_str *body, *response; |
| 288 |
icaltimezone *utc = icaltimezone_get_utc_timezone(); |
| 289 |
icaltimetype start, end; |
| 290 |
const char *start_str, *end_str; |
| 291 |
|
| 292 |
if (!(body = ast_str_create(512))) { |
| 293 |
ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n"); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
start = icaltime_from_timet_with_zone(start_time, 0, utc); |
| 298 |
end = icaltime_from_timet_with_zone(end_time, 0, utc); |
| 299 |
start_str = icaltime_as_ical_string(start); |
| 300 |
end_str = icaltime_as_ical_string(end); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
ast_str_append(&body, 0, |
| 307 |
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" |
| 308 |
"<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n" |
| 309 |
" <D:prop>\n" |
| 310 |
" <C:calendar-data>\n" |
| 311 |
" <C:expand start=\"%s\" end=\"%s\"/>\n" |
| 312 |
" </C:calendar-data>\n" |
| 313 |
" </D:prop>\n" |
| 314 |
" <C:filter>\n" |
| 315 |
" <C:comp-filter name=\"VCALENDAR\">\n" |
| 316 |
" <C:comp-filter name=\"VEVENT\">\n" |
| 317 |
" <C:time-range start=\"%s\" end=\"%s\"/>\n" |
| 318 |
" </C:comp-filter>\n" |
| 319 |
" </C:comp-filter>\n" |
| 320 |
" </C:filter>\n" |
| 321 |
"</C:calendar-query>\n", start_str, end_str, start_str, end_str); |
| 322 |
|
| 323 |
response = caldav_request(pvt, "REPORT", body, NULL, NULL); |
| 324 |
ast_free(body); |
| 325 |
if (response && !ast_str_strlen(response)) { |
| 326 |
ast_free(response); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
return response; |
| 331 |
} |
| 332 |
|
| 333 |
static time_t icalfloat_to_timet(icaltimetype time) |
| 334 |
{ |
| 335 |
struct ast_tm tm = {0,}; |
| 336 |
struct timeval tv; |
| 337 |
|
| 338 |
tm.tm_mday = time.day; |
| 339 |
tm.tm_mon = time.month - 1; |
| 340 |
tm.tm_year = time.year - 1900; |
| 341 |
tm.tm_hour = time.hour; |
| 342 |
tm.tm_min = time.minute; |
| 343 |
tm.tm_sec = time.second; |
| 344 |
tm.tm_isdst = -1; |
| 345 |
tv = ast_mktime(&tm, NULL); |
| 346 |
|
| 347 |
return tv.tv_sec; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static void caldav_add_event(icalcomponent *comp, ICAL_SPAN_CONST struct icaltime_span *span, void *data) |
| 359 |
{ |
| 360 |
struct caldav_pvt *pvt = data; |
| 361 |
struct ast_calendar_event *event; |
| 362 |
icaltimezone *utc = icaltimezone_get_utc_timezone(); |
| 363 |
icaltimetype start, end, tmp; |
| 364 |
icalcomponent *valarm; |
| 365 |
icalproperty *prop; |
| 366 |
struct icaltriggertype trigger; |
| 367 |
|
| 368 |
if (!(pvt && pvt->owner)) { |
| 369 |
ast_log(LOG_ERROR, "Require a private structure with an owner\n"); |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
if (!(event = ast_calendar_event_alloc(pvt->owner))) { |
| 374 |
ast_log(LOG_ERROR, "Could not allocate an event!\n"); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
start = icalcomponent_get_dtstart(comp); |
| 379 |
end = icalcomponent_get_dtend(comp); |
| 380 |
|
| 381 |
event->start = icaltime_get_tzid(start) ? span->start : icalfloat_to_timet(start); |
| 382 |
event->end = icaltime_get_tzid(end) ? span->end : icalfloat_to_timet(end); |
| 383 |
event->busy_state = span->is_busy ? AST_CALENDAR_BS_BUSY : AST_CALENDAR_BS_FREE; |
| 384 |
|
| 385 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) { |
| 386 |
ast_string_field_set(event, summary, icalproperty_get_value_as_string(prop)); |
| 387 |
} |
| 388 |
|
| 389 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) { |
| 390 |
ast_string_field_set(event, description, icalproperty_get_value_as_string(prop)); |
| 391 |
} |
| 392 |
|
| 393 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) { |
| 394 |
ast_string_field_set(event, organizer, icalproperty_get_value_as_string(prop)); |
| 395 |
} |
| 396 |
|
| 397 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) { |
| 398 |
ast_string_field_set(event, location, icalproperty_get_value_as_string(prop)); |
| 399 |
} |
| 400 |
|
| 401 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY))) { |
| 402 |
ast_string_field_set(event, categories, icalproperty_get_value_as_string(prop)); |
| 403 |
} |
| 404 |
|
| 405 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_PRIORITY_PROPERTY))) { |
| 406 |
event->priority = icalvalue_get_integer(icalproperty_get_value(prop)); |
| 407 |
} |
| 408 |
|
| 409 |
if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) { |
| 410 |
ast_string_field_set(event, uid, icalproperty_get_value_as_string(prop)); |
| 411 |
} else { |
| 412 |
ast_log(LOG_WARNING, "No UID found, but one is required. Generating, but updates may not be accurate\n"); |
| 413 |
if (!ast_strlen_zero(event->summary)) { |
| 414 |
ast_string_field_set(event, uid, event->summary); |
| 415 |
} else { |
| 416 |
char tmp[AST_TIME_T_LEN]; |
| 417 |
ast_time_t_to_string(event->start, tmp, sizeof(tmp)); |
| 418 |
ast_string_field_set(event, uid, tmp); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
for (prop = icalcomponent_get_first_property(comp, ICAL_ATTENDEE_PROPERTY); |
| 424 |
prop; prop = icalcomponent_get_next_property(comp, ICAL_ATTENDEE_PROPERTY)) { |
| 425 |
struct ast_calendar_attendee *attendee; |
| 426 |
const char *data; |
| 427 |
|
| 428 |
if (!(attendee = ast_calloc(1, sizeof(*attendee)))) { |
| 429 |
event = ast_calendar_unref_event(event); |
| 430 |
return; |
| 431 |
} |
| 432 |
data = icalproperty_get_attendee(prop); |
| 433 |
if (ast_strlen_zero(data)) { |
| 434 |
ast_free(attendee); |
| 435 |
continue; |
| 436 |
} |
| 437 |
attendee->data = ast_strdup(data); |
| 438 |
AST_LIST_INSERT_TAIL(&event->attendees, attendee, next); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) { |
| 446 |
ao2_link(pvt->events, event); |
| 447 |
event = ast_calendar_unref_event(event); |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) { |
| 452 |
ast_log(LOG_WARNING, "VALARM has no TRIGGER, skipping!\n"); |
| 453 |
ao2_link(pvt->events, event); |
| 454 |
event = ast_calendar_unref_event(event); |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
trigger = icalproperty_get_trigger(prop); |
| 459 |
|
| 460 |
if (icaltriggertype_is_null_trigger(trigger)) { |
| 461 |
ast_log(LOG_WARNING, "Bad TRIGGER for VALARM, skipping!\n"); |
| 462 |
ao2_link(pvt->events, event); |
| 463 |
event = ast_calendar_unref_event(event); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
if (!icaltime_is_null_time(trigger.time)) { |
| 468 |
tmp = icaltime_convert_to_zone(trigger.time, utc); |
| 469 |
event->alarm = icaltime_as_timet_with_zone(tmp, utc); |
| 470 |
} else { |
| 471 |
|
| 472 |
|
| 473 |
#if ICAL_MAJOR_VERSION >= 4 |
| 474 |
tmp = start; |
| 475 |
int sign = trigger.duration.is_neg ? -1 : 1; |
| 476 |
icaltime_adjust(&tmp, |
| 477 |
sign * (trigger.duration.days + trigger.duration.weeks * 7), |
| 478 |
sign * trigger.duration.hours, |
| 479 |
sign * trigger.duration.minutes, |
| 480 |
sign * trigger.duration.seconds); |
| 481 |
#else |
| 482 |
tmp = icaltime_add(start, trigger.duration); |
| 483 |
#endif |
| 484 |
event->alarm = icaltime_as_timet_with_zone(tmp, icaltime_get_timezone(start)); |
| 485 |
} |
| 486 |
|
| 487 |
ao2_link(pvt->events, event); |
| 488 |
event = ast_calendar_unref_event(event); |
| 489 |
|
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
struct xmlstate { |
| 494 |
int in_caldata; |
| 495 |
struct caldav_pvt *pvt; |
| 496 |
struct ast_str *cdata; |
| 497 |
time_t start; |
| 498 |
time_t end; |
| 499 |
}; |
| 500 |
|
| 501 |
static const xmlChar *caldav_node_localname = BAD_CAST "calendar-data"; |
| 502 |
static const xmlChar *caldav_node_nsuri = BAD_CAST "urn:ietf:params:xml:ns:caldav"; |
| 503 |
|
| 504 |
static void handle_start_element(xmlTextReaderPtr reader, struct xmlstate *state) |
| 505 |
{ |
| 506 |
const xmlChar *localname = xmlTextReaderConstLocalName(reader); |
| 507 |
const xmlChar *uri = xmlTextReaderConstNamespaceUri(reader); |
| 508 |
|
| 509 |
if (!xmlStrEqual(localname, caldav_node_localname) || !xmlStrEqual(uri, caldav_node_nsuri)) { |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
state->in_caldata = 1; |
| 514 |
ast_str_reset(state->cdata); |
| 515 |
} |
| 516 |
|
| 517 |
static void handle_end_element(xmlTextReaderPtr reader, struct xmlstate *state) |
| 518 |
{ |
| 519 |
struct icaltimetype start, end; |
| 520 |
icaltimezone *utc = icaltimezone_get_utc_timezone(); |
| 521 |
icalcomponent *iter; |
| 522 |
icalcomponent *comp; |
| 523 |
const xmlChar *localname = xmlTextReaderConstLocalName(reader); |
| 524 |
const xmlChar *uri = xmlTextReaderConstNamespaceUri(reader); |
| 525 |
|
| 526 |
if (!xmlStrEqual(localname, caldav_node_localname) || !xmlStrEqual(uri, caldav_node_nsuri)) { |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
state->in_caldata = 0; |
| 531 |
if (!(state->cdata && ast_str_strlen(state->cdata))) { |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
start = icaltime_from_timet_with_zone(state->start, 0, utc); |
| 537 |
end = icaltime_from_timet_with_zone(state->end, 0, utc); |
| 538 |
comp = icalparser_parse_string(ast_str_buffer(state->cdata)); |
| 539 |
|
| 540 |
for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT); |
| 541 |
iter; |
| 542 |
iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT)) |
| 543 |
{ |
| 544 |
icalcomponent_foreach_recurrence(iter, start, end, caldav_add_event, state->pvt); |
| 545 |
} |
| 546 |
|
| 547 |
icalcomponent_free(comp); |
| 548 |
} |
| 549 |
|
| 550 |
static void handle_characters(xmlTextReaderPtr reader, struct xmlstate *state) |
| 551 |
{ |
| 552 |
xmlChar *text; |
| 553 |
|
| 554 |
if (!state->in_caldata) { |
| 555 |
return; |
| 556 |
} |
| 557 |
|
| 558 |
text = xmlTextReaderValue(reader); |
| 559 |
if (text) { |
| 560 |
ast_str_append(&state->cdata, 0, "%s", text); |
| 561 |
xmlFree(text); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
static void parse_error_handler(void *arg, const char *msg, |
| 566 |
xmlParserSeverities severity, xmlTextReaderLocatorPtr locator) |
| 567 |
{ |
| 568 |
switch (severity) { |
| 569 |
case XML_PARSER_SEVERITY_VALIDITY_WARNING: |
| 570 |
case XML_PARSER_SEVERITY_WARNING: |
| 571 |
ast_log(LOG_WARNING, "While parsing CalDAV response at line %d: %s\n", |
| 572 |
xmlTextReaderLocatorLineNumber(locator), |
| 573 |
msg); |
| 574 |
break; |
| 575 |
case XML_PARSER_SEVERITY_VALIDITY_ERROR: |
| 576 |
case XML_PARSER_SEVERITY_ERROR: |
| 577 |
default: |
| 578 |
ast_log(LOG_ERROR, "While parsing CalDAV response at line %d: %s\n", |
| 579 |
xmlTextReaderLocatorLineNumber(locator), |
| 580 |
msg); |
| 581 |
break; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
static int update_caldav(struct caldav_pvt *pvt) |
| 586 |
{ |
| 587 |
struct timeval now = ast_tvnow(); |
| 588 |
time_t start, end; |
| 589 |
struct ast_str *response; |
| 590 |
xmlTextReaderPtr reader; |
| 591 |
struct xmlstate state = { |
| 592 |
.in_caldata = 0, |
| 593 |
.pvt = pvt |
| 594 |
}; |
| 595 |
|
| 596 |
start = now.tv_sec; |
| 597 |
end = now.tv_sec + 60 * pvt->owner->timeframe; |
| 598 |
if (!(response = caldav_get_events_between(pvt, start, end))) { |
| 599 |
return -1; |
| 600 |
} |
| 601 |
|
| 602 |
if (!(state.cdata = ast_str_create(512))) { |
| 603 |
ast_free(response); |
| 604 |
return -1; |
| 605 |
} |
| 606 |
|
| 607 |
state.start = start; |
| 608 |
state.end = end; |
| 609 |
|
| 610 |
reader = xmlReaderForMemory( |
| 611 |
ast_str_buffer(response), |
| 612 |
ast_str_strlen(response), |
| 613 |
NULL, |
| 614 |
NULL, |
| 615 |
0); |
| 616 |
|
| 617 |
if (reader) { |
| 618 |
int res; |
| 619 |
|
| 620 |
xmlTextReaderSetErrorHandler(reader, parse_error_handler, NULL); |
| 621 |
|
| 622 |
res = xmlTextReaderRead(reader); |
| 623 |
while (res == 1) { |
| 624 |
int node_type = xmlTextReaderNodeType(reader); |
| 625 |
switch (node_type) { |
| 626 |
case XML_READER_TYPE_ELEMENT: |
| 627 |
handle_start_element(reader, &state); |
| 628 |
break; |
| 629 |
case XML_READER_TYPE_END_ELEMENT: |
| 630 |
handle_end_element(reader, &state); |
| 631 |
break; |
| 632 |
case XML_READER_TYPE_TEXT: |
| 633 |
case XML_READER_TYPE_CDATA: |
| 634 |
handle_characters(reader, &state); |
| 635 |
break; |
| 636 |
default: |
| 637 |
break; |
| 638 |
} |
| 639 |
res = xmlTextReaderRead(reader); |
| 640 |
} |
| 641 |
xmlFreeTextReader(reader); |
| 642 |
} |
| 643 |
|
| 644 |
ast_calendar_merge_events(pvt->owner, pvt->events); |
| 645 |
|
| 646 |
ast_free(response); |
| 647 |
ast_free(state.cdata); |
| 648 |
|
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert) |
| 653 |
{ |
| 654 |
|
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
static void *caldav_load_calendar(void *void_data) |
| 659 |
{ |
| 660 |
struct caldav_pvt *pvt; |
| 661 |
const struct ast_config *cfg; |
| 662 |
struct ast_variable *v; |
| 663 |
struct ast_calendar *cal = void_data; |
| 664 |
ast_mutex_t refreshlock; |
| 665 |
|
| 666 |
if (!(cal && (cfg = ast_calendar_config_acquire()))) { |
| 667 |
ast_log(LOG_ERROR, "You must enable calendar support for res_caldav to load\n"); |
| 668 |
return NULL; |
| 669 |
} |
| 670 |
|
| 671 |
if (ao2_trylock(cal)) { |
| 672 |
if (cal->unloading) { |
| 673 |
ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n"); |
| 674 |
} else { |
| 675 |
ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n"); |
| 676 |
} |
| 677 |
ast_calendar_config_release(); |
| 678 |
return NULL; |
| 679 |
} |
| 680 |
|
| 681 |
if (!(pvt = ao2_alloc(sizeof(*pvt), caldav_destructor))) { |
| 682 |
ast_log(LOG_ERROR, "Could not allocate caldav_pvt structure for calendar: %s\n", cal->name); |
| 683 |
ast_calendar_config_release(); |
| 684 |
return NULL; |
| 685 |
} |
| 686 |
|
| 687 |
pvt->owner = cal; |
| 688 |
|
| 689 |
if (!(pvt->events = ast_calendar_event_container_alloc())) { |
| 690 |
ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name); |
| 691 |
pvt = unref_caldav(pvt); |
| 692 |
ao2_unlock(cal); |
| 693 |
ast_calendar_config_release(); |
| 694 |
return NULL; |
| 695 |
} |
| 696 |
|
| 697 |
if (ast_string_field_init(pvt, 32)) { |
| 698 |
ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name); |
| 699 |
pvt = unref_caldav(pvt); |
| 700 |
ao2_unlock(cal); |
| 701 |
ast_calendar_config_release(); |
| 702 |
return NULL; |
| 703 |
} |
| 704 |
|
| 705 |
for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) { |
| 706 |
if (!strcasecmp(v->name, "url")) { |
| 707 |
ast_string_field_set(pvt, url, v->value); |
| 708 |
} else if (!strcasecmp(v->name, "user")) { |
| 709 |
ast_string_field_set(pvt, user, v->value); |
| 710 |
} else if (!strcasecmp(v->name, "secret")) { |
| 711 |
ast_string_field_set(pvt, secret, v->value); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
ast_calendar_config_release(); |
| 716 |
|
| 717 |
if (ast_strlen_zero(pvt->url)) { |
| 718 |
ast_log(LOG_WARNING, "No URL was specified for CalDAV calendar '%s' - skipping.\n", cal->name); |
| 719 |
pvt = unref_caldav(pvt); |
| 720 |
ao2_unlock(cal); |
| 721 |
return NULL; |
| 722 |
} |
| 723 |
|
| 724 |
if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) { |
| 725 |
ast_log(LOG_WARNING, "Could not parse url '%s' for CalDAV calendar '%s' - skipping.\n", pvt->url, cal->name); |
| 726 |
pvt = unref_caldav(pvt); |
| 727 |
ao2_unlock(cal); |
| 728 |
return NULL; |
| 729 |
} |
| 730 |
|
| 731 |
if (pvt->uri.scheme == NULL) { |
| 732 |
pvt->uri.scheme = "http"; |
| 733 |
} |
| 734 |
|
| 735 |
if (pvt->uri.port == 0) { |
| 736 |
pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme); |
| 737 |
} |
| 738 |
|
| 739 |
pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port); |
| 740 |
ne_redirect_register(pvt->session); |
| 741 |
ne_set_server_auth(pvt->session, auth_credentials, pvt); |
| 742 |
if (!strcasecmp(pvt->uri.scheme, "https")) { |
| 743 |
ne_ssl_trust_default_ca(pvt->session); |
| 744 |
ne_ssl_set_verify(pvt->session, verify_cert, NULL); |
| 745 |
} |
| 746 |
|
| 747 |
cal->tech_pvt = pvt; |
| 748 |
|
| 749 |
ast_mutex_init(&refreshlock); |
| 750 |
|
| 751 |
|
| 752 |
update_caldav(pvt); |
| 753 |
|
| 754 |
ao2_unlock(cal); |
| 755 |
|
| 756 |
|
| 757 |
for (;;) { |
| 758 |
struct timeval tv = ast_tvnow(); |
| 759 |
struct timespec ts = {0,}; |
| 760 |
|
| 761 |
ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh); |
| 762 |
|
| 763 |
ast_mutex_lock(&refreshlock); |
| 764 |
while (!pvt->owner->unloading) { |
| 765 |
if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) { |
| 766 |
break; |
| 767 |
} |
| 768 |
} |
| 769 |
ast_mutex_unlock(&refreshlock); |
| 770 |
|
| 771 |
if (pvt->owner->unloading) { |
| 772 |
ast_debug(10, "Skipping refresh since we got a shutdown signal\n"); |
| 773 |
return NULL; |
| 774 |
} |
| 775 |
|
| 776 |
ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh); |
| 777 |
|
| 778 |
update_caldav(pvt); |
| 779 |
} |
| 780 |
|
| 781 |
return NULL; |
| 782 |
} |
| 783 |
|
| 784 |
static int load_module(void) |
| 785 |
{ |
| 786 |
ne_sock_init(); |
| 787 |
if (ast_calendar_register(&caldav_tech)) { |
| 788 |
ne_sock_exit(); |
| 789 |
return AST_MODULE_LOAD_DECLINE; |
| 790 |
} |
| 791 |
|
| 792 |
return AST_MODULE_LOAD_SUCCESS; |
| 793 |
} |
| 794 |
|
| 795 |
static int unload_module(void) |
| 796 |
{ |
| 797 |
ast_calendar_unregister(&caldav_tech); |
| 798 |
ne_sock_exit(); |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk CalDAV Calendar Integration", |
| 803 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 804 |
.load = load_module, |
| 805 |
.unload = unload_module, |
| 806 |
.load_pri = AST_MODPRI_DEVSTATE_PLUGIN, |
| 807 |
.requires = "res_calendar", |
| 808 |
); |