| 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 | |
|---|
| 24 | Implemented commands as follows: |
|---|
| 25 | |
|---|
| 26 | 4.2 Encode : Encoding Transformation |
|---|
| 27 | 4.3 Decode : Decoding Transformation |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | """ |
|---|
| 31 | |
|---|
| 32 | xep0106mapping = [ |
|---|
| 33 | [' ' ,'20'], |
|---|
| 34 | ['"' ,'22'], |
|---|
| 35 | ['&' ,'26'], |
|---|
| 36 | ['\'','27'], |
|---|
| 37 | ['/' ,'2f'], |
|---|
| 38 | [':' ,'3a'], |
|---|
| 39 | ['<' ,'3c'], |
|---|
| 40 | ['>' ,'3e'], |
|---|
| 41 | ['@' ,'40']] |
|---|
| 42 | |
|---|
| 43 | def 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 | |
|---|
| 51 | def JIDDecode(str): |
|---|
| 52 | for each in xep0106mapping: |
|---|
| 53 | str = str.replace('\\' + each[1], each[0]) |
|---|
| 54 | return str.replace('\\5c', '\\') |
|---|
| 55 | |
|---|
| 56 | if __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') |
|---|