Commit e6d1beede74fc5da3c7b20d03ab857f888454e45
- Diff rendering mode:
- inline
- side by side
IRCBot.rb
(10 / 0)
|   | |||
| 1 | |||
| 2 | require 'IRC' | ||
| 3 | |||
| 4 | class 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 | ||
| 10 | end |
IRCEvent.rb
(20 / 14)
|   | |||
| 18 | 18 | class IRCEvent | |
| 19 | 19 | @@handlers = { 'ping' => lambda {|event| IRCConnection.send_to_server("PONG #{event.message}") } } | |
| 20 | 20 | @@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 | ||
| 22 | 22 | def initialize (line) | |
| 23 | 23 | puts "FROM SERVER: #{line}" if $DEBUG | |
| 24 | 24 | ||
| … | … | ||
| 35 | 35 | puts @stats.join(" | ") if $DEBUG | |
| 36 | 36 | ||
| 37 | 37 | if @stats[0].match(/^PING/) | |
| 38 | @event_type = 'ping' | ||
| 38 | @type = 'ping' | ||
| 39 | 39 | elsif @message.match(/^(\x1(\w+))/) | |
| 40 | 40 | @from = @stats[0] | |
| 41 | @event_type = $2.downcase | ||
| 41 | @type = $2.downcase | ||
| 42 | 42 | ||
| 43 | 43 | @message.gsub!($1, "") | |
| 44 | 44 | elsif @stats[1] && @stats[1].match(/^\d+/) | |
| 45 | @event_type = EventLookup::find_by_number(@stats[1]); | ||
| 45 | @type = EventLookup::find_by_number(@stats[1]); | ||
| 46 | 46 | @channel = @stats[2] | |
| 47 | 47 | else | |
| 48 | @event_type = @stats[1].downcase if @stats[1] | ||
| 48 | @type = @stats[1].downcase if @stats[1] | ||
| 49 | 49 | end | |
| 50 | 50 | ||
| 51 | if @event_type != 'ping' | ||
| 51 | if @type != 'ping' | ||
| 52 | 52 | @from = @stats[0] | |
| 53 | 53 | @user = IRCUser.create_user(@from) | |
| 54 | 54 | end | |
| … | … | ||
| 67 | 67 | ||
| 68 | 68 | # Unfortunatly, not all messages are created equal. This is our | |
| 69 | 69 | # special exceptions section | |
| 70 | if @event_type == 'join' | ||
| 70 | if @type == 'join' | ||
| 71 | 71 | @channel = @message | |
| 72 | 72 | end | |
| 73 | 73 | ||
| 74 | puts "EVENT: #{@event_type}" if $DEBUG | ||
| 74 | puts "EVENT: #{@type}" if $DEBUG | ||
| 75 | 75 | ||
| 76 | 76 | end | |
| 77 | 77 | ||
| … | … | ||
| 93 | 93 | # for this event. | |
| 94 | 94 | def process | |
| 95 | 95 | handled = false | |
| 96 | if @@handlers[@event_type] | ||
| 97 | @@handlers[@event_type].call(self) | ||
| 96 | if @@handlers[@type] | ||
| 97 | @@handlers[@type].call(self) | ||
| 98 | 98 | handled = true | |
| 99 | 99 | end | |
| 100 | if @@callbacks[@event_type] | ||
| 101 | @@callbacks[@event_type].call(self) | ||
| 100 | |||
| 101 | if @@callbacks[@type] | ||
| 102 | @@callbacks[@type].call(self) | ||
| 102 | 103 | handled = true | |
| 103 | 104 | 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 | ||
| 106 | 112 | end | |
| 107 | 113 | end | |
| 108 | 114 | end |

