Asterisk - The Open Source Telephony Project GIT-master-4c84066
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
res_calendar_caldav.c File Reference

Resource for handling CalDAV calendars. More...

#include "asterisk.h"
#include <libical/ical.h>
#include <ne_session.h>
#include <ne_uri.h>
#include <ne_request.h>
#include <ne_auth.h>
#include <ne_redirect.h>
#include <libxml/xmlreader.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/calendar.h"
#include "asterisk/lock.h"
#include "asterisk/config.h"
#include "asterisk/astobj2.h"
Include dependency graph for res_calendar_caldav.c:

Go to the source code of this file.

Data Structures

struct  caldav_pvt
 
struct  xmlstate
 

Macros

#define ICAL_SPAN_CONST
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int auth_credentials (void *userdata, const char *realm, int attempts, char *username, char *secret)
 
static void caldav_add_event (icalcomponent *comp, ICAL_SPAN_CONST struct icaltime_span *span, void *data)
 
static void caldav_destructor (void *obj)
 
static struct ast_strcaldav_get_events_between (struct caldav_pvt *pvt, time_t start_time, time_t end_time)
 
static void * caldav_load_calendar (void *data)
 
static struct ast_strcaldav_request (struct caldav_pvt *pvt, const char *method, struct ast_str *req_body, struct ast_str *subdir, const char *content_type)
 
static int caldav_write_event (struct ast_calendar_event *event)
 
static int debug_response_handler (void *userdata, ne_request *req, const ne_status *st)
 
static int fetch_response_reader (void *data, const char *block, size_t len)
 
static void handle_characters (xmlTextReaderPtr reader, struct xmlstate *state)
 
static void handle_end_element (xmlTextReaderPtr reader, struct xmlstate *state)
 
static void handle_start_element (xmlTextReaderPtr reader, struct xmlstate *state)
 
static time_t icalfloat_to_timet (icaltimetype time)
 
static int load_module (void)
 
static void parse_error_handler (void *arg, const char *msg, xmlParserSeverities severity, xmlTextReaderLocatorPtr locator)
 
static int unload_module (void)
 
static void * unref_caldav (void *obj)
 
static int update_caldav (struct caldav_pvt *pvt)
 
static int verify_cert (void *userdata, int failures, const ne_ssl_certificate *cert)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk CalDAV Calendar Integration" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PLUGIN, .requires = "res_calendar", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const xmlChar * caldav_node_localname = BAD_CAST "calendar-data"
 
static const xmlChar * caldav_node_nsuri = BAD_CAST "urn:ietf:params:xml:ns:caldav"
 
static struct ast_calendar_tech caldav_tech
 

Detailed Description

Resource for handling CalDAV calendars.

Definition in file res_calendar_caldav.c.

Macro Definition Documentation

◆ ICAL_SPAN_CONST

#define ICAL_SPAN_CONST

Definition at line 38 of file res_calendar_caldav.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 808 of file res_calendar_caldav.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 808 of file res_calendar_caldav.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 808 of file res_calendar_caldav.c.

◆ auth_credentials()

static int auth_credentials ( void *  userdata,
const char *  realm,
int  attempts,
char *  username,
char *  secret 
)
static

Definition at line 119 of file res_calendar_caldav.c.

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}
#define ast_log
Definition astobj2.c:42
#define LOG_WARNING
const ast_string_field name
Definition calendar.h:129
struct ast_calendar * owner
const ast_string_field user
const ast_string_field secret

References ast_log, LOG_WARNING, ast_calendar::name, caldav_pvt::owner, caldav_pvt::secret, and caldav_pvt::user.

Referenced by caldav_load_calendar().

◆ caldav_add_event()

static void caldav_add_event ( icalcomponent *  comp,
ICAL_SPAN_CONST struct icaltime_span *  span,
void *  data 
)
static

Definition at line 358 of file res_calendar_caldav.c.

