Class: IRCEvent

Inherits:
Object
  • Object
show all
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.message}") } }
@@callbacks =
Hash.new()

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def channel
  @channel
end

#event_typeObject (readonly)

Returns the value of attribute event_type.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def event_type
  @event_type
end

#fromObject (readonly)

Returns the value of attribute from.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def from
  @from
end

#hostmaskObject (readonly)

Returns the value of attribute hostmask.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def hostmask
  @hostmask
end

#messageObject (readonly)

Returns the value of attribute message.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def message
  @message
end

#modeObject (readonly)

Returns the value of attribute mode.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def mode
  @mode
end

#statsObject (readonly)

Returns the value of attribute stats.



21
22
23
# File 'lib/IRCEvent.rb', line 21

def stats
  @stats
end

#targetObject (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(message_id, &callback)
  @@callbacks[message_id] = 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(message_id, proc=nil, &handler)
  if block_given?
    @@handlers[message_id] = handler
  elsif proc
    @@handlers[message_id] = proc
  end
end

Instance Method Details

#processObject

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