Created
October 8, 2009 17:48
-
-
Save hannahwhy/205226 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
module Megahal | |
COMMAND = 'megahal' | |
OPTIONS = ['--no-prompt', '--no-wrap', '--no-banner'] | |
def start_megahal | |
@io = IO.popen("#{COMMAND} #{OPTIONS.join(' ')}", 'w+') | |
end | |
def stop_megahal | |
@io.close | |
end | |
def send_megahal(message) | |
return if message =~ /^#.*/ | |
@io.write("#{message}\n\n") | |
@io.readline | |
end | |
def save_megahal | |
@io.write("#SAVE\n\n") | |
end | |
def restart_megahal | |
@io.write("#QUIT\n\n") | |
begin | |
stop_megahal | |
start_megahal | |
rescue Errno::EPIPE | |
end | |
end | |
end |
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'rif/bot' | |
require 'activesupport' | |
require 'facets' | |
require 'optparse' | |
require 'ostruct' | |
require File.dirname(__FILE__) + '/megahal' | |
class Stirspeare < RIF::Bot | |
MAX_MSG = 320 | |
include Megahal | |
attr_accessor :logger, :channels | |
def initialize(nick, server, port, name, channels) | |
super(nick, server, port, name) | |
self.channels = channels | |
self.logger = Logger.new('stirspeare.log') | |
start_megahal | |
end | |
def on_endofmotd(event) | |
join(*channels) | |
end | |
def on_message(event) | |
if event.message =~ /^#{nick}:\s*!restart/ | |
logger.info("#{event.nick} restarted Stirspeare.") | |
save_megahal | |
restart_megahal | |
elsif event.message =~ /^#{nick}:(.*)/ | |
query = $1.strip | |
logger.info("Query from #{event.nick} in #{event.channel}: #{query}") | |
response = send_megahal(query) | |
chunk(response.chomp) do |chunk| | |
send_message(event.channel, "#{event.nick}: #{chunk}") | |
end | |
else | |
if event.message =~ /^[\w\s,.\?\!]+$/ | |
# learn it | |
logger.info("Learned: #{event.message}") | |
send_megahal(event.message) | |
end | |
end | |
end | |
def chunk(response) | |
chunklen = [response.length, MAX_MSG].min | |
response.divide(/.{#{chunklen}}/).each { |chunk| yield chunk } | |
end | |
end | |
config = OpenStruct.new | |
parser = OptionParser.new do |p| | |
p.on('-n NAME', '--nick NAME', 'Nickname for bot (defaults to Stirspeare)') do |nick| | |
config.nick = nick | |
end | |
p.on('-s SERVER', '--server SERVER', 'IRC server to connect to') do |server| | |
config.server = server | |
end | |
p.on('-p PORT', '--port PORT', 'Port to connect to (defaults to 6667)') do |port| | |
config.port = port | |
end | |
p.on('-c CHANNELS', '--channels CHANNELS', Array, 'Comma-separated list of channels') do |channels| | |
config.channels = channels | |
end | |
p.on_tail('-h', '--help', "You're looking at it") do | |
puts parser | |
exit 1 | |
end | |
end | |
parser.parse!(ARGV) | |
config.nick ||= 'Stirspeare' | |
config.port ||= '6667' | |
unless [:server, :nick, :port, :channels].all? { |x| config.send(x) } | |
puts parser | |
exit 1 | |
else | |
ss = Stirspeare.new(config.nick, config.server, config.port, 'stirspeare', config.channels) | |
ss.connect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment