Sat Mar 26 11:26:12 2011

Asterisk developer's documentation


res_calendar_exchange.c File Reference

Resource for handling MS Exchange 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 <iksemel.h>
#include "asterisk/module.h"
#include "asterisk/calendar.h"
#include "asterisk/lock.h"
#include "asterisk/config.h"
#include "asterisk/astobj2.h"

Include dependency graph for res_calendar_exchange.c:

Go to the source code of this file.

Data Structures

struct  exchangecal_pvt
struct  xmlstate

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int auth_credentials (void *userdata, const char *realm, int attempts, char *username, char *secret)
static struct ast_str * bs_to_exchange_bs (struct ast_str *dst, enum ast_calendar_busy_state bs)
static struct ast_str * epoch_to_exchange_time (struct ast_str *dst, time_t epoch)
static void exchangecal_destructor (void *obj)
static struct ast_str * exchangecal_get_events_between (struct exchangecal_pvt *pvt, time_t start_time, time_t end_time)
static void * exchangecal_load_calendar (void *data)
static struct ast_str * exchangecal_request (struct exchangecal_pvt *pvt, const char *method, struct ast_str *req_body, struct ast_str *subdir)
static int exchangecal_write_event (struct ast_calendar_event *event)
static int fetch_response_reader (void *data, const char *block, size_t len)
static struct ast_str * generate_exchange_uuid (struct ast_str *uid)
static int is_valid_uuid (struct ast_str *uid)
static int load_module (void)
static enum ast_calendar_busy_state msbusy_to_bs (const char *msbusy)
static time_t mstime_to_time_t (char *mstime)
static int parse_cdata (void *data, char *value, size_t len)
static int parse_tag (void *data, char *name, char **atts, int type)
static int unload_module (void)
static void * unref_exchangecal (void *obj)
static int update_exchangecal (struct exchangecal_pvt *pvt)
static struct ast_str * xml_encode_str (struct ast_str *dst, const char *src)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk MS Exchange Calendar Integration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PLUGIN, }
static struct ast_module_info * ast_module_info = &__mod_info
static struct ast_calendar_tech exchangecal_tech


Detailed Description

Resource for handling MS Exchange calendars.

Definition in file res_calendar_exchange.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 762 of file res_calendar_exchange.c.

static void __unreg_module ( void   )  [static]

Definition at line 762 of file res_calendar_exchange.c.

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

Definition at line 366 of file res_calendar_exchange.c.

References ast_log(), LOG_WARNING, ast_calendar::name, exchangecal_pvt::owner, exchangecal_pvt::secret, and exchangecal_pvt::user.

Referenced by exchangecal_load_calendar().

00367 {
00368    struct exchangecal_pvt *pvt = userdata;
00369 
00370    if (attempts > 1) {
00371       ast_log(LOG_WARNING, "Invalid username or password for Exchange calendar '%s'\n", pvt->owner->name);
00372       return -1;
00373    }
00374 
00375    ne_strnzcpy(username, pvt->user, NE_ABUFSIZ);
00376    ne_strnzcpy(secret, pvt->secret, NE_ABUFSIZ);
00377 
00378    return 0;
00379 }

static struct ast_str* bs_to_exchange_bs ( struct ast_str *  dst,
enum ast_calendar_busy_state  bs 
) [static, read]

Definition at line 332 of file res_calendar_exchange.c.

References AST_CALENDAR_BS_BUSY, AST_CALENDAR_BS_BUSY_TENTATIVE, and ast_str_set().

Referenced by exchangecal_write_event().

00333 {
00334    switch (bs) {
00335    case AST_CALENDAR_BS_BUSY:
00336       ast_str_set(&dst, 0, "%s", "BUSY");
00337       break;
00338 
00339    case AST_CALENDAR_BS_BUSY_TENTATIVE:
00340       ast_str_set(&dst, 0, "%s", "TENTATIVE");
00341       break;
00342 
00343    default:
00344       ast_str_set(&dst, 0, "%s", "FREE");
00345    }
00346 
00347    return dst;
00348 }

static struct ast_str* epoch_to_exchange_time ( struct ast_str *  dst,
time_t  epoch 
) [static, read]

Definition at line 311 of file res_calendar_exchange.c.

References ast_copy_string(), and ast_str_append().

Referenced by exchangecal_write_event().

