Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz authored and Paweł Chrząszcz committed Jun 29, 2022
1 parent fe70ffa commit 9c5d64c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 243 deletions.
176 changes: 2 additions & 174 deletions src/mod_commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,19 @@
registered_users/1,
change_user_password/3,
list_sessions/1,
list_contacts/1,
add_contact/2,
add_contact/3,
add_contact/4,
delete_contacts/2,
delete_contact/2,
subscription/3,
set_subscription/3,
kick_session/3,
get_recent_messages/3,
get_recent_messages/4,
send_message/3,
send_stanza/1
]).

-ignore_xref([add_contact/2, add_contact/3, add_contact/4, change_user_password/3,
delete_contact/2, delete_contacts/2, get_recent_messages/3,
get_recent_messages/4, kick_session/3, list_contacts/1,
-ignore_xref([change_user_password/3, get_recent_messages/3, get_recent_messages/4, kick_session/3,
list_sessions/1, register/3, registered_commands/0, registered_users/1,
send_message/3, send_stanza/1, set_subscription/3, start/0, stop/0,
subscription/3, unregister/2]).
send_message/3, send_stanza/1, start/0, stop/0, unregister/2]).

-include("mongoose.hrl").
-include("jlib.hrl").
-include("mongoose_rsm.hrl").
-include("session.hrl").

start() ->
mongoose_commands:register(commands()).
Expand Down Expand Up @@ -118,76 +105,6 @@ commands() ->
{args, [{host, binary}, {user, binary}, {res, binary}]},
{result, ok}
],
[
{name, list_contacts},
{category, <<"contacts">>},
{desc, <<"Get roster">>},
{module, ?MODULE},
{function, list_contacts},
{action, read},
{security_policy, [user]},
{args, [{caller, binary}]},
{result, []}
],
[
{name, add_contact},
{category, <<"contacts">>},
{desc, <<"Add a contact to roster">>},
{module, ?MODULE},
{function, add_contact},
{action, create},
{security_policy, [user]},
{args, [{caller, binary}, {jid, binary}]},
{result, ok}
],
[
{name, subscription},
{category, <<"contacts">>},
{desc, <<"Send out a subscription request">>},
{module, ?MODULE},
{function, subscription},
{action, update},
{security_policy, [user]},
{identifiers, [caller, jid]},
% caller has to be in identifiers, otherwise it breaks admin rest api
{args, [{caller, binary}, {jid, binary}, {action, binary}]},
{result, ok}
],
[
{name, set_subscription},
{category, <<"contacts">>},
{subcategory, <<"manage">>},
{desc, <<"Set / unset mutual subscription">>},
{module, ?MODULE},
{function, set_subscription},
{action, update},
{identifiers, [caller, jid]},
{args, [{caller, binary}, {jid, binary}, {action, binary}]},
{result, ok}
],
[
{name, delete_contact},
{category, <<"contacts">>},
{desc, <<"Remove a contact from roster">>},
{module, ?MODULE},
{function, delete_contact},
{action, delete},
{security_policy, [user]},
{args, [{caller, binary}, {jid, binary}]},
{result, ok}
],
[
{name, delete_contacts},
{category, <<"contacts">>},
{subcategory, <<"multiple">>},
{desc, <<"Remove provided contacts from roster">>},
{module, ?MODULE},
{function, delete_contacts},
{action, delete},
{security_policy, [user]},
{args, [{caller, binary}, {jids, [binary]}]},
{result, []}
],
[
{name, send_message},
{category, <<"messages">>},
Expand Down Expand Up @@ -318,55 +235,6 @@ do_send_packet(From, To, Packet) ->
{error, unknown_domain}
end.

list_contacts(Caller) ->
case mod_roster_api:list_contacts(jid:from_binary(Caller)) of
{ok, Rosters} ->
[roster_info(mod_roster:item_to_map(R)) || R <- Rosters];
Error ->
skip_result_msg(Error)
end.

roster_info(M) ->
Jid = jid:to_binary(maps:get(jid, M)),
#{subscription := Sub, ask := Ask} = M,
#{jid => Jid, subscription => Sub, ask => Ask}.

add_contact(Caller, JabberID) ->
add_contact(Caller, JabberID, <<"">>, []).

add_contact(Caller, JabberID, Name) ->
add_contact(Caller, JabberID, Name, []).

add_contact(Caller, Other, Name, Groups) ->
case mongoose_stanza_helper:parse_from_to(Caller, Other) of
{ok, CallerJid, OtherJid} ->
Res = mod_roster_api:add_contact(CallerJid, OtherJid, Name, Groups),
skip_result_msg(Res);
E ->
E
end.

delete_contacts(Caller, ToDelete) ->
maybe_delete_contacts(Caller, ToDelete, []).

maybe_delete_contacts(_, [], NotDeleted) -> NotDeleted;
maybe_delete_contacts(Caller, [H | T], NotDeleted) ->
case delete_contact(Caller, H) of
ok ->
maybe_delete_contacts(Caller, T, NotDeleted);
_Error ->
maybe_delete_contacts(Caller, T, NotDeleted ++ [H])
end.

delete_contact(Caller, Other) ->
case mongoose_stanza_helper:parse_from_to(Caller, Other) of
{ok, CallerJID, OtherJID} ->
Res = mod_roster_api:delete_contact(CallerJID, OtherJID),
skip_result_msg(Res);
E ->
E
end.

registered_commands() ->
Items = collect_commands(),
sort_commands(Items).
Expand Down Expand Up @@ -424,43 +292,3 @@ row_to_map(#{id := Id, jid := From, packet := Msg}) ->
Body = exml_query:path(Msg, [{element, <<"body">>}, cdata]),
#{sender => Jbin, timestamp => round(Msec / 1000000), message_id => MsgId,
body => Body}.

subscription(Caller, Other, Action) ->
case decode_action(Action) of
error ->
{error, bad_request, <<"invalid action">>};
Act ->
case mongoose_stanza_helper:parse_from_to(Caller, Other) of
{ok, CallerJID, OtherJID} ->
Res = mod_roster_api:subscription(CallerJID, OtherJID, Act),
skip_result_msg(Res);
E ->
E
end
end.

decode_action(<<"subscribe">>) -> subscribe;
decode_action(<<"subscribed">>) -> subscribed;
decode_action(_) -> error.

set_subscription(Caller, Other, Action) ->
case mongoose_stanza_helper:parse_from_to(Caller, Other) of
{ok, CallerJID, OtherJID} ->
case decode_both_sub_action(Action) of
error ->
{error, bad_request, <<"invalid action">>};
ActionDecoded ->
Res = mod_roster_api:set_mutual_subscription(CallerJID, OtherJID,
ActionDecoded),
skip_result_msg(Res)
end;
E ->
E
end.

decode_both_sub_action(<<"connect">>) -> connect;
decode_both_sub_action(<<"disconnect">>) -> disconnect;
decode_both_sub_action(_) -> error.

skip_result_msg({ok, _Msg}) -> ok;
skip_result_msg({ErrCode, _Msg}) -> {error, ErrCode}.
Loading

0 comments on commit 9c5d64c

Please sign in to comment.