예제 #1
0
파일: compose.py 프로젝트: zsdlove/Mailpile
    def CreateForward(cls,
                      idx,
                      session,
                      refs,
                      msgid,
                      with_atts=False,
                      cid=None,
                      ephemeral=False):
        trees = [
            m.evaluate_pgp(m.get_message_tree(), decrypt=True) for m in refs
        ]
        ref_subjs = [t['headers_lc']['subject'] for t in trees]
        msg_bodies = []
        msg_atts = []
        for t in trees:
            # FIXME: Templates/settings for how we quote forwards?
            text = '-------- Original Message --------\n'
            for h in ('Date', 'Subject', 'From', 'To'):
                v = t['headers_lc'].get(h.lower(), None)
                if v:
                    text += '%s: %s\n' % (h, v)
            text += '\n'
            text += ''.join([
                p['data'] for p in t['text_parts']
                if p['type'] in cls._TEXT_PARTTYPES
            ])
            msg_bodies.append(text)
            if with_atts:
                for att in t['attachments']:
                    if att['mimetype'] not in cls._ATT_MIMETYPES:
                        msg_atts.append(att['part'])

        if not ephemeral:
            local_id, lmbox = session.config.open_local_mailbox(session)
        else:
            local_id, lmbox = -1, None
            fmt = 'forward-att-%s-%s' if msg_atts else 'forward-%s-%s'
            ephemeral = [
                fmt % (msgid[1:-1].replace('@', '_'), refs[0].msg_mid())
            ]

        if cid:
            # FIXME: Instead, we should use placeholders in the template
            #        and insert the quoted bits in the right place (or
            #        nowhere if the template doesn't want them).
            msg_bodies[:0] = [cls._get_canned(idx, cid)]

        email = Email.Create(idx,
                             local_id,
                             lmbox,
                             msg_text='\n\n'.join(msg_bodies),
                             msg_subject=cls.prefix_subject(
                                 ref_subjs[-1], 'Fwd:', cls._FW_REGEXP),
                             msg_id=msgid,
                             msg_atts=msg_atts,
                             save=(not ephemeral),
                             ephemeral_mid=ephemeral and ephemeral[0])

        return email, ephemeral
예제 #2
0
    def CreateReply(cls, idx, session, refs, msgid,
                    reply_all=False, cid=None, ephemeral=False):
        trees = [m.evaluate_pgp(m.get_message_tree(), decrypt=True)
                 for m in refs]

        headers = cls._create_from_to_cc(idx, session, trees)
        if not reply_all and 'cc' in headers:
            del headers['cc']

        ref_ids = [t['headers_lc'].get('message-id') for t in trees]
        ref_subjs = [t['headers_lc'].get('subject') for t in trees]
        msg_bodies = []
        for t in trees:
            # FIXME: Templates/settings for how we quote replies?
            quoted = ''.join([p['data'] for p in t['text_parts']
                              if p['type'] in cls._TEXT_PARTTYPES
                              and p['data']])
            if quoted:
                target_width = session.config.prefs.line_length
                if target_width > 40:
                    quoted = reflow_text(quoted, target_width=target_width-2)
                text = ((_('%s wrote:') % t['headers_lc']['from']) + '\n' +
                        quoted)
                msg_bodies.append('\n\n' + text.replace('\n', '\n> '))

        if not ephemeral:
            local_id, lmbox = session.config.open_local_mailbox(session)
        else:
            local_id, lmbox = -1, None
            fmt = 'reply-all-%s-%s' if reply_all else 'reply-%s-%s'
            ephemeral = [fmt % (msgid[1:-1].replace('@', '_'),
                                refs[0].msg_mid())]

        if 'cc' in headers:
            fmt = _('Composing a reply from %(from)s to %(to)s, cc %(cc)s')
        else:
            fmt = _('Composing a reply from %(from)s to %(to)s')
        session.ui.debug(fmt % headers)

        if cid:
            # FIXME: Instead, we should use placeholders in the template
            #        and insert the quoted bits in the right place (or
            #        nowhere if the template doesn't want them).
            msg_bodies[:0] = [cls._get_canned(idx, cid)]

        return (Email.Create(idx, local_id, lmbox,
                             msg_text='\n\n'.join(msg_bodies),
                             msg_subject=cls.prefix_subject(
                                 ref_subjs[-1], 'Re:', cls._RE_REGEXP),
                             msg_from=headers.get('from', None),
                             msg_to=headers.get('to', []),
                             msg_cc=headers.get('cc', []),
                             msg_references=[i for i in ref_ids if i],
                             msg_id=msgid,
                             save=(not ephemeral),
                             ephemeral_mid=ephemeral and ephemeral[0]),
                ephemeral)
예제 #3
0
 def CreateMessage(cls, idx, session, msgid, cid=None, ephemeral=False):
     if not ephemeral:
         local_id, lmbox = session.config.open_local_mailbox(session)
     else:
         local_id, lmbox = -1, None
         ephemeral = ['new-E-%s-mail' % msgid[1:-1].replace('@', '_')]
     profiles = session.config.vcards.find_vcards([], kinds=['profile'])
     return (Email.Create(idx, local_id, lmbox,
                          save=(not ephemeral),
                          msg_text=(cid and cls._get_canned(idx, cid)
                                    or ''),
                          msg_id=msgid,
                          ephemeral_mid=ephemeral and ephemeral[0],
                          use_default_from=(len(profiles) == 1)),
             ephemeral)
예제 #4
0
 def process_message(self, peer, mailfrom, rcpttos, data):
     # We can assume that the mailfrom and rcpttos have checked out
     # and this message is indeed intended for us. Spool it to disk
     # and add to the index!
     session, config = self.session, self.session.config
     blank_tid = config.get_tags(type='blank')[0]._key
     idx = config.index
     play_nice_with_threads()
     try:
         message = email.parser.Parser().parsestr(data)
         lid, lmbox = config.open_local_mailbox(session)
         e = Email.Create(idx, lid, lmbox, ephemeral_mid=False)
         idx.add_tag(session, blank_tid, msg_idxs=[e.msg_idx_pos],
                     conversation=False)
         e.update_from_msg(session, message)
         idx.remove_tag(session, blank_tid, msg_idxs=[e.msg_idx_pos],
                        conversation=False)
         return None
     except:
         traceback.print_exc()
         return '400 Oops wtf'