Commit e6d1beede74fc5da3c7b20d03ab857f888454e45

Added a class called IRCBot to handle dynamically irc events
IRCBot.rb
(10 / 0)
  
1
2require 'IRC'
3
4class IRCBot < IRC
5 def initialize(nick, server, port, realname='RBot')
6 super
7
8 IRCEvent.add_handler("unhandled", lambda { |event| __send__("on_#{event.type}".to_sym, event) } )
9 end
10end
IRCEvent.rb
(20 / 14)
  
1818class IRCEvent
1919 @@handlers = { 'ping' => lambda {|event| IRCConnection.send_to_server("PONG #{event.message}") } }
2020 @@callbacks = Hash.new()
21 attr_reader :hostmask, :message, :event_type, :from, :channel, :target, :mode, :stats, :nick, :ident
21 attr_reader :hostmask, :message, :type, :from, :channel, :target, :mode, :stats, :nick, :ident
2222 def initialize (line)
2323 puts "FROM SERVER: #{line}" if $DEBUG
2424
3535 puts @stats.join(" | ") if $DEBUG
3636
3737 if @stats[0].match(/^PING/)
38 @event_type = 'ping'
38 @type = 'ping'
3939 elsif @message.match(/^(\x1(\w+))/)
4040 @from = @stats[0]
41 @event_type = $2.downcase
41 @type = $2.downcase
4242
4343 @message.gsub!($1, "")
4444 elsif @stats[1] && @stats[1].match(/^\d+/)
45 @event_type = EventLookup::find_by_number(@stats[1]);
45 @type = EventLookup::find_by_number(@stats[1]);
4646 @channel = @stats[2]
4747 else
48 @event_type = @stats[1].downcase if @stats[1]
48 @type = @stats[1].downcase if @stats[1]
4949 end
5050
51 if @event_type != 'ping'
51 if @type != 'ping'
5252 @from = @stats[0]
5353 @user = IRCUser.create_user(@from)
5454 end
6767
6868 # Unfortunatly, not all messages are created equal. This is our
6969 # special exceptions section
70 if @event_type == 'join'
70 if @type == 'join'
7171 @channel = @message
7272 end
7373
74 puts "EVENT: #{@event_type}" if $DEBUG
74 puts "EVENT: #{@type}" if $DEBUG
7575
7676 end
7777
9393 # for this event.
9494 def process
9595 handled = false
96 if @@handlers[@event_type]
97 @@handlers[@event_type].call(self)
96 if @@handlers[@type]
97 @@handlers[@type].call(self)
9898 handled = true
9999 end
100 if @@callbacks[@event_type]
101 @@callbacks[@event_type].call(self)
100
101 if @@callbacks[@type]
102 @@callbacks[@type].call(self)
102103 handled = true
103104 end
104 if !handled
105 puts "No handler for event type #@event_type in #{self.class}" if $DEBUG
105
106 if not handled
107 if @@handlers["unhandled"]
108 @@handlers["unhandled"].call(self)
109 else
110 $stderr.puts "No handler for event type #@type in #{self.class}" if $DEBUG
111 end
106112 end
107113 end
108114end