sync/lib/user.js

738 lines
22 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 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-10-11 21:31:40 +00:00
var ActionLog = require("./actionlog");
var Server = require("./server");
var ACP = require("./acp");
var InfoGetter = require("./get-info");
2013-02-16 05:02:42 +00:00
// Represents a client connected via socket.io
2013-10-11 21:31:40 +00:00
var User = function (socket) {
2013-07-16 03:01:12 +00:00
this.ip = socket._ip;
2013-10-11 21:31:40 +00:00
this.server = Server.getServer();
2013-02-16 05:02:42 +00:00
this.socket = socket;
this.loggedIn = false;
this.loggingIn = false;
this.saverank = false;
2013-10-11 21:31:40 +00:00
this.rank = -1
this.global_rank = -1;
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-10-11 21:31:40 +00:00
if (this.server.announcement !== null) {
this.socket.emit("announcement", this.server.announcement);
2013-03-20 18:35:06 +00:00
}
2013-02-16 05:02:42 +00:00
};
2013-09-18 23:27:42 +00:00
User.prototype.inChannel = function () {
return this.channel !== null && !this.channel.dead;
};
2013-04-23 19:17:42 +00:00
// Throttling/cooldown
2013-09-26 18:29:36 +00:00
User.prototype.noflood = function (name, hz) {
2013-04-23 19:17:42 +00:00
var time = new Date().getTime();
2013-09-26 18:29:36 +00:00
if (!(name in this.throttle)) {
2013-04-23 19:17:42 +00:00
this.throttle[name] = [time];
return false;
2013-09-26 18:29:36 +00:00
} else if (name in this.flooded && time < this.flooded[name]) {
2013-04-23 19:17:42 +00:00
this.socket.emit("noflood", {
action: name,
msg: "You're still on cooldown!"
});
return true;
2013-09-26 18:29:36 +00:00
} else {
2013-04-23 19:17:42 +00:00
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
2013-09-26 18:29:36 +00:00
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];
2013-09-26 18:29:36 +00:00
if (rate > hz) {
2013-04-23 19:17:42 +00:00
this.flooded[name] = time + 5000;
this.socket.emit("noflood", {
action: name,
msg: "Stop doing that so fast! Cooldown: 5s"
});
return true;
}
return false;
}
}
2013-09-26 18:29:36 +00:00
};
2013-03-20 23:10:23 +00:00
User.prototype.setAFK = function (afk) {
2013-09-26 18:29:36 +00:00
if (!this.inChannel())
return;
2013-09-26 18:29:36 +00:00
if (this.meta.afk === afk)
2013-08-01 19:12:57 +00:00
return;
var chan = this.channel;
this.meta.afk = afk;
2013-09-26 18:29:36 +00:00
if (afk) {
if (chan.voteskip)
2013-08-07 17:11:58 +00:00
chan.voteskip.unvote(this.ip);
2013-09-12 03:16:56 +00:00
} else {
2013-08-01 13:39:10 +00:00
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
});
2013-09-26 18:29:36 +00:00
};
User.prototype.autoAFK = function () {
2013-09-26 18:29:36 +00:00
var self = this;
if (self.awaytimer)
clearTimeout(self.awaytimer);
2013-09-26 18:29:36 +00:00
if (!self.inChannel() || self.channel.opts.afk_timeout === 0)
return;
2013-09-26 18:29:36 +00:00
self.awaytimer = setTimeout(function () {
self.setAFK(true);
}, self.channel.opts.afk_timeout * 1000);
};
2013-09-26 18:29:36 +00:00
User.prototype.initCallbacks = function () {
2013-08-18 17:21:34 +00:00
var self = this;
2013-09-26 18:29:36 +00:00
self.socket.on("disconnect", function () {
2013-08-18 17:21:34 +00:00
self.awaytimer && clearTimeout(self.awaytimer);
2013-09-26 18:29:36 +00:00
if (self.inChannel())
2013-08-18 17:21:34 +00:00
self.channel.userLeave(self);
});
2013-09-26 18:29:36 +00:00
self.socket.on("joinChannel", function (data) {
if (self.inChannel())
2013-06-02 22:09:52 +00:00
return;
2013-09-26 18:29:36 +00:00
if (typeof data.name != "string")
return;
2013-09-26 18:29:36 +00:00
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);
2013-09-26 18:29:36 +00:00
if (self.loggedIn) {
2013-08-18 17:21:34 +00:00
self.channel.getRank(self.name, function (err, rank) {
2013-09-26 18:29:36 +00:00
if (!err && rank > self.rank)
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("login", function (data) {
var name = data.name || "";
var pw = data.pw || "";
var session = data.session || "";
2013-09-26 18:29:36 +00:00
if (pw.length > 100)
pw = pw.substring(0, 100);
if (self.loggedIn)
return;
if (self.loggingIn) {
var j = 0;
// Wait until current login finishes
var i = setInterval(function () {
j++;
if (!self.loggingIn) {
clearInterval(i);
if (!self.loggedIn)
self.login(name, pw, session);
return;
}
// Just in case to prevent the interval from going wild
if (j >= 4)
clearInterval(i);
}, 1000);
} else {
2013-08-18 17:21:34 +00:00
self.login(name, pw, session);
}
2013-08-18 17:21:34 +00:00
});
2013-02-16 05:02:42 +00:00
2013-09-26 18:29:36 +00:00
self.socket.on("assignLeader", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("promote", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("demote", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("setChannelRank", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.trySetRank(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("banName", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("banIP", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("unban", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("chatMsg", function (data) {
if (self.inChannel()) {
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-09-26 18:29:36 +00:00
self.socket.on("newPoll", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryOpenPoll(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("playerReady", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("requestPlaylist", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("queue", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("setTemp", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("delete", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("uncache", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryUncache(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("moveMedia", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("jumpTo", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryJumpTo(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("playNext", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("clearPlaylist", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("shufflePlaylist", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("togglePlaylistLock", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("mediaUpdate", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("searchMedia", function (data) {
if (self.inChannel()) {
if (data.source == "yt") {
2013-10-11 21:31:40 +00:00
var searchfn = InfoGetter.Getters.ytSearch;
2013-08-17 20:47:11 +00:00
searchfn(data.query.split(" "), function (e, vids) {
2013-09-26 18:29:36 +00:00
if (!e) {
2013-08-17 20:47:11 +00:00
self.socket.emit("searchResults", {
source: "yt",
2013-08-17 20:47:11 +00:00
results: vids
});
}
});
} else {
self.channel.search(data.query, function (vids) {
self.socket.emit("searchResults", {
source: "library",
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-09-26 18:29:36 +00:00
self.socket.on("closePoll", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("vote", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("registerChannel", function (data) {
if (!self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.socket.emit("channelRegistration", {
2013-03-17 17:14:34 +00:00
success: false,
error: "You're not in any channel!"
});
2013-09-26 18:29:36 +00:00
} 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-09-26 18:29:36 +00:00
self.socket.on("unregisterChannel", function () {
if (!self.inChannel()) {
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-09-26 18:29:36 +00:00
self.socket.on("setOptions", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryUpdateOptions(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("setPermissions", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("setChannelCSS", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("setChannelJS", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("updateFilter", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("removeFilter", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("moveFilter", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryMoveFilter(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-04-01 21:02:09 +00:00
2013-09-26 18:29:36 +00:00
self.socket.on("setMotd", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryUpdateMotd(self, data);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("requestLoginHistory", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("requestBanlist", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.sendBanlist(self);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("requestChatFilters", function () {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.sendChatFilters(self);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("requestChannelRanks", function () {
if (self.inChannel()) {
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-09-26 18:29:36 +00:00
self.socket.on("voteskip", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryVoteskip(self);
2013-04-02 19:07:22 +00:00
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("listPlaylists", function (data) {
if (self.name === "" || self.rank < 1) {
2013-08-18 17:21:34 +00:00
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) {
2013-09-26 18:29:36 +00:00
if (err)
2013-08-18 17:21:34 +00:00
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", {
2013-09-26 18:29:36 +00:00
pllist: list
2013-08-18 17:21:34 +00:00
});
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-09-26 18:29:36 +00:00
self.socket.on("savePlaylist", function (data) {
if (self.rank < 1) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
if (!self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.socket.emit("savePlaylist", {
2013-06-01 15:59:04 +00:00
success: false,
error: "Not in a channel"
});
return;
}
2013-09-26 18:29:36 +00:00
if (typeof data.name != "string") {
2013-06-01 15:59:04 +00:00
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) {
2013-09-26 18:29:36 +00:00
if (err) {
2013-08-18 17:21:34 +00:00
self.socket.emit("savePlaylist", {
success: false,
error: err
});
return;
}
self.socket.emit("savePlaylist", {
success: true
});
self.server.db.listUserPlaylists(self.name,
function (err, list) {
2013-09-26 18:29:36 +00:00
if (err)
2013-08-18 17:21:34 +00:00
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", {
2013-09-26 18:29:36 +00:00
pllist: list
2013-08-18 17:21:34 +00:00
});
});
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-09-26 18:29:36 +00:00
self.socket.on("queuePlaylist", function (data) {
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
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-09-26 18:29:36 +00:00
self.socket.on("deletePlaylist", function (data) {
if (typeof data.name != "string") {
2013-06-01 20:56:23 +00:00
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) {
2013-09-26 18:29:36 +00:00
if (err)
2013-08-18 17:21:34 +00:00
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", {
2013-09-26 18:29:36 +00:00
pllist: list
2013-08-18 17:21:34 +00:00
});
});
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 () {
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.tryReadLog(self);
}
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
self.socket.on("acp-init", function () {
2013-10-11 21:31:40 +00:00
if (self.global_rank >= 255)
ACP.init(self);
2013-08-18 17:21:34 +00:00
});
2013-06-23 14:25:49 +00:00
2013-09-26 18:29:36 +00:00
self.socket.on("borrow-rank", function (rank) {
if (self.global_rank < 255)
2013-06-23 14:25:49 +00:00
return;
2013-09-26 18:29:36 +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);
2013-09-26 18:29:36 +00:00
if (self.inChannel())
2013-08-18 17:21:34 +00:00
self.channel.broadcastUserUpdate(self);
2013-06-23 14:25:49 +00:00
2013-08-18 17:21:34 +00:00
});
2013-09-26 18:29:36 +00:00
};
2013-02-16 05:02:42 +00:00
2013-05-03 03:13:46 +00:00
var lastguestlogin = {};
2013-09-26 18:18:37 +00:00
User.prototype.guestLogin = function (name) {
2013-08-17 20:54:23 +00:00
var self = this;
2013-09-26 18:18:37 +00:00
if (self.ip in lastguestlogin) {
var diff = (Date.now() - lastguestlogin[self.ip])/1000;
if (diff < self.server.cfg["guest-login-delay"]) {
2013-08-17 20:54:23 +00:00
self.socket.emit("login", {
success: false,
2013-09-26 18:18:37 +00:00
error: "Guest logins are restricted to one per IP address "+
"per " + self.server.cfg["guest-login-delay"] +
2013-09-26 18:29:36 +00:00
" seconds."
2013-08-17 20:54:23 +00:00
});
2013-09-26 18:18:37 +00:00
return false;
2013-08-17 20:54:23 +00:00
}
2013-09-26 18:18:37 +00:00
}
2013-08-17 20:54:23 +00:00
2013-09-26 18:29:36 +00:00
if (!$util.isValidUserName(name)) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: "Invalid username. Usernames must be 1-20 characters "+
"long and consist only of characters a-z, A-Z, 0-9, -, "+
"and _"
});
return;
}
2013-08-17 20:54:23 +00:00
2013-09-26 18:18:37 +00:00
// Set the loggingIn flag to avoid race conditions with the callback
self.loggingIn = true;
self.server.db.isUsernameTaken(name, function (err, taken) {
self.loggingIn = false;
2013-09-26 18:29:36 +00:00
if (err) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: "Internal error: " + err
});
return;
}
2013-08-17 20:54:23 +00:00
2013-09-26 18:29:36 +00:00
if (taken) {
2013-08-17 20:54:23 +00:00
self.socket.emit("login", {
2013-09-26 18:18:37 +00:00
success: false,
error: "That username is registered and protected."
2013-02-16 05:02:42 +00:00
});
2013-09-26 18:18:37 +00:00
return;
}
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
var lname = name.toLowerCase();
2013-09-26 18:18:37 +00:00
for(var i = 0; i < self.channel.users.length; i++) {
if (self.channel.users[i].name.toLowerCase() === lname) {
2013-09-26 18:18:37 +00:00
self.socket.emit("login", {
success: false,
error: "That name is already in use on this channel"
});
return;
}
2013-06-19 21:54:27 +00:00
}
2013-09-26 18:18:37 +00:00
}
lastguestlogin[self.ip] = Date.now();
self.rank = 0;
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-09-26 18:18:37 +00:00
self.socket.emit("rank", self.rank);
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
2013-09-26 18:18:37 +00:00
self.channel.logger.log(self.ip + " signed in as " + name);
self.channel.broadcastNewUser(self);
}
});
2013-09-26 18:29:36 +00:00
};
2013-09-26 18:18:37 +00:00
// Attempt to login
2013-09-26 18:29:36 +00:00
User.prototype.login = function (name, pw, session) {
2013-09-26 18:18:37 +00:00
var self = this;
// No password => try guest login
2013-09-26 18:29:36 +00:00
if (pw === "" && session === "") {
2013-09-26 18:18:37 +00:00
this.guestLogin(name);
2013-08-17 20:54:23 +00:00
} else {
self.loggingIn = true;
2013-08-17 20:54:23 +00:00
self.server.db.userLogin(name, pw, session, function (err, row) {
2013-09-26 18:29:36 +00:00
if (err) {
self.loggingIn = false;
2013-10-11 21:31:40 +00:00
ActionLog.record(self.ip, name, "login-failure",
2013-09-26 18:18:37 +00:00
err);
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-09-26 18:29:36 +00:00
if (self.inChannel()) {
2013-09-26 18:18:37 +00:00
var n = name.toLowerCase();
2013-08-17 20:54:23 +00:00
for(var i = 0; i < self.channel.users.length; i++) {
2013-09-26 18:29:36 +00:00
if (self.channel.users[i].name.toLowerCase() === n) {
2013-09-26 18:18:37 +00:00
if (self.channel.users[i] === self) {
Logger.errlog.log("Wat: user.login() but user "+
2013-09-07 20:44:57 +00:00
"already logged in on channel");
break;
}
2013-09-26 18:18:37 +00:00
self.channel.kick(self.channel.users[i],
"Duplicate login");
2013-08-17 20:54:23 +00:00
}
}
}
2013-09-26 18:18:37 +00:00
// Record logins for administrator accounts
2013-09-26 18:29:36 +00:00
if (self.global_rank >= 255)
2013-10-11 21:31:40 +00:00
ActionLog.record(self.ip, name, "login-success");
2013-08-17 20:54:23 +00:00
self.loggedIn = true;
self.loggingIn = false;
2013-08-17 20:54:23 +00:00
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;
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
2013-08-18 17:21:34 +00:00
self.channel.logger.log(self.ip + " logged in as " +
name);
self.channel.broadcastNewUser(self);
}
};
2013-09-26 18:29:36 +00:00
if (self.inChannel()) {
2013-08-18 19:21:42 +00:00
self.channel.getRank(name, function (err, rank) {
2013-09-26 18:29:36 +00:00
if (!err) {
self.saverank = true;
2013-08-18 17:21:34 +00:00
self.rank = rank;
} else {
2013-09-26 18:18:37 +00:00
// If there was an error in retrieving the rank,
// don't overwrite it with a bad value
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-09-26 18:29:36 +00:00
};
2013-02-16 05:02:42 +00:00
2013-07-16 03:01:12 +00:00
module.exports = User;