IMAP folder path encoding (IMAP UTF-7) for Python

前端 未结 4 1656
感动是毒
感动是毒 2020-12-19 05:15

I would like to know if any \"official\" function/library existed in Python for IMAP4 UTF-7 folder path encoding.

In the imapInstance.list() I get the f

4条回答
  •  情话喂你
    2020-12-19 05:41

    I wrote a very simple IMAP UTF7 python 3 implementation which follows the specification, and it seems to work. ("foo\rbar\n\n\n\r\r" and many other roundtrips, '&BdAF6QXkBdQ-', 'Test&Co', "[Mails].Test&AOk-" and '~peter/mail/&ZeVnLIqe-/&U,BTFw-' behave as expected).

    #works with python 3
    
    import base64
    
    def b64padanddecode(b):
        """Decode unpadded base64 data"""
        b+=(-len(b)%4)*'=' #base64 padding (if adds '===', no valid padding anyway)
        return base64.b64decode(b,altchars='+,',validate=True).decode('utf-16-be')
    
    def imaputf7decode(s):
        """Decode a string encoded according to RFC2060 aka IMAP UTF7.
    
    Minimal validation of input, only works with trusted data"""
        lst=s.split('&')
        out=lst[0]
        for e in lst[1:]:
            u,a=e.split('-',1) #u: utf16 between & and 1st -, a: ASCII chars folowing it
            if u=='' : out+='&'
            else: out+=b64padanddecode(u)
            out+=a
        return out
    
    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    
    

    Given the simplicity of this code, I set it in the public domain, so feel free to use it as you want.

提交回复
热议问题