3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00
dbot/modules/web.js

130 lines
4.9 KiB
JavaScript
Raw Normal View History

2011-09-13 22:23:46 +02:00
var express = require('express');
var webInterface = function(dbot) {
2011-11-08 19:59:29 +01:00
var pub = 'public';
2011-09-13 22:55:20 +02:00
var app = express.createServer();
app.use(express.compiler({ src: pub, enable: ['sass'] }));
app.use(express.static(pub));
app.set('view engine', 'jade');
2011-09-13 22:23:46 +02:00
app.get('/', function(req, res) {
2012-06-15 19:57:47 +02:00
res.render('index', { 'name': dbot.name });
2011-11-08 16:36:18 +01:00
});
app.get('/connections', function(req, res) {
var connections = Object.keys(dbot.instance.connections);
res.render('connections', { 'name': dbot.name, 'connections': connections });
});
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);
res.render('channels', { 'name': dbot.name, 'connection': connection, 'channels': channels});
} else {
res.render('error', { 'name': dbot.name, 'message': 'No such connection.' });
}
});
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 = connections[connection].channels[channel].nicks;
res.render('users', { 'name': dbot.name, 'connection': connection,
'channel': channel, 'nicks': nicks });
} else {
res.render('error', { 'name': dbot.name, 'message': 'No such connection or channel.' });
}
});
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;
}
2012-11-25 19:54:12 +01:00
if(!dbot.db.kicks.hasOwnProperty(req.params.user)) {
var kicks = '0';
} else {
2012-11-25 19:54:12 +01:00
var kicks = dbot.db.kicks[req.params.user];
}
2012-11-25 19:54:12 +01:00
if(!dbot.db.kickers.hasOwnProperty(req.params.user)) {
var kicked = '0';
} else {
2012-11-25 19:54:12 +01:00
var kicked = dbot.db.kickers[req.params.user];
}
res.render('user', { 'name': dbot.name, 'user': req.params.user,
'channel': channel, 'connection': connection, 'cleanUser': user,
'quotecount': quoteCount, 'kicks': kicks, 'kicked': kicked });
});
// Lists the quote categories
2011-11-08 16:36:18 +01:00
app.get('/quotes', function(req, res) {
2012-03-10 18:49:57 +01:00
res.render('quotelist', { 'name': dbot.name, 'quotelist': Object.keys(dbot.db.quoteArrs) });
2011-09-13 22:23:46 +02:00
});
2011-09-15 13:30:52 +02:00
// Lists quotes in a category
2011-09-15 13:30:52 +02:00
app.get('/quotes/:key', function(req, res) {
2011-09-15 13:34:01 +02:00
var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) {
2012-03-10 18:49:57 +01:00
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
2011-09-15 13:30:52 +02:00
} else {
2012-03-10 18:49:57 +01:00
res.render('error', { 'name': dbot.name, 'message': 'No quotes under that key.' });
2011-09-15 13:30:52 +02:00
}
});
// Load random quote category page
2012-02-11 16:55:14 +01:00
app.get('/rq', function(req, res) {
var rCategory = Object.keys(dbot.db.quoteArrs).random();
2012-03-10 18:49:57 +01:00
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
});
2012-06-15 17:15:02 +02:00
// Lists all of the polls
app.get('/polls', function(req, res) {
res.render('polllist', { 'name': dbot.name, 'polllist': Object.keys(dbot.db.polls) });
});
// 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')) {
2012-06-15 17:15:02 +02:00
// 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-06-15 18:20:05 +02:00
res.render('polls', { 'name': dbot.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-06-15 17:15:02 +02:00
} else {
res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' });
}
});
2011-11-08 16:36:18 +01:00
2012-03-10 19:21:50 +01:00
app.listen(dbot.webPort);
2011-09-14 18:24:32 +02:00
return {
'name': 'web',
'ignorable': false,
'onDestroy': function() {
app.close();
}
2011-09-14 18:24:32 +02:00
};
2011-09-13 22:23:46 +02:00
};
exports.fetch = function(dbot) {
return webInterface(dbot);
};