1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2009 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 def modified_base64(s
):
29 s_utf7
= s
.encode('utf-7')
30 return s_utf7
[1:-1].replace('/', ',')
32 def modified_unbase64(s
):
33 s_utf7
= '+' + s
.replace(',', '/') + '-'
34 return s_utf7
.decode('utf-7')
40 if ord(c
) in (range(0x20, 0x26) + range(0x27, 0x7f)):
42 r
.extend(['&', modified_base64(''.join(_in
)), '-'])
47 r
.extend(['&', modified_base64(''.join(_in
)), '-'])
53 r
.extend(['&', modified_base64(''.join(_in
)), '-'])
54 return (''.join(r
), len(s
))
60 if c
== '&' and not decode
:
62 elif c
== '-' and decode
:
66 r
.append(modified_unbase64(''.join(decode
[1:])))
73 r
.append(modified_unbase64(''.join(decode
[1:])))
74 return (u
''.join(r
), len(s
))
76 class StreamReader(codecs
.StreamReader
):
77 def decode(self
, s
, errors
='strict'):
80 class StreamWriter(codecs
.StreamWriter
):
81 def decode(self
, s
, errors
='strict'):
84 def imap4_utf_7(name
):
85 if name
== 'imap4-utf-7':
86 return (encoder
, decoder
, StreamReader
, StreamWriter
)
88 codecs
.register(imap4_utf_7
)