Class: Vines::Stanza

Inherits:
Object
  • Object
show all
Includes:
Nokogiri::XML
Defined in:
lib/vines/stanza.rb,
lib/vines/stanza/iq.rb,
lib/vines/stanza/iq/auth.rb,
lib/vines/stanza/iq/ping.rb,
lib/vines/stanza/message.rb,
lib/vines/stanza/iq/error.rb,
lib/vines/stanza/iq/query.rb,
lib/vines/stanza/iq/vcard.rb,
lib/vines/stanza/presence.rb,
lib/vines/stanza/iq/result.rb,
lib/vines/stanza/iq/roster.rb,
lib/vines/stanza/iq/session.rb,
lib/vines/stanza/iq/disco_info.rb,
lib/vines/stanza/iq/disco_items.rb,
lib/vines/stanza/presence/error.rb,
lib/vines/stanza/presence/probe.rb,
lib/vines/stanza/iq/private_storage.rb,
lib/vines/stanza/presence/subscribe.rb,
lib/vines/stanza/presence/subscribed.rb,
lib/vines/stanza/presence/unavailable.rb,
lib/vines/stanza/presence/unsubscribe.rb,
lib/vines/stanza/presence/unsubscribed.rb

Direct Known Subclasses

Iq, Message, Presence

Defined Under Namespace

Classes: Iq, Message, Presence

Constant Summary collapse

MESSAGE =
'message'.freeze
@@types =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, stream) ⇒ Stanza

Returns a new instance of Stanza.



24
25
26
# File 'lib/vines/stanza.rb', line 24

def initialize(node, stream)
  @node, @stream = node, stream
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



84
85
86
# File 'lib/vines/stanza.rb', line 84

def method_missing(method, *args, &block)
  @node.send(method, *args, &block)
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



7
8
9
# File 'lib/vines/stanza.rb', line 7

def stream
  @stream
end

Class Method Details

.from_node(node, stream) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/vines/stanza.rb', line 16

def self.from_node(node, stream)
  # optimize common case
  return Message.new(node, stream) if node.name == MESSAGE
  found = @@types.select {|pair, v| node.xpath(*pair).any? }
    .sort {|a, b| b[0][0].length - a[0][0].length }.first
  found ? found[1].new(node, stream) : nil
end

.register(xpath, ns = {}) ⇒ Object



12
13
14
# File 'lib/vines/stanza.rb', line 12

def self.register(xpath, ns={})
  @@types[[xpath, ns]] = self
end

Instance Method Details

#broadcast(recipients) ⇒ Object

Send the stanza to all recipients, stamping it with from and to addresses first.



30
31
32
33
34
35
36
# File 'lib/vines/stanza.rb', line 30

def broadcast(recipients)
  @node['from'] = stream.user.jid.to_s
  recipients.each do |recipient|
    @node['to'] = recipient.user.jid.to_s
    recipient.write(@node)
  end
end

#local?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/vines/stanza.rb', line 38

def local?
  stream.router.local?(@node)
end

#processObject



54
55
56
# File 'lib/vines/stanza.rb', line 54

def process
  raise 'subclass must implement'
end

#routeObject



42
43
44
# File 'lib/vines/stanza.rb', line 42

def route
  stream.router.route(@node)
end

#routerObject



46
47
48
# File 'lib/vines/stanza.rb', line 46

def router
  stream.router
end

#send_unavailable(from, to) ⇒ Object

Broadcast unavailable presence from the user’s available resources to the recipient’s available resources. Route the stanza to a remote server if the recipient isn’t hosted locally.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vines/stanza.rb', line 61

def send_unavailable(from, to)
  router.available_resources(from).each do |stream|
    el = unavailable(stream.user.jid, to)
    if router.local_jid?(to)
      router.available_resources(to).each do |recipient|
        recipient.write(el)
      end
    else
      router.route(el)
    end
  end
end

#storage(domain = stream.domain) ⇒ Object



50
51
52
# File 'lib/vines/stanza.rb', line 50

def storage(domain=stream.domain)
  stream.storage(domain)
end

#unavailable(from, to) ⇒ Object

Return an unavailable presence stanza addressed to the given JID.



75
76
77
78
79
80
81
82
# File 'lib/vines/stanza.rb', line 75

def unavailable(from, to)
  doc = Document.new
  doc.create_element('presence',
    'from' => from.to_s,
    'id'   => Kit.uuid,
    'to'   => to.to_s,
    'type' => 'unavailable')
end