From 093f3685d1480205f7646e50f0ffe1d470ac4f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20=28kix=29?= Date: Tue, 10 Nov 2020 23:32:31 +0100 Subject: [PATCH] Included contrib nametrans_imap_to_utf8.py Script file to use as nametrans to convert IMAP UTF-7 to UTF-8. Please, take a look of issue #23 https://github.com/OfflineIMAP/offlineimap3/issues/23 Thanks a lot to @dnebauer and @sudipm-mukherjee Closes #23 --- contrib/nametrans_imap_to_utf8.py | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 contrib/nametrans_imap_to_utf8.py diff --git a/contrib/nametrans_imap_to_utf8.py b/contrib/nametrans_imap_to_utf8.py new file mode 100644 index 0000000..59172e1 --- /dev/null +++ b/contrib/nametrans_imap_to_utf8.py @@ -0,0 +1,43 @@ +""" +convert_utf7_to_utf8 used in nametrans + +Main code: Rodolfo García Peñas (kix) @thekix +Updated regex by @dnebauer + +Please, check https://github.com/OfflineIMAP/offlineimap3/issues/23 +for more info. +""" +import re + + +def convert_utf7_to_utf8(str_imap): + """ + This function converts an IMAP_UTF-7 string object to UTF-8. + It first replaces the ampersand (&) character with plus character (+) + in the cases of UTF-7 character and then decode the string to utf-8. + + If the str_imap string is already UTF-8, return it. + + For example, "abc&AK4-D" is translated to "abc+AK4-D" + and then, to "abc@D" + + Example code: + my_string = "abc&AK4-D" + print(convert_utf7_to_utf8(my_string)) + + Args: + bytes_imap: IMAP UTF7 string + + Returns: UTF-8 string + + Source: https://github.com/OfflineIMAP/offlineimap3/issues/23 + + """ + try: + str_utf7 = re.sub(r'&(\w{3}\-)', '+\\1', str_imap) + str_utf8 = str_utf7.encode('utf-8').decode('utf_7') + return str_utf8 + except UnicodeDecodeError: + # error decoding because already utf-8, so return original string + return str_imap +