nametrans_imap_to_utf8.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. convert_utf7_to_utf8 used in nametrans
  3. Main code: Rodolfo García Peñas (kix) @thekix
  4. Updated regex by @dnebauer
  5. Please, check https://github.com/OfflineIMAP/offlineimap3/issues/23
  6. for more info.
  7. """
  8. import re
  9. def convert_utf7_to_utf8(str_imap):
  10. """
  11. This function converts an IMAP_UTF-7 string object to UTF-8.
  12. It first replaces the ampersand (&) character with plus character (+)
  13. in the cases of UTF-7 character and then decode the string to utf-8.
  14. If the str_imap string is already UTF-8, return it.
  15. For example, "abc&AK4-D" is translated to "abc+AK4-D"
  16. and then, to "abc@D"
  17. Example code:
  18. my_string = "abc&AK4-D"
  19. print(convert_utf7_to_utf8(my_string))
  20. Args:
  21. bytes_imap: IMAP UTF7 string
  22. Returns: UTF-8 string
  23. Source: https://github.com/OfflineIMAP/offlineimap3/issues/23
  24. """
  25. try:
  26. str_utf7 = re.sub(r'&(\w{3}\-)', '+\\1', str_imap)
  27. str_utf8 = str_utf7.encode('utf-8').decode('utf_7')
  28. return str_utf8
  29. except UnicodeDecodeError:
  30. # error decoding because already utf-8, so return original string
  31. return str_imap