Example #1
0
    def as_text(self):
        from mailpile.www.jinjaextensions import MailpileCommand as JE

        clen = max(3, len("%d" % len(self.session.results)))
        cfmt = "%%%d.%ds" % (clen, clen)

        term_width = self.session.ui.term.max_width()
        fs_width = int((22 + 53) * (term_width / 79.0))
        f_width = min(32, int(0.30 * fs_width))
        s_width = fs_width - f_width

        text = []
        count = self["stats"]["start"]
        expand_ids = [e.msg_idx_pos for e in self.emails]
        addresses = self.get("data", {}).get("addresses", {})

        for mid in self["thread_ids"]:
            m = self["data"]["metadata"][mid]
            tags = [self["data"]["tags"].get(t) for t in m["tag_tids"]]
            tags = [t for t in tags if t]
            tag_names = [
                t["name"]
                for t in tags
                if not t.get("searched", False) and t.get("label", True) and t.get("display", "") != "invisible"
            ]
            tag_new = [t for t in tags if t.get("type") == "unread"]
            tag_names.sort()
            msg_meta = tag_names and ("  (" + "(".join(tag_names)) or ""

            # FIXME: this is a bit ugly, but useful for development
            es = ["", ""]
            for t in [t["slug"] for t in tags]:
                if t.startswith("mp_enc") and "none" not in t:
                    es[1] = "E"
                if t.startswith("mp_sig") and "none" not in t:
                    es[0] = "S"
            es = "".join([e for e in es if e])
            if es:
                msg_meta = (msg_meta or "  ") + ("[%s]" % es)
            elif msg_meta:
                msg_meta += ")"
            else:
                msg_meta += "  "
            msg_meta += elapsed_datetime(m["timestamp"])

            from_info = m["from"].get("fn") or m["from"].get("email") or "(anonymous)"
            if from_info[:1] in ("<", '"', "'"):
                from_info = from_info[1:]
                if from_info[-1:] in (">", '"', "'"):
                    from_info = from_info[:-1]
            if "@" in from_info and len(from_info) > 18:
                e, d = from_info.split("@", 1)
                if d in ("gmail.com", "yahoo.com", "hotmail.com"):
                    from_info = "%s@%s.." % (e, d[0])
                else:
                    from_info = "%s..@%s" % (e[0], d)

            if not expand_ids:

                def gg(pos):
                    return (pos < 10) and pos or ">"

                thr_mid = m["thread_mid"]
                thread = [ti[0] for ti in self["data"]["threads"][thr_mid]]
                if m["mid"] not in thread:
                    thread.append(m["mid"])
                pos = thread.index(m["mid"]) + 1
                if pos > 1:
                    from_info = "%s>%s" % (gg(pos - 1), from_info)
                else:
                    from_info = "  " + from_info
                if pos < len(thread):
                    from_info = "%s>%s" % (from_info[:20], gg(len(thread) - pos))

            subject = re.sub("^(\\[[^\\]]{6})[^\\]]{3,}\\]\\s*", "\\1..] ", JE._nice_subject(m))
            subject_width = max(1, s_width - (clen + len(msg_meta)))
            subject = self._fix_width(subject, subject_width)
            from_info = self._fix_width(from_info, f_width)

            # sfmt = '%%s%%s' % (subject_width, subject_width)
            # ffmt = ' %%s%%s' % (f_width, f_width)
            tfmt = cfmt + " %s%s%s%s"
            text.append(tfmt % (count, from_info, tag_new and "*" or " ", subject, msg_meta))

            if mid in self["data"].get("messages", {}):
                exp_email = self.emails[expand_ids.index(int(mid, 36))]
                msg_tree = exp_email.get_message_tree()
                text.append("-" * term_width)
                text.append(exp_email.get_editing_string(msg_tree, attachment_headers=False).strip())
                if msg_tree["attachments"]:
                    text.append("\nAttachments:")
                    for a in msg_tree["attachments"]:
                        text.append("%5.5s %s" % ("#%s" % a["count"], a["filename"]))
                text.append("-" * term_width)

            count += 1
        if not count:
            text = ["(No messages found)"]
        return "\n".join(text) + "\n"
Example #2
0
    def as_text(self):
        from mailpile.www.jinjaextensions import MailpileCommand as JE
        clen = max(3, len('%d' % len(self.session.results)))
        cfmt = '%%%d.%ds' % (clen, clen)

        term_width = self.session.ui.term.max_width()
        fs_width = int((22 + 53) * (term_width / 79.0))
        f_width = min(32, int(0.30 * fs_width))
        s_width = fs_width - f_width

        text = []
        count = self['stats']['start']
        expand_ids = [e.msg_idx_pos for e in self.emails]
        addresses = self.get('data', {}).get('addresses', {})

        for mid in self['thread_ids']:
            m = self['data']['metadata'][mid]
            tags = [self['data']['tags'].get(t) for t in m['tag_tids']]
            tags = [t for t in tags if t]
            tag_names = [
                t['name'] for t in tags
                if not t.get('searched', False) and t.get('label', True)
                and t.get('display', '') != 'invisible'
            ]
            tag_new = [t for t in tags if t.get('type') == 'unread']
            tag_names.sort()
            msg_meta = tag_names and ('  (' + '('.join(tag_names)) or ''

            # FIXME: this is a bit ugly, but useful for development
            es = ['', '']
            for t in [t['slug'] for t in tags]:
                if t.startswith('mp_enc') and 'none' not in t:
                    es[1] = 'E'
                if t.startswith('mp_sig') and 'none' not in t:
                    es[0] = 'S'
            es = ''.join([e for e in es if e])
            if es:
                msg_meta = (msg_meta or '  ') + ('[%s]' % es)
            elif msg_meta:
                msg_meta += ')'
            else:
                msg_meta += '  '
            msg_meta += elapsed_datetime(m['timestamp'])

            from_info = (m['from'].get('fn') or m['from'].get('email')
                         or '(anonymous)')
            if from_info[:1] in ('<', '"', '\''):
                from_info = from_info[1:]
                if from_info[-1:] in ('>', '"', '\''):
                    from_info = from_info[:-1]
            if '@' in from_info and len(from_info) > 18:
                e, d = from_info.split('@', 1)
                if d in ('gmail.com', 'yahoo.com', 'hotmail.com'):
                    from_info = '%s@%s..' % (e, d[0])
                else:
                    from_info = '%s..@%s' % (e[0], d)

            if not expand_ids:

                def gg(pos):
                    return (pos < 10) and pos or '>'

                thr_mid = m['thread_mid']
                thread = [ti[0] for ti in self['data']['threads'][thr_mid]]
                if m['mid'] not in thread:
                    thread.append(m['mid'])
                pos = thread.index(m['mid']) + 1
                if pos > 1:
                    from_info = '%s>%s' % (gg(pos - 1), from_info)
                else:
                    from_info = '  ' + from_info
                if pos < len(thread):
                    from_info = '%s>%s' % (from_info[:20],
                                           gg(len(thread) - pos))

            subject = re.sub('^(\\[[^\\]]{6})[^\\]]{3,}\\]\\s*', '\\1..] ',
                             JE._nice_subject(m))
            subject_width = max(1, s_width - (clen + len(msg_meta)))
            subject = self._fix_width(subject, subject_width)
            from_info = self._fix_width(from_info, f_width)

            #sfmt = '%%s%%s' % (subject_width, subject_width)
            #ffmt = ' %%s%%s' % (f_width, f_width)
            tfmt = cfmt + ' %s%s%s%s'
            text.append(
                tfmt %
                (count, from_info, tag_new and '*' or ' ', subject, msg_meta))

            if mid in self['data'].get('messages', {}):
                exp_email = self.emails[expand_ids.index(int(mid, 36))]
                msg_tree = exp_email.get_message_tree()
                text.append('-' * term_width)
                text.append(
                    exp_email.get_editing_string(
                        msg_tree, attachment_headers=False).strip())
                if msg_tree['attachments']:
                    text.append('\nAttachments:')
                    for a in msg_tree['attachments']:
                        text.append('%5.5s %s' %
                                    ('#%s' % a['count'], a['filename']))
                text.append('-' * term_width)

            count += 1
        if not count:
            text = ['(No messages found)']
        return '\n'.join(text) + '\n'
