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
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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').
|
|
|
|
|
2024-11-08 19:27:22 +01:00
|
|
|
-protocol({xep, 50, '1.2', '1.1.0', "complete", ""}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
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,
|
2018-01-23 08:54:52 +01:00
|
|
|
ping_item/4, ping_command/4, mod_opt_type/1, depends/2,
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_options/1, mod_doc/0]).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2023-08-04 17:53:50 +02:00
|
|
|
start(_Host, _Opts) ->
|
|
|
|
{ok, [{iq_handler, ejabberd_local, ?NS_COMMANDS, process_local_iq},
|
|
|
|
{iq_handler, ejabberd_sm, ?NS_COMMANDS, process_sm_iq},
|
|
|
|
{hook, disco_local_identity, get_local_identity, 99},
|
|
|
|
{hook, disco_local_features, get_local_features, 99},
|
|
|
|
{hook, disco_local_items, get_local_commands, 99},
|
|
|
|
{hook, disco_sm_identity, get_sm_identity, 99},
|
|
|
|
{hook, disco_sm_features, get_sm_features, 99},
|
|
|
|
{hook, disco_sm_items, get_sm_commands, 99},
|
|
|
|
{hook, adhoc_local_items, ping_item, 100},
|
|
|
|
{hook, adhoc_local_commands, ping_command, 100}]}.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2023-08-04 17:53:50 +02:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_local_commands(mod_disco:items_acc(), jid(), jid(), binary(), binary()) -> mod_disco:items_acc().
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Display = mod_adhoc_opt:report_commands_node(LServer),
|
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,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_sm_commands(mod_disco:items_acc(), jid(), jid(), binary(), binary()) -> mod_disco:items_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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Display = mod_adhoc_opt:report_commands_node(LServer),
|
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,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_local_identity([identity()], jid(), jid(), binary(), binary()) -> [identity()].
|
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">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_sm_identity([identity()], jid(), jid(), binary(), binary()) -> [identity()].
|
2006-01-19 03:17:31 +01:00
|
|
|
%% 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">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_local_features(mod_disco:features_acc(), jid(), jid(), binary(), binary()) -> mod_disco:features_acc().
|
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.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec get_sm_features(mod_disco:features_acc(), jid(), jid(), binary(), binary()) -> mod_disco:features_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
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec process_local_iq(iq()) -> iq() | ignore.
|
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
|
|
|
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec process_sm_iq(iq()) -> iq() | ignore.
|
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
|
|
|
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec process_adhoc_request(iq(), sm | local) -> iq() | ignore.
|
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,
|
2019-04-06 17:27:28 +02:00
|
|
|
[From, To, fix_lang(Lang, SubEl)]);
|
2016-08-09 09:56:32 +02:00
|
|
|
sm ->
|
|
|
|
ejabberd_hooks:run_fold(adhoc_sm_commands, Host, empty,
|
2019-04-06 17:27:28 +02:00
|
|
|
[From, To, fix_lang(Lang, SubEl)])
|
2016-08-09 09:56:32 +02:00
|
|
|
end,
|
|
|
|
case Res of
|
2016-07-26 08:52:29 +02:00
|
|
|
ignore ->
|
|
|
|
ignore;
|
|
|
|
empty ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No hook has processed this command"),
|
2016-07-26 08:52:29 +02:00
|
|
|
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
|
|
|
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec ping_item(mod_disco:items_acc(), 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">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
name = translate:translate(Lang, ?T("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,
|
2019-06-22 16:08:45 +02:00
|
|
|
data = translate:translate(Lang, ?T("Pong"))}]});
|
2016-03-31 10:00:29 +02:00
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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
|
|
|
|
2019-07-09 16:02:54 +02:00
|
|
|
-spec fix_lang(binary(), adhoc_command()) -> adhoc_command().
|
2019-04-06 17:27:28 +02:00
|
|
|
fix_lang(Lang, #adhoc_command{lang = <<>>} = Cmd) ->
|
|
|
|
Cmd#adhoc_command{lang = Lang};
|
|
|
|
fix_lang(_, Cmd) ->
|
|
|
|
Cmd.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(report_commands_node) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
mod_options(_Host) ->
|
|
|
|
[{report_commands_node, false}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
?T("This module implements https://xmpp.org/extensions/xep-0050.html"
|
|
|
|
"[XEP-0050: Ad-Hoc Commands]. It's an auxiliary module and is "
|
|
|
|
"only needed by some of the other modules."),
|
|
|
|
opts =>
|
|
|
|
[{report_commands_node,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
2020-04-21 20:58:01 +02:00
|
|
|
?T("Provide the Commands item in the Service Discovery. "
|
2020-01-08 10:24:51 +01:00
|
|
|
"Default value: 'false'.")}}]}.
|