app_read.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/file.h"
  29. #include "asterisk/pbx.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/app.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/indications.h"
  34. enum {
  35. OPT_SKIP = (1 << 0),
  36. OPT_INDICATION = (1 << 1),
  37. OPT_NOANSWER = (1 << 2),
  38. } read_option_flags;
  39. AST_APP_OPTIONS(read_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 = "Read";
  45. static char *synopsis = "Read a variable";
  46. static char *descrip =
  47. " Read(variable[,filename[&filename2...]][,maxdigits][,option][,attempts][,timeout])\n\n"
  48. "Reads a #-terminated string of digits a certain number of times from the\n"
  49. "user in to the given variable.\n"
  50. " filename -- file(s) to play before reading digits or tone with option i\n"
  51. " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
  52. " maxdigits have been entered (without requiring the user to\n"
  53. " press the '#' key).\n"
  54. " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
  55. " Any value below 0 means the same. Max accepted value is 255.\n"
  56. " option -- options are 's' , 'i', 'n'\n"
  57. " 's' to return immediately if the line is not up,\n"
  58. " 'i' to play filename as an indication tone from your indications.conf\n"
  59. " 'n' to read digits even if the line is not up.\n"
  60. " attempts -- if greater than 1, that many attempts will be made in the \n"
  61. " event no data is entered.\n"
  62. " timeout -- The number of seconds to wait for a digit response. If greater\n"
  63. " than 0, that value will override the default timeout. Can be floating point.\n"
  64. "This application sets the following channel variable upon completion:\n"
  65. " READSTATUS - This is the status of the read operation.\n"
  66. " Possible values are:\n"
  67. " OK | ERROR | HANGUP | INTERRUPTED | SKIPPED | TIMEOUT\n";
  68. #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
  69. static int read_exec(struct ast_channel *chan, void *data)
  70. {
  71. int res = 0;
  72. char tmp[256] = "";
  73. int maxdigits = 255;
  74. int tries = 1, to = 0, x = 0;
  75. double tosec;
  76. char *argcopy = NULL;
  77. struct tone_zone_sound *ts = NULL;
  78. struct ast_flags flags = {0};
  79. const char *status = "ERROR";
  80. AST_DECLARE_APP_ARGS(arglist,
  81. AST_APP_ARG(variable);
  82. AST_APP_ARG(filename);
  83. AST_APP_ARG(maxdigits);
  84. AST_APP_ARG(options);
  85. AST_APP_ARG(attempts);
  86. AST_APP_ARG(timeout);
  87. );
  88. pbx_builtin_setvar_helper(chan, "READSTATUS", status);
  89. if (ast_strlen_zero(data)) {
  90. ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
  91. return 0;
  92. }
  93. argcopy = ast_strdupa(data);
  94. AST_STANDARD_APP_ARGS(arglist, argcopy);
  95. if (!ast_strlen_zero(arglist.options)) {
  96. ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
  97. }
  98. if (!ast_strlen_zero(arglist.attempts)) {
  99. tries = atoi(arglist.attempts);
  100. if (tries <= 0)
  101. tries = 1;
  102. }
  103. if (!ast_strlen_zero(arglist.timeout)) {
  104. tosec = atof(arglist.timeout);
  105. if (tosec <= 0)
  106. to = 0;
  107. else
  108. to = tosec * 1000.0;
  109. }
  110. if (ast_strlen_zero(arglist.filename)) {
  111. arglist.filename = NULL;
  112. }
  113. if (!ast_strlen_zero(arglist.maxdigits)) {
  114. maxdigits = atoi(arglist.maxdigits);
  115. if ((maxdigits < 1) || (maxdigits > 255)) {
  116. maxdigits = 255;
  117. } else
  118. ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
  119. }
  120. if (ast_strlen_zero(arglist.variable)) {
  121. ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
  122. return 0;
  123. }
  124. if (ast_test_flag(&flags, OPT_INDICATION)) {
  125. if (! ast_strlen_zero(arglist.filename)) {
  126. ts = ast_get_indication_tone(chan->zone, arglist.filename);
  127. }
  128. }
  129. if (chan->_state != AST_STATE_UP) {
  130. if (ast_test_flag(&flags, OPT_SKIP)) {
  131. /* At the user's option, skip if the line is not up */
  132. pbx_builtin_setvar_helper(chan, arglist.variable, "");
  133. pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
  134. return 0;
  135. } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
  136. /* Otherwise answer unless we're supposed to read while on-hook */
  137. res = ast_answer(chan);
  138. }
  139. }
  140. if (!res) {
  141. while (tries && !res) {
  142. ast_stopstream(chan);
  143. if (ts && ts->data[0]) {
  144. if (!to)
  145. to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
  146. res = ast_playtones_start(chan, 0, ts->data, 0);
  147. for (x = 0; x < maxdigits; ) {
  148. res = ast_waitfordigit(chan, to);
  149. ast_playtones_stop(chan);
  150. if (res < 1) {
  151. if (res == 0)
  152. status = "TIMEOUT";
  153. tmp[x]='\0';
  154. break;
  155. }
  156. tmp[x++] = res;
  157. if (tmp[x-1] == '#') {
  158. tmp[x-1] = '\0';
  159. status = "OK";
  160. break;
  161. }
  162. if (x >= maxdigits) {
  163. status = "OK";
  164. }
  165. }
  166. } else {
  167. res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
  168. if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED)
  169. status = "OK";
  170. else if (res == AST_GETDATA_TIMEOUT)
  171. status = "TIMEOUT";
  172. else if (res == AST_GETDATA_INTERRUPTED)
  173. status = "INTERRUPTED";
  174. }
  175. if (res > -1) {
  176. pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
  177. if (!ast_strlen_zero(tmp)) {
  178. ast_verb(3, "User entered '%s'\n", tmp);
  179. tries = 0;
  180. } else {
  181. tries--;
  182. if (tries)
  183. ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
  184. else
  185. ast_verb(3, "User entered nothing.\n");
  186. }
  187. res = 0;
  188. } else {
  189. pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
  190. ast_verb(3, "User disconnected\n");
  191. }
  192. }
  193. }
  194. if (ast_check_hangup(chan))
  195. status = "HANGUP";
  196. pbx_builtin_setvar_helper(chan, "READSTATUS", status);
  197. return 0;
  198. }
  199. static int unload_module(void)
  200. {
  201. return ast_unregister_application(app);
  202. }
  203. static int load_module(void)
  204. {
  205. return ast_register_application(app, read_exec, synopsis, descrip);
  206. }
  207. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");