瀏覽代碼

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()
Daniel-Constantin Mierla 15 年之前
父節點
當前提交
a0f8762f13
共有 1 個文件被更改,包括 105 次插入8 次删除
  1. 105 8
      modules/app_lua/app_lua_exp.c

+ 105 - 8
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)
 
 /**
  *
@@ -46,6 +48,11 @@ static unsigned int _sr_lua_exp_reg_mods = 0;
  */
 static sl_api_t _lua_slb;
 
+/**
+ * tm
+ */
+static tm_api_t _lua_tmb;
+
 /**
  *
  */
@@ -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;
@@ -118,6 +126,79 @@ static const luaL_reg _sr_sl_Map [] = {
 	{NULL, NULL}
 };
 
+/**
+ *
+ */
+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}
+};
+
+
 /**
  *
  */
@@ -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);
 }