2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005 - 2006, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \author Russell Bryant <russell@digium.com>
23 * \brief Originate calls via the CLI
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/frame.h"
42 /*! The timeout for originated calls, in seconds */
46 * \brief orginate a call from the CLI
47 * \param fd file descriptor for cli
48 * \param chan channel to create type/data
49 * \param app application you want to run
50 * \param appdata data for application
51 * \retval CLI_SUCCESS on success.
52 * \retval CLI_SHOWUSAGE on failure.
54 static char *orig_app(int fd, const char *chan, const char *app, const char *appdata)
59 struct ast_format_cap *cap;
60 struct ast_format tmpfmt;
62 if (ast_strlen_zero(app))
65 chandata = ast_strdupa(chan);
67 chantech = strsep(&chandata, "/");
69 ast_cli(fd, "*** No data provided after channel type! ***\n");
73 if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
76 ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
77 ast_pbx_outgoing_app(chantech, cap, chandata, TIMEOUT * 1000, app, appdata, &reason, 0, NULL, NULL, NULL, NULL, NULL);
78 cap = ast_format_cap_destroy(cap);
84 * \brief orginate from extension
85 * \param fd file descriptor for cli
86 * \param chan channel to create type/data
87 * \param data contains exten\@context
88 * \retval CLI_SUCCESS on success.
89 * \retval CLI_SHOWUSAGE on failure.
91 static char *orig_exten(int fd, const char *chan, const char *data)
98 struct ast_format_cap *cap;
99 struct ast_format tmpfmt;
101 chandata = ast_strdupa(chan);
103 chantech = strsep(&chandata, "/");
105 ast_cli(fd, "*** No data provided after channel type! ***\n");
106 return CLI_SHOWUSAGE;
109 if (!ast_strlen_zero(data)) {
110 context = ast_strdupa(data);
111 exten = strsep(&context, "@");
114 if (ast_strlen_zero(exten))
116 if (ast_strlen_zero(context))
118 if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
121 ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
122 ast_pbx_outgoing_exten(chantech, cap, chandata, TIMEOUT * 1000, context, exten, 1, &reason, 0, NULL, NULL, NULL, NULL, NULL, 0);
123 cap = ast_format_cap_destroy(cap);
129 * \brief handle for orgination app or exten.
130 * \param e pointer to the CLI structure to initialize
131 * \param cmd operation to execute
132 * \param a structure that contains either application or extension arguments
133 * \retval CLI_SUCCESS on success.
134 * \retval CLI_SHOWUSAGE on failure.*/
135 static char *handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
137 static const char * const choices[] = { "application", "extension", NULL };
141 e->command = "channel originate";
143 " There are two ways to use this command. A call can be originated between a\n"
144 "channel and a specific application, or between a channel and an extension in\n"
145 "the dialplan. This is similar to call files or the manager originate action.\n"
146 "Calls originated with this command are given a timeout of 30 seconds.\n\n"
148 "Usage1: channel originate <tech/data> application <appname> [appdata]\n"
149 " This will originate a call between the specified channel tech/data and the\n"
150 "given application. Arguments to the application are optional. If the given\n"
151 "arguments to the application include spaces, all of the arguments to the\n"
152 "application need to be placed in quotation marks.\n\n"
154 "Usage2: channel originate <tech/data> extension [exten@][context]\n"
155 " This will originate a call between the specified channel tech/data and the\n"
156 "given extension. If no context is specified, the 'default' context will be\n"
157 "used. If no extension is given, the 's' extension will be used.\n";
160 /* ugly, can be removed when CLI entries have ast_module pointers */
161 ast_module_ref(ast_module_info->self);
163 res = ast_cli_complete(a->word, choices, a->n);
164 } else if (a->pos == 4) {
165 if (!strcasecmp("application", a->argv[3])) {
166 res = ast_complete_applications(a->line, a->word, a->n);
169 ast_module_unref(ast_module_info->self);
173 if (ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3]))
174 return CLI_SHOWUSAGE;
176 /* ugly, can be removed when CLI entries have ast_module pointers */
177 ast_module_ref(ast_module_info->self);
179 if (!strcasecmp("application", a->argv[3])) {
180 res = orig_app(a->fd, a->argv[2], a->argv[4], a->argv[5]);
181 } else if (!strcasecmp("extension", a->argv[3])) {
182 res = orig_exten(a->fd, a->argv[2], a->argv[4]);
187 ast_module_unref(ast_module_info->self);
192 static char *handle_redirect(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
194 const char *name, *dest;
195 struct ast_channel *chan;
200 e->command = "channel redirect";
202 "Usage: channel redirect <channel> <[[context,]exten,]priority>\n"
203 " Redirect an active channel to a specified extension.\n";
204 /*! \todo It would be nice to be able to redirect 2 channels at the same
205 * time like you can with AMI redirect. However, it is not possible to acquire
206 * two channels without the potential for a deadlock with how ast_channel structs
207 * are managed today. Once ast_channel is a refcounted object, this command
208 * will be able to support that. */
211 return ast_complete_channels(a->line, a->word, a->pos, a->n, 2);
214 if (a->argc != e->args + 2) {
215 return CLI_SHOWUSAGE;
221 if (!(chan = ast_channel_get_by_name(name))) {
222 ast_cli(a->fd, "Channel '%s' not found\n", name);
226 res = ast_async_parseable_goto(chan, dest);
228 chan = ast_channel_unref(chan);
231 ast_cli(a->fd, "Channel '%s' successfully redirected to %s\n", name, dest);
233 ast_cli(a->fd, "Channel '%s' failed to be redirected to %s\n", name, dest);
236 return res ? CLI_FAILURE : CLI_SUCCESS;
239 static struct ast_cli_entry cli_cliorig[] = {
240 AST_CLI_DEFINE(handle_orig, "Originate a call"),
241 AST_CLI_DEFINE(handle_redirect, "Redirect a call"),
244 static int unload_module(void)
246 return ast_cli_unregister_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
249 static int load_module(void)
252 res = ast_cli_register_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
253 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
256 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination and redirection from the CLI");