2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Anthony Minessale <anthmct@yahoo.com>
9 * Mark Spencer <markster@digium.com>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/options.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/config.h>
21 #include <asterisk/module.h>
22 #include <asterisk/lock.h>
23 #include <asterisk/cli.h>
28 #define next_one(var) var = var->next
29 #define crop_data(str) { *(str) = '\0' ; (str)++; }
31 static char *tdesc = "Realtime Data Lookup/Rewrite";
32 static char *app = "RealTime";
33 static char *uapp = "RealTimeUpdate";
34 static char *synopsis = "Realtime Data Lookup";
35 static char *usynopsis = "Realtime Data Rewrite";
36 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
37 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
38 static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
39 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
40 "All unique column names will be set as channel variables with optional prefix to the name.\n"
41 "e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
42 static char *udesc = "Use the RealTime config handler system to update a value\n"
43 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
44 "The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";
49 static int cli_load_realtime(int fd, int argc, char **argv)
51 char *header_format = "%30s %-30s\n";
52 struct ast_variable *var=NULL;
55 ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
56 return RESULT_FAILURE;
59 var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
62 ast_cli(fd, header_format, "Column Name", "Column Value");
63 ast_cli(fd, header_format, "--------------------", "--------------------");
65 ast_cli(fd, header_format, var->name, var->value);
69 ast_cli(fd, "No rows found matching search criteria.\n");
71 return RESULT_SUCCESS;
74 static int cli_update_realtime(int fd, int argc, char **argv) {
78 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");
79 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");
80 return RESULT_FAILURE;
83 res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
86 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
87 return RESULT_SUCCESS;
90 ast_cli(fd, "Updated RealTime record.\n");
92 return RESULT_SUCCESS;
95 static char cli_load_realtime_usage[] =
96 "Usage: realtime load <family> <colmatch> <value>\n"
97 " Prints out a list of variables using the RealTime driver.\n";
99 static struct ast_cli_entry cli_load_realtime_cmd = {
100 { "realtime", "load", NULL, NULL }, cli_load_realtime,
101 "Used to print out RealTime variables.", cli_load_realtime_usage, NULL };
103 static char cli_update_realtime_usage[] =
104 "Usage: realtime update <family> <colmatch> <value>\n"
105 " Update a single variable using the RealTime driver.\n";
107 static struct ast_cli_entry cli_update_realtime_cmd = {
108 { "realtime", "update", NULL, NULL }, cli_update_realtime,
109 "Used to update RealTime variables.", cli_update_realtime_usage, NULL };
111 static int realtime_update_exec(struct ast_channel *chan, void *data)
113 char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
117 ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
121 if ((family = ast_strdupa(data))) {
122 if ((colmatch = strchr(family,'|'))) {
124 if ((value = strchr(colmatch,'|'))) {
126 if ((newcol = strchr(value,'|'))) {
128 if ((newval = strchr(newcol,'|')))
134 if (! (family && value && colmatch && newcol && newval) ) {
135 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
138 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
141 LOCAL_USER_REMOVE(u);
147 static int realtime_exec(struct ast_channel *chan, void *data)
151 struct ast_variable *var, *itt;
152 char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
156 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
160 if ((family = ast_strdupa(data))) {
161 if ((colmatch = strchr(family,'|'))) {
163 if ((value = strchr(colmatch,'|'))) {
165 if ((prefix = strchr(value,'|')))
170 if (! (family && value && colmatch) ) {
171 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
174 if (option_verbose > 3)
175 ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
176 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
177 for (itt = var; itt; itt = itt->next) {
179 len = strlen(prefix) + strlen(itt->name) + 2;
181 snprintf(vname,len,"%s%s",prefix,itt->name);
186 pbx_builtin_setvar_helper(chan, vname, itt->value);
188 ast_destroy_realtime(var);
189 } else if (option_verbose > 3)
190 ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
193 LOCAL_USER_REMOVE(u);
197 int unload_module(void)
199 STANDARD_HANGUP_LOCALUSERS;
200 ast_cli_unregister(&cli_load_realtime_cmd);
201 ast_cli_unregister(&cli_update_realtime_cmd);
202 ast_unregister_application(uapp);
203 return ast_unregister_application(app);
206 int load_module(void)
208 ast_cli_register(&cli_load_realtime_cmd);
209 ast_cli_register(&cli_update_realtime_cmd);
210 ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
211 return ast_register_application(app, realtime_exec, synopsis, desc);
214 char *description(void)
222 STANDARD_USECOUNT(res);
228 return ASTERISK_GPL_KEY;