| 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/file.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/module.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 |
struct sortable_keys { |
| 93 |
char *key; |
| 94 |
float value; |
| 95 |
}; |
| 96 |
|
| 97 |
static int sort_subroutine(const void *arg1, const void *arg2) |
| 98 |
{ |
| 99 |
const struct sortable_keys *one=arg1, *two=arg2; |
| 100 |
if (one->value < two->value) |
| 101 |
return -1; |
| 102 |
else if (one->value == two->value) |
| 103 |
return 0; |
| 104 |
else |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
#define ERROR_NOARG (-1) |
| 109 |
#define ERROR_NOMEM (-2) |
| 110 |
#define ERROR_USAGE (-3) |
| 111 |
|
| 112 |
static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen) |
| 113 |
{ |
| 114 |
char *strings, *ptrkey, *ptrvalue; |
| 115 |
int count=1, count2, element_count=0; |
| 116 |
struct sortable_keys *sortable_keys; |
| 117 |
|
| 118 |
*buffer = '\0'; |
| 119 |
|
| 120 |
if (!data) |
| 121 |
return ERROR_NOARG; |
| 122 |
|
| 123 |
strings = ast_strdupa(data); |
| 124 |
|
| 125 |
for (ptrkey = strings; *ptrkey; ptrkey++) { |
| 126 |
if (*ptrkey == ',') |
| 127 |
count++; |
| 128 |
} |
| 129 |
|
| 130 |
sortable_keys = ast_alloca(count * sizeof(struct sortable_keys)); |
| 131 |
|
| 132 |
memset(sortable_keys, 0, count * sizeof(struct sortable_keys)); |
| 133 |
|
| 134 |
|
| 135 |
count2 = 0; |
| 136 |
while ((ptrkey = strsep(&strings, ","))) { |
| 137 |
ptrvalue = strchr(ptrkey, ':'); |
| 138 |
if (!ptrvalue) { |
| 139 |
count--; |
| 140 |
continue; |
| 141 |
} |
| 142 |
*ptrvalue++ = '\0'; |
| 143 |
sortable_keys[count2].key = ptrkey; |
| 144 |
sscanf(ptrvalue, "%30f", &sortable_keys[count2].value); |
| 145 |
count2++; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine); |
| 150 |
|
| 151 |
for (count2 = 0; count2 < count; count2++) { |
| 152 |
int blen = strlen(buffer); |
| 153 |
if (element_count++) { |
| 154 |
strncat(buffer + blen, ",", buflen - blen - 1); |
| 155 |
blen++; |
| 156 |
} |
| 157 |
strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1); |
| 158 |
} |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen) |
| 164 |
{ |
| 165 |
char *parse, ds[2], *var_expr; |
| 166 |
size_t delim_consumed; |
| 167 |
struct ast_str *var_value; |
| 168 |
AST_DECLARE_APP_ARGS(args, |
| 169 |
AST_APP_ARG(varname); |
| 170 |
AST_APP_ARG(delimiter); |
| 171 |
AST_APP_ARG(field); |
| 172 |
); |
| 173 |
|
| 174 |
parse = ast_strdupa(data); |
| 175 |
|
| 176 |
AST_STANDARD_APP_ARGS(args, parse); |
| 177 |
|
| 178 |
|
| 179 |
if (args.argc < 3) { |
| 180 |
return ERROR_NOARG; |
| 181 |
} |
| 182 |
var_expr = ast_alloca(strlen(args.varname) + 4); |
| 183 |
|
| 184 |
|
| 185 |
snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname); |
| 186 |
var_value = ast_str_create(16); |
| 187 |
ast_str_substitute_variables(&var_value, 0, chan, var_expr); |
| 188 |
|
| 189 |
|
| 190 |
if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) { |
| 191 |
ast_copy_string(ds, "-", sizeof(ds)); |
| 192 |
} |
| 193 |
ds[1] = '\0'; |
| 194 |
|
| 195 |
if (ast_str_strlen(var_value)) { |
| 196 |
int curfieldnum = 1; |
| 197 |
char *curfieldptr = ast_str_buffer(var_value); |
| 198 |
int out_field_count = 0; |
| 199 |
|
| 200 |
while (curfieldptr != NULL && args.field != NULL) { |
| 201 |
char *next_range = strsep(&(args.field), "&"); |
| 202 |
int start_field, stop_field; |
| 203 |
char trashchar; |
| 204 |
|
| 205 |
if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) { |
| 206 |
|
| 207 |
} else if (sscanf(next_range, "-%30d", &stop_field) == 1) { |
| 208 |
|
| 209 |
start_field = 1; |
| 210 |
} else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) { |
| 211 |
|
| 212 |
stop_field = INT_MAX; |
| 213 |
} else if (sscanf(next_range, "%30d", &start_field) == 1) { |
| 214 |
|
| 215 |
stop_field = start_field; |
| 216 |
} else { |
| 217 |
|
| 218 |
ast_free(var_value); |
| 219 |
return ERROR_USAGE; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
while (curfieldptr != NULL && curfieldnum < start_field) { |
| 224 |
strsep(&curfieldptr, ds); |
| 225 |
curfieldnum++; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if (curfieldnum > start_field) { |
| 230 |
ast_log(LOG_WARNING, "We're already past the field you wanted?\n"); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
while (curfieldptr != NULL && curfieldnum <= stop_field) { |
| 235 |
char *field_value = strsep(&curfieldptr, ds); |
| 236 |
ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value); |
| 237 |
curfieldnum++; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
ast_free(var_value); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 246 |
{ |
| 247 |
int ret = -1; |
| 248 |
|
| 249 |
switch (sort_internal(chan, data, buf, len)) { |
| 250 |
case ERROR_NOARG: |
| 251 |
ast_log(LOG_ERROR, "SORT() requires an argument\n"); |
| 252 |
break; |
| 253 |
case ERROR_NOMEM: |
| 254 |
ast_log(LOG_ERROR, "Out of memory\n"); |
| 255 |
break; |
| 256 |
case 0: |
| 257 |
ret = 0; |
| 258 |
break; |
| 259 |
default: |
| 260 |
ast_log(LOG_ERROR, "Unknown internal error\n"); |
| 261 |
} |
| 262 |
|
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 267 |
{ |
| 268 |
int ret = -1; |
| 269 |
struct ast_str *str = ast_str_create(16); |
| 270 |
|
| 271 |
switch (cut_internal(chan, data, &str, len)) { |
| 272 |
case ERROR_NOARG: |
| 273 |
ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n"); |
| 274 |
break; |
| 275 |
case ERROR_NOMEM: |
| 276 |
ast_log(LOG_ERROR, "Out of memory\n"); |
| 277 |
break; |
| 278 |
case ERROR_USAGE: |
| 279 |
ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n"); |
| 280 |
break; |
| 281 |
case 0: |
| 282 |
ret = 0; |
| 283 |
ast_copy_string(buf, ast_str_buffer(str), len); |
| 284 |
break; |
| 285 |
default: |
| 286 |
ast_log(LOG_ERROR, "Unknown internal error\n"); |
| 287 |
} |
| 288 |
ast_free(str); |
| 289 |
return ret; |
| 290 |
} |
| 291 |
|
| 292 |
static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 293 |
{ |
| 294 |
int ret = -1; |
| 295 |
|
| 296 |
switch (cut_internal(chan, data, buf, len)) { |
| 297 |
case ERROR_NOARG: |
| 298 |
ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n"); |
| 299 |
break; |
| 300 |
case ERROR_NOMEM: |
| 301 |
ast_log(LOG_ERROR, "Out of memory\n"); |
| 302 |
break; |
| 303 |
case ERROR_USAGE: |
| 304 |
ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n"); |
| 305 |
break; |
| 306 |
case 0: |
| 307 |
ret = 0; |
| 308 |
break; |
| 309 |
default: |
| 310 |
ast_log(LOG_ERROR, "Unknown internal error\n"); |
| 311 |
} |
| 312 |
|
| 313 |
return ret; |
| 314 |
} |
| 315 |
|
| 316 |
static struct ast_custom_function acf_sort = { |
| 317 |
.name = "SORT", |
| 318 |
.read = acf_sort_exec, |
| 319 |
}; |
| 320 |
|
| 321 |
static struct ast_custom_function acf_cut = { |
| 322 |
.name = "CUT", |
| 323 |
.read = acf_cut_exec, |
| 324 |
.read2 = acf_cut_exec2, |
| 325 |
}; |
| 326 |
|
| 327 |
static int unload_module(void) |
| 328 |
{ |
| 329 |
int res = 0; |
| 330 |
|
| 331 |
res |= ast_custom_function_unregister(&acf_cut); |
| 332 |
res |= ast_custom_function_unregister(&acf_sort); |
| 333 |
|
| 334 |
return res; |
| 335 |
} |
| 336 |
|
| 337 |
static int load_module(void) |
| 338 |
{ |
| 339 |
int res = 0; |
| 340 |
|
| 341 |
res |= ast_custom_function_register(&acf_cut); |
| 342 |
res |= ast_custom_function_register(&acf_sort); |
| 343 |
|
| 344 |
return res; |
| 345 |
} |
| 346 |
|
| 347 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string"); |