Start fucking api.js

This commit is contained in:
calzoneman 2013-08-15 19:51:03 -05:00
parent e7b22997c7
commit 5969558320
1 changed files with 115 additions and 118 deletions

101
api.js
View File

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