'Sets the encoding.'

def func(self):

    if (self.session is None):
        return
    sync = False
    if ('clear' in self.switches):
        old_encoding = self.session.protocol_flags.get('ENCODING', None)
        if old_encoding:
            string = ("Your    custom    text    encoding    ('%s')    was    cleared." % old_encoding)
        else:
            string = 'No    custom    encoding    was    set.'
        self.session.protocol_flags['ENCODING'] = 'utf-8'
        sync = True
    elif (not self.args):
        pencoding = self.session.protocol_flags.get('ENCODING', None)
        string = ''
        if pencoding:
            string += ('Default    encoding:    |g%s|n    (change    with    |w@encoding    <encoding>|n)' % pencoding)
        encodings = settings.ENCODINGS
        if encodings:
            string += ("\nServer's    alternative    encodings    (tested    in    this    order):\n            |g%s|n" % ',    '.join(encodings))
        if (not string):
            string = 'No    encodings    found.'
    else:
        old_encoding = self.session.protocol_flags.get('ENCODING', None)
        encoding = self.args
        try:
            utils.to_str(utils.to_unicode('test-string'), encoding=encoding)
        except LookupError:
            string = ("|rThe    encoding    '|w%s|r'    is    invalid.    Keeping    the    previous    encoding    '|w%s|r'.|n" % (encoding, old_encoding))
        else:
            self.session.protocol_flags['ENCODING'] = encoding
            string = ("Your    custom    text    encoding    was    changed    from    '|w%s|n'    to    '|w%s|n'." % (old_encoding, encoding))
            sync = True
    if sync:
        self.session.sessionhandler.session_portal_sync(self.session)
    self.caller.msg(string.strip())
