sync/user.js

377 lines
12 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +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-02-16 17:19:59 +00:00
var Rank = require("./rank.js");
var Auth = require("./auth.js");
var Channel = require("./channel.js").Channel;
var Server = require("./server.js");
var Database = require("./database.js");
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-02-16 05:02:42 +00:00
// Represents a client connected via socket.io
var User = function(socket, ip) {
this.ip = ip;
this.socket = socket;
this.loggedIn = false;
this.rank = Rank.Guest;
this.channel = null;
this.playerReady = false;
this.name = "";
this.initCallbacks();
2013-03-20 18:35:06 +00:00
if(Server.announcement != null) {
this.socket.emit("announcement", Server.announcement);
2013-03-20 18:35:06 +00:00
}
2013-02-16 05:02:42 +00:00
};
2013-03-20 23:10:23 +00:00
2013-02-16 05:02:42 +00:00
User.prototype.initCallbacks = function() {
this.socket.on("disconnect", function() {
2013-02-16 05:02:42 +00:00
if(this.channel != null)
this.channel.userLeave(this);
}.bind(this));
this.socket.on("joinChannel", function(data) {
if(data.name == undefined)
return;
2013-03-20 23:15:52 +00:00
if(!data.name.match(/^[a-zA-Z0-9]+$/))
2013-03-20 23:10:23 +00:00
return;
if(data.name.length > 100)
return;
data.name = data.name.toLowerCase();
2013-02-16 05:02:42 +00:00
// Channel already loaded
if(data.name in Server.channels) {
this.channel = Server.channels[data.name];
this.channel.userJoin(this);
}
// Channel not loaded
else {
Server.channels[data.name] = new Channel(data.name);
this.channel = Server.channels[data.name];
this.channel.userJoin(this);
}
}.bind(this));
this.socket.on("login", function(data) {
if(data.name == undefined || data.pw == undefined)
return;
if(data.pw.length > 100)
data.pw = data.pw.substring(0, 100);
2013-03-05 20:12:55 +00:00
if(this.name == "")
this.login(data.name, data.pw);
2013-02-16 05:02:42 +00:00
}.bind(this));
this.socket.on("register", function(data) {
if(data.name == undefined || data.pw == undefined)
return;
if(data.pw.length > 100)
data.pw = data.pw.substring(0, 100);
this.register(data.name, data.pw);
2013-02-16 05:02:42 +00:00
}.bind(this));
this.socket.on("assignLeader", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryChangeLeader(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("promote", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
2013-04-04 20:41:41 +00:00
this.channel.tryPromoteUser(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("demote", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
2013-04-04 20:41:41 +00:00
this.channel.tryDemoteUser(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("chatMsg", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryChat(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("playerReady", function() {
2013-02-16 05:02:42 +00:00
if(this.channel != null) {
this.channel.sendMediaUpdate(this);
}
this.playerReady = true;
}.bind(this));
this.socket.on("queue", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryQueue(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("unqueue", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryDequeue(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("moveMedia", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryMove(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("jumpTo", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryJumpTo(this, data);
}
}.bind(this));
this.socket.on("playNext", function() {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
2013-04-03 17:47:41 +00:00
this.channel.tryPlayNext(this);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("queueLock", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.trySetLock(this, data);
2013-03-16 20:39:58 +00:00
}
}.bind(this));
this.socket.on("mediaUpdate", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryUpdate(this, data);
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("searchLibrary", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
if(data.yt) {
var callback = function(vids) {
this.socket.emit("librarySearchResults", {
results: vids
});
}.bind(this);
this.channel.search(data.query, callback);
}
else {
this.socket.emit("librarySearchResults", {
results: this.channel.search(data.query)
});
}
2013-02-16 05:02:42 +00:00
}
}.bind(this));
this.socket.on("closePoll", function() {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryClosePoll(this);
2013-03-16 21:49:58 +00:00
}
}.bind(this));
this.socket.on("vote", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryVote(this, data);
2013-03-16 21:49:58 +00:00
}
}.bind(this));
this.socket.on("registerChannel", function(data) {
2013-03-17 17:14:34 +00:00
if(this.channel == null) {
this.socket.emit("channelRegistration", {
2013-03-17 17:14:34 +00:00
success: false,
error: "You're not in any channel!"
});
}
else {
this.channel.tryRegister(this);
}
}.bind(this));
this.socket.on("adm", function(data) {
2013-03-06 22:02:40 +00:00
if(Rank.hasPermission(this, "acp")) {
this.handleAdm(data);
}
}.bind(this));
2013-03-20 18:35:06 +00:00
this.socket.on("announce", function(data) {
2013-03-20 18:35:06 +00:00
if(Rank.hasPermission(this, "announce")) {
if(data.clear) {
Server.announcement = null;
}
else {
Server.io.sockets.emit("announcement", data);
2013-03-20 18:35:06 +00:00
Server.announcement = data;
}
}
}.bind(this));
this.socket.on("channelOpts", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryUpdateOptions(this, data);
}
}.bind(this));
this.socket.on("chatFilter", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryChangeFilter(this, data);
}
2013-04-01 21:02:09 +00:00
}.bind(this));
this.socket.on("updateMotd", function(data) {
2013-04-03 17:10:05 +00:00
if(this.channel != null) {
this.channel.tryUpdateMotd(this, data);
}
}.bind(this));
2013-04-02 19:07:22 +00:00
this.socket.on("voteskip", function(data) {
if(this.channel != null) {
2013-04-03 17:47:41 +00:00
this.channel.tryVoteskip(this);
2013-04-02 19:07:22 +00:00
}
}.bind(this));
2013-02-16 05:02:42 +00:00
}
2013-03-06 22:02:40 +00:00
// Handle administration
User.prototype.handleAdm = function(data) {
2013-03-23 18:29:47 +00:00
if(data.cmd == "listchannels") {
2013-03-06 22:02:40 +00:00
var chans = [];
for(var chan in Server.channels) {
2013-03-23 18:29:47 +00:00
var nowplaying = "-";
2013-04-06 21:08:26 +00:00
if(Server.channels[chan].media != null)
nowplaying = Server.channels[chan].media.title;
2013-03-06 22:02:40 +00:00
chans.push({
2013-03-23 18:29:47 +00:00
name: chan,
usercount: Server.channels[chan].users.length,
nowplaying: nowplaying
2013-03-06 22:02:40 +00:00
});
}
this.socket.emit("adm", {
2013-03-06 22:02:40 +00:00
cmd: "listchannels",
2013-03-23 18:29:47 +00:00
chans: chans
2013-03-06 22:02:40 +00:00
});
}
};
2013-02-16 05:02:42 +00:00
// Attempt to login
User.prototype.login = function(name, pw) {
2013-03-17 14:39:22 +00:00
if(this.channel != null && name != "") {
for(var i = 0; i < this.channel.users.length; i++) {
if(this.channel.users[i].name == name) {
this.socket.emit("login", {
2013-03-17 14:39:22 +00:00
success: false,
error: "The username " + name + " is already in use on this channel"
});
return false;
}
}
}
2013-02-16 05:02:42 +00:00
// No password => try guest login
if(pw == "") {
2013-04-03 17:10:05 +00:00
// Sorry bud, can't take that name
2013-02-16 05:02:42 +00:00
if(Auth.isRegistered(name)) {
this.socket.emit("login", {
2013-02-16 05:02:42 +00:00
success: false,
error: "That username is already taken"
});
return false;
}
// YOUR ARGUMENT IS INVALID
else if(!Auth.validateName(name)) {
this.socket.emit("login", {
2013-02-16 05:02:42 +00:00
success: false,
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
});
}
else {
2013-03-27 19:28:51 +00:00
Logger.syslog.log(this.ip + " signed in as " + name);
2013-02-16 05:02:42 +00:00
this.name = name;
this.loggedIn = false;
this.socket.emit("login", {
2013-02-16 05:02:42 +00:00
success: true
});
this.socket.emit("rank", {
2013-02-16 05:02:42 +00:00
rank: this.rank
});
if(this.channel != null) {
2013-03-27 19:28:51 +00:00
this.channel.logger.log(this.ip + " signed in as " + name);
2013-02-16 05:02:42 +00:00
this.channel.broadcastNewUser(this);
}
}
}
else {
var row;
if((row = Auth.login(name, pw))) {
2013-03-17 16:49:25 +00:00
this.loggedIn = true;
this.socket.emit("login", {
2013-02-16 05:02:42 +00:00
success: true
});
2013-03-27 19:28:51 +00:00
Logger.syslog.log(this.ip + " logged in as " + name);
2013-02-16 05:02:42 +00:00
var chanrank = (this.channel != null) ? this.channel.getRank(name)
: Rank.Guest;
2013-03-17 17:14:34 +00:00
var rank = (chanrank > row.global_rank) ? chanrank
2013-02-16 05:02:42 +00:00
: row.global_rank;
2013-03-17 17:14:34 +00:00
this.rank = (this.rank > rank) ? this.rank : rank;
this.socket.emit("rank", {
2013-02-16 05:02:42 +00:00
rank: this.rank
});
this.name = name;
if(this.channel != null) {
2013-03-27 19:28:51 +00:00
this.channel.logger.log(this.ip + " logged in as " + name);
2013-02-16 05:02:42 +00:00
this.channel.broadcastNewUser(this);
}
}
// Wrong password
else {
this.socket.emit("login", {
2013-02-16 05:02:42 +00:00
success: false,
error: "Invalid username/password pair"
});
return false;
}
}
}
// Attempt to register a user account
User.prototype.register = function(name, pw) {
if(pw == "") {
2013-02-16 05:02:42 +00:00
// Sorry bud, password required
this.socket.emit("register", {
2013-02-16 05:02:42 +00:00
success: false,
error: "You must provide a password"
});
return false;
}
else if(Auth.isRegistered(name)) {
this.socket.emit("register", {
2013-02-16 05:02:42 +00:00
success: false,
error: "That username is already taken"
});
return false;
}
else if(!Auth.validateName(name)) {
this.socket.emit("register", {
2013-02-16 05:02:42 +00:00
success: false,
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
});
}
else if(Auth.register(name, pw)) {
2013-02-16 05:02:42 +00:00
console.log(this.ip + " registered " + name);
this.socket.emit("register", {
2013-02-16 05:02:42 +00:00
success: true
});
this.login(name, pw);
2013-02-16 05:02:42 +00:00
}
else {
this.socket.emit("register", {
2013-02-16 05:02:42 +00:00
success: false,
error: "[](/ppshrug) Registration Failed."
});
}
}
exports.User = User;