コード例 #1
0
ファイル: email_message.py プロジェクト: pussbb/pymaillib
def decode_part(part_info, data):
    """Copy/paste from email module because if base64 string does not contains
    '==' if raise a Defect but value is decoded, but you will get original
     message.
    
    :param part_info: 
    :param data: 
    :return: 
    """
    if part_info.encoding == 'quoted-printable':
        return quopri.decodestring(data)
    elif part_info.encoding == 'base64':
        # XXX: this is a bit of a hack; decode_b should probably be factored
        # out somewhere, but I haven't figured out where yet.
        value, defects = decode_b(b''.join(data.splitlines()))
        for defect in defects:
            if isinstance(defect, InvalidBase64CharactersDefect):
                return b''.join(
                    [body_decode(line) for line in data.splitlines()])
        return value
    elif part_info.encoding in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
        in_file = BytesIO(data)
        out_file = BytesIO()
        try:
            uu.decode(in_file, out_file, quiet=True)
            return out_file.getvalue()
        except uu.Error:
            # Some decoding problem
            return data
コード例 #2
0
 def get_payload(self, i=None, decode=False):
     if self.is_multipart():
         if decode:
             return
         if i is None:
             return self._payload
         return self._payload[i]
     if i is not None and not isinstance(self._payload, list):
         raise TypeError('Expected list, got %s' % type(self._payload))
     payload = self._payload
     cte = str(self.get('content-transfer-encoding', '')).lower()
     if isinstance(payload, str):
         if utils._has_surrogates(payload):
             bpayload = payload.encode('ascii', 'surrogateescape')
             if not decode:
                 try:
                     payload = bpayload.decode(
                         self.get_param('charset', 'ascii'), 'replace')
                 except LookupError:
                     payload = bpayload.decode('ascii', 'replace')
                 if decode:
                     try:
                         bpayload = payload.encode('ascii')
                     except UnicodeError:
                         bpayload = payload.encode('raw-unicode-escape')
         elif decode:
             try:
                 bpayload = payload.encode('ascii')
             except UnicodeError:
                 bpayload = payload.encode('raw-unicode-escape')
     if not decode:
         return payload
     if cte == 'quoted-printable':
         return utils._qdecode(bpayload)
     if cte == 'base64':
         (value, defects) = decode_b(b''.join(bpayload.splitlines()))
         for defect in defects:
             self.policy.handle_defect(self, defect)
         return value
     if cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
         in_file = BytesIO(bpayload)
         out_file = BytesIO()
         try:
             uu.decode(in_file, out_file, quiet=True)
             return out_file.getvalue()
         except uu.Error:
             return bpayload
     if isinstance(payload, str):
         return bpayload
     return payload
コード例 #3
0
ファイル: message.py プロジェクト: johndpope/sims4-ai-engine
 def get_payload(self, i=None, decode=False):
     if self.is_multipart():
         if decode:
             return
         if i is None:
             return self._payload
         return self._payload[i]
     if i is not None and not isinstance(self._payload, list):
         raise TypeError('Expected list, got %s' % type(self._payload))
     payload = self._payload
     cte = str(self.get('content-transfer-encoding', '')).lower()
     if isinstance(payload, str):
         if utils._has_surrogates(payload):
             bpayload = payload.encode('ascii', 'surrogateescape')
             if not decode:
                 try:
                     payload = bpayload.decode(self.get_param('charset', 'ascii'), 'replace')
                 except LookupError:
                     payload = bpayload.decode('ascii', 'replace')
                 if decode:
                     try:
                         bpayload = payload.encode('ascii')
                     except UnicodeError:
                         bpayload = payload.encode('raw-unicode-escape')
         elif decode:
             try:
                 bpayload = payload.encode('ascii')
             except UnicodeError:
                 bpayload = payload.encode('raw-unicode-escape')
     if not decode:
         return payload
     if cte == 'quoted-printable':
         return utils._qdecode(bpayload)
     if cte == 'base64':
         (value, defects) = decode_b(b''.join(bpayload.splitlines()))
         for defect in defects:
             self.policy.handle_defect(self, defect)
         return value
     if cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
         in_file = BytesIO(bpayload)
         out_file = BytesIO()
         try:
             uu.decode(in_file, out_file, quiet=True)
             return out_file.getvalue()
         except uu.Error:
             return bpayload
     if isinstance(payload, str):
         return bpayload
     return payload
