Skip to content

Commit

Permalink
Allow to configure username_to_phone mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Mar 21, 2022
1 parent 094cb04 commit 4837817
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
7 changes: 7 additions & 0 deletions doc/modules/mod_jingle_sip.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ The value of the `c=` SDP attribute.

The SIP transport parameter used when calling the proxy.

### `modules.mod_jingle_sip.username_to_phone`
* **Syntax:** Array of TOML tables with the following keys: `username` and `phone`, and string values
* **Default:** `[]`
* **Example:** `username_to_phone = [{username = "2000006168", phone = +919177074440}]`

Allows mapping JIDs to phone numbers and vice versa.

The simplest configuration is the following:

```toml
Expand Down
7 changes: 4 additions & 3 deletions src/jingle_sip/jingle_sip_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ jingle_iq(ToBinary, FromBinary, JingleEl) ->

-spec maybe_rewrite_to_phone(mongoose_acc:t()) -> jid:jid().
maybe_rewrite_to_phone(Acc) ->
Server = mongoose_acc:lserver(Acc),
HT = mongoose_acc:host_type(Acc),
#jid{luser = ToUser} = JID = mongoose_acc:to_jid(Acc),
ToRewrite = gen_mod:get_module_opt(Server, mod_jingle_sip, username_to_phone, []),
ToRewrite = gen_mod:get_module_opt(HT, mod_jingle_sip, username_to_phone),
case lists:keyfind(ToUser, 1, ToRewrite) of
{ToUser, PhoneNumber} ->
JID#jid{user = PhoneNumber, luser = PhoneNumber};
Expand All @@ -64,7 +64,8 @@ maybe_rewrite_from_phone(_, Username) ->
Username.

try_to_rewrite_from_phone(Server, PhoneNumber) ->
ToRewrite = gen_mod:get_module_opt(Server, mod_jingle_sip, username_to_phone, []),
HT = mongoose_domain_api:get_host_type(Server),
ToRewrite = gen_mod:get_module_opt(HT, mod_jingle_sip, username_to_phone, []),
case lists:keyfind(PhoneNumber, 2, ToRewrite) of
{ToUser, PhoneNumber} ->
ToUser;
Expand Down
19 changes: 17 additions & 2 deletions src/jingle_sip/mod_jingle_sip.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,31 @@ config_spec() ->
<<"sdp_origin">> => #option{type = string,
validate = ip_address},
<<"transport">> => #option{type = string,
validate = {enum, ["udp", "tcp"]}}
validate = {enum, ["udp", "tcp"]}},
<<"username_to_phone">> => #list{items = username_to_phone_spec()}
},
format_items = map,
defaults = #{<<"proxy_host">> => "localhost",
<<"proxy_port">> => 5060,
<<"listen_port">> => 5600,
<<"local_host">> => "localhost",
<<"sdp_origin">> => "127.0.0.1",
<<"transport">> => "udp"}
<<"transport">> => "udp",
<<"username_to_phone">> => []}
}.

username_to_phone_spec() ->
#section{
items = #{<<"username">> => #option{type = binary},
<<"phone">> => #option{type = binary}},
required = all,
format_items = map,
process = fun process_u2p/1
}.

process_u2p(#{username := U, phone := P}) ->
{U, P}.

hooks(Host) ->
[{c2s_preprocessing_hook, Host, ?MODULE, intercept_jingle_stanza, 75}].

Expand Down
4 changes: 2 additions & 2 deletions test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,8 @@ default_mod_config(mod_inbox) ->
reset_markers => [<<"displayed">>],
iqdisc => no_queue};
default_mod_config(mod_jingle_sip) ->
#{proxy_host => "localhost", proxy_port => 5060, listen_port => 5600,
local_host => "localhost", sdp_origin => "127.0.0.1", transport => "udp"};
#{proxy_host => "localhost", proxy_port => 5060, listen_port => 5600, local_host => "localhost",
sdp_origin => "127.0.0.1", transport => "udp", username_to_phone => []};
default_mod_config(mod_keystore) ->
#{ram_key_size => 2048, keys => #{}};
default_mod_config(mod_last) ->
Expand Down
4 changes: 4 additions & 0 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,7 @@ http_upload_s3_expected_cfg() ->
{secret_access_key, "ILOVEU"}].

mod_jingle_sip(_Config) ->
check_module_defaults(mod_jingle_sip),
T = fun(Opts) -> #{<<"modules">> => #{<<"mod_jingle_sip">> => Opts}} end,
P = [modules, mod_jingle_sip],
?cfgh(P ++ [proxy_host], "proxxxy.com",
Expand All @@ -2097,6 +2098,9 @@ mod_jingle_sip(_Config) ->
T(#{<<"sdp_origin">> => <<"127.0.0.1">>})),
?cfgh(P ++ [transport], "tcp",
T(#{<<"transport">> => <<"tcp">>})),
?cfgh(P ++ [username_to_phone], [{<<"2000006168">>, <<"+919177074440">>}],
T(#{<<"username_to_phone">> => [#{<<"username">> => <<"2000006168">>,
<<"phone">> => <<"+919177074440">>}]})),
?errh(T(#{<<"proxy_host">> => 1})),
?errh(T(#{<<"proxy_port">> => 1000000})),
?errh(T(#{<<"listen_port">> => -1})),
Expand Down

0 comments on commit 4837817

Please sign in to comment.