Mon May 14 04:42:53 2007

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$")
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;
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       ast_cli(fd, header_format, "Column Name", "Column Value");
00086       ast_cli(fd, header_format, "--------------------", "--------------------");
00087       while(var) {
00088          ast_cli(fd, header_format, var->name, var->value);
00089          var = var->next;
00090       }
00091    } else {
00092       ast_cli(fd, "No rows found matching search criteria.\n");
00093    }
00094    return RESULT_SUCCESS;
00095 }
00096 
00097 static int cli_realtime_update(int fd, int argc, char **argv) {
00098    int res = 0;
00099 
00100    if(argc<7) {
00101       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");
00102       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");
00103       return RESULT_FAILURE;
00104    }
00105 
00106    res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
00107 
00108    if(res < 0) {
00109       ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00110       return RESULT_SUCCESS;
00111    }
00112 
00113        ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
00114 
00115    return RESULT_SUCCESS;
00116 }
00117 
00118 static char cli_realtime_load_usage[] =
00119 "Usage: realtime load <family> <colmatch> <value>\n"
00120 "       Prints out a list of variables using the RealTime driver.\n";
00121 
00122 static char cli_realtime_update_usage[] =
00123 "Usage: realtime update <family> <colmatch> <value>\n"
00124 "       Update a single variable using the RealTime driver.\n";
00125 
00126 static struct ast_cli_entry cli_realtime[] = {
00127    { { "realtime", "load", NULL, NULL },
00128    cli_realtime_load, "Used to print out RealTime variables.",
00129    cli_realtime_load_usage, NULL },
00130 
00131    { { "realtime", "update", NULL, NULL },
00132    cli_realtime_update, "Used to update RealTime variables.",
00133    cli_realtime_update_usage, NULL },
00134 };
00135 
00136 static int realtime_update_exec(struct ast_channel *chan, void *data) 
00137 {
00138    char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
00139    struct ast_module_user *u;
00140    int res = 0, count = 0;
00141    char countc[13];
00142 
00143         ast_log(LOG_WARNING, "The RealTimeUpdate application has been deprecated in favor of the REALTIME dialplan function.\n");
00144 
00145    if (ast_strlen_zero(data)) {
00146       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00147       return -1;
00148    }
00149    
00150    u = ast_module_user_add(chan);
00151 
00152    family = ast_strdupa(data);
00153    if ((colmatch = strchr(family,'|'))) {
00154       crop_data(colmatch);
00155       if ((value = strchr(colmatch,'|'))) {
00156          crop_data(value);
00157          if ((newcol = strchr(value,'|'))) {
00158             crop_data(newcol);
00159             if ((newval = strchr(newcol,'|'))) 
00160                crop_data(newval);
00161          }
00162       }
00163    }
00164    if (! (family && value && colmatch && newcol && newval) ) {
00165       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00166       res = -1;
00167    } else {
00168       count = ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
00169    }
00170 
00171    snprintf(countc, sizeof(countc), "%d", count);
00172    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00173 
00174    ast_module_user_remove(u);
00175    
00176    return res;
00177 }
00178 
00179 
00180 static int realtime_exec(struct ast_channel *chan, void *data)
00181 {
00182    int res=0, count=0;
00183    struct ast_module_user *u;
00184    struct ast_variable *var, *itt;
00185    char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
00186    char countc[13];
00187    size_t len;
00188       
00189         ast_log(LOG_WARNING, "The RealTime application has been deprecated in favor of the REALTIME dialplan function.\n");
00190 
00191    if (ast_strlen_zero(data)) {
00192       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00193       return -1;
00194    }
00195    
00196    u = ast_module_user_add(chan);
00197 
00198    family = ast_strdupa(data);
00199    if ((colmatch = strchr(family,'|'))) {
00200       crop_data(colmatch);
00201       if ((value = strchr(colmatch,'|'))) {
00202          crop_data(value);
00203          if ((prefix = strchr(value,'|')))
00204             crop_data(prefix);
00205       }
00206    }
00207    if (! (family && value && colmatch) ) {
00208       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00209       res = -1;
00210    } else {
00211       if (option_verbose > 3)
00212          ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
00213       if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
00214          for (itt = var; itt; itt = itt->next) {
00215             if(prefix) {
00216                len = strlen(prefix) + strlen(itt->name) + 2;
00217                vname = alloca(len);
00218                snprintf(vname,len,"%s%s",prefix,itt->name);
00219                
00220             } else 
00221                vname = itt->name;
00222 
00223             pbx_builtin_setvar_helper(chan, vname, itt->value);
00224             count++;
00225          }
00226          ast_variables_destroy(var);
00227       } else if (option_verbose > 3)
00228          ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
00229    }
00230    snprintf(countc, sizeof(countc), "%d", count);
00231    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00232    
00233    ast_module_user_remove(u);
00234    return res;
00235 }
00236 
00237 static int unload_module(void)
00238 {
00239    int res;
00240 
00241    ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00242    res = ast_unregister_application(uapp);
00243    res |= ast_unregister_application(app);
00244 
00245    ast_module_user_hangup_all();
00246 
00247    return res;
00248 }
00249 
00250 static int load_module(void)
00251 {
00252    int res;
00253 
00254    ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00255    res = ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
00256    res |= ast_register_application(app, realtime_exec, synopsis, desc);
00257 
00258    return res;
00259 }
00260 
00261 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");

Generated on Mon May 14 04:42:53 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1