Class: Jabber::MUC::MUCBrowser

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/muc/helper/mucbrowser.rb

Overview

The MUCBrowser helper can be used to discover Multi-User-Chat components via Service Discovery

See JEP 0045 sections 6.1. and 6.2.

Usage of its functions should be threaded as responses can take a while

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ MUCBrowser

Initialize a new MUCBrowser helper


20
21
22
# File 'lib/xmpp4r/muc/helper/mucbrowser.rb', line 20

def initialize(stream)
  @stream = stream
end

Instance Method Details

#muc_name(jid) ⇒ Object

Retrieve the name of a MUC component, depending upon whether the target entity supports the MUC protocol.

A return-value of nil does not mean that the entity does not exist or does not support Service Discovery! nil just means that this is not a MUC-compliant service.

Throws an ServerError when receiving <iq type='error'/>

jid
JID

Target entity (set only domain!)

return
String

or [nil]


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xmpp4r/muc/helper/mucbrowser.rb', line 37

def muc_name(jid)
  iq = Iq.new(:get, jid)
  iq.from = @stream.jid  # Enable components to use this
  iq.add(Discovery::IqQueryDiscoInfo.new)

  res = nil

  @stream.send_with_id(iq) do |answer|
    if answer.type == :result
      answer.query.each_element('feature') { |feature|
        # Look if the component has a MUC or Groupchat feature
        if feature.var == 'http://jabber.org/protocol/muc' or feature.var == 'gc-1.0'
          # If so, get the identity
          if answer.query.first_element('identity')
            res = answer.query.first_element('identity').iname
          end
        end
      }
      true
    else
      false
    end
  end

  res
end

#muc_rooms(jid) ⇒ Object

Retrieve the existing rooms of a MUC component

The resulting Hash contains pairs of room JID and room name

Usage:

my_mucbrowse_helper.muc_rooms('conference.jabber.org').each { |jid,name| ... }

Throws an exception when receiving <iq type='error'/>

jid
JID

Target entity (set only domain!)

return
Hash

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/xmpp4r/muc/helper/mucbrowser.rb', line 75

def muc_rooms(jid)
  iq = Iq.new(:get, jid)
  iq.from = @stream.jid  # Enable components to use this
  iq.add(Discovery::IqQueryDiscoItems.new)

  rooms = {}

  @stream.send_with_id(iq) do |answer|
    answer.query.each_element('item') { |item|
      rooms[item.jid] = item.iname
    }
  end

  rooms
end