diff --git a/README.md b/README.md index 4c925c5..564c135 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ IRC network connection and login info. ## channels List of channels to auto-join to. +## webadmin +Web interface settings. +* **enabled** - enabled web admin +* **port** - port to bound to +* **users** - list of users with access to web interface in `name: password` format + ## log Logging settings. * **file** - file to log to. Set to `false` to disable logging to file. @@ -85,3 +91,4 @@ To run the bot simply use `npm start` or `node bin/tipbot`. Donations are welcome! If you like my bot and want to donate me, here are my wallet addresses: * **Dogecoin**: DGLT1pTSKAXLSSSz1NGFgZNLioDyHdaDyA +* **Flappycoin**: FPEBPCC1sr9FAJ4DbD3r4YFwMqrr9TppeK diff --git a/bin/tipbot.js b/bin/tipbot.js index ba24af1..338e508 100644 --- a/bin/tipbot.js +++ b/bin/tipbot.js @@ -2,13 +2,23 @@ var irc = require('irc') , winston = require('winston') , fs = require('fs') , yaml = require('js-yaml') -, coin = require('node-dogecoin'); +, coin = require('node-dogecoin') +, webadmin = require('../lib/webadmin/app'); +// check if the config file exists if(!fs.existsSync('./config/config.yml')) { winston.error('Configuration file doesn\'t exist! Please read the README.md file first.'); process.exit(1); } +// handle sigint +process.on('exit', function() { + winston.info('Exiting...'); + if(client != null) { + client.disconnect('My master ordered me to leave.'); + } +}); + // load settings var settings = yaml.load(fs.readFileSync('./config/config.yml', 'utf-8')); @@ -43,6 +53,13 @@ coin.getBalance(function(err, balance) { winston.info('Connected to JSON RPC API. Current total balance is %d' + settings.coin.short_name, balance); }) +// run webadmin +if(settings.webadmin.enabled) +{ + winston.info('Running webadmin on port %d', settings.webadmin.port); + webadmin.app(settings.webadmin.port, coin, settings.webadmin, winston); +} + // connect to the server winston.info('Connecting to the server...'); diff --git a/config/config.sample.yml b/config/config.sample.yml index 2b2197b..bfdcfbe 100644 --- a/config/config.sample.yml +++ b/config/config.sample.yml @@ -25,6 +25,12 @@ coin: min_tip: 10 short_name: Đ full_name: dogecoin +webadmin: + enabled: false + port: 8080 + users: + unek: 123 + nix: 456 commands: help: pm: true diff --git a/lib/webadmin/app.js b/lib/webadmin/app.js new file mode 100644 index 0000000..f82848d --- /dev/null +++ b/lib/webadmin/app.js @@ -0,0 +1,43 @@ +var express = require('express') +, swig = require('swig'); + +module.exports.app = function(port, coin, settings, winston) { + var app = express(); + + app.use(express.basicAuth(function(user, password) { + var success = settings.users[user] == password; + if(success) + winston.info('%s logged into webadmin', user); + else + winston.warn('Failed webadmin login as %s', user); + + return success; + })); + + var tpl = swig.compileFile(__dirname + '/views/index.html'); + app.get('/', function(req, res) { + var accounts = []; + coin.listAccounts(5, function(err, reply) { + if(err) { + winston.error('Tried to get account listing but failed.', err); + res.send(500, 'Could not get account list'); + return; + } + for(var user in reply) { + if(user !== "") + accounts.push({nick: user, balance: reply[user]}); + }; + + accounts.sort(function(a, b) { + return b.balance - a.balance + }); + + var body = tpl({accounts: accounts}); + res.setHeader('Content-Type', 'text/html'); + res.setHeader('Content-Length', Buffer.byteLength(body)); + res.end(body); + }); + }); + + app.listen(port); +} diff --git a/lib/webadmin/views/index.html b/lib/webadmin/views/index.html new file mode 100644 index 0000000..59a497f --- /dev/null +++ b/lib/webadmin/views/index.html @@ -0,0 +1,67 @@ + + +
+ + + +| # | +Nick | +Balance | +
|---|---|---|
| {{ loop.index }} | +{{ account.nick }} | +{{ account.balance }} | +