359{
360 struct caldav_pvt *pvt = data;
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 /* Get the attendees */
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)))) {
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 /* Only set values for alarm based on VALARM. Can be overriden in main/calendar.c by autoreminder
443 * therefore, go ahead and add events even if their is no VALARM or it is malformed
444 * Currently we are only getting the first VALARM and are handling repitition in main/calendar.c from calendar.conf */
445 if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
446 ao2_link(pvt->events, 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);
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);
464 return;
465 }
466
467 if (!icaltime_is_null_time(trigger.time)) { /* This is an absolute time */
468 tmp = icaltime_convert_to_zone(trigger.time, utc);
469 event->alarm = icaltime_as_timet_with_zone(tmp, utc);
470 } else { /* Offset from either dtstart or dtend */
471 /* XXX Technically you can check RELATED to see if the event fires from the END of the event
472 * But, I'm not sure I've ever seen anyone implement it in calendaring software, so I'm ignoring for now */
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);
489
490 return;
491}
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ao2_link(container, obj)
Add an object to a container.
Definition astobj2.h:1532
@ AST_CALENDAR_BS_FREE
Definition calendar.h:84
@ AST_CALENDAR_BS_BUSY
Definition calendar.h:86
struct ast_calendar_event * ast_calendar_event_alloc(struct ast_calendar *cal)
Allocate an astobj2 ast_calendar_event object.
struct ast_calendar_event * ast_calendar_unref_event(struct ast_calendar_event *event)
Unreference an ast_calendar_event.
char * end
Definition eagi_proxy.c:73
#define LOG_ERROR
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
static time_t icalfloat_to_timet(icaltimetype time)
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
struct ast_calendar_attendee * next
Definition calendar.h:91
Calendar events.
Definition calendar.h:95
struct ao2_container * events
struct association categories[]
int ast_time_t_to_string(time_t time, char *buf, size_t length)
Converts to a string representation of a time_t as decimal seconds since the epoch....
Definition time.c:152
#define AST_TIME_T_LEN
Definition time.h:45

References ao2_link, AST_CALENDAR_BS_BUSY, AST_CALENDAR_BS_FREE, ast_calendar_event_alloc(), ast_calendar_unref_event(), ast_calloc, ast_free, AST_LIST_INSERT_TAIL, ast_log, ast_strdup, ast_string_field_set, ast_strlen_zero(), AST_TIME_T_LEN, ast_time_t_to_string(), categories, ast_calendar_attendee::data, end, caldav_pvt::events, icalfloat_to_timet(), LOG_ERROR, LOG_WARNING, ast_calendar_attendee::next, caldav_pvt::owner, and ast_calendar_event::start.

Referenced by handle_end_element().

◆ caldav_destructor()

static void caldav_destructor ( void *  obj)
static

Definition at line 79 of file res_calendar_caldav.c.

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);
89
91
92 ao2_ref(pvt->events, -1);
93}
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition astobj2.h:1693
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
@ OBJ_NODATA
Definition astobj2.h:1044
@ OBJ_MULTIPLE
Definition astobj2.h:1049
@ OBJ_UNLINK
Definition astobj2.h:1039
#define ast_debug(level,...)
Log a DEBUG message.
#define NULL
Definition resample.c:96
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
ne_session * session

References ao2_callback, ao2_ref, ast_debug, ast_string_field_free_memory, caldav_pvt::events, ast_calendar::name, NULL, OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, caldav_pvt::owner, caldav_pvt::session, and caldav_pvt::uri.

Referenced by caldav_load_calendar().

◆ caldav_get_events_between()

static struct ast_str * caldav_get_events_between ( struct caldav_pvt pvt,
time_t  start_time,
time_t  end_time 
)
static

Definition at line 285 of file res_calendar_caldav.c.

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 /* If I was really being efficient, I would store a collection of event URIs and etags,
303 * first doing a query of just the etag and seeing if anything had changed. If it had,
304 * then I would do a request for each of the events that had changed, and only bother
305 * updating those. Oh well. */
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}
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)
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:1139
size_t attribute_pure ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition strings.h:730
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
Support for dynamic strings.
Definition strings.h:623

