--- /usr/src/asterisk-addons/cdr_addon_mysql.c	2004-07-09 12:24:28.000000000 -0400
+++ cdr_mysql.c	2004-11-27 03:07:19.680208208 -0500
@@ -5,6 +5,9 @@
  * 
  * James Sharp <jsharp@psychoses.org>
  *
+ * Modified November 2004
+ * Richard Cook <richard@aspworld.com>
+ *
  * Modified August 2003
  * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
  *
@@ -30,22 +33,23 @@
 #include <unistd.h>
 #include <time.h>
 
-#include <mysql.h>
 #include <errmsg.h>
+#include <mysql/mysql.h>
 
+#define SQL_COMMAND 2048
 #define DATE_FORMAT "%Y-%m-%d %T"
 
 static char *desc = "MySQL CDR Backend";
-static char *name = "mysql";
+static char *synopsis = "MySQL CDR Backend";
+static char *app = "MySQLCDR";
 static char *config = "cdr_mysql.conf";
-static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL;
+static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *loguniqueid = NULL;
+static char *table= NULL;
 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0;
-static int dbport = 0;
-static int connected = 0;
-static time_t connect_time = 0;
+static int dbport = 0, loguniqueid_alloc = 0, table_alloc = 0, connected = 0;
 static int records = 0;
 static int totalrecords = 0;
-static int userfield = 0;
+static time_t connect_time = 0;
 
 AST_MUTEX_DEFINE_STATIC(mysql_lock);
 
@@ -55,204 +59,198 @@
 "Usage: cdr mysql status\n"
 "       Shows current connection status for cdr_mysql\n";
 
-static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
+static int db_check_connected(void)
 {
 	if (connected) {
-		char status[256], status2[100] = "";
-		int ctime = time(NULL) - connect_time;
-		if (dbport)
-			snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
-		else if (dbsock)
-			snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
-		else
-			snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
+		/* seems that we are connected - but let us check that */
+		if (mysql_ping(&mysql)==0) {
+			connected = 1;
+		}
+		else {
+			ast_log(LOG_ERROR, "MySQL CDR: FATAL - Database connection lost '%s'. \n", hostname);
+			connected = 0;
+		}
+	}
+	else {
+		/* we are not connected - so make the initial connection */
+		mysql_init(&mysql);
 
-		if (dbuser && *dbuser)
-			snprintf(status2, 99, " with username %s", dbuser);
-		if (ctime > 31536000) {
-			ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
-		} else if (ctime > 86400) {
-			ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
-		} else if (ctime > 3600) {
-			ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
-		} else if (ctime > 60) {
-			ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
-		} else {
-			ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
+		mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, NULL, 0);
+		if (!&mysql) {
+			ast_log(LOG_ERROR, "MySQL CDR: FATAL - Couldn't connect to database server '%s'. \n", hostname);
+			connected = 0;
+		}
+		else {
+			ast_log(LOG_DEBUG, "MySQL CDR: Successfully connected to database '%s'.\n", dbname);
+			connected = 1;
 		}
-		if (records == totalrecords)
-			ast_cli(fd, "  Wrote %d records since last restart.\n", totalrecords);
-		else
-			ast_cli(fd, "  Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
-		return RESULT_SUCCESS;
-	} else {
-		ast_cli(fd, "Not currently connected to a MySQL server.\n");
-		return RESULT_FAILURE;
 	}
-}
 
-static struct ast_cli_entry cdr_mysql_status_cli =
-	{ { "cdr", "mysql", "status", NULL },
-	handle_cdr_mysql_status, "Show connection status of cdr_mysql",
-	cdr_mysql_status_help, NULL };
+	return connected;
+}
 
 static int mysql_log(struct ast_cdr *cdr)
 {
+	char SQLcmd[SQL_COMMAND] = "";
+	char timestr[128];
+	int res = 0;
 	struct tm tm;
-	struct timeval tv;
-	struct localuser *u;
-	char *userfielddata = NULL;
-	char sqlcmd[2048], timestr[128];
-
-	ast_mutex_lock(&mysql_lock);
-
-	memset(sqlcmd,0,2048);
 
+	/* build the time */
 	localtime_r(&cdr->start.tv_sec,&tm);
-	strftime(timestr,128,DATE_FORMAT,&tm);
+	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 
-	if ((!connected) && (hostname || dbsock) && dbuser && password && dbname) {
-		/* Attempt to connect */
-		mysql_init(&mysql);
-		if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
-			connected = 1;
-			connect_time = time(NULL);
-			records = 0;
-		} else {
-			ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.  Call will not be logged\n", hostname);
-		}
-	} else {
-		/* Long connection - ping the server */
-		int error;
-		if ((error = mysql_ping(&mysql))) {
-			connected = 0;
-			records = 0;
-			switch (error) {
-				case CR_SERVER_GONE_ERROR:
-					ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n");
-					break;
-				default:
-					ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error\n");
-			}
-		}
-	}
+	/* set up the SQL */
+	ast_mutex_lock(&mysql_lock);
 
