Esempio n. 1
0
    def test_user_exist(self):
        u1 = models.User('just-an-dev')
        u2 = models.User('Just-An-dEv')
        self.assertEqual(True, u1.is_registered())
        self.assertEqual(False, u2.is_registered())

        self.assertEqual(True, user_function.user_exist('just-an-dev'))
        self.assertEqual(True, user_function.user_exist('Just-An-dEv'))
Esempio n. 2
0
def tip_user(rpc, msg):
    print('An user mention detected ')
    split_message = msg.body.strip().split()
    tip_index = split_message.index('+/u/sodogetiptest')

    if split_message[tip_index] == '+/u/sodogetiptest' and split_message[
            tip_index + 2] == 'doge':
        amount = split_message[tip_index + 1]

        if utils.check_amount_valid(amount):
            parent_comment = msg.parent()
            if user_function.user_exist(msg.author.name):

                # check we have enough
                user_balance = crypto.get_user_balance(rpc, msg.author.name)
                if int(amount) >= user_balance:
                    print(
                        'user %s not have enough to tip this amount (%s), balance = %s'
                        % (msg.author.name, amount, user_balance))
                    msg.reply('+/u/%s your balance is too low for this tip ' %
                              msg.author.name)
                else:

                    # check user have address before tip
                    if user_function.user_exist(parent_comment.author.name):
                        if crypto.tip_user(rpc, msg.author.name,
                                           parent_comment.author.name, amount):
                            user_function.add_to_history(
                                msg.author.name, msg.author.name,
                                parent_comment.author.name, amount, "tip")

                            print '%s tip %s to %s' % (
                                msg.author.name, str(amount),
                                parent_comment.author.name)
                            msg.reply('+/u/%s tip %s to %s' %
                                      (msg.author.name, str(amount),
                                       parent_comment.author.name))
                    else:
                        user_function.save_unregistered_tip(
                            msg.author.name, parent_comment.author.name,
                            amount)
                        user_function.add_to_history(
                            msg.author.name, msg.author.name,
                            parent_comment.author.name, amount, "tip", False)
                        print('user %s not registered' %
                              parent_comment.author.name)
                        msg.reply(
                            '+/u/%s need %s before can be tipped (tip saved during 3 day)'
                            % (parent_comment.author.name, linkRegister))
            else:
                msg.reply('You need %s before' % linkRegister)
        else:
            print('You must use valid amount')
            msg.reply('You must use valid amount')
Esempio n. 3
0
def withdraw_user(rpc, msg):
    split_message = msg.body.strip().split()

    if user_function.user_exist(msg.author.name):
        sender_address = user_function.get_user_address(msg.author.name)
        amount = split_message[1]
        user_balance = crypto.get_user_balance(rpc, msg.author.name)
        if int(amount) >= user_balance:
            print(
                'user %s not have enough to withdraw this amount (%s), balance = %s'
                % (msg.author.name, amount, user_balance))
            msg.reply('+/u/%s your balance is too low for this withdraw ' %
                      msg.author.name)
        else:
            if utils.check_amount_valid(amount):
                receiver_address = split_message[4]
                try:
                    if crypto.send_to(rpc, sender_address, receiver_address,
                                      amount, True):
                        user_function.add_to_history(msg.author.name,
                                                     sender_address,
                                                     receiver_address, amount,
                                                     "withdraw")
                        msg.reply('Withdraw : ' + str(amount) + ' to ' +
                                  receiver_address)

                except:
                    traceback.print_exc()
            else:
                print('You must use valid amount')
                msg.reply('You must use valid amount')
    else:
        msg.reply('You need %s before' % linkRegister)
