app_userevent.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. </application>
  60. ***/
  61. static char *app = "UserEvent";
  62. static int userevent_exec(struct ast_channel *chan, const char *data)
  63. {
  64. char *parse;
  65. int x;
  66. AST_DECLARE_APP_ARGS(args,
  67. AST_APP_ARG(eventname);
  68. AST_APP_ARG(extra)[100];
  69. );
  70. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  71. if (ast_strlen_zero(data)) {
  72. ast_log(LOG_WARNING, "UserEvent requires an argument (eventname,optional event body)\n");
  73. return -1;
  74. }
  75. parse = ast_strdupa(data);
  76. AST_STANDARD_APP_ARGS(args, parse);
  77. blob = ast_json_pack("{s: s}",
  78. "eventname", args.eventname);
  79. if (!blob) {
  80. return -1;
  81. }
  82. for (x = 0; x < args.argc - 1; x++) {
  83. char *key, *value = args.extra[x];
  84. struct ast_json *json_value;
  85. key = strsep(&value, ":");
  86. if (!value) {
  87. /* no ':' in string? */
  88. continue;
  89. }
  90. value = ast_strip(value);
  91. json_value = ast_json_string_create(value);
  92. if (!json_value) {
  93. return -1;
  94. }
  95. /* ref stolen by ast_json_object_set */
  96. if (ast_json_object_set(blob, key, json_value)) {
  97. return -1;
  98. }
  99. }
  100. ast_channel_lock(chan);
  101. ast_multi_object_blob_single_channel_publish(chan, ast_multi_user_event_type(), blob);
  102. ast_channel_unlock(chan);
  103. return 0;
  104. }
  105. static int unload_module(void)
  106. {
  107. return ast_unregister_application(app);
  108. }
  109. static int load_module(void)
  110. {
  111. return ast_register_application_xml(app, userevent_exec);
  112. }
  113. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");