summaryrefslogtreecommitdiff
path: root/apps/app_sendtext.c
blob: 1cefb6c6883a7cb3c2098b1726c4098d97645d15 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2005, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief App to transmit a text message
 *
 * \author Mark Spencer <markster@digium.com>
 *
 * \note Requires support of sending text messages from channel driver
 *
 * \ingroup applications
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/message.h"

/*** DOCUMENTATION
	<application name="SendText" language="en_US">
		<synopsis>
			Send a Text Message on a channel.
		</synopsis>
		<syntax>
			<parameter name="text" required="false" />
		</syntax>
		<description>
			<para>Sends <replaceable>text</replaceable> to the current channel.</para>
			<note><para><literal>current channel</literal> could be the caller or callee depending
			on the context in which this application is called.</para></note>
			<para>
			</para>
			<para>The following variables can be set:</para>
			<variablelist>
				<variable name="SENDTEXT_FROM_DISPLAYNAME">
					<para>If set and this channel supports enhanced messaging, this value will be
					used as the <literal>From</literal> display name.</para>
				</variable>
				<variable name="SENDTEXT_TO_DISPLAYNAME">
					<para>If set and this channel supports enhanced messaging, this value will be
					used as the <literal>To</literal> display name.</para>
				</variable>
				<variable name="SENDTEXT_CONTENT_TYPE">
					<para>If set and this channel supports enhanced messaging, this value will be
					used as the message <literal>Content-Type</literal>.  It <emphasis>MUST</emphasis>
					be a <literal>text/&#42;</literal> content type.  If not specified, the
					default of <literal>text/plain</literal> will be used.</para>
				</variable>
				<variable name="SENDTEXT_BODY">
					<para>If set this value will be used as the message body and any text supplied
					as a function parameter will be ignored.
					</para>
				</variable>
			</variablelist>
			<para>
			</para>
			<para>Result of transmission will be stored in the following variables:</para>
			<variablelist>
				<variable name="SENDTEXTTYPE">
					<value name="NONE">
						No message sent.
					</value>
					<value name="BASIC">
						Message body sent without attributes because the channel driver
						doesn't support enhanced messaging.
					</value>
					<value name="ENHANCED">
						The message was sent using enhanced messaging.
					</value>
				</variable>
				<variable name="SENDTEXTSTATUS">
					<value name="SUCCESS">
						Transmission succeeded.
					</value>
					<value name="FAILURE">
						Transmission failed.
					</value>
					<value name="UNSUPPORTED">
						Text transmission not supported by channel.
					</value>
				</variable>
			</variablelist>
			<para>
			</para>
			<note><para>The text encoding and transmission method is completely at the
			discretion of the channel driver.  chan_pjsip will use in-dialog SIP MESSAGE
			messages always.  chan_sip will use T.140 via RTP if a text media type was
			negotiated and in-dialog SIP MESSAGE messages otherwise.</para></note>
			<para>
			</para>
			<para>Examples:
			</para>
			<example title="Send a simple message">
			 same => n,SendText(Your Text Here)
			</example>
			<para>If the channel driver supports enhanced messaging (currently only chan_pjsip),
			you can set additional variables:</para>
			<example title="Alter the From display name">
			 same => n,Set(SENDTEXT_FROM_DISPLAYNAME=Really From Bob)
			 same => n,SendText(Your Text Here)
			</example>
			<example title="Send a JSON String">
			 same => n,Set(SENDTEXT_CONTENT_TYPE=text/json)
			 same => n,SendText({"foo":a, "bar":23})
			</example>
			<example title="Send a JSON String (alternate)">
			 same => n,Set(SENDTEXT_CONTENT_TYPE=text/json)
			 same => n,Set(SENDTEXT_BODY={"foo":a, "bar":23})
			 same => n,SendText()
			</example>
		</description>
		<see-also>
			<ref type="application">SendImage</ref>
			<ref type="application">SendURL</ref>
		</see-also>
	</application>
 ***/

static const char * const app = "SendText";

static int sendtext_exec(struct ast_channel *chan, const char *data)
{
	char *status;
	char *msg_type;
	struct ast_str *str;
	const char *from;
	const char *to;
	const char *content_type;
	const char *body;
	int rc = 0;

	ast_channel_lock(chan);
	from = pbx_builtin_getvar_helper(chan, "SENDTEXT_FROM_DISPLAYNAME");
	to = pbx_builtin_getvar_helper(chan, "SENDTEXT_TO_DISPLAYNAME");
	content_type = pbx_builtin_getvar_helper(chan, "SENDTEXT_CONTENT_TYPE");
	body = S_OR(pbx_builtin_getvar_helper(chan, "SENDTEXT_BODY"), data);
	body = S_OR(body, "");

	if (!(str = ast_str_alloca(strlen(body) + 1))) {
		rc = -1;
		goto cleanup;
	}
	ast_str_get_encoded_str(&str, -1, body);
	body = ast_str_buffer(str);

	msg_type = "NONE";
	status = "UNSUPPORTED";
	if (ast_channel_tech(chan)->send_text_data) {
		struct ast_msg_data *msg;
		struct ast_msg_data_attribute attrs[] =
		{
			{
				.type = AST_MSG_DATA_ATTR_FROM,
				.value = (char *)S_OR(from, ""),
			},
			{
				.type = AST_MSG_DATA_ATTR_TO,
				.value = (char *)S_OR(to, ""),
			},
			{
				.type = AST_MSG_DATA_ATTR_CONTENT_TYPE,
				.value = (char *)S_OR(content_type, ""),
			},
			{
				.type = AST_MSG_DATA_ATTR_BODY,
				.value = (char *)S_OR(body, ""),
			},
		};

		if (!ast_strlen_zero(content_type) && !ast_begins_with(content_type, "text/")) {
			ast_log(LOG_ERROR, "SENDTEXT_CONTENT_TYPE must begin with 'text/'\n");
			rc = -1;
			goto cleanup;
		}
		msg_type = "ENHANCED";
		msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, ARRAY_LEN(attrs));
		if (msg) {
			if (ast_sendtext_data(chan, msg) == 0) {
				status = "SUCCESS";
			} else {
				status = "FAILURE";
			}

			ast_free(msg);
		} else {
			rc = -1;
			goto cleanup;
		}

	} else if (ast_channel_tech(chan)->send_text) {
		msg_type = "BASIC";
		if (ast_sendtext(chan, body) == 0) {
			status = "SUCCESS";
		} else {
			status = "FAILURE";
		}
	}

	pbx_builtin_setvar_helper(chan, "SENDTEXTTYPE", msg_type);
	pbx_builtin_setvar_helper(chan, "SENDTEXTSTATUS", status);

cleanup:
	pbx_builtin_setvar_helper(chan, "SENDTEXT_FROM_DISPLAYNAME", NULL);
	pbx_builtin_setvar_helper(chan, "SENDTEXT_TO_DISPLAYNAME", NULL);
	pbx_builtin_setvar_helper(chan, "SENDTEXT_CONTENT_TYPE", NULL);
	pbx_builtin_setvar_helper(chan, "SENDTEXT_BODY", NULL);
	ast_channel_unlock(chan);

	return rc;
}

static int unload_module(void)
{
	return ast_unregister_application(app);
}

static int load_module(void)
{
	return ast_register_application_xml(app, sendtext_exec);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send Text Applications");