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-04-25 21:16:53 +00:00
|
|
|
var Auth = require("./auth.js");
|
2013-04-25 16:00:06 +00:00
|
|
|
var Server = require("./server.js");
|
|
|
|
var Logger = require("./logger.js");
|
|
|
|
var apilog = new Logger.Logger("api.log");
|
2013-05-03 17:52:14 +00:00
|
|
|
var Database = require("./database.js");
|
2013-05-03 20:53:10 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
|
|
|
|
var plainHandlers = {
|
|
|
|
"readlog" : handleReadLog
|
|
|
|
};
|
2013-04-25 16:00:06 +00:00
|
|
|
|
|
|
|
var jsonHandlers = {
|
2013-04-25 21:16:53 +00:00
|
|
|
"channeldata": handleChannelData,
|
2013-04-26 03:50:12 +00:00
|
|
|
"listloaded" : handleChannelList,
|
|
|
|
"login" : handleLogin,
|
2013-05-03 17:52:14 +00:00
|
|
|
"register" : handleRegister,
|
2013-05-04 18:01:04 +00:00
|
|
|
"changepass" : handlePasswordChange,
|
2013-05-03 17:52:14 +00:00
|
|
|
"globalbans" : handleGlobalBans,
|
|
|
|
"admreports" : handleAdmReports
|
2013-04-25 16:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function handle(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) {
|
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parts[0] == "json") {
|
2013-04-26 04:57:46 +00:00
|
|
|
res.callback = params.callback || false;
|
2013-04-25 16:00:06 +00:00
|
|
|
if(!(parts[1] in jsonHandlers)) {
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
error: "Unknown endpoint: " + parts[1]
|
|
|
|
}, null, 4));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jsonHandlers[parts[1]](params, req, res);
|
|
|
|
}
|
2013-05-03 20:53:10 +00:00
|
|
|
else if(parts[0] == "plain") {
|
|
|
|
if(!(parts[1] in plainHandlers)) {
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
plainHandlers[parts[1]](params, req, res);
|
|
|
|
}
|
2013-04-25 16:00:06 +00:00
|
|
|
else {
|
|
|
|
res.send(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.handle = handle;
|
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
function sendJSON(res, obj) {
|
|
|
|
var response = JSON.stringify(obj, null, 4);
|
2013-04-26 04:57:46 +00:00
|
|
|
if(res.callback) {
|
|
|
|
response = res.callback + "(" + response + ")";
|
|
|
|
}
|
2013-04-26 03:50:12 +00:00
|
|
|
var len = unescape(encodeURIComponent(response)).length;
|
|
|
|
|
|
|
|
res.setHeader("Content-Type", "application/json");
|
|
|
|
res.setHeader("Content-Length", len);
|
|
|
|
res.end(response);
|
|
|
|
}
|
|
|
|
|
2013-05-04 16:40:46 +00:00
|
|
|
function sendPlain(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);
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:00:06 +00:00
|
|
|
function handleChannelData(params, req, res) {
|
2013-04-25 21:16:53 +00:00
|
|
|
var clist = params.channel || "";
|
|
|
|
clist = clist.split(",");
|
|
|
|
var data = [];
|
|
|
|
for(var j = 0; j < clist.length; j++) {
|
|
|
|
var cname = clist[j];
|
2013-05-26 15:38:38 +00:00
|
|
|
if(!cname.match(/^[a-zA-Z0-9-_]+$/)) {
|
2013-04-25 21:16:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var d = {
|
2013-04-25 16:00:06 +00:00
|
|
|
name: cname,
|
|
|
|
loaded: (cname in Server.channels)
|
|
|
|
};
|
|
|
|
|
2013-04-25 21:16:53 +00:00
|
|
|
if(d.loaded) {
|
2013-04-25 16:00:06 +00:00
|
|
|
var chan = Server.channels[cname];
|
2013-05-12 15:46:19 +00:00
|
|
|
d.pagetitle = chan.opts.pagetitle;
|
2013-05-12 15:41:14 +00:00
|
|
|
d.media = chan.media ? chan.media.pack() : {};
|
2013-04-25 21:16:53 +00:00
|
|
|
d.usercount = chan.users.length;
|
|
|
|
d.users = [];
|
2013-04-25 16:00:06 +00:00
|
|
|
for(var i = 0; i < chan.users.length; i++) {
|
|
|
|
if(chan.users[i].name) {
|
2013-04-25 21:16:53 +00:00
|
|
|
d.users.push(chan.users[i].name);
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 21:16:53 +00:00
|
|
|
d.chat = [];
|
2013-04-25 16:00:06 +00:00
|
|
|
for(var i = 0; i < chan.chatbuffer.length; i++) {
|
2013-04-25 21:16:53 +00:00
|
|
|
d.chat.push(chan.chatbuffer[i]);
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 21:16:53 +00:00
|
|
|
data.push(d);
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
sendJSON(res, data);
|
2013-04-25 16:00:06 +00:00
|
|
|
}
|
2013-04-25 21:16:53 +00:00
|
|
|
|
|
|
|
function handleChannelList(params, req, res) {
|
2013-05-01 22:49:34 +00:00
|
|
|
if(params.filter == "public") {
|
|
|
|
var clist = [];
|
|
|
|
for(var key in Server.channels) {
|
|
|
|
if(Server.channels[key].opts.show_public) {
|
|
|
|
clist.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleChannelData({channel: clist.join(",")}, req, res);
|
|
|
|
}
|
2013-04-26 03:50:12 +00:00
|
|
|
var session = params.session || "";
|
2013-04-25 21:16:53 +00:00
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
2013-04-26 03:50:12 +00:00
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(!row || row.global_rank < 255) {
|
2013-04-25 21:16:53 +00:00
|
|
|
res.send(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var clist = [];
|
|
|
|
for(var key in Server.channels) {
|
|
|
|
clist.push(key);
|
|
|
|
}
|
|
|
|
handleChannelData({channel: clist.join(",")}, req, res);
|
|
|
|
}
|
2013-04-26 03:50:12 +00:00
|
|
|
|
|
|
|
function handleLogin(params, req, res) {
|
|
|
|
var session = params.session || "";
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
|
2013-04-26 17:50:31 +00:00
|
|
|
if(pw == "" && session == "") {
|
2013-04-26 04:04:51 +00:00
|
|
|
if(!Auth.isRegistered(name)) {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: ""
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "That username is already taken"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
var row = Auth.login(name, pw, session);
|
|
|
|
if(row) {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: row.session_hash
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendJSON(res, {
|
2013-05-01 18:02:06 +00:00
|
|
|
error: "Invalid session",
|
2013-04-26 03:50:12 +00:00
|
|
|
success: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 18:01:04 +00:00
|
|
|
function handlePasswordChange(params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var oldpw = params.oldpw || "";
|
|
|
|
var newpw = params.newpw || "";
|
|
|
|
if(oldpw == "" || newpw == "") {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Old password and new password cannot be empty"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var row = Auth.login(name, oldpw);
|
|
|
|
if(row) {
|
|
|
|
var success = Auth.setUserPassword(name, newpw);
|
|
|
|
sendJSON(res, {
|
|
|
|
success: success,
|
|
|
|
error: success ? "" : "Change password failed"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "Invalid username or password"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
function handleRegister(params, req, res) {
|
|
|
|
var name = params.name || "";
|
|
|
|
var pw = params.pw || "";
|
|
|
|
|
|
|
|
if(pw == "") {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "You must provide a password"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(Auth.isRegistered(name)) {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "That username is already taken"
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(!Auth.validateName(name)) {
|
|
|
|
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) {
|
|
|
|
Logger.syslog.log(this.ip + " registered " + name);
|
|
|
|
sendJSON(res, {
|
|
|
|
success: true,
|
|
|
|
session: session
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendJSON(res, {
|
|
|
|
success: false,
|
|
|
|
error: "I dunno what went wrong"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 17:52:14 +00:00
|
|
|
|
|
|
|
function handleGlobalBans(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var action = params.action || "list";
|
|
|
|
if(action == "list") {
|
|
|
|
var gbans = Database.refreshGlobalBans();
|
|
|
|
sendJSON(res, gbans);
|
|
|
|
}
|
|
|
|
else if(action == "add") {
|
|
|
|
var ip = params.ip || "";
|
|
|
|
var reason = params.reason || "";
|
|
|
|
if(!ip.match(/\d+\.\d+\.(\d+\.(\d+)?)?/)) {
|
|
|
|
sendJSON(res, {
|
|
|
|
error: "Invalid IP address"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var result = Database.addGlobalBan(ip, reason);
|
|
|
|
sendJSON(res, {
|
|
|
|
success: result,
|
|
|
|
ip: ip,
|
|
|
|
reason: reason
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(action == "remove") {
|
|
|
|
var ip = params.ip || "";
|
|
|
|
if(!ip.match(/\d+\.\d+\.(\d+\.(\d+)?)?/)) {
|
|
|
|
sendJSON(res, {
|
|
|
|
error: "Invalid IP address"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var result = Database.liftGlobalBan(ip);
|
|
|
|
sendJSON(res, {
|
|
|
|
success: result,
|
|
|
|
ip: ip,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendJSON(res, {
|
|
|
|
error: "Invalid action: " + action
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAdmReports(params, req, res) {
|
|
|
|
sendJSON(res, {
|
|
|
|
error: "Not implemented"
|
|
|
|
});
|
|
|
|
}
|
2013-05-03 20:53:10 +00:00
|
|
|
|
|
|
|
// Helper function
|
|
|
|
function pipeLast(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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleReadLog(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;
|
|
|
|
}
|
2013-05-04 16:40:46 +00:00
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
2013-05-03 20:53:10 +00:00
|
|
|
|
|
|
|
var type = params.type || "";
|
|
|
|
if(type == "sys") {
|
|
|
|
pipeLast(res, "sys.log", 1024*1024);
|
|
|
|
}
|
|
|
|
else if(type == "err") {
|
|
|
|
pipeLast(res, "error.log", 1024*1024);
|
|
|
|
}
|
|
|
|
else if(type == "channel") {
|
|
|
|
var chan = params.channel || "";
|
|
|
|
fs.exists("chanlogs/" + chan + ".log", function(exists) {
|
|
|
|
if(exists) {
|
|
|
|
pipeLast(res, "chanlogs/" + chan + ".log", 1024*1024);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(400);
|
|
|
|
}
|
|
|
|
}
|