Exemplo n.º 1
0
    def decode_mms_header(byte_iter):
        """
        Decodes the MMS header pointed by ``byte_iter``

        This method takes into account the assigned number values for MMS
        field names, as specified in [4], section 7.3, table 8.

        From [4], section 7.1::

            MMS-header = MMS-field-name MMS-value
            MMS-field-name = Short-integer
            MMS-value = Bcc-value | Cc-value | Content-location-value | Content-type-value | etc


        :raise wsp_pdu.DecodeError: The MMS field name could not be parsed.
                                    ``byte_iter`` will not be modified.

        :return: The decoded MMS header, in the format:
                 (<str:MMS-field-name>, <str:MMS-value>)
        :rtype: tuple
        """
        # Get the MMS-field-name
        mms_field_name = ''
        preview = byte_iter.preview()
        byte = wsp_pdu.Decoder.decode_short_integer_from_byte(preview)

        if byte in mms_field_names:
            byte_iter.next()
            mms_field_name = mms_field_names[byte][0]
        else:
            byte_iter.reset_preview()
            raise wsp_pdu.DecodeError('Invalid MMS Header: could '
                                      'not decode MMS field name')

        # Now get the MMS-value
        mms_value = ''
        try:
            name = mms_field_names[byte][1]
            mms_value = getattr(MMSDecoder, 'decode_%s' % name)(byte_iter)
        except wsp_pdu.DecodeError as msg:
            raise wsp_pdu.DecodeError('Invalid MMS Header: Could '
                                      'not decode MMS-value: %s' % msg)
        except:
            raise RuntimeError('A fatal error occurred, probably due to an '
                               'unimplemented decoding operation. Tried to '
                               'decode header: %s' % mms_field_name)

        return mms_field_name, mms_value
Exemplo n.º 2
0
    def decode_sender_visibility_value(byte_iter):
        """
        Decodes the sender visibility value pointed by ``byte_iter``

        Defined in [4], section 7.2.22::

           Sender-visibility-value = Hide | Show
           Hide = <Octet 128>
           Show = <Octet 129>

        :raise wsp_pdu.DecodeError: The sender visibility value could not be
                                    parsed. ``byte_iter`` will not be modified
                                    in this case.

        :return: The sender visibility: 'Hide' or 'Show'
        :rtype: str
        """
        byte = byte_iter.preview()
        if byte not in (128, 129):
            byte_iter.reset_preview()
            raise wsp_pdu.DecodeError('Error parsing sender visibility '
                                      'value for byte: %s' % hex(byte))

        byte = byte_iter.next()
        value = 'Hide' if byte == 128 else 'Show'
        return value
Exemplo n.º 3
0
    def decode_boolean_value(byte_iter):
        """
        Decodes the boolean value pointed by ``byte_iter``

        From [4], section 7.2.6::

           Delivery-report-value = Yes | No
           Yes = <Octet 128>
           No = <Octet 129>

        A lot of other yes/no fields use this encoding (read-reply,
        report-allowed, etc)

        :raise wsp_pdu.DecodeError: The boolean value could not be parsed.
                                    ``byte_iter`` will not be modified.

        :return: The value for the field
        :rtype: bool
        """
        byte = byte_iter.preview()
        if byte not in (128, 129):
            byte_iter.reset_preview()
            raise wsp_pdu.DecodeError('Error parsing boolean value '
                                      'for byte: %s' % hex(byte))
        byte = byte_iter.next()
        return byte == 128
Exemplo n.º 4
0
 def decode_delivery_time_value(byte_iter):
     value_length = wsp_pdu.Decoder.decode_value_length(byte_iter)
     token = byte_iter.next()
     value = wsp_pdu.Decoder.decode_long_integer(byte_iter)
     if token == 128:
         token_type = 'absolute'
     elif token == 129:
         token_type = 'relative'
     else:
         raise wsp_pdu.DecodeError('Delivery-Time type token value is undefined'
             ' (%s), should be either 128 or 129' % token)
     return (token_type, value)
Exemplo n.º 5
0
    def decode_priority_value(byte_iter):
        """
        Decode the "Priority" value pointed by ``byte_iter``

        Defined in [4], section 7.2.17

        :raise wsp_pdu.DecodeError: The priority value could not be decoded;
                                    ``byte_iter`` is not modified in this case.

        :return: The decoded priority value
        :rtype: str
        """
        priorities = {128: 'Low', 129: 'Normal', 130: 'High'}

        byte = byte_iter.preview()
        if byte in priorities:
            byte = byte_iter.next()
            return priorities[byte]

        byte_iter.reset_preview()
        raise wsp_pdu.DecodeError('Error parsing Priority value '
                                  'for byte: %s' % byte)
Exemplo n.º 6
0
    def decode_expiry_value(byte_iter):
        """
        Used to decode the "Expiry" MMS header.

        From [4], section 7.2.10::

            Expiry-value = Value-length (Absolute-token Date-value | Relative-token Delta-seconds-value)
            Absolute-token = <Octet 128>
            Relative-token = <Octet 129>

        :raise wsp_pdu.DecodeError: The Expiry-value could not be decoded

        :return: The decoded Expiry-value, either as a date, or as a delta-seconds value
        :rtype: str or int
        """
        value_length = MMSDecoder.decode_value_length(byte_iter)
        token = byte_iter.next()

        if token == 0x80:    # Absolute-token
            return MMSDecoder.decode_date_value(byte_iter)
        elif token == 0x81:  # Relative-token
            return MMSDecoder.decode_delta_seconds_value(byte_iter)

        raise wsp_pdu.DecodeError('Unrecognized token value: %s' % hex(token))