--- asterisk-addons-1.2.8/cdr_addon_mysql.c 2007-12-20 16:19:21.000000000 -0500 +++ asterisk-addons-1.2.8.new/cdr_addon_mysql.c 2007-12-21 13:27:24.000000000 -0500 @@ -47,8 +47,8 @@ static char *desc = "MySQL CDR Backend"; 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 char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL , *additionalfields = NULL; +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0, additionalfields_alloc = 0; static int dbport = 0; static int connected = 0; static time_t connect_time = 0; @@ -113,8 +113,11 @@ struct tm tm; struct timeval tv; struct localuser *u; + struct ast_var_t *variables; + struct varshead *headpa; char *userfielddata = NULL; - char sqlcmd[2048], timestr[128]; + char sqlcmd[2048], sqlcmd2[2048], timestr[128], temp[1024]; + char *var, *val, *allowedfield; char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL, *src=NULL, *dst=NULL, *accountcode=NULL; int retries = 5; #ifdef MYSQL_LOGUNIQUEID @@ -124,6 +127,8 @@ ast_mutex_lock(&mysql_lock); memset(sqlcmd, 0, 2048); + memset(sqlcmd2, 0, 2048); + memset(temp, 0, 1024); localtime_r(&cdr->start.tv_sec, &tm); strftime(timestr, 128, DATE_FORMAT, &tm); @@ -211,19 +216,41 @@ ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n"); + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode", dbtable); + sprintf(sqlcmd2, ") VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s'", timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode); + if (additionalfields) { + headpa = &cdr->varshead; + + AST_LIST_TRAVERSE(&cdr->varshead,variables,entries) { + if (variables && (var = ast_var_name(variables)) && (val = ast_var_value(variables)) && !ast_strlen_zero(var)) { + char fields[1024]; + strcpy(fields,additionalfields); + allowedfield=strtok(fields,"|"); + while (allowedfield) { + if (!strcmp(allowedfield,var)) { + sprintf(temp, ",`%s`", var); + strcat(sqlcmd,temp); + sprintf(temp, ",'%s'",val); + strcat(sqlcmd2,temp); + break; + } + allowedfield = strtok(NULL,"|"); + } + } + } + } if (userfield && userfielddata) { + strcat(sqlcmd,",userfield"); + sprintf(temp,",'%s'",userfielddata); + strcat(sqlcmd2,temp); + } #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, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid, userfielddata); -#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, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, userfielddata); -#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, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid); -#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, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode); + strcat(sqlcmd,",uniqueid"); + sprintf(temp,",'%s'",uniqueid); + strcat(sqlcmd2,temp); #endif - } + strcat(sqlcmd,sqlcmd2); + strcat(sqlcmd,")"); ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd); @@ -422,6 +450,21 @@ } } + tmp = ast_variable_retrieve(cfg, "global", "additionalfields"); + if (tmp) { + additionalfields = malloc(strlen(tmp) + 1); + if (additionalfields != NULL) { + additionalfields_alloc = 1; + strcpy(additionalfields, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "Additional fields not specified. Assuming none\n"); + additionalfields = NULL; + } + ast_config_destroy(cfg); ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);