| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "asterisk.h" |
| 30 |
|
| 31 |
#include "asterisk/module.h" |
| 32 |
#include "asterisk/channel.h" |
| 33 |
#include "asterisk/pbx.h" |
| 34 |
#include "asterisk/utils.h" |
| 35 |
#include "asterisk/linkedlists.h" |
| 36 |
#include "asterisk/presencestate.h" |
| 37 |
#include "asterisk/cli.h" |
| 38 |
#include "asterisk/astdb.h" |
| 39 |
#include "asterisk/app.h" |
| 40 |
#ifdef TEST_FRAMEWORK |
| 41 |
#include "asterisk/test.h" |
| 42 |
#include "asterisk/sem.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static const char astdb_family[] = "CustomPresence"; |
| 104 |
|
| 105 |
static int presence_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 106 |
{ |
| 107 |
int state; |
| 108 |
char *message = NULL; |
| 109 |
char *subtype = NULL; |
| 110 |
char *parse; |
| 111 |
int base64encode = 0; |
| 112 |
AST_DECLARE_APP_ARGS(args, |
| 113 |
AST_APP_ARG(provider); |
| 114 |
AST_APP_ARG(field); |
| 115 |
AST_APP_ARG(options); |
| 116 |
); |
| 117 |
|
| 118 |
if (ast_strlen_zero(data)) { |
| 119 |
ast_log(LOG_WARNING, "PRESENCE_STATE reading requires an argument \n"); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
parse = ast_strdupa(data); |
| 124 |
|
| 125 |
AST_STANDARD_APP_ARGS(args, parse); |
| 126 |
|
| 127 |
if (ast_strlen_zero(args.provider) || ast_strlen_zero(args.field)) { |
| 128 |
ast_log(LOG_WARNING, "PRESENCE_STATE reading requires both presence provider and presence field arguments. \n"); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
state = ast_presence_state_nocache(args.provider, &subtype, &message); |
| 133 |
if (state == AST_PRESENCE_INVALID) { |
| 134 |
ast_log(LOG_WARNING, "PRESENCE_STATE unknown \n"); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
if (!(ast_strlen_zero(args.options)) && (strchr(args.options, 'e'))) { |
| 139 |
base64encode = 1; |
| 140 |
} |
| 141 |
|
| 142 |
if (!ast_strlen_zero(subtype) && !strcasecmp(args.field, "subtype")) { |
| 143 |
if (base64encode) { |
| 144 |
ast_base64encode(buf, (unsigned char *) subtype, strlen(subtype), len); |
| 145 |
} else { |
| 146 |
ast_copy_string(buf, subtype, len); |
| 147 |
} |
| 148 |
} else if (!ast_strlen_zero(message) && !strcasecmp(args.field, "message")) { |
| 149 |
if (base64encode) { |
| 150 |
ast_base64encode(buf, (unsigned char *) message, strlen(message), len); |
| 151 |
} else { |
| 152 |
ast_copy_string(buf, message, len); |
| 153 |
} |
| 154 |
|
| 155 |
} else if (!strcasecmp(args.field, "value")) { |
| 156 |
ast_copy_string(buf, ast_presence_state2str(state), len); |
| 157 |
} |
| 158 |
|
| 159 |
ast_free(message); |
| 160 |
ast_free(subtype); |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static int parse_data(char *data, enum ast_presence_state *state, char **subtype, char **message, char **options) |
| 166 |
{ |
| 167 |
char *state_str; |
| 168 |
|
| 169 |
|
| 170 |
*subtype = ""; |
| 171 |
*message = ""; |
| 172 |
*options = ""; |
| 173 |
|
| 174 |
state_str = strsep(&data, ","); |
| 175 |
if (ast_strlen_zero(state_str)) { |
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
*state = ast_presence_state_val(state_str); |
| 180 |
|
| 181 |
|
| 182 |
if (*state == AST_PRESENCE_INVALID) { |
| 183 |
ast_log(LOG_WARNING, "Unknown presence state value %s\n", state_str); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
if (!(*subtype = strsep(&data,","))) { |
| 188 |
*subtype = ""; |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
if (!(*message = strsep(&data, ","))) { |
| 193 |
*message = ""; |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
if (!(*options = strsep(&data, ","))) { |
| 198 |
*options = ""; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
if (!ast_strlen_zero(*options) && !(strchr(*options, 'e'))) { |
| 203 |
ast_log(LOG_NOTICE, "Invalid options '%s'\n", *options); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
static int presence_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 211 |
{ |
| 212 |
size_t len = strlen("CustomPresence:"); |
| 213 |
char *tmp = data; |
| 214 |
char *args = ast_strdupa(value); |
| 215 |
enum ast_presence_state state; |
| 216 |
char *options, *message, *subtype; |
| 217 |
|
| 218 |
if (strncasecmp(data, "CustomPresence:", len)) { |
| 219 |
ast_log(LOG_WARNING, "The PRESENCE_STATE function can only set CustomPresence: presence providers.\n"); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
data += len; |
| 223 |
if (ast_strlen_zero(data)) { |
| 224 |
ast_log(LOG_WARNING, "PRESENCE_STATE function called with no custom device name!\n"); |
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
if (parse_data(args, &state, &subtype, &message, &options)) { |
| 229 |
ast_log(LOG_WARNING, "Invalid arguments to PRESENCE_STATE\n"); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
ast_db_put(astdb_family, data, value); |
| 234 |
|
| 235 |
if (strchr(options, 'e')) { |
| 236 |
|
| 237 |
char decoded_subtype[256] = { 0, }; |
| 238 |
char decoded_message[256] = { 0, }; |
| 239 |
|
| 240 |
ast_base64decode((unsigned char *) decoded_subtype, subtype, sizeof(decoded_subtype) -1); |
| 241 |
ast_base64decode((unsigned char *) decoded_message, message, sizeof(decoded_message) -1); |
| 242 |
|
| 243 |
ast_presence_state_changed_literal(state, decoded_subtype, decoded_message, tmp); |
| 244 |
} else { |
| 245 |
ast_presence_state_changed_literal(state, subtype, message, tmp); |
| 246 |
} |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
static enum ast_presence_state custom_presence_callback(const char *data, char **subtype, char **message) |
| 252 |
{ |
| 253 |
char buf[1301] = ""; |
| 254 |
enum ast_presence_state state; |
| 255 |
char *_options; |
| 256 |
char *_message; |
| 257 |
char *_subtype; |
| 258 |
|
| 259 |
if (ast_db_get(astdb_family, data, buf, sizeof(buf))) { |
| 260 |
return AST_PRESENCE_NOT_SET; |
| 261 |
} |
| 262 |
|
| 263 |
if (parse_data(buf, &state, &_subtype, &_message, &_options)) { |
| 264 |
return AST_PRESENCE_INVALID; |
| 265 |
} |
| 266 |
|
| 267 |
if ((strchr(_options, 'e'))) { |
| 268 |
char tmp[1301]; |
| 269 |
|
| 270 |
if (ast_strlen_zero(_subtype)) { |
| 271 |
*subtype = NULL; |
| 272 |
} else { |
| 273 |
memset(tmp, 0, sizeof(tmp)); |
| 274 |
ast_base64decode((unsigned char *) tmp, _subtype, sizeof(tmp) - 1); |
| 275 |
*subtype = ast_strdup(tmp); |
| 276 |
} |
| 277 |
|
| 278 |
if (ast_strlen_zero(_message)) { |
| 279 |
*message = NULL; |
| 280 |
} else { |
| 281 |
memset(tmp, 0, sizeof(tmp)); |
| 282 |
ast_base64decode((unsigned char *) tmp, _message, sizeof(tmp) - 1); |
| 283 |
*message = ast_strdup(tmp); |
| 284 |
} |
| 285 |
} else { |
| 286 |
*subtype = ast_strlen_zero(_subtype) ? NULL : ast_strdup(_subtype); |
| 287 |
*message = ast_strlen_zero(_message) ? NULL : ast_strdup(_message); |
| 288 |
} |
| 289 |
return state; |
| 290 |
} |
| 291 |
|
| 292 |
static struct ast_custom_function presence_function = { |
| 293 |
.name = "PRESENCE_STATE", |
| 294 |
.read = presence_read, |
| 295 |
.write = presence_write, |
| 296 |
}; |
| 297 |
|
| 298 |
static char *handle_cli_presencestate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 299 |
{ |
| 300 |
struct ast_db_entry *db_entry, *db_tree; |
| 301 |
|
| 302 |
switch (cmd) { |
| 303 |
case CLI_INIT: |
| 304 |
e->command = "presencestate list"; |
| 305 |
e->usage = |
| 306 |
"Usage: presencestate list\n" |
| 307 |
" List all custom presence states that have been set by using\n" |
| 308 |
" the PRESENCE_STATE dialplan function.\n"; |
| 309 |
return NULL; |
| 310 |
case CLI_GENERATE: |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
|
| 314 |
if (a->argc != e->args) { |
| 315 |
return CLI_SHOWUSAGE; |
| 316 |
} |
| 317 |
|
| 318 |
ast_cli(a->fd, "\n" |
| 319 |
"---------------------------------------------------------------------\n" |
| 320 |
"--- Custom Presence States ------------------------------------------\n" |
| 321 |
"---------------------------------------------------------------------\n" |
| 322 |
"---\n"); |
| 323 |
|
| 324 |
db_entry = db_tree = ast_db_gettree(astdb_family, NULL); |
| 325 |
if (!db_entry) { |
| 326 |
ast_cli(a->fd, "No custom presence states defined\n"); |
| 327 |
return CLI_SUCCESS; |
| 328 |
} |
| 329 |
for (; db_entry; db_entry = db_entry->next) { |
| 330 |
const char *object_name = strrchr(db_entry->key, '/') + 1; |
| 331 |
char state_info[1301]; |
| 332 |
enum ast_presence_state state; |
| 333 |
char *subtype; |
| 334 |
char *message; |
| 335 |
char *options; |
| 336 |
|
| 337 |
ast_copy_string(state_info, db_entry->data, sizeof(state_info)); |
| 338 |
if (parse_data(state_info, &state, &subtype, &message, &options)) { |
| 339 |
ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data); |
| 340 |
continue; |
| 341 |
} |
| 342 |
|
| 343 |
if (object_name <= (const char *) 1) { |
| 344 |
continue; |
| 345 |
} |
| 346 |
ast_cli(a->fd, "--- Name: 'CustomPresence:%s'\n" |
| 347 |
" --- State: '%s'\n" |
| 348 |
" --- Subtype: '%s'\n" |
| 349 |
" --- Message: '%s'\n" |
| 350 |
" --- Base64 Encoded: '%s'\n" |
| 351 |
"---\n", |
| 352 |
object_name, |
| 353 |
ast_presence_state2str(state), |
| 354 |
subtype, |
| 355 |
message, |
| 356 |
AST_CLI_YESNO(strchr(options, 'e'))); |
| 357 |
} |
| 358 |
ast_db_freetree(db_tree); |
| 359 |
db_tree = NULL; |
| 360 |
|
| 361 |
ast_cli(a->fd, |
| 362 |
"---------------------------------------------------------------------\n" |
| 363 |
"---------------------------------------------------------------------\n" |
| 364 |
"\n"); |
| 365 |
|
| 366 |
return CLI_SUCCESS; |
| 367 |
} |
| 368 |
|
| 369 |
static char *handle_cli_presencestate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 370 |
{ |
| 371 |
size_t len; |
| 372 |
const char *dev, *state, *full_dev; |
| 373 |
enum ast_presence_state state_val; |
| 374 |
char *message; |
| 375 |
char *subtype; |
| 376 |
char *options; |
| 377 |
char *args; |
| 378 |
|
| 379 |
switch (cmd) { |
| 380 |
case CLI_INIT: |
| 381 |
e->command = "presencestate change"; |
| 382 |
e->usage = |
| 383 |
"Usage: presencestate change <entity> <state>[,<subtype>[,message[,options]]]\n" |
| 384 |
" Change a custom presence to a new state.\n" |
| 385 |
" The possible values for the state are:\n" |
| 386 |
"NOT_SET | UNAVAILABLE | AVAILABLE | AWAY | XA | CHAT | DND\n" |
| 387 |
"Optionally, a custom subtype and message may be provided, along with any options\n" |
| 388 |
"accepted by func_presencestate. If the subtype or message provided contain spaces,\n" |
| 389 |
"be sure to enclose the data in quotation marks (\"\")\n" |
| 390 |
"\n" |
| 391 |
"Examples:\n" |
| 392 |
" presencestate change CustomPresence:mystate1 AWAY\n" |
| 393 |
" presencestate change CustomPresence:mystate1 AVAILABLE\n" |
| 394 |
" presencestate change CustomPresence:mystate1 \"Away,upstairs,eating lunch\"\n" |
| 395 |
" \n"; |
| 396 |
return NULL; |
| 397 |
case CLI_GENERATE: |
| 398 |
{ |
| 399 |
static const char * const cmds[] = { "NOT_SET", "UNAVAILABLE", "AVAILABLE", "AWAY", |
| 400 |
"XA", "CHAT", "DND", NULL }; |
| 401 |
|
| 402 |
if (a->pos == e->args + 1) { |
| 403 |
return ast_cli_complete(a->word, cmds, a->n); |
| 404 |
} |
| 405 |
|
| 406 |
return NULL; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if (a->argc != e->args + 2) { |
| 411 |
return CLI_SHOWUSAGE; |
| 412 |
} |
| 413 |
|
| 414 |
len = strlen("CustomPresence:"); |
| 415 |
full_dev = dev = a->argv[e->args]; |
| 416 |
state = a->argv[e->args + 1]; |
| 417 |
|
| 418 |
if (strncasecmp(dev, "CustomPresence:", len)) { |
| 419 |
ast_cli(a->fd, "The presencestate command can only be used to set 'CustomPresence:' presence state!\n"); |
| 420 |
return CLI_FAILURE; |
| 421 |
} |
| 422 |
|
| 423 |
dev += len; |
| 424 |
if (ast_strlen_zero(dev)) { |
| 425 |
return CLI_SHOWUSAGE; |
| 426 |
} |
| 427 |
|
| 428 |
args = ast_strdupa(state); |
| 429 |
if (parse_data(args, &state_val, &subtype, &message, &options)) { |
| 430 |
return CLI_SHOWUSAGE; |
| 431 |
} |
| 432 |
|
| 433 |
ast_cli(a->fd, "Changing %s to %s\n", dev, args); |
| 434 |
|
| 435 |
ast_db_put(astdb_family, dev, state); |
| 436 |
|
| 437 |
ast_presence_state_changed_literal(state_val, subtype, message, full_dev); |
| 438 |
|
| 439 |
return CLI_SUCCESS; |
| 440 |
} |
| 441 |
|
| 442 |
static struct ast_cli_entry cli_funcpresencestate[] = { |
| 443 |
AST_CLI_DEFINE(handle_cli_presencestate_list, "List currently know custom presence states"), |
| 444 |
AST_CLI_DEFINE(handle_cli_presencestate_change, "Change a custom presence state"), |
| 445 |
}; |
| 446 |
|
| 447 |
#ifdef TEST_FRAMEWORK |
| 448 |
|
| 449 |
struct test_string { |
| 450 |
char *parse_string; |
| 451 |
struct { |
| 452 |
int value; |
| 453 |
const char *subtype; |
| 454 |
const char *message; |
| 455 |
const char *options; |
| 456 |
} outputs; |
| 457 |
}; |
| 458 |
|
| 459 |
AST_TEST_DEFINE(test_valid_parse_data) |
| 460 |
{ |
| 461 |
int i; |
| 462 |
enum ast_presence_state state; |
| 463 |
char *subtype; |
| 464 |
char *message; |
| 465 |
char *options; |
| 466 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 467 |
|
| 468 |
struct test_string tests [] = { |
| 469 |
{ "away", |
| 470 |
{ AST_PRESENCE_AWAY, |
| 471 |
"", |
| 472 |
"", |
| 473 |
"" |
| 474 |
} |
| 475 |
}, |
| 476 |
{ "not_set", |
| 477 |
{ AST_PRESENCE_NOT_SET, |
| 478 |
"", |
| 479 |
"", |
| 480 |
"" |
| 481 |
} |
| 482 |
}, |
| 483 |
{ "unavailable", |
| 484 |
{ AST_PRESENCE_UNAVAILABLE, |
| 485 |
"", |
| 486 |
"", |
| 487 |
"" |
| 488 |
} |
| 489 |
}, |
| 490 |
{ "available", |
| 491 |
{ AST_PRESENCE_AVAILABLE, |
| 492 |
"", |
| 493 |
"", |
| 494 |
"" |
| 495 |
} |
| 496 |
}, |
| 497 |
{ "xa", |
| 498 |
{ AST_PRESENCE_XA, |
| 499 |
"", |
| 500 |
"", |
| 501 |
"" |
| 502 |
} |
| 503 |
}, |
| 504 |
{ "chat", |
| 505 |
{ AST_PRESENCE_CHAT, |
| 506 |
"", |
| 507 |
"", |
| 508 |
"" |
| 509 |
} |
| 510 |
}, |
| 511 |
{ "dnd", |
| 512 |
{ AST_PRESENCE_DND, |
| 513 |
"", |
| 514 |
"", |
| 515 |
"" |
| 516 |
} |
| 517 |
}, |
| 518 |
{ "away,down the hall", |
| 519 |
{ AST_PRESENCE_AWAY, |
| 520 |
"down the hall", |
| 521 |
"", |
| 522 |
"" |
| 523 |
} |
| 524 |
}, |
| 525 |
{ "away,down the hall,Quarterly financial meeting", |
| 526 |
{ AST_PRESENCE_AWAY, |
| 527 |
"down the hall", |
| 528 |
"Quarterly financial meeting", |
| 529 |
"" |
| 530 |
} |
| 531 |
}, |
| 532 |
{ "away,,Quarterly financial meeting", |
| 533 |
{ AST_PRESENCE_AWAY, |
| 534 |
"", |
| 535 |
"Quarterly financial meeting", |
| 536 |
"" |
| 537 |
} |
| 538 |
}, |
| 539 |
{ "away,,,e", |
| 540 |
{ AST_PRESENCE_AWAY, |
| 541 |
"", |
| 542 |
"", |
| 543 |
"e", |
| 544 |
} |
| 545 |
}, |
| 546 |
{ "away,down the hall,,e", |
| 547 |
{ AST_PRESENCE_AWAY, |
| 548 |
"down the hall", |
| 549 |
"", |
| 550 |
"e" |
| 551 |
} |
| 552 |
}, |
| 553 |
{ "away,down the hall,Quarterly financial meeting,e", |
| 554 |
{ AST_PRESENCE_AWAY, |
| 555 |
"down the hall", |
| 556 |
"Quarterly financial meeting", |
| 557 |
"e" |
| 558 |
} |
| 559 |
}, |
| 560 |
{ "away,,Quarterly financial meeting,e", |
| 561 |
{ AST_PRESENCE_AWAY, |
| 562 |
"", |
| 563 |
"Quarterly financial meeting", |
| 564 |
"e" |
| 565 |
} |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
switch (cmd) { |
| 570 |
case TEST_INIT: |
| 571 |
info->name = "parse_valid_presence_data"; |
| 572 |
info->category = "/funcs/func_presence/"; |
| 573 |
info->summary = "PRESENCESTATE parsing test"; |
| 574 |
info->description = |
| 575 |
"Ensure that parsing function accepts proper values, and gives proper outputs"; |
| 576 |
return AST_TEST_NOT_RUN; |
| 577 |
case TEST_EXECUTE: |
| 578 |
break; |
| 579 |
} |
| 580 |
|
| 581 |
for (i = 0; i < ARRAY_LEN(tests); ++i) { |
| 582 |
int parse_result; |
| 583 |
char *parse_string = ast_strdup(tests[i].parse_string); |
| 584 |
if (!parse_string) { |
| 585 |
res = AST_TEST_FAIL; |
| 586 |
break; |
| 587 |
} |
| 588 |
parse_result = parse_data(parse_string, &state, &subtype, &message, &options); |
| 589 |
if (parse_result == -1) { |
| 590 |
res = AST_TEST_FAIL; |
| 591 |
ast_free(parse_string); |
| 592 |
break; |
| 593 |
} |
| 594 |
if (tests[i].outputs.value != state || |
| 595 |
strcmp(tests[i].outputs.subtype, subtype) || |
| 596 |
strcmp(tests[i].outputs.message, message) || |
| 597 |
strcmp(tests[i].outputs.options, options)) { |
| 598 |
res = AST_TEST_FAIL; |
| 599 |
ast_free(parse_string); |
| 600 |
break; |
| 601 |
} |
| 602 |
ast_free(parse_string); |
| 603 |
} |
| 604 |
|
| 605 |
return res; |
| 606 |
} |
| 607 |
|
| 608 |
AST_TEST_DEFINE(test_invalid_parse_data) |
| 609 |
{ |
| 610 |
int i; |
| 611 |
enum ast_presence_state state; |
| 612 |
char *subtype; |
| 613 |
char *message; |
| 614 |
char *options; |
| 615 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 616 |
|
| 617 |
char *tests[] = { |
| 618 |
"", |
| 619 |
"bored", |
| 620 |
"away,,,i", |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
}; |
| 626 |
|
| 627 |
switch (cmd) { |
| 628 |
case TEST_INIT: |
| 629 |
info->name = "parse_invalid_presence_data"; |
| 630 |
info->category = "/funcs/func_presence/"; |
| 631 |
info->summary = "PRESENCESTATE parsing test"; |
| 632 |
info->description = |
| 633 |
"Ensure that parsing function rejects improper values"; |
| 634 |
return AST_TEST_NOT_RUN; |
| 635 |
case TEST_EXECUTE: |
| 636 |
break; |
| 637 |
} |
| 638 |
|
| 639 |
for (i = 0; i < ARRAY_LEN(tests); ++i) { |
| 640 |
int parse_result; |
| 641 |
char *parse_string = ast_strdup(tests[i]); |
| 642 |
if (!parse_string) { |
| 643 |
res = AST_TEST_FAIL; |
| 644 |
break; |
| 645 |
} |
| 646 |
parse_result = parse_data(parse_string, &state, &subtype, &message, &options); |
| 647 |
if (parse_result == 0) { |
| 648 |
ast_log(LOG_WARNING, "Invalid string parsing failed on %s\n", tests[i]); |
| 649 |
res = AST_TEST_FAIL; |
| 650 |
ast_free(parse_string); |
| 651 |
break; |
| 652 |
} |
| 653 |
ast_free(parse_string); |
| 654 |
} |
| 655 |
|
| 656 |
return res; |
| 657 |
} |
| 658 |
|
| 659 |
#define PRES_STATE "away" |
| 660 |
#define PRES_SUBTYPE "down the hall" |
| 661 |
#define PRES_MESSAGE "Quarterly financial meeting" |
| 662 |
|
| 663 |
struct test_cb_data { |
| 664 |
struct ast_presence_state_message *presence_state; |
| 665 |
|
| 666 |
struct ast_sem sem; |
| 667 |
}; |
| 668 |
|
| 669 |
static struct test_cb_data *test_cb_data_alloc(void) |
| 670 |
{ |
| 671 |
struct test_cb_data *cb_data = ast_calloc(1, sizeof(*cb_data)); |
| 672 |
|
| 673 |
if (!cb_data) { |
| 674 |
return NULL; |
| 675 |
} |
| 676 |
|
| 677 |
if (ast_sem_init(&cb_data->sem, 0, 0)) { |
| 678 |
ast_free(cb_data); |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
return cb_data; |
| 683 |
} |
| 684 |
|
| 685 |
static void test_cb_data_destroy(struct test_cb_data *cb_data) |
| 686 |
{ |
| 687 |
ao2_cleanup(cb_data->presence_state); |
| 688 |
ast_sem_destroy(&cb_data->sem); |
| 689 |
ast_free(cb_data); |
| 690 |
} |
| 691 |
|
| 692 |
static void test_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg) |
| 693 |
{ |
| 694 |
struct test_cb_data *cb_data = userdata; |
| 695 |
if (stasis_message_type(msg) != ast_presence_state_message_type()) { |
| 696 |
return; |
| 697 |
} |
| 698 |
cb_data->presence_state = stasis_message_data(msg); |
| 699 |
ao2_ref(cb_data->presence_state, +1); |
| 700 |
|
| 701 |
ast_sem_post(&cb_data->sem); |
| 702 |
} |
| 703 |
|
| 704 |
static enum ast_test_result_state presence_change_common(struct ast_test *test, |
| 705 |
const char *state, const char *subtype, const char *message, const char *options, |
| 706 |
char *out_state, size_t out_state_size, |
| 707 |
char *out_subtype, size_t out_subtype_size, |
| 708 |
char *out_message, size_t out_message_size) |
| 709 |
{ |
| 710 |
RAII_VAR(struct test_cb_data *, cb_data, test_cb_data_alloc(), test_cb_data_destroy); |
| 711 |
struct stasis_subscription *test_sub; |
| 712 |
char pres[1301]; |
| 713 |
|
| 714 |
if (!(test_sub = stasis_subscribe(ast_presence_state_topic_all(), test_cb, cb_data))) { |
| 715 |
return AST_TEST_FAIL; |
| 716 |
} |
| 717 |
|
| 718 |
if (ast_strlen_zero(options)) { |
| 719 |
snprintf(pres, sizeof(pres), "%s,%s,%s", state, subtype, message); |
| 720 |
} else { |
| 721 |
snprintf(pres, sizeof(pres), "%s,%s,%s,%s", state, subtype, message, options); |
| 722 |
} |
| 723 |
|
| 724 |
if (presence_write(NULL, "PRESENCESTATE", "CustomPresence:TestPresenceStateChange", pres)) { |
| 725 |
test_sub = stasis_unsubscribe_and_join(test_sub); |
| 726 |
return AST_TEST_FAIL; |
| 727 |
} |
| 728 |
|
| 729 |
ast_sem_wait(&cb_data->sem); |
| 730 |
|
| 731 |
ast_copy_string(out_state, ast_presence_state2str(cb_data->presence_state->state), out_state_size); |
| 732 |
ast_copy_string(out_subtype, cb_data->presence_state->subtype, out_subtype_size); |
| 733 |
ast_copy_string(out_message, cb_data->presence_state->message, out_message_size); |
| 734 |
|
| 735 |
test_sub = stasis_unsubscribe_and_join(test_sub); |
| 736 |
ast_db_del("CustomPresence", "TestPresenceStateChange"); |
| 737 |
|
| 738 |
return AST_TEST_PASS; |
| 739 |
} |
| 740 |
|
| 741 |
AST_TEST_DEFINE(test_presence_state_change) |
| 742 |
{ |
| 743 |
char out_state[32]; |
| 744 |
char out_subtype[32]; |
| 745 |
char out_message[32]; |
| 746 |
|
| 747 |
switch (cmd) { |
| 748 |
case TEST_INIT: |
| 749 |
info->name = "test_presence_state_change"; |
| 750 |
info->category = "/funcs/func_presence/"; |
| 751 |
info->summary = "presence state change subscription"; |
| 752 |
info->description = |
| 753 |
"Ensure that presence state changes are communicated to subscribers"; |
| 754 |
return AST_TEST_NOT_RUN; |
| 755 |
case TEST_EXECUTE: |
| 756 |
break; |
| 757 |
} |
| 758 |
|
| 759 |
if (presence_change_common(test, PRES_STATE, PRES_SUBTYPE, PRES_MESSAGE, NULL, |
| 760 |
out_state, sizeof(out_state), |
| 761 |
out_subtype, sizeof(out_subtype), |
| 762 |
out_message, sizeof(out_message)) == AST_TEST_FAIL) { |
| 763 |
return AST_TEST_FAIL; |
| 764 |
} |
| 765 |
|
| 766 |
if (strcmp(out_state, PRES_STATE) || |
| 767 |
strcmp(out_subtype, PRES_SUBTYPE) || |
| 768 |
strcmp(out_message, PRES_MESSAGE)) { |
| 769 |
ast_test_status_update(test, "Unexpected presence values, %s != %s, %s != %s, or %s != %s\n", |
| 770 |
PRES_STATE, out_state, |
| 771 |
PRES_SUBTYPE, out_subtype, |
| 772 |
PRES_MESSAGE, out_message); |
| 773 |
return AST_TEST_FAIL; |
| 774 |
} |
| 775 |
|
| 776 |
return AST_TEST_PASS; |
| 777 |
} |
| 778 |
|
| 779 |
AST_TEST_DEFINE(test_presence_state_base64_encode) |
| 780 |
{ |
| 781 |
char out_state[32]; |
| 782 |
char out_subtype[32]; |
| 783 |
char out_message[32]; |
| 784 |
char encoded_subtype[64]; |
| 785 |
char encoded_message[64]; |
| 786 |
|
| 787 |
switch (cmd) { |
| 788 |
case TEST_INIT: |
| 789 |
info->name = "test_presence_state_base64_encode"; |
| 790 |
info->category = "/funcs/func_presence/"; |
| 791 |
info->summary = "presence state base64 encoding"; |
| 792 |
info->description = |
| 793 |
"Ensure that base64-encoded presence state is stored base64-encoded but\n" |
| 794 |
"is presented to consumers decoded."; |
| 795 |
return AST_TEST_NOT_RUN; |
| 796 |
case TEST_EXECUTE: |
| 797 |
break; |
| 798 |
} |
| 799 |
|
| 800 |
ast_base64encode(encoded_subtype, (unsigned char *) PRES_SUBTYPE, strlen(PRES_SUBTYPE), sizeof(encoded_subtype) - 1); |
| 801 |
ast_base64encode(encoded_message, (unsigned char *) PRES_MESSAGE, strlen(PRES_MESSAGE), sizeof(encoded_message) - 1); |
| 802 |
|
| 803 |
if (presence_change_common(test, PRES_STATE, encoded_subtype, encoded_message, "e", |
| 804 |
out_state, sizeof(out_state), |
| 805 |
out_subtype, sizeof(out_subtype), |
| 806 |
out_message, sizeof(out_message)) == AST_TEST_FAIL) { |
| 807 |
return AST_TEST_FAIL; |
| 808 |
} |
| 809 |
|
| 810 |
if (strcmp(out_state, PRES_STATE) || |
| 811 |
strcmp(out_subtype, PRES_SUBTYPE) || |
| 812 |
strcmp(out_message, PRES_MESSAGE)) { |
| 813 |
ast_test_status_update(test, "Unexpected presence values, %s != %s, %s != %s, or %s != %s\n", |
| 814 |
PRES_STATE, out_state, |
| 815 |
PRES_SUBTYPE, out_subtype, |
| 816 |
PRES_MESSAGE, out_message); |
| 817 |
return AST_TEST_FAIL; |
| 818 |
} |
| 819 |
|
| 820 |
return AST_TEST_PASS; |
| 821 |
} |
| 822 |
|
| 823 |
#endif |
| 824 |
|
| 825 |
static int unload_module(void) |
| 826 |
{ |
| 827 |
int res = 0; |
| 828 |
|
| 829 |
res |= ast_custom_function_unregister(&presence_function); |
| 830 |
res |= ast_presence_state_prov_del("CustomPresence"); |
| 831 |
res |= ast_cli_unregister_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate)); |
| 832 |
#ifdef TEST_FRAMEWORK |
| 833 |
AST_TEST_UNREGISTER(test_valid_parse_data); |
| 834 |
AST_TEST_UNREGISTER(test_invalid_parse_data); |
| 835 |
AST_TEST_UNREGISTER(test_presence_state_change); |
| 836 |
AST_TEST_UNREGISTER(test_presence_state_base64_encode); |
| 837 |
#endif |
| 838 |
return res; |
| 839 |
} |
| 840 |
|
| 841 |
static int load_module(void) |
| 842 |
{ |
| 843 |
int res = 0; |
| 844 |
struct ast_db_entry *db_entry, *db_tree; |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
db_entry = db_tree = ast_db_gettree(astdb_family, NULL); |
| 849 |
for (; db_entry; db_entry = db_entry->next) { |
| 850 |
const char *dev_name = strrchr(db_entry->key, '/') + 1; |
| 851 |
enum ast_presence_state state; |
| 852 |
char *message = NULL; |
| 853 |
char *subtype = NULL; |
| 854 |
if (dev_name <= (const char *) 1) { |
| 855 |
continue; |
| 856 |
} |
| 857 |
state = custom_presence_callback(dev_name, &subtype, &message); |
| 858 |
ast_presence_state_changed(state, subtype, message, "CustomPresence:%s", dev_name); |
| 859 |
ast_free(subtype); |
| 860 |
ast_free(message); |
| 861 |
} |
| 862 |
ast_db_freetree(db_tree); |
| 863 |
db_tree = NULL; |
| 864 |
|
| 865 |
res |= ast_custom_function_register(&presence_function); |
| 866 |
res |= ast_presence_state_prov_add("CustomPresence", custom_presence_callback); |
| 867 |
res |= ast_cli_register_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate)); |
| 868 |
#ifdef TEST_FRAMEWORK |
| 869 |
AST_TEST_REGISTER(test_valid_parse_data); |
| 870 |
AST_TEST_REGISTER(test_invalid_parse_data); |
| 871 |
AST_TEST_REGISTER(test_presence_state_change); |
| 872 |
AST_TEST_REGISTER(test_presence_state_base64_encode); |
| 873 |
#endif |
| 874 |
|
| 875 |
return res; |
| 876 |
} |
| 877 |
|
| 878 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a presence state in the dialplan", |
| 879 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 880 |
.load = load_module, |
| 881 |
.unload = unload_module, |
| 882 |
.load_pri = AST_MODPRI_DEVSTATE_PROVIDER, |
| 883 |
); |