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
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_adhoc).
|
|
|
|
-author('henoch@dtek.chalmers.se').
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
|
|
|
process_local_iq/3,
|
|
|
|
process_sm_iq/3,
|
|
|
|
get_local_commands/5,
|
|
|
|
get_local_identity/5,
|
|
|
|
get_local_features/5,
|
|
|
|
get_sm_commands/5,
|
|
|
|
get_sm_identity/5,
|
|
|
|
get_sm_features/5,
|
|
|
|
ping_item/4,
|
|
|
|
ping_command/4]).
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("adhoc.hrl").
|
|
|
|
|
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2006-01-19 03:17:31 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
|
|
|
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_ADHOC,
|
2006-01-19 03:17:31 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_ADHOC,
|
2006-01-19 03:17:31 +01:00
|
|
|
?MODULE, process_sm_iq, IQDisc),
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(disco_local_identity, HostB, ?MODULE, get_local_identity, 99),
|
|
|
|
ejabberd_hooks:add(disco_local_features, HostB, ?MODULE, get_local_features, 99),
|
|
|
|
ejabberd_hooks:add(disco_local_items, HostB, ?MODULE, get_local_commands, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_identity, HostB, ?MODULE, get_sm_identity, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, HostB, ?MODULE, get_sm_features, 99),
|
|
|
|
ejabberd_hooks:add(disco_sm_items, HostB, ?MODULE, get_sm_commands, 99),
|
|
|
|
ejabberd_hooks:add(adhoc_local_items, HostB, ?MODULE, ping_item, 100),
|
|
|
|
ejabberd_hooks:add(adhoc_local_commands, HostB, ?MODULE, ping_command, 100).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(adhoc_local_commands, HostB, ?MODULE, ping_command, 100),
|
|
|
|
ejabberd_hooks:delete(adhoc_local_items, HostB, ?MODULE, ping_item, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_items, HostB, ?MODULE, get_sm_commands, 99),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, HostB, ?MODULE, get_sm_features, 99),
|
|
|
|
ejabberd_hooks:delete(disco_sm_identity, HostB, ?MODULE, get_sm_identity, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_items, HostB, ?MODULE, get_local_commands, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, HostB, ?MODULE, get_local_features, 99),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, HostB, ?MODULE, get_local_identity, 99),
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_ADHOC),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_ADHOC).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_commands(Acc, _From, To, <<>>, Lang) ->
|
|
|
|
Server = exmpp_jid:domain(To),
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
Display = gen_mod:get_module_opt(LServer, ?MODULE, report_commands_node, false),
|
|
|
|
case Display of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
Nodes = [#xmlel{ns = ?NS_DISCO_ITEMS,
|
|
|
|
name = 'item', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"jid">>, Server),
|
|
|
|
?XMLATTR(<<"node">>, ?NS_ADHOC_s),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Commands"))]
|
2008-07-25 16:26:59 +02:00
|
|
|
}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}
|
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_commands(_Acc, From, To, ?NS_ADHOC_b, Lang) ->
|
2009-06-01 18:37:15 +02:00
|
|
|
ejabberd_hooks:run_fold(adhoc_local_items, exmpp_jid:prep_domain(To), {result, []}, [From, To, Lang]);
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_sm_commands(Acc, _From, To, <<>>, Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
Display = gen_mod:get_module_opt(LServer, ?MODULE, report_commands_node, false),
|
|
|
|
case Display of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
Nodes = [#xmlel{ns = ?NS_DISCO_ITEMS,
|
|
|
|
name = 'item', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"jid">>, exmpp_jid:to_binary(To)),
|
|
|
|
?XMLATTR(<<"node">>, ?NS_ADHOC_s),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Commands"))]
|
2008-07-25 16:26:59 +02:00
|
|
|
}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}
|
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_sm_commands(_Acc, From, To, ?NS_ADHOC_b, Lang) ->
|
2009-06-01 18:37:15 +02:00
|
|
|
ejabberd_hooks:run_fold(adhoc_sm_items, exmpp_jid:prep_domain(To), {result, []}, [From, To, Lang]);
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
get_sm_commands(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% On disco info request to the ad-hoc node, return automation/command-list.
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_identity(Acc, _From, _To, ?NS_ADHOC_b, Lang) ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"category">>, <<"automation">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"command-list">>),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Commands"))]} | Acc];
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_identity(Acc, _From, _To, <<"ping">>, Lang) ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"category">>, <<"automation">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"command-node">>),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Ping"))]} | 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.
|
2008-07-25 16:26:59 +02:00
|
|
|
get_sm_identity(Acc, _From, _To, ?NS_ADHOC_s, Lang) ->
|
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"category">>, <<"automation">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"command-list">>),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Commands"))]} | Acc];
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_features(Acc, _From, _To, <<>>, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Feats = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
{result, Feats ++ [?NS_ADHOC_s]};
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_features(_Acc, _From, _To, ?NS_ADHOC_b, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_features(_Acc, _From, _To, <<"ping">>, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
%% override all lesser features...
|
2008-07-25 16:26:59 +02:00
|
|
|
{result, [?NS_ADHOC_s]};
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
get_local_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
get_sm_features(Acc, _From, _To, "", _Lang) ->
|
|
|
|
Feats = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
{result, Feats ++ [?NS_ADHOC_s]};
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
get_sm_features(_Acc, _From, _To, ?NS_ADHOC_s, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq(From, To, IQ_Rec) ->
|
|
|
|
process_adhoc_request(From, To, IQ_Rec, adhoc_local_commands).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_sm_iq(From, To, IQ_Rec) ->
|
|
|
|
process_adhoc_request(From, To, IQ_Rec, adhoc_sm_commands).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_adhoc_request(From, To, IQ_Rec, Hook) ->
|
|
|
|
?DEBUG("About to parse ~p...", [IQ_Rec]),
|
|
|
|
case adhoc:parse_request(IQ_Rec) of
|
2006-01-19 03:17:31 +01:00
|
|
|
{error, Error} ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, Error);
|
2006-01-19 03:17:31 +01:00
|
|
|
#adhoc_request{} = AdhocRequest ->
|
2009-06-01 18:37:15 +02:00
|
|
|
case ejabberd_hooks:run_fold(Hook, exmpp_jid:prep_domain(To), empty,
|
2008-09-29 12:38:04 +02:00
|
|
|
[From, To, AdhocRequest]) of
|
2006-01-19 03:17:31 +01:00
|
|
|
ignore ->
|
|
|
|
ignore;
|
|
|
|
empty ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'item-not-found');
|
2006-01-19 03:17:31 +01:00
|
|
|
{error, Error} ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, Error);
|
2006-01-19 03:17:31 +01:00
|
|
|
Command ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Command)
|
2006-01-19 03:17:31 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
ping_item(Acc, _From, To, Lang) ->
|
2009-01-08 15:54:00 +01:00
|
|
|
Server = exmpp_jid:domain(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
Items = case Acc of
|
|
|
|
{result, I} ->
|
|
|
|
I;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
Nodes = [#xmlel{ns = ?NS_DISCO_INFO, name = 'item', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"jid">>, Server),
|
|
|
|
?XMLATTR(<<"node">>, <<"ping">>),
|
|
|
|
?XMLATTR(<<"name">>, translate:translate(Lang, "Ping"))]}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}.
|
|
|
|
|
|
|
|
|
|
|
|
ping_command(_Acc, _From, _To,
|
|
|
|
#adhoc_request{lang = Lang,
|
|
|
|
node = "ping",
|
|
|
|
sessionid = _Sessionid,
|
|
|
|
action = Action} = Request) ->
|
|
|
|
if
|
|
|
|
Action == ""; Action == "execute" ->
|
|
|
|
adhoc:produce_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_response{status = completed,
|
|
|
|
notes = [{"info", translate:translate(
|
|
|
|
Lang,
|
|
|
|
"Pong")}]});
|
|
|
|
true ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'bad-request'}
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
ping_command(Acc, _From, _To, _Request) ->
|
|
|
|
Acc.
|
|
|
|
|