app_read.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Trivial application to read a variable
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/app.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/translate.h>
  21. #include <asterisk/options.h>
  22. #include <asterisk/utils.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. static char *tdesc = "Read Variable Application";
  26. static char *app = "Read";
  27. static char *synopsis = "Read a variable";
  28. static char *descrip =
  29. " Read(variable[|filename][|maxdigits][|option])\n\n"
  30. "Reads a #-terminated string of digits from the user in to the given variable,\n"
  31. "optionally playing a given filename first.\n"
  32. " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
  33. " maxdigits have been entered (without requiring the user to\n"
  34. " press the '#' key).\n"
  35. " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
  36. " Any value below 0 means the same. Max accepted value is 255.\n"
  37. " option -- may be 'skip' to return immediately if the line is not up,\n"
  38. " or 'noanswer' to read digits even if the line is not up.\n\n"
  39. "Returns -1 on hangup or error and 0 otherwise.\n";
  40. STANDARD_LOCAL_USER;
  41. LOCAL_USER_DECL;
  42. static int read_exec(struct ast_channel *chan, void *data)
  43. {
  44. int res = 0;
  45. struct localuser *u;
  46. char tmp[256];
  47. char argdata[256] = "";
  48. char *varname;
  49. char *filename;
  50. char *stringp;
  51. char *maxdigitstr;
  52. char *options;
  53. int option_skip = 0;
  54. int option_noanswer = 0;
  55. int maxdigits=255;
  56. if (!data || ast_strlen_zero((char *)data)) {
  57. ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
  58. return -1;
  59. }
  60. strncpy(argdata, (char *)data, sizeof(argdata)-1);
  61. stringp=argdata;
  62. varname = strsep(&stringp, "|");
  63. filename = strsep(&stringp, "|");
  64. maxdigitstr = strsep(&stringp,"|");
  65. options = strsep(&stringp, "|");
  66. if (options && !strcasecmp(options, "skip"))
  67. option_skip = 1;
  68. if (options && !strcasecmp(options, "noanswer"))
  69. option_noanswer = 1;
  70. if (!(filename) || ast_strlen_zero(filename))
  71. filename = NULL;
  72. if (maxdigitstr) {
  73. maxdigits = atoi(maxdigitstr);
  74. if ((maxdigits<1) || (maxdigits>255)) {
  75. maxdigits = 255;
  76. } else
  77. ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
  78. }
  79. if (!(varname) || ast_strlen_zero(varname)) {
  80. ast_log(LOG_WARNING, "Read requires an variable name\n");
  81. return -1;
  82. }
  83. LOCAL_USER_ADD(u);
  84. if (chan->_state != AST_STATE_UP) {
  85. if (option_skip) {
  86. /* At the user's option, skip if the line is not up */
  87. pbx_builtin_setvar_helper(chan, varname, "\0");
  88. LOCAL_USER_REMOVE(u);
  89. return 0;
  90. } else if (!option_noanswer) {
  91. /* Otherwise answer unless we're supposed to read while on-hook */
  92. res = ast_answer(chan);
  93. }
  94. }
  95. if (!res) {
  96. ast_stopstream(chan);
  97. res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
  98. if (res > -1) {
  99. pbx_builtin_setvar_helper(chan, varname, tmp);
  100. ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
  101. res = 0;
  102. } else {
  103. ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
  104. }
  105. }
  106. LOCAL_USER_REMOVE(u);
  107. return res;
  108. }
  109. int unload_module(void)
  110. {
  111. STANDARD_HANGUP_LOCALUSERS;
  112. return ast_unregister_application(app);
  113. }
  114. int load_module(void)
  115. {
  116. return ast_register_application(app, read_exec, synopsis, descrip);
  117. }
  118. char *description(void)
  119. {
  120. return tdesc;
  121. }
  122. int usecount(void)
  123. {
  124. int res;
  125. STANDARD_USECOUNT(res);
  126. return res;
  127. }
  128. char *key()
  129. {
  130. return ASTERISK_GPL_KEY;
  131. }