2010-08-04 04:33:28 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
2011-05-20 21:42:40 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-08-04 04:33:28 +00:00
|
|
|
|
2010-07-27 01:13:09 +00:00
|
|
|
class JID(object):
|
2010-08-04 04:33:28 +00:00
|
|
|
"""
|
|
|
|
A representation of a Jabber ID, or JID.
|
|
|
|
|
|
|
|
Each JID may have three components: a user, a domain, and an optional
|
|
|
|
resource. For example: user@domain/resource
|
|
|
|
|
|
|
|
When a resource is not used, the JID is called a bare JID.
|
|
|
|
The JID is a full JID otherwise.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
jid -- Alias for 'full'.
|
|
|
|
full -- The value of the full JID.
|
|
|
|
bare -- The value of the bare JID.
|
|
|
|
user -- The username portion of the JID.
|
|
|
|
domain -- The domain name portion of the JID.
|
|
|
|
server -- Alias for 'domain'.
|
|
|
|
resource -- The resource portion of the JID.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
reset -- Use a new JID value.
|
|
|
|
regenerate -- Recreate the JID from its components.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, jid):
|
|
|
|
"""Initialize a new JID"""
|
|
|
|
self.reset(jid)
|
|
|
|
|
|
|
|
def reset(self, jid):
|
|
|
|
"""
|
|
|
|
Start fresh from a new JID string.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid - The new JID value.
|
|
|
|
"""
|
2011-05-20 21:42:40 +00:00
|
|
|
if isinstance(jid, JID):
|
|
|
|
jid = jid.full
|
|
|
|
self._full = self._jid = jid
|
2010-08-04 04:33:28 +00:00
|
|
|
self._domain = None
|
|
|
|
self._resource = None
|
|
|
|
self._user = None
|
|
|
|
self._bare = None
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
|
|
|
Handle getting the JID values, using cache if available.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- One of: user, server, domain, resource,
|
|
|
|
full, or bare.
|
|
|
|
"""
|
|
|
|
if name == 'resource':
|
2010-10-24 22:22:41 +00:00
|
|
|
if self._resource is None and '/' in self._jid:
|
2010-08-04 04:33:28 +00:00
|
|
|
self._resource = self._jid.split('/', 1)[-1]
|
2010-08-21 22:48:43 +00:00
|
|
|
return self._resource or ""
|
2010-08-04 04:33:28 +00:00
|
|
|
elif name == 'user':
|
|
|
|
if self._user is None:
|
|
|
|
if '@' in self._jid:
|
|
|
|
self._user = self._jid.split('@', 1)[0]
|
|
|
|
else:
|
|
|
|
self._user = self._user
|
2010-08-21 22:48:43 +00:00
|
|
|
return self._user or ""
|
2010-10-14 23:34:16 +00:00
|
|
|
elif name in ('server', 'domain', 'host'):
|
2010-08-04 04:33:28 +00:00
|
|
|
if self._domain is None:
|
|
|
|
self._domain = self._jid.split('@', 1)[-1].split('/', 1)[0]
|
2010-08-21 22:48:43 +00:00
|
|
|
return self._domain or ""
|
2011-01-13 15:21:20 +00:00
|
|
|
elif name in ('full', 'jid'):
|
2010-08-21 22:48:43 +00:00
|
|
|
return self._jid or ""
|
2010-08-04 04:33:28 +00:00
|
|
|
elif name == 'bare':
|
|
|
|
if self._bare is None:
|
|
|
|
self._bare = self._jid.split('/', 1)[0]
|
2010-08-21 22:48:43 +00:00
|
|
|
return self._bare or ""
|
2010-08-04 04:33:28 +00:00
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
"""
|
|