diff -uNr asterisk-addons-1.2.1.orig/cdr_addon_mysql.c asterisk-addons-1.2.1/cdr_addon_mysql.c --- asterisk-addons-1.2.1.orig/cdr_addon_mysql.c 2005-12-02 01:34:41.000000000 +0100 +++ asterisk-addons-1.2.1/cdr_addon_mysql.c 2006-02-07 01:41:50.000000000 +0100 @@ -48,7 +48,7 @@ static char *name = "mysql"; static char *config = "cdr_mysql.conf"; static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL; -static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0; +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0, fields_alloc = 0, fieldsvar_alloc = 0; static int dbport = 0; static int connected = 0; static time_t connect_time = 0; @@ -56,6 +56,8 @@ static int totalrecords = 0; static int userfield = 0; static unsigned int timeout = 0; +static char *fields = NULL; +static char *fieldsvar = NULL; AST_MUTEX_DEFINE_STATIC(mysql_lock); @@ -117,6 +119,9 @@ char sqlcmd[2048], timestr[128]; char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL; int retries = 5; + char buf[2048]; + char fieldsbuf[2048]; + struct ast_channel dummy; #ifdef MYSQL_LOGUNIQUEID char *uniqueid = NULL; #endif @@ -192,6 +197,13 @@ if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)) mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield)); + memset(buf, 0 , sizeof(buf)); + /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */ + memset(&dummy, 0, sizeof(dummy)); + dummy.cdr = cdr; + pbx_substitute_variables_helper(&dummy, fieldsvar, buf, sizeof(buf) - 1); + sprintf(fieldsbuf,"%s",fields); + /* Check for all alloca failures above at once */ #ifdef MYSQL_LOGUNIQUEID if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) { @@ -207,15 +219,15 @@ if (userfield && userfielddata) { #ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s'%s)", dbtable, fieldsbuf, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata,buf); #else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s'%s)", dbtable, fieldsbuf, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata,buf); #endif } else { #ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s'%s)", dbtable, fieldsbuf, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid,buf); #else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s'%s)", dbtable, fieldsbuf, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode,buf); #endif } @@ -278,6 +290,18 @@ password = NULL; password_alloc = 0; } + + if (fields && fields_alloc) { + free(fields); + fields = NULL; + fields_alloc = 0; + } + + if (fieldsvar && fieldsvar_alloc) { + free(fieldsvar); + fieldsvar = NULL; + fieldsvar_alloc = 0; + } dbport = 0; ast_cdr_unregister(name); return 0; @@ -415,7 +439,37 @@ userfield = 0; } } - + + tmp = ast_variable_retrieve(cfg, "global", "fields"); + if (tmp) { + fields = malloc(strlen(tmp) + 1); + if (fields != NULL) { + fields_alloc = 1; + strcpy(fields, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_NOTICE, "Using no additional Fields\n"); + fields = ""; + } + + tmp = ast_variable_retrieve(cfg, "global", "fieldsvar"); + if (tmp) { + fieldsvar = malloc(strlen(tmp) + 1); + if (fieldsvar != NULL) { + fieldsvar_alloc = 1; + strcpy(fieldsvar, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_NOTICE, "Using no additional Fields\n"); + fieldsvar = ""; + } + ast_config_destroy(cfg); ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname); @@ -427,6 +481,9 @@ ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname); ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password); + ast_log(LOG_DEBUG, "cdr_mysql: got additional fields of \"%s\"\n", fields); + ast_log(LOG_DEBUG, "cdr_mysql: got additional fieldsvar of \"%s\"\n", fieldsvar); + mysql_init(&mysql); if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) { diff -uNr asterisk-addons-1.2.1.orig/res_config_mysql.c asterisk-addons-1.2.1/res_config_mysql.c --- asterisk-addons-1.2.1.orig/res_config_mysql.c 2005-11-29 19:51:27.000000000 +0100 +++ asterisk-addons-1.2.1/res_config_mysql.c 2006-02-07 01:25:38.000000000 +0100 @@ -62,6 +62,9 @@ static char dbpass[50]; static char dbname[50]; static char dbsock[50]; +static char fields[1048]; +static char fieldsvar[1048]; +static char dbsock[50]; static int dbport; static int connected; static time_t connect_time;