Esempio n. 1
0
    def apply(self, ui):
        # abort if command unset
        if not self.cmdlist:
            ui.notify(self.noop_msg, priority="error")
            return

        # get messages to pipe
        if self.whole_thread:
            thread = ui.current_buffer.get_selected_thread()
            if not thread:
                return
            to_print = thread.get_messages().keys()
        else:
            to_print = [ui.current_buffer.get_selected_message()]

        # ask for confirmation if needed
        if self.confirm_msg:
            if (yield ui.choice(self.confirm_msg, select="yes", cancel="no")) == "no":
                return

        # prepare message sources
        mailstrings = []
        if self.ids:
            mailstrings = [e.get_message_id() for e in to_print]
        else:
            mails = [m.get_email() for m in to_print]
            if self.decode:
                for mail in mails:
                    headertext = extract_headers(mail)
                    bodytext = extract_body(mail)
                    msg = "%s\n\n%s" % (headertext, bodytext)
                    mailstrings.append(msg.encode("utf-8"))
            else:
                mailstrings = [e.as_string() for e in mails]
        if not self.separately:
            mailstrings = ["\n\n".join(mailstrings)]

        # do teh monkey
        for mail in mailstrings:
            ui.logger.debug("%s" % mail)
            out, err, retval = helper.call_cmd(self.cmdlist, stdin=mail)
            if err:
                ui.notify(err, priority="error")
                return

        # display 'done' message
        if self.done_msg:
            ui.notify(self.done_msg)
Esempio n. 2
0
    def apply(self, ui):
        # abort if command unset
        if not self.cmd:
            ui.notify(self.noop_msg, priority='error')
            return

        # get messages to pipe
        if self.whole_thread:
            thread = ui.current_buffer.get_selected_thread()
            if not thread:
                return
            to_print = thread.get_messages().keys()
        else:
            to_print = [ui.current_buffer.get_selected_message()]

        # ask for confirmation if needed
        if self.confirm_msg:
            if (yield ui.choice(self.confirm_msg, select='yes',
                                cancel='no')) == 'no':
                return

        # prepare message sources
        pipestrings = []
        separator = '\n\n'
        logging.debug('PIPETO format')
        logging.debug(self.output_format)
        if self.output_format == 'raw':
            pipestrings = [m.get_email().as_string() for m in to_print]
        elif self.output_format == 'decoded':
            mails = [m.get_email() for m in to_print]
            for mail in mails:
                headertext = extract_headers(mail)
                bodytext = extract_body(mail)
                msg = '%s\n\n%s' % (headertext, bodytext)
                pipestrings.append(msg.encode('utf-8'))
        elif self.output_format == 'id':
            pipestrings = [e.get_message_id() for e in to_print]
            separator = '\n'
        elif self.output_format == 'filepath':
            pipestrings = [e.get_filename() for e in to_print]
            separator = '\n'

        if not self.separately:
            pipestrings = [separator.join(pipestrings)]

        # do teh monkey
        for mail in pipestrings:
            if self.background:
                logging.debug('call in background: %s' % str(self.cmd))
                proc = subprocess.Popen(self.cmd,
                                        shell=True, stdin=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                out, err = proc.communicate(mail)
            else:
                logging.debug('stop urwid screen')
                ui.mainloop.screen.stop()
                logging.debug('call: %s' % str(self.cmd))
                proc = subprocess.Popen(self.cmd, shell=True,
                                        stdin=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                out, err = proc.communicate(mail)
                logging.debug('start urwid screen')
                ui.mainloop.screen.start()
            if err:
                ui.notify(err, priority='error')
                return

        # display 'done' message
        if self.done_msg:
            ui.notify(self.done_msg)