| 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 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#include "asterisk.h" |
| 144 |
|
| 145 |
#include "asterisk/module.h" |
| 146 |
#include "asterisk/res_mwi_external.h" |
| 147 |
#include "asterisk/manager.h" |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static int mwi_mailbox_get(struct mansession *s, const struct message *m) |
| 163 |
{ |
| 164 |
char id_text[256]; |
| 165 |
const char *id; |
| 166 |
const char *mailbox_id = astman_get_header(m, "Mailbox"); |
| 167 |
const struct ast_mwi_mailbox_object *mailbox; |
| 168 |
struct ao2_container *mailboxes; |
| 169 |
unsigned count; |
| 170 |
struct ao2_iterator iter; |
| 171 |
|
| 172 |
if (ast_strlen_zero(mailbox_id)) { |
| 173 |
astman_send_error(s, m, "Missing mailbox parameter in request"); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
if (*mailbox_id == '/') { |
| 178 |
struct ast_str *regex_string; |
| 179 |
|
| 180 |
regex_string = ast_str_create(strlen(mailbox_id) + 1); |
| 181 |
if (!regex_string) { |
| 182 |
astman_send_error(s, m, "Memory Allocation Failure"); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if (ast_regex_string_to_regex_pattern(mailbox_id, ®ex_string) != 0) { |
| 188 |
astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id); |
| 189 |
ast_free(regex_string); |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string)); |
| 194 |
ast_free(regex_string); |
| 195 |
} else { |
| 196 |
mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL); |
| 197 |
if (mailboxes) { |
| 198 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 199 |
if (mailbox) { |
| 200 |
if (!ao2_link(mailboxes, (void *) mailbox)) { |
| 201 |
ao2_ref(mailboxes, -1); |
| 202 |
mailboxes = NULL; |
| 203 |
} |
| 204 |
ast_mwi_mailbox_unref(mailbox); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
if (!mailboxes) { |
| 209 |
astman_send_error(s, m, "Mailbox container creation failure"); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
astman_send_listack(s, m, "Mailboxes will follow", "start"); |
| 214 |
|
| 215 |
id = astman_get_header(m, "ActionID"); |
| 216 |
if (!ast_strlen_zero(id)) { |
| 217 |
snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id); |
| 218 |
} else { |
| 219 |
id_text[0] = '\0'; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
count = 0; |
| 224 |
iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK); |
| 225 |
for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { |
| 226 |
++count; |
| 227 |
astman_append(s, |
| 228 |
"Event: MWIGet\r\n" |
| 229 |
"Mailbox: %s\r\n" |
| 230 |
"OldMessages: %u\r\n" |
| 231 |
"NewMessages: %u\r\n" |
| 232 |
"%s" |
| 233 |
"\r\n", |
| 234 |
ast_mwi_mailbox_get_id(mailbox), |
| 235 |
ast_mwi_mailbox_get_msgs_old(mailbox), |
| 236 |
ast_mwi_mailbox_get_msgs_new(mailbox), |
| 237 |
id_text); |
| 238 |
} |
| 239 |
ao2_iterator_destroy(&iter); |
| 240 |
ao2_ref(mailboxes, -1); |
| 241 |
|
| 242 |
astman_send_list_complete_start(s, m, "MWIGetComplete", count); |
| 243 |
astman_send_list_complete_end(s); |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static int mwi_mailbox_delete(struct mansession *s, const struct message *m) |
| 260 |
{ |
| 261 |
const char *mailbox_id = astman_get_header(m, "Mailbox"); |
| 262 |
|
| 263 |
if (ast_strlen_zero(mailbox_id)) { |
| 264 |
astman_send_error(s, m, "Missing mailbox parameter in request"); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
if (*mailbox_id == '/') { |
| 269 |
struct ast_str *regex_string; |
| 270 |
|
| 271 |
regex_string = ast_str_create(strlen(mailbox_id) + 1); |
| 272 |
if (!regex_string) { |
| 273 |
astman_send_error(s, m, "Memory Allocation Failure"); |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
if (ast_regex_string_to_regex_pattern(mailbox_id, ®ex_string) != 0) { |
| 279 |
astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id); |
| 280 |
ast_free(regex_string); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string)); |
| 285 |
ast_free(regex_string); |
| 286 |
} else { |
| 287 |
ast_mwi_mailbox_delete(mailbox_id); |
| 288 |
} |
| 289 |
|
| 290 |
astman_send_ack(s, m, NULL); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
static int mwi_mailbox_update(struct mansession *s, const struct message *m) |
| 306 |
{ |
| 307 |
const char *mailbox_id = astman_get_header(m, "Mailbox"); |
| 308 |
const char *msgs_old = astman_get_header(m, "OldMessages"); |
| 309 |
const char *msgs_new = astman_get_header(m, "NewMessages"); |
| 310 |
struct ast_mwi_mailbox_object *mailbox; |
| 311 |
unsigned int num_old; |
| 312 |
unsigned int num_new; |
| 313 |
|
| 314 |
if (ast_strlen_zero(mailbox_id)) { |
| 315 |
astman_send_error(s, m, "Missing mailbox parameter in request"); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
num_old = 0; |
| 320 |
if (!ast_strlen_zero(msgs_old)) { |
| 321 |
if (sscanf(msgs_old, "%u", &num_old) != 1) { |
| 322 |
astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
num_new = 0; |
| 328 |
if (!ast_strlen_zero(msgs_new)) { |
| 329 |
if (sscanf(msgs_new, "%u", &num_new) != 1) { |
| 330 |
astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new); |
| 331 |
return 0; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
mailbox = ast_mwi_mailbox_alloc(mailbox_id); |
| 336 |
if (!mailbox) { |
| 337 |
astman_send_error(s, m, "Mailbox object creation failure"); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
ast_mwi_mailbox_set_msgs_old(mailbox, num_old); |
| 343 |
ast_mwi_mailbox_set_msgs_new(mailbox, num_new); |
| 344 |
if (ast_mwi_mailbox_update(mailbox)) { |
| 345 |
astman_send_error(s, m, "Update attempt failed"); |
| 346 |
} else { |
| 347 |
astman_send_ack(s, m, NULL); |
| 348 |
} |
| 349 |
ast_mwi_mailbox_unref(mailbox); |
| 350 |
|
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
static int unload_module(void) |
| 355 |
{ |
| 356 |
ast_manager_unregister("MWIGet"); |
| 357 |
ast_manager_unregister("MWIDelete"); |
| 358 |
ast_manager_unregister("MWIUpdate"); |
| 359 |
|
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
static int load_module(void) |
| 364 |
{ |
| 365 |
int res; |
| 366 |
|
| 367 |
res = 0; |
| 368 |
res |= ast_manager_register_xml("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get); |
| 369 |
res |= ast_manager_register_xml("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete); |
| 370 |
res |= ast_manager_register_xml("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update); |
| 371 |
if (res) { |
| 372 |
unload_module(); |
| 373 |
return AST_MODULE_LOAD_DECLINE; |
| 374 |
} |
| 375 |
|
| 376 |
return AST_MODULE_LOAD_SUCCESS; |
| 377 |
} |
| 378 |
|
| 379 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AMI support for external MWI", |
| 380 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 381 |
.load = load_module, |
| 382 |
.unload = unload_module, |
| 383 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, |
| 384 |
.requires = "res_mwi_external", |
| 385 |
); |