diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..447d533 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +kamailio diff --git a/kam_utils.lua b/kam_utils.lua new file mode 100644 index 0000000..4c229cd --- /dev/null +++ b/kam_utils.lua @@ -0,0 +1,34 @@ +#!/usr/bin/env lua5.1 +# Kamailio Lua utils + +-- cleans and sets string values from the table list +function sets_avps(list) + local i, v + + for i,v in pairs(list) do + -- sr.log("debug","i:" .. i .. " v:" .. v) + sr.pv.unset('$avp(' .. i ..')[*]') + sr.pv.sets('$avp(' .. i .. ')', v) + end +end + +-- cleans and sets int values from the table list +function seti_avps(list) + local i, v + + for i,v in pairs(list) do + -- sr.log("debug","i:" .. i .. " v:" .. v) + sr.pv.unset('$avp(' .. i ..')[*]') + sr.pv.seti('$avp(' .. i .. ')', v) + end +end + +function clean_avps(list) + local i,v + + for i,v in pairs(list) do + sr.pv.unset('$avp(' .. i .. ')[*]') + end +end + +#EOF \ No newline at end of file diff --git a/kamalio_conf.lua b/kamalio_conf.lua new file mode 100644 index 0000000..1c84fdd --- /dev/null +++ b/kamalio_conf.lua @@ -0,0 +1,36 @@ +#!/usr/bin/env lua5.1 +-- Kamailio Lua Config +require "kam_utils.lua" + +-- ROUTE_CLEAR_PEER_IN_PREF +function clear_peer_in_pref() + local list = { + peer_peer_callee_auth_user, + peer_peer_callee_auth_pass, + peer_peer_callee_auth_realm, + caller_use_rtpproxy, + peer_caller_ipv46_for_rtpproxy, + caller_force_outbound_calls_to_peer, + peer_caller_find_subscriber_by_uuid, + pstn_dp_caller_in_id, + pstn_dp_callee_in_id, + pstn_dp_caller_out_id, + pstn_dp_callee_out_id, + rewrite_caller_in_dpid, + rewrite_caller_out_dpid, + rewrite_callee_in_dpid, + rewrite_callee_out_dpid, + caller_peer_concurrent_max, + peer_caller_sst_enable, + peer_caller_sst_expires, + peer_caller_sst_min_timer, + peer_caller_sst_max_timer, + peer_caller_sst_refresh_method, + caller_inbound_upn, + caller_inbound_npn, + caller_inbound_uprn + } + + clean_avps(list) +end + diff --git a/utils.lua b/utils.lua new file mode 100644 index 0000000..d6ec83e --- /dev/null +++ b/utils.lua @@ -0,0 +1,101 @@ +#!/usr/bin/env lua5.1 +# Lua utils + +-- kamailio log for a table +function log_table(table, msg, level) + if not level then + level = "debug" + end + if msg then + sr.log(level, msg) + end + if not table then + -- empty table + return + end + for i,v in pairs(table) do + if type(i) == "number" then + iformat = "%d" + elseif type(i) == "string" then + iformat = "%s" + end + if type(v) == "string" then + sr.log(level, string.format("i:" .. iformat .. " v: %s", i, v)) + elseif type(v) == "number" then + sr.log(level, string.format("i:" .. iformat .. " v: %d", i, v)) + elseif type(v) == "table" then + log_table(v,string.format("i:" .. iformat .. " v:", i),level) + end + end +end + +-- copy a table +function table_deepcopy(object) + local lookup_table = {} + local function _copy(object) + if type(object) ~= "table" then + return object + elseif lookup_table[object] then + return lookup_table[object] + end + local new_table = {} + lookup_table[object] = new_table + for index, value in pairs(object) do + new_table[_copy(index)] = _copy(value) + end + return setmetatable(new_table, getmetatable(object)) + end + return _copy(object) +end + +-- from table to string +-- t = {'a','b'} +-- implode(",",t,"'") +-- "'a','b'" +-- implode("#",t) +-- "a#b" +function implode(delimiter, list, quoter) + local len = #list + if len == 0 then + return nil + end + if not quoter then + quoter = "" + end + local string = quoter .. list[1] .. quoter + for i = 2, len do + string = string .. delimiter .. quoter .. list[i] .. quoter + end + return string +end + +-- from string to table +function explode(delimiter, text) + local list = {}; local pos = 1 + if string.find("", delimiter, 1) then + -- We'll look at error handling later! + error("delimiter matches empty string!") + end + while 1 do + local first, last = string.find(text, delimiter, pos) + print (first, last) + if first then + table.insert(list, string.sub(text, pos, first-1)) + pos = last+1 + else + table.insert(list, string.sub(text, pos)) + break + end + end + return list +end + +function compare_desc_len(a,b) + return string.len(a) > string.len(b) +end + +function findpattern(text, pattern, start) + return string.sub(text, string.find(text, pattern, start)) +end + +#EOF \ No newline at end of file