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