[asterisk-commits] twilson: branch twilson/calendaring r155709 - in /team/twilson/calendaring: b...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Nov 10 01:38:52 CST 2008


Author: twilson
Date: Mon Nov 10 01:38:51 2008
New Revision: 155709

URL: http://svn.digium.com/view/asterisk?view=rev&rev=155709
Log:
Initial commit of caldav support + a couple of bug fixes

Added:
    team/twilson/calendaring/res/res_caldav.c   (with props)
Modified:
    team/twilson/calendaring/build_tools/menuselect-deps.in
    team/twilson/calendaring/main/calendar.c
    team/twilson/calendaring/res/res_exchangecal.c
    team/twilson/calendaring/res/res_icalendar.c

Modified: team/twilson/calendaring/build_tools/menuselect-deps.in
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/build_tools/menuselect-deps.in?view=diff&rev=155709&r1=155708&r2=155709
==============================================================================
--- team/twilson/calendaring/build_tools/menuselect-deps.in (original)
+++ team/twilson/calendaring/build_tools/menuselect-deps.in Mon Nov 10 01:38:51 2008
@@ -19,6 +19,7 @@
 IXJUSER=@PBX_IXJUSER@
 JACK=@PBX_JACK@
 LDAP=@PBX_LDAP@
+LIBXML2=@PBX_LIBXML2@
 LTDL=@PBX_LTDL@
 LUA=@PBX_LUA@
 MISDN=@PBX_MISDN@

Modified: team/twilson/calendaring/main/calendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/main/calendar.c?view=diff&rev=155709&r1=155708&r2=155709
==============================================================================
--- team/twilson/calendaring/main/calendar.c (original)
+++ team/twilson/calendaring/main/calendar.c Mon Nov 10 01:38:51 2008
@@ -88,7 +88,7 @@
 	return ast_str_hash(cal->name);
 }
 
-static int calendar_cmp_fn(void *obj, void *arg, int flags)
+static int calendar_cmp_fn(void *obj, void *arg, void *data, int flags)
 {
 	const struct ast_calendar *one = obj, *two = arg;
 	return !strcasecmp(one->name, two->name) ? CMP_MATCH : 0;
@@ -99,7 +99,7 @@
 	struct ast_calendar tmp = {
 		.name = name,
 	};
-	return ao2_find(calendars, &tmp, OBJ_POINTER);
+	return ao2_find(calendars, &tmp, NULL, OBJ_POINTER);
 }
 
 static int event_hash_fn(const void *obj, const int flags)
@@ -108,7 +108,7 @@
 	return ast_str_hash(event->uid);
 }
 
-static int event_cmp_fn(void *obj, void *arg, int flags)
+static int event_cmp_fn(void *obj, void *arg, void *data, int flags)
 {
 	const struct ast_calendar_event *one = obj, *two = arg;
 	return !strcasecmp(one->uid, two->uid) ? CMP_MATCH : 0;
@@ -119,7 +119,7 @@
 	struct ast_calendar_event tmp = {
 		.uid = uid,
 	};
-	return ao2_find(events, &tmp, OBJ_POINTER);
+	return ao2_find(events, &tmp, NULL, OBJ_POINTER);
 }
 
 struct ast_calendar_event *ast_calendar_unref_event(struct ast_calendar_event *event)

