Beispiel #1
0
 def test_default_arguments_yield_all_headers(self):
     actual = utils.extract_headers(self.mail)
     # collect all lines until the first empty line, hence all header lines
     expected = []
     for line in self.mailstring.splitlines():
         if not line:
             break
         expected.append(line)
     expected = u'\n'.join(expected) + u'\n'
     self.assertEqual(actual, expected)
Beispiel #2
0
 def test_default_arguments_yield_all_headers(self):
     actual = utils.extract_headers(self.mail)
     # collect all lines until the first empty line, hence all header lines
     expected = []
     for line in self.mailstring.splitlines():
         if not line:
             break
         expected.append(line)
     expected = u'\n'.join(expected) + u'\n'
     self.assertEqual(actual, expected)
Beispiel #3
0
 def test_header_values_are_not_decoded(self):
     actual = utils.extract_headers(self.mail, ['x-quoted'])
     expected = u"x-quoted: param=utf-8''%C3%9Cmlaut; second=plain%C3%9C\n",
     self.assertEqual(actual, expected)
Beispiel #4
0
 def test_case_is_prserved_in_header_keys_but_irelevant(self):
     headers = ['FROM', 'from']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'FROM: me\nfrom: me\n'
     self.assertEqual(actual, expected)
Beispiel #5
0
 def test_headers_can_be_retrieved_multible_times(self):
     headers = ['from', 'from']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'from: me\nfrom: me\n'
     self.assertEqual(actual, expected)
Beispiel #6
0
 def test_multible_headers_can_be_retrieved_in_predevined_order(self):
     headers = ['x-header', 'to', 'x-uppercase']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'x-header: param=one; and=two; or=three\nto: you\n' \
         u'x-uppercase: PARAM1=ONE; PARAM2=TWO\n'
     self.assertEqual(actual, expected)
Beispiel #7
0
 def test_single_headers_can_be_retrieved(self):
     actual = utils.extract_headers(self.mail, ['from'])
     expected = u'from: me\n'
     self.assertEqual(actual, expected)
Beispiel #8
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 == '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'
        else:
            for msg in to_print:
                mail = msg.get_email()
                if self.add_tags:
                    mail['Tags'] = encode_header('Tags',
                                                 ' '.join(msg.get_tags()))
                if self.output_format == 'raw':
                    pipestrings.append(mail.as_string())
                elif self.output_format == 'decoded':
                    headertext = extract_headers(mail)
                    bodytext = extract_body(mail)
                    msgtext = '%s\n\n%s' % (headertext, bodytext)
                    pipestrings.append(msgtext.encode('utf-8'))

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

        # 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,
                                        stdout=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,
                                        stdout=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
            if self.notify_stdout:
                ui.notify(out)

        # display 'done' message
        if self.done_msg:
            ui.notify(self.done_msg)
Beispiel #9
0
 def test_header_values_are_not_decoded(self):
     actual = utils.extract_headers(self.mail, ['x-quoted'])
     expected = u"x-quoted: param=utf-8''%C3%9Cmlaut; second=plain%C3%9C\n",
     self.assertEqual(actual, expected)
Beispiel #10
0
 def test_case_is_prserved_in_header_keys_but_irelevant(self):
     headers = ['FROM', 'from']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'FROM: me\nfrom: me\n'
     self.assertEqual(actual, expected)
Beispiel #11
0
 def test_headers_can_be_retrieved_multible_times(self):
     headers = ['from', 'from']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'from: me\nfrom: me\n'
     self.assertEqual(actual, expected)
Beispiel #12
0
 def test_multible_headers_can_be_retrieved_in_predevined_order(self):
     headers = ['x-header', 'to', 'x-uppercase']
     actual = utils.extract_headers(self.mail, headers)
     expected = u'x-header: param=one; and=two; or=three\nto: you\n' \
         u'x-uppercase: PARAM1=ONE; PARAM2=TWO\n'
     self.assertEqual(actual, expected)
Beispiel #13
0
 def test_single_headers_can_be_retrieved(self):
     actual = utils.extract_headers(self.mail, ['from'])
     expected = u'from: me\n'
     self.assertEqual(actual, expected)
Beispiel #14
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 == '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'
        else:
            for msg in to_print:
                mail = msg.get_email()
                if self.add_tags:
                    mail['Tags'] = encode_header('Tags',
                                                 ' '.join(msg.get_tags()))
                if self.output_format == 'raw':
                    pipestrings.append(mail.as_string())
                elif self.output_format == 'decoded':
                    headertext = extract_headers(mail)
                    bodytext = extract_body(mail)
                    msgtext = '%s\n\n%s' % (headertext, bodytext)
                    pipestrings.append(msgtext.encode('utf-8'))

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

        # 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,
                                        stdout=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,
                                        stdout=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
            if self.notify_stdout:
                ui.notify(out)

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