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 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00035
00036 #include "asterisk/file.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/cli.h"
00045
00046 #define next_one(var) var = var->next
00047 #define crop_data(str) { *(str) = '\0' ; (str)++; }
00048
00049 static char *tdesc = "Realtime Data Lookup/Rewrite";
00050 static char *app = "RealTime";
00051 static char *uapp = "RealTimeUpdate";
00052 static char *synopsis = "Realtime Data Lookup";
00053 static char *usynopsis = "Realtime Data Rewrite";
00054 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
00055 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
00056 static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
00057 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
00058 "All unique column names will be set as channel variables with optional prefix to the name.\n"
00059 "e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
00060 static char *udesc = "Use the RealTime config handler system to update a value\n"
00061 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
00062 "The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";
00063
00064 STANDARD_LOCAL_USER;
00065 LOCAL_USER_DECL;
00066
00067 static int cli_load_realtime(int fd, int argc, char **argv)
00068 {
00069 char *header_format = "%30s %-30s\n";
00070 struct ast_variable *var=NULL;
00071
00072 if(argc<5) {
00073 ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
00074 return RESULT_FAILURE;
00075 }
00076
00077 var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
00078
00079 if(var) {
00080 ast_cli(fd, header_format, "Column Name", "Column Value");
00081 ast_cli(fd, header_format, "--------------------", "--------------------");
00082 while(var) {
00083 ast_cli(fd, header_format, var->name, var->value);
00084 var = var->next;
00085 }
00086 } else {
00087 ast_cli(fd, "No rows found matching search criteria.\n");
00088 }
00089 return RESULT_SUCCESS;
00090 }
00091
00092 static int cli_update_realtime(int fd, int argc, char **argv) {
00093 int res = 0;
00094
00095 if(argc<7) {
00096 ast_cli(fd, "You must supply a family name, a column to update on, a new value, column to match, and value to to match.\n");
00097 ast_cli(fd, "Ex: realtime update sipfriends name bobsphone port 4343\n will execute SQL as UPDATE sipfriends SET port = 4343 WHERE name = bobsphone\n");
00098 return RESULT_FAILURE;
00099 }
00100
00101 res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
00102
00103 if(res < 0) {
00104 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00105 return RESULT_SUCCESS;
00106 }
00107
00108 ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
00109
00110 return RESULT_SUCCESS;
00111 }
00112
00113 static char cli_load_realtime_usage[] =
00114 "Usage: realtime load <family> <colmatch> <value>\n"
00115 " Prints out a list of variables using the RealTime driver.\n";
00116
00117 static struct ast_cli_entry cli_load_realtime_cmd = {
00118 { "realtime", "load", NULL, NULL }, cli_load_realtime,
00119 "Used to print out RealTime variables.", cli_load_realtime_usage, NULL };
00120
00121 static char cli_update_realtime_usage[] =
00122 "Usage: realtime update <family> <colmatch> <value>\n"
00123 " Update a single variable using the RealTime driver.\n";
00124
00125 static struct ast_cli_entry cli_update_realtime_cmd = {
00126 { "realtime", "update", NULL, NULL }, cli_update_realtime,
00127 "Used to update RealTime variables.", cli_update_realtime_usage, NULL };
00128
00129 static int realtime_update_exec(struct ast_channel *chan, void *data)
00130 {
00131 char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
00132 struct localuser *u;
00133 int res = 0;
00134
00135 if (ast_strlen_zero(data)) {
00136 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00137 return -1;
00138 }
00139
00140 LOCAL_USER_ADD(u);
00141
00142 if ((family = ast_strdupa(data))) {
00143 if ((colmatch = strchr(family,'|'))) {
00144 crop_data(colmatch);
00145 if ((value = strchr(colmatch,'|'))) {
00146 crop_data(value);
00147 if ((newcol = strchr(value,'|'))) {
00148 crop_data(newcol);
00149 if ((newval = strchr(newcol,'|')))
00150 crop_data(newval);
00151 }
00152 }
00153 }
00154 }
00155 if (! (family && value && colmatch && newcol && newval) ) {
00156 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00157 res = -1;
00158 } else {
00159 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
00160 }
00161
00162 LOCAL_USER_REMOVE(u);
00163
00164 return res;
00165 }
00166
00167
00168 static int realtime_exec(struct ast_channel *chan, void *data)
00169 {
00170 int res=0;
00171 struct localuser *u;
00172 struct ast_variable *var, *itt;
00173 char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
00174 size_t len;
00175
00176 if (ast_strlen_zero(data)) {
00177 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00178 return -1;
00179 }
00180
00181 LOCAL_USER_ADD(u);
00182
00183 if ((family = ast_strdupa(data))) {
00184 if ((colmatch = strchr(family,'|'))) {
00185 crop_data(colmatch);
00186 if ((value = strchr(colmatch,'|'))) {
00187 crop_data(value);
00188 if ((prefix = strchr(value,'|')))
00189 crop_data(prefix);
00190 }
00191 }
00192 }
00193 if (! (family && value && colmatch) ) {
00194 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00195 res = -1;
00196 } else {
00197 if (option_verbose > 3)
00198 ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
00199 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
00200 for (itt = var; itt; itt = itt->next) {
00201 if(prefix) {
00202 len = strlen(prefix) + strlen(itt->name) + 2;
00203 vname = alloca(len);
00204 snprintf(vname,len,"%s%s",prefix,itt->name);
00205
00206 } else
00207 vname = itt->name;
00208
00209 pbx_builtin_setvar_helper(chan, vname, itt->value);
00210 }
00211 ast_variables_destroy(var);
00212 } else if (option_verbose > 3)
00213 ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
00214 }
00215
00216 LOCAL_USER_REMOVE(u);
00217 return res;
00218 }
00219
00220 int unload_module(void)
00221 {
00222 int res;
00223
00224 res = ast_cli_unregister(&cli_load_realtime_cmd);
00225 res |= ast_cli_unregister(&cli_update_realtime_cmd);
00226 res |= ast_unregister_application(uapp);
00227 res |= ast_unregister_application(app);
00228
00229 STANDARD_HANGUP_LOCALUSERS;
00230
00231 return res;
00232 }
00233
00234 int load_module(void)
00235 {
00236 int res;
00237
00238 res = ast_cli_register(&cli_load_realtime_cmd);
00239 res |= ast_cli_register(&cli_update_realtime_cmd);
00240 res |= ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
00241 res |= ast_register_application(app, realtime_exec, synopsis, desc);
00242
00243 return res;
00244 }
00245
00246 char *description(void)
00247 {
00248 return tdesc;
00249 }
00250
00251 int usecount(void)
00252 {
00253 int res;
00254 STANDARD_USECOUNT(res);
00255 return res;
00256 }
00257
00258 char *key()
00259 {
00260 return ASTERISK_GPL_KEY;
00261 }
00262