2013-04-25 16:00:06 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var Auth = require("./auth");
|
|
|
|
var Logger = require("./logger");
|
2013-04-25 16:00:06 +00:00
|
|
|
var apilog = new Logger.Logger("api.log");
|
2013-07-15 22:57:33 +00:00
|
|
|
var ActionLog = require("./actionlog");
|
2013-05-03 20:53:10 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
|
2013-06-22 01:28:21 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
module.exports = function (Server) {
|
2013-07-28 15:49:29 +00:00
|
|
|
function getIP(req) {
|
|
|
|
var raw = req.connection.remoteAddress;
|
|
|
|
var forward = req.header("x-forwarded-for");
|
|
|
|
if(Server.cfg["trust-x-forward"] && forward) {
|
|
|
|
var ip = forward.split(",")[0];
|
|
|
|
Logger.syslog.log("REVPROXY " + raw + " => " + ip);
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2013-07-16 15:46:09 +00:00
|
|
|
var API = function () {
|
2013-07-15 22:57:33 +00:00
|
|
|
|
2013-07-16 15:46:09 +00:00
|
|
|
}
|
|
|
|
API.prototype = {
|
2013-07-15 22:57:33 +00:00
|
|
|
handle: function (path, req, res) {
|
|
|
|
var parts = path.split("/");
|
|
|
|
var last = parts[parts.length - 1];
|
|
|
|
var params = {};
|
|
|
|
if(last.indexOf("?") != -1) {
|
|
|
|
parts[parts.length - 1] = last.substring(0, last.indexOf("?"));
|
|
|
|
var plist = last.substring(last.indexOf("?") + 1).split("&");
|
|
|
|
for(var i = 0; i < plist.length; i++) {
|
|
|
|
var kv = plist[i].split("=");
|
|
|
|
if(kv.length != 2) {
|
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
params[unescape(kv[0])] = unescape(kv[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var i = 0; i < parts.length; i++) {
|
|
|
|
parts[i] = unescape(parts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parts.length != 2) {
|
2013-04-25 16:00:06 +00:00
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
if(parts[0] == "json") {
|
|
|
|
res.callback = params.callback || false;
|
|
|
|
if(!(parts[1] in this.jsonHandlers)) {
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
error: "Unknown endpoint: " + parts[1]
|
|
|
|
}, null, 4));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.jsonHandlers[parts[1]](params, req, res);
|
|
|
|
}
|
|
|
|
else if(parts[0] == "plain") {
|
|
|
|
if(!(parts[1] in this.plainHandlers)) {
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.plainHandlers[parts[1]](params, req, res);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(400);
|
|
|
|
}
|
|
|
|
},
|
2013-04-25 16:00:06 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
sendJSON: function (res, obj) {
|
|
|
|
var response = JSON.stringify(obj, null, 4);
|
|
|
|
if(res.callback) {
|
|
|
|
response = res.callback + "(" + response + ")";
|
|
|
|
}
|
|
|
|
var len = unescape(encodeURIComponent(response)).length;
|
2013-04-25 16:00:06 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
res.setHeader("Content-Type", "application/json");
|
|
|
|
res.setHeader("Content-Length", len);
|
|
|
|
res.end(response);
|
|
|
|
},
|
2013-04-26 03:50:12 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
sendPlain: function (res, str) {
|
|
|
|
if(res.callback) {
|
|
|
|
str = res.callback + "('" + str + "')";
|
|
|
|
}
|
|
|
|
var len = unescape(encodeURIComponent(str)).length;
|
|
|
|
|
|
|
|
res.setHeader("Content-Type", "text/plain");
|
|
|
|
res.setHeader("Content-Length", len);
|
|
|
|
res.end(response);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleChannelData: function (params, req, res) {
|
|
|
|
var clist = params.channel || "";
|
|
|
|
clist = clist.split(",");
|
|
|
|
var data = [];
|
|
|
|
for(var j = 0; j < clist.length; j++) {
|
|
|
|
var cname = clist[j];
|
|
|
|
if(!cname.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-26 03:50:12 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var d = {
|
|
|
|
name: cname,
|
|
|
|
loaded: Server.channelLoaded(cname)
|
|
|
|
};
|
2013-05-04 16:40:46 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
if(d.loaded) {
|
|
|
|
var chan = Server.getChannel(cname);
|
|
|
|
d.pagetitle = chan.opts.pagetitle;
|
|
|
|
d.media = chan.playlist.current ? chan.playlist.current.media.pack() : {};
|
|
|
|
d.usercount = chan.users.length;
|
|
|
|
d.users = [];
|
|
|
|
for(var i = 0; i < chan.users.length; i++) {
|
|
|
|
if(chan.users[i].name) {
|
|
|
|
d.users.push(chan.users[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.chat = [];
|
|
|
|
for(var i = 0; i < chan.chatbuffer.length; i++) {
|
|
|
|
d.chat.push(chan.chatbuffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.push(d);
|
|
|
|
}
|
2013-05-04 16:40:46 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleChannelList: function (params, req, res) {
|
|
|
|
if(params.filter == "public") {
|
|
|
|
var all = Server.channels;
|
|
|
|
var clist = [];
|
|
|
|
for(var key in all) {
|
|
|
|
if(all[key].opts.show_public) {
|
2013-07-16 15:49:32 +00:00
|
|
|
clist.push(all[key].name);
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
2013-07-16 15:48:11 +00:00
|
|
|
this.handleChannelData({channel: clist.join(",")}, req, res);
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
var session = params.session || "";
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
var clist = [];
|
|
|
|
for(var key in Server.channels) {
|
2013-07-16 15:50:08 +00:00
|
|
|
clist.push(Server.channels[key].name);
|
2013-05-01 22:49:34 +00:00
|
|
|
}
|
2013-07-16 16:12:00 +00:00
|
|
|
this.handleChannelData({channel: clist.join(",")}, req, res);
|
2013-07-15 22:57:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleLogin: function (params, req, res) {
|
|
|
|
var session = params.session || "";
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
|
|
|
|
if(pw == "" && session == "") {
|
|
|
|
if(!Auth.isRegistered(name)) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: ""
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "That username is already taken"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(row) {
|
2013-07-31 03:23:20 +00:00
|
|
|
if(row.global_rank >= 255)
|
|
|
|
ActionLog.record(getIP(req), name, "login-success");
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ActionLog.record(getIP(req), name, "login-failure");
|
|
|
|
this.sendJSON(res, {
|
|
|
|
error: "Invalid username/password",
|
|
|
|
success: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handlePasswordChange: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var oldpw = params.oldpw || "";
|
|
|
|
var newpw = params.newpw || "";
|
|
|
|
if(oldpw == "" || newpw == "") {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Old password and new password cannot be empty"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var row = Auth.login(name, oldpw);
|
|
|
|
if(row) {
|
|
|
|
ActionLog.record(getIP(req), name, "password-change");
|
|
|
|
var success = Auth.setUserPassword(name, newpw);
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: success,
|
|
|
|
error: success ? "" : "Change password failed",
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username/password"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2013-05-29 18:19:43 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
handlePasswordReset: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
2013-07-27 14:11:31 +00:00
|
|
|
var email = params.email || "";
|
2013-07-15 22:57:33 +00:00
|
|
|
var ip = getIP(req);
|
2013-05-29 18:19:43 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var hash = false;
|
|
|
|
try {
|
|
|
|
hash = Server.db.generatePasswordReset(ip, name, email);
|
|
|
|
ActionLog.record(ip, name, "password-reset-generate", email);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-30 18:07:29 +00:00
|
|
|
|
2013-07-28 15:49:29 +00:00
|
|
|
if(!Server.cfg["enable-mail"]) {
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "This server does not have email enabled. Contact an administrator"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!email) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "You don't have a recovery email address set. Contact an administrator"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var msg = [
|
|
|
|
"A password reset request was issued for your account `",
|
|
|
|
name,
|
|
|
|
"` on ",
|
2013-07-28 15:49:29 +00:00
|
|
|
Server.cfg["domain"],
|
2013-07-15 22:57:33 +00:00
|
|
|
". 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: ",
|
2013-07-28 15:49:29 +00:00
|
|
|
Server.cfg["domain"],
|
2013-07-15 22:57:33 +00:00
|
|
|
"/reset.html?",
|
|
|
|
hash
|
|
|
|
].join("");
|
|
|
|
|
|
|
|
var mail = {
|
2013-07-28 15:49:29 +00:00
|
|
|
from: "CyTube Services <" + Server.cfg["mail-from"] + ">",
|
2013-07-15 22:57:33 +00:00
|
|
|
to: email,
|
|
|
|
subject: "Password reset request",
|
|
|
|
text: msg
|
|
|
|
};
|
2013-07-21 01:38:03 +00:00
|
|
|
var api = this;
|
2013-07-28 21:10:35 +00:00
|
|
|
Server.cfg["nodemailer"].sendMail(mail, function(err, response) {
|
2013-07-15 22:57:33 +00:00
|
|
|
if(err) {
|
|
|
|
Logger.errlog.log("Mail fail: " + err);
|
2013-07-21 01:38:03 +00:00
|
|
|
api.sendJSON(res, {
|
2013-07-15 22:57:33 +00:00
|
|
|
success: false,
|
|
|
|
error: "Email failed. Contact an admin if this persists."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2013-07-21 01:38:03 +00:00
|
|
|
api.sendJSON(res, {
|
2013-07-15 22:57:33 +00:00
|
|
|
success: true
|
|
|
|
});
|
|
|
|
|
2013-07-28 15:49:29 +00:00
|
|
|
if(Server.cfg["debug"]) {
|
2013-07-15 22:57:33 +00:00
|
|
|
Logger.syslog.log(response);
|
|
|
|
}
|
|
|
|
}
|
2013-05-30 18:07:29 +00:00
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handlePasswordRecover: function (params, req, res) {
|
|
|
|
var hash = params.hash || "";
|
|
|
|
var ip = getIP(req);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var info = Server.db.recoverPassword(hash);
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
name: info[0],
|
|
|
|
pw: info[1]
|
|
|
|
});
|
2013-07-28 21:10:35 +00:00
|
|
|
ActionLog.record(ip, info[0], "password-recover-success");
|
|
|
|
Logger.syslog.log(ip + " recovered password for " + info[0]);
|
2013-07-15 22:57:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(e) {
|
2013-07-28 21:10:35 +00:00
|
|
|
ActionLog.record(ip, "", "password-recover-failure");
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleProfileGet: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
var prof = Server.db.getProfile(name);
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
profile_image: prof.profile_image,
|
|
|
|
profile_text: prof.profile_text
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleProfileChange: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
var session = params.session || "";
|
2013-07-27 14:11:31 +00:00
|
|
|
var img = params.profile_image || "";
|
|
|
|
var text = params.profile_text || "";
|
2013-07-15 22:57:33 +00:00
|
|
|
|
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid login"
|
|
|
|
});
|
|
|
|
return;
|
2013-05-30 18:07:29 +00:00
|
|
|
}
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var result = Server.db.setProfile(name, {
|
|
|
|
image: img,
|
|
|
|
text: text
|
|
|
|
});
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, {
|
|
|
|
success: result,
|
|
|
|
error: result ? "" : "Internal error. Contact an administrator"
|
|
|
|
});
|
2013-05-30 01:04:22 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var all = Server.channels;
|
|
|
|
for(var n in all) {
|
|
|
|
var chan = all[n];
|
|
|
|
for(var i = 0; i < chan.users.length; i++) {
|
|
|
|
if(chan.users[i].name.toLowerCase() == name) {
|
|
|
|
chan.users[i].profile = {
|
|
|
|
image: img,
|
|
|
|
text: text
|
|
|
|
};
|
|
|
|
chan.broadcastUserUpdate(chan.users[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEmailChange: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
2013-07-27 14:11:31 +00:00
|
|
|
var email = params.email || "";
|
2013-07-15 22:57:33 +00:00
|
|
|
// perhaps my email regex isn't perfect, but there's no freaking way
|
|
|
|
// I'm implementing this monstrosity:
|
|
|
|
// <http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html>
|
|
|
|
if(!email.match(/^[a-z0-9_\.]+@[a-z0-9_\.]+[a-z]+$/)) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid email"
|
|
|
|
});
|
|
|
|
return;
|
2013-05-30 18:34:54 +00:00
|
|
|
}
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Nice try, but no."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-29 15:49:09 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
if(pw == "") {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Password cannot be empty"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var row = Auth.login(name, pw);
|
|
|
|
if(row) {
|
|
|
|
var success = Server.db.setUserEmail(name, email);
|
|
|
|
ActionLog.record(getIP(req), name, "email-update", email);
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: success,
|
|
|
|
error: success ? "" : "Email update failed",
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username/password"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleRegister: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
if(ActionLog.tooManyRegistrations(getIP(req))) {
|
|
|
|
ActionLog.record(getIP(req), name, "register-failure",
|
|
|
|
"Too many recent registrations from this IP");
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Your IP address has registered several accounts in "+
|
|
|
|
"the past 48 hours. Please wait a while or ask an "+
|
|
|
|
"administrator for assistance."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-04 18:01:04 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
if(pw == "") {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "You must provide a password"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(Auth.isRegistered(name)) {
|
|
|
|
ActionLog.record(getIP(req), name, "register-failure",
|
|
|
|
"Name taken");
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "That username is already taken"
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(!Auth.validateName(name)) {
|
|
|
|
ActionLog.record(getIP(req), name, "register-failure",
|
|
|
|
"Invalid name");
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var session = Auth.register(name, pw);
|
|
|
|
if(session) {
|
|
|
|
ActionLog.record(getIP(req), name, "register-success");
|
|
|
|
Logger.syslog.log(getIP(req) + " registered " + name);
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: session
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Registration error. Contact an admin for assistance."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-04-26 03:50:12 +00:00
|
|
|
|
2013-08-06 15:20:15 +00:00
|
|
|
handleListUserChannels: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
var session = params.session || "";
|
|
|
|
|
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid login"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var channels = Server.db.listUserChannels(name);
|
|
|
|
|
|
|
|
this.sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
channels: channels
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
handleAdmReports: function (params, req, res) {
|
|
|
|
this.sendJSON(res, {
|
|
|
|
error: "Not implemented"
|
2013-04-26 03:50:12 +00:00
|
|
|
});
|
2013-07-15 22:57:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleReadActionLog: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
var session = params.session || "";
|
2013-07-31 03:21:32 +00:00
|
|
|
var types = params.actions || "";
|
2013-07-15 22:57:33 +00:00
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-13 16:05:58 +00:00
|
|
|
|
2013-07-31 03:21:32 +00:00
|
|
|
var actiontypes = types.split(",");
|
|
|
|
var actions = ActionLog.readLog(actiontypes);
|
2013-07-15 22:57:33 +00:00
|
|
|
this.sendJSON(res, actions);
|
|
|
|
},
|
2013-05-03 20:53:10 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// Helper function
|
|
|
|
pipeLast: function (res, file, len) {
|
|
|
|
fs.stat(file, function(err, data) {
|
|
|
|
if(err) {
|
|
|
|
res.send(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var start = data.size - len;
|
|
|
|
if(start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
var end = data.size - 1;
|
|
|
|
fs.createReadStream(file, {start: start, end: end}).pipe(res);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleReadLog: function (params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
var session = params.session || "";
|
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
2013-05-03 20:53:10 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var type = params.type || "";
|
|
|
|
if(type == "sys") {
|
|
|
|
this.pipeLast(res, "sys.log", 1024*1024);
|
|
|
|
}
|
|
|
|
else if(type == "err") {
|
|
|
|
this.pipeLast(res, "error.log", 1024*1024);
|
|
|
|
}
|
|
|
|
else if(type == "channel") {
|
|
|
|
var chan = params.channel || "";
|
|
|
|
fs.exists("chanlogs/" + chan + ".log", function(exists) {
|
|
|
|
if(exists) {
|
|
|
|
this.pipeLast(res, "chanlogs/" + chan + ".log", 1024*1024);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(404);
|
|
|
|
}
|
2013-07-16 15:46:09 +00:00
|
|
|
}.bind(this));
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-07-15 22:57:33 +00:00
|
|
|
res.send(400);
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-16 15:46:09 +00:00
|
|
|
|
|
|
|
var api = new API();
|
|
|
|
|
|
|
|
api.plainHandlers = {
|
|
|
|
"readlog" : api.handleReadLog.bind(api)
|
|
|
|
};
|
|
|
|
|
|
|
|
api.jsonHandlers = {
|
|
|
|
"channeldata" : api.handleChannelData.bind(api),
|
|
|
|
"listloaded" : api.handleChannelList.bind(api),
|
|
|
|
"login" : api.handleLogin.bind(api),
|
|
|
|
"register" : api.handleRegister.bind(api),
|
|
|
|
"changepass" : api.handlePasswordChange.bind(api),
|
|
|
|
"resetpass" : api.handlePasswordReset.bind(api),
|
|
|
|
"recoverpw" : api.handlePasswordRecover.bind(api),
|
|
|
|
"setprofile" : api.handleProfileChange.bind(api),
|
|
|
|
"getprofile" : api.handleProfileGet.bind(api),
|
2013-08-06 15:20:15 +00:00
|
|
|
"listuserchannels": api.handleListUserChannels.bind(api),
|
2013-07-16 15:46:09 +00:00
|
|
|
"setemail" : api.handleEmailChange.bind(api),
|
|
|
|
"admreports" : api.handleAdmReports.bind(api),
|
|
|
|
"readactionlog" : api.handleReadActionLog.bind(api)
|
|
|
|
};
|
|
|
|
|
|
|
|
return api;
|
2013-05-03 20:53:10 +00:00
|
|
|
}
|