class Vines::Stream::Client::Bind

Constants

MAX_ATTEMPTS
NS

Public Class Methods

new(stream, success=Ready) click to toggle source
Calls superclass method
# File lib/vines/stream/client/bind.rb, line 10
def initialize(stream, success=Ready)
  super
  @attempts = 0
end

Public Instance Methods

node(node) click to toggle source
# File lib/vines/stream/client/bind.rb, line 15
def node(node)
  @attempts += 1
  raise StreamErrors::NotAuthorized unless bind?(node)
  raise StreamErrors::PolicyViolation.new('max bind attempts reached') if @attempts > MAX_ATTEMPTS
  raise StanzaErrors::ResourceConstraint.new(node, 'wait') if resource_limit_reached?

  stream.bind!(resource(node))
  doc = Document.new
  result = doc.create_element('iq', 'id' => node['id'], 'type' => 'result') do |el|
    el << doc.create_element('bind') do |bind|
      bind.default_namespace = NS
      bind << doc.create_element('jid', stream.user.jid.to_s)
    end
  end
  stream.write(result)
  send_empty_features
  advance
end

Private Instance Methods

bind?(node) click to toggle source
# File lib/vines/stream/client/bind.rb, line 43
def bind?(node)
  node.name == 'iq' && node['type'] == 'set' && node.xpath('ns:bind', 'ns' => NS).any?
end
resource(node) click to toggle source
# File lib/vines/stream/client/bind.rb, line 47
def resource(node)
  el = node.xpath('ns:bind/ns:resource', 'ns' => NS).first
  resource = el ? el.text.strip : ''
  generate = resource.empty? || !resource_valid?(resource) || resource_used?(resource)
  generate ? Kit.uuid : resource
end
resource_limit_reached?() click to toggle source
# File lib/vines/stream/client/bind.rb, line 54
def resource_limit_reached?
  used = stream.connected_resources(stream.user.jid.bare).size
  used >= stream.max_resources_per_account
end
resource_used?(resource) click to toggle source
# File lib/vines/stream/client/bind.rb, line 59
def resource_used?(resource)
  stream.available_resources(stream.user.jid).any? do |c|
    c.user.jid.resource == resource
  end
end
resource_valid?(resource) click to toggle source
# File lib/vines/stream/client/bind.rb, line 65
def resource_valid?(resource)
  jid = stream.user.jid
  JID.new(jid.node, jid.domain, resource) rescue false
end
send_empty_features() click to toggle source

Write the final <stream:features/> element to the stream, indicating stream negotiation is complete and the client is cleared to send stanzas.

# File lib/vines/stream/client/bind.rb, line 39
def send_empty_features
  stream.write('<stream:features/>')
end