Sat Aug 6 00:39:21 2011

Asterisk developer's documentation


app_realtime.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Anthony Minessale <anthmct@yahoo.com>
00007  * Mark Spencer <markster@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief RealTime App
00023  *
00024  * \author Anthony Minessale <anthmct@yahoo.com>
00025  * \author Mark Spencer <markster@digium.com>
00026  * 
00027  * \ingroup applications
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 165255 $")
00033 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <unistd.h>
00038 
00039 #include "asterisk/file.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/options.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/config.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/lock.h"
00047 #include "asterisk/cli.h"
00048 
00049 #define next_one(var) var = var->next
00050 #define crop_data(str) { *(str) = '\0' ; (str)++; }
00051 
00052 static char *app = "RealTime";
00053 static char *uapp = "RealTimeUpdate";
00054 static char *synopsis = "Realtime Data Lookup";
00055 static char *usynopsis = "Realtime Data Rewrite";
00056 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
00057 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
00058 static char *desc =
00059 "Use the RealTime config handler system to read data into channel variables.\n"
00060 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
00061 "All unique column names will be set as channel variables with optional prefix\n"
00062 "to the name.  For example, a prefix of 'var_' would make the column 'name'\n"
00063 "become the variable ${var_name}.  REALTIMECOUNT will be set with the number\n"
00064 "of values read.\n";
00065 static char *udesc = "Use the RealTime config handler system to update a value\n"
00066 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
00067 "The column <newcol> in 'family' matching column <colmatch>=<value> will be\n"
00068 "updated to <newval>.  REALTIMECOUNT will be set with the number of rows\n"
00069 "updated or -1 if an error occurs.\n";
00070 
00071 
00072 static int cli_realtime_load(int fd, int argc, char **argv) 
00073 {
00074    char *header_format = "%30s  %-30s\n";
00075    struct ast_variable *var = NULL, *save = NULL;
00076 
00077    if (argc < 5) {
00078       ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
00079       return RESULT_FAILURE;
00080    }
00081 
00082    var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
00083 
00084    if (var) {
00085       save = var;
00086       ast_cli(fd, header_format, "Column Name", "Column Value");
00087       ast_cli(fd, header_format, "--------------------", "--------------------");
00088       while (var) {
00089          ast_cli(fd, header_format, var->name, var->value);
00090          var = var->next;
00091       }
00092       ast_variables_destroy(save);
00093    } else {
00094       ast_cli(fd, "No rows found matching search criteria.\n");
00095    }
00096    return RESULT_SUCCESS;
00097 }
00098 
00099 static int cli_realtime_update(int fd, int argc, char **argv) {
00100    int res = 0;
00101 
00102    if(argc<7) {
00103       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");
00104       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");
00105       return RESULT_FAILURE;
00106    }
00107 
00108    res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
00109 
00110    if(res < 0) {
00111       ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00112       return RESULT_SUCCESS;
00113    }
00114 
00115        ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
00116 
00117    return RESULT_SUCCESS;
00118 }
00119 
00120 static char cli_realtime_load_usage[] =
00121 "Usage: realtime load <family> <colmatch> <value>\n"
00122 "       Prints out a list of variables using the RealTime driver.\n";
00123 
00124 static char cli_realtime_update_usage[] =
00125 "Usage: realtime update <family> <colmatch> <value>\n"
00126 "       Update a single variable using the RealTime driver.\n";
00127 
00128 static struct ast_cli_entry cli_realtime[] = {
00129    { { "realtime", "load", NULL, NULL },
00130    cli_realtime_load, "Used to print out RealTime variables.",
00131    cli_realtime_load_usage, NULL },
00132 
00133    { { "realtime", "update", NULL, NULL },
00134    cli_realtime_update, "Used to update RealTime variables.",
00135    cli_realtime_update_usage, NULL },
00136 };
00137 
00138 static int realtime_update_exec(struct ast_channel *chan, void *data) 
00139 {
00140    char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
00141    struct ast_module_user *u;
00142    int res = 0, count = 0;
00143    char countc[13];
00144 
00145         ast_log(LOG_WARNING, "The RealTimeUpdate application has been deprecated in favor of the REALTIME dialplan function.\n");
00146 
00147    if (ast_strlen_zero(data)) {
00148       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00149       return -1;
00150    }
00151    
00152    u = ast_module_user_add(chan);
00153 
00154    family = ast_strdupa(data);
00155    if ((colmatch = strchr(family,'|'))) {
00156       crop_data(colmatch);
00157       if ((value = strchr(colmatch,'|'))) {
00158          crop_data(value);
00159          if ((newcol = strchr(value,'|'))) {
00160             crop_data(newcol);
00161             if ((newval = strchr(newcol,'|'))) 
00162                crop_data(newval);
00163          }
00164       }
00165    }
00166    if (! (family && value && colmatch && newcol && newval) ) {
00167       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00168       res = -1;
00169    } else {
00170       count = ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
00171    }
00172 
00173    snprintf(countc, sizeof(countc), "%d", count);
00174    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00175 
00176    ast_module_user_remove(u);
00177    
00178    return res;
00179 }
00180 
00181 
00182 static int realtime_exec(struct ast_channel *chan, void *data)
00183 {
00184    int res=0, count=0;
00185    struct ast_module_user *u;
00186    struct ast_variable *var, *itt;
00187    char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
00188    char countc[13];
00189    size_t len;
00190       
00191         ast_log(LOG_WARNING, "The RealTime application has been deprecated in favor of the REALTIME dialplan function.\n");
00192 
00193    if (ast_strlen_zero(data)) {
00194       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00195       return -1;
00196    }
00197    
00198    u = ast_module_user_add(chan);
00199 
00200    family = ast_strdupa(data);
00201    if ((colmatch = strchr(family,'|'))) {
00202       crop_data(colmatch);
00203       if ((value = strchr(colmatch,'|'))) {
00204          crop_data(value);
00205          if ((prefix = strchr(value,'|')))
00206             crop_data(prefix);
00207       }
00208    }
00209    if (! (family && value && colmatch) ) {
00210       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00211       res = -1;
00212    } else {
00213       if (option_verbose > 3)
00214          ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
00215       if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
00216          for (itt = var; itt; itt = itt->next) {
00217             if(prefix) {
00218                len = strlen(prefix) + strlen(itt->name) + 2;
00219                vname = alloca(len);
00220                snprintf(vname,len,"%s%s",prefix,itt->name);
00221                
00222             } else 
00223                vname = itt->name;
00224 
00225             pbx_builtin_setvar_helper(chan, vname, itt->value);
00226             count++;
00227          }
00228          ast_variables_destroy(var);
00229       } else if (option_verbose > 3)
00230          ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
00231    }
00232    snprintf(countc, sizeof(countc), "%d", count);
00233    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00234    
00235    ast_module_user_remove(u);
00236    return res;
00237 }
00238 
00239 static int unload_module(void)
00240 {
00241    int res;
00242 
00243    ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00244    res = ast_unregister_application(uapp);
00245    res |= ast_unregister_application(app);
00246 
00247    ast_module_user_hangup_all();
00248 
00249    return res;
00250 }
00251 
00252 static int load_module(void)
00253 {
00254    int res;
00255 
00256    ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00257    res = ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
00258    res |= ast_register_application(app, realtime_exec, synopsis, desc);
00259 
00260    return res;
00261 }
00262 
00263 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");

Generated on Sat Aug 6 00:39:21 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7