class Vines::Stanza::Presence

Constants

VALID_TYPES

Public Instance Methods

inbound?() click to toggle source
# File lib/vines/stanza/presence.rb, line 29
def inbound?
  stream.class == Vines::Stream::Server ||
  stream.class == Vines::Stream::Component
end
inbound_broadcast_presence() click to toggle source
# File lib/vines/stanza/presence.rb, line 71
def inbound_broadcast_presence
  broadcast(stream.available_resources(validate_to))
end
outbound?() click to toggle source
# File lib/vines/stanza/presence.rb, line 25
def outbound?
  !inbound?
end
outbound_broadcast_presence() click to toggle source
# File lib/vines/stanza/presence.rb, line 34
def outbound_broadcast_presence
  self['from'] = stream.user.jid.to_s
  to = validate_to
  type = (self['type'] || '').strip
  initial = to.nil? && type.empty? && !stream.available?

  recipients = if to.nil?
    stream.available_subscribers
  else
    stream.user.subscribed_from?(to) ? stream.available_resources(to) : []
  end

  broadcast(recipients)
  broadcast(stream.available_resources(stream.user.jid))

  if initial
    stream.available_subscribed_to_resources.each do |recipient|
      if recipient.last_broadcast_presence
        el = recipient.last_broadcast_presence.clone
        el['to'] = stream.user.jid.to_s
        el['from'] = recipient.user.jid.to_s
        stream.write(el)
      end
    end
    stream.remote_subscribed_to_followers.each do |follower|
      send_probe(follower.jid.bare)
    end
    stream.available!
  end

  stream.remote_subscribers(to).each do |follower|
    node = @node.clone
    node['to'] = follower.jid.bare.to_s
    router.route(node) rescue nil # ignore RemoteServerNotFound
  end
end
process() click to toggle source
# File lib/vines/stanza/presence.rb, line 16
def process
  stream.last_broadcast_presence = @node.clone unless validate_to
  unless self['type'].nil?
    raise StanzaErrors::BadRequest.new(self, 'modify')
  end
  dir = outbound? ? 'outbound' : 'inbound'
  method("#{dir}_broadcast_presence").call
end

Private Instance Methods

auto_reply_to_subscription_request(from, type) click to toggle source
# File lib/vines/stanza/presence.rb, line 88
def auto_reply_to_subscription_request(from, type)
  doc = Document.new
  node = doc.create_element('presence') do |el|
    el['from'] = from.to_s
    el['id'] = self['id'] if self['id']
    el['to'] = stream.user.jid.bare.to_s
    el['type'] = type
  end
  stream.write(node)
end
broadcast_subscription_change(follower) click to toggle source

Notify the current user's interested streams of a follower's subscription state change as a result of receiving a subscribed, unsubscribe, or unsubscribed presence stanza.

# File lib/vines/stanza/presence.rb, line 112
def broadcast_subscription_change(follower)
  stamp_from
  stream.interested_resources(stamp_to).each do |recipient|
    @node['to'] = recipient.user.jid.to_s
    recipient.write(@node)
    follower.send_roster_push(recipient)
  end
end
send_probe(to) click to toggle source
# File lib/vines/stanza/presence.rb, line 77
def send_probe(to)
  to = JID.new(to)
  doc = Document.new
  probe = doc.create_element('presence',
    'from' => stream.user.jid.bare.to_s,
    'id'   => Kit.uuid,
    'to'   => to.bare.to_s,
    'type' => 'probe')
  router.route(probe) rescue nil # ignore RemoteServerNotFound
end
send_roster_push(to) click to toggle source

Send the follower's roster item to the current user's interested streams. Roster pushes are required, following presence subscription updates, to notify the user's clients of the follower's current state.

# File lib/vines/stanza/presence.rb, line 102
def send_roster_push(to)
  follower = stream.user.follower(to)
  stream.interested_resources(stream.user.jid).each do |recipient|
    follower.send_roster_push(recipient)
  end
end
stamp_from() click to toggle source

Presence subscription stanzas must be addressed from the user's bare JID. Return the user's bare JID object that was stamped.

# File lib/vines/stanza/presence.rb, line 134
def stamp_from
  stream.user.jid.bare.tap do |bare|
    self['from'] = bare.to_s
  end
end
stamp_to() click to toggle source

Validate that the incoming stanza has a 'to' attribute and strip any resource part from it so it's a bare jid. Return the bare JID object that was stamped.

# File lib/vines/stanza/presence.rb, line 124
def stamp_to
  to = validate_to
  raise StanzaErrors::BadRequest.new(self, 'modify') unless to
  to.bare.tap do |bare|
    self['to'] = bare.to_s
  end
end