[sr-dev] git:master:44d41c44: app_lua: option to silently try to run a lua function

Daniel-Constantin Mierla miconda at gmail.com
Tue Apr 12 14:45:32 CEST 2016


Module: kamailio
Branch: master
Commit: 44d41c446e8f42c1ca008e8f37396da61c1f181b
URL: https://github.com/kamailio/kamailio/commit/44d41c446e8f42c1ca008e8f37396da61c1f181b

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2016-04-12T14:43:52+02:00

app_lua: option to silently try to run a lua function

- if lua function not found, do not write error message
- some routing blocks are optional (e.g., reply_route{}) and their
  equivalent may not be defined

---

Modified: modules/app_lua/app_lua_api.c
Modified: modules/app_lua/app_lua_api.h
Modified: modules/app_lua/app_lua_mod.c

---

Diff:  https://github.com/kamailio/kamailio/commit/44d41c446e8f42c1ca008e8f37396da61c1f181b.diff
Patch: https://github.com/kamailio/kamailio/commit/44d41c446e8f42c1ca008e8f37396da61c1f181b.patch

---

diff --git a/modules/app_lua/app_lua_api.c b/modules/app_lua/app_lua_api.c
index ee3a0f1..5a603df 100644
--- a/modules/app_lua/app_lua_api.c
+++ b/modules/app_lua/app_lua_api.c
@@ -621,8 +621,8 @@ int app_lua_runstring(struct sip_msg *msg, char *script)
 /**
  *
  */
-int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
-		char *p3)
+int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
+		char *p3, int emode)
 {
 	int n;
 	int ret;
@@ -648,13 +648,17 @@ int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
 	lua_getglobal(_sr_L_env.LL, func);
 	if(!lua_isfunction(_sr_L_env.LL, -1))
 	{
-		LM_ERR("no such function [%s] in lua scripts\n", func);
-		LM_ERR("top stack type [%d - %s]\n",
+		if(emode) {
+			LM_ERR("no such function [%s] in lua scripts\n", func);
+			LM_ERR("top stack type [%d - %s]\n",
 				lua_type(_sr_L_env.LL, -1),
 				lua_typename(_sr_L_env.LL,lua_type(_sr_L_env.LL, -1)));
-		txt = (char*)lua_tostring(_sr_L_env.LL, -1);
-		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
-		return -1;
+			txt = (char*)lua_tostring(_sr_L_env.LL, -1);
+			LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
+			return -1;
+		} else {
+			return 1;
+		}
 	}
 	n = 0;
 	if(p1!=NULL)
@@ -687,6 +691,15 @@ int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
 	return 1;
 }
 
+/**
+ *
+ */
+int app_lua_run(sip_msg_t *msg, char *func, char *p1, char *p2,
+		char *p3)
+{
+	return app_lua_run_ex(msg, func, p1, p2, p3, 1);
+}
+
 void app_lua_dump_stack(lua_State *L)
 {
 	int i;
diff --git a/modules/app_lua/app_lua_api.h b/modules/app_lua/app_lua_api.h
index dea48b0..4182184 100644
--- a/modules/app_lua/app_lua_api.h
+++ b/modules/app_lua/app_lua_api.h
@@ -72,8 +72,10 @@ int sr_lua_reload_module(unsigned int reload);
 int app_lua_dostring(struct sip_msg *msg, char *script);
 int app_lua_dofile(struct sip_msg *msg, char *script);
 int app_lua_runstring(struct sip_msg *msg, char *script);
-int app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
+int app_lua_run(sip_msg_t *msg, char *func, char *p1, char *p2,
 		char *p3);
+int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
+		char *p3, int emode);
 
 #define SRLUA_FALSE	0
 #define SRLUA_TRUE	1
diff --git a/modules/app_lua/app_lua_mod.c b/modules/app_lua/app_lua_mod.c
index 693d2c8..11134ea 100644
--- a/modules/app_lua/app_lua_mod.c
+++ b/modules/app_lua/app_lua_mod.c
@@ -112,9 +112,9 @@ int sr_kemi_config_engine_lua(sip_msg_t *msg, int rtype, str *rname)
 	int ret;
 
 	if(rtype==REQUEST_ROUTE) {
-		ret = app_lua_run(msg, "ksr_request_route", NULL, NULL, NULL);
+		ret = app_lua_run_ex(msg, "ksr_request_route", NULL, NULL, NULL, 1);
 	} else if(rtype==CORE_ONREPLY_ROUTE) {
-		ret = app_lua_run(msg, "ksr_reply_route", NULL, NULL, NULL);
+		ret = app_lua_run_ex(msg, "ksr_reply_route", NULL, NULL, NULL, 0);
 	} else {
 		if(rname!=NULL) {
 			LM_ERR("route type %d with name [%.*s] not implemented\n",




More information about the sr-dev mailing list