jabber4r-revive 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -4
- data/.rspec +3 -3
- data/.travis.yml +7 -7
- data/CHANGELOG +11 -1
- data/Gemfile +3 -3
- data/README.md +29 -29
- data/Rakefile +70 -70
- data/jabber4r-revive.gemspec +25 -25
- data/lib/jabber4r.rb +38 -33
- data/lib/jabber4r/bosh.rb +21 -0
- data/lib/jabber4r/bosh/authentication.rb +13 -0
- data/lib/jabber4r/bosh/authentication/non_sasl.rb +219 -0
- data/lib/jabber4r/bosh/authentication/sasl.rb +239 -0
- data/lib/jabber4r/bosh/session.rb +144 -0
- data/lib/jabber4r/connection.rb +259 -258
- data/lib/jabber4r/debugger.rb +60 -60
- data/lib/jabber4r/jid.rb +20 -19
- data/lib/jabber4r/protocol.rb +249 -257
- data/lib/jabber4r/protocol/authentication.rb +14 -0
- data/lib/jabber4r/protocol/authentication/non_sasl.rb +138 -0
- data/lib/jabber4r/protocol/authentication/sasl.rb +88 -0
- data/lib/jabber4r/protocol/iq.rb +259 -259
- data/lib/jabber4r/protocol/message.rb +245 -245
- data/lib/jabber4r/protocol/parsed_xml_element.rb +207 -207
- data/lib/jabber4r/protocol/presence.rb +160 -160
- data/lib/jabber4r/protocol/xml_element.rb +143 -143
- data/lib/jabber4r/rexml_1.8_patch.rb +15 -15
- data/lib/jabber4r/roster.rb +38 -38
- data/lib/jabber4r/session.rb +615 -615
- data/lib/jabber4r/version.rb +10 -3
- data/spec/lib/jabber4r/bosh/authentication/non_sasl_spec.rb +79 -0
- data/spec/lib/jabber4r/bosh/authentication/sasl_spec.rb +42 -0
- data/spec/lib/jabber4r/bosh/session_spec.rb +406 -0
- data/spec/lib/jabber4r/bosh_spec.rb +0 -0
- data/spec/lib/jabber4r/connection_spec.rb +174 -174
- data/spec/lib/jabber4r/debugger_spec.rb +35 -35
- data/spec/lib/jabber4r/jid_spec.rb +197 -197
- data/spec/lib/jabber4r/protocol/authentication/non_sasl_spec.rb +79 -0
- data/spec/lib/jabber4r/protocol/authentication/sasl_spec.rb +42 -0
- data/spec/spec_helper.rb +11 -11
- data/spec/support/mocks/tcp_socket_mock.rb +8 -8
- metadata +61 -45
- data/Gemfile.lock +0 -45
- data/lib/jabber4r/bosh_session.rb +0 -224
- data/spec/lib/jabber4r/bosh_session_spec.rb +0 -150
@@ -1,208 +1,208 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
# License: see LICENSE
|
4
|
-
# Jabber4R - Jabber Instant Messaging Library for Ruby
|
5
|
-
# Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
|
6
|
-
|
7
|
-
module Jabber::Protocol
|
8
|
-
##
|
9
|
-
# This class is constructed from XML data elements that are received from
|
10
|
-
# the Jabber service.
|
11
|
-
#
|
12
|
-
class ParsedXMLElement
|
13
|
-
|
14
|
-
##
|
15
|
-
# This class is used to return nil element values to prevent errors (and
|
16
|
-
# reduce the number of checks.
|
17
|
-
#
|
18
|
-
class NilParsedXMLElement
|
19
|
-
|
20
|
-
##
|
21
|
-
# Override to return nil
|
22
|
-
#
|
23
|
-
# return:: [nil]
|
24
|
-
#
|
25
|
-
def method_missing(methId, *args)
|
26
|
-
return nil
|
27
|
-
end
|
28
|
-
|
29
|
-
##
|
30
|
-
# Evaluate as nil
|
31
|
-
#
|
32
|
-
# return:: [Boolean] true
|
33
|
-
#
|
34
|
-
def nil?
|
35
|
-
return true
|
36
|
-
end
|
37
|
-
|
38
|
-
##
|
39
|
-
# Return a zero count
|
40
|
-
#
|
41
|
-
# return:: [Integer] 0
|
42
|
-
#
|
43
|
-
def count
|
44
|
-
0
|
45
|
-
end
|
46
|
-
|
47
|
-
include Singleton
|
48
|
-
end
|
49
|
-
|
50
|
-
# The <tag> as String
|
51
|
-
attr_reader :element_tag
|
52
|
-
|
53
|
-
# The parent ParsedXMLElement
|
54
|
-
attr_reader :element_parent
|
55
|
-
|
56
|
-
# A hash of ParsedXMLElement children
|
57
|
-
attr_reader :element_children
|
58
|
-
|
59
|
-
# The data <tag>data</tag> for a tag
|
60
|
-
attr_reader :element_data
|
61
|
-
|
62
|
-
##
|
63
|
-
# Construct an instance for the given tag
|
64
|
-
#
|
65
|
-
# tag:: [String] The tag
|
66
|
-
# parent:: [Jabber::Protocol::ParsedXMLElement = nil] The parent element
|
67
|
-
#
|
68
|
-
def initialize(tag, parent=nil)
|
69
|
-
@element_tag = tag
|
70
|
-
@element_parent = parent
|
71
|
-
@element_children = {}
|
72
|
-
@attributes = {}
|
73
|
-
@element_consumed = false
|
74
|
-
end
|
75
|
-
|
76
|
-
##
|
77
|
-
# Add the attribute to the element
|
78
|
-
# <tag name="value">data</tag>
|
79
|
-
#
|
80
|
-
# name:: [String] The attribute name
|
81
|
-
# value:: [String] The attribute value
|
82
|
-
# return:: [Jabber::Protocol::ParsedXMLElement] self for chaining
|
83
|
-
#
|
84
|
-
def add_attribute(name, value)
|
85
|
-
@attributes[name]=value
|
86
|
-
self
|
87
|
-
end
|
88
|
-
|
89
|
-
##
|
90
|
-
# Factory to build a child element from this element with the given tag
|
91
|
-
#
|
92
|
-
# tag:: [String] The tag name
|
93
|
-
# return:: [Jabber::Protocol::ParsedXMLElement] The newly created child element
|
94
|
-
#
|
95
|
-
def add_child(tag)
|
96
|
-
child = ParsedXMLElement.new(tag, self)
|
97
|
-
@element_children[tag] = Array.new if not @element_children.has_key? tag
|
98
|
-
@element_children[tag] << child
|
99
|
-
return child
|
100
|
-
end
|
101
|
-
|
102
|
-
##
|
103
|
-
# When an xml is received from the Jabber service and a ParsedXMLElement is created,
|
104
|
-
# it is propogated to all filters and listeners. Any one of those can consume the element
|
105
|
-
# to prevent its propogation to other filters or listeners. This method marks the element
|
106
|
-
# as consumed.
|
107
|
-
#
|
108
|
-
def consume_element
|
109
|
-
@element_consumed = true
|
110
|
-
end
|
111
|
-
|
112
|
-
##
|
113
|
-
# Checks if the element is consumed
|
114
|
-
#
|
115
|
-
# return:: [Boolean] True if the element is consumed
|
116
|
-
#
|
117
|
-
def element_consumed?
|
118
|
-
@element_consumed
|
119
|
-
end
|
120
|
-
|
121
|
-
##
|
122
|
-
# Appends data to the element
|
123
|
-
#
|
124
|
-
# data:: [String] The data to append
|
125
|
-
# return:: [Jabber::Protocol::ParsedXMLElement] self for chaining
|
126
|
-
#
|
127
|
-
def append_data(data)
|
128
|
-
@element_data = "" unless @element_data
|
129
|
-
@element_data += data
|
130
|
-
self
|
131
|
-
end
|
132
|
-
|
133
|
-
##
|
134
|
-
# Calls the parent's element_children (hash) index off of this elements
|
135
|
-
# tag and gets the supplied index. In this sense it gets its sibling based
|
136
|
-
# on offset.
|
137
|
-
#
|
138
|
-
# number:: [Integer] The number of the sibling to get
|
139
|
-
# return:: [Jabber::Protocol::ParsedXMLElement] The sibling element
|
140
|
-
#
|
141
|
-
def [](number)
|
142
|
-
return @element_parent.element_children[@element_tag][number] if @element_parent
|
143
|
-
end
|
144
|
-
|
145
|
-
##
|
146
|
-
# Returns the count of siblings with this element's tag
|
147
|
-
#
|
148
|
-
# return:: [Integer] The number of sibling elements
|
149
|
-
#
|
150
|
-
def count
|
151
|
-
return @element_parent.element_children[@element_tag].size if @element_parent
|
152
|
-
return 0
|
153
|
-
end
|
154
|
-
|
155
|
-
##
|
156
|
-
# see _count
|
157
|
-
#
|
158
|
-
def size
|
159
|
-
count
|
160
|
-
end
|
161
|
-
|
162
|
-
##
|
163
|
-
# Overrides to allow for directly accessing child elements
|
164
|
-
# and attributes. If prefaced by attr_ it looks for an attribute
|
165
|
-
# that matches or checks for a child with a tag that matches
|
166
|
-
# the method name. If no match occurs, it returns a
|
167
|
-
# NilParsedXMLElement (singleton) instance.
|
168
|
-
#
|
169
|
-
# Example:: <alpha number="1"><beta number="2">Beta Data</beta></alpha>
|
170
|
-
#
|
171
|
-
# element.element_tag #=> alpha
|
172
|
-
# element.attr_number #=> 1
|
173
|
-
# element.beta.element_data #=> Beta Data
|
174
|
-
#
|
175
|
-
def method_missing(methId, *args)
|
176
|
-
tag = methId.id2name
|
177
|
-
if tag[0..4]=="attr_"
|
178
|
-
return @attributes[tag[5..-1]]
|
179
|
-
end
|
180
|
-
list = @element_children[tag]
|
181
|
-
return list[0] if list
|
182
|
-
return NilParsedXMLElement.instance
|
183
|
-
end
|
184
|
-
|
185
|
-
##
|
186
|
-
# Returns the valid XML as a string
|
187
|
-
#
|
188
|
-
# return:: [String] XML string
|
189
|
-
def to_s
|
190
|
-
begin
|
191
|
-
result = "\n<#{@element_tag}"
|
192
|
-
@attributes.each {|key, value| result += (' '+key+'="'+value+'"') }
|
193
|
-
if @element_children.size>0 or @element_data
|
194
|
-
result += ">"
|
195
|
-
else
|
196
|
-
result += "/>"
|
197
|
-
end
|
198
|
-
result += @element_data if @element_data
|
199
|
-
@element_children.each_value {|array| array.each {|je| result += je.to_s} }
|
200
|
-
result += "\n" if @element_children.size>0
|
201
|
-
result += "</#{@element_tag}>" if @element_children.size>0 or @element_data
|
202
|
-
result
|
203
|
-
rescue => exception
|
204
|
-
puts exception.to_s
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# License: see LICENSE
|
4
|
+
# Jabber4R - Jabber Instant Messaging Library for Ruby
|
5
|
+
# Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
|
6
|
+
|
7
|
+
module Jabber::Protocol
|
8
|
+
##
|
9
|
+
# This class is constructed from XML data elements that are received from
|
10
|
+
# the Jabber service.
|
11
|
+
#
|
12
|
+
class ParsedXMLElement
|
13
|
+
|
14
|
+
##
|
15
|
+
# This class is used to return nil element values to prevent errors (and
|
16
|
+
# reduce the number of checks.
|
17
|
+
#
|
18
|
+
class NilParsedXMLElement
|
19
|
+
|
20
|
+
##
|
21
|
+
# Override to return nil
|
22
|
+
#
|
23
|
+
# return:: [nil]
|
24
|
+
#
|
25
|
+
def method_missing(methId, *args)
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Evaluate as nil
|
31
|
+
#
|
32
|
+
# return:: [Boolean] true
|
33
|
+
#
|
34
|
+
def nil?
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Return a zero count
|
40
|
+
#
|
41
|
+
# return:: [Integer] 0
|
42
|
+
#
|
43
|
+
def count
|
44
|
+
0
|
45
|
+
end
|
46
|
+
|
47
|
+
include Singleton
|
48
|
+
end
|
49
|
+
|
50
|
+
# The <tag> as String
|
51
|
+
attr_reader :element_tag
|
52
|
+
|
53
|
+
# The parent ParsedXMLElement
|
54
|
+
attr_reader :element_parent
|
55
|
+
|
56
|
+
# A hash of ParsedXMLElement children
|
57
|
+
attr_reader :element_children
|
58
|
+
|
59
|
+
# The data <tag>data</tag> for a tag
|
60
|
+
attr_reader :element_data
|
61
|
+
|
62
|
+
##
|
63
|
+
# Construct an instance for the given tag
|
64
|
+
#
|
65
|
+
# tag:: [String] The tag
|
66
|
+
# parent:: [Jabber::Protocol::ParsedXMLElement = nil] The parent element
|
67
|
+
#
|
68
|
+
def initialize(tag, parent=nil)
|
69
|
+
@element_tag = tag
|
70
|
+
@element_parent = parent
|
71
|
+
@element_children = {}
|
72
|
+
@attributes = {}
|
73
|
+
@element_consumed = false
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Add the attribute to the element
|
78
|
+
# <tag name="value">data</tag>
|
79
|
+
#
|
80
|
+
# name:: [String] The attribute name
|
81
|
+
# value:: [String] The attribute value
|
82
|
+
# return:: [Jabber::Protocol::ParsedXMLElement] self for chaining
|
83
|
+
#
|
84
|
+
def add_attribute(name, value)
|
85
|
+
@attributes[name]=value
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
89
|
+
##
|
90
|
+
# Factory to build a child element from this element with the given tag
|
91
|
+
#
|
92
|
+
# tag:: [String] The tag name
|
93
|
+
# return:: [Jabber::Protocol::ParsedXMLElement] The newly created child element
|
94
|
+
#
|
95
|
+
def add_child(tag)
|
96
|
+
child = ParsedXMLElement.new(tag, self)
|
97
|
+
@element_children[tag] = Array.new if not @element_children.has_key? tag
|
98
|
+
@element_children[tag] << child
|
99
|
+
return child
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# When an xml is received from the Jabber service and a ParsedXMLElement is created,
|
104
|
+
# it is propogated to all filters and listeners. Any one of those can consume the element
|
105
|
+
# to prevent its propogation to other filters or listeners. This method marks the element
|
106
|
+
# as consumed.
|
107
|
+
#
|
108
|
+
def consume_element
|
109
|
+
@element_consumed = true
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Checks if the element is consumed
|
114
|
+
#
|
115
|
+
# return:: [Boolean] True if the element is consumed
|
116
|
+
#
|
117
|
+
def element_consumed?
|
118
|
+
@element_consumed
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Appends data to the element
|
123
|
+
#
|
124
|
+
# data:: [String] The data to append
|
125
|
+
# return:: [Jabber::Protocol::ParsedXMLElement] self for chaining
|
126
|
+
#
|
127
|
+
def append_data(data)
|
128
|
+
@element_data = "" unless @element_data
|
129
|
+
@element_data += data
|
130
|
+
self
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Calls the parent's element_children (hash) index off of this elements
|
135
|
+
# tag and gets the supplied index. In this sense it gets its sibling based
|
136
|
+
# on offset.
|
137
|
+
#
|
138
|
+
# number:: [Integer] The number of the sibling to get
|
139
|
+
# return:: [Jabber::Protocol::ParsedXMLElement] The sibling element
|
140
|
+
#
|
141
|
+
def [](number)
|
142
|
+
return @element_parent.element_children[@element_tag][number] if @element_parent
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# Returns the count of siblings with this element's tag
|
147
|
+
#
|
148
|
+
# return:: [Integer] The number of sibling elements
|
149
|
+
#
|
150
|
+
def count
|
151
|
+
return @element_parent.element_children[@element_tag].size if @element_parent
|
152
|
+
return 0
|
153
|
+
end
|
154
|
+
|
155
|
+
##
|
156
|
+
# see _count
|
157
|
+
#
|
158
|
+
def size
|
159
|
+
count
|
160
|
+
end
|
161
|
+
|
162
|
+
##
|
163
|
+
# Overrides to allow for directly accessing child elements
|
164
|
+
# and attributes. If prefaced by attr_ it looks for an attribute
|
165
|
+
# that matches or checks for a child with a tag that matches
|
166
|
+
# the method name. If no match occurs, it returns a
|
167
|
+
# NilParsedXMLElement (singleton) instance.
|
168
|
+
#
|
169
|
+
# Example:: <alpha number="1"><beta number="2">Beta Data</beta></alpha>
|
170
|
+
#
|
171
|
+
# element.element_tag #=> alpha
|
172
|
+
# element.attr_number #=> 1
|
173
|
+
# element.beta.element_data #=> Beta Data
|
174
|
+
#
|
175
|
+
def method_missing(methId, *args)
|
176
|
+
tag = methId.id2name
|
177
|
+
if tag[0..4]=="attr_"
|
178
|
+
return @attributes[tag[5..-1]]
|
179
|
+
end
|
180
|
+
list = @element_children[tag]
|
181
|
+
return list[0] if list
|
182
|
+
return NilParsedXMLElement.instance
|
183
|
+
end
|
184
|
+
|
185
|
+
##
|
186
|
+
# Returns the valid XML as a string
|
187
|
+
#
|
188
|
+
# return:: [String] XML string
|
189
|
+
def to_s
|
190
|
+
begin
|
191
|
+
result = "\n<#{@element_tag}"
|
192
|
+
@attributes.each {|key, value| result += (' '+key+'="'+value+'"') }
|
193
|
+
if @element_children.size>0 or @element_data
|
194
|
+
result += ">"
|
195
|
+
else
|
196
|
+
result += "/>"
|
197
|
+
end
|
198
|
+
result += @element_data if @element_data
|
199
|
+
@element_children.each_value {|array| array.each {|je| result += je.to_s} }
|
200
|
+
result += "\n" if @element_children.size>0
|
201
|
+
result += "</#{@element_tag}>" if @element_children.size>0 or @element_data
|
202
|
+
result
|
203
|
+
rescue => exception
|
204
|
+
puts exception.to_s
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
208
|
end
|
@@ -1,160 +1,160 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
# License: see LICENSE
|
4
|
-
# Jabber4R - Jabber Instant Messaging Library for Ruby
|
5
|
-
# Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
|
6
|
-
|
7
|
-
module Jabber::Protocol
|
8
|
-
##
|
9
|
-
# The presence class is used to construct presence messages to
|
10
|
-
# send to the Jabber service.
|
11
|
-
#
|
12
|
-
class Presence
|
13
|
-
attr_accessor :to, :from, :id, :type
|
14
|
-
|
15
|
-
# The state to show (chat, xa, dnd, away)
|
16
|
-
attr_accessor :show
|
17
|
-
|
18
|
-
# The status message
|
19
|
-
attr_accessor :status
|
20
|
-
attr_accessor :priority
|
21
|
-
|
22
|
-
##
|
23
|
-
# Constructs a Presence object w/the supplied id
|
24
|
-
#
|
25
|
-
# id:: [String] The message ID
|
26
|
-
# show:: [String] The state to show
|
27
|
-
# status:: [String] The status message
|
28
|
-
#
|
29
|
-
def initialize(id, show=nil, status=nil)
|
30
|
-
@id = id
|
31
|
-
@show = show if show
|
32
|
-
@status = status if status
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# Generate a presence object for initial presence notification
|
37
|
-
#
|
38
|
-
# id:: [String] The message ID
|
39
|
-
# show:: [String] The state to show
|
40
|
-
# status:: [String] The status message
|
41
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
42
|
-
#
|
43
|
-
def self.gen_initial(id, show=nil, status=nil)
|
44
|
-
Presence.new(id, show, status)
|
45
|
-
end
|
46
|
-
|
47
|
-
##
|
48
|
-
# Generate a presence object w/show="normal" (normal availability)
|
49
|
-
#
|
50
|
-
# id:: [String] The message ID
|
51
|
-
# status:: [String=nil] The status message
|
52
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
53
|
-
#
|
54
|
-
def self.gen_normal(id, status=nil)
|
55
|
-
Presence.new(id, "normal", status)
|
56
|
-
end
|
57
|
-
|
58
|
-
##
|
59
|
-
# Generate a presence object w/show="chat" (free for chat)
|
60
|
-
#
|
61
|
-
# id:: [String] The message ID
|
62
|
-
# status:: [String=nil] The status message
|
63
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
64
|
-
#
|
65
|
-
def self.gen_chat(id, status=nil)
|
66
|
-
Presence.new(id, "chat", status)
|
67
|
-
end
|
68
|
-
|
69
|
-
##
|
70
|
-
# Generate a presence object w/show="xa" (extended away)
|
71
|
-
#
|
72
|
-
# id:: [String] The message ID
|
73
|
-
# status:: [String=nil] The status message
|
74
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
75
|
-
#
|
76
|
-
def self.gen_xa(id, status=nil)
|
77
|
-
Presence.new(id, "xa", status)
|
78
|
-
end
|
79
|
-
|
80
|
-
##
|
81
|
-
# Generate a presence object w/show="dnd" (do not disturb)
|
82
|
-
#
|
83
|
-
# id:: [String] The message ID
|
84
|
-
# status:: [String=nil] The status message
|
85
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
86
|
-
#
|
87
|
-
def self.gen_dnd(id, status=nil)
|
88
|
-
Presence.new(id, "dnd", status)
|
89
|
-
end
|
90
|
-
|
91
|
-
##
|
92
|
-
# Generate a presence object w/show="away" (away from resource)
|
93
|
-
#
|
94
|
-
# id:: [String] The message ID
|
95
|
-
# status:: [String=nil] The status message
|
96
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
97
|
-
#
|
98
|
-
def self.gen_away(id, status=nil)
|
99
|
-
Presence.new(id, "away", status)
|
100
|
-
end
|
101
|
-
|
102
|
-
##
|
103
|
-
# Generate a presence object w/show="unavailable" (not free for chat)
|
104
|
-
#
|
105
|
-
# id:: [String] The message ID
|
106
|
-
# status:: [String=nil] The status message
|
107
|
-
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
108
|
-
#
|
109
|
-
def self.gen_unavailable(id, status=nil)
|
110
|
-
p = Presence.new(id)
|
111
|
-
p.type="unavailable"
|
112
|
-
p
|
113
|
-
end
|
114
|
-
|
115
|
-
def self.gen_new_subscription(to)
|
116
|
-
p = Presence.new(Jabber.gen_random_id)
|
117
|
-
p.type = "subscribe"
|
118
|
-
p.to = to
|
119
|
-
p
|
120
|
-
end
|
121
|
-
|
122
|
-
def self.gen_accept_subscription(id, jid)
|
123
|
-
p = Presence.new(id)
|
124
|
-
p.type = "subscribed"
|
125
|
-
p.to = jid
|
126
|
-
p
|
127
|
-
end
|
128
|
-
|
129
|
-
def self.gen_accept_unsubscription(id, jid)
|
130
|
-
p = Presence.new(id)
|
131
|
-
p.type = "unsubscribed"
|
132
|
-
p.to = jid
|
133
|
-
p
|
134
|
-
end
|
135
|
-
|
136
|
-
##
|
137
|
-
# Generates the xml representation of this Presence object
|
138
|
-
#
|
139
|
-
# return:: [String] The presence XML message to send the Jabber service
|
140
|
-
#
|
141
|
-
def to_xml
|
142
|
-
e = XMLElement.new("presence")
|
143
|
-
e.add_attribute("id", @id) if @id
|
144
|
-
e.add_attribute("from", @from) if @from
|
145
|
-
e.add_attribute("to", @to) if @to
|
146
|
-
e.add_attribute("type", @type) if @type
|
147
|
-
e.add_child("show").add_data(@show) if @show
|
148
|
-
e.add_child("status").add_data(@status) if @status
|
149
|
-
e.add_child("priority") if @priority
|
150
|
-
e.to_s
|
151
|
-
end
|
152
|
-
|
153
|
-
##
|
154
|
-
# see _to_xml
|
155
|
-
#
|
156
|
-
def to_s
|
157
|
-
to_xml
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# License: see LICENSE
|
4
|
+
# Jabber4R - Jabber Instant Messaging Library for Ruby
|
5
|
+
# Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
|
6
|
+
|
7
|
+
module Jabber::Protocol
|
8
|
+
##
|
9
|
+
# The presence class is used to construct presence messages to
|
10
|
+
# send to the Jabber service.
|
11
|
+
#
|
12
|
+
class Presence
|
13
|
+
attr_accessor :to, :from, :id, :type
|
14
|
+
|
15
|
+
# The state to show (chat, xa, dnd, away)
|
16
|
+
attr_accessor :show
|
17
|
+
|
18
|
+
# The status message
|
19
|
+
attr_accessor :status
|
20
|
+
attr_accessor :priority
|
21
|
+
|
22
|
+
##
|
23
|
+
# Constructs a Presence object w/the supplied id
|
24
|
+
#
|
25
|
+
# id:: [String] The message ID
|
26
|
+
# show:: [String] The state to show
|
27
|
+
# status:: [String] The status message
|
28
|
+
#
|
29
|
+
def initialize(id, show=nil, status=nil)
|
30
|
+
@id = id
|
31
|
+
@show = show if show
|
32
|
+
@status = status if status
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Generate a presence object for initial presence notification
|
37
|
+
#
|
38
|
+
# id:: [String] The message ID
|
39
|
+
# show:: [String] The state to show
|
40
|
+
# status:: [String] The status message
|
41
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
42
|
+
#
|
43
|
+
def self.gen_initial(id, show=nil, status=nil)
|
44
|
+
Presence.new(id, show, status)
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Generate a presence object w/show="normal" (normal availability)
|
49
|
+
#
|
50
|
+
# id:: [String] The message ID
|
51
|
+
# status:: [String=nil] The status message
|
52
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
53
|
+
#
|
54
|
+
def self.gen_normal(id, status=nil)
|
55
|
+
Presence.new(id, "normal", status)
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Generate a presence object w/show="chat" (free for chat)
|
60
|
+
#
|
61
|
+
# id:: [String] The message ID
|
62
|
+
# status:: [String=nil] The status message
|
63
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
64
|
+
#
|
65
|
+
def self.gen_chat(id, status=nil)
|
66
|
+
Presence.new(id, "chat", status)
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Generate a presence object w/show="xa" (extended away)
|
71
|
+
#
|
72
|
+
# id:: [String] The message ID
|
73
|
+
# status:: [String=nil] The status message
|
74
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
75
|
+
#
|
76
|
+
def self.gen_xa(id, status=nil)
|
77
|
+
Presence.new(id, "xa", status)
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Generate a presence object w/show="dnd" (do not disturb)
|
82
|
+
#
|
83
|
+
# id:: [String] The message ID
|
84
|
+
# status:: [String=nil] The status message
|
85
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
86
|
+
#
|
87
|
+
def self.gen_dnd(id, status=nil)
|
88
|
+
Presence.new(id, "dnd", status)
|
89
|
+
end
|
90
|
+
|
91
|
+
##
|
92
|
+
# Generate a presence object w/show="away" (away from resource)
|
93
|
+
#
|
94
|
+
# id:: [String] The message ID
|
95
|
+
# status:: [String=nil] The status message
|
96
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
97
|
+
#
|
98
|
+
def self.gen_away(id, status=nil)
|
99
|
+
Presence.new(id, "away", status)
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Generate a presence object w/show="unavailable" (not free for chat)
|
104
|
+
#
|
105
|
+
# id:: [String] The message ID
|
106
|
+
# status:: [String=nil] The status message
|
107
|
+
# return:: [Jabber::Protocol::Presence] The newly created Presence object
|
108
|
+
#
|
109
|
+
def self.gen_unavailable(id, status=nil)
|
110
|
+
p = Presence.new(id)
|
111
|
+
p.type="unavailable"
|
112
|
+
p
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.gen_new_subscription(to)
|
116
|
+
p = Presence.new(Jabber.gen_random_id)
|
117
|
+
p.type = "subscribe"
|
118
|
+
p.to = to
|
119
|
+
p
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.gen_accept_subscription(id, jid)
|
123
|
+
p = Presence.new(id)
|
124
|
+
p.type = "subscribed"
|
125
|
+
p.to = jid
|
126
|
+
p
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.gen_accept_unsubscription(id, jid)
|
130
|
+
p = Presence.new(id)
|
131
|
+
p.type = "unsubscribed"
|
132
|
+
p.to = jid
|
133
|
+
p
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# Generates the xml representation of this Presence object
|
138
|
+
#
|
139
|
+
# return:: [String] The presence XML message to send the Jabber service
|
140
|
+
#
|
141
|
+
def to_xml
|
142
|
+
e = XMLElement.new("presence")
|
143
|
+
e.add_attribute("id", @id) if @id
|
144
|
+
e.add_attribute("from", @from) if @from
|
145
|
+
e.add_attribute("to", @to) if @to
|
146
|
+
e.add_attribute("type", @type) if @type
|
147
|
+
e.add_child("show").add_data(@show) if @show
|
148
|
+
e.add_child("status").add_data(@status) if @status
|
149
|
+
e.add_child("priority") if @priority
|
150
|
+
e.to_s
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# see _to_xml
|
155
|
+
#
|
156
|
+
def to_s
|
157
|
+
to_xml
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|