2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_irc_connection.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2009-06-15 20:56:52 +02:00
|
|
|
%%% Purpose :
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 15 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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_connection).
|
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
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2015-12-21 16:19:24 +01:00
|
|
|
-export([start_link/12, start/13, route_chan/4,
|
2013-03-14 10:33:02 +01:00
|
|
|
route_nick/3]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, open_socket/2, wait_for_registration/2,
|
|
|
|
stream_established/2, handle_event/3,
|
|
|
|
handle_sync_event/4, handle_info/3, terminate/3,
|
2003-02-16 21:07:21 +01:00
|
|
|
code_change/4]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-17 16:11:27 +01:00
|
|
|
-define(SETS, gb_sets).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{socket :: inet:socket(),
|
|
|
|
encoding = <<"">> :: binary(),
|
|
|
|
port = 0 :: inet:port_number(),
|
|
|
|
password = <<"">> :: binary(),
|
2015-02-11 12:08:16 +01:00
|
|
|
queue = queue:new() :: ?TQUEUE,
|
2013-03-14 10:33:02 +01:00
|
|
|
user = #jid{} :: jid(),
|
|
|
|
host = <<"">> :: binary(),
|
|
|
|
server = <<"">> :: binary(),
|
2015-12-21 16:19:24 +01:00
|
|
|
remoteAddr = <<"">> :: binary(),
|
|
|
|
ident = <<"">> :: binary(),
|
|
|
|
realname = <<"">> :: binary(),
|
2013-03-14 10:33:02 +01:00
|
|
|
nick = <<"">> :: binary(),
|
2015-02-11 12:08:16 +01:00
|
|
|
channels = dict:new() :: ?TDICT,
|
2013-03-14 10:33:02 +01:00
|
|
|
nickchannel :: binary(),
|
2015-12-21 16:19:24 +01:00
|
|
|
webirc_password :: binary(),
|
2013-03-14 10:33:02 +01:00
|
|
|
mod = mod_irc :: atom(),
|
|
|
|
inbuf = <<"">> :: binary(),
|
|
|
|
outbuf = <<"">> :: binary()}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
%-define(DBGFSM, true).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
-define(FSMOPTS, []).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-endif.
|
2006-02-03 04:28:15 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
start(From, Host, ServerHost, Server, Username,
|
2015-12-21 16:19:24 +01:00
|
|
|
Encoding, Port, Password, Ident, RemoteAddr, RealName, WebircPassword, Mod) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Supervisor = gen_mod:get_module_proc(ServerHost,
|
|
|
|
ejabberd_mod_irc_sup),
|
|
|
|
supervisor:start_child(Supervisor,
|
|
|
|
[From, Host, Server, Username, Encoding, Port,
|
2015-12-21 16:19:24 +01:00
|
|
|
Password, Ident, RemoteAddr, RealName, WebircPassword, Mod]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
start_link(From, Host, Server, Username, Encoding, Port,
|
2015-12-21 16:19:24 +01:00
|
|
|
Password, Ident, RemoteAddr, RealName, WebircPassword, Mod) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:start_link(?MODULE,
|
|
|
|
[From, Host, Server, Username, Encoding, Port, Password,
|
2015-12-21 16:19:24 +01:00
|
|
|
Ident, RemoteAddr, RealName, WebircPassword, Mod],
|
2006-02-03 04:28:15 +01:00
|
|
|
?FSMOPTS).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, StopReason}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
init([From, Host, Server, Username, Encoding, Port,
|
2015-12-21 16:19:24 +01:00
|
|
|
Password, Ident, RemoteAddr, RealName, WebircPassword, Mod]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
gen_fsm:send_event(self(), init),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, open_socket,
|
|
|
|
#state{queue = queue:new(), mod = Mod,
|
|
|
|
encoding = Encoding, port = Port, password = Password,
|
|
|
|
user = From, nick = Username, host = Host,
|
2015-12-21 16:19:24 +01:00
|
|
|
server = Server, ident = Ident, realname = RealName,
|
|
|
|
remoteAddr = RemoteAddr, webirc_password = WebircPassword }}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
open_socket(init, StateData) ->
|
|
|
|
Addr = StateData#state.server,
|
2009-08-07 16:43:44 +02:00
|
|
|
Port = StateData#state.port,
|
2010-08-30 23:23:28 +02:00
|
|
|
?DEBUG("Connecting with IPv6 to ~s:~p", [Addr, Port]),
|
2015-12-21 16:19:24 +01:00
|
|
|
From = StateData#state.user,
|
|
|
|
#jid{user = JidUser, server = JidServer, resource = JidResource} = From,
|
|
|
|
UserIP = ejabberd_sm:get_user_ip(JidUser, JidServer, JidResource),
|
|
|
|
UserIPStr = inet_parse:ntoa(element(1, UserIP)),
|
2013-03-14 10:33:02 +01:00
|
|
|
Connect6 = gen_tcp:connect(binary_to_list(Addr), Port,
|
|
|
|
[inet6, binary, {packet, 0}]),
|
2010-08-30 23:23:28 +02:00
|
|
|
Connect = case Connect6 of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, _} ->
|
|
|
|
?DEBUG("Connection with IPv6 to ~s:~p failed. "
|
|
|
|
"Now using IPv4.",
|
|
|
|
[Addr, Port]),
|
|
|
|
gen_tcp:connect(binary_to_list(Addr), Port,
|
|
|
|
[inet, binary, {packet, 0}]);
|
|
|
|
_ -> Connect6
|
2010-08-30 23:23:28 +02:00
|
|
|
end,
|
|
|
|
case Connect of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Socket} ->
|
|
|
|
NewStateData = StateData#state{socket = Socket},
|
2015-12-21 16:19:24 +01:00
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("WEBIRC ~s ~s ~s ~s\r\n", [StateData#state.webirc_password, JidResource, UserIPStr, UserIPStr])),
|
2013-03-14 10:33:02 +01:00
|
|
|
if StateData#state.password /= <<"">> ->
|
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("PASS ~s\r\n",
|
|
|
|
[StateData#state.password]));
|
|
|
|
true -> true
|
|
|
|
end,
|
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("NICK ~s\r\n", [StateData#state.nick])),
|
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("USER ~s ~s ~s :~s\r\n",
|
2015-12-21 16:19:24 +01:00
|
|
|
[StateData#state.ident,
|
|
|
|
StateData#state.nick,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.host,
|
2015-12-21 16:19:24 +01:00
|
|
|
StateData#state.realname])),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, wait_for_registration, NewStateData};
|
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("connect return ~p~n", [Reason]),
|
|
|
|
Text = case Reason of
|
|
|
|
timeout -> <<"Server Connect Timeout">>;
|
|
|
|
_ -> <<"Server Connect Failed">>
|
|
|
|
end,
|
|
|
|
bounce_messages(Text),
|
|
|
|
{stop, normal, StateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-17 20:43:50 +01:00
|
|
|
wait_for_registration(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-18 20:56:16 +01:00
|
|
|
stream_established({xmlstreamend, _Name}, StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
stream_established(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
stream_established(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2006-02-18 20:56:16 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Reply = ok, {reply, Reply, StateName, StateData}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-18 20:56:16 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
|
|
|
-define(SEND(S),
|
2013-03-14 10:33:02 +01:00
|
|
|
if StateName == stream_established ->
|
|
|
|
send_text(StateData, S), StateData;
|
|
|
|
true ->
|
|
|
|
StateData#state{outbuf = <<(StateData#state.outbuf)/binary,
|
|
|
|
(iolist_to_binary(S))/binary>>}
|
2003-02-16 21:07:21 +01:00
|
|
|
end).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_password_from_presence(#xmlel{name = <<"presence">>,
|
|
|
|
children = Els}) ->
|
|
|
|
case lists:filter(fun (El) ->
|
|
|
|
case El of
|
|
|
|
#xmlel{name = <<"x">>, attrs = Attrs} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_MUC -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Els)
|
|
|
|
of
|
|
|
|
[ElXMUC | _] ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(ElXMUC, <<"password">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"password">>} = PasswordTag ->
|
2016-02-03 19:03:17 +01:00
|
|
|
{true, fxml:get_tag_cdata(PasswordTag)};
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ -> false
|
|
|
|
end.
|
2012-04-24 13:11:17 +02:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-09-21 17:10:50 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-02-16 21:07:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({route_chan, Channel, Resource,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"presence">>, attrs = Attrs} =
|
|
|
|
Presence},
|
2003-02-16 21:07:21 +01:00
|
|
|
StateName, StateData) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
NewStateData = case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"unavailable">> ->
|
|
|
|
send_stanza_unavailable(Channel, StateData),
|
|
|
|
S1 = (?SEND((io_lib:format("PART #~s\r\n",
|
|
|
|
[Channel])))),
|
|
|
|
S1#state{channels =
|
|
|
|
dict:erase(Channel, S1#state.channels)};
|
|
|
|
<<"subscribe">> -> StateData;
|
|
|
|
<<"subscribed">> -> StateData;
|
|
|
|
<<"unsubscribe">> -> StateData;
|
|
|
|
<<"unsubscribed">> -> StateData;
|
|
|
|
<<"error">> -> stop;
|
|
|
|
_ ->
|
|
|
|
Nick = case Resource of
|
|
|
|
<<"">> -> StateData#state.nick;
|
|
|
|
_ -> Resource
|
|
|
|
end,
|
|
|
|
S1 = if Nick /= StateData#state.nick ->
|
|
|
|
S11 = (?SEND((io_lib:format("NICK ~s\r\n",
|
|
|
|
[Nick])))),
|
|
|
|
S11#state{nickchannel = Channel};
|
|
|
|
true -> StateData
|
|
|
|
end,
|
|
|
|
case dict:is_key(Channel, S1#state.channels) of
|
|
|
|
true -> S1;
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case get_password_from_presence(Presence) of
|
|
|
|
{true, Password} ->
|
|
|
|
S2 =
|
|
|
|
(?SEND((io_lib:format("JOIN #~s ~s\r\n",
|
|
|
|
[Channel,
|
|
|
|
Password]))));
|
|
|
|
_ ->
|
|
|
|
S2 = (?SEND((io_lib:format("JOIN #~s\r\n",
|
|
|
|
[Channel]))))
|
|
|
|
end,
|
|
|
|
S2#state{channels =
|
|
|
|
dict:store(Channel, (?SETS):new(),
|
|
|
|
S1#state.channels)}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
if NewStateData == stop -> {stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
case dict:fetch_keys(NewStateData#state.channels) of
|
|
|
|
[] -> {stop, normal, NewStateData};
|
|
|
|
_ -> {next_state, StateName, NewStateData}
|
|
|
|
end
|
2003-02-17 16:11:27 +01:00
|
|
|
end;
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({route_chan, Channel, Resource,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"message">>, attrs = Attrs} = El},
|
2003-02-16 21:07:21 +01:00
|
|
|
StateName, StateData) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
NewStateData = case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"groupchat">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_path_s(El, [{elem, <<"subject">>}, cdata])
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
<<"">> ->
|
|
|
|
ejabberd_router:route(
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary([Channel,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.nick),
|
|
|
|
StateData#state.user, El),
|
2016-02-03 19:03:17 +01:00
|
|
|
Body = fxml:get_path_s(El,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"body">>},
|
|
|
|
cdata]),
|
|
|
|
case Body of
|
|
|
|
<<"/quote ", Rest/binary>> ->
|
|
|
|
?SEND(<<Rest/binary, "\r\n">>);
|
|
|
|
<<"/msg ", Rest/binary>> ->
|
|
|
|
?SEND(<<"PRIVMSG ", Rest/binary, "\r\n">>);
|
|
|
|
<<"/me ", Rest/binary>> ->
|
|
|
|
Strings = str:tokens(Rest, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG #~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
<<"/ctcp ", Rest/binary>> ->
|
|
|
|
Words = str:tokens(Rest, <<" ">>),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _] ->
|
|
|
|
CtcpCmd = str:to_upper(
|
|
|
|
str:substr(Rest,
|
|
|
|
str:str(Rest,
|
|
|
|
<<" ">>)
|
|
|
|
+ 1)),
|
|
|
|
Res =
|
|
|
|
io_lib:format("PRIVMSG ~s :\001~s\001\r\n",
|
|
|
|
[CtcpDest,
|
|
|
|
CtcpCmd]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Strings = str:tokens(Body, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format("PRIVMSG #~s :~s\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
|
|
|
Subject ->
|
|
|
|
Strings = str:tokens(Subject, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format("TOPIC #~s :~s\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
|
|
|
Type
|
|
|
|
when Type == <<"chat">>;
|
|
|
|
Type == <<"">>;
|
|
|
|
Type == <<"normal">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Body = fxml:get_path_s(El, [{elem, <<"body">>}, cdata]),
|
2013-03-14 10:33:02 +01:00
|
|
|
case Body of
|
|
|
|
<<"/quote ", Rest/binary>> ->
|
|
|
|
?SEND(<<Rest/binary, "\r\n">>);
|
|
|
|
<<"/msg ", Rest/binary>> ->
|
|
|
|
?SEND(<<"PRIVMSG ", Rest/binary, "\r\n">>);
|
|
|
|
<<"/me ", Rest/binary>> ->
|
|
|
|
Strings = str:tokens(Rest, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Resource, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
<<"/ctcp ", Rest/binary>> ->
|
|
|
|
Words = str:tokens(Rest, <<" ">>),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _] ->
|
|
|
|
CtcpCmd = str:to_upper(
|
|
|
|
str:substr(Rest,
|
|
|
|
str:str(Rest,
|
|
|
|
<<" ">>)
|
|
|
|
+ 1)),
|
|
|
|
Res = io_lib:format("PRIVMSG ~s :~s\r\n",
|
|
|
|
[CtcpDest,
|
|
|
|
<<"\001",
|
|
|
|
CtcpCmd/binary,
|
|
|
|
"\001">>]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Strings = str:tokens(Body, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :~s\r\n",
|
|
|
|
[Resource, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
|
|
|
<<"error">> -> stop;
|
|
|
|
_ -> StateData
|
|
|
|
end,
|
|
|
|
if NewStateData == stop -> {stop, normal, StateData};
|
|
|
|
true -> {next_state, StateName, NewStateData}
|
2003-05-29 21:07:41 +02:00
|
|
|
end;
|
2003-02-24 21:39:14 +01:00
|
|
|
handle_info({route_chan, Channel, Resource,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"iq">>} = El},
|
2003-02-24 21:39:14 +01:00
|
|
|
StateName, StateData) ->
|
|
|
|
From = StateData#state.user,
|
2015-11-24 16:44:13 +01:00
|
|
|
To = jid:make(iolist_to_binary([Channel, <<"%">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, StateData#state.nick),
|
2009-04-08 20:52:52 +02:00
|
|
|
_ = case jlib:iq_query_info(El) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{xmlns = ?NS_MUC_ADMIN} = IQ ->
|
|
|
|
iq_admin(StateData, Channel, From, To, IQ);
|
|
|
|
#iq{xmlns = ?NS_VERSION} ->
|
|
|
|
Res = io_lib:format("PRIVMSG ~s :\001VERSION\001\r\n",
|
|
|
|
[Resource]),
|
|
|
|
_ = (?SEND(Res)),
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
#iq{xmlns = ?NS_TIME} ->
|
|
|
|
Res = io_lib:format("PRIVMSG ~s :\001TIME\001\r\n",
|
|
|
|
[Resource]),
|
|
|
|
_ = (?SEND(Res)),
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
#iq{xmlns = ?NS_VCARD} ->
|
|
|
|
Res = io_lib:format("WHOIS ~s \r\n", [Resource]),
|
|
|
|
_ = (?SEND(Res)),
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
#iq{} ->
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ -> ok
|
|
|
|
end,
|
2003-02-24 21:39:14 +01:00
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route_chan, _Channel, _Resource, _Packet},
|
|
|
|
StateName, StateData) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({route_nick, Nick,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"message">>, attrs = Attrs} = El},
|
2003-02-18 21:33:10 +01:00
|
|
|
StateName, StateData) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
NewStateData = case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"chat">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Body = fxml:get_path_s(El, [{elem, <<"body">>}, cdata]),
|
2013-03-14 10:33:02 +01:00
|
|
|
case Body of
|
|
|
|
<<"/quote ", Rest/binary>> ->
|
|
|
|
?SEND(<<Rest/binary, "\r\n">>);
|
|
|
|
<<"/msg ", Rest/binary>> ->
|
|
|
|
?SEND(<<"PRIVMSG ", Rest/binary, "\r\n">>);
|
|
|
|
<<"/me ", Rest/binary>> ->
|
|
|
|
Strings = str:tokens(Rest, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Nick, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
<<"/ctcp ", Rest/binary>> ->
|
|
|
|
Words = str:tokens(Rest, <<" ">>),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _] ->
|
|
|
|
CtcpCmd = str:to_upper(
|
|
|
|
str:substr(Rest,
|
|
|
|
str:str(Rest,
|
|
|
|
<<" ">>)
|
|
|
|
+ 1)),
|
|
|
|
Res = io_lib:format("PRIVMSG ~s :~s\r\n",
|
|
|
|
[CtcpDest,
|
|
|
|
<<"\001",
|
|
|
|
CtcpCmd/binary,
|
|
|
|
"\001">>]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Strings = str:tokens(Body, <<"\n">>),
|
|
|
|
Res = iolist_to_binary(
|
|
|
|
lists:map(
|
|
|
|
fun (S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :~s\r\n",
|
|
|
|
[Nick, S])
|
|
|
|
end,
|
|
|
|
Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
|
|
|
<<"error">> -> stop;
|
|
|
|
_ -> StateData
|
|
|
|
end,
|
|
|
|
if NewStateData == stop -> {stop, normal, StateData};
|
|
|
|
true -> {next_state, StateName, NewStateData}
|
2003-05-29 21:07:41 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route_nick, _Nick, _Packet}, StateName,
|
|
|
|
StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({ircstring,
|
|
|
|
<<$P, $I, $N, $G, $\s, ID/binary>>},
|
|
|
|
StateName, StateData) ->
|
|
|
|
send_text(StateData, <<"PONG ", ID/binary, "\r\n">>),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({ircstring, <<$:, String/binary>>},
|
|
|
|
wait_for_registration, StateData) ->
|
|
|
|
Words = str:tokens(String, <<" ">>),
|
|
|
|
{NewState, NewStateData} = case Words of
|
|
|
|
[_, <<"001">> | _] ->
|
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format("CODEPAGE ~s\r\n",
|
|
|
|
[StateData#state.encoding])),
|
|
|
|
{stream_established, StateData};
|
|
|
|
[_, <<"433">> | _] ->
|
|
|
|
{error,
|
|
|
|
{error,
|
|
|
|
error_nick_in_use(StateData, String),
|
|
|
|
StateData}};
|
|
|
|
[_, <<$4, _, _>> | _] ->
|
|
|
|
{error,
|
|
|
|
{error,
|
|
|
|
error_unknown_num(StateData, String,
|
|
|
|
<<"cancel">>),
|
|
|
|
StateData}};
|
|
|
|
[_, <<$5, _, _>> | _] ->
|
|
|
|
{error,
|
|
|
|
{error,
|
|
|
|
error_unknown_num(StateData, String,
|
|
|
|
<<"cancel">>),
|
|
|
|
StateData}};
|
|
|
|
_ ->
|
|
|
|
?DEBUG("unknown irc command '~s'~n",
|
|
|
|
[String]),
|
|
|
|
{wait_for_registration, StateData}
|
|
|
|
end,
|
|
|
|
if NewState == error -> {stop, normal, NewStateData};
|
|
|
|
true -> {next_state, NewState, NewStateData}
|
2009-06-15 20:56:52 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({ircstring, <<$:, String/binary>>},
|
|
|
|
_StateName, StateData) ->
|
|
|
|
Words = str:tokens(String, <<" ">>),
|
|
|
|
NewStateData = case Words of
|
|
|
|
[_, <<"353">> | Items] ->
|
|
|
|
process_channel_list(StateData, Items);
|
|
|
|
[_, <<"332">>, _Nick, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_channel_topic(StateData, Chan, String),
|
|
|
|
StateData;
|
|
|
|
[_, <<"333">>, _Nick, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_channel_topic_who(StateData, Chan, String),
|
|
|
|
StateData;
|
|
|
|
[_, <<"318">>, _, Nick | _] ->
|
|
|
|
process_endofwhois(StateData, String, Nick), StateData;
|
|
|
|
[_, <<"311">>, _, Nick, Ident, Irchost | _] ->
|
|
|
|
process_whois311(StateData, String, Nick, Ident,
|
|
|
|
Irchost),
|
|
|
|
StateData;
|
|
|
|
[_, <<"312">>, _, Nick, Ircserver | _] ->
|
|
|
|
process_whois312(StateData, String, Nick, Ircserver),
|
|
|
|
StateData;
|
|
|
|
[_, <<"319">>, _, Nick | _] ->
|
|
|
|
process_whois319(StateData, String, Nick), StateData;
|
|
|
|
[_, <<"433">> | _] ->
|
|
|
|
process_nick_in_use(StateData, String);
|
|
|
|
% CODEPAGE isn't standard, so don't complain if it's not there.
|
|
|
|
[_, <<"421">>, _, <<"CODEPAGE">> | _] -> StateData;
|
|
|
|
[_, <<$4, _, _>> | _] ->
|
|
|
|
process_num_error(StateData, String);
|
|
|
|
[_, <<$5, _, _>> | _] ->
|
|
|
|
process_num_error(StateData, String);
|
|
|
|
[From, <<"PRIVMSG">>, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_chanprivmsg(StateData, Chan, From, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"NOTICE">>, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_channotice(StateData, Chan, From, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"PRIVMSG">>, Nick, <<":\001VERSION\001">>
|
|
|
|
| _] ->
|
|
|
|
process_version(StateData, Nick, From), StateData;
|
|
|
|
[From, <<"PRIVMSG">>, Nick, <<":\001USERINFO\001">>
|
|
|
|
| _] ->
|
|
|
|
process_userinfo(StateData, Nick, From), StateData;
|
|
|
|
[From, <<"PRIVMSG">>, Nick | _] ->
|
|
|
|
process_privmsg(StateData, Nick, From, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"NOTICE">>, Nick | _] ->
|
|
|
|
process_notice(StateData, Nick, From, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"TOPIC">>, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_topic(StateData, Chan, From, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"PART">>, <<$#, Chan/binary>> | _] ->
|
|
|
|
process_part(StateData, Chan, From, String);
|
|
|
|
[From, <<"QUIT">> | _] ->
|
|
|
|
process_quit(StateData, From, String);
|
|
|
|
[From, <<"JOIN">>, Chan | _] ->
|
|
|
|
process_join(StateData, Chan, From, String);
|
|
|
|
[From, <<"MODE">>, <<$#, Chan/binary>>, <<"+o">>, Nick
|
|
|
|
| _] ->
|
|
|
|
process_mode_o(StateData, Chan, From, Nick,
|
|
|
|
<<"admin">>, <<"moderator">>),
|
|
|
|
StateData;
|
|
|
|
[From, <<"MODE">>, <<$#, Chan/binary>>, <<"-o">>, Nick
|
|
|
|
| _] ->
|
|
|
|
process_mode_o(StateData, Chan, From, Nick,
|
|
|
|
<<"member">>, <<"participant">>),
|
|
|
|
StateData;
|
|
|
|
[From, <<"KICK">>, <<$#, Chan/binary>>, Nick | _] ->
|
|
|
|
process_kick(StateData, Chan, From, Nick, String),
|
|
|
|
StateData;
|
|
|
|
[From, <<"NICK">>, Nick | _] ->
|
|
|
|
process_nick(StateData, From, Nick);
|
|
|
|
_ ->
|
|
|
|
?DEBUG("unknown irc command '~s'~n", [String]),
|
|
|
|
StateData
|
|
|
|
end,
|
|
|
|
NewStateData1 = case StateData#state.outbuf of
|
|
|
|
<<"">> -> NewStateData;
|
|
|
|
Data ->
|
|
|
|
send_text(NewStateData, Data),
|
|
|
|
NewStateData#state{outbuf = <<"">>}
|
|
|
|
end,
|
2003-02-17 16:11:27 +01:00
|
|
|
{next_state, stream_established, NewStateData1};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({ircstring,
|
|
|
|
<<$E, $R, $R, $O, $R, _/binary>> = String},
|
2003-02-18 21:33:10 +01:00
|
|
|
StateName, StateData) ->
|
|
|
|
process_error(StateData, String),
|
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({ircstring, String}, StateName,
|
|
|
|
StateData) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
?DEBUG("unknown irc command '~s'~n", [String]),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2003-02-23 21:13:39 +01:00
|
|
|
send_text(StateData, Text),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({tcp, _Socket, Data}, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Buf = <<(StateData#state.inbuf)/binary, Data/binary>>,
|
|
|
|
Strings = ejabberd_regexp:split(<< <<C>>
|
|
|
|
|| <<C>> <= Buf, C /= $\r >>,
|
|
|
|
<<"\n">>),
|
2003-10-07 22:31:44 +02:00
|
|
|
?DEBUG("strings=~p~n", [Strings]),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewBuf = process_lines(StateData#state.encoding,
|
|
|
|
Strings),
|
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{inbuf = NewBuf}};
|
|
|
|
handle_info({tcp_closed, _Socket}, StateName,
|
|
|
|
StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
gen_fsm:send_event(self(), closed),
|
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({tcp_error, _Socket, _Reason}, StateName,
|
|
|
|
StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
gen_fsm:send_event(self(), closed),
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2009-06-15 20:56:52 +02:00
|
|
|
terminate(_Reason, _StateName, FullStateData) ->
|
|
|
|
{Error, StateData} = case FullStateData of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, SError, SStateData} -> {SError, SStateData};
|
|
|
|
_ ->
|
|
|
|
{#xmlel{name = <<"error">>,
|
|
|
|
attrs = [{<<"code">>, <<"502">>}],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<"Server Connect Failed">>}]},
|
|
|
|
FullStateData}
|
|
|
|
end,
|
2015-03-03 22:19:51 +01:00
|
|
|
(StateData#state.mod):closed_connection(StateData#state.host,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server),
|
2013-03-14 10:33:02 +01:00
|
|
|
bounce_messages(<<"Server Connect Failed">>),
|
|
|
|
lists:foreach(fun (Chan) ->
|
|
|
|
Stanza = #xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"error">>}],
|
|
|
|
children = [Error]},
|
|
|
|
send_stanza(Chan, StateData, Stanza)
|
|
|
|
end,
|
|
|
|
dict:fetch_keys(StateData#state.channels)),
|
2003-02-16 21:07:21 +01:00
|
|
|
case StateData#state.socket of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined -> ok;
|
|
|
|
Socket -> gen_tcp:close(Socket)
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
2011-03-01 22:32:50 +01:00
|
|
|
send_stanza(Chan, StateData, Stanza) ->
|
|
|
|
ejabberd_router:route(
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary([Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.nick),
|
|
|
|
StateData#state.user, Stanza).
|
2011-03-01 22:32:50 +01:00
|
|
|
|
|
|
|
send_stanza_unavailable(Chan, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Affiliation = <<"member">>,
|
|
|
|
Role = <<"none">>,
|
|
|
|
Stanza = #xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
Affiliation},
|
|
|
|
{<<"role">>, Role}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"status">>,
|
|
|
|
attrs = [{<<"code">>, <<"110">>}],
|
|
|
|
children = []}]}]},
|
2011-03-01 22:32:50 +01:00
|
|
|
send_stanza(Chan, StateData, Stanza).
|
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
send_text(#state{socket = Socket, encoding = Encoding},
|
|
|
|
Text) ->
|
|
|
|
CText = iconv:convert(<<"utf-8">>, Encoding, iolist_to_binary(Text)),
|
2003-02-16 21:07:21 +01:00
|
|
|
gen_tcp:send(Socket, CText).
|
|
|
|
|
2003-02-17 20:43:50 +01:00
|
|
|
%send_queue(Socket, Q) ->
|
|
|
|
% case queue:out(Q) of
|
|
|
|
% {{value, El}, Q1} ->
|
|
|
|
% send_element(Socket, El),
|
|
|
|
% send_queue(Socket, Q1);
|
|
|
|
% {empty, Q1} ->
|
|
|
|
% ok
|
|
|
|
% end.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
bounce_messages(Reason) ->
|
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{send_element, El} ->
|
|
|
|
#xmlel{attrs = Attrs} = El,
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(El, <<"502">>, Reason),
|
2016-02-03 19:03:17 +01:00
|
|
|
From = jid:from_string(fxml:get_attr_s(<<"from">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs)),
|
2016-02-03 19:03:17 +01:00
|
|
|
To = jid:from_string(fxml:get_attr_s(<<"to">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end,
|
|
|
|
bounce_messages(Reason)
|
|
|
|
after 0 -> ok
|
2003-02-16 21:07:21 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
route_chan(Pid, Channel, Resource, Packet) ->
|
|
|
|
Pid ! {route_chan, Channel, Resource, Packet}.
|
|
|
|
|
|
|
|
route_nick(Pid, Nick, Packet) ->
|
|
|
|
Pid ! {route_nick, Nick, Packet}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_lines(_Encoding, [S]) -> S;
|
2003-02-23 21:13:39 +01:00
|
|
|
process_lines(Encoding, [S | Ss]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
self() !
|
|
|
|
{ircstring, iconv:convert(Encoding, <<"utf-8">>, S)},
|
2003-02-23 21:13:39 +01:00
|
|
|
process_lines(Encoding, Ss).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
process_channel_list(StateData, Items) ->
|
|
|
|
process_channel_list_find_chan(StateData, Items).
|
|
|
|
|
|
|
|
process_channel_list_find_chan(StateData, []) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData;
|
2013-03-14 10:33:02 +01:00
|
|
|
process_channel_list_find_chan(StateData,
|
|
|
|
[<<$#, Chan/binary>> | Items]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
process_channel_list_users(StateData, Chan, Items);
|
2013-03-14 10:33:02 +01:00
|
|
|
process_channel_list_find_chan(StateData,
|
|
|
|
[_ | Items]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
process_channel_list_find_chan(StateData, Items).
|
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_channel_list_users(StateData, _Chan, []) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData;
|
2013-03-14 10:33:02 +01:00
|
|
|
process_channel_list_users(StateData, Chan,
|
|
|
|
[User | Items]) ->
|
|
|
|
NewStateData = process_channel_list_user(StateData,
|
|
|
|
Chan, User),
|
2003-02-18 21:33:10 +01:00
|
|
|
process_channel_list_users(NewStateData, Chan, Items).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
process_channel_list_user(StateData, Chan, User) ->
|
|
|
|
User1 = case User of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<$:, U1/binary>> -> U1;
|
|
|
|
_ -> User
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{User2, Affiliation, Role} = case User1 of
|
|
|
|
<<$@, U2/binary>> ->
|
|
|
|
{U2, <<"admin">>, <<"moderator">>};
|
|
|
|
<<$+, U2/binary>> ->
|
|
|
|
{U2, <<"member">>, <<"participant">>};
|
|
|
|
<<$%, U2/binary>> ->
|
|
|
|
{U2, <<"admin">>, <<"moderator">>};
|
|
|
|
<<$&, U2/binary>> ->
|
|
|
|
{U2, <<"admin">>, <<"moderator">>};
|
|
|
|
<<$~, U2/binary>> ->
|
|
|
|
{U2, <<"admin">>, <<"moderator">>};
|
|
|
|
_ -> {User1, <<"member">>, <<"participant">>}
|
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, User2),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
Affiliation},
|
|
|
|
{<<"role">>,
|
|
|
|
Role}],
|
|
|
|
children = []}]}]}),
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
fun (Ps) -> (?SETS):add_element(User2, Ps) end,
|
|
|
|
StateData#state.channels)
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> StateData;
|
|
|
|
NS -> StateData#state{channels = NS}
|
2003-02-18 21:33:10 +01:00
|
|
|
end.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-27 21:09:03 +01:00
|
|
|
process_channel_topic(StateData, Chan, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Msg = ejabberd_regexp:replace(String, <<".*332[^:]*:">>,
|
|
|
|
<<"">>),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"subject">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg1}]},
|
|
|
|
#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<"Topic for #", Chan/binary,
|
|
|
|
": ", Msg1/binary>>}]}]}).
|
2006-02-18 20:56:16 +01:00
|
|
|
|
|
|
|
process_channel_topic_who(StateData, Chan, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Words = str:tokens(String, <<" ">>),
|
2006-02-18 20:56:16 +01:00
|
|
|
Msg1 = case Words of
|
2013-03-14 10:33:02 +01:00
|
|
|
[_, <<"333">>, _, _Chan, Whoset, Timeset] ->
|
|
|
|
{Unixtimeset, _Rest} = str:to_integer(Timeset),
|
|
|
|
<<"Topic for #", Chan/binary, " set by ", Whoset/binary,
|
|
|
|
" at ", (unixtime2string(Unixtimeset))/binary>>;
|
|
|
|
[_, <<"333">>, _, _Chan, Whoset | _] ->
|
|
|
|
<<"Topic for #", Chan/binary, " set by ",
|
|
|
|
Whoset/binary>>;
|
|
|
|
_ -> String
|
2006-02-18 20:56:16 +01:00
|
|
|
end,
|
|
|
|
Msg2 = filter_message(Msg1),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}).
|
2003-02-27 21:09:03 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
error_nick_in_use(_StateData, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*433 +[^ ]* +">>, <<"">>),
|
2009-06-15 20:56:52 +02:00
|
|
|
Msg1 = filter_message(Msg),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"error">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"code">>, <<"409">>}, {<<"type">>, <<"cancel">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"conflict">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STANZAS}], children = []},
|
|
|
|
#xmlel{name = <<"text">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STANZAS}],
|
|
|
|
children = [{xmlcdata, Msg1}]}]}.
|
2009-06-15 20:56:52 +02:00
|
|
|
|
|
|
|
process_nick_in_use(StateData, String) ->
|
|
|
|
Error = error_nick_in_use(StateData, String),
|
|
|
|
case StateData#state.nickchannel of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
|
|
|
% Shouldn't happen with a well behaved server
|
|
|
|
StateData;
|
|
|
|
Chan ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"error">>}],
|
|
|
|
children = [Error]}),
|
|
|
|
StateData#state{nickchannel = undefined}
|
2009-06-15 20:56:52 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_num_error(StateData, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Error = error_unknown_num(StateData, String,
|
|
|
|
<<"continue">>),
|
|
|
|
lists:foreach(fun (Chan) ->
|
|
|
|
ejabberd_router:route(
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary(
|
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>,
|
|
|
|
<<"error">>}],
|
|
|
|
children = [Error]})
|
|
|
|
end,
|
|
|
|
dict:fetch_keys(StateData#state.channels)),
|
|
|
|
StateData.
|
2003-02-27 21:09:03 +01:00
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_endofwhois(StateData, _String, Nick) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Nick,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<"End of WHOIS">>}]}]}).
|
|
|
|
|
|
|
|
process_whois311(StateData, String, Nick, Ident,
|
|
|
|
Irchost) ->
|
|
|
|
Fullname = ejabberd_regexp:replace(String,
|
|
|
|
<<".*311[^:]*:">>, <<"">>),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Nick,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
iolist_to_binary(
|
|
|
|
[<<"WHOIS: ">>,
|
|
|
|
Nick,
|
|
|
|
<<" is ">>,
|
|
|
|
Ident,
|
|
|
|
<<"@">>,
|
|
|
|
Irchost,
|
|
|
|
<<" : ">>,
|
|
|
|
Fullname])}]}]}).
|
2005-05-18 00:35:16 +02:00
|
|
|
|
|
|
|
process_whois312(StateData, String, Nick, Ircserver) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Ircserverdesc = ejabberd_regexp:replace(String,
|
|
|
|
<<".*312[^:]*:">>, <<"">>),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary([Nick,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
iolist_to_binary(
|
|
|
|
[<<"WHOIS: ">>,
|
|
|
|
Nick,
|
|
|
|
<<" use ">>,
|
|
|
|
Ircserver,
|
|
|
|
<<" : ">>,
|
|
|
|
Ircserverdesc])}]}]}).
|
2005-05-18 00:35:16 +02:00
|
|
|
|
|
|
|
process_whois319(StateData, String, Nick) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Chanlist = ejabberd_regexp:replace(String,
|
|
|
|
<<".*319[^:]*:">>, <<"">>),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Nick,
|
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
iolist_to_binary(
|
|
|
|
[<<"WHOIS: ">>,
|
|
|
|
Nick,
|
|
|
|
<<" is on ">>,
|
|
|
|
Chanlist])}]}]}).
|
2005-05-18 00:35:16 +02:00
|
|
|
|
2003-02-17 20:43:50 +01:00
|
|
|
process_chanprivmsg(StateData, Chan, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*PRIVMSG[^:]*:">>, <<"">>),
|
2003-02-17 16:11:27 +01:00
|
|
|
Msg1 = case Msg of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<1, $A, $C, $T, $I, $O, $N, $\s, Rest/binary>> ->
|
|
|
|
<<"/me ", Rest/binary>>;
|
|
|
|
_ -> Msg
|
2003-02-17 16:11:27 +01:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
|
|
|
process_channotice(StateData, Chan, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*NOTICE[^:]*:">>, <<"">>),
|
2003-07-21 22:01:22 +02:00
|
|
|
Msg1 = case Msg of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<1, $A, $C, $T, $I, $O, $N, $\s, Rest/binary>> ->
|
|
|
|
<<"/me ", Rest/binary>>;
|
|
|
|
_ -> <<"/me NOTICE: ", Msg/binary>>
|
2003-07-21 22:01:22 +02:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_privmsg(StateData, _Nick, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*PRIVMSG[^:]*:">>, <<"">>),
|
2003-02-18 21:33:10 +01:00
|
|
|
Msg1 = case Msg of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<1, $A, $C, $T, $I, $O, $N, $\s, Rest/binary>> ->
|
|
|
|
<<"/me ", Rest/binary>>;
|
|
|
|
_ -> Msg
|
2003-02-18 21:33:10 +01:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser,
|
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_notice(StateData, _Nick, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*NOTICE[^:]*:">>, <<"">>),
|
2003-07-21 22:01:22 +02:00
|
|
|
Msg1 = case Msg of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<1, $A, $C, $T, $I, $O, $N, $\s, Rest/binary>> ->
|
|
|
|
<<"/me ", Rest/binary>>;
|
|
|
|
_ -> <<"/me NOTICE: ", Msg/binary>>
|
2003-07-21 22:01:22 +02:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser,
|
|
|
|
<<"!">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"chat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_version(StateData, _Nick, From) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format("NOTICE ~s :\001VERSION ejabberd IRC "
|
|
|
|
"transport ~s (c) Alexey Shchepin\001\r\n",
|
|
|
|
[FromUser, ?VERSION])
|
|
|
|
++
|
|
|
|
io_lib:format("NOTICE ~s :\001VERSION http://ejabberd.jabber"
|
|
|
|
"studio.org/\001\r\n",
|
|
|
|
[FromUser])).
|
2003-02-17 20:43:50 +01:00
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_userinfo(StateData, _Nick, From) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format("NOTICE ~s :\001USERINFO xmpp:~s\001\r\n",
|
|
|
|
[FromUser,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(StateData#state.user)])).
|
2005-05-18 00:35:16 +02:00
|
|
|
|
2003-02-27 21:09:03 +01:00
|
|
|
process_topic(StateData, Chan, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*TOPIC[^:]*:">>, <<"">>),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"subject">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg1}]},
|
|
|
|
#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<"/me has changed the subject to: ",
|
|
|
|
Msg1/binary>>}]}]}).
|
2003-02-27 21:09:03 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
process_part(StateData, Chan, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | FromIdent] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*PART[^:]*:">>, <<"">>),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"member">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"none">>}],
|
|
|
|
children = []}]},
|
|
|
|
#xmlel{name = <<"status">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
list_to_binary(
|
|
|
|
[Msg1, " (",
|
|
|
|
FromIdent, ")"])}]}]}),
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
fun (Ps) -> remove_element(FromUser, Ps) end,
|
|
|
|
StateData#state.channels)
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> StateData;
|
|
|
|
NS -> StateData#state{channels = NS}
|
2003-02-18 21:33:10 +01:00
|
|
|
end.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-21 20:52:15 +01:00
|
|
|
process_quit(StateData, From, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | FromIdent] = str:tokens(From, <<"!">>),
|
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*QUIT[^:]*:">>, <<"">>),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2013-03-14 10:33:02 +01:00
|
|
|
dict:map(fun (Chan, Ps) ->
|
|
|
|
case (?SETS):is_member(FromUser, Ps) of
|
|
|
|
true ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>,
|
|
|
|
<<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"item">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"member">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"none">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]}]},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"status">>,
|
|
|
|
attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
list_to_binary(
|
|
|
|
[Msg1, " (",
|
|
|
|
FromIdent,
|
|
|
|
")"])}]}]}),
|
|
|
|
remove_element(FromUser, Ps);
|
|
|
|
_ -> Ps
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
StateData#state.channels),
|
2003-02-21 20:52:15 +01:00
|
|
|
StateData.
|
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
process_join(StateData, Channel, From, _String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | FromIdent] = str:tokens(From, <<"!">>),
|
|
|
|
[Chan | _] = binary:split(Channel, <<":#">>),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"member">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"participant">>}],
|
|
|
|
children = []}]},
|
|
|
|
#xmlel{name = <<"status">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
list_to_binary(FromIdent)}]}]}),
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
2013-03-14 10:33:02 +01:00
|
|
|
fun (Ps) -> (?SETS):add_element(FromUser, Ps) end,
|
|
|
|
StateData#state.channels)
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> StateData;
|
|
|
|
NS -> StateData#state{channels = NS}
|
2003-02-18 21:33:10 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_mode_o(StateData, Chan, _From, Nick,
|
|
|
|
Affiliation, Role) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, Nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
Affiliation},
|
|
|
|
{<<"role">>,
|
|
|
|
Role}],
|
|
|
|
children = []}]}]}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-18 20:56:16 +01:00
|
|
|
process_kick(StateData, Chan, From, Nick, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Msg = lists:last(str:tokens(String, <<":">>)),
|
|
|
|
Msg2 = <<Nick/binary, " kicked by ", From/binary, " (",
|
|
|
|
(filter_message(Msg))/binary, ")">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, <<"">>),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"message">>,
|
|
|
|
attrs = [{<<"type">>, <<"groupchat">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"body">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Msg2}]}]}),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(iolist_to_binary(
|
2013-03-14 10:33:02 +01:00
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host, Nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_MUC_USER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"none">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"none">>}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"status">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"code">>,
|
|
|
|
<<"307">>}],
|
|
|
|
children = []}]}]}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
process_nick(StateData, From, NewNick) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[FromUser | _] = str:tokens(From, <<"!">>),
|
|
|
|
[Nick | _] = binary:split(NewNick, <<":">>),
|
|
|
|
NewChans = dict:map(fun (Chan, Ps) ->
|
|
|
|
case (?SETS):is_member(FromUser, Ps) of
|
|
|
|
true ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary(
|
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
FromUser),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name =
|
|
|
|
<<"presence">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>,
|
|
|
|
<<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"x">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_MUC_USER}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"item">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"member">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"participant">>},
|
|
|
|
{<<"nick">>,
|
|
|
|
Nick}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]},
|
|
|
|
#xmlel{name
|
|
|
|
=
|
|
|
|
<<"status">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"code">>,
|
|
|
|
<<"303">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]}]}]}),
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary(
|
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
Nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name =
|
|
|
|
<<"presence">>,
|
|
|
|
attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"x">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_MUC_USER}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"item">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"affiliation">>,
|
|
|
|
<<"member">>},
|
|
|
|
{<<"role">>,
|
|
|
|
<<"participant">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]}]}]}),
|
|
|
|
(?SETS):add_element(Nick,
|
|
|
|
remove_element(FromUser,
|
|
|
|
Ps));
|
|
|
|
_ -> Ps
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
StateData#state.channels),
|
|
|
|
if FromUser == StateData#state.nick ->
|
|
|
|
StateData#state{nick = Nick, nickchannel = undefined,
|
|
|
|
channels = NewChans};
|
|
|
|
true -> StateData#state{channels = NewChans}
|
2009-06-15 20:56:52 +02:00
|
|
|
end.
|
2003-02-18 21:33:10 +01:00
|
|
|
|
|
|
|
process_error(StateData, String) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (Chan) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(
|
2013-03-14 10:33:02 +01:00
|
|
|
iolist_to_binary(
|
|
|
|
[Chan,
|
|
|
|
<<"%">>,
|
|
|
|
StateData#state.server]),
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.nick),
|
|
|
|
StateData#state.user,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>,
|
|
|
|
<<"error">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"error">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"code">>,
|
|
|
|
<<"502">>}],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
String}]}]})
|
|
|
|
end,
|
|
|
|
dict:fetch_keys(StateData#state.channels)).
|
2003-02-18 21:33:10 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
error_unknown_num(_StateData, String, Type) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Msg = ejabberd_regexp:replace(String,
|
|
|
|
<<".*[45][0-9][0-9] +[^ ]* +">>, <<"">>),
|
2009-06-15 20:56:52 +02:00
|
|
|
Msg1 = filter_message(Msg),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"error">>,
|
|
|
|
attrs = [{<<"code">>, <<"500">>}, {<<"type">>, Type}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"undefined-condition">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STANZAS}], children = []},
|
|
|
|
#xmlel{name = <<"text">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STANZAS}],
|
|
|
|
children = [{xmlcdata, Msg1}]}]}.
|
2003-02-17 16:11:27 +01:00
|
|
|
|
|
|
|
remove_element(E, Set) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?SETS):is_element(E, Set) of
|
|
|
|
true -> (?SETS):del_element(E, Set);
|
|
|
|
_ -> Set
|
2003-02-17 16:11:27 +01:00
|
|
|
end.
|
2003-02-24 21:39:14 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
iq_admin(StateData, Channel, From, To,
|
|
|
|
#iq{type = Type, xmlns = XMLNS, sub_el = SubEl} = IQ) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch process_iq_admin(StateData, Channel, Type,
|
|
|
|
SubEl)
|
|
|
|
of
|
|
|
|
{'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]);
|
|
|
|
Res ->
|
|
|
|
if Res /= ignore ->
|
|
|
|
ResIQ = case Res of
|
|
|
|
{result, ResEls} ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, XMLNS}],
|
|
|
|
children = ResEls}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
true -> ok
|
|
|
|
end
|
2003-02-24 21:39:14 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_iq_admin(StateData, Channel, set, SubEl) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(SubEl, <<"item">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> {error, ?ERR_BAD_REQUEST};
|
|
|
|
ItemEl ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Nick = fxml:get_tag_attr_s(<<"nick">>, ItemEl),
|
|
|
|
Affiliation = fxml:get_tag_attr_s(<<"affiliation">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
ItemEl),
|
2016-02-03 19:03:17 +01:00
|
|
|
Role = fxml:get_tag_attr_s(<<"role">>, ItemEl),
|
|
|
|
Reason = fxml:get_path_s(ItemEl,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"reason">>}, cdata]),
|
|
|
|
process_admin(StateData, Channel, Nick, Affiliation,
|
|
|
|
Role, Reason)
|
2003-02-24 21:39:14 +01:00
|
|
|
end;
|
2007-12-07 01:48:11 +01:00
|
|
|
process_iq_admin(_StateData, _Channel, get, _SubEl) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED}.
|
2003-02-24 21:39:14 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_admin(_StateData, _Channel, <<"">>,
|
|
|
|
_Affiliation, _Role, _Reason) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED};
|
2013-03-14 10:33:02 +01:00
|
|
|
process_admin(StateData, Channel, Nick, _Affiliation,
|
|
|
|
<<"none">>, Reason) ->
|
2003-02-24 21:39:14 +01:00
|
|
|
case Reason of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format("KICK #~s ~s\r\n", [Channel, Nick]));
|
|
|
|
_ ->
|
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format("KICK #~s ~s :~s\r\n",
|
|
|
|
[Channel, Nick, Reason]))
|
2003-02-24 21:39:14 +01:00
|
|
|
end,
|
|
|
|
{result, []};
|
2013-03-14 10:33:02 +01:00
|
|
|
process_admin(_StateData, _Channel, _Nick, _Affiliation,
|
|
|
|
_Role, _Reason) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED}.
|
2003-02-24 21:39:14 +01:00
|
|
|
|
2005-01-14 00:04:47 +01:00
|
|
|
filter_message(Msg) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
list_to_binary(
|
|
|
|
lists:filter(fun (C) ->
|
|
|
|
if (C < 32) and (C /= 9) and (C /= 10) and (C /= 13) ->
|
|
|
|
false;
|
|
|
|
true -> true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
binary_to_list(filter_mirc_colors(Msg)))).
|
2006-02-18 20:56:16 +01:00
|
|
|
|
|
|
|
filter_mirc_colors(Msg) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_regexp:greplace(Msg,
|
|
|
|
<<"(\\003[0-9]+)(,[0-9]+)?">>, <<"">>).
|
2006-02-18 20:56:16 +01:00
|
|
|
|
|
|
|
unixtime2string(Unixtime) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Secs = Unixtime +
|
|
|
|
calendar:datetime_to_gregorian_seconds({{1970, 1, 1},
|
|
|
|
{0, 0, 0}}),
|
2011-09-05 08:48:04 +02:00
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
2013-03-14 10:33:02 +01:00
|
|
|
calendar:universal_time_to_local_time(calendar:gregorian_seconds_to_datetime(Secs)),
|
|
|
|
iolist_to_binary(io_lib:format("~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second])).
|