forked from adhearsion/blather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.rb
77 lines (61 loc) · 1.85 KB
/
status.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Blather
class Stanza
class Presence
class Status < Presence
VALID_STATES = [:away, :chat, :dnd, :xa]
include Comparable
register :status
def self.new(state = nil, message = nil)
elem = super()
elem.state = state
elem.message = message
elem
end
##
# Ensures type is nil or :unavailable
def type=(type)
raise ArgumentError, "Invalid type (#{type}). Must be nil or unavailable" if type && type.to_sym != :unavailable
super
end
##
# Ensure state is one of :away, :chat, :dnd, :xa or nil
def state=(state)
state = state.to_sym if state
state = nil if state == :available
raise ArgumentError, "Invalid Status (#{state}), use: #{VALID_STATES*' '}" if state && !VALID_STATES.include?(state)
remove_child :show
self << XMPPNode.new('show', state) if state
end
##
# return:: :available if state is nil
def state
(type || content_from(:show) || :available).to_sym
end
##
# Ensure priority is between -128 and 127
def priority=(priority)
raise ArgumentError, 'Priority must be between -128 and +127' if priority && !(-128..127).include?(priority.to_i)
remove_child :priority
self << XMPPNode.new('priority', priority) if priority
end
def priority
@priority ||= content_from(:priority).to_i
end
def message=(msg)
remove_child :status
self << XMPPNode.new('status', msg) if msg
end
def message
content_from :status
end
##
# Compare status based on priority
# raises an error if the JIDs aren't the same
def <=>(o)
raise "Cannot compare status from different JIDs: #{[self.from, o.from].inspect}" unless self.from.stripped == o.from.stripped
self.priority <=> o.priority
end
end #Status
end #Presence
end #Stanza
end #Blather