Esempio n. 4
0
def balance_user(rpc, msg):
    if user_function.user_exist(msg.author.name):

        balance = crypto.get_user_confirmed_balance(rpc, msg.author.name)
        pending_balance = crypto.get_user_unconfirmed_balance(
            rpc, msg.author.name)
        spendable_balance = crypto.get_user_spendable_balance(
            rpc, msg.author.name) + balance
        bot_logger.logger.info('user %s balance = %s' %
                               (msg.author.name, balance))

        balance_value_usd = utils.get_coin_value(balance)
        pending_value_usd = utils.get_coin_value(pending_balance)
        spendable_value_usd = utils.get_coin_value(spendable_balance)
        msg.reply(
            Template(lang.message_balance + lang.message_footer).render(
                username=msg.author.name,
                balance=str(balance),
                balance_value_usd=str(balance_value_usd),
                pendingbalance=str(pending_balance),
                pending_value_usd=str(pending_value_usd),
                spendablebalance=str(spendable_balance),
                spendable_value_usd=str(spendable_value_usd)))

        user_function.add_to_history(msg.author.name, "", "", balance,
                                     "balance")
    else:
        bot_logger.logger.info('user %s not registered ' % msg.author.name)
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Esempio n. 5
0
def info_user(rpc, msg):
    if user_function.user_exist(msg.author.name):
        address = user_function.get_user_address(msg.author.name)
        balance = crypto.get_user_confirmed_balance(rpc, msg.author.name)
        pending_balance = crypto.get_user_unconfirmed_balance(
            rpc, msg.author.name)
        spendable_balance = crypto.get_user_spendable_balance(
            rpc, msg.author.name) + balance
        balance_value_usd = utils.get_coin_value(balance)
        pending_value_usd = utils.get_coin_value(pending_balance)
        spendable_value_usd = utils.get_coin_value(spendable_balance)
        msg.reply(
            Template(lang.message_account_details +
                     lang.message_footer).render(
                         username=msg.author.name,
                         balance=str(balance),
                         balance_value_usd=str(balance_value_usd),
                         pendingbalance=str(pending_balance),
                         pending_value_usd=str(pending_value_usd),
                         spendablebalance=str(spendable_balance),
                         spendable_value_usd=str(spendable_value_usd),
                         address=address))

    else:
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Esempio n. 6
0
def register_user(rpc, msg):
    if not user_function.user_exist(msg.author.name):
        address = rpc.getnewaddress("reddit-%s" % msg.author.name)
        if address:
            msg.reply(
                Template(lang.message_register_success +
                         lang.message_footer).render(username=msg.author.name,
                                                     address=address))
            user_function.add_user(msg.author.name, address)

            user_function.add_to_history(msg.author.name, "", "", "",
                                         "register")

            # create a backup of wallet
            rpc.backupwallet(
                config.backup_wallet_path + "backup_" +
                datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + ".dat")
        else:
            bot_logger.logger.warning('Error during register !')
    else:
        bot_logger.logger.info('%s are already registered ' % msg.author.name)
        balance = crypto.get_user_confirmed_balance(rpc, msg.author.name)
        address = user_function.get_user_address(msg.author.name)
        msg.reply(
            Template(lang.message_already_registered +
                     lang.message_account_details +
                     lang.message_footer).render(username=msg.author.name,
                                                 address=address,
                                                 balance=str(balance)))
Esempio n. 7
0
def balance_user(rpc, msg):
    if user_function.user_exist(msg.author.name):
        balance = crypto.get_user_balance(rpc, msg.author.name)
        print('user %s balance = %s' % (msg.author.name, balance))
        value_usd = utils.get_coin_value(balance)
        msg.reply('Your balance : ' + str(balance) + ' ( ' + str(value_usd) +
                  '$ ) ')
    else:
        print('user %s not registered ' % (msg.author.name))
        msg.reply('You need %s before' % linkRegister)
Esempio n. 8
0
def help_user(rpc, msg):
    if user_function.user_exist(msg.author.name):
        address = user_function.get_user_address(msg.author.name)
        msg.reply(
            Template(lang.message_help + lang.message_footer).render(
                username=msg.author.name, address=address))
    else:
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Esempio n. 9
0
def balance_user(rpc, msg, failover_time):
    if user_function.user_exist(msg.author.name):
        if time.time() > int(failover_time.value) + 86400:
            # not in safe mode
            balance = get_user_confirmed_balance(rpc, msg.author.name)
            spendable_balance = get_user_spendable_balance(rpc, msg.author.name) + balance
        else:
            # we are in safe mode
            spendable_balance = get_user_confirmed_balance(rpc, msg.author.name)

    return spendable_balance