00312 {
00313    icaltimezone *utc = icaltimezone_get_utc_timezone();
00314    icaltimetype tt = icaltime_from_timet_with_zone(epoch, 0, utc);
00315    char tmp[30];
00316    int i;
00317 
00318    ast_copy_string(tmp, icaltime_as_ical_string(tt), sizeof(tmp));
00319    for (i = 0; tmp[i]; i++) {
00320       ast_str_append(&dst, 0, "%c", tmp[i]);
00321       if (i == 3 || i == 5)
00322          ast_str_append(&dst, 0, "%c", '-');
00323       if (i == 10 || i == 12)
00324          ast_str_append(&dst, 0, "%c", ':');
00325       if (i == 14)
00326          ast_str_append(&dst, 0, "%s", ".000");
00327    }
00328 
00329    return dst;
00330 }

static void exchangecal_destructor ( void *  obj  )  [static]

Definition at line 216 of file res_calendar_exchange.c.

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

Referenced by exchangecal_load_calendar().

00217 {
00218    struct exchangecal_pvt *pvt = obj;
00219 
00220    ast_debug(1, "Destroying pvt for Exchange calendar %s\n", pvt->owner->name);
00221    if (pvt->session) {
00222       ne_session_destroy(pvt->session);
00223    }
00224    ast_string_field_free_memory(pvt);
00225 
00226    ao2_callback(pvt->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
00227 
00228    ao2_ref(pvt->events, -1);
00229 }

static struct ast_str* exchangecal_get_events_between ( struct exchangecal_pvt *  pvt,
time_t  start_time,
time_t  end_time 
) [static, read]

Definition at line 547 of file res_calendar_exchange.c.

References ast_debug, ast_free, ast_localtime(), ast_log(), ast_str_append(), ast_str_buffer(), ast_str_create(), ast_strftime(), exchangecal_request(), LOG_ERROR, and exchangecal_pvt::url.

Referenced by update_exchangecal().

00548 {
00549    struct ast_str *body, *response;
00550    char start[80], end[80];
00551    struct timeval tv = {0,};
00552    struct ast_tm tm;
00553 
00554    tv.tv_sec = start_time;
00555    ast_localtime(&tv, &tm, "UTC");
00556    ast_strftime(start, sizeof(start), "%Y/%m/%d %T", &tm);
00557 
00558    tv.tv_sec = end_time;
00559    ast_localtime(&tv, &tm, "UTC");
00560    ast_strftime(end, sizeof(end), "%Y/%m/%d %T", &tm);
00561 
00562    if (!(body = ast_str_create(512))) {
00563       ast_log(LOG_ERROR, "Could not allocate memory for body of request!\n");
00564       return NULL;
00565    }
00566 
00567    ast_str_append(&body, 0,
00568       "<?xml version=\"1.0\"?>\n"
00569       "<g:searchrequest xmlns:g=\"DAV:\">\n"
00570       "        <g:sql> SELECT \"urn:schemas:calendar:location\", \"urn:schemas:httpmail:subject\",\n"
00571       "                \"urn:schemas:calendar:dtstart\", \"urn:schemas:calendar:dtend\",\n"
00572       "                \"urn:schemas:calendar:busystatus\", \"urn:schemas:calendar:instancetype\",\n"
00573       "                \"urn:schemas:calendar:uid\", \"urn:schemas:httpmail:textdescription\",\n"
00574       "                \"urn:schemas:calendar:organizer\", \"urn:schemas:calendar:reminderoffset\"\n"
00575       "                FROM Scope('SHALLOW TRAVERSAL OF \"%s/Calendar\"')\n"
00576       "                WHERE NOT \"urn:schemas:calendar:instancetype\" = 1\n"
00577       "                AND \"DAV:contentclass\" = 'urn:content-classes:appointment'\n"
00578       "                AND NOT (\"urn:schemas:calendar:dtend\" &lt; '%s'\n"
00579       "                OR \"urn:schemas:calendar:dtstart\" &gt; '%s')\n"
00580       "                ORDER BY \"urn:schemas:calendar:dtstart\" ASC\n"
00581       "         </g:sql>\n"
00582       "</g:searchrequest>\n", pvt->url, start, end);
00583 
00584    ast_debug(5, "Request:\n%s\n", ast_str_buffer(body));
00585    response = exchangecal_request(pvt, "SEARCH", body, NULL);
00586    ast_debug(5, "Response:\n%s\n", ast_str_buffer(response));
00587    ast_free(body);
00588 
00589    return response;
00590 }

static void * exchangecal_load_calendar ( void *  data  )  [static]

Definition at line 615 of file res_calendar_exchange.c.

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(), exchangecal_pvt::events, exchangecal_destructor(), LOG_ERROR, LOG_WARNING, ast_variable::name, ast_calendar::name, ast_variable::next, exchangecal_pvt::owner, ast_calendar::refresh, refreshlock, secret, exchangecal_pvt::session, ast_calendar::tech_pvt, ast_calendar::unload, ast_calendar::unloading, unref_exchangecal(), update_exchangecal(), exchangecal_pvt::uri, exchangecal_pvt::url, url, and ast_variable::value.

00616 {
00617    struct exchangecal_pvt *pvt;
00618    const struct ast_config *cfg;
00619    struct ast_variable *v;
00620    struct ast_calendar *cal = void_data;
00621    ast_mutex_t refreshlock;
00622 
00623    if (!(cal && (cfg = ast_calendar_config_acquire()))) {
00624       ast_log(LOG_ERROR, "You must enable calendar support for res_exchangecal to load\n");
00625       return NULL;
00626    }
00627 
00628    if (ao2_trylock(cal)) {
00629       if (cal->unloading) {
00630          ast_log(LOG_WARNING, "Unloading module, load_calendar cancelled.\n");
00631       } else {
00632          ast_log(LOG_WARNING, "Could not lock calendar, aborting!\n");
00633       }
00634       ast_calendar_config_release();
00635       return NULL;
00636    }
00637 
00638    if (!(pvt = ao2_alloc(sizeof(*pvt), exchangecal_destructor))) {
00639       ast_log(LOG_ERROR, "Could not allocate exchangecal_pvt structure for calendar: %s\n", cal->name);
00640       ast_calendar_config_release();
00641       return NULL;
00642    }
00643 
00644    pvt->owner = cal;
00645 
00646    if (!(pvt->events = ast_calendar_event_container_alloc())) {
00647       ast_log(LOG_ERROR, "Could not allocate space for fetching events for calendar: %s\n", cal->name);
00648       pvt = unref_exchangecal(pvt);
00649       ao2_unlock(cal);
00650       ast_calendar_config_release();
00651       return NULL;
00652    }
00653 
00654    if (ast_string_field_init(pvt, 32)) {
00655       ast_log(LOG_ERROR, "Couldn't allocate string field space for calendar: %s\n", cal->name);
00656       pvt = unref_exchangecal(pvt);
00657       ao2_unlock(cal);
00658       ast_calendar_config_release();
00659       return NULL;
00660    }
00661 
00662    for (v = ast_variable_browse(cfg, cal->name); v; v = v->next) {
00663       if (!strcasecmp(v->name, "url")) {
00664          ast_string_field_set(pvt, url, v->value);
00665       } else if (!strcasecmp(v->name, "user")) {
00666          ast_string_field_set(pvt, user, v->value);
00667       } else if (!strcasecmp(v->name, "secret")) {
00668          ast_string_field_set(pvt, secret, v->value);
00669       }
00670    }
00671 
00672    ast_calendar_config_release();
00673 
00674    if (ast_strlen_zero(pvt->url)) {
00675       ast_log(LOG_WARNING, "No URL was specified for Exchange calendar '%s' - skipping.\n", cal->name);
00676       pvt = unref_exchangecal(pvt);
00677       ao2_unlock(cal);
00678       return NULL;
00679    }
00680 
00681    if (ne_uri_parse(pvt->url, &pvt->uri) || pvt->uri.host == NULL || pvt->uri.path == NULL) {
00682       ast_log(LOG_WARNING, "Could not parse url '%s' for Exchange calendar '%s' - skipping.\n", pvt->url, cal->name);
00683       pvt = unref_exchangecal(pvt);
00684       ao2_unlock(cal);
00685       return NULL;
00686    }
00687 
00688    if (pvt->uri.scheme == NULL) {
00689       pvt->uri.scheme = "http";
00690    }
00691 
00692    if (pvt->uri.port == 0) {
00693       pvt->uri.port = ne_uri_defaultport(pvt->uri.scheme);
00694    }
00695 
00696    pvt->session = ne_session_create(pvt->uri.scheme, pvt->uri.host, pvt->uri.port);
00697    ne_redirect_register(pvt->session);
00698    ne_set_server_auth(pvt->session, auth_credentials, pvt);
00699    if (!strcasecmp(pvt->uri.scheme, "https")) {
00700       ne_ssl_trust_default_ca(pvt->session);
00701    }
00702 
00703    cal->tech_pvt = pvt;
00704 
00705    ast_mutex_init(&refreshlock);
00706 
00707    /* Load it the first time */
00708    update_exchangecal(pvt);
00709 
00710    ao2_unlock(cal);
00711 
00712    /* The only writing from another thread will be if unload is true */
00713    for (;;) {
00714       struct timeval tv = ast_tvnow();
00715       struct timespec ts = {0,};
00716 
00717       ts.tv_sec = tv.tv_sec + (60 * pvt->owner->refresh);
00718 
00719       ast_mutex_lock(&refreshlock);
00720       while (!pvt->owner->unloading) {
00721          if (ast_cond_timedwait(&pvt->owner->unload, &refreshlock, &ts) == ETIMEDOUT) {
00722             break;
00723          }
00724       }
00725       ast_mutex_unlock(&refreshlock);
00726 
00727       if (pvt->owner->unloading) {
00728          ast_debug(10, "Skipping refresh since we got a shutdown signal\n");
00729          return NULL;
00730       }
00731 
00732       ast_debug(10, "Refreshing after %d minute timeout\n", pvt->owner->refresh);
00733 
00734       update_exchangecal(pvt);
00735    }
00736 
00737    return NULL;
00738 }

static struct ast_str* exchangecal_request ( struct exchangecal_pvt *  pvt,
const char *  method,
struct ast_str *  req_body,
struct ast_str *  subdir 
) [static, read]

Definition at line 381 of file res_calendar_exchange.c.

References ast_free, ast_log(), ast_str_buffer(), ast_str_create(), ast_str_strlen(), fetch_response_reader(), LOG_ERROR, LOG_WARNING, ast_calendar::name, exchangecal_pvt::owner, exchangecal_pvt::session, exchangecal_pvt::uri, and exchangecal_pvt::url.

Referenced by exchangecal_get_events_between(), and exchangecal_write_event().

00382 {
00383    struct ast_str *response;
00384    ne_request *req;
00385    int ret;
00386    char buf[1000];
00387 
00388    if (!pvt) {
00389       ast_log(LOG_ERROR, "There is no private!\n");
00390       return NULL;
00391    }
00392 
00393    if (!(response = ast_str_create(512))) {
00394       ast_log(LOG_ERROR, "Could not allocate memory for response.\n");
00395       return NULL;
00396    }
00397 
00398    snprintf(buf, sizeof(buf), "%s%s", pvt->uri.path, subdir ? ast_str_buffer(subdir) : "");
00399 
00400    req = ne_request_create(pvt->session, method, buf);
00401    ne_add_response_body_reader(req, ne_accept_2xx, fetch_response_reader, &response);
00402    ne_set_request_body_buffer(req, ast_str_buffer(req_body), ast_str_strlen(req_body));
00403    ne_add_request_header(req, "Content-type", "text/xml");
00404 
00405    ret = ne_request_dispatch(req);
00406 
00407    if (ret != NE_OK || !ast_str_strlen(response)) {
00408       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));
00409       ast_free(response);
00410       return NULL;
00411    }
00412 
00413    return response;
00414 }

