From 3473d9db20f0f3f312fa24fbf16440a1bef70fd2 Mon Sep 17 00:00:00 2001 From: Florent Daigniere Date: Fri, 6 Sep 2024 13:56:32 +0200 Subject: [PATCH] fix #2996 (cherry picked from commit 61b444f00f900e7c41a5f7314f6a04be38e87b5e) --- optional/fetchmail/fetchmail.py | 18 +++++++++++++++++- towncrier/newsfragments/2296.bugfix | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 towncrier/newsfragments/2296.bugfix diff --git a/optional/fetchmail/fetchmail.py b/optional/fetchmail/fetchmail.py index 410ce4fb..8e26e82e 100755 --- a/optional/fetchmail/fetchmail.py +++ b/optional/fetchmail/fetchmail.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import base64 import time import os from pathlib import Path @@ -32,6 +33,21 @@ poll "{host}" proto {protocol} port {port} {lmtp} """ +def imaputf7encode(s): + """"Encode a string into RFC2060 aka IMAP UTF7""" + s=s.replace('&','&-') + iters=iter(s) + unipart=out='' + for c in s: + if 0x20<=ord(c)<=0x7f : + if unipart!='' : + out+='&'+base64.b64encode(unipart.encode('utf-16-be')).decode('ascii').rstrip('=')+'-' + unipart='' + out+=c + else : unipart+=c + if unipart!='' : + out+='&'+base64.b64encode(unipart.encode('utf-16-be')).decode('ascii').rstrip('=')+'-' + return out def escape_rc_string(arg): return "".join("\\x%2x" % ord(char) for char in arg) @@ -54,7 +70,7 @@ def run(debug): options = "options antispam 501, 504, 550, 553, 554" options += " ssl" if fetch["tls"] else "" options += " keep" if fetch["keep"] else " fetchall" - folders = "folders %s" % ((','.join('"' + item + '"' for item in fetch['folders'])) if fetch['folders'] else '"INBOX"') + folders = "folders %s" % ((','.join('"' + imaputf7encode(item) + '"' for item in fetch['folders'])) if fetch['folders'] else '"INBOX"') fetchmailrc += RC_LINE.format( user_email=escape_rc_string(fetch["user_email"]), protocol=fetch["protocol"], diff --git a/towncrier/newsfragments/2296.bugfix b/towncrier/newsfragments/2296.bugfix new file mode 100644 index 00000000..493238ff --- /dev/null +++ b/towncrier/newsfragments/2296.bugfix @@ -0,0 +1 @@ +Ensure fetchmail can deal with special characters in folder names