Class: Blather::JID

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/blather/jid.rb

Overview

Jabber ID or JID

See [RFC 3920 Section 3 - Addressing](xmpp.org/rfcs/rfc3920.html#addressing)

An entity is anything that can be considered a network endpoint (i.e., an ID on the network) and that can communicate using XMPP. All such entities are uniquely addressable in a form that is consistent with RFC 2396 [URI]. For historical reasons, the address of an XMPP entity is called a Jabber Identifier or JID. A valid JID contains a set of ordered elements formed of a domain identifier, node identifier, and resource identifier.

The syntax for a JID is defined below using the Augmented Backus-Naur Form as defined in [ABNF]. (The IPv4address and IPv6address rules are defined in Appendix B of [IPv6]; the allowable character sequences that conform to the node rule are defined by the Nodeprep profile of [STRINGPREP] as documented in Appendix A of this memo; the allowable character sequences that conform to the resource rule are defined by the Resourceprep profile of [STRINGPREP] as documented in Appendix B of this memo; and the sub-domain rule makes reference to the concept of an internationalized domain label as described in [IDNA].)

jid             = [ node "@" ] domain [ "/" resource ]
domain          = fqdn / address-literal
fqdn            = (sub-domain 1*("." sub-domain))
sub-domain      = (internationalized domain label)
address-literal = IPv4address / IPv6address

All JIDs are based on the foregoing structure. The most common use of this structure is to identify an instant messaging user, the server to which the user connects, and the user’s connected resource (e.g., a specific client) in the form of <user@host/resource>. However, node types other than clients are possible; for example, a specific chat room offered by a multi-user chat service could be addressed as <room@service> (where “room” is the name of the chat room and “service” is the hostname of the multi-user chat service) and a specific occupant of such a room could be addressed as <room@service/nick> (where “nick” is the occupant’s room nickname). Many other JID types are possible (e.g., <domain/resource> could be a server-side script or service).

Each allowable portion of a JID (node identifier, domain identifier, and resource identifier) MUST NOT be more than 1023 bytes in length, resulting in a maximum total size (including the ‘@’ and ‘/’ separators) of 3071 bytes.

Constant Summary collapse

PATTERN =
/^(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?$/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid) ⇒ Blather::JID #initialize(jid) ⇒ Blather::JID #initialize(node, domain = nil, resource = nil) ⇒ Blather::JID

Create a new JID object

Overloads:

  • #initialize(jid) ⇒ Blather::JID

    Passes the jid object right back out

    Parameters:

  • #initialize(jid) ⇒ Blather::JID

    Creates a new JID parsed out of the provided jid (“node@domain/resource”)

    Parameters:

    • jid (String)

      a jid in the standard format

  • #initialize(node, domain = nil, resource = nil) ⇒ Blather::JID

    Creates a new JID

    Parameters:

    • node (String)

      the node of the JID

    • domian (String, nil)

      the domain of the JID

    • resource (String, nil) (defaults to: nil)

      the resource of the JID

Raises:

  • (ArgumentError)

    if the parts of the JID are too large (1023 bytes)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blather/jid.rb', line 75

def initialize(node, domain = nil, resource = nil)
  @resource = resource
  @domain = domain
  @node = node

  if @domain.nil? && @resource.nil?
    @node, @domain, @resource = @node.to_s.scan(PATTERN).first
  end

  @node.downcase!   if @node
  @domain.downcase! if @domain

  raise ArgumentError, 'Node too long' if (@node || '').length > 1023
  raise ArgumentError, 'Domain too long' if (@domain || '').length > 1023
  raise ArgumentError, 'Resource too long' if (@resource || '').length > 1023
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



51
52
53
# File 'lib/blather/jid.rb', line 51

def domain
  @domain
end

#nodeObject (readonly)

Returns the value of attribute node.



51
52
53
# File 'lib/blather/jid.rb', line 51

def node
  @node
end

#resourceObject (readonly)

Returns the value of attribute resource.



51
52
53
# File 'lib/blather/jid.rb', line 51

def resource
  @resource
end

Class Method Details

.new(node, domain = nil, resource = nil) ⇒ Object



55
56
57
# File 'lib/blather/jid.rb', line 55

def self.new(node, domain = nil, resource = nil)
  node.is_a?(JID) ? node : super
end

Instance Method Details

#<=>(other) ⇒ Fixnum<-1, 0, 1>

Compare two JIDs, helpful for sorting etc.

String representations are compared, see JID#to_s

Parameters:

  • other (#to_s)

    a JID to comare against

Returns:

  • (Fixnum<-1, 0, 1>)


129
130
131
# File 'lib/blather/jid.rb', line 129

def <=>(other)
  to_s <=> other.to_s
end

#strip!Blather::JID

Removes the resource (sets it to nil)

Returns:



118
119
120
121
# File 'lib/blather/jid.rb', line 118

def strip!
  @resource = nil
  self
end

#strippedBlather::JID

Returns a new JID with resource removed.

Returns:



111
112
113
# File 'lib/blather/jid.rb', line 111

def stripped
  dup.strip!
end

#stripped?true, false

Test if JID is stripped

Returns:

  • (true, false)


137
138
139
# File 'lib/blather/jid.rb', line 137

def stripped?
  @resource.nil?
end

#to_sString

Turn the JID into a string

  • “”

  • “domain”

  • “node@domain”

  • “domain/resource”

  • “node@domain/resource”

Returns:

  • (String)

    the JID as a string



101
102
103
104
105
106
# File 'lib/blather/jid.rb', line 101

def to_s
  s = @domain
  s = "#{@node}@#{s}" if @node
  s = "#{s}/#{@resource}" if @resource
  s
end