static int exchangecal_write_event ( struct ast_calendar_event *  event  )  [static]

Definition at line 416 of file res_calendar_exchange.c.

References ast_free, ast_log(), ast_str_append(), ast_str_buffer(), ast_str_create(), ast_str_set(), ast_strlen_zero(), ast_verb, bs_to_exchange_bs(), ast_calendar_event::busy_state, ast_calendar_event::description, ast_calendar_event::end, epoch_to_exchange_time(), exchangecal_request(), generate_exchange_uuid(), is_valid_uuid(), ast_calendar_event::location, LOG_ERROR, LOG_WARNING, ast_calendar_event::organizer, ast_calendar_event::owner, ast_calendar_event::start, ast_calendar_event::summary, ast_calendar::tech_pvt, ast_calendar_event::uid, and xml_encode_str().

00417 {
00418    struct ast_str *body = NULL, *response = NULL, *subdir = NULL;
00419    struct ast_str *uid = NULL, *summary = NULL, *description = NULL, *organizer = NULL,
00420                   *location = NULL, *start = NULL, *end = NULL, *busystate = NULL;
00421    int ret = -1;
00422 
00423    if (!event) {
00424       ast_log(LOG_WARNING, "No event passed!\n");
00425       return -1;
00426    }
00427 
00428    if (!(event->start && event->end)) {
00429       ast_log(LOG_WARNING, "The event must contain a start and an end\n");
00430       return -1;
00431    }
00432    if (!(body = ast_str_create(512)) ||
00433       !(subdir = ast_str_create(32)) ||
00434       !(response = ast_str_create(512))) {
00435       ast_log(LOG_ERROR, "Could not allocate memory for request and response!\n");
00436       goto write_cleanup;
00437    }
00438 
00439    if (!(uid = ast_str_create(32)) ||
00440       !(summary = ast_str_create(32)) ||
00441       !(description = ast_str_create(32)) ||
00442       !(organizer = ast_str_create(32)) ||
00443       !(location = ast_str_create(32)) ||
00444       !(start = ast_str_create(32)) ||
00445       !(end = ast_str_create(32)) ||
00446       !(busystate = ast_str_create(32))) {
00447       ast_log(LOG_ERROR, "Unable to allocate memory for request values\n");
00448       goto write_cleanup;
00449    }
00450 
00451    if (ast_strlen_zero(event->uid)) {
00452       uid = generate_exchange_uuid(uid);
00453    } else {
00454       ast_str_set(&uid, 36, "%s", event->uid);
00455    }
00456 
00457    if (!is_valid_uuid(uid)) {
00458       ast_log(LOG_WARNING, "An invalid uid was provided, you may leave this field blank to have one generated for you\n");
00459       goto write_cleanup;
00460    }
00461 
00462    summary = xml_encode_str(summary, event->summary);
00463    description = xml_encode_str(description, event->description);
00464    organizer = xml_encode_str(organizer, event->organizer);
00465    location = xml_encode_str(location, event->location);
00466    start = epoch_to_exchange_time(start, event->start);
00467    end = epoch_to_exchange_time(end, event->end);
00468    busystate = bs_to_exchange_bs(busystate, event->busy_state);
00469 
00470    ast_str_append(&body, 0,
00471       "<?xml version=\"1.0\"?>\n"
00472       "<a:propertyupdate\n"
00473       "  xmlns:a=\"DAV:\"\n"
00474       "  xmlns:e=\"http://schemas.microsoft.com/exchange/\"\n"
00475       "  xmlns:mapi=\"http://schemas.microsoft.com/mapi/\"\n"
00476       "  xmlns:mapit=\"http://schemas.microsoft.com/mapi/proptag/\"\n"
00477       "  xmlns:x=\"xml:\" xmlns:cal=\"urn:schemas:calendar:\"\n"
00478       "  xmlns:dt=\"uuid:%s/\"\n" /* uid */
00479       "  xmlns:header=\"urn:schemas:mailheader:\"\n"
00480       "  xmlns:mail=\"urn:schemas:httpmail:\"\n"
00481       ">\n"
00482       "    <a:set>\n"
00483       "      <a:prop>\n"
00484       "        <a:contentclass>urn:content-classes:appointment</a:contentclass>\n"
00485       "        <e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>\n"
00486       "        <mail:subject>%s</mail:subject>\n" /* summary */
00487       "        <mail:description>%s</mail:description>\n" /* description */
00488       "        <header:to>%s</header:to>\n" /* organizer */
00489       "        <cal:location>%s</cal:location>\n" /* location */
00490       "        <cal:dtstart dt:dt=\"dateTime.tz\">%s</cal:dtstart>\n" /* start */
00491       "        <cal:dtend dt:dt=\"dateTime.tz\">%s</cal:dtend>\n" /* end */
00492       "        <cal:instancetype dt:dt=\"int\">0</cal:instancetype>\n"
00493       "        <cal:busystatus>%s</cal:busystatus>\n" /* busy_state (BUSY, FREE, BUSY_TENTATIVE) */
00494       "        <cal:meetingstatus>CONFIRMED</cal:meetingstatus>\n"
00495       "        <cal:alldayevent dt:dt=\"boolean\">0</cal:alldayevent>\n" /* XXX need to add event support for all day events */
00496       "        <cal:responserequested dt:dt=\"boolean\">0</cal:responserequested>\n"
00497       "        <mapi:finvited dt:dt=\"boolean\">1</mapi:finvited>\n"
00498       "      </a:prop>\n"
00499       "    </a:set>\n"
00500       "</a:propertyupdate>\n",
00501       ast_str_buffer(uid), ast_str_buffer(summary), ast_str_buffer(description), ast_str_buffer(organizer), ast_str_buffer(location), ast_str_buffer(start), ast_str_buffer(end), ast_str_buffer(busystate));
00502    ast_verb(0, "\n\n%s\n\n", ast_str_buffer(body));
00503    ast_str_set(&subdir, 0, "/Calendar/%s.eml", ast_str_buffer(uid));
00504 
00505    response = exchangecal_request(event->owner->tech_pvt, "PROPPATCH", body, subdir);
00506 
00507    ret = 0;
00508 write_cleanup:
00509    if (uid) {
00510       ast_free(uid);
00511    }
00512    if (summary) {
00513       ast_free(summary);
00514    }
00515    if (description) {
00516       ast_free(description);
00517    }
00518    if (organizer) {
00519       ast_free(organizer);
00520    }
00521    if (location) {
00522       ast_free(location);
00523    }
00524    if (start) {
00525       ast_free(start);
00526    }
00527    if (end) {
00528       ast_free(end);
00529    }
00530    if (busystate) {
00531       ast_free(busystate);
00532    }
00533    if (body) {
00534       ast_free(body);
00535    }
00536    if (response) {
00537       ast_free(response);
00538    }
00539    if (subdir) {
00540       ast_free(subdir);
00541    }
00542 
00543    return ret;
00544 }

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

