34 #include <libical/ical.h>
35 #include <ne_session.h>
37 #include <ne_request.h>
39 #include <ne_redirect.h>
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
55 .description =
"CalDAV calendars",
80 ne_session_destroy(pvt->
session);
99 struct ast_str **response = data;
105 memcpy(tmp, block, len);
122 ne_strnzcpy(username, pvt->
user, NE_ABUFSIZ);
123 ne_strnzcpy(secret, pvt->
secret, NE_ABUFSIZ);
145 snprintf(buf,
sizeof(buf),
"%s%s", pvt->
uri.path, subdir ?
ast_str_buffer(subdir) :
"");
147 req = ne_request_create(pvt->
session, method, buf);
150 ne_add_request_header(req,
"Content-type",
ast_strlen_zero(content_type) ?
"text/xml" : content_type);
152 ret = ne_request_dispatch(req);
153 ne_request_destroy(req);
169 struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
170 icalcomponent *calendar, *icalevent;
171 icaltimezone *utc = icaltimezone_get_utc_timezone();
179 if (!(event->
start && event->
end)) {
192 unsigned short val[8];
194 for (x = 0; x < 8; x++) {
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]);
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"));
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)));
213 icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->
organizer));
216 icalcomponent_add_property(icalevent, icalproperty_new_summary(event->
summary));
219 icalcomponent_add_property(icalevent, icalproperty_new_description(event->
description));
222 icalcomponent_add_property(icalevent, icalproperty_new_location(event->
location));
225 icalcomponent_add_property(icalevent, icalproperty_new_categories(event->
categories));
228 icalcomponent_add_property(icalevent, icalproperty_new_priority(event->
priority));
233 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));
237 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE));
241 icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE));
244 icalcomponent_add_component(calendar, icalevent);
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);
249 response =
caldav_request(pvt,
"PUT", body, subdir,
"text/calendar");
269 struct ast_str *body, *response;
270 icaltimezone *utc = icaltimezone_get_utc_timezone();
271 icaltimetype start, end;
272 const char *start_str, *end_str;
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);
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"
292 " <C:calendar-data>\n"
293 " <C:expand start=\"%s\" end=\"%s\"/>\n"
294 " </C:calendar-data>\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"
303 "</C:calendar-query>\n", start_str, end_str, start_str, end_str);
317 tm.
tm_mon = time.month - 1;
340 icaltimezone *utc = icaltimezone_get_utc_timezone();
342 icalcomponent *valarm;
344 struct icaltriggertype trigger;
346 if (!(pvt && pvt->
owner)) {
356 start = icalcomponent_get_dtstart(comp);
357 end = icalcomponent_get_dtend(comp);
363 if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) {
367 if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) {
371 if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) {
375 if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) {
379 if ((prop = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY))) {
383 if ((prop = icalcomponent_get_first_property(comp, ICAL_PRIORITY_PROPERTY))) {
384 event->priority = icalvalue_get_integer(icalproperty_get_value(prop));
387 if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) {
390 ast_log(
LOG_WARNING,
"No UID found, but one is required. Generating, but updates may not be acurate\n");
395 snprintf(tmp,
sizeof(tmp),
"%ld", event->
start);
401 for (prop = icalcomponent_get_first_property(comp, ICAL_ATTENDEE_PROPERTY);
402 prop; prop = icalcomponent_get_next_property(comp, ICAL_ATTENDEE_PROPERTY)) {
406 if (!(attendee =
ast_calloc(1,
sizeof(*attendee)))) {
410 data = icalproperty_get_attendee(prop);
423 if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
429 if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) {
436 trigger = icalproperty_get_trigger(prop);
438 if (icaltriggertype_is_null_trigger(trigger)) {
445 if (!icaltime_is_null_time(trigger.time)) {
446 tmp = icaltime_convert_to_zone(trigger.time, utc);
447 event->alarm = icaltime_as_timet_with_zone(tmp, utc);
451 tmp = icaltime_add(start, trigger.duration);
452 event->alarm = icaltime_as_timet_with_zone(tmp, utc);
473 if (!xmlStrcasecmp(fullname, BAD_CAST
"C:calendar-data")) {
482 struct icaltimetype start, end;
483 icaltimezone *utc = icaltimezone_get_utc_timezone();
487 if (xmlStrcasecmp(name, BAD_CAST
"C:calendar-data")) {
497 start = icaltime_from_timet_with_zone(state->
start, 0, utc);
498 end = icaltime_from_timet_with_zone(state->
end, 0, utc);
501 for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT);
503 iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT))
508 icalcomponent_free(comp);
520 tmp = xmlStrndup(ch, len);
530 xmlSAXHandler saxHandler;
550 memset(&saxHandler, 0,
sizeof(saxHandler));
565 static int verify_cert(
void *userdata,
int failures,
const ne_ssl_certificate *cert)
580 ast_log(
LOG_ERROR,
"You must enable calendar support for res_caldav to load\n");
603 ast_log(
LOG_ERROR,
"Could not allocate space for fetching events for calendar: %s\n", cal->
name);
619 if (!strcasecmp(v->
name,
"url")) {
621 }
else if (!strcasecmp(v->
name,
"user")) {
623 }
else if (!strcasecmp(v->
name,
"secret")) {
637 if (ne_uri_parse(pvt->
url, &pvt->
uri) || pvt->
uri.host == NULL || pvt->
uri.path == NULL) {
644 if (pvt->
uri.scheme == NULL) {
645 pvt->
uri.scheme =
"http";
648 if (pvt->
uri.port == 0) {
649 pvt->
uri.port = ne_uri_defaultport(pvt->
uri.scheme);
652 pvt->
session = ne_session_create(pvt->
uri.scheme, pvt->
uri.host, pvt->
uri.port);
653 ne_redirect_register(pvt->
session);
655 if (!strcasecmp(pvt->
uri.scheme,
"https")) {
656 ne_ssl_trust_default_ca(pvt->
session);
672 struct timespec ts = {0,};
685 ast_debug(10,
"Skipping refresh since we got a shutdown signal\n");
Asterisk locking-related definitions:
int ast_calendar_register(struct ast_calendar_tech *tech)
Register a new calendar technology.
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
static time_t icalfloat_to_timet(icaltimetype time)
static void handle_start_element(void *data, const xmlChar *fullname, const xmlChar **atts)
static int load_module(void)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define ao2_callback(c, flags, cb_fn, arg)
struct ast_calendar_attendee * next
Structure for variables, used for configurations and for channel variables.
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.
const ast_string_field uid
enum ast_calendar_busy_state busy_state
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
struct ast_str * ast_str_create(size_t init_len)
Create a malloc'ed dynamic length string.
#define ast_mutex_lock(a)
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
const ast_string_field description
static void handle_characters(void *data, const xmlChar *ch, int len)
const ast_string_field organizer
struct ast_config * ast_calendar_config_acquire(void)
Grab and lock pointer to the calendar config (read only)
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
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.
#define ast_debug(level,...)
Log a DEBUG message.
const ast_string_field location
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.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
struct ao2_container * ast_calendar_event_container_alloc(void)
Allocate an astobj2 container for ast_calendar_event objects.
#define AST_STRING_FIELD(name)
Declare a string field.
#define ao2_ref(o, delta)
long int ast_random(void)
A general API for managing calendar events with Asterisk.
static int fetch_response_reader(void *data, const char *block, size_t len)
const ast_string_field name
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.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
struct ast_calendar * owner
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 ...
static ast_mutex_t refreshlock
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...
#define ao2_alloc(data_size, destructor_fn)
static void * unref_caldav(void *obj)
struct ast_calendar_event * ast_calendar_unref_event(struct ast_calendar_event *event)
Unreference an ast_calendar_event.
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.
const ast_string_field categories
struct ast_calendar_event::attendees attendees
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
void ast_calendar_unregister(struct ast_calendar_tech *tech)
Unregister a new calendar technology.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
static int verify_cert(void *userdata, int failures, const ne_ssl_certificate *cert)
struct timeval ast_mktime(struct ast_tm *const tmp, const char *zone)
Timezone-independent version of mktime(3).
Individual calendaring technology data.
static int unload_module(void)
const ast_string_field url
struct ast_variable * next
#define ast_mutex_init(pmutex)
Asterisk calendar structure.
void ast_calendar_config_release(void)
Release the calendar config.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
const ast_string_field summary
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
#define ast_cond_timedwait(cond, mutex, time)
static int caldav_write_event(struct ast_calendar_event *event)
Structure for mutex and tracking information.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
const ast_string_field user
#define ast_mutex_unlock(a)
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
struct ao2_container * events
static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)