コード例 #4
0
ファイル: message.py プロジェクト: luw630/ddzserver
    def get_payload(self, i=None, decode=False):
        """Return a reference to the payload.

        The payload will either be a list object or a string.  If you mutate
        the list object, you modify the message's payload in place.  Optional
        i returns that index into the payload.

        Optional decode is a flag indicating whether the payload should be
        decoded or not, according to the Content-Transfer-Encoding header
        (default is False).

        When True and the message is not a multipart, the payload will be
        decoded if this header's value is `quoted-printable' or `base64'.  If
        some other encoding is used, or the header is missing, or if the
        payload has bogus data (i.e. bogus base64 or uuencoded data), the
        payload is returned as-is.

        If the message is a multipart and the decode flag is True, then None
        is returned.
        """
        # Here is the logic table for this code, based on the email5.0.0 code:
        #   i     decode  is_multipart  result
        # ------  ------  ------------  ------------------------------
        #  None   True    True          None
        #   i     True    True          None
        #  None   False   True          _payload (a list)
        #   i     False   True          _payload element i (a Message)
        #   i     False   False         error (not a list)
        #   i     True    False         error (not a list)
        #  None   False   False         _payload
        #  None   True    False         _payload decoded (bytes)
        # Note that Barry planned to factor out the 'decode' case, but that
        # isn't so easy now that we handle the 8 bit data, which needs to be
        # converted in both the decode and non-decode path.
        if self.is_multipart():
            if decode:
                return None
            if i is None:
                return self._payload
            else:
                return self._payload[i]
        # For backward compatibility, Use isinstance and this error message
        # instead of the more logical is_multipart test.
        if i is not None and not isinstance(self._payload, list):
            raise TypeError('Expected list, got %s' % type(self._payload))
        payload = self._payload
        # cte might be a Header, so for now stringify it.
        cte = str(self.get('content-transfer-encoding', '')).lower()
        # payload may be bytes here.
        if isinstance(payload, str):
            if utils._has_surrogates(payload):
                bpayload = payload.encode('ascii', 'surrogateescape')
                if not decode:
                    try:
                        payload = bpayload.decode(
                            self.get_param('charset', 'ascii'), 'replace')
                    except LookupError:
                        payload = bpayload.decode('ascii', 'replace')
            elif decode:
                try:
                    bpayload = payload.encode('ascii')
                except UnicodeError:
                    # This won't happen for RFC compliant messages (messages
                    # containing only ASCII codepoints in the unicode input).
                    # If it does happen, turn the string into bytes in a way
                    # guaranteed not to fail.
                    bpayload = payload.encode('raw-unicode-escape')
        if not decode:
            return payload
        if cte == 'quoted-printable':
            return utils._qdecode(bpayload)
        elif cte == 'base64':
            # XXX: this is a bit of a hack; decode_b should probably be factored
            # out somewhere, but I haven't figured out where yet.
            value, defects = decode_b(b''.join(bpayload.splitlines()))
            for defect in defects:
                self.policy.handle_defect(self, defect)
            return value
        elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
            in_file = BytesIO(bpayload)
            out_file = BytesIO()
            try:
                uu.decode(in_file, out_file, quiet=True)
                return out_file.getvalue()
            except uu.Error:
                # Some decoding problem
                return bpayload
        if isinstance(payload, str):
            return bpayload
        return payload
 def _test(self, source, ex_result, ex_defects=[]):
     result, defects = _ew.decode_b(source)
     self.assertEqual(result, ex_result)
     self.assertDefectsEqual(defects, ex_defects)
