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

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Protocols::LineText2
Defined in:
lib/rflow/components/irc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



74
75
76
# File 'lib/rflow/components/irc/client.rb', line 74

def client
  @client
end

#client_ipObject (readonly)

Returns the value of attribute client_ip.



75
76
77
# File 'lib/rflow/components/irc/client.rb', line 75

def client_ip
  @client_ip
end

#client_portObject (readonly)

Returns the value of attribute client_port.



75
76
77
# File 'lib/rflow/components/irc/client.rb', line 75

def client_port
  @client_port
end

#server_ipObject (readonly)

Returns the value of attribute server_ip.



75
76
77
# File 'lib/rflow/components/irc/client.rb', line 75

def server_ip
  @server_ip
end

#server_portObject (readonly)

Returns the value of attribute server_port.



75
76
77
# File 'lib/rflow/components/irc/client.rb', line 75

def server_port
  @server_port
end

Instance Method Details

#command(cmd, args = [], prefix = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rflow/components/irc/client.rb', line 145

def command(cmd, args = [], prefix = nil)
  RFlow.logger.debug("command: '#{cmd}' with args ['#{args.join("', '")}'] and prefix '#{prefix}'")
  line = ''
  if prefix
    line << ":#{prefix} "
  end

  line << cmd.upcase

  last_arg = args.pop
  line << " #{args.join ' '}" unless args.empty?

  if last_arg =~ /\s/;
    line << ' :' << last_arg
  else
    line << ' ' << last_arg
  end

  send_irc_line line
end

#connection_completedObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rflow/components/irc/client.rb', line 84

def connection_completed
  @reconnecting = false
  @connected = true

  RFlow.logger.info("Connected to IRC server #{client.config['server']}:#{client.config['port']}")

  command "PASS", [client.config['server_password']] unless client.config['server_password'].nil?
  command "NICK", [client.config['nickname']]
  command "USER", [client.config['username'], client.config['username'], client.config['username'], client.config['username']]
  command "NickServ IDENTIFY", [client.config['nickname'], client.config['nickserv_password']] unless client.config['nickserv_password'].nil?
  command "OPER", [client.config['oper_user'] || client.config['nickname'], client.config['oper_password']] unless client.config['oper_password'].nil?
end

#post_initObject



77
78
79
80
81
82
# File 'lib/rflow/components/irc/client.rb', line 77

def post_init
  @client_port, @client_ip = Socket.unpack_sockaddr_in(get_peername) rescue ["?", "?.?.?.?"]
  @server_port, @server_ip = Socket.unpack_sockaddr_in(get_sockname) rescue ["?", "?.?.?.?"]
  RFlow.logger.debug "Connection from #{@client_ip}:#{@client_port} to #{@server_ip}:#{@server_port}"
  super
end

#receive_line(line) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rflow/components/irc/client.rb', line 97

def receive_line(line)
  RFlow.logger.debug("IRCClient#receive_line: #{line}")
  prefix, cmd, params = IRC.parse_irc_line(line)

  # Now have an optional prefix, required cmd, and optional param array
  case cmd
  when /PING/
    command('PONG', params)
  else
    # create an IRC message here and send it along
    RFlow.logger.debug("Sending IRC message '#{line}', signature '#{signature.class}:#{signature}', '#{signature.to_s.class}:#{signature.to_s}'")

    client.from_server.send_message(RFlow::Message.new('RFlow::Message::Data::IRC::Message').tap do |m|
      m.data.prefix = prefix
      m.data.command = cmd
      m.data.parameters = params
      m.provenance << RFlow::Message::ProcessingEvent.new(client.instance_uuid, Time.now.utc).tap do |e|
        e.context = signature.to_s
        e.completed_at = Time.now.utc
      end
    end)
  end
end

#send_irc_line(line) ⇒ Object



140
141
142
143
# File 'lib/rflow/components/irc/client.rb', line 140

def send_irc_line(line)
  RFlow.logger.debug "Sending line '#{line}'"
  send_data "#{line}\r\n"
end

#send_irc_message(irc_message) ⇒ Object



135
136
137
138
# File 'lib/rflow/components/irc/client.rb', line 135

def send_irc_message(irc_message)
  RFlow.logger.debug "Sending an IRC message to #{client_ip}:#{client_port}"
  command irc_message.data.command, irc_message.data.parameters, irc_message.data.prefix
end

#unbind(reason = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rflow/components/irc/client.rb', line 121

def unbind(reason = nil)
  if @connected || @reconnecting
    RFlow.logger.error("Disconnected from IRC server #{client.config['server']}:#{client.config['port']} due to '#{reason}', reconnecting ...")
    EM.add_timer(client.config['reconnect_interval']) do
      RFlow.logger.error "Attempting reconnect to IRC server #{client.config['server']}:#{client.config['port']}"
      reconnect(client.config['server'], client.config['port'])
    end
    @connected = false
    @reconnecting = true
  else
    raise RuntimeError, "Unable to connect to IRC server #{client.config['server']}:#{client.config['port']} due to '#{reason}'"
  end
end