| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/channel.h" |
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/cli.h" |
| 37 |
#include "asterisk/utils.h" |
| 38 |
#include "asterisk/frame.h" |
| 39 |
#include "asterisk/format_cache.h" |
| 40 |
|
| 41 |
|
| 42 |
#define TIMEOUT 30 |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static char *orig_app(int fd, const char *chan, const char *app, const char *appdata) |
| 54 |
{ |
| 55 |
char *chantech; |
| 56 |
char *chandata; |
| 57 |
int reason = 0; |
| 58 |
struct ast_format_cap *cap; |
| 59 |
|
| 60 |
if (ast_strlen_zero(app)) |
| 61 |
return CLI_SHOWUSAGE; |
| 62 |
|
| 63 |
chandata = ast_strdupa(chan); |
| 64 |
|
| 65 |
chantech = strsep(&chandata, "/"); |
| 66 |
if (!chandata) { |
| 67 |
ast_cli(fd, "*** No data provided after channel type! ***\n"); |
| 68 |
return CLI_SHOWUSAGE; |
| 69 |
} |
| 70 |
|
| 71 |
if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 72 |
return CLI_FAILURE; |
| 73 |
} |
| 74 |
ast_format_cap_append(cap, ast_format_slin, 0); |
| 75 |
ast_pbx_outgoing_app(chantech, cap, chandata, TIMEOUT * 1000, app, appdata, |
| 76 |
&reason, AST_OUTGOING_NO_WAIT, NULL, NULL, NULL, NULL, |
| 77 |
NULL, NULL); |
| 78 |
ao2_ref(cap, -1); |
| 79 |
|
| 80 |
return CLI_SUCCESS; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static char *orig_exten(int fd, const char *chan, const char *data) |
| 92 |
{ |
| 93 |
char *chantech; |
| 94 |
char *chandata; |
| 95 |
char *exten = NULL; |
| 96 |
char *context = NULL; |
| 97 |
int reason = 0; |
| 98 |
struct ast_format_cap *cap; |
| 99 |
|
| 100 |
chandata = ast_strdupa(chan); |
| 101 |
|
| 102 |
chantech = strsep(&chandata, "/"); |
| 103 |
if (!chandata) { |
| 104 |
ast_cli(fd, "*** No data provided after channel type! ***\n"); |
| 105 |
return CLI_SHOWUSAGE; |
| 106 |
} |
| 107 |
|
| 108 |
if (!ast_strlen_zero(data)) { |
| 109 |
context = ast_strdupa(data); |
| 110 |
exten = strsep(&context, "@"); |
| 111 |
} |
| 112 |
|
| 113 |
if (ast_strlen_zero(exten)) |
| 114 |
exten = "s"; |
| 115 |
if (ast_strlen_zero(context)) |
| 116 |
context = "default"; |
| 117 |
if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 118 |
return CLI_FAILURE; |
| 119 |
} |
| 120 |
ast_format_cap_append(cap, ast_format_slin, 0); |
| 121 |
ast_pbx_outgoing_exten(chantech, cap, chandata, TIMEOUT * 1000, context, |
| 122 |
exten, 1, &reason, AST_OUTGOING_NO_WAIT, NULL, NULL, |
| 123 |
NULL, NULL, NULL, 0, NULL); |
| 124 |
ao2_ref(cap, -1); |
| 125 |
|
| 126 |
return CLI_SUCCESS; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static char *handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 137 |
{ |
| 138 |
static const char * const choices[] = { "application", "extension", NULL }; |
| 139 |
char *res = NULL; |
| 140 |
switch (cmd) { |
| 141 |
case CLI_INIT: |
| 142 |
e->command = "channel originate"; |
| 143 |
e->usage = |
| 144 |
" There are two ways to use this command. A call can be originated between a\n" |
| 145 |
"channel and a specific application, or between a channel and an extension in\n" |
| 146 |
"the dialplan. This is similar to call files or the manager originate action.\n" |
| 147 |
"Calls originated with this command are given a timeout of 30 seconds.\n\n" |
| 148 |
|
| 149 |
"Usage1: channel originate <tech/data> application <appname> [appdata]\n" |
| 150 |
" This will originate a call between the specified channel tech/data and the\n" |
| 151 |
"given application. Arguments to the application are optional. If the given\n" |
| 152 |
"arguments to the application include spaces, all of the arguments to the\n" |
| 153 |
"application need to be placed in quotation marks.\n\n" |
| 154 |
|
| 155 |
"Usage2: channel originate <tech/data> extension [exten@][context]\n" |
| 156 |
" This will originate a call between the specified channel tech/data and the\n" |
| 157 |
"given extension. If no context is specified, the 'default' context will be\n" |
| 158 |
"used. If no extension is given, the 's' extension will be used.\n"; |
| 159 |
return NULL; |
| 160 |
case CLI_GENERATE: |
| 161 |
if (a->pos == 3) { |
| 162 |
res = ast_cli_complete(a->word, choices, a->n); |
| 163 |
} else if (a->pos == 4) { |
| 164 |
if (!strcasecmp("application", a->argv[3])) { |
| 165 |
res = ast_complete_applications(a->line, a->word, a->n); |
| 166 |
} |
| 167 |
} |
| 168 |
return res; |
| 169 |
} |
| 170 |
|
| 171 |
if (ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) |
| 172 |
return CLI_SHOWUSAGE; |
| 173 |
|
| 174 |
if (!strcasecmp("application", a->argv[3])) { |
| 175 |
res = orig_app(a->fd, a->argv[2], a->argv[4], a->argv[5]); |
| 176 |
} else if (!strcasecmp("extension", a->argv[3])) { |
| 177 |
res = orig_exten(a->fd, a->argv[2], a->argv[4]); |
| 178 |
} else { |
| 179 |
res = CLI_SHOWUSAGE; |
| 180 |
} |
| 181 |
|
| 182 |
return res; |
| 183 |
} |
| 184 |
|
| 185 |
static char *handle_redirect(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 186 |
{ |
| 187 |
const char *name, *dest; |
| 188 |
struct ast_channel *chan; |
| 189 |
int res; |
| 190 |
|
| 191 |
switch (cmd) { |
| 192 |
case CLI_INIT: |
| 193 |
e->command = "channel redirect"; |
| 194 |
e->usage = "" |
| 195 |
"Usage: channel redirect <channel> <[[context,]exten,]priority>\n" |
| 196 |
" Redirect an active channel to a specified extension.\n"; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
return NULL; |
| 203 |
case CLI_GENERATE: |
| 204 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 2); |
| 205 |
} |
| 206 |
|
| 207 |
if (a->argc != e->args + 2) { |
| 208 |
return CLI_SHOWUSAGE; |
| 209 |
} |
| 210 |
|
| 211 |
name = a->argv[2]; |
| 212 |
dest = a->argv[3]; |
| 213 |
|
| 214 |
if (!(chan = ast_channel_get_by_name(name))) { |
| 215 |
ast_cli(a->fd, "Channel '%s' not found\n", name); |
| 216 |
return CLI_FAILURE; |
| 217 |
} |
| 218 |
|
| 219 |
res = ast_async_parseable_goto(chan, dest); |
| 220 |
|
| 221 |
chan = ast_channel_unref(chan); |
| 222 |
|
| 223 |
if (!res) { |
| 224 |
ast_cli(a->fd, "Channel '%s' successfully redirected to %s\n", name, dest); |
| 225 |
} else { |
| 226 |
ast_cli(a->fd, "Channel '%s' failed to be redirected to %s\n", name, dest); |
| 227 |
} |
| 228 |
|
| 229 |
return res ? CLI_FAILURE : CLI_SUCCESS; |
| 230 |
} |
| 231 |
|
| 232 |
static struct ast_cli_entry cli_cliorig[] = { |
| 233 |
AST_CLI_DEFINE(handle_orig, "Originate a call"), |
| 234 |
AST_CLI_DEFINE(handle_redirect, "Redirect a call"), |
| 235 |
}; |
| 236 |
|
| 237 |
static int unload_module(void) |
| 238 |
{ |
| 239 |
return ast_cli_unregister_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig)); |
| 240 |
} |
| 241 |
|
| 242 |
static int load_module(void) |
| 243 |
{ |
| 244 |
int res; |
| 245 |
res = ast_cli_register_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig)); |
| 246 |
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 247 |
} |
| 248 |
|
| 249 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination and redirection from the CLI"); |