| 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 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/channel.h" |
| 38 |
#include "asterisk/pbx.h" |
| 39 |
#include "asterisk/app.h" |
| 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 |
struct config_item { |
| 69 |
AST_RWLIST_ENTRY(config_item) entry; |
| 70 |
struct ast_config *cfg; |
| 71 |
char filename[0]; |
| 72 |
}; |
| 73 |
|
| 74 |
static AST_RWLIST_HEAD_STATIC(configs, config_item); |
| 75 |
|
| 76 |
static int config_function_read(struct ast_channel *chan, const char *cmd, char *data, |
| 77 |
char *buf, size_t len) |
| 78 |
{ |
| 79 |
struct ast_config *cfg; |
| 80 |
struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED }; |
| 81 |
char *parse; |
| 82 |
struct config_item *cur; |
| 83 |
int index = 0; |
| 84 |
struct ast_variable *var; |
| 85 |
struct ast_variable *found = NULL; |
| 86 |
int ix = 0; |
| 87 |
AST_DECLARE_APP_ARGS(args, |
| 88 |
AST_APP_ARG(filename); |
| 89 |
AST_APP_ARG(category); |
| 90 |
AST_APP_ARG(variable); |
| 91 |
AST_APP_ARG(index); |
| 92 |
); |
| 93 |
|
| 94 |
if (ast_strlen_zero(data)) { |
| 95 |
ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n"); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
parse = ast_strdupa(data); |
| 100 |
AST_STANDARD_APP_ARGS(args, parse); |
| 101 |
|
| 102 |
if (ast_strlen_zero(args.filename)) { |
| 103 |
ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n"); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
if (ast_strlen_zero(args.category)) { |
| 108 |
ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n"); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
|
| 112 |
if (ast_strlen_zero(args.variable)) { |
| 113 |
ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n"); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
if (!ast_strlen_zero(args.index)) { |
| 118 |
if (!sscanf(args.index, "%d", &index)) { |
| 119 |
ast_log(LOG_ERROR, "AST_CONFIG() index must be an integer\n"); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) { |
| 125 |
return -1; |
| 126 |
} |
| 127 |
|
| 128 |
if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 129 |
|
| 130 |
AST_RWLIST_RDLOCK(&configs); |
| 131 |
AST_RWLIST_TRAVERSE(&configs, cur, entry) { |
| 132 |
if (!strcmp(cur->filename, args.filename)) { |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (!cur) { |
| 138 |
|
| 139 |
AST_RWLIST_UNLOCK(&configs); |
| 140 |
AST_RWLIST_WRLOCK(&configs); |
| 141 |
if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) { |
| 142 |
AST_RWLIST_UNLOCK(&configs); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
strcpy(cur->filename, args.filename); |
| 147 |
|
| 148 |
ast_clear_flag(&cfg_flags, CONFIG_FLAG_FILEUNCHANGED); |
| 149 |
if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) { |
| 150 |
ast_free(cur); |
| 151 |
AST_RWLIST_UNLOCK(&configs); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
cur->cfg = cfg; |
| 156 |
AST_RWLIST_INSERT_TAIL(&configs, cur, entry); |
| 157 |
} |
| 158 |
|
| 159 |
cfg = cur->cfg; |
| 160 |
} else { |
| 161 |
|
| 162 |
AST_RWLIST_WRLOCK(&configs); |
| 163 |
AST_RWLIST_TRAVERSE(&configs, cur, entry) { |
| 164 |
if (!strcmp(cur->filename, args.filename)) { |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (!cur) { |
| 170 |
if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) { |
| 171 |
AST_RWLIST_UNLOCK(&configs); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
strcpy(cur->filename, args.filename); |
| 176 |
cur->cfg = cfg; |
| 177 |
|
| 178 |
AST_RWLIST_INSERT_TAIL(&configs, cur, entry); |
| 179 |
} else { |
| 180 |
ast_config_destroy(cur->cfg); |
| 181 |
cur->cfg = cfg; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
for (var = ast_category_root(cfg, args.category); var; var = var->next) { |
| 186 |
if (strcasecmp(args.variable, var->name)) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
found = var; |
| 190 |
if (index == -1) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
if (ix == index) { |
| 194 |
break; |
| 195 |
} |
| 196 |
found = NULL; |
| 197 |
ix++; |
| 198 |
} |
| 199 |
|
| 200 |
if (!found) { |
| 201 |
ast_debug(1, "'%s' not found at index %d in [%s] of '%s'. Maximum index found: %d\n", |
| 202 |
args.variable, index, args.category, args.filename, ix); |
| 203 |
AST_RWLIST_UNLOCK(&configs); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
ast_copy_string(buf, found->value, len); |
| 208 |
|
| 209 |
|
| 210 |
AST_RWLIST_UNLOCK(&configs); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
static struct ast_custom_function config_function = { |
| 216 |
.name = "AST_CONFIG", |
| 217 |
.read = config_function_read, |
| 218 |
}; |
| 219 |
|
| 220 |
static int unload_module(void) |
| 221 |
{ |
| 222 |
struct config_item *current; |
| 223 |
int res = ast_custom_function_unregister(&config_function); |
| 224 |
|
| 225 |
AST_RWLIST_WRLOCK(&configs); |
| 226 |
while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) { |
| 227 |
ast_config_destroy(current->cfg); |
| 228 |
ast_free(current); |
| 229 |
} |
| 230 |
AST_RWLIST_UNLOCK(&configs); |
| 231 |
|
| 232 |
return res; |
| 233 |
} |
| 234 |
|
| 235 |
static int load_module(void) |
| 236 |
{ |
| 237 |
return ast_custom_function_register(&config_function); |
| 238 |
} |
| 239 |
|
| 240 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access"); |