func_presencestate.c 23 KB

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