Commits

Anonymous committed 79f4953

SSL Support added.

  • Participants
  • Parent commits 25bd369

Comments (0)

Files changed (3)

File src/ircbot.erl

 -behaviour(application).
 
 -export([start/2, stop/1]).
+-export([connect/1, test/0]).
 
 start(_Type, StartArgs) ->
 	case ircbot_supervisor:start_link() of
 		Error
 	end.
 
+connect([Server, Port, Nick, Real, ServerName]) ->
+	ircbot_supervisor:add_server([Server, Port, Nick, Real, ServerName]).
+
+
+test() ->
+	Nick = "neckbeard",
+	Real = "erlang lol",
+	
+	connect(["irc.freenode.net", 6667, Nick, Real, "freenode"]).	
 
 stop(_State)->
 	exit(whereis(eb_sup), shutdown).

File src/ircbot_server.erl

 
 -define(LINEFEED, "\r\n").
 -define(TCP_OPTIONS, [binary, {packet, line}, {keepalive, true} ]).
+-define(SSL_OPTIONS, [binary, {verify, 0}, {packet, line}]).
 
 
 
 -behaviour(gen_server).
 
--export([start_link/0, test/2, connect/5, join/2, say/3, start/0, pong/2]).
+-export([start_link/0, connect/5, ssl_connect/6, join/2, say/3, pong/2]).
 
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
          code_change/3]).
+-export([add/2, send_line/2, send_line/3]).
 
--record(conn_info, {socket, server, port, nickname, realname, channels = [], pid=self()}).
+-record(conn_info, {socket, server, port, nickname, realname, channels = [], pid=self(), ssl=false}).
 
 %%--------------
 %%
 %%
 %%--------------
 
+add(A, B) ->
+	A + B.
 
-start() ->
-   {ok, Pid} = start_link(),
-   connect(Pid, "irc.synirc.net", 6667, "neckbeard", "neckbeard").
 start_link() ->
 %%  irc_event_manager:start_link(),
 %%  irc_event_manager:add_handler(irc_rawtext_handler),
 
 
   gen_server:start_link(?MODULE, [], []).
-
-test(Pid, Msg) ->
-   gen_server:cast(Pid, {test, Msg}).
-
+ 
 connect(Pid, Server, Port, Nick, Real) ->
 	gen_server:call(Pid, {connect, [Server, Port, Nick, Real]}).
 
+ssl_connect(Pid, Server, Port, Nick, Real, Pass) ->
+	gen_server:call(Pid, {connect, [Server, Port, Nick, Real, {ssl, true}, {password, Pass}]}).
+
 join(Pid, Channel) ->
 	gen_server:call(Pid, {join, Channel}).
 
 pong(Pid, Server) ->
 	gen_server:call(Pid, {pong, Server}).
 
+
+send_line(Socket, Message, {ssl, true}) ->
+	io:fwrite("~w~n", [Message]),
+	ssl:send(Socket, Message ++ ?LINEFEED);
+send_line(Socket, Message, {ssl, false}) ->
+	gen_tcp:send(Socket, Message ++ ?LINEFEED);
+
+send_line(Socket, Message, _) ->
+	gen_tcp:send(Socket, Message ++ ?LINEFEED).
+
+send_line(Socket, Message) ->
+	gen_tcp:send(Socket, Message ++ ?LINEFEED).
+	
+
 %%------------------------------
 %%
 %% gen_server callback functions
 	State = #conn_info{},
 
 	{ok, State}.
+handle_call({connect, [Server, Port, Nick, Real, {ssl, true}, {password, Pass}]}, From, State) ->
+	ssl:start(),
+	{ok, Sock} = ssl:connect(Server, Port, ?SSL_OPTIONS),
+	NewState = #conn_info{socket=Sock, server=Server, port=Port, 
+				nickname=Nick, realname=Real, channels=[], ssl=true, pid=self() },
+	
+	send_line(Sock, ["NICK ", Nick], {ssl, true}),
+	send_line(Sock, ["PASS ", Pass], {ssl, true}),
+	send_line(Sock, ["USER ", Nick, " 8 * :", Real], {ssl, true}),
 
+	{reply, ok, NewState};
 handle_call({connect, [Server, Port, Nick, Real]}, From, State) ->
 
 	%% set our configuration_info record
 	
 	
 	{ok, Sock} = gen_tcp:connect(Server, Port, ?TCP_OPTIONS),
-	NewState = State#conn_info{socket=Sock, server=Server, port=Port, 
-				   nickname=Nick, realname=Real, channels=[], pid=self()},
+	NewState = #conn_info{socket=Sock, server=Server, port=Port, 
+				   nickname=Nick, realname=Real, channels=[], pid=self(), ssl=false},
 	
