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

194 lines
6.7 KiB
JavaScript
Raw Normal View History

2012-12-24 06:47:47 +01:00
var express = require('express'),
passport = require('passport'),
passHash = require('password-hash'),
flash = require('connect-flash'),
2013-01-13 16:51:54 +01:00
_ = require('underscore')._,
fs = require('fs'),
2014-09-04 16:41:57 +02:00
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
expressSession = require('express-session'),
methodOverride = require('method-override'),
LocalStrategy = require('passport-local').Strategy;
var webInterface = function(dbot) {
this.config = dbot.config.modules.web;
this.indexLinks = {};
2013-04-14 19:21:24 +02:00
this.pub = 'public';
this.app = express();
2013-04-14 19:21:24 +02:00
this.app.use(express.static(this.pub));
this.app.set('view engine', 'jade');
2014-09-04 16:19:23 +02:00
this.app.use(cookieParser());
2014-09-04 16:41:57 +02:00
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ 'extended': true }));
2014-09-04 16:19:23 +02:00
this.app.use(methodOverride());
this.app.use(expressSession({ 'secret': 'wat' }));
this.app.use(flash());
this.app.use(passport.initialize());
this.app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
2014-09-04 16:41:57 +02:00
dbot.api.users.getUser(id, function(err, user) {
2014-10-21 23:25:50 +02:00
// Get user access level. Sigh.
if(user) {
user.access = 'user';
if(_.include(dbot.config.admins, user.primaryNick)) {
user.access = 'admin';
} else if(_.include(dbot.config.moderators, user.primaryNick)) {
user.access = 'moderator';
} else if(_.include(dbot.config.power_users, user.primaryNick)) {
user.access = 'power_user';
}
}
console.log(user);
done(null, user);
});
});
passport.use(new LocalStrategy(function(username, password, callback) {
var splitUser = username.split('@'),
server = splitUser[1],
username = splitUser[0];
2013-08-31 17:38:57 +02:00
if(!server || !username) return callback(null, false, { 'message':
'Please provide a username in the format of name@server (Servers: ' +
_.keys(dbot.config.servers).join(', ') + ')' });
if(!_.has(dbot.config.servers, server)) return callback(null, false, { 'message':
'Please provide a valid server (Servers: ' +
_.keys(dbot.config.servers).join(', ') + ')' });
2013-08-31 17:38:57 +02:00
2014-09-04 16:19:23 +02:00
dbot.api.users.resolveUser(server, username, function(err, user) {
if(user) {
this.api.getWebUser(user.id, function(webUser) {
if(webUser) {
if(passHash.verify(password, webUser.password)) {
return callback(null, user);
} else {
return callback(null, false, { 'message': 'Incorrect password.' });
}
} else {
return callback(null, false, { 'message': 'Use ~setwebpass to set up your account for web login.' });
}
});
} else {
return callback(null, false, { 'message': 'Unknown user' });
}
}.bind(this));
}.bind(this)));
var server = this.app.listen(this.config.webPort);
this.reloadPages = function() {
var pages = dbot.pages;
2012-12-24 06:47:47 +01:00
for(var p in pages) {
2013-01-13 16:51:54 +01:00
if(_.has(pages, p)) {
var func = pages[p],
2014-10-21 23:25:50 +02:00
mod = func.module,
type = func.type || 'get';
2014-10-21 23:25:50 +02:00
this.app[type](p, this.api.hasAccess, (function(req, resp) {
2012-12-24 06:47:47 +01:00
// Crazy shim to seperate module views.
var shim = Object.create(resp);
shim.render = (function(view, one, two) {
// Render with express.js
_.extend(one, {
'name': dbot.config.name,
2013-12-12 20:19:44 +01:00
'user': req.user,
2013-12-13 01:08:20 +01:00
'routes': dbot.modules.web.indexLinks
});
resp.render(this.module + '/' + view, one, two);
2012-12-24 06:47:47 +01:00
}).bind(this);
shim.render_core = resp.render;
this.call(this.module, req, shim);
}).bind(func));
}
}
}.bind(this);
this.onLoad = function() {
2014-01-02 14:11:42 +01:00
this.reloadPages();
2014-09-04 16:19:23 +02:00
var routes = _.pluck(_.without(_.pluck(this.app._router.stack, 'route'), undefined), 'path'),
moduleNames = _.keys(dbot.modules);
_.each(moduleNames, function(moduleName) {
var modulePath = '/' + moduleName;
if(_.include(routes, modulePath)) {
moduleName = moduleName.charAt(0).toUpperCase() +
moduleName.slice(1);
this.indexLinks[modulePath] = moduleName;
}
}.bind(this));
this.app.get('/', function(req, res) {
res.render('index', {
'name': dbot.config.name,
'user': req.user,
'routes': this.indexLinks
});
}.bind(this));
this.app.get('/login', function(req, res) {
res.render('login', {
2013-07-27 19:31:00 +02:00
'user': req.user,
2014-10-21 17:08:26 +02:00
'message': req.flash('error'),
'routes': this.indexLinks
2013-07-27 19:31:00 +02:00
});
2014-10-21 17:08:26 +02:00
}.bind(this));
2013-07-27 19:31:00 +02:00
this.app.post('/login', passport.authenticate('local', {
'failureRedirect': '/login',
2014-10-20 00:55:45 +02:00
'failureFlash': true,
2014-10-21 17:08:26 +02:00
'routes': this.indexLinks
2013-07-27 19:31:00 +02:00
}), function(req, res) {
if(req.body.redirect) {
res.redirect(req.body.redirect);
} else {
2013-07-29 22:45:35 +02:00
res.redirect('/');
}
2013-07-27 19:31:00 +02:00
});
this.app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
2013-08-18 19:42:23 +02:00
if(_.has(dbot.modules, 'log')) {
2013-12-30 15:16:03 +01:00
dbot.api.log.ignoreCommand('setwebpass');
2013-08-18 19:42:23 +02:00
}
}.bind(this);
this.onDestroy = function() {
server.close();
};
this.commands = {
'~setwebpass': function(event) {
var newPass = event.input[1];
this.api.getWebUser(event.rUser.id, function(webUser) {
if(!webUser) {
webUser = {
'id': event.rUser.id,
'password': false
}
}
webUser.password = passHash.generate(newPass);
this.db.save('web_users', webUser.id, webUser, function(result) {
event.reply(dbot.t('web_pass_set'));
});
}.bind(this));
2013-04-13 20:38:53 +02:00
}
};
2013-12-29 19:38:24 +01:00
this.commands['~setwebpass'].regex = [/^setwebpass ([^ ]+)$/, 2]
};
exports.fetch = function(dbot) {
return new webInterface(dbot);
};