Class: IRC::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/em-ruby-irc/IRC-Event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, connection) ⇒ Event

Returns a new instance of Event.



18
19
20
21
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
# File 'lib/em-ruby-irc/IRC-Event.rb', line 18

def initialize(data, connection)
  @connection = connection
  @logged_in = false
  data.chomp!
  data.sub!(/^:/, '')
  mess_parts = data.split(':', 2);
  unless mess_parts.nil? or mess_parts.size < 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.\#\@\+]+/)
    unless @stats[0].nil?
      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].downcase unless @stats[3].nil?
        @channel = @stats[3] if @stats[3].nil?
      else
        @event_type = @stats[2].downcase if @stats[2]
      end
  
      if @event_type != 'ping'
        @from    = @stats[0].downcase
      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].downcase if @stats[3] && !@channel
      @target  = @stats[5].downcase if @stats[5]
      @mode    = @stats[4] if @stats[4]
      run_handlers(@event_type) unless @event_type.nil?
    end
  end
  #logger.debug(data)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def channel
  @channel
end

#connectionObject

Returns the value of attribute connection.



17
18
19
# File 'lib/em-ruby-irc/IRC-Event.rb', line 17

def connection
  @connection
end

#event_typeObject (readonly)

Returns the value of attribute event_type.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def event_type
  @event_type
end

#fromObject (readonly)

Returns the value of attribute from.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def from
  @from
end

#hostmaskObject (readonly)

Returns the value of attribute hostmask.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def hostmask
  @hostmask
end

#logged_inObject

Returns the value of attribute logged_in.



17
18
19
# File 'lib/em-ruby-irc/IRC-Event.rb', line 17

def logged_in
  @logged_in
end

#messageObject

Returns the value of attribute message.



17
18
19
# File 'lib/em-ruby-irc/IRC-Event.rb', line 17

def message
  @message
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def mode
  @mode
end

#statsObject (readonly)

Returns the value of attribute stats.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def stats
  @stats
end

#targetObject (readonly)

Returns the value of attribute target.



16
17
18
# File 'lib/em-ruby-irc/IRC-Event.rb', line 16

def target
  @target
end

Instance Method Details

#run_handlers(event_type) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/em-ruby-irc/IRC-Event.rb', line 55

def run_handlers(event_type)
  begin
    return if connection.irc_handlers.size == 0 or connection.irc_handlers[event_type].nil?
    connection.irc_handlers[event_type].each do |handler|
      handler.call(self) unless handler.nil?
    end
  rescue => err
    log_error(err)
  end
end