| 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 |
|
| 35 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include <pjsip.h> |
| 38 |
#include <pjlib.h> |
| 39 |
|
| 40 |
#include "asterisk/app.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/sorcery.h" |
| 44 |
#include "asterisk/res_pjsip.h" |
| 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 |
static int pjsip_aor_function_read(struct ast_channel *chan, |
| 93 |
const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 94 |
{ |
| 95 |
struct ast_sorcery *pjsip_sorcery; |
| 96 |
char *parsed_data = ast_strdupa(data); |
| 97 |
RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup); |
| 98 |
int res = 0; |
| 99 |
|
| 100 |
AST_DECLARE_APP_ARGS(args, |
| 101 |
AST_APP_ARG(aor_name); |
| 102 |
AST_APP_ARG(field_name); |
| 103 |
); |
| 104 |
|
| 105 |
|
| 106 |
if (ast_strlen_zero(parsed_data)) { |
| 107 |
ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
AST_STANDARD_APP_ARGS(args, parsed_data); |
| 112 |
|
| 113 |
if (ast_strlen_zero(args.aor_name)) { |
| 114 |
ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
if (ast_strlen_zero(args.field_name)) { |
| 119 |
ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
pjsip_sorcery = ast_sip_get_sorcery(); |
| 124 |
if (!pjsip_sorcery) { |
| 125 |
ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n"); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name); |
| 130 |
if (!aor_obj) { |
| 131 |
ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
if (!strcmp(args.field_name, "contact")) { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); |
| 140 |
struct ao2_iterator i; |
| 141 |
struct ast_sip_contact *contact; |
| 142 |
int first = 1; |
| 143 |
|
| 144 |
contacts = ast_sip_location_retrieve_aor_contacts(aor_obj); |
| 145 |
if (!contacts) { |
| 146 |
ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
i = ao2_iterator_init(contacts, 0); |
| 151 |
while ((contact = ao2_iterator_next(&i))) { |
| 152 |
if (!first) { |
| 153 |
ast_str_append(buf, len, "%s", ","); |
| 154 |
} |
| 155 |
|
| 156 |
ast_str_append(buf, len, "%s", ast_sorcery_object_get_id(contact)); |
| 157 |
first = 0; |
| 158 |
|
| 159 |
ao2_ref(contact, -1); |
| 160 |
} |
| 161 |
ao2_iterator_destroy(&i); |
| 162 |
} else { |
| 163 |
struct ast_variable *change_set; |
| 164 |
struct ast_variable *it_change_set; |
| 165 |
|
| 166 |
change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj); |
| 167 |
if (!change_set) { |
| 168 |
ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
|
| 172 |
for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) { |
| 173 |
if (!strcmp(it_change_set->name, args.field_name)) { |
| 174 |
ast_str_set(buf, len, "%s", it_change_set->value); |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (!it_change_set) { |
| 180 |
ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name); |
| 181 |
res = 1; |
| 182 |
} |
| 183 |
|
| 184 |
ast_variables_destroy(change_set); |
| 185 |
} |
| 186 |
|
| 187 |
return res; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
static struct ast_custom_function pjsip_aor_function = { |
| 192 |
.name = "PJSIP_AOR", |
| 193 |
.read2 = pjsip_aor_function_read, |
| 194 |
}; |
| 195 |
|
| 196 |
static int unload_module(void) |
| 197 |
{ |
| 198 |
return ast_custom_function_unregister(&pjsip_aor_function); |
| 199 |
} |
| 200 |
|
| 201 |
static int load_module(void) |
| 202 |
{ |
| 203 |
return ast_custom_function_register(&pjsip_aor_function); |
| 204 |
} |
| 205 |
|
| 206 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP AOR", |
| 207 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 208 |
.load = load_module, |
| 209 |
.unload = unload_module, |
| 210 |
.requires = "res_pjsip", |
| 211 |
); |