--- cdr_addon_mysql.c.orig	2007-12-17 15:12:07.000000000 -0500
+++ cdr_addon_mysql.c	2007-12-18 10:21:48.000000000 -0500
@@ -57,10 +57,7 @@
 static char *name = "mysql";
 static char *config = "cdr_mysql.conf";
 
-static struct ast_str *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL,
-			   *calldate_field = NULL, *clid_field = NULL, *src_field = NULL, *dst_field = NULL, *dcontext_field = NULL, *channel_field = NULL,
-			   *dstchannel_field = NULL, *lastapp_field = NULL, *lastdata_field = NULL, *duration_field = NULL, *billsec_field =NULL,
-			   *disposition_field = NULL, *amaflags_field = NULL, *accountcode_field = NULL, *userfield_field = NULL, *uniqueid_field = NULL;
+static struct ast_str *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL, *dispositions = NULL, *calldate_field = NULL, *clid_field = NULL, *src_field = NULL, *dst_field = NULL, *dcontext_field = NULL, *channel_field = NULL, *dstchannel_field = NULL, *lastapp_field = NULL, *lastdata_field = NULL, *duration_field = NULL, *billsec_field =NULL, *disposition_field = NULL, *amaflags_field = NULL, *accountcode_field = NULL, *userfield_field = NULL, *uniqueid_field = NULL;
 
 static struct ast_str *ssl_ca = NULL, *ssl_cert = NULL, *ssl_key = NULL;
 
@@ -69,10 +66,14 @@
 static time_t connect_time = 0;
 static int records = 0;
 static int totalrecords = 0;
+static int skipped = 0;
+static int totalskipped = 0;
 static int userfield = 0;
 static int timeout = 0;
 static int loguniqueid = 0;
 
+static unsigned short disposition_mask = AST_CDR_FAILED|AST_CDR_BUSY|AST_CDR_NOANSWER| AST_CDR_ANSWERED;
+
 AST_MUTEX_DEFINE_STATIC(mysql_lock);
 
 struct unload_string {
@@ -125,10 +126,17 @@
 		} else {
 			ast_cli(a->fd, "%s%s for %d seconds.\n", status, status2, ctime);
 		}
+		if (!ast_strlen_zero(dispositions->str)) {
+			ast_cli(a->fd, "Disposition mask: %s\n", dispositions->str);
+		}
 		if (records == totalrecords)
 			ast_cli(a->fd, "  Wrote %d records since last restart.\n", totalrecords);
 		else
 			ast_cli(a->fd, "  Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
+		if (skipped == totalskipped)
+			ast_cli(fd, "  Skipped %d records since last restart.\n", totalskipped);
+		else
+			ast_cli(fd, "  Skipped %d records since last restart and %d records since last reconnect.\n", totalskipped, skipped);
 	} else {
 		ast_cli(a->fd, "Not currently connected to a MySQL server.\n");
 	}
@@ -152,6 +160,14 @@
 	my_bool my_bool_true = 1;
 #endif
 
+        if (cdr->disposition & ~disposition_mask) {
+		ast_debug(1, "Ignoring CDR with disposition of '%s'\n",
+			ast_cdr_disp2str(cdr->disposition));
+		skipped++;
+		totalskipped++;
+		return 0;
+	}
+
 	ast_mutex_lock(&mysql_lock);
 
 	memset(sqlcmd, 0, 2048);
@@ -181,6 +197,7 @@
 			connected = 1;
 			connect_time = time(NULL);
 			records = 0;
+			skipped = 0;
 		} else {
 			ast_log(LOG_ERROR, "Cannot connect to database server %s: (%d) %s\n", hostname->str, mysql_errno(&mysql), mysql_error(&mysql));
 			connected = 0;
@@ -191,6 +208,7 @@
 		if ((error = mysql_ping(&mysql))) {
 			connected = 0;
 			records = 0;
+			skipped = 0;
 			switch (mysql_errno(&mysql)) {
 				case CR_SERVER_GONE_ERROR:
 				case CR_SERVER_LOST:
@@ -284,6 +302,7 @@
 		mysql_close(&mysql);
 		connected = 0;
 		records = 0;
+		skipped = 0;
 	}
 
 	AST_LIST_LOCK(&unload_strings);
@@ -354,7 +373,7 @@
 	struct ast_config *cfg;
 	struct ast_variable *var;
 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
-	char *temp;
+	char *temp, **stringp, *token;
 #if MYSQL_VERSION_ID >= 50013
 	my_bool my_bool_true = 1;
 #endif
@@ -393,7 +412,7 @@
 	res |= my_load_config_number(cfg, "global", "userfield", &userfield, 0);
 
 	res |= my_load_config_bool(cfg, "global", "loguniqueid", &loguniqueid, 0);
-
+	res |= my_load_config_string(cfg, "global", "dispositions", &dispositions, "");
 	res |= my_load_config_string(cfg, "global", "calldate_field", &calldate_field, "calldate");
 	res |= my_load_config_string(cfg, "global", "clid_field", &clid_field, "clid");	
 	res |= my_load_config_string(cfg, "global", "src_field", &src_field, "src");
@@ -425,6 +444,7 @@
 	ast_debug(1, "Got user of %s\n", dbuser->str);
 	ast_debug(1, "Got dbname of %s\n", dbname->str);
 	ast_debug(1, "Got password of %s\n", password->str);
+	ast_debug(1, "Got dispositions of %s\n", dispositions->str);
 
 	ast_debug(1, "Got calldate field of %s\n", calldate_field->str);
 	ast_debug(1, "Got clid field of %s\n", clid_field->str);
@@ -442,6 +462,32 @@
 	ast_debug(1, "Got userfield field of %s\n", userfield_field->str);
 	ast_debug(1, "Got uniqueid field of %s\n", uniqueid_field->str);
 
+	if (!ast_strlen_zero(dispositions->str)) {
+		stringp = &dispositions->str;
+		disposition_mask = 0;
+		while (token = strsep(stringp, "|,")) {
+			token = ast_strip(token);	
+			if (!strcasecmp(token, "FAILED")) {
+				disposition_mask |= AST_CDR_FAILED;
+			}
+			else if (!strcasecmp(token, "BUSY")) {
+				disposition_mask |= AST_CDR_BUSY;
+			}
+			else if (!strcasecmp(token, "NOANSWER")) {
+				disposition_mask |= AST_CDR_NOANSWER;
+			}
+			else if (!strcasecmp(token, "ANSWERED")) {
+				disposition_mask |= AST_CDR_ANSWERED;
+			}
+			else {
+				ast_debug(1, "cdr_mysql: Invalid disposition '%s' specified. Using default.\n", token);
+				disposition_mask = AST_CDR_FAILED|AST_CDR_BUSY|AST_CDR_NOANSWER|AST_CDR_ANSWERED;
+				ast_str_set(&dispositions->str, 0, "FAILED|BUSY|NOANSWER|ANSWERED");
+				break;
+			}
+		}
+	}
+	
 	mysql_init(&mysql);
 
 	if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
@@ -463,10 +509,12 @@
 		ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname->str, hostname->str);
 		connected = 0;
 		records = 0;
+		skipped = 0;
 	} else {
 		ast_debug(1, "Successfully connected to MySQL database.\n");
 		connected = 1;
 		records = 0;
+		skipped = 0;
 		connect_time = time(NULL);
 	}
 
