Newer
Older
} else {
c->cdr = ast_cdr_alloc();
if (!c->cdr) {
ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
free(c->pbx);
return -1;
}
ast_cdr_init(c->cdr, c);
}
}
memset(c->pbx, 0, sizeof(struct ast_pbx));
/* Set reasonable defaults */
c->pbx->rtimeout = 10;
c->pbx->dtimeout = 5;
if (option_debug)
ast_log(LOG_DEBUG, "PBX_THREAD(%s)\n", c->name);
else if (option_verbose > 1) {
if (c->callerid)
ast_verbose( VERBOSE_PREFIX_2 "Accepting call on '%s' (%s)\n", c->name, c->callerid);
else
ast_verbose( VERBOSE_PREFIX_2 "Accepting call on '%s'\n", c->name);
}
/* Start by trying whatever the channel is set to */
if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
strncpy(c->context, "default", sizeof(c->context)-1);
strncpy(c->exten, "s", sizeof(c->exten)-1);
while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
manager_event(EVENT_FLAG_CALL, "Newexten",
"Channel: %s\r\n"
"Context: %s\r\n"
"Extension: %s\r\n"
"Priority: %d\r\n",
c->name, c->context, c->exten, c->priority);
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
/* Something bad happened, or a hangup has been requested. */
switch(res) {
case AST_PBX_KEEPALIVE:
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
default:
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
c->_softhangup =0;
break;
}
goto out;
if (c->_softhangup) {
ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
/* If we're playing something in the background, wait for it to finish or for a digit */
digit = ast_waitstream(c, AST_DIGIT_ANY);
ast_stopstream(c);
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
c->_softhangup = 0;
} else {
/* Hang up if something goes wrong */
if (digit < 0) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Lost connection on %s\n", c->name);
goto out;
}
else if (digit) {
exten[pos++] = digit;
break;
}
}
}
firstpass = 0;
c->priority++;
}
if (!ast_exists_extension(c, c->context, c->exten, 1, c->callerid)) {
if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
c->name, c->exten, c->context);
/* Done, wait for an extension */
if (digit)
waittime = c->pbx->dtimeout;
else
waittime = c->pbx->rtimeout;
while(!ast_exists_extension(c, c->context, exten, 1, c->callerid) &&
ast_canmatch_extension(c, c->context, exten, 1, c->callerid)) {
/* As long as we're willing to wait, and as long as it's not defined,
keep reading digits until we can't possibly get a right answer anymore. */
digit = ast_waitfordigit(c, waittime * 1000);
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
c->_softhangup = 0;
} else {
if (!digit)
/* No entry */
break;
if (digit < 0)
/* Error, maybe a hangup */
goto out;
exten[pos++] = digit;
waittime = c->pbx->dtimeout;
}
if (ast_exists_extension(c, c->context, exten, 1, c->callerid)) {
/* No such extension */
if (strlen(exten)) {
/* An invalid extension */
if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Invalid extension, but no rule 'i' in context '%s'\n", c->context);
goto out;
}
if (ast_exists_extension(c, c->context, "t", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
goto out;
}
}
}
}
}
if (firstpass)
ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
out:
if (ast_exists_extension(c, c->context, "h", 1, c->callerid)) {
strcpy(c->exten, "h");
c->priority = 1;
while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
/* Something bad happened, or a hangup has been requested. */
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
break;
}
c->priority++;
}
}
if (res != AST_PBX_KEEPALIVE)
ast_hangup(c);
return 0;
}
static void *pbx_thread(void *data)
{
/* Oh joyeous kernel, we're a new thread, with nothing to do but
answer this channel and get it going. The setjmp stuff is fairly
confusing, but necessary to get smooth transitions between
the execution of different applications (without the use of
additional threads) */
struct ast_channel *c = data;
ast_pbx_run(c);
}
int ast_pbx_start(struct ast_channel *c)
{
pthread_t t;
if (!c) {
ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
return -1;
}
/* Start a new thread, and get something handling this channel. */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&t, &attr, pbx_thread, c)) {
ast_log(LOG_WARNING, "Failed to create new channel thread\n");
return -1;
}
return 0;
}
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
/*
* This function locks contexts list by &conlist, search for the rigt context
* structure, leave context list locked and call ast_context_remove_include2
* which removes include, unlock contexts list and return ...
*/
int ast_context_remove_include(char *context, char *include, char *registrar)
{
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts and search for the right one ...*/
c = ast_walk_contexts(NULL);
while (c) {
/* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) {
int ret;
/* remove include from this context ... */
ret = ast_context_remove_include2(c, include, registrar);
ast_unlock_contexts();
/* ... return results */
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right one context */
ast_unlock_contexts();
return -1;
}
/*
* When we call this function, &conlock lock must be locked, because when
* we giving *con argument, some process can remove/change this context
* and after that there can be segfault.
*
* This function locks given context, removes include, unlock context and
* return.
*/
int ast_context_remove_include2(struct ast_context *con, char *include, char *registrar)
{
struct ast_include *i, *pi = NULL;
if (ast_pthread_mutex_lock(&con->lock)) return -1;
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
/* walk includes */
i = con->includes;
while (i) {
/* find our include */
if (!strcmp(i->name, include) &&
(!strcmp(i->registrar, registrar) || !registrar)) {
/* remove from list */
if (pi)
pi->next = i->next;
else
con->includes = i->next;
/* free include and return */
free(i);
ast_pthread_mutex_unlock(&con->lock);
return 0;
}
pi = i;
i = i->next;
}
/* we can't find the right include */
ast_pthread_mutex_unlock(&con->lock);
return -1;
}
/*
* This function locks contexts list by &conlist, search for the rigt context
* structure, leave context list locked and call ast_context_remove_switch2
* which removes switch, unlock contexts list and return ...
*/
int ast_context_remove_switch(char *context, char *sw, char *data, char *registrar)
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts and search for the right one ...*/
c = ast_walk_contexts(NULL);
while (c) {
/* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) {
int ret;
/* remove switch from this context ... */
ret = ast_context_remove_switch2(c, sw, data, registrar);
ast_unlock_contexts();
/* ... return results */
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right one context */
ast_unlock_contexts();
/*
* When we call this function, &conlock lock must be locked, because when
* we giving *con argument, some process can remove/change this context
* and after that there can be segfault.
*
* This function locks given context, removes switch, unlock context and
* return.
*/
int ast_context_remove_switch2(struct ast_context *con, char *sw, char *data, char *registrar)
{
struct ast_sw *i, *pi = NULL;
if (ast_pthread_mutex_lock(&con->lock)) return -1;
1338
1339
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
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
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
1497
1498
1499
1500
1501
1502
1503
/* walk switchs */
i = con->alts;
while (i) {
/* find our switch */
if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
(!strcmp(i->registrar, registrar) || !registrar)) {
/* remove from list */
if (pi)
pi->next = i->next;
else
con->alts = i->next;
/* free switch and return */
free(i);
ast_pthread_mutex_unlock(&con->lock);
return 0;
}
pi = i;
i = i->next;
}
/* we can't find the right switch */
ast_pthread_mutex_unlock(&con->lock);
return -1;
}
/*
* This functions lock contexts list, search for the right context,
* call ast_context_remove_extension2, unlock contexts list and return.
* In this function we are using
*/
int ast_context_remove_extension(char *context, char *extension, int priority, char *registrar)
{
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts ... */
c = ast_walk_contexts(NULL);
while (c) {
/* ... search for the right one ... */
if (!strcmp(ast_get_context_name(c), context)) {
/* ... remove extension ... */
int ret = ast_context_remove_extension2(c, extension, priority,
registrar);
/* ... unlock contexts list and return */
ast_unlock_contexts();
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right context */
ast_unlock_contexts();
return -1;
}
/*
* When do you want to call this function, make sure that &conlock is locked,
* because some process can handle with your *con context before you lock
* it.
*
* This functionc locks given context, search for the right extension and
* fires out all peer in this extensions with given priority. If priority
* is set to 0, all peers are removed. After that, unlock context and
* return.
*/
int ast_context_remove_extension2(struct ast_context *con, char *extension, int priority, char *registrar)
{
struct ast_exten *exten, *prev_exten = NULL;
if (ast_pthread_mutex_lock(&con->lock)) return -1;
/* go through all extensions in context and search the right one ... */
exten = con->root;
while (exten) {
/* look for right extension */
if (!strcmp(exten->exten, extension) &&
(!strcmp(exten->registrar, registrar) || !registrar)) {
struct ast_exten *peer;
/* should we free all peers in this extension? (priority == 0)? */
if (priority == 0) {
/* remove this extension from context list */
if (prev_exten)
prev_exten->next = exten->next;
else
con->root = exten->next;
/* fire out all peers */
peer = exten;
while (peer) {
exten = peer->peer;
peer->datad(peer->data);
free(peer);
peer = exten;
}
ast_pthread_mutex_unlock(&con->lock);
return 0;
} else {
/* remove only extension with exten->priority == priority */
struct ast_exten *previous_peer = NULL;
peer = exten;
while (peer) {
/* is this our extension? */
if (peer->priority == priority &&
(!strcmp(peer->registrar, registrar) || !registrar)) {
/* we are first priority extension? */
if (!previous_peer) {
/* exists previous extension here? */
if (prev_exten) {
/* yes, so we must change next pointer in
* previous connection to next peer
*/
if (peer->peer) {
prev_exten->next = peer->peer;
peer->peer->next = exten->next;
} else
prev_exten->next = exten->next;
} else {
/* no previous extension, we are first
* extension, so change con->root ...
*/
if (peer->peer)
con->root = peer->peer;
else
con->root = exten->next;
}
} else {
/* we are not first priority in extension */
previous_peer->peer = peer->peer;
}
/* now, free whole priority extension */
peer->datad(peer->data);
free(peer);
ast_pthread_mutex_unlock(&con->lock);
return 0;
} else {
/* this is not right extension, skip to next peer */
previous_peer = peer;
peer = peer->peer;
}
}
ast_pthread_mutex_unlock(&con->lock);
return -1;
}
}
prev_exten = exten;
exten = exten->next;
}
/* we can't find right extension */
ast_pthread_mutex_unlock(&con->lock);
return -1;
}
int ast_register_application(char *app, int (*execute)(struct ast_channel *, void *), char *synopsis, char *description)
ast_log(LOG_ERROR, "Unable to lock application list\n");
return -1;
}
tmp = apps;
while(tmp) {
if (!strcasecmp(app, tmp->name)) {
ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
return -1;
}
tmp = tmp->next;
}
tmp = malloc(sizeof(struct ast_app));
if (tmp) {
tmp->synopsis = synopsis;
tmp->description = description;
tmp->next = apps;
apps = tmp;
} else {
ast_log(LOG_WARNING, "Out of memory\n");
ast_verbose( VERBOSE_PREFIX_2 "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
struct ast_switch *tmp, *prev=NULL;
if (ast_pthread_mutex_lock(&switchlock)) {
ast_log(LOG_ERROR, "Unable to lock switch lock\n");
if (!strcasecmp(tmp->name, sw->name))
break;
prev = tmp;
if (tmp) {
ast_pthread_mutex_unlock(&switchlock);
ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
return -1;
}
sw->next = NULL;
if (prev)
prev->next = sw;
else
switches = sw;
ast_pthread_mutex_unlock(&switchlock);
return 0;
void ast_unregister_switch(struct ast_switch *sw)
struct ast_switch *tmp, *prev=NULL;
if (ast_pthread_mutex_lock(&switchlock)) {
ast_log(LOG_ERROR, "Unable to lock switch lock\n");
return;
if (tmp == sw) {
if (prev)
prev->next = tmp->next;
else
switches = tmp->next;
tmp->next = NULL;
break;
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
/*
* Help for CLI commands ...
*/
static char show_application_help[] =
"Usage: show application <application> [<application> [<application> [...]]]\n"
" Describes a particular application.\n";
static char show_applications_help[] =
"Usage: show applications\n"
" List applications which are currently available.\n";
static char show_dialplan_help[] =
"Usage: show dialplan [exten@][context]\n"
" Show dialplan\n";
static char show_switches_help[] =
"Usage: show switches\n"
" Show registered switches\n";
/*
* IMPLEMENTATION OF CLI FUNCTIONS IS IN THE SAME ORDER AS COMMANDS HELPS
*
*/
/*
* 'show application' CLI command implementation functions ...
*/
/*
* There is a possibility to show informations about more than one
* application at one time. You can type 'show application Dial Echo' and
* you will see informations about these two applications ...
*/
static char *complete_show_application(char *line, char *word,
int pos, int state)
/* try to lock applications list ... */
if (ast_pthread_mutex_lock(&applock)) {
ast_log(LOG_ERROR, "Unable to lock application list\n");
/* ... walk all applications ... */
a = apps;
while (a) {
/* ... check if word matches this application ... */
if (!strncasecmp(word, a->name, strlen(word))) {
/* ... if this is right app serve it ... */
if (++which > state) {
char *ret = strdup(a->name);
ast_pthread_mutex_unlock(&applock);
return ret;
}
/* no application match */
ast_pthread_mutex_unlock(&applock);
return NULL;
}
static int handle_show_application(int fd, int argc, char *argv[])
{
int app, no_registered_app = 1;
if (argc < 3) return RESULT_SHOWUSAGE;
/* try to lock applications list ... */
if (ast_pthread_mutex_lock(&applock)) {
ast_log(LOG_ERROR, "Unable to lock application list\n");
return -1;
}
/* ... go through all applications ... */
a = apps;
while (a) {
/* ... compare this application name with all arguments given
* to 'show application' command ... */
for (app = 2; app < argc; app++) {
if (!strcasecmp(a->name, argv[app])) {
no_registered_app = 0;
/* ... one of our applications, show info ...*/
snprintf(buf, sizeof(buf),
"\n -= Info about application '%s' =- \n\n"
"[Synopsis]:\n %s\n\n"
"[Description]:\n%s\n",
a->name,
a->synopsis ? a->synopsis : "Not available",
a->description ? a-> description : "Not available");
ast_cli(fd, buf);
}
/* we found at least one app? no? */
if (no_registered_app) {
ast_cli(fd, "Your application(s) is (are) not registered\n");
return RESULT_FAILURE;
}
static int handle_show_switches(int fd, int argc, char *argv[])
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
struct ast_switch *sw;
if (!switches) {
ast_cli(fd, "There are no registered alternative switches\n");
return RESULT_SUCCESS;
}
/* ... we have applications ... */
ast_cli(fd, "\n -= Registered Asterisk Alternative Switches =-\n");
if (ast_pthread_mutex_lock(&switchlock)) {
ast_log(LOG_ERROR, "Unable to lock switches\n");
return -1;
}
sw = switches;
while (sw) {
ast_cli(fd, "%s: %s\n", sw->name, sw->description);
sw = sw->next;
}
ast_pthread_mutex_unlock(&switchlock);
return RESULT_SUCCESS;
}
/*
* 'show applications' CLI command implementation functions ...
*/
static int handle_show_applications(int fd, int argc, char *argv[])
{
struct ast_app *a;
/* try to lock applications list ... */
if (ast_pthread_mutex_lock(&applock)) {
ast_log(LOG_ERROR, "Unable to lock application list\n");
return -1;
}
/* ... go to first application ... */
a = apps;
/* ... have we got at least one application (first)? no? */
if (!a) {
ast_cli(fd, "There is no registered applications\n");
ast_pthread_mutex_unlock(&applock);
return -1;
}
/* ... we have applications ... */
ast_cli(fd, "\n -= Registered Asterisk Applications =-\n");
/* ... go through all applications ... */
while (a) {
/* ... show informations about applications ... */
ast_cli(fd," %15s: %s\n",
a->name,
a->synopsis ? a->synopsis : "<Synopsis not available>");
a = a->next;
}
/* ... unlock and return */
ast_pthread_mutex_unlock(&applock);
return RESULT_SUCCESS;
}
/*
* 'show dialplan' CLI command implementation functions ...
*/
static char *complete_show_dialplan_context(char *line, char *word, int pos,
int state)
{
struct ast_context *c;
int which = 0;
/* we are do completion of [exten@]context on second postion only */
if (pos != 2) return NULL;
/* try to lock contexts list ... */
if (ast_lock_contexts()) {
ast_log(LOG_ERROR, "Unable to lock context list\n");
return NULL;
}
/* ... walk through all contexts ... */
c = ast_walk_contexts(NULL);
while(c) {
/* ... word matches context name? yes? ... */
if (!strncasecmp(word, ast_get_context_name(c), strlen(word))) {
/* ... for serve? ... */
if (++which > state) {
/* ... yes, serve this context name ... */
char *ret = strdup(ast_get_context_name(c));
ast_unlock_contexts();
return ret;
}
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
/* ... unlock and return */
ast_unlock_contexts();
return NULL;
}
static int handle_show_dialplan(int fd, int argc, char *argv[])
{
struct ast_context *c;
char *exten = NULL, *context = NULL;
int context_existence = 0, extension_existence = 0;
if (argc != 3 && argc != 2) return -1;
/* we obtain [exten@]context? if yes, split them ... */
if (argc == 3) {
char *splitter = argv[2];
/* is there a '@' character? */
if (strchr(argv[2], '@')) {
/* yes, split into exten & context ... */
exten = strsep(&splitter, "@");
context = splitter;
/* check for length and change to NULL if !strlen() */
if (!strlen(exten)) exten = NULL;
if (!strlen(context)) context = NULL;
} else
{
/* no '@' char, only context given */
context = argv[2];
if (!strlen(context)) context = NULL;
}
}
/* try to lock contexts */
if (ast_lock_contexts()) {
ast_cli(LOG_WARNING, "Failed to lock contexts list\n");
return RESULT_FAILURE;
}
/* walk all contexts ... */
c = ast_walk_contexts(NULL);
while (c) {
/* show this context? */
if (!context ||
!strcmp(ast_get_context_name(c), context)) {
context_existence = 1;
/* try to lock context before walking in ... */
if (!ast_lock_context(c)) {
struct ast_exten *e;
struct ast_include *i;
struct ast_ignorepat *ip;
struct ast_sw *sw;
char buf[256], buf2[256];
int context_info_printed = 0;
/* are we looking for exten too? if yes, we print context
* if we our extension only
*/
if (!exten) {
ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
ast_get_context_name(c), ast_get_context_registrar(c));
context_info_printed = 1;
}
/* walk extensions ... */
e = ast_walk_context_extensions(c, NULL);
while (e) {
struct ast_exten *p;
/* looking for extension? is this our extension? */
if (exten &&
strcmp(ast_get_extension_name(e), exten))
{
/* we are looking for extension and it's not our
* extension, so skip to next extension */
e = ast_walk_context_extensions(c, e);
continue;
}
extension_existence = 1;
/* may we print context info? */
if (!context_info_printed) {
ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
ast_get_context_name(c),
ast_get_context_registrar(c));
context_info_printed = 1;
}
/* write extension name and first peer */
bzero(buf, sizeof(buf));
snprintf(buf, sizeof(buf), "'%s' =>",
ast_get_extension_name(e));
snprintf(buf2, sizeof(buf2),
"%d. %s(%s)",
ast_get_extension_priority(e),
ast_get_extension_app(e),
(char *)ast_get_extension_app_data(e));
ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2,
ast_get_extension_registrar(e));
/* walk next extension peers */
p = ast_walk_extension_priorities(e, e);
while (p) {
bzero((void *)buf2, sizeof(buf2));
snprintf(buf2, sizeof(buf2),
"%d. %s(%s)",
ast_get_extension_priority(p),
ast_get_extension_app(p),
(char *)ast_get_extension_app_data(p));
ast_cli(fd," %-17s %-45s [%s]\n",
"", buf2,
ast_get_extension_registrar(p));
p = ast_walk_extension_priorities(e, p);
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
/* include & ignorepat we all printing if we are not
* looking for exact extension
*/
if (!exten) {
if (ast_walk_context_extensions(c, NULL))
ast_cli(fd, "\n");
/* walk included and write info ... */
i = ast_walk_context_includes(c, NULL);
while (i) {
bzero(buf, sizeof(buf));
snprintf(buf, sizeof(buf), "'%s'",
ast_get_include_name(i));
ast_cli(fd, " Include => %-45s [%s]\n",
buf, ast_get_include_registrar(i));
i = ast_walk_context_includes(c, i);
}
/* walk ignore patterns and write info ... */
ip = ast_walk_context_ignorepats(c, NULL);
while (ip) {
bzero(buf, sizeof(buf));
snprintf(buf, sizeof(buf), "'%s'",
ast_get_ignorepat_name(ip));
ast_cli(fd, " Ignore pattern => %-45s [%s]\n",
buf, ast_get_ignorepat_registrar(ip));
ip = ast_walk_context_ignorepats(c, ip);
}
sw = ast_walk_context_switches(c, NULL);
while(sw) {
bzero(buf, sizeof(buf));
snprintf(buf, sizeof(buf), "'%s/%s'",
ast_get_switch_name(sw),
ast_get_switch_data(sw));
ast_cli(fd, " Alt. Switch => %-45s [%s]\n",
buf, ast_get_switch_registrar(sw));
sw = ast_walk_context_switches(c, sw);
}
}
ast_unlock_context(c);
/* if we print something in context, make an empty line */
if (context_info_printed) ast_cli(fd, "\n");
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
ast_unlock_contexts();
/* check for input failure and throw some error messages */
if (context && !context_existence) {
ast_cli(fd, "There is no existence of '%s' context\n",
context);
return RESULT_FAILURE;
}
if (exten && !extension_existence) {
if (context)
ast_cli(fd, "There is no existence of %s@%s extension\n",
exten, context);
else
ast_cli(fd,
"There is no existence of '%s' extension in all contexts\n",
exten);
return RESULT_FAILURE;
}
/* everything ok */
/*
* CLI entries for upper commands ...
*/