(closes issue #12846)
[asterisk-bristuff.git] / apps / app_channelredirect.c
bloba8eedf9b4e03bc9447eb142d61f54ba92e9915c0
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Sergey Basmanov
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 ChannelRedirect application
21 * \author Sergey Basmanov <sergey_basmanov@mail.ru>
23 * \ingroup applications
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/app.h"
42 #include "asterisk/features.h"
43 #include "asterisk/options.h"
45 static char *app = "ChannelRedirect";
46 static char *synopsis = "Redirects given channel to a dialplan target.";
47 static char *descrip =
48 "ChannelRedirect(channel|[[context|]extension|]priority):\n"
49 " Sends the specified channel to the specified extension priority\n";
52 static int asyncgoto_exec(struct ast_channel *chan, void *data)
54 int res = -1;
55 struct ast_module_user *u;
56 char *info, *context, *exten, *priority;
57 int prio = 1;
58 struct ast_channel *chan2 = NULL;
60 AST_DECLARE_APP_ARGS(args,
61 AST_APP_ARG(channel);
62 AST_APP_ARG(label);
65 if (ast_strlen_zero(data)) {
66 ast_log(LOG_WARNING, "%s requires an argument (channel|[[context|]exten|]priority)\n", app);
67 return -1;
70 u = ast_module_user_add(chan);
72 info = ast_strdupa(data);
73 AST_STANDARD_APP_ARGS(args, info);
75 if (ast_strlen_zero(args.channel) || ast_strlen_zero(args.label)) {
76 ast_log(LOG_WARNING, "%s requires an argument (channel|[[context|]exten|]priority)\n", app);
77 goto quit;
80 chan2 = ast_get_channel_by_name_locked(args.channel);
81 if (!chan2) {
82 ast_log(LOG_WARNING, "No such channel: %s\n", args.channel);
83 goto quit;
86 /* Parsed right to left, so standard parsing won't work */
87 context = strsep(&args.label, "|");
88 exten = strsep(&args.label, "|");
89 if (exten) {
90 priority = strsep(&args.label, "|");
91 if (!priority) {
92 priority = exten;
93 exten = context;
94 context = NULL;
96 } else {
97 priority = context;
98 context = NULL;
101 /* ast_findlabel_extension does not convert numeric priorities; it only does a lookup */
102 if (!(prio = atoi(priority)) && !(prio = ast_findlabel_extension(chan2, S_OR(context, chan2->context),
103 S_OR(exten, chan2->exten), priority, chan2->cid.cid_num))) {
104 ast_log(LOG_WARNING, "'%s' is not a known priority or label\n", priority);
105 goto chanquit;
108 if (option_debug > 1)
109 ast_log(LOG_DEBUG, "Attempting async goto (%s) to %s|%s|%d\n", args.channel, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio);
111 if (ast_async_goto_if_exists(chan2, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio))
112 ast_log(LOG_WARNING, "%s failed for %s\n", app, args.channel);
113 else
114 res = 0;
116 chanquit:
117 ast_mutex_unlock(&chan2->lock);
118 quit:
119 ast_module_user_remove(u);
121 return res;
124 static int unload_module(void)
126 int res;
128 res = ast_unregister_application(app);
130 ast_module_user_hangup_all();
132 return res;
135 static int load_module(void)
137 return ast_register_application(app, asyncgoto_exec, synopsis, descrip);
140 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel Redirect");