コード例 #1
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_add(msg, obj):
    from email import MIMEMultipart, MIMEText
    validate_message_structure(msg)
    # First, assure it to be multipart.
    if msg.is_multipart():
        # This is already multipart.
        msg.attach(obj)
        return msg

    # If not, wrap the original Message with a new Multipart object.
    def move_headers(destmsg, srcmsg):
        # clear all headers
        for k in srcmsg.keys():
            if k.lower() not in UNTOUCHED_HEADERS:
                del destmsg[k]
        # append headers (with avoiding duplication)
        for (k, v) in srcmsg.items():
            if k.lower() not in UNTOUCHED_HEADERS:
                del srcmsg[k]
                destmsg[k] = v
        return

    # Create a Multipart message object.
    multi = MIMEMultipart.MIMEMultipart()
    # Move the old headers to the new one (EXCEPT mime-related headers).
    move_headers(multi, msg)
    multi.preamble = 'This message contains MIME objects.\n\n'
    # Sometime get_content_charset returns a unicode object!
    # We must coerce it to str.
    charset = msg.get_content_charset(config.MESSAGE_CHARSET)
    # Obtain the original content (which must be text) and reattach it.
    multi.attach(MIMEText.MIMEText(msg.get_payload(), _charset=str(charset)))
    # Attach the object.
    multi.attach(obj)
    return multi
コード例 #2
0
ファイル: message.py プロジェクト: yasusii/shaling
def create_message(corpus, data, msg0=None):
    from email import Charset, Parser, MIMEMessage

    if not isinstance(data, unicode):
        raise TypeError("data must be a unicode.")
    p = Parser.FeedParser()
    p.feed(data)
    msg1 = p.close()
    csmap = Charset.Charset(config.MESSAGE_CHARSET)
    attach_msgs = []
    # Re-encode the headers.
    headers = []
    labels = []
    for (k, v) in msg1.items():
        v = rmsp(v)
        if not v:
            continue
        kl = k.lower()
        if kl == "x-forward-msg":
            attach_msgs.extend(get_numbers(v))
            continue
        if kl == "label":
            labels = v.split(",")
            continue
        headers.append((k.encode("ascii", "strict"), encode_header(v, csmap)))
    # Remove all the existing headers.
    for k in msg1.keys():
        del msg1[k]
    # Reattach the headers.
    for (k, v) in headers:
        msg1[k] = v
    # Change the body.
    data = msg1.get_payload(decode=False)
    try:
        # First try to encode with us-ascii.
        data.encode("ascii", "strict")
        # Succeed.
        msg1.set_charset("ascii")
    except UnicodeError:
        # Re-encode the body.
        if not csmap.output_charset:
            csmap = Charset.Charset("utf-8")
        msg1.set_charset(str(csmap.output_charset))
        data = data.encode(str(csmap.output_codec), "replace")
    msg1.set_payload(data)
    # Attach other messages (for forwarding).
    if attach_msgs:
        for loc in attach_msgs:
            p = Parser.FeedParser()
            p.feed(corpus.get_message(loc))
            msg1 = mime_add(msg1, MIMEMessage.MIMEMessage(p.close()))
    # Integrate other mime objects.
    if msg0 and msg0.is_multipart():
        for obj in msg0.get_payload()[1:]:
            msg1 = mime_add(msg1, obj)
    validate_message_structure(msg1)
    return (msg1, labels)
