1 # =XMPP4R - XMPP Library for Ruby
2 # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
3 # Website::http://home.gna.org/xmpp4r/
7 # The JID class represents a Jabber Identifier as described by
10 # Note that you can use JIDs also for Sorting, Hash keys, ...
14 PATTERN = /^(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?$/
20 USE_STRINGPREP = false
24 # Create a new JID. If called as new('a@b/c'), parse the string and
25 # split (node, domain, resource)
26 def initialize(node = "", domain = nil, resource = nil)
30 if @domain.nil? and @resource.nil? and @node
31 @node, @domain, @resource = @node.to_s.scan(PATTERN).first
35 @node = IDN::Stringprep.nodeprep(@node) if @node
36 @domain = IDN::Stringprep.nameprep(@domain) if @domain
37 @resource = IDN::Stringprep.resourceprep(@resource) if @resource
39 @node.downcase! if @node
40 @domain.downcase! if @domain
43 raise ArgumentError, 'Node too long' if (@node || '').length > 1023
44 raise ArgumentError, 'Domain too long' if (@domain || '').length > 1023
45 raise ArgumentError, 'Resource too long' if (@resource || '').length > 1023
49 # Returns a string representation of the JID
54 # * "node@domain/resource"
57 s = "#{@node}@#{s}" if @node
58 s += "/#{@resource}" if @resource
63 # Returns a new JID with resource removed.
66 JID::new(@node, @domain)
68 alias_method :bare, :strip
71 # Removes the resource (sets it to nil)
77 alias_method :bare!, :strip!
80 # Returns a hash value of the String representation
87 # Ccompare to another JID
89 # String representations are compared, see JID#to_s
95 # Ccompare to another JID
97 # String representations are compared, see JID#to_s
104 # helpful for sorting etc.
106 # String representations are compared, see JID#to_s
120 @node = IDN::Stringprep.nodeprep(@node) if @node
124 # Get the JID's domain
126 return nil if @domain.empty?
130 # Set the JID's domain
134 @domain = IDN::Stringprep.nodeprep(@domain)
138 # Get the JID's resource
143 # Set the JID's resource
147 @resource = IDN::Stringprep.nodeprep(@resource)
153 return jid.to_s.gsub('@', '%')
156 # Test if jid is empty
161 # Test id jid is strepped
165 alias_method :bared?, :stripped?