Permalink
Browse files

Merge pull request #13 from nudded/irc-client-fix

IRC server connect issues
  • Loading branch information...
2 parents 91ea57b + f729b31 commit 0cf95a5efb0f169421bb5a65a45d012945608372 @joshk joshk committed Mar 12, 2012
Showing with 40 additions and 1 deletion.
  1. +15 −1 lib/irc_client.rb
  2. +25 −0 spec/irc_client_spec.rb
View
@@ -10,10 +10,13 @@
require 'socket'
class IrcClient
- attr_accessor :socket
+ attr_accessor :socket, :ping_thread
def initialize(server, nick, options = {})
@socket = TCPSocket.open(server, options[:port] || 6667)
+
+ @ping_thread = start_ping_thread
+
socket.puts "PASS #{options[:password]}" if options[:password]
socket.puts "NICK #{nick}"
socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
@@ -39,6 +42,17 @@ def say(message, channel, use_notice = false)
def quit
socket.puts 'QUIT'
socket.gets until socket.eof?
+ ping_thread.exit
+ end
+
+ private
+
+ def start_ping_thread
+ Thread.new(socket) do |s|
+ loop do
+ s.puts "PONG #{$1}" if s.gets =~ /^PING (.*)/
+ end
+ end
end
end
@@ -7,6 +7,7 @@
let(:nick) { 'travis_bot' }
let(:channel) { 'travis' }
let(:password) { 'secret' }
+ let(:ping) { 'testping' }
describe 'on initialization' do
describe 'with no port specified' do
@@ -49,6 +50,30 @@ def expect_standard_sequence
end
end
end
+
+ describe 'should connect to a server which requires ping/pong' do
+ before do
+ @socket = mock
+ TCPSocket.stubs(:open).returns @socket
+ @socket.stubs(:gets).returns("PING #{ping}").then.returns ""
+ end
+
+ def expect_standard_sequence
+ @socket.expects(:puts).with("NICK #{nick}")
+ @socket.expects(:puts).with("USER #{nick} #{nick} #{nick} :#{nick}")
+ @socket.expects(:puts).with("PONG #{ping}")
+ end
+
+ describe "without a password" do
+ it 'by sending NICK then USER' do
+ expect_standard_sequence
+ IrcClient.new(server, nick)
+ # this sleep is here so that the ping thread has a chance to run
+ sleep 0.5
+ end
+ end
+
+ end
end
describe 'with connection established' do

0 comments on commit 0cf95a5

Please sign in to comment.