def process(message): """ The function which actually processes a received command email message. :param message: The received command email message. :type message: ``bytes`` """ assert isinstance(message, six.binary_type), 'Message must be given as bytes' msg = message_from_bytes(message) # msg = message_from_string(message) if 'X-Loop' in msg and PTS_CONTROL_EMAIL in msg.get_all('X-Loop'): return # Get the first plain-text part of the message plain_text_part = next(typed_subpart_iterator(msg, 'text', 'plain'), None) if not plain_text_part: # There is no plain text in the email send_plain_text_warning(msg) return # Decode the plain text into a unicode string try: text = get_decoded_message_payload(plain_text_part) except UnicodeDecodeError: send_plain_text_warning(msg) return lines = extract_command_from_subject(msg) + text.splitlines() # Process the commands factory = CommandFactory({ 'email': extract_email_address_from_header(msg['From']), }) confirmation_set = ConfirmationSet() processor = CommandProcessor(factory) processor.confirmation_set = confirmation_set processor.process(lines) confirmation_set.ask_confirmation_all() # Send a response only if there were some commands processed if processor.is_success(): send_response( msg, processor.get_output(), set(confirmation_set.get_emails()))
def handle(self): from pts.mail.control.commands import CommandFactory, CommandProcessor command_confirmation = get_or_none( CommandConfirmation, confirmation_key=self.confirmation_key) if not command_confirmation: self.error('Confirmation failed: Unknown key') return lines = command_confirmation.commands.splitlines() processor = CommandProcessor(CommandFactory({}), confirmed=True) processor.process(lines) if processor.is_success(): self.reply('Successfully confirmed commands.') self.reply(processor.get_output()) else: self.error('No commands confirmed') self.reply(processor.get_output()) command_confirmation.delete()