func_presencestate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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/event.h"
  40. #include <semaphore.h>
  41. #endif
  42. /*** DOCUMENTATION
  43. <function name="PRESENCE_STATE" language="en_US">
  44. <synopsis>
  45. Get or Set a presence state.
  46. </synopsis>
  47. <syntax>
  48. <parameter name="provider" required="true">
  49. <para>The provider of the presence, such as <literal>CustomPresence</literal></para>
  50. </parameter>
  51. <parameter name="field" required="true">
  52. <para>Which field of the presence state information is wanted.</para>
  53. <optionlist>
  54. <option name="value">
  55. <para>The current presence, such as <literal>away</literal></para>
  56. </option>
  57. <option name="subtype">
  58. <para>Further information about the current presence</para>
  59. </option>
  60. <option name="message">
  61. <para>A custom message that may indicate further details about the presence</para>
  62. </option>
  63. </optionlist>
  64. </parameter>
  65. <parameter name="options" required="false">
  66. <optionlist>
  67. <option name="e">
  68. <para>On Write - Use this option when the subtype and message provided are Base64
  69. encoded. On Read - Retrieves message/subtype in Base64 encoded form.</para>
  70. </option>
  71. </optionlist>
  72. </parameter>
  73. </syntax>
  74. <description>
  75. <para>The PRESENCE_STATE function can be used to retrieve the presence from any
  76. presence provider. For example:</para>
  77. <para>NoOp(SIP/mypeer has presence ${PRESENCE_STATE(SIP/mypeer,value)})</para>
  78. <para>NoOp(Conference number 1234 has presence message ${PRESENCE_STATE(MeetMe:1234,message)})</para>
  79. <para>The PRESENCE_STATE function can also be used to set custom presence state from
  80. the dialplan. The <literal>CustomPresence:</literal> prefix must be used. For example:</para>
  81. <para>Set(PRESENCE_STATE(CustomPresence:lamp1)=away,temporary,Out to lunch)</para>
  82. <para>Set(PRESENCE_STATE(CustomPresence:lamp2)=dnd,,Trying to get work done)</para>
  83. <para>Set(PRESENCE_STATE(CustomPresence:lamp3)=xa,T24gdmFjYXRpb24=,,e)</para>
  84. <para>Set(BASE64_LAMP3_PRESENCE=${PRESENCE_STATE(CustomPresence:lamp3,subtype,e)})</para>
  85. <para>You can subscribe to the status of a custom presence state using a hint in
  86. the dialplan:</para>
  87. <para>exten => 1234,hint,,CustomPresence:lamp1</para>
  88. <para>The possible values for both uses of this function are:</para>
  89. <para>not_set | unavailable | available | away | xa | chat | dnd</para>
  90. </description>
  91. </function>
  92. ***/
  93. static const char astdb_family[] = "CustomPresence";
  94. static int presence_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  95. {
  96. int state;
  97. char *message = NULL;
  98. char *subtype = NULL;
  99. char *parse;
  100. int base64encode = 0;
  101. AST_DECLARE_APP_ARGS(args,
  102. AST_APP_ARG(provider);
  103. AST_APP_ARG(field);
  104. AST_APP_ARG(options);
  105. );
  106. if (ast_strlen_zero(data)) {
  107. ast_log(LOG_WARNING, "PRESENCE_STATE reading requires an argument \n");
  108. return -1;
  109. }
  110. parse = ast_strdupa(data);
  111. AST_STANDARD_APP_ARGS(args, parse);
  112. if (ast_strlen_zero(args.provider) || ast_strlen_zero(args.field)) {
  113. ast_log(LOG_WARNING, "PRESENCE_STATE reading requires both presence provider and presence field arguments. \n");
  114. return -1;
  115. }
  116. state = ast_presence_state_nocache(args.provider, &subtype, &message);
  117. if (state == AST_PRESENCE_INVALID) {
  118. ast_log(LOG_WARNING, "PRESENCE_STATE unknown \n");
  119. return -1;
  120. }
  121. if (!(ast_strlen_zero(args.options)) && (strchr(args.options, 'e'))) {
  122. base64encode = 1;
  123. }
  124. if (!ast_strlen_zero(subtype) && !strcasecmp(args.field, "subtype")) {
  125. if (base64encode) {
  126. ast_base64encode(buf, (unsigned char *) subtype, strlen(subtype), len);
  127. } else {
  128. ast_copy_string(buf, subtype, len);
  129. }
  130. } else if (!ast_strlen_zero(message) && !strcasecmp(args.field, "message")) {
  131. if (base64encode) {
  132. ast_base64encode(buf, (unsigned char *) message, strlen(message), len);
  133. } else {
  134. ast_copy_string(buf, message, len);
  135. }
  136. } else if (!strcasecmp(args.field, "value")) {
  137. ast_copy_string(buf, ast_presence_state2str(state), len);
  138. }
  139. ast_free(message);
  140. ast_free(subtype);
  141. return 0;
  142. }
  143. static int parse_data(char *data, enum ast_presence_state *state, char **subtype, char **message, char **options)
  144. {
  145. char *state_str;
  146. /* data syntax is state,subtype,message,options */
  147. *subtype = "";
  148. *message = "";
  149. *options = "";
  150. state_str = strsep(&data, ",");
  151. if (ast_strlen_zero(state_str)) {
  152. return -1; /* state is required */
  153. }
  154. *state = ast_presence_state_val(state_str);
  155. /* not a valid state */
  156. if (*state == AST_PRESENCE_INVALID) {
  157. ast_log(LOG_WARNING, "Unknown presence state value %s\n", state_str);
  158. return -1;
  159. }
  160. if (!(*subtype = strsep(&data,","))) {
  161. *subtype = "";
  162. return 0;
  163. }
  164. if (!(*message = strsep(&data, ","))) {
  165. *message = "";
  166. return 0;
  167. }
  168. if (!(*options = strsep(&data, ","))) {
  169. *options = "";
  170. return 0;
  171. }
  172. if (!ast_strlen_zero(*options) && !(strchr(*options, 'e'))) {
  173. ast_log(LOG_NOTICE, "Invalid options '%s'\n", *options);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. static int presence_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  179. {
  180. size_t len = strlen("CustomPresence:");
  181. char *tmp = data;
  182. char *args = ast_strdupa(value);
  183. enum ast_presence_state state;
  184. char *options, *message, *subtype;
  185. if (strncasecmp(data, "CustomPresence:", len)) {
  186. ast_log(LOG_WARNING, "The PRESENCE_STATE function can only set CustomPresence: presence providers.\n");
  187. return -1;
  188. }
  189. data += len;
  190. if (ast_strlen_zero(data)) {
  191. ast_log(LOG_WARNING, "PRESENCE_STATE function called with no custom device name!\n");
  192. return -1;
  193. }
  194. if (parse_data(args, &state, &subtype, &message, &options)) {
  195. ast_log(LOG_WARNING, "Invalid arguments to PRESENCE_STATE\n");
  196. return -1;
  197. }
  198. ast_db_put(astdb_family, data, value);
  199. ast_presence_state_changed_literal(state, subtype, message, tmp);
  200. return 0;
  201. }
  202. static enum ast_presence_state custom_presence_callback(const char *data, char **subtype, char **message)
  203. {
  204. char buf[1301] = "";
  205. enum ast_presence_state state;
  206. char *_options;
  207. char *_message;
  208. char *_subtype;
  209. ast_db_get(astdb_family, data, buf, sizeof(buf));
  210. if (parse_data(buf, &state, &_subtype, &_message, &_options)) {
  211. return AST_PRESENCE_INVALID;
  212. }
  213. if ((strchr(_options, 'e'))) {
  214. char tmp[1301];
  215. if (ast_strlen_zero(_subtype)) {
  216. *subtype = NULL;
  217. } else {
  218. memset(tmp, 0, sizeof(tmp));
  219. ast_base64decode((unsigned char *) tmp, _subtype, sizeof(tmp) - 1);
  220. *subtype = ast_strdup(tmp);
  221. }
  222. if (ast_strlen_zero(_message)) {
  223. *message = NULL;
  224. } else {
  225. memset(tmp, 0, sizeof(tmp));
  226. ast_base64decode((unsigned char *) tmp, _message, sizeof(tmp) - 1);
  227. *message = ast_strdup(tmp);
  228. }
  229. } else {
  230. *subtype = ast_strlen_zero(_subtype) ? NULL : ast_strdup(_subtype);
  231. *message = ast_strlen_zero(_message) ? NULL : ast_strdup(_message);
  232. }
  233. return state;
  234. }
  235. static struct ast_custom_function presence_function = {
  236. .name = "PRESENCE_STATE",
  237. .read = presence_read,
  238. .write = presence_write,
  239. };
  240. static char *handle_cli_presencestate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  241. {
  242. struct ast_db_entry *db_entry, *db_tree;
  243. switch (cmd) {
  244. case CLI_INIT:
  245. e->command = "presencestate list";
  246. e->usage =
  247. "Usage: presencestate list\n"
  248. " List all custom presence states that have been set by using\n"
  249. " the PRESENCE_STATE dialplan function.\n";
  250. return NULL;
  251. case CLI_GENERATE:
  252. return NULL;
  253. }
  254. if (a->argc != e->args) {
  255. return CLI_SHOWUSAGE;
  256. }
  257. ast_cli(a->fd, "\n"
  258. "---------------------------------------------------------------------\n"
  259. "--- Custom Presence States ------------------------------------------\n"
  260. "---------------------------------------------------------------------\n"
  261. "---\n");
  262. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  263. if (!db_entry) {
  264. ast_cli(a->fd, "No custom presence states defined\n");
  265. return CLI_SUCCESS;
  266. }
  267. for (; db_entry; db_entry = db_entry->next) {
  268. const char *object_name = strrchr(db_entry->key, '/') + 1;
  269. char state_info[1301];
  270. enum ast_presence_state state;
  271. char *subtype;
  272. char *message;
  273. char *options;
  274. ast_copy_string(state_info, db_entry->data, sizeof(state_info));
  275. if (parse_data(state_info, &state, &subtype, &message, &options)) {
  276. ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data);
  277. continue;
  278. }
  279. if (object_name <= (const char *) 1) {
  280. continue;
  281. }
  282. ast_cli(a->fd, "--- Name: 'CustomPresence:%s'\n"
  283. " --- State: '%s'\n"
  284. " --- Subtype: '%s'\n"
  285. " --- Message: '%s'\n"
  286. " --- Base64 Encoded: '%s'\n"
  287. "---\n",
  288. object_name,
  289. ast_presence_state2str(state),
  290. subtype,
  291. message,
  292. AST_CLI_YESNO(strchr(options, 'e')));
  293. }
  294. ast_db_freetree(db_tree);
  295. db_tree = NULL;
  296. ast_cli(a->fd,
  297. "---------------------------------------------------------------------\n"
  298. "---------------------------------------------------------------------\n"
  299. "\n");
  300. return CLI_SUCCESS;
  301. }
  302. static char *handle_cli_presencestate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  303. {
  304. size_t len;
  305. const char *dev, *state, *full_dev;
  306. enum ast_presence_state state_val;
  307. char *message;
  308. char *subtype;
  309. char *options;
  310. char *args;
  311. switch (cmd) {
  312. case CLI_INIT:
  313. e->command = "presencestate change";
  314. e->usage =
  315. "Usage: presencestate change <entity> <state>[,<subtype>[,message[,options]]]\n"
  316. " Change a custom presence to a new state.\n"
  317. " The possible values for the state are:\n"
  318. "NOT_SET | UNAVAILABLE | AVAILABLE | AWAY | XA | CHAT | DND\n"
  319. "Optionally, a custom subtype and message may be provided, along with any options\n"
  320. "accepted by func_presencestate. If the subtype or message provided contain spaces,\n"
  321. "be sure to enclose the data in quotation marks (\"\")\n"
  322. "\n"
  323. "Examples:\n"
  324. " presencestate change CustomPresence:mystate1 AWAY\n"
  325. " presencestate change CustomPresence:mystate1 AVAILABLE\n"
  326. " presencestate change CustomPresence:mystate1 \"Away,upstairs,eating lunch\"\n"
  327. " \n";
  328. return NULL;
  329. case CLI_GENERATE:
  330. {
  331. static const char * const cmds[] = { "NOT_SET", "UNAVAILABLE", "AVAILABLE", "AWAY",
  332. "XA", "CHAT", "DND", NULL };
  333. if (a->pos == e->args + 1) {
  334. return ast_cli_complete(a->word, cmds, a->n);
  335. }
  336. return NULL;
  337. }
  338. }
  339. if (a->argc != e->args + 2) {
  340. return CLI_SHOWUSAGE;
  341. }
  342. len = strlen("CustomPresence:");
  343. full_dev = dev = a->argv[e->args];
  344. state = a->argv[e->args + 1];
  345. if (strncasecmp(dev, "CustomPresence:", len)) {
  346. ast_cli(a->fd, "The presencestate command can only be used to set 'CustomPresence:' presence state!\n");
  347. return CLI_FAILURE;
  348. }
  349. dev += len;
  350. if (ast_strlen_zero(dev)) {
  351. return CLI_SHOWUSAGE;
  352. }
  353. args = ast_strdupa(state);
  354. if (parse_data(args, &state_val, &subtype, &message, &options)) {
  355. return CLI_SHOWUSAGE;
  356. }
  357. if (state_val == AST_PRESENCE_NOT_SET) {
  358. return CLI_SHOWUSAGE;
  359. }
  360. ast_cli(a->fd, "Changing %s to %s\n", dev, args);
  361. ast_db_put(astdb_family, dev, state);
  362. ast_presence_state_changed_literal(state_val, subtype, message, full_dev);
  363. return CLI_SUCCESS;
  364. }
  365. static struct ast_cli_entry cli_funcpresencestate[] = {
  366. AST_CLI_DEFINE(handle_cli_presencestate_list, "List currently know custom presence states"),
  367. AST_CLI_DEFINE(handle_cli_presencestate_change, "Change a custom presence state"),
  368. };
  369. #ifdef TEST_FRAMEWORK
  370. struct test_string {
  371. char *parse_string;
  372. struct {
  373. int value;
  374. const char *subtype;
  375. const char *message;
  376. const char *options;
  377. } outputs;
  378. };
  379. AST_TEST_DEFINE(test_valid_parse_data)
  380. {
  381. int i;
  382. enum ast_presence_state state;
  383. char *subtype;
  384. char *message;
  385. char *options;
  386. enum ast_test_result_state res = AST_TEST_PASS;
  387. struct test_string tests [] = {
  388. { "away",
  389. { AST_PRESENCE_AWAY,
  390. "",
  391. "",
  392. ""
  393. }
  394. },
  395. { "not_set",
  396. { AST_PRESENCE_NOT_SET,
  397. "",
  398. "",
  399. ""
  400. }
  401. },
  402. { "unavailable",
  403. { AST_PRESENCE_UNAVAILABLE,
  404. "",
  405. "",
  406. ""
  407. }
  408. },
  409. { "available",
  410. { AST_PRESENCE_AVAILABLE,
  411. "",
  412. "",
  413. ""
  414. }
  415. },
  416. { "xa",
  417. { AST_PRESENCE_XA,
  418. "",
  419. "",
  420. ""
  421. }
  422. },
  423. { "chat",
  424. { AST_PRESENCE_CHAT,
  425. "",
  426. "",
  427. ""
  428. }
  429. },
  430. { "dnd",
  431. { AST_PRESENCE_DND,
  432. "",
  433. "",
  434. ""
  435. }
  436. },
  437. { "away,down the hall",
  438. { AST_PRESENCE_AWAY,
  439. "down the hall",
  440. "",
  441. ""
  442. }
  443. },
  444. { "away,down the hall,Quarterly financial meeting",
  445. { AST_PRESENCE_AWAY,
  446. "down the hall",
  447. "Quarterly financial meeting",
  448. ""
  449. }
  450. },
  451. { "away,,Quarterly financial meeting",
  452. { AST_PRESENCE_AWAY,
  453. "",
  454. "Quarterly financial meeting",
  455. ""
  456. }
  457. },
  458. { "away,,,e",
  459. { AST_PRESENCE_AWAY,
  460. "",
  461. "",
  462. "e",
  463. }
  464. },
  465. { "away,down the hall,,e",
  466. { AST_PRESENCE_AWAY,
  467. "down the hall",
  468. "",
  469. "e"
  470. }
  471. },
  472. { "away,down the hall,Quarterly financial meeting,e",
  473. { AST_PRESENCE_AWAY,
  474. "down the hall",
  475. "Quarterly financial meeting",
  476. "e"
  477. }
  478. },
  479. { "away,,Quarterly financial meeting,e",
  480. { AST_PRESENCE_AWAY,
  481. "",
  482. "Quarterly financial meeting",
  483. "e"
  484. }
  485. }
  486. };
  487. switch (cmd) {
  488. case TEST_INIT:
  489. info->name = "parse_valid_presence_data";
  490. info->category = "/funcs/func_presence/";
  491. info->summary = "PRESENCESTATE parsing test";
  492. info->description =
  493. "Ensure that parsing function accepts proper values, and gives proper outputs";
  494. return AST_TEST_NOT_RUN;
  495. case TEST_EXECUTE:
  496. break;
  497. }
  498. for (i = 0; i < ARRAY_LEN(tests); ++i) {
  499. int parse_result;
  500. char *parse_string = ast_strdup(tests[i].parse_string);
  501. if (!parse_string) {
  502. res = AST_TEST_FAIL;
  503. break;
  504. }
  505. parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
  506. if (parse_result == -1) {
  507. res = AST_TEST_FAIL;
  508. ast_free(parse_string);
  509. break;
  510. }
  511. if (tests[i].outputs.value != state ||
  512. strcmp(tests[i].outputs.subtype, subtype) ||
  513. strcmp(tests[i].outputs.message, message) ||
  514. strcmp(tests[i].outputs.options, options)) {
  515. res = AST_TEST_FAIL;
  516. ast_free(parse_string);
  517. break;
  518. }
  519. ast_free(parse_string);
  520. }
  521. return res;
  522. }
  523. AST_TEST_DEFINE(test_invalid_parse_data)
  524. {
  525. int i;
  526. enum ast_presence_state state;
  527. char *subtype;
  528. char *message;
  529. char *options;
  530. enum ast_test_result_state res = AST_TEST_PASS;
  531. char *tests[] = {
  532. "",
  533. "bored",
  534. "away,,,i",
  535. /* XXX The following actually is parsed correctly. Should that
  536. * be changed?
  537. * "away,,,,e",
  538. */
  539. };
  540. switch (cmd) {
  541. case TEST_INIT:
  542. info->name = "parse_invalid_presence_data";
  543. info->category = "/funcs/func_presence/";
  544. info->summary = "PRESENCESTATE parsing test";
  545. info->description =
  546. "Ensure that parsing function rejects improper values";
  547. return AST_TEST_NOT_RUN;
  548. case TEST_EXECUTE:
  549. break;
  550. }
  551. for (i = 0; i < ARRAY_LEN(tests); ++i) {
  552. int parse_result;
  553. char *parse_string = ast_strdup(tests[i]);
  554. if (!parse_string) {
  555. res = AST_TEST_FAIL;
  556. break;
  557. }
  558. parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
  559. if (parse_result == 0) {
  560. ast_log(LOG_WARNING, "Invalid string parsing failed on %s\n", tests[i]);
  561. res = AST_TEST_FAIL;
  562. ast_free(parse_string);
  563. break;
  564. }
  565. ast_free(parse_string);
  566. }
  567. return res;
  568. }
  569. struct test_cb_data {
  570. enum ast_presence_state presence;
  571. const char *provider;
  572. const char *subtype;
  573. const char *message;
  574. /* That's right. I'm using a semaphore */
  575. sem_t sem;
  576. };
  577. static void test_cb(const struct ast_event *event, void *userdata)
  578. {
  579. struct test_cb_data *cb_data = userdata;
  580. cb_data->presence = ast_event_get_ie_uint(event, AST_EVENT_IE_PRESENCE_STATE);
  581. cb_data->provider = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_PROVIDER));
  582. cb_data->subtype = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_SUBTYPE));
  583. cb_data->message = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_MESSAGE));
  584. sem_post(&cb_data->sem);
  585. }
  586. /* XXX This test could probably stand to be moved since
  587. * it does not test func_presencestate but rather code in
  588. * presencestate.h and presencestate.c. However, the convenience
  589. * of presence_write() makes this a nice location for this test.
  590. */
  591. AST_TEST_DEFINE(test_presence_state_change)
  592. {
  593. struct ast_event_sub *test_sub;
  594. struct test_cb_data *cb_data;
  595. switch (cmd) {
  596. case TEST_INIT:
  597. info->name = "test_presence_state_change";
  598. info->category = "/funcs/func_presence/";
  599. info->summary = "presence state change subscription";
  600. info->description =
  601. "Ensure that presence state changes are communicated to subscribers";
  602. return AST_TEST_NOT_RUN;
  603. case TEST_EXECUTE:
  604. break;
  605. }
  606. cb_data = ast_calloc(1, sizeof(*cb_data));
  607. if (!cb_data) {
  608. return AST_TEST_FAIL;
  609. }
  610. if (!(test_sub = ast_event_subscribe(AST_EVENT_PRESENCE_STATE,
  611. test_cb, "Test presence state callbacks", cb_data, AST_EVENT_IE_END))) {
  612. return AST_TEST_FAIL;
  613. }
  614. if (sem_init(&cb_data->sem, 0, 0)) {
  615. return AST_TEST_FAIL;
  616. }
  617. presence_write(NULL, "PRESENCESTATE", "CustomPresence:TestPresenceStateChange", "away,down the hall,Quarterly financial meeting");
  618. sem_wait(&cb_data->sem);
  619. if (cb_data->presence != AST_PRESENCE_AWAY ||
  620. strcmp(cb_data->provider, "CustomPresence:TestPresenceStateChange") ||
  621. strcmp(cb_data->subtype, "down the hall") ||
  622. strcmp(cb_data->message, "Quarterly financial meeting")) {
  623. return AST_TEST_FAIL;
  624. }
  625. ast_free((char *)cb_data->provider);
  626. ast_free((char *)cb_data->subtype);
  627. ast_free((char *)cb_data->message);
  628. ast_free((char *)cb_data);
  629. ast_db_del("CustomPresence", "TestPresenceStateChange");
  630. return AST_TEST_PASS;
  631. }
  632. #endif
  633. static int unload_module(void)
  634. {
  635. int res = 0;
  636. res |= ast_custom_function_unregister(&presence_function);
  637. res |= ast_presence_state_prov_del("CustomPresence");
  638. res |= ast_cli_unregister_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
  639. #ifdef TEST_FRAMEWORK
  640. AST_TEST_UNREGISTER(test_valid_parse_data);
  641. AST_TEST_UNREGISTER(test_invalid_parse_data);
  642. AST_TEST_UNREGISTER(test_presence_state_change);
  643. #endif
  644. return res;
  645. }
  646. static int load_module(void)
  647. {
  648. int res = 0;
  649. struct ast_db_entry *db_entry, *db_tree;
  650. /* Populate the presence state cache on the system with all of the currently
  651. * known custom presence states. */
  652. db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
  653. for (; db_entry; db_entry = db_entry->next) {
  654. const char *dev_name = strrchr(db_entry->key, '/') + 1;
  655. char state_info[1301];
  656. enum ast_presence_state state;
  657. char *message;
  658. char *subtype;
  659. char *options;
  660. if (dev_name <= (const char *) 1) {
  661. continue;
  662. }
  663. ast_copy_string(state_info, db_entry->data, sizeof(state_info));
  664. if (parse_data(state_info, &state, &subtype, &message, &options)) {
  665. ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data);
  666. continue;
  667. }
  668. ast_presence_state_changed(state, subtype, message, "CustomPresence:%s", dev_name);
  669. }
  670. ast_db_freetree(db_tree);
  671. db_tree = NULL;
  672. res |= ast_custom_function_register(&presence_function);
  673. res |= ast_presence_state_prov_add("CustomPresence", custom_presence_callback);
  674. res |= ast_cli_register_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
  675. #ifdef TEST_FRAMEWORK
  676. AST_TEST_REGISTER(test_valid_parse_data);
  677. AST_TEST_REGISTER(test_invalid_parse_data);
  678. AST_TEST_REGISTER(test_presence_state_change);
  679. #endif
  680. return res;
  681. }
  682. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a presence state in the dialplan",
  683. .load = load_module,
  684. .unload = unload_module,
  685. .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
  686. );