00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211539 $")
00031
00032 #include "asterisk/file.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/app.h"
00036 #include "asterisk/module.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 static char *app_readfile = "ReadFile";
00069
00070 static int readfile_exec(struct ast_channel *chan, const char *data)
00071 {
00072 int res=0;
00073 char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00074 int len=0;
00075 static int deprecation_warning = 0;
00076
00077 if (ast_strlen_zero(data)) {
00078 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00079 return -1;
00080 }
00081
00082 s = ast_strdupa(data);
00083
00084 varname = strsep(&s, "=");
00085 file = strsep(&s, ",");
00086 length = s;
00087
00088 if (deprecation_warning++ % 10 == 0)
00089 ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
00090
00091 if (!varname || !file) {
00092 ast_log(LOG_ERROR, "No file or variable specified!\n");
00093 return -1;
00094 }
00095
00096 if (length) {
00097 if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
00098 ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00099 len = 0;
00100 }
00101 }
00102
00103 if ((returnvar = ast_read_textfile(file))) {
00104 if (len > 0) {
00105 if (len < strlen(returnvar))
00106 returnvar[len]='\0';
00107 else
00108 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00109 }
00110 pbx_builtin_setvar_helper(chan, varname, returnvar);
00111 ast_free(returnvar);
00112 }
00113
00114 return res;
00115 }
00116
00117
00118 static int unload_module(void)
00119 {
00120 return ast_unregister_application(app_readfile);
00121 }
00122
00123 static int load_module(void)
00124 {
00125 return ast_register_application_xml(app_readfile, readfile_exec);
00126 }
00127
00128 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");