2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_irc.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-02-16 21:07:21 +01:00
|
|
|
%%% Purpose : IRC transport
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 15 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2007-12-24 14:57:53 +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 14:57:53 +01:00
|
|
|
%%%
|
2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_irc).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, export/1, import/1,
|
2016-04-14 11:18:04 +02:00
|
|
|
import/3, closed_connection/3, get_connection_params/3,
|
2016-08-03 19:57:05 +02:00
|
|
|
data_to_binary/2, process_disco_info/1, process_disco_items/1,
|
|
|
|
process_register/1, process_vcard/1, process_command/1]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3,
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type/1, mod_options/1, depends/2]).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-08-03 19:57:05 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-04-14 11:18:04 +02:00
|
|
|
-include("mod_irc.hrl").
|
2018-01-08 09:29:17 +01:00
|
|
|
-include("translate.hrl").
|
2016-04-14 11:18:04 +02:00
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
-define(DEFAULT_IRC_PORT, 6667).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(POSSIBLE_ENCODINGS,
|
2015-12-21 16:19:24 +01:00
|
|
|
[<<"koi8-r">>, <<"iso8859-15">>, <<"iso8859-1">>, <<"iso8859-2">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"utf-8">>, <<"utf-8+latin-1">>]).
|
|
|
|
|
2017-08-08 16:46:26 +02:00
|
|
|
-record(state, {hosts = [] :: [binary()],
|
2013-03-14 10:33:02 +01:00
|
|
|
server_host = <<"">> :: binary(),
|
|
|
|
access = all :: atom()}).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2016-04-14 11:18:04 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #irc_custom{}) -> ok | pass.
|
2016-08-09 09:56:32 +02:00
|
|
|
-callback get_data(binary(), binary(), jid()) -> error | empty | irc_data().
|
|
|
|
-callback set_data(binary(), binary(), jid(), irc_data()) -> {atomic, any()}.
|
2016-04-14 11:18:04 +02:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%%====================================================================
|
2017-02-14 10:39:26 +01:00
|
|
|
%% gen_mod API
|
2006-02-03 04:28:15 +01:00
|
|
|
%%====================================================================
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2006-02-03 04:28:15 +01:00
|
|
|
start_supervisor(Host),
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, Host, Opts).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
stop_supervisor(Host),
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, Host).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {reload, Host, NewOpts, OldOpts}).
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Opts]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2016-02-03 11:17:16 +01:00
|
|
|
ejabberd:start_app(iconv),
|
2018-01-23 08:54:52 +01:00
|
|
|
MyHosts = gen_mod:get_opt_hosts(Host, Opts),
|
2016-04-14 11:18:04 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
2018-01-23 08:54:52 +01:00
|
|
|
Access = gen_mod:get_opt(access, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(irc_connection,
|
|
|
|
[named_table, public,
|
|
|
|
{keypos, #irc_connection.jid_server_host}]),
|
2017-08-08 16:46:26 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(MyHost) ->
|
2018-02-11 10:54:15 +01:00
|
|
|
register_hooks(MyHost),
|
2017-08-08 16:46:26 +02:00
|
|
|
ejabberd_router:register_route(MyHost, Host)
|
|
|
|
end, MyHosts),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok,
|
2017-08-08 16:46:26 +02:00
|
|
|
#state{hosts = MyHosts, server_host = Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
access = Access}}.
|
2006-02-03 04:28:15 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_cast({reload, ServerHost, NewOpts, OldOpts}, State) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
NewHosts = gen_mod:get_opt_hosts(ServerHost, NewOpts),
|
|
|
|
OldHosts = gen_mod:get_opt_hosts(ServerHost, OldOpts),
|
2017-02-22 17:46:47 +01:00
|
|
|
NewMod = gen_mod:db_mod(ServerHost, NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(ServerHost, OldOpts, ?MODULE),
|
2018-01-23 08:54:52 +01:00
|
|
|
Access = gen_mod:get_opt(access, NewOpts),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(ServerHost, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2017-08-08 16:46:26 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(NewHost) ->
|
|
|
|
ejabberd_router:register_route(NewHost, ServerHost),
|
2018-02-11 10:54:15 +01:00
|
|
|
register_hooks(NewHost)
|
2017-08-08 16:46:26 +02:00
|
|
|
end, NewHosts -- OldHosts),
|
|
|
|
lists:foreach(
|
|
|
|
fun(OldHost) ->
|
|
|
|
ejabberd_router:unregister_route(OldHost),
|
|
|
|
unregister_hooks(OldHost)
|
|
|
|
end, OldHosts -- NewHosts),
|
2018-01-23 08:54:52 +01:00
|
|
|
Access = gen_mod:get_opt(access, NewOpts),
|
2017-08-08 16:46:26 +02:00
|
|
|
{noreply, State#state{hosts = NewHosts, access = Access}};
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("unexpected cast: ~p", [Msg]),
|
|
|
|
{noreply, State}.
|
2006-02-03 04:28:15 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, Packet},
|
2017-08-08 16:46:26 +02:00
|
|
|
#state{server_host = ServerHost, access = Access} =
|
2013-03-14 10:33:02 +01:00
|
|
|
State) ->
|
2017-08-08 16:46:26 +02:00
|
|
|
To = xmpp:get_to(Packet),
|
|
|
|
Host = To#jid.lserver,
|
2017-02-16 09:00:26 +01:00
|
|
|
case catch do_route(Host, ServerHost, Access, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ -> ok
|
2006-02-03 04:28:15 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2006-02-03 04:28:15 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
2017-08-08 16:46:26 +02:00
|
|
|
terminate(_Reason, #state{hosts = MyHosts}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(MyHost) ->
|
|
|
|
ejabberd_router:unregister_route(MyHost),
|
|
|
|
unregister_hooks(MyHost)
|
|
|
|
end, MyHosts).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2018-02-11 10:54:15 +01:00
|
|
|
register_hooks(Host) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_INFO,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_disco_info),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_ITEMS,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_disco_items),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_REGISTER,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_register),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_VCARD,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_vcard),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_COMMANDS,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_command).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
|
|
|
unregister_hooks(Host) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_COMMANDS).
|
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
start_supervisor(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Proc = gen_mod:get_module_proc(Host,
|
|
|
|
ejabberd_mod_irc_sup),
|
|
|
|
ChildSpec = {Proc,
|
|
|
|
{ejabberd_tmp_sup, start_link,
|
|
|
|
[Proc, mod_irc_connection]},
|
|
|
|
permanent, infinity, supervisor, [ejabberd_tmp_sup]},
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, ChildSpec).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
stop_supervisor(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Proc = gen_mod:get_module_proc(Host,
|
|
|
|
ejabberd_mod_irc_sup),
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:terminate_child(ejabberd_gen_mod_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_gen_mod_sup, Proc).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Host, ServerHost, Access, Packet) ->
|
|
|
|
#jid{luser = LUser, lresource = LResource} = xmpp:get_to(Packet),
|
|
|
|
From = xmpp:get_from(Packet),
|
2007-12-10 15:44:43 +01:00
|
|
|
case acl:match_rule(ServerHost, Access, From) of
|
2016-08-03 19:57:05 +02:00
|
|
|
allow ->
|
|
|
|
case Packet of
|
|
|
|
#iq{} when LUser == <<"">>, LResource == <<"">> ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:process_iq(Packet);
|
2016-08-03 19:57:05 +02:00
|
|
|
#iq{} when LUser == <<"">>, LResource /= <<"">> ->
|
|
|
|
Err = xmpp:err_service_unavailable(),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2016-08-03 19:57:05 +02:00
|
|
|
_ ->
|
2017-02-16 09:00:26 +01:00
|
|
|
sm_route(Host, ServerHost, Packet)
|
2016-08-03 19:57:05 +02:00
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
2017-09-24 13:32:37 +02:00
|
|
|
Err = xmpp:err_forbidden(<<"Access denied by service policy">>, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2016-08-03 19:57:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_disco_info(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_info(#iq{type = get, lang = Lang, to = To,
|
|
|
|
sub_els = [#disco_info{node = Node}]} = IQ) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, ServerHost,
|
|
|
|
[], [ServerHost, ?MODULE, <<"">>, <<"">>]),
|
|
|
|
case iq_disco(ServerHost, Node, Lang) of
|
|
|
|
undefined ->
|
|
|
|
xmpp:make_iq_result(IQ, #disco_info{});
|
|
|
|
DiscoInfo ->
|
|
|
|
xmpp:make_iq_result(IQ, DiscoInfo#disco_info{xdata = Info})
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_disco_items(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_items(#iq{type = get, lang = Lang, to = To,
|
|
|
|
sub_els = [#disco_items{node = Node}]} = IQ) ->
|
|
|
|
case Node of
|
2016-08-05 07:41:08 +02:00
|
|
|
<<"">> ->
|
2016-08-03 19:57:05 +02:00
|
|
|
xmpp:make_iq_result(IQ, #disco_items{});
|
|
|
|
<<"join">> ->
|
|
|
|
xmpp:make_iq_result(IQ, #disco_items{});
|
|
|
|
<<"register">> ->
|
|
|
|
xmpp:make_iq_result(IQ, #disco_items{});
|
|
|
|
?NS_COMMANDS ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #disco_items{node = Node,
|
|
|
|
items = command_items(ServerHost, Host, Lang)});
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Node not found">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang))
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_register(#iq{type = get, to = To, from = From, lang = Lang} = IQ) ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
case get_form(ServerHost, Host, From, Lang) of
|
|
|
|
{result, Res} ->
|
|
|
|
xmpp:make_iq_result(IQ, Res);
|
|
|
|
{error, Error} ->
|
|
|
|
xmpp:make_error(IQ, Error)
|
|
|
|
end;
|
|
|
|
process_register(#iq{type = set, lang = Lang, to = To, from = From,
|
|
|
|
sub_els = [#register{xdata = #xdata{} = X}]} = IQ) ->
|
|
|
|
case X#xdata.type of
|
|
|
|
cancel ->
|
|
|
|
xmpp:make_iq_result(IQ, #register{});
|
|
|
|
submit ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
case set_form(ServerHost, Host, From, Lang, X) of
|
|
|
|
{result, Res} ->
|
|
|
|
xmpp:make_iq_result(IQ, Res);
|
|
|
|
{error, Error} ->
|
|
|
|
xmpp:make_error(IQ, Error)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Incorrect value of 'type' attribute">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
|
|
|
|
end;
|
|
|
|
process_register(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"No data form found">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang)).
|
|
|
|
|
|
|
|
process_vcard(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_vcard(#iq{type = get, lang = Lang} = IQ) ->
|
|
|
|
xmpp:make_iq_result(IQ, iq_get_vcard(Lang)).
|
|
|
|
|
|
|
|
process_command(#iq{type = get, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'get' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_command(#iq{type = set, lang = Lang, to = To, from = From,
|
|
|
|
sub_els = [#adhoc_command{node = Node} = Request]} = IQ) ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
case lists:keyfind(Node, 1, commands(ServerHost)) of
|
|
|
|
{_, _, Function} ->
|
|
|
|
try Function(From, To, Request) of
|
|
|
|
ignore ->
|
|
|
|
ignore;
|
|
|
|
{error, Error} ->
|
|
|
|
xmpp:make_error(IQ, Error);
|
|
|
|
Command ->
|
|
|
|
xmpp:make_iq_result(IQ, Command)
|
|
|
|
catch E:R ->
|
|
|
|
?ERROR_MSG("ad-hoc handler failed: ~p",
|
|
|
|
[{E, {R, erlang:get_stacktrace()}}]),
|
2016-08-03 20:00:22 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error())
|
2016-08-03 19:57:05 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Node not found">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang))
|
2004-07-11 22:51:54 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
sm_route(Host, ServerHost, Packet) ->
|
|
|
|
From = xmpp:get_from(Packet),
|
|
|
|
#jid{user = ChanServ, resource = Resource} = xmpp:get_to(Packet),
|
2016-08-03 19:57:05 +02:00
|
|
|
case str:tokens(ChanServ, <<"%">>) of
|
|
|
|
[<<_, _/binary>> = Channel, <<_, _/binary>> = Server] ->
|
|
|
|
case ets:lookup(irc_connection, {From, Server, Host}) of
|
|
|
|
[] ->
|
|
|
|
?DEBUG("open new connection~n", []),
|
|
|
|
{Username, Encoding, Port, Password} =
|
|
|
|
get_connection_params(Host, ServerHost, From, Server),
|
|
|
|
ConnectionUsername = case Packet of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% If the user tries to join a
|
|
|
|
%% chatroom, the packet for sure
|
|
|
|
%% contains the desired username.
|
2016-08-03 19:57:05 +02:00
|
|
|
#presence{} -> Resource;
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Otherwise, there is no firm
|
|
|
|
%% conclusion from the packet.
|
|
|
|
%% Better to use the configured
|
|
|
|
%% username (which defaults to the
|
|
|
|
%% username part of the JID).
|
|
|
|
_ -> Username
|
2016-08-03 19:57:05 +02:00
|
|
|
end,
|
|
|
|
Ident = extract_ident(Packet),
|
|
|
|
RemoteAddr = extract_ip_address(Packet),
|
|
|
|
RealName = get_realname(ServerHost),
|
|
|
|
WebircPassword = get_webirc_password(ServerHost),
|
|
|
|
{ok, Pid} = mod_irc_connection:start(
|
|
|
|
From, Host, ServerHost, Server,
|
|
|
|
ConnectionUsername, Encoding, Port,
|
|
|
|
Password, Ident, RemoteAddr, RealName,
|
|
|
|
WebircPassword, ?MODULE),
|
|
|
|
ets:insert(irc_connection,
|
|
|
|
#irc_connection{
|
|
|
|
jid_server_host = {From, Server, Host},
|
|
|
|
pid = Pid}),
|
|
|
|
mod_irc_connection:route_chan(Pid, Channel, Resource, Packet);
|
|
|
|
[R] ->
|
|
|
|
Pid = R#irc_connection.pid,
|
|
|
|
?DEBUG("send to process ~p~n", [Pid]),
|
|
|
|
mod_irc_connection:route_chan(Pid, Channel, Resource, Packet)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
case str:tokens(ChanServ, <<"!">>) of
|
|
|
|
[<<_, _/binary>> = Nick, <<_, _/binary>> = Server] ->
|
|
|
|
case ets:lookup(irc_connection, {From, Server, Host}) of
|
2003-02-16 21:07:21 +01:00
|
|
|
[] ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"IRC connection not found">>,
|
2016-08-03 19:57:05 +02:00
|
|
|
Err = xmpp:err_service_unavailable(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2003-02-16 21:07:21 +01:00
|
|
|
[R] ->
|
|
|
|
Pid = R#irc_connection.pid,
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("send to process ~p~n", [Pid]),
|
2016-08-03 19:57:05 +02:00
|
|
|
mod_irc_connection:route_nick(Pid, Nick, Packet)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Failed to parse chanserv">>,
|
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2016-08-03 19:57:05 +02:00
|
|
|
end
|
2003-02-16 21:07:21 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
closed_connection(Host, From, Server) ->
|
|
|
|
ets:delete(irc_connection, {From, Server, Host}).
|
2003-02-17 16:11:27 +01:00
|
|
|
|
2018-01-08 09:29:17 +01:00
|
|
|
iq_disco(ServerHost, <<"">>, Lang) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Name = gen_mod:get_module_opt(ServerHost, ?MODULE, name),
|
2016-08-03 19:57:05 +02:00
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"conference">>,
|
|
|
|
type = <<"irc">>,
|
2018-01-08 09:29:17 +01:00
|
|
|
name = translate:translate(Lang, Name)}],
|
2016-08-03 19:57:05 +02:00
|
|
|
features = [?NS_DISCO_INFO, ?NS_DISCO_ITEMS, ?NS_MUC,
|
|
|
|
?NS_REGISTER, ?NS_VCARD, ?NS_COMMANDS]};
|
2012-04-27 11:52:05 +02:00
|
|
|
iq_disco(ServerHost, Node, Lang) ->
|
2016-08-03 20:00:22 +02:00
|
|
|
case lists:keyfind(Node, 1, commands(ServerHost)) of
|
2016-08-03 19:57:05 +02:00
|
|
|
{_, Name, _} ->
|
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-node">>,
|
|
|
|
name = translate:translate(Lang, Name)}],
|
|
|
|
features = [?NS_COMMANDS, ?NS_XDATA]};
|
|
|
|
_ ->
|
|
|
|
undefined
|
2009-06-15 20:56:52 +02:00
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2004-02-26 23:00:04 +01:00
|
|
|
iq_get_vcard(Lang) ->
|
2016-08-03 19:57:05 +02:00
|
|
|
#vcard_temp{fn = <<"ejabberd/mod_irc">>,
|
2018-06-14 13:00:47 +02:00
|
|
|
url = ejabberd_config:get_uri(),
|
|
|
|
desc = misc:get_descr(Lang, <<"ejabberd IRC module">>)}.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
command_items(ServerHost, Host, Lang) ->
|
2016-08-03 19:57:05 +02:00
|
|
|
lists:map(fun({Node, Name, _Function}) ->
|
|
|
|
#disco_item{jid = jid:make(Host),
|
|
|
|
node = Node,
|
|
|
|
name = translate:translate(Lang, Name)}
|
|
|
|
end, commands(ServerHost)).
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
commands(ServerHost) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"join">>, <<"Join channel">>, fun adhoc_join/3},
|
|
|
|
{<<"register">>,
|
|
|
|
<<"Configure username, encoding, port and "
|
|
|
|
"password">>,
|
|
|
|
fun (From, To, Request) ->
|
|
|
|
adhoc_register(ServerHost, From, To, Request)
|
2012-04-27 11:52:05 +02:00
|
|
|
end}].
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_data(ServerHost, Host, From) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-14 11:18:04 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_data(LServer, Host, From).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
get_form(ServerHost, Host, From, Lang) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
#jid{user = User, server = Server} = From,
|
2010-11-02 22:40:08 +01:00
|
|
|
DefaultEncoding = get_default_encoding(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
Customs = case get_data(ServerHost, Host, From) of
|
2016-08-03 19:57:05 +02:00
|
|
|
error ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt1 = <<"Database failure">>,
|
2016-08-03 19:57:05 +02:00
|
|
|
{error, xmpp:err_internal_server_error(Txt1, Lang)};
|
|
|
|
empty -> {User, []};
|
|
|
|
Data -> get_username_and_connection_params(Data)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2003-02-23 21:13:39 +01:00
|
|
|
case Customs of
|
2016-08-03 19:57:05 +02:00
|
|
|
{error, _Error} ->
|
|
|
|
Customs;
|
|
|
|
{Username, ConnectionsParams} ->
|
|
|
|
Fs = [#xdata_field{type = 'text-single',
|
|
|
|
label = translate:translate(Lang, <<"IRC Username">>),
|
|
|
|
var = <<"username">>,
|
|
|
|
values = [Username]},
|
|
|
|
#xdata_field{type = fixed,
|
2016-11-24 13:06:06 +01:00
|
|
|
values = [str:format(
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"If you want to specify"
|
|
|
|
" different ports, "
|
|
|
|
"passwords, encodings "
|
|
|
|
"for IRC servers, "
|
|
|
|
"fill this list with "
|
|
|
|
"values in format "
|
|
|
|
"'{\"irc server\", "
|
|
|
|
"\"encoding\", port, "
|
|
|
|
"\"password\"}'. "
|
|
|
|
"By default this "
|
|
|
|
"service use \"~s\" "
|
|
|
|
"encoding, port ~p, "
|
|
|
|
"empty password.">>),
|
|
|
|
[DefaultEncoding, ?DEFAULT_IRC_PORT])]},
|
2016-08-03 19:57:05 +02:00
|
|
|
#xdata_field{type = fixed,
|
|
|
|
values = [translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Example: [{\"irc.lucky.net\", \"koi8-r\", "
|
|
|
|
"6667, \"secret\"}, {\"vendetta.fef.net\", "
|
|
|
|
"\"iso8859-1\", 7000}, {\"irc.sometestserver.n"
|
|
|
|
"et\", \"utf-8\"}].">>)]},
|
|
|
|
#xdata_field{type = 'text-multi',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang, <<"Connections parameters">>),
|
|
|
|
var = <<"connections_params">>,
|
2016-11-24 13:06:06 +01:00
|
|
|
values = str:tokens(str:format(
|
|
|
|
"~p.",
|
|
|
|
[conn_params_to_list(
|
|
|
|
ConnectionsParams)]),
|
2016-08-03 19:57:05 +02:00
|
|
|
<<"\n">>)}],
|
|
|
|
X = #xdata{type = form,
|
|
|
|
title = <<(translate:translate(
|
|
|
|
Lang, <<"Registration in mod_irc for ">>))/binary,
|
|
|
|
User/binary, "@", Server/binary>>,
|
|
|
|
instructions =
|
|
|
|
[translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Enter username, encodings, ports and "
|
|
|
|
"passwords you wish to use for connecting "
|
|
|
|
"to IRC servers">>)],
|
|
|
|
fields = Fs},
|
|
|
|
{result,
|
|
|
|
#register{instructions =
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"You need an x:data capable client to "
|
|
|
|
"configure mod_irc settings">>),
|
|
|
|
xdata = X}}
|
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
set_data(ServerHost, Host, From, Data) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-14 11:18:04 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:set_data(LServer, Host, From, data_to_binary(From, Data)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
set_form(ServerHost, Host, From, Lang, XData) ->
|
|
|
|
case {xmpp_util:get_xdata_values(<<"username">>, XData),
|
|
|
|
xmpp_util:get_xdata_values(<<"connections_params">>, XData)} of
|
|
|
|
{[Username], [_|_] = Strings} ->
|
|
|
|
EncString = lists:foldl(fun (S, Res) ->
|
|
|
|
<<Res/binary, S/binary, "\n">>
|
|
|
|
end, <<"">>, Strings),
|
|
|
|
case erl_scan:string(binary_to_list(EncString)) of
|
|
|
|
{ok, Tokens, _} ->
|
|
|
|
case erl_parse:parse_term(Tokens) of
|
|
|
|
{ok, ConnectionsParams} ->
|
|
|
|
case set_data(ServerHost, Host, From,
|
|
|
|
[{username, Username},
|
|
|
|
{connections_params, ConnectionsParams}]) of
|
|
|
|
{atomic, _} ->
|
|
|
|
{result, undefined};
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Database failure">>,
|
|
|
|
{error, xmpp:err_internal_server_error(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Parse error">>,
|
|
|
|
{error, xmpp:err_not_acceptable(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, xmpp:err_not_acceptable(<<"Scan error">>, Lang)}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Incorrect value in data form">>,
|
|
|
|
{error, xmpp:err_not_acceptable(Txt, Lang)}
|
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
get_connection_params(Host, From, IRCServer) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[_ | HostTail] = str:tokens(Host, <<".">>),
|
|
|
|
ServerHost = str:join(HostTail, <<".">>),
|
|
|
|
get_connection_params(Host, ServerHost, From,
|
|
|
|
IRCServer).
|
2010-11-02 22:40:08 +01:00
|
|
|
|
|
|
|
get_default_encoding(ServerHost) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Result = gen_mod:get_module_opt(ServerHost, ?MODULE, default_encoding),
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("The default_encoding configured for "
|
|
|
|
"host ~p is: ~p~n",
|
|
|
|
[ServerHost, Result]),
|
2010-11-02 22:40:08 +01:00
|
|
|
Result.
|
|
|
|
|
2015-12-21 16:19:24 +01:00
|
|
|
get_realname(ServerHost) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(ServerHost, ?MODULE, realname).
|
2015-12-21 16:19:24 +01:00
|
|
|
|
|
|
|
get_webirc_password(ServerHost) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(ServerHost, ?MODULE, webirc_password).
|
2015-12-21 16:19:24 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_connection_params(Host, ServerHost, From,
|
|
|
|
IRCServer) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
#jid{user = User, server = _Server} = From,
|
2010-11-02 22:40:08 +01:00
|
|
|
DefaultEncoding = get_default_encoding(ServerHost),
|
2012-04-27 11:52:05 +02:00
|
|
|
case get_data(ServerHost, Host, From) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error ->
|
|
|
|
{User, DefaultEncoding, ?DEFAULT_IRC_PORT, <<"">>};
|
|
|
|
empty ->
|
|
|
|
{User, DefaultEncoding, ?DEFAULT_IRC_PORT, <<"">>};
|
|
|
|
Data ->
|
|
|
|
{Username, ConnParams} = get_username_and_connection_params(Data),
|
|
|
|
{NewUsername, NewEncoding, NewPort, NewPassword} = case
|
|
|
|
lists:keysearch(IRCServer,
|
|
|
|
1,
|
|
|
|
ConnParams)
|
|
|
|
of
|
|
|
|
{value,
|
|
|
|
{_, Encoding,
|
|
|
|
Port,
|
|
|
|
Password}} ->
|
|
|
|
{Username,
|
|
|
|
Encoding,
|
|
|
|
Port,
|
|
|
|
Password};
|
|
|
|
{value,
|
|
|
|
{_, Encoding,
|
|
|
|
Port}} ->
|
|
|
|
{Username,
|
|
|
|
Encoding,
|
|
|
|
Port,
|
|
|
|
<<"">>};
|
|
|
|
{value,
|
|
|
|
{_,
|
|
|
|
Encoding}} ->
|
|
|
|
{Username,
|
|
|
|
Encoding,
|
|
|
|
?DEFAULT_IRC_PORT,
|
|
|
|
<<"">>};
|
|
|
|
_ ->
|
|
|
|
{Username,
|
|
|
|
DefaultEncoding,
|
|
|
|
?DEFAULT_IRC_PORT,
|
|
|
|
<<"">>}
|
|
|
|
end,
|
|
|
|
{iolist_to_binary(NewUsername),
|
|
|
|
iolist_to_binary(NewEncoding),
|
|
|
|
if NewPort >= 0 andalso NewPort =< 65535 -> NewPort;
|
|
|
|
true -> ?DEFAULT_IRC_PORT
|
|
|
|
end,
|
|
|
|
iolist_to_binary(NewPassword)}
|
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
adhoc_join(_From, _To, #adhoc_command{action = cancel} = Request) ->
|
|
|
|
xmpp_util:make_adhoc_response(Request, #adhoc_command{status = canceled});
|
|
|
|
adhoc_join(_From, _To, #adhoc_command{lang = Lang, xdata = undefined} = Request) ->
|
|
|
|
X = #xdata{type = form,
|
|
|
|
title = translate:translate(Lang, <<"Join IRC channel">>),
|
|
|
|
fields = [#xdata_field{var = <<"channel">>,
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang, <<"IRC channel (don't put the first #)">>),
|
|
|
|
required = true},
|
|
|
|
#xdata_field{var = <<"server">>,
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(Lang, <<"IRC server">>),
|
|
|
|
required = true}]},
|
2016-08-03 20:00:22 +02:00
|
|
|
xmpp_util:make_adhoc_response(
|
2016-08-03 19:57:05 +02:00
|
|
|
Request, #adhoc_command{status = executing, xdata = X});
|
|
|
|
adhoc_join(From, To, #adhoc_command{lang = Lang, xdata = X} = Request) ->
|
|
|
|
Channel = case xmpp_util:get_xdata_values(<<"channel">>, X) of
|
|
|
|
[C] -> C;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
Server = case xmpp_util:get_xdata_values(<<"server">>, X) of
|
|
|
|
[S] -> S;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if Channel /= false, Server /= false ->
|
|
|
|
RoomJID = jid:make(<<Channel/binary, "%", Server/binary>>,
|
|
|
|
To#jid.server),
|
|
|
|
Reason = translate:translate(Lang, <<"Join the IRC channel here.">>),
|
2016-11-26 08:05:22 +01:00
|
|
|
BodyTxt = {<<"Join the IRC channel in this Jabber ID: ~s">>,
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(RoomJID)]},
|
2016-08-03 19:57:05 +02:00
|
|
|
Invite = #message{
|
2017-02-16 09:00:26 +01:00
|
|
|
from = RoomJID, to = From,
|
2016-11-26 08:05:22 +01:00
|
|
|
body = xmpp:mk_text(BodyTxt, Lang),
|
2016-08-03 19:57:05 +02:00
|
|
|
sub_els = [#muc_user{
|
|
|
|
invites = [#muc_invite{from = From,
|
|
|
|
reason = Reason}]},
|
|
|
|
#x_conference{reason = Reason,
|
|
|
|
jid = RoomJID}]},
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Invite),
|
2016-08-03 19:57:05 +02:00
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request, #adhoc_command{status = completed});
|
2009-06-15 20:56:52 +02:00
|
|
|
true ->
|
2016-08-03 19:57:05 +02:00
|
|
|
Txt = <<"Missing 'channel' or 'server' in the data form">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2009-06-15 20:56:52 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
-spec adhoc_register(binary(), jid(), jid(), adhoc_command()) ->
|
2016-09-08 16:08:48 +02:00
|
|
|
adhoc_command() | {error, stanza_error()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
adhoc_register(_ServerHost, _From, _To,
|
2016-08-03 19:57:05 +02:00
|
|
|
#adhoc_command{action = cancel} = Request) ->
|
|
|
|
xmpp_util:make_adhoc_response(Request, #adhoc_command{status = canceled});
|
2013-03-14 10:33:02 +01:00
|
|
|
adhoc_register(ServerHost, From, To,
|
2016-08-03 19:57:05 +02:00
|
|
|
#adhoc_command{lang = Lang, xdata = X,
|
|
|
|
action = Action} = Request) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
#jid{user = User} = From,
|
2009-06-15 20:56:52 +02:00
|
|
|
#jid{lserver = Host} = To,
|
2016-08-03 19:57:05 +02:00
|
|
|
{Username, ConnectionsParams} =
|
|
|
|
if X == undefined ->
|
|
|
|
case get_data(ServerHost, Host, From) of
|
|
|
|
error -> {User, []};
|
|
|
|
empty -> {User, []};
|
|
|
|
Data -> get_username_and_connection_params(Data)
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{case xmpp_util:get_xdata_values(<<"username">>, X) of
|
|
|
|
[U] -> U;
|
|
|
|
_ -> User
|
|
|
|
end, parse_connections_params(X)}
|
|
|
|
end,
|
|
|
|
if Action == complete ->
|
|
|
|
case set_data(ServerHost, Host, From,
|
|
|
|
[{username, Username},
|
|
|
|
{connections_params, ConnectionsParams}]) of
|
|
|
|
{atomic, _} ->
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request, #adhoc_command{status = completed});
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Database failure">>,
|
|
|
|
{error, xmpp:err_internal_server_error(Txt, Lang)}
|
|
|
|
end;
|
2009-06-15 20:56:52 +02:00
|
|
|
true ->
|
2016-08-03 19:57:05 +02:00
|
|
|
Form = generate_adhoc_register_form(Lang, Username,
|
|
|
|
ConnectionsParams),
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request, #adhoc_command{
|
|
|
|
status = executing,
|
|
|
|
xdata = Form,
|
|
|
|
actions = #adhoc_actions{next = true,
|
|
|
|
complete = true}})
|
2009-06-15 20:56:52 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
generate_adhoc_register_form(Lang, Username,
|
|
|
|
ConnectionsParams) ->
|
2016-08-03 19:57:05 +02:00
|
|
|
#xdata{type = form,
|
|
|
|
title = translate:translate(Lang, <<"IRC settings">>),
|
|
|
|
instructions = [translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Enter username and encodings you wish "
|
|
|
|
"to use for connecting to IRC servers. "
|
|
|
|
" Press 'Next' to get more fields to "
|
|
|
|
"fill in. Press 'Complete' to save settings.">>)],
|
|
|
|
fields = [#xdata_field{
|
|
|
|
var = <<"username">>,
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(Lang, <<"IRC username">>),
|
|
|
|
required = true,
|
|
|
|
values = [Username]}
|
|
|
|
| generate_connection_params_fields(
|
|
|
|
Lang, ConnectionsParams, 1, [])]}.
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
generate_connection_params_fields(Lang, [], Number,
|
|
|
|
Acc) ->
|
|
|
|
Field = generate_connection_params_field(Lang, <<"">>,
|
|
|
|
<<"">>, -1, <<"">>, Number),
|
2009-06-15 20:56:52 +02:00
|
|
|
lists:reverse(Field ++ Acc);
|
2013-03-14 10:33:02 +01:00
|
|
|
generate_connection_params_fields(Lang,
|
|
|
|
[ConnectionParams | ConnectionsParams],
|
|
|
|
Number, Acc) ->
|
2009-08-07 16:43:44 +02:00
|
|
|
case ConnectionParams of
|
2013-03-14 10:33:02 +01:00
|
|
|
{Server, Encoding, Port, Password} ->
|
|
|
|
Field = generate_connection_params_field(Lang, Server,
|
|
|
|
Encoding, Port, Password,
|
|
|
|
Number),
|
|
|
|
generate_connection_params_fields(Lang,
|
|
|
|
ConnectionsParams, Number + 1,
|
|
|
|
Field ++ Acc);
|
|
|
|
{Server, Encoding, Port} ->
|
|
|
|
Field = generate_connection_params_field(Lang, Server,
|
|
|
|
Encoding, Port, <<"">>, Number),
|
|
|
|
generate_connection_params_fields(Lang,
|
|
|
|
ConnectionsParams, Number + 1,
|
|
|
|
Field ++ Acc);
|
|
|
|
{Server, Encoding} ->
|
|
|
|
Field = generate_connection_params_field(Lang, Server,
|
|
|
|
Encoding, -1, <<"">>, Number),
|
|
|
|
generate_connection_params_fields(Lang,
|
|
|
|
ConnectionsParams, Number + 1,
|
|
|
|
Field ++ Acc);
|
|
|
|
_ -> []
|
2009-08-07 16:43:44 +02:00
|
|
|
end.
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
generate_connection_params_field(Lang, Server, Encoding,
|
|
|
|
Port, Password, Number) ->
|
2009-06-15 20:56:52 +02:00
|
|
|
EncodingUsed = case Encoding of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<>> -> get_default_encoding(Server);
|
|
|
|
_ -> Encoding
|
2009-06-15 20:56:52 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
PortUsedInt = if Port >= 0 andalso Port =< 65535 ->
|
|
|
|
Port;
|
|
|
|
true -> ?DEFAULT_IRC_PORT
|
|
|
|
end,
|
2016-11-24 13:06:06 +01:00
|
|
|
PortUsed = integer_to_binary(PortUsedInt),
|
2009-08-07 16:43:44 +02:00
|
|
|
PasswordUsed = case Password of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<>> -> <<>>;
|
|
|
|
_ -> Password
|
|
|
|
end,
|
2016-11-24 13:06:06 +01:00
|
|
|
NumberString = integer_to_binary(Number),
|
2016-08-03 19:57:05 +02:00
|
|
|
[#xdata_field{var = <<"password", NumberString/binary>>,
|
|
|
|
type = 'text-single',
|
2016-11-24 13:06:06 +01:00
|
|
|
label = str:format(
|
|
|
|
translate:translate(Lang, <<"Password ~b">>),
|
|
|
|
[Number]),
|
2016-08-03 19:57:05 +02:00
|
|
|
values = [PasswordUsed]},
|
|
|
|
#xdata_field{var = <<"port", NumberString/binary>>,
|
|
|
|
type = 'text-single',
|
2016-11-24 13:06:06 +01:00
|
|
|
label = str:format(
|
|
|
|
translate:translate(Lang, <<"Port ~b">>),
|
|
|
|
[Number]),
|
2016-08-03 19:57:05 +02:00
|
|
|
values = [PortUsed]},
|
|
|
|
#xdata_field{var = <<"encoding", NumberString/binary>>,
|
|
|
|
type = 'list-single',
|
2016-11-24 13:06:06 +01:00
|
|
|
label = str:format(
|
|
|
|
translate:translate(Lang, <<"Encoding for server ~b">>),
|
|
|
|
[Number]),
|
2016-08-03 19:57:05 +02:00
|
|
|
values = [EncodingUsed],
|
|
|
|
options = [#xdata_option{label = E, value = E}
|
|
|
|
|| E <- ?POSSIBLE_ENCODINGS]},
|
|
|
|
#xdata_field{var = <<"server", NumberString/binary>>,
|
|
|
|
type = 'text-single',
|
2016-11-24 13:06:06 +01:00
|
|
|
label = str:format(
|
|
|
|
translate:translate(Lang, <<"Server ~b">>),
|
|
|
|
[Number]),
|
2016-08-03 19:57:05 +02:00
|
|
|
values = [Server]}].
|
|
|
|
|
|
|
|
parse_connections_params(#xdata{fields = Fields}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Servers = lists:flatmap(
|
2016-08-03 19:57:05 +02:00
|
|
|
fun(#xdata_field{var = <<"server", Var/binary>>,
|
|
|
|
values = Values}) ->
|
|
|
|
[{Var, Values}];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fields),
|
|
|
|
Encodings = lists:flatmap(
|
2016-08-03 19:57:05 +02:00
|
|
|
fun(#xdata_field{var = <<"encoding", Var/binary>>,
|
|
|
|
values = Values}) ->
|
|
|
|
[{Var, Values}];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fields),
|
|
|
|
Ports = lists:flatmap(
|
2016-08-03 19:57:05 +02:00
|
|
|
fun(#xdata_field{var = <<"port", Var/binary>>,
|
|
|
|
values = Values}) ->
|
|
|
|
[{Var, Values}];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fields),
|
|
|
|
Passwords = lists:flatmap(
|
2016-08-03 19:57:05 +02:00
|
|
|
fun(#xdata_field{var = <<"password", Var/binary>>,
|
|
|
|
values = Values}) ->
|
|
|
|
[{Var, Values}];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fields),
|
2016-08-03 19:57:05 +02:00
|
|
|
parse_connections_params(Servers, Encodings, Ports, Passwords).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
retrieve_connections_params(ConnectionParams,
|
|
|
|
ServerN) ->
|
2009-08-07 16:43:44 +02:00
|
|
|
case ConnectionParams of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{ConnectionParamN, ConnectionParam}
|
|
|
|
| ConnectionParamsTail] ->
|
|
|
|
if ServerN == ConnectionParamN ->
|
|
|
|
{ConnectionParam, ConnectionParamsTail};
|
|
|
|
ServerN < ConnectionParamN ->
|
|
|
|
{[],
|
|
|
|
[{ConnectionParamN, ConnectionParam}
|
|
|
|
| ConnectionParamsTail]};
|
|
|
|
ServerN > ConnectionParamN -> {[], ConnectionParamsTail}
|
|
|
|
end;
|
|
|
|
_ -> {[], []}
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
parse_connections_params([], _, _, _) -> [];
|
|
|
|
parse_connections_params(_, [], [], []) -> [];
|
|
|
|
parse_connections_params([{ServerN, Server} | Servers],
|
|
|
|
Encodings, Ports, Passwords) ->
|
|
|
|
{NewEncoding, NewEncodings} =
|
|
|
|
retrieve_connections_params(Encodings, ServerN),
|
|
|
|
{NewPort, NewPorts} = retrieve_connections_params(Ports,
|
|
|
|
ServerN),
|
|
|
|
{NewPassword, NewPasswords} =
|
|
|
|
retrieve_connections_params(Passwords, ServerN),
|
|
|
|
[{Server, NewEncoding, NewPort, NewPassword}
|
|
|
|
| parse_connections_params(Servers, NewEncodings,
|
|
|
|
NewPorts, NewPasswords)].
|
|
|
|
|
|
|
|
get_username_and_connection_params(Data) ->
|
|
|
|
Username = case lists:keysearch(username, 1, Data) of
|
|
|
|
{value, {_, U}} when is_binary(U) ->
|
|
|
|
U;
|
|
|
|
_ ->
|
|
|
|
<<"">>
|
|
|
|
end,
|
|
|
|
ConnParams = case lists:keysearch(connections_params, 1, Data) of
|
|
|
|
{value, {_, L}} when is_list(L) ->
|
|
|
|
L;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{Username, ConnParams}.
|
|
|
|
|
2014-06-07 05:45:19 +02:00
|
|
|
data_to_binary(JID, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(
|
|
|
|
fun({username, U}) ->
|
|
|
|
{username, iolist_to_binary(U)};
|
|
|
|
({connections_params, Params}) ->
|
2014-06-07 05:45:19 +02:00
|
|
|
{connections_params,
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Param) ->
|
|
|
|
try
|
|
|
|
[conn_param_to_binary(Param)]
|
|
|
|
catch _:_ ->
|
2014-06-09 08:35:52 +02:00
|
|
|
if JID /= error ->
|
|
|
|
?ERROR_MSG("failed to convert "
|
|
|
|
"parameter ~p for user ~s",
|
|
|
|
[Param,
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode(JID)]);
|
2014-06-09 08:35:52 +02:00
|
|
|
true ->
|
|
|
|
?ERROR_MSG("failed to convert "
|
|
|
|
"parameter ~p",
|
|
|
|
[Param])
|
2014-06-09 08:40:35 +02:00
|
|
|
end,
|
|
|
|
[]
|
2014-06-07 05:45:19 +02:00
|
|
|
end
|
|
|
|
end, Params)};
|
2013-03-14 10:33:02 +01:00
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Data).
|
|
|
|
|
2014-06-07 05:45:19 +02:00
|
|
|
conn_param_to_binary({S}) ->
|
|
|
|
{iolist_to_binary(S)};
|
|
|
|
conn_param_to_binary({S, E}) ->
|
|
|
|
{iolist_to_binary(S), iolist_to_binary(E)};
|
|
|
|
conn_param_to_binary({S, E, Port}) when is_integer(Port) ->
|
|
|
|
{iolist_to_binary(S), iolist_to_binary(E), Port};
|
|
|
|
conn_param_to_binary({S, E, Port, P}) when is_integer(Port) ->
|
|
|
|
{iolist_to_binary(S), iolist_to_binary(E), Port, iolist_to_binary(P)}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
conn_params_to_list(Params) ->
|
|
|
|
lists:map(
|
2012-10-25 14:42:58 +02:00
|
|
|
fun({S}) ->
|
|
|
|
{binary_to_list(S)};
|
|
|
|
({S, E}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{binary_to_list(S), binary_to_list(E)};
|
|
|
|
({S, E, Port}) ->
|
|
|
|
{binary_to_list(S), binary_to_list(E), Port};
|
|
|
|
({S, E, Port, P}) ->
|
|
|
|
{binary_to_list(S), binary_to_list(E),
|
|
|
|
Port, binary_to_list(P)}
|
|
|
|
end, Params).
|
|
|
|
|
2016-04-14 11:18:04 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-04-14 11:18:04 +02:00
|
|
|
import(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-04-14 11:18:04 +02:00
|
|
|
import(LServer, DBType, Data) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, Data).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(default_encoding) ->
|
|
|
|
fun iolist_to_binary/1;
|
2018-01-08 09:29:17 +01:00
|
|
|
mod_opt_type(name) ->
|
|
|
|
fun iolist_to_binary/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
2017-08-08 16:46:26 +02:00
|
|
|
mod_opt_type(hosts) ->
|
|
|
|
fun (L) -> lists:map(fun iolist_to_binary/1, L) end;
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type(realname) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(webirc_password) ->
|
2018-02-11 10:54:15 +01:00
|
|
|
fun iolist_to_binary/1.
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{access, all},
|
|
|
|
{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
|
|
|
{default_encoding, <<"iso8859-15">>},
|
|
|
|
{host, <<"irc.@HOST@">>},
|
|
|
|
{hosts, []},
|
|
|
|
{realname, <<"WebIRC-User">>},
|
|
|
|
{webirc_password, <<"">>},
|
|
|
|
{name, ?T("IRC Transport")}].
|
2015-12-21 16:19:24 +01:00
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
-spec extract_ident(stanza()) -> binary().
|
2015-12-21 16:19:24 +01:00
|
|
|
extract_ident(Packet) ->
|
2016-08-03 19:57:05 +02:00
|
|
|
Hdrs = extract_headers(Packet),
|
|
|
|
proplists:get_value(<<"X-Irc-Ident">>, Hdrs, <<"chatmovil">>).
|
2015-12-21 16:19:24 +01:00
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
-spec extract_ip_address(stanza()) -> binary().
|
2015-12-21 16:19:24 +01:00
|
|
|
extract_ip_address(Packet) ->
|
2016-08-03 19:57:05 +02:00
|
|
|
Hdrs = extract_headers(Packet),
|
|
|
|
proplists:get_value(<<"X-Forwarded-For">>, Hdrs, <<"127.0.0.1">>).
|
|
|
|
|
|
|
|
-spec extract_headers(stanza()) -> [{binary(), binary()}].
|
|
|
|
extract_headers(Packet) ->
|
|
|
|
case xmpp:get_subtag(Packet, #shim{}) of
|
|
|
|
#shim{headers = Hs} -> Hs;
|
|
|
|
false -> []
|
2015-12-21 16:19:24 +01:00
|
|
|
end.
|