app_readfile.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Matt O'Gorman <mogorman@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 ReadFile application -- Reads in a File for you.
  21. *
  22. * \author Matt O'Gorman <mogorman@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <defaultenabled>no</defaultenabled>
  28. <support_level>deprecated</support_level>
  29. <replacement>func_env (FILE())</replacement>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/file.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/module.h"
  38. /*** DOCUMENTATION
  39. <application name="ReadFile" language="en_US">
  40. <synopsis>
  41. Read the contents of a text file into a channel variable.
  42. </synopsis>
  43. <syntax argsep="=">
  44. <parameter name="varname" required="true">
  45. <para>Result stored here.</para>
  46. </parameter>
  47. <parameter name="fileparams" required="true">
  48. <argument name="file" required="true">
  49. <para>The name of the file to read.</para>
  50. </argument>
  51. <argument name="length" required="false">
  52. <para>Maximum number of characters to capture.</para>
  53. <para>If not specified defaults to max.</para>
  54. </argument>
  55. </parameter>
  56. </syntax>
  57. <description>
  58. <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
  59. <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
  60. </description>
  61. <see-also>
  62. <ref type="application">System</ref>
  63. <ref type="application">Read</ref>
  64. </see-also>
  65. </application>
  66. ***/
  67. static char *app_readfile = "ReadFile";
  68. static int readfile_exec(struct ast_channel *chan, const char *data)
  69. {
  70. int res=0;
  71. char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
  72. int len=0;
  73. static int deprecation_warning = 0;
  74. if (ast_strlen_zero(data)) {
  75. ast_log(LOG_WARNING, "ReadFile require an argument!\n");
  76. return -1;
  77. }
  78. s = ast_strdupa(data);
  79. varname = strsep(&s, "=");
  80. file = strsep(&s, ",");
  81. length = s;
  82. if (deprecation_warning++ % 10 == 0)
  83. ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
  84. if (!varname || !file) {
  85. ast_log(LOG_ERROR, "No file or variable specified!\n");
  86. return -1;
  87. }
  88. if (length) {
  89. if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
  90. ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
  91. len = 0;
  92. }
  93. }
  94. if ((returnvar = ast_read_textfile(file))) {
  95. if (len > 0) {
  96. if (len < strlen(returnvar))
  97. returnvar[len]='\0';
  98. else
  99. ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
  100. }
  101. pbx_builtin_setvar_helper(chan, varname, returnvar);
  102. ast_free(returnvar);
  103. }
  104. return res;
  105. }
  106. static int unload_module(void)
  107. {
  108. return ast_unregister_application(app_readfile);
  109. }
  110. static int load_module(void)
  111. {
  112. return ast_register_application_xml(app_readfile, readfile_exec);
  113. }
  114. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");