Added: team/twilson/calendaring/res/res_caldav.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_caldav.c?view=auto&rev=155709
==============================================================================
--- team/twilson/calendaring/res/res_caldav.c (added)
+++ team/twilson/calendaring/res/res_caldav.c Mon Nov 10 01:38:51 2008
@@ -1,0 +1,648 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2008, Digium, Inc.
+ *
+ * Terry Wilson <twilson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Resource for handling iCalnedar calendars
+ */
+
+/*** MODULEINFO
+	<depend>neon</depend>
+	<depend>ical</depend>
+	<depend>libxml2</depend>
+***/
+#include "asterisk.h"
+
+#include <libical/ical.h>
+#include <neon/ne_session.h>
+#include <neon/ne_uri.h>
+#include <neon/ne_request.h>
+#include <neon/ne_auth.h>
+#include <libxml/xmlmemory.h>
+#include <libxml/parser.h>
+
+#include "asterisk/module.h"
+#include "asterisk/calendar.h"
+#include "asterisk/lock.h"
+#include "asterisk/config.h"
+#include "asterisk/astobj2.h"
+
+static void *caldav_load_calendar(void *data);
+static void *unref_caldav(void *obj);
+static int caldav_write_event(struct ast_calendar_event *event);
+
+static const struct ast_calendar_tech caldav_tech = {
+	.type = "caldav",
+	.description = "CalDAV calendars",
+	.module = AST_MODULE,
+	.load_calendar = caldav_load_calendar,
+	.unref_calendar = unref_caldav,
+	.write_event = caldav_write_event,
+};
+
+struct caldav_pvt {
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(url);
+		AST_STRING_FIELD(user);
+		AST_STRING_FIELD(secret);
+	);
+	struct ast_calendar *owner;
+	ne_uri uri;
+	ne_session *session;
+	struct ao2_container *events;
+};
+
+static void caldav_destructor(void *obj)
+{
+	struct caldav_pvt *pvt = obj;
+
+	ast_debug(1, "Destroying pvt for iCalendar %s\n", pvt->owner->name);
+	if (pvt->session) {
+		ne_session_destroy(pvt->session);
+	}
+	ast_string_field_free_memory(pvt);
+}
+
+static void *unref_caldav(void *obj)
+{
+	struct caldav_pvt *pvt = obj;
+	
+	ao2_ref(pvt, -1);
+	return NULL;
+}
+
+/*
+static struct ast_str *xml_encode_str(struct ast_str *dst, const char *src)
+{
+	char *tmp;
+	char buf[7];
+
+	for (tmp = (char *)src; *tmp; tmp++) {
+		switch (*tmp) {
+			case '\"' : strcpy(buf, "&quot;"); break;
+			case '\'' : strcpy(buf, "&apos;"); break;
+			case '&' : strcpy(buf, "&amp;"); break;
+			case '<' : strcpy(buf, "&lt;"); break;
+			case '>' : strcpy(buf, "&gt;"); break;
+			default  : sprintf(buf, "%c", *tmp);
+		} 
+
+		ast_str_append(&dst, 0, "%s", buf);
+	}
+
+	return dst;
+}
+*/
+static int fetch_response_reader(void *data, const char *block, size_t len)
+{
+	struct ast_str **response = data;
+	unsigned char *tmp;
+
+	tmp = ast_malloc(len + 1);
+	memcpy(tmp, block, len);
+	tmp[len] = '\0';
+	ast_str_append(response, 0, "%s", tmp);
+	ast_free(tmp);
+
+	return 0;
+}
+
+static int auth_credentials(void *userdata, const char *realm, int attempts, char *username, char *secret)
+{
+	struct caldav_pvt *pvt = userdata;
+
+	if (attempts > 1) {
+		ast_log(LOG_WARNING, "Invalid username or password for iCalendar '%s'\n", pvt->owner->name);
+		return -1;
+	}
+
+	ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
+	ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
+
+	return 0;
+}
+
+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)
+{
+	struct ast_str *response;
+	ne_request *req;
+	int ret;
+	char buf[1000];
+
+	if (!pvt) {
+		ast_log(LOG_ERROR, "There is no private!\n");
+		return NULL;
+	}
+
+	if (!(response = ast_str_create(512))) {
+		ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
+		return NULL;
+	}
+
+	snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? subdir->str : "");
+
+	ast_log(LOG_NOTICE, "buf is '%s'\n", buf);
+	sleep(1);
+
+	req = ne_request_create(pvt->session, method, buf);
+	ne_add_response_body_reader(req, ne_accept_2xx, fetch_response_reader, &response);
+	ne_set_request_body_buffer(req, req_body->str, req_body->used);
+	ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type);
+	ast_log(LOG_NOTICE, "Content-type: %s\n", ast_strlen_zero(content_type) ? "text/xml" : content_type);
+
+	ret = ne_request_dispatch(req);
+
+#if 1
+	do {
+		void *cursor = NULL;
+		const char *name, *value;
+		while ((cursor = ne_response_header_iterate(req, cursor, &name, &value))) {
+			printf("%s: %s\n", name, value);
+		}
+	} while(0);
+	ast_log(LOG_NOTICE, "status is %d for request to %s\n", ne_get_status(req)->code, pvt->url);
+#endif
+
+	if (ret != NE_OK) {
+		ast_log(LOG_WARNING, "Unknown response to CalDAV calendar %s, request %s to %s: %s\n", pvt->owner->name, method, pvt->url, ne_get_error(pvt->session));
+		ast_free(response);
+		return NULL;
+	}
+
+	return response;
+}
+
+static int caldav_write_event(struct ast_calendar_event *event)
+{
+	struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
+	icalcomponent *calendar, *icalevent;
+	icaltimezone *utc = icaltimezone_get_utc_timezone();
+	int ret = -1;
+
+	if (!event) {
+		ast_log(LOG_WARNING, "No event passed!\n");
+		return -1;
+	}
+
+	if (!(event->start && event->end)) {
+		ast_log(LOG_WARNING, "The event must contain a start and an end\n");
+		return -1;
+	}
+	if (!(body = ast_str_create(512)) ||
+		!(subdir = ast_str_create(32)) ||
+		!(response = ast_str_create(512))) {
+		ast_log(LOG_ERROR, "Could not allocate memory for request and response!\n");
+		goto write_cleanup;
+	}
+
+	if (ast_strlen_zero(event->uid)) {
+		unsigned short val[8];
+		int x;
+		for (x = 0; x < 8; x++) {
+			val[x] = ast_random();
+		}
+		ast_string_field_build(event, uid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
+	}
+
+	calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
+	icalcomponent_add_property(calendar, icalproperty_new_version("2.0"));
+	icalcomponent_add_property(calendar, icalproperty_new_prodid("-//Digium, Inc.//res_caldav//EN"));
+
+	icalevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
+	icalcomponent_add_property(icalevent, icalproperty_new_dtstamp(icaltime_current_time_with_zone(utc)));
+	icalcomponent_add_property(icalevent, icalproperty_new_uid(event->uid));
+	icalcomponent_add_property(icalevent, icalproperty_new_dtstart(icaltime_from_timet_with_zone(event->start, 0, utc)));
+	icalcomponent_add_property(icalevent, icalproperty_new_dtend(icaltime_from_timet_with_zone(event->end, 0, utc)));
+	if (!ast_strlen_zero(event->organizer)) {
+		icalcomponent_add_property(icalevent, icalproperty_new_organizer(event->organizer));
+	}
+	if (!ast_strlen_zero(event->summary)) {
+		icalcomponent_add_property(icalevent, icalproperty_new_summary(event->summary));
+	}
+	if (!ast_strlen_zero(event->description)) {
+		icalcomponent_add_property(icalevent, icalproperty_new_description(event->description));
+	}
+	if (!ast_strlen_zero(event->location)) {
+		icalcomponent_add_property(icalevent, icalproperty_new_location(event->location));
+	}
+	switch (event->busy_state) {
+		case AST_CALENDAR_BS_BUSY : icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_CONFIRMED));break;
+		case AST_CALENDAR_BS_BUSY_TENTATIVE : icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_TENTATIVE));break;
+		default : icalcomponent_add_property(icalevent, icalproperty_new_status(ICAL_STATUS_NONE));break;
+	}
+	icalcomponent_add_component(calendar, icalevent);
+
+	ast_str_append(&body, 0, "%s", icalcomponent_as_ical_string(calendar));
+	ast_verb(0, "\n\n%s\n\n", body->str);
+	ast_str_set(&subdir, 0, "%s%s.ics", body->str[body->used] == '/' ? "" : "/", event->uid);
+
+	response = caldav_request(event->owner->tech_pvt, "PUT", body, subdir, "text/calendar");
+	ast_verb(1, "%s", response->str);
+
+	ret = 0;
+write_cleanup:
+	if (body) ast_free(body);
+	if (response) ast_free(response);
+	if (subdir) ast_free(subdir);
+
+	return ret;
+}
+
+static struct ast_str *caldav_get_events_between(struct caldav_pvt *pvt, time_t start_time, time_t end_time)
+{
+	struct ast_str *body, *response;
+	icaltimezone *utc = icaltimezone_get_utc_timezone();
+	icaltimetype start, end;
+	const char *start_str, *end_str;
+
+	if (!(body = ast_str_create(512))) {
+		ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
+		return NULL;
+	}
+
+	start = icaltime_from_timet_with_zone(start_time, 0, utc);
+	end = icaltime_from_timet_with_zone(end_time, 0, utc);
+	start_str = icaltime_as_ical_string(start);
+	end_str = icaltime_as_ical_string(end);
+
+	/* If I was really being efficient, I would store a collection of event URIs and etags,
+	 * first doing a query of just the etag and seeing if anything had changed.  If it had,
+	 * then I would do a request for each of the events that had changed, and only bother
+	 * updating those.  Oh well. */
+	ast_str_append(&body, 0,
+		"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
+		"<C:calendar-query xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n"
+		"  <D:prop>\n"
+		"    <C:calendar-data>\n"
+		"      <C:expand start=\"%s\" end=\"%s\"/>\n"
+		"    </C:calendar-data>\n"
+		"  </D:prop>\n"
+		"  <C:filter>\n"
+		"    <C:comp-filter name=\"VCALENDAR\">\n"
+		"      <C:comp-filter name=\"VEVENT\">\n"
+		"        <C:time-range start=\"%s\" end=\"%s\"/>\n"
+		"      </C:comp-filter>\n"
+		"    </C:comp-filter>\n"
+		"  </C:filter>\n"
+		"</C:calendar-query>\n", start_str, end_str, start_str, end_str);
+
+	response = caldav_request(pvt, "REPORT", body, NULL, NULL);
+	ast_free(body);
+
+	return response;
+}
+
+static void caldav_add_event(icalcomponent *comp, struct icaltime_span *span, void *data)
+{
+	struct caldav_pvt *pvt = data;
+	struct ast_calendar_event *event;
+	icaltimezone *utc = icaltimezone_get_utc_timezone();
+	icaltimetype start, end, tmp;
+	icalcomponent *valarm;
+	icalproperty *prop;
+	struct icaltriggertype trigger;
+
+	if (!(pvt && pvt->owner)) {
+		ast_log(LOG_ERROR, "Require a private structure with an ownenr\n");
+		return;
+	}
+
+	if (!(event = ast_calendar_event_alloc(pvt->owner))) {
+		ast_log(LOG_ERROR, "Could not allocate an event!\n");
+		return;
+	}
+
+	if (ast_string_field_init(event, 32)) {
+		ast_log(LOG_ERROR, "Could not allocate string fields for event!\n");
+		return;
+	}
+
+	start = icaltime_from_timet_with_zone(span->start, 0, utc);
+	end = icaltime_from_timet_with_zone(span->end, 0, utc);
+	event->start = span->start;
+	event->end = span->end;
+
+	switch(icalcomponent_get_status(comp))
+	{
+	case ICAL_STATUS_CONFIRMED : event->busy_state = AST_CALENDAR_BS_BUSY;break;
+	case ICAL_STATUS_TENTATIVE : event->busy_state = AST_CALENDAR_BS_BUSY_TENTATIVE;break;
+	default                    : event->busy_state = AST_CALENDAR_BS_FREE;
+	}
+
+	if ((prop = icalcomponent_get_first_property(comp, ICAL_SUMMARY_PROPERTY))) {
+		ast_string_field_set(event, summary, icalproperty_get_value_as_string(prop));
+	}
+
+	if ((prop = icalcomponent_get_first_property(comp, ICAL_DESCRIPTION_PROPERTY))) {
+		ast_string_field_set(event, description, icalproperty_get_value_as_string(prop));
+	}
+
+	if ((prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY))) {
+		ast_string_field_set(event, organizer, icalproperty_get_value_as_string(prop));
+	}
+
+	if ((prop = icalcomponent_get_first_property(comp, ICAL_LOCATION_PROPERTY))) {
+		ast_string_field_set(event, location, icalproperty_get_value_as_string(prop));
+	}
+
+	if ((prop = icalcomponent_get_first_property(comp, ICAL_UID_PROPERTY))) {
+		ast_string_field_set(event, uid, icalproperty_get_value_as_string(prop));
+	} else {
+		ast_log(LOG_WARNING, "No UID found, but one is required. Generating, but updates may not be acurate\n");
+		if (!ast_strlen_zero(event->summary)) {
+			ast_string_field_set(event, uid, event->summary);
+		} else {
+			char tmp[100];
+			snprintf(tmp, sizeof(tmp), "%lu", event->start);
+			ast_string_field_set(event, uid, tmp);
+		}
+	}
+
+	/* Only set values for alarm based on VALARM.  Can be overriden in main/calendar.c by autoreminder */
+	/* Currently we are only getting the first VALARM and are handling repitition in main/calendar.c from calendar.conf */
+	if (!(valarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT))) {
+		ao2_link(pvt->events, event);
+		event = ast_calendar_unref_event(event);
+		return;
+	}
+	
+	if (!(prop = icalcomponent_get_first_property(valarm, ICAL_TRIGGER_PROPERTY))) {
+		ast_log(LOG_WARNING, "VALARM has no TRIGGER, skipping!\n");
+		ao2_link(pvt->events, event);
+		event = ast_calendar_unref_event(event);
+		return;
+	}
+
+	trigger = icalproperty_get_trigger(prop);
+
+	if (icaltriggertype_is_null_trigger(trigger)) {
+		ast_log(LOG_WARNING, "Bad TRIGGER for VALARM, skipping!\n");
+		ao2_link(pvt->events, event);
+		event = ast_calendar_unref_event(event);
+		return;
+	}
+
+	if (!icaltime_is_null_time(trigger.time)) { /* This is an absolute time */
+		tmp = icaltime_convert_to_zone(trigger.time, utc);
+		event->alarm = icaltime_as_timet_with_zone(tmp, utc);
+	} else { /* Offset from either dtstart or dtend */
+		/* XXX Technically you can check RELATED to see if the event fires from the END of the event
+		 * But, I'm not sure I've ever seen anyone implement it in calendaring software, so I'm ignoring for now */
+		tmp = icaltime_add(start, trigger.duration);
+		event->alarm = icaltime_as_timet_with_zone(tmp, utc);
+	}
+
+	ao2_link(pvt->events, event);
+	event = ast_calendar_unref_event(event);
+
+	return;
+}
+
+struct xmlstate {
+	int in_caldata;
+	struct caldav_pvt *pvt;
+	struct ast_str *cdata;
+	time_t start;
+	time_t end;
+};
+
+static void handle_start_element(void *data, const xmlChar *fullname, const xmlChar **atts)
+{
+	struct xmlstate *state = data;
+
+	if (!xmlStrcasecmp(fullname, BAD_CAST "C:calendar-data")) {
+		state->in_caldata = 1;
+		ast_str_reset(state->cdata);
+	}
+}
+
+static void handle_end_element(void *data, const xmlChar *name)
+{
+	struct xmlstate *state = data;
+	struct icaltimetype start, end;
+	icaltimezone *utc = icaltimezone_get_utc_timezone();
+	icalcomponent *iter;
+	icalcomponent *comp;
+
+	if (xmlStrcasecmp(name, BAD_CAST "C:calendar-data")) {
+		return;
+	}
+
+	state->in_caldata = 0;
+	if (ast_strlen_zero(state->cdata->str)) {
+		return;
+	}
+	/* XXX Parse the calendar blurb for recurrence events in the time range,
+	 * create an event, and add it to pvt->events */
+	start = icaltime_from_timet_with_zone(state->start, 0, utc);
+	end = icaltime_from_timet_with_zone(state->end, 0, utc);
+	comp = icalparser_parse_string(state->cdata->str);
+
+	for (iter = icalcomponent_get_first_component(comp, ICAL_VEVENT_COMPONENT);
+	     iter;
+	     iter = icalcomponent_get_next_component(comp, ICAL_VEVENT_COMPONENT))
+	{
+		icalcomponent_foreach_recurrence(iter, start, end, caldav_add_event, state->pvt);
+	}
+
+	icalcomponent_free(comp);
+}
+
+static void handle_characters(void *data, const xmlChar *ch, int len)
+{
+	struct xmlstate *state = data;
+	xmlChar *tmp;
+
+	if (!state->in_caldata) {
+		return;
+	}
+
+	tmp = xmlStrndup(ch, len);
+	ast_str_append(&state->cdata, 0, "%s", (char *)tmp);
+	ast_free(tmp);
+}
+
+static int update_caldav(struct caldav_pvt *pvt)
+{
+	struct timeval now = ast_tvnow();
+	time_t start, end;
+	struct ast_str *response;
+	xmlSAXHandler saxHandler;
+	struct xmlstate state = {
+		.in_caldata = 0,
+		.pvt = pvt
+	};
+
+	start = now.tv_sec;
+	end = now.tv_sec + 60 * pvt->owner->timeframe;
+	if (!(response = caldav_get_events_between(pvt, start, end))) {
+		return -1;
+	}
+
+	if (!(state.cdata = ast_str_create(512))) {
+		ast_free(response);
+		return -1;
+	}
+
+	state.start = start;
+	state.end = end;
+
+	memset(&saxHandler, 0, sizeof(saxHandler));
+	saxHandler.startElement = handle_start_element;
+	saxHandler.endElement = handle_end_element;
+	saxHandler.characters = handle_characters;
+
+	xmlSAXUserParseMemory(&saxHandler, &state, response->str, response->used);
+
+	ast_calendar_merge_events(pvt->owner, pvt->events);
+
+	ast_free(response);
+	ast_free(state.cdata);
+
+	return 0;
+}
+
+static void *caldav_load_calendar(void *void_data)
+{
+	struct caldav_pvt *pvt;
+	struct ast_variable *v;
+	struct ast_calendar_load_data *data = void_data;
+	struct ast_config *cfg = data->cfg;
+	struct ast_calendar *cal = data->cal;
+	ast_mutex_t refreshlock;
+
+	if (!(cal && cfg)) {
+		ast_log(LOG_ERROR, "Required parameters not provided\n");
+		return NULL; 
+	}
+
+	if (!(pvt = ao2_alloc(sizeof(*pvt), caldav_destructor))) {
+		ast_log(LOG_ERROR, "Could not allocate caldav_pvt structure for calendar: %s\n", cal->name);
+		return NULL;
+	}
+
+	ao2_lock(cal);
+
+	pvt->owner = cal;
+
+	if (!(pvt->events = ast_calendar_event_container_alloc())) {
+		ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
+		pvt = unref_caldav(pvt);
+		return NULL;
+	}
+
+	if (ast_string_field_init(pvt, 32)) {
+		ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
+		pvt = unref_caldav(pvt);
+		return NULL;
+	}
+
+	for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
+		if (!strcasecmp(v->name, "url")) {
+			ast_string_field_set(pvt, url, v->value);
+		} else if (!strcasecmp(v->name, "user")) {
+			ast_string_field_set(pvt, user, v->value);
+		} else if (!strcasecmp(v->name, "secret")) {
+			ast_string_field_set(pvt, secret, v->value);
+		}
+	}
+
+	if (ast_strlen_zero(pvt->url)) {
+		ast_log(LOG_WARNING, "No URL was specified for iCalendar '%s' - skipping.\n", cal->name);
+		pvt = unref_caldav(pvt);
+		return NULL;
+	}
+
+	if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
+		ast_log(LOG_WARNING, "Could not parse url '%s' for iCalendar '%s' - skipping.\n", pvt->url, cal->name); 
+		pvt = unref_caldav(pvt);
+		return NULL;
+	}
+
+	if (pvt->uri.scheme == NULL) {
+		pvt->uri.scheme = "http";
+	}
+
+	if (pvt->uri.port == 0) {
+		pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
+	}
+	
+	pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
+	ne_set_server_auth(pvt->session, auth_credentials, pvt);
+	if (!strncasecmp(pvt->uri.scheme, "https", sizeof(pvt->uri.scheme))) {
+		ne_ssl_trust_default_ca(pvt->session);
+	}
+
+	cal->tech_pvt = pvt;
+	
+	ast_mutex_init(&refreshlock);
+
+	/* Load it the first time */
+	update_caldav(pvt);
+
+	ao2_unlock(cal);
+
+	cfg = NULL;
+	cal = NULL;
+	ast_free(data);
+
+	/* The only writing from another thread will be if unload is true */
+	for(;;) {
+		struct timeval tv = ast_tvnow();
+		struct timespec ts = {0,};
+
+		ts.tv_sec = tv.tv_sec + pvt->owner->refresh;
+
+		ast_mutex_lock(&refreshlock);
+		while (!pvt->owner->unloading) {
+			if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
+				break;
+			}
+		}
+		ast_mutex_unlock(&refreshlock);
+
+		if (pvt->owner->unloading) {
+			ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
+			return NULL;
+		}
+
+		ast_debug(10, "Refreshing after %d second timeout\n", pvt->owner->refresh);
+		
+		update_caldav(pvt);
+	}
+
+	return NULL;
+}
+
+static int load_module(void)
+{
+	ne_sock_init();
+	ast_calendar_register(&caldav_tech);
+	return 0;
+}
+
+static int unload_module(void)
+{
+	ast_calendar_unregister(&caldav_tech);
+	ne_sock_exit();
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Asterisk MS Exchange Calendar Integration",
+		.load = load_module,
+		.unload = unload_module,
+	);

