Start fucking api.js

This commit is contained in:
calzoneman 2013-08-15 19:51:03 -05:00
parent e7b22997c7
commit 5969558320

101
api.js
View file

@ -14,7 +14,6 @@ var Logger = require("./logger");
var ActionLog = require("./actionlog"); var ActionLog = require("./actionlog");
var fs = require("fs"); var fs = require("fs");
module.exports = function (Server) { module.exports = function (Server) {
function getIP(req) { function getIP(req) {
var raw = req.connection.remoteAddress; var raw = req.connection.remoteAddress;
@ -52,6 +51,7 @@ module.exports = function (Server) {
} }
var app = Server.app; var app = Server.app;
var db = Server.db;
/* <https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol> */ /* <https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol> */
app.get("/api/coffee", function (req, res) { app.get("/api/coffee", function (req, res) {
@ -283,22 +283,21 @@ module.exports = function (Server) {
var ip = getIP(req); var ip = getIP(req);
var hash = false; var hash = false;
try { db.genPasswordReset(ip, name, email, function (err, hash) {
hash = Server.db.generatePasswordReset(ip, name, email); if(err) {
ActionLog.record(ip, name, "password-reset-generate", email);
} catch(e) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: e error: err
}); });
return; return;
} }
ActionLog.record(ip, name, "password-reset-generate", email);
if(!Server.cfg["enable-mail"]) { if(!Server.cfg["enable-mail"]) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "This server does not have email recovery enabled."+ error: "This server does not have email recovery " +
" Contact an administrator for assistance." "enabled. Contact an administrator for " +
"assistance."
}); });
return; return;
} }
@ -312,12 +311,13 @@ module.exports = function (Server) {
return; return;
} }
var msg = "A password reset request was issued for your account '"+ var msg = "A password reset request was issued for your " +
name + "' on " + Server.cfg["domain"] + ". This request"+ "account '"+ name + "' on " + Server.cfg["domain"] +
" is valid for 24 hours. If you did not initiate this, "+ ". This request is valid for 24 hours. If you did "+
"there is no need to take action. To reset your "+ "not initiate this, there is no need to take action."+
"password, copy and paste the following link into your "+ " To reset your password, copy and paste the " +
"browser: " + Server.cfg["domain"] + "/reset.html?"+hash; "following link into your browser: " +
Server.cfg["domain"] + "/reset.html?"+hash;
var mail = { var mail = {
from: "CyTube Services <" + Server.cfg["mail-from"] + ">", from: "CyTube Services <" + Server.cfg["mail-from"] + ">",
@ -341,6 +341,7 @@ module.exports = function (Server) {
} }
}); });
}); });
});
/* password recovery */ /* password recovery */
app.get("/api/account/passwordrecover", function (req, res) { app.get("/api/account/passwordrecover", function (req, res) {
@ -348,21 +349,22 @@ module.exports = function (Server) {
var hash = req.query.hash; var hash = req.query.hash;
var ip = getIP(req); var ip = getIP(req);
try { db.recoverUserPassword(hash, function (err, auth) {
var info = Server.db.recoverPassword(hash); if(err) {
res.jsonp({
success: true,
name: info[0],
pw: info[1]
});
ActionLog.record(ip, info[0], "password-recover-success");
} catch(e) {
ActionLog.record(ip, "", "password-recover-failure", hash); ActionLog.record(ip, "", "password-recover-failure", hash);
res.jsonp({ res.jsonp({
success: false, success: false,
error: e error: err
}); });
return;
} }
ActionLog.record(ip, info[0], "password-recover-success");
res.jsonp({
success: true,
name: auth.name,
pw: auth.pw
});
});
}); });
/* profile retrieval */ /* profile retrieval */
@ -370,19 +372,21 @@ module.exports = function (Server) {
res.type("application/jsonp"); res.type("application/jsonp");
var name = req.params.user; var name = req.params.user;
try { db.getUserProfile(name, function (err, profile) {
var prof = Server.db.getProfile(name); if(err) {
res.jsonp({
success: true,
profile_image: prof.profile_image,
profile_text: prof.profile_text
});
} catch(e) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: e error: err
}); });
return;
} }
res.jsonp({
success: true,
profile_image: profile.profile_image,
profile_text: profile.profile_text
});
});
}); });
/* profile change */ /* profile change */
@ -403,24 +407,17 @@ module.exports = function (Server) {
return; return;
} }
var result = Server.db.setProfile(name, { db.setUserProfile(name, { image: img, text: text },
image: img, function (err, dbres) {
text: text if(err) {
});
if(!result) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "Server error. Contact an administrator for assistance" error: err
}); });
return; return;
} }
res.jsonp({ res.jsonp({ success: true });
success: true
});
// Update profile on all channels the user is connected to
name = name.toLowerCase(); name = name.toLowerCase();
for(var i in Server.channels) { for(var i in Server.channels) {
var chan = Server.channels[i]; var chan = Server.channels[i];
@ -435,7 +432,7 @@ module.exports = function (Server) {
} }
} }
} }
});
}); });
/* set email */ /* set email */
@ -470,14 +467,13 @@ module.exports = function (Server) {
return; return;
} }
var success = Server.db.setUserEmail(name, email); db.setUserEmail(name, email, function (err, dbres) {
if(!success) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "Email update failed. Contact an administrator "+ error: err
"for assistance."
}); });
return false; return;
} }
ActionLog.record(getIP(req), name, "email-update", email); ActionLog.record(getIP(req), name, "email-update", email);
@ -486,6 +482,7 @@ module.exports = function (Server) {
session: row.session_hash session: row.session_hash
}); });
}); });
});
/* my channels */ /* my channels */
app.get("/api/account/mychannels", function (req, res) { app.get("/api/account/mychannels", function (req, res) {