diff --git a/services/irc.rb b/services/irc.rb index e29d829e6..12d20b474 100644 --- a/services/irc.rb +++ b/services/irc.rb @@ -37,34 +37,35 @@ def send_messages(messages) botname = data['nick'].to_s.empty? ? "GitHub#{rand(200)}" : data['nick'] command = data['notice'].to_i == 1 ? 'NOTICE' : 'PRIVMSG' - self.puts "PASS #{data['password']}" if !data['password'].to_s.empty? - self.puts "NICK #{botname}" - self.puts "MSG NICKSERV IDENTIFY #{data['nickservidentify']}" if !data['nickservidentify'].to_s.empty? - self.puts "USER #{botname} 8 * :GitHub IRCBot" + irc_puts "PASS #{data['password']}" if !data['password'].to_s.empty? + irc_puts "NICK #{botname}" + irc_puts "MSG NICKSERV IDENTIFY #{data['nickservidentify']}" if !data['nickservidentify'].to_s.empty? + irc_puts "USER #{botname} 8 * :GitHub IRCBot" loop do - case self.gets + case irc_gets when / 00[1-4] #{Regexp.escape(botname)} / break when /^PING\s*:\s*(.*)$/ - self.puts "PONG #{$1}" + irc_puts "PONG #{$1}" end end without_join = data['message_without_join'] == '1' rooms.each do |room| room, pass = room.split("::") - self.puts "JOIN #{room} #{pass}" unless without_join + irc_puts "JOIN #{room} #{pass}" unless without_join Array(messages).each do |message| - self.puts "#{command} #{room} :#{message}" + irc_puts "#{command} #{room} :#{message}" end - self.puts "PART #{room}" unless without_join + irc_puts "PART #{room}" unless without_join end - self.puts "QUIT" - self.gets until self.eof? + irc_puts "QUIT" + irc_response = [] + irc_response << irc_gets unless irc_eof? rescue SocketError => boom if boom.to_s =~ /getaddrinfo: Name or service not known/ raise_config_error 'Invalid host' @@ -79,15 +80,15 @@ def send_messages(messages) raise_config_error 'Host does not support SSL' end - def gets + def irc_gets irc.gets end - def eof? + def irc_eof? irc.eof? end - def puts(*args) + def irc_puts(*args) irc.puts *args end @@ -125,7 +126,7 @@ def format_commit_message(commit) "\002#{sha1[0..6]}\002 (#{files.size} files in #{dirs.size} dirs): #{short}" end end - + def branch_name_matches? return true if data['branch_regexes'].nil? return true if data['branch_regexes'].strip == "" diff --git a/test/irc_test.rb b/test/irc_test.rb index ea8e63d4e..91c3dba24 100644 --- a/test/irc_test.rb +++ b/test/irc_test.rb @@ -11,15 +11,15 @@ def writable_io @writable_io ||= StringIO.new end - def puts(*args) + def irc_puts(*args) writable_io.puts *args end - def gets + def irc_gets readable_io.gets end - def eof? + def irc_eof? true end @@ -44,10 +44,10 @@ def test_push assert_equal "QUIT", msgs.shift.strip assert_nil msgs.shift end - + def test_push_with_empty_branch_regex svc = service({'room' => 'r', 'nick' => 'n', 'branch_regexes' => ''}, payload) - + svc.receive_push msgs = svc.writable_io.string.split("\n") assert_equal "NICK n", msgs.shift @@ -61,7 +61,7 @@ def test_push_with_empty_branch_regex assert_equal "QUIT", msgs.shift.strip assert_nil msgs.shift end - + def test_push_with_single_matching_branch_regex svc = service({'room' => 'r', 'nick' => 'n', 'branch_regexes' => 'mast*'}, payload) @@ -78,7 +78,7 @@ def test_push_with_single_matching_branch_regex assert_equal "QUIT", msgs.shift.strip assert_nil msgs.shift end - + def test_push_with_single_mismatching_branch_regex svc = service({'room' => 'r', 'nick' => 'n', 'branch_regexes' => '^ticket*'}, payload) @@ -86,7 +86,7 @@ def test_push_with_single_mismatching_branch_regex msgs = svc.writable_io.string.split("\n") assert_nil msgs.shift end - + def test_push_with_multiple_branch_regexes_where_all_match svc = service({'room' => 'r', 'nick' => 'n', 'branch_regexes' => 'mast*,^ticket*'}, payload)