1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2018 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
%% |
17 |
|
%% @author Michal Piotrowski <michal.piotrowski@erlang-solutions.com> |
18 |
|
-module(jingle_sip_helper). |
19 |
|
|
20 |
|
-export([jingle_element/3]). |
21 |
|
-export([jingle_iq/3]). |
22 |
|
-export([maybe_rewrite_to_phone/1]). |
23 |
|
-export([maybe_rewrite_from_phone/2]). |
24 |
|
|
25 |
|
-include("jlib.hrl"). |
26 |
|
|
27 |
|
-spec jingle_element(CallID :: binary(), Action :: binary(), [exml:element()]) -> |
28 |
|
exml:element(). |
29 |
|
jingle_element(CallID, Action, Children) -> |
30 |
75 |
#xmlel{name = <<"jingle">>, |
31 |
|
attrs = [{<<"xmlns">>, ?JINGLE_NS}, |
32 |
|
{<<"action">>, Action}, |
33 |
|
{<<"sid">>, CallID}], |
34 |
|
children = Children}. |
35 |
|
|
36 |
|
-spec jingle_iq(ToBinary :: jid:literal_jid(), FromBinary :: jid:literal_jid(), exml:element()) -> |
37 |
|
exml:element(). |
38 |
|
jingle_iq(ToBinary, FromBinary, JingleEl) -> |
39 |
68 |
#xmlel{name = <<"iq">>, |
40 |
|
attrs = [{<<"from">>, FromBinary}, |
41 |
|
{<<"to">>, ToBinary}, |
42 |
|
{<<"id">>, uuid:uuid_to_string(uuid:get_v4(), binary_standard)}, |
43 |
|
{<<"type">>, <<"set">>}], |
44 |
|
children = [JingleEl]}. |
45 |
|
|
46 |
|
-spec maybe_rewrite_to_phone(mongoose_acc:t()) -> jid:jid(). |
47 |
|
maybe_rewrite_to_phone(Acc) -> |
48 |
25 |
HostType = mongoose_acc:host_type(Acc), |
49 |
25 |
#jid{luser = ToUser} = JID = mongoose_acc:to_jid(Acc), |
50 |
25 |
ToRewrite = gen_mod:get_module_opt(HostType, mod_jingle_sip, username_to_phone), |
51 |
25 |
case lists:keyfind(ToUser, 1, ToRewrite) of |
52 |
|
{ToUser, PhoneNumber} -> |
53 |
:-( |
JID#jid{luser = PhoneNumber}; |
54 |
|
_ -> |
55 |
25 |
JID |
56 |
|
end. |
57 |
|
|
58 |
|
-spec maybe_rewrite_from_phone(jid:lserver(), binary()) -> jid:luser(). |
59 |
|
maybe_rewrite_from_phone(Server, <<"+", _/binary>> = PhoneNumber) -> |
60 |
2 |
try_to_rewrite_from_phone(Server, PhoneNumber); |
61 |
|
maybe_rewrite_from_phone(Server, <<"*", _/binary>> = PhoneNumber) -> |
62 |
3 |
try_to_rewrite_from_phone(Server, PhoneNumber); |
63 |
|
maybe_rewrite_from_phone(_, Username) -> |
64 |
129 |
Username. |
65 |
|
|
66 |
|
try_to_rewrite_from_phone(Server, PhoneNumber) -> |
67 |
5 |
case mongoose_domain_api:get_host_type(Server) of |
68 |
|
{ok, HostType} -> |
69 |
:-( |
ToRewrite = gen_mod:get_module_opt(HostType, mod_jingle_sip, username_to_phone), |
70 |
:-( |
case lists:keyfind(PhoneNumber, 2, ToRewrite) of |
71 |
|
{ToUser, PhoneNumber} -> |
72 |
:-( |
ToUser; |
73 |
|
_ -> |
74 |
:-( |
PhoneNumber |
75 |
|
end; |
76 |
|
{error, not_found} -> |
77 |
5 |
PhoneNumber |
78 |
|
end. |
79 |
|
|