app_userevent.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/pbx.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/manager.h"
  30. #include "asterisk/app.h"
  31. #include "asterisk/json.h"
  32. #include "asterisk/stasis_channels.h"
  33. /*** DOCUMENTATION
  34. <application name="UserEvent" language="en_US">
  35. <synopsis>
  36. Send an arbitrary user-defined event to parties interested in a channel (AMI users and relevant res_stasis applications).
  37. </synopsis>
  38. <syntax>
  39. <parameter name="eventname" required="true" />
  40. <parameter name="body" />
  41. </syntax>
  42. <description>
  43. <para>Sends an arbitrary event to interested parties, with an optional
  44. <replaceable>body</replaceable> representing additional arguments. The
  45. <replaceable>body</replaceable> may be specified as
  46. a <literal>,</literal> delimited list of key:value pairs.</para>
  47. <para>For AMI, each additional argument will be placed on a new line in
  48. the event and the format of the event will be:</para>
  49. <para> Event: UserEvent</para>
  50. <para> UserEvent: &lt;specified event name&gt;</para>
  51. <para> [body]</para>
  52. <para>If no <replaceable>body</replaceable> is specified, only Event and
  53. UserEvent headers will be present.</para>
  54. <para>For res_stasis applications, the event will be provided as a JSON
  55. blob with additional arguments appearing as keys in the object and the
  56. <replaceable>eventname</replaceable> under the
  57. <literal>eventname</literal> key.</para>
  58. </description>
  59. <see-also>
  60. <ref type="manager">UserEvent</ref>
  61. <ref type="managerEvent">UserEvent</ref>
  62. </see-also>
  63. </application>
  64. ***/
  65. static char *app = "UserEvent";
  66. static int userevent_exec(struct ast_channel *chan, const char *data)
  67. {
  68. char *parse;
  69. int x;
  70. AST_DECLARE_APP_ARGS(args,
  71. AST_APP_ARG(eventname);
  72. AST_APP_ARG(extra)[100];
  73. );
  74. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  75. if (ast_strlen_zero(data)) {
  76. ast_log(LOG_WARNING, "UserEvent requires an argument (eventname,optional event body)\n");
  77. return -1;
  78. }
  79. parse = ast_strdupa(data);
  80. AST_STANDARD_APP_ARGS(args, parse);
  81. blob = ast_json_pack("{s: s}",
  82. "eventname", args.eventname);
  83. if (!blob) {
  84. return -1;
  85. }
  86. for (x = 0; x < args.argc - 1; x++) {
  87. char *key, *value = args.extra[x];
  88. struct ast_json *json_value;
  89. key = strsep(&value, ":");
  90. if (!value) {
  91. /* no ':' in string? */
  92. continue;
  93. }
  94. value = ast_strip(value);
  95. json_value = ast_json_string_create(value);
  96. if (!json_value) {
  97. return -1;
  98. }
  99. /* ref stolen by ast_json_object_set */
  100. if (ast_json_object_set(blob, key, json_value)) {
  101. return -1;
  102. }
  103. }
  104. ast_channel_lock(chan);
  105. ast_multi_object_blob_single_channel_publish(chan, ast_multi_user_event_type(), blob);
  106. ast_channel_unlock(chan);
  107. return 0;
  108. }
  109. static int unload_module(void)
  110. {
  111. return ast_unregister_application(app);
  112. }
  113. static int load_module(void)
  114. {
  115. return ast_register_application_xml(app, userevent_exec);
  116. }
  117. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");