Esempio n. 10
0
def withdraw_user(rpc, msg, failover_time):
    split_message = msg.body.strip().split()

    if user_function.user_exist(msg.author.name):
        sender_address = user_function.get_user_address(msg.author.name)
        amount = float(split_message[1])
        amount = round(amount - 0.5)
        print(amount)
        user_balance = crypto.get_user_confirmed_balance(rpc, msg.author.name)
        user_spendable_balance = crypto.get_user_spendable_balance(
            rpc, msg.author.name)
        if utils.check_amount_valid(
                amount) and split_message[4] != sender_address:
            if amount >= float(user_balance) + float(user_spendable_balance):
                bot_logger.logger.info(
                    'user %s not have enough to withdraw this amount (%s), balance = %s'
                    % (msg.author.name, amount, user_balance))
                msg.reply(
                    Template(lang.message_balance_low_withdraw).render(
                        username=msg.author.name,
                        user_balance=str(user_balance),
                        amount=str(amount)) + lang.message_footer)
            else:
                receiver_address = split_message[4]
                if time.time() > int(failover_time.value) + 86400:
                    send = crypto.send_to(rpc, sender_address,
                                          receiver_address, amount)
                else:
                    send = crypto.send_to_failover(rpc, sender_address,
                                                   receiver_address, amount)

                if send:
                    user_function.add_to_history(msg.author.name,
                                                 sender_address,
                                                 receiver_address, amount,
                                                 "withdraw")
                    value_usd = utils.get_coin_value(amount)
                    msg.reply(
                        Template(lang.message_withdraw +
                                 lang.message_footer).render(
                                     username=msg.author.name,
                                     receiver_address=receiver_address,
                                     amount=str(amount),
                                     value_usd=str(value_usd)))

        elif split_message[4] == sender_address:
            msg.reply(lang.message_withdraw_to_self + lang.message_footer)
        else:
            bot_logger.logger.info(lang.message_invalid_amount)
            msg.reply(lang.message_invalid_amount + lang.message_footer)
    else:
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Esempio n. 11
0
def register_user(rpc, msg):
    if not user_function.user_exist(msg.author.name):
        address = rpc.getnewaddress()
        if address:
            msg.reply(msg.author.name + ' registered, your address is ' +
                      address)
            user_function.add_user(msg.author.name, address)

            pending_tips(rpc, msg)
        else:
            print 'Error during register !'
    else:
        print msg.author.name + ' are already registered '
        msg.reply('You are already registered ')
Esempio n. 12
0
def replay_remove_pending_tip(rpc, reddit, tx_queue, failover_time):
    # check if it's not too old & replay tipping
    limit_date = datetime.datetime.now() - datetime.timedelta(days=3)

    # check if user have pending tips
    list_tips = user_function.get_unregistered_tip()

    if list_tips:
        for tip in list_tips:
            bot_logger.logger.info("replay tipping check for %s" %
                                   str(tip['id']))
            if (datetime.datetime.strptime(tip['time'], '%Y-%m-%dT%H:%M:%S.%f')
                    > limit_date):
                if (user_function.user_exist(tip['receiver'])):
                    bot_logger.logger.info(
                        "replay tipping %s - %s send %s to %s  " %
                        (str(tip['id']), tip['sender'], tip['amount'],
                         tip['receiver']))
                    txid = crypto.tip_user(rpc, tip['sender'], tip['receiver'],
                                           tip['amount'], tx_queue,
                                           failover_time)
                    user_function.remove_pending_tip(tip['id'])

                    value_usd = utils.get_coin_value(tip['amount'])

                    if 'message_fullname' in tip.keys():
                        msg_id = re.sub(r't\d+_(?P<id>\w+)', r'\g<id>',
                                        tip['message_fullname'])
                        msg = Comment(reddit, msg_id)
                        msg.reply(
                            Template(lang.message_tip).render(
                                sender=tip['sender'],
                                receiver=tip['receiver'],
                                amount=str(tip['amount']),
                                value_usd=str(value_usd),
                                txid=txid))

                else:
                    bot_logger.logger.info(
                        "replay check for %s - user %s not registered " %
                        (str(tip['id']), tip['receiver']))
            else:
                bot_logger.logger.info(
                    "delete old tipping - %s send %s for %s  " %
                    (tip['sender'], tip['amount'], tip['receiver']))
                user_function.remove_pending_tip(tip['id'])
    else:
        bot_logger.logger.info("no pending tipping")
Esempio n. 13
0
def history_user(msg):
    if user_function.user_exist(msg.author.name):
        data = user_function.get_user_history(msg.author.name)
        history_table = "\n\nDate|Sender|Receiver|Amount|Action|Finish|\n"
        history_table += "---|---|---|---|:-:|:-:\n"
        for tip in data:
            str_finish = "Pending"

            if tip['finish']:
                str_finish = "Successful"

            history_table += "%s|%s|%s|%s|%s|%s|\n" % (
                datetime.datetime.strptime(
                    tip['time'], '%Y-%m-%dT%H:%M:%S.%f'), tip['sender'],
                tip['receiver'], str(tip['amount']), tip['action'], str_finish)

        msg.reply(
            Template(lang.message_history + history_table +
                     lang.message_footer).render(username=msg.author.name))
    else:
        bot_logger.logger.info('user %s not registered ' % msg.author.name)
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Esempio n. 14
0
def info_user(rpc, msg):
    if user_function.user_exist(msg.author.name):
        user_function.get_user_info(msg)
    else:
        msg.reply('You need %s before' % linkRegister)
