2008-10-12 13:53:25 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_commands.erl
|
|
|
|
%%% Author : Badlop <badlop@process-one.net>
|
|
|
|
%%% Purpose : Management of ejabberd commands
|
|
|
|
%%% Created : 20 May 2008 by Badlop <badlop@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 ProcessOne
|
2008-10-12 13:53:25 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-10-12 13:53:25 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_commands).
|
|
|
|
-author('badlop@process-one.net').
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-define(DEFAULT_VERSION, 1000000).
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-export([start_link/0,
|
2008-10-12 13:53:25 +02:00
|
|
|
list_commands/0,
|
2016-03-31 13:53:31 +02:00
|
|
|
list_commands/1,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_format/1,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_format/2,
|
|
|
|
get_command_format/3,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_definition/1,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_definition/2,
|
2008-10-12 13:53:25 +02:00
|
|
|
get_tags_commands/0,
|
2016-03-31 13:53:31 +02:00
|
|
|
get_tags_commands/1,
|
2008-10-12 13:53:25 +02:00
|
|
|
register_commands/1,
|
2021-07-29 23:13:17 +02:00
|
|
|
register_commands/2,
|
2016-10-05 13:21:11 +02:00
|
|
|
unregister_commands/1,
|
|
|
|
get_commands_spec/0,
|
|
|
|
get_commands_definition/0,
|
|
|
|
get_commands_definition/1,
|
|
|
|
execute_command2/3,
|
|
|
|
execute_command2/4]).
|
2017-02-26 13:10:59 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2008-10-12 13:53:25 +02:00
|
|
|
|
|
|
|
-include("ejabberd_commands.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-03-31 13:53:31 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2015-10-19 19:16:04 +02:00
|
|
|
-define(POLICY_ACCESS, '$policy').
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-type auth() :: {binary(), binary(), binary() | {oauth, binary()}, boolean()} | map().
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
|
|
|
[
|
2015-12-28 12:19:49 +01:00
|
|
|
#ejabberd_commands{name = gen_html_doc_for_commands, tags = [documentation],
|
|
|
|
desc = "Generates html documentation for ejabberd_commands",
|
|
|
|
module = ejabberd_commands_doc, function = generate_html_output,
|
|
|
|
args = [{file, binary}, {regexp, binary}, {examples, binary}],
|
|
|
|
result = {res, rescode},
|
|
|
|
args_desc = ["Path to file where generated "
|
|
|
|
"documentation should be stored",
|
|
|
|
"Regexp matching names of commands or modules "
|
|
|
|
"that will be included inside generated document",
|
2018-01-27 17:35:38 +01:00
|
|
|
"Comma separated list of languages (chosen from java, perl, xmlrpc, json)"
|
2015-12-28 12:19:49 +01:00
|
|
|
"that will have example invocation include in markdown document"],
|
2018-01-27 17:35:38 +01:00
|
|
|
result_desc = "0 if command failed, 1 when succeeded",
|
2015-12-28 12:19:49 +01:00
|
|
|
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
|
|
|
|
result_example = ok},
|
|
|
|
#ejabberd_commands{name = gen_markdown_doc_for_commands, tags = [documentation],
|
|
|
|
desc = "Generates markdown documentation for ejabberd_commands",
|
|
|
|
module = ejabberd_commands_doc, function = generate_md_output,
|
|
|
|
args = [{file, binary}, {regexp, binary}, {examples, binary}],
|
|
|
|
result = {res, rescode},
|
|
|
|
args_desc = ["Path to file where generated "
|
|
|
|
"documentation should be stored",
|
|
|
|
"Regexp matching names of commands or modules "
|
|
|
|
"that will be included inside generated document",
|
2018-01-27 17:35:38 +01:00
|
|
|
"Comma separated list of languages (chosen from java, perl, xmlrpc, json)"
|
2015-12-28 12:19:49 +01:00
|
|
|
"that will have example invocation include in markdown document"],
|
2018-01-27 17:35:38 +01:00
|
|
|
result_desc = "0 if command failed, 1 when succeeded",
|
2015-12-28 12:19:49 +01:00
|
|
|
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
|
2021-08-21 20:50:02 +02:00
|
|
|
result_example = ok},
|
|
|
|
#ejabberd_commands{name = gen_markdown_doc_for_tags, tags = [documentation],
|
|
|
|
desc = "Generates markdown documentation for ejabberd_commands",
|
2021-12-02 16:43:17 +01:00
|
|
|
note = "added in 21.12",
|
2021-08-21 20:50:02 +02:00
|
|
|
module = ejabberd_commands_doc, function = generate_tags_md,
|
|
|
|
args = [{file, binary}],
|
|
|
|
result = {res, rescode},
|
|
|
|
args_desc = ["Path to file where generated "
|
|
|
|
"documentation should be stored"],
|
|
|
|
result_desc = "0 if command failed, 1 when succeeded",
|
|
|
|
args_example = ["/home/me/docs/tags.md"],
|
2016-01-26 10:00:11 +01:00
|
|
|
result_example = ok}].
|
2017-02-26 13:10:59 +01:00
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2016-11-19 11:05:13 +01:00
|
|
|
try mnesia:transform_table(ejabberd_commands, ignore,
|
|
|
|
record_info(fields, ejabberd_commands))
|
|
|
|
catch exit:{aborted, {no_exists, _}} -> ok
|
|
|
|
end,
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, ejabberd_commands,
|
2016-07-26 12:15:03 +02:00
|
|
|
[{ram_copies, [node()]},
|
2016-03-31 13:53:31 +02:00
|
|
|
{local_content, true},
|
2016-07-26 12:15:03 +02:00
|
|
|
{attributes, record_info(fields, ejabberd_commands)},
|
|
|
|
{type, bag}]),
|
2016-10-05 13:21:11 +02:00
|
|
|
register_commands(get_commands_spec()),
|
2017-02-26 13:10:59 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2017-02-26 13:10:59 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec register_commands([ejabberd_commands()]) -> ok.
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
register_commands(Commands) ->
|
2021-07-29 23:13:17 +02:00
|
|
|
register_commands(unknown, Commands).
|
|
|
|
|
|
|
|
register_commands(Definer, Commands) ->
|
2008-10-12 13:53:25 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Command) ->
|
2016-07-26 12:15:03 +02:00
|
|
|
%% XXX check if command exists
|
2021-07-29 23:13:17 +02:00
|
|
|
mnesia:dirty_write(Command#ejabberd_commands{definer = Definer})
|
2016-07-26 12:15:03 +02:00
|
|
|
%% ?DEBUG("This command is already defined:~n~p", [Command])
|
2008-10-12 13:53:25 +02:00
|
|
|
end,
|
2016-10-05 13:21:11 +02:00
|
|
|
Commands),
|
|
|
|
ejabberd_access_permissions:invalidate(),
|
|
|
|
ok.
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec unregister_commands([ejabberd_commands()]) -> ok.
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
unregister_commands(Commands) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Command) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
mnesia:dirty_delete_object(Command)
|
2008-10-12 13:53:25 +02:00
|
|
|
end,
|
2016-10-05 13:21:11 +02:00
|
|
|
Commands),
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_access_permissions:invalidate().
|
2016-07-26 12:15:03 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec list_commands() -> [{atom(), [aterm()], string()}].
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
list_commands() ->
|
2016-03-31 13:53:31 +02:00
|
|
|
list_commands(?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec list_commands(integer()) -> [{atom(), [aterm()], string()}].
|
|
|
|
|
|
|
|
list_commands(Version) ->
|
|
|
|
Commands = get_commands_definition(Version),
|
|
|
|
[{Name, Args, Desc} || #ejabberd_commands{name = Name,
|
2016-07-25 11:43:49 +02:00
|
|
|
args = Args,
|
|
|
|
desc = Desc} <- Commands].
|
2016-03-31 13:53:31 +02:00
|
|
|
|
2019-06-18 11:09:35 +02:00
|
|
|
-spec get_command_format(atom()) -> {[aterm()], [{atom(),atom()}], rterm()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_format(Name) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_format(Name, noauth, ?DEFAULT_VERSION).
|
|
|
|
get_command_format(Name, Version) when is_integer(Version) ->
|
|
|
|
get_command_format(Name, noauth, Version);
|
|
|
|
get_command_format(Name, Auth) ->
|
|
|
|
get_command_format(Name, Auth, ?DEFAULT_VERSION).
|
|
|
|
|
2019-06-18 11:09:35 +02:00
|
|
|
-spec get_command_format(atom(), noauth | admin | auth(), integer()) -> {[aterm()], [{atom(),atom()}], rterm()}.
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_format(Name, Auth, Version) ->
|
2016-05-25 13:01:07 +02:00
|
|
|
Admin = is_admin(Name, Auth, #{}),
|
2016-03-31 13:53:31 +02:00
|
|
|
#ejabberd_commands{args = Args,
|
|
|
|
result = Result,
|
2019-06-18 11:09:35 +02:00
|
|
|
args_rename = Rename,
|
2016-07-25 11:43:49 +02:00
|
|
|
policy = Policy} =
|
|
|
|
get_command_definition(Name, Version),
|
2016-03-31 13:53:31 +02:00
|
|
|
case Policy of
|
2016-07-25 11:43:49 +02:00
|
|
|
user when Admin;
|
|
|
|
Auth == noauth ->
|
2019-06-19 09:26:28 +02:00
|
|
|
{[{user, binary}, {host, binary} | Args], Rename, Result};
|
2016-07-25 11:43:49 +02:00
|
|
|
_ ->
|
2019-06-18 11:09:35 +02:00
|
|
|
{Args, Rename, Result}
|
2008-10-12 13:53:25 +02:00
|
|
|
end.
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-spec get_command_definition(atom()) -> ejabberd_commands().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
get_command_definition(Name) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_command_definition(Name, ?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec get_command_definition(atom(), integer()) -> ejabberd_commands().
|
|
|
|
|
|
|
|
get_command_definition(Name, Version) ->
|
|
|
|
case lists:reverse(
|
2016-07-25 11:43:49 +02:00
|
|
|
lists:sort(
|
|
|
|
mnesia:dirty_select(
|
|
|
|
ejabberd_commands,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_commands{name = N, version = V} = C)
|
|
|
|
when N == Name, V =< Version ->
|
|
|
|
{V, C}
|
|
|
|
end)))) of
|
|
|
|
[{_, Command} | _ ] -> Command;
|
2016-07-31 22:48:24 +02:00
|
|
|
_E -> throw({error, unknown_command})
|
2008-10-12 13:53:25 +02:00
|
|
|
end.
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
get_commands_definition() ->
|
|
|
|
get_commands_definition(?DEFAULT_VERSION).
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-spec get_commands_definition(integer()) -> [ejabberd_commands()].
|
|
|
|
|
|
|
|
get_commands_definition(Version) ->
|
|
|
|
L = lists:reverse(
|
2016-07-25 11:43:49 +02:00
|
|
|
lists:sort(
|
|
|
|
mnesia:dirty_select(
|
|
|
|
ejabberd_commands,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_commands{name = Name, version = V} = C)
|
|
|
|
when V =< Version ->
|
|
|
|
{Name, V, C}
|
|
|
|
end)))),
|
2016-03-31 13:53:31 +02:00
|
|
|
F = fun({_Name, _V, Command}, []) ->
|
2016-07-25 11:43:49 +02:00
|
|
|
[Command];
|
|
|
|
({Name, _V, _Command}, [#ejabberd_commands{name=Name}|_T] = Acc) ->
|
|
|
|
Acc;
|
|
|
|
({_Name, _V, Command}, Acc) -> [Command | Acc]
|
|
|
|
end,
|
2016-03-31 13:53:31 +02:00
|
|
|
lists:foldl(F, [], L).
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
execute_command2(Name, Arguments, CallerInfo) ->
|
2016-10-14 13:55:50 +02:00
|
|
|
execute_command2(Name, Arguments, CallerInfo, ?DEFAULT_VERSION).
|
2016-10-05 13:21:11 +02:00
|
|
|
|
|
|
|
execute_command2(Name, Arguments, CallerInfo, Version) ->
|
|
|
|
Command = get_command_definition(Name, Version),
|
|
|
|
case ejabberd_access_permissions:can_access(Name, CallerInfo) of
|
|
|
|
allow ->
|
|
|
|
do_execute_command(Command, Arguments);
|
|
|
|
_ ->
|
|
|
|
throw({error, access_rules_unauthorized})
|
|
|
|
end.
|
|
|
|
|
2016-07-23 17:57:44 +02:00
|
|
|
|
|
|
|
do_execute_command(Command, Arguments) ->
|
2008-10-12 13:53:25 +02:00
|
|
|
Module = Command#ejabberd_commands.module,
|
|
|
|
Function = Command#ejabberd_commands.function,
|
2009-10-08 19:22:48 +02:00
|
|
|
?DEBUG("Executing command ~p:~p with Args=~p", [Module, Function, Arguments]),
|
2018-12-11 11:07:07 +01:00
|
|
|
ejabberd_hooks:run(api_call, [Module, Function, Arguments]),
|
2016-03-31 13:53:31 +02:00
|
|
|
apply(Module, Function, Arguments).
|
2008-10-12 13:53:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_tags_commands() -> [{string(), [string()]}].
|
|
|
|
|
2008-10-12 13:53:25 +02:00
|
|
|
get_tags_commands() ->
|
2016-03-31 13:53:31 +02:00
|
|
|
get_tags_commands(?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
-spec get_tags_commands(integer()) -> [{string(), [string()]}].
|
|
|
|
|
|
|
|
get_tags_commands(Version) ->
|
|
|
|
CommandTags = [{Name, Tags} ||
|
|
|
|
#ejabberd_commands{name = Name, tags = Tags}
|
|
|
|
<- get_commands_definition(Version)],
|
2008-10-12 13:53:25 +02:00
|
|
|
Dict = lists:foldl(
|
2016-03-31 13:53:31 +02:00
|
|
|
fun({CommandNameAtom, CTags}, D) ->
|
2008-10-12 13:53:25 +02:00
|
|
|
CommandName = atom_to_list(CommandNameAtom),
|
|
|
|
case CTags of
|
|
|
|
[] ->
|
|
|
|
orddict:append("untagged", CommandName, D);
|
|
|
|
_ ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(TagAtom, DD) ->
|
|
|
|
Tag = atom_to_list(TagAtom),
|
|
|
|
orddict:append(Tag, CommandName, DD)
|
|
|
|
end,
|
|
|
|
D,
|
|
|
|
CTags)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
orddict:new(),
|
|
|
|
CommandTags),
|
|
|
|
orddict:to_list(Dict).
|
2009-04-17 15:43:15 +02:00
|
|
|
|
|
|
|
%% -----------------------------
|
|
|
|
%% Access verification
|
|
|
|
%% -----------------------------
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec is_admin(atom(), admin | noauth | auth(), map()) -> boolean().
|
2016-05-25 13:01:07 +02:00
|
|
|
is_admin(_Name, admin, _Extra) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
true;
|
2016-05-25 13:01:07 +02:00
|
|
|
is_admin(_Name, {_User, _Server, _, false}, _Extra) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
false;
|
2016-10-05 13:21:11 +02:00
|
|
|
is_admin(_Name, Map, _extra) when is_map(Map) ->
|
|
|
|
true;
|
2019-06-14 11:33:26 +02:00
|
|
|
is_admin(_Name, _Auth, _Extra) ->
|
|
|
|
false.
|