Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2008 - 2009, Digium, Inc.
*
* Terry Wilson <twilson@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 Calendaring API
* \todo Support responding to a meeting invite
* \todo Support writing attendees
/*! \li \ref res_calendar.c uses the configuration file \ref calendar.conf
* \addtogroup configuration_file Configuration Files
*/
* \page calendar.conf calendar.conf
* \verbinclude calendar.conf.sample
*/
<support_level>extended</support_level>
#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/channel.h"
#include "asterisk/calendar.h"
#include "asterisk/utils.h"
#include "asterisk/astobj2.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/devicestate.h"
#include "asterisk/linkedlists.h"
#include "asterisk/sched.h"
#include "asterisk/dial.h"
#include "asterisk/cli.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/format_cache.h"
/*** DOCUMENTATION
<function name="CALENDAR_BUSY" language="en_US">
<synopsis>
Determine if the calendar is marked busy at this time.
</synopsis>
<syntax>
<parameter name="calendar" required="true" />
</syntax>
<para>Check the specified calendar's current busy status.</para>
</description>
<see-also>
<ref type="function">CALENDAR_EVENT</ref>
<ref type="function">CALENDAR_QUERY</ref>
<ref type="function">CALENDAR_QUERY_RESULT</ref>
<ref type="function">CALENDAR_WRITE</ref>
</see-also>
</function>
<function name="CALENDAR_EVENT" language="en_US">
<synopsis>
Get calendar event notification data from a notification call.
</synopsis>
<syntax>
<parameter name="field" required="true">
<enumlist>
<enum name="summary"><para>The VEVENT SUMMARY property or Exchange event 'subject'</para></enum>
<enum name="description"><para>The text description of the event</para></enum>
<enum name="organizer"><para>The organizer of the event</para></enum>
<enum name="location"><para>The location of the eventt</para></enum>
<enum name="categories"><para>The categories of the event</para></enum>
<enum name="priority"><para>The priority of the event</para></enum>
<enum name="calendar"><para>The name of the calendar associated with the event</para></enum>
<enum name="uid"><para>The unique identifier for this event</para></enum>
<enum name="start"><para>The start time of the event</para></enum>
<enum name="end"><para>The end time of the event</para></enum>
<enum name="busystate"><para>The busy state of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
</enumlist>
</parameter>
</syntax>
<description>
<para>Whenever a calendar event notification call is made, the event data
may be accessed with this function.</para>
</description>
<see-also>
<ref type="function">CALENDAR_BUSY</ref>
<ref type="function">CALENDAR_QUERY</ref>
<ref type="function">CALENDAR_QUERY_RESULT</ref>
<ref type="function">CALENDAR_WRITE</ref>
</see-also>
</function>
<function name="CALENDAR_QUERY" language="en_US">
<synopsis>Query a calendar server and store the data on a channel
</synopsis>
<syntax>
<parameter name="calendar" required="true">
<para>The calendar that should be queried</para>
</parameter>
<parameter name="start" required="false">
<para>The start time of the query (in seconds since epoch)</para>
</parameter>
<parameter name="end" required="false">
<para>The end time of the query (in seconds since epoch)</para>
</parameter>
</syntax>
<description>
<para>Get a list of events in the currently accessible timeframe of the <replaceable>calendar</replaceable>
The function returns the id for accessing the result with CALENDAR_QUERY_RESULT()</para>
</description>
<see-also>
<ref type="function">CALENDAR_BUSY</ref>
<ref type="function">CALENDAR_EVENT</ref>
<ref type="function">CALENDAR_QUERY_RESULT</ref>
<ref type="function">CALENDAR_WRITE</ref>
</see-also>
</function>
<function name="CALENDAR_QUERY_RESULT" language="en_US">
<synopsis>
Retrieve data from a previously run CALENDAR_QUERY() call
</synopsis>
<syntax>
<parameter name="id" required="true">
<para>The query ID returned by <literal>CALENDAR_QUERY</literal></para>
</parameter>
<parameter name="field" required="true">
<enumlist>
<enum name="getnum"><para>number of events occurring during time range</para></enum>
<enum name="summary"><para>A summary of the event</para></enum>
<enum name="description"><para>The full event description</para></enum>
<enum name="organizer"><para>The event organizer</para></enum>
<enum name="location"><para>The event location</para></enum>
<enum name="categories"><para>The categories of the event</para></enum>
<enum name="priority"><para>The priority of the event</para></enum>
<enum name="calendar"><para>The name of the calendar associted with the event</para></enum>
<enum name="uid"><para>The unique identifier for the event</para></enum>
<enum name="start"><para>The start time of the event (in seconds since epoch)</para></enum>
<enum name="end"><para>The end time of the event (in seconds since epoch)</para></enum>
<enum name="busystate"><para>The busy status of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
</enumlist>
</parameter>
<parameter name="entry" required="false" default="1">
<para>Return data from a specific event returned by the query</para>
</parameter>
</syntax>
<description>
<para>After running CALENDAR_QUERY and getting a result <replaceable>id</replaceable>, calling
<literal>CALENDAR_QUERY</literal> with that <replaceable>id</replaceable> and a <replaceable>field</replaceable>
will return the data for that field. If multiple events matched the query, and <replaceable>entry</replaceable>
is provided, information from that event will be returned.</para>
</description>
<see-also>
<ref type="function">CALENDAR_BUSY</ref>
<ref type="function">CALENDAR_EVENT</ref>
<ref type="function">CALENDAR_QUERY</ref>
<ref type="function">CALENDAR_WRITE</ref>
</see-also>
</function>
<function name="CALENDAR_WRITE" language="en_US">
<synopsis>Write an event to a calendar</synopsis>
<syntax>
<parameter name="calendar" required="true">
<para>The calendar to write to</para>
</parameter>
<parameter name="field" multiple="true" required="true">
<enumlist>
<enum name="summary"><para>A summary of the event</para></enum>
<enum name="description"><para>The full event description</para></enum>
<enum name="organizer"><para>The event organizer</para></enum>
<enum name="location"><para>The event location</para></enum>
<enum name="categories"><para>The categories of the event</para></enum>
<enum name="priority"><para>The priority of the event</para></enum>
<enum name="uid"><para>The unique identifier for the event</para></enum>
<enum name="start"><para>The start time of the event (in seconds since epoch)</para></enum>
<enum name="end"><para>The end time of the event (in seconds since epoch)</para></enum>
<enum name="busystate"><para>The busy status of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
</enumlist>
</parameter>
</syntax>
<description>
<para>Example: CALENDAR_WRITE(calendar,field1,field2,field3)=val1,val2,val3</para>
<para>The field and value arguments can easily be set/passed using the HASHKEYS() and HASH() functions</para>
<variablelist>
<variable name="CALENDAR_SUCCESS">
<para>The status of the write operation to the calendar</para>
<value name="1" >
The event was successfully written to the calendar.
</value>
<value name="0" >
The event was not written to the calendar due to network issues, permissions, etc.
</value>
</variable>
</variablelist>
<see-also>
<ref type="function">CALENDAR_BUSY</ref>
<ref type="function">CALENDAR_EVENT</ref>
<ref type="function">CALENDAR_QUERY</ref>
<ref type="function">CALENDAR_QUERY_RESULT</ref>
</see-also>
</function>
***/
#define CALENDAR_BUCKETS 19
static struct ao2_container *calendars;
static struct ast_sched_context *sched;
static pthread_t refresh_thread = AST_PTHREADT_NULL;
static ast_mutex_t refreshlock;
static ast_cond_t refresh_condition;
static ast_mutex_t reloadlock;
static void event_notification_destroy(void *data);
static void *event_notification_duplicate(void *data);
static void eventlist_destroy(void *data);
static void *eventlist_duplicate(void *data);
static const struct ast_datastore_info event_notification_datastore = {
.type = "EventNotification",
.destroy = event_notification_destroy,
.duplicate = event_notification_duplicate,
};
static const struct ast_datastore_info eventlist_datastore_info = {
.type = "CalendarEventList",
.destroy = eventlist_destroy,
.duplicate = eventlist_duplicate,
};
struct evententry {
struct ast_calendar_event *event;
AST_LIST_ENTRY(evententry) list;
};
static AST_LIST_HEAD_STATIC(techs, ast_calendar_tech);
AST_LIST_HEAD_NOLOCK(eventlist, evententry); /* define the type */
static struct ast_config *calendar_config;
AST_RWLOCK_DEFINE_STATIC(config_lock);
const struct ast_config *ast_calendar_config_acquire(void)
{
ast_rwlock_rdlock(&config_lock);
if (!calendar_config) {
ast_rwlock_unlock(&config_lock);
return NULL;
}
return calendar_config;
}
void ast_calendar_config_release(void)
{
ast_rwlock_unlock(&config_lock);
}
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
static struct ast_calendar *unref_calendar(struct ast_calendar *cal)
{
ao2_ref(cal, -1);
return NULL;
}
static int calendar_hash_fn(const void *obj, const int flags)
{
const struct ast_calendar *cal = obj;
return ast_str_case_hash(cal->name);
}
static int calendar_cmp_fn(void *obj, void *arg, int flags)
{
const struct ast_calendar *one = obj, *two = arg;
return !strcasecmp(one->name, two->name) ? CMP_MATCH | CMP_STOP: 0;
}
static struct ast_calendar *find_calendar(const char *name)
{
struct ast_calendar tmp = {
.name = name,
};
return ao2_find(calendars, &tmp, OBJ_POINTER);
}
static int event_hash_fn(const void *obj, const int flags)
{
const struct ast_calendar_event *event = obj;
return ast_str_hash(event->uid);
}
static int event_cmp_fn(void *obj, void *arg, int flags)
{
const struct ast_calendar_event *one = obj, *two = arg;
return !strcmp(one->uid, two->uid) ? CMP_MATCH | CMP_STOP : 0;
}
static struct ast_calendar_event *find_event(struct ao2_container *events, const char *uid)
{
struct ast_calendar_event tmp = {
.uid = uid,
};
return ao2_find(events, &tmp, OBJ_POINTER);
}
struct ast_calendar_event *ast_calendar_unref_event(struct ast_calendar_event *event)
{
ao2_ref(event, -1);
return NULL;
}
static void calendar_destructor(void *obj)
{
struct ast_calendar *cal = obj;
ast_debug(3, "Destroying calendar %s\n", cal->name);
ao2_lock(cal);
cal->unloading = 1;
ast_cond_signal(&cal->unload);
pthread_join(cal->thread, NULL);
if (cal->tech_pvt) {
cal->tech_pvt = cal->tech->unref_calendar(cal->tech_pvt);
}
ast_calendar_clear_events(cal);
ast_string_field_free_memory(cal);
ast_variables_destroy(cal->vars);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
ao2_ref(cal->events, -1);
ao2_unlock(cal);
}
static void eventlist_destructor(void *obj)
{
struct eventlist *events = obj;
struct evententry *entry;
while ((entry = AST_LIST_REMOVE_HEAD(events, list))) {
ao2_ref(entry->event, -1);
ast_free(entry);
}
}
static int calendar_busy_callback(void *obj, void *arg, int flags)
{
struct ast_calendar_event *event = obj;
int *is_busy = arg;
struct timeval tv = ast_tvnow();
if (tv.tv_sec >= event->start && tv.tv_sec <= event->end && event->busy_state > AST_CALENDAR_BS_FREE) {
*is_busy = 1;
return CMP_STOP;
}
return 0;
}
static int calendar_is_busy(struct ast_calendar *cal)
{
int is_busy = 0;
ao2_callback(cal->events, OBJ_NODATA, calendar_busy_callback, &is_busy);
return is_busy;
}
static enum ast_device_state calendarstate(const char *data)
{
struct ast_calendar *cal;
if (ast_strlen_zero(data) || (!(cal = find_calendar(data)))) {
return AST_DEVICE_INVALID;
}
if (cal->tech->is_busy) {
state = cal->tech->is_busy(cal) ? AST_DEVICE_INUSE : AST_DEVICE_NOT_INUSE;
} else {
state = calendar_is_busy(cal) ? AST_DEVICE_INUSE : AST_DEVICE_NOT_INUSE;
cal = unref_calendar(cal);
return state;
}
static struct ast_calendar *build_calendar(struct ast_config *cfg, const char *cat, const struct ast_calendar_tech *tech)
{
struct ast_calendar *cal;
cal = find_calendar(cat);
if (cal && cal->fetch_again_at_reload) {
/** Create new calendar, old will be removed during reload */
cal = unref_calendar(cal);
}
if (!cal) {
new_calendar = 1;
if (!(cal = ao2_alloc(sizeof(*cal), calendar_destructor))) {
ast_log(LOG_ERROR, "Could not allocate calendar structure. Stopping.\n");
return NULL;
}
cal->events = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
CALENDAR_BUCKETS, event_hash_fn, NULL, event_cmp_fn);
if (!cal->events) {
ast_log(LOG_ERROR, "Could not allocate events container for %s\n", cat);
cal = unref_calendar(cal);
return NULL;
}
if (ast_string_field_init(cal, 32)) {
ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", cat);
cal = unref_calendar(cal);
return NULL;
}
} else {
cal->pending_deletion = 0;
}
ast_string_field_set(cal, name, cat);
cal->tech = tech;
cal->refresh = 3600;
cal->timeframe = 60;
cal->fetch_again_at_reload = 0;
for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
if (!strcasecmp(v->name, "autoreminder")) {
cal->autoreminder = atoi(v->value);
} else if (!strcasecmp(v->name, "channel")) {
ast_string_field_set(cal, notify_channel, v->value);
} else if (!strcasecmp(v->name, "context")) {
ast_string_field_set(cal, notify_context, v->value);
} else if (!strcasecmp(v->name, "extension")) {
ast_string_field_set(cal, notify_extension, v->value);
} else if (!strcasecmp(v->name, "waittime")) {
int i = atoi(v->value);
if (i > 0) {
cal->notify_waittime = 1000 * i;
}
} else if (!strcasecmp(v->name, "app")) {
ast_string_field_set(cal, notify_app, v->value);
} else if (!strcasecmp(v->name, "appdata")) {
ast_string_field_set(cal, notify_appdata, v->value);
} else if (!strcasecmp(v->name, "refresh")) {
cal->refresh = atoi(v->value);
} else if (!strcasecmp(v->name, "fetch_again_at_reload")) {
cal->fetch_again_at_reload = ast_true(v->value);
} else if (!strcasecmp(v->name, "timeframe")) {
cal->timeframe = atoi(v->value);
} else if (!strcasecmp(v->name, "setvar")) {
char *name, *value;
struct ast_variable *var;
if ((name = (value = ast_strdup(v->value)))) {
strsep(&value, "=");
if (value) {
if ((var = ast_variable_new(ast_strip(name), ast_strip(value), ""))) {
if (last) {
last->next = var;
} else {
cal->vars = var;
}
last = var;
}
} else {
ast_log(LOG_WARNING, "Malformed argument. Should be '%s: variable=value'\n", v->name);
}
ast_free(name);
}
if (cal->autoreminder && ast_strlen_zero(cal->notify_channel)) {
ast_log(LOG_WARNING,
"You have set 'autoreminder' but not 'channel' for calendar '%s.' "
"Notifications will not occur.\n",
cal->name);
}
cal->thread = AST_PTHREADT_NULL;
ast_cond_init(&cal->unload, NULL);
ao2_link(calendars, cal);
if (ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal)) {
/* If we start failing to create threads, go ahead and return NULL
* and the tech module will be unregistered
ao2_unlink(calendars, cal);
cal = unref_calendar(cal);
}
}
return cal;
}
static int load_tech_calendars(struct ast_calendar_tech *tech)
{
struct ast_calendar *cal;
const char *cat = NULL;
const char *val;
ast_log(LOG_WARNING, "Calendar support disabled, not loading %s calendar module\n", tech->type);
return -1;
}
ast_rwlock_wrlock(&config_lock);
while ((cat = ast_category_browse(calendar_config, cat))) {
if (!strcasecmp(cat, "general")) {
continue;
}
if (!(val = ast_variable_retrieve(calendar_config, cat, "type")) || strcasecmp(val, tech->type)) {
continue;
}
/* A serious error occurred loading calendars from this tech and it should be disabled */
if (!(cal = build_calendar(calendar_config, cat, tech))) {
ast_rwlock_unlock(&config_lock);
return -1;
}
cal = unref_calendar(cal);
}
ast_rwlock_unlock(&config_lock);
return 0;
}
int ast_calendar_register(struct ast_calendar_tech *tech)
{
struct ast_calendar_tech *iter;
if (!calendar_config) {
ast_log(LOG_WARNING, "Calendar support disabled, not loading %s calendar module\n", tech->type);
return -1;
}
AST_LIST_LOCK(&techs);
AST_LIST_TRAVERSE(&techs, iter, list) {
if(!strcasecmp(tech->type, iter->type)) {
ast_log(LOG_WARNING, "Already have a handler for calendar type '%s'\n", tech->type);
AST_LIST_UNLOCK(&techs);
return -1;
}
}
AST_LIST_INSERT_HEAD(&techs, tech, list);
tech->user = ast_module_user_add(NULL);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
AST_LIST_UNLOCK(&techs);
ast_verb(2, "Registered calendar type '%s' (%s)\n", tech->type, tech->description);
return load_tech_calendars(tech);
}
static int match_caltech_cb(void *user_data, void *arg, int flags)
{
struct ast_calendar *cal = user_data;
struct ast_calendar_tech *tech = arg;
if (cal->tech == tech) {
return CMP_MATCH;
}
return 0;
}
void ast_calendar_unregister(struct ast_calendar_tech *tech)
{
struct ast_calendar_tech *iter;
AST_LIST_LOCK(&techs);
AST_LIST_TRAVERSE_SAFE_BEGIN(&techs, iter, list) {
if (iter != tech) {
continue;
}
ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, match_caltech_cb, tech);
AST_LIST_REMOVE_CURRENT(list);
ast_module_user_remove(iter->user);
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
ast_verb(2, "Unregistered calendar type '%s'\n", tech->type);
break;
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&techs);
}
static void calendar_event_destructor(void *obj)
{
struct ast_calendar_event *event = obj;
struct ast_calendar_attendee *attendee;
ast_debug(3, "Destroying event for calendar '%s'\n", event->owner->name);
ast_string_field_free_memory(event);
while ((attendee = AST_LIST_REMOVE_HEAD(&event->attendees, next))) {
if (attendee->data) {
ast_free(attendee->data);
}
ast_free(attendee);
}
}
/* This is only called from ao2_callbacks that are going to unref the event for us,
* so we don't unref the event here. */
static struct ast_calendar_event *destroy_event(struct ast_calendar_event *event)
{
if (event->notify_sched > -1 && ast_sched_del(sched, event->notify_sched)) {
ast_debug(3, "Notification running, can't delete sched entry\n");
}
if (event->bs_start_sched > -1 && ast_sched_del(sched, event->bs_start_sched)) {
ast_debug(3, "Devicestate update (start) running, can't delete sched entry\n");
}
if (event->bs_end_sched > -1 && ast_sched_del(sched, event->bs_end_sched)) {
ast_debug(3, "Devicestate update (end) running, can't delete sched entry\n");
}
/* If an event is being deleted and we've fired an event changing the status at the beginning,
* but haven't hit the end event yet, go ahead and set the devicestate to the current busy status */
if (event->bs_start_sched < 0 && event->bs_end_sched >= 0) {
if (!calendar_is_busy(event->owner)) {
ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
}
}
return NULL;
}
static int clear_events_cb(void *user_data, void *arg, int flags)
{
struct ast_calendar_event *event = user_data;
event = destroy_event(event);
return CMP_MATCH;
}
void ast_calendar_clear_events(struct ast_calendar *cal)
{
ast_debug(3, "Clearing all events for calendar %s\n", cal->name);
ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, clear_events_cb, NULL);
}
struct ast_calendar_event *ast_calendar_event_alloc(struct ast_calendar *cal)
{
struct ast_calendar_event *event;
if (!(event = ao2_alloc(sizeof(*event), calendar_event_destructor))) {
return NULL;
}
if (ast_string_field_init(event, 32)) {
event = ast_calendar_unref_event(event);
return NULL;
}
event->owner = cal;
event->notify_sched = -1;
event->bs_start_sched = -1;
event->bs_end_sched = -1;
AST_LIST_HEAD_INIT_NOLOCK(&event->attendees);
return event;
}
struct ao2_container *ast_calendar_event_container_alloc(void)
{
return ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, CALENDAR_BUCKETS,
event_hash_fn, NULL, event_cmp_fn);
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
}
static void event_notification_destroy(void *data)
{
struct ast_calendar_event *event = data;
event = ast_calendar_unref_event(event);
}
static void *event_notification_duplicate(void *data)
{
struct ast_calendar_event *event = data;
if (!event) {
return NULL;
}
ao2_ref(event, +1);
return event;
}
/*! \brief Generate 32 byte random string (stolen from chan_sip.c)*/
static char *generate_random_string(char *buf, size_t size)
{
int x;
for (x = 0; x < 4; x++) {
val[x] = ast_random();
}
snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
return buf;
}
static int null_chan_write(struct ast_channel *chan, struct ast_frame *frame)
static const struct ast_channel_tech null_tech = {
.type = "NULL",
.description = "Null channel (should not see this)",
.write = null_chan_write,
};
static void *do_notify(void *data)
{
struct ast_calendar_event *event = data;
struct ast_str *apptext = NULL, *tmpstr = NULL;
struct ast_datastore *datastore;
enum ast_dial_result res;
struct ast_channel *chan = NULL;
struct ast_format_cap *caps;
tech = ast_strdupa(event->owner->notify_channel);
if ((dest = strchr(tech, '/'))) {
*dest = '\0';
dest++;
ast_log(LOG_WARNING, "Channel should be in form Tech/Dest (was '%s')\n", tech);
goto notify_cleanup;
}
if (!(dial = ast_dial_create())) {
ast_log(LOG_ERROR, "Could not create dial structure\n");
goto notify_cleanup;
}
if (ast_dial_append(dial, tech, dest, NULL) < 0) {
ast_log(LOG_ERROR, "Could not append channel\n");
goto notify_cleanup;
}
ast_dial_set_global_timeout(dial, event->owner->notify_waittime);
generate_random_string(buf, sizeof(buf));
if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, 0, 0, 0, 0, 0, NULL, NULL, 0, "Calendar/%s-%s", event->owner->name, buf))) {
ast_log(LOG_ERROR, "Could not allocate notification channel\n");
goto notify_cleanup;
}
ast_channel_tech_set(chan, &null_tech);
ast_channel_set_writeformat(chan, ast_format_slin);
ast_channel_set_readformat(chan, ast_format_slin);
ast_channel_set_rawwriteformat(chan, ast_format_slin);
ast_channel_set_rawreadformat(chan, ast_format_slin);
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
if (!caps) {
ast_log(LOG_ERROR, "Could not allocate capabilities, notification not being sent!\n");
goto notify_cleanup;
}
ast_format_cap_append(caps, ast_format_slin, 0);
ast_channel_nativeformats_set(chan, caps);
ao2_ref(caps, -1);
if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
ast_log(LOG_ERROR, "Could not allocate datastore, notification not being sent!\n");
goto notify_cleanup;
}
datastore->data = event;
datastore->inheritance = DATASTORE_INHERIT_FOREVER;
ao2_ref(event, +1);
ast_channel_lock(chan);
res = ast_channel_datastore_add(chan, datastore);
ast_channel_unlock(chan);
if (!(tmpstr = ast_str_create(32))) {
goto notify_cleanup;
}
for (itervar = event->owner->vars; itervar; itervar = itervar->next) {
ast_str_substitute_variables(&tmpstr, 0, chan, itervar->value);
pbx_builtin_setvar_helper(chan, itervar->name, ast_str_buffer(tmpstr));
if (!(apptext = ast_str_create(32))) {
goto notify_cleanup;
}
if (!ast_strlen_zero(event->owner->notify_app)) {
ast_str_set(&apptext, 0, "%s,%s", event->owner->notify_app, event->owner->notify_appdata);
ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, ast_str_buffer(apptext));
ast_verb(3, "Dialing %s for notification on calendar %s\n", event->owner->notify_channel, event->owner->name);
res = ast_dial_run(dial, chan, 0);
if (res != AST_DIAL_RESULT_ANSWERED) {
ast_verb(3, "Notification call for %s was not completed\n", event->owner->name);
} else {
struct ast_channel *answered;
answered = ast_dial_answered_steal(dial);
if (ast_strlen_zero(event->owner->notify_app)) {
ast_channel_context_set(answered, event->owner->notify_context);
ast_channel_exten_set(answered, event->owner->notify_extension);
ast_channel_priority_set(answered, 1);
ast_pbx_run(answered);
}
notify_cleanup:
if (apptext) {
ast_free(apptext);
}
if (tmpstr) {
ast_free(tmpstr);
}
if (dial) {
ast_dial_destroy(dial);
}
if (chan) {
ast_channel_release(chan);
}
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
event = ast_calendar_unref_event(event);
return NULL;
}
static int calendar_event_notify(const void *data)
{
struct ast_calendar_event *event = (void *)data;
int res = -1;
pthread_t notify_thread = AST_PTHREADT_NULL;
if (!(event && event->owner)) {
ast_log(LOG_ERROR, "Extremely low-cal...in fact cal is NULL!\n");
return res;
}
ao2_ref(event, +1);
event->notify_sched = -1;
if (ast_pthread_create_background(¬ify_thread, NULL, do_notify, event) < 0) {
ast_log(LOG_ERROR, "Could not create notification thread\n");
return res;
}
res = 0;
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
return res;
}
static int calendar_devstate_change(const void *data)
{
struct ast_calendar_event *event = (struct ast_calendar_event *)data;
struct timeval now = ast_tvnow();
int is_end_event;
if (!event) {
ast_log(LOG_WARNING, "Event was NULL!\n");
return 0;
}
ao2_ref(event, +1);
is_end_event = event->end <= now.tv_sec;
if (is_end_event) {
event->bs_end_sched = -1;
} else {
event->bs_start_sched = -1;
}
/* We can have overlapping events, so ignore the event->busy_state and check busy state
* based on all events in the calendar */
if (!calendar_is_busy(event->owner)) {
ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
}
event = ast_calendar_unref_event(event);
return 0;
}
static void copy_event_data(struct ast_calendar_event *dst, struct ast_calendar_event *src)
{
struct ast_calendar_attendee *attendee;
ast_string_field_set(dst, summary, src->summary);
ast_string_field_set(dst, description, src->description);
ast_string_field_set(dst, organizer, src->organizer);
ast_string_field_set(dst, location, src->location);
ast_string_field_set(dst, uid, src->uid);
ast_string_field_set(dst, categories, src->categories);
dst->priority = src->priority;
dst->owner = src->owner;
dst->start = src->start;
dst->end = src->end;
dst->alarm = src->alarm;
dst->busy_state = src->busy_state;
/* Delete any existing attendees */
while ((attendee = AST_LIST_REMOVE_HEAD(&dst->attendees, next))) {
ast_free(attendee);
}
/* Copy over the new attendees */
while ((attendee = AST_LIST_REMOVE_HEAD(&src->attendees, next))) {
AST_LIST_INSERT_TAIL(&dst->attendees, attendee, next);
}
}
static int schedule_calendar_event(struct ast_calendar *cal, struct ast_calendar_event *old_event, struct ast_calendar_event *cmp_event)
{
struct timeval now = ast_tvnow();
struct ast_calendar_event *event;
time_t alarm_notify_sched = 0, devstate_sched_start, devstate_sched_end;
int changed = 0;
event = cmp_event ? cmp_event : old_event;
ao2_lock(event);
if (!ast_strlen_zero(cal->notify_channel) && (!cmp_event || old_event->alarm != event->alarm)) {
changed = 1;
if (cal->autoreminder) {
alarm_notify_sched = (event->start - (60 * cal->autoreminder) - now.tv_sec) * 1000;
} else if (event->alarm) {
alarm_notify_sched = (event->alarm - now.tv_sec) * 1000;
}
/* For now, send the notification if we missed it, but the meeting hasn't happened yet */
if (alarm_notify_sched <= 0) {
alarm_notify_sched = 1;
}
ast_mutex_lock(&refreshlock);
AST_SCHED_REPLACE(old_event->notify_sched, sched, alarm_notify_sched, calendar_event_notify, old_event);
ast_mutex_unlock(&refreshlock);
ast_debug(3, "Calendar alarm event notification scheduled to happen in %ld ms\n", (long) alarm_notify_sched);
}
}
if (!cmp_event || old_event->start != event->start) {
changed = 1;
devstate_sched_start = (event->start - now.tv_sec) * 1000;
if (devstate_sched_start < 1) {
devstate_sched_start = 1;
}
ast_mutex_lock(&refreshlock);
AST_SCHED_REPLACE(old_event->bs_start_sched, sched, devstate_sched_start, calendar_devstate_change, old_event);
ast_mutex_unlock(&refreshlock);
ast_debug(3, "Calendar bs_start event notification scheduled to happen in %ld ms\n", (long) devstate_sched_start);
}
if (!cmp_event || old_event->end != event->end) {
changed = 1;
devstate_sched_end = (event->end - now.tv_sec) * 1000;