~singpolyma/jmp-dialplan

9d8a4ff6122fd1441579d480c90103b904e7743d — Stephen Paul Weber 4 months ago
Initial commit
1 files changed, 42 insertions(+), 0 deletions(-)

A extensions.lua
A  => extensions.lua +42 -0
@@ 1,42 @@
local redis = (require "redis").connect("127.0.0.1", 6380)

local JMP_CATAPULT_U_V2 = "REDACTED"

function hex_to_char(x)
	return string.char(tonumber(x, 16))
end

function uri_decode(uri)
	return uri:gsub("%%(%x%x)", hex_to_char)
end

extensions = {
	jmp = {
		["_+1NXXNXXXXXX"] = function(context, extension)
			local from_cheogram = channel.CHEOGRAM:get() == "yes"
			if from_cheogram then
				channel.CDR("context"):set("jmp")
				local from_header = channel.SIP_HEADER("From"):get()
				local from = from_header:gsub("^[^<]*<sip:", ""):gsub(">.*$", "")
				local from_jid = uri_decode(from:sub(0, from:find("@sip.cheogram.com") - 1))
				local customer_id = redis:get("jmp_customer_id-" .. from_jid)
				local catapult_cred = redis:lrange("catapult_cred-" .. from_jid, 0, 3)
				if customer_id then
					channel.CDR("customer_id"):set(customer_id)

					if not catapult_cred or not catapult_cred[1] then
						catapult_cred = redis:lrange("catapult_cred-customer_" .. customer_id .. "@jmp.chat", 0, 3)
					end
				end
				if catapult_cred and catapult_cred[1] and catapult_cred[1] == JMP_CATAPULT_U_V2 then
					channel.CALLERID("all"):set(catapult_cred[4] .. " <" .. catapult_cred[4] .. ">")
					app.dial("SIP/bandwidth/" .. extension)
				else
					app.log("NOTICE", "JMP rejected call from " .. from .. " (" .. from_jid .. "): no caller id found")
				end
			else
				app.log("NOTICE", "Call from '' (" .. channel.CHANNEL("peerip"):get() .. ":0) to extension '" .. extension .. "' rejected in context 'jmp'.")
			end
		end;
	};
}