コード例 #6
0
ファイル: message.py プロジェクト: AlexHorlenko/ironpython3
    def get_payload(self, i=None, decode=False):
        """Return a reference to the payload.

        The payload will either be a list object or a string.  If you mutate
        the list object, you modify the message's payload in place.  Optional
        i returns that index into the payload.

        Optional decode is a flag indicating whether the payload should be
        decoded or not, according to the Content-Transfer-Encoding header
        (default is False).

        When True and the message is not a multipart, the payload will be
        decoded if this header's value is `quoted-printable' or `base64'.  If
        some other encoding is used, or the header is missing, or if the
        payload has bogus data (i.e. bogus base64 or uuencoded data), the
        payload is returned as-is.

        If the message is a multipart and the decode flag is True, then None
        is returned.
        """
        # Here is the logic table for this code, based on the email5.0.0 code:
        #   i     decode  is_multipart  result
        # ------  ------  ------------  ------------------------------
        #  None   True    True          None
        #   i     True    True          None
        #  None   False   True          _payload (a list)
        #   i     False   True          _payload element i (a Message)
        #   i     False   False         error (not a list)
        #   i     True    False         error (not a list)
        #  None   False   False         _payload
        #  None   True    False         _payload decoded (bytes)
        # Note that Barry planned to factor out the 'decode' case, but that
        # isn't so easy now that we handle the 8 bit data, which needs to be
        # converted in both the decode and non-decode path.
        if self.is_multipart():
            if decode:
                return None
            if i is None:
                return self._payload
            else:
                return self._payload[i]
        # For backward compatibility, Use isinstance and this error message
        # instead of the more logical is_multipart test.
        if i is not None and not isinstance(self._payload, list):
            raise TypeError('Expected list, got %s' % type(self._payload))
        payload = self._payload
        # cte might be a Header, so for now stringify it.
        cte = str(self.get('content-transfer-encoding', '')).lower()
        # payload may be bytes here.
        if isinstance(payload, str):
            if utils._has_surrogates(payload):
                bpayload = payload.encode('ascii', 'surrogateescape')
                if not decode:
                    try:
                        payload = bpayload.decode(self.get_param('charset', 'ascii'), 'replace')
                    except LookupError:
                        payload = bpayload.decode('ascii', 'replace')
            elif decode:
                try:
                    bpayload = payload.encode('ascii')
                except UnicodeError:
                    # This won't happen for RFC compliant messages (messages
                    # containing only ASCII codepoints in the unicode input).
                    # If it does happen, turn the string into bytes in a way
                    # guaranteed not to fail.
                    bpayload = payload.encode('raw-unicode-escape')
        if not decode:
            return payload
        if cte == 'quoted-printable':
            return utils._qdecode(bpayload)
        elif cte == 'base64':
            # XXX: this is a bit of a hack; decode_b should probably be factored
            # out somewhere, but I haven't figured out where yet.
            value, defects = decode_b(b''.join(bpayload.splitlines()))
            for defect in defects:
                self.policy.handle_defect(self, defect)
            return value
        elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
            in_file = BytesIO(bpayload)
            out_file = BytesIO()
            try:
                uu.decode(in_file, out_file, quiet=True)
                return out_file.getvalue()
            except uu.Error:
                # Some decoding problem
                return bpayload
        if isinstance(payload, str):
            return bpayload
        return payload
コード例 #7
0
    def get_payload(self, i=None, decode=False):
        """Return a reference to the payload.

        The payload will either be a list object or a string.  If you mutate
        the list object, you modify the message's payload in place.  Optional
        i returns that index into the payload.

        Optional decode is a flag indicating whether the payload should be
        decoded or not, according to the Content-Transfer-Encoding header
        (default is False).

        When True and the message is not a multipart, the payload will be
        decoded if this header's value is `quoted-printable' or `base64'.  If
        some other encoding is used, or the header is missing, or if the
        payload has bogus data (i.e. bogus base64 or uuencoded data), the
        payload is returned as-is.

        If the message is a multipart and the decode flag is True, then None
        is returned.
        """
        if self.is_multipart():
            if decode:
                return None
            if i is None:
                return self._payload
            else:
                return self._payload[i]
        if i is not None and not isinstance(self._payload, list):
            raise TypeError('Expected list, got %s' % type(self._payload))
        payload = self._payload
        cte = str(self.get('content-transfer-encoding', '')).lower()
        if isinstance(payload, str):
            if utils._has_surrogates(payload):
                bpayload = payload.encode('ascii', 'surrogateescape')
                if not decode:
                    try:
                        payload = bpayload.decode(
                            self.get_param('charset', 'ascii'), 'replace')
                    except LookupError:
                        payload = bpayload.decode('ascii', 'replace')
            elif decode:
                try:
                    bpayload = payload.encode('ascii')
                except UnicodeError:
                    bpayload = payload.encode('raw-unicode-escape')
        if not decode:
            return payload
        if cte == 'quoted-printable':
            return quopri.decodestring(bpayload)
        elif cte == 'base64':
            value, defects = decode_b(b''.join(bpayload.splitlines()))
            for defect in defects:
                self.policy.handle_defect(self, defect)
            return value
        elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
            in_file = BytesIO(bpayload)
            out_file = BytesIO()
            try:
                uu.decode(in_file, out_file, quiet=True)
                return out_file.getvalue()
            except uu.Error:
                return bpayload
        if isinstance(payload, str):
            return bpayload
        return payload
コード例 #8
0
 def _test(self, source, ex_result, ex_defects=[]):
     result, defects = _ew.decode_b(source)
     self.assertEqual(result, ex_result)
     self.assertDefectsEqual(defects, ex_defects)