app_readexten.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007-2008, Trinity College Computing Center
  5. * Written by David Chappell <David.Chappell@trincoll.edu>
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief Trivial application to read an extension into a variable
  20. *
  21. * \author David Chappell <David.Chappell@trincoll.edu>
  22. *
  23. * \ingroup applications
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #include "asterisk/file.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/app.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/indications.h"
  34. #include "asterisk/channel.h"
  35. /*** DOCUMENTATION
  36. <application name="ReadExten" language="en_US">
  37. <synopsis>
  38. Read an extension into a variable.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="variable" required="true" />
  42. <parameter name="filename">
  43. <para>File to play before reading digits or tone with option <literal>i</literal></para>
  44. </parameter>
  45. <parameter name="context">
  46. <para>Context in which to match extensions.</para>
  47. </parameter>
  48. <parameter name="option">
  49. <optionlist>
  50. <option name="s">
  51. <para>Return immediately if the channel is not answered.</para>
  52. </option>
  53. <option name="i">
  54. <para>Play <replaceable>filename</replaceable> as an indication tone from your
  55. <filename>indications.conf</filename> or a directly specified list of
  56. frequencies and durations.</para>
  57. </option>
  58. <option name="n">
  59. <para>Read digits even if the channel is not answered.</para>
  60. </option>
  61. <option name="p">
  62. <para>The extension entered will be considered complete when a <literal>#</literal>
  63. is entered.</para>
  64. </option>
  65. </optionlist>
  66. </parameter>
  67. <parameter name="timeout">
  68. <para>An integer number of seconds to wait for a digit response. If
  69. greater than <literal>0</literal>, that value will override the default timeout.</para>
  70. </parameter>
  71. </syntax>
  72. <description>
  73. <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
  74. <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
  75. <variablelist>
  76. <variable name="READEXTENSTATUS">
  77. <value name="OK">
  78. A valid extension exists in ${variable}.
  79. </value>
  80. <value name="TIMEOUT">
  81. No extension was entered in the specified time. Also sets ${variable} to "t".
  82. </value>
  83. <value name="INVALID">
  84. An invalid extension, ${INVALID_EXTEN}, was entered. Also sets ${variable} to "i".
  85. </value>
  86. <value name="SKIP">
  87. Line was not up and the option 's' was specified.
  88. </value>
  89. <value name="ERROR">
  90. Invalid arguments were passed.
  91. </value>
  92. </variable>
  93. </variablelist>
  94. </description>
  95. </application>
  96. ***/
  97. enum readexten_option_flags {
  98. OPT_SKIP = (1 << 0),
  99. OPT_INDICATION = (1 << 1),
  100. OPT_NOANSWER = (1 << 2),
  101. OPT_POUND_TO_END = (1 << 3),
  102. };
  103. AST_APP_OPTIONS(readexten_app_options, {
  104. AST_APP_OPTION('s', OPT_SKIP),
  105. AST_APP_OPTION('i', OPT_INDICATION),
  106. AST_APP_OPTION('n', OPT_NOANSWER),
  107. AST_APP_OPTION('p', OPT_POUND_TO_END),
  108. });
  109. static char *app = "ReadExten";
  110. static int readexten_exec(struct ast_channel *chan, const char *data)
  111. {
  112. int res = 0;
  113. char exten[256] = "";
  114. int maxdigits = sizeof(exten) - 1;
  115. int timeout = 0, digit_timeout = 0, x = 0;
  116. char *argcopy = NULL, *status = "";
  117. struct ast_tone_zone_sound *ts = NULL;
  118. struct ast_flags flags = {0};
  119. AST_DECLARE_APP_ARGS(arglist,
  120. AST_APP_ARG(variable);
  121. AST_APP_ARG(filename);
  122. AST_APP_ARG(context);
  123. AST_APP_ARG(options);
  124. AST_APP_ARG(timeout);
  125. );
  126. if (ast_strlen_zero(data)) {
  127. ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
  128. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
  129. return 0;
  130. }
  131. argcopy = ast_strdupa(data);
  132. AST_STANDARD_APP_ARGS(arglist, argcopy);
  133. if (ast_strlen_zero(arglist.variable)) {
  134. ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
  135. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
  136. return 0;
  137. }
  138. if (ast_strlen_zero(arglist.filename)) {
  139. arglist.filename = NULL;
  140. }
  141. if (ast_strlen_zero(arglist.context)) {
  142. arglist.context = ast_strdupa(ast_channel_context(chan));
  143. }
  144. if (!ast_strlen_zero(arglist.options)) {
  145. ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
  146. }
  147. if (!ast_strlen_zero(arglist.timeout)) {
  148. timeout = atoi(arglist.timeout);
  149. if (timeout > 0)
  150. timeout *= 1000;
  151. }
  152. if (timeout <= 0)
  153. timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
  154. digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
  155. if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
  156. ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
  157. }
  158. do {
  159. if (ast_channel_state(chan) != AST_STATE_UP) {
  160. if (ast_test_flag(&flags, OPT_SKIP)) {
  161. /* At the user's option, skip if the line is not up */
  162. pbx_builtin_setvar_helper(chan, arglist.variable, "");
  163. status = "SKIP";
  164. break;
  165. } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
  166. /* Otherwise answer unless we're supposed to read while on-hook */
  167. res = ast_answer(chan);
  168. }
  169. }
  170. if (res < 0) {
  171. status = "HANGUP";
  172. break;
  173. }
  174. ast_playtones_stop(chan);
  175. ast_stopstream(chan);
  176. if (ts && ts->data[0]) {
  177. res = ast_playtones_start(chan, 0, ts->data, 0);
  178. } else if (arglist.filename) {
  179. if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
  180. /*
  181. * We were asked to play an indication that did not exist in the config.
  182. * If no such file exists, play it as a tonelist. With any luck they won't
  183. * have a file named "350+440.ulaw"
  184. * (but honestly, who would do something so silly?)
  185. */
  186. res = ast_playtones_start(chan, 0, arglist.filename, 0);
  187. } else {
  188. res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
  189. }
  190. }
  191. for (x = 0; x < maxdigits; x++) {
  192. ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
  193. res = ast_waitfordigit(chan, timeout);
  194. ast_playtones_stop(chan);
  195. ast_stopstream(chan);
  196. timeout = digit_timeout;
  197. if (res < 1) { /* timeout expired or hangup */
  198. if (ast_check_hangup(chan)) {
  199. status = "HANGUP";
  200. } else if (x == 0) {
  201. pbx_builtin_setvar_helper(chan, arglist.variable, "t");
  202. status = "TIMEOUT";
  203. }
  204. break;
  205. }
  206. if (ast_test_flag(&flags, OPT_POUND_TO_END) && res == '#') {
  207. exten[x] = 0;
  208. break;
  209. }
  210. exten[x] = res;
  211. if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
  212. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  213. if (!ast_exists_extension(chan, arglist.context, exten, 1,
  214. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
  215. && res == '#') {
  216. exten[x] = '\0';
  217. }
  218. break;
  219. }
  220. }
  221. if (!ast_strlen_zero(status))
  222. break;
  223. if (ast_exists_extension(chan, arglist.context, exten, 1,
  224. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  225. ast_debug(3, "User entered valid extension '%s'\n", exten);
  226. pbx_builtin_setvar_helper(chan, arglist.variable, exten);
  227. status = "OK";
  228. } else {
  229. ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
  230. pbx_builtin_setvar_helper(chan, arglist.variable, "i");
  231. pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
  232. status = "INVALID";
  233. }
  234. } while (0);
  235. if (ts) {
  236. ts = ast_tone_zone_sound_unref(ts);
  237. }
  238. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
  239. return status[0] == 'H' ? -1 : 0;
  240. }
  241. static int unload_module(void)
  242. {
  243. int res = ast_unregister_application(app);
  244. return res;
  245. }
  246. static int load_module(void)
  247. {
  248. int res = ast_register_application_xml(app, readexten_exec);
  249. return res;
  250. }
  251. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");