Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
} else {
context = NULL;
exten = NULL;
}
ast_pthread_mutex_lock(&conlock);
con = contexts;
while(con) {
if (!context || (!strcasecmp(context, con->name))) {
ast_cli(fd, "\n [ Context '%s' created by '%s']\n", con->name, con->registrar);
eroot = con->root;
while(eroot) {
if (!exten || (!strcasecmp(exten, eroot->exten))) {
memset(tmp, ' ', sizeof(tmp));
snprintf(tmp, sizeof(tmp), " '%s' => ", eroot->exten);
spaces = strlen(tmp);
tmp[spaces] = ' ';
if (spaces < 19)
spaces = 19;
snprintf(tmp2, sizeof(tmp2), "%d. %s(%s)", eroot->priority, eroot->app, (char *)eroot->data);
snprintf(tmp + spaces, sizeof(tmp) - spaces, "%-45s [%s]\n",
tmp2, eroot->registrar);
ast_cli(fd, tmp);
memset(tmp, ' ', spaces);
e = eroot->peer;
while(e) {
snprintf(tmp2, sizeof(tmp2), "%d. %s(%s)", e->priority, e->app, (char *)e->data);
snprintf(tmp + spaces, sizeof(tmp) - spaces, "%-45s [%s]\n",
tmp2, e->registrar);
ast_cli(fd, tmp);
e = e->peer;
}
}
eroot = eroot->next;
}
inc = con->includes;
while(inc) {
snprintf(tmp, sizeof(tmp), " Include => '%s'", inc->name);
ast_cli(fd, "%s [%s]\n", tmp, inc->registrar);
inc = inc->next;
}
}
con = con->next;
}
ast_pthread_mutex_unlock(&conlock);
return RESULT_SUCCESS;
}
static struct ast_cli_entry showapps = { { "show", "applications", NULL },
handle_show_applications, "Shows registered applications", apps_help };
static struct ast_cli_entry showdialplan = { { "show", "dialplan", NULL },
handle_dialplan, "Displays all or part of dialplan", dialplan_help, complete_context };
static struct ast_cli_entry showapp = { { "show", "application", NULL },
handle_show_application, "Describe a specific application", app_help, complete_app };
int ast_unregister_application(char *app) {
struct ast_app *tmp, *tmpl = NULL;
ast_log(LOG_ERROR, "Unable to lock application list\n");
return -1;
}
tmp = apps;
while(tmp) {
if (!strcasecmp(app, tmp->name)) {
if (tmpl)
tmpl->next = tmp->next;
else
apps = tmp->next;
if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Unregistered application '%s'\n", tmp->name);
return 0;
}
tmpl = tmp;
tmp = tmp->next;
}
struct ast_context *ast_context_create(char *name, char *registrar)
tmp = contexts;
while(tmp) {
if (!strcasecmp(tmp->name, name)) {
ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
return NULL;
}
tmp = tmp->next;
}
tmp = malloc(sizeof(struct ast_context));
if (tmp) {
pthread_mutex_init(&tmp->lock, NULL);
strncpy(tmp->name, name, sizeof(tmp->name));
tmp->root = NULL;
contexts = tmp;
if (option_debug)
ast_log(LOG_DEBUG, "Registered context '%s'\n", tmp->name);
else if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Registered extension context '%s'\n", tmp->name);
} else
ast_log(LOG_WARNING, "Out of memory\n");
int ast_context_add_include2(struct ast_context *con, char *value, char *registrar)
{
struct ast_include *inc, *incc, *incl = NULL;
inc = malloc(sizeof(struct ast_include));
if (!inc) {
ast_log(LOG_WARNING, "Out of memory\n");
return -1;
}
strncpy(inc->name, value, sizeof(inc->name));
inc->next = NULL;
pthread_mutex_lock(&con->lock);
incc = con->includes;
while(incc) {
incl = incc;
if (!strcasecmp(incc->name, value)) {
/* Already there */
pthread_mutex_unlock(&con->lock);
return 0;
}
incc = incc->next;
}
if (incl)
incl->next = inc;
else
con->includes = inc;
pthread_mutex_unlock(&con->lock);
return 0;
}
int ast_add_extension2(struct ast_context *con,
int replace, char *extension, int priority,
char *application, void *data, void (*datad)(void *),
char *registrar)
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
{
#define LOG { if (option_debug) \
ast_log(LOG_DEBUG, "Added extension '%s' priority %d to %s\n", tmp->exten, tmp->priority, con->name); \
else if (option_verbose > 2) \
ast_verbose( VERBOSE_PREFIX_3 "Added extension '%s' priority %d to %s\n", tmp->exten, tmp->priority, con->name); \
}
/*
* This is a fairly complex routine. Different extensions are kept
* in order by the extension number. Then, extensions of different
* priorities (same extension) are kept in a list, according to the
* peer pointer.
*/
struct ast_exten *tmp, *e, *el = NULL, *ep = NULL;
int res;
/* Be optimistic: Build the extension structure first */
tmp = malloc(sizeof(struct ast_exten));
if (tmp) {
strncpy(tmp->exten, extension, sizeof(tmp->exten));
tmp->priority = priority;
strncpy(tmp->app, application, sizeof(tmp->app));
tmp->data = data;
tmp->datad = datad;
tmp->peer = NULL;
tmp->next = NULL;
} else {
ast_log(LOG_WARNING, "Out of memory\n");
return -1;
}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
free(tmp);
/* And properly destroy the data */
datad(data);
ast_log(LOG_WARNING, "Failed to lock context '%s'\n", con->name);
return -1;
}
e = con->root;
while(e) {
res= strcasecmp(e->exten, extension);
if (res == 0) {
/* We have an exact match, now we find where we are
and be sure there's no duplicates */
while(e) {
if (e->priority == tmp->priority) {
/* Can't have something exactly the same. Is this a
replacement? If so, replace, otherwise, bonk. */
if (replace) {
if (ep) {
/* We're in the peer list, insert ourselves */
ep->peer = tmp;
tmp->peer = e->peer;
} else if (el) {
/* We're the first extension. Take over e's functions */
el->next = tmp;
tmp->next = e->next;
tmp->peer = e->peer;
} else {
/* We're the very first extension. */
con->root = tmp;
tmp->next = e->next;
tmp->peer = e->peer;
}
/* Destroy the old one */
e->datad(e->data);
free(e);
/* And immediately return success. */
LOG;
return 0;
} else {
ast_log(LOG_WARNING, "Unable to register extension '%s', priority %d in '%s', already in use\n", tmp->exten, tmp->priority, con->name);
tmp->datad(tmp->data);
free(tmp);
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
return -1;
}
} else if (e->priority > tmp->priority) {
/* Slip ourselves in just before e */
if (ep) {
/* Easy enough, we're just in the peer list */
ep->peer = tmp;
tmp->peer = e;
} else if (el) {
/* We're the first extension in this peer list */
el->next = tmp;
tmp->next = e->next;
e->next = NULL;
tmp->peer = e;
} else {
/* We're the very first extension altogether */
tmp->next = con->root;
/* Con->root must always exist or we couldn't get here */
tmp->peer = con->root->peer;
con->root = tmp;
}
/* And immediately return success. */
LOG;
return 0;
}
ep = e;
e = e->peer;
}
/* If we make it here, then it's time for us to go at the very end.
ep *must* be defined or we couldn't have gotten here. */
ep->peer = tmp;
/* And immediately return success. */
LOG;
return 0;
} else if (res > 0) {
/* Insert ourselves just before 'e'. We're the first extension of
this kind */
tmp->next = e;
if (el) {
/* We're in the list somewhere */
el->next = tmp;
} else {
/* We're at the top of the list */
con->root = tmp;
}
/* And immediately return success. */
LOG;
return 0;
}
el = e;
e = e->next;
}
/* If we fall all the way through to here, then we need to be on the end. */
if (el)
el->next = tmp;
else
con->root = tmp;
void ast_context_destroy(struct ast_context *con, char *registrar)
if (((tmp == con) || !con) &&
(!registrar || !strcasecmp(registrar, tmp->registrar))) {
/* Okay, let's lock the structure to be sure nobody else
is searching through it. */
ast_log(LOG_WARNING, "Unable to lock context lock\n");
return;
}
if (tmpl)
tmpl->next = tmp->next;
else
contexts = tmp->next;
/* Okay, now we're safe to let it go -- in a sense, we were
ready to let it go as soon as we locked it. */
for (tmpi = tmp->includes; tmpi; ) {
/* Free includes */
tmpil = tmpi;
tmpi = tmpi->next;
free(tmpil);
tmpil = tmpi;
}
if (!con) {
/* Might need to get another one -- restart */
tmp = contexts;
tmpl = NULL;
tmpil = NULL;
continue;
}
ast_pthread_mutex_unlock(&conlock);
return;
}
tmpl = tmp;
tmp = tmp->next;
}
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
static void wait_for_hangup(struct ast_channel *chan)
{
int res;
struct ast_frame *f;
do {
res = ast_waitfor(chan, -1);
if (res < 0)
return;
f = ast_read(chan);
if (f)
ast_frfree(f);
} while(f);
}
static int pbx_builtin_ringing(struct ast_channel *chan, void *data)
{
ast_indicate(chan, AST_CONTROL_RINGING);
return 0;
}
static int pbx_builtin_busy(struct ast_channel *chan, void *data)
{
ast_indicate(chan, AST_CONTROL_BUSY);
wait_for_hangup(chan);
return -1;
}
static int pbx_builtin_congestion(struct ast_channel *chan, void *data)
{
ast_indicate(chan, AST_CONTROL_CONGESTION);
wait_for_hangup(chan);
return -1;
}
static int pbx_builtin_answer(struct ast_channel *chan, void *data)
if (option_debug)
ast_log(LOG_DEBUG, "Ignoring answer request since line is not ringing\n");
return 0;
} else
return ast_answer(chan);
}
static int pbx_builtin_setlanguage(struct ast_channel *chan, void *data)
{
/* Copy the language as specified */
strncpy(chan->language, (char *)data, sizeof(chan->language));
return 0;
}
static int pbx_builtin_hangup(struct ast_channel *chan, void *data)
{
/* Just return non-zero and it will hang up */
return -1;
}
static int pbx_builtin_stripmsd(struct ast_channel *chan, void *data)
{
char newexten[AST_MAX_EXTENSION] = "";
if (!data || !atoi(data)) {
ast_log(LOG_DEBUG, "Ignoring, since number of digits to strip is 0\n");
return 0;
}
if (strlen(chan->exten) > atoi(data)) {
strncpy(newexten, chan->exten + atoi(data), sizeof(newexten));
}
strncpy(chan->exten, newexten, sizeof(chan->exten));
return 0;
}
static int pbx_builtin_prefix(struct ast_channel *chan, void *data)
{
char newexten[AST_MAX_EXTENSION] = "";
if (!data || !strlen(data)) {
ast_log(LOG_DEBUG, "Ignoring, since there is no prefix to add\n");
return 0;
}
snprintf(newexten, sizeof(newexten), "%s%s", (char *)data, chan->exten);
strncpy(chan->exten, newexten, sizeof(chan->exten));
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Prepended prefix, new extension is %s\n", chan->exten);
static int pbx_builtin_wait(struct ast_channel *chan, void *data)
{
/* Wait for "n" seconds */
if (data && atoi((char *)data))
sleep(atoi((char *)data));
return 0;
}
static int pbx_builtin_background(struct ast_channel *chan, void *data)
/* Answer if need be */
if (chan->state != AST_STATE_UP)
if (ast_answer(chan))
return -1;
/* Stop anything playing */
ast_stopstream(chan);
/* Stream a file */
res = ast_streamfile(chan, (char *)data, chan->language);
static int pbx_builtin_rtimeout(struct ast_channel *chan, void *data)
{
/* Set the timeout for how long to wait between digits */
chan->pbx->rtimeout = atoi((char *)data);
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Set Response Timeout to %d\n", chan->pbx->rtimeout);
return 0;
}
static int pbx_builtin_dtimeout(struct ast_channel *chan, void *data)
{
/* Set the timeout for how long to wait between digits */
chan->pbx->dtimeout = atoi((char *)data);
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Set Digit Timeout to %d\n", chan->pbx->dtimeout);
return 0;
}
static int pbx_builtin_goto(struct ast_channel *chan, void *data)
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
{
char *s;
char *exten, *pri, *context;
if (!data) {
ast_log(LOG_WARNING, "Goto requires an argument (optional context|optional extension|priority)\n");
return -1;
}
s = strdup((void *) data);
context = strtok(s, "|");
exten = strtok(NULL, "|");
if (!exten) {
/* Only a priority in this one */
pri = context;
exten = NULL;
context = NULL;
} else {
pri = strtok(NULL, "|");
if (!pri) {
/* Only an extension and priority in this one */
pri = exten;
exten = context;
context = NULL;
}
}
if (atoi(pri) < 0) {
ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", pri);
free(s);
return -1;
}
/* At this point we have a priority and maybe an extension and a context */
chan->priority = atoi(pri) - 1;
strncpy(chan->exten, exten, sizeof(chan->exten));
if (context)
strncpy(chan->context, context, sizeof(chan->context));
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Goto (%s,%s,%d)\n", chan->context,chan->exten, chan->priority+1);
return 0;
}
int load_pbx(void)
{
int x;
/* Initialize the PBX */
if (option_verbose) {
ast_verbose( "Asterisk PBX Core Initializing\n");
ast_verbose( "Registering builtin applications:\n");
}
ast_cli_register(&showapps);
ast_cli_register(&showapp);
ast_cli_register(&showdialplan);
for (x=0;x<sizeof(builtins) / sizeof(struct pbx_builtin); x++) {
if (option_verbose)
ast_verbose( VERBOSE_PREFIX_1 "[%s]\n", builtins[x].name);
if (ast_register_application(builtins[x].name, builtins[x].execute, builtins[x].synopsis, builtins[x].description)) {
ast_log(LOG_ERROR, "Unable to register builtin application '%s'\n", builtins[x].name);
return -1;
}
}
return 0;
}