Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c1a072df36
Fetching contributors…

Cannot retrieve contributors at this time

file 202 lines (172 sloc) 6.848 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
-module(pony_client).

-behaviour(gen_server).

-include_lib("kernel/include/inet.hrl").

%% API
-export([start_link/4, msg/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-define(SERVER, ?MODULE).

-record(state, { socket, listener,
                 cont = <<>> :: binary(),
                 synchronized = no :: 'no' | {'yes', string()},
                 username = <<"*">>,
                 realname = <<"*">>,
                 nickname = <<"*">>
               }).

%%%===================================================================
%%% API
%%%===================================================================

%% @doc
%% Starts the server
%% @end
start_link(ListenerPid, Socket, Transport, Opts) ->
    gen_server:start_link(?MODULE, [ListenerPid,
                                    Socket,
                                    Transport,
                                    Opts], []).

msg(Client, Msg) ->
    gen_server:cast(Client, Msg).

%%%===================================================================

%% @private
init([ListenerPid, Socket, Transport, _Opts]) ->
    {ok, #state{ listener = ListenerPid,
                 socket = {Transport, Socket},
                 cont = <<>>,
                 synchronized = no }, 0}. %% Note immediate timeout

%% @private
handle_call(Request, _From, State) ->
    lager:debug("Unknown request in call: ~p", [Request]),
    Reply = ok,
    {reply, Reply, State}.

%% @private
handle_cast({numeric, Numeric, Text},
            #state { socket = Sock,
                     synchronized = {yes, _} } = State) ->
    out(Sock, pony_protocol:render_numeric(Numeric, Text)),
    {noreply, State};
handle_cast({msg, M}, #state { socket = Sock,
                               synchronized = {yes, _} } = State) ->
    out(Sock, pony_protocol:render(M)),
    {noreply, State};
handle_cast(Msg, State) ->
    lager:debug("Unknown message in cast: ~p", [Msg]),
    {noreply, State}.

%% @private
handle_info({tcp_closed, S},
            #state { socket = {_, S} } = State) ->
    {stop, normal, State};
handle_info({tcp, S, Chunk},
            #state { socket = {_, S} = Socket,
                     synchronized = {yes, _},
                     cont = Cont } = State) ->
    ack(Socket),
    case process_stream_chunk(Chunk, Cont) of
        {ok, NewCont} ->
            {noreply, State#state { cont = NewCont }};
        {msgs, Msgs, NewCont} ->
            {noreply,
             lists:foldl(fun handle_message/2,
                         State#state { cont = NewCont },
                         Msgs)}
    end;
handle_info(timeout, #state { synchronized = no,
                              listener = Listener,
                              socket = Socket,
                              nickname = Nick } = State) ->
    {ok, Hostname} = sync(Socket),
    %% We only let ranch continue when this client has been accepted
    %% This effectively throttles the inbound connection so we at most
    %% process a limited amount of new connections
    ranch:accept_ack(Listener),
    ack(Socket),
    send_numeric('RPL_WELCOME', [pony:me(), Nick, pony:description(), Nick]),
    send_numeric('RPL_YOURHOST', [pony:me(), Nick, pony:server(), pony:version()]),
    %% this.SendMotd()
    {noreply, State#state { synchronized = {yes, Hostname} }};
handle_info(Info, State) ->
    lager:warning("Unknown message received: ~p State: ~p", [Info, State]),
    {noreply, State}.

%% @private
terminate(_Reason, _State) ->
    ok.

%% @private
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%%===================================================================

ack({Transport, Socket}) ->
    Transport:setopts(Socket, [{active, once}]).

send_numeric(Numeric, Args) ->
    msg(self(), {numeric, Numeric, Args}).

respond(M) ->
    msg(self(), {msg, M}).

handle_message(M, State) ->
    case pony_protocol:parse(M) of
        {ok, Prefix, Command, Args} ->
            handle_message(Prefix, Command, Args, State);
        {error, _} ->
            %% Ignore errors for now
            State
    end.


handle_message(Prefix, Command, Args, #state { nickname = CurNick } = State) ->
    case {Prefix, Command, Args} of
        {<<>>, nick, []} ->
            send_numeric('ERR_NONICKNAMEGIVEN', [pony:me(), CurNick]),
            State;
        {<<>>, nick, [NickName]} ->
            case pony_nick_srv:register(NickName) of
                ok ->
                    gproc:add_local_name({nick, NickName}),
                    State#state { nickname = NickName };
                nick_in_use ->
                    send_numeric('ERR_ERRONEUSNICKNAME', [pony:me(), CurNick]),
                    State
            end;
        {<<>>, user, L} when is_list(L), length(L) < 4 ->
            send_numeric('ERR_NEEDMOREPARAMS', [pony:me(), CurNick, "USER"]),
            State;
        {<<>>, user, [Username, _, _, RealName]} ->
            State#state { username = <<"~", Username/binary>>,
                          realname = RealName };
        {<<>>, ping, []} ->
            send_numeric('ERR_NEEDMOREPARAMS', [pony:me(), CurNick, "PING"]),
            State;
        {<<>>, ping, [Server]} ->
            respond({pong, Server}),
            State;
        _ ->
            lager:debug("Unhandled message: ~p", [[{prefix, Prefix},
                                                   {command, Command},
                                                   {args, Args}]]),
            State
    end.

%% @doc Synchronize the socket
sync(Sock) ->
    out(Sock, "NOTICE AUTH :*** Processing connection to ~s ...",
        [pony:me()]),
    out(Sock, "NOTICE AUTH :*** Looking up your hostname ..."),
    {ok, Hostname} = lookup_hostname(Sock),
    out(Sock, "NOTICE AUTH :*** Found your hostname (~s) ...", [Hostname]),
    %% @todo Unregistered proto handling goes here
    {ok, Hostname}.

out({Transport, Socket}, Data) ->
    Transport:send(Socket, [Data, <<"\r\n">>]).

out({Transport, Socket}, Format, Params) ->
    Transport:send(Socket, [io_lib:format(Format, Params), <<"\r\n">>]).

lookup_hostname({_Transport, Socket}) ->
    %% @todo should probably be a service on its own
    {ok, {Address, _Port}} = inet:peername(Socket),
    {ok, #hostent { h_name = Hostname }} = inet:gethostbyaddr(Address),
    {ok, Hostname}.

process_stream_chunk(Chunk, Cont) ->
    process_stream_chunk(Chunk, Cont, []).

process_stream_chunk(Chunk, Cont, Msgs) ->
    Data = <<Cont/binary, Chunk/binary>>,
    case binary:split(Data, <<"\r\n">>, []) of
        [_] ->
            return_messages(Msgs, Data);
        [Line, Rest] ->
            process_stream_chunk(<<>>, Rest, [Line | Msgs])
    end.

return_messages([], Data) -> {ok, Data};
return_messages(Msgs, Data) -> {msgs, lists:reverse(Msgs), Data}.
Something went wrong with that request. Please try again.