From f3f5c42e9a151046694c5b03b1289f312b34439e Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 1 Jun 2011 02:20:58 +0900 Subject: [PATCH 1/6] add ssl support --- lib/shout-bot.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index 5630a23..17e33d5 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -31,11 +31,11 @@ require "socket" class ShoutBot - def self.shout(uri, password = nil, &block) + def self.shout(uri, password = nil, ssl = false, &block) raise ArgumentError unless block_given? uri = Addressable::URI.parse(uri) - irc = new(uri.host, uri.port, uri.user, uri.password) do |irc| + irc = new(uri.host, uri.port, uri.user, uri.password, ssl) do |irc| if channel = uri.fragment irc.join(channel, password, &block) else @@ -47,10 +47,16 @@ def self.shout(uri, password = nil, &block) attr_accessor :channel - def initialize(server, port, nick, password=nil) + def initialize(server, port, nick, password = nil, ssl) raise ArgumentError unless block_given? - @socket = TCPSocket.open(server, port || 6667) + if ssl + @socket = OpenSSL::SSL::SSLSocket.new(TCPSocket.new(server, port || 6667), OpenSSL::SSL::SSLContext.new) + @socket.sync = true + @socket.connect + else + @socket = TCPSocket.open(server, port || 6667) + end @socket.puts "PASSWORD #{password}" if password @socket.puts "NICK #{nick}" @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}" @@ -60,7 +66,7 @@ def initialize(server, port, nick, password=nil) @socket.gets until @socket.eof? end - def join(channel, password = nil) + def join(channel, password) raise ArgumentError unless block_given? @channel = "##{channel}" From 04988ac2b7bcc3d71b29c2c8526d2cf4b822d99e Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 1 Jun 2011 12:41:25 +0900 Subject: [PATCH 2/6] require openssl --- lib/shout-bot.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index 17e33d5..0116d92 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -29,6 +29,7 @@ require "addressable/uri" require "socket" +require "openssl" class ShoutBot def self.shout(uri, password = nil, ssl = false, &block) From e7a635a868803e76444faa828b1343bfec9c2f51 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 1 Jun 2011 19:35:06 +0900 Subject: [PATCH 3/6] ssl verify none :/ --- lib/shout-bot.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index 0116d92..3a2947a 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -52,8 +52,10 @@ def initialize(server, port, nick, password = nil, ssl) raise ArgumentError unless block_given? if ssl - @socket = OpenSSL::SSL::SSLSocket.new(TCPSocket.new(server, port || 6667), OpenSSL::SSL::SSLContext.new) - @socket.sync = true + ssl_context = OpenSSL::SSL::SSLContext.new + ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE + tcp_socket = TCPSocket.new(server, port || 6667) + @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context) @socket.connect else @socket = TCPSocket.open(server, port || 6667) From f6c404ac36f69235a1bd32c32059085340c54d86 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 1 Jun 2011 21:01:03 +0900 Subject: [PATCH 4/6] added sendln, corrected cmds, fixed ssl support --- lib/shout-bot.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index 3a2947a..e96c970 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -48,24 +48,31 @@ def self.shout(uri, password = nil, ssl = false, &block) attr_accessor :channel + def sendln(cmd) + puts "Send: #{cmd}" + @socket.write("#{cmd}\r\n") + STDOUT.flush + end + def initialize(server, port, nick, password = nil, ssl) raise ArgumentError unless block_given? + tcp_socket = TCPSocket.new(server, port || 6667) if ssl ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE - tcp_socket = TCPSocket.new(server, port || 6667) @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context) + @socket.sync = true @socket.connect else - @socket = TCPSocket.open(server, port || 6667) + @socket = tcp_socket end - @socket.puts "PASSWORD #{password}" if password - @socket.puts "NICK #{nick}" - @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}" + sendln "PASS #{password}" if password + sendln "NICK #{nick}" + sendln "USER #{nick} 0 * :#{nick}" sleep 1 yield self - @socket.puts "QUIT" + sendln "QUIT :quit" @socket.gets until @socket.eof? end @@ -74,13 +81,13 @@ def join(channel, password) @channel = "##{channel}" password = password && " #{password}" || "" - @socket.puts "JOIN #{@channel}#{password}" + sendln "JOIN #{@channel}#{password}" yield self - @socket.puts "PART #{@channel}" + sendln "PART #{@channel}" end def say(message) return unless @channel - @socket.puts "PRIVMSG #{@channel} :#{message}" + sendln "PRIVMSG #{@channel} :#{message}" end end From 229aad93bf601ede314cf3cd220bfb2fbc1968de Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Thu, 2 Jun 2011 00:58:59 +0900 Subject: [PATCH 5/6] remove init password default, 1.8.x made a fuss --- lib/shout-bot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index e96c970..37f73e2 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -54,7 +54,7 @@ def sendln(cmd) STDOUT.flush end - def initialize(server, port, nick, password = nil, ssl) + def initialize(server, port, nick, password, ssl) raise ArgumentError unless block_given? tcp_socket = TCPSocket.new(server, port || 6667) From fa3e29450e46983a781953ac38ebd92e45b67c58 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Thu, 2 Jun 2011 01:10:03 +0900 Subject: [PATCH 6/6] remove debugging puts --- lib/shout-bot.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/shout-bot.rb b/lib/shout-bot.rb index 37f73e2..d9e3db7 100644 --- a/lib/shout-bot.rb +++ b/lib/shout-bot.rb @@ -49,7 +49,6 @@ def self.shout(uri, password = nil, ssl = false, &block) attr_accessor :channel def sendln(cmd) - puts "Send: #{cmd}" @socket.write("#{cmd}\r\n") STDOUT.flush end