app_readfile.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * \ingroup applications
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/file.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/options.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/app.h"
  36. #include "asterisk/module.h"
  37. static char *tdesc = "Stores output of file into a variable";
  38. static char *app_readfile = "ReadFile";
  39. static char *readfile_synopsis = "ReadFile(varname=file,length)";
  40. static char *readfile_descrip =
  41. "ReadFile(varname=file,length)\n"
  42. " Varname - Result stored here.\n"
  43. " File - The name of the file to read.\n"
  44. " Length - Maximum number of characters to capture.\n";
  45. STANDARD_LOCAL_USER;
  46. LOCAL_USER_DECL;
  47. static int readfile_exec(struct ast_channel *chan, void *data)
  48. {
  49. int res=0;
  50. struct localuser *u;
  51. char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
  52. int len=0;
  53. if (ast_strlen_zero(data)) {
  54. ast_log(LOG_WARNING, "ReadFile require an argument!\n");
  55. return -1;
  56. }
  57. LOCAL_USER_ADD(u);
  58. s = ast_strdupa(data);
  59. if (!s) {
  60. ast_log(LOG_ERROR, "Out of memory\n");
  61. LOCAL_USER_REMOVE(u);
  62. return -1;
  63. }
  64. varname = strsep(&s, "=");
  65. file = strsep(&s, "|");
  66. length = s;
  67. if (!varname || !file) {
  68. ast_log(LOG_ERROR, "No file or variable specified!\n");
  69. LOCAL_USER_REMOVE(u);
  70. return -1;
  71. }
  72. if (length) {
  73. if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
  74. ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
  75. len = 0;
  76. }
  77. }
  78. returnvar = ast_read_textfile(file);
  79. if(len > 0){
  80. if(len < strlen(returnvar))
  81. returnvar[len]='\0';
  82. else
  83. ast_log(LOG_WARNING,"%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
  84. }
  85. pbx_builtin_setvar_helper(chan, varname, returnvar);
  86. free(returnvar);
  87. LOCAL_USER_REMOVE(u);
  88. return res;
  89. }
  90. int unload_module(void)
  91. {
  92. int res;
  93. res = ast_unregister_application(app_readfile);
  94. STANDARD_HANGUP_LOCALUSERS;
  95. return res;
  96. }
  97. int load_module(void)
  98. {
  99. return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
  100. }
  101. char *description(void)
  102. {
  103. return tdesc;
  104. }
  105. int usecount(void)
  106. {
  107. int res;
  108. STANDARD_USECOUNT(res);
  109. return res;
  110. }
  111. char *key()
  112. {
  113. return ASTERISK_GPL_KEY;
  114. }