Definition at line 350 of file res_calendar_exchange.c.

References ast_free, ast_malloc, and ast_str_append().

Referenced by exchangecal_request().

00351 {
00352    struct ast_str **response = data;
00353    unsigned char *tmp;
00354 
00355    if (!(tmp = ast_malloc(len + 1))) {
00356       return -1;
00357    }
00358    memcpy(tmp, block, len);
00359    tmp[len] = '\0';
00360    ast_str_append(response, 0, "%s", tmp);
00361    ast_free(tmp);
00362 
00363    return 0;
00364 }

static struct ast_str* generate_exchange_uuid ( struct ast_str *  uid  )  [static, read]

Definition at line 240 of file res_calendar_exchange.c.

References ast_random(), and ast_str_set().

Referenced by exchangecal_write_event().

00241 {
00242    unsigned short val[8];
00243    int x;
00244 
00245    for (x = 0; x < 8; x++) {
00246       val[x] = ast_random();
00247    }
00248    ast_str_set(&uid, 0, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
00249 
00250    return uid;
00251 }

static int is_valid_uuid ( struct ast_str *  uid  )  [static]

Definition at line 253 of file res_calendar_exchange.c.

References ast_str_buffer(), and ast_str_strlen().

Referenced by exchangecal_write_event().

00254 {
00255    int i;
00256 
00257    if (ast_str_strlen(uid) != 36) {
00258       return 0;
00259    }
00260 
00261    for (i = 0; i < ast_str_strlen(uid); i++) {
00262       if (i == 8 || i == 13 || i == 18 || i == 23) {
00263          if (ast_str_buffer(uid)[i] != '-') {
00264             return 0;
00265          }
00266       } else if (!((ast_str_buffer(uid)[i] > 47 && ast_str_buffer(uid)[i] < 58) || (ast_str_buffer(uid)[i] > 96 && ast_str_buffer(uid)[i] < 103))) {
00267          return 0;
00268       }
00269    }
00270 
00271    return 1;
00272 }

static int load_module ( void   )  [static]

Definition at line 740 of file res_calendar_exchange.c.

References ast_calendar_register(), AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

00741 {
00742    ne_sock_init();
00743    if (ast_calendar_register(&exchangecal_tech)) {
00744       ne_sock_exit();
00745       return AST_MODULE_LOAD_DECLINE;
00746    }
00747 
00748    return AST_MODULE_LOAD_SUCCESS;
00749 }

static enum ast_calendar_busy_state msbusy_to_bs ( const char *  msbusy  )  [static]

Definition at line 157 of file res_calendar_exchange.c.

References AST_CALENDAR_BS_BUSY, AST_CALENDAR_BS_BUSY_TENTATIVE, and AST_CALENDAR_BS_FREE.

Referenced by parse_cdata().

00158 {
00159    if (!strcasecmp(msbusy, "FREE")) {
00160       return AST_CALENDAR_BS_FREE;
00161    } else if (!strcasecmp(msbusy, "TENTATIVE")) {
00162       return AST_CALENDAR_BS_BUSY_TENTATIVE;
00163    } else {
00164       return AST_CALENDAR_BS_BUSY;
00165    }
00166 }

static time_t mstime_to_time_t ( char *  mstime  )  [static]

Definition at line 137 of file res_calendar_exchange.c.

Referenced by parse_cdata().

00138 {
00139    char *read, *write;
00140    icaltimetype tt;
00141    for (read = write = mstime; *read; read++) {
00142       if (*read == '.') {
00143          *write++ = 'Z';
00144          *write = '\0';
00145          break;
00146       }
00147       if (*read == '-' || *read == ':')
00148          continue;
00149       *write = *read;
00150       write++;
00151    }
00152 
00153    tt = icaltime_from_string(mstime);
00154    return icaltime_as_timet(tt);
00155 }

static int parse_cdata ( void *  data,
char *  value,
size_t  len 
) [static]

Definition at line 168 of file res_calendar_exchange.c.

References ast_calloc, ast_free, ast_skip_blanks(), ast_string_field_build, ast_calendar_event::description, xmlstate::in_prop, xmlstate::in_propstat, xmlstate::in_response, ast_calendar_event::location, msbusy_to_bs(), mstime_to_time_t(), ast_calendar_event::organizer, xmlstate::ptr, str, ast_calendar_event::summary, xmlstate::tag, and ast_calendar_event::uid.

Referenced by update_exchangecal().

00169 {
00170    char *str;
00171    struct xmlstate *state = data;
00172    struct ast_calendar_event *event = state->ptr;
00173 
00174 
00175    str = ast_skip_blanks(value);
00176 
00177    if (str == value + len)
00178       return IKS_OK;
00179 
00180    if (!(str = ast_calloc(1, len + 1))) {
00181       return IKS_NOMEM;
00182    }
00183    memcpy(str, value, len);
00184    if (!(state->in_response && state->in_propstat && state->in_prop)) {
00185       ast_free(str);
00186       return IKS_OK;
00187    }
00188    /* We use ast_string_field_build here because libiksemel is parsing CDATA with &lt; as
00189     * new elements which is a bit odd and shouldn't happen */
00190    if (!strcasecmp(state->tag, "subject")) {
00191       ast_string_field_build(event, summary, "%s%s", event->summary, str);
00192    } else if (!strcasecmp(state->tag, "location")) {
00193       ast_string_field_build(event, location, "%s%s", event->location, str);
00194    } else if (!strcasecmp(state->tag, "uid")) {
00195       ast_string_field_build(event, uid, "%s%s", event->location, str);
00196    } else if (!strcasecmp(state->tag, "organizer")) {
00197       ast_string_field_build(event, organizer, "%s%s", event->organizer, str);
00198    } else if (!strcasecmp(state->tag, "textdescription")) {
00199       ast_string_field_build(event, description, "%s%s", event->description, str);
00200    } else if (!strcasecmp(state->tag, "dtstart")) {
00201       event->start = mstime_to_time_t(str);
00202    } else if (!strcasecmp(state->tag, "dtend")) {
00203       event->end = mstime_to_time_t(str);
00204    } else if (!strcasecmp(state->tag, "busystatus")) {
00205       event->busy_state = msbusy_to_bs(str);
00206    } else if (!strcasecmp(state->tag, "reminderoffset")) {
00207       /*XXX Currently we rely on event->start being set first which means we rely on the response order
00208        * which technically should be fine since the query returns in the order we ask for, but ... */
00209       event->alarm = event->start - atoi(str);
00210    }
00211 
00212    ast_free(str);
00213    return IKS_OK;
00214 }

static int parse_tag ( void *  data,
char *  name,
char **  atts,
int  type 
) [static]

Definition at line 80 of file res_calendar_exchange.c.

References ao2_link, ast_calendar_event_alloc(), ast_calendar_unref_event(), ast_copy_string(), ast_log(), ast_strlen_zero(), caldav_pvt::events, events, xmlstate::in_prop, xmlstate::in_propstat, xmlstate::in_response, LOG_ERROR, caldav_pvt::owner, xmlstate::ptr, xmlstate::pvt, and xmlstate::tag.

Referenced by update_exchangecal().

00081 {
00082    struct xmlstate *state = data;
00083    char *tmp;
00084 
00085    if ((tmp = strchr(name, ':'))) {
00086       tmp++;
00087    } else {
00088       return IKS_HOOK;
00089    }
00090 
00091    ast_copy_string(state->tag, tmp, sizeof(state->tag));
00092 
00093    switch (type) {
00094    case IKS_OPEN:
00095       if (!strcasecmp(state->tag, "response")) {
00096          struct ast_calendar_event *event;
00097 
00098          state->in_response = 1;
00099          if (!(event = ast_calendar_event_alloc(state->pvt->owner))) {
00100             return IKS_NOMEM;
00101          }
00102          state->ptr = event;
00103       } else if (!strcasecmp(state->tag, "propstat")) {
00104          state->in_propstat = 1;
00105       } else if (!strcasecmp(state->tag, "prop")) {
00106          state->in_prop = 1;
00107       }
00108       break;
00109 
00110    case IKS_CLOSE:
00111       if (!strcasecmp(state->tag, "response")) {
00112          struct ao2_container *events = state->pvt->events;
00113          struct ast_calendar_event *event = state->ptr;
00114 
00115          state->in_response = 0;
00116          if (ast_strlen_zero(event->uid)) {
00117             ast_log(LOG_ERROR, "This event has no UID, something has gone wrong\n");
00118             event = ast_calendar_unref_event(event);
00119             return IKS_HOOK;
00120          }
00121          ao2_link(events, event);
00122          event = ast_calendar_unref_event(event);
00123       } else if (!strcasecmp(state->tag, "propstat")) {
00124          state->in_propstat = 0;
00125       } else if (!strcasecmp(state->tag, "prop")) {
00126          state->in_prop = 0;
00127       }
00128       break;
00129 
00130    default:
00131       return IKS_OK;
00132    }
00133 
00134    return IKS_OK;
00135 }

static int unload_module ( void   )  [static]

Definition at line 751 of file res_calendar_exchange.c.

References ast_calendar_unregister().

00752 {
00753    ast_calendar_unregister(&exchangecal_tech);
00754    ne_sock_exit();
00755    return 0;
00756 }

static void * unref_exchangecal ( void *  obj  )  [static]

Definition at line 231 of file res_calendar_exchange.c.

References ao2_ref.

Referenced by exchangecal_load_calendar().

00232 {
00233    struct exchangecal_pvt *pvt = obj;
00234 
00235    ao2_ref(pvt, -1);
00236    return NULL;
00237 }

static int update_exchangecal ( struct exchangecal_pvt *  pvt  )  [static]

Definition at line 592 of file res_calendar_exchange.c.

References ast_calendar_merge_events(), ast_free, ast_str_buffer(), ast_str_strlen(), ast_tvnow(), exchangecal_pvt::events, exchangecal_get_events_between(), exchangecal_pvt::owner, parse_cdata(), parse_tag(), xmlstate::pvt, and ast_calendar::timeframe.

Referenced by exchangecal_load_calendar().

00593 {
00594    struct xmlstate state;
00595    struct timeval now = ast_tvnow();
00596    time_t start, end;
00597    struct ast_str *response;
00598    iksparser *p;
00599 
00600    state.pvt = pvt;
00601    start = now.tv_sec;
00602    end = now.tv_sec + 60 * pvt->owner->timeframe;
00603    if (!(response = exchangecal_get_events_between(pvt, start, end))) {
00604       return -1;
00605    }
00606 
00607    p = iks_sax_new(&state, parse_tag, parse_cdata);
00608    iks_parse(p, ast_str_buffer(response), ast_str_strlen(response), 1);
00609    ast_calendar_merge_events(pvt->owner, pvt->events);
00610    ast_free(response);
00611 
00612    return 0;
00613 }

static struct ast_str* xml_encode_str ( struct ast_str *  dst,
const char *  src 
) [static, read]

Definition at line 274 of file res_calendar_exchange.c.

References ast_str_append().

Referenced by exchangecal_write_event().

00275 {
00276    const char *tmp;
00277    char buf[7];
00278 
00279    for (tmp = src; *tmp; tmp++) {
00280       switch (*tmp) {
00281       case '\"':
00282          strcpy(buf, "&quot;");
00283          break;
00284 
00285       case '\'':
00286          strcpy(buf, "&apos;");
00287          break;
00288 
00289       case '&':
00290          strcpy(buf, "&amp;");
00291          break;
00292 
00293       case '<':
00294          strcpy(buf, "&lt;");
00295          break;
00296 
00297       case '>':
00298          strcpy(buf, "&gt;");
00299          break;
00300 
00301       default:
00302          sprintf(buf, "%c", *tmp);
00303       }
00304 
00305       ast_str_append(&dst, 0, "%s", buf);
00306    }
00307 
00308    return dst;
00309 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk MS Exchange Calendar Integration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PLUGIN, } [static]

Definition at line 762 of file res_calendar_exchange.c.

Definition at line 762 of file res_calendar_exchange.c.

Definition at line 50 of file res_calendar_exchange.c.


Generated on Sat Mar 26 11:26:14 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9