Skip to content

Commit

Permalink
KAZOO-4779 review and simplify freeswitch vars translation
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo committed May 22, 2016
1 parent fe83243 commit b61feec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
4 changes: 3 additions & 1 deletion applications/ecallmgr/src/ecallmgr.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
%% [{AMQP-Header, FS-var-name}]
%% so FS-var-name of "foo_var" would become "foo_var=foo_val" in the channel/call string
-define(SPECIAL_CHANNEL_VARS, [{<<"Auto-Answer">>, <<"sip_auto_answer">>}
,{<<"Auto-Answer-Notify">>, <<"sip_auto_answer_notify">>}
,{<<"Auto-Answer-Notify">>, <<"sip_auto_answer_suppress_notify">>}
,{<<"Eavesdrop-Group">>, <<"eavesdrop_group">>}
,{<<"Outbound-Caller-ID-Name">>, <<"origination_caller_id_name">>}
,{<<"Outbound-Caller-ID-Number">>,<<"origination_caller_id_number">>}
Expand Down Expand Up @@ -277,6 +277,8 @@
,{<<"To-URI">>, <<"sip_to_uri">>}
,{<<"Request-URI">>, <<"sip_req_uri">>}
,{<<"Loopback-Request-URI">>, <<"sip_loopback_req_uri">>}
,{<<"Hold-Media">>, <<"hold_music">>}
,{<<"Diversions">>, <<"sip_h_Diversion">>}
]).

%% [{FreeSWITCH-App-Name, Kazoo-App-Name}]
Expand Down
29 changes: 12 additions & 17 deletions applications/ecallmgr/src/ecallmgr_fs_command.erl
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ process_fs_kv(Node, UUID, [K|KVs], 'unset'=Action)
process_fs_kv_fold(Node, UUID, Prop, Action, Acc)
end, [<<X1/binary, "=">>], KVs).

process_fs_kv_fold(_, _, {_Key, 'undefined'}, _, Acc) -> Acc;
process_fs_kv_fold(_Node, UUID, {K, V}, Action, Acc) when is_binary(V) ->
process_fs_kv_fold(_Node, UUID, {K, V}, Action, Acc) ->
[format_fs_kv(K, V, UUID, Action) | Acc];
process_fs_kv_fold(_Node, _UUID, K, 'unset', Acc)
when is_binary(K) ->
Expand All @@ -165,23 +164,19 @@ process_fs_kv_fold(_, _, _, _, Acc) ->
Acc.

-spec format_fs_kv(ne_binary(), binary(), ne_binary(), atom()) -> [binary()].
format_fs_kv(<<"Hold-Media">>, _, _, 'unset') ->
[<<"hold_music=">>];
format_fs_kv(<<"ringback">>, _, _, 'unset') ->
[<<"ringback=">>];
format_fs_kv(<<"Auto-Answer">>, _, _, 'unset') ->
[<<"alert_info=">>, <<"Auto-Answer=">>];
format_fs_kv(Key, _Value, _UUID, 'unset') ->
K = ecallmgr_util:get_fs_key(Key),
[<<K/binary, "=">>];
format_fs_kv(<<"ringback">>, Media, _, _) ->
[<<"ringback=", Media/binary>>, <<"transfer_ringback=", Media/binary>>];
format_fs_kv(<<"Auto-Answer">> = Key, Value, _, _) ->
[<<"alert_info=intercom">>, <<Key/binary, "=", Value/binary>>];
format_fs_kv(Key, Value, UUID, 'unset') ->
case ecallmgr_util:get_fs_key_and_value(Key, Value, UUID) of
'skip' -> [];
{K, _V} -> [<<K/binary, "=">>];
KVs -> [<<K/binary, "=">> || {K,_V} <- KVs]
end;
format_fs_kv(_Key, 'undefined', _UUID, _) -> [];
format_fs_kv(Key, Value, UUID, _) ->
{K, V} = ecallmgr_util:get_fs_key_and_value(Key, Value, UUID),
[<<K/binary, "=", V/binary>>].
case ecallmgr_util:get_fs_key_and_value(Key, Value, UUID) of
'skip' -> [];
{K, V} -> [<<K/binary, "=", V/binary>>];
KVs -> [<<K/binary, "=", V/binary>> || {K,V} <- KVs]
end.

-spec maybe_export_vars(atom(), ne_binary(), kz_proplist()) -> kz_proplist().
maybe_export_vars(Node, UUID, Props) ->
Expand Down
54 changes: 31 additions & 23 deletions applications/ecallmgr/src/ecallmgr_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,17 @@ is_node_up(Node, UUID) ->
get_fs_kv(Key, Value) ->
get_fs_kv(Key, Value, 'undefined').

