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'
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
def command(self): session, config, idx = self.session, self.session.config, self._idx() if self.args and self.args[0].lower() == 'all': reply_all = self.args.pop(0) or True else: reply_all = False refs = [Email(idx, i) for i in self._choose_messages(self.args)] if refs: trees = [ m.evaluate_pgp(m.get_message_tree(), decrypt=True) for m in refs ] ref_ids = [t['headers_lc'].get('message-id') for t in trees] ref_subjs = [t['headers_lc'].get('subject') for t in trees] msg_to = [ t['headers_lc'].get('reply-to', t['headers_lc']['from']) for t in trees ] msg_cc = [] if reply_all: msg_cc += [t['headers_lc'].get('to', '') for t in trees] msg_cc += [t['headers_lc'].get('cc', '') for t in trees] msg_bodies = [] for t in trees: # FIXME: Templates/settings for how we quote replies? text = (('%s wrote:\n' % t['headers_lc']['from']) + ''.join([ p['data'] for p in t['text_parts'] if p['type'] in ('text', 'quote', 'pgpsignedtext', 'pgpsecuretext', 'pgpverifiedtext') ])) msg_bodies.append(text.replace('\n', '\n> ')) local_id, lmbox = config.open_local_mailbox(session) try: email = Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Re: %s' % ref_subjs[-1]), msg_to=msg_to, msg_cc=[r for r in msg_cc if r], msg_references=[i for i in ref_ids if i]) try: idx.add_tag( session, session.config.get_tag_id('Drafts'), msg_idxs=[int(email.get_msg_info(idx.MSG_IDX), 36)], conversation=False) except (TypeError, ValueError, IndexError): self._ignore_exception() except NoFromAddressError: return self._error('You must configure a From address first.') return self._edit_new_messages(session, idx, [email]) else: return self._error('No message found')
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)
def CreateMessage(cls, idx, session, ephemeral=False): if not ephemeral: local_id, lmbox = session.config.open_local_mailbox(session) else: local_id, lmbox = -1, None ephemeral = ['new:mail'] return (Email.Create(idx, local_id, lmbox, save=(not ephemeral), ephemeral_mid=ephemeral and ephemeral[0]), ephemeral)
def CreateReply(cls, idx, session, refs, reply_all=False, ephemeral=False): trees = [ m.evaluate_pgp(m.get_message_tree(), decrypt=True) for m in refs ] ref_ids = [t['headers_lc'].get('message-id') for t in trees] ref_subjs = [t['headers_lc'].get('subject') for t in trees] msg_to = [ cls._add_gpg_key( idx, session, t['headers_lc'].get('reply-to', t['headers_lc']['from'])) for t in trees ] msg_cc_raw = [] if reply_all: msg_cc_raw += [t['headers_lc'].get('to', '') for t in trees] msg_cc_raw += [t['headers_lc'].get('cc', '') for t in trees] msg_cc = [] for hdr in msg_cc_raw: addrs = re.split("[,;]", hdr) for addr in [a.strip() for a in addrs]: if addr: msg_cc.append(cls._add_gpg_key(idx, session, addr)) msg_bodies = [] for t in trees: # FIXME: Templates/settings for how we quote replies? text = ((_('%s wrote:') % t['headers_lc']['from']) + '\n' + ''.join([ p['data'] for p in t['text_parts'] if p['type'] in cls._TEXT_PARTTYPES ])) 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 if reply_all: ephemeral = ['reply-all-%s' % refs[0].msg_mid()] else: ephemeral = ['reply-%s' % refs[0].msg_mid()] return (Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Re: %s' % ref_subjs[-1]), msg_to=msg_to, msg_cc=[r for r in msg_cc if r], msg_references=[i for i in ref_ids if i], save=(not ephemeral), ephemeral_mid=ephemeral and ephemeral[0]), ephemeral)
def CreateForward(cls, idx, session, refs, with_atts=False, 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 if msg_atts: ephemeral = ['forward-att:%s' % refs[0].msg_mid()] else: ephemeral = ['forward:%s' % refs[0].msg_mid()] email = Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Fwd: %s' % ref_subjs[-1]), save=(not ephemeral), ephemeral_mid=ephemeral and ephemeral[0]) if msg_atts: msg = email.get_msg() for att in msg_atts: msg.attach(att) email.update_from_msg(session, msg) return email, ephemeral
def CreateMessage(cls, idx, session, cid=None, ephemeral=False): if not ephemeral: local_id, lmbox = session.config.open_local_mailbox(session) else: local_id, lmbox = -1, None ephemeral = ['new-mail'] return (Email.Create(idx, local_id, lmbox, save=(not ephemeral), msg_text=(cid and cls._get_canned(idx, cid) or ''), ephemeral_mid=ephemeral and ephemeral[0]), ephemeral)
def command(self): session, config, idx = self.session, self.session.config, self._idx() if self.args and self.args[0].lower().startswith('att'): with_atts = self.args.pop(0) or True else: with_atts = False refs = [Email(idx, i) for i in self._choose_messages(self.args)] if refs: 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 self._TEXT_PARTTYPES ]) msg_bodies.append(text) if with_atts: for att in t['attachments']: if att['mimetype'] not in self._ATT_MIMETYPES: msg_atts.append(att['part']) local_id, lmbox = config.open_local_mailbox(session) email = Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Fwd: %s' % ref_subjs[-1])) if msg_atts: msg = email.get_msg() for att in msg_atts: msg.attach(att) email.update_from_msg(msg) if self.BLANK_TAG: self._tag_emails([email], self.BLANK_TAG) return self._edit_messages([email]) else: return self._error('No message found')
def command(self): session, config, idx = self.session, self.session.config, self._idx() if self.args and self.args[0].lower() == 'all': reply_all = self.args.pop(0) or True else: reply_all = False refs = [Email(idx, i) for i in self._choose_messages(self.args)] if refs: trees = [ m.evaluate_pgp(m.get_message_tree(), decrypt=True) for m in refs ] ref_ids = [t['headers_lc'].get('message-id') for t in trees] ref_subjs = [t['headers_lc'].get('subject') for t in trees] msg_to = [ t['headers_lc'].get('reply-to', t['headers_lc']['from']) for t in trees ] msg_cc = [] if reply_all: msg_cc += [t['headers_lc'].get('to', '') for t in trees] msg_cc += [t['headers_lc'].get('cc', '') for t in trees] msg_bodies = [] for t in trees: # FIXME: Templates/settings for how we quote replies? text = (('%s wrote:\n' % t['headers_lc']['from']) + ''.join([ p['data'] for p in t['text_parts'] if p['type'] in self._TEXT_PARTTYPES ])) msg_bodies.append(text.replace('\n', '\n> ')) local_id, lmbox = config.open_local_mailbox(session) try: email = Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Re: %s' % ref_subjs[-1]), msg_to=msg_to, msg_cc=[r for r in msg_cc if r], msg_references=[i for i in ref_ids if i]) if self.BLANK_TAG: self._tag_emails([email], self.BLANK_TAG) except NoFromAddressError: return self._error('You must configure a From address first.') return self._edit_messages([email]) else: return self._error('No message found')
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)
def command(self): session, idx = self.session, self._idx() email_updates = self._get_email_updates(idx, create=True) if email_updates and email_updates[0][0]: return self._error('Please use update for editing messages') else: update_string = email_updates and email_updates[0][1] or None local_id, lmbox = session.config.open_local_mailbox(session) emails = [Email.Create(idx, local_id, lmbox)] if self.BLANK_TAG: self._tag_emails(emails, self.BLANK_TAG) if update_string: for email in emails: email.update_from_string(update_string) return self._edit_messages(emails, new=True)
def command(self): if 'mid' in self.data: return self._error('Please use update for editing messages') session, idx = self.session, self._idx() local_id, lmbox = session.config.open_local_mailbox(session) emails = [Email.Create(idx, local_id, lmbox)] if self.BLANK_TAG: self._tag_emails(emails, self.BLANK_TAG) for email in emails: email_updates = self._get_email_updates(idx, [email]) update_string = email_updates and email_updates[0][1] if update_string: email.update_from_string(update_string) return self._edit_messages(emails, new=True)
def command(self): session, config, idx = self.session, self.session.config, self._idx() if self.args: emails = [Email(idx, i) for i in self._choose_messages(self.args)] else: local_id, lmbox = config.open_local_mailbox(session) emails = [Email.Create(idx, local_id, lmbox)] try: msg_idxs = [ int(e.get_msg_info(idx.MSG_IDX), 36) for e in emails ] idx.add_tag(session, session.config.get_tag_id('Drafts'), msg_idxs=msg_idxs, conversation=False) except (TypeError, ValueError, IndexError): self._ignore_exception() return self._edit_new_messages(session, idx, emails)
def command(self): session, config, idx = self.session, self.session.config, self._idx() if self.args and self.args[0].lower().startswith('att'): with_atts = self.args.pop(0) or True else: with_atts = False refs = [Email(idx, i) for i in self._choose_messages(self.args)] if refs: 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 ('text', 'quote', 'pgpsignedtext', 'pgpsecuretext', 'pgpverifiedtext') ]) msg_bodies.append(text) if with_atts: for att in t['attachments']: if att['mimetype'] not in ( 'application/pgp-signature', ): msg_atts.append(att['part']) local_id, lmbox = config.open_local_mailbox(session) email = Email.Create(idx, local_id, lmbox, msg_text='\n\n'.join(msg_bodies), msg_subject=('Fwd: %s' % ref_subjs[-1])) if msg_atts: msg = email.get_msg() for att in msg_atts: msg.attach(att) email.update_from_msg(msg) try: idx.add_tag( session, session.config.get_tag_id('Drafts'), msg_idxs=[int(email.get_msg_info(idx.MSG_IDX), 36)], conversation=False) except (TypeError, ValueError, IndexError): self._ignore_exception() return self._edit_new_messages(session, idx, [email]) else: return self._error('No message found')