| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include "asterisk/file.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/pbx.h" |
| 41 |
#include "asterisk/module.h" |
| 42 |
#include "asterisk/app.h" |
| 43 |
#include "asterisk/format_cache.h" |
| 44 |
|
| 45 |
static const char app_originate[] = "Originate"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
enum { |
| 146 |
OPT_PREDIAL_CALLEE = (1 << 0), |
| 147 |
OPT_PREDIAL_CALLER = (1 << 1), |
| 148 |
OPT_ASYNC = (1 << 2), |
| 149 |
OPT_CALLER_NUM = (1 << 3), |
| 150 |
OPT_CALLER_NAME = (1 << 4), |
| 151 |
OPT_CODECS = (1 << 5), |
| 152 |
OPT_VARIABLES = (1 << 6), |
| 153 |
}; |
| 154 |
|
| 155 |
enum { |
| 156 |
OPT_ARG_PREDIAL_CALLEE, |
| 157 |
OPT_ARG_PREDIAL_CALLER, |
| 158 |
OPT_ARG_CALLER_NUM, |
| 159 |
OPT_ARG_CALLER_NAME, |
| 160 |
OPT_ARG_CODECS, |
| 161 |
OPT_ARG_VARIABLES, |
| 162 |
|
| 163 |
OPT_ARG_ARRAY_SIZE, |
| 164 |
}; |
| 165 |
|
| 166 |
AST_APP_OPTIONS(originate_exec_options, BEGIN_OPTIONS |
| 167 |
AST_APP_OPTION('a', OPT_ASYNC), |
| 168 |
AST_APP_OPTION_ARG('b', OPT_PREDIAL_CALLEE, OPT_ARG_PREDIAL_CALLEE), |
| 169 |
AST_APP_OPTION_ARG('B', OPT_PREDIAL_CALLER, OPT_ARG_PREDIAL_CALLER), |
| 170 |
AST_APP_OPTION_ARG('C', OPT_CODECS, OPT_ARG_CODECS), |
| 171 |
AST_APP_OPTION_ARG('c', OPT_CALLER_NUM, OPT_ARG_CALLER_NUM), |
| 172 |
AST_APP_OPTION_ARG('n', OPT_CALLER_NAME, OPT_ARG_CALLER_NAME), |
| 173 |
AST_APP_OPTION_ARG('v', OPT_VARIABLES, OPT_ARG_VARIABLES), |
| 174 |
END_OPTIONS ); |
| 175 |
|
| 176 |
static int originate_exec(struct ast_channel *chan, const char *data) |
| 177 |
{ |
| 178 |
AST_DECLARE_APP_ARGS(args, |
| 179 |
AST_APP_ARG(tech_data); |
| 180 |
AST_APP_ARG(type); |
| 181 |
AST_APP_ARG(arg1); |
| 182 |
AST_APP_ARG(arg2); |
| 183 |
AST_APP_ARG(arg3); |
| 184 |
AST_APP_ARG(timeout); |
| 185 |
AST_APP_ARG(options); |
| 186 |
); |
| 187 |
struct ast_flags64 opts = { 0, }; |
| 188 |
char *opt_args[OPT_ARG_ARRAY_SIZE]; |
| 189 |
char *predial_callee = NULL; |
| 190 |
char *parse, *cnum = NULL, *cname = NULL; |
| 191 |
|
| 192 |
struct ast_variable *vars = NULL; |
| 193 |
char *chantech, *chandata; |
| 194 |
int res = -1; |
| 195 |
int continue_in_dialplan = 0; |
| 196 |
int outgoing_status = 0; |
| 197 |
unsigned int timeout = 30; |
| 198 |
static const char default_exten[] = "s"; |
| 199 |
struct ast_format_cap *capabilities; |
| 200 |
capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 201 |
|
| 202 |
ast_autoservice_start(chan); |
| 203 |
if (!capabilities) { |
| 204 |
goto return_cleanup; |
| 205 |
} |
| 206 |
|
| 207 |
ast_format_cap_append(capabilities, ast_format_slin, 0); |
| 208 |
|
| 209 |
if (ast_strlen_zero(data)) { |
| 210 |
ast_log(LOG_ERROR, "Originate() requires arguments\n"); |
| 211 |
goto return_cleanup; |
| 212 |
} |
| 213 |
|
| 214 |
parse = ast_strdupa(data); |
| 215 |
|
| 216 |
AST_STANDARD_APP_ARGS(args, parse); |
| 217 |
|
| 218 |
if (args.argc < 3) { |
| 219 |
ast_log(LOG_ERROR, "Incorrect number of arguments\n"); |
| 220 |
goto return_cleanup; |
| 221 |
} |
| 222 |
|
| 223 |
if (!ast_strlen_zero(args.timeout)) { |
| 224 |
if(sscanf(args.timeout, "%u", &timeout) != 1) { |
| 225 |
ast_log(LOG_NOTICE, "Invalid timeout: '%s'. Setting timeout to 30 seconds\n", args.timeout); |
| 226 |
timeout = 30; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
chandata = ast_strdupa(args.tech_data); |
| 231 |
chantech = strsep(&chandata, "/"); |
| 232 |
|
| 233 |
if (ast_strlen_zero(chandata) || ast_strlen_zero(chantech)) { |
| 234 |
ast_log(LOG_ERROR, "Channel Tech/Data invalid: '%s'\n", args.tech_data); |
| 235 |
goto return_cleanup; |
| 236 |
} |
| 237 |
|
| 238 |
if (!ast_strlen_zero(args.options) && |
| 239 |
ast_app_parse_options64(originate_exec_options, &opts, opt_args, args.options)) { |
| 240 |
ast_log(LOG_ERROR, "Invalid options: '%s'\n", args.options); |
| 241 |
goto return_cleanup; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
if (ast_test_flag64(&opts, OPT_PREDIAL_CALLER) |
| 246 |
&& !ast_strlen_zero(opt_args[OPT_ARG_PREDIAL_CALLER])) { |
| 247 |
ast_replace_subargument_delimiter(opt_args[OPT_ARG_PREDIAL_CALLER]); |
| 248 |
ast_app_exec_sub(NULL, chan, opt_args[OPT_ARG_PREDIAL_CALLER], 0); |
| 249 |
} |
| 250 |
|
| 251 |
if (ast_test_flag64(&opts, OPT_PREDIAL_CALLEE) |
| 252 |
&& !ast_strlen_zero(opt_args[OPT_ARG_PREDIAL_CALLEE])) { |
| 253 |
ast_replace_subargument_delimiter(opt_args[OPT_ARG_PREDIAL_CALLEE]); |
| 254 |
predial_callee = opt_args[OPT_ARG_PREDIAL_CALLEE]; |
| 255 |
} |
| 256 |
|
| 257 |
if (strcasecmp(args.type, "exten") && strcasecmp(args.type, "app")) { |
| 258 |
ast_log(LOG_ERROR, "Incorrect type, it should be 'exten' or 'app': %s\n", |
| 259 |
args.type); |
| 260 |
goto return_cleanup; |
| 261 |
} |
| 262 |
|
| 263 |
if (ast_test_flag64(&opts, OPT_CODECS)) { |
| 264 |
if (!ast_strlen_zero(opt_args[OPT_ARG_CODECS])) { |
| 265 |
ast_format_cap_remove_by_type(capabilities, AST_MEDIA_TYPE_UNKNOWN); |
| 266 |
ast_format_cap_update_by_allow_disallow(capabilities, opt_args[OPT_ARG_CODECS], 1); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (ast_test_flag64(&opts, OPT_CALLER_NUM)) { |
| 271 |
if (!ast_strlen_zero(opt_args[OPT_ARG_CALLER_NUM])) { |
| 272 |
cnum = opt_args[OPT_ARG_CALLER_NUM]; |
| 273 |
} else if (ast_channel_caller(chan)->id.number.str) { |
| 274 |
cnum = ast_channel_caller(chan)->id.number.str; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (ast_test_flag64(&opts, OPT_CALLER_NAME)) { |
| 279 |
if (!ast_strlen_zero(opt_args[OPT_ARG_CALLER_NAME])) { |
| 280 |
cname = opt_args[OPT_ARG_CALLER_NAME]; |
| 281 |
} else if (ast_channel_caller(chan)->id.name.str) { |
| 282 |
cname = ast_channel_caller(chan)->id.name.str; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
if (ast_test_flag64(&opts, OPT_VARIABLES) |
| 288 |
&& !ast_strlen_zero(opt_args[OPT_ARG_VARIABLES])) { |
| 289 |
char *vartext; |
| 290 |
char *text = opt_args[OPT_ARG_VARIABLES]; |
| 291 |
while ((vartext = ast_strsep(&text, '^', 0))) { |
| 292 |
struct ast_variable *var; |
| 293 |
char *varname, *varvalue; |
| 294 |
if (!(varname = ast_strsep(&vartext, '=', 0))) { |
| 295 |
ast_log(LOG_ERROR, "Variable syntax error: %s\n", vartext); |
| 296 |
goto return_cleanup; |
| 297 |
} |
| 298 |
if (!(varvalue = ast_strsep(&vartext, '=', 0))) { |
| 299 |
varvalue = ""; |
| 300 |
} |
| 301 |
var = ast_variable_new(varname, varvalue, ""); |
| 302 |
if (!var) { |
| 303 |
ast_log(LOG_ERROR, "Failed to allocate variable: %s\n", varname); |
| 304 |
goto return_cleanup; |
| 305 |
} |
| 306 |
ast_debug(1, "Appending variable '%s' with value '%s'", varname, varvalue); |
| 307 |
ast_variable_list_append(&vars, var); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if (!strcasecmp(args.type, "exten")) { |
| 312 |
const char *cid_num = cnum; |
| 313 |
const char *cid_name = cname; |
| 314 |
int priority = 1; |
| 315 |
const char *exten = args.arg2; |
| 316 |
|
| 317 |
if (args.argc == 5) { |
| 318 |
|
| 319 |
if (sscanf(args.arg3, "%30d", &priority) != 1) { |
| 320 |
ast_log(LOG_ERROR, "Invalid priority: '%s'\n", args.arg3); |
| 321 |
goto return_cleanup; |
| 322 |
} |
| 323 |
} else if (args.argc == 3) { |
| 324 |
|
| 325 |
exten = default_exten; |
| 326 |
} |
| 327 |
|
| 328 |
ast_debug(1, "Originating call to '%s/%s' and connecting them to extension %s,%s,%d\n", |
| 329 |
chantech, chandata, args.arg1, exten, priority); |
| 330 |
|
| 331 |
res = ast_pbx_outgoing_exten_predial(chantech, capabilities, chandata, |
| 332 |
timeout * 1000, args.arg1, exten, priority, &outgoing_status, |
| 333 |
ast_test_flag64(&opts, OPT_ASYNC) ? AST_OUTGOING_NO_WAIT : AST_OUTGOING_WAIT, |
| 334 |
cid_num, cid_name, vars, NULL, NULL, 0, NULL, |
| 335 |
predial_callee); |
| 336 |
} else { |
| 337 |
const char *cid_num = cnum; |
| 338 |
const char *cid_name = cname; |
| 339 |
ast_debug(1, "Originating call to '%s/%s' and connecting them to %s(%s)\n", |
| 340 |
chantech, chandata, args.arg1, S_OR(args.arg2, "")); |
| 341 |
|
| 342 |
res = ast_pbx_outgoing_app_predial(chantech, capabilities, chandata, |
| 343 |
timeout * 1000, args.arg1, args.arg2, &outgoing_status, |
| 344 |
ast_test_flag64(&opts, OPT_ASYNC) ? AST_OUTGOING_NO_WAIT : AST_OUTGOING_WAIT, |
| 345 |
cid_num, cid_name, vars, NULL, NULL, NULL, |
| 346 |
predial_callee); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (res && outgoing_status) { |
| 356 |
res = 0; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
continue_in_dialplan = 1; |
| 361 |
|
| 362 |
return_cleanup: |
| 363 |
if (res) { |
| 364 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "FAILED"); |
| 365 |
} else { |
| 366 |
switch (outgoing_status) { |
| 367 |
case 0: |
| 368 |
case AST_CONTROL_ANSWER: |
| 369 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "SUCCESS"); |
| 370 |
break; |
| 371 |
case AST_CONTROL_BUSY: |
| 372 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "BUSY"); |
| 373 |
break; |
| 374 |
case AST_CONTROL_CONGESTION: |
| 375 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "CONGESTION"); |
| 376 |
break; |
| 377 |
case AST_CONTROL_HANGUP: |
| 378 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "HANGUP"); |
| 379 |
break; |
| 380 |
case AST_CONTROL_RINGING: |
| 381 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "RINGING"); |
| 382 |
break; |
| 383 |
default: |
| 384 |
ast_log(LOG_WARNING, "Unknown originate status result of '%d'\n", |
| 385 |
outgoing_status); |
| 386 |
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "UNKNOWN"); |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
if (vars) { |
| 391 |
ast_variables_destroy(vars); |
| 392 |
} |
| 393 |
ao2_cleanup(capabilities); |
| 394 |
ast_autoservice_stop(chan); |
| 395 |
|
| 396 |
return continue_in_dialplan ? 0 : -1; |
| 397 |
} |
| 398 |
|
| 399 |
static int unload_module(void) |
| 400 |
{ |
| 401 |
return ast_unregister_application(app_originate); |
| 402 |
} |
| 403 |
|
| 404 |
static int load_module(void) |
| 405 |
{ |
| 406 |
int res; |
| 407 |
|
| 408 |
res = ast_register_application_xml(app_originate, originate_exec); |
| 409 |
|
| 410 |
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 411 |
} |
| 412 |
|
| 413 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Originate call"); |