| 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 220
220: def <=>(o)
221: if priority.to_i == o.priority.to_i
222: cmp_interest(o)
223: else
224: priority.to_i <=> o.priority.to_i
225: end
226: end
# File lib/xmpp4r/presence.rb, line 238
238: def cmp_interest(o)
239: if type.nil?
240: if o.type.nil?
241: # both available.
242: PRESENCE_STATUS[show] <=> PRESENCE_STATUS[o.show]
243: else
244: return -1
245: end
246: elsif o.type.nil?
247: return 1
248: else
249: # both are non-nil. We consider this is equal.
250: return 0
251: end
252: 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 201
201: def priority=(val)
202: if val.nil?
203: delete_element('priority')
204: else
205: replace_element_text('priority', val)
206: end
207: end
Get Availability Status (RFC3921 - 5.2)
| result: | [Symbol] or [Nil] Valid values according to RFC3921: |
# File lib/xmpp4r/presence.rb, line 113
113: def show
114: e = first_element('show')
115: text = e ? e.text : nil
116: case text
117: when 'away' then :away
118: when 'chat' then :chat
119: when 'dnd' then :dnd
120: when 'xa' then :xa
121: else nil
122: end
123: end
Set Availability Status
| val: | [Symbol] or [Nil] See show for explanation |
# File lib/xmpp4r/presence.rb, line 128
128: def show=(val)
129: xe = first_element('show')
130: if xe.nil?
131: xe = add_element('show')
132: end
133: case val
134: when :away then text = 'away'
135: when :chat then text = 'chat'
136: when :dnd then text = 'dnd'
137: when :xa then text = 'xa'
138: when nil then text = nil
139: else raise "Invalid value for show."
140: end
141:
142: if text.nil?
143: delete_element(xe)
144: else
145: xe.text = text
146: end
147: 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 of this stanza
| result: | [REXML::Element] or nil |
# File lib/xmpp4r/presence.rb, line 99
99: def x
100: xe = nil
101: each_element('x') { |e| xe = e }
102: xe
103: end