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.
24 * \author Anthony Minessale <anthmct@yahoo.com>
25 * \author Mark Spencer <markster@digium.com>
27 * \ingroup applications
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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>)";
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"
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
;
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
);
85 ast_cli(fd
, header_format
, "Column Name", "Column Value");
86 ast_cli(fd
, header_format
, "--------------------", "--------------------");
88 ast_cli(fd
, header_format
, var
->name
, var
->value
);
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
) {
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
);
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;
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
);
150 u
= ast_module_user_add(chan
);
152 family
= ast_strdupa(data
);
153 if ((colmatch
= strchr(family
,'|'))) {
155 if ((value
= strchr(colmatch
,'|'))) {
157 if ((newcol
= strchr(value
,'|'))) {
159 if ((newval
= strchr(newcol
,'|')))
164 if (! (family
&& value
&& colmatch
&& newcol
&& newval
) ) {
165 ast_log(LOG_ERROR
,"Invalid input: usage %s\n",UUSAGE
);
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
);
180 static int realtime_exec(struct ast_channel
*chan
, void *data
)
183 struct ast_module_user
*u
;
184 struct ast_variable
*var
, *itt
;
185 char *family
=NULL
, *colmatch
=NULL
, *value
=NULL
, *prefix
=NULL
, *vname
=NULL
;
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
);
196 u
= ast_module_user_add(chan
);
198 family
= ast_strdupa(data
);
199 if ((colmatch
= strchr(family
,'|'))) {
201 if ((value
= strchr(colmatch
,'|'))) {
203 if ((prefix
= strchr(value
,'|')))
207 if (! (family
&& value
&& colmatch
) ) {
208 ast_log(LOG_ERROR
,"Invalid input: usage %s\n",USAGE
);
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
) {
216 len
= strlen(prefix
) + strlen(itt
->name
) + 2;
218 snprintf(vname
,len
,"%s%s",prefix
,itt
->name
);
223 pbx_builtin_setvar_helper(chan
, vname
, itt
->value
);
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
);
237 static int unload_module(void)
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();
250 static int load_module(void)
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
);
261 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Realtime Data Lookup/Rewrite");