[Asterisk-code-review] manager - Add Content-Type parameter to the SendText action (asterisk[13])

Kevin Harwell asteriskteam at digium.com
Wed Jun 10 17:18:04 CDT 2020


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14517 )


Change subject: manager - Add Content-Type parameter to the SendText action
......................................................................

manager - Add Content-Type parameter to the SendText action

This patch allows a user of AMI to now specify the type of message
content contained within by setting the 'Content-Type' parameter.

Note, the AMI version has been bumped for this change.

ASTERISK-28945 #close

Change-Id: Ibb5315702532c6b954e1498beddc8855fabdf4bb
---
A doc/CHANGES-staging/ami_sendtext_content_type.txt
M include/asterisk/manager.h
M include/asterisk/message.h
M main/channel.c
M main/manager.c
M main/message.c
6 files changed, 69 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/17/14517/1

diff --git a/doc/CHANGES-staging/ami_sendtext_content_type.txt b/doc/CHANGES-staging/ami_sendtext_content_type.txt
new file mode 100644
index 0000000..45037ff
--- /dev/null
+++ b/doc/CHANGES-staging/ami_sendtext_content_type.txt
@@ -0,0 +1,4 @@
+Subject: AMI
+
+You can now specify an optional 'Content-Type' as an argument for the Asterisk
+SendText manager action.
diff --git a/include/asterisk/manager.h b/include/asterisk/manager.h
index 9968ec8..85c26c1 100644
--- a/include/asterisk/manager.h
+++ b/include/asterisk/manager.h
@@ -54,7 +54,7 @@
 - \ref manager.c Main manager code file
  */
 
-#define AMI_VERSION                     "2.10.5"
+#define AMI_VERSION                     "2.10.6"
 #define DEFAULT_MANAGER_PORT 5038	/* Default port for Asterisk management via TCP */
 #define DEFAULT_MANAGER_TLS_PORT 5039	/* Default port for Asterisk management via TCP */
 
diff --git a/include/asterisk/message.h b/include/asterisk/message.h
index f5b7a75..94b5e91 100644
--- a/include/asterisk/message.h
+++ b/include/asterisk/message.h
@@ -480,6 +480,24 @@
 	struct ast_msg_data_attribute attributes[], size_t count);
 
 /*!
+ * \brief Allocates an ast_msg_data structure.
+ * \since 13.35.0
+ * \since 16.12.0
+ * \since 17.6.0
+ *
+ * \param source The source type of the message
+ * \param to Where the message is sent to
+ * \param from Where the message is sent from
+ * \param content_type Content type of the body
+ * \param body The message body
+ *
+ * \return Pointer to msg structure or NULL on allocation failure.
+ *         Caller must call ast_free when done.
+ */
+struct ast_msg_data *ast_msg_data_alloc2(enum ast_msg_data_source_type source_type,
+	const char *to, const char *from, const char *content_type, const char *body);
+
+/*!
  * \brief Clone an ast_msg_data structure
  * \since 13.22.0
  * \since 15.5.0
diff --git a/main/channel.c b/main/channel.c
index 9f68a9a..688e370 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -4115,7 +4115,7 @@
 					break;
 				case AST_FRAME_READ_ACTION_SEND_TEXT:
 					ast_channel_unlock(chan);
-					ast_sendtext(chan, (const char *) read_action_payload->payload);
+					ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
 					ast_channel_lock(chan);
 					break;
 				}
diff --git a/main/manager.c b/main/manager.c
index c79d4f1..72527c8 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -102,6 +102,7 @@
 #include "asterisk/format_cache.h"
 #include "asterisk/translate.h"
 #include "asterisk/taskprocessor.h"
+#include "asterisk/message.h"
 
 /*** DOCUMENTATION
 	<manager name="Ping" language="en_US">
@@ -877,6 +878,9 @@
 			<parameter name="Message" required="true">
 				<para>Message to send.</para>
 			</parameter>
+			<parameter name="Content-Type" required="false" default="text">
+				<para>The type of content in the message</para>
+			</parameter>
 		</syntax>
 		<description>
 			<para>Sends A Text Message to a channel while in a call.</para>
@@ -4814,7 +4818,9 @@
 	struct ast_channel *c;
 	const char *name = astman_get_header(m, "Channel");
 	const char *textmsg = astman_get_header(m, "Message");
+	const char *content_type = astman_get_header(m, "Content-Type");
 	struct ast_control_read_action_payload *frame_payload;
+	struct ast_msg_data *msg;
 	int payload_size;
 	int frame_size;
 	int res;
@@ -4835,22 +4841,32 @@
 		return 0;
 	}
 
-	payload_size = strlen(textmsg) + 1;
+	msg = ast_msg_data_alloc2(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN,
+		NULL, NULL, content_type, textmsg);
+	if (!msg) {
+		ast_channel_unref(c);
+		astman_send_error(s, m, "Unable to allocate message");
+		return 0;
+	}
+
+	payload_size = ast_msg_data_get_length(msg);
 	frame_size = payload_size + sizeof(*frame_payload);
 
 	frame_payload = ast_malloc(frame_size);
 	if (!frame_payload) {
+		ast_free(msg);
 		ast_channel_unref(c);
-		astman_send_error(s, m, "Failure");
+		astman_send_error(s, m, "Unable to allocate message's frame payload");
 		return 0;
 	}
 
 	frame_payload->action = AST_FRAME_READ_ACTION_SEND_TEXT;
 	frame_payload->payload_size = payload_size;
-	memcpy(frame_payload->payload, textmsg, payload_size);
+	memcpy(frame_payload->payload, msg, payload_size);
 	res = ast_queue_control_data(c, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
 
 	ast_free(frame_payload);
+	ast_free(msg);
 	ast_channel_unref(c);
 
 	if (res >= 0) {
diff --git a/main/message.c b/main/message.c
index b3d739e..1eea006 100644
--- a/main/message.c
+++ b/main/message.c
@@ -1427,6 +1427,32 @@
 	return msg;
 }
 
+struct ast_msg_data *ast_msg_data_alloc2(enum ast_msg_data_source_type source_type,
+	const char *to, const char *from, const char *content_type, const char *body)
+{
+	struct ast_msg_data_attribute attrs[] =
+	{
+		{
+			.type = AST_MSG_DATA_ATTR_TO,
+			.value = (char *)S_OR(to, ""),
+		},
+		{
+			.type = AST_MSG_DATA_ATTR_FROM,
+			.value = (char *)S_OR(from, ""),
+		},
+		{
+			.type = AST_MSG_DATA_ATTR_CONTENT_TYPE,
+			.value = (char *)S_OR(content_type, ""),
+		},
+		{
+			.type = AST_MSG_DATA_ATTR_BODY,
+			.value = (char *)S_OR(body, ""),
+		},
+	};
+
+	return ast_msg_data_alloc(source_type, attrs, ARRAY_LEN(attrs));
+}
+
 struct ast_msg_data *ast_msg_data_dup(struct ast_msg_data *msg)
 {
 	struct ast_msg_data *dest;

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14517
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: Ibb5315702532c6b954e1498beddc8855fabdf4bb
Gerrit-Change-Number: 14517
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200610/4305bbe8/attachment-0001.html>


More information about the asterisk-code-review mailing list