[sr-dev] git:master: app_lua: exported first TM functions to Lua

Daniel-Constantin Mierla miconda at gmail.com
Mon Nov 15 19:43:32 CET 2010


Module: sip-router
Branch: master
Commit: a0f8762f139ca907a199219e3bab7d656ca2731a
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=a0f8762f139ca907a199219e3bab7d656ca2731a

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Mon Nov 15 19:41:20 2010 +0100

app_lua: exported first TM functions to Lua

- you can call t_reply() and t_relay() natively in Lua via
  sr.tm.t_reply(code, reason) and sr.tm.t_relay()

---

 modules/app_lua/app_lua_exp.c |  113 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 105 insertions(+), 8 deletions(-)

diff --git a/modules/app_lua/app_lua_exp.c b/modules/app_lua/app_lua_exp.c
index e2cd272..2cc1a91 100644
--- a/modules/app_lua/app_lua_exp.c
+++ b/modules/app_lua/app_lua_exp.c
@@ -31,10 +31,12 @@
 #include "../../ut.h"
 
 #include "../../modules/sl/sl.h"
+#include "../../modules/tm/tm_load.h"
 
 #include "app_lua_api.h"
 
 #define SR_LUA_EXP_MOD_SL	(1<<0)
+#define SR_LUA_EXP_MOD_TM	(1<<1)
 
 /**
  *
@@ -47,6 +49,11 @@ static unsigned int _sr_lua_exp_reg_mods = 0;
 static sl_api_t _lua_slb;
 
 /**
+ * tm
+ */
+static tm_api_t _lua_tmb;
+
+/**
  *
  */
 static int lua_sr_sl_send_reply (lua_State *L)
@@ -61,26 +68,27 @@ static int lua_sr_sl_send_reply (lua_State *L)
 	if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL))
 	{
 		LM_WARN("weird: sl function executed but module not registered\n");
-		return 0;
+		return app_lua_return_false(L);
 	}
 
 	code = lua_tointeger(L, -2);
 
-	if(code<100 || code>=700)
-		return 0;
+	if(code<100 || code>=800)
+		return app_lua_return_false(L);
 	
 	txt.s = (char*)lua_tostring(L, -1);
-	if(txt.s!=NULL || env_L->msg==NULL)
+	if(txt.s!=NULL && env_L->msg!=NULL)
 	{
 		txt.len = strlen(txt.s);
 		ret = _lua_slb.freply(env_L->msg, code, &txt);
 		if(ret<0)
 		{
 			LM_WARN("sl send_reply returned false\n");
-			return 0;
+			return app_lua_return_false(L);
 		}
+		return app_lua_return_true(L);
 	}
-	return 0;
+	return app_lua_return_false(L);
 }
 
 /**
@@ -97,13 +105,13 @@ static int lua_sr_sl_get_reply_totag (lua_State *L)
 	if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL))
 	{
 		LM_WARN("weird: sl function executed but module not registered\n");
-		return 0;
+		return app_lua_return_false(L);
 	}
 	ret = _lua_slb.get_reply_totag(env_L->msg, &txt);
 	if(ret<0)
 	{
 		LM_WARN("sl get_reply_totag returned false\n");
-		return 0;
+		return app_lua_return_false(L);
 	}
 	lua_pushlstring(L, txt.s, txt.len);
 	return 1;
@@ -121,6 +129,79 @@ static const luaL_reg _sr_sl_Map [] = {
 /**
  *
  */
+static int lua_sr_tm_t_reply(lua_State *L)
+{
+	char *txt;
+	int code;
+	int ret;
+	sr_lua_env_t *env_L;
+
+	env_L = sr_lua_env_get();
+
+	if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
+	{
+		LM_WARN("weird: tm function executed but module not registered\n");
+		return app_lua_return_false(L);
+	}
+
+	code = lua_tointeger(L, -2);
+
+	if(code<100 || code>=800)
+		return app_lua_return_false(L);
+
+	txt = (char*)lua_tostring(L, -1);
+	if(txt!=NULL && env_L->msg!=NULL)
+	{
+		ret = _lua_tmb.t_reply(env_L->msg, code, txt);
+		if(ret<0)
+		{
+			LM_WARN("tm t_reply returned false\n");
+			/* shall push FALSE to Lua ?!? */
+			return app_lua_return_false(L);
+		}
+		return app_lua_return_true(L);
+	}
+	return app_lua_return_false(L);
+}
+
+/**
+ *
+ */
+static int lua_sr_tm_t_relay(lua_State *L)
+{
+	int ret;
+	sr_lua_env_t *env_L;
+
+	env_L = sr_lua_env_get();
+
+	if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
+	{
+		LM_WARN("weird: tm function executed but module not registered\n");
+		return app_lua_return_false(L);
+	}
+	ret = _lua_tmb.t_relay(env_L->msg, NULL, NULL);
+	if(ret<0)
+	{
+		LM_WARN("tm t_relay returned false\n");
+		return app_lua_return_false(L);
+	}
+	return app_lua_return_true(L);
+}
+
+
+/**
+ *
+ */
+static const luaL_reg _sr_tm_Map [] = {
+	{"t_reply", lua_sr_tm_t_reply},
+	{"t_relay", lua_sr_tm_t_relay},
+	{NULL, NULL}
+};
+
+
+/**
+ *
+ */
 int lua_sr_exp_init_mod(void)
 {
 	if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL)
@@ -132,6 +213,16 @@ int lua_sr_exp_init_mod(void)
 		}
 		LM_DBG("loaded sl api\n");
 	}
+	if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM)
+	{
+		/* bind the TM API */
+		if (tm_load_api(&_lua_tmb) == -1)
+		{
+			LM_ERR("cannot bind to TM API\n");
+			return -1;
+		}
+		LM_DBG("loaded tm api\n");
+	}
 	return 0;
 }
 
@@ -148,7 +239,11 @@ int lua_sr_exp_register_mod(char *mname)
 	{
 		_sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SL;
 		return 0;
+	} else 	if(len==2 && strcmp(mname, "tm")==0) {
+		_sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_TM;
+		return 0;
 	}
+
 	return -1;
 }
 
@@ -159,6 +254,8 @@ void lua_sr_exp_openlibs(lua_State *L)
 {
 	if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL)
 		luaL_openlib(L, "sr.sl",   _sr_sl_Map,   0);
+	if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM)
+		luaL_openlib(L, "sr.tm",   _sr_tm_Map,   0);
 }
 
 




More information about the sr-dev mailing list