References ast_free, ast_log, ast_str_append(), ast_str_create, ast_str_strlen(), caldav_request(), end, LOG_ERROR, and NULL.

Referenced by update_caldav().

◆ caldav_load_calendar()

static void * caldav_load_calendar ( void *  data)
static

Definition at line 658 of file res_calendar_caldav.c.

659{
660 struct caldav_pvt *pvt;
661 const struct ast_config *cfg;
662 struct ast_variable *v;
663 struct ast_calendar *cal = void_data;
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 }
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);
684 return NULL;
685 }
686
687 pvt->owner = cal;
688
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);
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);
702 return NULL;
703 }
704
705 for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
706 if (!strcasecmp(v->name, "url")) {
708 } else if (!strcasecmp(v->name, "user")) {
710 } else if (!strcasecmp(v->name, "secret")) {
711 ast_string_field_set(pvt, secret, v->value);
712 }
713 }
714
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
750
751 /* Load it the first time */
752 update_caldav(pvt);
753
754 ao2_unlock(cal);
755
756 /* The only writing from another thread will be if unload is true */
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
764 while (!pvt->owner->unloading) {
765 if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
766 break;
767 }
768 }
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}
#define ao2_unlock(a)
Definition astobj2.h:729
#define ao2_trylock(a)
Definition astobj2.h:739
#define ao2_alloc(data_size, destructor_fn)
Definition astobj2.h:409
void ast_calendar_config_release(void)
Release the calendar config.
struct ao2_container * ast_calendar_event_container_alloc(void)
Allocate an astobj2 container for ast_calendar_event objects.
const struct ast_config * ast_calendar_config_acquire(void)
Grab and lock pointer to the calendar config (read only)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition extconf.c:1213
#define ast_cond_timedwait(cond, mutex, time)
Definition lock.h:213
#define ast_mutex_init(pmutex)
Definition lock.h:193
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_mutex_lock(a)
Definition lock.h:196
static ast_mutex_t refreshlock
static void * unref_caldav(void *obj)
static int update_caldav(struct caldav_pvt *pvt)
static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
static void caldav_destructor(void *obj)
static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
static char url[512]
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Asterisk calendar structure.
Definition calendar.h:119
void * tech_pvt
Definition calendar.h:121
ast_cond_t unload
Definition calendar.h:137
unsigned int unloading
Definition calendar.h:138
Structure for mutex and tracking information.
Definition lock.h:142
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
const ast_string_field url
structure to hold users read from phoneprov_users.conf
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition time.h:159

References ao2_alloc, ao2_trylock, ao2_unlock, ast_calendar_config_acquire(), ast_calendar_config_release(), ast_calendar_event_container_alloc(), ast_cond_timedwait, ast_debug, ast_log, ast_mutex_init, ast_mutex_lock, ast_mutex_unlock, ast_string_field_init, ast_string_field_set, ast_strlen_zero(), ast_tvnow(), ast_variable_browse(), auth_credentials(), caldav_destructor(), caldav_pvt::events, LOG_ERROR, LOG_WARNING, ast_calendar::name, ast_variable::name, ast_variable::next, NULL, caldav_pvt::owner, ast_calendar::refresh, refreshlock, caldav_pvt::session, ast_calendar::tech_pvt, ast_calendar::unload, ast_calendar::unloading, unref_caldav(), update_caldav(), caldav_pvt::uri, caldav_pvt::url, url, ast_variable::value, and verify_cert().

◆ caldav_request()

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 
)
static

Definition at line 147 of file res_calendar_caldav.c.

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}
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int debug_response_handler(void *userdata, ne_request *req, const ne_status *st)
static int fetch_response_reader(void *data, const char *block, size_t len)
const char * method
Definition res_pjsip.c:1273
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761

References ast_free, ast_log, ast_str_buffer(), ast_str_create, ast_str_strlen(), ast_strlen_zero(), buf, debug_response_handler(), fetch_response_reader(), LOG_ERROR, LOG_WARNING, method, ast_calendar::name, NULL, caldav_pvt::owner, caldav_pvt::session, and caldav_pvt::uri.

