app_userevent.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief UserEvent application -- send manager event
00020  * 
00021  * \ingroup applications
00022  */
00023 
00024 /*** MODULEINFO
00025    <support_level>core</support_level>
00026  ***/
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 414406 $")
00031 
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/module.h"
00034 #include "asterisk/manager.h"
00035 #include "asterisk/app.h"
00036 #include "asterisk/json.h"
00037 #include "asterisk/stasis_channels.h"
00038 
00039 /*** DOCUMENTATION
00040    <application name="UserEvent" language="en_US">
00041       <synopsis>
00042          Send an arbitrary user-defined event to parties interested in a channel (AMI users and relevant res_stasis applications).
00043       </synopsis>
00044       <syntax>
00045          <parameter name="eventname" required="true" />
00046          <parameter name="body" />
00047       </syntax>
00048       <description>
00049          <para>Sends an arbitrary event to interested parties, with an optional
00050          <replaceable>body</replaceable> representing additional arguments. The
00051          <replaceable>body</replaceable> may be specified as
00052          a <literal>,</literal> delimited list of key:value pairs.</para>
00053          <para>For AMI, each additional argument will be placed on a new line in
00054          the event and the format of the event will be:</para>
00055          <para>    Event: UserEvent</para>
00056          <para>    UserEvent: &lt;specified event name&gt;</para>
00057          <para>    [body]</para>
00058          <para>If no <replaceable>body</replaceable> is specified, only Event and
00059          UserEvent headers will be present.</para>
00060          <para>For res_stasis applications, the event will be provided as a JSON
00061          blob with additional arguments appearing as keys in the object and the
00062          <replaceable>eventname</replaceable> under the
00063          <literal>eventname</literal> key.</para>
00064       </description>
00065    </application>
00066  ***/
00067 
00068 static char *app = "UserEvent";
00069 
00070 static int userevent_exec(struct ast_channel *chan, const char *data)
00071 {
00072    char *parse;
00073    int x;
00074    AST_DECLARE_APP_ARGS(args,
00075       AST_APP_ARG(eventname);
00076       AST_APP_ARG(extra)[100];
00077    );
00078    RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
00079 
00080    if (ast_strlen_zero(data)) {
00081       ast_log(LOG_WARNING, "UserEvent requires an argument (eventname,optional event body)\n");
00082       return -1;
00083    }
00084 
00085    parse = ast_strdupa(data);
00086 
00087    AST_STANDARD_APP_ARGS(args, parse);
00088 
00089    blob = ast_json_pack("{s: s}",
00090               "eventname", args.eventname);
00091    if (!blob) {
00092       return -1;
00093    }
00094 
00095    for (x = 0; x < args.argc - 1; x++) {
00096       char *key, *value = args.extra[x];
00097       struct ast_json *json_value;
00098 
00099       key = strsep(&value, ":");
00100       if (!value) {
00101          /* no ':' in string? */
00102          continue;
00103       }
00104 
00105       value = ast_strip(value);
00106       json_value = ast_json_string_create(value);
00107       if (!json_value) {
00108          return -1;
00109       }
00110 
00111       /* ref stolen by ast_json_object_set */
00112       if (ast_json_object_set(blob, key, json_value)) {
00113          return -1;
00114       }
00115    }
00116 
00117    ast_channel_lock(chan);
00118    ast_multi_object_blob_single_channel_publish(chan, ast_multi_user_event_type(), blob);
00119    ast_channel_unlock(chan);
00120    return 0;
00121 }
00122 
00123 static int unload_module(void)
00124 {
00125    return ast_unregister_application(app);
00126 }
00127 
00128 static int load_module(void)
00129 {
00130    return ast_register_application_xml(app, userevent_exec);
00131 }
00132 
00133 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");

Generated on Thu Apr 16 06:27:12 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6