diff --git a/doc/modules/mod_jingle_sip.md b/doc/modules/mod_jingle_sip.md index eabfa3e6a8..36fa95b2d9 100644 --- a/doc/modules/mod_jingle_sip.md +++ b/doc/modules/mod_jingle_sip.md @@ -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 diff --git a/src/jingle_sip/jingle_sip_helper.erl b/src/jingle_sip/jingle_sip_helper.erl index 7425101e8b..206990a291 100644 --- a/src/jingle_sip/jingle_sip_helper.erl +++ b/src/jingle_sip/jingle_sip_helper.erl @@ -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}; @@ -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; diff --git a/src/jingle_sip/mod_jingle_sip.erl b/src/jingle_sip/mod_jingle_sip.erl index 8be1f0ca46..54b8102332 100644 --- a/src/jingle_sip/mod_jingle_sip.erl +++ b/src/jingle_sip/mod_jingle_sip.erl @@ -97,7 +97,8 @@ 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", @@ -105,8 +106,22 @@ config_spec() -> <<"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}]. diff --git a/test/common/config_parser_helper.erl b/test/common/config_parser_helper.erl index 45b3b270d2..10a262759a 100644 --- a/test/common/config_parser_helper.erl +++ b/test/common/config_parser_helper.erl @@ -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) -> diff --git a/test/config_parser_SUITE.erl b/test/config_parser_SUITE.erl index 50894195dc..e3cfe5b3e0 100644 --- a/test/config_parser_SUITE.erl +++ b/test/config_parser_SUITE.erl @@ -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", @@ -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})),