diff --git a/apps/app_forkcdr.c b/apps/app_forkcdr.c index c74de67b7381db489803ccbb50b52e76eed7d534..8d01a829c4d7740d111054bcea9ea031738f09e3 100644 --- a/apps/app_forkcdr.c +++ b/apps/app_forkcdr.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/cdr.h" +#include "asterisk/app.h" #include "asterisk/module.h" static char *app = "ForkCDR"; @@ -43,10 +44,51 @@ static char *descrip = " ForkCDR([options]): Causes the Call Data Record to fork an additional\n" "cdr record starting from the time of the fork call\n" " Options:\n" -" v - If the option is passed all cdr variables will be passed along also.\n"; - - -static void ast_cdr_fork(struct ast_channel *chan) +" a - update the answer time on the NEW CDR just after it's been inited..\n" +" The new CDR may have been answered already, the reset that forkcdr.\n" +" does will erase the answer time. This will bring it back, but.\n" +" the answer time will be a copy of the fork/start time. It will.\n" +" only do this if the initial cdr was indeed already answered..\n" +" D - Copy the disposition forward from the old cdr, after the .\n" +" init..\n" +" d - Clear the dstchannel on the new CDR after reset..\n" +" e - end the original CDR. Do this after all the necc. data.\n" +" is copied from the original CDR to the new forked CDR..\n" +" R - do NOT reset the new cdr..\n" +" s(name=val) - Set the CDR var 'name' in the original CDR, with value.\n" +" 'val'.\n" +" v - When the new CDR is forked, it gets a copy of the vars attached\n" +" to the current CDR. The vars attached to the original CDR are removed\n" +" unless this option is specified.\n"; + + +enum { + OPT_SETANS = (1 << 0), + OPT_SETDISP = (1 << 1), + OPT_RESETDEST = (1 << 2), + OPT_ENDCDR = (1 << 3), + OPT_NORESET = (1 << 4), + OPT_KEEPVARS = (1 << 5), + OPT_VARSET = (1 << 6), +}; + +enum { + OPT_ARG_VARSET = 0, + /* note: this entry _MUST_ be the last one in the enum */ + OPT_ARG_ARRAY_SIZE, +}; + +AST_APP_OPTIONS(forkcdr_exec_options, { + AST_APP_OPTION('a', OPT_SETANS), + AST_APP_OPTION('d', OPT_SETDISP), + AST_APP_OPTION('D', OPT_RESETDEST), + AST_APP_OPTION('e', OPT_ENDCDR), + AST_APP_OPTION('R', OPT_NORESET), + AST_APP_OPTION_ARG('s', OPT_VARSET, OPT_ARG_VARSET), + AST_APP_OPTION('v', OPT_KEEPVARS), +}); + +static void ast_cdr_fork(struct ast_channel *chan, struct ast_flags optflags, char *set) { struct ast_cdr *cdr; struct ast_cdr *newcdr; @@ -61,27 +103,66 @@ static void ast_cdr_fork(struct ast_channel *chan) return; ast_cdr_append(cdr, newcdr); - ast_cdr_reset(newcdr, &flags); - + + if (!ast_test_flag(&optflags, OPT_NORESET)) + ast_cdr_reset(newcdr, &flags); + if (!ast_test_flag(cdr, AST_CDR_FLAG_KEEP_VARS)) ast_cdr_free_vars(cdr, 0); + if (!ast_strlen_zero(set)) { + char *varname = ast_strdupa(set), *varval; + varval = strchr(varname,'='); + if (varval) { + *varval = 0; + varval++; + ast_cdr_setvar(cdr, varname, varval, 0); + } + } + + if (ast_test_flag(&optflags, OPT_SETANS) && !ast_tvzero(cdr->answer)) + newcdr->answer = newcdr->start; + + if (ast_test_flag(&optflags, OPT_SETDISP)) + newcdr->disposition = cdr->disposition; + + if (ast_test_flag(&optflags, OPT_RESETDEST)) + newcdr->dstchannel[0] = 0; + + if (ast_test_flag(&optflags, OPT_ENDCDR)) + ast_cdr_end(cdr); + ast_set_flag(cdr, AST_CDR_FLAG_CHILD | AST_CDR_FLAG_LOCKED); } static int forkcdr_exec(struct ast_channel *chan, void *data) { int res = 0; + char *argcopy = NULL; + struct ast_flags flags = {0}; + char *opts[OPT_ARG_ARRAY_SIZE]; + AST_DECLARE_APP_ARGS(arglist, + AST_APP_ARG(options); + ); if (!chan->cdr) { ast_log(LOG_WARNING, "Channel does not have a CDR\n"); return 0; } + argcopy = ast_strdupa(data); + + AST_STANDARD_APP_ARGS(arglist, argcopy); + + if (!ast_strlen_zero(arglist.options)) { + ast_app_parse_options(forkcdr_exec_options, &flags, opts, arglist.options); + } else + opts[OPT_ARG_VARSET] = 0; + if (!ast_strlen_zero(data)) - ast_set2_flag(chan->cdr, strchr(data, 'v'), AST_CDR_FLAG_KEEP_VARS); + ast_set2_flag(chan->cdr, ast_test_flag(&flags, OPT_KEEPVARS), AST_CDR_FLAG_KEEP_VARS); - ast_cdr_fork(chan); + ast_cdr_fork(chan, flags, opts[OPT_ARG_VARSET]); return res; } diff --git a/main/cdr.c b/main/cdr.c index 3c45baf85f770a3239da72d6353bfc927b9a210c..0a91f74fd5e0b892d5c336f82d77d1ff94e45615 100644 --- a/main/cdr.c +++ b/main/cdr.c @@ -306,23 +306,21 @@ int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int } for (; cdr; cdr = recur ? cdr->next : NULL) { - if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) { - headp = &cdr->varshead; - AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) { - if (!strcasecmp(ast_var_name(newvariable), name)) { - /* there is already such a variable, delete it */ - AST_LIST_REMOVE_CURRENT(entries); - ast_var_delete(newvariable); - break; - } - } - AST_LIST_TRAVERSE_SAFE_END; - - if (value) { - newvariable = ast_var_assign(name, value); - AST_LIST_INSERT_HEAD(headp, newvariable, entries); + headp = &cdr->varshead; + AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) { + if (!strcasecmp(ast_var_name(newvariable), name)) { + /* there is already such a variable, delete it */ + AST_LIST_REMOVE_CURRENT(entries); + ast_var_delete(newvariable); + break; } } + AST_LIST_TRAVERSE_SAFE_END; + + if (value) { + newvariable = ast_var_assign(name, value); + AST_LIST_INSERT_HEAD(headp, newvariable, entries); + } } return 0; @@ -383,6 +381,7 @@ int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char } for (i = 0; cdr_readonly_vars[i]; i++) { + workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */ ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0); if (!tmp) continue; @@ -690,13 +689,11 @@ void ast_cdr_answer(struct ast_cdr *cdr) { for (; cdr; cdr = cdr->next) { - if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) { - check_post(cdr); - if (cdr->disposition < AST_CDR_ANSWERED) - cdr->disposition = AST_CDR_ANSWERED; - if (ast_tvzero(cdr->answer)) - cdr->answer = ast_tvnow(); - } + check_post(cdr); + if (cdr->disposition < AST_CDR_ANSWERED) + cdr->disposition = AST_CDR_ANSWERED; + if (ast_tvzero(cdr->answer)) + cdr->answer = ast_tvnow(); } } @@ -835,20 +832,30 @@ int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c) return 0; } +/* Three routines were "fixed" via 10668, and later shown that + users were depending on this behavior. ast_cdr_end, + ast_cdr_setvar and ast_cdr_answer are the three routines. + While most of the other routines would not touch + LOCKED cdr's, these three routines were designed to + operate on locked CDR's as a matter of course. + I now appreciate how this plays with the ForkCDR app, + which forms these cdr chains in the first place. + cdr_end is pretty key: all cdrs created are closed + together. They only vary by start time. Arithmetically, + users can calculate the subintervals they wish to track. */ + void ast_cdr_end(struct ast_cdr *cdr) { for ( ; cdr ; cdr = cdr->next) { - if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) { - check_post(cdr); - if (ast_tvzero(cdr->end)) - cdr->end = ast_tvnow(); - if (ast_tvzero(cdr->start)) { - ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>")); - cdr->disposition = AST_CDR_FAILED; - } else - cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec; - cdr->billsec = ast_tvzero(cdr->answer) ? 0 : cdr->end.tv_sec - cdr->answer.tv_sec; - } + check_post(cdr); + if (ast_tvzero(cdr->end)) + cdr->end = ast_tvnow(); + if (ast_tvzero(cdr->start)) { + ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>")); + cdr->disposition = AST_CDR_FAILED; + } else + cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec; + cdr->billsec = ast_tvzero(cdr->answer) ? 0 : cdr->end.tv_sec - cdr->answer.tv_sec; } }