blather 0.4.7 → 0.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +162 -0
- data/examples/{print_heirarchy.rb → print_hierarchy.rb} +5 -5
- data/examples/stream_only.rb +27 -0
- data/lib/blather.rb +4 -0
- data/lib/blather/client/client.rb +91 -73
- data/lib/blather/client/dsl.rb +156 -32
- data/lib/blather/client/dsl/pubsub.rb +86 -54
- data/lib/blather/core_ext/active_support.rb +9 -9
- data/lib/blather/core_ext/active_support/inheritable_attributes.rb +2 -2
- data/lib/blather/core_ext/nokogiri.rb +12 -7
- data/lib/blather/errors.rb +25 -14
- data/lib/blather/errors/sasl_error.rb +21 -3
- data/lib/blather/errors/stanza_error.rb +37 -21
- data/lib/blather/errors/stream_error.rb +27 -17
- data/lib/blather/jid.rb +79 -24
- data/lib/blather/roster.rb +39 -21
- data/lib/blather/roster_item.rb +43 -21
- data/lib/blather/stanza.rb +88 -40
- data/lib/blather/stanza/disco.rb +12 -2
- data/lib/blather/stanza/disco/disco_info.rb +112 -20
- data/lib/blather/stanza/disco/disco_items.rb +81 -12
- data/lib/blather/stanza/iq.rb +94 -38
- data/lib/blather/stanza/iq/query.rb +16 -22
- data/lib/blather/stanza/iq/roster.rb +98 -20
- data/lib/blather/stanza/message.rb +266 -111
- data/lib/blather/stanza/presence.rb +118 -42
- data/lib/blather/stanza/presence/status.rb +140 -60
- data/lib/blather/stanza/presence/subscription.rb +44 -10
- data/lib/blather/stanza/pubsub.rb +70 -15
- data/lib/blather/stanza/pubsub/affiliations.rb +36 -7
- data/lib/blather/stanza/pubsub/create.rb +26 -4
- data/lib/blather/stanza/pubsub/errors.rb +13 -4
- data/lib/blather/stanza/pubsub/event.rb +56 -10
- data/lib/blather/stanza/pubsub/items.rb +46 -6
- data/lib/blather/stanza/pubsub/publish.rb +52 -7
- data/lib/blather/stanza/pubsub/retract.rb +45 -6
- data/lib/blather/stanza/pubsub/subscribe.rb +30 -4
- data/lib/blather/stanza/pubsub/subscription.rb +74 -6
- data/lib/blather/stanza/pubsub/subscriptions.rb +35 -9
- data/lib/blather/stanza/pubsub/unsubscribe.rb +30 -4
- data/lib/blather/stanza/pubsub_owner.rb +17 -7
- data/lib/blather/stanza/pubsub_owner/delete.rb +23 -5
- data/lib/blather/stanza/pubsub_owner/purge.rb +23 -5
- data/lib/blather/stream.rb +96 -29
- data/lib/blather/stream/parser.rb +6 -9
- data/lib/blather/xmpp_node.rb +101 -153
- data/spec/blather/client/client_spec.rb +1 -1
- data/spec/blather/errors_spec.rb +5 -5
- data/spec/blather/stanza/message_spec.rb +56 -0
- data/spec/blather/stanza/presence/status_spec.rb +1 -1
- data/spec/blather/stanza_spec.rb +3 -3
- data/spec/blather/xmpp_node_spec.rb +19 -74
- metadata +6 -10
- data/README.rdoc +0 -185
- data/examples/drb_client.rb +0 -5
- data/examples/ping.rb +0 -11
- data/examples/pong.rb +0 -6
- data/examples/pubsub/cli.rb +0 -64
- data/examples/pubsub/ping_pong.rb +0 -18
@@ -1,44 +1,44 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'active_support', 'inheritable_attributes')
|
2
2
|
|
3
|
-
class Object
|
3
|
+
class Object # @private
|
4
4
|
def duplicable?; true; end
|
5
5
|
def blank?; respond_to?(:empty?) ? empty? : !self; end
|
6
6
|
def present?; !blank?; end
|
7
7
|
end
|
8
8
|
|
9
|
-
class Array
|
9
|
+
class Array # @private
|
10
10
|
alias_method :blank?, :empty?
|
11
11
|
def extract_options!; last.is_a?(::Hash) ? pop : {}; end
|
12
12
|
end
|
13
13
|
|
14
|
-
class Hash
|
14
|
+
class Hash # @private
|
15
15
|
alias_method :blank?, :empty?
|
16
16
|
end
|
17
17
|
|
18
|
-
class String
|
18
|
+
class String # @private
|
19
19
|
def blank?; self !~ /\S/; end
|
20
20
|
end
|
21
21
|
|
22
|
-
class NilClass
|
22
|
+
class NilClass # @private
|
23
23
|
def duplicable?; false; end
|
24
24
|
def blank?; true; end
|
25
25
|
end
|
26
26
|
|
27
|
-
class FalseClass
|
27
|
+
class FalseClass # @private
|
28
28
|
def duplicable?; false; end
|
29
29
|
def blank?; true; end
|
30
30
|
end
|
31
31
|
|
32
|
-
class TrueClass
|
32
|
+
class TrueClass # @private
|
33
33
|
def duplicable?; false; end
|
34
34
|
def blank?; false; end
|
35
35
|
end
|
36
36
|
|
37
|
-
class Symbol
|
37
|
+
class Symbol # @private
|
38
38
|
def duplicable?; false; end
|
39
39
|
end
|
40
40
|
|
41
|
-
class Numeric
|
41
|
+
class Numeric # @private
|
42
42
|
def duplicable?; false; end
|
43
43
|
def blank?; false; end
|
44
44
|
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
|
5
5
|
# to, for example, an array without those additions being shared with either their parent, siblings, or
|
6
6
|
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
|
7
|
-
class Class
|
7
|
+
class Class # @private
|
8
8
|
def class_inheritable_reader(*syms)
|
9
9
|
syms.each do |sym|
|
10
10
|
next if sym.is_a?(Hash)
|
@@ -114,4 +114,4 @@ class Class # :nodoc:
|
|
114
114
|
|
115
115
|
alias inherited_without_inheritable_attributes inherited
|
116
116
|
alias inherited inherited_with_inheritable_attributes
|
117
|
-
end
|
117
|
+
end # Class
|
@@ -1,22 +1,25 @@
|
|
1
|
-
module Nokogiri
|
1
|
+
module Nokogiri
|
2
2
|
module XML
|
3
3
|
|
4
4
|
class Node
|
5
|
+
# Alias #name to #element_name so we can use #name in an XMPP Stanza context
|
5
6
|
alias_method :element_name, :name
|
6
7
|
alias_method :element_name=, :name=
|
7
8
|
|
8
|
-
alias_method :attr_set, :[]=
|
9
|
+
alias_method :attr_set, :[]= # :nodoc:
|
10
|
+
# Override Nokogiri's attribute setter to add the ability to kill an attribute
|
11
|
+
# by setting it to nil and to be able to lookup an attribute by symbol
|
12
|
+
#
|
13
|
+
# @param [#to_s] name the name of the attribute
|
14
|
+
# @param [#to_s, nil] value the new value or nil to remove it
|
9
15
|
def []=(name, value)
|
10
16
|
name = name.to_s
|
11
17
|
value.nil? ? remove_attribute(name) : attr_set(name, value.to_s)
|
12
18
|
end
|
13
19
|
|
14
|
-
# alias_method :attr_get, :[]
|
15
|
-
# def [](name)
|
16
|
-
# attr_get name.to_s
|
17
|
-
# end
|
18
|
-
|
19
20
|
alias_method :nokogiri_xpath, :xpath
|
21
|
+
# Override Nokogiri's #xpath method to add the ability to use symbols for lookup
|
22
|
+
# and namespace designation
|
20
23
|
def xpath(*paths)
|
21
24
|
paths[0] = paths[0].to_s
|
22
25
|
if paths.size > 1 && (namespaces = paths.pop).is_a?(Hash)
|
@@ -26,6 +29,8 @@ module XML
|
|
26
29
|
end
|
27
30
|
alias_method :find, :xpath
|
28
31
|
|
32
|
+
# Return the first element at a specified xpath
|
33
|
+
# @see #xpath
|
29
34
|
def find_first(*paths)
|
30
35
|
xpath(*paths).first
|
31
36
|
end
|
data/lib/blather/errors.rb
CHANGED
@@ -1,41 +1,50 @@
|
|
1
1
|
module Blather
|
2
2
|
# Main error class
|
3
|
+
# This starts the error hierarchy
|
4
|
+
#
|
5
|
+
# @handler :error
|
3
6
|
class BlatherError < StandardError
|
4
|
-
class_inheritable_array :
|
5
|
-
self.
|
7
|
+
class_inheritable_array :handler_hierarchy
|
8
|
+
self.handler_hierarchy ||= []
|
6
9
|
|
10
|
+
# @private
|
7
11
|
@@handler_list = []
|
8
12
|
|
9
|
-
##
|
10
13
|
# Register the class's handler
|
14
|
+
#
|
15
|
+
# @param [Symbol] handler the handler name
|
11
16
|
def self.register(handler)
|
12
17
|
@@handler_list << handler
|
13
|
-
self.
|
18
|
+
self.handler_hierarchy.unshift handler
|
14
19
|
end
|
15
20
|
|
16
|
-
##
|
17
21
|
# The list of registered handlers
|
22
|
+
#
|
23
|
+
# @return [Array<Symbol>] a list of currently registered handlers
|
18
24
|
def self.handler_list
|
19
25
|
@@handler_list
|
20
26
|
end
|
21
27
|
|
22
28
|
register :error
|
23
29
|
|
30
|
+
# @private
|
24
31
|
# HACK!! until I can refactor the entire Error object model
|
25
|
-
def id
|
32
|
+
def id
|
26
33
|
nil
|
27
34
|
end
|
28
|
-
end
|
35
|
+
end # BlatherError
|
29
36
|
|
30
|
-
##
|
31
37
|
# Used in cases where a stanza only allows specific values for its attributes
|
32
38
|
# and an invalid value is attempted.
|
39
|
+
#
|
40
|
+
# @handler :argument_error
|
33
41
|
class ArgumentError < BlatherError
|
34
42
|
register :argument_error
|
35
|
-
end
|
43
|
+
end # ArgumentError
|
36
44
|
|
37
|
-
##
|
38
45
|
# The stream handler received a response it didn't know how to handle
|
46
|
+
#
|
47
|
+
# @handler :unknown_response_error
|
39
48
|
class UnknownResponse < BlatherError
|
40
49
|
register :unknown_response_error
|
41
50
|
attr_reader :node
|
@@ -43,10 +52,11 @@ module Blather
|
|
43
52
|
def initialize(node)
|
44
53
|
@node = node
|
45
54
|
end
|
46
|
-
end
|
55
|
+
end # UnknownResponse
|
47
56
|
|
48
|
-
##
|
49
57
|
# Something bad happened while parsing the incoming stream
|
58
|
+
#
|
59
|
+
# @handler :parse_error
|
50
60
|
class ParseError < BlatherError
|
51
61
|
register :parse_error
|
52
62
|
attr_reader :message
|
@@ -54,5 +64,6 @@ module Blather
|
|
54
64
|
def initialize(msg)
|
55
65
|
@message = msg.to_s
|
56
66
|
end
|
57
|
-
end
|
58
|
-
|
67
|
+
end # ParseError
|
68
|
+
|
69
|
+
end # Blather
|
@@ -1,25 +1,43 @@
|
|
1
1
|
module Blather
|
2
2
|
|
3
|
+
# General SASL Errors
|
4
|
+
# Check #name for the error name
|
5
|
+
#
|
6
|
+
# @handler :sasl_error
|
3
7
|
class SASLError < BlatherError
|
4
8
|
SASL_ERR_NS = 'urn:ietf:params:xml:ns:xmpp-sasl'
|
5
9
|
|
6
10
|
class_inheritable_accessor :err_name
|
11
|
+
# @private
|
7
12
|
@@registrations = {}
|
8
13
|
|
9
14
|
register :sasl_error
|
10
15
|
|
16
|
+
# Import the stanza
|
17
|
+
#
|
18
|
+
# @param [Blather::XMPPNode] node the error node
|
19
|
+
# @return [Blather::SASLError]
|
11
20
|
def self.import(node)
|
12
21
|
self.new node
|
13
22
|
end
|
14
23
|
|
24
|
+
# Create a new SASLError
|
25
|
+
#
|
26
|
+
# @param [Blather::XMPPNode] node the error node
|
15
27
|
def initialize(node)
|
16
28
|
super()
|
17
29
|
@node = node
|
18
30
|
end
|
19
31
|
|
32
|
+
# The actual error name
|
33
|
+
#
|
34
|
+
# @return [Symbol] a symbol representing the error name
|
20
35
|
def name
|
21
|
-
|
36
|
+
if @node
|
37
|
+
name = @node.find_first('ns:*', :ns => SASL_ERR_NS).element_name
|
38
|
+
name.gsub('-', '_').to_sym
|
39
|
+
end
|
22
40
|
end
|
23
|
-
end
|
41
|
+
end # SASLError
|
24
42
|
|
25
|
-
end
|
43
|
+
end # Blather
|
@@ -1,19 +1,21 @@
|
|
1
1
|
module Blather
|
2
2
|
|
3
|
-
##
|
4
3
|
# Stanza errors
|
5
4
|
# RFC3920 Section 9.3 (http://xmpp.org/rfcs/rfc3920.html#stanzas-error)
|
5
|
+
#
|
6
|
+
# @handler :stanza_error
|
6
7
|
class StanzaError < BlatherError
|
7
8
|
STANZA_ERR_NS = 'urn:ietf:params:xml:ns:xmpp-stanzas'
|
8
|
-
VALID_TYPES = [:cancel, :continue, :modify, :auth, :wait]
|
9
|
+
VALID_TYPES = [:cancel, :continue, :modify, :auth, :wait].freeze
|
9
10
|
|
10
11
|
register :stanza_error
|
11
12
|
|
12
13
|
attr_reader :original, :name, :type, :text, :extras
|
13
14
|
|
14
|
-
|
15
|
-
#
|
16
|
-
#
|
15
|
+
# Factory method for instantiating the proper class for the error
|
16
|
+
#
|
17
|
+
# @param [Blather::XMPPNode] node the error node to import
|
18
|
+
# @return [Blather::StanzaError]
|
17
19
|
def self.import(node)
|
18
20
|
original = node.copy
|
19
21
|
original.remove_child 'error'
|
@@ -30,12 +32,14 @@ class StanzaError < BlatherError
|
|
30
32
|
self.new original, name, type, text, extras
|
31
33
|
end
|
32
34
|
|
33
|
-
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
35
|
+
# Create a new StanzaError
|
36
|
+
#
|
37
|
+
# @param [Blather::XMPPNode] original the original stanza
|
38
|
+
# @param [String] name the error name
|
39
|
+
# @param [#to_s] type the error type as specified in
|
40
|
+
# [RFC3920](http://xmpp.org/rfcs/rfc3920.html#rfc.section.9.3.2)
|
41
|
+
# @param [String, nil] text additional text for the error
|
42
|
+
# @param [Array<Blather::XMPPNode>] extras an array of extra nodes to add
|
39
43
|
def initialize(original, name, type, text = nil, extras = [])
|
40
44
|
@original = original
|
41
45
|
@name = name
|
@@ -44,20 +48,29 @@ class StanzaError < BlatherError
|
|
44
48
|
@extras = extras
|
45
49
|
end
|
46
50
|
|
47
|
-
|
48
|
-
#
|
51
|
+
# Set the error type
|
52
|
+
#
|
53
|
+
# @param [#to_sym] type the new error type. Must be on of
|
54
|
+
# Blather::StanzaError::VALID_TYPES
|
55
|
+
# @see [RFC3920 Section 9.3.2](http://xmpp.org/rfcs/rfc3920.html#rfc.section.9.3.2)
|
49
56
|
def type=(type)
|
50
57
|
type = type.to_sym
|
51
|
-
|
58
|
+
if !VALID_TYPES.include?(type)
|
59
|
+
raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}"
|
60
|
+
end
|
52
61
|
@type = type
|
53
62
|
end
|
54
63
|
|
64
|
+
# The error name
|
65
|
+
#
|
66
|
+
# @return [Symbol]
|
55
67
|
def name
|
56
68
|
@name.gsub('-','_').to_sym
|
57
69
|
end
|
58
70
|
|
59
|
-
##
|
60
71
|
# Creates an XML node from the error
|
72
|
+
#
|
73
|
+
# @return [Blather::XMPPNode]
|
61
74
|
def to_node
|
62
75
|
node = self.original.reply
|
63
76
|
node.type = 'error'
|
@@ -76,16 +89,19 @@ class StanzaError < BlatherError
|
|
76
89
|
node
|
77
90
|
end
|
78
91
|
|
79
|
-
|
80
|
-
#
|
92
|
+
# Convert the object to a proper node then convert it to a string
|
93
|
+
#
|
94
|
+
# @return [String]
|
81
95
|
def to_xml
|
82
96
|
to_node.to_s
|
83
97
|
end
|
84
98
|
|
85
|
-
|
99
|
+
# @private
|
100
|
+
def inspect
|
86
101
|
"Stanza Error (#{@name}): #{self.text} [#{self.extras}]"
|
87
102
|
end
|
88
|
-
|
89
|
-
|
103
|
+
# @private
|
104
|
+
alias_method :to_s, :inspect
|
105
|
+
end # StanzaError
|
90
106
|
|
91
|
-
end
|
107
|
+
end # Blather
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Blather
|
2
2
|
|
3
|
-
##
|
4
3
|
# Stream Errors
|
5
|
-
# RFC3920 Section 9.3
|
4
|
+
# [RFC3920 Section 9.3](http://xmpp.org/rfcs/rfc3920.html#streams-error-rules)
|
5
|
+
#
|
6
|
+
# @handler :stream_error
|
6
7
|
class StreamError < BlatherError
|
7
8
|
STREAM_ERR_NS = 'urn:ietf:params:xml:ns:xmpp-streams'
|
8
9
|
|
@@ -10,9 +11,9 @@ class StreamError < BlatherError
|
|
10
11
|
|
11
12
|
attr_reader :text, :extras
|
12
13
|
|
13
|
-
|
14
|
-
#
|
15
|
-
#
|
14
|
+
# Factory method for instantiating the proper class for the error
|
15
|
+
#
|
16
|
+
# @param [Blather::XMPPNode] node the importable node
|
16
17
|
def self.import(node)
|
17
18
|
name = node.find_first('descendant::*[name()!="text"]', STREAM_ERR_NS).element_name
|
18
19
|
|
@@ -24,23 +25,29 @@ class StreamError < BlatherError
|
|
24
25
|
self.new name, text, extras
|
25
26
|
end
|
26
27
|
|
27
|
-
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
28
|
+
# Create a new Stream Error
|
29
|
+
# [RFC3920 Section 4.7.2](http://xmpp.org/rfcs/rfc3920.html#rfc.section.4.7.2)
|
30
|
+
#
|
31
|
+
# @param [String] name the error name
|
32
|
+
# @param [String, nil] text optional error text
|
33
|
+
# @param [Array<Blather::XMPPNode>] extras an array of extras to attach to the
|
34
|
+
# error
|
32
35
|
def initialize(name, text = nil, extras = [])
|
33
36
|
@name = name
|
34
37
|
@text = text
|
35
38
|
@extras = extras
|
36
39
|
end
|
37
40
|
|
41
|
+
# The error name
|
42
|
+
#
|
43
|
+
# @return [Symbol]
|
38
44
|
def name
|
39
45
|
@name.gsub('-','_').to_sym
|
40
46
|
end
|
41
47
|
|
42
|
-
##
|
43
48
|
# Creates an XML node from the error
|
49
|
+
#
|
50
|
+
# @return [Blather::XMPPNode]
|
44
51
|
def to_node
|
45
52
|
node = XMPPNode.new('stream:error')
|
46
53
|
|
@@ -57,16 +64,19 @@ class StreamError < BlatherError
|
|
57
64
|
node
|
58
65
|
end
|
59
66
|
|
60
|
-
|
61
|
-
#
|
67
|
+
# Convert the object to a proper node then convert it to a string
|
68
|
+
#
|
69
|
+
# @return [String]
|
62
70
|
def to_xml
|
63
71
|
to_node.to_s
|
64
72
|
end
|
65
73
|
|
66
|
-
|
74
|
+
# @private
|
75
|
+
def inspect
|
67
76
|
"Stream Error (#{@name}): #{self.text}" + (self.extras.empty? ? '' : " [#{self.extras}]")
|
68
77
|
end
|
69
|
-
|
70
|
-
|
78
|
+
# @private
|
79
|
+
alias_method :to_s, :inspect
|
80
|
+
end # StreamError
|
71
81
|
|
72
|
-
end
|
82
|
+
end # Blather
|
data/lib/blather/jid.rb
CHANGED
@@ -1,7 +1,48 @@
|
|
1
1
|
module Blather
|
2
2
|
|
3
|
-
|
4
|
-
#
|
3
|
+
# Jabber ID or JID
|
4
|
+
#
|
5
|
+
# See [RFC 3920 Section 3 - Addressing](http://xmpp.org/rfcs/rfc3920.html#addressing)
|
6
|
+
#
|
7
|
+
# An entity is anything that can be considered a network endpoint (i.e., an
|
8
|
+
# ID on the network) and that can communicate using XMPP. All such entities
|
9
|
+
# are uniquely addressable in a form that is consistent with RFC 2396 [URI].
|
10
|
+
# For historical reasons, the address of an XMPP entity is called a Jabber
|
11
|
+
# Identifier or JID. A valid JID contains a set of ordered elements formed
|
12
|
+
# of a domain identifier, node identifier, and resource identifier.
|
13
|
+
#
|
14
|
+
# The syntax for a JID is defined below using the Augmented Backus-Naur Form
|
15
|
+
# as defined in [ABNF]. (The IPv4address and IPv6address rules are defined
|
16
|
+
# in Appendix B of [IPv6]; the allowable character sequences that conform to
|
17
|
+
# the node rule are defined by the Nodeprep profile of [STRINGPREP] as
|
18
|
+
# documented in Appendix A of this memo; the allowable character sequences
|
19
|
+
# that conform to the resource rule are defined by the Resourceprep profile
|
20
|
+
# of [STRINGPREP] as documented in Appendix B of this memo; and the
|
21
|
+
# sub-domain rule makes reference to the concept of an internationalized
|
22
|
+
# domain label as described in [IDNA].)
|
23
|
+
#
|
24
|
+
# jid = [ node "@" ] domain [ "/" resource ]
|
25
|
+
# domain = fqdn / address-literal
|
26
|
+
# fqdn = (sub-domain 1*("." sub-domain))
|
27
|
+
# sub-domain = (internationalized domain label)
|
28
|
+
# address-literal = IPv4address / IPv6address
|
29
|
+
#
|
30
|
+
# All JIDs are based on the foregoing structure. The most common use of this
|
31
|
+
# structure is to identify an instant messaging user, the server to which
|
32
|
+
# the user connects, and the user's connected resource (e.g., a specific
|
33
|
+
# client) in the form of <user@host/resource>. However, node types other
|
34
|
+
# than clients are possible; for example, a specific chat room offered by a
|
35
|
+
# multi-user chat service could be addressed as <room@service> (where "room"
|
36
|
+
# is the name of the chat room and "service" is the hostname of the
|
37
|
+
# multi-user chat service) and a specific occupant of such a room could be
|
38
|
+
# addressed as <room@service/nick> (where "nick" is the occupant's room
|
39
|
+
# nickname). Many other JID types are possible (e.g., <domain/resource>
|
40
|
+
# could be a server-side script or service).
|
41
|
+
#
|
42
|
+
# Each allowable portion of a JID (node identifier, domain identifier, and
|
43
|
+
# resource identifier) MUST NOT be more than 1023 bytes in length, resulting
|
44
|
+
# in a maximum total size (including the '@' and '/' separators) of 3071
|
45
|
+
# bytes.
|
5
46
|
class JID
|
6
47
|
include Comparable
|
7
48
|
|
@@ -11,19 +52,27 @@ module Blather
|
|
11
52
|
:domain,
|
12
53
|
:resource
|
13
54
|
|
14
|
-
##
|
15
|
-
# Create a new JID. If called as new('a@b/c'), parse the string and split (node, domain, resource).
|
16
|
-
# * +node+ - can be any of the following:
|
17
|
-
# * a string representing the JID ("node@domain.tld/resource")
|
18
|
-
# * a JID. in which case nothing will be done and the original JID will be passed back
|
19
|
-
# * a string representing the node
|
20
|
-
# * +domain+ - the domain of the JID
|
21
|
-
# * +resource+ - the resource the connection should be bound to
|
22
55
|
def self.new(node, domain = nil, resource = nil)
|
23
56
|
node.is_a?(JID) ? node : super
|
24
57
|
end
|
25
58
|
|
26
|
-
|
59
|
+
# Create a new JID object
|
60
|
+
#
|
61
|
+
# @overload initialize(jid)
|
62
|
+
# Passes the jid object right back out
|
63
|
+
# @param [Blather::JID] jid a jid object
|
64
|
+
# @overload initialize(jid)
|
65
|
+
# Creates a new JID parsed out of the provided jid
|
66
|
+
# @param [String] jid a jid in the standard format
|
67
|
+
# ("node@domain/resource")
|
68
|
+
# @overload initialize(node, domain = nil, resource = nil)
|
69
|
+
# Creates a new JID
|
70
|
+
# @param [String] node the node of the JID
|
71
|
+
# @param [String, nil] domian the domain of the JID
|
72
|
+
# @param [String, nil] resource the resource of the JID
|
73
|
+
# @raise [ArgumentError] if the parts of the JID are too large (1023 bytes)
|
74
|
+
# @return [Blather::JID] a new jid object
|
75
|
+
def initialize(node, domain = nil, resource = nil)
|
27
76
|
@resource = resource
|
28
77
|
@domain = domain
|
29
78
|
@node = node
|
@@ -35,18 +84,20 @@ module Blather
|
|
35
84
|
@node.downcase! if @node
|
36
85
|
@domain.downcase! if @domain
|
37
86
|
|
38
|
-
raise ArgumentError, 'Node too long'
|
39
|
-
raise ArgumentError, 'Domain too long'
|
40
|
-
raise ArgumentError, 'Resource too long'
|
87
|
+
raise ArgumentError, 'Node too long' if (@node || '').length > 1023
|
88
|
+
raise ArgumentError, 'Domain too long' if (@domain || '').length > 1023
|
89
|
+
raise ArgumentError, 'Resource too long' if (@resource || '').length > 1023
|
41
90
|
end
|
42
91
|
|
43
|
-
|
44
|
-
#
|
92
|
+
# Turn the JID into a string
|
93
|
+
#
|
45
94
|
# * ""
|
46
95
|
# * "domain"
|
47
96
|
# * "node@domain"
|
48
97
|
# * "domain/resource"
|
49
98
|
# * "node@domain/resource"
|
99
|
+
#
|
100
|
+
# @return [String] the JID as a string
|
50
101
|
def to_s
|
51
102
|
s = @domain
|
52
103
|
s = "#{@node}@#{s}" if @node
|
@@ -54,34 +105,38 @@ module Blather
|
|
54
105
|
s
|
55
106
|
end
|
56
107
|
|
57
|
-
##
|
58
108
|
# Returns a new JID with resource removed.
|
109
|
+
#
|
110
|
+
# @return [Blather::JID] a new JID without a resource
|
59
111
|
def stripped
|
60
112
|
dup.strip!
|
61
113
|
end
|
62
114
|
|
63
|
-
##
|
64
115
|
# Removes the resource (sets it to nil)
|
116
|
+
#
|
117
|
+
# @return [Blather::JID] the JID without a resource
|
65
118
|
def strip!
|
66
119
|
@resource = nil
|
67
120
|
self
|
68
121
|
end
|
69
122
|
|
70
|
-
|
71
|
-
# Compare two JIDs,
|
72
|
-
# helpful for sorting etc.
|
123
|
+
# Compare two JIDs, helpful for sorting etc.
|
73
124
|
#
|
74
125
|
# String representations are compared, see JID#to_s
|
126
|
+
#
|
127
|
+
# @param [#to_s] other a JID to comare against
|
128
|
+
# @return [Fixnum<-1, 0, 1>]
|
75
129
|
def <=>(other)
|
76
130
|
to_s <=> other.to_s
|
77
131
|
end
|
78
132
|
alias_method :eql?, :==
|
79
133
|
|
80
|
-
##
|
81
134
|
# Test if JID is stripped
|
135
|
+
#
|
136
|
+
# @return [true, false]
|
82
137
|
def stripped?
|
83
138
|
@resource.nil?
|
84
139
|
end
|
85
|
-
end
|
140
|
+
end # JID
|
86
141
|
|
87
|
-
end
|
142
|
+
end # Blather
|