Class: RFlow::Components::IRC::Client

Inherits:
RFlow::Component
  • Object
show all
Defined in:
lib/rflow/components/irc/client.rb

Overview

Assumes a single connection, meaning that it can choose to be ‘promiscuous’ and not limit its sending to only those messages derived from one of its own

Defined Under Namespace

Classes: Connection

Constant Summary collapse

DEFAULT_CONFIG =
{
  'server' => '127.0.0.1',
  'port' => 6667,
  'server_password' => nil,
  'nickname' => 'rflow',
  'username' => 'rflow',
  'oper_user' => nil,
  'oper_password' => nil,
  'nickserv_password' => nil,
  'reconnect_interval' => 2,
  'promiscuous' => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_connectionObject

Returns the value of attribute client_connection.



15
16
17
# File 'lib/rflow/components/irc/client.rb', line 15

def client_connection
  @client_connection
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/rflow/components/irc/client.rb', line 14

def config
  @config
end

#portObject

Returns the value of attribute port.



16
17
18
# File 'lib/rflow/components/irc/client.rb', line 16

def port
  @port
end

#serverObject

Returns the value of attribute server.



16
17
18
# File 'lib/rflow/components/irc/client.rb', line 16

def server
  @server
end

Instance Method Details

#configure!(config) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rflow/components/irc/client.rb', line 31

def configure!(config)
  @config = DEFAULT_CONFIG.merge config

  @config['port'] = @config['port'].to_i
  @config['reconnect_interval'] = @config['reconnect_interval'].to_i
  # TODO: More config sanitization

  @server = @config['server']
  @port = @config['port'].to_i
end

#process_message(input_port, input_port_key, connection, message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rflow/components/irc/client.rb', line 48

def process_message(input_port, input_port_key, connection, message)
  RFlow.logger.debug "Received a message"
  return unless message.data_type_name == 'RFlow::Message::Data::IRC::Message'

  if config['promiscuous']
    RFlow.logger.debug "Received an IRC::Message message, sending to client connection"
    client_connection.send_irc_message message
  else
    RFlow.logger.debug "Received an IRC::Message message, determining if its mine"
    my_events = message.provenance.find_all {|event| event.component_instance_uuid == instance_uuid}
    RFlow.logger.debug "Found #{my_events.size} processing events from me"
    # Attempt to send the data to each context match.
    # TODO: check for correctness
    my_events.each do |event|
      RFlow.logger.debug "Inspecting context #{client_connection.signature.to_s} == #{event.context.to_s}"
      if client_connection.signature.to_s == event.context.to_s
        RFlow.logger.debug "Found connection for #{event.context}, sending message to associated client connection"
        client_connection.send_irc_message message
      end
    end
  end
end

#run!Object



42
43
44
45
46
# File 'lib/rflow/components/irc/client.rb', line 42

def run!
  @client_connection = EM.connect(@server, @port, Connection) do |conn|
    conn.client = self
  end
end