jid.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # -*- test-case-name: twisted.words.test.test_jabberjid -*-
  2. #
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. Jabber Identifier support.
  7. This module provides an object to represent Jabber Identifiers (JIDs) and
  8. parse string representations into them with proper checking for illegal
  9. characters, case folding and canonicalisation through L{stringprep<twisted.words.protocols.jabber.xmpp_stringprep>}.
  10. """
  11. from twisted.python.compat import _PY3, unicode
  12. from twisted.words.protocols.jabber.xmpp_stringprep import nodeprep, resourceprep, nameprep
  13. class InvalidFormat(Exception):
  14. """
  15. The given string could not be parsed into a valid Jabber Identifier (JID).
  16. """
  17. def parse(jidstring):
  18. """
  19. Parse given JID string into its respective parts and apply stringprep.
  20. @param jidstring: string representation of a JID.
  21. @type jidstring: L{unicode}
  22. @return: tuple of (user, host, resource), each of type L{unicode} as
  23. the parsed and stringprep'd parts of the given JID. If the
  24. given string did not have a user or resource part, the respective
  25. field in the tuple will hold L{None}.
  26. @rtype: L{tuple}
  27. """
  28. user = None
  29. host = None
  30. resource = None
  31. # Search for delimiters
  32. user_sep = jidstring.find("@")
  33. res_sep = jidstring.find("/")
  34. if user_sep == -1:
  35. if res_sep == -1:
  36. # host
  37. host = jidstring
  38. else:
  39. # host/resource
  40. host = jidstring[0:res_sep]
  41. resource = jidstring[res_sep + 1:] or None
  42. else:
  43. if res_sep == -1:
  44. # user@host
  45. user = jidstring[0:user_sep] or None
  46. host = jidstring[user_sep + 1:]
  47. else:
  48. if user_sep < res_sep:
  49. # user@host/resource
  50. user = jidstring[0:user_sep] or None
  51. host = jidstring[user_sep + 1:user_sep + (res_sep - user_sep)]
  52. resource = jidstring[res_sep + 1:] or None
  53. else:
  54. # host/resource (with an @ in resource)
  55. host = jidstring[0:res_sep]
  56. resource = jidstring[res_sep + 1:] or None
  57. return prep(user, host, resource)
  58. def prep(user, host, resource):
  59. """
  60. Perform stringprep on all JID fragments.
  61. @param user: The user part of the JID.
  62. @type user: L{unicode}
  63. @param host: The host part of the JID.
  64. @type host: L{unicode}
  65. @param resource: The resource part of the JID.
  66. @type resource: L{unicode}
  67. @return: The given parts with stringprep applied.
  68. @rtype: L{tuple}
  69. """
  70. if user:
  71. try:
  72. user = nodeprep.prepare(unicode(user))
  73. except UnicodeError:
  74. raise InvalidFormat("Invalid character in username")
  75. else:
  76. user = None
  77. if not host:
  78. raise InvalidFormat("Server address required.")
  79. else:
  80. try:
  81. host = nameprep.prepare(unicode(host))
  82. except UnicodeError:
  83. raise InvalidFormat("Invalid character in hostname")
  84. if resource:
  85. try:
  86. resource = resourceprep.prepare(unicode(resource))
  87. except UnicodeError:
  88. raise InvalidFormat("Invalid character in resource")
  89. else:
  90. resource = None
  91. return (user, host, resource)
  92. __internJIDs = {}
  93. def internJID(jidstring):
  94. """
  95. Return interned JID.
  96. @rtype: L{JID}
  97. """
  98. if jidstring in __internJIDs:
  99. return __internJIDs[jidstring]
  100. else:
  101. j = JID(jidstring)
  102. __internJIDs[jidstring] = j
  103. return j
  104. class JID(object):
  105. """
  106. Represents a stringprep'd Jabber ID.
  107. JID objects are hashable so they can be used in sets and as keys in
  108. dictionaries.
  109. """
  110. def __init__(self, str=None, tuple=None):
  111. if not (str or tuple):
  112. raise RuntimeError("You must provide a value for either 'str' or "
  113. "'tuple' arguments.")
  114. if str:
  115. user, host, res = parse(str)
  116. else:
  117. user, host, res = prep(*tuple)
  118. self.user = user
  119. self.host = host
  120. self.resource = res
  121. def userhost(self):
  122. """
  123. Extract the bare JID as a unicode string.
  124. A bare JID does not have a resource part, so this returns either
  125. C{user@host} or just C{host}.
  126. @rtype: L{unicode}
  127. """
  128. if self.user:
  129. return u"%s@%s" % (self.user, self.host)
  130. else:
  131. return self.host
  132. def userhostJID(self):
  133. """
  134. Extract the bare JID.
  135. A bare JID does not have a resource part, so this returns a
  136. L{JID} object representing either C{user@host} or just C{host}.
  137. If the object this method is called upon doesn't have a resource
  138. set, it will return itself. Otherwise, the bare JID object will
  139. be created, interned using L{internJID}.
  140. @rtype: L{JID}
  141. """
  142. if self.resource:
  143. return internJID(self.userhost())
  144. else:
  145. return self
  146. def full(self):
  147. """
  148. Return the string representation of this JID.
  149. @rtype: L{unicode}
  150. """
  151. if self.user:
  152. if self.resource:
  153. return u"%s@%s/%s" % (self.user, self.host, self.resource)
  154. else:
  155. return u"%s@%s" % (self.user, self.host)
  156. else:
  157. if self.resource:
  158. return u"%s/%s" % (self.host, self.resource)
  159. else:
  160. return self.host
  161. def __eq__(self, other):
  162. """
  163. Equality comparison.
  164. L{JID}s compare equal if their user, host and resource parts all
  165. compare equal. When comparing against instances of other types, it
  166. uses the default comparison.
  167. """
  168. if isinstance(other, JID):
  169. return (self.user == other.user and
  170. self.host == other.host and
  171. self.resource == other.resource)
  172. else:
  173. return NotImplemented
  174. def __ne__(self, other):
  175. """
  176. Inequality comparison.
  177. This negates L{__eq__} for comparison with JIDs and uses the default
  178. comparison for other types.
  179. """
  180. result = self.__eq__(other)
  181. if result is NotImplemented:
  182. return result
  183. else:
  184. return not result
  185. def __hash__(self):
  186. """
  187. Calculate hash.
  188. L{JID}s with identical constituent user, host and resource parts have
  189. equal hash values. In combination with the comparison defined on JIDs,
  190. this allows for using L{JID}s in sets and as dictionary keys.
  191. """
  192. return hash((self.user, self.host, self.resource))
  193. def __unicode__(self):
  194. """
  195. Get unicode representation.
  196. Return the string representation of this JID as a unicode string.
  197. @see: L{full}
  198. """
  199. return self.full()
  200. if _PY3:
  201. __str__ = __unicode__
  202. def __repr__(self):
  203. """
  204. Get object representation.
  205. Returns a string that would create a new JID object that compares equal
  206. to this one.
  207. """
  208. return 'JID(%r)' % self.full()