Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 11 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Showing with 33 additions and 13 deletions.
  1. +32 −12 lib/shout-bot.rb
  2. +1 −1  shout-bot.gemspec
44 lib/shout-bot.rb
View
@@ -29,13 +29,14 @@
require "addressable/uri"
require "socket"
+require "openssl"
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,31 +48,50 @@ def self.shout(uri, password = nil, &block)
attr_accessor :channel
- def initialize(server, port, nick, password=nil)
+ def sendln(cmd)
+ @socket.write("#{cmd}\r\n")
+ STDOUT.flush
+ end
+
+ def initialize(server, port, nick, password, ssl)
raise ArgumentError unless block_given?
- @socket = TCPSocket.open(server, port || 6667)
- @socket.puts "PASSWORD #{password}" if password
- @socket.puts "NICK #{nick}"
- @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
+ tcp_socket = TCPSocket.new(server, port || 6667)
+ if ssl
+ ssl_context = OpenSSL::SSL::SSLContext.new
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
+ @socket.sync = true
+ @socket.connect
+ else
+ @socket = tcp_socket
+ end
+ 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
- def join(channel, password = nil)
+ def join(channel, password)
raise ArgumentError unless block_given?
@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
+
+ def notice(message)
+ return unless @channel
+ sendln "NOTICE #{@channel} :#{message}"
end
end
2  shout-bot.gemspec
View
@@ -8,6 +8,6 @@ Gem::Specification.new do |s|
s.authors = ["Simon Rozet", "Harry Vangberg"]
s.email = "simon@rozet.name"
s.files = ["lib/shout-bot.rb"]
- s.require_paths = ["."]
+ s.require_paths = ["lib"]
s.add_dependency("addressable")
end

No commit comments for this range

Something went wrong with that request. Please try again.