app_sendtext.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief App to transmit a text message
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \note Requires support of sending text messages from channel driver
  25. *
  26. * \ingroup applications
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/file.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/message.h"
  39. /*** DOCUMENTATION
  40. <application name="SendText" language="en_US">
  41. <synopsis>
  42. Send a Text Message on a channel.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="text" required="false" />
  46. </syntax>
  47. <description>
  48. <para>Sends <replaceable>text</replaceable> to the current channel.</para>
  49. <note><para><literal>current channel</literal> could be the caller or callee depending
  50. on the context in which this application is called.</para></note>
  51. <para>
  52. </para>
  53. <para>The following variables can be set:</para>
  54. <variablelist>
  55. <variable name="SENDTEXT_FROM_DISPLAYNAME">
  56. <para>If set and this channel supports enhanced messaging, this value will be
  57. used as the <literal>From</literal> display name.</para>
  58. </variable>
  59. <variable name="SENDTEXT_TO_DISPLAYNAME">
  60. <para>If set and this channel supports enhanced messaging, this value will be
  61. used as the <literal>To</literal> display name.</para>
  62. </variable>
  63. <variable name="SENDTEXT_CONTENT_TYPE">
  64. <para>If set and this channel supports enhanced messaging, this value will be
  65. used as the message <literal>Content-Type</literal>. If not specified, the
  66. default of <literal>text/plain</literal> will be used.</para>
  67. <para><emphasis>Warning:</emphasis> Messages of types other than
  68. <literal>text/&#42;</literal> cannot be sent via channel drivers that do not
  69. support Enhanced Messaging. An attempt to do so will be ignored and will result
  70. in the <literal>SENDTEXTSTATUS</literal> variable being set to
  71. <literal>UNSUPPORTED</literal>.</para>
  72. </variable>
  73. <variable name="SENDTEXT_BODY">
  74. <para>If set this value will be used as the message body and any text supplied
  75. as a function parameter will be ignored.
  76. </para>
  77. </variable>
  78. </variablelist>
  79. <para>
  80. </para>
  81. <para>Result of transmission will be stored in the following variables:</para>
  82. <variablelist>
  83. <variable name="SENDTEXTTYPE">
  84. <value name="NONE">
  85. No message sent.
  86. </value>
  87. <value name="BASIC">
  88. Message body sent without attributes because the channel driver
  89. doesn't support enhanced messaging.
  90. </value>
  91. <value name="ENHANCED">
  92. The message was sent using enhanced messaging.
  93. </value>
  94. </variable>
  95. <variable name="SENDTEXTSTATUS">
  96. <value name="SUCCESS">
  97. Transmission succeeded.
  98. </value>
  99. <value name="FAILURE">
  100. Transmission failed.
  101. </value>
  102. <value name="UNSUPPORTED">
  103. Text transmission not supported by channel.
  104. </value>
  105. </variable>
  106. </variablelist>
  107. <para>
  108. </para>
  109. <note><para>The text encoding and transmission method is completely at the
  110. discretion of the channel driver. chan_pjsip will use in-dialog SIP MESSAGE
  111. messages always. chan_sip will use T.140 via RTP if a text media type was
  112. negotiated and in-dialog SIP MESSAGE messages otherwise.</para></note>
  113. <para>
  114. </para>
  115. <para>Examples:
  116. </para>
  117. <example title="Send a simple message">
  118. same => n,SendText(Your Text Here)
  119. </example>
  120. <para>If the channel driver supports enhanced messaging (currently only chan_pjsip),
  121. you can set additional variables:</para>
  122. <example title="Alter the From display name">
  123. same => n,Set(SENDTEXT_FROM_DISPLAYNAME=Really From Bob)
  124. same => n,SendText(Your Text Here)
  125. </example>
  126. <example title="Send a JSON String">
  127. same => n,Set(SENDTEXT_CONTENT_TYPE=text/json)
  128. same => n,SendText({"foo":a, "bar":23})
  129. </example>
  130. <example title="Send a JSON String (alternate)">
  131. same => n,Set(SENDTEXT_CONTENT_TYPE=text/json)
  132. same => n,Set(SENDTEXT_BODY={"foo":a, "bar":23})
  133. same => n,SendText()
  134. </example>
  135. </description>
  136. <see-also>
  137. <ref type="application">SendImage</ref>
  138. <ref type="application">SendURL</ref>
  139. </see-also>
  140. </application>
  141. ***/
  142. static const char * const app = "SendText";
  143. static int sendtext_exec(struct ast_channel *chan, const char *data)
  144. {
  145. char *status;
  146. char *msg_type;
  147. struct ast_str *str;
  148. const char *from;
  149. const char *to;
  150. const char *content_type;
  151. const char *body;
  152. int rc = 0;
  153. ast_channel_lock(chan);
  154. from = pbx_builtin_getvar_helper(chan, "SENDTEXT_FROM_DISPLAYNAME");
  155. to = pbx_builtin_getvar_helper(chan, "SENDTEXT_TO_DISPLAYNAME");
  156. content_type = pbx_builtin_getvar_helper(chan, "SENDTEXT_CONTENT_TYPE");
  157. body = S_OR(pbx_builtin_getvar_helper(chan, "SENDTEXT_BODY"), data);
  158. body = S_OR(body, "");
  159. if (!(str = ast_str_alloca(strlen(body) + 1))) {
  160. rc = -1;
  161. goto cleanup;
  162. }
  163. ast_str_get_encoded_str(&str, -1, body);
  164. body = ast_str_buffer(str);
  165. msg_type = "NONE";
  166. status = "UNSUPPORTED";
  167. if (ast_channel_tech(chan)->send_text_data) {
  168. struct ast_msg_data *msg;
  169. struct ast_msg_data_attribute attrs[] =
  170. {
  171. {
  172. .type = AST_MSG_DATA_ATTR_FROM,
  173. .value = (char *)S_OR(from, ""),
  174. },
  175. {
  176. .type = AST_MSG_DATA_ATTR_TO,
  177. .value = (char *)S_OR(to, ""),
  178. },
  179. {
  180. .type = AST_MSG_DATA_ATTR_CONTENT_TYPE,
  181. .value = (char *)S_OR(content_type, ""),
  182. },
  183. {
  184. .type = AST_MSG_DATA_ATTR_BODY,
  185. .value = (char *)S_OR(body, ""),
  186. },
  187. };
  188. msg_type = "ENHANCED";
  189. msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, ARRAY_LEN(attrs));
  190. if (msg) {
  191. if (ast_sendtext_data(chan, msg) == 0) {
  192. status = "SUCCESS";
  193. } else {
  194. status = "FAILURE";
  195. }
  196. ast_free(msg);
  197. } else {
  198. rc = -1;
  199. goto cleanup;
  200. }
  201. } else if (ast_channel_tech(chan)->send_text) {
  202. if (!ast_strlen_zero(content_type) && !ast_begins_with(content_type, "text/")) {
  203. rc = -1;
  204. goto cleanup;
  205. }
  206. msg_type = "BASIC";
  207. if (ast_sendtext(chan, body) == 0) {
  208. status = "SUCCESS";
  209. } else {
  210. status = "FAILURE";
  211. }
  212. }
  213. pbx_builtin_setvar_helper(chan, "SENDTEXTTYPE", msg_type);
  214. pbx_builtin_setvar_helper(chan, "SENDTEXTSTATUS", status);
  215. cleanup:
  216. pbx_builtin_setvar_helper(chan, "SENDTEXT_FROM_DISPLAYNAME", NULL);
  217. pbx_builtin_setvar_helper(chan, "SENDTEXT_TO_DISPLAYNAME", NULL);
  218. pbx_builtin_setvar_helper(chan, "SENDTEXT_CONTENT_TYPE", NULL);
  219. pbx_builtin_setvar_helper(chan, "SENDTEXT_BODY", NULL);
  220. ast_channel_unlock(chan);
  221. return rc;
  222. }
  223. static int unload_module(void)
  224. {
  225. return ast_unregister_application(app);
  226. }
  227. static int load_module(void)
  228. {
  229. return ast_register_application_xml(app, sendtext_exec);
  230. }
  231. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send Text Applications");