Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created November 17, 2008 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vangberg/25830 to your computer and use it in GitHub Desktop.
Save vangberg/25830 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'addressable/uri'
require 'socket'
class IRC
def self.shoot(uri, options={}, &block)
raise ArgumentError unless block_given?
uri = Addressable::URI.parse(uri)
irc = new(uri.host, uri.port, options.delete(:as)) do |irc|
irc.join(uri.path[1..-1], &block)
end
end
def initialize(server, port, nick)
raise ArgumentError unless block_given?
@socket = TCPSocket.open(server, port)
@socket.puts "NICK #{nick}"
@socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
yield self
@socket.puts "QUIT"
@socket.gets until @socket.eof?
end
def join(channel)
raise ArgumentError unless block_given?
@channel = "##{channel}"
@socket.puts "JOIN #{@channel}"
yield self
@socket.puts "PART #{@channel}"
end
def say(message)
return unless @channel
@socket.puts "PRIVMSG #{@channel} :#{message}"
end
end
if $0 == __FILE__
IRC.shoot('irc://irc.freenode.net:6667/integrity', :as => "Integrity#{rand(20)}") do |channel|
channel.say "harryjr, check me out! http://gist.github.com/25886"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment