12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- /**
- * $Id$
- *
- * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
- *
- * This file is part of Kamailio, a free SIP server.
- *
- * Kamailio is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * Kamailio is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include "../../sr_module.h"
- #include "../../dprint.h"
- #include "../../route_struct.h"
- #include "../../action.h"
- #include "../../ut.h"
- #include "../../mem/mem.h"
- #include "../../data_lump.h"
- #include "../../data_lump_rpl.h"
- #include "../../forward.h"
- #include "../../flags.h"
- #include "../../dset.h"
- #include "../../parser/parse_uri.h"
- #include "../../lib/kcore/cmpapi.h"
- #include "app_lua_api.h"
- #include "app_lua_sr.h"
- /**
- *
- */
- static int lua_sr_probe (lua_State *L)
- {
- LM_DBG("someone probing from lua\n");
- return 0;
- }
- /**
- *
- */
- static int lua_sr_dbg (lua_State *L)
- {
- char *txt;
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- LM_DBG("%s", txt);
- return 0;
- }
- /**
- *
- */
- static int lua_sr_err (lua_State *L)
- {
- char *txt;
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- LM_ERR("%s", txt);
- return 0;
- }
- /**
- *
- */
- static int lua_sr_log (lua_State *L)
- {
- char *txt;
- char *level;
- level = (char*)lua_tostring(L, -2);
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- {
- if(level==NULL)
- {
- LM_ERR("%s", txt);
- } else {
- if(strcasecmp(level, "dbg")==0) {
- LM_DBG("%s", txt);
- } else if(strcasecmp(level, "info")==0) {
- LM_INFO("%s", txt);
- } else if(strcasecmp(level, "warn")==0) {
- LM_WARN("%s", txt);
- } else if(strcasecmp(level, "crit")==0) {
- LM_CRIT("%s", txt);
- } else {
- LM_ERR("%s", txt);
- }
- }
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_modf (lua_State *L)
- {
- int ret;
- char *luav[MAX_ACTIONS];
- char *argv[MAX_ACTIONS];
- int argc;
- int i;
- int mod_type;
- struct run_act_ctx ra_ctx;
- unsigned modver;
- struct action *act;
- sr31_cmd_export_t* expf;
- sr_lua_env_t *env_L;
- ret = 1;
- act = NULL;
- argc = 0;
- memset(luav, 0, MAX_ACTIONS*sizeof(char*));
- memset(argv, 0, MAX_ACTIONS*sizeof(char*));
- env_L = sr_lua_env_get();
- if(env_L->msg==NULL)
- goto error;
- #if 0
- app_lua_dump_stack(L);
- #endif
- argc = lua_gettop(L);
- if(argc==0)
- {
- LM_ERR("name of module function not provided\n");
- goto error;
- }
- if(argc>=MAX_ACTIONS)
- {
- LM_ERR("too many parameters\n");
- goto error;
- }
- /* first is function name, then parameters */
- for(i=1; i<=argc; i++)
- {
- if (!lua_isstring(L, i))
- {
- LM_ERR("invalid parameter type (%d)\n", i);
- goto error;
- }
- luav[i-1] = (char*)lua_tostring(L, i);
- }
- /* pkg copy only parameters */
- for(i=1; i<MAX_ACTIONS; i++)
- {
- if(luav[i]!=NULL)
- {
- argv[i] = (char*)pkg_malloc(strlen(luav[i])+1);
- if(argv[i]==NULL)
- {
- LM_ERR("no more pkg\n");
- goto error;
- }
- strcpy(argv[i], luav[i]);
- }
- }
- expf = find_export_record(luav[0], argc-1, 0, &modver);
- if (expf==NULL) {
- LM_ERR("function '%s' is not available\n", luav[0]);
- goto error;
- }
- /* check fixups */
- if (expf->fixup!=NULL && expf->free_fixup==NULL) {
- LM_ERR("function '%s' has fixup - cannot be used\n", luav[0]);
- goto error;
- }
- switch(expf->param_no) {
- case 0:
- mod_type = MODULE0_T;
- break;
- case 1:
- mod_type = MODULE1_T;
- break;
- case 2:
- mod_type = MODULE2_T;
- break;
- case 3:
- mod_type = MODULE3_T;
- break;
- case 4:
- mod_type = MODULE4_T;
- break;
- case 5:
- mod_type = MODULE5_T;
- break;
- case 6:
- mod_type = MODULE6_T;
- break;
- case VAR_PARAM_NO:
- mod_type = MODULEX_T;
- break;
- default:
- LM_ERR("unknown/bad definition for function '%s' (%d params)\n",
- luav[0], expf->param_no);
- goto error;
- }
- act = mk_action(mod_type, argc+1 /* number of (type, value) pairs */,
- MODEXP_ST, expf, /* function */
- NUMBER_ST, argc-1, /* parameter number */
- STRING_ST, argv[1], /* param. 1 */
- STRING_ST, argv[2], /* param. 2 */
- STRING_ST, argv[3], /* param. 3 */
- STRING_ST, argv[4], /* param. 4 */
- STRING_ST, argv[5], /* param. 5 */
- STRING_ST, argv[6] /* param. 6 */
- );
- if (act==NULL) {
- LM_ERR("action structure could not be created for '%s'\n", luav[0]);
- goto error;
- }
- /* handle fixups */
- if (expf->fixup) {
- if(argc==1)
- { /* no parameters */
- if(expf->fixup(0, 0)<0)
- {
- LM_ERR("Error in fixup (0) for '%s'\n", luav[0]);
- goto error;
- }
- } else {
- for(i=1; i<=argc; i++)
- {
- if(expf->fixup(&(act->val[i+1].u.data), i)<0)
- {
- LM_ERR("Error in fixup (%d) for '%s'\n", i, luav[0]);
- goto error;
- }
- act->val[i+1].type = MODFIXUP_ST;
- }
- }
- }
- init_run_actions_ctx(&ra_ctx);
- ret = do_action(&ra_ctx, act, env_L->msg);
- /* free fixups */
- if (expf->fixup) {
- for(i=1; i<=argc; i++)
- {
- if ((act->val[i+1].type == MODFIXUP_ST) && (act->val[i+1].u.data))
- {
- expf->free_fixup(&(act->val[i+1].u.data), i);
- }
- }
- }
- pkg_free(act);
- lua_pushinteger(L, ret);
- return 1;
- error:
- if(act!=NULL)
- pkg_free(act);
- for(i=0; i<MAX_ACTIONS; i++)
- {
- if(argv[i]!=NULL) pkg_free(argv[i]);
- argv[i] = 0;
- }
- lua_pushinteger(L, -1);
- return 1;
- }
- /**
- *
- */
- static int lua_sr_is_myself (lua_State *L)
- {
- str uri;
- struct sip_uri puri;
- int ret;
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(uri.len>4 && (strncmp(uri.s, "sip:", 4)==0
- || strncmp(uri.s, "sips:", 5)==0))
- {
- if(parse_uri(uri.s, uri.len, &puri)!=0)
- {
- LM_ERR("failed to parse uri [%s]\n", uri.s);
- return app_lua_return_false(L);
- }
- ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
- (puri.transport_val.s)?puri.proto:0);
- } else {
- ret = check_self(&uri, 0, 0);
- }
- if(ret==1)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_setflag (lua_State *L)
- {
- int flag;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- setflag(env_L->msg, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetflag (lua_State *L)
- {
- int flag;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- resetflag(env_L->msg, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_isflagset (lua_State *L)
- {
- int flag;
- int ret;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- ret = isflagset(env_L->msg, flag);
- if(ret>0)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_setbflag (lua_State *L)
- {
- int flag;
- int branch;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- setbflag(branch, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetbflag (lua_State *L)
- {
- int flag;
- int branch;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- resetbflag(branch, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_isbflagset (lua_State *L)
- {
- int flag;
- int branch;
- int ret;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- ret = isbflagset(branch, flag);
- if(ret>0)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_seturi (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_URI_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_setuser (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_USER_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_sethost (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_HOST_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_setdsturi (lua_State *L)
- {
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (set_dst_uri(env_L->msg, &uri)<0)
- {
- LM_ERR("setting dst uri failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetdsturi (lua_State *L)
- {
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- reset_dst_uri(env_L->msg);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static const luaL_reg _sr_core_Map [] = {
- {"probe", lua_sr_probe},
- {"dbg", lua_sr_dbg},
- {"err", lua_sr_err},
- {"log", lua_sr_log},
- {"modf", lua_sr_modf},
- {"is_myself", lua_sr_is_myself},
- {"setflag", lua_sr_setflag},
- {"resetflag", lua_sr_resetflag},
- {"isflagset", lua_sr_isflagset},
- {"setbflag", lua_sr_setbflag},
- {"resetbflag", lua_sr_resetbflag},
- {"isbflagset", lua_sr_isbflagset},
- {"seturi", lua_sr_seturi},
- {"setuser", lua_sr_setuser},
- {"sethost", lua_sr_sethost},
- {"setdsturi", lua_sr_setdsturi},
- {"resetdsturi", lua_sr_resetdsturi},
- {NULL, NULL}
- };
- /**
- *
- */
- static int lua_sr_hdr_append (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- int len;
- char *hdr;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("append hf: %s\n", txt);
- if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1)
- {
- LM_ERR("error while parsing message\n");
- return 0;
- }
- hf = env_L->msg->last_header;
- len = strlen(txt);
- hdr = (char*)pkg_malloc(len);
- if(hdr==NULL)
- {
- LM_ERR("no pkg memory left\n");
- return 0;
- }
- memcpy(hdr, txt, len);
- anchor = anchor_lump(env_L->msg,
- hf->name.s + hf->len - env_L->msg->buf, 0, 0);
- if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
- {
- LM_ERR("can't insert lump\n");
- pkg_free(hdr);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_remove (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- str hname;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("remove hf: %s\n", txt);
- if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1) {
- LM_ERR("error while parsing message\n");
- return 0;
- }
- hname.s = txt;
- hname.len = strlen(txt);
- for (hf=env_L->msg->headers; hf; hf=hf->next)
- {
- if (cmp_hdrname_str(&hf->name, &hname)==0)
- {
- anchor=del_lump(env_L->msg,
- hf->name.s - env_L->msg->buf, hf->len, 0);
- if (anchor==0)
- {
- LM_ERR("cannot remove hdr %s\n", txt);
- return 0;
- }
- }
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_insert (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- int len;
- char *hdr;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("insert hf: %s\n", txt);
- hf = env_L->msg->headers;
- len = strlen(txt);
- hdr = (char*)pkg_malloc(len);
- if(hdr==NULL)
- {
- LM_ERR("no pkg memory left\n");
- return 0;
- }
- memcpy(hdr, txt, len);
- anchor = anchor_lump(env_L->msg,
- hf->name.s + hf->len - env_L->msg->buf, 0, 0);
- if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
- {
- LM_ERR("can't insert lump\n");
- pkg_free(hdr);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_append_to_reply (lua_State *L)
- {
- char *txt;
- int len;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("append to reply: %s\n", txt);
- len = strlen(txt);
- if(add_lump_rpl(env_L->msg, txt, len, LUMP_RPL_HDR)==0)
- {
- LM_ERR("unable to add reply lump\n");
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static const luaL_reg _sr_hdr_Map [] = {
- {"append", lua_sr_hdr_append},
- {"remove", lua_sr_hdr_remove},
- {"insert", lua_sr_hdr_insert},
- {"append_to_reply", lua_sr_hdr_append_to_reply},
- {NULL, NULL}
- };
- /**
- *
- */
- static int lua_sr_pv_get (lua_State *L)
- {
- str pvn;
- pv_spec_t pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv get: %s\n", pvn.s);
- if(pv_parse_spec(&pvn, &pvs)<0)
- {
- LM_ERR("unable to parse pv [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- 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(val.flags&PV_VAL_NULL)
- {
- return 0;
- }
- if(val.flags&PV_TYPE_INT)
- {
- lua_pushinteger(L, val.ri);
- return 1;
- }
- lua_pushlstring(L, val.rs.s, val.rs.len);
- return 1;
- }
- /**
- *
- */
- static int lua_sr_pv_seti (lua_State *L)
- {
- str pvn;
- pv_spec_t pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)<2)
- {
- LM_ERR("to few parameters [%d]\n", lua_gettop(L));
- return 0;
- }
- if(!lua_isnumber(L, -1))
- {
- LM_ERR("invalid int parameter\n");
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.ri = lua_tointeger(L, -1);
- val.flags |= PV_TYPE_INT|PV_VAL_INT;
-
- pvn.s = (char*)lua_tostring(L, -2);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv set: %s\n", pvn.s);
- if(pv_parse_spec(&pvn, &pvs)<0)
- {
- LM_ERR("unable to parse pv [%s]\n", pvn.s);
- return 0;
- }
- if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
- {
- LM_ERR("unable to set pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_sets (lua_State *L)
- {
- str pvn;
- pv_spec_t pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)<2)
- {
- LM_ERR("to few parameters [%d]\n",lua_gettop(L));
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.rs.s = (char*)lua_tostring(L, -1);
- val.rs.len = strlen(val.rs.s);
- val.flags |= PV_VAL_STR;
-
- pvn.s = (char*)lua_tostring(L, -2);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv set: %s\n", pvn.s);
- if(pv_parse_spec(&pvn, &pvs)<0)
- {
- LM_ERR("unable to parse pv [%s]\n", pvn.s);
- return 0;
- }
- if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
- {
- LM_ERR("unable to set pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_unset (lua_State *L)
- {
- str pvn;
- pv_spec_t pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv unset: %s\n", pvn.s);
- if(pv_parse_spec(&pvn, &pvs)<0)
- {
- LM_ERR("unable to parse pv [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.flags |= PV_VAL_NULL;
- if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
- {
- LM_ERR("unable to unset pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_is_null (lua_State *L)
- {
- str pvn;
- pv_spec_t pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv is null test: %s\n", pvn.s);
- if(pv_parse_spec(&pvn, &pvs)<0)
- {
- LM_ERR("unable to parse pv [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- 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(val.flags&PV_VAL_NULL)
- {
- lua_pushboolean(L, 1);
- } else {
- lua_pushboolean(L, 0);
- }
- return 1;
- }
- /**
- *
- */
- static const luaL_reg _sr_pv_Map [] = {
- {"get", lua_sr_pv_get},
- {"seti", lua_sr_pv_seti},
- {"sets", lua_sr_pv_sets},
- {"unset", lua_sr_pv_unset},
- {"is_null", lua_sr_pv_is_null},
- {NULL, NULL}
- };
- /**
- *
- */
- void lua_sr_core_openlibs(lua_State *L)
- {
- luaL_openlib(L, "sr", _sr_core_Map, 0);
- luaL_openlib(L, "sr.hdr", _sr_hdr_Map, 0);
- luaL_openlib(L, "sr.pv", _sr_pv_Map, 0);
- }
|