| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <sys/stat.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/channel.h" |
| 38 |
#include "asterisk/pbx.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/app.h" |
| 41 |
#include "asterisk/astobj2.h" |
| 42 |
#include "asterisk/astdb.h" |
| 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 struct ao2_container *group_container = NULL; |
| 83 |
|
| 84 |
struct group_entry { |
| 85 |
char name[AST_CHANNEL_NAME]; |
| 86 |
}; |
| 87 |
|
| 88 |
struct group { |
| 89 |
char name[AST_MAX_EXTENSION]; |
| 90 |
struct ao2_container *entries; |
| 91 |
}; |
| 92 |
|
| 93 |
static void group_destroy(void *vgroup) |
| 94 |
{ |
| 95 |
struct group *group = vgroup; |
| 96 |
ao2_ref(group->entries, -1); |
| 97 |
} |
| 98 |
|
| 99 |
static int group_hash_fn(const void *obj, const int flags) |
| 100 |
{ |
| 101 |
const struct group *g = obj; |
| 102 |
return ast_str_hash(g->name); |
| 103 |
} |
| 104 |
|
| 105 |
static int group_cmp_fn(void *obj1, void *name2, int flags) |
| 106 |
{ |
| 107 |
struct group *g1 = obj1, *g2 = name2; |
| 108 |
char *name = name2; |
| 109 |
if (flags & OBJ_POINTER) |
| 110 |
return strcmp(g1->name, g2->name) ? 0 : CMP_MATCH | CMP_STOP; |
| 111 |
else |
| 112 |
return strcmp(g1->name, name) ? 0 : CMP_MATCH | CMP_STOP; |
| 113 |
} |
| 114 |
|
| 115 |
static int entry_hash_fn(const void *obj, const int flags) |
| 116 |
{ |
| 117 |
const struct group_entry *e = obj; |
| 118 |
return ast_str_hash(e->name); |
| 119 |
} |
| 120 |
|
| 121 |
static int entry_cmp_fn(void *obj1, void *name2, int flags) |
| 122 |
{ |
| 123 |
struct group_entry *e1 = obj1, *e2 = name2; |
| 124 |
char *name = name2; |
| 125 |
if (flags & OBJ_POINTER) |
| 126 |
return strcmp(e1->name, e2->name) ? 0 : CMP_MATCH | CMP_STOP; |
| 127 |
else |
| 128 |
return strcmp(e1->name, name) ? 0 : CMP_MATCH | CMP_STOP; |
| 129 |
} |
| 130 |
|
| 131 |
static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 132 |
{ |
| 133 |
struct ao2_iterator i; |
| 134 |
struct group *grhead = ao2_find(group_container, data, 0); |
| 135 |
struct group_entry *entry; |
| 136 |
size_t bufused = 0; |
| 137 |
int trunc_warning = 0; |
| 138 |
int res = 0; |
| 139 |
|
| 140 |
if (!grhead) { |
| 141 |
if (!ast_strlen_zero(cmd)) { |
| 142 |
ast_log(LOG_WARNING, "No such dialgroup '%s'\n", data); |
| 143 |
} |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
buf[0] = '\0'; |
| 148 |
|
| 149 |
i = ao2_iterator_init(grhead->entries, 0); |
| 150 |
while ((entry = ao2_iterator_next(&i))) { |
| 151 |
int tmp = strlen(entry->name); |
| 152 |
|
| 153 |
if (len - bufused > tmp + 2) { |
| 154 |
if (bufused != 0) |
| 155 |
buf[bufused++] = '&'; |
| 156 |
ast_copy_string(buf + bufused, entry->name, len - bufused); |
| 157 |
bufused += tmp; |
| 158 |
} else if (trunc_warning++ == 0) { |
| 159 |
if (!ast_strlen_zero(cmd)) { |
| 160 |
ast_log(LOG_WARNING, "Dialgroup '%s' is too large. Truncating list.\n", data); |
| 161 |
} else { |
| 162 |
res = 1; |
| 163 |
ao2_ref(entry, -1); |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
ao2_ref(entry, -1); |
| 168 |
} |
| 169 |
ao2_iterator_destroy(&i); |
| 170 |
ao2_ref(grhead, -1); |
| 171 |
|
| 172 |
return res; |
| 173 |
} |
| 174 |
|
| 175 |
static int dialgroup_refreshdb(struct ast_channel *chan, const char *cdialgroup) |
| 176 |
{ |
| 177 |
int len = 500, res = 0; |
| 178 |
char *buf = NULL; |
| 179 |
char *new_buf; |
| 180 |
char *dialgroup = ast_strdupa(cdialgroup); |
| 181 |
|
| 182 |
do { |
| 183 |
len *= 2; |
| 184 |
new_buf = ast_realloc(buf, len); |
| 185 |
if (!new_buf) { |
| 186 |
ast_free(buf); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
buf = new_buf; |
| 190 |
|
| 191 |
if ((res = dialgroup_read(chan, "", dialgroup, buf, len)) < 0) { |
| 192 |
ast_free(buf); |
| 193 |
return -1; |
| 194 |
} |
| 195 |
} while (res == 1); |
| 196 |
|
| 197 |
if (ast_strlen_zero(buf)) { |
| 198 |
ast_db_del("dialgroup", cdialgroup); |
| 199 |
} else { |
| 200 |
ast_db_put("dialgroup", cdialgroup, buf); |
| 201 |
} |
| 202 |
ast_free(buf); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue) |
| 207 |
{ |
| 208 |
struct group *grhead; |
| 209 |
struct group_entry *entry; |
| 210 |
int j, needrefresh = 1; |
| 211 |
AST_DECLARE_APP_ARGS(args, |
| 212 |
AST_APP_ARG(group); |
| 213 |
AST_APP_ARG(op); |
| 214 |
); |
| 215 |
AST_DECLARE_APP_ARGS(inter, |
| 216 |
AST_APP_ARG(faces)[100]; |
| 217 |
); |
| 218 |
char *value = ast_strdupa(cvalue); |
| 219 |
|
| 220 |
AST_STANDARD_APP_ARGS(args, data); |
| 221 |
AST_NONSTANDARD_APP_ARGS(inter, value, '&'); |
| 222 |
|
| 223 |
if (!(grhead = ao2_find(group_container, args.group, 0))) { |
| 224 |
|
| 225 |
grhead = ao2_alloc(sizeof(*grhead), group_destroy); |
| 226 |
if (!grhead) |
| 227 |
return -1; |
| 228 |
grhead->entries = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37, |
| 229 |
entry_hash_fn, NULL, entry_cmp_fn); |
| 230 |
if (!grhead->entries) { |
| 231 |
ao2_ref(grhead, -1); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
ast_copy_string(grhead->name, args.group, sizeof(grhead->name)); |
| 235 |
ao2_link(group_container, grhead); |
| 236 |
} |
| 237 |
|
| 238 |
if (ast_strlen_zero(args.op)) { |
| 239 |
|
| 240 |
args.op = "add"; |
| 241 |
|
| 242 |
|
| 243 |
ao2_ref(grhead->entries, -1); |
| 244 |
grhead->entries = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37, |
| 245 |
entry_hash_fn, NULL, entry_cmp_fn); |
| 246 |
if (!grhead->entries) { |
| 247 |
ao2_unlink(group_container, grhead); |
| 248 |
ao2_ref(grhead, -1); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if (strcasecmp(args.op, "add") == 0) { |
| 254 |
for (j = 0; j < inter.argc; j++) { |
| 255 |
|
| 256 |
if ((entry = ao2_find(grhead->entries, inter.faces[j], 0))) { |
| 257 |
ao2_ref(entry, -1); |
| 258 |
continue; |
| 259 |
} |
| 260 |
if ((entry = ao2_alloc(sizeof(*entry), NULL))) { |
| 261 |
ast_copy_string(entry->name, inter.faces[j], sizeof(entry->name)); |
| 262 |
ao2_link(grhead->entries, entry); |
| 263 |
ao2_ref(entry, -1); |
| 264 |
} else { |
| 265 |
ast_log(LOG_WARNING, "Unable to add '%s' to dialgroup '%s'\n", inter.faces[j], grhead->name); |
| 266 |
} |
| 267 |
} |
| 268 |
} else if (strncasecmp(args.op, "del", 3) == 0) { |
| 269 |
for (j = 0; j < inter.argc; j++) { |
| 270 |
if ((entry = ao2_find(grhead->entries, inter.faces[j], OBJ_UNLINK))) { |
| 271 |
ao2_ref(entry, -1); |
| 272 |
} else { |
| 273 |
ast_log(LOG_WARNING, "Interface '%s' not found in dialgroup '%s'\n", inter.faces[j], grhead->name); |
| 274 |
} |
| 275 |
} |
| 276 |
} else { |
| 277 |
ast_log(LOG_ERROR, "Unrecognized operation: %s\n", args.op); |
| 278 |
needrefresh = 0; |
| 279 |
} |
| 280 |
ao2_ref(grhead, -1); |
| 281 |
|
| 282 |
if (needrefresh) { |
| 283 |
dialgroup_refreshdb(chan, args.group); |
| 284 |
} |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
static struct ast_custom_function dialgroup_function = { |
| 290 |
.name = "DIALGROUP", |
| 291 |
.read = dialgroup_read, |
| 292 |
.write = dialgroup_write, |
| 293 |
}; |
| 294 |
|
| 295 |
static int unload_module(void) |
| 296 |
{ |
| 297 |
int res = ast_custom_function_unregister(&dialgroup_function); |
| 298 |
ao2_ref(group_container, -1); |
| 299 |
return res; |
| 300 |
} |
| 301 |
|
| 302 |
static int load_module(void) |
| 303 |
{ |
| 304 |
struct ast_db_entry *dbtree, *tmp; |
| 305 |
char groupname[AST_MAX_EXTENSION], *ptr; |
| 306 |
|
| 307 |
group_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37, |
| 308 |
group_hash_fn, NULL, group_cmp_fn); |
| 309 |
if (group_container) { |
| 310 |
|
| 311 |
if ((dbtree = ast_db_gettree("dialgroup", NULL))) { |
| 312 |
for (tmp = dbtree; tmp; tmp = tmp->next) { |
| 313 |
ast_copy_string(groupname, tmp->key, sizeof(groupname)); |
| 314 |
if ((ptr = strrchr(groupname, '/'))) { |
| 315 |
ptr++; |
| 316 |
dialgroup_write(NULL, "", ptr, tmp->data); |
| 317 |
} |
| 318 |
} |
| 319 |
ast_db_freetree(dbtree); |
| 320 |
} |
| 321 |
return ast_custom_function_register(&dialgroup_function); |
| 322 |
} else { |
| 323 |
return AST_MODULE_LOAD_DECLINE; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialgroup dialplan function"); |