Class: Hector::Connection

Inherits:
EventMachine::Protocols::LineAndTextProtocol
  • Object
show all
Includes:
Hector::Concerns::Authentication
Defined in:
lib/hector/connection.rb

Direct Known Subclasses

SSLConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hector::Concerns::Authentication

#on_cap, #on_nick, #on_pass, #on_user

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



5
6
7
# File 'lib/hector/connection.rb', line 5

def identity
  @identity
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/hector/connection.rb', line 5

def request
  @request
end

#sessionObject (readonly)

Returns the value of attribute session.



5
6
7
# File 'lib/hector/connection.rb', line 5

def session
  @session
end

Instance Method Details

#addressObject



59
60
61
# File 'lib/hector/connection.rb', line 59

def address
  peer_info[1]
end

#error(klass, *args) ⇒ Object



51
52
53
# File 'lib/hector/connection.rb', line 51

def error(klass, *args)
  handle_error(klass.new(*args))
end

#handle_error(error) ⇒ Object



46
47
48
49
# File 'lib/hector/connection.rb', line 46

def handle_error(error)
  respond_with(error.response)
  close_connection(true) if error.fatal?
end

#log(level, *args) ⇒ Object



55
56
57
# File 'lib/hector/connection.rb', line 55

def log(level, *args)
  Hector.logger.send(level, [log_tag, *args].join(" "))
end

#portObject



63
64
65
# File 'lib/hector/connection.rb', line 63

def port
  peer_info[0]
end

#post_initObject



7
8
9
# File 'lib/hector/connection.rb', line 7

def post_init
  log(:info, "opened connection")
end

#receive_line(line) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hector/connection.rb', line 11

def receive_line(line)
  @request = Request.new(line)
  log(:debug, "received", @request.to_s.inspect) unless @request.sensitive?

  if session
    session.receive(request)
  else
    if respond_to?(request.event_name)
      send(request.event_name)
    else
      close_connection(true)
    end
  end

rescue IrcError => e
  handle_error(e)

rescue Exception => e
  log(:error, [e, *e.backtrace].join("\n"))

ensure
  @request = nil
end

#respond_with(response, *args) ⇒ Object



40
41
42
43
44
# File 'lib/hector/connection.rb', line 40

def respond_with(response, *args)
  response = Response.new(response, *args) unless response.is_a?(Response)
  send_data(response.to_s)
  log(:debug, "sent", response.to_s.inspect)
end

#unbindObject



35
36
37
38
# File 'lib/hector/connection.rb', line 35

def unbind
  session.destroy if session
  log(:info, "closing connection")
end