From 4e6af293f9f5a0ecc8d1e62284b128e23d799970 Mon Sep 17 00:00:00 2001
From: Russell Bryant <russell@russellbryant.com>
Date: Tue, 14 Mar 2006 16:49:34 +0000
Subject: [PATCH] add an option to cdr.conf that enables ending CDRs before
 executing the "h" extension as opposed to afterwards (issue #6193)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@12896 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 cdr.c                      | 13 ++++++++-----
 configs/cdr.conf.sample    |  7 +++++++
 include/asterisk/options.h |  5 ++++-
 pbx.c                      |  2 ++
 4 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/cdr.c b/cdr.c
index ce908d82fd..58c5e02dba 100644
--- a/cdr.c
+++ b/cdr.c
@@ -657,6 +657,11 @@ void ast_cdr_end(struct ast_cdr *cdr)
 			ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
 		if (ast_tvzero(cdr->end))
 			cdr->end = ast_tvnow();
+		cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
+		if (!ast_tvzero(cdr->answer))
+			cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
+		else
+			cdr->billsec = 0;
 		cdr = cdr->next;
 	}
 }
@@ -804,11 +809,6 @@ static void post_cdr(struct ast_cdr *cdr)
 			ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
 		if (ast_tvzero(cdr->start))
 			ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
-		cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
-		if (!ast_tvzero(cdr->answer))
-			cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
-		else
-			cdr->billsec = 0;
 		ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
 		AST_LIST_LOCK(&be_list);
 		AST_LIST_TRAVERSE(&be_list, i, list) {
@@ -1121,6 +1121,7 @@ static int do_reload(void)
 	const char *batchsafeshutdown_value;
 	const char *size_value;
 	const char *time_value;
+	const char *end_before_h_value;
 	int cfg_size;
 	int cfg_time;
 	int was_enabled;
@@ -1171,6 +1172,8 @@ static int do_reload(void)
 			else
 				batchtime = cfg_time;
 		}
+		if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
+			ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_END_CDR_BEFORE_H_EXTEN);
 	}
 
 	if (enabled && !batchmode) {
diff --git a/configs/cdr.conf.sample b/configs/cdr.conf.sample
index 2d45e0d566..e8b3a6886c 100644
--- a/configs/cdr.conf.sample
+++ b/configs/cdr.conf.sample
@@ -49,6 +49,13 @@
 ; is "yes".
 ;safeshutdown=yes
 ;
+
+; Normally, CDR's are not closed out until after all extensions are finished
+; executing.  By enabling this option, the CDR will be ended before executing
+; the "h" extension so that CDR values such as "end" and "billsec" may be
+; retrieved inside of of this extension.
+;endbeforehexten=no
+
 ;[csv]
 ;usegmtime=yes ;log date/time in GMT
 ;loguniqueid=yes ;log uniqueid
diff --git a/include/asterisk/options.h b/include/asterisk/options.h
index 4c4031a5a2..73d98b2755 100644
--- a/include/asterisk/options.h
+++ b/include/asterisk/options.h
@@ -69,7 +69,9 @@ enum ast_option_flags {
 	/*! Transmit Silence during Record() */
 	AST_OPT_FLAG_TRANSMIT_SILENCE = (1 << 17),
 	/*! Suppress some warnings */
-	AST_OPT_FLAG_DONT_WARN = (1 << 18)
+	AST_OPT_FLAG_DONT_WARN = (1 << 18),
+	/*! End CDRs before the 'h' extension */
+	AST_OPT_END_CDR_BEFORE_H_EXTEN = (1 << 19)
 };
 
 #define ast_opt_exec_includes		ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES)
@@ -91,6 +93,7 @@ enum ast_option_flags {
 #define ast_opt_reconnect		ast_test_flag(&ast_options, AST_OPT_FLAG_RECONNECT)
 #define ast_opt_transmit_silence	ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE)
 #define ast_opt_dont_warn		ast_test_flag(&ast_options, AST_OPT_FLAG_DONT_WARN)
+#define ast_opt_end_cdr_before_h_exten	ast_test_flag(&ast_options, AST_OPT_END_CDR_BEFORE_H_EXTEN)
 
 extern struct ast_flags ast_options;
 
diff --git a/pbx.c b/pbx.c
index 458f9d707d..eca359cc74 100644
--- a/pbx.c
+++ b/pbx.c
@@ -2278,6 +2278,8 @@ static int __ast_pbx_run(struct ast_channel *c)
 		ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
 out:
 	if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->cid.cid_num)) {
+		if (c->cdr && ast_opt_end_cdr_before_h_exten)
+			ast_cdr_end(c->cdr);
 		c->exten[0] = 'h';
 		c->exten[1] = '\0';
 		c->priority = 1;
-- 
GitLab