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 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030
00031 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 14462 $")
00034
00035 #include "asterisk/file.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/options.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/module.h"
00042
00043 static char *tdesc = "Stores output of file into a variable";
00044
00045 static char *app_readfile = "ReadFile";
00046
00047 static char *readfile_synopsis = "ReadFile(varname=file,length)";
00048
00049 static char *readfile_descrip =
00050 "ReadFile(varname=file,length)\n"
00051 " Varname - Result stored here.\n"
00052 " File - The name of the file to read.\n"
00053 " Length - Maximum number of characters to capture.\n";
00054
00055 STANDARD_LOCAL_USER;
00056
00057 LOCAL_USER_DECL;
00058
00059
00060 static int readfile_exec(struct ast_channel *chan, void *data)
00061 {
00062 int res=0;
00063 struct localuser *u;
00064 char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00065 int len=0;
00066
00067 if (ast_strlen_zero(data)) {
00068 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00069 return -1;
00070 }
00071
00072 LOCAL_USER_ADD(u);
00073
00074 s = ast_strdupa(data);
00075 if (!s) {
00076 ast_log(LOG_ERROR, "Out of memory\n");
00077 LOCAL_USER_REMOVE(u);
00078 return -1;
00079 }
00080
00081 varname = strsep(&s, "=");
00082 file = strsep(&s, "|");
00083 length = s;
00084
00085 if (!varname || !file) {
00086 ast_log(LOG_ERROR, "No file or variable specified!\n");
00087 LOCAL_USER_REMOVE(u);
00088 return -1;
00089 }
00090
00091 if (length) {
00092 if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
00093 ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00094 len = 0;
00095 }
00096 }
00097
00098 if ((returnvar = ast_read_textfile(file))) {
00099 if (len > 0) {
00100 if (len < strlen(returnvar))
00101 returnvar[len]='\0';
00102 else
00103 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00104 }
00105 pbx_builtin_setvar_helper(chan, varname, returnvar);
00106 free(returnvar);
00107 }
00108 LOCAL_USER_REMOVE(u);
00109 return res;
00110 }
00111
00112
00113 int unload_module(void)
00114 {
00115 int res;
00116
00117 res = ast_unregister_application(app_readfile);
00118
00119 STANDARD_HANGUP_LOCALUSERS;
00120
00121 return res;
00122 }
00123
00124 int load_module(void)
00125 {
00126 return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
00127 }
00128
00129 char *description(void)
00130 {
00131 return tdesc;
00132 }
00133
00134 int usecount(void)
00135 {
00136 int res;
00137 STANDARD_USECOUNT(res);
00138 return res;
00139 }
00140
00141 char *key()
00142 {
00143 return ASTERISK_GPL_KEY;
00144 }