Fix a few things I missed to ensure zt_chan_conf structure is not modified in mkintf
[asterisk-bristuff.git] / apps / app_realtime.c
blob48e1dca5bd6d5c0965bc5dbae4be49ea7afe676c
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Anthony Minessale <anthmct@yahoo.com>
7 * Mark Spencer <markster@digium.com>
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.
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.
20 /*! \file
22 * \brief RealTime App
24 * \author Anthony Minessale <anthmct@yahoo.com>
25 * \author Mark Spencer <markster@digium.com>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
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"
49 #define next_one(var) var = var->next
50 #define crop_data(str) { *(str) = '\0' ; (str)++; }
52 static char *app = "RealTime";
53 static char *uapp = "RealTimeUpdate";
54 static char *synopsis = "Realtime Data Lookup";
55 static char *usynopsis = "Realtime Data Rewrite";
56 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
57 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
58 static char *desc =
59 "Use the RealTime config handler system to read data into channel variables.\n"
60 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
61 "All unique column names will be set as channel variables with optional prefix\n"
62 "to the name. For example, a prefix of 'var_' would make the column 'name'\n"
63 "become the variable ${var_name}. REALTIMECOUNT will be set with the number\n"
64 "of values read.\n";
65 static char *udesc = "Use the RealTime config handler system to update a value\n"
66 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
67 "The column <newcol> in 'family' matching column <colmatch>=<value> will be\n"
68 "updated to <newval>. REALTIMECOUNT will be set with the number of rows\n"
69 "updated or -1 if an error occurs.\n";
72 static int cli_realtime_load(int fd, int argc, char **argv)
74 char *header_format = "%30s %-30s\n";
75 struct ast_variable *var=NULL;
77 if(argc<5) {
78 ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
79 return RESULT_FAILURE;
82 var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
84 if(var) {
85 ast_cli(fd, header_format, "Column Name", "Column Value");
86 ast_cli(fd, header_format, "--------------------", "--------------------");
87 while(var) {
88 ast_cli(fd, header_format, var->name, var->value);
89 var = var->next;
91 } else {
92 ast_cli(fd, "No rows found matching search criteria.\n");
94 return RESULT_SUCCESS;
97 static int cli_realtime_update(int fd, int argc, char **argv) {
98 int res = 0;
100 if(argc<7) {
101 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");
102 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");
103 return RESULT_FAILURE;
106 res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
108 if(res < 0) {
109 ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
110 return RESULT_SUCCESS;
113 ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
115 return RESULT_SUCCESS;
118 static char cli_realtime_load_usage[] =
119 "Usage: realtime load <family> <colmatch> <value>\n"
120 " Prints out a list of variables using the RealTime driver.\n";
122 static char cli_realtime_update_usage[] =
123 "Usage: realtime update <family> <colmatch> <value>\n"
124 " Update a single variable using the RealTime driver.\n";
126 static struct ast_cli_entry cli_realtime[] = {
127 { { "realtime", "load", NULL, NULL },
128 cli_realtime_load, "Used to print out RealTime variables.",
129 cli_realtime_load_usage, NULL },
131 { { "realtime", "update", NULL, NULL },
132 cli_realtime_update, "Used to update RealTime variables.",
133 cli_realtime_update_usage, NULL },
136 static int realtime_update_exec(struct ast_channel *chan, void *data)
138 char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
139 struct ast_module_user *u;
140 int res = 0, count = 0;
141 char countc[13];
143 ast_log(LOG_WARNING, "The RealTimeUpdate application has been deprecated in favor of the REALTIME dialplan function.\n");
145 if (ast_strlen_zero(data)) {
146 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
147 return -1;
150 u = ast_module_user_add(chan);
152 family = ast_strdupa(data);
153 if ((colmatch = strchr(family,'|'))) {
154 crop_data(colmatch);
155 if ((value = strchr(colmatch,'|'))) {
156 crop_data(value);
157 if ((newcol = strchr(value,'|'))) {
158 crop_data(newcol);
159 if ((newval = strchr(newcol,'|')))
160 crop_data(newval);
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);
171 snprintf(countc, sizeof(countc), "%d", count);
172 pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
174 ast_module_user_remove(u);
176 return res;
180 static int realtime_exec(struct ast_channel *chan, void *data)
182 int res=0, count=0;
183 struct ast_module_user *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;
189 ast_log(LOG_WARNING, "The RealTime application has been deprecated in favor of the REALTIME dialplan function.\n");
191 if (ast_strlen_zero(data)) {
192 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
193 return -1;
196 u = ast_module_user_add(chan);
198 family = ast_strdupa(data);
199 if ((colmatch = strchr(family,'|'))) {
200 crop_data(colmatch);
201 if ((value = strchr(colmatch,'|'))) {
202 crop_data(value);
203 if ((prefix = strchr(value,'|')))
204 crop_data(prefix);
207 if (! (family && value && colmatch) ) {
208 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
209 res = -1;
210 } else {
211 if (option_verbose > 3)
212 ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
213 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
214 for (itt = var; itt; itt = itt->next) {
215 if(prefix) {
216 len = strlen(prefix) + strlen(itt->name) + 2;
217 vname = alloca(len);
218 snprintf(vname,len,"%s%s",prefix,itt->name);
220 } else
221 vname = itt->name;
223 pbx_builtin_setvar_helper(chan, vname, itt->value);
224 count++;
226 ast_variables_destroy(var);
227 } else if (option_verbose > 3)
228 ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
230 snprintf(countc, sizeof(countc), "%d", count);
231 pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
233 ast_module_user_remove(u);
234 return res;
237 static int unload_module(void)
239 int res;
241 ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
242 res = ast_unregister_application(uapp);
243 res |= ast_unregister_application(app);
245 ast_module_user_hangup_all();
247 return res;
250 static int load_module(void)
252 int res;
254 ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
255 res = ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
256 res |= ast_register_application(app, realtime_exec, synopsis, desc);
258 return res;
261 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");