2004-01-11 21:42:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_ctl.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2008-10-12 13:58:30 +02:00
|
|
|
%%% Purpose : ejabberd command line admin tool
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 11 Jan 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 12:41:41 +01: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.
|
2008-07-18 17:55:39 +02:00
|
|
|
%%%
|
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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2004-01-11 21:42:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%%% @headerfile "ejabberd_ctl.hrl"
|
|
|
|
|
|
|
|
%%% @doc Management of ejabberdctl commands and frontend to ejabberd commands.
|
|
|
|
%%%
|
|
|
|
%%% An ejabberdctl command is an abstract function identified by a
|
|
|
|
%%% name, with a defined number of calling arguments, that can be
|
|
|
|
%%% defined in any Erlang module and executed using ejabberdctl
|
|
|
|
%%% administration script.
|
|
|
|
%%%
|
|
|
|
%%% Note: strings cannot have blankspaces
|
|
|
|
%%%
|
|
|
|
%%% Does not support commands that have arguments with ctypes: list, tuple
|
|
|
|
%%%
|
|
|
|
%%% TODO: Update the guide
|
|
|
|
%%% TODO: Mention this in the release notes
|
|
|
|
%%% Note: the commands with several words use now the underline: _
|
|
|
|
%%% It is still possible to call the commands with dash: -
|
|
|
|
%%% but this is deprecated, and may be removed in a future version.
|
|
|
|
|
|
|
|
|
2004-01-11 21:42:57 +01:00
|
|
|
-module(ejabberd_ctl).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-export([start/0, start_link/0, process/1, process2/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
register_commands/3, unregister_commands/3]).
|
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]).
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2006-02-13 05:02:59 +01:00
|
|
|
-include("ejabberd_ctl.hrl").
|
2008-10-12 13:58:30 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
-define(DEFAULT_VERSION, 1000000).
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Module
|
|
|
|
%%-----------------------------
|
|
|
|
|
2004-01-11 21:42:57 +01:00
|
|
|
start() ->
|
2019-10-25 14:27:47 +02:00
|
|
|
disable_logging(),
|
2015-12-08 11:25:26 +01:00
|
|
|
[SNode, Timeout, Args] = case init:get_plain_arguments() of
|
|
|
|
[SNode2, "--no-timeout" | Args2] ->
|
|
|
|
[SNode2, infinity, Args2];
|
|
|
|
[SNode3 | Args3] ->
|
2015-12-08 11:28:03 +01:00
|
|
|
[SNode3, 60000, Args3];
|
2015-12-08 11:25:26 +01:00
|
|
|
_ ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(?DEFAULT_VERSION),
|
2015-12-08 11:25:26 +01:00
|
|
|
halt(?STATUS_USAGE)
|
|
|
|
end,
|
|
|
|
SNode1 = case string:tokens(SNode, "@") of
|
|
|
|
[_Node, _Server] ->
|
|
|
|
SNode;
|
|
|
|
_ ->
|
|
|
|
case net_kernel:longnames() of
|
|
|
|
true ->
|
|
|
|
lists:flatten([SNode, "@", inet_db:gethostname(),
|
|
|
|
".", inet_db:res_option(domain)]);
|
|
|
|
false ->
|
|
|
|
lists:flatten([SNode, "@", inet_db:gethostname()]);
|
|
|
|
_ ->
|
|
|
|
SNode
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Node = list_to_atom(SNode1),
|
2019-07-12 11:30:38 +02:00
|
|
|
Status = case ejabberd_cluster:call(Node, ?MODULE, process, [Args], Timeout) of
|
2015-12-08 11:25:26 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
print("Failed RPC connection to the node ~p: ~p~n",
|
|
|
|
[Node, Reason]),
|
|
|
|
%% TODO: show minimal start help
|
|
|
|
?STATUS_BADRPC;
|
2016-03-31 13:53:31 +02:00
|
|
|
{invalid_version, V} ->
|
|
|
|
print("Invalid API version number: ~p~n", [V]),
|
|
|
|
?STATUS_ERROR;
|
2015-12-08 11:25:26 +01:00
|
|
|
S ->
|
|
|
|
S
|
|
|
|
end,
|
|
|
|
halt(Status).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2006-02-20 05:07:42 +01:00
|
|
|
ets:new(ejabberd_ctl_cmds, [named_table, set, public]),
|
2017-02-26 13:10:59 +01:00
|
|
|
ets:new(ejabberd_ctl_host_cmds, [named_table, set, public]),
|
|
|
|
{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.
|
2006-02-20 05:07:42 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%%-----------------------------
|
2018-01-27 17:35:38 +01:00
|
|
|
%% ejabberdctl Command management
|
2008-10-12 13:58:30 +02:00
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
register_commands(CmdDescs, Module, Function) ->
|
|
|
|
ets:insert(ejabberd_ctl_cmds, CmdDescs),
|
|
|
|
ejabberd_hooks:add(ejabberd_ctl_process,
|
|
|
|
Module, Function, 50),
|
|
|
|
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,
|
|
|
|
Module, Function, 50),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Process
|
|
|
|
%%-----------------------------
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec process([string()]) -> non_neg_integer().
|
2016-03-31 13:53:31 +02:00
|
|
|
process(Args) ->
|
|
|
|
process(Args, ?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
|
|
|
|
-spec process([string()], non_neg_integer()) -> non_neg_integer().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%% The commands status, stop and restart are defined here to ensure
|
|
|
|
%% they are usable even if ejabberd is completely stopped.
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["status"], _Version) ->
|
2006-02-13 05:02:59 +01:00
|
|
|
{InternalStatus, ProvidedStatus} = init:get_status(),
|
2013-12-10 08:40:43 +01:00
|
|
|
print("The node ~p is ~p with status: ~p~n",
|
2008-10-12 13:58:30 +02:00
|
|
|
[node(), InternalStatus, ProvidedStatus]),
|
2018-07-14 20:27:30 +02:00
|
|
|
case lists:keymember(ejabberd, 1, application:which_applications()) of
|
2006-04-27 00:28:05 +02:00
|
|
|
false ->
|
2013-06-27 10:58:56 +02:00
|
|
|
EjabberdLogPath = ejabberd_logger:get_log_path(),
|
2013-12-10 08:40:43 +01:00
|
|
|
print("ejabberd is not running in that node~n"
|
2019-09-23 14:17:20 +02:00
|
|
|
"Check for error messages: ~ts~n"
|
2008-10-25 00:11:42 +02:00
|
|
|
"or other files in that directory.~n", [EjabberdLogPath]),
|
2006-04-27 00:28:05 +02:00
|
|
|
?STATUS_ERROR;
|
2018-07-14 20:27:30 +02:00
|
|
|
true ->
|
2019-09-23 14:17:20 +02:00
|
|
|
print("ejabberd ~ts is running in that node~n", [ejabberd_option:version()]),
|
2006-04-27 00:28:05 +02:00
|
|
|
?STATUS_SUCCESS
|
|
|
|
end;
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["stop"], _Version) ->
|
2009-05-08 00:46:51 +02:00
|
|
|
%%ejabberd_cover:stop(),
|
2006-02-13 05:02:59 +01:00
|
|
|
init:stop(),
|
|
|
|
?STATUS_SUCCESS;
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["restart"], _Version) ->
|
2006-02-13 05:02:59 +01:00
|
|
|
init:restart(),
|
|
|
|
?STATUS_SUCCESS;
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2019-05-03 14:59:24 +02:00
|
|
|
%% TODO: Mnesia operations should not be hardcoded in ejabberd_ctl module.
|
|
|
|
%% For now, I leave them there to avoid breaking those commands for people that
|
|
|
|
%% may be using it (as format of response is going to change).
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["mnesia"], _Version) ->
|
2013-12-10 08:40:43 +01:00
|
|
|
print("~p~n", [mnesia:system_info(all)]),
|
2007-12-21 22:50:22 +01:00
|
|
|
?STATUS_SUCCESS;
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["mnesia", "info"], _Version) ->
|
2007-12-21 22:50:22 +01:00
|
|
|
mnesia:info(),
|
|
|
|
?STATUS_SUCCESS;
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["mnesia", Arg], _Version) ->
|
2007-12-21 22:50:22 +01:00
|
|
|
case catch mnesia:system_info(list_to_atom(Arg)) of
|
2013-12-10 08:40:43 +01:00
|
|
|
{'EXIT', Error} -> print("Error: ~p~n", [Error]);
|
|
|
|
Return -> print("~p~n", [Return])
|
2007-12-21 22:50:22 +01:00
|
|
|
end,
|
|
|
|
?STATUS_SUCCESS;
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%% The arguments --long and --dual are not documented because they are
|
|
|
|
%% automatically selected depending in the number of columns of the shell
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["help" | Mode], Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{MaxC, ShCode} = get_shell_info(),
|
|
|
|
case Mode of
|
|
|
|
[] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(dual, MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
?STATUS_USAGE;
|
|
|
|
["--dual"] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(dual, MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
?STATUS_USAGE;
|
|
|
|
["--long"] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(long, MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
?STATUS_USAGE;
|
|
|
|
["--tags"] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_tags(MaxC, ShCode, Version),
|
2006-05-07 23:26:06 +02:00
|
|
|
?STATUS_SUCCESS;
|
2008-10-12 13:58:30 +02:00
|
|
|
["--tags", Tag] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_tags(Tag, MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
?STATUS_SUCCESS;
|
|
|
|
["help"] ->
|
|
|
|
print_usage_help(MaxC, ShCode),
|
|
|
|
?STATUS_SUCCESS;
|
2010-01-25 19:32:45 +01:00
|
|
|
[CmdString | _] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
CmdStringU = ejabberd_regexp:greplace(
|
|
|
|
list_to_binary(CmdString), <<"-">>, <<"_">>),
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_commands2(binary_to_list(CmdStringU), MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
?STATUS_SUCCESS
|
2006-02-20 05:07:42 +01:00
|
|
|
end;
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["--version", Arg | Args], _) ->
|
2016-07-30 13:08:30 +02:00
|
|
|
Version =
|
2016-03-31 13:53:31 +02:00
|
|
|
try
|
|
|
|
list_to_integer(Arg)
|
|
|
|
catch _:_ ->
|
|
|
|
throw({invalid_version, Arg})
|
|
|
|
end,
|
|
|
|
process(Args, Version);
|
|
|
|
|
|
|
|
process(Args, Version) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{String, Code} = process2(Args, [], Version),
|
2009-12-11 21:04:36 +01:00
|
|
|
case String of
|
|
|
|
[] -> ok;
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io:format("~ts~n", [String])
|
2009-12-11 21:04:36 +01:00
|
|
|
end,
|
2008-10-12 13:58:30 +02:00
|
|
|
Code.
|
|
|
|
|
2009-04-17 15:48:59 +02:00
|
|
|
%% @spec (Args::[string()], AccessCommands) -> {String::string(), Code::integer()}
|
2016-04-11 17:21:44 +02:00
|
|
|
process2(Args, AccessCommands) ->
|
|
|
|
process2(Args, AccessCommands, ?DEFAULT_VERSION).
|
|
|
|
|
|
|
|
%% @spec (Args::[string()], AccessCommands, Version) -> {String::string(), Code::integer()}
|
2016-03-31 13:53:31 +02:00
|
|
|
process2(["--auth", User, Server, Pass | Args], AccessCommands, Version) ->
|
|
|
|
process2(Args, AccessCommands, {list_to_binary(User), list_to_binary(Server),
|
|
|
|
list_to_binary(Pass), true}, Version);
|
|
|
|
process2(Args, AccessCommands, Version) ->
|
2016-04-06 12:43:07 +02:00
|
|
|
process2(Args, AccessCommands, noauth, Version).
|
2016-03-31 13:53:31 +02:00
|
|
|
|
|
|
|
|
2009-04-17 15:48:59 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
process2(Args, AccessCommands, Auth, Version) ->
|
|
|
|
case try_run_ctp(Args, Auth, AccessCommands, Version) of
|
2008-10-12 13:58:30 +02:00
|
|
|
{String, wrong_command_arguments}
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_list(String) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
io:format(lists:flatten(["\n" | String]++["\n"])),
|
|
|
|
[CommandString | _] = Args,
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["help" | [CommandString]], Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
{lists:flatten(String), ?STATUS_ERROR};
|
|
|
|
{String, Code}
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_list(String) and is_integer(Code) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{lists:flatten(String), Code};
|
|
|
|
String
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_list(String) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{lists:flatten(String), ?STATUS_SUCCESS};
|
|
|
|
Code
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_integer(Code) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{"", Code};
|
|
|
|
Other ->
|
|
|
|
{"Erroneous result: " ++ io_lib:format("~p", [Other]), ?STATUS_ERROR}
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Command calling
|
|
|
|
%%-----------------------------
|
|
|
|
|
2009-04-17 15:48:59 +02:00
|
|
|
%% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()}
|
2016-03-31 13:53:31 +02:00
|
|
|
try_run_ctp(Args, Auth, AccessCommands, Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
try ejabberd_hooks:run_fold(ejabberd_ctl_process, false, [Args]) of
|
|
|
|
false when Args /= [] ->
|
2016-03-31 13:53:31 +02:00
|
|
|
try_call_command(Args, Auth, AccessCommands, Version);
|
2006-02-15 05:15:54 +01:00
|
|
|
false ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
{"", ?STATUS_USAGE};
|
2006-02-15 05:15:54 +01:00
|
|
|
Status ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{"", Status}
|
|
|
|
catch
|
|
|
|
exit:Why ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(Version),
|
2008-10-25 00:11:42 +02:00
|
|
|
{io_lib:format("Error in ejabberd ctl process: ~p", [Why]), ?STATUS_USAGE};
|
|
|
|
Error:Why ->
|
|
|
|
%% In this case probably ejabberd is not started, so let's show Status
|
2016-03-31 13:53:31 +02:00
|
|
|
process(["status"], Version),
|
2013-12-10 08:40:43 +01:00
|
|
|
print("~n", []),
|
2008-10-25 00:11:42 +02:00
|
|
|
{io_lib:format("Error in ejabberd ctl process: '~p' ~p", [Error, Why]), ?STATUS_USAGE}
|
2006-02-15 05:15:54 +01:00
|
|
|
end.
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2009-04-17 15:48:59 +02:00
|
|
|
%% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()}
|
2016-03-31 13:53:31 +02:00
|
|
|
try_call_command(Args, Auth, AccessCommands, Version) ->
|
|
|
|
try call_command(Args, Auth, AccessCommands, Version) of
|
2019-06-14 11:33:26 +02:00
|
|
|
{Reason, wrong_command_arguments} ->
|
|
|
|
{Reason, ?STATUS_ERROR};
|
2008-10-12 13:58:30 +02:00
|
|
|
Res ->
|
|
|
|
Res
|
|
|
|
catch
|
2019-04-30 09:05:06 +02:00
|
|
|
throw:{error, unknown_command} ->
|
|
|
|
KnownCommands = [Cmd || {Cmd, _, _} <- ejabberd_commands:list_commands(Version)],
|
|
|
|
UnknownCommand = list_to_atom(hd(Args)),
|
|
|
|
{io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Error: unknown command '~ts'. Did you mean '~ts'?",
|
2019-04-30 09:05:06 +02:00
|
|
|
[hd(Args), misc:best_match(UnknownCommand, KnownCommands)]),
|
|
|
|
?STATUS_ERROR};
|
2016-03-31 13:53:31 +02:00
|
|
|
throw:Error ->
|
|
|
|
{io_lib:format("~p", [Error]), ?STATUS_ERROR};
|
2018-12-13 11:45:45 +01:00
|
|
|
?EX_RULE(A, Why, Stack) ->
|
2019-07-08 22:14:31 +02:00
|
|
|
StackTrace = ?EX_STACK(Stack),
|
2019-09-23 14:17:20 +02:00
|
|
|
{io_lib:format("Unhandled exception occurred executing the command:~n** ~ts",
|
2019-07-08 22:14:31 +02:00
|
|
|
[misc:format_exception(2, A, Why, StackTrace)]),
|
|
|
|
?STATUS_ERROR}
|
2008-10-12 13:58:30 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-17 19:16:43 +02:00
|
|
|
%% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()} | {error, ErrorType}
|
2016-11-25 07:48:26 +01:00
|
|
|
call_command([CmdString | Args], Auth, _AccessCommands, Version) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
CmdStringU = ejabberd_regexp:greplace(
|
|
|
|
list_to_binary(CmdString), <<"-">>, <<"_">>),
|
|
|
|
Command = list_to_atom(binary_to_list(CmdStringU)),
|
2019-06-18 11:09:35 +02:00
|
|
|
{ArgsFormat, _, ResultFormat} = ejabberd_commands:get_command_format(Command, Auth, Version),
|
2019-06-14 11:33:26 +02:00
|
|
|
case (catch format_args(Args, ArgsFormat)) of
|
|
|
|
ArgsFormatted when is_list(ArgsFormatted) ->
|
|
|
|
CI = case Auth of
|
|
|
|
{U, S, _, _} -> #{usr => {U, S, <<"">>}, caller_host => S};
|
|
|
|
_ -> #{}
|
|
|
|
end,
|
|
|
|
CI2 = CI#{caller_module => ?MODULE},
|
|
|
|
Result = ejabberd_commands:execute_command2(Command,
|
|
|
|
ArgsFormatted,
|
|
|
|
CI2,
|
|
|
|
Version),
|
|
|
|
format_result(Result, ResultFormat);
|
|
|
|
{'EXIT', {function_clause,[{lists,zip,[A1, A2], _} | _]}} ->
|
|
|
|
{NumCompa, TextCompa} =
|
|
|
|
case {length(A1), length(A2)} of
|
|
|
|
{L1, L2} when L1 < L2 -> {L2-L1, "less argument"};
|
|
|
|
{L1, L2} when L1 > L2 -> {L1-L2, "more argument"}
|
|
|
|
end,
|
2019-09-23 14:17:20 +02:00
|
|
|
{io_lib:format("Error: the command ~p requires ~p ~ts.",
|
2019-06-14 11:33:26 +02:00
|
|
|
[CmdString, NumCompa, TextCompa]),
|
|
|
|
wrong_command_arguments}
|
2008-10-12 13:58:30 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Format arguments
|
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
format_args(Args, ArgsFormat) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({{_ArgName, ArgFormat}, Arg}, Res) ->
|
|
|
|
Formatted = format_arg(Arg, ArgFormat),
|
|
|
|
Res ++ [Formatted]
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
lists:zip(ArgsFormat, Args)).
|
|
|
|
|
2009-12-28 12:45:46 +01:00
|
|
|
format_arg(Arg, integer) ->
|
|
|
|
format_arg2(Arg, "~d");
|
2013-03-14 10:33:02 +01:00
|
|
|
format_arg(Arg, binary) ->
|
2016-02-12 23:37:48 +01:00
|
|
|
unicode:characters_to_binary(Arg, utf8);
|
2009-12-28 12:45:46 +01:00
|
|
|
format_arg("", string) ->
|
|
|
|
"";
|
|
|
|
format_arg(Arg, string) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NumChars = integer_to_list(length(Arg)),
|
2009-12-28 12:45:46 +01:00
|
|
|
Parse = "~" ++ NumChars ++ "c",
|
|
|
|
format_arg2(Arg, Parse).
|
|
|
|
|
|
|
|
format_arg2(Arg, Parse)->
|
2008-10-12 13:58:30 +02:00
|
|
|
{ok, [Arg2], _RemainingArguments} = io_lib:fread(Parse, Arg),
|
|
|
|
Arg2.
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Format result
|
|
|
|
%%-----------------------------
|
|
|
|
|
2009-04-17 15:48:59 +02:00
|
|
|
format_result({error, ErrorAtom}, _) ->
|
|
|
|
{io_lib:format("Error: ~p", [ErrorAtom]), make_status(error)};
|
|
|
|
|
2016-07-30 13:08:30 +02:00
|
|
|
%% An error should always be allowed to return extended error to help with API.
|
|
|
|
%% Extended error is of the form:
|
|
|
|
%% {error, type :: atom(), code :: int(), Desc :: string()}
|
|
|
|
format_result({error, ErrorAtom, Code, _Msg}, _) ->
|
|
|
|
{io_lib:format("Error: ~p", [ErrorAtom]), make_status(Code)};
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
format_result(Atom, {_Name, atom}) ->
|
|
|
|
io_lib:format("~p", [Atom]);
|
|
|
|
|
|
|
|
format_result(Int, {_Name, integer}) ->
|
|
|
|
io_lib:format("~p", [Int]);
|
|
|
|
|
2016-02-17 20:43:35 +01:00
|
|
|
format_result([A|_]=String, {_Name, string}) when is_list(String) and is_integer(A) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("~ts", [String]);
|
2008-10-12 13:58:30 +02:00
|
|
|
|
2013-06-14 16:08:47 +02:00
|
|
|
format_result(Binary, {_Name, string}) when is_binary(Binary) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("~ts", [binary_to_list(Binary)]);
|
2013-06-14 16:08:47 +02:00
|
|
|
|
2015-05-15 17:47:10 +02:00
|
|
|
format_result(Atom, {_Name, string}) when is_atom(Atom) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("~ts", [atom_to_list(Atom)]);
|
2015-05-15 17:47:10 +02:00
|
|
|
|
|
|
|
format_result(Integer, {_Name, string}) when is_integer(Integer) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("~ts", [integer_to_list(Integer)]);
|
2015-05-15 17:47:10 +02:00
|
|
|
|
|
|
|
format_result(Other, {_Name, string}) ->
|
|
|
|
io_lib:format("~p", [Other]);
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
format_result(Code, {_Name, rescode}) ->
|
|
|
|
make_status(Code);
|
|
|
|
|
|
|
|
format_result({Code, Text}, {_Name, restuple}) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
{io_lib:format("~ts", [Text]), make_status(Code)};
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
%% The result is a list of something: [something()]
|
|
|
|
format_result([], {_Name, {list, _ElementsDef}}) ->
|
|
|
|
"";
|
|
|
|
format_result([FirstElement | Elements], {_Name, {list, ElementsDef}}) ->
|
|
|
|
%% Start formatting the first element
|
|
|
|
[format_result(FirstElement, ElementsDef) |
|
|
|
|
%% If there are more elements, put always first a newline character
|
|
|
|
lists:map(
|
|
|
|
fun(Element) ->
|
|
|
|
["\n" | format_result(Element, ElementsDef)]
|
|
|
|
end,
|
|
|
|
Elements)];
|
|
|
|
|
|
|
|
%% The result is a tuple with several elements: {something1(), something2(),...}
|
|
|
|
%% NOTE: the elements in the tuple are separated with tabular characters,
|
|
|
|
%% if a string is empty, it will be difficult to notice in the shell,
|
|
|
|
%% maybe a different separation character should be used, like ;;?
|
|
|
|
format_result(ElementsTuple, {_Name, {tuple, ElementsDef}}) ->
|
|
|
|
ElementsList = tuple_to_list(ElementsTuple),
|
|
|
|
[{FirstE, FirstD} | ElementsAndDef] = lists:zip(ElementsList, ElementsDef),
|
|
|
|
[format_result(FirstE, FirstD) |
|
|
|
|
lists:map(
|
|
|
|
fun({Element, ElementDef}) ->
|
|
|
|
["\t" | format_result(Element, ElementDef)]
|
|
|
|
end,
|
2015-10-07 00:06:58 +02:00
|
|
|
ElementsAndDef)];
|
|
|
|
|
|
|
|
format_result(404, {_Name, _}) ->
|
|
|
|
make_status(not_found).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
make_status(ok) -> ?STATUS_SUCCESS;
|
|
|
|
make_status(true) -> ?STATUS_SUCCESS;
|
2016-07-30 13:18:39 +02:00
|
|
|
make_status(Code) when is_integer(Code), Code > 255 -> ?STATUS_ERROR;
|
|
|
|
make_status(Code) when is_integer(Code), Code > 0 -> Code;
|
2008-10-12 13:58:30 +02:00
|
|
|
make_status(_Error) -> ?STATUS_ERROR.
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
get_list_commands(Version) ->
|
|
|
|
try ejabberd_commands:list_commands(Version) of
|
2008-10-12 13:58:30 +02:00
|
|
|
Commands ->
|
|
|
|
[tuple_command_help(Command)
|
|
|
|
|| {N,_,_}=Command <- Commands,
|
|
|
|
%% Don't show again those commands, because they are already
|
|
|
|
%% announced by ejabberd_ctl itself
|
|
|
|
N /= status, N /= stop, N /= restart]
|
|
|
|
catch
|
|
|
|
exit:_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% Return: {string(), [string()], string()}
|
2015-09-25 14:53:25 +02:00
|
|
|
tuple_command_help({Name, _Args, Desc}) ->
|
2019-06-18 11:09:35 +02:00
|
|
|
{Args, _, _} = ejabberd_commands:get_command_format(Name, admin),
|
2008-10-12 13:58:30 +02:00
|
|
|
Arguments = [atom_to_list(ArgN) || {ArgN, _ArgF} <- Args],
|
|
|
|
Prepend = case is_supported_args(Args) of
|
|
|
|
true -> "";
|
|
|
|
false -> "*"
|
|
|
|
end,
|
|
|
|
CallString = atom_to_list(Name),
|
|
|
|
{CallString, Arguments, Prepend ++ Desc}.
|
|
|
|
|
|
|
|
is_supported_args(Args) ->
|
|
|
|
lists:all(
|
|
|
|
fun({_Name, Format}) ->
|
|
|
|
(Format == integer)
|
|
|
|
or (Format == string)
|
2014-04-29 01:11:08 +02:00
|
|
|
or (Format == binary)
|
2008-10-12 13:58:30 +02:00
|
|
|
end,
|
|
|
|
Args).
|
|
|
|
|
|
|
|
get_list_ctls() ->
|
|
|
|
case catch ets:tab2list(ejabberd_ctl_cmds) of
|
|
|
|
{'EXIT', _} -> [];
|
|
|
|
Cs -> [{NameArgs, [], Desc} || {NameArgs, Desc} <- Cs]
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Print help
|
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
%% Bold
|
|
|
|
-define(B1, "\e[1m").
|
|
|
|
-define(B2, "\e[22m").
|
|
|
|
-define(B(S), case ShCode of true -> [?B1, S, ?B2]; false -> S end).
|
|
|
|
|
|
|
|
%% Underline
|
|
|
|
-define(U1, "\e[4m").
|
|
|
|
-define(U2, "\e[24m").
|
|
|
|
-define(U(S), case ShCode of true -> [?U1, S, ?U2]; false -> S end).
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
{MaxC, ShCode} = get_shell_info(),
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage(dual, MaxC, ShCode, Version).
|
|
|
|
print_usage(HelpMode, MaxC, ShCode, Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
AllCommands =
|
|
|
|
[
|
|
|
|
{"status", [], "Get ejabberd status"},
|
|
|
|
{"stop", [], "Stop ejabberd"},
|
|
|
|
{"restart", [], "Restart ejabberd"},
|
|
|
|
{"help", ["[--tags [tag] | com?*]"], "Show help (try: ejabberdctl help help)"},
|
|
|
|
{"mnesia", ["[info]"], "show information of Mnesia system"}] ++
|
2016-03-31 13:53:31 +02:00
|
|
|
get_list_commands(Version) ++
|
2008-10-12 13:58:30 +02:00
|
|
|
get_list_ctls(),
|
|
|
|
|
2013-12-10 08:40:43 +01:00
|
|
|
print(
|
2016-04-15 15:35:57 +02:00
|
|
|
["Usage: ", ?B("ejabberdctl"), " [--no-timeout] [--node ", ?U("nodename"), "] [--version ", ?U("api_version"), "] ",
|
2009-04-17 15:48:59 +02:00
|
|
|
?U("command"), " [", ?U("options"), "]\n"
|
2008-10-12 13:58:30 +02:00
|
|
|
"\n"
|
|
|
|
"Available commands in this ejabberd node:\n"], []),
|
|
|
|
print_usage_commands(HelpMode, MaxC, ShCode, AllCommands),
|
2013-12-10 08:40:43 +01:00
|
|
|
print(
|
2008-10-12 13:58:30 +02:00
|
|
|
["\n"
|
|
|
|
"Examples:\n"
|
|
|
|
" ejabberdctl restart\n"
|
|
|
|
" ejabberdctl --node ejabberd@host restart\n"],
|
|
|
|
[]).
|
|
|
|
|
|
|
|
print_usage_commands(HelpMode, MaxC, ShCode, Commands) ->
|
|
|
|
CmdDescsSorted = lists:keysort(1, Commands),
|
|
|
|
|
|
|
|
%% What is the length of the largest command?
|
|
|
|
{CmdArgsLenDescsSorted, Lens} =
|
|
|
|
lists:mapfoldl(
|
|
|
|
fun({Cmd, Args, Desc}, Lengths) ->
|
|
|
|
Len =
|
|
|
|
length(Cmd) +
|
|
|
|
lists:foldl(fun(Arg, R) ->
|
|
|
|
R + 1 + length(Arg)
|
|
|
|
end,
|
|
|
|
0,
|
|
|
|
Args),
|
|
|
|
{{Cmd, Args, Len, Desc}, [Len | Lengths]}
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
CmdDescsSorted),
|
|
|
|
MaxCmdLen = case Lens of
|
|
|
|
[] -> 80;
|
|
|
|
_ -> lists:max(Lens)
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% For each command in the list of commands
|
|
|
|
%% Convert its definition to a line
|
|
|
|
FmtCmdDescs = format_command_lines(CmdArgsLenDescsSorted, MaxCmdLen, MaxC, ShCode, HelpMode),
|
|
|
|
|
2013-12-10 08:40:43 +01:00
|
|
|
print([FmtCmdDescs], []).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
%% Get some info about the shell:
|
|
|
|
%% how many columns of width
|
|
|
|
%% and guess if it supports text formatting codes.
|
|
|
|
get_shell_info() ->
|
|
|
|
%% This function was introduced in OTP R12B-0
|
|
|
|
try io:columns() of
|
|
|
|
{ok, C} -> {C-2, true};
|
|
|
|
{error, enotsup} -> {78, false}
|
|
|
|
catch
|
|
|
|
_:_ -> {78, false}
|
|
|
|
end.
|
2006-02-20 05:07:42 +01:00
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%% Split this command description in several lines of proper length
|
|
|
|
prepare_description(DescInit, MaxC, Desc) ->
|
|
|
|
Words = string:tokens(Desc, " "),
|
|
|
|
prepare_long_line(DescInit, MaxC, Words).
|
|
|
|
|
|
|
|
prepare_long_line(DescInit, MaxC, Words) ->
|
|
|
|
MaxSegmentLen = MaxC - DescInit,
|
|
|
|
MarginString = lists:duplicate(DescInit, $\s), % Put spaces
|
|
|
|
[FirstSegment | MoreSegments] = split_desc_segments(MaxSegmentLen, Words),
|
|
|
|
MoreSegmentsMixed = mix_desc_segments(MarginString, MoreSegments),
|
|
|
|
[FirstSegment | MoreSegmentsMixed].
|
|
|
|
|
|
|
|
mix_desc_segments(MarginString, Segments) ->
|
|
|
|
[["\n", MarginString, Segment] || Segment <- Segments].
|
|
|
|
|
|
|
|
split_desc_segments(MaxL, Words) ->
|
|
|
|
join(MaxL, Words).
|
|
|
|
|
|
|
|
%% Join words in a segment,
|
|
|
|
%% but stop adding to a segment if adding this word would pass L
|
|
|
|
join(L, Words) ->
|
|
|
|
join(L, Words, 0, [], []).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
join(_Len, [], _CurSegLen, CurSeg, AllSegs) ->
|
|
|
|
lists:reverse([CurSeg | AllSegs]);
|
|
|
|
join(Len, [Word | Tail], CurSegLen, CurSeg, AllSegs) ->
|
|
|
|
WordLen = length(Word),
|
|
|
|
SegSize = WordLen + CurSegLen + 1,
|
|
|
|
{NewCurSeg, NewAllSegs, NewCurSegLen} =
|
|
|
|
if SegSize < Len ->
|
|
|
|
{[CurSeg, " ", Word], AllSegs, SegSize};
|
|
|
|
true ->
|
|
|
|
{Word, [CurSeg | AllSegs], WordLen}
|
|
|
|
end,
|
|
|
|
NewLen = case string:str(Word, "\n") of
|
|
|
|
0 ->
|
|
|
|
NewCurSegLen;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end,
|
|
|
|
join(Len, Tail, NewLen, NewCurSeg, NewAllSegs).
|
|
|
|
|
2006-02-15 05:15:54 +01:00
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
format_command_lines(CALD, MaxCmdLen, MaxC, ShCode, dual)
|
|
|
|
when MaxC - MaxCmdLen < 40 ->
|
|
|
|
%% If the space available for descriptions is too narrow, enforce long help mode
|
|
|
|
format_command_lines(CALD, MaxCmdLen, MaxC, ShCode, long);
|
|
|
|
|
|
|
|
format_command_lines(CALD, MaxCmdLen, MaxC, ShCode, dual) ->
|
|
|
|
lists:map(
|
|
|
|
fun({Cmd, Args, CmdArgsL, Desc}) ->
|
|
|
|
DescFmt = prepare_description(MaxCmdLen+4, MaxC, Desc),
|
2014-03-26 16:39:35 +01:00
|
|
|
[" ", ?B(Cmd), " ", [[?U(Arg), " "] || Arg <- Args],
|
2013-03-14 10:33:02 +01:00
|
|
|
string:chars($\s, MaxCmdLen - CmdArgsL + 1),
|
2008-10-12 13:58:30 +02:00
|
|
|
DescFmt, "\n"]
|
|
|
|
end, CALD);
|
|
|
|
|
|
|
|
format_command_lines(CALD, _MaxCmdLen, MaxC, ShCode, long) ->
|
|
|
|
lists:map(
|
|
|
|
fun({Cmd, Args, _CmdArgsL, Desc}) ->
|
|
|
|
DescFmt = prepare_description(8, MaxC, Desc),
|
2014-03-26 16:39:35 +01:00
|
|
|
["\n ", ?B(Cmd), " ", [[?U(Arg), " "] || Arg <- Args], "\n", " ",
|
2008-10-12 13:58:30 +02:00
|
|
|
DescFmt, "\n"]
|
|
|
|
end, CALD).
|
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Print Tags
|
|
|
|
%%-----------------------------
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_tags(MaxC, ShCode, Version) ->
|
2013-12-10 08:40:43 +01:00
|
|
|
print("Available tags and commands:", []),
|
2016-03-31 13:53:31 +02:00
|
|
|
TagsCommands = ejabberd_commands:get_tags_commands(Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Tag, Commands} = _TagCommands) ->
|
2013-12-10 08:40:43 +01:00
|
|
|
print(["\n\n ", ?B(Tag), "\n "], []),
|
2008-10-12 13:58:30 +02:00
|
|
|
Words = lists:sort(Commands),
|
|
|
|
Desc = prepare_long_line(5, MaxC, Words),
|
2013-12-10 08:40:43 +01:00
|
|
|
print(Desc, [])
|
2008-10-12 13:58:30 +02:00
|
|
|
end,
|
|
|
|
TagsCommands),
|
2013-12-10 08:40:43 +01:00
|
|
|
print("\n\n", []).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_tags(Tag, MaxC, ShCode, Version) ->
|
2013-12-10 08:40:43 +01:00
|
|
|
print(["Available commands with tag ", ?B(Tag), ":", "\n"], []),
|
2008-10-12 13:58:30 +02:00
|
|
|
HelpMode = long,
|
2016-03-31 13:53:31 +02:00
|
|
|
TagsCommands = ejabberd_commands:get_tags_commands(Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
CommandsNames = case lists:keysearch(Tag, 1, TagsCommands) of
|
|
|
|
{value, {Tag, CNs}} -> CNs;
|
|
|
|
false -> []
|
|
|
|
end,
|
|
|
|
CommandsList = lists:map(
|
|
|
|
fun(NameString) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
C = ejabberd_commands:get_command_definition(
|
2016-03-31 13:53:31 +02:00
|
|
|
list_to_atom(NameString), Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
#ejabberd_commands{name = Name,
|
|
|
|
args = Args,
|
|
|
|
desc = Desc} = C,
|
|
|
|
tuple_command_help({Name, Args, Desc})
|
|
|
|
end,
|
|
|
|
CommandsNames),
|
|
|
|
print_usage_commands(HelpMode, MaxC, ShCode, CommandsList),
|
2013-12-10 08:40:43 +01:00
|
|
|
print("\n", []).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Print usage of 'help' command
|
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
print_usage_help(MaxC, ShCode) ->
|
|
|
|
LongDesc =
|
|
|
|
["The special 'help' ejabberdctl command provides help of ejabberd commands.\n\n"
|
|
|
|
"The format is:\n ", ?B("ejabberdctl"), " ", ?B("help"), " [", ?B("--tags"), " ", ?U("[tag]"), " | ", ?U("com?*"), "]\n\n"
|
|
|
|
"The optional arguments:\n"
|
|
|
|
" ",?B("--tags")," Show all tags and the names of commands in each tag\n"
|
|
|
|
" ",?B("--tags"), " ", ?U("tag")," Show description of commands in this tag\n"
|
|
|
|
" ",?U("command")," Show detailed description of the command\n"
|
|
|
|
" ",?U("com?*")," Show detailed description of commands that match this glob.\n"
|
|
|
|
" You can use ? to match a simple character,\n"
|
|
|
|
" and * to match several characters.\n"
|
|
|
|
"\n",
|
|
|
|
"Some example usages:\n",
|
|
|
|
" ejabberdctl help\n",
|
|
|
|
" ejabberdctl help --tags\n",
|
|
|
|
" ejabberdctl help --tags accounts\n",
|
|
|
|
" ejabberdctl help register\n",
|
|
|
|
" ejabberdctl help regist*\n",
|
|
|
|
"\n",
|
|
|
|
"Please note that 'ejabberdctl help' shows all ejabberd commands,\n",
|
|
|
|
"even those that cannot be used in the shell with ejabberdctl.\n",
|
|
|
|
"Those commands can be identified because the description starts with: *"],
|
|
|
|
ArgsDef = [],
|
|
|
|
C = #ejabberd_commands{
|
2019-06-14 11:33:26 +02:00
|
|
|
name = help,
|
|
|
|
desc = "Show help of ejabberd commands",
|
|
|
|
longdesc = lists:flatten(LongDesc),
|
|
|
|
args = ArgsDef,
|
|
|
|
result = {help, string}},
|
|
|
|
print_usage_command2("help", C, MaxC, ShCode).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
%%-----------------------------
|
|
|
|
%% Print usage command
|
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
%% @spec (CmdSubString::string(), MaxC::integer(), ShCode::boolean()) -> ok
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_commands2(CmdSubString, MaxC, ShCode, Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
%% Get which command names match this substring
|
2016-03-31 13:53:31 +02:00
|
|
|
AllCommandsNames = [atom_to_list(Name) || {Name, _, _} <- ejabberd_commands:list_commands(Version)],
|
2008-10-12 13:58:30 +02:00
|
|
|
Cmds = filter_commands(AllCommandsNames, CmdSubString),
|
|
|
|
case Cmds of
|
2016-03-31 13:53:31 +02:00
|
|
|
[] -> io:format("Error: no command found that match: ~p~n", [CmdSubString]);
|
|
|
|
_ -> print_usage_commands3(lists:sort(Cmds), MaxC, ShCode, Version)
|
2008-10-12 13:58:30 +02:00
|
|
|
end.
|
2006-02-20 05:07:42 +01:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_commands3(Cmds, MaxC, ShCode, Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
%% Then for each one print it
|
|
|
|
lists:mapfoldl(
|
|
|
|
fun(Cmd, Remaining) ->
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_command(Cmd, MaxC, ShCode, Version),
|
2008-10-12 13:58:30 +02:00
|
|
|
case Remaining > 1 of
|
2013-12-10 08:40:43 +01:00
|
|
|
true -> print([" ", lists:duplicate(MaxC, 126), " \n"], []);
|
2008-10-12 13:58:30 +02:00
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
{ok, Remaining-1}
|
|
|
|
end,
|
|
|
|
length(Cmds),
|
|
|
|
Cmds).
|
|
|
|
|
|
|
|
filter_commands(All, SubString) ->
|
|
|
|
case lists:member(SubString, All) of
|
|
|
|
true -> [SubString];
|
|
|
|
false -> filter_commands_regexp(All, SubString)
|
|
|
|
end.
|
2006-02-15 05:15:54 +01:00
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
filter_commands_regexp(All, Glob) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
RegExp = ejabberd_regexp:sh_to_awk(list_to_binary(Glob)),
|
2008-10-12 13:58:30 +02:00
|
|
|
lists:filter(
|
|
|
|
fun(Command) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_regexp:run(list_to_binary(Command), RegExp) of
|
2011-11-22 23:11:21 +01:00
|
|
|
match ->
|
2008-10-12 13:58:30 +02:00
|
|
|
true;
|
2011-11-22 23:11:21 +01:00
|
|
|
nomatch ->
|
2008-10-12 13:58:30 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
All).
|
|
|
|
|
|
|
|
%% @spec (Cmd::string(), MaxC::integer(), ShCode::boolean()) -> ok
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_command(Cmd, MaxC, ShCode, Version) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
Name = list_to_atom(Cmd),
|
2019-06-14 11:33:26 +02:00
|
|
|
C = ejabberd_commands:get_command_definition(Name, Version),
|
|
|
|
print_usage_command2(Cmd, C, MaxC, ShCode).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
print_usage_command2(Cmd, C, MaxC, ShCode) ->
|
2008-10-12 13:58:30 +02:00
|
|
|
#ejabberd_commands{
|
|
|
|
tags = TagsAtoms,
|
|
|
|
desc = Desc,
|
|
|
|
longdesc = LongDesc,
|
|
|
|
result = ResultDef} = C,
|
|
|
|
|
|
|
|
NameFmt = [" ", ?B("Command Name"), ": ", Cmd, "\n"],
|
|
|
|
|
|
|
|
%% Initial indentation of result is 13 = length(" Arguments: ")
|
2019-06-18 11:09:35 +02:00
|
|
|
{ArgsDef, _, _} = ejabberd_commands:get_command_format(
|
2015-09-25 14:53:25 +02:00
|
|
|
C#ejabberd_commands.name, admin),
|
2008-10-12 13:58:30 +02:00
|
|
|
Args = [format_usage_ctype(ArgDef, 13) || ArgDef <- ArgsDef],
|
|
|
|
ArgsMargin = lists:duplicate(13, $\s),
|
|
|
|
ArgsListFmt = case Args of
|
|
|
|
[] -> "\n";
|
|
|
|
_ -> [ [Arg, "\n", ArgsMargin] || Arg <- Args]
|
|
|
|
end,
|
|
|
|
ArgsFmt = [" ", ?B("Arguments"), ": ", ArgsListFmt],
|
|
|
|
|
|
|
|
%% Initial indentation of result is 11 = length(" Returns: ")
|
|
|
|
ResultFmt = format_usage_ctype(ResultDef, 11),
|
|
|
|
ReturnsFmt = [" ",?B("Returns"),": ", ResultFmt],
|
|
|
|
|
|
|
|
XmlrpcFmt = "", %%+++ [" ",?B("XML-RPC"),": ", format_usage_xmlrpc(ArgsDef, ResultDef), "\n\n"],
|
|
|
|
|
|
|
|
TagsFmt = [" ",?B("Tags"),": ", prepare_long_line(8, MaxC, [atom_to_list(TagA) || TagA <- TagsAtoms])],
|
|
|
|
|
|
|
|
DescFmt = [" ",?B("Description"),": ", prepare_description(15, MaxC, Desc)],
|
|
|
|
|
|
|
|
LongDescFmt = case LongDesc of
|
|
|
|
"" -> "";
|
|
|
|
_ -> ["", prepare_description(0, MaxC, LongDesc), "\n\n"]
|
|
|
|
end,
|
|
|
|
|
|
|
|
NoteEjabberdctl = case is_supported_args(ArgsDef) of
|
|
|
|
true -> "";
|
2008-10-13 11:01:08 +02:00
|
|
|
false -> [" ", ?B("Note:"), " This command cannot be executed using ejabberdctl. Try ejabberd_xmlrpc.\n\n"]
|
2008-10-12 13:58:30 +02:00
|
|
|
end,
|
|
|
|
|
2013-12-10 08:40:43 +01:00
|
|
|
print(["\n", NameFmt, "\n", ArgsFmt, "\n", ReturnsFmt, "\n\n", XmlrpcFmt, TagsFmt, "\n\n", DescFmt, "\n\n", LongDescFmt, NoteEjabberdctl], []).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
2010-10-15 21:53:03 +02:00
|
|
|
format_usage_ctype(Type, _Indentation)
|
2013-03-13 11:13:19 +01:00
|
|
|
when (Type==atom) or (Type==integer) or (Type==string) or (Type==binary) or (Type==rescode) or (Type==restuple)->
|
2010-10-15 21:53:03 +02:00
|
|
|
io_lib:format("~p", [Type]);
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
format_usage_ctype({Name, Type}, _Indentation)
|
2013-03-13 11:13:19 +01:00
|
|
|
when (Type==atom) or (Type==integer) or (Type==string) or (Type==binary) or (Type==rescode) or (Type==restuple)->
|
2008-10-12 13:58:30 +02:00
|
|
|
io_lib:format("~p::~p", [Name, Type]);
|
|
|
|
|
|
|
|
format_usage_ctype({Name, {list, ElementDef}}, Indentation) ->
|
|
|
|
NameFmt = atom_to_list(Name),
|
|
|
|
Indentation2 = Indentation + length(NameFmt) + 4,
|
|
|
|
ElementFmt = format_usage_ctype(ElementDef, Indentation2),
|
|
|
|
[NameFmt, "::[ ", ElementFmt, " ]"];
|
|
|
|
|
|
|
|
format_usage_ctype({Name, {tuple, ElementsDef}}, Indentation) ->
|
|
|
|
NameFmt = atom_to_list(Name),
|
|
|
|
Indentation2 = Indentation + length(NameFmt) + 4,
|
|
|
|
ElementsFmt = format_usage_tuple(ElementsDef, Indentation2),
|
|
|
|
[NameFmt, "::{ " | ElementsFmt].
|
|
|
|
|
|
|
|
format_usage_tuple([], _Indentation) ->
|
|
|
|
[];
|
|
|
|
format_usage_tuple([ElementDef], Indentation) ->
|
|
|
|
[format_usage_ctype(ElementDef, Indentation) , " }"];
|
|
|
|
format_usage_tuple([ElementDef | ElementsDef], Indentation) ->
|
|
|
|
ElementFmt = format_usage_ctype(ElementDef, Indentation),
|
|
|
|
MarginString = lists:duplicate(Indentation, $\s), % Put spaces
|
|
|
|
[ElementFmt, ",\n", MarginString, format_usage_tuple(ElementsDef, Indentation)].
|
|
|
|
|
2013-12-10 08:40:43 +01:00
|
|
|
print(Format, Args) ->
|
|
|
|
io:format(lists:flatten(Format), Args).
|
2008-10-12 13:58:30 +02:00
|
|
|
|
2019-10-25 14:27:47 +02:00
|
|
|
-ifdef(LAGER).
|
|
|
|
disable_logging() ->
|
|
|
|
ok.
|
|
|
|
-else.
|
|
|
|
disable_logging() ->
|
|
|
|
logger:set_primary_config(level, none).
|
|
|
|
-endif.
|
|
|
|
|
2008-10-12 13:58:30 +02:00
|
|
|
%%-----------------------------
|
2018-01-27 17:35:38 +01:00
|
|
|
%% Command management
|
2008-10-12 13:58:30 +02:00
|
|
|
%%-----------------------------
|
|
|
|
|
|
|
|
%%+++
|
|
|
|
%% Struct(Integer res) create_account(Struct(String user, String server, String password))
|
|
|
|
%%format_usage_xmlrpc(ArgsDef, ResultDef) ->
|
|
|
|
%% ["aaaa bbb ccc"].
|