Skip to content

Commit

Permalink
Refactored hook handlers in ejabberd_ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlooss1 committed Dec 2, 2022
1 parent 83ed0f6 commit c869795
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 79 deletions.
1 change: 0 additions & 1 deletion src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ start(normal, _Args) ->
mongoose_graphql:init(),
translate:start(),
ejabberd_node_id:start(),
ejabberd_ctl:init(),
ejabberd_commands:init(),
mongoose_graphql_commands:start(),
mongoose_config:start(),
Expand Down
70 changes: 7 additions & 63 deletions src/ejabberd_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@
-author('[email protected]').

-export([start/0,
init/0,
process/1,
process2/2,
register_commands/3,
unregister_commands/3]).
process2/2]).

-ignore_xref([process/1, process2/2, register_commands/3, start/0, unregister_commands/3]).
-ignore_xref([process/1, process2/2, start/0]).

-include("ejabberd_ctl.hrl").
-include("ejabberd_commands.hrl").
-include("mongoose_logger.hrl").

-type format() :: integer | string | binary | {list, format()}.
-type format_type() :: binary() | string() | char().
Expand Down Expand Up @@ -105,37 +103,6 @@ start() ->
halt(?STATUS_USAGE)
end.


-spec init() -> atom() | ets:tid().
init() ->
ets:new(ejabberd_ctl_cmds, [named_table, set, public]),
ets:new(ejabberd_ctl_host_cmds, [named_table, set, public]).


%%-----------------------------
%% mongooseimctl Command managment
%%-----------------------------

-spec register_commands(CmdDescs :: [tuple()] | tuple(),
Module :: atom(),
Function :: atom()) -> 'ok'.
register_commands(CmdDescs, Module, Function) ->
ets:insert(ejabberd_ctl_cmds, CmdDescs),
ejabberd_hooks:add(ejabberd_ctl_process, global, Module, Function, 50),
ok.


-spec unregister_commands(CmdDescs :: [any()],
Module :: atom(),
Function :: atom()) -> 'ok'.
unregister_commands(CmdDescs, Module, Function) ->
lists:foreach(fun(CmdDesc) ->
ets:delete_object(ejabberd_ctl_cmds, CmdDesc)
end, CmdDescs),
ejabberd_hooks:delete(ejabberd_ctl_process, global, Module, Function, 50),
ok.


%%-----------------------------
%% Process
%%-----------------------------
Expand Down Expand Up @@ -276,7 +243,7 @@ process2(Args, AccessCommands) ->

%% @private
process2(Args, Auth, AccessCommands) ->
case try_run_ctp(Args, Auth, AccessCommands) of
case try_call_command(Args, Auth, AccessCommands) of
{String, wrong_command_arguments} when is_list(String) ->
io:format(lists:flatten(["\n" | String]++["\n"])),
[CommandString | _] = Args,
Expand All @@ -299,36 +266,13 @@ get_accesscommands() ->
%%-----------------------------
%% Command calling
%%-----------------------------

-spec try_run_ctp(Args :: [string()],
Auth :: ejabberd_commands:auth(),
AccessCommands :: ejabberd_commands:access_commands()
) -> string() | integer() | {string(), integer()} | {string(), wrong_command_arguments}.
try_run_ctp(Args, Auth, AccessCommands) ->
try mongoose_hooks:ejabberd_ctl_process(false, Args) of
false when Args /= [] ->
try_call_command(Args, Auth, AccessCommands);
false ->
print_usage(),
{"", ?STATUS_USAGE};
Status ->
{"", Status}
catch
exit:Why ->
print_usage(),
{io_lib:format("Error in mongooseimctl process: ~p", [Why]), ?STATUS_USAGE};
Error:Why ->
%% In this case probably ejabberd is not started, so let's show Status
process(["status"]),
?PRINT("~n", []),
{io_lib:format("Error in mongooseimctl process: '~p' ~p", [Error, Why]), ?STATUS_USAGE}
end.


-spec try_call_command(Args :: [string()],
Auth :: ejabberd_commands:auth(),
AccessCommands :: ejabberd_commands:access_commands()
) -> string() | integer() | {string(), integer()} | {string(), wrong_command_arguments}.
try_call_command([], _, _) ->
print_usage(),
{"", ?STATUS_USAGE};
try_call_command(Args, Auth, AccessCommands) ->
try call_command(Args, Auth, AccessCommands) of
{error, command_unknown} ->
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

