Esempio n. 1
0
def process_command(author: AccountId,
                    message: str,
                    server: Server,
                    prefix=''):
    """Processes an arbitrary command."""
    split_msg = message.split()
    if len(split_msg) == 0:
        return 'Hi %s! You sent me an empty message.' % (author.readable())

    # Expand aliases if necessary.
    while split_msg[0] in ALIASES:
        split_msg[0] = ALIASES[split_msg[0]]

    if split_msg[0] in COMMANDS:
        try:
            cmd = COMMANDS[split_msg[0]]
            if len(cmd) >= 4 and cmd[3].value > Authorization.CITIZEN.value:
                assert_authorized(author, server, cmd[3])

            platform_name = 'Discord' if isinstance(
                author, DiscordAccountId) else 'Reddit'
            return cmd[2](author,
                          message,
                          server,
                          prefix=prefix,
                          platform_name=platform_name)
        except CommandException as e:
            return str(e)
    else:
        return 'Hi %s! I didn\'t quite understand command your command `%s`.' % (
            author.readable(), split_msg[0])  # Sends the help message.
Esempio n. 2
0
def process_balance(author: AccountId, message: str, server: Server, **kwargs):
    """Processes a message requesting the balance on an account."""
    if not server.has_account(author):
        return 'Hi there %s. I can\'t tell you what the balance on your account is because you don\'t have an account yet. ' \
               'You can open one with the `open` command.' % author.readable()

    account = server.get_account(author)
    main_response = 'The balance on your account is %s.' % fraction_to_str(
        account.get_balance())
    return 'Hi there %s %s. %s Have a great day.' % (account.get_authorization(
    ).name.lower(), author.readable(), main_response)
Esempio n. 3
0
def process_request_alias(author: AccountId, message: str, server: Server, **kwargs):
    """Processes a request for an alias code."""
    account = assert_is_account(author, server)

    split_msg = message.split()
    if len(split_msg) != 2:
        raise CommandException('Incorrect formatting. Expected `request-alias ALIAS_ACCOUNT_NAME`.')

    _, alias_name = split_msg
    alias_id = parse_account_id(alias_name)
    if server.has_account(alias_id):
        raise CommandException(
            'An account has already been associated with %s, so it cannot be an alias for this account.' %
            alias_id.readable())

    # To generate an alias code, we generate an ECC public/private key pair, use the
    # private key to generate a signed version of the aliased account name and associate
    # the public key with the account.
    key = ECC.generate(curve='P-256')
    signature = sign_message(str(alias_id), key)
    server.add_public_key(account, key.public_key())

    # At this point we will allow the private key to be forgotten.

    # Compose a helpful message for as to how the bot can be contacted to link accounts.
    if isinstance(alias_id, RedditAccountId):
        contact_message = 'Send me that exact command as a Reddit Private Message (not a direct chat) from %s.' % alias_id.readable()
    elif isinstance(alias_id, DiscordAccountId):
        contact_message = 'Send me that exact command prefixed with a mention of my name via Discord from account %s.' % alias_id
    else:
        contact_message = ''

    return ('I created an alias request code for you. '
        'Make {0} an alias for this account ({2}) by sending me the following message from {3}.\n\n```\nadd-alias {4} {1}\n```\n\n{5}').format(
            str(alias_id), signature, author.readable(), alias_id.readable(), str(author), contact_message)
Esempio n. 4
0
def process_reference(author: AccountId, message: str, server: Server,
                      **kwargs):
    """Gets the command reference message for the economy bot."""
    return '''
Hi %s! Here's a list of the commands I understand:

%s''' % (author.readable(), list_commands_as_markdown(author, server))
Esempio n. 5
0
def process_open_account(author: AccountId,
                         message: str,
                         server: Server,
                         prefix='',
                         platform_name='Reddit',
                         **kwargs):
    """Processes a message that tries to open a new account."""
    if server.has_account(author):
        return 'Hi there %s. Looks like you already have an account. No need to open another one.' % author.readable(
        )

    server.open_account(author)
    return 'Hi there %s. Your account has been opened successfully. Thank you for your business. %s' % (
        author.readable(),
        get_generic_help_message(author, prefix, platform_name))
Esempio n. 6
0
def process_add_alias(author: AccountId, message: str, server: Server,
                      **kwargs):
    """Processes a request to add `author` as an alias to an account."""
    if server.has_account(author):
        raise CommandException(
            'An account has already been associated with %s, so it cannot be an alias for another account.'
            % author.readable())

    split_msg = message.split()
    if len(split_msg) != 3:
        raise CommandException(
            'Incorrect formatting. Expected format `add-alias ALIASED_ACCOUNT ALIAS_REQUEST_CODE`.'
        )

    _, aliased_account_name, signature = split_msg
    aliased_account = assert_is_account(aliased_account_name, server)

    if is_signed_by(aliased_account, str(author), signature):
        server.add_account_alias(aliased_account, author)
        return 'Alias set up successfully. %s and %s now refer to the same account.' % (
            aliased_account_name, author.readable())
    else:
        raise CommandException(
            'Cannot set up alias because the signature is invalid.')
Esempio n. 7
0
def process_help(author: AccountId, message: str, server: Server, prefix='', platform_name='Reddit', **kwargs):
    """Gets the help message for the economy bot."""
    if server.has_account(author):
        return '''Howdy partner! It's always a pleasure to meet an account holder. %s %s''' % (get_how_to_reach_message(platform_name), get_generic_help_message(author, prefix, platform_name))
    else:
        return '''Hi! You look new. {1} If you don't have an account with me yet, you can open one using this command:

> {0}open

Alternatively, if you already have an account then please don't create a new one here.
Instead, link your existing account to {2} by running the following command from a username that's already associated with the account.

> request-alias {3}

(If you're sending me that over Discord you'll also have to ping me at the start of that command.)'''.format(
    prefix, get_how_to_reach_message(platform_name), author.readable(), str(author))
Esempio n. 8
0
def process_proxy_command(author: AccountId, message: str, server: Server,
                          **kwargs):
    """Processes a command by proxy."""
    account_id, signature, command = parse_proxy_command(message)
    account = assert_is_account(account_id, server)

    if signature is None:
        author_account = assert_is_account(author, server)
        if author_account in account.get_proxies():
            return process_command(ProxyAccountId(author, account_id), command,
                                   server)
        else:
            raise CommandException(
                'Cannot execute command by proxy because %s is not an authorized proxy for %s.'
                % (author.readable(), account_id.readable()))

    elif is_signed_by(account, command, signature):
        return process_command(ProxyAccountId(author, account_id), command,
                               server)

    else:
        raise CommandException(
            'Cannot execute command by proxy because the signature is invalid.'
        )
Esempio n. 9
0
def process_name(author: AccountId, message: str, server: Server, **kwargs):
    """Processes a request for an account name."""
    return 'Hello there %s. Your local account ID is `%s`.' % (author.readable(), str(author))