Created
May 12, 2010 11:33
-
-
Save matsadler/398468 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 'thread' | |
require 'rubygems' | |
require 'net/irc' | |
# undo weirdness in the net/irc library, so that we get NoMethodError when we | |
# call a method that doesn't exist | |
class Net::IRC::Client | |
remove_method :method_missing if defined? method_missing | |
end | |
# Exmple irc bot to notify you of progress of a long running task. It can be | |
# silenced by sending it the message "quit" | |
# | |
# bot = Net::IRC::Simple.new("foobar", 6667, | |
# :nick => "foobartest", | |
# :user => "foobartest", | |
# :real => "foobartest") | |
# | |
# bot.join("#bot_test") | |
# | |
# bot.on_message do |m| | |
# privmsg = Net::IRC::Simple::PRIVMSG | |
# if m.command == privmsg && m[0] == bot.opts.user && m[1] == "quit" | |
# bot.quit("bye") | |
# end | |
# end | |
# | |
# (1..100).each do |i| | |
# sleep 1 | |
# bot.message("#bot_test", "#{i}% done") unless bot.finished? | |
# end | |
# | |
# bot.quit("done") | |
# | |
class Net::IRC::Simple < Net::IRC::Client | |
def initialize(*args) | |
super | |
@main = Thread.current | |
@thread = Thread.new {start} | |
@thread.abort_on_exception = true | |
Thread.stop | |
end | |
def join(room) | |
post JOIN, room | |
end | |
def message(room, string) | |
post PRIVMSG, room, string | |
end | |
def quit(string="") | |
post QUIT, string | |
finish | |
end | |
def started? | |
@started | |
end | |
def finished? | |
@socket.closed? | |
end | |
def on_message(message=nil, &block) | |
if message | |
super(message) | |
@callback.call(message) if @callback | |
else | |
@callback = block | |
end | |
end | |
def wait | |
@thread.join | |
end | |
protected | |
def on_rpl_welcome(m) | |
@main.run | |
@started = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment