From 7106023a4e1b86cf148088351fb7cfd8ea7ce81c Mon Sep 17 00:00:00 2001 From: unek Date: Sat, 22 Feb 2014 22:21:36 +0100 Subject: [PATCH] now with proper account status checking --- bin/tipbot | 260 +++++++++++++++++++++++++-------------------------- package.json | 2 +- 2 files changed, 131 insertions(+), 131 deletions(-) diff --git a/bin/tipbot b/bin/tipbot index d9de830..29faed6 100644 --- a/bin/tipbot +++ b/bin/tipbot @@ -130,174 +130,174 @@ client.addListener('message', function(from, channel, message) { // if pms, make sure to respond to pms instead to itself if(channel == client.nick) channel = from; - switch(command) { - case 'tip': - var match = message.match(/^.?tip (\S+) (\d+)/); - if(match == null || match < 3) { - client.say(channel, 'Usage: !tip ') - return; - } - var to = match[1]; - var amount = Number(match[2]); + client.getLoginStatus(from, function(status) { + // check if the sending user is logged in (identified) + if(status != 3) { + winston.info('%s tried to use command `%s`, but is not identified.', from, message); + client.say(channel, settings.messages.not_identified.expand({name: from})); + return; + } + + switch(command) { + case 'tip': + var match = message.match(/^.?tip (\S+) (\d+)/); + if(match == null || match < 3) { + client.say(channel, 'Usage: !tip ') + return; + } + var to = match[1]; + var amount = Number(match[2]); - if(to.toLowerCase() == from.toLowerCase()) { - client.say(channel, settings.messages.self_tip.expand({name: from})); - return; - } + if(to.toLowerCase() == from.toLowerCase()) { + client.say(channel, settings.messages.self_tip.expand({name: from})); + return; + } - if(amount < settings.coin.min_tip) { - client.say(channel, settings.messages.tip_too_small.expand({from: from, to: to, amount: amount})); - return; - } + if(amount < settings.coin.min_tip) { + client.say(channel, settings.messages.tip_too_small.expand({from: from, to: to, amount: amount})); + return; + } + // check balance with min. 5 confirmations + coin.getBalance(from, settings.coin.min_confirmations, function(err, balance) { + if(err) { + winston.error('Error in !tip command.', err); + client.say(channel, settings.messages.error.expand({name: from})); + return; + } + var balance = typeof(balance) == 'object' ? balance.result : balance; - client.getLoginStatus(from, function(status) { - // check if the sending user is logged in (identified) - if(status == 3) { - // check balance with min. 5 confirmations - coin.getBalance(from, settings.coin.min_confirmations, function(err, balance) { + if(balance >= amount) { + client.getAddress(to, function(err, to_address) { // get the address to actually create a new one if(err) { - winston.error('Error in !tip command.', err); + winston.error('Error in !tip command', err); client.say(channel, settings.messages.error.expand({name: from})); return; } - var balance = typeof(balance) == 'object' ? balance.result : balance; - if(balance >= amount) { - client.getAddress(to, function(err, to_address) { // get the address to actually create a new one - if(err) { - winston.error('Error in !tip command', err); - client.say(channel, settings.messages.error.expand({name: from})); - return; - } + coin.send('move', from.toLowerCase(), to.toLowerCase(), amount, function(err, reply) { + if(err || !reply) { + winston.error('Error in !tip command', err); + client.say(channel, settings.messages.error.expand({name: from})); + return; + } - coin.send('move', from.toLowerCase(), to.toLowerCase(), amount, function(err, reply) { - if(err || !reply) { - winston.error('Error in !tip command', err); - client.say(channel, settings.messages.error.expand({name: from})); - return; - } - - winston.info('%s tipped %s %d%s', from, to, amount, settings.coin.short_name) - client.say(channel, settings.messages.tipped.expand({from: from, to: to, amount: amount})); - }); - }) - } else { - winston.info('%s tried to tip %s %d, but has only %d', from, to, amount, balance); - client.say(channel, settings.messages.no_funds.expand({name: from, balance: balance, short: amount - balance, amount: amount})); - } + winston.info('%s tipped %s %d%s', from, to, amount, settings.coin.short_name) + client.say(channel, settings.messages.tipped.expand({from: from, to: to, amount: amount})); + }); }) } else { - client.say(channel, settings.messages.not_identified.expand({name: from})); + winston.info('%s tried to tip %s %d, but has only %d', from, to, amount, balance); + client.say(channel, settings.messages.no_funds.expand({name: from, balance: balance, short: amount - balance, amount: amount})); } - }); - break; - case 'address': - var user = from.toLowerCase(); - client.getAddress(user, function(err, address) { - if(err) { - winston.error('Error in !address command', err); - client.say(channel, settings.messages.error.expand({name: from})); - return; - } - - client.say(channel, settings.messages.deposit_address.expand({name: user, address: address})); - }); - break; - case 'balance': - var user = from.toLowerCase(); - coin.getBalance(user, settings.coin.min_confirmations, function(err, balance) { - if(err) { - winston.error('Error in !balance command', err); - client.say(channel, settings.messages.error.expand({name: from})); - return; - } - - var balance = typeof(balance) == 'object' ? balance.result : balance; - - coin.getBalance(user, 0, function(err, unconfirmed_balance) { - if(err) { - winston.error('Error in !balance command', err); - client.say(channel, settings.messages.balance.expand({balance: balance, name: user})); + break; + case 'address': + var user = from.toLowerCase(); + client.getAddress(user, function(err, address) { + if(err) { + winston.error('Error in !address command', err); + client.say(channel, settings.messages.error.expand({name: from})); return; } - var unconfirmed_balance = typeof(unconfirmed_balance) == 'object' ? unconfirmed_balance.result : unconfirmed_balance; + client.say(channel, settings.messages.deposit_address.expand({name: user, address: address})); + }); + break; + case 'balance': + var user = from.toLowerCase(); + coin.getBalance(user, settings.coin.min_confirmations, function(err, balance) { + if(err) { + winston.error('Error in !balance command', err); + client.say(channel, settings.messages.error.expand({name: from})); + return; + } - client.say(channel, settings.messages.balance_unconfirmed.expand({balance: balance, name: user, unconfirmed: unconfirmed_balance - balance})); - }) - }); - break; - case 'withdraw': - var match = message.match(/^.?withdraw (\S+)$/); - if(match == null) { - client.say(channel, 'Usage: !withdraw <' + settings.coin.full_name + ' address>'); - return; - } - var address = match[1]; + var balance = typeof(balance) == 'object' ? balance.result : balance; - coin.validateAddress(address, function(err, reply) { - if(err) { - winston.error('Error in !withdraw command', err); - client.say(channel, settings.messages.error.expand({name: from})); + coin.getBalance(user, 0, function(err, unconfirmed_balance) { + if(err) { + winston.error('Error in !balance command', err); + client.say(channel, settings.messages.balance.expand({balance: balance, name: user})); + return; + } + + var unconfirmed_balance = typeof(unconfirmed_balance) == 'object' ? unconfirmed_balance.result : unconfirmed_balance; + + client.say(channel, settings.messages.balance_unconfirmed.expand({balance: balance, name: user, unconfirmed: unconfirmed_balance - balance})); + }) + }); + break; + case 'withdraw': + var match = message.match(/^.?withdraw (\S+)$/); + if(match == null) { + client.say(channel, 'Usage: !withdraw <' + settings.coin.full_name + ' address>'); return; } + var address = match[1]; - if(reply.isvalid) { - coin.getBalance(from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) { - if(err) { - winston.error('Error in !withdraw command', err); - client.say(channel, settings.messages.error.expand({name: from})); - return; - } - var balance = typeof(balance) == 'object' ? balance.result : balance; + coin.validateAddress(address, function(err, reply) { + if(err) { + winston.error('Error in !withdraw command', err); + client.say(channel, settings.messages.error.expand({name: from})); + return; + } - if(balance < settings.coin.min_withdraw) { - winston.warn('%s tried to withdraw %d, but min is set to %d', from, balance, settings.coin.min_withdraw); - client.say(channel, settings.messages.withdraw_too_small.expand({name: from, balance: balance})); - return; - } - - coin.sendFrom(from.toLowerCase(), address, balance - settings.coin.withdrawal_fee, function(err, reply) { + if(reply.isvalid) { + coin.getBalance(from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) { if(err) { winston.error('Error in !withdraw command', err); client.say(channel, settings.messages.error.expand({name: from})); return; } + var balance = typeof(balance) == 'object' ? balance.result : balance; - var values = {name: from, address: address, balance: balance, amount: balance - settings.coin.withdrawal_fee, transaction: reply} - for(var i = 0; i < settings.messages.withdraw_success.length; i++) { - var msg = settings.messages.withdraw_success[i]; - client.say(channel, msg.expand(values)); - }; + if(balance < settings.coin.min_withdraw) { + winston.warn('%s tried to withdraw %d, but min is set to %d', from, balance, settings.coin.min_withdraw); + client.say(channel, settings.messages.withdraw_too_small.expand({name: from, balance: balance})); + return; + } - // transfer the rest (usually withdrawal fee - txfee) to bots wallet - coin.getBalance(from.toLowerCase(), function(err, balance) { + coin.sendFrom(from.toLowerCase(), address, balance - settings.coin.withdrawal_fee, function(err, reply) { if(err) { - winston.error('Something went wrong while transferring fees', err); + winston.error('Error in !withdraw command', err); + client.say(channel, settings.messages.error.expand({name: from})); return; } - var balance = typeof(balance) == 'object' ? balance.result : balance; - coin.move(from.toLowerCase(), settings.login.nickname, balance, function(err) { + var values = {name: from, address: address, balance: balance, amount: balance - settings.coin.withdrawal_fee, transaction: reply} + for(var i = 0; i < settings.messages.withdraw_success.length; i++) { + var msg = settings.messages.withdraw_success[i]; + client.say(channel, msg.expand(values)); + }; + + // transfer the rest (usually withdrawal fee - txfee) to bots wallet + coin.getBalance(from.toLowerCase(), function(err, balance) { if(err) { winston.error('Something went wrong while transferring fees', err); return; } + + var balance = typeof(balance) == 'object' ? balance.result : balance; + coin.move(from.toLowerCase(), settings.login.nickname, balance, function(err) { + if(err) { + winston.error('Something went wrong while transferring fees', err); + return; + } + }); }); }); }); - }); - } else { - winston.warn('%s tried to withdraw to an invalid address', from); - client.say(channel, settings.messages.invalid_address.expand({address: address, name: from})); + } else { + winston.warn('%s tried to withdraw to an invalid address', from); + client.say(channel, settings.messages.invalid_address.expand({address: address, name: from})); + } + }); + break; + case 'help': + for(var i = 0; i < settings.messages.help.length; i++) { + var message = settings.messages.help[i]; + client.say(channel, message.expand({})); } - }); - break; - case 'help': - for(var i = 0; i < settings.messages.help.length; i++) { - var message = settings.messages.help[i]; - client.say(channel, message.expand({})); - } - break; - } + break; + } + }); }); diff --git a/package.json b/package.json index d8c9c0b..e3ecb1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-tip-bot", - "version": "1.0.0", + "version": "1.0.1", "description": "node.js based tipping bot for dogecoin", "main": "bin/tipbot", "dependencies": {