2002-06-19 06:09:11 +02:00
|
|
|
# IMAP utility module
|
2015-01-01 21:41:11 +01:00
|
|
|
# Copyright (C) 2002-2015 John Goerzen & contributors
|
2002-06-19 06:09:11 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-19 06:09:11 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-19 06:09:11 +02:00
|
|
|
|
2011-03-11 22:13:21 +01:00
|
|
|
import re
|
|
|
|
import string
|
2017-10-02 01:06:18 +02:00
|
|
|
import binascii
|
|
|
|
import codecs
|
2011-01-05 17:00:55 +01:00
|
|
|
from offlineimap.ui import getglobalui
|
2012-02-05 12:17:02 +01:00
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
|
2012-11-28 18:29:23 +01:00
|
|
|
## Globals
|
|
|
|
|
|
|
|
# Message headers that use space as the separator (for label storage)
|
|
|
|
SPACE_SEPARATED_LABEL_HEADERS = ('X-Label', 'Keywords')
|
|
|
|
|
2015-08-29 14:53:20 +02:00
|
|
|
# Find the modified UTF-7 shifts of an international mailbox name.
|
|
|
|
MUTF7_SHIFT_RE = re.compile(r'&[^-]*-|\+')
|
|
|
|
|
2012-11-28 18:29:23 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __debug(*args):
|
2002-10-01 20:57:56 +02:00
|
|
|
msg = []
|
|
|
|
for arg in args:
|
|
|
|
msg.append(str(arg))
|
2011-01-05 17:00:55 +01:00
|
|
|
getglobalui().debug('imap', " ".join(msg))
|
2002-06-19 06:09:11 +02:00
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
def dequote(s):
|
2011-09-26 15:14:10 +02:00
|
|
|
"""Takes string which may or may not be quoted and unquotes it.
|
2002-06-19 06:09:11 +02:00
|
|
|
|
2011-09-26 15:14:10 +02:00
|
|
|
It only considers double quotes. This function does NOT consider
|
2015-01-01 21:41:11 +01:00
|
|
|
parenthised lists to be quoted."""
|
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
if s and s.startswith('"') and s.endswith('"'):
|
|
|
|
s = s[1:-1] # Strip off the surrounding quotes.
|
|
|
|
s = s.replace('\\"', '"')
|
|
|
|
s = s.replace('\\\\', '\\')
|
|
|
|
return s
|
2002-06-19 06:09:11 +02:00
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
def quote(s):
|
2012-10-16 20:20:35 +02:00
|
|
|
"""Takes an unquoted string and quotes it.
|
|
|
|
|
|
|
|
It only adds double quotes. This function does NOT consider
|
2015-01-01 21:41:11 +01:00
|
|
|
parenthised lists to be quoted."""
|
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
s = s.replace('"', '\\"')
|
|
|
|
s = s.replace('\\', '\\\\')
|
|
|
|
return '"%s"'% s
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
def flagsplit(s):
|
2011-08-16 12:16:46 +02:00
|
|
|
"""Converts a string of IMAP flags to a list
|
|
|
|
|
|
|
|
:returns: E.g. '(\\Draft \\Deleted)' returns ['\\Draft','\\Deleted'].
|
|
|
|
(FLAGS (\\Seen Old) UID 4807) returns
|
|
|
|
['FLAGS,'(\\Seen Old)','UID', '4807']
|
|
|
|
"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
if s[0] != '(' or s[-1] != ')':
|
|
|
|
raise ValueError("Passed s '%s' is not a flag list"% s)
|
|
|
|
return imapsplit(s[1:-1])
|
2002-06-20 08:26:28 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __options2hash(list):
|
2011-08-16 12:16:46 +02:00
|
|
|
"""convert list [1,2,3,4,5,6] to {1:2, 3:4, 5:6}"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
# effectively this does dict(zip(l[::2],l[1::2])), however
|
|
|
|
# measurements seemed to have indicated that the manual variant is
|
|
|
|
# faster for mosly small lists.
|
2002-06-20 08:26:28 +02:00
|
|
|
retval = {}
|
|
|
|
counter = 0
|
|
|
|
while (counter < len(list)):
|
|
|
|
retval[list[counter]] = list[counter + 1]
|
|
|
|
counter += 2
|
2014-03-16 13:27:35 +01:00
|
|
|
__debug("__options2hash returning:", retval)
|
2002-06-20 08:26:28 +02:00
|
|
|
return retval
|
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
def flags2hash(flags):
|
|
|
|
"""Converts IMAP response string from eg IMAP4.fetch() to a hash.
|
2012-01-20 11:01:27 +01:00
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
E.g. '(FLAGS (\\Seen Old) UID 4807)' leads to
|
|
|
|
{'FLAGS': '(\\Seen Old)', 'UID': '4807'}"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
return __options2hash(flagsplit(flags))
|
2002-06-19 07:22:21 +02:00
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
def imapsplit(imapstring):
|
2002-06-19 06:09:11 +02:00
|
|
|
"""Takes a string from an IMAP conversation and returns a list containing
|
|
|
|
its components. One example string is:
|
|
|
|
|
|
|
|
(\\HasNoChildren) "." "INBOX.Sent"
|
|
|
|
|
|
|
|
The result from parsing this will be:
|
|
|
|
|
|
|
|
['(\\HasNoChildren)', '"."', '"INBOX.Sent"']"""
|
2002-10-01 20:57:56 +02:00
|
|
|
|
2016-05-08 15:29:41 +02:00
|
|
|
if not isinstance(imapstring, str):
|
2014-03-16 13:27:35 +01:00
|
|
|
__debug("imapsplit() got a non-string input; working around.")
|
2002-10-07 22:18:02 +02:00
|
|
|
# Sometimes, imaplib will throw us a tuple if the input
|
|
|
|
# contains a literal. See Python bug
|
|
|
|
# #619732 at https://sourceforge.net/tracker/index.php?func=detail&aid=619732&group_id=5470&atid=105470
|
|
|
|
# One example is:
|
|
|
|
# result[0] = '() "\\\\" Admin'
|
|
|
|
# result[1] = ('() "\\\\" {19}', 'Folder\\2')
|
|
|
|
#
|
|
|
|
# This function will effectively get result[0] or result[1], so
|
|
|
|
# if we get the result[1] version, we need to parse apart the tuple
|
|
|
|
# and figure out what to do with it. Each even-numbered
|
|
|
|
# part of it should end with the {} number, and each odd-numbered
|
|
|
|
# part should be directly a part of the result. We'll
|
|
|
|
# artificially quote it to help out.
|
|
|
|
retval = []
|
|
|
|
for i in range(len(imapstring)):
|
|
|
|
if i % 2: # Odd: quote then append.
|
|
|
|
arg = imapstring[i]
|
|
|
|
# Quote code lifted from imaplib
|
|
|
|
arg = arg.replace('\\', '\\\\')
|
|
|
|
arg = arg.replace('"', '\\"')
|
|
|
|
arg = '"%s"' % arg
|
2015-01-08 17:13:33 +01:00
|
|
|
__debug("imapsplit() non-string [%d]: Appending %s"% (i, arg))
|
2002-10-07 22:18:02 +02:00
|
|
|
retval.append(arg)
|
|
|
|
else:
|
|
|
|
# Even -- we have a string that ends with a literal
|
|
|
|
# size specifier. We need to strip off that, then run
|
|
|
|
# what remains through the regular imapsplit parser.
|
|
|
|
# Recursion to the rescue.
|
|
|
|
arg = imapstring[i]
|
2002-10-08 22:18:11 +02:00
|
|
|
arg = re.sub('\{\d+\}$', '', arg)
|
2015-01-08 17:13:33 +01:00
|
|
|
__debug("imapsplit() non-string [%d]: Feeding %s to recursion"%\
|
|
|
|
(i, arg))
|
2002-10-16 01:07:02 +02:00
|
|
|
retval.extend(imapsplit(arg))
|
2014-03-16 13:27:35 +01:00
|
|
|
__debug("imapsplit() non-string: returning %s" % str(retval))
|
2002-10-07 22:18:02 +02:00
|
|
|
return retval
|
2012-01-20 11:01:27 +01:00
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
workstr = imapstring.strip()
|
2002-06-19 06:09:11 +02:00
|
|
|
retval = []
|
|
|
|
while len(workstr):
|
2012-01-20 11:01:27 +01:00
|
|
|
# handle parenthized fragments (...()...)
|
2002-07-23 21:55:18 +02:00
|
|
|
if workstr[0] == '(':
|
2003-01-03 03:05:14 +01:00
|
|
|
rparenc = 1 # count of right parenthesis to match
|
|
|
|
rpareni = 1 # position to examine
|
2012-02-05 13:23:12 +01:00
|
|
|
while rparenc: # Find the end of the group.
|
|
|
|
if workstr[rpareni] == ')': # end of a group
|
|
|
|
rparenc -= 1
|
|
|
|
elif workstr[rpareni] == '(': # start of a group
|
|
|
|
rparenc += 1
|
|
|
|
rpareni += 1 # Move to next character.
|
2002-07-23 21:55:18 +02:00
|
|
|
parenlist = workstr[0:rpareni]
|
|
|
|
workstr = workstr[rpareni:].lstrip()
|
2002-06-19 06:09:11 +02:00
|
|
|
retval.append(parenlist)
|
|
|
|
elif workstr[0] == '"':
|
2012-01-20 11:01:27 +01:00
|
|
|
# quoted fragments '"...\"..."'
|
2014-03-16 13:27:35 +01:00
|
|
|
(quoted, rest) = __split_quoted(workstr)
|
2013-09-19 20:56:55 +02:00
|
|
|
retval.append(quoted)
|
|
|
|
workstr = rest
|
2002-06-19 06:09:11 +02:00
|
|
|
else:
|
2016-05-08 15:51:59 +02:00
|
|
|
splits = None
|
|
|
|
# Python2
|
|
|
|
if hasattr(string, 'split'):
|
|
|
|
splits = string.split(workstr, maxsplit = 1)
|
|
|
|
# Python3
|
|
|
|
else:
|
|
|
|
splits = str.split(workstr, maxsplit = 1)
|
2002-07-23 21:55:18 +02:00
|
|
|
splitslen = len(splits)
|
|
|
|
# The unquoted word is splits[0]; the remainder is splits[1]
|
|
|
|
if splitslen == 2:
|
|
|
|
# There's an unquoted word, and more string follows.
|
|
|
|
retval.append(splits[0])
|
|
|
|
workstr = splits[1] # split will have already lstripped it
|
|
|
|
continue
|
|
|
|
elif splitslen == 1:
|
|
|
|
# We got a last unquoted word, but nothing else
|
|
|
|
retval.append(splits[0])
|
|
|
|
# Nothing remains. workstr would be ''
|
|
|
|
break
|
|
|
|
elif splitslen == 0:
|
|
|
|
# There was not even an unquoted word.
|
|
|
|
break
|
2002-06-19 06:09:11 +02:00
|
|
|
return retval
|
2012-01-20 11:01:27 +01:00
|
|
|
|
2010-12-08 18:46:46 +01:00
|
|
|
flagmap = [('\\Seen', 'S'),
|
|
|
|
('\\Answered', 'R'),
|
|
|
|
('\\Flagged', 'F'),
|
|
|
|
('\\Deleted', 'T'),
|
|
|
|
('\\Draft', 'D')]
|
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
def flagsimap2maildir(flagstring):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Convert string '(\\Draft \\Deleted)' into a flags set(DR)."""
|
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
retval = set()
|
|
|
|
imapflaglist = flagstring[1:-1].split()
|
2010-12-08 18:46:46 +01:00
|
|
|
for imapflag, maildirflag in flagmap:
|
2011-08-16 12:16:46 +02:00
|
|
|
if imapflag in imapflaglist:
|
|
|
|
retval.add(maildirflag)
|
2002-06-20 09:40:29 +02:00
|
|
|
return retval
|
|
|
|
|
2015-11-20 20:09:10 +01:00
|
|
|
def flagsimap2keywords(flagstring):
|
|
|
|
"""Convert string '(\\Draft \\Deleted somekeyword otherkeyword)' into a
|
|
|
|
keyword set (somekeyword otherkeyword)."""
|
|
|
|
|
|
|
|
imapflagset = set(flagstring[1:-1].split())
|
|
|
|
serverflagset = set([flag for (flag, c) in flagmap])
|
|
|
|
return imapflagset - serverflagset
|
|
|
|
|
2010-12-08 18:46:46 +01:00
|
|
|
def flagsmaildir2imap(maildirflaglist):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Convert set of flags ([DR]) into a string '(\\Deleted \\Draft)'."""
|
|
|
|
|
2002-06-20 09:40:29 +02:00
|
|
|
retval = []
|
2010-12-08 18:46:46 +01:00
|
|
|
for imapflag, maildirflag in flagmap:
|
|
|
|
if maildirflag in maildirflaglist:
|
|
|
|
retval.append(imapflag)
|
2012-02-05 13:17:16 +01:00
|
|
|
return '(' + ' '.join(sorted(retval)) + ')'
|
2002-06-20 09:40:29 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
def uid_sequence(uidlist):
|
|
|
|
"""Collapse UID lists into shorter sequence sets
|
|
|
|
|
2011-09-06 20:15:05 +02:00
|
|
|
[1,2,3,4,5,10,12,13] will return "1:5,10,12:13". This function sorts
|
|
|
|
the list, and only collapses if subsequent entries form a range.
|
2015-01-01 21:41:11 +01:00
|
|
|
:returns: The collapsed UID list as string."""
|
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
def getrange(start, end):
|
2002-07-16 03:46:21 +02:00
|
|
|
if start == end:
|
|
|
|
return(str(start))
|
2015-01-14 22:58:25 +01:00
|
|
|
return "%s:%s"% (start, end)
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
if not len(uidlist): return '' # Empty list, return
|
|
|
|
start, end = None, None
|
|
|
|
retval = []
|
2011-08-30 09:22:33 +02:00
|
|
|
# Force items to be longs and sort them
|
|
|
|
sorted_uids = sorted(map(int, uidlist))
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-30 09:22:33 +02:00
|
|
|
for item in iter(sorted_uids):
|
2011-08-22 12:21:11 +02:00
|
|
|
item = int(item)
|
2011-08-22 12:21:11 +02:00
|
|
|
if start == None: # First item
|
|
|
|
start, end = item, item
|
|
|
|
elif item == end + 1: # Next item in a range
|
|
|
|
end = item
|
|
|
|
else: # Starting a new range
|
|
|
|
retval.append(getrange(start, end))
|
|
|
|
start, end = item, item
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
retval.append(getrange(start, end)) # Add final range/item
|
2002-07-16 03:46:21 +02:00
|
|
|
return ",".join(retval)
|
2013-09-19 20:56:55 +02:00
|
|
|
|
|
|
|
|
2015-01-08 22:08:11 +01:00
|
|
|
def __split_quoted(s):
|
|
|
|
"""Looks for the ending quote character in the string that starts
|
|
|
|
with quote character, splitting out quoted component and the
|
|
|
|
rest of the string (without possible space between these two
|
|
|
|
parts.
|
|
|
|
|
|
|
|
First character of the string is taken to be quote character.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
- "this is \" a test" (\\None) => ("this is \" a test", (\\None))
|
|
|
|
- "\\" => ("\\", )
|
|
|
|
"""
|
|
|
|
|
|
|
|
if len(s) == 0:
|
|
|
|
return ('', '')
|
|
|
|
|
|
|
|
q = quoted = s[0]
|
|
|
|
rest = s[1:]
|
|
|
|
while True:
|
|
|
|
next_q = rest.find(q)
|
|
|
|
if next_q == -1:
|
|
|
|
raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
|
|
|
|
# If quote is preceeded by even number of backslashes,
|
|
|
|
# then it is the ending quote, otherwise the quote
|
|
|
|
# character is escaped by backslash, so we should
|
|
|
|
# continue our search.
|
|
|
|
is_escaped = False
|
|
|
|
i = next_q - 1
|
|
|
|
while i >= 0 and rest[i] == '\\':
|
|
|
|
i -= 1
|
|
|
|
is_escaped = not is_escaped
|
|
|
|
quoted += rest[0:next_q + 1]
|
|
|
|
rest = rest[next_q + 1:]
|
|
|
|
if not is_escaped:
|
|
|
|
return (quoted, rest.lstrip())
|
2012-11-28 18:29:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def format_labels_string(header, labels):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Formats labels for embedding into a message,
|
2012-11-28 18:29:23 +01:00
|
|
|
with format according to header name.
|
2015-01-08 17:13:33 +01:00
|
|
|
|
2012-11-28 18:29:23 +01:00
|
|
|
Headers from SPACE_SEPARATED_LABEL_HEADERS keep space-separated list
|
|
|
|
of labels, the rest uses comma (',') as the separator.
|
|
|
|
|
|
|
|
Also see parse_labels_string() and modify it accordingly
|
2015-01-01 21:41:11 +01:00
|
|
|
if logics here gets changed."""
|
2012-11-28 18:29:23 +01:00
|
|
|
|
|
|
|
if header in SPACE_SEPARATED_LABEL_HEADERS:
|
|
|
|
sep = ' '
|
|
|
|
else:
|
|
|
|
sep = ','
|
|
|
|
|
|
|
|
return sep.join(labels)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_labels_string(header, labels_str):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Parses a string into a set of labels, with a format according to
|
2012-11-28 18:29:23 +01:00
|
|
|
the name of the header.
|
|
|
|
|
|
|
|
See __format_labels_string() for explanation on header handling
|
|
|
|
and keep these two functions synced with each other.
|
|
|
|
|
|
|
|
TODO: add test to ensure that
|
2015-01-01 21:41:11 +01:00
|
|
|
- format_labels_string * parse_labels_string is unity
|
2012-11-28 18:29:23 +01:00
|
|
|
and
|
2015-01-01 21:41:11 +01:00
|
|
|
- parse_labels_string * format_labels_string is unity
|
2012-11-28 18:29:23 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
if header in SPACE_SEPARATED_LABEL_HEADERS:
|
|
|
|
sep = ' '
|
|
|
|
else:
|
|
|
|
sep = ','
|
|
|
|
|
|
|
|
labels = labels_str.strip().split(sep)
|
|
|
|
|
|
|
|
return set([l.strip() for l in labels if l.strip()])
|
|
|
|
|
|
|
|
|
|
|
|
def labels_from_header(header_name, header_value):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Helper that builds label set from the corresponding header value.
|
2012-11-28 18:29:23 +01:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
- header_name: name of the header that keeps labels;
|
|
|
|
- header_value: value of the said header, can be None
|
|
|
|
|
|
|
|
Returns: set of labels parsed from the header (or empty set).
|
|
|
|
"""
|
|
|
|
|
|
|
|
if header_value:
|
|
|
|
labels = parse_labels_string(header_name, header_value)
|
|
|
|
else:
|
|
|
|
labels = set()
|
|
|
|
|
|
|
|
return labels
|
|
|
|
|
2015-08-29 14:53:20 +02:00
|
|
|
|
|
|
|
def decode_mailbox_name(name):
|
|
|
|
"""Decodes a modified UTF-7 mailbox name.
|
|
|
|
|
|
|
|
If the string cannot be decoded, it is returned unmodified.
|
|
|
|
|
|
|
|
See RFC 3501, sec. 5.1.3.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
- name: string, possibly encoded with modified UTF-7
|
|
|
|
|
|
|
|
Returns: decoded UTF-8 string.
|
|
|
|
"""
|
|
|
|
def demodify(m):
|
|
|
|
s = m.group()
|
|
|
|
if s == '+':
|
|
|
|
return '+-'
|
|
|
|
return '+' + s[1:-1].replace(',', '/') + '-'
|
|
|
|
|
|
|
|
ret = MUTF7_SHIFT_RE.sub(demodify, name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return ret.decode('utf-7').encode('utf-8')
|
2016-10-31 15:47:00 +01:00
|
|
|
except (UnicodeDecodeError, UnicodeEncodeError):
|
2015-08-29 14:53:20 +02:00
|
|
|
return name
|
2017-10-02 01:06:18 +02:00
|
|
|
|
|
|
|
# Functionality to convert folder names encoded in IMAP_utf_7 to utf_8.
|
|
|
|
# This is achieved by defining 'imap4_utf_7' as a proper encoding scheme.
|
|
|
|
|
2017-09-26 11:26:33 +02:00
|
|
|
# Public API, to be used in repository definitions
|
|
|
|
|
|
|
|
def IMAP_utf8(foldername):
|
|
|
|
"""Convert IMAP4_utf_7 encoded string to utf-8"""
|
|
|
|
return foldername.decode('imap4-utf-7').encode('utf-8')
|
|
|
|
|
|
|
|
def utf8_IMAP(foldername):
|
|
|
|
"""Convert utf-8 encoded string to IMAP4_utf_7"""
|
|
|
|
return foldername.decode('utf-8').encode('imap4-utf-7')
|
|
|
|
|
|
|
|
# Codec definition
|
|
|
|
|
2017-10-02 01:06:18 +02:00
|
|
|
def modified_base64(s):
|
|
|
|
s = s.encode('utf-16be')
|
|
|
|
return binascii.b2a_base64(s).rstrip('\n=').replace('/', ',')
|
|
|
|
|
|
|
|
def doB64(_in, r):
|
|
|
|
if _in:
|
|
|
|
r.append('&%s-' % modified_base64(''.join(_in)))
|
|
|
|
del _in[:]
|
|
|
|
|
|
|
|
def encoder(s):
|
|
|
|
r = []
|
|
|
|
_in = []
|
|
|
|
for c in s:
|
|
|
|
ordC = ord(c)
|
|
|
|
if 0x20 <= ordC <= 0x25 or 0x27 <= ordC <= 0x7e:
|
|
|
|
doB64(_in, r)
|
|
|
|
r.append(c)
|
|
|
|
elif c == '&':
|
|
|
|
doB64(_in, r)
|
|
|
|
r.append('&-')
|
|
|
|
else:
|
|
|
|
_in.append(c)
|
|
|
|
doB64(_in, r)
|
|
|
|
return (str(''.join(r)), len(s))
|
|
|
|
|
|
|
|
# decoding
|
|
|
|
def modified_unbase64(s):
|
|
|
|
b = binascii.a2b_base64(s.replace(',', '/') + '===')
|
2020-08-28 03:32:43 +02:00
|
|
|
return str(b, 'utf-16be')
|
2017-10-02 01:06:18 +02:00
|
|
|
|
|
|
|
def decoder(s):
|
|
|
|
r = []
|
|
|
|
decode = []
|
|
|
|
for c in s:
|
|
|
|
if c == '&' and not decode:
|
|
|
|
decode.append('&')
|
|
|
|
elif c == '-' and decode:
|
|
|
|
if len(decode) == 1:
|
|
|
|
r.append('&')
|
|
|
|
else:
|
|
|
|
r.append(modified_unbase64(''.join(decode[1:])))
|
|
|
|
decode = []
|
|
|
|
elif decode:
|
|
|
|
decode.append(c)
|
|
|
|
else:
|
|
|
|
r.append(c)
|
|
|
|
|
|
|
|
if decode:
|
|
|
|
r.append(modified_unbase64(''.join(decode[1:])))
|
|
|
|
bin_str = ''.join(r)
|
|
|
|
return (bin_str, len(s))
|
|
|
|
|
|
|
|
class StreamReader(codecs.StreamReader):
|
|
|
|
def decode(self, s, errors='strict'):
|
|
|
|
return decoder(s)
|
|
|
|
|
|
|
|
class StreamWriter(codecs.StreamWriter):
|
|
|
|
def decode(self, s, errors='strict'):
|
|
|
|
return encoder(s)
|
|
|
|
|
|
|
|
def imap4_utf_7(name):
|
|
|
|
if name == 'imap4-utf-7':
|
|
|
|
return (encoder, decoder, StreamReader, StreamWriter)
|
|
|
|
|
|
|
|
|
|
|
|
codecs.register(imap4_utf_7)
|