-ignore_xref([add/4, delete/4, error_running_hook/3, start_link/0,
% temporary until the module is deleted
add/1, delete/1]).
add/1, delete/1, add/5, delete/5]).

-include("mongoose.hrl").

Expand Down
6 changes: 5 additions & 1 deletion src/gen_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@

-type hook_list() :: [hook_tuple()].

-export_type([hook_fn/0, hook_list/0, hook_fn_ret/0, hook_fn_ret/1]).
-export_type([hook_fn/0,
hook_list/0,
hook_fn_ret/0,
hook_fn_ret/1,
extra/0]).

-record(hook_handler, {prio :: pos_integer(),
hook_fn :: hook_fn(),
Expand Down
17 changes: 13 additions & 4 deletions src/mod_roster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,16 @@ create_sub_el(Items, Version) ->
Params :: #{jid := jid:jid()},
Extra :: gen_hook:extra().
get_user_roster(Acc, #{jid := #jid{luser = LUser, lserver = LServer}}, #{host_type := HostType}) ->
NewAcc = case mongoose_acc:get(roster, show_full_roster, false, Acc) of
NewAcc = get_user_roster(Acc, LUser, LServer, HostType),
{ok, NewAcc}.

-spec get_user_roster(mongoose_acc:t(),
jid:luser(),
jid:lserver(),
mongooseim:host_type()
) -> mongoose_acc:t().
get_user_roster(Acc, LUser, LServer, HostType) ->
case mongoose_acc:get(roster, show_full_roster, false, Acc) of
true ->
Roster = get_roster(HostType, LUser, LServer),
mongoose_acc:append(roster, items, Roster, Acc);
Expand All @@ -345,8 +354,7 @@ get_user_roster(Acc, #{jid := #jid{luser = LUser, lserver = LServer}}, #{host_ty
true
end, get_roster(HostType, LUser, LServer)),
mongoose_acc:append(roster, items, Roster, Acc)
end,
{ok, NewAcc}.
end.

item_to_xml(Item) ->
Attrs1 = [{<<"jid">>,
Expand Down Expand Up @@ -826,7 +834,8 @@ try_send_unsubscription_to_rosteritems(Acc, JID) ->
%% Both or To, send a "unsubscribe" presence stanza.
-spec send_unsubscription_to_rosteritems(mongoose_acc:t(), jid:jid()) -> mongoose_acc:t().
send_unsubscription_to_rosteritems(Acc, JID) ->
{ok, Acc1} = get_user_roster(Acc, #{jid => JID}, #{host_type => mongoose_acc:host_type(Acc)}),
#jid{luser = LUser, lserver = LServer} = JID,
Acc1 = get_user_roster(Acc, LUser, LServer, mongoose_acc:host_type(Acc)),
RosterItems = mongoose_acc:get(roster, items, [], Acc1),
lists:foreach(fun(RosterItem) ->
send_unsubscribing_presence(JID, RosterItem)
Expand Down
8 changes: 0 additions & 8 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
anonymous_purge_hook/3,
auth_failed/3,
does_user_exist/3,
ejabberd_ctl_process/2,
failed_to_store_message/1,
filter_local_packet/1,
filter_packet/1,
Expand Down Expand Up @@ -260,13 +259,6 @@ node_cleanup(Node) ->
ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args),
run_global_hook(node_cleanup, #{}, ParamsWithLegacyArgs).

-spec ejabberd_ctl_process(Acc, Args) -> Result when
Acc :: any(),
Args :: [string()],
Result :: any().
ejabberd_ctl_process(Acc, Args) ->
run_global_hook(ejabberd_ctl_process, Acc, [Args]).

-spec failed_to_store_message(Acc) -> Result when
Acc :: mongoose_acc:t(),
Result :: mongoose_acc:t().
Expand Down
2 changes: 1 addition & 1 deletion src/vcard/mod_vcard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ process_local_iq(Acc, _From, _To, IQ = #iq{type = get}, _Extra) ->
From :: jid:jid(),
To :: jid:jid(),
IQ :: jlib:iq(),
Extra :: gen_hook:extra()) ->
Extra :: map()) ->
{stop, mongoose_acc:t()} | {mongoose_acc:t(), jlib:iq()}.
process_sm_iq(Acc, From, To, IQ = #iq{type = set, sub_el = VCARD}, _Extra) ->
HostType = mongoose_acc:host_type(Acc),
Expand Down

0 comments on commit c869795

Please sign in to comment.