Newer
Older
if (args) {
if (process_token(state->fdn, args, sizeof(state->fdn) - 1, ARG_STRING | ARG_NUMBER))
ast_log(LOG_WARNING, "'%s' is not a valid token for FDN at line %d of %s\n", args, lineno, script);
} else
ast_log(LOG_WARNING, "Missing argument for FDN at line %d of %s\n", lineno, script);
} else if (!strcasecmp(keyword, "KEY")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "KEY definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
break;
}
state->key = getkeybyname(state, vname, script, lineno);
if (!state->key) {
ast_log(LOG_WARNING, "Out of key space at line %d of %s\n", lineno, script);
break;
}
if (state->key->defined) {
ast_log(LOG_WARNING, "Cannot redefine key '%s' at line %d of %s\n", vname, lineno, script);
break;
}
args = get_token(&buf, script, lineno);
if (!args || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
break;
}
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "KEY definition missing short name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY short name at line %d of %s\n", args, lineno, script);
break;
}
args = get_token(&buf, script, lineno);
if (args) {
if (strcasecmp(args, "OR")) {
ast_log(LOG_WARNING, "Expecting 'OR' but got '%s' instead at line %d of %s\n", args, lineno, script);
break;
}
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "KEY definition missing optional long name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp2, args, sizeof(tmp2) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY long name at line %d of %s\n", args, lineno, script);
ast_copy_string(tmp2, tmp, sizeof(tmp2));
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
}
if (strlen(tmp2) > 18) {
ast_log(LOG_WARNING, "Truncating full name to 18 characters at line %d of %s\n", lineno, script);
tmp2[18] = '\0';
}
if (strlen(tmp) > 7) {
ast_log(LOG_WARNING, "Truncating short name to 7 bytes at line %d of %s\n", lineno, script);
tmp[7] = '\0';
}
/* Setup initial stuff */
state->key->retstr[0] = 128;
/* 1 has the length */
state->key->retstr[2] = state->key->id;
/* Put the Full name in */
memcpy(state->key->retstr + 3, tmp2, strlen(tmp2));
/* Update length */
state->key->retstrlen = strlen(tmp2) + 3;
/* Put trailing 0xff */
state->key->retstr[state->key->retstrlen++] = 0xff;
/* Put the short name */
memcpy(state->key->retstr + state->key->retstrlen, tmp, strlen(tmp));
/* Update length */
state->key->retstrlen += strlen(tmp);
/* Put trailing 0xff */
state->key->retstr[state->key->retstrlen++] = 0xff;
/* Record initial length */
state->key->initlen = state->key->retstrlen;
state->state = STATE_INKEY;
} else if (!strcasecmp(keyword, "SUB")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
break;
}
state->sub = getsubbyname(state, vname, script, lineno);
if (!state->sub) {
ast_log(LOG_WARNING, "Out of subroutine space at line %d of %s\n", lineno, script);
break;
}
if (state->sub->defined) {
ast_log(LOG_WARNING, "Cannot redefine subroutine '%s' at line %d of %s\n", vname, lineno, script);
break;
}
/* Setup sub */
state->sub->data[0] = 130;
/* 1 is the length */
state->sub->data[2] = 0x0; /* Clear extensibility bit */
state->sub->datalen = 3;
if (state->sub->id) {
/* If this isn't the main subroutine, make a subroutine label for it */
state->sub->data[3] = 9;
state->sub->data[4] = state->sub->id;
/* 5 is length */
state->sub->data[6] = 0xff;
state->sub->datalen = 7;
}
args = get_token(&buf, script, lineno);
if (!args || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
break;
}
state->state = STATE_INSUB;
} else if (!strcasecmp(keyword, "STATE")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "STATE definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a STATE name at line %d of %s\n", args, lineno, script);
break;
}
if (getstatebyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "State '%s' is already defined at line %d of %s\n", vname, lineno, script);
break;
}
getstatebyname(state, vname, script, lineno, 1);
} else if (!strcasecmp(keyword, "FLAG")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "FLAG definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a FLAG name at line %d of %s\n", args, lineno, script);
break;
}
if (getflagbyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "Flag '%s' is already defined\n", vname);
break;
}
getflagbyname(state, vname, script, lineno, 1);
} else if (!strcasecmp(keyword, "DISPLAY")) {
lrci = 0;
wi = 0;
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
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
1218
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
1265
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
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
break;
}
if (getdisplaybyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "State '%s' is already defined\n", vname);
break;
}
disp = getdisplaybyname(state, vname, script, lineno, 1);
if (!disp)
break;
args = get_token(&buf, script, lineno);
if (!args || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Missing 'IS' at line %d of %s\n", lineno, script);
break;
}
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "Missing Column 1 text at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "Token '%s' is not valid column 1 text at line %d of %s\n", args, lineno, script);
break;
}
if (strlen(tmp) > 20) {
ast_log(LOG_WARNING, "Truncating column one to 20 characters at line %d of %s\n", lineno, script);
tmp[20] = '\0';
}
memcpy(disp->data + 5, tmp, strlen(tmp));
disp->datalen = strlen(tmp) + 5;
disp->data[disp->datalen++] = 0xff;
args = get_token(&buf, script, lineno);
if (args && !process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
/* Got a column two */
if (strlen(tmp) > 20) {
ast_log(LOG_WARNING, "Truncating column two to 20 characters at line %d of %s\n", lineno, script);
tmp[20] = '\0';
}
memcpy(disp->data + disp->datalen, tmp, strlen(tmp));
disp->datalen += strlen(tmp);
args = get_token(&buf, script, lineno);
}
while(args) {
if (!strcasecmp(args, "JUSTIFY")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "Qualifier 'JUSTIFY' requires an argument at line %d of %s\n", lineno, script);
break;
}
lrci = getjustifybyname(args);
if (lrci < 0) {
ast_log(LOG_WARNING, "'%s' is not a valid justification at line %d of %s\n", args, lineno, script);
break;
}
} else if (!strcasecmp(args, "WRAP")) {
wi = 0x80;
} else {
ast_log(LOG_WARNING, "'%s' is not a known qualifier at line %d of %s\n", args, lineno, script);
break;
}
args = get_token(&buf, script, lineno);
}
if (args) {
/* Something bad happened */
break;
}
disp->data[0] = 129;
disp->data[1] = disp->datalen - 2;
disp->data[2] = ((lrci & 0x3) << 6) | disp->id;
disp->data[3] = wi;
disp->data[4] = 0xff;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in PROGRAM\n", keyword);
}
break;
case STATE_INKEY:
if (process_returncode(state->key, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDKEY")) {
/* Return to normal operation and increment current key */
state->state = STATE_NORMAL;
state->key->defined = 1;
state->key->retstr[1] = state->key->retstrlen - 2;
state->key = NULL;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SOFTKEY definition at line %d of %s\n", keyword, lineno, script);
}
}
break;
case STATE_INIF:
if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDIF")) {
/* Return to normal SUB operation and increment current key */
state->state = STATE_INSUB;
state->sub->defined = 1;
/* Store the proper number of instructions */
state->sub->ifdata[2] = state->sub->ifinscount;
} else if (!strcasecmp(keyword, "GOTO")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "GOTO clause missing Subscript name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid subscript name token at line %d of %s\n", args, lineno, script);
break;
}
newsub = getsubbyname(state, tmp, script, lineno);
if (!newsub)
break;
/* Somehow you use GOTO to go to another place */
state->sub->data[state->sub->datalen++] = 0x8;
state->sub->data[state->sub->datalen++] = state->sub->ifdata[1];
state->sub->data[state->sub->datalen++] = newsub->id;
/* Terminate */
state->sub->data[state->sub->datalen++] = 0xff;
/* Increment counters */
state->sub->inscount++;
state->sub->ifinscount++;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in IF clause at line %d of %s\n", keyword, lineno, script);
}
} else
state->sub->ifinscount++;
break;
case STATE_INSUB:
if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDSUB")) {
/* Return to normal operation and increment current key */
state->state = STATE_NORMAL;
state->sub->defined = 1;
/* Store the proper length */
state->sub->data[1] = state->sub->datalen - 2;
if (state->sub->id) {
/* if this isn't main, store number of instructions, too */
state->sub->data[5] = state->sub->inscount;
}
state->sub = NULL;
} else if (!strcasecmp(keyword, "IFEVENT")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "IFEVENT clause missing Event name at line %d of %s\n", lineno, script);
break;
}
event = geteventbyname(args);
if (event < 1) {
ast_log(LOG_WARNING, "'%s' is not a valid event\n", args);
break;
}
args = get_token(&buf, script, lineno);
if (!args || strcasecmp(args, "THEN")) {
ast_log(LOG_WARNING, "IFEVENT clause missing 'THEN' at line %d of %s\n", lineno, script);
break;
}
state->sub->ifinscount = 0;
state->sub->ifdata = state->sub->data +
state->sub->datalen;
/* Reserve header and insert op codes */
state->sub->ifdata[0] = 0x1;
state->sub->ifdata[1] = event;
/* 2 is for the number of instructions */
state->sub->ifdata[3] = 0xff;
state->sub->datalen += 4;
/* Update Subscript instruction count */
state->sub->inscount++;
state->state = STATE_INIF;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SUB definition at line %d of %s\n", keyword, lineno, script);
}
}
break;
default:
ast_log(LOG_WARNING, "Can't process keyword '%s' in weird state %d\n", keyword, state->state);
}
return 0;
}
static struct adsi_script *compile_script(char *script)
{
FILE *f;
char fn[256];
char buf[256];
char *c;
int lineno=0;
int x, err;
struct adsi_script *scr;
if (script[0] == '/')
ast_copy_string(fn, script, sizeof(fn));
snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, script);
f = fopen(fn, "r");
if (!f) {
ast_log(LOG_WARNING, "Can't open file '%s'\n", fn);
return NULL;
}
if (!(scr = ast_calloc(1, sizeof(*scr)))) {
fclose(f);
return NULL;
}
/* Create "main" as first subroutine */
getsubbyname(scr, "main", NULL, 0);
while(!feof(f)) {
fgets(buf, sizeof(buf), f);
if (!feof(f)) {
lineno++;
/* Trim off trailing return */
buf[strlen(buf) - 1] = '\0';
c = strchr(buf, ';');
/* Strip comments */
if (c)
*c = '\0';
Mark Spencer
committed
if (!ast_strlen_zero(buf))
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
adsi_process(scr, buf, script, lineno);
}
}
fclose(f);
/* Make sure we're in the main routine again */
switch(scr->state) {
case STATE_NORMAL:
break;
case STATE_INSUB:
ast_log(LOG_WARNING, "Missing ENDSUB at end of file %s\n", script);
free(scr);
return NULL;
case STATE_INKEY:
ast_log(LOG_WARNING, "Missing ENDKEY at end of file %s\n", script);
free(scr);
return NULL;
}
err = 0;
/* Resolve all keys and record their lengths */
for (x=0;x<scr->numkeys;x++) {
if (!scr->keys[x].defined) {
ast_log(LOG_WARNING, "Key '%s' referenced but never defined in file %s\n", scr->keys[x].vname, fn);
err++;
}
}
/* Resolve all subs */
for (x=0;x<scr->numsubs;x++) {
if (!scr->subs[x].defined) {
ast_log(LOG_WARNING, "Subscript '%s' referenced but never defined in file %s\n", scr->subs[x].vname, fn);
err++;
}
if (x == (scr->numsubs - 1)) {
/* Clear out extension bit on last message */
scr->subs[x].data[2] = 0x80;
}
}
if (err) {
free(scr);
return NULL;
}
return scr;
}
#ifdef DUMP_MESSAGES
static void dump_message(char *type, char *vname, unsigned char *buf, int buflen)
{
int x;
printf("%s %s: [ ", type, vname);
for (x=0;x<buflen;x++)
printf("%02x ", buf[x]);
printf("]\n");
}
#endif
static int adsi_prog(struct ast_channel *chan, char *script)
{
struct adsi_script *scr;
int x;
unsigned char buf[1024];
int bytes;
scr = compile_script(script);
if (!scr)
return -1;
/* Start an empty ADSI Session */
if (ast_adsi_load_session(chan, NULL, 0, 1) < 1)
return -1;
/* Now begin the download attempt */
if (ast_adsi_begin_download(chan, scr->desc, scr->fdn, scr->sec, scr->ver)) {
/* User rejected us for some reason */
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "User rejected download attempt\n");
ast_log(LOG_NOTICE, "User rejected download on channel %s\n", chan->name);
free(scr);
return -1;
}
bytes = 0;
/* Start with key definitions */
for (x=0;x<scr->numkeys;x++) {
if (bytes + scr->keys[x].retstrlen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->keys[x].retstr, scr->keys[x].retstrlen);
bytes += scr->keys[x].retstrlen;
#ifdef DUMP_MESSAGES
dump_message("Key", scr->keys[x].vname, scr->keys[x].retstr, scr->keys[x].retstrlen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
/* Continue with the display messages */
for (x=0;x<scr->numdisplays;x++) {
if (bytes + scr->displays[x].datalen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->displays[x].data, scr->displays[x].datalen);
bytes += scr->displays[x].datalen;
#ifdef DUMP_MESSAGES
dump_message("Display", scr->displays[x].vname, scr->displays[x].data, scr->displays[x].datalen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
/* Send subroutines */
for (x=0;x<scr->numsubs;x++) {
if (bytes + scr->subs[x].datalen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->subs[x].data, scr->subs[x].datalen);
bytes += scr->subs[x].datalen;
#ifdef DUMP_MESSAGES
dump_message("Sub", scr->subs[x].vname, scr->subs[x].data, scr->subs[x].datalen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
bytes += ast_adsi_display(buf, ADSI_INFO_PAGE, 1, ADSI_JUST_LEFT, 0, "Download complete.", "");
bytes += ast_adsi_set_line(buf, ADSI_INFO_PAGE, 1);
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY) < 0)
if (ast_adsi_end_download(chan)) {
/* Download failed for some reason */
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Download attempt failed\n");
ast_log(LOG_NOTICE, "Download failed on %s\n", chan->name);
free(scr);
return -1;
}
free(scr);
ast_adsi_unload_session(chan);
return 0;
}
static int adsi_exec(struct ast_channel *chan, void *data)
{
int res=0;
struct ast_module_user *u;
Russell Bryant
committed
u = ast_module_user_add(chan);
Russell Bryant
committed
if (ast_strlen_zero(data))
Russell Bryant
committed
if (!ast_adsi_available(chan)) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "ADSI Unavailable on CPE. Not bothering to try.\n");
} else {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "ADSI Available on CPE. Attempting Upload.\n");
res = adsi_prog(chan, data);
}
Russell Bryant
committed
ast_module_user_remove(u);
Russell Bryant
committed
static int unload_module(void)
Russell Bryant
committed
int res;
ast_module_user_hangup_all();
Russell Bryant
committed
res = ast_unregister_application(app);
return res;
static int load_module(void)
{
return ast_register_application(app, adsi_exec, synopsis, descrip);
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk ADSI Programming Application");