Skip to content
Browse files

WHISTLE-1370: made e164 converter into a group of pattern matches wit…

…h capture groups, prefixes, and suffixes
  • Loading branch information...
1 parent f832c0f commit 927e2d2cb2b9ac1c99f47e826c676eb3150b3e76 @k-anderson k-anderson committed
Showing with 67 additions and 67 deletions.
  1. +27 −29 ecallmgr/src/ecallmgr_authz.erl
  2. +40 −38 lib/whistle_number_manager-1.0.0/src/wnm_util.erl
View
56 ecallmgr/src/ecallmgr_authz.erl
@@ -25,15 +25,17 @@
-spec maybe_authorize_channel/2 :: (wh_proplist(), atom()) -> boolean().
maybe_authorize_channel(Props, Node) ->
+
CallId = props:get_value(<<"Unique-ID">>, Props),
- is_authz_enabled(Props, CallId, Node).
+ is_authz_enabled(Props, CallId, Node).
-spec is_authz_enabled/3 :: (wh_proplist(), ne_binary(), atom()) -> boolean().
is_authz_enabled(Props, CallId, Node) ->
+
case wh_util:is_true(ecallmgr_config:get(<<"authz_enabled">>, false)) of
- true -> is_global_resource(Props, CallId, Node);
- false ->
- lager:debug("config ecallmgr.authz is disabled", []),
+ true ->
+ is_global_resource(Props, CallId, Node);
+ false -> lager:debug("config ecallmgr.authz is disabled", []),
allow_call(Props, CallId, Node)
end.
@@ -81,16 +83,31 @@ ensure_account_id_exists(Props, CallId, Node) ->
{error, _R} ->
lager:debug("unable to determine the account id: ~p", [_R]),
maybe_deny_call(Props, CallId, Node);
- {ok, _}=Ok ->
- update_account_id(Ok, CallId, Node),
- update_reseller_id(Ok, CallId, Node),
- authorize_account(Props, CallId, Node)
+ {ok, Resp} ->
+ update_account_id(Resp, Props, CallId, Node)
end;
- AccountId ->
- put(account_id, AccountId),
+ _Else ->
authorize_account(Props, CallId, Node)
end.
+-spec update_account_id/4 :: (wh_proplist(), wh_proplist(), ne_binary(), atom()) -> 'ok'.
+update_account_id(Resp, Props, CallId, Node) ->
+ case props:get_value(?GET_CCV(<<"Account-ID">>), Resp) of
+ undefined -> update_reseller_id(Resp, Props, CallId, Node);
+ AccountId ->
+ 'ok' = ecallmgr_util:send_cmd(Node, CallId, "export", ?SET_CCV(<<"Account-ID">>, AccountId)),
+ update_reseller_id(Resp, [{?GET_CCV(<<"Account-ID">>), AccountId}|Props], CallId, Node)
+ end.
+
+-spec update_reseller_id/4 :: (wh_proplist(), wh_proplist(), ne_binary(), atom()) -> 'ok'.
+update_reseller_id(Resp, Props, CallId, Node) ->
+ case props:get_value(?GET_CCV(<<"Reseller-ID">>), Resp) of
+ undefined -> authorize_account(Props, CallId, Node);
+ ResellerId ->
+ 'ok' = ecallmgr_util:send_cmd(Node, CallId, "set", ?SET_CCV(<<"Reseller-ID">>, ResellerId)),
+ authorize_account([{?GET_CCV(<<"Reseller-ID">>), ResellerId}|Props], CallId, Node)
+ end.
+
-spec authorize_account/3 :: (wh_proplist(), ne_binary(), atom()) -> boolean().
authorize_account(Props, CallId, Node) ->
AccountId = props:get_value(?GET_CCV(<<"Account-ID">>), Props),
@@ -333,22 +350,3 @@ rating_req(CallId, Props) ->
get_time_value(Key, Props) ->
V = props:get_value(Key, Props, 0),
wh_util:unix_seconds_to_gregorian_seconds(wh_util:microseconds_to_seconds(V)).
-
--spec update_account_id/3 :: ({'ok', wh_proplist()}, ne_binary(), atom()) -> 'ok'.
-update_account_id({ok, Props}, CallId, Node) ->
- case props:get_value(?GET_CCV(<<"Account-ID">>), Props) of
- undefined -> ok;
- AccountId ->
- 'ok' = ecallmgr_util:send_cmd(Node, CallId, "export", ?SET_CCV(<<"Account-ID">>, AccountId)),
- put(account_id, AccountId),
- ok
- end.
-
--spec update_reseller_id/3 :: ({'ok', wh_proplist()}, ne_binary(), atom()) -> 'ok'.
-update_reseller_id({ok, Props}, CallId, Node) ->
- case props:get_value(?GET_CCV(<<"Reseller-ID">>), Props) of
- undefined -> ok;
- ResellerId ->
- 'ok' = ecallmgr_util:send_cmd(Node, CallId, "set", ?SET_CCV(<<"Reseller-ID">>, ResellerId)),
- ok
- end.
View
78 lib/whistle_number_manager-1.0.0/src/wnm_util.erl
@@ -29,6 +29,14 @@
-define(DEFAULT_CLASSIFIERS, [{<<"tollfree_us">>, <<"^\\+1(800|888|877|866|855)\\d{7}$">>}
,{<<"did_us">>, <<"^\\+1\\d{10}$">>}
]).
+-define(DEFAULT_E164_CONVERTERS, [{<<"^\\+?1?([2-9][0-9]{2}[2-9][0-9]{6})$">>
+ ,wh_json:from_list([{<<"prefix">>, <<"+1">>}])
+ }
+ ,{<<"^011(\\d*)$|^00(\\d*)$">>
+ ,wh_json:from_list([{<<"prefix">>, <<"+">>}])
+ }
+ ]).
+-define(DEFAULT_RECONCILE_REGEX, <<"^\\+?1?\\d{10}$">>).
%%--------------------------------------------------------------------
%% @public
@@ -64,7 +72,7 @@ classify_number(Num, [{Classification, Classifier}|Classifiers]) ->
%%--------------------------------------------------------------------
-spec is_reconcilable/1 :: (ne_binary()) -> boolean().
is_reconcilable(Number) ->
- Regex = whapps_config:get_binary(?WNM_CONFIG_CAT, <<"reconcile_regex">>, <<"^\\+?1?\\d{10}$">>),
+ Regex = whapps_config:get_binary(?WNM_CONFIG_CAT, <<"reconcile_regex">>, ?DEFAULT_RECONCILE_REGEX),
Num = wnm_util:normalize_number(Number),
case re:run(Num, Regex) of
nomatch ->
@@ -146,57 +154,51 @@ normalize_number(Number) ->
-spec is_1npan/1 :: (ne_binary()) -> boolean().
is_e164(DID) ->
- re:run(DID, <<"^\\+1\\d{10}$">>) =/= nomatch.
+ DID =:= to_e164(DID).
is_npan(DID) ->
- re:run(DID, <<"^\\d{10}$">>) =/= nomatch.
+ re:run(DID, <<"^[2-9][0-9]{2}[2-9][0-9]{6}$">>) =/= nomatch.
is_1npan(DID) ->
- re:run(DID, <<"^1\\d{10}$">>) =/= nomatch.
+ re:run(DID, <<"^\\+1[2-9][0-9]{2}[2-9][0-9]{6}$">>) =/= nomatch.
%% +18001234567 -> +18001234567
-spec to_e164/1 :: (ne_binary()) -> ne_binary().
to_e164(<<$+, _/binary>> = N) ->
N;
-to_e164(<<"011", N/binary>>) ->
- <<$+, N/binary>>;
-to_e164(<<"00", N/binary>>) ->
- <<$+, N/binary>>;
-to_e164(<<"+1", _/binary>> = N) when erlang:byte_size(N) =:= 12 ->
- N;
-%% 18001234567 -> +18001234567
-to_e164(<<$1, _/binary>> = N) when erlang:byte_size(N) =:= 11 ->
- << $+, N/binary>>;
-%% 8001234567 -> +18001234567
-to_e164(N) when erlang:byte_size(N) =:= 10 ->
- <<$+, $1, N/binary>>;
-to_e164(Other) ->
- Other.
+to_e164(Number) ->
+ Default = wh_json:from_list(?DEFAULT_E164_CONVERTERS),
+ Converters = whapps_config:get(?WNM_CONFIG_CAT, <<"e164_converters">>, Default),
+ Regexs = wh_json:get_keys(Converters),
+ maybe_convert_to_e164(Regexs, Converters, Number).
+
+maybe_convert_to_e164([], _, Number) ->
+ Number;
+maybe_convert_to_e164([Regex|Regexs], Converters, Number) ->
+ case re:run(Number, Regex, [{capture, all, binary}]) of
+ nomatch ->
+ maybe_convert_to_e164(Regexs, Converters, Number);
+ {match, Captures} ->
+ Root = lists:last(Captures),
+ Prefix = wh_json:get_binary_value([Regex, <<"prefix">>], Converters, <<>>),
+ Suffix = wh_json:get_binary_value([Regex, <<"suffix">>], Converters, <<>>),
+ <<Prefix/binary, Root/binary, Suffix/binary>>
+ end.
%% end up with 8001234567 from 1NPAN and E.164
-spec to_npan/1 :: (ne_binary()) -> ne_binary().
-to_npan(<<"011", N/binary>>) ->
- to_npan(N);
-to_npan(<<$+, $1, N/binary>>) when erlang:byte_size(N) =:= 10 ->
- N;
-to_npan(<<$1, N/binary>>) when erlang:byte_size(N) =:= 10 ->
- N;
-to_npan(NPAN) when erlang:byte_size(NPAN) =:= 10 ->
- NPAN;
-to_npan(Other) ->
- Other.
+to_npan(Number) ->
+ case re:run(Number, <<"^\\+?1?([2-9][0-9]{2}[2-9][0-9]{6})$">>, [{capture, [1], binary}]) of
+ nomatch -> Number;
+ {match, [NPAN]} -> NPAN
+ end.
-spec to_1npan/1 :: (ne_binary()) -> ne_binary().
-to_1npan(<<"011", N/binary>>) ->
- to_1npan(N);
-to_1npan(<<$+, $1, N/binary>>) when erlang:byte_size(N) =:= 10 ->
- <<$1, N/binary>>;
-to_1npan(<<$1, N/binary>>=NPAN1) when erlang:byte_size(N) =:= 10 ->
- NPAN1;
-to_1npan(NPAN) when erlang:byte_size(NPAN) =:= 10 ->
- <<$1, NPAN/binary>>;
-to_1npan(Other) ->
- Other.
+to_1npan(Number) ->
+ case re:run(Number, <<"^\\+?1?([2-9][0-9]{2}[2-9][0-9]{6})$">>, [{capture, [1], binary}]) of
+ nomatch -> Number;
+ {match, [NPAN]} -> <<$1, NPAN/binary>>
+ end.
%%--------------------------------------------------------------------
%% @public

0 comments on commit 927e2d2

Please sign in to comment.
Something went wrong with that request. Please try again.