From 81c6e0bc108b8f4211ffade6de7a7448d1ba72f9 Mon Sep 17 00:00:00 2001
From: Victor Seva <linuxmaniac@torreviejawireless.org>
Date: Thu, 2 Jan 2014 20:39:43 +0100
Subject: [PATCH] acc: allow pseudo-variables as first parameter for functions.

---
 modules/acc/acc_api.h   |  1 +
 modules/acc/acc_logic.c | 52 +++++++++++++++++++++++++++++++++++++++++++++----
 modules/acc/acc_logic.h |  2 +-
 modules/acc/acc_mod.c   | 24 ++++++++++++++---------
 4 files changed, 65 insertions(+), 14 deletions(-)

diff --git a/modules/acc/acc_api.h b/modules/acc/acc_api.h
index aa51380..a4d8bc6 100644
--- a/modules/acc/acc_api.h
+++ b/modules/acc/acc_api.h
@@ -48,6 +48,7 @@ typedef struct acc_param {
 	int code;
 	str code_s;
 	str reason;
+	pv_elem_p  elem;
 } acc_param_t;
 
 /* various acc variables */
diff --git a/modules/acc/acc_logic.c b/modules/acc/acc_logic.c
index 18437fc..0641388 100644
--- a/modules/acc/acc_logic.c
+++ b/modules/acc/acc_logic.c
@@ -166,14 +166,49 @@ static inline int acc_preparse_req(struct sip_msg *req)
 	return 0;
 }
 
+int acc_parse_code(char *p, struct acc_param *param)
+{
+	if (p==NULL||param==NULL)
+		return -1;
 
+	/* any code? */
+	if (param->reason.len>=3 && isdigit((int)p[0])
+	&& isdigit((int)p[1]) && isdigit((int)p[2]) ) {
+		param->code = (p[0]-'0')*100 + (p[1]-'0')*10 + (p[2]-'0');
+		param->code_s.s = p;
+		param->code_s.len = 3;
+		param->reason.s += 3;
+		for( ; isspace((int)param->reason.s[0]) ; param->reason.s++ );
+		param->reason.len = strlen(param->reason.s);
+	}
+	return 0;
+}
+
+int acc_get_param_value(struct sip_msg *rq, struct acc_param *param)
+{
+	if(param->elem!=NULL) {
+		if(pv_printf_s(rq, param->elem, &param->reason)==-1) {
+			LM_ERR("Can't get value for %.*s\n", param->reason.len, param->reason.s);
+			return -1;
+		}
+		if(acc_parse_code(param->reason.s, param)<0)
+		{
+			LM_ERR("Can't parse code\n");
+			return -1;
+		}
+	}
+	return 0;
+}
 
 int w_acc_log_request(struct sip_msg *rq, char *comment, char *foo)
 {
+	struct acc_param *param = (struct acc_param*)comment;
 	if (acc_preparse_req(rq)<0)
 		return -1;
+	if(acc_get_param_value(rq, param)<0)
+		return -1;
 	env_set_to( rq->to );
-	env_set_comment((struct acc_param*)comment);
+	env_set_comment(param);
 	env_set_text( ACC_REQUEST, ACC_REQUEST_LEN);
 	return acc_log_request(rq);
 }
@@ -212,6 +247,7 @@ int acc_db_set_table_name(struct sip_msg *msg, void *param, str *table)
 
 int w_acc_db_request(struct sip_msg *rq, char *comment, char *table)
 {
+	struct acc_param *param = (struct acc_param*)comment;
 	if (!table) {
 		LM_ERR("db support not configured\n");
 		return -1;
@@ -222,8 +258,10 @@ int w_acc_db_request(struct sip_msg *rq, char *comment, char *table)
 		LM_ERR("cannot set table name\n");
 		return -1;
 	}
+	if(acc_get_param_value(rq, param)<0)
+		return -1;
 	env_set_to( rq->to );
-	env_set_comment((struct acc_param*)comment);
+	env_set_comment(param);
 	return acc_db_request(rq);
 }
 #endif
@@ -232,10 +270,13 @@ int w_acc_db_request(struct sip_msg *rq, char *comment, char *table)
 #ifdef RAD_ACC
 int w_acc_rad_request(struct sip_msg *rq, char *comment, char *foo)
 {
+	struct acc_param *param = (struct acc_param*)comment;
 	if (acc_preparse_req(rq)<0)
 		return -1;
+	if(acc_get_param_value(rq, param)<0)
+		return -1;
 	env_set_to( rq->to );
-	env_set_comment((struct acc_param*)comment);
+	env_set_comment(param);
 	return acc_rad_request(rq);
 }
 #endif
@@ -244,10 +285,13 @@ int w_acc_rad_request(struct sip_msg *rq, char *comment, char *foo)
 #ifdef DIAM_ACC
 int w_acc_diam_request(struct sip_msg *rq, char *comment, char *foo)
 {
+	struct acc_param *param = (struct acc_param*)comment;
 	if (acc_preparse_req(rq)<0)
 		return -1;
+	if(acc_get_param_value(rq, param)<0)
+		return -1;
 	env_set_to( rq->to );
-	env_set_comment((struct acc_param*)comment);
+	env_set_comment(param);
 	return acc_diam_request(rq);
 }
 #endif
diff --git a/modules/acc/acc_logic.h b/modules/acc/acc_logic.h
index 7348471..29ee8f7 100644
--- a/modules/acc/acc_logic.h
+++ b/modules/acc/acc_logic.h
@@ -41,7 +41,7 @@
 #include "../../modules/tm/t_hooks.h"
 #include "acc_api.h"
 
-
+int acc_parse_code(char *p, struct acc_param *param);
 void acc_onreq( struct cell* t, int type, struct tmcb_params *ps );
 
 int w_acc_log_request(struct sip_msg *rq, char *comment, char *foo);
diff --git a/modules/acc/acc_mod.c b/modules/acc/acc_mod.c
index 2fb9258..8be152a 100644
--- a/modules/acc/acc_mod.c
+++ b/modules/acc/acc_mod.c
@@ -355,15 +355,21 @@ static int acc_fixup(void** param, int param_no)
 		memset( accp, 0, sizeof(struct acc_param));
 		accp->reason.s = p;
 		accp->reason.len = strlen(p);
-		/* any code? */
-		if (accp->reason.len>=3 && isdigit((int)p[0])
-		&& isdigit((int)p[1]) && isdigit((int)p[2]) ) {
-			accp->code = (p[0]-'0')*100 + (p[1]-'0')*10 + (p[2]-'0');
-			accp->code_s.s = p;
-			accp->code_s.len = 3;
-			accp->reason.s += 3;
-			for( ; isspace((int)accp->reason.s[0]) ; accp->reason.s++ );
-			accp->reason.len = strlen(accp->reason.s);
+		if (strchr(p,PV_MARKER)!=NULL) { /* is a variable $xxxxx */
+			if (pv_parse_format(&accp->reason, &accp->elem)<0)
+			{
+				LM_ERR("bad param 1; "
+					"parse format error [%.*s]\n", accp->reason.len, accp->reason.s);
+				return E_UNSPEC;
+			}
+		}
+		else {
+			if(acc_parse_code(p,accp)<0)
+			{
+				LM_ERR("bad param 1;"
+					"parse code error\n");
+				return E_UNSPEC;
+			}
 		}
 		*param = (void*)accp;
 #ifdef SQL_ACC
-- 
1.8.5.2