Referenced by caldav_get_events_between(), and caldav_write_event().

◆ caldav_write_event()

static int caldav_write_event ( struct ast_calendar_event event)
static

Definition at line 184 of file res_calendar_caldav.c.

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) {
251 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));
252 break;
253
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
271write_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}
@ AST_CALENDAR_BS_BUSY_TENTATIVE
Definition calendar.h:85
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
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:1113
long int ast_random(void)
Definition utils.c:2346

References AST_CALENDAR_BS_BUSY, AST_CALENDAR_BS_BUSY_TENTATIVE, ast_free, ast_log, ast_random(), ast_str_append(), ast_str_create, ast_str_set(), ast_string_field_build, ast_strlen_zero(), caldav_request(), LOG_ERROR, LOG_WARNING, NULL, caldav_pvt::owner, ast_calendar::tech_pvt, and caldav_pvt::url.

◆ debug_response_handler()

static int debug_response_handler ( void *  userdata,
ne_request *  req,
const ne_status *  st 
)
static

Definition at line 134 of file res_calendar_caldav.c.

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}

References ast_debug.

Referenced by caldav_request().

◆ fetch_response_reader()

static int fetch_response_reader ( void *  data,
const char *  block,
size_t  len 
)
static

Definition at line 103 of file res_calendar_caldav.c.

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}
#define ast_malloc(len)
A wrapper for malloc()
Definition astmm.h:191
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References ast_free, ast_malloc, ast_str_append(), and len().

Referenced by caldav_request().

◆ handle_characters()

static void handle_characters ( xmlTextReaderPtr  reader,
struct xmlstate state 
)
static

Definition at line 550 of file res_calendar_caldav.c.

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}
char * text
Definition app_queue.c:1791

References ast_str_append(), and text.

Referenced by update_caldav().

◆ handle_end_element()

static void handle_end_element ( xmlTextReaderPtr  reader,
struct xmlstate state 
)
static

Definition at line 517 of file res_calendar_caldav.c.

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 /* XXX Parse the calendar blurb for recurrence events in the time range,
535 * create an event, and add it to pvt->events */
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}
static void caldav_add_event(icalcomponent *comp, ICAL_SPAN_CONST struct icaltime_span *span, void *data)
static const xmlChar * caldav_node_nsuri
static const xmlChar * caldav_node_localname

References ast_str_buffer(), ast_str_strlen(), caldav_add_event(), caldav_node_localname, caldav_node_nsuri, and end.

Referenced by update_caldav().

◆ handle_start_element()

static void handle_start_element ( xmlTextReaderPtr  reader,
struct xmlstate state 
)
static

Definition at line 504 of file res_calendar_caldav.c.

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}
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:693

References ast_str_reset(), caldav_node_localname, and caldav_node_nsuri.

Referenced by update_caldav().

◆ icalfloat_to_timet()

static time_t icalfloat_to_timet ( icaltimetype  time)
static

Definition at line 333 of file res_calendar_caldav.c.

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}
struct timeval ast_mktime(struct ast_tm *const tmp, const char *zone)
Timezone-independent version of mktime(3).
Definition localtime.c:2357
int tm_mday
Definition localtime.h:39
int tm_sec
Definition localtime.h:36
int tm_hour
Definition localtime.h:38
int tm_isdst
Definition localtime.h:44
int tm_min
Definition localtime.h:37
int tm_year
Definition localtime.h:41
int tm_mon
Definition localtime.h:40

References ast_mktime(), NULL, ast_tm::tm_hour, ast_tm::tm_isdst, ast_tm::tm_mday, ast_tm::tm_min, ast_tm::tm_mon, ast_tm::tm_sec, and ast_tm::tm_year.

Referenced by caldav_add_event().

◆ load_module()

static int load_module ( void  )
static

Definition at line 784 of file res_calendar_caldav.c.

