d4ded9c2084cdc57f1fa6c6a14dde338256c5c55
[asterisk/asterisk.git] / apps / app_realtime.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * RealTime App
5  * 
6  * Copyright (C) 1999 - 2005, 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 <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "asterisk.h"
20
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
22
23 #include "asterisk/file.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/channel.h"
26 #include "asterisk/options.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/config.h"
29 #include "asterisk/module.h"
30 #include "asterisk/lock.h"
31 #include "asterisk/cli.h"
32
33 #define next_one(var) var = var->next
34 #define crop_data(str) { *(str) = '\0' ; (str)++; }
35
36 static char *tdesc = "Realtime Data Lookup/Rewrite";
37 static char *app = "RealTime";
38 static char *uapp = "RealTimeUpdate";
39 static char *synopsis = "Realtime Data Lookup";
40 static char *usynopsis = "Realtime Data Rewrite";
41 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
42 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
43 static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
44 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
45 "All unique column names will be set as channel variables with optional prefix to the name.\n"
46 "e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
47 static char *udesc = "Use the RealTime config handler system to update a value\n"
48 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
49 "The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";
50
51 STANDARD_LOCAL_USER;
52 LOCAL_USER_DECL;
53
54 static int cli_load_realtime(int fd, int argc, char **argv) 
55 {
56         char *header_format = "%30s  %-30s\n";
57         struct ast_variable *var=NULL;
58
59         if(argc<5) {
60                 ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
61                 return RESULT_FAILURE;
62         }
63
64         var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
65
66         if(var) {
67                 ast_cli(fd, header_format, "Column Name", "Column Value");
68                 ast_cli(fd, header_format, "--------------------", "--------------------");
69                 while(var) {
70                         ast_cli(fd, header_format, var->name, var->value);
71                         var = var->next;
72                 }
73         } else {
74                 ast_cli(fd, "No rows found matching search criteria.\n");
75         }
76         return RESULT_SUCCESS;
77 }
78
79 static int cli_update_realtime(int fd, int argc, char **argv) {
80         int res = 0;
81
82         if(argc<7) {
83                 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");
84                 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");
85                 return RESULT_FAILURE;
86         }
87
88         res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
89
90         if(res < 0) {
91                 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
92                 return RESULT_SUCCESS;
93         }
94
95        ast_cli(fd, "Updated %d RealTime record(s).\n", res);
96
97         return RESULT_SUCCESS;
98 }
99
100 static char cli_load_realtime_usage[] =
101 "Usage: realtime load <family> <colmatch> <value>\n"
102 "       Prints out a list of variables using the RealTime driver.\n";
103
104 static struct ast_cli_entry cli_load_realtime_cmd = {
105         { "realtime", "load", NULL, NULL }, cli_load_realtime,
106         "Used to print out RealTime variables.", cli_load_realtime_usage, NULL };
107
108 static char cli_update_realtime_usage[] =
109 "Usage: realtime update <family> <colmatch> <value>\n"
110 "       Update a single variable using the RealTime driver.\n";
111
112 static struct ast_cli_entry cli_update_realtime_cmd = {
113         { "realtime", "update", NULL, NULL }, cli_update_realtime,
114         "Used to update RealTime variables.", cli_update_realtime_usage, NULL };
115
116 static int realtime_update_exec(struct ast_channel *chan, void *data) 
117 {
118         char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
119         struct localuser *u;
120         int res = 0;
121         if (!data) {
122         ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
123         return -1;
124     }
125         LOCAL_USER_ADD(u);
126         if ((family = ast_strdupa(data))) {
127                 if ((colmatch = strchr(family,'|'))) {
128                         crop_data(colmatch);
129                         if ((value = strchr(colmatch,'|'))) {
130                                 crop_data(value);
131                                 if ((newcol = strchr(value,'|'))) {
132                                         crop_data(newcol);
133                                         if ((newval = strchr(newcol,'|'))) 
134                                                 crop_data(newval);
135                                 }
136                         }
137                 }
138         }
139         if (! (family && value && colmatch && newcol && newval) ) {
140                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
141                 res = -1;
142         } else {
143                 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
144         }
145
146         LOCAL_USER_REMOVE(u);
147         return res;
148
149 }
150
151
152 static int realtime_exec(struct ast_channel *chan, void *data)
153 {
154         int res=0;
155         struct localuser *u;
156         struct ast_variable *var, *itt;
157         char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
158         size_t len;
159
160         if (!data) {
161                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
162                 return -1;
163         }
164         LOCAL_USER_ADD(u);
165         if ((family = ast_strdupa(data))) {
166                 if ((colmatch = strchr(family,'|'))) {
167                         crop_data(colmatch);
168                         if ((value = strchr(colmatch,'|'))) {
169                                 crop_data(value);
170                                 if ((prefix = strchr(value,'|')))
171                                         crop_data(prefix);
172                         }
173                 }
174         }
175         if (! (family && value && colmatch) ) {
176                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
177                 res = -1;
178         } else {
179                 if (option_verbose > 3)
180                         ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
181                 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
182                         for (itt = var; itt; itt = itt->next) {
183                                 if(prefix) {
184                                         len = strlen(prefix) + strlen(itt->name) + 2;
185                                         vname = alloca(len);
186                                         snprintf(vname,len,"%s%s",prefix,itt->name);
187                                         
188                                 } else 
189                                         vname = itt->name;
190
191                                 pbx_builtin_setvar_helper(chan, vname, itt->value);
192                         }
193                         ast_variables_destroy(var);
194                 } else if (option_verbose > 3)
195                         ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
196         }
197         
198         LOCAL_USER_REMOVE(u);
199         return res;
200 }
201
202 int unload_module(void)
203 {
204         STANDARD_HANGUP_LOCALUSERS;
205         ast_cli_unregister(&cli_load_realtime_cmd);
206         ast_cli_unregister(&cli_update_realtime_cmd);
207         ast_unregister_application(uapp);
208         return ast_unregister_application(app);
209 }
210
211 int load_module(void)
212 {
213         ast_cli_register(&cli_load_realtime_cmd);
214         ast_cli_register(&cli_update_realtime_cmd);
215         ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
216         return ast_register_application(app, realtime_exec, synopsis, desc);
217 }
218
219 char *description(void)
220 {
221         return tdesc;
222 }
223
224 int usecount(void)
225 {
226         int res;
227         STANDARD_USECOUNT(res);
228         return res;
229 }
230
231 char *key()
232 {
233         return ASTERISK_GPL_KEY;
234 }
235