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 <semaphore.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. ast_db_get(astdb_family, data, buf, sizeof(buf));
  220. if (parse_data(buf, &state, &_subtype, &_message, &_options)) {
  221. return AST_PRESENCE_INVALID;
  222. }
  223. if ((strchr(_options, 'e'))) {
  224. char tmp[1301];
  225. if (ast_strlen_zero(_subtype)) {
  226. *subtype = NULL;
  227. } else {
  228. memset(tmp, 0, sizeof(tmp));
  229. ast_base64decode((unsigned char *) tmp, _subtype, sizeof(tmp) - 1);
  230. *subtype = ast_strdup(tmp);
  231. }
  232. if (ast_strlen_zero(_message)) {
  233. *message = NULL;
  234. } else {
  235. memset(tmp, 0, sizeof(tmp));
  236. ast_base64decode((unsigned char *) tmp, _message, sizeof(tmp) - 1);
  237. *message = ast_strdup(tmp);
  238. }
  239. } else {
  240. *subtype = ast_strlen_zero(_subtype) ? NULL : ast_strdup(_subtype);
  241. *message = ast_strlen_zero(_message) ? NULL : ast_strdup(_message);
  242. }
  243. return state;
  244. }
  245. static struct ast_custom_function presence_function = {
  246. .name = "PRESENCE_STATE",
  247. .read = presence_read,
  248. .write = presence_write,
  249. };
  250. static char *handle_cli_presencestate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  251. {
  252. struct ast_db_entry *db_entry, *db_tree;
  253. switch (cmd) {
  254. case CLI_INIT:
  255. e->command = "presencestate list";
  256. e->usage =
  257. "Usage: presencestate list\n"
  258. " List all custom presence states that have been set by using\n"
  259. " the PRESENCE_STATE dialplan function.\n";
  260. return NULL;
  261. case CLI_GENERATE:
  262. return NULL;
  263. }
  264. if (a->argc != e->args) {
  265. return CLI_SHOWUSAGE;
  266. }
  267. ast_cli(a->fd, "\n"
  268. "---------------------------------------------------------------------\n"
  269. "--- Custom Presence States ------------------------------------------\n"
  270. "---------------------------------------------------------------------\n"
  271. "---\n");
  272. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  273. if (!db_entry) {
  274. ast_cli(a->fd, "No custom presence states defined\n");
  275. return CLI_SUCCESS;
  276. }
  277. for (; db_entry; db_entry = db_entry->next) {
  278. const char *object_name = strrchr(db_entry->key, '/') + 1;
  279. char state_info[1301];
  280. enum ast_presence_state state;
  281. char *subtype;
  282. char *message;
  283. char *options;
  284. ast_copy_string(state_info, db_entry->data, sizeof(state_info));
  285. if (parse_data(state_info, &state, &subtype, &message, &options)) {
  286. ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data);
  287. continue;
  288. }
  289. if (object_name <= (const char *) 1) {
  290. continue;
  291. }
  292. ast_cli(a->fd, "--- Name: 'CustomPresence:%s'\n"
  293. " --- State: '%s'\n"
  294. " --- Subtype: '%s'\n"
  295. " --- Message: '%s'\n"
  296. " --- Base64 Encoded: '%s'\n"
  297. "---\n",
  298. object_name,
  299. ast_presence_state2str(state),
  300. subtype,
  301. message,
  302. AST_CLI_YESNO(strchr(options, 'e')));
  303. }
  304. ast_db_freetree(db_tree);
  305. db_tree = NULL;
  306. ast_cli(a->fd,
  307. "---------------------------------------------------------------------\n"
  308. "---------------------------------------------------------------------\n"
  309. "\n");
  310. return CLI_SUCCESS;
  311. }
  312. static char *handle_cli_presencestate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  313. {
  314. size_t len;
  315. const char *dev, *state, *full_dev;
  316. enum ast_presence_state state_val;
  317. char *message;
  318. char *subtype;
  319. char *options;
  320. char *args;
  321. switch (cmd) {
  322. case CLI_INIT:
  323. e->command = "presencestate change";
  324. e->usage =
  325. "Usage: presencestate change <entity> <state>[,<subtype>[,message[,options]]]\n"
  326. " Change a custom presence to a new state.\n"
  327. " The possible values for the state are:\n"
  328. "NOT_SET | UNAVAILABLE | AVAILABLE | AWAY | XA | CHAT | DND\n"
  329. "Optionally, a custom subtype and message may be provided, along with any options\n"
  330. "accepted by func_presencestate. If the subtype or message provided contain spaces,\n"
  331. "be sure to enclose the data in quotation marks (\"\")\n"
  332. "\n"
  333. "Examples:\n"
  334. " presencestate change CustomPresence:mystate1 AWAY\n"
  335. " presencestate change CustomPresence:mystate1 AVAILABLE\n"
  336. " presencestate change CustomPresence:mystate1 \"Away,upstairs,eating lunch\"\n"
  337. " \n";
  338. return NULL;
  339. case CLI_GENERATE:
  340. {
  341. static const char * const cmds[] = { "NOT_SET", "UNAVAILABLE", "AVAILABLE", "AWAY",
  342. "XA", "CHAT", "DND", NULL };
  343. if (a->pos == e->args + 1) {
  344. return ast_cli_complete(a->word, cmds, a->n);
  345. }
  346. return NULL;
  347. }
  348. }
  349. if (a->argc != e->args + 2) {
  350. return CLI_SHOWUSAGE;
  351. }
  352. len = strlen("CustomPresence:");
  353. full_dev = dev = a->argv[e->args];
  354. state = a->argv[e->args + 1];
  355. if (strncasecmp(dev, "CustomPresence:", len)) {
  356. ast_cli(a->fd, "The presencestate command can only be used to set 'CustomPresence:' presence state!\n");
  357. return CLI_FAILURE;
  358. }
  359. dev += len;
  360. if (ast_strlen_zero(dev)) {
  361. return CLI_SHOWUSAGE;
  362. }
  363. args = ast_strdupa(state);
  364. if (parse_data(args, &state_val, &subtype, &message, &options)) {
  365. return CLI_SHOWUSAGE;
  366. }
  367. if (state_val == AST_PRESENCE_NOT_SET) {
  368. return CLI_SHOWUSAGE;
  369. }
  370. ast_cli(a->fd, "Changing %s to %s\n", dev, args);
  371. ast_db_put(astdb_family, dev, state);
  372. ast_presence_state_changed_literal(state_val, subtype, message, full_dev);
  373. return CLI_SUCCESS;
  374. }
  375. static struct ast_cli_entry cli_funcpresencestate[] = {
  376. AST_CLI_DEFINE(handle_cli_presencestate_list, "List currently know custom presence states"),
  377. AST_CLI_DEFINE(handle_cli_presencestate_change, "Change a custom presence state"),
  378. };
  379. #ifdef TEST_FRAMEWORK
  380. struct test_string {
  381. char *parse_string;
  382. struct {
  383. int value;
  384. const char *subtype;
  385. const char *message;
  386. const char *options;
  387. } outputs;
  388. };
  389. AST_TEST_DEFINE(test_valid_parse_data)
  390. {
  391. int i;
  392. enum ast_presence_state state;
  393. char *subtype;
  394. char *message;
  395. char *options;
  396. enum ast_test_result_state res = AST_TEST_PASS;
  397. struct test_string tests [] = {
  398. { "away",
  399. { AST_PRESENCE_AWAY,
  400. "",
  401. "",
  402. ""
  403. }
  404. },
  405. { "not_set",
  406. { AST_PRESENCE_NOT_SET,
  407. "",
  408. "",
  409. ""
  410. }
  411. },
  412. { "unavailable",
  413. { AST_PRESENCE_UNAVAILABLE,
  414. "",
  415. "",
  416. ""
  417. }
  418. },
  419. { "available",
  420. { AST_PRESENCE_AVAILABLE,
  421. "",
  422. "",
  423. ""
  424. }
  425. },
  426. { "xa",
  427. { AST_PRESENCE_XA,
  428. "",
  429. "",
  430. ""
  431. }
  432. },
  433. { "chat",
  434. { AST_PRESENCE_CHAT,
  435. "",
  436. "",
  437. ""
  438. }
  439. },
  440. { "dnd",
  441. { AST_PRESENCE_DND,
  442. "",
  443. "",
  444. ""
  445. }
  446. },
  447. { "away,down the hall",
  448. { AST_PRESENCE_AWAY,
  449. "down the hall",
  450. "",
  451. ""
  452. }
  453. },
  454. { "away,down the hall,Quarterly financial meeting",
  455. { AST_PRESENCE_AWAY,
  456. "down the hall",
  457. "Quarterly financial meeting",
  458. ""
  459. }
  460. },
  461. { "away,,Quarterly financial meeting",
  462. { AST_PRESENCE_AWAY,
  463. "",
  464. "Quarterly financial meeting",
  465. ""
  466. }
  467. },
  468. { "away,,,e",
  469. { AST_PRESENCE_AWAY,
  470. "",
  471. "",
  472. "e",
  473. }
  474. },
  475. { "away,down the hall,,e",
  476. { AST_PRESENCE_AWAY,
  477. "down the hall",
  478. "",
  479. "e"
  480. }
  481. },
  482. { "away,down the hall,Quarterly financial meeting,e",
  483. { AST_PRESENCE_AWAY,
  484. "down the hall",
  485. "Quarterly financial meeting",
  486. "e"
  487. }
  488. },
  489. { "away,,Quarterly financial meeting,e",
  490. { AST_PRESENCE_AWAY,
  491. "",
  492. "Quarterly financial meeting",
  493. "e"
  494. }
  495. }
  496. };
  497. switch (cmd) {
  498. case TEST_INIT:
  499. info->name = "parse_valid_presence_data";
  500. info->category = "/funcs/func_presence/";
  501. info->summary = "PRESENCESTATE parsing test";
  502. info->description =
  503. "Ensure that parsing function accepts proper values, and gives proper outputs";
  504. return AST_TEST_NOT_RUN;
  505. case TEST_EXECUTE:
  506. break;
  507. }
  508. for (i = 0; i < ARRAY_LEN(tests); ++i) {
  509. int parse_result;
  510. char *parse_string = ast_strdup(tests[i].parse_string);
  511. if (!parse_string) {
  512. res = AST_TEST_FAIL;
  513. break;
  514. }
  515. parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
  516. if (parse_result == -1) {
  517. res = AST_TEST_FAIL;
  518. ast_free(parse_string);
  519. break;
  520. }
  521. if (tests[i].outputs.value != state ||
  522. strcmp(tests[i].outputs.subtype, subtype) ||
  523. strcmp(tests[i].outputs.message, message) ||
  524. strcmp(tests[i].outputs.options, options)) {
  525. res = AST_TEST_FAIL;
  526. ast_free(parse_string);
  527. break;
  528. }
  529. ast_free(parse_string);
  530. }
  531. return res;
  532. }
  533. AST_TEST_DEFINE(test_invalid_parse_data)
  534. {
  535. int i;
  536. enum ast_presence_state state;
  537. char *subtype;
  538. char *message;
  539. char *options;
  540. enum ast_test_result_state res = AST_TEST_PASS;
  541. char *tests[] = {
  542. "",
  543. "bored",
  544. "away,,,i",
  545. /* XXX The following actually is parsed correctly. Should that
  546. * be changed?
  547. * "away,,,,e",
  548. */
  549. };
  550. switch (cmd) {
  551. case TEST_INIT:
  552. info->name = "parse_invalid_presence_data";
  553. info->category = "/funcs/func_presence/";
  554. info->summary = "PRESENCESTATE parsing test";
  555. info->description =
  556. "Ensure that parsing function rejects improper values";
  557. return AST_TEST_NOT_RUN;
  558. case TEST_EXECUTE:
  559. break;
  560. }
  561. for (i = 0; i < ARRAY_LEN(tests); ++i) {
  562. int parse_result;
  563. char *parse_string = ast_strdup(tests[i]);
  564. if (!parse_string) {
  565. res = AST_TEST_FAIL;
  566. break;
  567. }
  568. parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
  569. if (parse_result == 0) {
  570. ast_log(LOG_WARNING, "Invalid string parsing failed on %s\n", tests[i]);
  571. res = AST_TEST_FAIL;
  572. ast_free(parse_string);
  573. break;
  574. }
  575. ast_free(parse_string);
  576. }
  577. return res;
  578. }
  579. #define PRES_STATE "away"
  580. #define PRES_SUBTYPE "down the hall"
  581. #define PRES_MESSAGE "Quarterly financial meeting"
  582. struct test_cb_data {
  583. struct ast_presence_state_message *presence_state;
  584. /* That's right. I'm using a semaphore */
  585. sem_t sem;
  586. };
  587. static struct test_cb_data *test_cb_data_alloc(void)
  588. {
  589. struct test_cb_data *cb_data = ast_calloc(1, sizeof(*cb_data));
  590. if (!cb_data) {
  591. return NULL;
  592. }
  593. if (sem_init(&cb_data->sem, 0, 0)) {
  594. ast_free(cb_data);
  595. return NULL;
  596. }
  597. return cb_data;
  598. }
  599. static void test_cb_data_destroy(struct test_cb_data *cb_data)
  600. {
  601. ao2_cleanup(cb_data->presence_state);
  602. sem_destroy(&cb_data->sem);
  603. ast_free(cb_data);
  604. }
  605. static void test_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
  606. {
  607. struct test_cb_data *cb_data = userdata;
  608. if (stasis_message_type(msg) != ast_presence_state_message_type()) {
  609. return;
  610. }
  611. cb_data->presence_state = stasis_message_data(msg);
  612. ao2_ref(cb_data->presence_state, +1);
  613. sem_post(&cb_data->sem);
  614. }
  615. static enum ast_test_result_state presence_change_common(struct ast_test *test,
  616. const char *state, const char *subtype, const char *message, const char *options,
  617. char *out_state, size_t out_state_size,
  618. char *out_subtype, size_t out_subtype_size,
  619. char *out_message, size_t out_message_size)
  620. {
  621. RAII_VAR(struct test_cb_data *, cb_data, test_cb_data_alloc(), test_cb_data_destroy);
  622. struct stasis_subscription *test_sub;
  623. char pres[1301];
  624. if (!(test_sub = stasis_subscribe(ast_presence_state_topic_all(), test_cb, cb_data))) {
  625. return AST_TEST_FAIL;
  626. }
  627. if (ast_strlen_zero(options)) {
  628. snprintf(pres, sizeof(pres), "%s,%s,%s", state, subtype, message);
  629. } else {
  630. snprintf(pres, sizeof(pres), "%s,%s,%s,%s", state, subtype, message, options);
  631. }
  632. if (presence_write(NULL, "PRESENCESTATE", "CustomPresence:TestPresenceStateChange", pres)) {
  633. test_sub = stasis_unsubscribe_and_join(test_sub);
  634. return AST_TEST_FAIL;
  635. }
  636. sem_wait(&cb_data->sem);
  637. ast_copy_string(out_state, ast_presence_state2str(cb_data->presence_state->state), out_state_size);
  638. ast_copy_string(out_subtype, cb_data->presence_state->subtype, out_subtype_size);
  639. ast_copy_string(out_message, cb_data->presence_state->message, out_message_size);
  640. test_sub = stasis_unsubscribe_and_join(test_sub);
  641. ast_db_del("CustomPresence", "TestPresenceStateChange");
  642. return AST_TEST_PASS;
  643. }
  644. AST_TEST_DEFINE(test_presence_state_change)
  645. {
  646. char out_state[32];
  647. char out_subtype[32];
  648. char out_message[32];
  649. switch (cmd) {
  650. case TEST_INIT:
  651. info->name = "test_presence_state_change";
  652. info->category = "/funcs/func_presence/";
  653. info->summary = "presence state change subscription";
  654. info->description =
  655. "Ensure that presence state changes are communicated to subscribers";
  656. return AST_TEST_NOT_RUN;
  657. case TEST_EXECUTE:
  658. break;
  659. }
  660. if (presence_change_common(test, PRES_STATE, PRES_SUBTYPE, PRES_MESSAGE, NULL,
  661. out_state, sizeof(out_state),
  662. out_subtype, sizeof(out_subtype),
  663. out_message, sizeof(out_message)) == AST_TEST_FAIL) {
  664. return AST_TEST_FAIL;
  665. }
  666. if (strcmp(out_state, PRES_STATE) ||
  667. strcmp(out_subtype, PRES_SUBTYPE) ||
  668. strcmp(out_message, PRES_MESSAGE)) {
  669. ast_test_status_update(test, "Unexpected presence values, %s != %s, %s != %s, or %s != %s\n",
  670. PRES_STATE, out_state,
  671. PRES_SUBTYPE, out_subtype,
  672. PRES_MESSAGE, out_message);
  673. return AST_TEST_FAIL;
  674. }
  675. return AST_TEST_PASS;
  676. }
  677. AST_TEST_DEFINE(test_presence_state_base64_encode)
  678. {
  679. char out_state[32];
  680. char out_subtype[32];
  681. char out_message[32];
  682. char encoded_subtype[64];
  683. char encoded_message[64];
  684. switch (cmd) {
  685. case TEST_INIT:
  686. info->name = "test_presence_state_base64_encode";
  687. info->category = "/funcs/func_presence/";
  688. info->summary = "presence state base64 encoding";
  689. info->description =
  690. "Ensure that base64-encoded presence state is stored base64-encoded but\n"
  691. "is presented to consumers decoded.";
  692. return AST_TEST_NOT_RUN;
  693. case TEST_EXECUTE:
  694. break;
  695. }
  696. ast_base64encode(encoded_subtype, (unsigned char *) PRES_SUBTYPE, strlen(PRES_SUBTYPE), sizeof(encoded_subtype) - 1);
  697. ast_base64encode(encoded_message, (unsigned char *) PRES_MESSAGE, strlen(PRES_MESSAGE), sizeof(encoded_message) - 1);
  698. if (presence_change_common(test, PRES_STATE, encoded_subtype, encoded_message, "e",
  699. out_state, sizeof(out_state),
  700. out_subtype, sizeof(out_subtype),
  701. out_message, sizeof(out_message)) == AST_TEST_FAIL) {
  702. return AST_TEST_FAIL;
  703. }
  704. if (strcmp(out_state, PRES_STATE) ||
  705. strcmp(out_subtype, PRES_SUBTYPE) ||
  706. strcmp(out_message, PRES_MESSAGE)) {
  707. ast_test_status_update(test, "Unexpected presence values, %s != %s, %s != %s, or %s != %s\n",
  708. PRES_STATE, out_state,
  709. PRES_SUBTYPE, out_subtype,
  710. PRES_MESSAGE, out_message);
  711. return AST_TEST_FAIL;
  712. }
  713. return AST_TEST_PASS;
  714. }
  715. #endif
  716. static int unload_module(void)
  717. {
  718. int res = 0;
  719. res |= ast_custom_function_unregister(&presence_function);
  720. res |= ast_presence_state_prov_del("CustomPresence");
  721. res |= ast_cli_unregister_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
  722. #ifdef TEST_FRAMEWORK
  723. AST_TEST_UNREGISTER(test_valid_parse_data);
  724. AST_TEST_UNREGISTER(test_invalid_parse_data);
  725. AST_TEST_UNREGISTER(test_presence_state_change);
  726. AST_TEST_UNREGISTER(test_presence_state_base64_encode);
  727. #endif
  728. return res;
  729. }
  730. static int load_module(void)
  731. {
  732. int res = 0;
  733. struct ast_db_entry *db_entry, *db_tree;
  734. /* Populate the presence state cache on the system with all of the currently
  735. * known custom presence states. */
  736. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  737. for (; db_entry; db_entry = db_entry->next) {
  738. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  739. enum ast_presence_state state;
  740. char *message;
  741. char *subtype;
  742. if (dev_name <= (const char *) 1) {
  743. continue;
  744. }
  745. state = custom_presence_callback(dev_name, &subtype, &message);
  746. ast_presence_state_changed(state, subtype, message, "CustomPresence:%s", dev_name);
  747. ast_free(subtype);
  748. ast_free(message);
  749. }
  750. ast_db_freetree(db_tree);
  751. db_tree = NULL;
  752. res |= ast_custom_function_register(&presence_function);
  753. res |= ast_presence_state_prov_add("CustomPresence", custom_presence_callback);
  754. res |= ast_cli_register_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
  755. #ifdef TEST_FRAMEWORK
  756. AST_TEST_REGISTER(test_valid_parse_data);
  757. AST_TEST_REGISTER(test_invalid_parse_data);
  758. AST_TEST_REGISTER(test_presence_state_change);
  759. AST_TEST_REGISTER(test_presence_state_base64_encode);
  760. #endif
  761. return res;
  762. }
  763. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a presence state in the dialplan",
  764. .support_level = AST_MODULE_SUPPORT_CORE,
  765. .load = load_module,
  766. .unload = unload_module,
  767. .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
  768. );