jep0106.py :  » Development » xmpp.py » xmpppy-0.5.0rc1 » xmpp » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » xmpp.py 
xmpp.py » xmpppy 0.5.0rc1 » xmpp » jep0106.py

# JID Escaping XEP-0106 for the xmpppy based transports written by Norman Rasmussen

"""This file is the XEP-0106 commands.

Implemented commands as follows:

4.2 Encode : Encoding Transformation
4.3 Decode : Decoding Transformation


"""

xep0106mapping = [
  [' ' ,'20'],
  ['"' ,'22'],
  ['&' ,'26'],
  ['\'','27'],
  ['/' ,'2f'],
  [':' ,'3a'],
  ['<' ,'3c'],
  ['>' ,'3e'],
  ['@' ,'40']]

def JIDEncode(str):
  str = str.replace('\\5c', '\\5c5c')
  for each in xep0106mapping:
    str = str.replace('\\' + each[1], '\\5c' + each[1])
  for each in xep0106mapping:
    str = str.replace(each[0], '\\' + each[1])
  return str

def JIDDecode(str):
  for each in xep0106mapping:
    str = str.replace('\\' + each[1], each[0])
  return str.replace('\\5c', '\\')

if __name__ == "__main__":
  def test(before,valid):
    during = JIDEncode(before)
    after = JIDDecode(during)
    if during == valid and after == before:
      print 'PASS Before: ' + before
      print 'PASS During: ' + during
    else:
      print 'FAIL Before: ' + before
      print 'FAIL During: ' + during
      print 'FAIL After : ' + after
    print

  test('jid escaping',r'jid\20escaping')
  test(r'\3and\2is\5@example.com',r'\5c3and\2is\5\40example.com')
  test(r'\3catsand\2catsis\5cats@example.com',r'\5c3catsand\2catsis\5c5cats\40example.com')
  test(r'\2plus\2is\4',r'\2plus\2is\4')
  test(r'foo\bar',r'foo\bar')
  test(r'foob\41r',r'foob\41r')
  test('here\'s_a wild_&_/cr%zy/_address@example.com',r'here\27s_a\20wild_\26_\2fcr%zy\2f_address\40example.com')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.