Created
November 17, 2008 17:20
-
-
Save vangberg/25830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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