| 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/module.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
static int isnull(struct ast_channel *chan, const char *cmd, char *data, |
| 178 |
char *buf, size_t len) |
| 179 |
{ |
| 180 |
strcpy(buf, data && *data ? "0" : "1"); |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, |
| 186 |
size_t len) |
| 187 |
{ |
| 188 |
strcpy(buf, data && *data ? "1" : "0"); |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf, |
| 194 |
size_t len) |
| 195 |
{ |
| 196 |
struct ast_timing timing; |
| 197 |
char *expr; |
| 198 |
char *iftrue; |
| 199 |
char *iffalse; |
| 200 |
|
| 201 |
data = ast_strip_quoted(data, "\"", "\""); |
| 202 |
expr = strsep(&data, "?"); |
| 203 |
iftrue = strsep(&data, ":"); |
| 204 |
iffalse = data; |
| 205 |
|
| 206 |
if (ast_strlen_zero(expr) || !(iftrue || iffalse)) { |
| 207 |
ast_log(LOG_WARNING, |
| 208 |
"Syntax IFTIME(<timespec>?[<true>][:<false>])\n"); |
| 209 |
return -1; |
| 210 |
} |
| 211 |
|
| 212 |
if (!ast_build_timing(&timing, expr)) { |
| 213 |
ast_log(LOG_WARNING, "Invalid Time Spec.\n"); |
| 214 |
ast_destroy_timing(&timing); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
|
| 218 |
if (iftrue) |
| 219 |
iftrue = ast_strip_quoted(iftrue, "\"", "\""); |
| 220 |
if (iffalse) |
| 221 |
iffalse = ast_strip_quoted(iffalse, "\"", "\""); |
| 222 |
|
| 223 |
ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len); |
| 224 |
ast_destroy_timing(&timing); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf, |
| 230 |
size_t len) |
| 231 |
{ |
| 232 |
AST_DECLARE_APP_ARGS(args1, |
| 233 |
AST_APP_ARG(expr); |
| 234 |
AST_APP_ARG(remainder); |
| 235 |
); |
| 236 |
AST_DECLARE_APP_ARGS(args2, |
| 237 |
AST_APP_ARG(iftrue); |
| 238 |
AST_APP_ARG(iffalse); |
| 239 |
); |
| 240 |
args2.iftrue = args2.iffalse = NULL; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
AST_NONSTANDARD_APP_ARGS(args1, data, '?'); |
| 248 |
AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':'); |
| 249 |
|
| 250 |
if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) { |
| 251 |
ast_debug(1, "<expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
|
| 255 |
args1.expr = ast_strip(args1.expr); |
| 256 |
if (args2.iftrue) |
| 257 |
args2.iftrue = ast_strip(args2.iftrue); |
| 258 |
if (args2.iffalse) |
| 259 |
args2.iffalse = ast_strip(args2.iffalse); |
| 260 |
|
| 261 |
ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len); |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, |
| 267 |
size_t len) |
| 268 |
{ |
| 269 |
char *varname; |
| 270 |
char *val; |
| 271 |
|
| 272 |
varname = strsep(&data, "="); |
| 273 |
val = data; |
| 274 |
|
| 275 |
if (ast_strlen_zero(varname) || !val) { |
| 276 |
ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n"); |
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
varname = ast_strip(varname); |
| 281 |
val = ast_strip(val); |
| 282 |
pbx_builtin_setvar_helper(chan, varname, val); |
| 283 |
ast_copy_string(buf, val, len); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len) |
| 289 |
{ |
| 290 |
if (len > -1) { |
| 291 |
ast_str_make_space(str, len == 0 ? strlen(data) : len); |
| 292 |
} |
| 293 |
return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str)); |
| 294 |
} |
| 295 |
|
| 296 |
static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len) |
| 297 |
{ |
| 298 |
AST_DECLARE_APP_ARGS(args, |
| 299 |
AST_APP_ARG(channel); |
| 300 |
AST_APP_ARG(varname); |
| 301 |
); |
| 302 |
AST_STANDARD_APP_ARGS(args, data); |
| 303 |
if (buf) { |
| 304 |
*buf = '\0'; |
| 305 |
} |
| 306 |
|
| 307 |
if (!ast_strlen_zero(args.varname)) { |
| 308 |
struct ast_channel *chan2; |
| 309 |
|
| 310 |
if ((chan2 = ast_channel_get_by_name(args.channel))) { |
| 311 |
char *s = ast_alloca(strlen(args.varname) + 4); |
| 312 |
sprintf(s, "${%s}", args.varname); |
| 313 |
ast_channel_lock(chan2); |
| 314 |
if (buf) { |
| 315 |
pbx_substitute_variables_helper(chan2, s, buf, len); |
| 316 |
} else { |
| 317 |
ast_str_substitute_variables(str, len, chan2, s); |
| 318 |
} |
| 319 |
ast_channel_unlock(chan2); |
| 320 |
chan2 = ast_channel_unref(chan2); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 328 |
{ |
| 329 |
return import_helper(chan, cmd, data, buf, NULL, len); |
| 330 |
} |
| 331 |
|
| 332 |
static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len) |
| 333 |
{ |
| 334 |
return import_helper(chan, cmd, data, NULL, str, len); |
| 335 |
} |
| 336 |
|
| 337 |
static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 338 |
{ |
| 339 |
pbx_builtin_setvar_helper(chan, data, NULL); |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data, |
| 345 |
char *buf, size_t len) |
| 346 |
{ |
| 347 |
const char *var = pbx_builtin_getvar_helper(chan, data); |
| 348 |
|
| 349 |
strcpy(buf, var ? "1" : "0"); |
| 350 |
|
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
static struct ast_custom_function isnull_function = { |
| 355 |
.name = "ISNULL", |
| 356 |
.read = isnull, |
| 357 |
.read_max = 2, |
| 358 |
}; |
| 359 |
|
| 360 |
static struct ast_custom_function set_function = { |
| 361 |
.name = "SET", |
| 362 |
.read = set, |
| 363 |
.read2 = set2, |
| 364 |
}; |
| 365 |
|
| 366 |
static struct ast_custom_function exists_function = { |
| 367 |
.name = "EXISTS", |
| 368 |
.read = exists, |
| 369 |
.read_max = 2, |
| 370 |
}; |
| 371 |
|
| 372 |
static struct ast_custom_function if_function = { |
| 373 |
.name = "IF", |
| 374 |
.read = acf_if, |
| 375 |
}; |
| 376 |
|
| 377 |
static struct ast_custom_function if_time_function = { |
| 378 |
.name = "IFTIME", |
| 379 |
.read = iftime, |
| 380 |
}; |
| 381 |
|
| 382 |
static struct ast_custom_function import_function = { |
| 383 |
.name = "IMPORT", |
| 384 |
.read = import_read, |
| 385 |
.read2 = import_read2, |
| 386 |
}; |
| 387 |
|
| 388 |
static struct ast_custom_function delete_function = { |
| 389 |
.name = "DELETE", |
| 390 |
.write = delete_write, |
| 391 |
}; |
| 392 |
|
| 393 |
static struct ast_custom_function variable_exists_function = { |
| 394 |
.name = "VARIABLE_EXISTS", |
| 395 |
.read = variable_exists_read, |
| 396 |
}; |
| 397 |
|
| 398 |
static int unload_module(void) |
| 399 |
{ |
| 400 |
int res = 0; |
| 401 |
|
| 402 |
res |= ast_custom_function_unregister(&isnull_function); |
| 403 |
res |= ast_custom_function_unregister(&set_function); |
| 404 |
res |= ast_custom_function_unregister(&exists_function); |
| 405 |
res |= ast_custom_function_unregister(&if_function); |
| 406 |
res |= ast_custom_function_unregister(&if_time_function); |
| 407 |
res |= ast_custom_function_unregister(&import_function); |
| 408 |
res |= ast_custom_function_unregister(&delete_function); |
| 409 |
res |= ast_custom_function_unregister(&variable_exists_function); |
| 410 |
|
| 411 |
return res; |
| 412 |
} |
| 413 |
|
| 414 |
static int load_module(void) |
| 415 |
{ |
| 416 |
int res = 0; |
| 417 |
|
| 418 |
res |= ast_custom_function_register(&isnull_function); |
| 419 |
res |= ast_custom_function_register(&set_function); |
| 420 |
res |= ast_custom_function_register(&exists_function); |
| 421 |
res |= ast_custom_function_register(&if_function); |
| 422 |
res |= ast_custom_function_register(&if_time_function); |
| 423 |
res |= ast_custom_function_register(&import_function); |
| 424 |
res |= ast_custom_function_register(&delete_function); |
| 425 |
res |= ast_custom_function_register(&variable_exists_function); |
| 426 |
|
| 427 |
return res; |
| 428 |
} |
| 429 |
|
| 430 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions"); |