Propchange: team/twilson/calendaring/res/res_caldav.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/calendaring/res/res_caldav.c
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: team/twilson/calendaring/res/res_caldav.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/twilson/calendaring/res/res_exchangecal.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_exchangecal.c?view=diff&rev=155709&r1=155708&r2=155709
==============================================================================
--- team/twilson/calendaring/res/res_exchangecal.c (original)
+++ team/twilson/calendaring/res/res_exchangecal.c Mon Nov 10 01:38:51 2008
@@ -27,7 +27,7 @@
 ***/
 #include "asterisk.h"
 
-#include <ical.h>
+#include <libical/ical.h>
 #include <neon/ne_session.h>
 #include <neon/ne_uri.h>
 #include <neon/ne_request.h>
@@ -298,7 +298,11 @@
 
 static struct ast_str *bs_to_exchange_bs(struct ast_str *dst, enum ast_calendar_busy_state bs)
 {
-	ast_str_set(&dst, 0, "%s", "BUSY");
+	switch (bs) {
+		case AST_CALENDAR_BS_BUSY : ast_str_set(&dst, 0, "%s", "BUSY"); break;
+		case AST_CALENDAR_BS_BUSY_TENTATIVE : ast_str_set(&dst, 0, "%s", "TENTATIVE"); break;
+		default : ast_str_set(&dst, 0, "%s", "FREE"); break;
+	}
 
 	return dst;
 }

