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