Exemplo n.º 1
0
 def genlines(msg):
     charset = msg.get_content_charset(config.MESSAGE_CHARSET)
     # Show part(s).
     for (i, mpart, level) in enum_message_parts(msg, favor="text/plain"):
         if i != None and level:
             yield term.color(config.COLOR4MIMETREE, "--- " + get_mime_info(i, mpart))
         # Show the child headers.
         if headerlevel:
             for (h, v) in get_headers(mpart):
                 s = "%s: %s" % (h, rmsp(v))
                 color = config.HEADER_COLOR.get(h.lower(), "")
                 for line in fold_text(term, s, indent2="    "):
                     yield highlight(term, selection, color, line.rstrip())
             yield ""
         # Show the payload.
         if mpart.get_content_maintype() == "text":
             r = u""
             text = get_body_text(mpart, charset)
             if MAX_PP_SIZE < len(text):
                 for line in text.splitlines():
                     yield term.normal(line)
             else:
                 for line in fold_text(term, text):
                     r += line + "\n"
                 for line in highlight(term, selection, "", r).splitlines():
                     yield line
         yield ""
     return
Exemplo n.º 2
0
 def genlines(msg):
     charset = msg.get_content_charset(config.MESSAGE_CHARSET)
     # Show part(s).
     for (i, mpart, level) in enum_message_parts(msg, favor='text/plain'):
         if i != None and level:
             yield term.color(config.COLOR4MIMETREE,
                              '--- ' + get_mime_info(i, mpart))
         # Show the child headers.
         if headerlevel:
             for (h, v) in get_headers(mpart):
                 s = '%s: %s' % (h, rmsp(v))
                 color = config.HEADER_COLOR.get(h.lower(), '')
                 for line in fold_text(term, s, indent2='    '):
                     yield highlight(term, selection, color, line.rstrip())
             yield ''
         # Show the payload.
         if mpart.get_content_maintype() == 'text':
             r = u''
             text = get_body_text(mpart, charset)
             if MAX_PP_SIZE < len(text):
                 for line in text.splitlines():
                     yield term.normal(line)
             else:
                 for line in fold_text(term, text):
                     r += line + '\n'
                 for line in highlight(term, selection, '', r).splitlines():
                     yield line
         yield ''
     return
Exemplo n.º 3
0
def get_editable_string(msg, labels):
    mpart = msg
    if msg.is_multipart():
        mpart = get_message_part(msg, 1)
        if mpart.get_content_maintype() != "text":
            raise MessageStructureError("The first part is not text??.")

    # Construct text.
    text = u""
    for h in config.EDITABLE_HEADERS:
        for v in unicode_getall(msg, h):
            text += u"%s: %s\n" % (h, rmsp(v))
    if labels:
        text += u"Label: %s\n" % get_label_names(labels)
    text += u"\n" + get_body_text(mpart, msg.get_content_charset())
    return text
Exemplo n.º 4
0
def get_editable_string(msg, labels):
    mpart = msg
    if msg.is_multipart():
        mpart = get_message_part(msg, 1)
        if mpart.get_content_maintype() != 'text':
            raise MessageStructureError('The first part is not text??.')

    # Construct text.
    text = u''
    for h in config.EDITABLE_HEADERS:
        for v in unicode_getall(msg, h):
            text += u'%s: %s\n' % (h, rmsp(v))
    if labels:
        text += u'Label: %s\n' % get_label_names(labels)
    text += u'\n' + get_body_text(mpart, msg.get_content_charset())
    return text
Exemplo n.º 5
0
def show_mime_part(term, msg, part, headerlevel=0, charset=None):
    mpart = get_message_part(msg, part)

    # Binary - might get InterfaceError.
    if mpart.get_content_type() != "text/plain":
        content = mpart.get_payload(decode=True)
        term.show_binary(content, mpart.get_content_type())
        return

    # Headers are normally not displayed.
    if 2 <= headerlevel:
        for (h, v) in mpart.items():
            term.display(term.normal("%s: %s\n" % (h, rmsp(v))))
        term.display("\n")

    charset = msg.get_content_charset(charset or config.MESSAGE_CHARSET)
    term.display(term.normal(get_body_text(mpart, charset)))
    return
Exemplo n.º 6
0
def show_mime_part(term, msg, part, headerlevel=0, charset=None):
    mpart = get_message_part(msg, part)

    # Binary - might get InterfaceError.
    if mpart.get_content_type() != 'text/plain':
        content = mpart.get_payload(decode=True)
        term.show_binary(content, mpart.get_content_type())
        return

    # Headers are normally not displayed.
    if 2 <= headerlevel:
        for (h, v) in mpart.items():
            term.display(term.normal('%s: %s\n' % (h, rmsp(v))))
        term.display('\n')

    charset = msg.get_content_charset(charset or config.MESSAGE_CHARSET)
    term.display(term.normal(get_body_text(mpart, charset)))
    return
