app_readexten.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007 Dave Chappell
  5. *
  6. * David Chappell <David.Chappell@trincoll.edu>
  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 an extension into a variable
  21. *
  22. * \author David Chappell <David.Chappell@trincoll.edu>
  23. *
  24. * \ingroup applications
  25. */
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/file.h"
  29. #include "asterisk/pbx.h"
  30. #include "asterisk/app.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/indications.h"
  33. #include "asterisk/channel.h"
  34. enum {
  35. OPT_SKIP = (1 << 0),
  36. OPT_INDICATION = (1 << 1),
  37. OPT_NOANSWER = (1 << 2),
  38. } readexten_option_flags;
  39. AST_APP_OPTIONS(readexten_app_options, {
  40. AST_APP_OPTION('s', OPT_SKIP),
  41. AST_APP_OPTION('i', OPT_INDICATION),
  42. AST_APP_OPTION('n', OPT_NOANSWER),
  43. });
  44. static char *app = "ReadExten";
  45. static char *synopsis = "Read an extension into a variable";
  46. static char *descrip =
  47. " ReadExten(<variable>[,[<filename>][,[<context>][,[<option>][,<timeout>]]]])\n\n"
  48. "Reads a #-terminated string of digits from the user into the given variable.\n"
  49. " filename file to play before reading digits or tone with option i\n"
  50. " context context in which to match extensions\n"
  51. " option options are:\n"
  52. " s - Return immediately if the channel is not answered,\n"
  53. " i - Play filename as an indication tone from your\n"
  54. " indications.conf\n"
  55. " n - Read digits even if the channel is not answered.\n"
  56. " timeout An integer number of seconds to wait for a digit response. If\n"
  57. " greater than 0, that value will override the default timeout.\n\n"
  58. "ReadExten will set READEXTENSTATUS on exit with one of the following statuses:\n"
  59. " OK A valid extension exists in ${variable}\n"
  60. " TIMEOUT No extension was entered in the specified time\n"
  61. " INVALID An invalid extension, ${INVALID_EXTEN}, was entered\n"
  62. " SKIP Line was not up and the option 's' was specified\n"
  63. " ERROR Invalid arguments were passed\n";
  64. static int readexten_exec(struct ast_channel *chan, void *data)
  65. {
  66. int res = 0;
  67. char exten[256] = "";
  68. int maxdigits = sizeof(exten) - 1;
  69. int timeout = 0, digit_timeout = 0, x = 0;
  70. char *argcopy = NULL, *status = "";
  71. struct tone_zone_sound *ts = NULL;
  72. struct ast_flags flags = {0};
  73. AST_DECLARE_APP_ARGS(arglist,
  74. AST_APP_ARG(variable);
  75. AST_APP_ARG(filename);
  76. AST_APP_ARG(context);
  77. AST_APP_ARG(options);
  78. AST_APP_ARG(timeout);
  79. );
  80. if (ast_strlen_zero(data)) {
  81. ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
  82. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
  83. return 0;
  84. }
  85. argcopy = ast_strdupa(data);
  86. AST_STANDARD_APP_ARGS(arglist, argcopy);
  87. if (ast_strlen_zero(arglist.variable)) {
  88. ast_log(LOG_WARNING, "Invalid! Usage: ReadExten(variable[|filename][|context][|options][|timeout])\n\n");
  89. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
  90. return 0;
  91. }
  92. if (ast_strlen_zero(arglist.filename))
  93. arglist.filename = NULL;
  94. if (ast_strlen_zero(arglist.context))
  95. arglist.context = chan->context;
  96. if (!ast_strlen_zero(arglist.options))
  97. ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
  98. if (!ast_strlen_zero(arglist.timeout)) {
  99. timeout = atoi(arglist.timeout);
  100. if (timeout > 0)
  101. timeout *= 1000;
  102. }
  103. if (timeout <= 0)
  104. timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
  105. if (digit_timeout <= 0)
  106. digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
  107. if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename))
  108. ts = ast_get_indication_tone(chan->zone, arglist.filename);
  109. do {
  110. if (chan->_state != AST_STATE_UP) {
  111. if (ast_test_flag(&flags, OPT_SKIP)) {
  112. /* At the user's option, skip if the line is not up */
  113. pbx_builtin_setvar_helper(chan, arglist.variable, "");
  114. status = "SKIP";
  115. break;
  116. } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
  117. /* Otherwise answer unless we're supposed to read while on-hook */
  118. res = ast_answer(chan);
  119. }
  120. }
  121. if (res < 0) {
  122. status = "HANGUP";
  123. break;
  124. }
  125. ast_playtones_stop(chan);
  126. ast_stopstream(chan);
  127. if (ts && ts->data[0])
  128. res = ast_playtones_start(chan, 0, ts->data, 0);
  129. else if (arglist.filename)
  130. res = ast_streamfile(chan, arglist.filename, chan->language);
  131. for (x = 0; x < maxdigits; x++) {
  132. ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
  133. res = ast_waitfordigit(chan, timeout);
  134. ast_playtones_stop(chan);
  135. ast_stopstream(chan);
  136. timeout = digit_timeout;
  137. if (res < 1) { /* timeout expired or hangup */
  138. if (ast_check_hangup(chan))
  139. status = "HANGUP";
  140. else
  141. status = "TIMEOUT";
  142. break;
  143. }
  144. exten[x] = res;
  145. if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */, chan->cid.cid_num)) {
  146. if (!ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num) && res == '#') {
  147. exten[x] = '\0';
  148. }
  149. break;
  150. }
  151. }
  152. if (!ast_strlen_zero(status))
  153. break;
  154. if (ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num)) {
  155. ast_debug(3, "User entered valid extension '%s'\n", exten);
  156. pbx_builtin_setvar_helper(chan, arglist.variable, exten);
  157. status = "OK";
  158. } else {
  159. ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
  160. pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
  161. status = "INVALID";
  162. }
  163. } while (0);
  164. pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
  165. return status[0] == 'H' ? -1 : 0;
  166. }
  167. static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
  168. {
  169. int priority_int;
  170. AST_DECLARE_APP_ARGS(args,
  171. AST_APP_ARG(context);
  172. AST_APP_ARG(extension);
  173. AST_APP_ARG(priority);
  174. );
  175. AST_STANDARD_APP_ARGS(args, parse);
  176. if (ast_strlen_zero(args.context))
  177. args.context = chan->context;
  178. if (ast_strlen_zero(args.extension)) {
  179. ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
  180. return -1;
  181. }
  182. if (ast_strlen_zero(args.priority))
  183. priority_int = 1;
  184. else
  185. priority_int = atoi(args.priority);
  186. if (ast_exists_extension(chan, args.context, args.extension, priority_int, chan->cid.cid_num))
  187. ast_copy_string(buffer, "1", buflen);
  188. else
  189. ast_copy_string(buffer, "0", buflen);
  190. return 0;
  191. }
  192. static struct ast_custom_function acf_isexten = {
  193. .name = "VALID_EXTEN",
  194. .synopsis = "Determine whether an extension exists or not",
  195. .syntax = "VALID_EXTEN([<context>],<extension>[,<priority>])",
  196. .desc =
  197. "Returns a true value if the indicated context, extension, and priority exist.\n"
  198. "Context defaults to the current context, priority defaults to 1.\n",
  199. .read = acf_isexten_exec,
  200. };
  201. static int unload_module(void)
  202. {
  203. int res = ast_unregister_application(app);
  204. res |= ast_custom_function_unregister(&acf_isexten);
  205. return res;
  206. }
  207. static int load_module(void)
  208. {
  209. int res = ast_register_application(app, readexten_exec, synopsis, descrip);
  210. res |= ast_custom_function_register(&acf_isexten);
  211. return res;
  212. }
  213. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");