app_userevent.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief UserEvent application -- send manager event
  19. *
  20. * \ingroup applications
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include "asterisk/pbx.h"
  27. #include "asterisk/module.h"
  28. #include "asterisk/manager.h"
  29. #include "asterisk/app.h"
  30. #include "asterisk/json.h"
  31. #include "asterisk/stasis_channels.h"
  32. /*** DOCUMENTATION
  33. <application name="UserEvent" language="en_US">
  34. <synopsis>
  35. Send an arbitrary user-defined event to parties interested in a channel (AMI users and relevant res_stasis applications).
  36. </synopsis>
  37. <syntax>
  38. <parameter name="eventname" required="true" />
  39. <parameter name="body" />
  40. </syntax>
  41. <description>
  42. <para>Sends an arbitrary event to interested parties, with an optional
  43. <replaceable>body</replaceable> representing additional arguments. The
  44. <replaceable>body</replaceable> may be specified as
  45. a <literal>,</literal> delimited list of key:value pairs.</para>
  46. <para>For AMI, each additional argument will be placed on a new line in
  47. the event and the format of the event will be:</para>
  48. <para> Event: UserEvent</para>
  49. <para> UserEvent: &lt;specified event name&gt;</para>
  50. <para> [body]</para>
  51. <para>If no <replaceable>body</replaceable> is specified, only Event and
  52. UserEvent headers will be present.</para>
  53. <para>For res_stasis applications, the event will be provided as a JSON
  54. blob with additional arguments appearing as keys in the object and the
  55. <replaceable>eventname</replaceable> under the
  56. <literal>eventname</literal> key.</para>
  57. </description>
  58. <see-also>
  59. <ref type="manager">UserEvent</ref>
  60. <ref type="managerEvent">UserEvent</ref>
  61. </see-also>
  62. </application>
  63. ***/
  64. static char *app = "UserEvent";
  65. static int userevent_exec(struct ast_channel *chan, const char *data)
  66. {
  67. char *parse;
  68. int x;
  69. AST_DECLARE_APP_ARGS(args,
  70. AST_APP_ARG(eventname);
  71. AST_APP_ARG(extra)[100];
  72. );
  73. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  74. if (ast_strlen_zero(data)) {
  75. ast_log(LOG_WARNING, "UserEvent requires an argument (eventname,optional event body)\n");
  76. return -1;
  77. }
  78. parse = ast_strdupa(data);
  79. AST_STANDARD_APP_ARGS(args, parse);
  80. blob = ast_json_pack("{s: s}",
  81. "eventname", args.eventname);
  82. if (!blob) {
  83. return -1;
  84. }
  85. for (x = 0; x < args.argc - 1; x++) {
  86. char *key, *value = args.extra[x];
  87. struct ast_json *json_value;
  88. key = strsep(&value, ":");
  89. if (!value) {
  90. /* no ':' in string? */
  91. continue;
  92. }
  93. value = ast_strip(value);
  94. json_value = ast_json_string_create(value);
  95. if (!json_value) {
  96. return -1;
  97. }
  98. /* ref stolen by ast_json_object_set */
  99. if (ast_json_object_set(blob, key, json_value)) {
  100. return -1;
  101. }
  102. }
  103. ast_channel_lock(chan);
  104. ast_multi_object_blob_single_channel_publish(chan, ast_multi_user_event_type(), blob);
  105. ast_channel_unlock(chan);
  106. return 0;
  107. }
  108. static int unload_module(void)
  109. {
  110. return ast_unregister_application(app);
  111. }
  112. static int load_module(void)
  113. {
  114. return ast_register_application_xml(app, userevent_exec);
  115. }
  116. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");