| 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 |
#include "asterisk.h" |
| 28 |
|
| 29 |
#include <regex.h> |
| 30 |
|
| 31 |
#include <pjsip.h> |
| 32 |
#include <pjsip_simple.h> |
| 33 |
|
| 34 |
#include "asterisk/res_pjsip.h" |
| 35 |
#include "asterisk/res_pjsip_outbound_publish.h" |
| 36 |
#include "asterisk/res_pjsip_pubsub.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/logger.h" |
| 39 |
#include "asterisk/mwi.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 |
|
| 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 |
struct asterisk_devicestate_publisher_state { |
| 108 |
|
| 109 |
struct ast_sip_outbound_publish_client *client; |
| 110 |
|
| 111 |
struct stasis_subscription *device_state_subscription; |
| 112 |
|
| 113 |
regex_t device_state_regex; |
| 114 |
|
| 115 |
unsigned int device_state_filter; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
struct asterisk_mwi_publisher_state { |
| 120 |
|
| 121 |
struct ast_sip_outbound_publish_client *client; |
| 122 |
|
| 123 |
struct stasis_subscription *mailbox_state_subscription; |
| 124 |
|
| 125 |
regex_t mailbox_state_regex; |
| 126 |
|
| 127 |
unsigned int mailbox_state_filter; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
struct asterisk_publication_config { |
| 132 |
|
| 133 |
SORCERY_OBJECT(details); |
| 134 |
|
| 135 |
AST_DECLARE_STRING_FIELDS( |
| 136 |
|
| 137 |
AST_STRING_FIELD(devicestate_publish); |
| 138 |
|
| 139 |
AST_STRING_FIELD(mailboxstate_publish); |
| 140 |
); |
| 141 |
|
| 142 |
unsigned int device_state; |
| 143 |
|
| 144 |
regex_t device_state_regex; |
| 145 |
|
| 146 |
unsigned int device_state_filter; |
| 147 |
|
| 148 |
unsigned int mailbox_state; |
| 149 |
|
| 150 |
regex_t mailbox_state_regex; |
| 151 |
|
| 152 |
unsigned int mailbox_state_filter; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
static void asterisk_devicestate_publisher_state_destroy(void *obj) |
| 157 |
{ |
| 158 |
struct asterisk_devicestate_publisher_state *publisher_state = obj; |
| 159 |
|
| 160 |
ao2_cleanup(publisher_state->client); |
| 161 |
|
| 162 |
if (publisher_state->device_state_filter) { |
| 163 |
regfree(&publisher_state->device_state_regex); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
static const struct ast_datastore_info asterisk_devicestate_publisher_state_datastore = { |
| 169 |
.type = "asterisk-devicestate-publisher", |
| 170 |
.destroy = asterisk_devicestate_publisher_state_destroy, |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
static void asterisk_mwi_publisher_state_destroy(void *obj) |
| 175 |
{ |
| 176 |
struct asterisk_mwi_publisher_state *publisher_state = obj; |
| 177 |
|
| 178 |
ao2_cleanup(publisher_state->client); |
| 179 |
|
| 180 |
if (publisher_state->mailbox_state_filter) { |
| 181 |
regfree(&publisher_state->mailbox_state_regex); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
static const struct ast_datastore_info asterisk_mwi_publisher_state_datastore = { |
| 187 |
.type = "asterisk-mwi-publisher", |
| 188 |
.destroy = asterisk_mwi_publisher_state_destroy, |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static void asterisk_publisher_devstate_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg) |
| 197 |
{ |
| 198 |
struct ast_datastore *datastore = data; |
| 199 |
struct asterisk_devicestate_publisher_state *publisher_state = datastore->data; |
| 200 |
struct ast_device_state_message *dev_state; |
| 201 |
char eid_str[20]; |
| 202 |
struct ast_json *json; |
| 203 |
char *text; |
| 204 |
struct ast_sip_body body = { |
| 205 |
.type = "application", |
| 206 |
.subtype = "json", |
| 207 |
}; |
| 208 |
|
| 209 |
if (!stasis_subscription_is_subscribed(sub) || ast_device_state_message_type() != stasis_message_type(msg)) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
dev_state = stasis_message_data(msg); |
| 214 |
if (!dev_state->eid || ast_eid_cmp(&ast_eid_default, dev_state->eid)) { |
| 215 |
|
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (publisher_state->device_state_filter && regexec(&publisher_state->device_state_regex, dev_state->device, 0, NULL, 0)) { |
| 220 |
|
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); |
| 225 |
json = ast_json_pack( |
| 226 |
"{ s: s, s: s, s: s, s: i, s:s }", |
| 227 |
"type", "devicestate", |
| 228 |
"device", dev_state->device, |
| 229 |
"state", ast_devstate_str(dev_state->state), |
| 230 |
"cachable", dev_state->cachable, |
| 231 |
"eid", eid_str); |
| 232 |
if (!json) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
text = ast_json_dump_string(json); |
| 237 |
if (!text) { |
| 238 |
ast_json_unref(json); |
| 239 |
return; |
| 240 |
} |
| 241 |
body.body_text = text; |
| 242 |
|
| 243 |
ast_sip_publish_client_send(publisher_state->client, &body); |
| 244 |
|
| 245 |
ast_json_free(text); |
| 246 |
ast_json_unref(json); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static void asterisk_publisher_mwistate_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg) |
| 255 |
{ |
| 256 |
struct ast_datastore *datastore = data; |
| 257 |
struct asterisk_mwi_publisher_state *publisher_state = datastore->data; |
| 258 |
struct ast_mwi_state *mwi_state; |
| 259 |
char eid_str[20]; |
| 260 |
struct ast_json *json; |
| 261 |
char *text; |
| 262 |
struct ast_sip_body body = { |
| 263 |
.type = "application", |
| 264 |
.subtype = "json", |
| 265 |
}; |
| 266 |
|
| 267 |
if (!stasis_subscription_is_subscribed(sub) || ast_mwi_state_type() != stasis_message_type(msg)) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
mwi_state = stasis_message_data(msg); |
| 272 |
if (ast_eid_cmp(&ast_eid_default, &mwi_state->eid)) { |
| 273 |
|
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
if (publisher_state->mailbox_state_filter && regexec(&publisher_state->mailbox_state_regex, mwi_state->uniqueid, 0, NULL, 0)) { |
| 278 |
|
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); |
| 283 |
json = ast_json_pack( |
| 284 |
"{ s: s, s: s, s: i, s: i, s:s }", |
| 285 |
"type", "mailboxstate", |
| 286 |
"uniqueid", mwi_state->uniqueid, |
| 287 |
"old", mwi_state->old_msgs, |
| 288 |
"new", mwi_state->new_msgs, |
| 289 |
"eid", eid_str); |
| 290 |
if (!json) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
text = ast_json_dump_string(json); |
| 295 |
if (!text) { |
| 296 |
ast_json_unref(json); |
| 297 |
return; |
| 298 |
} |
| 299 |
body.body_text = text; |
| 300 |
|
| 301 |
ast_sip_publish_client_send(publisher_state->client, &body); |
| 302 |
|
| 303 |
ast_json_free(text); |
| 304 |
ast_json_unref(json); |
| 305 |
} |
| 306 |
|
| 307 |
static int cached_devstate_cb(void *obj, void *arg, int flags) |
| 308 |
{ |
| 309 |
struct stasis_message *msg = obj; |
| 310 |
struct ast_datastore *datastore = arg; |
| 311 |
struct asterisk_devicestate_publisher_state *publisher_state = datastore->data; |
| 312 |
|
| 313 |
asterisk_publisher_devstate_cb(arg, publisher_state->device_state_subscription, msg); |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
static int cached_mwistate_cb(void *obj, void *arg, int flags) |
| 319 |
{ |
| 320 |
struct stasis_message *msg = obj; |
| 321 |
struct ast_datastore *datastore = arg; |
| 322 |
struct asterisk_mwi_publisher_state *publisher_state = datastore->data; |
| 323 |
|
| 324 |
asterisk_publisher_mwistate_cb(arg, publisher_state->mailbox_state_subscription, msg); |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
static int build_regex(regex_t *regex, const char *text) |
| 330 |
{ |
| 331 |
int res; |
| 332 |
|
| 333 |
if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) { |
| 334 |
size_t len = regerror(res, regex, NULL, 0); |
| 335 |
char buf[len]; |
| 336 |
regerror(res, regex, buf, len); |
| 337 |
ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf); |
| 338 |
return -1; |
| 339 |
} |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
static int asterisk_start_devicestate_publishing(struct ast_sip_outbound_publish *configuration, |
| 345 |
struct ast_sip_outbound_publish_client *client) |
| 346 |
{ |
| 347 |
RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup); |
| 348 |
struct asterisk_devicestate_publisher_state *publisher_state; |
| 349 |
const char *value; |
| 350 |
struct ao2_container *cached; |
| 351 |
|
| 352 |
datastore = ast_sip_publish_client_alloc_datastore(&asterisk_devicestate_publisher_state_datastore, |
| 353 |
"asterisk-devicestate-publisher"); |
| 354 |
if (!datastore) { |
| 355 |
return -1; |
| 356 |
} |
| 357 |
|
| 358 |
publisher_state = ast_calloc(1, sizeof(struct asterisk_devicestate_publisher_state)); |
| 359 |
if (!publisher_state) { |
| 360 |
return -1; |
| 361 |
} |
| 362 |
datastore->data = publisher_state; |
| 363 |
|
| 364 |
value = ast_sorcery_object_get_extended(configuration, "device_state_filter"); |
| 365 |
if (!ast_strlen_zero(value)) { |
| 366 |
if (build_regex(&publisher_state->device_state_regex, value)) { |
| 367 |
return -1; |
| 368 |
} |
| 369 |
publisher_state->device_state_filter = 1; |
| 370 |
} |
| 371 |
|
| 372 |
publisher_state->client = ao2_bump(client); |
| 373 |
|
| 374 |
if (ast_sip_publish_client_add_datastore(client, datastore)) { |
| 375 |
return -1; |
| 376 |
} |
| 377 |
|
| 378 |
publisher_state->device_state_subscription = stasis_subscribe(ast_device_state_topic_all(), |
| 379 |
asterisk_publisher_devstate_cb, ao2_bump(datastore)); |
| 380 |
if (!publisher_state->device_state_subscription) { |
| 381 |
ast_sip_publish_client_remove_datastore(client, "asterisk-devicestate-publisher"); |
| 382 |
ao2_ref(datastore, -1); |
| 383 |
return -1; |
| 384 |
} |
| 385 |
stasis_subscription_accept_message_type(publisher_state->device_state_subscription, ast_device_state_message_type()); |
| 386 |
stasis_subscription_accept_message_type(publisher_state->device_state_subscription, stasis_subscription_change_type()); |
| 387 |
stasis_subscription_set_filter(publisher_state->device_state_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 388 |
|
| 389 |
cached = stasis_cache_dump(ast_device_state_cache(), NULL); |
| 390 |
ao2_callback(cached, OBJ_NODATA, cached_devstate_cb, datastore); |
| 391 |
ao2_ref(cached, -1); |
| 392 |
|
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
static int asterisk_stop_devicestate_publishing(struct ast_sip_outbound_publish_client *client) |
| 397 |
{ |
| 398 |
RAII_VAR(struct ast_datastore *, datastore, ast_sip_publish_client_get_datastore(client, "asterisk-devicestate-publisher"), |
| 399 |
ao2_cleanup); |
| 400 |
struct asterisk_devicestate_publisher_state *publisher_state; |
| 401 |
|
| 402 |
if (!datastore) { |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
publisher_state = datastore->data; |
| 407 |
if (publisher_state->device_state_subscription) { |
| 408 |
stasis_unsubscribe_and_join(publisher_state->device_state_subscription); |
| 409 |
ao2_ref(datastore, -1); |
| 410 |
} |
| 411 |
|
| 412 |
ast_sip_publish_client_remove_datastore(client, "asterisk-devicestate-publisher"); |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
struct ast_sip_event_publisher_handler asterisk_devicestate_publisher_handler = { |
| 418 |
.event_name = "asterisk-devicestate", |
| 419 |
.start_publishing = asterisk_start_devicestate_publishing, |
| 420 |
.stop_publishing = asterisk_stop_devicestate_publishing, |
| 421 |
}; |
| 422 |
|
| 423 |
static int asterisk_start_mwi_publishing(struct ast_sip_outbound_publish *configuration, |
| 424 |
struct ast_sip_outbound_publish_client *client) |
| 425 |
{ |
| 426 |
RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup); |
| 427 |
struct asterisk_mwi_publisher_state *publisher_state; |
| 428 |
const char *value; |
| 429 |
struct ao2_container *cached; |
| 430 |
|
| 431 |
datastore = ast_sip_publish_client_alloc_datastore(&asterisk_mwi_publisher_state_datastore, "asterisk-mwi-publisher"); |
| 432 |
if (!datastore) { |
| 433 |
return -1; |
| 434 |
} |
| 435 |
|
| 436 |
publisher_state = ast_calloc(1, sizeof(struct asterisk_mwi_publisher_state)); |
| 437 |
if (!publisher_state) { |
| 438 |
return -1; |
| 439 |
} |
| 440 |
datastore->data = publisher_state; |
| 441 |
|
| 442 |
value = ast_sorcery_object_get_extended(configuration, "mailbox_state_filter"); |
| 443 |
if (!ast_strlen_zero(value)) { |
| 444 |
if (build_regex(&publisher_state->mailbox_state_regex, value)) { |
| 445 |
return -1; |
| 446 |
} |
| 447 |
publisher_state->mailbox_state_filter = 1; |
| 448 |
} |
| 449 |
|
| 450 |
publisher_state->client = ao2_bump(client); |
| 451 |
|
| 452 |
if (ast_sip_publish_client_add_datastore(client, datastore)) { |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
publisher_state->mailbox_state_subscription = stasis_subscribe(ast_mwi_topic_all(), |
| 457 |
asterisk_publisher_mwistate_cb, ao2_bump(datastore)); |
| 458 |
if (!publisher_state->mailbox_state_subscription) { |
| 459 |
ast_sip_publish_client_remove_datastore(client, "asterisk-mwi-publisher"); |
| 460 |
ao2_ref(datastore, -1); |
| 461 |
return -1; |
| 462 |
} |
| 463 |
stasis_subscription_accept_message_type(publisher_state->mailbox_state_subscription, ast_mwi_state_type()); |
| 464 |
stasis_subscription_accept_message_type(publisher_state->mailbox_state_subscription, stasis_subscription_change_type()); |
| 465 |
stasis_subscription_set_filter(publisher_state->mailbox_state_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 466 |
|
| 467 |
cached = stasis_cache_dump(ast_mwi_state_cache(), NULL); |
| 468 |
ao2_callback(cached, OBJ_NODATA, cached_mwistate_cb, datastore); |
| 469 |
ao2_ref(cached, -1); |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |
| 473 |
|
| 474 |
static int asterisk_stop_mwi_publishing(struct ast_sip_outbound_publish_client *client) |
| 475 |
{ |
| 476 |
RAII_VAR(struct ast_datastore *, datastore, ast_sip_publish_client_get_datastore(client, "asterisk-mwi-publisher"), |
| 477 |
ao2_cleanup); |
| 478 |
struct asterisk_mwi_publisher_state *publisher_state; |
| 479 |
|
| 480 |
if (!datastore) { |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
publisher_state = datastore->data; |
| 485 |
if (publisher_state->mailbox_state_subscription) { |
| 486 |
stasis_unsubscribe_and_join(publisher_state->mailbox_state_subscription); |
| 487 |
ao2_ref(datastore, -1); |
| 488 |
} |
| 489 |
|
| 490 |
ast_sip_publish_client_remove_datastore(client, "asterisk-mwi-publisher"); |
| 491 |
|
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
struct ast_sip_event_publisher_handler asterisk_mwi_publisher_handler = { |
| 496 |
.event_name = "asterisk-mwi", |
| 497 |
.start_publishing = asterisk_start_mwi_publishing, |
| 498 |
.stop_publishing = asterisk_stop_mwi_publishing, |
| 499 |
}; |
| 500 |
|
| 501 |
static int asterisk_publication_new(struct ast_sip_endpoint *endpoint, const char *resource, const char *event_configuration) |
| 502 |
{ |
| 503 |
RAII_VAR(struct asterisk_publication_config *, config, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "asterisk-publication", |
| 504 |
event_configuration), ao2_cleanup); |
| 505 |
|
| 506 |
|
| 507 |
if (!config) { |
| 508 |
return 404; |
| 509 |
} |
| 510 |
|
| 511 |
return 200; |
| 512 |
} |
| 513 |
|
| 514 |
static int asterisk_publication_devicestate(struct ast_sip_publication *pub, struct asterisk_publication_config *config, |
| 515 |
struct ast_eid *pubsub_eid, struct ast_json *json) |
| 516 |
{ |
| 517 |
const char *device = ast_json_string_get(ast_json_object_get(json, "device")); |
| 518 |
const char *state = ast_json_string_get(ast_json_object_get(json, "state")); |
| 519 |
int cachable = ast_json_integer_get(ast_json_object_get(json, "cachable")); |
| 520 |
|
| 521 |
if (!config->device_state) { |
| 522 |
ast_debug(2, "Received device state event for resource '%s' but it is not configured to accept them\n", |
| 523 |
ast_sorcery_object_get_id(config)); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
if (ast_strlen_zero(device) || ast_strlen_zero(state)) { |
| 528 |
ast_debug(1, "Received incomplete device state event for resource '%s'\n", |
| 529 |
ast_sorcery_object_get_id(config)); |
| 530 |
return -1; |
| 531 |
} |
| 532 |
|
| 533 |
if (config->device_state_filter && regexec(&config->device_state_regex, device, 0, NULL, 0)) { |
| 534 |
ast_debug(2, "Received device state on resource '%s' for device '%s' but it has been filtered out\n", |
| 535 |
ast_sorcery_object_get_id(config), device); |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
ast_publish_device_state_full(device, ast_devstate_val(state), |
| 540 |
cachable == AST_DEVSTATE_CACHABLE ? AST_DEVSTATE_CACHABLE : AST_DEVSTATE_NOT_CACHABLE, |
| 541 |
pubsub_eid); |
| 542 |
|
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static int asterisk_publication_mailboxstate(struct ast_sip_publication *pub, struct asterisk_publication_config *config, |
| 547 |
struct ast_eid *pubsub_eid, struct ast_json *json) |
| 548 |
{ |
| 549 |
const char *uniqueid = ast_json_string_get(ast_json_object_get(json, "uniqueid")); |
| 550 |
int old_msgs = ast_json_integer_get(ast_json_object_get(json, "old")); |
| 551 |
int new_msgs = ast_json_integer_get(ast_json_object_get(json, "new")); |
| 552 |
char *item_id; |
| 553 |
const char *mailbox; |
| 554 |
|
| 555 |
if (!config->mailbox_state) { |
| 556 |
ast_debug(2, "Received mailbox state event for resource '%s' but it is not configured to accept them\n", |
| 557 |
ast_sorcery_object_get_id(config)); |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
if (ast_strlen_zero(uniqueid)) { |
| 562 |
ast_debug(1, "Received incomplete mailbox state event for resource '%s'\n", |
| 563 |
ast_sorcery_object_get_id(config)); |
| 564 |
return -1; |
| 565 |
} |
| 566 |
|
| 567 |
if (config->mailbox_state_filter && regexec(&config->mailbox_state_regex, uniqueid, 0, NULL, 0)) { |
| 568 |
ast_debug(2, "Received mailbox state on resource '%s' for uniqueid '%s' but it has been filtered out\n", |
| 569 |
ast_sorcery_object_get_id(config), uniqueid); |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
item_id = ast_strdupa(uniqueid); |
| 574 |
mailbox = strsep(&item_id, "@"); |
| 575 |
|
| 576 |
ast_publish_mwi_state_full(mailbox, item_id, new_msgs, old_msgs, NULL, pubsub_eid); |
| 577 |
|
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
static int asterisk_publication_devicestate_refresh(struct ast_sip_publication *pub, |
| 582 |
struct asterisk_publication_config *config, struct ast_eid *pubsub_eid, struct ast_json *json) |
| 583 |
{ |
| 584 |
struct ast_sip_outbound_publish_client *client; |
| 585 |
struct ast_datastore *datastore; |
| 586 |
struct ao2_container *cached; |
| 587 |
|
| 588 |
if (ast_strlen_zero(config->devicestate_publish)) { |
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
client = ast_sip_publish_client_get(config->devicestate_publish); |
| 593 |
if (!client) { |
| 594 |
ast_log(LOG_ERROR, "Received refresh request for devicestate on publication '%s' but publish '%s' is not available\n", |
| 595 |
ast_sorcery_object_get_id(config), config->devicestate_publish); |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
datastore = ast_sip_publish_client_get_datastore(client, "asterisk-devicestate-publisher"); |
| 600 |
if (!datastore) { |
| 601 |
ao2_ref(client, -1); |
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
cached = stasis_cache_dump(ast_device_state_cache(), NULL); |
| 606 |
if (cached) { |
| 607 |
ao2_callback(cached, OBJ_NODATA, cached_devstate_cb, datastore); |
| 608 |
ao2_ref(cached, -1); |
| 609 |
} |
| 610 |
ao2_ref(client, -1); |
| 611 |
ao2_ref(datastore, -1); |
| 612 |
|
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
static int asterisk_publication_devicestate_state_change(struct ast_sip_publication *pub, pjsip_msg_body *body, |
| 617 |
enum ast_sip_publish_state state) |
| 618 |
{ |
| 619 |
RAII_VAR(struct asterisk_publication_config *, config, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "asterisk-publication", |
| 620 |
ast_sip_publication_get_event_configuration(pub)), ao2_cleanup); |
| 621 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 622 |
const char *eid, *type; |
| 623 |
struct ast_eid pubsub_eid; |
| 624 |
int res = -1; |
| 625 |
|
| 626 |
|
| 627 |
if (!config) { |
| 628 |
return -1; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
if (!body) { |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
if (!ast_sip_is_content_type(&body->content_type, "application", "json")) { |
| 638 |
ast_debug(2, "Received unsupported content type for Asterisk event on resource '%s'\n", |
| 639 |
ast_sorcery_object_get_id(config)); |
| 640 |
return -1; |
| 641 |
} |
| 642 |
|
| 643 |
json = ast_json_load_buf(body->data, body->len, NULL); |
| 644 |
if (!json) { |
| 645 |
ast_debug(1, "Received unparseable JSON event for resource '%s'\n", |
| 646 |
ast_sorcery_object_get_id(config)); |
| 647 |
return -1; |
| 648 |
} |
| 649 |
|
| 650 |
eid = ast_json_string_get(ast_json_object_get(json, "eid")); |
| 651 |
if (!eid) { |
| 652 |
ast_debug(1, "Received event without eid for resource '%s'\n", |
| 653 |
ast_sorcery_object_get_id(config)); |
| 654 |
return -1; |
| 655 |
} |
| 656 |
ast_str_to_eid(&pubsub_eid, eid); |
| 657 |
|
| 658 |
type = ast_json_string_get(ast_json_object_get(json, "type")); |
| 659 |
if (!type) { |
| 660 |
ast_debug(1, "Received event without type for resource '%s'\n", |
| 661 |
ast_sorcery_object_get_id(config)); |
| 662 |
return -1; |
| 663 |
} else if (!strcmp(type, "devicestate")) { |
| 664 |
res = asterisk_publication_devicestate(pub, config, &pubsub_eid, json); |
| 665 |
} else if (!strcmp(type, "refresh")) { |
| 666 |
res = asterisk_publication_devicestate_refresh(pub, config, &pubsub_eid, json); |
| 667 |
} |
| 668 |
|
| 669 |
return res; |
| 670 |
} |
| 671 |
|
| 672 |
static int asterisk_publication_mwi_refresh(struct ast_sip_publication *pub, |
| 673 |
struct asterisk_publication_config *config, struct ast_eid *pubsub_eid, struct ast_json *json) |
| 674 |
{ |
| 675 |
struct ast_sip_outbound_publish_client *client; |
| 676 |
struct ast_datastore *datastore; |
| 677 |
struct ao2_container *cached; |
| 678 |
|
| 679 |
if (ast_strlen_zero(config->mailboxstate_publish)) { |
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
client = ast_sip_publish_client_get(config->mailboxstate_publish); |
| 684 |
if (!client) { |
| 685 |
ast_log(LOG_ERROR, "Received refresh request for mwi state on publication '%s' but publish '%s' is not available\n", |
| 686 |
ast_sorcery_object_get_id(config), config->mailboxstate_publish); |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
datastore = ast_sip_publish_client_get_datastore(client, "asterisk-mwi-publisher"); |
| 691 |
if (!datastore) { |
| 692 |
ao2_ref(client, -1); |
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
cached = stasis_cache_dump(ast_mwi_state_cache(), NULL); |
| 697 |
if (cached) { |
| 698 |
ao2_callback(cached, OBJ_NODATA, cached_mwistate_cb, datastore); |
| 699 |
ao2_ref(cached, -1); |
| 700 |
} |
| 701 |
ao2_ref(client, -1); |
| 702 |
ao2_ref(datastore, -1); |
| 703 |
|
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
static int asterisk_publication_mwi_state_change(struct ast_sip_publication *pub, pjsip_msg_body *body, |
| 708 |
enum ast_sip_publish_state state) |
| 709 |
{ |
| 710 |
RAII_VAR(struct asterisk_publication_config *, config, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "asterisk-publication", |
| 711 |
ast_sip_publication_get_event_configuration(pub)), ao2_cleanup); |
| 712 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 713 |
const char *eid, *type; |
| 714 |
struct ast_eid pubsub_eid; |
| 715 |
int res = -1; |
| 716 |
|
| 717 |
|
| 718 |
if (!config) { |
| 719 |
return -1; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
if (!body) { |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
if (!ast_sip_is_content_type(&body->content_type, "application", "json")) { |
| 729 |
ast_debug(2, "Received unsupported content type for Asterisk event on resource '%s'\n", |
| 730 |
ast_sorcery_object_get_id(config)); |
| 731 |
return -1; |
| 732 |
} |
| 733 |
|
| 734 |
json = ast_json_load_buf(body->data, body->len, NULL); |
| 735 |
if (!json) { |
| 736 |
ast_debug(1, "Received unparseable JSON event for resource '%s'\n", |
| 737 |
ast_sorcery_object_get_id(config)); |
| 738 |
return -1; |
| 739 |
} |
| 740 |
|
| 741 |
eid = ast_json_string_get(ast_json_object_get(json, "eid")); |
| 742 |
if (!eid) { |
| 743 |
ast_debug(1, "Received event without eid for resource '%s'\n", |
| 744 |
ast_sorcery_object_get_id(config)); |
| 745 |
return -1; |
| 746 |
} |
| 747 |
ast_str_to_eid(&pubsub_eid, eid); |
| 748 |
|
| 749 |
type = ast_json_string_get(ast_json_object_get(json, "type")); |
| 750 |
if (!type) { |
| 751 |
ast_debug(1, "Received event without type for resource '%s'\n", |
| 752 |
ast_sorcery_object_get_id(config)); |
| 753 |
return -1; |
| 754 |
} else if (!strcmp(type, "mailboxstate")) { |
| 755 |
res = asterisk_publication_mailboxstate(pub, config, &pubsub_eid, json); |
| 756 |
} else if (!strcmp(type, "refresh")) { |
| 757 |
res = asterisk_publication_mwi_refresh(pub, config, &pubsub_eid, json); |
| 758 |
} |
| 759 |
|
| 760 |
return res; |
| 761 |
} |
| 762 |
|
| 763 |
static int send_refresh_cb(void *obj, void *arg, int flags) |
| 764 |
{ |
| 765 |
struct asterisk_publication_config *config = obj; |
| 766 |
struct ast_sip_outbound_publish_client *client; |
| 767 |
|
| 768 |
if (!ast_strlen_zero(config->devicestate_publish)) { |
| 769 |
client = ast_sip_publish_client_get(config->devicestate_publish); |
| 770 |
if (client) { |
| 771 |
ast_sip_publish_client_send(client, arg); |
| 772 |
ao2_ref(client, -1); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
if (!ast_strlen_zero(config->mailboxstate_publish)) { |
| 777 |
client = ast_sip_publish_client_get(config->mailboxstate_publish); |
| 778 |
if (client) { |
| 779 |
ast_sip_publish_client_send(client, arg); |
| 780 |
ao2_ref(client, -1); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
static void asterisk_publication_send_refresh(void) |
| 789 |
{ |
| 790 |
struct ao2_container *publications = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "asterisk-publication", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 791 |
char eid_str[20]; |
| 792 |
struct ast_json *json; |
| 793 |
char *text; |
| 794 |
struct ast_sip_body body = { |
| 795 |
.type = "application", |
| 796 |
.subtype = "json", |
| 797 |
}; |
| 798 |
|
| 799 |
if (!publications) { |
| 800 |
return; |
| 801 |
} |
| 802 |
|
| 803 |
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); |
| 804 |
json = ast_json_pack( |
| 805 |
"{ s: s, s: s }", |
| 806 |
"type", "refresh", |
| 807 |
"eid", eid_str); |
| 808 |
if (!json) { |
| 809 |
ao2_ref(publications, -1); |
| 810 |
return; |
| 811 |
} |
| 812 |
|
| 813 |
text = ast_json_dump_string(json); |
| 814 |
if (!text) { |
| 815 |
ast_json_unref(json); |
| 816 |
ao2_ref(publications, -1); |
| 817 |
return; |
| 818 |
} |
| 819 |
body.body_text = text; |
| 820 |
|
| 821 |
ao2_callback(publications, OBJ_NODATA, send_refresh_cb, &body); |
| 822 |
|
| 823 |
ast_json_free(text); |
| 824 |
ast_json_unref(json); |
| 825 |
ao2_ref(publications, -1); |
| 826 |
} |
| 827 |
|
| 828 |
struct ast_sip_publish_handler asterisk_devicestate_publication_handler = { |
| 829 |
.event_name = "asterisk-devicestate", |
| 830 |
.new_publication = asterisk_publication_new, |
| 831 |
.publication_state_change = asterisk_publication_devicestate_state_change, |
| 832 |
}; |
| 833 |
|
| 834 |
struct ast_sip_publish_handler asterisk_mwi_publication_handler = { |
| 835 |
.event_name = "asterisk-mwi", |
| 836 |
.new_publication = asterisk_publication_new, |
| 837 |
.publication_state_change = asterisk_publication_mwi_state_change, |
| 838 |
}; |
| 839 |
|
| 840 |
|
| 841 |
static void asterisk_publication_config_destroy(void *obj) |
| 842 |
{ |
| 843 |
struct asterisk_publication_config *config = obj; |
| 844 |
|
| 845 |
ast_string_field_free_memory(config); |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
static void *asterisk_publication_config_alloc(const char *name) |
| 850 |
{ |
| 851 |
struct asterisk_publication_config *config = ast_sorcery_generic_alloc(sizeof(*config), |
| 852 |
asterisk_publication_config_destroy); |
| 853 |
|
| 854 |
if (!config || ast_string_field_init(config, 256)) { |
| 855 |
ao2_cleanup(config); |
| 856 |
return NULL; |
| 857 |
} |
| 858 |
|
| 859 |
return config; |
| 860 |
} |
| 861 |
|
| 862 |
static int regex_filter_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 863 |
{ |
| 864 |
struct asterisk_publication_config *config = obj; |
| 865 |
int res = -1; |
| 866 |
|
| 867 |
if (ast_strlen_zero(var->value)) { |
| 868 |
return 0; |
| 869 |
} |
| 870 |
|
| 871 |
if (!strcmp(var->name, "device_state_filter")) { |
| 872 |
if (!(res = build_regex(&config->device_state_regex, var->value))) { |
| 873 |
config->device_state_filter = 1; |
| 874 |
} |
| 875 |
} else if (!strcmp(var->name, "mailbox_state_filter")) { |
| 876 |
if (!(res = build_regex(&config->mailbox_state_regex, var->value))) { |
| 877 |
config->mailbox_state_filter = 1; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return res; |
| 882 |
} |
| 883 |
|
| 884 |
static int load_module(void) |
| 885 |
{ |
| 886 |
if (ast_eid_is_empty(&ast_eid_default)) { |
| 887 |
ast_log(LOG_ERROR, "Entity ID is not set.\n"); |
| 888 |
return AST_MODULE_LOAD_DECLINE; |
| 889 |
} |
| 890 |
|
| 891 |
ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_publish_asterisk"); |
| 892 |
ast_sorcery_apply_default(ast_sip_get_sorcery(), "asterisk-publication", "config", "pjsip.conf,criteria=type=asterisk-publication"); |
| 893 |
|
| 894 |
if (ast_sorcery_object_register(ast_sip_get_sorcery(), "asterisk-publication", asterisk_publication_config_alloc, NULL, NULL)) { |
| 895 |
ast_log(LOG_ERROR, "Unable to register 'asterisk-publication' type with sorcery\n"); |
| 896 |
return AST_MODULE_LOAD_DECLINE; |
| 897 |
} |
| 898 |
|
| 899 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "asterisk-publication", "type", "", OPT_NOOP_T, 0, 0); |
| 900 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "asterisk-publication", "devicestate_publish", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct asterisk_publication_config, devicestate_publish)); |
| 901 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "asterisk-publication", "mailboxstate_publish", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct asterisk_publication_config, mailboxstate_publish)); |
| 902 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "asterisk-publication", "device_state", "no", OPT_BOOL_T, 1, FLDSET(struct asterisk_publication_config, device_state)); |
| 903 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "asterisk-publication", "device_state_filter", "", regex_filter_handler, NULL, NULL, 0, 0); |
| 904 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "asterisk-publication", "mailbox_state", "no", OPT_BOOL_T, 1, FLDSET(struct asterisk_publication_config, mailbox_state)); |
| 905 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "asterisk-publication", "mailbox_state_filter", "", regex_filter_handler, NULL, NULL, 0, 0); |
| 906 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "asterisk-publication"); |
| 907 |
|
| 908 |
if (ast_sip_register_publish_handler(&asterisk_devicestate_publication_handler)) { |
| 909 |
ast_log(LOG_WARNING, "Unable to register event publication handler %s\n", |
| 910 |
asterisk_devicestate_publication_handler.event_name); |
| 911 |
return AST_MODULE_LOAD_DECLINE; |
| 912 |
} |
| 913 |
if (ast_sip_register_publish_handler(&asterisk_mwi_publication_handler)) { |
| 914 |
ast_log(LOG_WARNING, "Unable to register event publication handler %s\n", |
| 915 |
asterisk_mwi_publication_handler.event_name); |
| 916 |
ast_sip_unregister_publish_handler(&asterisk_devicestate_publication_handler); |
| 917 |
return AST_MODULE_LOAD_DECLINE; |
| 918 |
} |
| 919 |
if (ast_sip_register_event_publisher_handler(&asterisk_devicestate_publisher_handler)) { |
| 920 |
ast_log(LOG_WARNING, "Unable to register event publisher handler %s\n", |
| 921 |
asterisk_devicestate_publisher_handler.event_name); |
| 922 |
ast_sip_unregister_publish_handler(&asterisk_devicestate_publication_handler); |
| 923 |
ast_sip_unregister_publish_handler(&asterisk_mwi_publication_handler); |
| 924 |
return AST_MODULE_LOAD_DECLINE; |
| 925 |
} |
| 926 |
if (ast_sip_register_event_publisher_handler(&asterisk_mwi_publisher_handler)) { |
| 927 |
ast_log(LOG_WARNING, "Unable to register event publisher handler %s\n", |
| 928 |
asterisk_mwi_publisher_handler.event_name); |
| 929 |
ast_sip_unregister_event_publisher_handler(&asterisk_mwi_publisher_handler); |
| 930 |
ast_sip_unregister_publish_handler(&asterisk_devicestate_publication_handler); |
| 931 |
ast_sip_unregister_publish_handler(&asterisk_mwi_publication_handler); |
| 932 |
return AST_MODULE_LOAD_DECLINE; |
| 933 |
} |
| 934 |
|
| 935 |
asterisk_publication_send_refresh(); |
| 936 |
|
| 937 |
return AST_MODULE_LOAD_SUCCESS; |
| 938 |
} |
| 939 |
|
| 940 |
static int reload_module(void) |
| 941 |
{ |
| 942 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "asterisk-publication"); |
| 943 |
asterisk_publication_send_refresh(); |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
static int unload_module(void) |
| 948 |
{ |
| 949 |
ast_sip_unregister_publish_handler(&asterisk_devicestate_publication_handler); |
| 950 |
ast_sip_unregister_publish_handler(&asterisk_mwi_publication_handler); |
| 951 |
ast_sip_unregister_event_publisher_handler(&asterisk_devicestate_publisher_handler); |
| 952 |
ast_sip_unregister_event_publisher_handler(&asterisk_mwi_publisher_handler); |
| 953 |
ast_sorcery_object_unregister(ast_sip_get_sorcery(), "asterisk-publication"); |
| 954 |
return 0; |
| 955 |
} |
| 956 |
|
| 957 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Asterisk Event PUBLISH Support", |
| 958 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 959 |
.load = load_module, |
| 960 |
.reload = reload_module, |
| 961 |
.unload = unload_module, |
| 962 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND + 5, |
| 963 |
.requires = "res_pjsip,res_pjsip_outbound_publish,res_pjsip_pubsub", |
| 964 |
); |