예제 #1
0
def __handleBinary(view, mimePart, parentMIMEContainer, counter, buf, level):
    contype = mimePart.get_content_type()

    if verbose():
        __trace(contype, buf, level)

    """skip AppleDouble resource files per RFC1740"""
    if contype == "application/applefile":
        return

    mimeBinary = Mail.MIMEBinary(view=view)

    """Get the attachments data"""
    body = mimePart.get_payload(decode=1)
    assert body is not None, "__handleBinary body is None"

    mimeBinary.filesize = len(body)
    mimeBinary.filename = __getFileName(mimePart, counter)
    mimeBinary.mimeType = contype

    """Try to figure out what the real mimetype is"""
    if contype == "application/octet-stream" and \
       not mimeBinary.filename.endswith(".bin"):
       result = mimetypes.guess_type(mimeBinary.filename, strict=False)
       if result[0] is not None:
             mimeBinary.mimeType = result[0]

    mimeBinary.body = utils.dataToBinary(mimeBinary, "body", body, mimeBinary.mimeType, 'bz2')

    parentMIMEContainer.mimeParts.append(mimeBinary)
예제 #2
0
def messageObjectToKind(view, messageObject, messageText=None):
    """
    This method converts a email message string to
    a Chandler C{Mail.MailMessage} object

    @param messageObject: A C{email.Message} object representation of a mail message
    @type messageObject: C{email.Message}
    @return: C{Mail.MailMessage}
    """

    assert isinstance(messageObject, Message.Message), \
           "messageObject must be a Python email.Message.Message instance"

    assert len(messageObject.keys()) > 0, \
           "messageObject data is not a valid RFC2882 message"

    assert messageText is None or isinstance(messageText, str), \
           "messageText can either be a string or None"

    m = Mail.MailMessage(view=view)

    """Save the original message text in a text blob"""
    if messageText is None:
        messageText = messageObject.as_string()

    m.rfc2822Message = utils.dataToBinary(m, "rfc2822Message", messageText, \
                                          'message/rfc822', 'bz2')

    counter = utils.Counter()
    bodyBuffer = []
    buf = None

    if verbose():
        if messageObject.has_key("Message-ID"):
            messageId = messageObject["Message-ID"]
        else:
            messageId = "<Unknown Message>"

        buf = ["Message: %s\n-------------------------------" % messageId]

    __parsePart(view, messageObject, m, bodyBuffer, counter, buf)

    """If the message has attachments set hasMimeParts to True"""
    if len(m.mimeParts) > 0:
        m.hasMimeParts = True

    body = (constants.LF.join(bodyBuffer)).replace(constants.CR, constants.EMPTY)

    m.body = utils.unicodeToText(m, "body", body, indexText=False)

    __parseHeaders(view, messageObject, m)

    if verbose():
        logging.warn("\n\n%s\n\n" % '\n'.join(buf))

    return m
예제 #3
0
def kindToMessageText(mailMessage, saveMessage=True):
    """
    This method converts a email message string to
    a Chandler C{Mail.MailMessage} object

    @param messageObject: A C{email.Message} object representation of a mail message
    @type messageObject: C{email.Message}
    @param saveMessage: save the message text converted from the C{email.Message} in the mailMessage.rfc2882Message
                        attribute
    @type saveMessage: C{Boolean}
    @return: C{str}
    """

    assert isinstance(mailMessage, Mail.MailMessageMixin), \
    "mailMessage must be an instance of Kind Mail.MailMessage"

    messageObject = kindToMessageObject(mailMessage)
    messageText   = messageObject.as_string()

    if saveMessage:
        mailMessage.rfc2882Message = utils.dataToBinary(mailMessage, "rfc2822Message", \
                                                        messageText, 'message/rfc822', 'bz2')

    return messageText