Last active
May 1, 2017 08:35
-
-
Save moteus/e21ca9720776b90cc9ca3dab2fb6899b to your computer and use it in GitHub Desktop.
API command to fix ALPHANUMERIC source numbers received from mod_gsmopen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- API command to fix ALPHANUMERIC source numbers received from mod_gsmopen | |
-- Usage: <action application="set" data="from=${lua(sms_bcd2gsm.lua ${from})}" /> | |
-- !! Do not forget that for resending uses not `from` header but `from_full`. | |
-- | |
-- https://github.com/moteus/lua-tpdu | |
-- | |
assert(stream, 'this is API command') | |
local tut = require "tpdu.utils" | |
local Bit7 = require "tpdu.bit7" | |
local Bcd = require "tpdu.bcd" | |
local function split_first(str, sep, plain) | |
local e, e2 = string.find(str, sep, nil, plain) | |
if e then | |
return string.sub(str, 1, e - 1), string.sub(str, e2 + 1) | |
end | |
return str | |
end | |
-- mod_gsmopen incorrectly decode ALPHANUMERIC numbers as BCD | |
-- so we revert it back and decode as Bit7/Gsm encoded string | |
local function fixup_number(number) | |
-- we really can not be sure what type of number we have because we have no access | |
-- to TOA structure. So we can only guess | |
-- | |
-- 1. if string has non digit symbols | |
-- 2. if string has only hex symbols | |
-- 3. all symbols in string have same case | |
-- 4. sting length is even | |
if string.find(number,'%D') | |
and string.match(number, '^%x+$') | |
and ((string.upper(number) == number) or (string.lower(number) == number)) | |
and (#number % 2 == 0) | |
then | |
number = Bit7.GsmDecode ( tut.hex2bin( Bcd.Decode(number) ) ) | |
end | |
return number | |
end | |
local function fixup_from(from) | |
local user, host, proto = split_first(from, '@', true) | |
proto, user = split_first(user, ':', true) | |
if not user then user, proto = proto end | |
user = fixup_number(user) | |
if host then user = user .. '@' .. host end | |
if proto then user = proto .. ':' .. user end | |
return user | |
end | |
local number = assert(argv[1]) | |
local fixed = fixup_from(number) | |
freeswitch.consoleLog('debug', string.format('[sms_bcd2gsm] `%s` => `%s`\n', number, fixed)) | |
stream:write(fixed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment