xep0106.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # JID Escaping XEP-0106 for the xmpppy based transports written by Norman Rasmussen
  2. """This file is the XEP-0106 commands.
  3. Implemented commands as follows:
  4. 4.2 Encode : Encoding Transformation
  5. 4.3 Decode : Decoding Transformation
  6. """
  7. xep0106mapping = [
  8. [' ' ,'20'],
  9. ['"' ,'22'],
  10. ['&' ,'26'],
  11. ['\'','27'],
  12. ['/' ,'2f'],
  13. [':' ,'3a'],
  14. ['<' ,'3c'],
  15. ['>' ,'3e'],
  16. ['@' ,'40']]
  17. def JIDEncode(str):
  18. str = str.replace('\\5c', '\\5c5c')
  19. for each in xep0106mapping:
  20. str = str.replace('\\' + each[1], '\\5c' + each[1])
  21. for each in xep0106mapping:
  22. str = str.replace(each[0], '\\' + each[1])
  23. return str
  24. def JIDDecode(str):
  25. for each in xep0106mapping:
  26. str = str.replace('\\' + each[1], each[0])
  27. return str.replace('\\5c', '\\')
  28. if __name__ == "__main__":
  29. def test(before,valid):
  30. during = JIDEncode(before)
  31. after = JIDDecode(during)
  32. if during == valid and after == before:
  33. print(('PASS Before: ' + before))
  34. print(('PASS During: ' + during))
  35. else:
  36. print(('FAIL Before: ' + before))
  37. print(('FAIL During: ' + during))
  38. print(('FAIL After : ' + after))
  39. print()
  40. test('jid escaping',r'jid\20escaping')
  41. test(r'\3and\2is\5@example.com',r'\5c3and\2is\5\40example.com')
  42. test(r'\3catsand\2catsis\5cats@example.com',r'\5c3catsand\2catsis\5c5cats\40example.com')
  43. test(r'\2plus\2is\4',r'\2plus\2is\4')
  44. test(r'foo\bar',r'foo\bar')
  45. test(r'foob\41r',r'foob\41r')
  46. test('here\'s_a wild_&_/cr%zy/_address@example.com',r'here\27s_a\20wild_\26_\2fcr%zy\2f_address\40example.com')