Class: Jabber::Discovery::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/discovery/helper/responder.rb

Overview

Responds to Service Discovery queries on a given node

Modify returned elements by these attributes:

  • Responder#identities

  • Responder#features (Responder#add_features is a short-cut accepting an Array of Strings, too)

  • Responder#forms

  • Responder#items

Constant Summary collapse

CALLBACK_PRIORITY =
180

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, node = nil, identities = [], features = [], items = []) ⇒ Responder

Initialize responder for a specific node

stream
Jabber::Stream
node
nil

or [String]



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 64

def initialize(stream, node=nil, identities=[], features=[], items=[])
  @stream = stream
  @my_jid = nil
  @node = node
  @identities = identities
  @features = []
  add_features(features)
  @forms = []
  @items = items

  @stream.add_iq_callback(CALLBACK_PRIORITY, self) do |iq|
    my_nodes = [@node, "#{@node}##{generate_ver}"]

    if iq.type == :get and
       iq.query.kind_of? IqQueryDiscoInfo and
       my_nodes.include?(iq.query.node)

      answer = iq.answer(false)
      answer.type = :result
      query = answer.add(IqQueryDiscoInfo.new)
      query.node = iq.query.node
      (@identities + @features + @forms).each do |element|
        query.add(element)
      end
      @stream.send(answer)

      true  # handled

    elsif iq.type == :get and
          iq.query.kind_of? IqQueryDiscoItems and
          my_nodes.include?(iq.query.node)

      answer = iq.answer(false)
      answer.type = :result
      query = answer.add(IqQueryDiscoItems.new)
      query.node = iq.query.node
      @items.each do |item|
        if item.kind_of? Responder
          query.add(item.generate_item)
        else
          query.add(item)
        end
      end
      @stream.send(answer)

      true  # handled

    else
      false # not handled
    end
  end
end

Instance Attribute Details

#featuresObject

Features returned on Discovery Info query,

Array of [Discovery::Feature]



35
36
37
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 35

def features
  @features
end

#formsObject

Forms returned on Discovery Info query (such as Software Information)

Array of [Dataforms::XData]



41
42
43
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 41

def forms
  @forms
end

#identitiesObject

Identities returned on Discovery Info query

Array of [Discovery::Identity]



30
31
32
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 30

def identities
  @identities
end

#itemsObject

Children returned on Discovery Item query

May contain other Discovery::Responder instances which will generate an item dynamically from their first identity

Array of [Discovery::Item] or [Discovery::Responder] (mixed)



51
52
53
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 51

def items
  @items
end

#my_jidObject

Set the JID this helper feels responsible for (default: nil, responsible for any JID)



56
57
58
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 56

def my_jid
  @my_jid
end

#nodeObject (readonly)

Service Discovery node this Responder is responsible for (will not answer queries for other nodes)



24
25
26
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 24

def node
  @node
end

Instance Method Details

#add_feature(feature) ⇒ Object

Add a feature

feature
Jabber::Discovery::Feature

or [String]



120
121
122
123
124
125
126
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 120

def add_feature(feature)
  if feature.kind_of? Feature
    @features << feature
  else
    @features << Feature.new(feature.to_s)
  end
end

#add_features(features) ⇒ Object

Add a series of features

features

Array of [Jabber::Discovery::Feature] or [String]



131
132
133
134
135
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 131

def add_features(features)
  features.each { |feature|
    add_feature(feature)
  }
end

#generate_capsObject

Generate a XEP-0115: Entity Capabilities <c/> element for inclusion in Presence stanzas. This enables efficient caching of Service Discovery information.



141
142
143
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 141

def generate_caps
  Caps::C.new(@node, generate_ver)
end

#generate_itemObject

Generate an item for inclusion in items discovery in other responders

return
Discovery::Item

or nil



149
150
151
152
153
154
155
156
# File 'lib/xmpp4r/discovery/helper/responder.rb', line 149

def generate_item
  i = @identities.first
  if i
    Item.new(@my_jid || @stream.jid, i.iname, @node)
  else
    nil
  end
end