222 #define CALENDAR_BUCKETS 19
226 static pthread_t refresh_thread = AST_PTHREADT_NULL;
228 static ast_cond_t refresh_condition;
230 static int module_unloading;
232 static void event_notification_destroy(
void *data);
233 static void *event_notification_duplicate(
void *data);
234 static void eventlist_destroy(
void *data);
235 static void *eventlist_duplicate(
void *data);
238 .
type =
"EventNotification",
239 .destroy = event_notification_destroy,
240 .duplicate = event_notification_duplicate,
244 .
type =
"CalendarEventList",
245 .destroy = eventlist_destroy,
246 .duplicate = eventlist_duplicate,
258 AST_RWLOCK_DEFINE_STATIC(config_lock);
262 ast_rwlock_rdlock(&config_lock);
264 if (!calendar_config) {
265 ast_rwlock_unlock(&config_lock);
269 return calendar_config;
274 ast_rwlock_unlock(&config_lock);
283 static int calendar_hash_fn(
const void *obj,
const int flags)
289 static int calendar_cmp_fn(
void *obj,
void *arg,
int flags)
303 static int event_hash_fn(
const void *obj,
const int flags)
309 static int event_cmp_fn(
void *obj,
void *arg,
int flags)
329 static void calendar_destructor(
void *obj)
337 ast_cond_signal(&cal->unload);
338 pthread_join(cal->
thread, NULL);
349 static void eventlist_destructor(
void *obj)
360 static int calendar_busy_callback(
void *obj,
void *arg,
int flags)
366 if (tv.tv_sec >=
event->start && tv.tv_sec <=
event->end &&
event->busy_state > AST_CALENDAR_BS_FREE) {
388 if (ast_strlen_zero(data) || (!(cal = find_calendar(data)))) {
398 cal = unref_calendar(cal);
406 int new_calendar = 0;
408 cal = find_calendar(cat);
411 cal = unref_calendar(cal);
415 if (!(cal = ao2_alloc(
sizeof(*cal), calendar_destructor))) {
416 ast_log(LOG_ERROR,
"Could not allocate calendar structure. Stopping.\n");
421 CALENDAR_BUCKETS, event_hash_fn, NULL, event_cmp_fn);
423 ast_log(LOG_ERROR,
"Could not allocate events container for %s\n", cat);
424 cal = unref_calendar(cal);
429 ast_log(LOG_ERROR,
"Couldn't create string fields for %s\n", cat);
430 cal = unref_calendar(cal);
434 cal->pending_deletion = 0;
445 for (v = ast_variable_browse(cfg, cat); v; v = v->
next) {
446 if (!strcasecmp(v->
name,
"autoreminder")) {
448 }
else if (!strcasecmp(v->
name,
"channel")) {
450 }
else if (!strcasecmp(v->
name,
"context")) {
452 }
else if (!strcasecmp(v->
name,
"extension")) {
454 }
else if (!strcasecmp(v->
name,
"waittime")) {
455 int i = atoi(v->
value);
459 }
else if (!strcasecmp(v->
name,
"app")) {
461 }
else if (!strcasecmp(v->
name,
"appdata")) {
463 }
else if (!strcasecmp(v->
name,
"refresh")) {
465 }
else if (!strcasecmp(v->
name,
"fetch_again_at_reload")) {
467 }
else if (!strcasecmp(v->
name,
"timeframe")) {
469 }
else if (!strcasecmp(v->
name,
"setvar")) {
485 ast_log(LOG_WARNING,
"Malformed argument. Should be '%s: variable=value'\n", v->
name);
494 "You have set 'autoreminder' but not 'channel' for calendar '%s.' "
495 "Notifications will not occur.\n",
500 cal->
thread = AST_PTHREADT_NULL;
501 ast_cond_init(&cal->unload, NULL);
508 cal = unref_calendar(cal);
518 const char *cat = NULL;
521 if (!calendar_config) {
522 ast_log(LOG_WARNING,
"Calendar support disabled, not loading %s calendar module\n", tech->type);
526 ast_rwlock_wrlock(&config_lock);
528 if (!strcasecmp(cat,
"general")) {
532 if (!(val = ast_variable_retrieve(calendar_config, cat,
"type")) || strcasecmp(val, tech->type)) {
539 ast_rwlock_unlock(&config_lock);
543 cal = unref_calendar(cal);
546 ast_rwlock_unlock(&config_lock);
555 if (!calendar_config) {
556 ast_log(LOG_WARNING,
"Calendar support disabled, not loading %s calendar module\n", tech->type);
562 if(!strcasecmp(tech->type, iter->type)) {
563 ast_log(LOG_WARNING,
"Already have a handler for calendar type '%s'\n", tech->type);
569 tech->user = ast_module_user_add(NULL);
572 ast_verb(2,
"Registered calendar type '%s' (%s)\n", tech->type, tech->description);
574 return load_tech_calendars(tech);
577 static int match_caltech_cb(
void *user_data,
void *arg,
int flags)
582 if (cal->tech == tech) {
602 ast_module_user_remove(iter->user);
603 ast_verb(2,
"Unregistered calendar type '%s'\n", tech->type);
611 static void calendar_event_destructor(
void *obj)
616 ast_debug(3,
"Destroying event for calendar '%s'\n",
event->owner->name);
619 if (attendee->data) {
620 ast_free(attendee->data);
631 ast_debug(3,
"Notification running, can't delete sched entry\n");
634 ast_debug(3,
"Devicestate update (start) running, can't delete sched entry\n");
637 ast_debug(3,
"Devicestate update (end) running, can't delete sched entry\n");
643 if (!calendar_is_busy(event->
owner)) {
653 static int clear_events_cb(
void *user_data,
void *arg,
int flags)
657 event = destroy_event(event);
664 ast_debug(3,
"Clearing all events for calendar %s\n", cal->
name);
672 if (!(event = ao2_alloc(
sizeof(*event), calendar_event_destructor))) {
682 event->notify_sched = -1;
683 event->bs_start_sched = -1;
684 event->bs_end_sched = -1;
694 event_hash_fn, NULL, event_cmp_fn);
697 static void event_notification_destroy(
void *data)
705 static void *event_notification_duplicate(
void *data)
721 unsigned long val[4];
724 for (x = 0; x < 4; x++) {
725 val[x] = ast_random();
727 snprintf(buf, size,
"%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
739 .description =
"Null channel (should not see this)",
740 .write = null_chan_write,
743 static void *do_notify(
void *data)
747 struct ast_str *apptext = NULL, *tmpstr = NULL;
758 if ((dest = strchr(tech,
'/'))) {
762 ast_log(LOG_WARNING,
"Channel should be in form Tech/Dest (was '%s')\n", tech);
767 ast_log(LOG_ERROR,
"Could not create dial structure\n");
772 ast_log(LOG_ERROR,
"Could not append channel\n");
779 if (!(chan =
ast_channel_alloc(1,
AST_STATE_DOWN, 0, 0, 0, 0, 0, NULL, NULL, 0,
"Calendar/%s-%s", event->
owner->
name, buf))) {
780 ast_log(LOG_ERROR,
"Could not allocate notification channel\n");
784 ast_channel_tech_set(chan, &null_tech);
792 ast_log(LOG_ERROR,
"Could not allocate capabilities, notification not being sent!\n");
796 ast_channel_nativeformats_set(chan, caps);
799 ast_channel_unlock(chan);
801 if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
802 ast_log(LOG_ERROR,
"Could not allocate datastore, notification not being sent!\n");
806 datastore->
data = event;
807 datastore->
inheritance = DATASTORE_INHERIT_FOREVER;
811 ast_channel_lock(chan);
813 ast_channel_unlock(chan);
819 for (itervar = event->
owner->
vars; itervar; itervar = itervar->
next) {
838 ast_verb(3,
"Notification call for %s was not completed\n", event->
owner->
name);
846 ast_channel_priority_set(answered, 1);
870 static int calendar_event_notify(
const void *data)
874 pthread_t notify_thread = AST_PTHREADT_NULL;
876 if (!(event && event->
owner)) {
877 ast_log(LOG_ERROR,
"Extremely low-cal...in fact cal is NULL!\n");
882 event->notify_sched = -1;
884 if (ast_pthread_create_background(¬ify_thread, NULL, do_notify, event) < 0) {
885 ast_log(LOG_ERROR,
"Could not create notification thread\n");
894 static int calendar_devstate_change(
const void *data)
901 ast_log(LOG_WARNING,
"Event was NULL!\n");
907 is_end_event =
event->end <= now.tv_sec;
910 event->bs_end_sched = -1;
912 event->bs_start_sched = -1;
917 if (!calendar_is_busy(event->
owner)) {
960 time_t alarm_notify_sched = 0, devstate_sched_start, devstate_sched_end;
963 event = cmp_event ? cmp_event : old_event;
969 alarm_notify_sched = (
event->start - (60 * cal->
autoreminder) - now.tv_sec) * 1000;
970 }
else if (event->
alarm) {
971 alarm_notify_sched = (
event->alarm - now.tv_sec) * 1000;
975 if (event->
start >= now.tv_sec) {
976 if (alarm_notify_sched <= 0) {
977 alarm_notify_sched = 1;
979 ast_mutex_lock(&refreshlock);
980 AST_SCHED_REPLACE(old_event->
notify_sched, sched, alarm_notify_sched, calendar_event_notify, old_event);
981 ast_mutex_unlock(&refreshlock);
982 ast_debug(3,
"Calendar alarm event notification scheduled to happen in %ld ms\n", (
long) alarm_notify_sched);
986 if (!cmp_event || old_event->
start != event->
start) {
988 devstate_sched_start = (
event->start - now.tv_sec) * 1000;
990 if (devstate_sched_start < 1) {
991 devstate_sched_start = 1;
994 ast_mutex_lock(&refreshlock);
995 AST_SCHED_REPLACE(old_event->
bs_start_sched, sched, devstate_sched_start, calendar_devstate_change, old_event);
996 ast_mutex_unlock(&refreshlock);
997 ast_debug(3,
"Calendar bs_start event notification scheduled to happen in %ld ms\n", (
long) devstate_sched_start);
1000 if (!cmp_event || old_event->
end != event->
end) {
1002 devstate_sched_end = (
event->end - now.tv_sec) * 1000;
1004 if (devstate_sched_end <= 0) {
1005 ast_log(LOG_WARNING,
"Whoops! Event end notification scheduled in the past: %ld ms\n", (
long) devstate_sched_end);
1007 ast_mutex_lock(&refreshlock);
1008 AST_SCHED_REPLACE(old_event->
bs_end_sched, sched, devstate_sched_end, calendar_devstate_change, old_event);
1009 ast_mutex_unlock(&refreshlock);
1010 ast_debug(3,
"Calendar bs_end event notification scheduled to happen in %ld ms\n", (
long) devstate_sched_end);
1015 ast_cond_signal(&refresh_condition);
1023 static int merge_events_cb(
void *obj,
void *arg,
int flags)
1029 if (!(new_event = find_event(new_events, old_event->uid))) {
1030 old_event = destroy_event(old_event);
1036 schedule_calendar_event(old_event->
owner, old_event, new_event);
1040 copy_event_data(old_event, new_event);
1050 static int add_new_event_cb(
void *obj,
void *arg,
int flags)
1056 schedule_calendar_event(new_event->
owner, new_event, NULL);
1072 static int load_config(
int reload)
1077 if (!(tmpcfg =
ast_config_load2(
"calendar.conf",
"calendar", config_flags)) ||
1078 tmpcfg == CONFIG_STATUS_FILEINVALID) {
1079 ast_log(LOG_ERROR,
"Unable to load config calendar.conf\n");
1083 if (tmpcfg == CONFIG_STATUS_FILEUNCHANGED) {
1087 ast_rwlock_wrlock(&config_lock);
1088 if (calendar_config) {
1092 calendar_config = tmpcfg;
1093 ast_rwlock_unlock(&config_lock);
1103 if (ast_strlen_zero(data)) {
1104 ast_log(LOG_WARNING,
"CALENDAR_BUSY requires an argument: CALENDAR_BUSY(<calendar_name>)\n");
1108 cal = find_calendar(data);
1111 ast_log(LOG_WARNING,
"Could not find calendar '%s'\n", data);
1115 strcpy(buf, calendar_is_busy(cal) ?
"1" :
"0");
1116 cal = unref_calendar(cal);
1122 .
name =
"CALENDAR_BUSY",
1129 long event_startdiff = labs(start - event->
start);
1130 long event_enddiff = labs(end - event->
end);
1133 if (!(entry =
ast_calloc(1,
sizeof(*entry)))) {
1134 ast_log(LOG_ERROR,
"Unable to allocate memory for event list\n");
1138 entry->event = event;
1143 long startdiff = labs(iter->event->
start - start);
1145 ast_debug(10,
"Comparing %s with startdiff %ld to %s with startdiff %ld\n", event->summary, event_startdiff, iter->event->summary, startdiff);
1147 if (startdiff > event_startdiff) {
1151 if (startdiff == event_startdiff) {
1152 long enddiff = labs(iter->event->
end - end);
1154 if (enddiff > event_enddiff) {
1158 if (event_startdiff == enddiff) {
1159 if (strcmp(event->uid, iter->event->uid) < 0) {
1181 if ((iter->event->
end - iter->event->
start) == (event->
end - event->
start)) {
1182 if (strcmp(event->uid, iter->event->uid) < 0) {
1187 if ((iter->event->
end - iter->event->
start) < (event->
end - event->
start)) {
1200 static void eventlist_destroy(
void *data)
1207 static void *eventlist_duplicate(
void *data)
1220 static int calendar_query_exec(
struct ast_channel *chan,
const char *cmd,
char *data,
char *buf,
size_t len)
1226 time_t start = INT_MIN, end = INT_MAX;
1235 ast_log(LOG_WARNING,
"%s requires a channel to store the data on\n", cmd);
1241 if (ast_strlen_zero(args.calendar)) {
1242 ast_log(LOG_WARNING,
"%s requires a calendar argument\n", cmd);
1246 if (!(cal = find_calendar(args.calendar))) {
1247 ast_log(LOG_WARNING,
"Unknown calendar '%s'\n", args.calendar);
1251 if (!(events = ao2_alloc(
sizeof(*events), eventlist_destructor))) {
1252 ast_log(LOG_ERROR,
"Unable to allocate memory for event list\n");
1253 cal = unref_calendar(cal);
1257 if (!ast_strlen_zero(args.start)) {
1258 start = atoi(args.start);
1261 if (!ast_strlen_zero(args.end)) {
1262 end = atoi(args.end);
1266 while ((event = ao2_iterator_next(&i))) {
1267 if (!(start > event->
end || end < event->start)) {
1268 ast_debug(10,
"%s (%ld - %ld) overlapped with (%ld - %ld)\n", event->summary, (
long) event->
start, (
long) event->
end, (
long) start, (
long) end);
1269 if (add_event_to_list(events, event, start, end) < 0) {
1271 cal = unref_calendar(cal);
1282 ast_channel_lock(chan);
1286 ast_channel_unlock(chan);
1288 if (!(eventlist_datastore = ast_datastore_alloc(&eventlist_datastore_info, buf))) {
1289 ast_log(LOG_ERROR,
"Could not allocate datastore!\n");
1290 cal = unref_calendar(cal);
1295 eventlist_datastore->
inheritance = DATASTORE_INHERIT_FOREVER;
1298 ast_channel_lock(chan);
1300 ast_channel_unlock(chan);
1302 cal = unref_calendar(cal);
1307 .
name =
"CALENDAR_QUERY",
1308 .read = calendar_query_exec,
1311 static void calendar_join_attendees(
struct ast_calendar_event *event,
char *buf,
size_t len)
1317 ast_log(LOG_ERROR,
"Could not allocate memory for attendees!\n");
1329 static int calendar_query_result_exec(
struct ast_channel *chan,
const char *cmd,
char *data,
char *buf,
size_t len)
1343 ast_log(LOG_WARNING,
"%s requires a channel\n", cmd);
1349 if (ast_strlen_zero(args.id) || ast_strlen_zero(args.field)) {
1350 ast_log(LOG_WARNING,
"%s requires an id and a field", cmd);
1354 ast_channel_lock(chan);
1356 ast_log(LOG_WARNING,
"There is no event notification datastore with id '%s' on '%s'!\n", args.id, ast_channel_name(chan));
1357 ast_channel_unlock(chan);
1360 ast_channel_unlock(chan);
1362 if (!(events = datastore->
data)) {
1363 ast_log(LOG_WARNING,
"The datastore contains no data!\n");
1367 if (!ast_strlen_zero(args.row)) {
1368 row = atoi(args.row);
1375 if (!strcasecmp(args.field,
"getnum")) {
1376 snprintf(buf, len,
"%zu", listlen);
1384 if (!strcasecmp(args.field,
"summary")) {
1386 }
else if (!strcasecmp(args.field,
"description")) {
1388 }
else if (!strcasecmp(args.field,
"organizer")) {
1390 }
else if (!strcasecmp(args.field,
"location")) {
1392 }
else if (!strcasecmp(args.field,
"categories")) {
1394 }
else if (!strcasecmp(args.field,
"priority")) {
1395 snprintf(buf, len,
"%d", entry->event->
priority);
1396 }
else if (!strcasecmp(args.field,
"calendar")) {
1398 }
else if (!strcasecmp(args.field,
"uid")) {
1400 }
else if (!strcasecmp(args.field,
"start")) {
1401 snprintf(buf, len,
"%ld", (
long) entry->event->
start);
1402 }
else if (!strcasecmp(args.field,
"end")) {
1403 snprintf(buf, len,
"%ld", (
long) entry->event->
end);
1404 }
else if (!strcasecmp(args.field,
"busystate")) {
1405 snprintf(buf, len,
"%u", entry->event->
busy_state);
1406 }
else if (!strcasecmp(args.field,
"attendees")) {
1407 calendar_join_attendees(entry->event, buf, len);
1409 ast_log(LOG_WARNING,
"Unknown field '%s'\n", args.field);
1418 .
name =
"CALENDAR_QUERY_RESULT",
1419 .read = calendar_query_result_exec,
1422 static int calendar_write_exec(
struct ast_channel *chan,
const char *cmd,
char *data,
const char *value)
1425 char *val_dup = NULL;
1437 ast_log(LOG_ERROR,
"Could not allocate memory for values\n");
1446 if (!(cal = find_calendar(fields.field[0]))) {
1447 ast_log(LOG_WARNING,
"Couldn't find calendar '%s'\n", fields.field[0]);
1452 ast_log(LOG_WARNING,
"Calendar '%s' has no write function!\n", cal->
name);
1460 if (ast_strlen_zero(fields.field[0])) {
1461 ast_log(LOG_WARNING,
"CALENDAR_WRITE requires a calendar name!\n");
1465 if (fields.argc - 1 !=
values.argc) {
1466 ast_log(LOG_WARNING,
"CALENDAR_WRITE should have the same number of fields (%u) and values (%u)!\n", fields.argc - 1,
values.argc);
1472 for (i = 1, j = 0; i < fields.argc; i++, j++) {
1473 if (!strcasecmp(fields.field[i],
"summary")) {
1475 }
else if (!strcasecmp(fields.field[i],
"description")) {
1477 }
else if (!strcasecmp(fields.field[i],
"organizer")) {
1479 }
else if (!strcasecmp(fields.field[i],
"location")) {
1481 }
else if (!strcasecmp(fields.field[i],
"categories")) {
1483 }
else if (!strcasecmp(fields.field[i],
"priority")) {
1484 event->priority = atoi(
values.value[j]);
1485 }
else if (!strcasecmp(fields.field[i],
"uid")) {
1487 }
else if (!strcasecmp(fields.field[i],
"start")) {
1488 event->start = atoi(
values.value[j]);
1489 }
else if (!strcasecmp(fields.field[i],
"end")) {
1490 event->end = atoi(
values.value[j]);
1491 }
else if (!strcasecmp(fields.field[i],
"busystate")) {
1492 event->busy_state = atoi(
values.value[j]);
1494 ast_log(LOG_WARNING,
"Unknown calendar event field '%s'\n", fields.field[i]);
1498 if (!event->
start) {
1499 event->start = tv.tv_sec;
1503 event->end = tv.tv_sec;
1507 ast_log(LOG_WARNING,
"Writing event to calendar '%s' failed!\n", cal->
name);
1517 cal = unref_calendar(cal);
1530 .
name =
"CALENDAR_WRITE",
1531 .write = calendar_write_exec,
1537 #define FORMAT "%-20.20s %-10.10s %-6.6s\n"
1543 e->
command =
"calendar show calendars";
1545 "Usage: calendar show calendars\n"
1546 " Lists all registered calendars.\n";
1552 ast_cli(a->fd, FORMAT,
"Calendar",
"Type",
"Status");
1553 ast_cli(a->fd, FORMAT,
"--------",
"----",
"------");
1555 while ((cal = ao2_iterator_next(&i))) {
1556 ast_cli(a->fd, FORMAT, cal->
name, cal->tech->type, calendar_is_busy(cal) ?
"busy" :
"free");
1557 cal = unref_calendar(cal);
1568 #define FORMAT "%-10.10s %-30.30s\n"
1574 e->
command =
"calendar show types";
1576 "Usage: calendar show types\n"
1577 " Lists all registered calendars types.\n";
1583 ast_cli(a->fd, FORMAT,
"Type",
"Description");
1586 ast_cli(a->fd, FORMAT, iter->type, iter->description);
1594 static char *epoch_to_string(
char *buf,
size_t buflen, time_t epoch)
1597 struct timeval tv = {
1611 static const char *ast_calendar_busy_state_to_str(
enum ast_calendar_busy_state busy_state)
1613 switch (busy_state) {
1614 case AST_CALENDAR_BS_FREE:
1616 case AST_CALENDAR_BS_BUSY_TENTATIVE:
1617 return "Busy (Tentative)";
1618 case AST_CALENDAR_BS_BUSY:
1621 return "Unknown (Busy)";
1628 #define FORMAT "%-18.18s : %-20.20s\n"
1629 #define FORMAT2 "%-12.12s: %-40.60s\n"
1638 e->
command =
"calendar show calendar";
1640 "Usage: calendar show calendar <calendar name>\n"
1641 " Displays information about a calendar\n";
1649 while ((cal = ao2_iterator_next(&i))) {
1650 if (!strncasecmp(a->word, cal->
name, strlen(a->word)) && ++which > a->n) {
1652 cal = unref_calendar(cal);
1655 cal = unref_calendar(cal);
1662 return CLI_SHOWUSAGE;
1665 if (!(cal = find_calendar(a->argv[3]))) {
1669 ast_cli(a->fd, FORMAT,
"Name", cal->
name);
1673 ast_cli(a->fd, FORMAT,
"Notify application", cal->
notify_app);
1675 ast_cli(a->fd,
"%-17.17s : %d\n",
"Refresh time", cal->
refresh);
1676 ast_cli(a->fd,
"%-17.17s : %d\n",
"Timeframe", cal->
timeframe);
1679 ast_cli(a->fd,
"%-17.17s : %d minutes before event\n",
"Autoreminder", cal->
autoreminder);
1681 ast_cli(a->fd,
"%-17.17s : None\n",
"Autoreminder");
1684 ast_cli(a->fd,
"%s\n",
"Events");
1685 ast_cli(a->fd,
"%s\n",
"------");
1688 while ((event = ao2_iterator_next(&i))) {
1691 ast_cli(a->fd, FORMAT2,
"Summary", event->summary);
1692 ast_cli(a->fd, FORMAT2,
"Description", event->description);
1693 ast_cli(a->fd, FORMAT2,
"Organizer", event->organizer);
1694 ast_cli(a->fd, FORMAT2,
"Location", event->location);
1695 ast_cli(a->fd, FORMAT2,
"Categories", event->categories);
1696 ast_cli(a->fd,
"%-12.12s: %d\n",
"Priority", event->
priority);
1697 ast_cli(a->fd, FORMAT2,
"UID", event->uid);
1698 ast_cli(a->fd, FORMAT2,
"Start", epoch_to_string(buf,
sizeof(buf), event->
start));
1699 ast_cli(a->fd, FORMAT2,
"End", epoch_to_string(buf,
sizeof(buf), event->
end));
1700 ast_cli(a->fd, FORMAT2,
"Alarm", epoch_to_string(buf,
sizeof(buf), event->
alarm));
1701 ast_cli(a->fd, FORMAT2,
"Busy State", ast_calendar_busy_state_to_str(event->
busy_state));
1702 ast_cli(a->fd,
"\n");
1707 cal = unref_calendar(cal);
1717 e->
command =
"calendar dump sched";
1719 "Usage: calendar dump sched\n"
1720 " Dump the calendar sched context";
1733 AST_CLI_DEFINE(handle_show_calendar,
"Display information about a calendar"),
1735 AST_CLI_DEFINE(handle_dump_sched,
"Dump calendar sched context"),
1739 static int calendar_event_read(
struct ast_channel *chan,
const char *cmd,
char *data,
char *buf,
size_t len)
1745 ast_log(LOG_WARNING,
"No channel was provided to %s function.\n", cmd);
1749 if (ast_strlen_zero(data)) {
1750 ast_log(LOG_WARNING,
"%s requires an argument\n", cmd);
1754 ast_channel_lock(chan);
1756 ast_log(LOG_WARNING,
"There is no event notification datastore on '%s'!\n", ast_channel_name(chan));
1757 ast_channel_unlock(chan);
1760 ast_channel_unlock(chan);
1762 if (!(event = datastore->
data)) {
1763 ast_log(LOG_WARNING,
"The datastore contains no data!\n");
1767 if (!strcasecmp(data,
"summary")) {
1769 }
else if (!strcasecmp(data,
"description")) {
1771 }
else if (!strcasecmp(data,
"organizer")) {
1773 }
else if (!strcasecmp(data,
"location")) {
1775 }
else if (!strcasecmp(data,
"categories")) {
1777 }
else if (!strcasecmp(data,
"priority")) {
1778 snprintf(buf, len,
"%d", event->
priority);
1779 }
else if (!strcasecmp(data,
"calendar")) {
1781 }
else if (!strcasecmp(data,
"uid")) {
1783 }
else if (!strcasecmp(data,
"start")) {
1784 snprintf(buf, len,
"%ld", (
long)event->
start);
1785 }
else if (!strcasecmp(data,
"end")) {
1786 snprintf(buf, len,
"%ld", (
long)event->
end);
1787 }
else if (!strcasecmp(data,
"busystate")) {
1789 }
else if (!strcasecmp(data,
"attendees")) {
1790 calendar_join_attendees(event, buf, len);
1798 .
name =
"CALENDAR_EVENT",
1799 .read = calendar_event_read,
1802 static int cb_pending_deletion(
void *user_data,
void *arg,
int flags)
1806 cal->pending_deletion = 1;
1811 static int cb_rm_pending_deletion(
void *user_data,
void *arg,
int flags)
1815 return cal->pending_deletion ?
CMP_MATCH : 0;
1818 static int reload(
void)
1822 ast_mutex_lock(&reloadlock);
1830 if (load_tech_calendars(iter)) {
1831 ast_log(LOG_WARNING,
"Failed to reload %s calendars, module disabled\n", iter->type);
1839 ast_mutex_unlock(&reloadlock);
1844 static void *do_refresh(
void *data)
1848 struct timespec ts = {0,};
1851 ast_mutex_lock(&refreshlock);
1853 while (!module_unloading) {
1858 ts.tv_sec = (now.tv_sec + wait / 1000) + 1;
1859 if (ast_cond_timedwait(&refresh_condition, &refreshlock, &ts) == ETIMEDOUT) {
1863 ast_mutex_unlock(&refreshlock);
1865 if (module_unloading) {
1875 static int unload_module(
void)
1889 ao2_cleanup(calendars);
1892 ast_mutex_lock(&refreshlock);
1893 module_unloading = 1;
1894 ast_cond_signal(&refresh_condition);
1895 ast_mutex_unlock(&refreshlock);
1896 pthread_join(refresh_thread, NULL);
1908 calendar_config = NULL;
1926 calendar_hash_fn, NULL, calendar_cmp_fn);
1928 ast_log(LOG_ERROR,
"Unable to allocate calendars container!\n");
1932 if (load_config(0)) {
1937 ast_mutex_init(&refreshlock);
1938 ast_cond_init(&refresh_condition, NULL);
1939 ast_mutex_init(&reloadlock);
1942 ast_log(LOG_ERROR,
"Unable to create sched context\n");
1944 calendar_config = NULL;
1948 if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
1949 ast_log(LOG_ERROR,
"Unable to start refresh thread--notifications disabled!\n");
1963 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
"Asterisk Calendar integration",
1964 .support_level = AST_MODULE_SUPPORT_EXTENDED,
1966 .unload = unload_module,
struct ast_variable * next
static char * handle_show_calendars(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI command to list available calendars.
static char * generate_random_string(char *buf, size_t size)
Generate 32 byte random string (stolen from chan_sip.c)
int fetch_again_at_reload
static char * handle_show_calendars_types(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI command to list of all calendars types currently loaded on the backend.
Main Channel structure associated with a channel.
ast_device_state
Device States.
int ast_dial_destroy(struct ast_dial *dial)
Destroys a dialing structure.
#define AST_LIST_LOCK(head)
Locks a list.
int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
Enables an option globally.
Asterisk main include file. File version handling, generic pbx functions.
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
struct ao2_container * ast_calendar_event_container_alloc(void)
Allocate an astobj2 container for ast_calendar_event objects.
static int load_module(void)
Load the module.
Main dialing structure. Contains global options, channels being dialed, and more! ...
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
int ast_sched_runq(struct ast_sched_context *con)
Runs the queue.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
Set the maximum time (globally) allowed for trying to ring phones.
struct ast_variable * vars
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
descriptor for a cli entry.
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
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)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Structure for variables, used for configurations and for channel variables.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
int ast_devstate_prov_del(const char *label)
Remove device state provider.
Structure for a data store type.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
const ast_string_field notify_app
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
Add device state provider.
enum ast_calendar_busy_state busy_state
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
static struct ast_calendar * build_calendar(struct ast_config *cfg, const char *cat, const struct ast_calendar_tech *tech)
int ast_calendar_register(struct ast_calendar_tech *tech)
Register a new calendar technology.
#define ast_strdup(str)
A wrapper for strdup()
Structure for a data store object.
void ast_calendar_unregister(struct ast_calendar_tech *tech)
Unregister a new calendar technology.
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
const ast_string_field notify_context
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
struct ast_calendar_event * ast_calendar_event_alloc(struct ast_calendar *cal)
Allocate an astobj2 ast_calendar_event object.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Configuration File Parser.
ast_dial_result
List of return codes for dial run API calls.
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
General Asterisk PBX channel definitions.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define ast_strdupa(s)
duplicate a string in memory from the stack
void *(* unref_calendar)(void *obj)
const struct ast_config * ast_calendar_config_acquire(void)
Grab and lock pointer to the calendar config (read only)
Data structure associated with a custom dialplan function.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Scheduler Routines (derived from cheops)
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
const ast_string_field notify_appdata
A general API for managing calendar events with Asterisk.
static struct stasis_rest_handlers events
REST handler for /api-docs/events.json.
A set of macros to manage forward-linked lists.
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
const ast_string_field name
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Core PBX routines and definitions.
enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
Execute dialing synchronously or asynchronously.
struct ao2_container * events
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
static int calendar_busy_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
A dialplan function that can be used to determine the busy status of a calendar.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Support for dynamic strings.
#define ao2_unlink(container, obj)
Remove an object from a container.
int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode)
Unload a module.
int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
Append a channel.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
void *(* load_calendar)(void *data)
#define ast_calloc(num, len)
A wrapper for calloc()
struct ast_dial * ast_dial_create(void)
New dialing structure.
struct ast_calendar * owner
void ast_calendar_config_release(void)
Release the calendar config.
const ast_string_field notify_extension
Prototypes for public functions only of internal interest,.
int ast_sched_del(struct ast_sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event.
Module has failed to load, may be in an inconsistent state.
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
int(* is_busy)(struct ast_calendar *calendar)
Structure used to handle boolean flags.
struct ast_channel * ast_dial_answered_steal(struct ast_dial *dial)
Steal the channel that answered.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Individual calendaring technology data.
void ast_sched_dump(struct ast_sched_context *con)
Dumps the scheduler contents.
enum ast_pbx_result ast_pbx_run(struct ast_channel *c)
Execute the PBX in the current thread.
int ast_sched_wait(struct ast_sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place.
Data structure associated with a single frame of data.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Asterisk calendar structure.
#define AST_LIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list entry before the current entry during a traversal.
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
int(* write_event)(struct ast_calendar_event *event)
#define ASTERISK_GPL_KEY
The text the key() function should return.
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
struct ast_calendar_event * ast_calendar_unref_event(struct ast_calendar_event *event)
Unreference an ast_calendar_event.
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
void ast_calendar_clear_events(struct ast_calendar *cal)
Remove all events from calendar.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
#define ast_custom_function_register(acf)
Register a custom function.
Structure for mutex and tracking information.
static force_inline int attribute_pure ast_str_case_hash(const char *str)
Compute a hash value on a case-insensitive string.
const ast_string_field notify_channel
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define AST_APP_ARG(name)
Define an application argument.
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
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_string_field_set(x, field, data)
Set a field to a simple string value.
#define ao2_link(container, obj)
Add an object to a container.