2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Anthony Minessale <anthmct@yahoo.com>
9 * Mark Spencer <markster@digium.com>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
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>
27 #define next_one(var) var = var->next
28 #define crop_data(str) { *(str) = '\0' ; (str)++; }
30 static char *tdesc = "Realtime Data Lookup/Rewrite";
31 static char *app = "RealTime";
32 static char *uapp = "RealTimeUpdate";
33 static char *synopsis = "Realtime Data Lookup";
34 static char *usynopsis = "Realtime Data Rewrite";
35 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
36 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
37 static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
38 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
39 "All unique column names will be set as channel variables with optional prefix to the name.\n"
40 "e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
41 static char *udesc = "Use the RealTime config handler system to update a value\n"
42 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
43 "The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";
48 static int realtime_update_exec(struct ast_channel *chan, void *data) {
49 char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
53 ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
57 if ((family = ast_strdupa(data))) {
58 if ((colmatch = strchr(family,'|'))) {
60 if ((value = strchr(colmatch,'|'))) {
62 if ((newcol = strchr(value,'|'))) {
64 if ((newval = strchr(newcol,'|')))
70 if (! (family && value && colmatch && newcol && newval) ) {
71 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
74 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
83 static int realtime_exec(struct ast_channel *chan, void *data)
87 struct ast_variable *var, *itt;
88 char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
92 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
96 if ((family = ast_strdupa(data))) {
97 if ((colmatch = strchr(family,'|'))) {
99 if ((value = strchr(colmatch,'|'))) {
101 if ((prefix = strchr(value,'|')))
106 if (! (family && value && colmatch) ) {
107 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
110 if (option_verbose > 3)
111 ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
112 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
113 for (itt = var; itt; itt = itt->next) {
115 len = strlen(prefix) + strlen(itt->name) + 2;
117 snprintf(vname,len,"%s%s",prefix,itt->name);
122 pbx_builtin_setvar_helper(chan, vname, itt->value);
124 ast_destroy_realtime(var);
125 } else if (option_verbose > 3)
126 ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
129 LOCAL_USER_REMOVE(u);
133 int unload_module(void)
135 STANDARD_HANGUP_LOCALUSERS;
136 ast_unregister_application(uapp);
137 return ast_unregister_application(app);
140 int load_module(void)
142 ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
143 return ast_register_application(app, realtime_exec, synopsis, desc);
146 char *description(void)
154 STANDARD_USECOUNT(res);
160 return ASTERISK_GPL_KEY;