Esempio n. 15
0
def tip_user(rpc, reddit, msg, tx_queue, failover_time):
    bot_logger.logger.info('An user mention detected ')
    bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value)))

    split_message = msg.body.lower().strip().split()
    tip_index = split_message.index(str('+/u/' + config.bot_name))

    if split_message[tip_index] == str(
            '+/u/' + config.bot_name) and split_message[tip_index +
                                                        2] == 'doge':

        amount = float(split_message[tip_index + 1])
        amount = round(amount - 0.5)
        if utils.check_amount_valid(amount):
            parent_comment = msg.parent()
            if user_function.user_exist(msg.author.name) and (
                    msg.author.name != parent_comment.author.name):

                # check we have enough
                user_balance = crypto.get_user_confirmed_balance(
                    rpc, msg.author.name)
                user_pending_balance = crypto.get_user_unconfirmed_balance(
                    rpc, msg.author.name)

                user_spendable_balance = crypto.balance_user(
                    rpc, msg, failover_time)
                bot_logger.logger.debug('user_spendable_balance = %s' %
                                        user_spendable_balance)

                # in failover we need to use only user_balance
                if amount >= float(user_spendable_balance):
                    # not enough for tip
                    if amount < float(user_pending_balance):
                        reddit.redditor(msg.author.name).message(
                            'pending tip',
                            Template(lang.message_balance_pending_tip).render(
                                username=msg.author.name))
                    else:
                        bot_logger.logger.info(
                            'user %s not have enough to tip this amount (%s), balance = %s'
                            %
                            (msg.author.name, str(amount), str(user_balance)))
                        reddit.redditor(msg.author.name).message(
                            'low balance',
                            Template(lang.message_balance_low_tip).render(
                                username=msg.author.name))
                else:

                    value_usd = utils.get_coin_value(amount)

                    # check user have address before tip
                    if user_function.user_exist(parent_comment.author.name):
                        txid = crypto.tip_user(rpc, msg.author.name,
                                               parent_comment.author.name,
                                               amount, tx_queue, failover_time)
                        if txid:
                            user_function.add_to_history(
                                msg.author.name, msg.author.name,
                                parent_comment.author.name, amount, "tip send",
                                txid)
                            user_function.add_to_history(
                                parent_comment.author.name, msg.author.name,
                                parent_comment.author.name, amount,
                                "tip receive", txid)

                            bot_logger.logger.info(
                                '%s tip %s to %s' %
                                (msg.author.name, str(amount),
                                 parent_comment.author.name))
                            # if user have 'verify' in this command he will have confirmation
                            if split_message.count(
                                    'verify') or int(amount) >= 1000:
                                msg.reply(
                                    Template(lang.message_tip).render(
                                        sender=msg.author.name,
                                        receiver=parent_comment.author.name,
                                        amount=str(int(amount)),
                                        value_usd=str(value_usd),
                                        txid=txid))
                    else:
                        user_function.save_unregistered_tip(
                            msg.author.name, parent_comment.author.name,
                            amount, msg.fullname)
                        user_function.add_to_history(
                            msg.author.name, msg.author.name,
                            parent_comment.author.name, amount, "tip send",
                            False)
                        user_function.add_to_history(
                            parent_comment.author.name, msg.author.name,
                            parent_comment.author.name, amount, "tip receive",
                            False)
                        bot_logger.logger.info('user %s not registered' %
                                               parent_comment.author.name)
                        reddit.redditor(msg.author.name).message(
                            'tipped user not registered',
                            Template(lang.message_recipient_register).render(
                                username=parent_comment.author.name))

                        reddit.redditor(parent_comment.author.name).message(
                            Template(lang.message_recipient_need_register_title
                                     ).render(amount=str(amount)),
                            Template(
                                lang.message_recipient_need_register_message).
                            render(username=parent_comment.author.name,
                                   sender=msg.author.name,
                                   amount=str(amount),
                                   value_usd=str(value_usd)))
            elif user_function.user_exist(
                    msg.author.name) and (msg.author.name
                                          == parent_comment.author.name):
                reddit.redditor(msg.author.name).message(
                    'cannot tip self',
                    Template(lang.message_recipient_self).render(
                        username=msg.author.name))
            else:
                reddit.redditor(msg.author.name).message(
                    'tipped user not registered',
                    Template(lang.message_need_register).render(
                        username=msg.author.name))
        else:
            bot_logger.logger.info(lang.message_invalid_amount)
            reddit.redditor(msg.author.name).message(
                'invalid amount', lang.message_invalid_amount)
Esempio n. 16
0
    def __init__(self, user):
        self.username = user
        self.address = None

        if user_function.user_exist(self.username):
            self.address = user_function.get_user_address(self.username)