فهرست منبع

modules/app_lua: added param to sr.xavp.get to choose between all the values (default) or just the first ones.

Victor Seva 12 سال پیش
والد
کامیت
8cd7a48479
1فایلهای تغییر یافته به همراه81 افزوده شده و 7 حذف شده
  1. 81 7
      modules/app_lua/app_lua_sr.c

+ 81 - 7
modules/app_lua/app_lua_sr.c

@@ -1237,6 +1237,57 @@ static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp) {
 	return 1;
 }
 
+ /**
+ * creates and push a table to the lua stack with
+ * only the firsts elements of the xavp
+ */
+static int lua_sr_push_xavp_table_simple(lua_State *L, sr_xavp_t *xavp) {
+	lua_Number i = 1;
+	sr_xavp_t *avp = NULL;
+
+	if(xavp->val.type!=SR_XTYPE_XAVP){
+		LM_ERR("%s not xavp?\n", xavp->name.s);
+		return 0;
+	}
+	avp = xavp->val.v.xavp;
+
+	lua_newtable(L);
+	while(avp!=NULL){
+		switch(avp->val.type) {
+			case SR_XTYPE_NULL:
+				lua_pushnil(L);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_INT:
+				i = avp->val.v.i;
+				lua_pushnumber(L, i);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_STR:
+				lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
+				lua_setfield(L, -2, avp->name.s);
+			break;
+			case SR_XTYPE_TIME:
+			case SR_XTYPE_LONG:
+			case SR_XTYPE_LLONG:
+			case SR_XTYPE_DATA:
+				lua_pushnil(L);
+				lua_setfield(L, -2, avp->name.s);
+				LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
+			break;
+			case SR_XTYPE_XAVP:
+				if(!lua_sr_push_xavp_table(L,avp->val.v.xavp)){
+					LM_ERR("xavp:%.*s subtable error. Nil value added\n", avp->name.len, avp->name.s);
+					lua_pushnil(L);
+				}
+				lua_setfield(L, -2, avp->name.s);
+			break;
+		}
+		avp = avp->next;
+	}
+	return 1;
+}
+
 /**
  * puts a table with content of a xavp
  */
@@ -1246,23 +1297,38 @@ static int lua_sr_xavp_get(lua_State *L)
 	int indx = 0;
 	sr_lua_env_t *env_L;
 	sr_xavp_t *avp;
+	int num_param = 0;
+	int param = -1;
+	int simple_flag = 0;
 
 	env_L = sr_lua_env_get();
-
-	if(lua_gettop(L)<2)
+	num_param = lua_gettop(L);
+	if(num_param<2 && num_param>3)
 	{
-		LM_ERR("to few parameters [%d]\n",lua_gettop(L));
+		LM_ERR("wrong number of parameters [%d]\n", num_param);
 		return 0;
 	}
 
-	if(!lua_isnumber(L, -1))
+	if(num_param==3)
+	{
+		if(!lua_isnumber(L, param))
+		{
+			LM_ERR("invalid int parameter\n");
+			return 0;
+		}
+		simple_flag = lua_tointeger(L, param);
+		param = param - 1;
+	}
+
+	if(!lua_isnumber(L, param))
 	{
 		LM_ERR("invalid int parameter\n");
 		return 0;
 	}
-	indx = lua_tointeger(L, -1);
+	indx = lua_tointeger(L, param);
+	param = param - 1;
 
-	xavp_name.s = (char*)lua_tostring(L, -2);
+	xavp_name.s = (char*)lua_tostring(L, param);
 	if(xavp_name.s==NULL || env_L->msg==NULL)
 		return 0;
 	xavp_name.len = strlen(xavp_name.s);
@@ -1273,7 +1339,15 @@ static int lua_sr_xavp_get(lua_State *L)
 		lua_pushnil(L);
 		return 1;
 	}
-	lua_sr_push_xavp_table(L, avp);
+
+	if (simple_flag != 0)
+	{
+		lua_sr_push_xavp_table_simple(L, avp);
+	}
+	else
+	{
+		lua_sr_push_xavp_table(L, avp);
+	}
 	return 1;
 }