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

UCC git Repository :: git.ucc.asn.au