2012-12-24 06:47:47 +01:00
|
|
|
var express = require('express'),
|
2013-07-27 19:19:53 +02:00
|
|
|
passport = require('passport'),
|
|
|
|
passHash = require('password-hash'),
|
|
|
|
flash = require('connect-flash'),
|
2013-01-13 16:51:54 +01:00
|
|
|
_ = require('underscore')._,
|
2013-07-27 19:19:53 +02:00
|
|
|
fs = require('fs'),
|
|
|
|
LocalStrategy = require('passport-local').Strategy;
|
2012-12-11 17:04:52 +01:00
|
|
|
|
|
|
|
var webInterface = function(dbot) {
|
2013-06-04 01:00:23 +02:00
|
|
|
this.config = dbot.config.modules.web;
|
2013-04-14 19:21:24 +02:00
|
|
|
this.pub = 'public';
|
|
|
|
this.app = express();
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-04-14 19:21:24 +02:00
|
|
|
this.app.use(express.static(this.pub));
|
|
|
|
this.app.set('view engine', 'jade');
|
2013-07-27 19:19:53 +02:00
|
|
|
this.app.use(express.cookieParser());
|
2013-07-28 20:11:10 +02:00
|
|
|
this.app.use(express.bodyParser());
|
2013-07-27 19:19:53 +02:00
|
|
|
this.app.use(express.methodOverride());
|
|
|
|
this.app.use(express.session({ 'secret': 'wat' }));
|
|
|
|
this.app.use(flash());
|
|
|
|
|
|
|
|
this.app.use(passport.initialize());
|
|
|
|
this.app.use(passport.session());
|
2013-07-27 20:14:00 +02:00
|
|
|
this.app.use(this.app.router);
|
2013-07-27 19:19:53 +02:00
|
|
|
|
2013-07-27 20:14:00 +02:00
|
|
|
passport.serializeUser(function(user, done) {
|
2013-07-29 22:08:25 +02:00
|
|
|
console.log('serialising ' + user);
|
|
|
|
done(null, user.id);
|
2013-07-27 20:14:00 +02:00
|
|
|
});
|
|
|
|
|
2013-07-29 22:08:25 +02:00
|
|
|
passport.deserializeUser(function(id, done) {
|
|
|
|
dbot.api.users.getUser(id, function(user) {
|
|
|
|
console.log(id);
|
|
|
|
console.log(user);
|
|
|
|
done(null, user);
|
|
|
|
});
|
2013-07-27 20:14:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
passport.use(new LocalStrategy(function(username, password, callback) {
|
2013-07-27 19:19:53 +02:00
|
|
|
var splitUser = username.split('@'),
|
|
|
|
server = splitUser[1],
|
|
|
|
username = splitUser[0];
|
|
|
|
|
|
|
|
dbot.api.users.resolveUser(server, username, function(user) {
|
|
|
|
if(user) {
|
|
|
|
this.api.getWebUser(user.id, function(webUser) {
|
|
|
|
if(webUser) {
|
2013-07-28 20:11:10 +02:00
|
|
|
if(passHash.verify(password, webUser.password)) {
|
2013-07-27 19:19:53 +02:00
|
|
|
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));
|
2013-07-27 20:14:00 +02:00
|
|
|
}.bind(this)));
|
2013-06-04 01:00:23 +02:00
|
|
|
|
|
|
|
var server = this.app.listen(this.config.webPort);
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-15 14:58:13 +01:00
|
|
|
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)) {
|
2012-12-24 06:47:47 +01:00
|
|
|
var func = pages[p];
|
|
|
|
var mod = func.module;
|
2013-07-29 21:03:24 +02:00
|
|
|
this.app.get(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
|
2013-07-29 22:08:25 +02:00
|
|
|
_.extend(one, {
|
|
|
|
'name': dbot.config.name,
|
|
|
|
'user': req.user
|
|
|
|
});
|
2013-01-15 14:58:13 +01:00
|
|
|
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));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 14:58:13 +01:00
|
|
|
}.bind(this);
|
2013-01-15 17:54:51 +01:00
|
|
|
|
2013-04-30 21:33:26 +02:00
|
|
|
this.onLoad = function() {
|
|
|
|
var routes = _.pluck(dbot.modules.web.app.routes.get, 'path');
|
|
|
|
var moduleNames = _.keys(dbot.modules);
|
|
|
|
var indexModules = [];
|
|
|
|
|
2013-07-29 22:25:07 +02:00
|
|
|
// fix the thingy
|
2013-04-30 21:33:26 +02:00
|
|
|
_.each(moduleNames, function(moduleName) {
|
|
|
|
var modulePath = '/' + moduleName;
|
|
|
|
if(_.include(routes, modulePath)) {
|
|
|
|
indexModules.push(moduleName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.get('/', function(req, res) {
|
|
|
|
res.render('index', {
|
|
|
|
'name': dbot.config.name,
|
2013-07-29 22:08:25 +02:00
|
|
|
'user': req.user,
|
2013-04-30 21:33:26 +02:00
|
|
|
'routes': indexModules
|
|
|
|
});
|
|
|
|
});
|
2013-07-27 19:19:53 +02:00
|
|
|
|
|
|
|
this.app.get('/login', function(req, res) {
|
2013-07-27 20:14:00 +02:00
|
|
|
res.render('login', {
|
2013-07-27 19:31:00 +02:00
|
|
|
'user': req.user,
|
2013-07-27 20:14:00 +02:00
|
|
|
'message': req.flash('error')
|
2013-07-27 19:31:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.post('/login', passport.authenticate('local', {
|
|
|
|
'failureRedirect': '/login',
|
|
|
|
'failureFlash': true
|
|
|
|
}), function(req, res) {
|
2013-07-29 22:08:25 +02:00
|
|
|
if(req.body.redirect) {
|
|
|
|
res.redirect(req.body.redirect);
|
|
|
|
} else {
|
2013-07-29 22:45:35 +02:00
|
|
|
res.redirect('/');
|
2013-07-29 22:08:25 +02:00
|
|
|
}
|
2013-07-27 19:31:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.app.get('/logout', function(req, res) {
|
|
|
|
req.logout();
|
|
|
|
res.redirect('/');
|
2013-07-27 19:19:53 +02:00
|
|
|
});
|
2013-04-30 21:33:26 +02:00
|
|
|
}.bind(this);
|
|
|
|
|
2013-01-15 17:54:51 +01:00
|
|
|
this.onDestroy = function() {
|
|
|
|
server.close();
|
2013-04-13 00:53:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.api = {
|
|
|
|
'getUrl': function(path) {
|
2013-05-06 22:09:19 +02:00
|
|
|
if(path.charAt(0) == '/') path = path.substr(1);
|
2013-04-13 00:53:45 +02:00
|
|
|
if(this.config.externalPath) {
|
|
|
|
return this.config.externalPath + '/' + path;
|
|
|
|
} else {
|
2013-04-14 05:53:53 +02:00
|
|
|
return 'http://' + this.config.webHost + ':' + this.config.webPort + '/' + path;
|
2013-04-13 00:53:45 +02:00
|
|
|
}
|
2013-07-27 19:19:53 +02:00
|
|
|
},
|
|
|
|
|
2013-07-28 20:11:10 +02:00
|
|
|
'getWebUser': function(id, callback) {
|
2013-07-27 19:19:53 +02:00
|
|
|
this.db.read('web_users', id, function(err, webUser) {
|
2013-07-28 20:11:10 +02:00
|
|
|
callback(webUser);
|
2013-07-27 19:19:53 +02:00
|
|
|
});
|
2013-07-29 21:03:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'hasAccess': function(req, res, next) {
|
|
|
|
var module = req.route.path.split('/')[1];
|
|
|
|
module = dbot.modules[module];
|
|
|
|
|
|
|
|
if(module.config.requireWebLogin == true) {
|
|
|
|
if(req.isAuthenticated()) {
|
|
|
|
if(!_.isUndefined(module.config.webAccess)) {
|
|
|
|
var allowedUsers = dbot.config.admins;
|
|
|
|
if(module.config.webAccess == 'moderators') {
|
|
|
|
allowedUsers = _.union(allowedUsers, dbot.config.moderators);
|
|
|
|
}
|
|
|
|
if(module.config.webAccess == 'power_users') {
|
|
|
|
allowedUsers = _.union(allowedUsers, dbot.config.moderators);
|
|
|
|
allowedUsers = _.union(allowedUsers, dbot.config.power_users);
|
|
|
|
}
|
2013-07-29 22:36:27 +02:00
|
|
|
|
2013-07-29 21:03:24 +02:00
|
|
|
if(_.include(allowedUsers, req.user.primaryNick)) {
|
|
|
|
return next();
|
|
|
|
} else {
|
2013-07-29 22:45:35 +02:00
|
|
|
res.redirect('/');
|
2013-07-29 21:03:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-13 00:14:14 +02:00
|
|
|
console.log(req);
|
2013-07-29 21:03:24 +02:00
|
|
|
res.render('login', {
|
2013-07-29 22:08:25 +02:00
|
|
|
'message': 'You need to log in to access this module.',
|
2013-08-13 00:14:14 +02:00
|
|
|
'redirect': req.originalUrl
|
2013-07-29 21:03:24 +02:00
|
|
|
});
|
|
|
|
}
|
2013-07-29 21:12:16 +02:00
|
|
|
} else {
|
|
|
|
return next();
|
2013-07-29 21:03:24 +02:00
|
|
|
}
|
2013-07-27 19:19:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.commands = {
|
2013-07-28 20:11:10 +02:00
|
|
|
'~setwebpass': function(event) {
|
2013-07-27 19:19:53 +02:00
|
|
|
var newPass = event.input[1];
|
2013-07-28 20:11:10 +02:00
|
|
|
console.log(newPass);
|
2013-07-27 19:19:53 +02:00
|
|
|
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'));
|
|
|
|
});
|
2013-07-28 20:11:10 +02:00
|
|
|
}.bind(this));
|
2013-04-13 20:38:53 +02:00
|
|
|
}
|
2013-04-13 00:53:45 +02:00
|
|
|
};
|
2013-07-28 20:11:10 +02:00
|
|
|
this.commands['~setwebpass'].regex = [/^~setwebpass ([^ ]+)$/, 2]
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-15 14:58:13 +01:00
|
|
|
return new webInterface(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|