Add CLI for realtime stuff (bug #2626)
[asterisk/asterisk.git] / apps / app_realtime.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * RealTime App
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Anthony Minessale <anthmct@yahoo.com>
9  * Mark Spencer <markster@digium.com>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License
13  */
14
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>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 #define next_one(var) var = var->next
29 #define crop_data(str) { *(str) = '\0' ; (str)++; }
30
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";
45
46 STANDARD_LOCAL_USER;
47 LOCAL_USER_DECL;
48
49 static int cli_load_realtime(int fd, int argc, char **argv) 
50 {
51         char *header_format = "%30s  %-30s\n";
52         struct ast_variable *var=NULL;
53
54         if(argc<5) {
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;
57         }
58
59         var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
60
61         if(var) {
62                 ast_cli(fd, header_format, "Column Name", "Column Value");
63                 ast_cli(fd, header_format, "--------------------", "--------------------");
64                 while(var) {
65                         ast_cli(fd, header_format, var->name, var->value);
66                         var = var->next;
67                 }
68         } else {
69                 ast_cli(fd, "No rows found matching search criteria.\n");
70         }
71         return RESULT_SUCCESS;
72 }
73
74 static int cli_update_realtime(int fd, int argc, char **argv) {
75         int res = 0;
76
77         if(argc<7) {
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;
81         }
82
83         res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
84
85         if(res < 0) {
86                 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
87                 return RESULT_SUCCESS;
88         }
89
90         ast_cli(fd, "Updated RealTime record.\n");
91
92         return RESULT_SUCCESS;
93 }
94
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";
98
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 };
102
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";
106
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 };
110
111 static int realtime_update_exec(struct ast_channel *chan, void *data) 
112 {
113         char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
114         struct localuser *u;
115         int res = 0;
116         if (!data) {
117         ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
118         return -1;
119     }
120         LOCAL_USER_ADD(u);
121         if ((family = ast_strdupa(data))) {
122                 if ((colmatch = strchr(family,'|'))) {
123                         crop_data(colmatch);
124                         if ((value = strchr(colmatch,'|'))) {
125                                 crop_data(value);
126                                 if ((newcol = strchr(value,'|'))) {
127                                         crop_data(newcol);
128                                         if ((newval = strchr(newcol,'|'))) 
129                                                 crop_data(newval);
130                                 }
131                         }
132                 }
133         }
134         if (! (family && value && colmatch && newcol && newval) ) {
135                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
136                 res = -1;
137         } else {
138                 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
139         }
140
141         LOCAL_USER_REMOVE(u);
142         return res;
143
144 }
145
146
147 static int realtime_exec(struct ast_channel *chan, void *data)
148 {
149         int res=0;
150         struct localuser *u;
151         struct ast_variable *var, *itt;
152         char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
153         size_t len;
154
155         if (!data) {
156                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
157                 return -1;
158         }
159         LOCAL_USER_ADD(u);
160         if ((family = ast_strdupa(data))) {
161                 if ((colmatch = strchr(family,'|'))) {
162                         crop_data(colmatch);
163                         if ((value = strchr(colmatch,'|'))) {
164                                 crop_data(value);
165                                 if ((prefix = strchr(value,'|')))
166                                         crop_data(prefix);
167                         }
168                 }
169         }
170         if (! (family && value && colmatch) ) {
171                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
172                 res = -1;
173         } else {
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) {
178                                 if(prefix) {
179                                         len = strlen(prefix) + strlen(itt->name) + 2;
180                                         vname = alloca(len);
181                                         snprintf(vname,len,"%s%s",prefix,itt->name);
182                                         
183                                 } else 
184                                         vname = itt->name;
185
186                                 pbx_builtin_setvar_helper(chan, vname, itt->value);
187                         }
188                         ast_destroy_realtime(var);
189                 } else if (option_verbose > 3)
190                         ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
191         }
192         
193         LOCAL_USER_REMOVE(u);
194         return res;
195 }
196
197 int unload_module(void)
198 {
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);
204 }
205
206 int load_module(void)
207 {
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);
212 }
213
214 char *description(void)
215 {
216         return tdesc;
217 }
218
219 int usecount(void)
220 {
221         int res;
222         STANDARD_USECOUNT(res);
223         return res;
224 }
225
226 char *key()
227 {
228         return ASTERISK_GPL_KEY;
229 }
230