app_readexten.c 8.1 KB

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