Sat Aug 6 00:39:21 2011

Asterisk developer's documentation


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 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 253349 $")
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <unistd.h>
00032 
00033 #include "asterisk/lock.h"
00034 #include "asterisk/file.h"
00035 #include "asterisk/logger.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/manager.h"
00040 #include "asterisk/app.h"
00041 
00042 static char *app = "UserEvent";
00043 
00044 static char *synopsis = "Send an arbitrary event to the manager interface";
00045 
00046 static char *descrip = 
00047 "  UserEvent(eventname[|body]): Sends an arbitrary event to the manager\n"
00048 "interface, with an optional body representing additional arguments.  The\n"
00049 "body may be specified as a | delimited list of headers. Each additional\n"
00050 "argument will be placed on a new line in the event. The format of the\n"
00051 "event will be:\n"
00052 "    Event: UserEvent\n"
00053 "    UserEvent: <specified event name>\n"
00054 "    [body]\n"
00055 "If no body is specified, only Event and UserEvent headers will be present.\n";
00056 
00057 
00058 static int userevent_exec(struct ast_channel *chan, void *data)
00059 {
00060    struct ast_module_user *u;
00061    char *parse, buf[2048] = "";
00062    int x, buflen = 0, xlen;
00063    AST_DECLARE_APP_ARGS(args,
00064       AST_APP_ARG(eventname);
00065       AST_APP_ARG(extra)[100];
00066    );
00067 
00068    if (ast_strlen_zero(data)) {
00069       ast_log(LOG_WARNING, "UserEvent requires an argument (eventname|optional event body)\n");
00070       return -1;
00071    }
00072 
00073    u = ast_module_user_add(chan);
00074 
00075    parse = ast_strdupa(data);
00076 
00077    AST_STANDARD_APP_ARGS(args, parse);
00078 
00079    for (x = 0; x < args.argc - 1; x++) {
00080       /* Stop once a header comes up that exceeds our buffer. */
00081       if (sizeof(buf) <= buflen + (xlen = strlen(args.extra[x])) + 3) {
00082          ast_log(LOG_WARNING, "UserEvent exceeds our buffer length!  Truncating.\n");
00083          break;
00084       }
00085       ast_copy_string(buf + buflen, args.extra[x], sizeof(buf) - buflen - 3);
00086       buflen += xlen;
00087       ast_copy_string(buf + buflen, "\r\n", 3);
00088       buflen += 2;
00089    }
00090 
00091    manager_event(EVENT_FLAG_USER, "UserEvent", "UserEvent: %s\r\n%s", args.eventname, buf);
00092 
00093    ast_module_user_remove(u);
00094 
00095    return 0;
00096 }
00097 
00098 static int unload_module(void)
00099 {
00100    int res;
00101 
00102    res = ast_unregister_application(app);
00103 
00104    ast_module_user_hangup_all();
00105 
00106    return res; 
00107 }
00108 
00109 static int load_module(void)
00110 {
00111    return ast_register_application(app, userevent_exec, synopsis, descrip);
00112 }
00113 
00114 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");

Generated on Sat Aug 6 00:39:21 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7