Class | Jabber::Presence |
In: |
lib/xmpp4r/presence.rb
|
Parent: | XMLStanza |
The presence class is used to construct presence messages to send to the Jabber service.
PRESENCE_STATUS | = | { :chat => 4, nil => 3, :dnd => 2, :away => 1, :xa => 0, :unavailable => -1, :error => -2 } | Compare two presences. The most suitable to talk with is the biggest. |
Create presence stanza
show: | [String] Initial Availability Status |
status: | [String] Initial status message |
priority: | [Fixnum] Initial priority value |
# File lib/xmpp4r/presence.rb, line 20 20: def initialize(show=nil, status=nil, priority=nil) 21: super("presence") 22: set_show(show) if show 23: set_status(status) if status 24: set_priority(priority) if priority 25: end
Compare two presences using priority (with cmp_interest as fall-back).
# File lib/xmpp4r/presence.rb, line 224 224: def <=>(o) 225: if priority.to_i == o.priority.to_i 226: cmp_interest(o) 227: else 228: priority.to_i <=> o.priority.to_i 229: end 230: end
# File lib/xmpp4r/presence.rb, line 242 242: def cmp_interest(o) 243: if type.nil? 244: if o.type.nil? 245: # both available. 246: PRESENCE_STATUS[show] <=> PRESENCE_STATUS[o.show] 247: else 248: return -1 249: end 250: elsif o.type.nil? 251: return 1 252: else 253: # both are non-nil. We consider this is equal. 254: return 0 255: end 256: end
Set presence priority
val: | [Integer] Priority value between -128 and +127 |
*Warning:* negative values make you receive no subscription requests etc. (RFC3921 - 2.2.2.3.)
# File lib/xmpp4r/presence.rb, line 205 205: def priority=(val) 206: if val.nil? 207: delete_element('priority') 208: else 209: replace_element_text('priority', val) 210: end 211: end
Get Availability Status (RFC3921 - 5.2)
result: | [Symbol] or [Nil] Valid values according to RFC3921: |
# File lib/xmpp4r/presence.rb, line 117 117: def show 118: e = first_element('show') 119: text = e ? e.text : nil 120: case text 121: when 'away' then :away 122: when 'chat' then :chat 123: when 'dnd' then :dnd 124: when 'xa' then :xa 125: else nil 126: end 127: end
Set Availability Status
val: | [Symbol] or [Nil] See show for explanation |
# File lib/xmpp4r/presence.rb, line 132 132: def show=(val) 133: xe = first_element('show') 134: if xe.nil? 135: xe = add_element('show') 136: end 137: case val 138: when :away then text = 'away' 139: when :chat then text = 'chat' 140: when :dnd then text = 'dnd' 141: when :xa then text = 'xa' 142: when nil then text = nil 143: else raise "Invalid value for show." 144: end 145: 146: if text.nil? 147: delete_element(xe) 148: else 149: xe.text = text 150: end 151: end
Get type of presence
result: | [Symbol] or [Nil] Possible values are: |
See RFC3921 - 2.2.1. for explanation.
# File lib/xmpp4r/presence.rb, line 59 59: def type 60: case super 61: when 'error' then :error 62: when 'probe' then :probe 63: when 'subscribe' then :subscribe 64: when 'subscribed' then :subscribed 65: when 'unavailable' then :unavailable 66: when 'unsubscribe' then :unsubscribe 67: when 'unsubscribed' then :unsubscribed 68: else nil 69: end 70: end
Set type of presence
val: | [Symbol] See type for possible subscription types |
# File lib/xmpp4r/presence.rb, line 75 75: def type=(val) 76: case val 77: when :error then super('error') 78: when :probe then super('probe') 79: when :subscribe then super('subscribe') 80: when :subscribed then super('subscribed') 81: when :unavailable then super('unavailable') 82: when :unsubscribe then super('unsubscribe') 83: when :unsubscribed then super('unsubscribed') 84: else super(nil) 85: end 86: end
Add an element to the presence stanza
element: | [REXML::Element] to add |
# File lib/xmpp4r/presence.rb, line 31 31: def typed_add(element) 32: if element.kind_of?(REXML::Element) && (element.name == 'x') 33: super(X::import(element)) 34: else 35: super(element) 36: end 37: end
Get the first <x/> element in this stanza, or nil if none found.
namespace: | [String] Optional, find the first <x/> element having this xmlns |
result: | [REXML::Element] or nil |
# File lib/xmpp4r/presence.rb, line 100 100: def x(namespace=nil) 101: each_element('x') { |x| 102: if namespace.nil? or namespace == x.namespace 103: return x 104: end 105: } 106: nil 107: end