<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def format_message(self, msg, botreply=False):
 """Format a user's message for safe processing. This runs substitutions on the message and strips out any remaining symbols (depending on UTF-8 mode). :param str msg: The user's message. :param bool botreply: Whether this formatting is being done for the bot's last reply (e.g. in a ``%Previous`` command). :return str: The formatted message. """


        # Make sure the string is Unicode for Python 2.
        if sys.version_info[0] < 3 and isinstance(msg, str):
            msg = msg.decode()

        # Lowercase it.
        msg = msg.lower()

        # Run substitutions on it.
        msg = self.substitute(msg, "sub")

        # In UTF-8 mode, only strip metacharacters and HTML brackets
        # (to protect from obvious XSS attacks).
        if self.utf8:
            msg = re.sub(RE.utf8_meta, '', msg)
            msg = re.sub(self.master.unicode_punctuation, '', msg)

            # For the bot's reply, also strip common punctuation.
            if botreply:
                msg = re.sub(RE.utf8_punct, '', msg)
        else:
            # For everything else, strip all non-alphanumerics.
            msg = utils.strip_nasties(msg)
            msg = msg.strip() # Strip leading and trailing white space
            msg = RE.ws.sub(" ",msg) # Replace the multiple whitespaces by single whitespace
        return msg