| 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 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "asterisk/manager.h" |
| 29 |
#include "asterisk/app.h" |
| 30 |
#include "asterisk/mwi.h" |
| 31 |
#include "asterisk/channel.h" |
| 32 |
#include "asterisk/stasis_message_router.h" |
| 33 |
#include "asterisk/stasis.h" |
| 34 |
|
| 35 |
struct stasis_message_router *mwi_state_router; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static struct stasis_forward *topic_forwarder; |
| 44 |
|
| 45 |
|
| 46 |
static int exclude_event_cb(const char *key) |
| 47 |
{ |
| 48 |
if (!strcmp(key, "Event")) { |
| 49 |
return -1; |
| 50 |
} |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static void mwi_app_event_cb(void *data, struct stasis_subscription *sub, |
| 56 |
struct stasis_message *message) |
| 57 |
{ |
| 58 |
struct ast_mwi_blob *payload = stasis_message_data(message); |
| 59 |
RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free); |
| 60 |
RAII_VAR(struct ast_str *, event_buffer, NULL, ast_free); |
| 61 |
struct ast_json *event_json = ast_json_object_get(payload->blob, "Event"); |
| 62 |
|
| 63 |
if (!event_json) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if (payload->mwi_state && payload->mwi_state->snapshot) { |
| 68 |
channel_event_string = ast_manager_build_channel_state_string(payload->mwi_state->snapshot); |
| 69 |
} |
| 70 |
|
| 71 |
event_buffer = ast_manager_str_from_json_object(payload->blob, exclude_event_cb); |
| 72 |
if (!event_buffer) { |
| 73 |
ast_log(AST_LOG_WARNING, "Failed to create payload for event %s\n", ast_json_string_get(event_json)); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
manager_event(EVENT_FLAG_CALL, ast_json_string_get(event_json), |
| 78 |
"Mailbox: %s\r\n" |
| 79 |
"%s" |
| 80 |
"%s", |
| 81 |
payload->mwi_state ? payload->mwi_state->uniqueid : "Unknown", |
| 82 |
ast_str_buffer(event_buffer), |
| 83 |
channel_event_string ? ast_str_buffer(channel_event_string) : ""); |
| 84 |
} |
| 85 |
|
| 86 |
static void mwi_update_cb(void *data, struct stasis_subscription *sub, |
| 87 |
struct stasis_message *message) |
| 88 |
{ |
| 89 |
struct ast_mwi_state *mwi_state; |
| 90 |
RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free); |
| 91 |
|
| 92 |
if (ast_mwi_state_type() != stasis_message_type(message)) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
mwi_state = stasis_message_data(message); |
| 97 |
if (!mwi_state) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if (mwi_state->snapshot) { |
| 102 |
channel_event_string = ast_manager_build_channel_state_string(mwi_state->snapshot); |
| 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 |
manager_event(EVENT_FLAG_CALL, "MessageWaiting", |
| 142 |
"%s" |
| 143 |
"Mailbox: %s\r\n" |
| 144 |
"Waiting: %d\r\n" |
| 145 |
"New: %d\r\n" |
| 146 |
"Old: %d\r\n", |
| 147 |
AS_OR(channel_event_string, ""), |
| 148 |
mwi_state->uniqueid, |
| 149 |
ast_app_has_voicemail(mwi_state->uniqueid, NULL), |
| 150 |
mwi_state->new_msgs, |
| 151 |
mwi_state->old_msgs); |
| 152 |
} |
| 153 |
|
| 154 |
static void manager_mwi_shutdown(void) |
| 155 |
{ |
| 156 |
stasis_forward_cancel(topic_forwarder); |
| 157 |
topic_forwarder = NULL; |
| 158 |
} |
| 159 |
|
| 160 |
int manager_mwi_init(void) |
| 161 |
{ |
| 162 |
int ret = 0; |
| 163 |
struct stasis_topic *manager_topic; |
| 164 |
struct stasis_topic *mwi_topic; |
| 165 |
struct stasis_message_router *message_router; |
| 166 |
|
| 167 |
manager_topic = ast_manager_get_topic(); |
| 168 |
if (!manager_topic) { |
| 169 |
return -1; |
| 170 |
} |
| 171 |
message_router = ast_manager_get_message_router(); |
| 172 |
if (!message_router) { |
| 173 |
return -1; |
| 174 |
} |
| 175 |
mwi_topic = ast_mwi_topic_all(); |
| 176 |
if (!mwi_topic) { |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
topic_forwarder = stasis_forward_all(mwi_topic, manager_topic); |
| 181 |
if (!topic_forwarder) { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
ast_register_cleanup(manager_mwi_shutdown); |
| 186 |
|
| 187 |
ret |= stasis_message_router_add(message_router, |
| 188 |
ast_mwi_state_type(), |
| 189 |
mwi_update_cb, |
| 190 |
NULL); |
| 191 |
|
| 192 |
ret |= stasis_message_router_add(message_router, |
| 193 |
ast_mwi_vm_app_type(), |
| 194 |
mwi_app_event_cb, |
| 195 |
NULL); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if (ret) { |
| 201 |
manager_mwi_shutdown(); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |