Module: kamailio
Branch: master
Commit: c3e36a1eb2ff70f94b83a42de3e901f4ba142813
URL:
https://github.com/kamailio/kamailio/commit/c3e36a1eb2ff70f94b83a42de3e901f…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2018-05-15T07:54:24+02:00
app_lua: kemi - exported KSR.pv.getw("$...")
- get the value of the pseudo-variable if it is not $null and the string
"<<null>>" if it is $null
- useful to use in building log messages without testing if the variable
is set, otherwise lua throws error while trying to concatenate null
value
---
Modified: src/modules/app_lua/app_lua_sr.c
---
Diff:
https://github.com/kamailio/kamailio/commit/c3e36a1eb2ff70f94b83a42de3e901f…
Patch:
https://github.com/kamailio/kamailio/commit/c3e36a1eb2ff70f94b83a42de3e901f…
---
diff --git a/src/modules/app_lua/app_lua_sr.c b/src/modules/app_lua/app_lua_sr.c
index f6155dc741..6edba5d7a7 100644
--- a/src/modules/app_lua/app_lua_sr.c
+++ b/src/modules/app_lua/app_lua_sr.c
@@ -884,7 +884,7 @@ static const luaL_Reg _sr_hdr_Map [] = {
/**
*
*/
-static int lua_sr_pv_get (lua_State *L)
+static int lua_sr_pv_get_val (lua_State *L, int rmode)
{
str pvn;
pv_spec_t *pvs;
@@ -895,35 +895,56 @@ static int lua_sr_pv_get (lua_State *L)
env_L = sr_lua_env_get();
pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
+ if(pvn.s==NULL || env_L->msg==NULL) {
+ if(rmode) {
+ lua_pushlstring(L, "<<null>>", 8);
+ return 1;
+ } else {
+ return 0;
+ }
+ }
pvn.len = strlen(pvn.s);
LM_DBG("pv get: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
+ if(pl != pvn.len) {
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
+ if(rmode) {
+ lua_pushlstring(L, "<<null>>", 8);
+ return 1;
+ } else {
+ return 0;
+ }
}
pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
+ if(pvs==NULL) {
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
+ if(rmode) {
+ lua_pushlstring(L, "<<null>>", 8);
+ return 1;
+ } else {
+ return 0;
+ }
}
memset(&val, 0, sizeof(pv_value_t));
- if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
- {
+ if(pv_get_spec_value(env_L->msg, pvs, &val) != 0) {
LM_ERR("unable to get pv value for [%s]\n", pvn.s);
- return 0;
+ if(rmode) {
+ lua_pushlstring(L, "<<null>>", 8);
+ return 1;
+ } else {
+ return 0;
+ }
}
- if(val.flags&PV_VAL_NULL)
- {
- return 0;
+ if(val.flags&PV_VAL_NULL) {
+ if(rmode) {
+ lua_pushlstring(L, "<<null>>", 8);
+ return 1;
+ } else {
+ return 0;
+ }
}
- if(val.flags&PV_TYPE_INT)
- {
+ if(val.flags&PV_TYPE_INT) {
lua_pushinteger(L, val.ri);
return 1;
}
@@ -931,6 +952,22 @@ static int lua_sr_pv_get (lua_State *L)
return 1;
}
+/**
+ *
+ */
+static int lua_sr_pv_get (lua_State *L)
+{
+ return lua_sr_pv_get_val(L, 0);
+}
+
+/**
+ *
+ */
+static int lua_sr_pv_getw (lua_State *L)
+{
+ return lua_sr_pv_get_val(L, 1);
+}
+
/**
*
*/
@@ -1137,6 +1174,7 @@ static int lua_sr_pv_is_null (lua_State *L)
*/
static const luaL_Reg _sr_pv_Map [] = {
{"get", lua_sr_pv_get},
+ {"getw", lua_sr_pv_getw},
{"seti", lua_sr_pv_seti},
{"sets", lua_sr_pv_sets},
{"unset", lua_sr_pv_unset},