Asterisk - The Open Source Telephony Project GIT-master-f36a736
app_userevent.c
Go to the documentation of this file.
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"
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
70static char *app = "UserEvent";
71
72static int userevent_exec(struct ast_channel *chan, const char *data)
73{
74 char *parse;
75 int x;
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
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
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);
121 ast_channel_unlock(chan);
122 return 0;
123}
124
125static int unload_module(void)
126{
128}
129
130static int load_module(void)
131{
133}
134
135AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");
static int userevent_exec(struct ast_channel *chan, const char *data)
Definition: app_userevent.c:72
static char * app
Definition: app_userevent.c:70
static int load_module(void)
static int unload_module(void)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ast_channel_lock(chan)
Definition: channel.h:2968
#define ast_channel_unlock(chan)
Definition: channel.h:2969
void ast_multi_object_blob_single_channel_publish(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
Create and publish a stasis message blob on a channel with it's snapshot.
Definition: stasis.c:2011
struct stasis_message_type * ast_multi_user_event_type(void)
Message type for custom user defined events with multi object blobs.
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
char * strsep(char **str, const char *delims)
#define LOG_WARNING
Asterisk JSON abstraction layer.
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:223
Main Channel structure associated with a channel.
Abstract JSON element (object, array, string, int, ...).
int value
Definition: syslog.c:37
const char * args
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941