25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Merge pull request #4088 from badlop/get-roster-command

Improve get_roster command result: show groups as a list
This commit is contained in:
badlop 2023-09-25 13:15:26 +02:00 committed by GitHub
commit b33d660f88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 19 deletions

View File

@ -335,7 +335,7 @@ call_command([CmdString | Args], Auth, _AccessCommands, Version) ->
ArgsFormatted, ArgsFormatted,
CI2, CI2,
Version), Version),
format_result(Result, ResultFormat); format_result_preliminary(Result, ResultFormat);
{'EXIT', {function_clause,[{lists,zip,[A1,A2|_], _} | _]}} -> {'EXIT', {function_clause,[{lists,zip,[A1,A2|_], _} | _]}} ->
{NumCompa, TextCompa} = {NumCompa, TextCompa} =
case {length(A1), length(A2)} of case {length(A1), length(A2)} of
@ -385,6 +385,11 @@ format_arg2(Arg, Parse)->
%% Format result %% Format result
%%----------------------------- %%-----------------------------
format_result_preliminary(Result, {A, {list, B}}) ->
format_result(Result, {A, {top_result_list, B}});
format_result_preliminary(Result, ResultFormat) ->
format_result(Result, ResultFormat).
format_result({error, ErrorAtom}, _) -> format_result({error, ErrorAtom}, _) ->
{io_lib:format("Error: ~p", [ErrorAtom]), make_status(error)}; {io_lib:format("Error: ~p", [ErrorAtom]), make_status(error)};
@ -421,6 +426,16 @@ format_result(Code, {_Name, rescode}) ->
format_result({Code, Text}, {_Name, restuple}) -> format_result({Code, Text}, {_Name, restuple}) ->
{io_lib:format("~ts", [Text]), make_status(Code)}; {io_lib:format("~ts", [Text]), make_status(Code)};
format_result([], {_Name, {top_result_list, _ElementsDef}}) ->
"";
format_result([FirstElement | Elements], {_Name, {top_result_list, ElementsDef}}) ->
[format_result(FirstElement, ElementsDef) |
lists:map(
fun(Element) ->
["\n" | format_result(Element, ElementsDef)]
end,
Elements)];
%% The result is a list of something: [something()] %% The result is a list of something: [something()]
format_result([], {_Name, {list, _ElementsDef}}) -> format_result([], {_Name, {list, _ElementsDef}}) ->
""; "";
@ -430,7 +445,7 @@ format_result([FirstElement | Elements], {_Name, {list, ElementsDef}}) ->
%% If there are more elements, put always first a newline character %% If there are more elements, put always first a newline character
lists:map( lists:map(
fun(Element) -> fun(Element) ->
["\n" | format_result(Element, ElementsDef)] [";" | format_result(Element, ElementsDef)]
end, end,
Elements)]; Elements)];
@ -755,7 +770,10 @@ print_usage_help(MaxC, ShCode) ->
"\n", "\n",
"Please note that 'ejabberdctl' shows all ejabberd commands,\n", "Please note that 'ejabberdctl' shows all ejabberd commands,\n",
"even those that cannot be used in the shell with ejabberdctl.\n", "even those that cannot be used in the shell with ejabberdctl.\n",
"Those commands can be identified because their description starts with: *"], "Those commands can be identified because their description starts with: *\n",
"\n",
"Some commands return lists, like get_roster and get_user_subscriptions.\n",
"In those commands, the elements in the list are separated with: ;\n"],
ArgsDef = [], ArgsDef = [],
C = #ejabberd_commands{ C = #ejabberd_commands{
name = help, name = help,
@ -817,6 +835,11 @@ filter_commands_regexp(All, Glob) ->
end, end,
All). All).
maybe_add_policy_arguments(Args, user) ->
[{user, binary}, {host, binary} | Args];
maybe_add_policy_arguments(Args, _) ->
Args.
-spec print_usage_command(Cmd::string(), MaxC::integer(), -spec print_usage_command(Cmd::string(), MaxC::integer(),
ShCode::boolean(), Version::integer()) -> ok. ShCode::boolean(), Version::integer()) -> ok.
print_usage_command(Cmd, MaxC, ShCode, Version) -> print_usage_command(Cmd, MaxC, ShCode, Version) ->
@ -829,13 +852,15 @@ print_usage_command2(Cmd, C, MaxC, ShCode) ->
tags = TagsAtoms, tags = TagsAtoms,
definer = Definer, definer = Definer,
desc = Desc, desc = Desc,
args = ArgsDef, args = ArgsDefPreliminary,
policy = Policy,
longdesc = LongDesc, longdesc = LongDesc,
result = ResultDef} = C, result = ResultDef} = C,
NameFmt = [" ", ?B("Command Name"), ": ", ?C(Cmd), "\n"], NameFmt = [" ", ?B("Command Name"), ": ", ?C(Cmd), "\n"],
%% Initial indentation of result is 13 = length(" Arguments: ") %% Initial indentation of result is 13 = length(" Arguments: ")
ArgsDef = maybe_add_policy_arguments(ArgsDefPreliminary, Policy),
Args = [format_usage_ctype(ArgDef, 13) || ArgDef <- ArgsDef], Args = [format_usage_ctype(ArgDef, 13) || ArgDef <- ArgsDef],
ArgsMargin = lists:duplicate(13, $\s), ArgsMargin = lists:duplicate(13, $\s),
ArgsListFmt = case Args of ArgsListFmt = case Args of

