root/xmppony/jep0106.py @ 1:0603290d9eb7

Revision 1:0603290d9eb7, 2.2 kB (checked in by elghinn, 18 months ago)

* welcome new headers (licence + copyright)

Line 
1# -*- coding: utf-8 -*-
2
3#  xmppony
4#  jep106.py
5
6#  Copyright (c) 2009 Anaël Verrier
7#  Copyright (c) 2003-2009 Alexey "Snake" Nezhdanov, Norman Rasmussen
8
9#  This program is free software; you can redistribute it and/or modify
10#  it under the terms of the GNU General Public License as published by
11#  the Free Software Foundation; version 3 only.
12
13#  This program is distributed in the hope that it will be useful,
14#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#  GNU General Public License for more details.
17
18#  You should have received a copy of the GNU General Public License
19#  along with this program; if not, write to the Free Software
20#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22"""This file is the XEP-0106 commands.
23
24Implemented commands as follows:
25
264.2 Encode : Encoding Transformation
274.3 Decode : Decoding Transformation
28
29
30"""
31
32xep0106mapping = [
33        [' ' ,'20'],
34        ['"' ,'22'],
35        ['&' ,'26'],
36        ['\'','27'],
37        ['/' ,'2f'],
38        [':' ,'3a'],
39        ['<' ,'3c'],
40        ['>' ,'3e'],
41        ['@' ,'40']]
42
43def JIDEncode(str):
44        str = str.replace('\\5c', '\\5c5c')
45        for each in xep0106mapping:
46                str = str.replace('\\' + each[1], '\\5c' + each[1])
47        for each in xep0106mapping:
48                str = str.replace(each[0], '\\' + each[1])
49        return str
50
51def JIDDecode(str):
52        for each in xep0106mapping:
53                str = str.replace('\\' + each[1], each[0])
54        return str.replace('\\5c', '\\')
55
56if __name__ == "__main__":
57        def test(before,valid):
58                during = JIDEncode(before)
59                after = JIDDecode(during)
60                if during == valid and after == before:
61                        print 'PASS Before: ' + before
62                        print 'PASS During: ' + during
63                else:
64                        print 'FAIL Before: ' + before
65                        print 'FAIL During: ' + during
66                        print 'FAIL After : ' + after
67                print
68
69        test('jid escaping',r'jid\20escaping')
70        test(r'\3and\2is\5@example.com',r'\5c3and\2is\5\40example.com')
71        test(r'\3catsand\2catsis\5cats@example.com',r'\5c3catsand\2catsis\5c5cats\40example.com')
72        test(r'\2plus\2is\4',r'\2plus\2is\4')
73        test(r'foo\bar',r'foo\bar')
74        test(r'foob\41r',r'foob\41r')
75        test('here\'s_a wild_&_/cr%zy/_address@example.com',r'here\27s_a\20wild_\26_\2fcr%zy\2f_address\40example.com')
Note: See TracBrowser for help on using the browser.