revert my pass through the tree to remove checks of the result of ast_strdupa
[asterisk/asterisk.git] / apps / app_realtime.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Anthony Minessale <anthmct@yahoo.com>
7  * Mark Spencer <markster@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief RealTime App
23  *
24  * \author Anthony Minessale <anthmct@yahoo.com>
25  * \author Mark Spencer <markster@digium.com>
26  * 
27  * \ingroup applications
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/options.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/config.h"
45 #include "asterisk/module.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/cli.h"
48
49 #define next_one(var) var = var->next
50 #define crop_data(str) { *(str) = '\0' ; (str)++; }
51
52 static char *tdesc = "Realtime Data Lookup/Rewrite";
53 static char *app = "RealTime";
54 static char *uapp = "RealTimeUpdate";
55 static char *synopsis = "Realtime Data Lookup";
56 static char *usynopsis = "Realtime Data Rewrite";
57 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
58 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
59 static char *desc =
60 "Use the RealTime config handler system to read data into channel variables.\n"
61 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
62 "All unique column names will be set as channel variables with optional prefix\n"
63 "to the name.  For example, a prefix of 'var_' would make the column 'name'\n"
64 "become the variable ${var_name}.  REALTIMECOUNT will be set with the number\n"
65 "of values read.\n";
66 static char *udesc = "Use the RealTime config handler system to update a value\n"
67 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
68 "The column <newcol> in 'family' matching column <colmatch>=<value> will be\n"
69 "updated to <newval>.  REALTIMECOUNT will be set with the number of rows\n"
70 "updated or -1 if an error occurs.\n";
71
72 STANDARD_LOCAL_USER;
73 LOCAL_USER_DECL;
74
75 static int cli_load_realtime(int fd, int argc, char **argv) 
76 {
77         char *header_format = "%30s  %-30s\n";
78         struct ast_variable *var=NULL;
79
80         if(argc<5) {
81                 ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
82                 return RESULT_FAILURE;
83         }
84
85         var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
86
87         if(var) {
88                 ast_cli(fd, header_format, "Column Name", "Column Value");
89                 ast_cli(fd, header_format, "--------------------", "--------------------");
90                 while(var) {
91                         ast_cli(fd, header_format, var->name, var->value);
92                         var = var->next;
93                 }
94         } else {
95                 ast_cli(fd, "No rows found matching search criteria.\n");
96         }
97         return RESULT_SUCCESS;
98 }
99
100 static int cli_update_realtime(int fd, int argc, char **argv) {
101         int res = 0;
102
103         if(argc<7) {
104                 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");
105                 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");
106                 return RESULT_FAILURE;
107         }
108
109         res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
110
111         if(res < 0) {
112                 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
113                 return RESULT_SUCCESS;
114         }
115
116        ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
117
118         return RESULT_SUCCESS;
119 }
120
121 static char cli_load_realtime_usage[] =
122 "Usage: realtime load <family> <colmatch> <value>\n"
123 "       Prints out a list of variables using the RealTime driver.\n";
124
125 static struct ast_cli_entry cli_load_realtime_cmd = {
126         { "realtime", "load", NULL, NULL }, cli_load_realtime,
127         "Used to print out RealTime variables.", cli_load_realtime_usage, NULL };
128
129 static char cli_update_realtime_usage[] =
130 "Usage: realtime update <family> <colmatch> <value>\n"
131 "       Update a single variable using the RealTime driver.\n";
132
133 static struct ast_cli_entry cli_update_realtime_cmd = {
134         { "realtime", "update", NULL, NULL }, cli_update_realtime,
135         "Used to update RealTime variables.", cli_update_realtime_usage, NULL };
136
137 static int realtime_update_exec(struct ast_channel *chan, void *data) 
138 {
139         char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
140         struct localuser *u;
141         int res = 0, count = 0;
142         char countc[13];
143
144         if (ast_strlen_zero(data)) {
145                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
146                 return -1;
147         }
148         
149         LOCAL_USER_ADD(u);
150
151         if ((family = ast_strdupa(data))) {
152                 if ((colmatch = strchr(family,'|'))) {
153                         crop_data(colmatch);
154                         if ((value = strchr(colmatch,'|'))) {
155                                 crop_data(value);
156                                 if ((newcol = strchr(value,'|'))) {
157                                         crop_data(newcol);
158                                         if ((newval = strchr(newcol,'|'))) 
159                                                 crop_data(newval);
160                                 }
161                         }
162                 }
163         }
164         if (! (family && value && colmatch && newcol && newval) ) {
165                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
166                 res = -1;
167         } else {
168                 count = ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
169         }
170
171         snprintf(countc, sizeof(countc), "%d", count);
172         pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
173
174         LOCAL_USER_REMOVE(u);
175         
176         return res;
177 }
178
179
180 static int realtime_exec(struct ast_channel *chan, void *data)
181 {
182         int res=0, count=0;
183         struct localuser *u;
184         struct ast_variable *var, *itt;
185         char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
186         char countc[13];
187         size_t len;
188                 
189         if (ast_strlen_zero(data)) {
190                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
191                 return -1;
192         }
193         
194         LOCAL_USER_ADD(u);
195
196         if ((family = ast_strdupa(data))) {
197                 if ((colmatch = strchr(family,'|'))) {
198                         crop_data(colmatch);
199                         if ((value = strchr(colmatch,'|'))) {
200                                 crop_data(value);
201                                 if ((prefix = strchr(value,'|')))
202                                         crop_data(prefix);
203                         }
204                 }
205         }
206         if (! (family && value && colmatch) ) {
207                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
208                 res = -1;
209         } else {
210                 if (option_verbose > 3)
211                         ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
212                 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
213                         for (itt = var; itt; itt = itt->next) {
214                                 if(prefix) {
215                                         len = strlen(prefix) + strlen(itt->name) + 2;
216                                         vname = alloca(len);
217                                         snprintf(vname,len,"%s%s",prefix,itt->name);
218                                         
219                                 } else 
220                                         vname = itt->name;
221
222                                 pbx_builtin_setvar_helper(chan, vname, itt->value);
223                                 count++;
224                         }
225                         ast_variables_destroy(var);
226                 } else if (option_verbose > 3)
227                         ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
228         }
229         snprintf(countc, sizeof(countc), "%d", count);
230         pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
231         
232         LOCAL_USER_REMOVE(u);
233         return res;
234 }
235
236 int unload_module(void)
237 {
238         int res;
239
240         res = ast_cli_unregister(&cli_load_realtime_cmd);
241         res |= ast_cli_unregister(&cli_update_realtime_cmd);
242         res |= ast_unregister_application(uapp);
243         res |= ast_unregister_application(app);
244
245         STANDARD_HANGUP_LOCALUSERS;
246
247         return res;
248 }
249
250 int load_module(void)
251 {
252         int res;
253
254         res = ast_cli_register(&cli_load_realtime_cmd);
255         res |= ast_cli_register(&cli_update_realtime_cmd);
256         res |= ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
257         res |= ast_register_application(app, realtime_exec, synopsis, desc);
258
259         return res;
260 }
261
262 char *description(void)
263 {
264         return tdesc;
265 }
266
267 int usecount(void)
268 {
269         int res;
270         STANDARD_USECOUNT(res);
271         return res;
272 }
273
274 char *key()
275 {
276         return ASTERISK_GPL_KEY;
277 }
278