2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_adhoc.erl
|
|
|
|
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se>
|
|
|
|
%%% Purpose : Handle incoming ad-doc requests (JEP-0050)
|
|
|
|
%%% Created : 15 Nov 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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) ->
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ADHOC,
|
2006-01-19 03:17:31 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2008-08-14 15:36:11 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ADHOC,
|
2006-01-19 03:17:31 +01:00
|
|
|
?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).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
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),
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ADHOC),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ADHOC).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
get_local_commands(Acc, _From, #jid{domain = Server, ldomain = LServer} = _To, "", Lang) ->
|
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 =
|
|
|
|
[#xmlattr{name = 'jid', value = Server},
|
|
|
|
#xmlattr{name = 'node', value = ?NS_ADHOC_s},
|
|
|
|
#xmlattr{name = 'name', value = translate:translate(Lang, "Commands")}]
|
|
|
|
}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}
|
|
|
|
end;
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
get_local_commands(_Acc, From, #jid{ldomain = LServer} = To, ?NS_ADHOC_s, Lang) ->
|
2008-09-29 12:38:04 +02:00
|
|
|
ejabberd_hooks:run_fold(adhoc_local_items, LServer, {result, []}, [From, To, Lang]);
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
get_local_commands(_Acc, _From, _To, "ping", _Lang) ->
|
|
|
|
{result, []};
|
|
|
|
|
|
|
|
get_local_commands(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
get_sm_commands(Acc, _From, #jid{ldomain = LServer} = To, "", Lang) ->
|
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 =
|
2008-08-06 15:51:42 +02:00
|
|
|
[#xmlattr{name = 'jid', value = exmpp_jid:jid_to_list(To)},
|
2008-07-25 16:26:59 +02:00
|
|
|
#xmlattr{name = 'node', value = ?NS_ADHOC_s},
|
|
|
|
#xmlattr{name = 'name', value = translate:translate(Lang, "Commands")}]
|
|
|
|
}],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}
|
|
|
|
end;
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
get_sm_commands(_Acc, From, #jid{ldomain = LServer} = To, ?NS_ADHOC_s, Lang) ->
|
2008-09-29 12:38:04 +02:00
|
|
|
ejabberd_hooks:run_fold(adhoc_sm_items, LServer, {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.
|
2008-07-25 16:26:59 +02:00
|
|
|
get_local_identity(Acc, _From, _To, ?NS_ADHOC_s, Lang) ->
|
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
|
|
|
[#xmlattr{name = 'category', value = "automation"},
|
|
|
|
#xmlattr{name = 'type', value = "command-list"},
|
|
|
|
#xmlattr{name = 'name', value = translate:translate(Lang, "Commands")}]} | Acc];
|
2006-01-19 03:17:31 +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 =
|
|
|
|
[#xmlattr{name = 'category', value = "automation"},
|
|
|
|
#xmlattr{name = 'type', value = "command-node"},
|
|
|
|
#xmlattr{name = 'name', value = 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 =
|
|
|
|
[#xmlattr{name = 'category', value = "automation"},
|
|
|
|
#xmlattr{name = 'type', value = "command-list"},
|
|
|
|
#xmlattr{name = 'name', value = translate:translate(Lang, "Commands")}]} | Acc];
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
get_local_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_local_features(_Acc, _From, _To, ?NS_ADHOC_s, _Lang) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
|
|
|
get_local_features(_Acc, _From, _To, "ping", _Lang) ->
|
|
|
|
%% 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 ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Host = To#jid.ldomain,
|
2006-01-19 03:17:31 +01:00
|
|
|
case ejabberd_hooks:run_fold(Hook, Host, 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.
|
|
|
|
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
ping_item(Acc, _From, #jid{domain = Server} = _To, Lang) ->
|
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 =
|
|
|
|
[#xmlattr{name = 'jid', value = Server},
|
|
|
|
#xmlattr{name = 'node', value = "ping"},
|
|
|
|
#xmlattr{name = 'name', value = 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.
|
|
|
|
|