diff --git a/configs/samples/manager.conf.sample b/configs/samples/manager.conf.sample index 38aa04232f..7aa22a9547 100644 --- a/configs/samples/manager.conf.sample +++ b/configs/samples/manager.conf.sample @@ -333,7 +333,8 @@ Both Legacy and Advanced filter entries are processed as follows: ; Suite. Note that this is only enabled when the TEST_FRAMEWORK ; compiler flag is defined. ; security - Security Events. Read-only. -; message - Permissions to send out of call messages. Write-only +; message - Permissions to send out of call messages and receive delivery +; notifications. ; ;read = system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan ;write = system,call,agent,user,config,command,reporting,originate,message diff --git a/include/asterisk/message.h b/include/asterisk/message.h index f858a9ecda..d45dd415e2 100644 --- a/include/asterisk/message.h +++ b/include/asterisk/message.h @@ -557,6 +557,17 @@ const char *ast_msg_data_get_attribute(struct ast_msg_data *msg, */ int ast_msg_data_queue_frame(struct ast_channel *channel, struct ast_msg_data *msg); +/*! + * \brief Raise a message delivery event + * + * \param delivered Whether or not the message was delivered (1 = delivered; 0 = failed; + * -1 dispatched, but delivery status unknown). + * \param status_code The status code, if one was received from the recipient. + * \param delivery_id The unique delivery ID, if one was created by action_messagesend. + */ +void ast_msg_event(const char *to, const char *from, const char *endpoint_name, + int delivered, int status_code, const char *delivery_id); + /*! * @} */ diff --git a/main/message.c b/main/message.c index 496d8b234c..1d646da0c7 100644 --- a/main/message.c +++ b/main/message.c @@ -41,6 +41,7 @@ #include "asterisk/app.h" #include "asterisk/taskprocessor.h" #include "asterisk/message.h" +#include "asterisk/uuid.h" /*** DOCUMENTATION @@ -241,6 +242,57 @@ + + + + Raised after the final attempt to deliver a text (out of call) message. + + + + + + + If the message was sent via AMI, a unique DeliveryID is generated + and included in both the acknowledgement and the delivery event. + + + + + + The endpoint responded with a success (2xx) status + code. + + + + The endpoint either rejected the message, or did + not respond. + + + + The message technology dispatched the message, but + was not able to determine if it was successfully delivered. + + + + + + + The status code received in response to the message, or 0 if + none was received. + + + + + This enables external systems connected to AMI (e.g., monitoring tools, + SMS gateways, or other custom applications) to observe the outcome (delivery, + rejection, time out) of SIP MESSAGE requests and take appropriate action. + + + + MessageSend + + + ***/ struct msg_data { @@ -1246,6 +1298,19 @@ static const struct ast_msg_handler *msg_handler_find_by_tech_name(const char *t return NULL; } +void ast_msg_event(const char *to, const char *from, const char *endpoint_name, + int delivered, int status_code, const char *delivery_id) +{ + manager_event(EVENT_FLAG_MESSAGE, "TextMessageDeliveryStatus", + "To: %s\r\n" + "From: %s\r\n" + "Endpoint: %s\r\n" + "Delivered: %d\r\n" + "StatusCode: %d\r\n" + "DeliveryID: %s\r\n", + to, from, endpoint_name, delivered, status_code, delivery_id); +} + /*! * \internal * \brief MessageSend() application @@ -1321,6 +1386,12 @@ static int msg_send_exec(struct ast_channel *chan, const char *data) res = msg_tech->msg_send(msg, S_OR(args.destination, ""), S_OR(args.from, "")); ao2_unlock(msg); + if (res == 1) { + res = 0; + } else if (res == 0) { + ast_msg_event(S_OR(args.destination, ""), S_OR(args.from, ""), "", -1, 0, ""); + } + pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", res ? "FAILURE" : "SUCCESS"); exit_cleanup: @@ -1337,6 +1408,7 @@ static int action_messagesend(struct mansession *s, const struct message *m) const char *from = astman_get_header(m, "From"); const char *body = astman_get_header(m, "Body"); const char *base64body = astman_get_header(m, "Base64Body"); + const char *action_id = astman_get_header(m, "ActionID"); const char *to_override = NULL; char base64decoded[1301] = { 0, }; char *tech_name = NULL; @@ -1345,6 +1417,7 @@ static int action_messagesend(struct mansession *s, const struct message *m) const struct ast_msg_tech *msg_tech; struct ast_msg *msg; int res = -1; + char delivery_id[AST_UUID_STR_LEN] = ""; if (!ast_strlen_zero(destination)) { if (!ast_strlen_zero(to)) { @@ -1385,6 +1458,15 @@ static int action_messagesend(struct mansession *s, const struct message *m) ast_msg_set_var_outbound(msg, vars->name, vars->value); } + /* + * The delivery event cannot be sent before the response is sent to the AMI + * client. Therefore, generating the DeliveryID here is safe, provided the + * AMI client does not internally process delivery events before receiving + * the response. + */ + ast_uuid_generate_str(delivery_id, sizeof(delivery_id)); + ast_msg_set_var(msg, "DeliveryID", delivery_id); + ast_msg_set_body(msg, "%s", body); if (to_override) { @@ -1398,10 +1480,23 @@ static int action_messagesend(struct mansession *s, const struct message *m) ast_variables_destroy(vars); ao2_ref(msg, -1); + if (res == 1) { + res = 0; + } else if (res == 0) { + ast_msg_event(S_OR(to, ""), S_OR(from, ""), "", -1, 0, delivery_id); + } + if (res) { astman_send_error(s, m, "Message failed to send."); } else { - astman_send_ack(s, m, "Message successfully sent"); + astman_append(s, "Response: Success\r\n"); + if (!ast_strlen_zero(action_id)) { + astman_append(s, "ActionID: %s\r\n", action_id); + } + astman_append(s, "Message: Message successfully sent\r\n" + "DeliveryID: %s\r\n" + "\r\n", + delivery_id); } return 0; } @@ -1433,6 +1528,12 @@ int ast_msg_send(struct ast_msg *msg, const char *to, const char *from) ast_rwlock_unlock(&msg_techs_lock); + if (res == 1) { + res = 0; + } else if (res == 0) { + ast_msg_event(S_OR(to, ""), S_OR(from, ""), "", -1, 0, ""); + } + ao2_ref(msg, -1); return res; diff --git a/res/res_pjsip_messaging.c b/res/res_pjsip_messaging.c index ddcc8bb945..508a289305 100644 --- a/res/res_pjsip_messaging.c +++ b/res/res_pjsip_messaging.c @@ -122,6 +122,7 @@ #include #include +#include "asterisk/manager.h" #include "asterisk/message.h" #include "asterisk/module.h" #include "asterisk/pbx.h" @@ -255,6 +256,7 @@ static int is_msg_var_blocked(const char *name) "Content-Length", "Content-Type", "Request-URI", + "DeliveryID", }; for (i = 0; i < ARRAY_LEN(hdr); ++i) { @@ -590,6 +592,7 @@ struct message_response_data { char *to; char *body; char *content_type; + char *delivery_id; struct ast_sip_redirect_state *redirect_state; }; @@ -601,6 +604,7 @@ static void message_response_data_destroy(void *obj) ast_free(resp_data->to); ast_free(resp_data->body); ast_free(resp_data->content_type); + ast_free(resp_data->delivery_id); if (resp_data->redirect_state) { ast_sip_redirect_state_destroy(resp_data->redirect_state); @@ -613,7 +617,8 @@ static struct message_response_data *message_response_data_create( const char *to, const char *body, const char *content_type, - const char *initial_uri) + const char *initial_uri, + const char *delivery_id) { struct message_response_data *resp_data; @@ -626,8 +631,9 @@ static struct message_response_data *message_response_data_create( resp_data->to = ast_strdup(to); resp_data->body = ast_strdup(body); resp_data->content_type = ast_strdup(content_type); + resp_data->delivery_id = ast_strdup(S_OR(delivery_id, "")); - if (!resp_data->from || !resp_data->to || !resp_data->body || !resp_data->content_type) { + if (!resp_data->from || !resp_data->to || !resp_data->body || !resp_data->content_type || !resp_data->delivery_id) { ast_log(LOG_ERROR, "Failed to allocate memory for response data strings on endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint)); ao2_ref(resp_data, -1); @@ -753,9 +759,11 @@ static int send_message_to_uri(struct message_response_data *resp_data, const ch new_resp_data->to = ast_strdup(resp_data->to); new_resp_data->body = ast_strdup(resp_data->body); new_resp_data->content_type = ast_strdup(resp_data->content_type); + new_resp_data->delivery_id = ast_strdup(resp_data->delivery_id); /* Check for allocation failures */ - if (!new_resp_data->from || !new_resp_data->to || !new_resp_data->body || !new_resp_data->content_type) { + if (!new_resp_data->from || !new_resp_data->to || !new_resp_data->body || + !new_resp_data->content_type || !new_resp_data->delivery_id) { pjsip_tx_data_dec_ref(tdata); ao2_ref(new_resp_data, -1); ast_log(LOG_ERROR, "Failed to allocate memory for redirect callback strings for endpoint '%s'.\n", @@ -769,9 +777,9 @@ static int send_message_to_uri(struct message_response_data *resp_data, const ch /* Send with callback for potential further redirects */ if (ast_sip_send_request(tdata, NULL, endpoint, new_resp_data, msg_response_callback)) { - ao2_ref(new_resp_data, -1); ast_log(LOG_ERROR, "Failed to send redirect request to '%s' on endpoint '%s'.\n", new_resp_data->to, ast_sorcery_object_get_id(endpoint)); + ao2_ref(new_resp_data, -1); return -1; } @@ -784,29 +792,32 @@ static int send_message_to_uri(struct message_response_data *resp_data, const ch * * \param resp_data Response callback data * \param rdata The redirect response data + * + * \return 0: success, -1: failure */ -static void handle_message_redirect(struct message_response_data *resp_data, pjsip_rx_data *rdata) +static int handle_message_redirect(struct message_response_data *resp_data, pjsip_rx_data *rdata) { char *uri = NULL; int hop_count; + int response; if (!resp_data->redirect_state) { ast_log(LOG_ERROR, "MESSAGE redirect: no redirect state available\n"); - return; + return -1; } /* Parse the redirect response and extract contacts */ if (ast_sip_redirect_parse_3xx(rdata, resp_data->redirect_state)) { ast_debug(1, "MESSAGE redirect on endpoint '%s': not following redirect (parse failed or conditions not met)\n", ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state))); - return; + return -1; } /* Get the first URI to try */ if (ast_sip_redirect_get_next_uri(resp_data->redirect_state, &uri)) { ast_log(LOG_WARNING, "MESSAGE redirect on endpoint '%s': no valid URIs to try\n", ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state))); - return; + return -1; } hop_count = ast_sip_redirect_get_hop_count(resp_data->redirect_state); @@ -814,8 +825,9 @@ static void handle_message_redirect(struct message_response_data *resp_data, pjs ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state)), uri, hop_count, AST_SIP_MAX_REDIRECT_HOPS); /* Try the first contact */ - send_message_to_uri(resp_data, uri); + response = send_message_to_uri(resp_data, uri); ast_free(uri); + return response; } /*! @@ -841,8 +853,14 @@ static void msg_response_callback(void *token, pjsip_event *e) if (state && !ast_sip_redirect_get_next_uri(state, &next_uri)) { ast_log(LOG_NOTICE, "MESSAGE timed out on endpoint '%s', trying next Contact: '%s'\n", ast_sorcery_object_get_id(endpoint), next_uri); - send_message_to_uri(resp_data, next_uri); + if (send_message_to_uri(resp_data, next_uri) == -1) { + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 0, 0, resp_data->delivery_id); + } ast_free(next_uri); + } else { + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 0, 0, resp_data->delivery_id); } ao2_ref(resp_data, -1); return; @@ -858,22 +876,33 @@ static void msg_response_callback(void *token, pjsip_event *e) rdata = e->body.tsx_state.src.rdata; status_code = e->body.tsx_state.tsx->status_code; - ast_debug(3, "Received MESSAGE response %d on endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint)); - /* Handle 3xx redirects */ if (PJSIP_IS_STATUS_IN_CLASS(status_code, 300)) { - handle_message_redirect(resp_data, rdata); + if (handle_message_redirect(resp_data, rdata) == -1) { + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 0, status_code, resp_data->delivery_id); + } } /* If non-2xx response and we have pending contacts, try the next one */ else if (status_code >= 400 && state && !ast_sip_redirect_get_next_uri(state, &next_uri)) { ast_log(LOG_NOTICE, "MESSAGE to redirect target failed (%d) on endpoint '%s', trying next Contact: '%s'\n", status_code, ast_sorcery_object_get_id(endpoint), next_uri); - send_message_to_uri(resp_data, next_uri); + if (send_message_to_uri(resp_data, next_uri) == -1) { + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 0, status_code, resp_data->delivery_id); + } ast_free(next_uri); } /* Success (2xx) - don't try other contacts */ else if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) { ast_debug(1, "MESSAGE successfully delivered (%d) for endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint)); + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 1, status_code, resp_data->delivery_id); + /* Failed and there are no further contacts. */ + } else if (status_code >= 400) { + ast_debug(1, "MESSAGE failed (%d) for endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint)); + ast_msg_event(resp_data->to, resp_data->from, ast_sorcery_object_get_id(endpoint), + 0, status_code, resp_data->delivery_id); } ao2_ref(resp_data, -1); @@ -905,6 +934,7 @@ static int msg_send(void *data) struct message_response_data *resp_data; const char *from_uri; const char *to_uri; + const char *delivery_id; struct ast_sip_body body = { .type = "text", @@ -1053,15 +1083,18 @@ static int msg_send(void *data) if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) { to_uri = ast_msg_get_to(mdata->msg); } else { - to_uri = ""; + to_uri = uri; } /* Build content type string */ ast_str_set(&content_type_buf, 0, "%s/%s", body.type, body.subtype); + /* Retrieve DeliveryID, if there was one */ + delivery_id = ast_msg_get_var(mdata->msg, "DeliveryID"); + /* Create callback data */ resp_data = message_response_data_create(endpoint, from_uri, to_uri, - body.body_text, ast_str_buffer(content_type_buf), uri); + body.body_text, ast_str_buffer(content_type_buf), uri, delivery_id); if (!resp_data) { pjsip_tx_data_dec_ref(tdata); @@ -1077,7 +1110,8 @@ static int msg_send(void *data) return -1; } - return 0; + /* Return 1 to indicate we will handle ast_msg_event in the callback. */ + return 1; } static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from)