Module: kamailio Branch: master Commit: 15499c19bae30f1ca5b98e06d840e8db1b37ff20 URL: https://github.com/kamailio/kamailio/commit/15499c19bae30f1ca5b98e06d840e8db...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: 2016-04-28T00:04:46+02:00
app_lua: implemented KSR.x.exit()
- specific extension to stop execution of lua script (similar to exit in kamailio.cfg) - it's a wrapper around lua function error() with a predefined message handled after the return of lua_pcall()
---
Modified: modules/app_lua/app_lua_api.c Modified: modules/app_lua/app_lua_api.h Modified: modules/app_lua/app_lua_sr.c
---
Diff: https://github.com/kamailio/kamailio/commit/15499c19bae30f1ca5b98e06d840e8db... Patch: https://github.com/kamailio/kamailio/commit/15499c19bae30f1ca5b98e06d840e8db...
---
diff --git a/modules/app_lua/app_lua_api.c b/modules/app_lua/app_lua_api.c index ca1dee3..d11c627 100644 --- a/modules/app_lua/app_lua_api.c +++ b/modules/app_lua/app_lua_api.c @@ -627,12 +627,25 @@ int app_lua_runstring(sip_msg_t *msg, char *script) /** * */ +static str _sr_kemi_lua_exit_string = str_init("~~ksr~exit~~"); + +/** + * + */ +str* sr_kemi_lua_exit_string_get(void) +{ + return &_sr_kemi_lua_exit_string; +} + +/** + * + */ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2, char *p3, int emode) { int n; int ret; - char *txt; + str txt; sip_msg_t *bmsg;
if(_sr_L_env.LL==NULL) @@ -660,8 +673,8 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2, 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"); + txt.s = (char*)lua_tostring(_sr_L_env.LL, -1); + LM_ERR("error from Lua: %s\n", (txt.s)?txt.s:"unknown"); return -1; } else { return 1; @@ -689,11 +702,31 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2, _sr_L_env.msg = bmsg; if(ret!=0) { - LM_ERR("error executing: %s (err: %d)\n", func, ret); - txt = (char*)lua_tostring(_sr_L_env.LL, -1); - LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown"); + txt.s = (char*)lua_tostring(_sr_L_env.LL, -1); + n = 0; + if(txt.s!=NULL) { + for(n=0; txt.s[n]!='\0' && _sr_kemi_lua_exit_string.s[n]!='\0'; + n++) { + if(txt.s[n] != _sr_kemi_lua_exit_string.s[n]) + break; + } + if(txt.s[n]!='\0' || _sr_kemi_lua_exit_string.s[n]!='\0') { + LM_ERR("error from Lua: %s\n", txt.s); + n = 0; + } else { + LM_DBG("ksr error call from Lua: %s\n", txt.s); + n = 1; + } + } else { + LM_ERR("error from Lua: unknown\n"); + } lua_pop(_sr_L_env.LL, 1); - return -1; + if(n==1) { + return 1; + } else { + LM_ERR("error executing: %s (err: %d)\n", func, ret); + return -1; + } }
return 1; diff --git a/modules/app_lua/app_lua_api.h b/modules/app_lua/app_lua_api.h index 4182184..3147f0d 100644 --- a/modules/app_lua/app_lua_api.h +++ b/modules/app_lua/app_lua_api.h @@ -87,5 +87,7 @@ int app_lua_return_error(lua_State *L);
void app_lua_dump_stack(lua_State *L);
+str* sr_kemi_lua_exit_string_get(void); + #endif
diff --git a/modules/app_lua/app_lua_sr.c b/modules/app_lua/app_lua_sr.c index df7700a..e5a87a7 100644 --- a/modules/app_lua/app_lua_sr.c +++ b/modules/app_lua/app_lua_sr.c @@ -1691,8 +1691,24 @@ int sr_kemi_exec_func(lua_State* L, str *mname, int midx, str *fname) /** * */ +static int sr_kemi_lua_exit (lua_State *L) +{ + str *s; + + LM_DBG("script exit call\n"); + s = sr_kemi_lua_exit_string_get(); + lua_getglobal(L, "error"); + lua_pushstring(L, s->s); + lua_call(L, 1, 0); + return 0; +} + +/** + * + */ static const luaL_Reg _sr_kemi_x_Map [] = { - {"modf", lua_sr_modf}, + {"modf", lua_sr_modf}, + {"exit", sr_kemi_lua_exit}, {NULL, NULL} };