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