785{
786 ne_sock_init();
788 ne_sock_exit();
790 }
791
793}
int ast_calendar_register(struct ast_calendar_tech *tech)
Register a new calendar technology.
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
static struct ast_calendar_tech caldav_tech

References ast_calendar_register(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and caldav_tech.

◆ parse_error_handler()

static void parse_error_handler ( void *  arg,
const char *  msg,
xmlParserSeverities  severity,
xmlTextReaderLocatorPtr  locator 
)
static

Definition at line 565 of file res_calendar_caldav.c.

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}
enum ast_security_event_severity severity

References ast_log, LOG_ERROR, LOG_WARNING, and severity.

Referenced by update_caldav().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 795 of file res_calendar_caldav.c.

796{
798 ne_sock_exit();
799 return 0;
800}
void ast_calendar_unregister(struct ast_calendar_tech *tech)
Unregister a new calendar technology.

References ast_calendar_unregister(), and caldav_tech.

◆ unref_caldav()

static void * unref_caldav ( void *  obj)
static

Definition at line 95 of file res_calendar_caldav.c.

96{
97 struct caldav_pvt *pvt = obj;
98
99 ao2_ref(pvt, -1);
100 return NULL;
101}

References ao2_ref, and NULL.

Referenced by caldav_load_calendar().

◆ update_caldav()

static int update_caldav ( struct caldav_pvt pvt)
static

Definition at line 585 of file res_calendar_caldav.c.

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
611 ast_str_buffer(response),
612 ast_str_strlen(response),
613 NULL,
614 NULL,
615 0);
616
617 if (reader) {
618 int res;
619
621
623 while (res == 1) {
625 switch (node_type) {
628 break;
631 break;
635 break;
636 default:
637 break;
638 }
640 }
642 }
643
645
646 ast_free(response);
647 ast_free(state.cdata);
648
649 return 0;
650}
node_type
Definition ast_expr2.c:333
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.
static void handle_start_element(xmlTextReaderPtr reader, struct xmlstate *state)
static struct ast_str * caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
static void handle_characters(xmlTextReaderPtr reader, struct xmlstate *state)
static void handle_end_element(xmlTextReaderPtr reader, struct xmlstate *state)
static void parse_error_handler(void *arg, const char *msg, xmlParserSeverities severity, xmlTextReaderLocatorPtr locator)
struct caldav_pvt * pvt

References ast_calendar_merge_events(), ast_free, ast_str_buffer(), ast_str_create, ast_str_strlen(), ast_tvnow(), caldav_get_events_between(), end, caldav_pvt::events, handle_characters(), handle_end_element(), handle_start_element(), NULL, caldav_pvt::owner, parse_error_handler(), xmlstate::pvt, xmlstate::start, and ast_calendar::timeframe.

Referenced by caldav_load_calendar().

◆ verify_cert()

static int verify_cert ( void *  userdata,
int  failures,
const ne_ssl_certificate *  cert 
)
static

Definition at line 652 of file res_calendar_caldav.c.

653{
654 /* Verify all certs */
655 return 0;
656}

Referenced by caldav_load_calendar().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk CalDAV Calendar Integration" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PLUGIN, .requires = "res_calendar", }
static

Definition at line 808 of file res_calendar_caldav.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 808 of file res_calendar_caldav.c.

◆ caldav_node_localname

const xmlChar* caldav_node_localname = BAD_CAST "calendar-data"
static

Definition at line 501 of file res_calendar_caldav.c.

Referenced by handle_end_element(), and handle_start_element().

◆ caldav_node_nsuri

const xmlChar* caldav_node_nsuri = BAD_CAST "urn:ietf:params:xml:ns:caldav"
static

Definition at line 502 of file res_calendar_caldav.c.

Referenced by handle_end_element(), and handle_start_element().

◆ caldav_tech

struct ast_calendar_tech caldav_tech
static

Definition at line 58 of file res_calendar_caldav.c.

58 {
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};
#define AST_MODULE
static void * caldav_load_calendar(void *data)
static int caldav_write_event(struct ast_calendar_event *event)

Referenced by load_module(), and unload_module().