Exemplo n.º 7
0
def setup_template_string(
    fromaddr, addrs_to, addrs_cc, addrs_bcc, is_group, subject, labels, headermsgs, includemsgs, forwardlocs
):
    width = 80

    def fold_header(items, sep=", "):
        lines = []
        line = ""
        for x in items:
            if not line:
                line = x
            elif width < len(line) + len(sep) + len(x):
                lines.append(line + sep)
                line = "\t" + x
            else:
                line += sep + x
        if line:
            lines.append(line)
        return "\n".join(lines)

    if forwardlocs:
        # Append 'Fwd:' if necessary.
        for m in headermsgs:
            # Take the subject of the first message.
            if not subject and m["subject"]:
                subject = "Fwd: " + unicode_header(m["subject"])
                break
    else:
        # Append 'Re:' if necessary.
        for m in headermsgs:
            # Take the subject if not set.
            if not subject and m["subject"]:
                subject = unicode_header(m["subject"])
                if not re.match("\s*re:", subject, re.I):
                    subject = "Re: " + subject
                break
        # Add extra recipients.
        for m in headermsgs:
            if m["mail-reply-to"]:
                addrs_to.extend(unicode_getall(m, "mail-reply-to"))
            elif m["reply-to"]:
                addrs_to.extend(unicode_getall(m, "reply-to"))
            else:
                addrs_to.append(unicode_header(m["from"]))
            if is_group:
                addrs_cc.extend(formataddr((n, a)) for (n, a) in unicode_getalladdrs(m, "to", "cc"))
    #
    msgids = set()
    refids = set()
    if headermsgs:
        for msg in headermsgs:
            if msg["message-id"]:
                msgids.add(msg["message-id"])
            if msg["references"]:
                for x in msg.get_all("references"):
                    refids.update(get_msgids(x))
    #
    lines = []
    lines.append("From: %s" % fromaddr)
    lines.append("To: %s" % fold_header(addrs_to))
    lines.append("Cc: %s" % fold_header(addrs_cc))
    lines.append("Bcc: %s" % fold_header(addrs_bcc))
    lines.append("Label: %s" % get_label_names(labels))
    lines.append("Subject: %s" % subject)
    if headermsgs:
        lines.append("References: %s" % fold_header(msgids.union(refids), " "))
        lines.append("In-Reply-To: %s" % fold_header(msgids, " "))
    if forwardlocs:
        lines.append("X-Forward-Msg: %s" % (", ".join(forwardlocs)))
    lines.append("")
    if includemsgs:
        for msg in includemsgs:
            from0 = unicode_header(msg["from"]) or "unknown person"
            date0 = unicode_header(msg["date"]) or "unknown date"
            subject0 = unicode_header(msg["subject"]) or "unknown subject"
            quoted_lines = []
            charset = msg.get_content_charset()
            for (i, mpart, level) in enum_message_parts(msg, favor="text/plain"):
                if mpart.get_content_maintype() == "text":
                    text = get_body_text(mpart, charset)
                    quoted_lines.extend(text.splitlines())
            lines.extend(config.quotemsg(from0, date0, subject0, quoted_lines))

    return "\n".join(lines) + "\n"
Exemplo n.º 8
0
def setup_template_string(fromaddr, addrs_to, addrs_cc, addrs_bcc, is_group,
                          subject, labels, headermsgs, includemsgs,
                          forwardlocs):
    width = 80

    def fold_header(items, sep=', '):
        lines = []
        line = ''
        for x in items:
            if not line:
                line = x
            elif width < len(line) + len(sep) + len(x):
                lines.append(line + sep)
                line = '\t' + x
            else:
                line += sep + x
        if line:
            lines.append(line)
        return '\n'.join(lines)

    if forwardlocs:
        # Append 'Fwd:' if necessary.
        for m in headermsgs:
            # Take the subject of the first message.
            if not subject and m['subject']:
                subject = 'Fwd: ' + unicode_header(m['subject'])
                break
    else:
        # Append 'Re:' if necessary.
        for m in headermsgs:
            # Take the subject if not set.
            if not subject and m['subject']:
                subject = unicode_header(m['subject'])
                if not re.match('\s*re:', subject, re.I):
                    subject = 'Re: ' + subject
                break
        # Add extra recipients.
        for m in headermsgs:
            if m['mail-reply-to']:
                addrs_to.extend(unicode_getall(m, 'mail-reply-to'))
            elif m['reply-to']:
                addrs_to.extend(unicode_getall(m, 'reply-to'))
            else:
                addrs_to.append(unicode_header(m['from']))
            if is_group:
                addrs_cc.extend(
                    formataddr((n, a))
                    for (n, a) in unicode_getalladdrs(m, 'to', 'cc'))
    #
    msgids = set()
    refids = set()
    if headermsgs:
        for msg in headermsgs:
            if msg['message-id']:
                msgids.add(msg['message-id'])
            if msg['references']:
                for x in msg.get_all('references'):
                    refids.update(get_msgids(x))
    #
    lines = []
    lines.append('From: %s' % fromaddr)
    lines.append('To: %s' % fold_header(addrs_to))
    lines.append('Cc: %s' % fold_header(addrs_cc))
    lines.append('Bcc: %s' % fold_header(addrs_bcc))
    lines.append('Label: %s' % get_label_names(labels))
    lines.append('Subject: %s' % subject)
    if headermsgs:
        lines.append('References: %s' % fold_header(msgids.union(refids), ' '))
        lines.append('In-Reply-To: %s' % fold_header(msgids, ' '))
    if forwardlocs:
        lines.append('X-Forward-Msg: %s' % (', '.join(forwardlocs)))
    lines.append('')
    if includemsgs:
        for msg in includemsgs:
            from0 = unicode_header(msg['from']) or 'unknown person'
            date0 = unicode_header(msg['date']) or 'unknown date'
            subject0 = unicode_header(msg['subject']) or 'unknown subject'
            quoted_lines = []
            charset = msg.get_content_charset()
            for (i, mpart, level) in enum_message_parts(msg,
                                                        favor='text/plain'):
                if mpart.get_content_maintype() == 'text':
                    text = get_body_text(mpart, charset)
                    quoted_lines.extend(text.splitlines())
            lines.extend(config.quotemsg(from0, date0, subject0, quoted_lines))

    return '\n'.join(lines) + '\n'