2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_adhoc.erl
|
|
|
|
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se>
|
2010-04-14 01:00:14 +02:00
|
|
|
%%% Purpose : Handle incoming ad-doc requests (XEP-0050)
|
2006-01-19 03:17:31 +01:00
|
|
|
%%% Created : 15 Nov 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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.
|
2009-01-12 15:44:42 +01: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
|
|
|
%%%
|
2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_adhoc).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
-author('henoch@dtek.chalmers.se').
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 50, '1.2'}).
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process_local_iq/1,
|
2016-07-26 08:52:29 +02:00
|
|
|
process_sm_iq/1, get_local_commands/5,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_identity/5, get_local_features/5,
|
|
|
|
get_sm_commands/5, get_sm_identity/5, get_sm_features/5,
|
2016-07-06 13:58:48 +02:00
|
|
|
ping_item/4, ping_command/4, mod_opt_type/1, depends/2]).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-26 08:52:29 +02:00
|
|
|
-include("xmpp.hrl").
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
start(Host, Opts) ->
|
2017-05-04 11:24:47 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, gen_iq_handler:iqdisc(Host)),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_COMMANDS, ?MODULE, process_local_iq,
|
|
|
|
IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_COMMANDS, ?MODULE, process_sm_iq, IQDisc),
|
|
|
|
ejabberd_hooks:add(disco_local_identity, Host, ?MODULE,
|
|
|
|
get_local_identity, 99),
|
|
|
|
ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
|
|
|
|
get_local_features, 99),
|
|
|
|
ejabberd_hooks:add(disco_local_items, Host, ?MODULE,
|
|
|
|
get_local_commands, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_identity, Host, ?MODULE,
|
|
|
|
get_sm_identity, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_items, Host, ?MODULE,
|
|
|
|
get_sm_commands, 99),
|
|
|
|
ejabberd_hooks:add(adhoc_local_items, Host, ?MODULE,
|
|
|
|
ping_item, 100),
|
|
|
|
ejabberd_hooks:add(adhoc_local_commands, Host, ?MODULE,
|
|
|
|
ping_command, 100).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(adhoc_local_commands, Host,
|
|
|
|
?MODULE, ping_command, 100),
|
|
|
|
ejabberd_hooks:delete(adhoc_local_items, Host, ?MODULE,
|
|
|
|
ping_item, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_items, Host, ?MODULE,
|
|
|
|
get_sm_commands, 99),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 99),
|
|
|
|
ejabberd_hooks:delete(disco_sm_identity, Host, ?MODULE,
|
|
|
|
get_sm_identity, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_items, Host, ?MODULE,
|
|
|
|
get_local_commands, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, Host,
|
|
|
|
?MODULE, get_local_features, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, Host,
|
|
|
|
?MODULE, get_local_identity, 99),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_COMMANDS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_COMMANDS).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2017-05-04 11:24:47 +02:00
|
|
|
case gen_mod:is_equal_opt(iqdisc, NewOpts, OldOpts, gen_iq_handler:iqdisc(Host)) of
|
2017-02-22 17:46:47 +01:00
|
|
|
{false, IQDisc, _} ->
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_COMMANDS,
|
|
|
|
?MODULE, process_local_iq, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_COMMANDS,
|
|
|
|
?MODULE, process_sm_iq, IQDisc);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_commands(Acc, _From,
|
2016-08-05 07:41:08 +02:00
|
|
|
#jid{server = Server, lserver = LServer} = _To, <<"">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
Lang) ->
|
|
|
|
Display = gen_mod:get_module_opt(LServer, ?MODULE,
|
|
|
|
report_commands_node,
|
|
|
|
false),
|
2006-01-19 03:17:31 +01:00
|
|
|
case Display of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2016-07-26 08:52:29 +02:00
|
|
|
Nodes = [#disco_item{jid = jid:make(Server),
|
|
|
|
node = ?NS_COMMANDS,
|
|
|
|
name = translate:translate(Lang, <<"Commands">>)}],
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, Items ++ Nodes}
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_commands(_Acc, From,
|
|
|
|
#jid{lserver = LServer} = To, ?NS_COMMANDS, Lang) ->
|
|
|
|
ejabberd_hooks:run_fold(adhoc_local_items, LServer,
|
|
|
|
{result, []}, [From, To, Lang]);
|
|
|
|
get_local_commands(_Acc, _From, _To, <<"ping">>,
|
|
|
|
_Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, []};
|
|
|
|
get_local_commands(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_commands(Acc, _From,
|
2016-08-05 07:41:08 +02:00
|
|
|
#jid{lserver = LServer} = To, <<"">>, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Display = gen_mod:get_module_opt(LServer, ?MODULE,
|
|
|
|
report_commands_node,
|
|
|
|
false),
|
2006-01-19 03:17:31 +01:00
|
|
|
case Display of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2016-07-26 08:52:29 +02:00
|
|
|
Nodes = [#disco_item{jid = To,
|
|
|
|
node = ?NS_COMMANDS,
|
|
|
|
name = translate:translate(Lang, <<"Commands">>)}],
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, Items ++ Nodes}
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_commands(_Acc, From,
|
|
|
|
#jid{lserver = LServer} = To, ?NS_COMMANDS, Lang) ->
|
|
|
|
ejabberd_hooks:run_fold(adhoc_sm_items, LServer,
|
|
|
|
{result, []}, [From, To, Lang]);
|
|
|
|
get_sm_commands(Acc, _From, _To, _Node, _Lang) -> Acc.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% On disco info request to the ad-hoc node, return automation/command-list.
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_identity(Acc, _From, _To, ?NS_COMMANDS,
|
|
|
|
Lang) ->
|
2016-07-26 08:52:29 +02:00
|
|
|
[#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-list">>,
|
|
|
|
name = translate:translate(Lang, <<"Commands">>)}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Acc];
|
|
|
|
get_local_identity(Acc, _From, _To, <<"ping">>, Lang) ->
|
2016-07-26 08:52:29 +02:00
|
|
|
[#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-node">>,
|
|
|
|
name = translate:translate(Lang, <<"Ping">>)}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Acc];
|
2006-01-19 03:17:31 +01:00
|
|
|
get_local_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% On disco info request to the ad-hoc node, return automation/command-list.
|
|
|
|
get_sm_identity(Acc, _From, _To, ?NS_COMMANDS, Lang) ->
|
2016-07-26 08:52:29 +02:00
|
|
|
[#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-list">>,
|
|
|
|
name = translate:translate(Lang, <<"Commands">>)}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Acc];
|
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) -> Acc.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec get_local_features({error, stanza_error()} | {result, [binary()]} | empty,
|
2016-08-09 09:56:32 +02:00
|
|
|
jid(), jid(), binary(), binary()) ->
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_error()} | {result, [binary()]} | empty.
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_features(Acc, _From, _To, <<"">>, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Feats = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
2006-01-19 03:17:31 +01:00
|
|
|
end,
|
|
|
|
{result, Feats ++ [?NS_COMMANDS]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_features(_Acc, _From, _To, ?NS_COMMANDS,
|
|
|
|
_Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, []};
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_features(_Acc, _From, _To, <<"ping">>,
|
|
|
|
_Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, [?NS_COMMANDS]};
|
|
|
|
get_local_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features(Acc, _From, _To, <<"">>, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Feats = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
2006-01-19 03:17:31 +01:00
|
|
|
end,
|
|
|
|
{result, Feats ++ [?NS_COMMANDS]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features(_Acc, _From, _To, ?NS_COMMANDS,
|
|
|
|
_Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, []};
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) -> Acc.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2016-07-26 08:52:29 +02:00
|
|
|
process_local_iq(IQ) ->
|
2016-08-09 09:56:32 +02:00
|
|
|
process_adhoc_request(IQ, local).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2016-07-26 08:52:29 +02:00
|
|
|
process_sm_iq(IQ) ->
|
2016-08-09 09:56:32 +02:00
|
|
|
process_adhoc_request(IQ, sm).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2016-07-26 08:52:29 +02:00
|
|
|
process_adhoc_request(#iq{from = From, to = To,
|
|
|
|
type = set, lang = Lang,
|
2016-08-09 09:56:32 +02:00
|
|
|
sub_els = [#adhoc_command{} = SubEl]} = IQ, Type) ->
|
2016-07-26 08:52:29 +02:00
|
|
|
Host = To#jid.lserver,
|
2016-08-09 09:56:32 +02:00
|
|
|
Res = case Type of
|
|
|
|
local ->
|
|
|
|
ejabberd_hooks:run_fold(adhoc_local_commands, Host, empty,
|
|
|
|
[From, To, SubEl]);
|
|
|
|
sm ->
|
|
|
|
ejabberd_hooks:run_fold(adhoc_sm_commands, Host, empty,
|
|
|
|
[From, To, SubEl])
|
|
|
|
end,
|
|
|
|
case Res of
|
2016-07-26 08:52:29 +02:00
|
|
|
ignore ->
|
|
|
|
ignore;
|
|
|
|
empty ->
|
|
|
|
Txt = <<"No hook has processed this command">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang));
|
|
|
|
{error, Error} ->
|
|
|
|
xmpp:make_error(IQ, Error);
|
|
|
|
Command ->
|
|
|
|
xmpp:make_iq_result(IQ, Command)
|
|
|
|
end;
|
|
|
|
process_adhoc_request(#iq{} = IQ, _Hooks) ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request()).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec ping_item(empty | {error, stanza_error()} | {result, [disco_item()]},
|
2016-08-09 09:56:32 +02:00
|
|
|
jid(), jid(), binary()) -> {result, [disco_item()]}.
|
2013-03-14 10:33:02 +01:00
|
|
|
ping_item(Acc, _From, #jid{server = Server} = _To,
|
|
|
|
Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Items = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
2006-01-19 03:17:31 +01:00
|
|
|
end,
|
2016-07-26 08:52:29 +02:00
|
|
|
Nodes = [#disco_item{jid = jid:make(Server),
|
|
|
|
node = <<"ping">>,
|
|
|
|
name = translate:translate(Lang, <<"Ping">>)}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}.
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec ping_command(adhoc_command(), jid(), jid(), adhoc_command()) ->
|
2016-09-08 16:08:48 +02:00
|
|
|
adhoc_command() | {error, stanza_error()}.
|
2006-01-19 03:17:31 +01:00
|
|
|
ping_command(_Acc, _From, _To,
|
2016-07-26 08:52:29 +02:00
|
|
|
#adhoc_command{lang = Lang, node = <<"ping">>,
|
|
|
|
action = Action} = Request) ->
|
|
|
|
if Action == execute ->
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{
|
|
|
|
status = completed,
|
|
|
|
notes = [#adhoc_note{
|
|
|
|
type = info,
|
|
|
|
data = translate:translate(Lang, <<"Pong">>)}]});
|
2016-03-31 10:00:29 +02:00
|
|
|
true ->
|
|
|
|
Txt = <<"Incorrect value of 'action' attribute">>,
|
2016-07-26 08:52:29 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
ping_command(Acc, _From, _To, _Request) -> Acc.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(report_commands_node) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(_) -> [iqdisc, report_commands_node].
|