Class: IRCEvent
- Inherits:
-
Object
- Object
- IRCEvent
- Defined in:
- lib/IRCEvent.rb
Overview
Handles an IRC generated event. Handlers are for the IRC framework to use Callbacks are for users to add. Both handlers and callbacks can be called for the same event.
Constant Summary collapse
- @@handlers =
{ 'ping' => lambda {|event| IRCConnection.send_to_server("PONG #{event.}") } }
- @@callbacks =
Hash.new()
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#event_type ⇒ Object
readonly
Returns the value of attribute event_type.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#hostmask ⇒ Object
readonly
Returns the value of attribute hostmask.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
-
.add_callback(message_id, &callback) ⇒ Object
Adds a callback for the specified irc message.
-
.add_handler(message_id, proc = nil, &handler) ⇒ Object
Adds a handler to the handler function hash.
Instance Method Summary collapse
-
#initialize(line) ⇒ IRCEvent
constructor
A new instance of IRCEvent.
-
#process ⇒ Object
Process this event, preforming which ever handler and callback is specified for this event.
Constructor Details
#initialize(line) ⇒ IRCEvent
Returns a new instance of IRCEvent.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/IRCEvent.rb', line 22 def initialize (line) line.sub!(/^:/, '') mess_parts = line.split(':', 2); # mess_parts[0] is server info # mess_parts[1] is the message that was sent @message = mess_parts[1] @stats = mess_parts[0].scan(/[-`\^\{\}\[\]\w.\#\@\+=]+/) if @stats[0].match(/^PING/) @event_type = 'ping' elsif @stats[1] && @stats[1].match(/^\d+/) @event_type = EventLookup::find_by_number(@stats[1]); @channel = @stats[3] else @event_type = @stats[2].downcase if @stats[2] end if @event_type != 'ping' @from = @stats[0] @user = IRCUser.create_user(@from) end # FIXME: this list would probably be more accurate to exclude commands than to include them @hostmask = @stats[1] if %W(topic privmsg join).include? @event_type @channel = @stats[3] if @stats[3] && !@channel @target = @stats[5] if @stats[5] @mode = @stats[4] if @stats[4] if @mode.nil? && @event_type == 'mode' # Server modes (like +i) are sent in the 'message' part, and not # the 'stat' part of the message. @mode = @message end # Unfortunatly, not all messages are created equal. This is our # special exceptions section if @event_type == 'join' @channel = @message end end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def channel @channel end |
#event_type ⇒ Object (readonly)
Returns the value of attribute event_type.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def event_type @event_type end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def from @from end |
#hostmask ⇒ Object (readonly)
Returns the value of attribute hostmask.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def hostmask @hostmask end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def @message end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def mode @mode end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def stats @stats end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
21 22 23 |
# File 'lib/IRCEvent.rb', line 21 def target @target end |
Class Method Details
.add_callback(message_id, &callback) ⇒ Object
Adds a callback for the specified irc message.
62 63 64 |
# File 'lib/IRCEvent.rb', line 62 def IRCEvent.add_callback(, &callback) @@callbacks[] = callback end |
.add_handler(message_id, proc = nil, &handler) ⇒ Object
Adds a handler to the handler function hash.
67 68 69 70 71 72 73 |
# File 'lib/IRCEvent.rb', line 67 def IRCEvent.add_handler(, proc=nil, &handler) if block_given? @@handlers[] = handler elsif proc @@handlers[] = proc end end |
Instance Method Details
#process ⇒ Object
Process this event, preforming which ever handler and callback is specified for this event.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/IRCEvent.rb', line 77 def process handled = nil if @@handlers[@event_type] @@handlers[@event_type].call(self) handled = 1 end if @@callbacks[@event_type] @@callbacks[@event_type].call(self) handled = 1 end if !handled end end |