Class: Jabber::Roster

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/roster.rb

Overview

The Roster class encapsulates the runtime roster of the session instance. The Roster contains all subscriptions in a Jabber::Roster::RosterItem hash.

Defined Under Namespace

Classes: RosterItem

Constant Summary collapse

ITEM_ADDED =
1
ITEM_DELETED =
2
RESOURCE_ADDED =
4
RESOURCE_UPDATED =
8
RESOURCE_DELETED =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Roster

Creates a Roster for the session

session
Jabber::Session

The session instance



28
29
30
31
32
# File 'lib/jabber4r/roster.rb', line 28

def initialize(session)
  @session = session
  @map = {}
  @listeners = {}
end

Instance Attribute Details

#sessionObject (readonly)

The Jabber::Session instance



21
22
23
# File 'lib/jabber4r/roster.rb', line 21

def session
  @session
end

Instance Method Details

#[](jid) ⇒ Object

Returns a Jabber::Roster::RosterItem based on the JID

jid
Jabber::JID | String

The Jabber ID

return
Jabber::Roster::RosterItem

The roster item



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/jabber4r/roster.rb', line 255

def [](jid)
  if jid.kind_of? String
    jid = JID.new(jid) 
    jid.strip_resource
  elsif jid.kind_of? JID
    jid = JID.new(jid.node+"@"+jid.host)
  else
    return
  end
  return @map[jid.to_s]
end

#add(jid, subscription, name, group = nil) ⇒ Object

Adds a subscription to be tracked in the Roster

jid
JID | String

The Jabber ID

subscription
String

The subscription type (both)

name
String

The nickname

group
String = nil

The name of the group of the roster item.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/jabber4r/roster.rb', line 231

def add(jid, subscription, name, group=nil)
  if jid.kind_of? String
    jid = JID.new(jid) 
    jid.strip_resource
  elsif jid.kind_of? JID
    jid = JID.new(jid.node+"@"+jid.host)
  else
    return
  end
  begin
    item = RosterItem.new(self, jid, subscription, name, group)
    @map[jid.to_s] = item
    notify_listeners(ITEM_ADDED, item)
  rescue => ex
    puts ex.backtrace.join("\n")
  end
end

#add_listener(&block) ⇒ Object

Adds a listener to the roster to process roster changes

&block
Block |event, rosteritem|

The block to process roster changes

return
String

The listener id to use to deregister



208
209
210
211
212
# File 'lib/jabber4r/roster.rb', line 208

def add_listener(&block)
  id = Jabber.gen_random_id("", 10)
  @listeners[id]=block if block
  return id
end

#delete(jid) ⇒ Object

Deletes a roster item based on the supplied Jabber ID

jid
Jabber::JID | String


272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/jabber4r/roster.rb', line 272

def delete(jid)
  if jid.kind_of? String
    jid = JID.new(jid) 
    jid.strip_resource
  elsif jid.kind_of? JID
    jid = JID.new(jid.node+"@"+jid.host)
  else
    return
  end
  item = @map.delete(jid.to_s)
  notify_listeners(ITEM_DELETED, item) if item
  item
end

#delete_listener(id) ⇒ Object

Deletes a listener for processing roster messages

id
String

A listener id (given by add_listener)



219
220
221
# File 'lib/jabber4r/roster.rb', line 219

def delete_listener(id)
  @listeners.delete(id)
end

#each_itemObject

Iterates over each RosterItem

yield
Jabber::Roster::RosterItem

The roster item.



291
292
293
# File 'lib/jabber4r/roster.rb', line 291

def each_item
  @map.each_value {|item| yield item}
end

#notify_listeners(event, object) ⇒ Object

Notifies listeners of a roster change event

event
Integer

The roster event

object
RosterItem

The modified item



315
316
317
# File 'lib/jabber4r/roster.rb', line 315

def notify_listeners(event, object)
  @listeners.each_value {|listener| listener.call(event, object)}
end

#to_sObject

Dumps the Roster state as a string

return
String

The roster state



300
301
302
303
304
305
306
307
# File 'lib/jabber4r/roster.rb', line 300

def to_s
  result = "ROSTER DUMP\n"
  each_item do |item|
    result += (item.to_s+"\n")
    item.each_resource {|resource| result+= "    #{resource.to_s}\n"}
  end
  return result
end