| 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 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/app.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/sorcery.h" |
| 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 |
static int sorcery_function_read(struct ast_channel *chan, |
| 83 |
const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 84 |
{ |
| 85 |
char *parsed_data = ast_strdupa(data); |
| 86 |
RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref); |
| 87 |
RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup); |
| 88 |
struct ast_variable *change_set; |
| 89 |
struct ast_variable *it_change_set; |
| 90 |
int found, field_number = 1, ix, method; |
| 91 |
char *separator = ","; |
| 92 |
|
| 93 |
enum methods { |
| 94 |
CONCAT, |
| 95 |
SINGLE, |
| 96 |
}; |
| 97 |
|
| 98 |
AST_DECLARE_APP_ARGS(args, |
| 99 |
AST_APP_ARG(module_name); |
| 100 |
AST_APP_ARG(object_type); |
| 101 |
AST_APP_ARG(object_id); |
| 102 |
AST_APP_ARG(field_name); |
| 103 |
AST_APP_ARG(method); |
| 104 |
AST_APP_ARG(method_arg); |
| 105 |
); |
| 106 |
|
| 107 |
|
| 108 |
if (ast_strlen_zero(parsed_data)) { |
| 109 |
ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
AST_STANDARD_APP_ARGS(args, parsed_data); |
| 114 |
|
| 115 |
if (ast_strlen_zero(args.module_name)) { |
| 116 |
ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
if (ast_strlen_zero(args.object_type)) { |
| 121 |
ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
if (ast_strlen_zero(args.object_id)) { |
| 126 |
ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
if (ast_strlen_zero(args.field_name)) { |
| 131 |
ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
if (ast_strlen_zero(args.method)) { |
| 136 |
method = CONCAT; |
| 137 |
} else { |
| 138 |
if (strcmp(args.method, "concat") == 0) { |
| 139 |
method = CONCAT; |
| 140 |
if (ast_strlen_zero(args.method_arg)) { |
| 141 |
separator = ","; |
| 142 |
} else { |
| 143 |
separator = args.method_arg; |
| 144 |
} |
| 145 |
|
| 146 |
} else if (strcmp(args.method, "single") == 0) { |
| 147 |
method = SINGLE; |
| 148 |
if (!ast_strlen_zero(args.method_arg)) { |
| 149 |
if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) { |
| 150 |
ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n"); |
| 151 |
return -1; |
| 152 |
} |
| 153 |
} |
| 154 |
} else { |
| 155 |
ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n"); |
| 156 |
return -1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
sorcery = ast_sorcery_retrieve_by_module_name(args.module_name); |
| 161 |
if (!sorcery) { |
| 162 |
ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name); |
| 163 |
return -1; |
| 164 |
} |
| 165 |
|
| 166 |
sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id); |
| 167 |
if (!sorcery_obj) { |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj); |
| 172 |
if (!change_set) { |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
ix=1; |
| 177 |
found = 0; |
| 178 |
for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) { |
| 179 |
|
| 180 |
if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) { |
| 181 |
ast_str_append(buf, 0, "%s%s", it_change_set->value, separator); |
| 182 |
found = 1; |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0 && ix++ == field_number) { |
| 187 |
ast_str_set(buf, len, "%s", it_change_set->value); |
| 188 |
found = 1; |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
ast_variables_destroy(change_set); |
| 194 |
|
| 195 |
if (!found) { |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
if (method == CONCAT) { |
| 200 |
ast_str_truncate(*buf, -1); |
| 201 |
} |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static struct ast_custom_function sorcery_function = { |
| 207 |
.name = "AST_SORCERY", |
| 208 |
.read2 = sorcery_function_read, |
| 209 |
}; |
| 210 |
|
| 211 |
static int unload_module(void) |
| 212 |
{ |
| 213 |
return ast_custom_function_unregister(&sorcery_function); |
| 214 |
} |
| 215 |
|
| 216 |
static int load_module(void) |
| 217 |
{ |
| 218 |
return ast_custom_function_register(&sorcery_function); |
| 219 |
} |
| 220 |
|
| 221 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object"); |