-        gen_tcp:send(NewState#conn_info.socket, ["NICK ", Nick, ?LINEFEED]),
-        gen_tcp:send(NewState#conn_info.socket, ["USER ", Nick, " 8 * :", Real, ?LINEFEED]),
-
+	send_line(Sock, ["NICK ", Nick], {ssl, false}),
+	send_line(Sock, ["USER ", Nick, " 8 * :", Real], {ssl, false}), 
         { reply, ok, NewState };
 
 handle_call({disconnect, QuitMsg}, _From, State) ->
-	gen_tcp:send(State#conn_info.socket, ["QUIT ", QuitMsg]),
-	gen_tcp:close(State#conn_info.socket),
+	
+	send_line(State#conn_info.socket, ["QUIT ", QuitMsg], {ssl, State#conn_info.ssl}),
+	%%close_conn(State#conn_info.socket, {ssl, State#conn_info.ssl}),
 	NewState = #conn_info{},
 
 	{ reply, ok, NewState };
 handle_call({nick, Nick}, _From, State) ->
 	%% TODO add event handler for Operation #433 for failed nicknames 
 
-	gen_tcp:send(State#conn_info.socket, "NICK " ++ Nick ++ " " ++ ?LINEFEED),
+	send_line(State#conn_info.socket, "NICK " ++ Nick ++ " ", {ssl, State#conn_info.ssl}),
 	NewState = State#conn_info{nickname=Nick},
 
 	{reply, ok, NewState};
 	%% Note we do not update the channels list here 
 	%% we'll need to add an event handler for operation 353 for a successful join
 
-	gen_tcp:send(State#conn_info.socket, ["JOIN ", Channel, ?LINEFEED]),
+	send_line(State#conn_info.socket, ["JOIN ", Channel], {ssl, State#conn_info.ssl}),
 	{reply, ok, State };
 
 handle_call({pong, Server}, _From, State) ->
-	gen_tcp:send(State#conn_info.socket, ["PONG :", Server, ?LINEFEED]),
+	send_line(State#conn_info.socket, ["PONG :", Server, ?LINEFEED], {ssl, State#conn_info.ssl}),
 	{reply, ok, State};
 
 handle_call({say, Focus, Msg}, _From, State) ->
-	gen_tcp:send(State#conn_info.socket, ["PRIVMSG ", Focus, " :",  Msg, ?LINEFEED]),
+	send_line(State#conn_info.socket, ["PRIVMSG ", Focus, " :",  Msg, ?LINEFEED, {ssl, State#conn_info.ssl}]),
 	{reply, ok, State};
 
 
 handle_call({raw, String}, _From, State) ->
-	gen_tcp:send(State#conn_info.socket, String),
+	send_line(State#conn_info.socket, String, {ssl, State#conn_info.ssl}),
 	{reply, ok, State};
 
 handle_call(_Request, _From, State) ->
 
 
 
-handle_info({tcp, _Sock, <<$:, ServerBin/binary>>}, State) ->
+handle_info({_, _Sock, <<$:, ServerBin/binary>>}, State) ->
 	ServerMessage = binary_to_list(ServerBin),
 	%% I copied this method of parsing server messages from manderlbot, it seems to be the easiest way.
 	%% props to mickael remond <3
 			irc_event_manager:notify({parsed_msg, From, Operation, To}),
 			{noreply, State}
 	end;	 
-handle_info({tcp, _Sock, <<"PING :",  FromBin/binary>>}, State) ->
+handle_info({_, _Sock, <<"PING :",  FromBin/binary>>}, State) ->
 	%% PING/PONG responses are fairly low-level so we've implemented this here.  This does not need to be user editable and so should remain in this low level portion of the bot.
 
 	From = binary_to_list(FromBin),
-	gen_tcp:send(State#conn_info.socket, ["PONG :", From, ?LINEFEED]),
+	send_line(State#conn_info.socket, ["PONG :", From, ?LINEFEED], {ssl, State#conn_info.ssl}),
 
 	{noreply, State};
 
-handle_info({tcp, _Sock, Data}, State) ->
+handle_info({_, _Sock, Data}, State) ->
 
 	irc_event_manager:notify({raw_text,  Data}),
 	{noreply, State};
 		
 	
-handle_info(_Info, State) ->
+handle_info(Info, State) ->
+  io:fwrite("~w~n", [Info]),
   {noreply, State}.
 
 

File src/ircbot_supervisor.erl

 -author(jrb).
 -behaviour(supervisor).
 -export([start_link/0]).
--export([init/1, add_server/1, test/0]).
+-export([init/1, add_server/1]).
+-export([add_ssl_server/1]).
 -define(TCP_OPTIONS, [binary, {packet, line}, {keepalive, true} ]).
 
 start_link() ->
 	
 	{ok, {{one_for_one, 5, 10}, [EventManager]}}.
 
-test() ->
-	irc_event_manager:add_handler(irc_rawtext_handler),
-	add_server(["irc.synirc.net", 6667, "neckbeard23", "neckbeard", "synirc"]),
-        add_server(["irc.gamesurge.net", 6667, "nackbard23", "nackbard23", "gamesurge"]).
+add_ssl_server([Server, Port, Nick, Real, ServerName, Password]) ->
 
-add_server([Server, Port, Nick, Real, ServerName ]) ->
+       ServerAtom = list_to_atom(ServerName),
+
+
+        ChildRef = {ServerAtom, {ircbot_server, start_link, []}, temporary, 2000, worker, [gen_server]},
+
+        supervisor:start_child(ircbot_supervisor, ChildRef),
+
+        Children = supervisor:which_children(ircbot_supervisor),
+
+        {ServerAtom, ServerPid, _, _}  = lists:keyfind(ServerAtom, 1, Children),
+        ircbot_server:ssl_connect(ServerPid, Server, Port, Nick, Real, Password),
+        {ok, ServerPid}.
+
+add_server([Server, Port, Nick, Real, ServerName]) ->
 	%% Add a new server process to the supervision tree and connect
 
 	%% Grab our socket