View File

@ -574,7 +574,10 @@ get_commands_spec() ->
]}} ]}}
}}}, }}},
#ejabberd_commands{name = get_roster, tags = [roster], #ejabberd_commands{name = get_roster, tags = [roster],
desc = "Get roster of a local user", desc = "Get list of contacts in a local user roster",
longdesc =
"Subscription can be: \"none\", \"from\", \"to\", \"both\". "
"Pending can be: \"in\", \"out\", \"none\".",
policy = user, policy = user,
module = ?MODULE, function = get_roster, module = ?MODULE, function = get_roster,
args = [], args = [],
@ -583,8 +586,8 @@ get_commands_spec() ->
{jid, string}, {jid, string},
{nick, string}, {nick, string},
{subscription, string}, {subscription, string},
{ask, string}, {pending, string},
{group, string} {groups, {list, {group, string}}}
]}}}}}, ]}}}}},
#ejabberd_commands{name = push_roster, tags = [roster], #ejabberd_commands{name = push_roster, tags = [roster],
desc = "Push template roster from file to a user", desc = "Push template roster from file to a user",
@ -1334,25 +1337,16 @@ get_roster(User, Server) ->
make_roster_xmlrpc(Items) make_roster_xmlrpc(Items)
end. end.
%% Note: if a contact is in several groups, the contact is returned
%% several times, each one in a different group.
make_roster_xmlrpc(Roster) -> make_roster_xmlrpc(Roster) ->
lists:foldl( lists:map(
fun(#roster_item{jid = JID, name = Nick, subscription = Sub, ask = Ask} = Item, Res) -> fun(#roster_item{jid = JID, name = Nick, subscription = Sub, ask = Ask, groups = Groups} = Item) ->
JIDS = jid:encode(JID), JIDS = jid:encode(JID),
Subs = atom_to_list(Sub), Subs = atom_to_list(Sub),
Asks = atom_to_list(Ask), Asks = atom_to_list(Ask),
Groups = case Item#roster_item.groups of {JIDS, Nick, Subs, Asks, Groups}
[] -> [<<>>];
Gs -> Gs
end, end,
ItemsX = [{JIDS, Nick, Subs, Asks, Group} || Group <- Groups],
ItemsX ++ Res
end,
[],
Roster). Roster).
%%----------------------------- %%-----------------------------
%% Push Roster from file %% Push Roster from file
%%----------------------------- %%-----------------------------