Wed Jan 8 2020 09:49:49

Asterisk developer's documentation


res_calendar_caldav.c
Go to the documentation of this file.
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 
19 /*! \file
20  * \brief Resource for handling CalDAV calendars
21  */
22 
23 /*** MODULEINFO
24  <depend>neon</depend>
25  <depend>ical</depend>
26  <depend>libxml2</depend>
27  <support_level>core</support_level>
28 ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
33 
34 #include <libical/ical.h>
35 #include <ne_session.h>
36 #include <ne_uri.h>
37 #include <ne_request.h>
38 #include <ne_auth.h>
39 #include <ne_redirect.h>
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
42 
43 #include "asterisk/module.h"
44 #include "asterisk/calendar.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/config.h"
47 #include "asterisk/astobj2.h"
48 
49 static void *caldav_load_calendar(void *data);
50 static void *unref_caldav(void *obj);
51 static int caldav_write_event(struct ast_calendar_event *event);
52 
53 static struct ast_calendar_tech caldav_tech = {
54  .type = "caldav",
55  .description = "CalDAV calendars",
56  .module = AST_MODULE,
57  .load_calendar = caldav_load_calendar,
58  .unref_calendar = unref_caldav,
59  .write_event = caldav_write_event,
60 };
61 
62 struct caldav_pvt {
67  );
69  ne_uri uri;
70  ne_session *session;
72 };
73 
74 static void caldav_destructor(void *obj)
75 {
76  struct caldav_pvt *pvt = obj;
77 
78  ast_debug(1, "Destroying pvt for CalDAV calendar %s\n", pvt->owner->name);
79  if (pvt->session) {
80  ne_session_destroy(pvt->session);
81  }
83 
84  ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
85 
86  ao2_ref(pvt->events, -1);
87 }
88 
89 static void *unref_caldav(void *obj)
90 {
91  struct caldav_pvt *pvt = obj;
92 
93  ao2_ref(pvt, -1);
94  return NULL;
95 }
96 
97 static int fetch_response_reader(void *data, const char *block, size_t len)
98 {
99  struct ast_str **response = data;
100  unsigned char *tmp;
101 
102  if (!(tmp = ast_malloc(len + 1))) {
103  return -1;
104  }
105  memcpy(tmp, block, len);
106  tmp[len] = '\0';
107  ast_str_append(response, 0, "%s", tmp);
108  ast_free(tmp);
109 
110  return 0;
111 }
112 
113 static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
114 {
115  struct caldav_pvt *pvt = userdata;
116 
117  if (attempts > 1) {
118  ast_log(LOG_WARNING, "Invalid username or password for CalDAV calendar '%s'\n", pvt->owner->name);
119  return -1;
120  }
121 
122  ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
123  ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
124 
125  return 0;
126 }
127 
128 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)
129 {
130  struct ast_str *response;
131  ne_request *req;
132  int ret;
133  char buf[1000];
134 
135  if (!pvt) {
136  ast_log(LOG_ERROR, "There is no private!\n");
137  return NULL;
138  }
139 
140  if (!(response = ast_str_create(512))) {
141  ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
142  return NULL;
143  }
144 
145  snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : "");
146 
147  req = ne_request_create(pvt->session, method, buf);
148  ne_add_response_body_reader(req, ne_accept_2xx, fetch_response_reader, &response);
149  ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body));
150  ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type);
151 
152  ret = ne_request_dispatch(req);
153  ne_request_destroy(req);
154 
155  if (ret != NE_OK || !ast_str_strlen(response)) {
156  if (ret != NE_OK) {
157  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));
158  }
159  ast_free(response);
160  return NULL;
161  }
162 
163  return response;
164 }
165 
166 static int caldav_write_event(struct ast_calendar_event *event)
167 {
168  struct caldav_pvt *pvt;
169  struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
170  icalcomponent *calendar, *icalevent;
171  icaltimezone *utc = icaltimezone_get_utc_timezone();
172  int ret = -1;
173 
174  if (!event) {
175  ast_log(LOG_WARNING, "No event passed!\n");
176  return -1;
177  }
178 
179  if (!(event->start && event->end)) {
180  ast_log(LOG_WARNING, "The event must contain a start and an end\n");
181  return -1;
182  }
183  if (!(body = ast_str_create(512)) ||
184  !(subdir = ast_str_create(32))) {
185  ast_log(LOG_ERROR, "Could not allocate memory for request!\n");
186  goto write_cleanup;
187  }
188 
189  pvt = event->owner->tech_pvt;
190 
191  if (ast_strlen_zero(event->uid)) {
192  unsigned short val[8];
193  int x;
194  for (x = 0; x < 8; x++) {
195  val[x] = ast_random();
196  }
197  ast_string_field_build(event, uid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
198  (unsigned)val[0], (unsigned)val[1], (unsigned)val[2],
199  (unsigned)val[3], (unsigned)val[4], (unsigned)val[5],
200  (unsigned)val[6], (unsigned)val[7]);
201  }
202 
203  calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
204  icalcomponent_add_property(calendar, icalproperty_new_version("2.0"));
205  icalcomponent_add_property(calendar, icalproperty_new_prodid("-//Digium, Inc.//res_caldav//EN"));
206 
207  icalevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
208  icalcomponent_add_property(icalevent, icalproperty_new_dtstamp(icaltime_current_time_with_zone(utc)));
209  icalcomponent_add_property(icalevent, icalproperty_new_uid(event->uid));
210  icalcomponent_add_property(icalevent, icalproperty_new_dtstart(icaltime_from_timet_with_zone(event->start, 0, utc)));
211  icalcomponent_add_property(icalevent, icalproperty_new_dtend(icaltime_from_timet_with_zone(event->end, 0, utc)));
212  if (!ast_strlen_zero(event->organizer)) {
213  icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->organizer));
214  }
215  if (!ast_strlen_zero(event->summary)) {
216  icalcomponent_add_property(icalevent, icalproperty_new_summary(event->summary));
217  }
218  if (!ast_strlen_zero(event->description)) {
219  icalcomponent_add_property(icalevent, icalproperty_new_description(event->description));
220  }
221  if (!ast_strlen_zero(event->location)) {
222  icalcomponent_add_property(icalevent, icalproperty_new_location(event->location));
223  }
224  if (!ast_strlen_zero(event->categories)) {
225  icalcomponent_add_property(icalevent, icalproperty_new_categories(event->categories));
226  }
227  if (event->priority > 0) {
228  icalcomponent_add_property(icalevent, icalproperty_new_priority(event->priority));
229  }
230 
231  switch (event->busy_state) {
233  icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));
234  break;
235 
237  icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE));
238  break;
239 
240  default:
241  icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE));
242  }
243 
244  icalcomponent_add_component(calendar, icalevent);
245 
246  ast_str_append(&body, 0, "%s", icalcomponent_as_ical_string(calendar));
247  ast_str_set(&subdir, 0, "%s%s.ics", pvt->url[strlen(pvt->url) - 1] == '/' ? "" : "/", event->uid);
248 
249  response = caldav_request(pvt, "PUT", body, subdir, "text/calendar");
250 
251  ret = 0;
252 
253 write_cleanup:
254  if (body) {
255  ast_free(body);
256  }
257  if (response) {
258  ast_free(response);
259  }
260  if (subdir) {
261  ast_free(subdir);
262  }
263 
264  return ret;
265 }
266 
267 static struct ast_str *caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
268 {
269  struct ast_str *body, *response;
270  icaltimezone *utc = icaltimezone_get_utc_timezone();
271  icaltimetype start, end;
272  const char *start_str, *end_str;
273 
274  if (!(body = ast_str_create(512))) {
275  ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
276  return NULL;
277  }
278 
279  start = icaltime_from_timet_with_zone(start_time, 0, utc);
280  end = icaltime_from_timet_with_zone(end_time, 0, utc);
281  start_str = icaltime_as_ical_string(start);
282  end_str = icaltime_as_ical_string(end);
283 
284  /* If I was really being efficient, I would store a collection of event URIs and etags,
285  * first doing a query of just the etag and seeing if anything had changed. If it had,
286  * then I would do a request for each of the events that had changed, and only bother
287  * updating those. Oh well. */
288  ast_str_append(&body, 0,
289  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
290  "<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n"
291  " <D:prop>\n"
292  " <C:calendar-data>\n"
293  " <C:expand start=\"%s\" end=\"%s\"/>\n"
294  " </C:calendar-data>\n"
295  " </D:prop>\n"
296  " <C:filter>\n"
297  " <C:comp-filter name=\"VCALENDAR\">\n"
298  " <C:comp-filter name=\"VEVENT\">\n"
299  " <C:time-range start=\"%s\" end=\"%s\"/>\n"
300  " </C:comp-filter>\n"
301  " </C:comp-filter>\n"
302  " </C:filter>\n"
303  "</C:calendar-query>\n", start_str, end_str, start_str, end_str);
304 
305  response = caldav_request(pvt, "REPORT", body, NULL, NULL);
306  ast_free(body);
307 
308  return response;
309 }
310 
311 static time_t icalfloat_to_timet(icaltimetype time)
312 {
313  struct ast_tm tm = {0,};
314  struct timeval tv;
315 
316  tm.tm_mday = time.day;
317  tm.tm_mon = time.month - 1;
318  tm.tm_year = time.year - 1900;
319  tm.tm_hour = time.hour;
320  tm.tm_min = time.minute;
321  tm.tm_sec = time.second;
322  tm.tm_isdst = -1;
323  tv = ast_mktime(&tm, NULL);
324 
325  return tv.tv_sec;
326 }
327 
328 /* span->start & span->end may be dates or floating times which have no timezone,
329  * which would mean that they should apply to the local timezone for all recepients.
330  * For example, if a meeting was set for 1PM-2PM floating time, people in different time
331  * zones would not be scheduled at the same local times. Dates are often treated as
332  * floating times, so all day events will need to be converted--so we can trust the
333  * span here, and instead will grab the start and end from the component, which will
334  * allow us to test for floating times or dates.
335  */
336 static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)
337 {
338  struct caldav_pvt *pvt = data;
339  struct ast_calendar_event *event;
340  icaltimezone *utc = icaltimezone_get_utc_timezone();
341  icaltimetype start, end, tmp;
342  icalcomponent *valarm;
343  icalproperty *prop;
344  struct icaltriggertype trigger;
345 
346  if (!(pvt && pvt->owner)) {
347  ast_log(LOG_ERROR, "Require a private structure with an owner\n");
348  return;
349  }
350 
351  if (!(event = ast_calendar_event_alloc(pvt->owner))) {
352  ast_log(LOG_ERROR, "Could not allocate an event!\n");
353  return;
354  }
355 
356  start = icalcomponent_get_dtstart(comp);
357  end = icalcomponent_get_dtend(comp);
358 
359  event->start = icaltime_get_tzid(start) ? span->start : icalfloat_to_timet(start);
360  event->end = icaltime_get_tzid(end) ? span->end : icalfloat_to_timet(end);
361  event->busy_state = span->is_busy ? AST_CALENDAR_BS_BUSY : AST_CALENDAR_BS_FREE;
362 
363  if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) {
364  ast_string_field_set(event, summary, icalproperty_get_value_as_string(prop));
365  }
366 
367  if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) {
368  ast_string_field_set(event, description, icalproperty_get_value_as_string(prop));
369  }
370 
371  if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) {
372  ast_string_field_set(event, organizer, icalproperty_get_value_as_string(prop));
373  }
374 
375  if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) {
376  ast_string_field_set(event, location, icalproperty_get_value_as_string(prop));
377  }
378 
379  if ((prop = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY))) {
380  ast_string_field_set(event, categories, icalproperty_get_value_as_string(prop));
381  }
382 
383  if ((prop = icalcomponent_get_first_property(comp, ICAL_PRIORITY_PROPERTY))) {
384  event->priority = icalvalue_get_integer(icalproperty_get_value(prop));
385  }
386 
387  if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) {
388  ast_string_field_set(event, uid, icalproperty_get_value_as_string(prop));
389  } else {
390  ast_log(LOG_WARNING, "No UID found, but one is required. Generating, but updates may not be acurate\n");
391  if (!ast_strlen_zero(event->summary)) {
392  ast_string_field_set(event, uid, event->summary);
393  } else {
394  char tmp[100];
395  snprintf(tmp, sizeof(tmp), "%ld", event->start);
396  ast_string_field_set(event, uid, tmp);
397  }
398  }
399 
400  /* Get the attendees */
401  for (prop = icalcomponent_get_first_property(comp, ICAL_ATTENDEE_PROPERTY);
402  prop; prop = icalcomponent_get_next_property(comp, ICAL_ATTENDEE_PROPERTY)) {
403  struct ast_calendar_attendee *attendee;
404  const char *data;
405 
406  if (!(attendee = ast_calloc(1, sizeof(*attendee)))) {
407  event = ast_calendar_unref_event(event);
408  return;
409  }
410  data = icalproperty_get_attendee(prop);
411  if (ast_strlen_zero(data)) {
412  ast_free(attendee);
413  continue;
414  }
415  attendee->data = ast_strdup(data);
416  AST_LIST_INSERT_TAIL(&event->attendees, attendee, next);
417  }
418 
419 
420  /* Only set values for alarm based on VALARM. Can be overriden in main/calendar.c by autoreminder
421  * therefore, go ahead and add events even if their is no VALARM or it is malformed
422  * Currently we are only getting the first VALARM and are handling repitition in main/calendar.c from calendar.conf */
423  if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
424  ao2_link(pvt->events, event);
425  event = ast_calendar_unref_event(event);
426  return;
427  }
428 
429  if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) {
430  ast_log(LOG_WARNING, "VALARM has no TRIGGER, skipping!\n");
431  ao2_link(pvt->events, event);
432  event = ast_calendar_unref_event(event);
433  return;
434  }
435 
436  trigger = icalproperty_get_trigger(prop);
437 
438  if (icaltriggertype_is_null_trigger(trigger)) {
439  ast_log(LOG_WARNING, "Bad TRIGGER for VALARM, skipping!\n");
440  ao2_link(pvt->events, event);
441  event = ast_calendar_unref_event(event);
442  return;
443  }
444 
445  if (!icaltime_is_null_time(trigger.time)) { /* This is an absolute time */
446  tmp = icaltime_convert_to_zone(trigger.time, utc);
447  event->alarm = icaltime_as_timet_with_zone(tmp, utc);
448  } else { /* Offset from either dtstart or dtend */
449  /* XXX Technically you can check RELATED to see if the event fires from the END of the event
450  * But, I'm not sure I've ever seen anyone implement it in calendaring software, so I'm ignoring for now */
451  tmp = icaltime_add(start, trigger.duration);
452  event->alarm = icaltime_as_timet_with_zone(tmp, utc);
453  }
454 
455  ao2_link(pvt->events, event);
456  event = ast_calendar_unref_event(event);
457 
458  return;
459 }
460 
461 struct xmlstate {
463  struct caldav_pvt *pvt;
464  struct ast_str *cdata;
465  time_t start;
466  time_t end;
467 };
468 
469 static void handle_start_element(void *data, const xmlChar *fullname, const xmlChar **atts)
470 {
471  struct xmlstate *state = data;
472 
473  if (!xmlStrcasecmp(fullname, BAD_CAST "C:calendar-data")) {
474  state->in_caldata = 1;
475  ast_str_reset(state->cdata);
476  }
477 }
478 
479 static void handle_end_element(void *data, const xmlChar *name)
480 {
481  struct xmlstate *state = data;
482  struct icaltimetype start, end;
483  icaltimezone *utc = icaltimezone_get_utc_timezone();
484  icalcomponent *iter;
485  icalcomponent *comp;
486 
487  if (xmlStrcasecmp(name, BAD_CAST "C:calendar-data")) {
488  return;
489  }
490 
491  state->in_caldata = 0;
492  if (!(state->cdata && ast_str_strlen(state->cdata))) {
493  return;
494  }
495  /* XXX Parse the calendar blurb for recurrence events in the time range,
496  * create an event, and add it to pvt->events */
497  start = icaltime_from_timet_with_zone(state->start, 0, utc);
498  end = icaltime_from_timet_with_zone(state->end, 0, utc);
499  comp = icalparser_parse_string(ast_str_buffer(state->cdata));
500 
501  for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT);
502  iter;
503  iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT))
504  {
505  icalcomponent_foreach_recurrence(iter, start, end, caldav_add_event, state->pvt);
506  }
507 
508  icalcomponent_free(comp);
509 }
510 
511 static void handle_characters(void *data, const xmlChar *ch, int len)
512 {
513  struct xmlstate *state = data;
514  xmlChar *tmp;
515 
516  if (!state->in_caldata) {
517  return;
518  }
519 
520  tmp = xmlStrndup(ch, len);
521  ast_str_append(&state->cdata, 0, "%s", (char *)tmp);
522  xmlFree(tmp);
523 }
524 
525 static int update_caldav(struct caldav_pvt *pvt)
526 {
527  struct timeval now = ast_tvnow();
528  time_t start, end;
529  struct ast_str *response;
530  xmlSAXHandler saxHandler;
531  struct xmlstate state = {
532  .in_caldata = 0,
533  .pvt = pvt
534  };
535 
536  start = now.tv_sec;
537  end = now.tv_sec + 60 * pvt->owner->timeframe;
538  if (!(response = caldav_get_events_between(pvt, start, end))) {
539  return -1;
540  }
541 
542  if (!(state.cdata = ast_str_create(512))) {
543  ast_free(response);
544  return -1;
545  }
546 
547  state.start = start;
548  state.end = end;
549 
550  memset(&saxHandler, 0, sizeof(saxHandler));
551  saxHandler.startElement = handle_start_element;
552  saxHandler.endElement = handle_end_element;
553  saxHandler.characters = handle_characters;
554 
555  xmlSAXUserParseMemory(&saxHandler, &state, ast_str_buffer(response), ast_str_strlen(response));
556 
558 
559  ast_free(response);
560  ast_free(state.cdata);
561 
562  return 0;
563 }
564 
565 static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
566 {
567  /* Verify all certs */
568  return 0;
569 }
570 
571 static void *caldav_load_calendar(void *void_data)
572 {
573  struct caldav_pvt *pvt;
574  const struct ast_config *cfg;
575  struct ast_variable *v;
576  struct ast_calendar *cal = void_data;
578 
579  if (!(cal && (cfg = ast_calendar_config_acquire()))) {
580  ast_log(LOG_ERROR, "You must enable calendar support for res_caldav to load\n");
581  return NULL;
582  }
583 
584  if (ao2_trylock(cal)) {
585  if (cal->unloading) {
586  ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
587  } else {
588  ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
589  }
591  return NULL;
592  }
593 
594  if (!(pvt = ao2_alloc(sizeof(*pvt), caldav_destructor))) {
595  ast_log(LOG_ERROR, "Could not allocate caldav_pvt structure for calendar: %s\n", cal->name);
597  return NULL;
598  }
599 
600  pvt->owner = cal;
601 
602  if (!(pvt->events = ast_calendar_event_container_alloc())) {
603  ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
604  pvt = unref_caldav(pvt);
605  ao2_unlock(cal);
607  return NULL;
608  }
609 
610  if (ast_string_field_init(pvt, 32)) {
611  ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
612  pvt = unref_caldav(pvt);
613  ao2_unlock(cal);
615  return NULL;
616  }
617 
618  for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
619  if (!strcasecmp(v->name, "url")) {
620  ast_string_field_set(pvt, url, v->value);
621  } else if (!strcasecmp(v->name, "user")) {
622  ast_string_field_set(pvt, user, v->value);
623  } else if (!strcasecmp(v->name, "secret")) {
625  }
626  }
627 
629 
630  if (ast_strlen_zero(pvt->url)) {
631  ast_log(LOG_WARNING, "No URL was specified for CalDAV calendar '%s' - skipping.\n", cal->name);
632  pvt = unref_caldav(pvt);
633  ao2_unlock(cal);
634  return NULL;
635  }
636 
637  if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
638  ast_log(LOG_WARNING, "Could not parse url '%s' for CalDAV calendar '%s' - skipping.\n", pvt->url, cal->name);
639  pvt = unref_caldav(pvt);
640  ao2_unlock(cal);
641  return NULL;
642  }
643 
644  if (pvt->uri.scheme == NULL) {
645  pvt->uri.scheme = "http";
646  }
647 
648  if (pvt->uri.port == 0) {
649  pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
650  }
651 
652  pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
653  ne_redirect_register(pvt->session);
654  ne_set_server_auth(pvt->session, auth_credentials, pvt);
655  if (!strcasecmp(pvt->uri.scheme, "https")) {
656  ne_ssl_trust_default_ca(pvt->session);
657  ne_ssl_set_verify(pvt->session, verify_cert, NULL);
658  }
659 
660  cal->tech_pvt = pvt;
661 
662  ast_mutex_init(&refreshlock);
663 
664  /* Load it the first time */
665  update_caldav(pvt);
666 
667  ao2_unlock(cal);
668 
669  /* The only writing from another thread will be if unload is true */
670  for (;;) {
671  struct timeval tv = ast_tvnow();
672  struct timespec ts = {0,};
673 
674  ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);
675 
676  ast_mutex_lock(&refreshlock);
677  while (!pvt->owner->unloading) {
678  if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
679  break;
680  }
681  }
682  ast_mutex_unlock(&refreshlock);
683 
684  if (pvt->owner->unloading) {
685  ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
686  return NULL;
687  }
688 
689  ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);
690 
691  update_caldav(pvt);
692  }
693 
694  return NULL;
695 }
696 
697 static int load_module(void)
698 {
699  ne_sock_init();
700  if (ast_calendar_register(&caldav_tech)) {
701  ne_sock_exit();
703  }
704 
706 }
707 
708 static int unload_module(void)
709 {
710  ast_calendar_unregister(&caldav_tech);
711  ne_sock_exit();
712  return 0;
713 }
714 
715 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk CalDAV Calendar Integration",
716  .load = load_module,
717  .unload = unload_module,
718  .load_pri = AST_MODPRI_DEVSTATE_PLUGIN,
719  );
struct ast_str * cdata
ast_cond_t unload
Definition: calendar.h:133
Asterisk locking-related definitions:
int ast_calendar_register(struct ast_calendar_tech *tech)
Register a new calendar technology.
Definition: res_calendar.c:490
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
int unloading
Definition: calendar.h:134
static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
static time_t icalfloat_to_timet(icaltimetype time)
ne_session * session
#define ast_strdup(a)
Definition: astmm.h:109
Definition: ast_expr2.c:325
static void handle_start_element(void *data, const xmlChar *fullname, const xmlChar **atts)
#define AST_MODULE
Definition: evt.c:58
static int load_module(void)
#define LOG_WARNING
Definition: logger.h:144
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
struct ast_calendar_attendee * next
Definition: calendar.h:89
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
Configuration File Parser.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:900
struct caldav_pvt * pvt
const ast_string_field uid
Definition: calendar.h:101
enum ast_calendar_busy_state busy_state
Definition: calendar.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:235
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ao2_unlock(a)
Definition: astobj2.h:497
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
int timeframe
Definition: calendar.h:131
const ast_string_field description
Definition: calendar.h:101
int tm_year
Definition: localtime.h:41
static void handle_characters(void *data, const xmlChar *ch, int len)
const ast_string_field organizer
Definition: calendar.h:101
struct ast_config * ast_calendar_config_acquire(void)
Grab and lock pointer to the calendar config (read only)
Definition: res_calendar.c:237
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
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)
void ast_calendar_merge_events(struct ast_calendar *cal, struct ao2_container *new_events)
Add an event to the list of events for a calendar.
Definition: res_calendar.c:961
void * tech_pvt
Definition: calendar.h:119
const char * type
Definition: calendar.h:70
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
const ast_string_field location
Definition: calendar.h:101
const char * value
Definition: config.h:79
static void handle_end_element(void *data, const xmlChar *name)
static void caldav_destructor(void *obj)
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:249
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
struct ao2_container * ast_calendar_event_container_alloc(void)
Allocate an astobj2 container for ast_calendar_event objects.
Definition: res_calendar.c:625
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:220
int tm_mon
Definition: localtime.h:40
#define ao2_ref(o, delta)
Definition: astobj2.h:472
long int ast_random(void)
Definition: utils.c:1640
A general API for managing calendar events with Asterisk.
const char * name
Definition: config.h:77
int tm_mday
Definition: localtime.h:39
static int fetch_response_reader(void *data, const char *block, size_t len)
const ast_string_field name
Definition: calendar.h:127
static struct ast_calendar_tech caldav_tech
static int update_caldav(struct caldav_pvt *pvt)
const ast_string_field secret
struct ast_calendar_event * ast_calendar_event_alloc(struct ast_calendar *cal)
Allocate an astobj2 ast_calendar_event object.
Definition: res_calendar.c:603
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
struct ast_calendar * owner
#define ao2_trylock(a)
Definition: astobj2.h:506
static struct ast_str * caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
static ast_mutex_t refreshlock
Definition: res_calendar.c:204
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void * caldav_load_calendar(void *data)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
static void * unref_caldav(void *obj)
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
struct ast_calendar_event * ast_calendar_unref_event(struct ast_calendar_event *event)
Unreference an ast_calendar_event.
Definition: res_calendar.c:300
int tm_hour
Definition: localtime.h:38
structure to hold users read from users.conf
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:367
const ast_string_field categories
Definition: calendar.h:101
struct ast_calendar_event::attendees attendees
int tm_sec
Definition: localtime.h:36
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:436
void ast_calendar_unregister(struct ast_calendar_tech *tech)
Unregister a new calendar technology.
Definition: res_calendar.c:523
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
static char secret[50]
Definition: chan_h323.c:148
struct timeval ast_mktime(struct ast_tm *const tmp, const char *zone)
Timezone-independent version of mktime(3).
Definition: localtime.c:2185
#define ast_calloc(a, b)
Definition: astmm.h:82
Individual calendaring technology data.
Definition: calendar.h:69
static int unload_module(void)
int tm_isdst
Definition: localtime.h:44
const ast_string_field url
struct ast_variable * next
Definition: config.h:82
#define ast_mutex_init(pmutex)
Definition: lock.h:152
Asterisk calendar structure.
Definition: calendar.h:117
void ast_calendar_config_release(void)
Release the calendar config.
Definition: res_calendar.c:249
static char url[512]
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
#define ast_malloc(a)
Definition: astmm.h:91
Asterisk module definitions.
const ast_string_field summary
Definition: calendar.h:101
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:253
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:172
static int caldav_write_event(struct ast_calendar_event *event)
int tm_min
Definition: localtime.h:37
Structure for mutex and tracking information.
Definition: lock.h:121
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
const ast_string_field user
#define ast_mutex_unlock(a)
Definition: lock.h:156
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:344
struct ao2_container * events
static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)