2012-12-11 17:04:52 +01:00
|
|
|
var express = require('express');
|
|
|
|
|
|
|
|
var webInterface = function(dbot) {
|
|
|
|
var pub = 'public';
|
2012-12-24 05:06:04 +01:00
|
|
|
var app = express();
|
2012-12-11 17:04:52 +01:00
|
|
|
|
|
|
|
app.use(express.static(pub));
|
|
|
|
app.set('view engine', 'jade');
|
|
|
|
|
|
|
|
app.get('/', function(req, res) {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('index', { 'name': dbot.config.name });
|
2012-12-11 17:04:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/connections', function(req, res) {
|
|
|
|
var connections = Object.keys(dbot.instance.connections);
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('connections', { 'name': dbot.config.name, 'connections': connections });
|
2012-12-11 17:04:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/channels/:connection', function(req, res) {
|
|
|
|
var connection = req.params.connection;
|
|
|
|
if(dbot.instance.connections.hasOwnProperty(connection)) {
|
|
|
|
var channels = Object.keys(dbot.instance.connections[connection].channels);
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('channels', { 'name': dbot.config.name, 'connection': connection, 'channels': channels});
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('error', { 'name': dbot.config.name, 'message': 'No such connection.' });
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/users/:connection/:channel', function(req, res) {
|
|
|
|
var connection = req.params.connection;
|
|
|
|
var channel = '#' + req.params.channel;
|
|
|
|
var connections = dbot.instance.connections;
|
|
|
|
|
|
|
|
if(connections.hasOwnProperty(connection) &&
|
|
|
|
connections[connection].channels.hasOwnProperty(channel)) {
|
|
|
|
var nicks = Object.keys(connections[connection].channels[channel].nicks);
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('users', { 'name': dbot.config.name, 'connection': connection,
|
2012-12-11 17:04:52 +01:00
|
|
|
'channel': channel, 'nicks': nicks });
|
|
|
|
} else {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('error', { 'name': dbot.config.name, 'message': 'No such connection or channel.' });
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/user/:connection/:channel/:user', function(req, res) {
|
|
|
|
var connection = req.params.connection;
|
|
|
|
var channel = '#' + req.params.channel;
|
|
|
|
var user = dbot.cleanNick(req.params.user);
|
|
|
|
|
|
|
|
var quoteCount = 'no';
|
|
|
|
if(dbot.db.quoteArrs.hasOwnProperty(user)) {
|
|
|
|
var quoteCount = dbot.db.quoteArrs[user].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!dbot.db.kicks.hasOwnProperty(req.params.user)) {
|
|
|
|
var kicks = '0';
|
|
|
|
} else {
|
|
|
|
var kicks = dbot.db.kicks[req.params.user];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!dbot.db.kickers.hasOwnProperty(req.params.user)) {
|
|
|
|
var kicked = '0';
|
|
|
|
} else {
|
|
|
|
var kicked = dbot.db.kickers[req.params.user];
|
|
|
|
}
|
|
|
|
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('user', { 'name': dbot.config.name, 'user': req.params.user,
|
2012-12-11 17:04:52 +01:00
|
|
|
'channel': channel, 'connection': connection, 'cleanUser': user,
|
|
|
|
'quotecount': quoteCount, 'kicks': kicks, 'kicked': kicked });
|
|
|
|
});
|
|
|
|
|
|
|
|
// Lists the quote categories
|
|
|
|
app.get('/quotes', function(req, res) {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('quotelist', { 'name': dbot.config.name, 'quotelist': Object.keys(dbot.db.quoteArrs) });
|
2012-12-11 17:04:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Lists quotes in a category
|
|
|
|
app.get('/quotes/:key', function(req, res) {
|
|
|
|
var key = req.params.key.toLowerCase();
|
|
|
|
if(dbot.db.quoteArrs.hasOwnProperty(key)) {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' });
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load random quote category page
|
|
|
|
app.get('/rq', function(req, res) {
|
|
|
|
var rCategory = Object.keys(dbot.db.quoteArrs).random();
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
|
2012-12-11 17:04:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Lists all of the polls
|
|
|
|
app.get('/polls', function(req, res) {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('polllist', { 'name': dbot.config.name, 'polllist': Object.keys(dbot.db.polls) });
|
2012-12-11 17:04:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Shows the results of a poll
|
|
|
|
app.get('/polls/:key', function(req, res) {
|
|
|
|
var key = req.params.key.toLowerCase();
|
|
|
|
if(dbot.db.polls.hasOwnProperty(key) && dbot.db.polls[key].hasOwnProperty('description')) {
|
|
|
|
// tally the votes
|
|
|
|
var totalVotes = 0;
|
|
|
|
for( var v in dbot.db.polls[key].votes ) {
|
|
|
|
var N = Number(dbot.db.polls[key].votes[v]);
|
|
|
|
if( !isNaN(N) ) {
|
|
|
|
totalVotes += N;
|
|
|
|
}
|
|
|
|
}
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('polls', { 'name': dbot.config.name, 'description': dbot.db.polls[key].description, 'votees': Object.keys(dbot.db.polls[key].votees), 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } });
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2012-12-17 18:18:31 +01:00
|
|
|
res.render('error', { 'name': dbot.config.name, 'message': 'No polls under that key.' });
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-12 19:36:12 +01:00
|
|
|
app.listen(dbot.config.web.webPort);
|
2012-12-11 17:04:52 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'web',
|
|
|
|
'ignorable': false,
|
|
|
|
|
|
|
|
'onDestroy': function() {
|
|
|
|
app.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return webInterface(dbot);
|
|
|
|
};
|