get_fs_kv(<<?CHANNEL_VAR_PREFIX, Key/binary>>, Val, _) ->
list_to_binary([?CHANNEL_VAR_PREFIX, kz_util:to_list(Key), "=", kz_util:to_list(Val)]);
get_fs_kv(<<"Hold-Media">>, Media, UUID) ->
list_to_binary(["hold_music="
,kz_util:to_list(media_path(Media, 'extant', UUID, kz_json:new()))
]);
get_fs_kv(<<?CHANNEL_VAR_PREFIX, Key/binary>>, Val, UUID) ->
get_fs_kv(Key, Val, UUID);
get_fs_kv(Key, Val, _) ->
case lists:keyfind(Key, 1, ?SPECIAL_CHANNEL_VARS) of
'false' ->
list_to_binary([?CHANNEL_VAR_PREFIX, kz_util:to_list(Key), "=", kz_util:to_list(Val)]);
{_, Prefix} ->
V = maybe_sanitize_fs_value(Key, Val),
list_to_binary([Prefix, "=", kz_util:to_list(V), ""])
end.
list_to_binary([get_fs_key(Key), "=", maybe_sanitize_fs_value(Key, Val)]).

-spec get_fs_key(ne_binary()) -> binary().
get_fs_key(<<?CHANNEL_VAR_PREFIX, _/binary>>=Key) -> Key;
get_fs_key(<<?CHANNEL_VAR_PREFIX, Key/binary>>) -> get_fs_key(Key);
get_fs_key(Key) ->
case lists:keyfind(Key, 1, ?SPECIAL_CHANNEL_VARS) of
'false' -> <<?CHANNEL_VAR_PREFIX, Key/binary>>;
Expand All @@ -402,20 +396,34 @@ get_fs_key(Key) ->
{ne_binary(), binary()} |
[{ne_binary(), binary()}] |
'skip'.
get_fs_key_and_value(<<"Hold-Media">>, Media, UUID) ->
{<<"hold_music">>, media_path(Media, 'extant', UUID, kz_json:new())};
get_fs_key_and_value(<<"Diversions">>, Diversions, _UUID) ->
get_fs_key_and_value(<<"Hold-Media">>=Key, Media, UUID) ->
{get_fs_key(Key), media_path(Media, 'extant', UUID, kz_json:new())};
get_fs_key_and_value(<<"Diversions">>=Key, Diversions, _UUID) ->
K = get_fs_key(Key),
lager:debug("setting diversions ~p on the channel", [Diversions]),
[{<<"sip_h_Diversion">>, D} || D <- Diversions];
get_fs_key_and_value(<<?CHANNEL_VAR_PREFIX, Key/binary>>=Prefix, Val, _UUID) ->
{Prefix, maybe_sanitize_fs_value(Key, Val)};
get_fs_key_and_value(Key, Val, _UUID) when is_binary(Val) ->
case lists:keyfind(Key, 1, ?SPECIAL_CHANNEL_VARS) of
'false' ->
{list_to_binary([?CHANNEL_VAR_PREFIX, Key]), Val};
{_, Prefix} ->
{Prefix, maybe_sanitize_fs_value(Key, Val)}
end;
[{K, D} || D <- Diversions];
get_fs_key_and_value(<<"Auto-Answer">> = Key, Value, _UUID) ->
[{<<"alert_info">>, <<"intercom">>}
,{get_fs_key(Key), maybe_sanitize_fs_value(Key, Value)}
];
get_fs_key_and_value(<<"Auto-Answer-Notify">> = Key, Value, _UUID) ->
[{<<"alert_info">>, <<"intercom">>}
,{get_fs_key(<<"Auto-Answer">>), maybe_sanitize_fs_value(<<"Auto-Answer">>, <<"true">>)}
,{get_fs_key(Key), maybe_sanitize_fs_value(Key, Value)}
];
get_fs_key_and_value(<<"ringback">>=Key, Value, _UUID) ->
[{<<"ringback">>, maybe_sanitize_fs_value(Key, Value)}
,{<<"transfer_ringback">>, maybe_sanitize_fs_value(<<"transfer_ringback">>, Value)}
];
get_fs_key_and_value(<<?CHANNEL_VAR_PREFIX, Key/binary>>, Val, UUID) ->
get_fs_key_and_value(Key, Val, UUID);
get_fs_key_and_value(Key, Val, _UUID)
when is_binary(Val);
is_atom(Val);
is_number(Val);
is_list(Val);
is_boolean(Val) ->
{get_fs_key(Key), maybe_sanitize_fs_value(Key, Val)};
get_fs_key_and_value(_, _, _) -> 'skip'.

%%--------------------------------------------------------------------
Expand Down

0 comments on commit b61feec

Please sign in to comment.