Example #3
0
    def as_text(self):
        from mailpile.www.jinjaextensions import MailpileCommand as JE
        clen = max(3, len('%d' % len(self.session.results)))
        cfmt = '%%%d.%ds' % (clen, clen)

        term_width = self.session.ui.term.max_width()
        fs_width = int((22 + 53) * (term_width / 79.0))
        f_width = min(32, int(0.30 * fs_width))
        s_width = fs_width - f_width

        text = []
        count = self['stats']['start']
        expand_ids = [e.msg_idx_pos for e in self.emails]
        addresses = self.get('data', {}).get('addresses', {})

        for mid in self['thread_ids']:
            m = self['data']['metadata'][mid]
            tags = [self['data']['tags'].get(t) for t in m['tag_tids']]
            tags = [t for t in tags if t]
            tag_names = [t['name'] for t in tags
                         if not t.get('searched', False)
                         and t.get('label', True)
                         and t.get('display', '') != 'invisible']
            tag_new = [t for t in tags if t.get('type') == 'unread']
            tag_names.sort()
            msg_meta = tag_names and ('  (' + '('.join(tag_names)) or ''

            # FIXME: this is a bit ugly, but useful for development
            es = ['', '']
            for t in [t['slug'] for t in tags]:
                if t.startswith('mp_enc') and 'none' not in t:
                    es[1] = 'E'
                if t.startswith('mp_sig') and 'none' not in t:
                    es[0] = 'S'
            es = ''.join([e for e in es if e])
            if es:
                msg_meta = (msg_meta or '  ') + ('[%s]' % es)
            elif msg_meta:
                msg_meta += ')'
            else:
                msg_meta += '  '
            msg_meta += elapsed_datetime(m['timestamp'])

            from_info = (m['from'].get('fn') or m['from'].get('email')
                         or '(anonymous)')
            if from_info[:1] in ('<', '"', '\''):
                from_info = from_info[1:]
                if from_info[-1:] in ('>', '"', '\''):
                    from_info = from_info[:-1]
            if '@' in from_info and len(from_info) > 18:
                e, d = from_info.split('@', 1)
                if d in ('gmail.com', 'yahoo.com', 'hotmail.com'):
                    from_info = '%s@%s..' % (e, d[0])
                else:
                    from_info = '%s..@%s' % (e[0], d)

            if not expand_ids:
                def gg(pos):
                    return (pos < 10) and pos or '>'
                thr_mid = m['thread_mid']
                thread = [ti[0] for ti in self['data']['threads'][thr_mid]]
                if m['mid'] not in thread:
                    thread.append(m['mid'])
                pos = thread.index(m['mid']) + 1
                if pos > 1:
                    from_info = '%s>%s' % (gg(pos-1), from_info)
                else:
                    from_info = '  ' + from_info
                if pos < len(thread):
                    from_info = '%s>%s' % (from_info[:20], gg(len(thread)-pos))

            subject = re.sub('^(\\[[^\\]]{6})[^\\]]{3,}\\]\\s*', '\\1..] ',
                             JE._nice_subject(m))
            subject_width = max(1, s_width - (clen + len(msg_meta)))
            subject = self._fix_width(subject, subject_width)
            from_info = self._fix_width(from_info, f_width)

            #sfmt = '%%s%%s' % (subject_width, subject_width)
            #ffmt = ' %%s%%s' % (f_width, f_width)
            tfmt = cfmt + ' %s%s%s%s'
            text.append(tfmt % (count, from_info, tag_new and '*' or ' ',
                                subject, msg_meta))

            if mid in self['data'].get('messages', {}):
                exp_email = self.emails[expand_ids.index(int(mid, 36))]
                msg_tree = exp_email.get_message_tree()
                text.append('-' * term_width)
                text.append(exp_email.get_editing_string(msg_tree,
                    attachment_headers=False).strip())
                if msg_tree['attachments']:
                    text.append('\nAttachments:')
                    for a in msg_tree['attachments']:
                        text.append('%5.5s %s' % ('#%s' % a['count'],
                                                  a['filename']))
                text.append('-' * term_width)

            count += 1
        if not count:
            text = ['(No messages found)']
        return '\n'.join(text) + '\n'