app_read.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 Trivial application to read a variable
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/indications.h"
  37. /*** DOCUMENTATION
  38. <application name="Read" language="en_US">
  39. <synopsis>
  40. Read a variable.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="variable" required="true">
  44. <para>The input digits will be stored in the given <replaceable>variable</replaceable>
  45. name.</para>
  46. </parameter>
  47. <parameter name="filenames" argsep="&amp;">
  48. <argument name="filename" required="true">
  49. <para>file(s) to play before reading digits or tone with option i</para>
  50. </argument>
  51. <argument name="filename2" multiple="true" />
  52. </parameter>
  53. <parameter name="maxdigits">
  54. <para>Maximum acceptable number of digits. Stops reading after
  55. <replaceable>maxdigits</replaceable> have been entered (without
  56. requiring the user to press the <literal>#</literal> key).</para>
  57. <para>Defaults to <literal>0</literal> - no limit - wait for the
  58. user press the <literal>#</literal> key. Any value below
  59. <literal>0</literal> means the same. Max accepted value is
  60. <literal>255</literal>.</para>
  61. </parameter>
  62. <parameter name="options">
  63. <optionlist>
  64. <option name="s">
  65. <para>to return immediately if the line is not up.</para>
  66. </option>
  67. <option name="i">
  68. <para>to play filename as an indication tone from your
  69. <filename>indications.conf</filename>.</para>
  70. </option>
  71. <option name="n">
  72. <para>to read digits even if the line is not up.</para>
  73. </option>
  74. </optionlist>
  75. </parameter>
  76. <parameter name="attempts">
  77. <para>If greater than <literal>1</literal>, that many
  78. <replaceable>attempts</replaceable> will be made in the
  79. event no data is entered.</para>
  80. </parameter>
  81. <parameter name="timeout">
  82. <para>The number of seconds to wait for a digit response. If greater
  83. than <literal>0</literal>, that value will override the default timeout.
  84. Can be floating point.</para>
  85. </parameter>
  86. </syntax>
  87. <description>
  88. <para>Reads a #-terminated string of digits a certain number of times from the
  89. user in to the given <replaceable>variable</replaceable>.</para>
  90. <para>This application sets the following channel variable upon completion:</para>
  91. <variablelist>
  92. <variable name="READSTATUS">
  93. <para>This is the status of the read operation.</para>
  94. <value name="OK" />
  95. <value name="ERROR" />
  96. <value name="HANGUP" />
  97. <value name="INTERRUPTED" />
  98. <value name="SKIPPED" />
  99. <value name="TIMEOUT" />
  100. </variable>
  101. </variablelist>
  102. </description>
  103. <see-also>
  104. <ref type="application">SendDTMF</ref>
  105. </see-also>
  106. </application>
  107. ***/
  108. enum read_option_flags {
  109. OPT_SKIP = (1 << 0),
  110. OPT_INDICATION = (1 << 1),
  111. OPT_NOANSWER = (1 << 2),
  112. };
  113. AST_APP_OPTIONS(read_app_options, {
  114. AST_APP_OPTION('s', OPT_SKIP),
  115. AST_APP_OPTION('i', OPT_INDICATION),
  116. AST_APP_OPTION('n', OPT_NOANSWER),
  117. });
  118. static char *app = "Read";
  119. static int read_exec(struct ast_channel *chan, const char *data)
  120. {
  121. int res = 0;
  122. char tmp[256] = "";
  123. int maxdigits = 255;
  124. int tries = 1, to = 0, x = 0;
  125. double tosec;
  126. char *argcopy = NULL;
  127. struct ast_tone_zone_sound *ts = NULL;
  128. struct ast_flags flags = {0};
  129. const char *status = "ERROR";
  130. AST_DECLARE_APP_ARGS(arglist,
  131. AST_APP_ARG(variable);
  132. AST_APP_ARG(filename);
  133. AST_APP_ARG(maxdigits);
  134. AST_APP_ARG(options);
  135. AST_APP_ARG(attempts);
  136. AST_APP_ARG(timeout);
  137. );
  138. pbx_builtin_setvar_helper(chan, "READSTATUS", status);
  139. if (ast_strlen_zero(data)) {
  140. ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
  141. return 0;
  142. }
  143. argcopy = ast_strdupa(data);
  144. AST_STANDARD_APP_ARGS(arglist, argcopy);
  145. if (!ast_strlen_zero(arglist.options)) {
  146. ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
  147. }
  148. if (!ast_strlen_zero(arglist.attempts)) {
  149. tries = atoi(arglist.attempts);
  150. if (tries <= 0)
  151. tries = 1;
  152. }
  153. if (!ast_strlen_zero(arglist.timeout)) {
  154. tosec = atof(arglist.timeout);
  155. if (tosec <= 0)
  156. to = 0;
  157. else
  158. to = tosec * 1000.0;
  159. }
  160. if (ast_strlen_zero(arglist.filename)) {
  161. arglist.filename = NULL;
  162. }
  163. if (!ast_strlen_zero(arglist.maxdigits)) {
  164. maxdigits = atoi(arglist.maxdigits);
  165. if ((maxdigits < 1) || (maxdigits > 255)) {
  166. maxdigits = 255;
  167. } else
  168. ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
  169. }
  170. if (ast_strlen_zero(arglist.variable)) {
  171. ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
  172. return 0;
  173. }
  174. if (ast_test_flag(&flags, OPT_INDICATION)) {
  175. if (!ast_strlen_zero(arglist.filename)) {
  176. ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
  177. }
  178. }
  179. if (ast_channel_state(chan) != AST_STATE_UP) {
  180. if (ast_test_flag(&flags, OPT_SKIP)) {
  181. /* At the user's option, skip if the line is not up */
  182. pbx_builtin_setvar_helper(chan, arglist.variable, "");
  183. pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
  184. return 0;
  185. } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
  186. /* Otherwise answer unless we're supposed to read while on-hook */
  187. res = ast_answer(chan);
  188. }
  189. }
  190. if (!res) {
  191. while (tries && !res) {
  192. ast_stopstream(chan);
  193. if (ts && ts->data[0]) {
  194. if (!to)
  195. to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000;
  196. res = ast_playtones_start(chan, 0, ts->data, 0);
  197. for (x = 0; x < maxdigits; ) {
  198. res = ast_waitfordigit(chan, to);
  199. ast_playtones_stop(chan);
  200. if (res < 1) {
  201. if (res == 0)
  202. status = "TIMEOUT";
  203. tmp[x]='\0';
  204. break;
  205. }
  206. tmp[x++] = res;
  207. if (tmp[x-1] == '#') {
  208. tmp[x-1] = '\0';
  209. status = "OK";
  210. break;
  211. }
  212. if (x >= maxdigits) {
  213. status = "OK";
  214. }
  215. }
  216. } else {
  217. res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
  218. if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED)
  219. status = "OK";
  220. else if (res == AST_GETDATA_TIMEOUT)
  221. status = "TIMEOUT";
  222. else if (res == AST_GETDATA_INTERRUPTED)
  223. status = "INTERRUPTED";
  224. }
  225. if (res > -1) {
  226. pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
  227. if (!ast_strlen_zero(tmp)) {
  228. ast_verb(3, "User entered '%s'\n", tmp);
  229. tries = 0;
  230. } else {
  231. tries--;
  232. if (tries)
  233. ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
  234. else
  235. ast_verb(3, "User entered nothing.\n");
  236. }
  237. res = 0;
  238. } else {
  239. pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
  240. ast_verb(3, "User disconnected\n");
  241. }
  242. }
  243. }
  244. if (ts) {
  245. ts = ast_tone_zone_sound_unref(ts);
  246. }
  247. if (ast_check_hangup(chan))
  248. status = "HANGUP";
  249. pbx_builtin_setvar_helper(chan, "READSTATUS", status);
  250. return 0;
  251. }
  252. static int unload_module(void)
  253. {
  254. return ast_unregister_application(app);
  255. }
  256. static int load_module(void)
  257. {
  258. return ast_register_application_xml(app, read_exec);
  259. }
  260. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");