コード例 #3
0
ファイル: message.py プロジェクト: yasusii/shaling
def create_message(corpus, data, msg0=None):
    from email import Charset, Parser, MIMEMessage
    if not isinstance(data, unicode):
        raise TypeError('data must be a unicode.')
    p = Parser.FeedParser()
    p.feed(data)
    msg1 = p.close()
    csmap = Charset.Charset(config.MESSAGE_CHARSET)
    attach_msgs = []
    # Re-encode the headers.
    headers = []
    labels = []
    for (k, v) in msg1.items():
        v = rmsp(v)
        if not v: continue
        kl = k.lower()
        if kl == 'x-forward-msg':
            attach_msgs.extend(get_numbers(v))
            continue
        if kl == 'label':
            labels = v.split(',')
            continue
        headers.append((k.encode('ascii', 'strict'), encode_header(v, csmap)))
    # Remove all the existing headers.
    for k in msg1.keys():
        del msg1[k]
    # Reattach the headers.
    for (k, v) in headers:
        msg1[k] = v
    # Change the body.
    data = msg1.get_payload(decode=False)
    try:
        # First try to encode with us-ascii.
        data.encode('ascii', 'strict')
        # Succeed.
        msg1.set_charset('ascii')
    except UnicodeError:
        # Re-encode the body.
        if not csmap.output_charset:
            csmap = Charset.Charset('utf-8')
        msg1.set_charset(str(csmap.output_charset))
        data = data.encode(str(csmap.output_codec), 'replace')
    msg1.set_payload(data)
    # Attach other messages (for forwarding).
    if attach_msgs:
        for loc in attach_msgs:
            p = Parser.FeedParser()
            p.feed(corpus.get_message(loc))
            msg1 = mime_add(msg1, MIMEMessage.MIMEMessage(p.close()))
    # Integrate other mime objects.
    if msg0 and msg0.is_multipart():
        for obj in msg0.get_payload()[1:]:
            msg1 = mime_add(msg1, obj)
    validate_message_structure(msg1)
    return (msg1, labels)
コード例 #4
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_del(msg, part):
    validate_message_structure(msg)
    # Get the old object to be removed.
    mpart = get_message_part(msg, part)
    # Get the list of the children of the parent object.
    children = msg.get_payload()
    if mpart not in children:
        raise MessageStructureError("Cannot remove that part.")
    i = children.index(mpart)
    if i == 0:
        raise MessageStructureError("Cannot remove the first text part.")
    # Remove it.
    children.remove(mpart)
    return msg
コード例 #5
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_alter(msg, part, obj):
    validate_message_structure(msg)
    # Get the old object to be replaced.
    mpart = get_message_part(msg, part)
    # Get the list of the children of the parent object.
    children = msg.get_payload()
    if mpart not in children:
        raise MessageStructureError("Cannot change that part.")
    i = children.index(mpart)
    if i == 0:
        raise MessageStructureError("Cannot change the first text part.")
    # Replace it with a new one.
    children[i] = obj
    return msg
コード例 #6
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_del(msg, part):
    validate_message_structure(msg)
    # Get the old object to be removed.
    mpart = get_message_part(msg, part)
    # Get the list of the children of the parent object.
    children = msg.get_payload()
    if mpart not in children:
        raise MessageStructureError('Cannot remove that part.')
    i = children.index(mpart)
    if i == 0:
        raise MessageStructureError('Cannot remove the first text part.')
    # Remove it.
    children.remove(mpart)
    return msg
コード例 #7
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_alter(msg, part, obj):
    validate_message_structure(msg)
    # Get the old object to be replaced.
    mpart = get_message_part(msg, part)
    # Get the list of the children of the parent object.
    children = msg.get_payload()
    if mpart not in children:
        raise MessageStructureError('Cannot change that part.')
    i = children.index(mpart)
    if i == 0:
        raise MessageStructureError('Cannot change the first text part.')
    # Replace it with a new one.
    children[i] = obj
    return msg
コード例 #8
0
ファイル: message.py プロジェクト: yasusii/shaling
def mime_add(msg, obj):
    from email import MIMEMultipart, MIMEText

    validate_message_structure(msg)
    # First, assure it to be multipart.
    if msg.is_multipart():
        # This is already multipart.
        msg.attach(obj)
        return msg

    # If not, wrap the original Message with a new Multipart object.
    def move_headers(destmsg, srcmsg):
        # clear all headers
        for k in srcmsg.keys():
            if k.lower() not in UNTOUCHED_HEADERS:
                del destmsg[k]
        # append headers (with avoiding duplication)
        for (k, v) in srcmsg.items():
            if k.lower() not in UNTOUCHED_HEADERS:
                del srcmsg[k]
                destmsg[k] = v
        return

    # Create a Multipart message object.
    multi = MIMEMultipart.MIMEMultipart()
    # Move the old headers to the new one (EXCEPT mime-related headers).
    move_headers(multi, msg)
    multi.preamble = "This message contains MIME objects.\n\n"
    # Sometime get_content_charset returns a unicode object!
    # We must coerce it to str.
    charset = msg.get_content_charset(config.MESSAGE_CHARSET)
    # Obtain the original content (which must be text) and reattach it.
    multi.attach(MIMEText.MIMEText(msg.get_payload(), _charset=str(charset)))
    # Attach the object.
    multi.attach(obj)
    return multi