sync/user.js

690 lines
21 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
2013-03-24 02:28:20 +00:00
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:
2013-03-24 02:28:20 +00:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2013-03-24 02:28:20 +00:00
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 Channel = require("./channel.js").Channel;
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-08-18 17:49:54 +00:00
var $util = require("./utilities");
2013-02-16 05:02:42 +00:00
// Represents a client connected via socket.io
2013-07-16 03:01:12 +00:00
var User = function(socket, Server) {
this.ip = socket._ip;
this.server = Server;
2013-02-16 05:02:42 +00:00
this.socket = socket;
this.loggedIn = false;
this.saverank = false;
2013-05-22 19:38:16 +00:00
this.rank = Rank.Anonymous;
2013-06-20 23:45:21 +00:00
this.global_rank = Rank.Anonymous;
2013-02-16 05:02:42 +00:00
this.channel = null;
this.name = "";
this.meta = {
2013-06-25 14:18:33 +00:00
afk: false,
icon: false
};
2013-06-25 14:18:33 +00:00
this.muted = false;
2013-04-23 19:17:42 +00:00
this.throttle = {};
this.flooded = {};
2013-08-29 00:25:53 +00:00
this.queueLimiter = $util.newRateLimiter();
this.profile = {
image: "",
text: ""
};
this.awaytimer = false;
this.autoAFK();
2013-02-16 05:02:42 +00:00
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-04-23 19:17:42 +00:00
// Throttling/cooldown
User.prototype.noflood = function(name, hz) {
var time = new Date().getTime();
if(!(name in this.throttle)) {
this.throttle[name] = [time];
return false;
}
else if(name in this.flooded && time < this.flooded[name]) {
this.socket.emit("noflood", {
action: name,
msg: "You're still on cooldown!"
});
return true;
}
else {
this.throttle[name].push(time);
var diff = (time - this.throttle[name][0]) / 1000.0;
2013-04-30 15:30:59 +00:00
// Twice might be an accident, more than that is probably spam
if(this.throttle[name].length > 2) {
2013-04-23 19:17:42 +00:00
var rate = this.throttle[name].length / diff;
this.throttle[name] = [time];
if(rate > hz) {
this.flooded[name] = time + 5000;
this.socket.emit("noflood", {
action: name,
msg: "Stop doing that so fast! Cooldown: 5s"
});
return true;
}
return false;
}
}
}
2013-03-20 23:10:23 +00:00
User.prototype.setAFK = function (afk) {
if(this.channel === null)
return;
2013-08-01 19:12:57 +00:00
if(this.meta.afk === afk)
return;
var chan = this.channel;
this.meta.afk = afk;
2013-08-01 13:39:10 +00:00
if(afk) {
if(chan.afkers.indexOf(this.name.toLowerCase()) == -1)
chan.afkers.push(this.name.toLowerCase());
2013-08-07 17:11:58 +00:00
if(chan.voteskip)
chan.voteskip.unvote(this.ip);
}
2013-08-01 13:39:10 +00:00
else {
if(chan.afkers.indexOf(this.name.toLowerCase()) != -1)
chan.afkers.splice(chan.afkers.indexOf(this.name.toLowerCase()), 1);
this.autoAFK();
}
2013-08-01 13:39:10 +00:00
chan.checkVoteskipPass();
2013-08-01 19:12:57 +00:00
chan.sendAll("setAFK", {
name: this.name,
afk: afk
});
}
User.prototype.autoAFK = function () {
if(this.awaytimer)
clearTimeout(this.awaytimer);
if(this.channel === null || this.channel.opts.afk_timeout == 0)
return;
this.awaytimer = setTimeout(function () {
this.setAFK(true);
}.bind(this), this.channel.opts.afk_timeout * 1000);
}
2013-02-16 05:02:42 +00:00
User.prototype.initCallbacks = function() {
2013-08-18 17:21:34 +00:00
var self = this;
self.socket.on("disconnect", function() {
self.awaytimer && clearTimeout(self.awaytimer);
if(self.channel != null)
self.channel.userLeave(self);
});
self.socket.on("joinChannel", function(data) {
if(self.channel != null)
2013-06-02 22:09:52 +00:00
return;
if(typeof data.name != "string")
return;
if(!data.name.match(/^[\w-_]{1,30}$/)) {
2013-08-18 17:21:34 +00:00
self.socket.emit("errorMsg", {
msg: "Invalid channel name. Channel names may consist of"+
" 1-30 characters in the set a-z, A-Z, 0-9, -, and _"
});
2013-08-18 17:21:34 +00:00
self.socket.emit("kick", {
reason: "Bad channel name"
});
return;
}
data.name = data.name.toLowerCase();
2013-08-18 17:21:34 +00:00
self.channel = self.server.getChannel(data.name);
if(self.loggedIn) {
self.channel.getRank(self.name, function (err, rank) {
if(!err && rank > self.rank)
self.rank = rank;
});
}
2013-08-18 17:21:34 +00:00
self.channel.userJoin(self);
2013-08-22 04:10:55 +00:00
self.autoAFK();
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("login", function(data) {
var name = data.name || "";
var pw = data.pw || "";
var session = data.session || "";
if(pw.length > 100)
pw = pw.substring(0, 100);
2013-08-18 17:21:34 +00:00
if(self.name == "")
self.login(name, pw, session);
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("assignLeader", function(data) {
if(self.channel != null) {
self.channel.tryChangeLeader(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("promote", function(data) {
if(self.channel != null) {
self.channel.tryPromoteUser(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("demote", function(data) {
if(self.channel != null) {
self.channel.tryDemoteUser(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setChannelRank", function(data) {
if(self.channel != null) {
self.channel.trySetRank(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("banName", function(data) {
if(self.channel != null) {
self.channel.banName(self, data.name || "");
2013-05-21 16:17:01 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-21 16:17:01 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("banIP", function(data) {
if(self.channel != null) {
self.channel.tryIPBan(self, data);
2013-05-21 16:17:01 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-21 16:17:01 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("unban", function(data) {
if(self.channel != null) {
self.channel.tryUnban(self, data);
2013-05-21 16:17:01 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-21 16:17:01 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("chatMsg", function(data) {
if(self.channel != null) {
if(data.msg.indexOf("/afk") != 0) {
2013-08-18 17:21:34 +00:00
self.setAFK(false);
self.autoAFK();
2013-07-30 00:06:01 +00:00
}
2013-08-18 17:21:34 +00:00
self.channel.tryChat(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("newPoll", function(data) {
if(self.channel != null) {
self.channel.tryOpenPoll(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("playerReady", function() {
if(self.channel != null) {
self.channel.sendMediaUpdate(self);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("requestPlaylist", function() {
if(self.channel != null) {
self.channel.sendPlaylist(self);
2013-04-17 19:05:45 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-04-17 19:05:45 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("queue", function(data) {
if(self.channel != null) {
self.channel.tryQueue(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setTemp", function(data) {
if(self.channel != null) {
self.channel.trySetTemp(self, data);
2013-05-04 22:54:28 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-04 22:54:28 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("delete", function(data) {
if(self.channel != null) {
self.channel.tryDequeue(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("uncache", function(data) {
if(self.channel != null) {
self.channel.tryUncache(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("moveMedia", function(data) {
if(self.channel != null) {
self.channel.tryMove(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("jumpTo", function(data) {
if(self.channel != null) {
self.channel.tryJumpTo(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("playNext", function() {
if(self.channel != null) {
self.channel.tryPlayNext(self);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("clearPlaylist", function() {
if(self.channel != null) {
self.channel.tryClearqueue(self);
2013-04-22 20:37:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-04-22 20:37:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("shufflePlaylist", function() {
if(self.channel != null) {
self.channel.tryShufflequeue(self);
2013-04-22 20:37:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-04-22 20:37:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("togglePlaylistLock", function() {
if(self.channel != null) {
self.channel.tryToggleLock(self);
2013-03-16 20:39:58 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-03-16 20:39:58 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("mediaUpdate", function(data) {
if(self.channel != null) {
self.channel.tryUpdate(self, data);
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("searchMedia", function(data) {
if(self.channel != null) {
2013-06-11 19:41:03 +00:00
if(data.source == "yt") {
2013-08-17 20:47:11 +00:00
var searchfn = self.server.infogetter.Getters["ytSearch"];
searchfn(data.query.split(" "), function (e, vids) {
if(!e) {
self.socket.emit("searchResults", {
results: vids
});
}
});
} else {
self.channel.search(data.query, function (vids) {
self.socket.emit("searchResults", {
results: vids
});
});
}
2013-02-16 05:02:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("closePoll", function() {
if(self.channel != null) {
self.channel.tryClosePoll(self);
2013-03-16 21:49:58 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-03-16 21:49:58 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("vote", function(data) {
if(self.channel != null) {
self.channel.tryVote(self, data);
2013-03-16 21:49:58 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-03-16 21:49:58 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("registerChannel", function(data) {
if(self.channel == null) {
self.socket.emit("channelRegistration", {
2013-03-17 17:14:34 +00:00
success: false,
error: "You're not in any channel!"
});
}
else {
2013-08-18 17:21:34 +00:00
self.channel.tryRegister(self);
2013-03-17 17:14:34 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-03-17 17:14:34 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("unregisterChannel", function() {
if(self.channel == null) {
2013-05-13 19:41:29 +00:00
return;
}
2013-08-18 17:21:34 +00:00
self.channel.unregister(self);
});
2013-05-13 19:41:29 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setOptions", function(data) {
if(self.channel != null) {
self.channel.tryUpdateOptions(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("setPermissions", function(data) {
if(self.channel != null) {
self.channel.tryUpdatePermissions(self, data);
2013-05-22 19:38:16 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-22 19:38:16 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setChannelCSS", function(data) {
if(self.channel != null) {
self.channel.trySetCSS(self, data);
2013-05-15 15:34:27 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-15 15:34:27 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setChannelJS", function(data) {
if(self.channel != null) {
self.channel.trySetJS(self, data);
2013-05-15 15:34:27 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-05-15 15:34:27 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("updateFilter", function(data) {
if(self.channel != null) {
self.channel.tryUpdateFilter(self, data);
2013-06-18 15:51:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-06-18 15:51:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("removeFilter", function(data) {
if(self.channel != null) {
self.channel.tryRemoveFilter(self, data);
2013-06-18 15:51:42 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-06-18 15:51:42 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("moveFilter", function(data) {
if(self.channel != null) {
self.channel.tryMoveFilter(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-04-01 21:02:09 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("setMotd", function(data) {
if(self.channel != null) {
self.channel.tryUpdateMotd(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("requestLoginHistory", function() {
if(self.channel != null) {
self.channel.sendLoginHistory(self);
2013-06-18 03:57:29 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-06-18 03:57:29 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("requestBanlist", function() {
if(self.channel != null) {
self.channel.sendBanlist(self);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("requestChatFilters", function() {
if(self.channel != null) {
self.channel.sendChatFilters(self);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("requestChannelRanks", function() {
if(self.channel != null) {
if(self.noflood("requestChannelRanks", 0.25))
2013-05-26 16:43:11 +00:00
return;
2013-08-18 17:21:34 +00:00
self.channel.sendChannelRanks(self);
2013-04-29 23:59:51 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-04-29 23:59:51 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("voteskip", function(data) {
if(self.channel != null) {
self.channel.tryVoteskip(self);
2013-04-02 19:07:22 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("listPlaylists", function(data) {
if(self.name == "" || self.rank < 1) {
self.socket.emit("listPlaylists", {
2013-06-01 15:59:04 +00:00
pllist: [],
error: "You must be logged in to manage playlists"
});
return;
}
2013-08-18 17:21:34 +00:00
self.server.db.listUserPlaylists(self.name, function (err, list) {
if(err)
list = [];
for(var i = 0; i < list.length; i++) {
list[i].time = $util.formatTime(list[i].time);
2013-08-18 17:21:34 +00:00
}
self.socket.emit("listPlaylists", {
pllist: list,
});
2013-06-01 15:59:04 +00:00
});
2013-08-18 17:21:34 +00:00
});
2013-06-01 15:59:04 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("savePlaylist", function(data) {
if(self.rank < 1) {
self.socket.emit("savePlaylist", {
2013-06-01 15:59:04 +00:00
success: false,
error: "You must be logged in to manage playlists"
});
return;
}
2013-08-18 17:21:34 +00:00
if(self.channel == null) {
self.socket.emit("savePlaylist", {
2013-06-01 15:59:04 +00:00
success: false,
error: "Not in a channel"
});
return;
}
if(typeof data.name != "string") {
return;
}
2013-08-18 17:21:34 +00:00
var pl = self.channel.playlist.items.toArray();
self.server.db.saveUserPlaylist(pl, self.name, data.name,
function (err, res) {
if(err) {
self.socket.emit("savePlaylist", {
success: false,
error: err
});
return;
}
self.socket.emit("savePlaylist", {
success: true
});
self.server.db.listUserPlaylists(self.name,
function (err, list) {
if(err)
list = [];
for(var i = 0; i < list.length; i++) {
list[i].time = $util.formatTime(list[i].time);
2013-08-18 17:21:34 +00:00
}
self.socket.emit("listPlaylists", {
pllist: list,
});
});
2013-06-01 20:56:23 +00:00
});
2013-08-18 17:21:34 +00:00
});
2013-06-01 19:42:08 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("queuePlaylist", function(data) {
if(self.channel != null) {
self.channel.tryQueuePlaylist(self, data);
2013-06-01 19:42:08 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-06-01 20:56:23 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("deletePlaylist", function(data) {
2013-06-01 20:56:23 +00:00
if(typeof data.name != "string") {
return;
}
2013-08-18 17:21:34 +00:00
self.server.db.deleteUserPlaylist(self.name, data.name,
function () {
self.server.db.listUserPlaylists(self.name,
function (err, list) {
if(err)
list = [];
for(var i = 0; i < list.length; i++) {
list[i].time = $util.formatTime(list[i].time);
2013-08-18 17:21:34 +00:00
}
self.socket.emit("listPlaylists", {
pllist: list,
});
});
2013-06-01 20:56:23 +00:00
});
2013-08-18 17:21:34 +00:00
});
2013-06-20 23:45:21 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("readChanLog", function () {
if(self.channel !== null) {
self.channel.tryReadLog(self);
}
2013-08-18 17:21:34 +00:00
});
2013-08-18 17:21:34 +00:00
self.socket.on("acp-init", function() {
if(self.global_rank >= Rank.Siteadmin)
self.server.acp.init(self);
});
2013-06-23 14:25:49 +00:00
2013-08-18 17:21:34 +00:00
self.socket.on("borrow-rank", function(rank) {
if(self.global_rank < 255)
2013-06-23 14:25:49 +00:00
return;
2013-08-18 17:21:34 +00:00
if(rank > self.global_rank)
2013-06-23 14:25:49 +00:00
return;
2013-08-18 17:21:34 +00:00
self.rank = rank;
self.socket.emit("rank", rank);
if(self.channel != null)
self.channel.broadcastUserUpdate(self);
2013-06-23 14:25:49 +00:00
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
}
2013-05-03 03:13:46 +00:00
var lastguestlogin = {};
2013-02-16 05:02:42 +00:00
// Attempt to login
User.prototype.login = function(name, pw, session) {
2013-08-17 20:54:23 +00:00
var self = this;
2013-02-16 05:02:42 +00:00
// No password => try guest login
if(pw == "" && session == "") {
2013-08-17 20:54:23 +00:00
if(self.ip in lastguestlogin) {
var diff = (Date.now() - lastguestlogin[self.ip])/1000;
if(diff < self.server.cfg["guest-login-delay"]) {
self.socket.emit("login", {
2013-05-03 03:13:46 +00:00
success: false,
error: ["Guest logins are restricted to one per ",
2013-08-17 20:54:23 +00:00
self.server.cfg["guest-login-delay"]
2013-07-28 15:49:29 +00:00
+ " seconds per IP. ",
2013-05-03 03:13:46 +00:00
"This restriction does not apply to registered users."
].join("")
});
return false;
}
}
2013-08-17 20:54:23 +00:00
if(!$util.isValidUserName(name)) {
self.socket.emit("login", {
success: false,
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
});
return;
}
self.server.db.isUsernameTaken(name, function (err, taken) {
if(err) {
self.socket.emit("login", {
2013-06-19 21:54:27 +00:00
success: false,
2013-08-17 20:54:23 +00:00
error: "Internal error: " + err
2013-06-19 21:54:27 +00:00
});
2013-08-17 20:54:23 +00:00
return;
2013-06-19 21:54:27 +00:00
}
2013-08-17 20:54:23 +00:00
if(taken) {
self.socket.emit("login", {
2013-06-19 21:54:27 +00:00
success: false,
2013-08-17 20:54:23 +00:00
error: "That username is taken"
2013-06-19 21:54:27 +00:00
});
2013-08-17 20:54:23 +00:00
return;
2013-06-19 21:54:27 +00:00
}
2013-08-17 20:54:23 +00:00
if(self.channel != null) {
for(var i = 0; i < self.channel.users.length; i++) {
if(self.channel.users[i].name == name) {
self.socket.emit("login", {
success: false,
error: "That name is already taken on self channel"
});
return;
2013-07-03 03:24:03 +00:00
}
}
2013-06-19 21:54:27 +00:00
}
2013-08-17 20:54:23 +00:00
lastguestlogin[self.ip] = Date.now();
self.rank = Rank.Guest;
Logger.syslog.log(self.ip + " signed in as " + name);
self.server.db.recordVisit(self.ip, name);
self.name = name;
self.loggedIn = false;
self.socket.emit("login", {
success: true,
name: name
2013-02-16 05:02:42 +00:00
});
2013-08-17 20:54:23 +00:00
self.socket.emit("rank", self.rank);
if(self.channel != null) {
self.channel.logger.log(self.ip + " signed in as " + name);
self.channel.broadcastNewUser(self);
2013-06-19 21:54:27 +00:00
}
});
2013-08-17 20:54:23 +00:00
} else {
self.server.db.userLogin(name, pw, session, function (err, row) {
if(err) {
2013-08-18 19:21:42 +00:00
self.server.actionlog.record(self.ip, name, "login-failure");
2013-08-17 20:54:23 +00:00
self.socket.emit("login", {
2013-06-19 21:54:27 +00:00
success: false,
2013-08-17 20:54:23 +00:00
error: err
2013-06-19 21:54:27 +00:00
});
2013-08-17 20:54:23 +00:00
return;
2013-02-16 05:02:42 +00:00
}
2013-08-17 20:54:23 +00:00
if(self.channel != null) {
for(var i = 0; i < self.channel.users.length; i++) {
2013-08-18 19:21:42 +00:00
if(self.channel.users[i].name.toLowerCase() == name.toLowerCase()) {
2013-08-17 20:54:23 +00:00
self.channel.kick(self.channel.users[i], "Duplicate login");
}
}
}
if(self.global_rank >= 255)
2013-08-17 23:44:48 +00:00
self.server.actionlog.record(self.ip, name, "login-success");
2013-08-17 20:54:23 +00:00
self.loggedIn = true;
self.socket.emit("login", {
success: true,
session: row.session_hash,
name: name
2013-02-16 05:02:42 +00:00
});
2013-08-17 20:54:23 +00:00
Logger.syslog.log(self.ip + " logged in as " + name);
self.server.db.recordVisit(self.ip, name);
self.profile = {
image: row.profile_image,
text: row.profile_text
};
self.global_rank = row.global_rank;
2013-08-18 17:21:34 +00:00
var afterRankLookup = function () {
self.socket.emit("rank", self.rank);
self.name = name;
if(self.channel != null) {
self.channel.logger.log(self.ip + " logged in as " +
name);
self.channel.broadcastNewUser(self);
}
};
if(self.channel !== null) {
2013-08-18 19:21:42 +00:00
self.channel.getRank(name, function (err, rank) {
if(!err) {
self.saverank = true;
2013-08-18 17:21:34 +00:00
self.rank = rank;
} else {
self.saverank = false;
2013-08-18 19:21:42 +00:00
self.rank = self.global_rank;
}
2013-08-18 17:21:34 +00:00
afterRankLookup();
});
} else {
self.rank = self.global_rank;
afterRankLookup();
2013-08-17 20:54:23 +00:00
}
2013-02-16 05:02:42 +00:00
});
}
}
2013-07-16 03:01:12 +00:00
module.exports = User;