Modified: team/twilson/calendaring/res/res_icalendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_icalendar.c?view=diff&rev=155709&r1=155708&r2=155709
==============================================================================
--- team/twilson/calendaring/res/res_icalendar.c (original)
+++ team/twilson/calendaring/res/res_icalendar.c Mon Nov 10 01:38:51 2008
@@ -26,7 +26,7 @@
 ***/
 #include "asterisk.h"
 
-#include <ical.h>
+#include <libical/ical.h>
 #include <neon/ne_session.h>
 #include <neon/ne_uri.h>
 #include <neon/ne_request.h>
@@ -171,11 +171,10 @@
 		return;
 	}
 
-    /* This is a workaround as icaltime_as_timet_with_zone seems to be broken */
-	start = icaltime_convert_to_zone(icalcomponent_get_dtstart(comp), utc);
-	event->start = icaltime_as_timet_with_zone(start, utc);
-	end = icaltime_convert_to_zone(icalcomponent_get_dtend(comp), utc);
-	event->end = icaltime_as_timet_with_zone(end, utc);
+	start = icaltime_from_timet_with_zone(span->start, 0, utc);
+	end = icaltime_from_timet_with_zone(span->end, 0, utc);
+	event->start = span->start;
+	event->end = span->end;
 
 	switch(icalcomponent_get_status(comp))
 	{




More information about the asterisk-commits mailing list