(closes issue #12846)
[asterisk-bristuff.git] / apps / app_userevent.c
blobdf7bc58a79fabf436ed716f0be85c14a66dc7464
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
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.
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.
17 /*! \file
19 * \brief UserEvent application -- send manager event
21 * \ingroup applications
24 #include "asterisk.h"
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "asterisk/lock.h"
34 #include "asterisk/file.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/app.h"
42 static char *app = "UserEvent";
44 static char *synopsis = "Send an arbitrary event to the manager interface";
46 static char *descrip =
47 " UserEvent(eventname[|body]): Sends an arbitrary event to the manager\n"
48 "interface, with an optional body representing additional arguments. The\n"
49 "body may be specified as a | delimeted list of headers. Each additional\n"
50 "argument will be placed on a new line in the event. The format of the\n"
51 "event will be:\n"
52 " Event: UserEvent\n"
53 " UserEvent: <specified event name>\n"
54 " [body]\n"
55 "If no body is specified, only Event and UserEvent headers will be present.\n";
58 static int userevent_exec(struct ast_channel *chan, void *data)
60 struct ast_module_user *u;
61 char *parse, buf[2048] = "";
62 int x, buflen = 0;
63 AST_DECLARE_APP_ARGS(args,
64 AST_APP_ARG(eventname);
65 AST_APP_ARG(extra)[100];
68 if (ast_strlen_zero(data)) {
69 ast_log(LOG_WARNING, "UserEvent requires an argument (eventname|optional event body)\n");
70 return -1;
73 u = ast_module_user_add(chan);
75 parse = ast_strdupa(data);
77 AST_STANDARD_APP_ARGS(args, parse);
79 for (x = 0; x < args.argc - 1; x++) {
80 ast_copy_string(buf + buflen, args.extra[x], sizeof(buf) - buflen - 2);
81 buflen += strlen(args.extra[x]);
82 ast_copy_string(buf + buflen, "\r\n", 3);
83 buflen += 2;
86 manager_event(EVENT_FLAG_USER, "UserEvent", "UserEvent: %s\r\n%s", args.eventname, buf);
88 ast_module_user_remove(u);
90 return 0;
93 static int unload_module(void)
95 int res;
97 res = ast_unregister_application(app);
99 ast_module_user_hangup_all();
101 return res;
104 static int load_module(void)
106 return ast_register_application(app, userevent_exec, synopsis, descrip);
109 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Custom User Event Application");