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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:11:32 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
%%%
|
2007-12-24 14:57:53 +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
|
|
|
|
%%%
|
2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_irc).
|
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).
|
2003-02-16 21:07:21 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%% API
|
|
|
|
-export([start_link/2,
|
|
|
|
start/2,
|
|
|
|
stop/1,
|
2009-06-15 20:56:52 +02:00
|
|
|
closed_connection/3,
|
2009-08-07 16:43:44 +02:00
|
|
|
get_connection_params/3]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2009-06-15 20:56:52 +02:00
|
|
|
-include("adhoc.hrl").
|
|
|
|
|
|
|
|
-define(DEFAULT_IRC_ENCODING, "iso8859-1").
|
2009-08-07 16:43:44 +02:00
|
|
|
-define(DEFAULT_IRC_PORT, 6667).
|
2009-06-15 20:56:52 +02:00
|
|
|
-define(POSSIBLE_ENCODINGS, ["koi8-r", "iso8859-1", "iso8859-2", "utf-8", "utf-8+latin-1"]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(irc_connection, {jid_server_host, pid}).
|
|
|
|
-record(irc_custom, {us_host, data}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
-record(state, {host, server_host, access}).
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2005-05-23 02:30:29 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_irc).
|
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2006-02-03 04:28:15 +01:00
|
|
|
start_supervisor(Host),
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
ChildSpec =
|
|
|
|
{Proc,
|
|
|
|
{?MODULE, start_link, [Host, Opts]},
|
|
|
|
temporary,
|
|
|
|
1000,
|
|
|
|
worker,
|
|
|
|
[?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
stop_supervisor(Host),
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Opts]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
iconv:start(),
|
2003-02-23 21:13:39 +01:00
|
|
|
mnesia:create_table(irc_custom,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, irc_custom)}]),
|
2007-08-25 19:24:00 +02:00
|
|
|
MyHost = gen_mod:get_opt_host(Host, Opts, "irc.@HOST@"),
|
2005-06-20 05:18:13 +02:00
|
|
|
update_table(MyHost),
|
2004-07-11 22:51:54 +02:00
|
|
|
Access = gen_mod:get_opt(access, Opts, all),
|
2003-02-16 21:07:21 +01:00
|
|
|
catch ets:new(irc_connection, [named_table,
|
|
|
|
public,
|
2005-04-17 20:08:34 +02:00
|
|
|
{keypos, #irc_connection.jid_server_host}]),
|
2006-02-03 04:28:15 +01:00
|
|
|
ejabberd_router:register_route(MyHost),
|
|
|
|
{ok, #state{host = MyHost,
|
|
|
|
server_host = Host,
|
|
|
|
access = Access}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({route, From, To, Packet},
|
|
|
|
#state{host = Host,
|
|
|
|
server_host = ServerHost,
|
|
|
|
access = Access} = State) ->
|
2009-06-15 20:56:52 +02:00
|
|
|
case catch do_route(Host, ServerHost, Access, From, To, Packet) of
|
2006-02-03 04:28:15 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
2003-02-16 21:07:21 +01:00
|
|
|
_ ->
|
2006-02-03 04:28:15 +01:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
ejabberd_router:unregister_route(State#state.host),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_supervisor(Host) ->
|
|
|
|
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]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-03 04:28:15 +01:00
|
|
|
stop_supervisor(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ejabberd_mod_irc_sup),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
do_route(Host, ServerHost, Access, From, To, Packet) ->
|
2007-12-10 15:44:43 +01:00
|
|
|
case acl:match_rule(ServerHost, Access, From) of
|
2004-07-11 22:51:54 +02:00
|
|
|
allow ->
|
2009-06-15 20:56:52 +02:00
|
|
|
do_route1(Host, ServerHost, From, To, Packet);
|
2004-07-11 22:51:54 +02:00
|
|
|
_ ->
|
|
|
|
{xmlelement, _Name, Attrs, _Els} = Packet,
|
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
|
|
|
ErrText = "Access denied by service policy",
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_FORBIDDEN(Lang, ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end.
|
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
do_route1(Host, ServerHost, From, To, Packet) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = ChanServ, resource = Resource} = To,
|
2007-12-07 01:48:11 +01:00
|
|
|
{xmlelement, _Name, _Attrs, _Els} = Packet,
|
2003-02-16 21:07:21 +01:00
|
|
|
case ChanServ of
|
|
|
|
"" ->
|
2003-02-17 20:43:50 +01:00
|
|
|
case Resource of
|
|
|
|
"" ->
|
|
|
|
case jlib:iq_query_info(Packet) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_INFO = XMLNS,
|
2009-06-15 20:56:52 +02:00
|
|
|
sub_el = SubEl, lang = Lang} = IQ ->
|
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
2009-07-17 22:45:44 +02:00
|
|
|
Info = ejabberd_hooks:run_fold(
|
|
|
|
disco_info, ServerHost, [],
|
|
|
|
[ServerHost, ?MODULE, "", ""]),
|
2009-06-15 20:56:52 +02:00
|
|
|
case iq_disco(Node, Lang) of
|
|
|
|
[] ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
[]}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
DiscoInfo ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
2009-07-17 22:45:44 +02:00
|
|
|
DiscoInfo ++ Info}]},
|
2009-06-15 20:56:52 +02:00
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res))
|
|
|
|
end;
|
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_ITEMS = XMLNS,
|
|
|
|
sub_el = SubEl, lang = Lang} = IQ ->
|
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
|
|
|
case Node of
|
|
|
|
[] ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
[]}]},
|
|
|
|
Res = jlib:iq_to_xml(ResIQ);
|
|
|
|
"join" ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
[]}]},
|
|
|
|
Res = jlib:iq_to_xml(ResIQ);
|
|
|
|
"register" ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
[]}]},
|
|
|
|
Res = jlib:iq_to_xml(ResIQ);
|
|
|
|
?NS_COMMANDS ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS},
|
|
|
|
{"node", Node}],
|
|
|
|
command_items(Host, Lang)}]},
|
|
|
|
Res = jlib:iq_to_xml(ResIQ);
|
|
|
|
_ ->
|
|
|
|
Res = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_ITEM_NOT_FOUND)
|
|
|
|
end,
|
2004-07-10 00:34:26 +02:00
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
2009-06-15 20:56:52 +02:00
|
|
|
Res);
|
2004-02-26 23:00:04 +01:00
|
|
|
#iq{xmlns = ?NS_REGISTER} = IQ ->
|
2009-06-15 20:56:52 +02:00
|
|
|
process_register(Host, From, To, IQ);
|
2004-02-26 23:00:04 +01:00
|
|
|
#iq{type = get, xmlns = ?NS_VCARD = XMLNS,
|
|
|
|
lang = Lang} = IQ ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
2004-03-02 22:16:55 +01:00
|
|
|
[{xmlelement, "vCard",
|
2004-02-26 23:00:04 +01:00
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
iq_get_vcard(Lang)}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res));
|
2009-06-15 20:56:52 +02:00
|
|
|
#iq{type = set, xmlns = ?NS_COMMANDS,
|
|
|
|
lang = _Lang, sub_el = SubEl} = IQ ->
|
|
|
|
Request = adhoc:parse_request(IQ),
|
|
|
|
case lists:keysearch(Request#adhoc_request.node, 1, commands()) of
|
|
|
|
{value, {_, _, Function}} ->
|
|
|
|
case catch Function(From, To, Request) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nfor ad-hoc handler of ~p",
|
|
|
|
[Reason, {From, To, IQ}]),
|
|
|
|
Res = IQ#iq{type = error, sub_el = [SubEl,
|
|
|
|
?ERR_INTERNAL_SERVER_ERROR]};
|
|
|
|
ignore ->
|
|
|
|
Res = ignore;
|
|
|
|
{error, Error} ->
|
|
|
|
Res = IQ#iq{type = error, sub_el = [SubEl, Error]};
|
|
|
|
Command ->
|
|
|
|
Res = IQ#iq{type = result, sub_el = [Command]}
|
|
|
|
end,
|
|
|
|
if Res /= ignore ->
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(Res));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_ITEM_NOT_FOUND),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2007-12-07 01:48:11 +01:00
|
|
|
#iq{} = _IQ ->
|
2003-02-17 20:43:50 +01:00
|
|
|
Err = jlib:make_error_reply(
|
2003-12-17 21:13:21 +01:00
|
|
|
Packet, ?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-02-17 20:43:50 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2003-10-07 22:31:44 +02:00
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST),
|
2003-02-17 20:43:50 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2003-02-16 21:07:21 +01:00
|
|
|
_ ->
|
|
|
|
case string:tokens(ChanServ, "%") of
|
|
|
|
[[_ | _] = Channel, [_ | _] = Server] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case ets:lookup(irc_connection, {From, Server, Host}) of
|
2003-02-16 21:07:21 +01:00
|
|
|
[] ->
|
2008-03-21 15:44:16 +01:00
|
|
|
?DEBUG("open new connection~n", []),
|
2009-08-07 16:43:44 +02:00
|
|
|
{Username, Encoding, Port, Password} = get_connection_params(
|
2010-11-02 22:40:08 +01:00
|
|
|
Host, ServerHost, From, Server),
|
2009-06-15 20:56:52 +02:00
|
|
|
ConnectionUsername =
|
|
|
|
case Packet of
|
|
|
|
%% If the user tries to join a
|
|
|
|
%% chatroom, the packet for sure
|
|
|
|
%% contains the desired username.
|
|
|
|
{xmlelement, "presence", _, _} ->
|
|
|
|
Resource;
|
|
|
|
%% 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
|
|
|
|
end,
|
2003-02-16 21:07:21 +01:00
|
|
|
{ok, Pid} = mod_irc_connection:start(
|
2006-02-03 04:28:15 +01:00
|
|
|
From, Host, ServerHost, Server,
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionUsername, Encoding, Port, Password),
|
2003-02-16 21:07:21 +01:00
|
|
|
ets:insert(
|
|
|
|
irc_connection,
|
2005-04-17 20:08:34 +02:00
|
|
|
#irc_connection{jid_server_host = {From, Server, Host},
|
2003-02-16 21:07:21 +01:00
|
|
|
pid = Pid}),
|
2003-02-18 21:33:10 +01:00
|
|
|
mod_irc_connection:route_chan(
|
2003-02-16 21:07:21 +01:00
|
|
|
Pid, Channel, Resource, Packet),
|
|
|
|
ok;
|
|
|
|
[R] ->
|
|
|
|
Pid = R#irc_connection.pid,
|
2008-03-21 15:44:16 +01:00
|
|
|
?DEBUG("send to process ~p~n",
|
2003-02-16 21:07:21 +01:00
|
|
|
[Pid]),
|
2003-02-18 21:33:10 +01:00
|
|
|
mod_irc_connection:route_chan(
|
2003-02-16 21:07:21 +01:00
|
|
|
Pid, Channel, Resource, Packet),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2003-02-18 21:33:10 +01:00
|
|
|
case string:tokens(ChanServ, "!") of
|
|
|
|
[[_ | _] = Nick, [_ | _] = Server] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case ets:lookup(irc_connection, {From, Server, Host}) of
|
2003-02-18 21:33:10 +01:00
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-10-07 22:31:44 +02:00
|
|
|
Packet, ?ERR_SERVICE_UNAVAILABLE),
|
2003-02-18 21:33:10 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
[R] ->
|
|
|
|
Pid = R#irc_connection.pid,
|
2008-03-21 15:44:16 +01:00
|
|
|
?DEBUG("send to process ~p~n",
|
2003-02-18 21:33:10 +01:00
|
|
|
[Pid]),
|
|
|
|
mod_irc_connection:route_nick(
|
|
|
|
Pid, Nick, Packet),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-10-07 22:31:44 +02:00
|
|
|
Packet, ?ERR_BAD_REQUEST),
|
2003-02-18 21:33:10 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
2003-02-16 21:07:21 +01:00
|
|
|
end
|
|
|
|
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
|
|
|
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
iq_disco([], Lang) ->
|
2003-02-23 21:13:39 +01:00
|
|
|
[{xmlelement, "identity",
|
|
|
|
[{"category", "conference"},
|
|
|
|
{"type", "irc"},
|
2007-05-29 15:14:09 +02:00
|
|
|
{"name", translate:translate(Lang, "IRC Transport")}], []},
|
2008-11-26 17:04:27 +01:00
|
|
|
{xmlelement, "feature", [{"var", ?NS_DISCO_INFO}], []},
|
|
|
|
{xmlelement, "feature", [{"var", ?NS_MUC}], []},
|
|
|
|
{xmlelement, "feature", [{"var", ?NS_REGISTER}], []},
|
2009-06-15 20:56:52 +02:00
|
|
|
{xmlelement, "feature", [{"var", ?NS_VCARD}], []},
|
|
|
|
{xmlelement, "feature", [{"var", ?NS_COMMANDS}], []}];
|
|
|
|
iq_disco(Node, Lang) ->
|
|
|
|
case lists:keysearch(Node, 1, commands()) of
|
|
|
|
{value, {_, Name, _}} ->
|
|
|
|
[{xmlelement, "identity",
|
|
|
|
[{"category", "automation"},
|
|
|
|
{"type", "command-node"},
|
|
|
|
{"name", translate:translate(Lang, Name)}], []},
|
|
|
|
{xmlelement, "feature",
|
|
|
|
[{"var", ?NS_COMMANDS}], []},
|
|
|
|
{xmlelement, "feature",
|
|
|
|
[{"var", ?NS_XDATA}], []}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2004-02-26 23:00:04 +01:00
|
|
|
iq_get_vcard(Lang) ->
|
|
|
|
[{xmlelement, "FN", [],
|
|
|
|
[{xmlcdata, "ejabberd/mod_irc"}]},
|
|
|
|
{xmlelement, "URL", [],
|
2007-11-28 00:35:26 +01:00
|
|
|
[{xmlcdata, ?EJABBERD_URI}]},
|
2004-02-26 23:00:04 +01:00
|
|
|
{xmlelement, "DESC", [],
|
2009-06-15 20:56:52 +02:00
|
|
|
[{xmlcdata, translate:translate(Lang, "ejabberd IRC module") ++
|
2010-01-12 17:11:32 +01:00
|
|
|
"\nCopyright (c) 2003-2010 Alexey Shchepin"}]}].
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
command_items(Host, Lang) ->
|
|
|
|
lists:map(fun({Node, Name, _Function})
|
|
|
|
-> {xmlelement, "item",
|
|
|
|
[{"jid", Host},
|
|
|
|
{"node", Node},
|
|
|
|
{"name", translate:translate(Lang, Name)}], []}
|
|
|
|
end, commands()).
|
|
|
|
|
|
|
|
commands() ->
|
|
|
|
[{"join", "Join channel", fun adhoc_join/3},
|
2009-08-07 16:43:44 +02:00
|
|
|
{"register", "Configure username, encoding, port and password", fun adhoc_register/3}].
|
2009-06-15 20:56:52 +02:00
|
|
|
|
|
|
|
process_register(Host, From, To, #iq{} = IQ) ->
|
|
|
|
case catch process_irc_register(Host, From, To, IQ) of
|
2003-02-23 21:13:39 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
ResIQ ->
|
|
|
|
if
|
|
|
|
ResIQ /= ignore ->
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(ResIQ));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2004-02-10 21:50:41 +01:00
|
|
|
find_xdata_el({xmlelement, _Name, _Attrs, SubEls}) ->
|
|
|
|
find_xdata_el1(SubEls).
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2004-02-10 21:50:41 +01:00
|
|
|
find_xdata_el1([]) ->
|
|
|
|
false;
|
|
|
|
|
|
|
|
find_xdata_el1([{xmlelement, Name, Attrs, SubEls} | Els]) ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_XDATA ->
|
|
|
|
{xmlelement, Name, Attrs, SubEls};
|
|
|
|
_ ->
|
|
|
|
find_xdata_el1(Els)
|
|
|
|
end;
|
|
|
|
|
|
|
|
find_xdata_el1([_ | Els]) ->
|
|
|
|
find_xdata_el1(Els).
|
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
process_irc_register(Host, From, _To,
|
2004-02-26 23:00:04 +01:00
|
|
|
#iq{type = Type, xmlns = XMLNS,
|
|
|
|
lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-02-23 21:13:39 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2004-02-10 21:50:41 +01:00
|
|
|
XDataEl = find_xdata_el(SubEl),
|
|
|
|
case XDataEl of
|
|
|
|
false ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ACCEPTABLE]};
|
2007-12-07 01:48:11 +01:00
|
|
|
{xmlelement, _Name, Attrs, _SubEls} ->
|
2004-02-10 21:50:41 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"cancel" ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}], []}]};
|
|
|
|
"submit" ->
|
|
|
|
XData = jlib:parse_xdata_submit(XDataEl),
|
|
|
|
case XData of
|
|
|
|
invalid ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
2004-02-10 21:50:41 +01:00
|
|
|
sub_el = [SubEl, ?ERR_BAD_REQUEST]};
|
|
|
|
_ ->
|
|
|
|
Node = string:tokens(
|
|
|
|
xml:get_tag_attr_s("node", SubEl),
|
|
|
|
"/"),
|
2005-04-17 20:08:34 +02:00
|
|
|
case set_form(
|
|
|
|
Host, From, Node, Lang, XData) of
|
2004-02-10 21:50:41 +01:00
|
|
|
{result, Res} ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
Res
|
|
|
|
}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, Error]}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_BAD_REQUEST]}
|
|
|
|
end
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
get ->
|
|
|
|
Node =
|
|
|
|
string:tokens(xml:get_tag_attr_s("node", SubEl), "/"),
|
2009-06-15 20:56:52 +02:00
|
|
|
case get_form(Host, From, Node, Lang) of
|
2003-02-23 21:13:39 +01:00
|
|
|
{result, Res} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
Res
|
|
|
|
}]};
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, Error} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, Error]}
|
2003-02-23 21:13:39 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
get_form(Host, From, [], Lang) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, server = Server,
|
|
|
|
luser = LUser, lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2010-11-02 22:40:08 +01:00
|
|
|
DefaultEncoding = get_default_encoding(Host),
|
2003-02-23 21:13:39 +01:00
|
|
|
Customs =
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_read({irc_custom, {US, Host}}) of
|
2007-12-07 01:48:11 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR};
|
2003-02-23 21:13:39 +01:00
|
|
|
[] ->
|
|
|
|
{User, []};
|
|
|
|
[#irc_custom{data = Data}] ->
|
|
|
|
{xml:get_attr_s(username, Data),
|
2009-08-07 16:43:44 +02:00
|
|
|
xml:get_attr_s(connections_params, Data)}
|
2003-02-23 21:13:39 +01:00
|
|
|
end,
|
|
|
|
case Customs of
|
2005-04-17 20:08:34 +02:00
|
|
|
{error, _Error} ->
|
2003-02-23 21:13:39 +01:00
|
|
|
Customs;
|
2009-08-07 16:43:44 +02:00
|
|
|
{Username, ConnectionsParams} ->
|
2003-02-23 21:13:39 +01:00
|
|
|
{result,
|
2004-02-10 21:50:41 +01:00
|
|
|
[{xmlelement, "instructions", [],
|
2003-02-23 21:13:39 +01:00
|
|
|
[{xmlcdata,
|
2004-02-10 21:50:41 +01:00
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
"You need an x:data capable client "
|
|
|
|
"to configure mod_irc settings")}]},
|
|
|
|
{xmlelement, "x", [{"xmlns", ?NS_XDATA}],
|
|
|
|
[{xmlelement, "title", [],
|
|
|
|
[{xmlcdata,
|
2004-02-26 23:00:04 +01:00
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
"Registration in mod_irc for ") ++ User ++ "@" ++ Server}]},
|
|
|
|
{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
2009-08-07 16:43:44 +02:00
|
|
|
"Enter username, encodings, ports and passwords you wish to use for "
|
2004-02-26 23:00:04 +01:00
|
|
|
"connecting to IRC servers")}]},
|
2004-02-10 21:50:41 +01:00
|
|
|
{xmlelement, "field", [{"type", "text-single"},
|
|
|
|
{"label",
|
|
|
|
translate:translate(
|
|
|
|
Lang, "IRC Username")},
|
|
|
|
{"var", "username"}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, Username}]}]},
|
|
|
|
{xmlelement, "field", [{"type", "fixed"}],
|
|
|
|
[{xmlelement, "value", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
2009-08-07 16:43:44 +02:00
|
|
|
"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."),
|
2010-11-02 22:40:08 +01:00
|
|
|
[DefaultEncoding, ?DEFAULT_IRC_PORT]))}]}]},
|
2004-02-10 21:50:41 +01:00
|
|
|
{xmlelement, "field", [{"type", "fixed"}],
|
|
|
|
[{xmlelement, "value", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
2009-08-07 16:43:44 +02:00
|
|
|
"Example: [{\"irc.lucky.net\", \"koi8-r\", 6667, \"secret\"}, "
|
|
|
|
"{\"vendetta.fef.net\", \"iso8859-1\", 7000}, {\"irc.sometestserver.net\", \"utf-8\"}]."
|
2004-02-10 21:50:41 +01:00
|
|
|
)}]}]},
|
|
|
|
{xmlelement, "field", [{"type", "text-multi"},
|
|
|
|
{"label",
|
2009-08-07 16:43:44 +02:00
|
|
|
translate:translate(Lang, "Connections parameters")},
|
|
|
|
{"var", "connections_params"}],
|
2004-02-10 21:50:41 +01:00
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
{xmlelement, "value", [], [{xmlcdata, S}]}
|
|
|
|
end,
|
|
|
|
string:tokens(
|
|
|
|
lists:flatten(
|
2009-08-07 16:43:44 +02:00
|
|
|
io_lib:format("~p.", [ConnectionsParams])),
|
2004-02-10 21:50:41 +01:00
|
|
|
"\n"))
|
|
|
|
}
|
|
|
|
]}]}
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
get_form(_Host, _, _, _Lang) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_SERVICE_UNAVAILABLE}.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
set_form(Host, From, [], _Lang, XData) ->
|
2003-02-23 21:13:39 +01:00
|
|
|
{LUser, LServer, _} = jlib:jid_tolower(From),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2003-02-23 21:13:39 +01:00
|
|
|
case {lists:keysearch("username", 1, XData),
|
2009-08-07 16:43:44 +02:00
|
|
|
lists:keysearch("connections_params", 1, XData)} of
|
2003-02-23 21:13:39 +01:00
|
|
|
{{value, {_, [Username]}}, {value, {_, Strings}}} ->
|
|
|
|
EncString = lists:foldl(fun(S, Res) ->
|
|
|
|
Res ++ S ++ "\n"
|
|
|
|
end, "", Strings),
|
|
|
|
case erl_scan:string(EncString) of
|
|
|
|
{ok, Tokens, _} ->
|
|
|
|
case erl_parse:parse_term(Tokens) of
|
2009-08-07 16:43:44 +02:00
|
|
|
{ok, ConnectionsParams} ->
|
2003-02-23 21:13:39 +01:00
|
|
|
case mnesia:transaction(
|
|
|
|
fun() ->
|
|
|
|
mnesia:write(
|
2005-04-17 20:08:34 +02:00
|
|
|
#irc_custom{us_host =
|
|
|
|
{US, Host},
|
2003-02-23 21:13:39 +01:00
|
|
|
data =
|
|
|
|
[{username,
|
|
|
|
Username},
|
2009-08-07 16:43:44 +02:00
|
|
|
{connections_params,
|
|
|
|
ConnectionsParams}]})
|
2003-02-23 21:13:39 +01:00
|
|
|
end) of
|
|
|
|
{atomic, _} ->
|
|
|
|
{result, []};
|
|
|
|
_ ->
|
2004-02-15 21:10:40 +01:00
|
|
|
{error, ?ERR_NOT_ACCEPTABLE}
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2004-02-15 21:10:40 +01:00
|
|
|
{error, ?ERR_NOT_ACCEPTABLE}
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2004-02-15 21:10:40 +01:00
|
|
|
{error, ?ERR_NOT_ACCEPTABLE}
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2004-02-15 21:10:40 +01:00
|
|
|
{error, ?ERR_NOT_ACCEPTABLE}
|
2003-02-23 21:13:39 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
set_form(_Host, _, _, _Lang, _XData) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_SERVICE_UNAVAILABLE}.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
|
|
|
|
2010-11-02 22:40:08 +01:00
|
|
|
%% Host = "irc.example.com"
|
|
|
|
%% ServerHost = "example.com"
|
2009-08-07 16:43:44 +02:00
|
|
|
get_connection_params(Host, From, IRCServer) ->
|
2010-11-02 22:40:08 +01:00
|
|
|
[_ | HostTail] = string:tokens(Host, "."),
|
|
|
|
ServerHost = string:join(HostTail, "."),
|
|
|
|
get_connection_params(Host, ServerHost, From, IRCServer).
|
|
|
|
|
|
|
|
get_default_encoding(ServerHost) ->
|
|
|
|
Result = gen_mod:get_module_opt(
|
|
|
|
ServerHost, ?MODULE, default_encoding,
|
|
|
|
?DEFAULT_IRC_ENCODING),
|
|
|
|
?INFO_MSG("The default_encoding configured for host ~p is: ~p~n", [ServerHost, Result]),
|
|
|
|
Result.
|
|
|
|
|
|
|
|
get_connection_params(Host, ServerHost, From, IRCServer) ->
|
2007-12-07 01:48:11 +01:00
|
|
|
#jid{user = User, server = _Server,
|
2003-10-07 22:31:44 +02:00
|
|
|
luser = LUser, lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2010-11-02 22:40:08 +01:00
|
|
|
DefaultEncoding = get_default_encoding(ServerHost),
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_read({irc_custom, {US, Host}}) of
|
2007-12-07 01:48:11 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2010-11-02 22:40:08 +01:00
|
|
|
{User, DefaultEncoding, ?DEFAULT_IRC_PORT, ""};
|
2003-02-23 21:13:39 +01:00
|
|
|
[] ->
|
2010-11-02 22:40:08 +01:00
|
|
|
{User, DefaultEncoding, ?DEFAULT_IRC_PORT, ""};
|
2003-02-23 21:13:39 +01:00
|
|
|
[#irc_custom{data = Data}] ->
|
2009-08-07 16:43:44 +02:00
|
|
|
Username = xml:get_attr_s(username, Data),
|
|
|
|
{NewUsername, NewEncoding, NewPort, NewPassword} =
|
|
|
|
case lists:keysearch(IRCServer, 1, xml:get_attr_s(connections_params, Data)) of
|
|
|
|
{value, {_, Encoding, Port, Password}} ->
|
|
|
|
{Username, Encoding, Port, Password};
|
|
|
|
{value, {_, Encoding, Port}} ->
|
|
|
|
{Username, Encoding, Port, ""};
|
|
|
|
{value, {_, Encoding}} ->
|
|
|
|
{Username, Encoding, ?DEFAULT_IRC_PORT, ""};
|
|
|
|
_ ->
|
2010-11-02 22:40:08 +01:00
|
|
|
{Username, DefaultEncoding, ?DEFAULT_IRC_PORT, ""}
|
2009-08-07 16:43:44 +02:00
|
|
|
end,
|
|
|
|
{NewUsername,
|
|
|
|
NewEncoding,
|
|
|
|
if
|
|
|
|
NewPort >= 0 andalso NewPort =< 65535 ->
|
|
|
|
NewPort;
|
|
|
|
true ->
|
|
|
|
?DEFAULT_IRC_PORT
|
|
|
|
end,
|
|
|
|
NewPassword}
|
|
|
|
end.
|
2003-02-23 21:13:39 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
adhoc_join(_From, _To, #adhoc_request{action = "cancel"} = Request) ->
|
|
|
|
adhoc:produce_response(Request,
|
|
|
|
#adhoc_response{status = canceled});
|
|
|
|
adhoc_join(From, To, #adhoc_request{lang = Lang,
|
|
|
|
node = _Node,
|
|
|
|
action = _Action,
|
|
|
|
xdata = XData} = Request) ->
|
|
|
|
%% Access control has already been taken care of in do_route.
|
|
|
|
if XData == false ->
|
|
|
|
Form =
|
|
|
|
{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_XDATA},
|
|
|
|
{"type", "form"}],
|
|
|
|
[{xmlelement, "title", [], [{xmlcdata, translate:translate(Lang, "Join IRC channel")}]},
|
|
|
|
{xmlelement, "field",
|
|
|
|
[{"var", "channel"},
|
|
|
|
{"type", "text-single"},
|
2009-06-23 02:05:08 +02:00
|
|
|
{"label", translate:translate(Lang, "IRC channel (don't put the first #)")}],
|
2009-06-15 20:56:52 +02:00
|
|
|
[{xmlelement, "required", [], []}]},
|
|
|
|
{xmlelement, "field",
|
|
|
|
[{"var", "server"},
|
|
|
|
{"type", "text-single"},
|
2009-06-23 02:05:08 +02:00
|
|
|
{"label", translate:translate(Lang, "IRC server")}],
|
2009-06-15 20:56:52 +02:00
|
|
|
[{xmlelement, "required", [], []}]}]},
|
|
|
|
adhoc:produce_response(Request,
|
|
|
|
#adhoc_response{status = executing,
|
|
|
|
elements = [Form]});
|
|
|
|
true ->
|
|
|
|
case jlib:parse_xdata_submit(XData) of
|
|
|
|
invalid ->
|
|
|
|
{error, ?ERR_BAD_REQUEST};
|
|
|
|
Fields ->
|
|
|
|
Channel = case lists:keysearch("channel", 1, Fields) of
|
|
|
|
{value, {"channel", C}} ->
|
|
|
|
C;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
Server = case lists:keysearch("server", 1, Fields) of
|
|
|
|
{value, {"server", S}} ->
|
|
|
|
S;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if Channel /= false,
|
|
|
|
Server /= false ->
|
|
|
|
RoomJID = Channel ++ "%" ++ Server ++ "@" ++ To#jid.server,
|
|
|
|
Invite = {xmlelement, "message", [],
|
|
|
|
[{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "invite",
|
|
|
|
[{"from", jlib:jid_to_string(From)}],
|
|
|
|
[{xmlelement, "reason", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
"Join the IRC channel here.")}]}]}]},
|
|
|
|
{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_XCONFERENCE}],
|
|
|
|
[{xmlcdata, translate:translate(Lang,
|
|
|
|
"Join the IRC channel here.")}]},
|
|
|
|
{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, io_lib:format(
|
|
|
|
translate:translate(Lang,
|
2009-06-23 02:05:08 +02:00
|
|
|
"Join the IRC channel in this Jabber ID: ~s"),
|
2009-06-15 20:56:52 +02:00
|
|
|
[RoomJID])}]}]},
|
|
|
|
ejabberd_router:route(jlib:string_to_jid(RoomJID), From, Invite),
|
|
|
|
adhoc:produce_response(Request, #adhoc_response{status = completed});
|
|
|
|
true ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
adhoc_register(_From, _To, #adhoc_request{action = "cancel"} = Request) ->
|
|
|
|
adhoc:produce_response(Request,
|
|
|
|
#adhoc_response{status = canceled});
|
|
|
|
adhoc_register(From, To, #adhoc_request{lang = Lang,
|
|
|
|
node = _Node,
|
|
|
|
xdata = XData,
|
|
|
|
action = Action} = Request) ->
|
|
|
|
#jid{user = User, luser = LUser, lserver = LServer} = From,
|
|
|
|
#jid{lserver = Host} = To,
|
|
|
|
US = {LUser, LServer},
|
|
|
|
%% Generate form for setting username and encodings. If the user
|
|
|
|
%% hasn't begun to fill out the form, generate an initial form
|
|
|
|
%% based on current values.
|
|
|
|
if XData == false ->
|
|
|
|
case catch mnesia:dirty_read({irc_custom, {US, Host}}) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
Username = User,
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionsParams = [];
|
2009-06-15 20:56:52 +02:00
|
|
|
[] ->
|
|
|
|
Username = User,
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionsParams = [];
|
2009-06-15 20:56:52 +02:00
|
|
|
[#irc_custom{data = Data}] ->
|
|
|
|
Username = xml:get_attr_s(username, Data),
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionsParams = xml:get_attr_s(connections_params, Data)
|
2009-06-15 20:56:52 +02:00
|
|
|
end,
|
|
|
|
Error = false;
|
|
|
|
true ->
|
|
|
|
case jlib:parse_xdata_submit(XData) of
|
|
|
|
invalid ->
|
|
|
|
Error = {error, ?ERR_BAD_REQUEST},
|
|
|
|
Username = false,
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionsParams = false;
|
2009-06-15 20:56:52 +02:00
|
|
|
Fields ->
|
|
|
|
Username = case lists:keysearch("username", 1, Fields) of
|
|
|
|
{value, {"username", U}} ->
|
|
|
|
U;
|
|
|
|
_ ->
|
|
|
|
User
|
|
|
|
end,
|
2009-08-07 16:43:44 +02:00
|
|
|
ConnectionsParams = parse_connections_params(Fields),
|
2009-06-15 20:56:52 +02:00
|
|
|
Error = false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
if Error /= false ->
|
|
|
|
Error;
|
|
|
|
Action == "complete" ->
|
|
|
|
case mnesia:transaction(
|
|
|
|
fun () ->
|
|
|
|
mnesia:write(
|
|
|
|
#irc_custom{us_host =
|
|
|
|
{US, Host},
|
|
|
|
data =
|
|
|
|
[{username,
|
|
|
|
Username},
|
2009-08-07 16:43:44 +02:00
|
|
|
{connections_params,
|
|
|
|
ConnectionsParams}]})
|
2009-06-15 20:56:52 +02:00
|
|
|
end) of
|
|
|
|
{atomic, _} ->
|
|
|
|
adhoc:produce_response(Request, #adhoc_response{status = completed});
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end;
|
|
|
|
true ->
|
2009-08-07 16:43:44 +02:00
|
|
|
Form = generate_adhoc_register_form(Lang, Username, ConnectionsParams),
|
2009-06-15 20:56:52 +02:00
|
|
|
adhoc:produce_response(Request,
|
|
|
|
#adhoc_response{status = executing,
|
|
|
|
elements = [Form],
|
|
|
|
actions = ["next", "complete"]})
|
|
|
|
end.
|
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
generate_adhoc_register_form(Lang, Username, ConnectionsParams) ->
|
2009-06-15 20:56:52 +02:00
|
|
|
{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_XDATA},
|
|
|
|
{"type", "form"}],
|
|
|
|
[{xmlelement, "title", [], [{xmlcdata, translate:translate(Lang, "IRC settings")}]},
|
|
|
|
{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
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.")}]},
|
|
|
|
{xmlelement, "field",
|
|
|
|
[{"var", "username"},
|
|
|
|
{"type", "text-single"},
|
|
|
|
{"label", translate:translate(Lang, "IRC username")}],
|
|
|
|
[{xmlelement, "required", [], []},
|
|
|
|
{xmlelement, "value", [], [{xmlcdata, Username}]}]}] ++
|
2009-08-07 16:43:44 +02:00
|
|
|
generate_connection_params_fields(Lang, ConnectionsParams, 1, [])}.
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
generate_connection_params_fields(Lang, [], Number, Acc) ->
|
2009-08-12 13:37:05 +02:00
|
|
|
Field = generate_connection_params_field(Lang, "", "", -1, "", Number),
|
2009-06-15 20:56:52 +02:00
|
|
|
lists:reverse(Field ++ Acc);
|
2009-08-07 16:43:44 +02:00
|
|
|
|
|
|
|
generate_connection_params_fields(Lang, [ConnectionParams | ConnectionsParams], Number, Acc) ->
|
|
|
|
case ConnectionParams of
|
|
|
|
{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, [], [], Number),
|
|
|
|
generate_connection_params_fields(Lang, ConnectionsParams, Number + 1, Field ++ Acc);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
2009-06-15 20:56:52 +02:00
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
generate_connection_params_field(Lang, Server, Encoding, Port, Password, Number) ->
|
2009-06-15 20:56:52 +02:00
|
|
|
EncodingUsed = case Encoding of
|
|
|
|
[] ->
|
2010-11-02 22:40:08 +01:00
|
|
|
get_default_encoding(Server);
|
2009-06-15 20:56:52 +02:00
|
|
|
_ ->
|
|
|
|
Encoding
|
|
|
|
end,
|
2009-08-12 13:37:05 +02:00
|
|
|
PortUsedInt = if
|
2009-08-07 16:43:44 +02:00
|
|
|
Port >= 0 andalso Port =< 65535 ->
|
|
|
|
Port;
|
|
|
|
true ->
|
|
|
|
?DEFAULT_IRC_PORT
|
|
|
|
end,
|
2009-08-12 13:37:05 +02:00
|
|
|
PortUsed = integer_to_list(PortUsedInt),
|
2009-08-07 16:43:44 +02:00
|
|
|
PasswordUsed = case Password of
|
|
|
|
[] ->
|
|
|
|
"";
|
|
|
|
_ ->
|
|
|
|
Password
|
|
|
|
end,
|
2009-08-12 13:37:05 +02:00
|
|
|
NumberString = integer_to_list(Number),
|
2009-06-15 20:56:52 +02:00
|
|
|
%% Fields are in reverse order, as they will be reversed again later.
|
|
|
|
[{xmlelement, "field",
|
2009-08-12 13:37:05 +02:00
|
|
|
[{"var", "password" ++ NumberString},
|
2009-08-07 16:43:44 +02:00
|
|
|
{"type", "text-single"},
|
|
|
|
{"label", io_lib:format(translate:translate(Lang, "Password ~b"), [Number])}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, PasswordUsed}]}]},
|
|
|
|
{xmlelement, "field",
|
2009-08-12 13:37:05 +02:00
|
|
|
[{"var", "port" ++ NumberString},
|
2009-08-07 16:43:44 +02:00
|
|
|
{"type", "text-single"},
|
|
|
|
{"label", io_lib:format(translate:translate(Lang, "Port ~b"), [Number])}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, PortUsed}]}]},
|
|
|
|
{xmlelement, "field",
|
2009-08-12 13:37:05 +02:00
|
|
|
[{"var", "encoding" ++ NumberString},
|
2009-06-15 20:56:52 +02:00
|
|
|
{"type", "list-single"},
|
|
|
|
{"label", io_lib:format(translate:translate(Lang, "Encoding for server ~b"), [Number])}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, EncodingUsed}]} |
|
|
|
|
lists:map(fun(E) ->
|
|
|
|
{xmlelement, "option", [{"label", E}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, E}]}]}
|
|
|
|
end, ?POSSIBLE_ENCODINGS)]},
|
|
|
|
{xmlelement, "field",
|
2009-08-12 13:37:05 +02:00
|
|
|
[{"var", "server" ++ NumberString},
|
2009-06-15 20:56:52 +02:00
|
|
|
{"type", "text-single"},
|
|
|
|
{"label", io_lib:format(translate:translate(Lang, "Server ~b"), [Number])}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, Server}]}]}].
|
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
parse_connections_params(Fields) ->
|
|
|
|
%% Find all fields staring with serverN, encodingN, portN and passwordN for any values
|
2009-06-15 20:56:52 +02:00
|
|
|
%% of N, and generate lists of {"N", Value}.
|
|
|
|
Servers = lists:sort(
|
|
|
|
[{lists:nthtail(6, Var), lists:flatten(Value)} || {Var, Value} <- Fields,
|
|
|
|
lists:prefix("server", Var)]),
|
|
|
|
Encodings = lists:sort(
|
|
|
|
[{lists:nthtail(8, Var), lists:flatten(Value)} || {Var, Value} <- Fields,
|
|
|
|
lists:prefix("encoding", Var)]),
|
2009-08-07 16:43:44 +02:00
|
|
|
|
|
|
|
Ports = lists:sort(
|
|
|
|
[{lists:nthtail(4, Var), lists:flatten(Value)} || {Var, Value} <- Fields,
|
|
|
|
lists:prefix("port", Var)]),
|
|
|
|
|
|
|
|
Passwords = lists:sort(
|
|
|
|
[{lists:nthtail(8, Var), lists:flatten(Value)} || {Var, Value} <- Fields,
|
|
|
|
lists:prefix("password", Var)]),
|
2009-06-15 20:56:52 +02:00
|
|
|
|
|
|
|
%% Now sort the lists, and find the corresponding pairs.
|
2009-08-07 16:43:44 +02:00
|
|
|
parse_connections_params(Servers, Encodings, Ports, Passwords).
|
|
|
|
|
|
|
|
retrieve_connections_params(ConnectionParams, ServerN) ->
|
|
|
|
case ConnectionParams of
|
|
|
|
[{ConnectionParamN, ConnectionParam} | ConnectionParamsTail] ->
|
|
|
|
if
|
|
|
|
ServerN == ConnectionParamN ->
|
|
|
|
{ConnectionParam, ConnectionParamsTail};
|
|
|
|
ServerN < ConnectionParamN ->
|
|
|
|
{[], [{ConnectionParamN, ConnectionParam} | ConnectionParamsTail]};
|
|
|
|
ServerN > ConnectionParamN ->
|
|
|
|
{[], ConnectionParamsTail}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
|
|
|
|
|
|
|
parse_connections_params([], _, _, _) ->
|
2009-06-15 20:56:52 +02:00
|
|
|
[];
|
2009-08-07 16:43:44 +02:00
|
|
|
parse_connections_params(_, [], [], []) ->
|
|
|
|
[];
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2009-08-07 16:43:44 +02:00
|
|
|
parse_connections_params([{ServerN, Server} | Servers], Encodings, Ports, Passwords) ->
|
|
|
|
%% Try to match matches of servers, ports, passwords and encodings, no matter what fields
|
|
|
|
%% the client might have left out.
|
|
|
|
{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)].
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(Host) ->
|
|
|
|
Fields = record_info(fields, irc_custom),
|
|
|
|
case mnesia:table_info(irc_custom, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[userserver, data] ->
|
|
|
|
?INFO_MSG("Converting irc_custom table from "
|
|
|
|
"{userserver, data} format", []),
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_irc_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, irc_custom},
|
|
|
|
{attributes, record_info(fields, irc_custom)}]),
|
|
|
|
mnesia:transform_table(irc_custom, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_irc_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#irc_custom{us_host = US} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_irc_tmp_table,
|
|
|
|
R#irc_custom{us_host = {US, Host}})
|
|
|
|
end, ok, irc_custom)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(irc_custom),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(irc_custom),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_irc_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_irc_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating irc_custom table", []),
|
|
|
|
mnesia:transform_table(irc_custom, ignore, Fields)
|
|
|
|
end.
|