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) email = extract_email_address_from_header(msg.get('From', '')) logdata = { 'from': email, 'msgid': msg.get('Message-ID', 'no-msgid-present@localhost'), } logger.info("control <= %(from)s %(msgid)s", logdata) if 'X-Loop' in msg and DISTRO_TRACKER_CONTROL_EMAIL in msg.get_all('X-Loop'): logger.info("control :: discarded %(msgid)s due to X-Loop", logdata) 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, logdata) 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, logdata) return lines = extract_command_from_subject(msg) + text.splitlines() # Process the commands factory = CommandFactory({'email': email}) 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(), recipient_email=email, cc=set(confirmation_set.get_emails())) else: logger.info("control :: no command processed in %(msgid)s", logdata)
def handle(self): from distro_tracker.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()