2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_irc_connection.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 15 Feb 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_irc_connection).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2006-02-03 04:28:15 +01:00
|
|
|
-export([start_link/5, start/6, route_chan/4, route_nick/3]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
|
|
|
-export([init/1,
|
|
|
|
open_socket/2,
|
2003-02-17 20:43:50 +01:00
|
|
|
wait_for_registration/2,
|
2003-02-16 21:07:21 +01:00
|
|
|
stream_established/2,
|
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
handle_info/3,
|
|
|
|
terminate/3,
|
|
|
|
code_change/4]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
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).
|
|
|
|
|
2006-01-24 00:13:06 +01:00
|
|
|
-record(state, {socket, encoding, queue,
|
2005-04-17 20:08:34 +02:00
|
|
|
user, host, server, nick,
|
2003-02-18 21:33:10 +01:00
|
|
|
channels = dict:new(),
|
2003-02-16 21:07:21 +01:00
|
|
|
inbuf = "", outbuf = ""}).
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
%-define(DBGFSM, true).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2006-02-03 04:28:15 +01:00
|
|
|
start(From, Host, ServerHost, Server, Username, Encoding) ->
|
|
|
|
Supervisor = gen_mod:get_module_proc(ServerHost, ejabberd_mod_irc_sup),
|
|
|
|
supervisor:start_child(
|
|
|
|
Supervisor, [From, Host, Server, Username, Encoding]).
|
|
|
|
|
|
|
|
start_link(From, Host, Server, Username, Encoding) ->
|
|
|
|
gen_fsm:start_link(?MODULE, [From, Host, Server, Username, Encoding],
|
|
|
|
?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 |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-02-23 21:13:39 +01:00
|
|
|
init([From, Host, Server, Username, Encoding]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
gen_fsm:send_event(self(), init),
|
|
|
|
{ok, open_socket, #state{queue = queue:new(),
|
2003-02-23 21:13:39 +01:00
|
|
|
encoding = Encoding,
|
2003-02-16 21:07:21 +01:00
|
|
|
user = From,
|
2003-02-23 21:13:39 +01:00
|
|
|
nick = Username,
|
2005-04-17 20:08:34 +02:00
|
|
|
host = Host,
|
2003-02-16 21:07:21 +01:00
|
|
|
server = Server}}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
open_socket(init, StateData) ->
|
|
|
|
Addr = StateData#state.server,
|
|
|
|
Port = 6667,
|
|
|
|
?DEBUG("connecting to ~s:~p~n", [Addr, Port]),
|
|
|
|
case gen_tcp:connect(Addr, Port, [binary, {packet, 0}]) of
|
|
|
|
{ok, Socket} ->
|
2003-02-23 21:13:39 +01:00
|
|
|
NewStateData = StateData#state{socket = Socket},
|
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("NICK ~s\r\n", [StateData#state.nick])),
|
|
|
|
send_text(NewStateData,
|
2003-02-16 21:07:21 +01:00
|
|
|
io_lib:format(
|
|
|
|
"USER ~s ~s ~s :~s\r\n",
|
|
|
|
[StateData#state.nick,
|
|
|
|
StateData#state.nick,
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host,
|
2003-02-16 21:07:21 +01:00
|
|
|
StateData#state.nick])),
|
2003-07-21 22:01:22 +02:00
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format("CODEPAGE ~s\r\n", [StateData#state.encoding])),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, wait_for_registration,
|
2003-02-23 21:13:39 +01:00
|
|
|
NewStateData};
|
2003-02-16 21:07:21 +01:00
|
|
|
{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}
|
|
|
|
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} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%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} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
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} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2006-02-18 20:56:16 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
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),
|
|
|
|
if
|
|
|
|
StateName == stream_established ->
|
2003-02-23 21:13:39 +01:00
|
|
|
send_text(StateData, S),
|
2003-02-16 21:07:21 +01:00
|
|
|
StateData;
|
|
|
|
true ->
|
|
|
|
StateData#state{outbuf = StateData#state.outbuf ++ S}
|
|
|
|
end).
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({route_chan, Channel, Resource,
|
|
|
|
{xmlelement, "presence", Attrs, Els}},
|
2003-02-16 21:07:21 +01:00
|
|
|
StateName, StateData) ->
|
|
|
|
NewStateData =
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
2003-02-17 16:11:27 +01:00
|
|
|
S1 = ?SEND(io_lib:format("PART #~s\r\n", [Channel])),
|
|
|
|
S1#state{channels =
|
2003-02-18 21:33:10 +01:00
|
|
|
dict:erase(Channel, S1#state.channels)};
|
2003-02-16 21:07:21 +01:00
|
|
|
"subscribe" -> StateData;
|
|
|
|
"subscribed" -> StateData;
|
|
|
|
"unsubscribe" -> StateData;
|
|
|
|
"unsubscribed" -> StateData;
|
2003-05-29 21:07:41 +02:00
|
|
|
"error" -> stop;
|
2003-02-16 21:07:21 +01:00
|
|
|
_ ->
|
2003-02-21 20:52:15 +01:00
|
|
|
Nick = case Resource of
|
|
|
|
"" ->
|
|
|
|
StateData#state.nick;
|
|
|
|
_ ->
|
|
|
|
Resource
|
|
|
|
end,
|
2003-02-18 21:33:10 +01:00
|
|
|
S1 = ?SEND(io_lib:format("NICK ~s\r\n"
|
|
|
|
"JOIN #~s\r\n",
|
2003-02-21 20:52:15 +01:00
|
|
|
[Nick, Channel])),
|
2003-02-18 21:33:10 +01:00
|
|
|
case dict:is_key(Channel, S1#state.channels) of
|
|
|
|
true ->
|
2003-02-21 20:52:15 +01:00
|
|
|
S1#state{nick = Nick};
|
2003-02-18 21:33:10 +01:00
|
|
|
_ ->
|
2003-02-21 20:52:15 +01:00
|
|
|
S1#state{nick = Nick,
|
2003-02-18 21:33:10 +01:00
|
|
|
channels =
|
|
|
|
dict:store(Channel, ?SETS:new(),
|
|
|
|
S1#state.channels)}
|
|
|
|
end
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
2003-05-29 21:07:41 +02:00
|
|
|
if
|
|
|
|
NewStateData == stop ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
case length(dict:fetch_keys(NewStateData#state.channels)) of
|
|
|
|
0 ->
|
|
|
|
{stop, normal, NewStateData};
|
|
|
|
_ ->
|
|
|
|
{next_state, StateName, NewStateData}
|
|
|
|
end
|
2003-02-17 16:11:27 +01:00
|
|
|
end;
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({route_chan, Channel, Resource,
|
2003-02-16 21:07:21 +01:00
|
|
|
{xmlelement, "message", Attrs, Els} = El},
|
|
|
|
StateName, StateData) ->
|
|
|
|
NewStateData =
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"groupchat" ->
|
2003-02-27 21:09:03 +01:00
|
|
|
case xml:get_path_s(El, [{elem, "subject"}, cdata]) of
|
|
|
|
"" ->
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat(
|
|
|
|
[Channel, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, StateData#state.nick),
|
2003-02-27 21:09:03 +01:00
|
|
|
StateData#state.user, El),
|
|
|
|
Body = xml:get_path_s(El, [{elem, "body"}, cdata]),
|
2004-04-17 21:00:10 +02:00
|
|
|
case Body of
|
|
|
|
"/quote " ++ Rest ->
|
|
|
|
?SEND(Rest ++ "\r\n");
|
2006-02-18 20:56:16 +01:00
|
|
|
"/msg " ++ Rest ->
|
|
|
|
?SEND("PRIVMSG " ++ Rest ++ "\r\n");
|
|
|
|
"/me " ++ Rest ->
|
|
|
|
Strings = string:tokens(Rest, "\n"),
|
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG #~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
"/ctcp " ++ Rest ->
|
|
|
|
Words = string:tokens(Rest, " "),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _] ->
|
|
|
|
CtcpCmd =
|
|
|
|
toupper(
|
|
|
|
string:substr(
|
|
|
|
Rest,
|
|
|
|
string:str(Rest, " ") + 1)),
|
|
|
|
Res = io_lib:format(
|
|
|
|
"PRIVMSG ~s :\001~s\001\r\n",
|
|
|
|
[CtcpDest, CtcpCmd]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2004-04-17 21:00:10 +02:00
|
|
|
_ ->
|
2006-02-18 20:56:16 +01:00
|
|
|
Strings = string:tokens(Body, "\n"),
|
2004-04-17 21:00:10 +02:00
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG #~s :~s\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
|
|
|
Subject ->
|
|
|
|
Strings = string:tokens(Subject, "\n"),
|
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format("TOPIC #~s :~s\r\n",
|
|
|
|
[Channel, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
2006-02-18 20:56:16 +01:00
|
|
|
Type when Type == "chat"; Type == ""; Type == "normal" ->
|
2004-04-17 21:00:10 +02:00
|
|
|
Body = xml:get_path_s(El, [{elem, "body"}, cdata]),
|
|
|
|
case Body of
|
|
|
|
"/quote " ++ Rest ->
|
|
|
|
?SEND(Rest ++ "\r\n");
|
2006-02-18 20:56:16 +01:00
|
|
|
"/msg " ++ Rest ->
|
|
|
|
?SEND("PRIVMSG " ++ Rest ++ "\r\n");
|
|
|
|
"/me " ++ Rest ->
|
|
|
|
Strings = string:tokens(Rest, "\n"),
|
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Resource, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
"/ctcp " ++ Rest ->
|
|
|
|
Words = string:tokens(Rest, " "),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _ ] ->
|
|
|
|
CtcpCmd =
|
|
|
|
toupper(
|
|
|
|
string:substr(
|
|
|
|
Rest, string:str(Rest, " ") + 1)),
|
|
|
|
Res = io_lib:format(
|
|
|
|
"PRIVMSG ~s :~s\r\n",
|
|
|
|
[CtcpDest, "\001" ++ CtcpCmd ++ "\001"]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2004-04-17 21:00:10 +02:00
|
|
|
_ ->
|
2006-02-18 20:56:16 +01:00
|
|
|
Strings = string:tokens(Body, "\n"),
|
2003-02-27 21:09:03 +01:00
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
2004-04-17 21:00:10 +02:00
|
|
|
io_lib:format("PRIVMSG ~s :~s\r\n",
|
|
|
|
[Resource, S])
|
2003-02-27 21:09:03 +01:00
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
2003-05-29 21:07:41 +02:00
|
|
|
"error" ->
|
|
|
|
stop;
|
2003-02-18 21:33:10 +01:00
|
|
|
_ ->
|
|
|
|
StateData
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
2003-05-29 21:07:41 +02:00
|
|
|
if
|
|
|
|
NewStateData == stop ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
{next_state, StateName, NewStateData}
|
|
|
|
end;
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-24 21:39:14 +01:00
|
|
|
|
|
|
|
handle_info({route_chan, Channel, Resource,
|
|
|
|
{xmlelement, "iq", Attrs, Els} = El},
|
|
|
|
StateName, StateData) ->
|
|
|
|
From = StateData#state.user,
|
2004-08-03 00:17:05 +02:00
|
|
|
To = jlib:make_jid(lists:concat([Channel, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, StateData#state.nick),
|
2003-02-24 21:39:14 +01:00
|
|
|
case jlib:iq_query_info(El) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{xmlns = ?NS_MUC_ADMIN} = IQ ->
|
|
|
|
iq_admin(StateData, Channel, From, To, IQ);
|
|
|
|
#iq{xmlns = ?NS_VERSION} ->
|
2003-08-12 21:23:55 +02:00
|
|
|
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);
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{xmlns = ?NS_TIME} ->
|
2003-08-12 21:23:55 +02:00
|
|
|
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);
|
2005-05-18 00:35:16 +02:00
|
|
|
#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);
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{} ->
|
2003-02-24 21:39:14 +01:00
|
|
|
Err = jlib:make_error_reply(
|
2003-05-29 21:07:41 +02:00
|
|
|
El, ?ERR_FEATURE_NOT_IMPLEMENTED),
|
2003-12-17 21:13:21 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-02-24 21:39:14 +01:00
|
|
|
end,
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({route_chan, Channel, Resource, Packet}, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
|
|
|
|
|
|
|
handle_info({route_nick, Nick,
|
|
|
|
{xmlelement, "message", Attrs, Els} = El},
|
|
|
|
StateName, StateData) ->
|
|
|
|
NewStateData =
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"chat" ->
|
|
|
|
Body = xml:get_path_s(El, [{elem, "body"}, cdata]),
|
2004-04-17 21:00:10 +02:00
|
|
|
case Body of
|
|
|
|
"/quote " ++ Rest ->
|
|
|
|
?SEND(Rest ++ "\r\n");
|
2006-02-18 20:56:16 +01:00
|
|
|
"/msg " ++ Rest ->
|
|
|
|
?SEND("PRIVMSG " ++ Rest ++ "\r\n");
|
|
|
|
"/me " ++ Rest ->
|
|
|
|
Strings = string:tokens(Rest, "\n"),
|
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format(
|
|
|
|
"PRIVMSG ~s :\001ACTION ~s\001\r\n",
|
|
|
|
[Nick, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res);
|
|
|
|
"/ctcp " ++ Rest ->
|
|
|
|
Words = string:tokens(Rest, " "),
|
|
|
|
case Words of
|
|
|
|
[CtcpDest | _ ] ->
|
|
|
|
CtcpCmd = toupper(string:substr(Rest, string:str(Rest, " ")+1 )),
|
|
|
|
Res = io_lib:format(
|
|
|
|
"PRIVMSG ~s :~s\r\n",
|
|
|
|
[CtcpDest, "\001" ++ CtcpCmd ++ "\001"]),
|
|
|
|
?SEND(Res);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2004-04-17 21:00:10 +02:00
|
|
|
_ ->
|
2006-02-18 20:56:16 +01:00
|
|
|
Strings = string:tokens(Body, "\n"),
|
2004-04-17 21:00:10 +02:00
|
|
|
Res = lists:concat(
|
|
|
|
lists:map(
|
|
|
|
fun(S) ->
|
|
|
|
io_lib:format("PRIVMSG ~s :~s\r\n",
|
|
|
|
[Nick, S])
|
|
|
|
end, Strings)),
|
|
|
|
?SEND(Res)
|
|
|
|
end;
|
2003-05-29 21:07:41 +02:00
|
|
|
"error" ->
|
|
|
|
stop;
|
2003-02-18 21:33:10 +01:00
|
|
|
_ ->
|
|
|
|
StateData
|
|
|
|
end,
|
2003-05-29 21:07:41 +02:00
|
|
|
if
|
|
|
|
NewStateData == stop ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
{next_state, StateName, NewStateData}
|
|
|
|
end;
|
2003-02-18 21:33:10 +01:00
|
|
|
|
|
|
|
handle_info({route_nick, Nick, Packet}, StateName, StateData) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
|
|
|
|
|
|
|
handle_info({ircstring, [$P, $I, $N, $G, $ | ID]}, StateName, StateData) ->
|
2003-02-23 21:13:39 +01:00
|
|
|
send_text(StateData, "PONG " ++ ID ++ "\r\n"),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
|
|
|
handle_info({ircstring, [$: | String]}, StateName, StateData) ->
|
|
|
|
Words = string:tokens(String, " "),
|
|
|
|
NewStateData =
|
2003-02-17 16:11:27 +01:00
|
|
|
case Words of
|
|
|
|
[_, "353" | Items] ->
|
2003-02-18 21:33:10 +01:00
|
|
|
process_channel_list(StateData, Items);
|
2003-02-27 21:09:03 +01:00
|
|
|
[_, "332", Nick, [$# | Chan] | _] ->
|
|
|
|
process_channel_topic(StateData, Chan, String),
|
|
|
|
StateData;
|
2006-02-18 20:56:16 +01:00
|
|
|
[_, "333", Nick, [$# | Chan] | _] ->
|
|
|
|
process_channel_topic_who(StateData, Chan, String),
|
|
|
|
StateData;
|
2005-05-18 00:35:16 +02:00
|
|
|
[_, "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;
|
2003-02-17 16:11:27 +01:00
|
|
|
[From, "PRIVMSG", [$# | Chan] | _] ->
|
2003-02-17 20:43:50 +01:00
|
|
|
process_chanprivmsg(StateData, Chan, From, String),
|
|
|
|
StateData;
|
2003-07-21 22:01:22 +02:00
|
|
|
[From, "NOTICE", [$# | Chan] | _] ->
|
|
|
|
process_channotice(StateData, Chan, From, String),
|
|
|
|
StateData;
|
2003-02-17 20:43:50 +01:00
|
|
|
[From, "PRIVMSG", Nick, ":\001VERSION\001" | _] ->
|
|
|
|
process_version(StateData, Nick, From),
|
2003-02-17 16:11:27 +01:00
|
|
|
StateData;
|
2005-05-18 00:35:16 +02:00
|
|
|
[From, "PRIVMSG", Nick, ":\001USERINFO\001" | _] ->
|
|
|
|
process_userinfo(StateData, Nick, From),
|
|
|
|
StateData;
|
2003-02-18 21:33:10 +01:00
|
|
|
[From, "PRIVMSG", Nick | _] ->
|
|
|
|
process_privmsg(StateData, Nick, From, String),
|
2003-02-17 16:11:27 +01:00
|
|
|
StateData;
|
2003-07-21 22:01:22 +02:00
|
|
|
[From, "NOTICE", Nick | _] ->
|
|
|
|
process_notice(StateData, Nick, From, String),
|
|
|
|
StateData;
|
2003-02-27 21:09:03 +01:00
|
|
|
[From, "TOPIC", [$# | Chan] | _] ->
|
|
|
|
process_topic(StateData, Chan, From, String),
|
|
|
|
StateData;
|
2003-02-18 21:33:10 +01:00
|
|
|
[From, "PART", [$# | Chan] | _] ->
|
|
|
|
process_part(StateData, Chan, From, String);
|
2003-02-21 20:52:15 +01:00
|
|
|
[From, "QUIT" | _] ->
|
|
|
|
process_quit(StateData, From, String);
|
2003-02-17 16:11:27 +01:00
|
|
|
[From, "JOIN", Chan | _] ->
|
2003-02-18 21:33:10 +01:00
|
|
|
process_join(StateData, Chan, From, String);
|
2003-02-17 16:11:27 +01:00
|
|
|
[From, "MODE", [$# | Chan], "+o", Nick | _] ->
|
|
|
|
process_mode_o(StateData, Chan, From, Nick,
|
|
|
|
"admin", "moderator"),
|
|
|
|
StateData;
|
|
|
|
[From, "MODE", [$# | Chan], "-o", Nick | _] ->
|
|
|
|
process_mode_o(StateData, Chan, From, Nick,
|
|
|
|
"member", "participant"),
|
|
|
|
StateData;
|
|
|
|
[From, "KICK", [$# | Chan], Nick | _] ->
|
2006-02-18 20:56:16 +01:00
|
|
|
process_kick(StateData, Chan, From, Nick, String),
|
2003-02-17 16:11:27 +01:00
|
|
|
StateData;
|
2003-02-18 21:33:10 +01:00
|
|
|
[From, "NICK", Nick | _] ->
|
|
|
|
process_nick(StateData, From, Nick);
|
2003-02-17 16:11:27 +01:00
|
|
|
_ ->
|
2003-10-07 22:31:44 +02:00
|
|
|
?DEBUG("unknown irc command '~s'~n", [String]),
|
2003-02-17 16:11:27 +01:00
|
|
|
StateData
|
|
|
|
end,
|
|
|
|
NewStateData1 =
|
2003-02-16 21:07:21 +01:00
|
|
|
case StateData#state.outbuf of
|
|
|
|
"" ->
|
2003-02-17 16:11:27 +01:00
|
|
|
NewStateData;
|
2003-02-16 21:07:21 +01:00
|
|
|
Data ->
|
2003-02-23 21:13:39 +01:00
|
|
|
send_text(NewStateData, Data),
|
2003-02-17 16:11:27 +01:00
|
|
|
NewStateData#state{outbuf = ""}
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
2003-02-17 16:11:27 +01:00
|
|
|
{next_state, stream_established, NewStateData1};
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
handle_info({ircstring, [$E, $R, $R, $O, $R | _] = String},
|
|
|
|
StateName, StateData) ->
|
|
|
|
process_error(StateData, String),
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
2003-02-16 21:07:21 +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};
|
|
|
|
handle_info({tcp, Socket, Data}, StateName, StateData) ->
|
|
|
|
Buf = StateData#state.inbuf ++ binary_to_list(Data),
|
2003-02-20 18:12:03 +01:00
|
|
|
{ok, Strings} = regexp:split([C || C <- Buf, C /= $\r], "\n"),
|
2003-10-07 22:31:44 +02:00
|
|
|
?DEBUG("strings=~p~n", [Strings]),
|
2003-02-23 21:13:39 +01:00
|
|
|
NewBuf = process_lines(StateData#state.encoding, Strings),
|
2003-02-16 21:07:21 +01:00
|
|
|
{next_state, StateName, StateData#state{inbuf = NewBuf}};
|
|
|
|
handle_info({tcp_closed, Socket}, StateName, StateData) ->
|
|
|
|
gen_fsm:send_event(self(), closed),
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({tcp_error, Socket, Reason}, StateName, StateData) ->
|
|
|
|
gen_fsm:send_event(self(), closed),
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(Reason, StateName, StateData) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mod_irc:closed_connection(StateData#state.host,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server),
|
2003-02-18 21:33:10 +01:00
|
|
|
bounce_messages("Server Connect Failed"),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Chan) ->
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, StateData#state.nick),
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "error"}],
|
|
|
|
[{xmlelement, "error", [{"code", "502"}],
|
|
|
|
[{xmlcdata, "Server Connect Failed"}]}]})
|
|
|
|
end, dict:fetch_keys(StateData#state.channels)),
|
2003-02-16 21:07:21 +01:00
|
|
|
case StateData#state.socket of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Socket ->
|
|
|
|
gen_tcp:close(Socket)
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-02-23 21:13:39 +01:00
|
|
|
send_text(#state{socket = Socket, encoding = Encoding}, Text) ->
|
|
|
|
CText = iconv:convert("utf-8", Encoding, lists:flatten(Text)),
|
2003-10-07 22:31:44 +02:00
|
|
|
%?DEBUG("IRC OUTu: ~s~nIRC OUTk: ~s~n", [Text, CText]),
|
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
|
|
|
|
{send_element, El} ->
|
|
|
|
{xmlelement, Name, Attrs, SubTags} = El,
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
"502", Reason),
|
|
|
|
From = jlib:string_to_jid(xml:get_attr_s("from", Attrs)),
|
|
|
|
To = jlib:string_to_jid(xml:get_attr_s("to", Attrs)),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2003-02-16 21:07:21 +01:00
|
|
|
end,
|
|
|
|
bounce_messages(Reason)
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
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
|
|
|
|
|
|
|
|
2003-02-23 21:13:39 +01:00
|
|
|
process_lines(Encoding, [S]) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
S;
|
2003-02-23 21:13:39 +01:00
|
|
|
process_lines(Encoding, [S | Ss]) ->
|
|
|
|
self() ! {ircstring, iconv:convert(Encoding, "utf-8", S)},
|
|
|
|
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;
|
2003-02-16 21:07:21 +01:00
|
|
|
process_channel_list_find_chan(StateData, [[$# | Chan] | Items]) ->
|
|
|
|
process_channel_list_users(StateData, Chan, Items);
|
|
|
|
process_channel_list_find_chan(StateData, [_ | Items]) ->
|
|
|
|
process_channel_list_find_chan(StateData, Items).
|
|
|
|
|
|
|
|
process_channel_list_users(StateData, Chan, []) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData;
|
2003-02-16 21:07:21 +01:00
|
|
|
process_channel_list_users(StateData, Chan, [User | Items]) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
NewStateData = process_channel_list_user(StateData, Chan, User),
|
|
|
|
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
|
|
|
|
[$: | U1] -> U1;
|
|
|
|
_ -> User
|
|
|
|
end,
|
|
|
|
{User2, Affiliation, Role} =
|
|
|
|
case User1 of
|
|
|
|
[$@ | U2] -> {U2, "admin", "moderator"};
|
2003-07-21 22:01:22 +02:00
|
|
|
[$+ | U2] -> {U2, "member", "participant"};
|
2006-02-18 21:05:05 +01:00
|
|
|
[$\% | U2] -> {U2, "admin", "moderator"};
|
|
|
|
[$& | U2] -> {U2, "admin", "moderator"};
|
|
|
|
[$~ | U2] -> {U2, "admin", "moderator"};
|
2003-02-16 21:07:21 +01:00
|
|
|
_ -> {User1, "member", "participant"}
|
|
|
|
end,
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, User2),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", Affiliation},
|
|
|
|
{"role", Role}],
|
|
|
|
[]}]}]}),
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
|
|
|
fun(Ps) ->
|
|
|
|
?SETS:add_element(User2, Ps)
|
|
|
|
end, StateData#state.channels) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
StateData;
|
|
|
|
NS ->
|
|
|
|
StateData#state{channels = NS}
|
|
|
|
end.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
|
2003-02-27 21:09:03 +01:00
|
|
|
process_channel_topic(StateData, Chan, String) ->
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*332[^:]*:", ""),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2006-02-18 20:56:16 +01:00
|
|
|
StateData#state.host, ""),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
2006-02-18 20:56:16 +01:00
|
|
|
[{xmlelement, "subject", [], [{xmlcdata, Msg1}]},
|
|
|
|
{xmlelement, "body", [], [{xmlcdata, "Topic for #" ++ Chan ++ ": " ++ Msg1}]}
|
|
|
|
]}).
|
|
|
|
|
|
|
|
process_channel_topic_who(StateData, Chan, String) ->
|
|
|
|
Words = string:tokens(String, " "),
|
|
|
|
Msg1 = case Words of
|
|
|
|
[_, "333", _, _Chan, Whoset , Timeset] ->
|
|
|
|
case string:to_integer(Timeset) of
|
|
|
|
{Unixtimeset, Rest} ->
|
|
|
|
"Topic for #" ++ Chan ++ " set by " ++ Whoset ++
|
|
|
|
" at " ++ unixtime2string(Unixtimeset);
|
|
|
|
_->
|
|
|
|
"Topic for #" ++ Chan ++ " set by " ++ Whoset
|
|
|
|
end;
|
|
|
|
[_, "333", _, _Chan, Whoset | _] ->
|
|
|
|
"Topic for #" ++ Chan ++ " set by " ++ Whoset;
|
|
|
|
_ ->
|
|
|
|
String
|
|
|
|
end,
|
|
|
|
Msg2 = filter_message(Msg1),
|
|
|
|
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}).
|
|
|
|
|
2003-02-27 21:09:03 +01:00
|
|
|
|
|
|
|
|
2005-05-18 00:35:16 +02:00
|
|
|
process_endofwhois(StateData, String, Nick) ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Nick, "!", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [], [{xmlcdata, "End of WHOIS"}]}]}).
|
|
|
|
|
|
|
|
process_whois311(StateData, String, Nick, Ident, Irchost) ->
|
|
|
|
{ok, Fullname, _} = regexp:sub(String, ".*311[^:]*:", ""),
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Nick, "!", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, lists:concat(
|
|
|
|
["WHOIS: ", Nick, " is ",
|
|
|
|
Ident, "@" , Irchost, " : " , Fullname])}]}]}).
|
|
|
|
|
|
|
|
process_whois312(StateData, String, Nick, Ircserver) ->
|
|
|
|
{ok, Ircserverdesc, _} = regexp:sub(String, ".*312[^:]*:", ""),
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Nick, "!", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, lists:concat(["WHOIS: ", Nick, " use ",
|
|
|
|
Ircserver, " : ", Ircserverdesc])}]}]}).
|
|
|
|
|
|
|
|
process_whois319(StateData, String, Nick) ->
|
|
|
|
{ok, Chanlist, _} = regexp:sub(String, ".*319[^:]*:", ""),
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Nick, "!", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, lists:concat(["WHOIS: ", Nick, " is on ",
|
|
|
|
Chanlist])}]}]}).
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-17 20:43:50 +01:00
|
|
|
process_chanprivmsg(StateData, Chan, From, String) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
2003-02-17 21:01:48 +01:00
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*PRIVMSG[^:]*:", ""),
|
2003-02-17 16:11:27 +01:00
|
|
|
Msg1 = case Msg of
|
|
|
|
[1, $A, $C, $T, $I, $O, $N, $ | Rest] ->
|
|
|
|
"/me " ++ Rest;
|
|
|
|
_ ->
|
|
|
|
Msg
|
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-07-21 22:01:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
process_channotice(StateData, Chan, From, String) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*NOTICE[^:]*:", ""),
|
|
|
|
Msg1 = case Msg of
|
|
|
|
[1, $A, $C, $T, $I, $O, $N, $ | Rest] ->
|
|
|
|
"/me " ++ Rest;
|
|
|
|
_ ->
|
2006-02-18 20:56:16 +01:00
|
|
|
"/me NOTICE: " ++ Msg
|
2003-07-21 22:01:22 +02:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
2006-02-18 20:56:16 +01:00
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
process_privmsg(StateData, Nick, From, String) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*PRIVMSG[^:]*:", ""),
|
|
|
|
Msg1 = case Msg of
|
|
|
|
[1, $A, $C, $T, $I, $O, $N, $ | Rest] ->
|
|
|
|
"/me " ++ Rest;
|
|
|
|
_ ->
|
|
|
|
Msg
|
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2003-02-18 21:33:10 +01:00
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(lists:concat([FromUser, "!", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, ""),
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}).
|
|
|
|
|
2003-07-21 22:01:22 +02:00
|
|
|
|
|
|
|
process_notice(StateData, Nick, From, String) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*NOTICE[^:]*:", ""),
|
|
|
|
Msg1 = case Msg of
|
|
|
|
[1, $A, $C, $T, $I, $O, $N, $ | Rest] ->
|
|
|
|
"/me " ++ Rest;
|
|
|
|
_ ->
|
2006-02-18 20:56:16 +01:00
|
|
|
"/me NOTICE: " ++ Msg
|
2003-07-21 22:01:22 +02:00
|
|
|
end,
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg2 = filter_message(Msg1),
|
2003-07-21 22:01:22 +02:00
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(lists:concat([FromUser, "!", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, ""),
|
2003-07-21 22:01:22 +02:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
2006-02-18 20:56:16 +01:00
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
|
|
|
|
2003-02-17 20:43:50 +01:00
|
|
|
process_version(StateData, Nick, From) ->
|
2003-02-18 21:33:10 +01:00
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
send_text(
|
2003-02-23 21:13:39 +01:00
|
|
|
StateData,
|
2003-02-18 21:33:10 +01:00
|
|
|
io_lib:format("NOTICE ~s :\001VERSION "
|
|
|
|
"ejabberd IRC transport ~s (c) Alexey Shchepin"
|
|
|
|
"\001\r\n",
|
|
|
|
[FromUser, ?VERSION]) ++
|
|
|
|
io_lib:format("NOTICE ~s :\001VERSION "
|
2003-04-28 15:56:58 +02:00
|
|
|
"http://ejabberd.jabberstudio.org/"
|
2003-02-18 21:33:10 +01:00
|
|
|
"\001\r\n",
|
|
|
|
[FromUser])).
|
2003-02-17 20:43:50 +01:00
|
|
|
|
|
|
|
|
2005-05-18 00:35:16 +02:00
|
|
|
process_userinfo(StateData, Nick, From) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
send_text(
|
|
|
|
StateData,
|
|
|
|
io_lib:format("NOTICE ~s :\001USERINFO "
|
2006-02-18 20:56:16 +01:00
|
|
|
"xmpp:~s"
|
2005-05-18 00:35:16 +02:00
|
|
|
"\001\r\n",
|
|
|
|
[FromUser,
|
|
|
|
jlib:jid_to_string(StateData#state.user)])).
|
|
|
|
|
|
|
|
|
2003-02-27 21:09:03 +01:00
|
|
|
process_topic(StateData, Chan, From, String) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*TOPIC[^:]*:", ""),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
|
|
|
[{xmlelement, "subject", [], [{xmlcdata, Msg1}]},
|
|
|
|
{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, "/me has changed the subject to: " ++
|
|
|
|
Msg1}]}]}).
|
2003-02-27 21:09:03 +01:00
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
process_part(StateData, Chan, From, String) ->
|
2003-07-21 22:01:22 +02:00
|
|
|
[FromUser | FromIdent] = string:tokens(From, "!"),
|
2006-02-18 20:56:16 +01:00
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*PART[^:]*:", ""),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "unavailable"}],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "member"},
|
|
|
|
{"role", "none"}],
|
|
|
|
[]}]},
|
|
|
|
{xmlelement, "status", [],
|
2006-02-18 20:56:16 +01:00
|
|
|
[{xmlcdata, Msg1 ++ " (" ++ FromIdent ++ ")"}]}]
|
2003-10-31 11:48:06 +01:00
|
|
|
}),
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
|
|
|
fun(Ps) ->
|
|
|
|
remove_element(FromUser, Ps)
|
|
|
|
end, StateData#state.channels) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
StateData;
|
|
|
|
NS ->
|
|
|
|
StateData#state{channels = NS}
|
|
|
|
end.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
|
2003-02-21 20:52:15 +01:00
|
|
|
process_quit(StateData, From, String) ->
|
2003-07-21 22:01:22 +02:00
|
|
|
[FromUser | FromIdent] = string:tokens(From, "!"),
|
|
|
|
|
|
|
|
{ok, Msg, _} = regexp:sub(String, ".*QUIT[^:]*:", ""),
|
2005-01-14 00:04:47 +01:00
|
|
|
Msg1 = filter_message(Msg),
|
2003-02-21 20:52:15 +01:00
|
|
|
NewChans =
|
|
|
|
dict:map(
|
|
|
|
fun(Chan, Ps) ->
|
|
|
|
case ?SETS:is_member(FromUser, Ps) of
|
|
|
|
true ->
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-02-21 20:52:15 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "unavailable"}],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "member"},
|
|
|
|
{"role", "none"}],
|
2003-07-21 22:01:22 +02:00
|
|
|
[]}]},
|
|
|
|
{xmlelement, "status", [],
|
2006-02-18 20:56:16 +01:00
|
|
|
[{xmlcdata, Msg1 ++ " (" ++ FromIdent ++ ")"}]}
|
2003-07-21 22:01:22 +02:00
|
|
|
]}),
|
2003-02-21 20:52:15 +01:00
|
|
|
remove_element(FromUser, Ps);
|
|
|
|
_ ->
|
|
|
|
Ps
|
|
|
|
end
|
|
|
|
end, StateData#state.channels),
|
|
|
|
StateData.
|
|
|
|
|
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
process_join(StateData, Channel, From, String) ->
|
2003-07-21 22:01:22 +02:00
|
|
|
[FromUser | FromIdent] = string:tokens(From, "!"),
|
2003-02-16 21:07:21 +01:00
|
|
|
Chan = lists:subtract(Channel, ":#"),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "member"},
|
|
|
|
{"role", "participant"}],
|
|
|
|
[]}]},
|
|
|
|
{xmlelement, "status", [],
|
|
|
|
[{xmlcdata, FromIdent}]}]}),
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
case catch dict:update(Chan,
|
|
|
|
fun(Ps) ->
|
|
|
|
?SETS:add_element(FromUser, Ps)
|
|
|
|
end, StateData#state.channels) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
StateData;
|
|
|
|
NS ->
|
|
|
|
StateData#state{channels = NS}
|
|
|
|
end.
|
|
|
|
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
process_mode_o(StateData, Chan, From, Nick, Affiliation, Role) ->
|
|
|
|
%Msg = lists:last(string:tokens(String, ":")),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, Nick),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", Affiliation},
|
|
|
|
{"role", Role}],
|
|
|
|
[]}]}]}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2006-02-18 20:56:16 +01:00
|
|
|
process_kick(StateData, Chan, From, Nick, String) ->
|
|
|
|
Msg = lists:last(string:tokens(String, ":")),
|
|
|
|
Msg2 = Nick ++ " kicked by " ++ From ++ " (" ++ filter_message(Msg) ++ ")",
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
|
|
|
StateData#state.host, ""),
|
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "message", [{"type", "groupchat"}],
|
|
|
|
[{xmlelement, "body", [], [{xmlcdata, Msg2}]}]}),
|
2003-10-31 11:48:06 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, Nick),
|
2003-10-31 11:48:06 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "unavailable"}],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "none"},
|
|
|
|
{"role", "none"}],
|
|
|
|
[]},
|
|
|
|
{xmlelement, "status", [{"code", "307"}], []}
|
|
|
|
]}]}).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2003-02-18 21:33:10 +01:00
|
|
|
process_nick(StateData, From, NewNick) ->
|
|
|
|
[FromUser | _] = string:tokens(From, "!"),
|
|
|
|
Nick = lists:subtract(NewNick, ":"),
|
|
|
|
NewChans =
|
|
|
|
dict:map(
|
|
|
|
fun(Chan, Ps) ->
|
|
|
|
case ?SETS:is_member(FromUser, Ps) of
|
|
|
|
true ->
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, FromUser),
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "unavailable"}],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "member"},
|
|
|
|
{"role", "participant"},
|
|
|
|
{"nick", Nick}],
|
|
|
|
[]},
|
|
|
|
{xmlelement, "status", [{"code", "303"}], []}
|
|
|
|
]}]}),
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, Nick),
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [],
|
|
|
|
[{xmlelement, "x", [{"xmlns", ?NS_MUC_USER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"affiliation", "member"},
|
|
|
|
{"role", "participant"}],
|
|
|
|
[]}
|
|
|
|
]}]}),
|
|
|
|
?SETS:add_element(Nick,
|
|
|
|
remove_element(FromUser, Ps));
|
|
|
|
_ ->
|
|
|
|
Ps
|
|
|
|
end
|
|
|
|
end, StateData#state.channels),
|
|
|
|
StateData#state{channels = NewChans}.
|
|
|
|
|
|
|
|
|
|
|
|
process_error(StateData, String) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Chan) ->
|
|
|
|
ejabberd_router:route(
|
2003-10-31 11:48:06 +01:00
|
|
|
jlib:make_jid(
|
|
|
|
lists:concat([Chan, "%", StateData#state.server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
StateData#state.host, StateData#state.nick),
|
2003-02-18 21:33:10 +01:00
|
|
|
StateData#state.user,
|
|
|
|
{xmlelement, "presence", [{"type", "error"}],
|
|
|
|
[{xmlelement, "error", [{"code", "502"}],
|
|
|
|
[{xmlcdata, String}]}]})
|
|
|
|
end, dict:fetch_keys(StateData#state.channels)).
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-17 16:11:27 +01:00
|
|
|
|
|
|
|
remove_element(E, Set) ->
|
|
|
|
case ?SETS:is_element(E, Set) of
|
|
|
|
true ->
|
|
|
|
?SETS:del_element(E, Set);
|
|
|
|
_ ->
|
|
|
|
Set
|
|
|
|
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) ->
|
2003-02-24 21:39:14 +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} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
ResEls
|
|
|
|
}]};
|
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-24 21:39:14 +01:00
|
|
|
end,
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(ResIQ));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
process_iq_admin(StateData, Channel, set, SubEl) ->
|
|
|
|
case xml:get_subtag(SubEl, "item") of
|
|
|
|
false ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_BAD_REQUEST};
|
2003-02-24 21:39:14 +01:00
|
|
|
ItemEl ->
|
|
|
|
Nick = xml:get_tag_attr_s("nick", ItemEl),
|
|
|
|
Affiliation = xml:get_tag_attr_s("affiliation", ItemEl),
|
|
|
|
Role = xml:get_tag_attr_s("role", ItemEl),
|
|
|
|
Reason = xml:get_path_s(ItemEl, [{elem, "reason"}, cdata]),
|
|
|
|
process_admin(StateData, Channel, Nick, Affiliation, Role, Reason)
|
|
|
|
end;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process_admin(StateData, Channel, "", Affiliation, Role, Reason) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED};
|
2003-02-24 21:39:14 +01:00
|
|
|
|
|
|
|
process_admin(StateData, Channel, Nick, Affiliation, "none", Reason) ->
|
|
|
|
case Reason of
|
|
|
|
"" ->
|
|
|
|
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]))
|
|
|
|
end,
|
|
|
|
{result, []};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(C) ->
|
|
|
|
if (C < 32) and
|
|
|
|
(C /= 9) and
|
|
|
|
(C /= 10) and
|
|
|
|
(C /= 13) ->
|
|
|
|
false;
|
|
|
|
true -> true
|
|
|
|
end
|
2006-02-18 20:56:16 +01:00
|
|
|
end, filter_mirc_colors(Msg)).
|
|
|
|
|
|
|
|
filter_mirc_colors(Msg) ->
|
|
|
|
case regexp:gsub(Msg, "(\\003[0-9]+)(,[0-9]+)?", "") of
|
|
|
|
{ok, Msg2, _} ->
|
|
|
|
Msg2;
|
|
|
|
_ ->
|
|
|
|
Msg
|
|
|
|
end.
|
|
|
|
|
|
|
|
unixtime2string(Unixtime) ->
|
|
|
|
Secs = Unixtime + calendar:datetime_to_gregorian_seconds(
|
|
|
|
{{1970, 1, 1}, {0,0,0}}),
|
|
|
|
case calendar:universal_time_to_local_time(
|
|
|
|
calendar:gregorian_seconds_to_datetime(Secs)) of
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} ->
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format("~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second]));
|
|
|
|
_->
|
|
|
|
"0000-00-00 00:00:00"
|
|
|
|
end.
|
|
|
|
|
|
|
|
toupper([C | Cs]) ->
|
|
|
|
if
|
|
|
|
C >= $a, C =< $z ->
|
|
|
|
[C - 32 | toupper(Cs)];
|
|
|
|
true ->
|
|
|
|
[C | toupper(Cs)]
|
|
|
|
end;
|
|
|
|
toupper([]) ->
|
|
|
|
[].
|
|
|
|
|