Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[asterisk/asterisk.git] / apps / app_celgenuserevent.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, 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 Generate User-Defined CEL event
20  *
21  * \author Steve Murphy
22  *
23  * \ingroup applications
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/module.h"
33 #include "asterisk/app.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/cel.h"
36
37 /*** DOCUMENTATION
38         <application name="CELGenUserEvent" language="en_US">
39                 <synopsis>
40                         Generates a CEL User Defined Event.
41                 </synopsis>
42                 <syntax>
43                         <parameter name="event-name" required="true">
44                                 <argument name="event-name" required="true">
45                                 </argument>
46                                 <argument name="extra" required="false">
47                                         <para>Extra text to be included with the event.</para>
48                                 </argument>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>A CEL event will be immediately generated by this channel, with the supplied name for a type.</para>
53                 </description>
54         </application>
55  ***/
56
57 static char *app = "CELGenUserEvent";
58
59 static int celgenuserevent_exec(struct ast_channel *chan, const char *data)
60 {
61         int res = 0;
62         char *parse;
63         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
64         AST_DECLARE_APP_ARGS(args,
65                 AST_APP_ARG(event);
66                 AST_APP_ARG(extra);
67         );
68
69         if (ast_strlen_zero(data)) {
70                 return 0;
71         }
72
73         parse = ast_strdupa(data);
74         AST_STANDARD_APP_ARGS(args, parse);
75
76         blob = ast_json_pack("{s: s, s: {s: s}}",
77                 "event", args.event,
78                 "extra", "extra", S_OR(args.extra, ""));
79         if (!blob) {
80                 return res;
81         }
82         ast_cel_publish_event(chan, AST_CEL_USER_DEFINED, blob);
83         return res;
84 }
85
86 static int unload_module(void)
87 {
88         ast_unregister_application(app);
89         return 0;
90 }
91
92 static int load_module(void)
93 {
94         int res = ast_register_application_xml(app, celgenuserevent_exec);
95         if (res) {
96                 return AST_MODULE_LOAD_DECLINE;
97         } else {
98                 return AST_MODULE_LOAD_SUCCESS;
99         }
100 }
101
102 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generate an User-Defined CEL event",
103         .support_level = AST_MODULE_SUPPORT_CORE,
104         .load = load_module,
105         .unload = unload_module,
106 );