-	if (connected) {
+	/* check the database connection */
+	if (db_check_connected()) {
 		char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
-#ifdef MYSQL_LOGUNIQUEID
-		char *uniqueid=NULL;
-#endif
+		char *uniqueid=NULL, *userfield=NULL;
 
-		/* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
+		/* maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
 		if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, clid, cdr->clid, strlen(cdr->clid));
+
 		if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, dcontext, cdr->dcontext, strlen(cdr->dcontext));
+
 		if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, channel, cdr->channel, strlen(cdr->channel));
+
 		if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
+
 		if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, lastapp, cdr->lastapp, strlen(cdr->lastapp));
+
 		if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
 			mysql_real_escape_string(&mysql, lastdata, cdr->lastdata, strlen(cdr->lastdata));
-#ifdef MYSQL_LOGUNIQUEID
-		if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
-			mysql_real_escape_string(&mysql, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
-#endif
-
-		if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
-			mysql_real_escape_string(&mysql, userfielddata, cdr->userfield, strlen(cdr->userfield));		
-
-		/* Check for all alloca failures above at once */
-#ifdef MYSQL_LOGUNIQUEID
-		if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
-#else
-		if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
-#endif
-			ast_log(LOG_ERROR, "cdr_mysql:  Out of memory error (insert fails)\n");
-			ast_mutex_unlock(&mysql_lock);
-			return -1;
+
+		/* check for the loguniqueid flag and escape as necessary */
+		if((loguniqueid != NULL) && (ast_true(loguniqueid)))	{
+			if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
+				mysql_real_escape_string(&mysql, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
+			
+			if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
+				mysql_real_escape_string(&mysql, userfield, cdr->userfield, strlen(cdr->userfield));
+		}
+
+		/* check for all alloca failures above at once */
+		if ((loguniqueid != NULL) && (ast_true(loguniqueid)))	{
+			if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
+				ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.  Calls will not be logged!\n");
+				
+				ast_mutex_unlock(&mysql_lock);
+
+				return -1;
+			}
+		}
+		else {
+			if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
+				ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.  Calls will not be logged!\n");
+				
+				ast_mutex_unlock(&mysql_lock);
+
+				return -1;
+			}
 		}
 
-		ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n");
+		/* set up memory */
+		memset(SQLcmd, 0, SQL_COMMAND);
 
-		if (userfield && userfielddata)
-		{
-#ifdef MYSQL_LOGUNIQUEID
-			sprintf(sqlcmd,"INSERT INTO cdr (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')",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);
-#else
-			sprintf(sqlcmd,"INSERT INTO cdr (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')",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);
-#endif  
+		/* build the query */
+		if((loguniqueid != NULL) && (ast_true(loguniqueid)))	{
+			sprintf(SQLcmd,"INSERT INTO %s (uniqueid,userfield,accountcode,src,dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,calldate,duration,billsec,disposition,amaflags) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%i,%i,'%i',%i)",table,uniqueid,userfield,cdr->accountcode,cdr->src,cdr->dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,timestr,cdr->duration,cdr->billsec,cdr->disposition,cdr->amaflags);
 		}
-		else
-		{
-#ifdef MYSQL_LOGUNIQUEID
-			sprintf(sqlcmd,"INSERT INTO cdr (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')",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);
-#else
-			sprintf(sqlcmd,"INSERT INTO cdr (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')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode);
-#endif  
-		}
-		
-		ast_log(LOG_DEBUG,"cdr_mysql: SQL command as follows:  %s\n",sqlcmd);
-	
-		if (mysql_real_query(&mysql,sqlcmd,strlen(sqlcmd))) {
-			ast_log(LOG_ERROR,"Failed to insert into database.");
-			ast_mutex_unlock(&mysql_lock);
-			return -1;
-		} else {
-			records++;
-			totalrecords++;
+		else {
+			sprintf(SQLcmd,"INSERT INTO %s (accountcode,src,dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,calldate,duration,billsec,disposition,amaflags) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%i,%i,'%i',%i)",table,cdr->accountcode,cdr->src,cdr->dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,timestr,cdr->duration,cdr->billsec,cdr->disposition,cdr->amaflags);
+		}
+
+		/* attempt to insert a fresh CDR record */
+		ast_log(LOG_DEBUG,"MySQL CDR: Attempting to insert a CDR record: %s\n", SQLcmd);
+
+		if (mysql_real_query(&mysql, SQLcmd, strlen(SQLcmd))) {
+			ast_log(LOG_ERROR, "MySQL CDR: Failed to insert into database, call was not logged!\n");
 		}
 	}
+
 	ast_mutex_unlock(&mysql_lock);
-	return 0;
-}
 
-char *description(void)
-{
-	return desc;
+	return 0;
 }
-
-static int my_unload_module(void)
+static int mysql_unload_module(void)
 { 
-	ast_cli_unregister(&cdr_mysql_status_cli);
+	/* close up the db connection */
 	if (connected) {
+		ast_log(LOG_NOTICE, "MySQL CDR: Disconnecting from %s\n", dbname);
 		mysql_close(&mysql);
 		connected = 0;
-		records = 0;
 	}
+
+	/* free up the hostname */
 	if (hostname && hostname_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free hostname\n");
 		free(hostname);
 		hostname = NULL;
 		hostname_alloc = 0;
 	}
+
+	/* free up the database name */
 	if (dbname && dbname_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free dbname\n");
 		free(dbname);
 		dbname = NULL;
 		dbname_alloc = 0;
 	}
+
+	/* free up the database user */
 	if (dbuser && dbuser_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free dbuser\n");
 		free(dbuser);
 		dbuser = NULL;
 		dbuser_alloc = 0;
 	}
+
+	/* free up the database sock */
 	if (dbsock && dbsock_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free dbsock\n");
 		free(dbsock);
 		dbsock = NULL;
 		dbsock_alloc = 0;
 	}
+
+	/* free up the database password */
 	if (password && password_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free password\n");
 		free(password);
 		password = NULL;
 		password_alloc = 0;
 	}
-	dbport = 0;
-	ast_cdr_unregister(name);
+
+	/* free up the loguniqueid flag */
+	if (loguniqueid && loguniqueid_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free loguniqueid\n");
+		free(loguniqueid);
+		loguniqueid  = NULL;
+		loguniqueid_alloc = 0;
+	}
+
+	/* free up the table name */
+	if (table && table_alloc) {
+		ast_log(LOG_DEBUG, "MySQL CDR: free table name\n");
+		free(table);
+		table = NULL;
+		table_alloc = 0;
+	}
+
+	/* unregister */
+	ast_cdr_unregister(app);
+
 	return 0;
 }
 
-static int my_load_module(void)
+static int mysql_load_module(void)
 {
 	int res;
 	struct ast_config *cfg;
@@ -261,135 +259,162 @@
 
 	cfg = ast_load(config);
 	if (!cfg) {
-		ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
+		ast_log(LOG_WARNING, "MySQL CDR: Unable to load config for MySQL CDR: %s\n", config);
 		return 0;
 	}
-	
+
 	var = ast_variable_browse(cfg, "global");
 	if (!var) {
-		/* nothing configured */
+		/* nothing configured - not used */
 		return 0;
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","hostname");
+	/* get the hostname */
+	tmp = ast_variable_retrieve(cfg, "global", "hostname");
 	if (tmp) {
 		hostname = malloc(strlen(tmp) + 1);
 		if (hostname != NULL) {
 			hostname_alloc = 1;
-			strcpy(hostname,tmp);
+			strcpy(hostname, tmp);
 		} else {
-			ast_log(LOG_ERROR,"Out of memory error.\n");
+			ast_log(LOG_ERROR, "Out of memory error.\n");
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING,"MySQL server hostname not specified.  Assuming localhost\n");
+		ast_log(LOG_WARNING, "MySQL CDR: Database hostname not specified. Assuming 'localhost'\n");
 		hostname = "localhost";
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","dbname");
+	/* get the database name */
+	tmp = ast_variable_retrieve(cfg, "global", "dbname");
 	if (tmp) {
 		dbname = malloc(strlen(tmp) + 1);
 		if (dbname != NULL) {
 			dbname_alloc = 1;
-			strcpy(dbname,tmp);
+			strcpy(dbname, tmp);
 		} else {
-			ast_log(LOG_ERROR,"Out of memory error.\n");
+			ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.\n");
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING,"MySQL database not specified.  Assuming asteriskcdrdb\n");
-		dbname = "asteriskcdrdb";
+		ast_log(LOG_WARNING, "MySQL CDR: Database name not specified. Assuming 'asteriskdb'\n");
+		dbname = "asteriskdb";
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","user");
+	/* get the database user */
+	tmp = ast_variable_retrieve(cfg, "global", "user");
 	if (tmp) {
 		dbuser = malloc(strlen(tmp) + 1);
 		if (dbuser != NULL) {
 			dbuser_alloc = 1;
-			strcpy(dbuser,tmp);
+			strcpy(dbuser, tmp);
 		} else {
-			ast_log(LOG_ERROR,"Out of memory error.\n");
+			ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.\n");
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING,"MySQL database user not specified.  Assuming root\n");
+		ast_log(LOG_WARNING, "MySQL CDR: Database user not specified.  Assuming 'root'\n");
 		dbuser = "root";
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","sock");
+	/* get the database password */
+	tmp = ast_variable_retrieve(cfg, "global", "password");
 	if (tmp) {
-		dbsock = malloc(strlen(tmp) + 1);
-		if (dbsock != NULL) {
-			dbsock_alloc = 1;
-			strcpy(dbsock,tmp);
+		password = malloc(strlen(tmp) + 1);
+		if (password != NULL) {
+			password_alloc = 1;
+			strcpy(password, tmp);
 		} else {
-			ast_log(LOG_ERROR,"Out of memory error.\n");
+			ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.\n");
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING,"MySQL database sock file not specified.  Using default\n");
-		dbsock = NULL;
+		ast_log(LOG_WARNING, "MySQL CDR: Database password not specified.  Assuming blank\n");
+		password = "";
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","password");
+	/* get the database sock */
+	tmp = ast_variable_retrieve(cfg, "global", "sock");
 	if (tmp) {
-		password = malloc(strlen(tmp) + 1);
-		if (password != NULL) {
-			password_alloc = 1;
-			strcpy(password,tmp);
+		dbsock = malloc(strlen(tmp) + 1);
+		if (dbsock != NULL) {
+			dbsock_alloc = 1;
+			strcpy(dbsock, tmp);
 		} else {
-			ast_log(LOG_ERROR,"Out of memory error.\n");
+			ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.\n");
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING,"MySQL database password not specified.  Assuming blank\n");
-		password = "";
+		ast_log(LOG_WARNING, "MySQL CDR: Database sock file not specified.  Using default\n");
+		dbsock = NULL;
 	}
 
-	tmp = ast_variable_retrieve(cfg,"global","port");
+	/* get the database port */
+	tmp = ast_variable_retrieve(cfg, "global", "port");
 	if (tmp) {
-		if (sscanf(tmp,"%d",&dbport) < 1) {
-			ast_log(LOG_WARNING,"Invalid MySQL port number.  Using default\n");
+		if (sscanf(tmp, "%d", &dbport) < 1) {
+			ast_log(LOG_WARNING, "MySQL CDR: Invalid MySQL port number.  Using default\n");
 			dbport = 0;
 		}
 	}
-	
-	tmp = ast_variable_retrieve(cfg,"global","userfield");
+
+	/* get the uniqueid flag */
+	tmp = ast_variable_retrieve(cfg, "global", "loguniqueid");
+	if (tmp)
+	{
+		loguniqueid = malloc(strlen(tmp) + 1);
+		if (loguniqueid != NULL)
+		{
+			strcpy(loguniqueid, tmp);
+			loguniqueid_alloc = 1;
+			ast_log(LOG_NOTICE, "MySQL CDR: Logging UniqueID\n");
+		}
+		else
+		{
+			ast_log(LOG_ERROR, "MySQL CDR: Not logging UniqueID\n");
+			loguniqueid_alloc = 1;
+			loguniqueid = NULL;
+		}
+	}
+	else
+	{
+		ast_log(LOG_WARNING, "MySQL CDR: Not logging UniqueID\n");
+		loguniqueid = NULL;
+	}
+
+	/* get the table name */
+	tmp = ast_variable_retrieve(cfg, "global", "table");
 	if (tmp) {
-		if (sscanf(tmp,"%d",&userfield) < 1) {
-			ast_log(LOG_WARNING,"Invalid MySQL configurtation file\n");
-			userfield = 0;
+		table = malloc(strlen(tmp) + 1);
+		if (table != NULL) {
+			table_alloc = 1;
+			strcpy(table, tmp);
+		} else {
+			ast_log(LOG_ERROR, "MySQL CDR: Out of memory error.\n");
+			return -1;
 		}
+	} else {
+		ast_log(LOG_WARNING, "MySQL CDR: Table not specified.  Assuming 'cdr'\n");
+		table = "cdr";
 	}
-	
+
+	/* clean up */
 	ast_destroy(cfg);
 
-	ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
-	ast_log(LOG_DEBUG,"cdr_mysql: got port of %d\n",dbport);
-	if (dbsock)
-		ast_log(LOG_DEBUG,"cdr_mysql: got sock file of %s\n",dbsock);
-	ast_log(LOG_DEBUG,"cdr_mysql: got user of %s\n",dbuser);
-	ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
-	ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);
+	/* verbose the module configuration */
+	ast_log(LOG_NOTICE, "MySQL CDR: Database name is %s\n", dbname);
+	ast_log(LOG_NOTICE, "MySQL CDR: Database username is %s\n", dbuser);
+	ast_log(LOG_NOTICE, "MySQL CDR: Database password is [secret]\n");
+	ast_log(LOG_NOTICE, "MySQL CDR: Database hostname is %s\n", hostname);
+	ast_log(LOG_NOTICE, "MySQL CDR: Table name is %s\n", table);
 
-	mysql_init(&mysql);
+	/* Check DB connection (it will automatically set up the connection) */
+	db_check_connected();
 
-	if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
-		ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
-		connected = 0;
-		records = 0;
-	} else {
-		ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n");
-		connected = 1;
-		records = 0;
-		connect_time = time(NULL);
-	}
-
-	res = ast_cdr_register(name, desc, mysql_log);
+	/* register the application with Asterisk */
+	res = ast_cdr_register(app, desc, mysql_log);
 	if (res) {
-		ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
-	} else {
-		res = ast_cli_register(&cdr_mysql_status_cli);
+		ast_log(LOG_ERROR, "MySQL CDR: Unable to register MySQL CDR handling\n");
 	}
 
 	return res;
@@ -397,18 +422,20 @@
 
 int load_module(void)
 {
-	return my_load_module();
+	return mysql_load_module();
 }
-
 int unload_module(void)
 {
-	return my_unload_module();
+	return mysql_unload_module();
 }
-
 int reload(void)
 {
-	my_unload_module();
-	return my_load_module();
+	mysql_unload_module();
+	return mysql_load_module();
+}
+char *description(void)
+{
+	return desc;
 }
 
 int usecount(void)
@@ -422,7 +449,8 @@
 	}
 }
 
-char *key()
+char *key(void)
 {
 	return ASTERISK_GPL_KEY;
 }
+
