Clean up loadDump

This commit is contained in:
calzoneman 2013-11-24 23:20:41 -06:00
parent 01464ed394
commit 0945fc0d7b
2 changed files with 127 additions and 110 deletions

View file

@ -29,7 +29,9 @@ var InfoGetter = require("./get-info");
var Channel = function(name) { var Channel = function(name) {
var self = this; var self = this;
Logger.syslog.log("Opening channel " + name); Logger.syslog.log("Opening channel " + name);
self.initialized = false; self.shouldDump = false;
self.ready = false;
self.pendingJoins = [];
self.dbloaded = false; self.dbloaded = false;
self.server = Server.getServer(); self.server = Server.getServer();
@ -137,9 +139,7 @@ var Channel = function(name) {
else if (!err || err === "channel_unregistered") else if (!err || err === "channel_unregistered")
self.dbloaded = true; self.dbloaded = true;
if(self.registered) { self.tryLoadDump();
self.loadDump();
}
}); });
} }
@ -160,19 +160,29 @@ Channel.prototype.hasPermission = function(user, key) {
} }
/* REGION Channel data */ /* REGION Channel data */
Channel.prototype.loadDump = function() {
Channel.prototype.tryLoadDump = function () {
var self = this; var self = this;
if(self.name === "") // Not sure how this would ever happen, but it was there before
if (self.name === "") {
return; return;
}
if (!self.registered) {
self.ready = true;
self.handlePendingJoins();
return;
}
fs.stat(path.join(__dirname, "../chandump", self.name), fs.stat(path.join(__dirname, "../chandump", self.name),
function (err, stats) { function (err, stats) {
if (self.dead) if (self.dead)
return; return;
if(!err) { if (!err) {
var mb = stats.size / 1048576; var mb = stats.size / 1048576;
mb = parseInt(mb * 100) / 100; mb = parseInt(mb * 100) / 100;
if(mb > 1) { if (mb > 1) {
Logger.errlog.log("Large chandump detected: " + self.name + Logger.errlog.log("Large chandump detected: " + self.name +
" (" + mb + " MB)"); " (" + mb + " MB)");
self.updateMotd("Your channel file has exceeded the " + self.updateMotd("Your channel file has exceeded the " +
@ -182,49 +192,42 @@ Channel.prototype.loadDump = function() {
return; return;
} }
} }
self.loadDump();
});
};
Channel.prototype.loadDump = function() {
var self = this;
if (self.dead || !self.name) {
return;
}
fs.readFile(path.join(__dirname, "../chandump", self.name), fs.readFile(path.join(__dirname, "../chandump", self.name),
function(err, data) { function(err, data) {
if (self.dead) if (self.dead)
return; return;
if(err) { if (err) {
if(err.code == "ENOENT") { if (err.code == "ENOENT") {
Logger.errlog.log("WARN: missing dump for " + self.name); self.shouldDump = true;
self.initialized = true; self.ready = true;
self.handlePendingJoins();
self.saveDump(); self.saveDump();
} } else {
else {
Logger.errlog.log("Failed to open channel dump " + self.name); Logger.errlog.log("Failed to open channel dump " + self.name);
Logger.errlog.log(err); Logger.errlog.log(err);
self.ready = true;
self.handlePendingJoins();
} }
return; return;
} }
try { try {
self.logger.log("*** Loading channel dump from disk"); self.logger.log("*** Loading channel dump from disk");
data = JSON.parse(data); data = JSON.parse(data);
/* Load the playlist */
// Old /* Load the playlist */
if(data.queue) { if ("playlist" in data) {
if(data.position < 0) // TODO change callback to return value
data.position = 0;
for(var i = 0; i < data.queue.length; i++) {
var e = data.queue[i];
var m = new Media(e.id, e.title, e.seconds, e.type);
var p = self.playlist.makeItem(m);
p.queueby = data.queue[i].queueby ? data.queue[i].queueby
: "";
p.temp = e.temp;
self.playlist.items.append(p);
if(i == data.position)
self.playlist.current = p;
}
self.sendAll("playlist", self.playlist.items.toArray());
self.broadcastPlaylistMeta();
self.playlist.startPlayback();
}
// Current
else if(data.playlist) {
self.playlist.load(data.playlist, function() { self.playlist.load(data.playlist, function() {
if (self.dead) if (self.dead)
return; return;
@ -233,71 +236,68 @@ Channel.prototype.loadDump = function() {
self.playlist.startPlayback(data.playlist.time); self.playlist.startPlayback(data.playlist.time);
}); });
} }
for(var key in data.opts) {
// Gotta love backwards compatibility /* Load playlist lock */
if(key == "customcss" || key == "customjs") { self.setLock(!(data.openqueue || false));
var k = key.substring(6);
self.opts[k] = data.opts[key]; /* Load configurable options */
if ("customcss" in data.opts) {
data.opts.css = data.opts.customcss;
} }
else { if ("customjs" in data.opts) {
data.opts.js = data.opts.customjs;
}
for (var key in data.opts) {
self.opts[key] = data.opts[key]; self.opts[key] = data.opts[key];
} }
}
for(var key in data.permissions) { /* Load permissions */
for (var key in data.permissions) {
self.permissions[key] = data.permissions[key]; self.permissions[key] = data.permissions[key];
} }
self.sendAll("setPermissions", self.permissions);
self.broadcastOpts(); /* Load chat filters */
self.users.forEach(function (u) { // TODO change the way default filters work?
u.autoAFK(); for (var i = 0; i < data.filters.length; i++) {
});
if(data.filters) {
for(var i = 0; i < data.filters.length; i++) {
var f = data.filters[i]; var f = data.filters[i];
// Backwards compatibility // Old filter system used to use arrays
if(f[0] != undefined) { if (f instanceof Array) {
var filt = new Filter("", f[0], "g", f[1]); continue;
filt.active = f[2];
self.updateFilter(filt, false);
} }
else {
var filt = new Filter(f.name, f.source, f.flags, f.replace); var filt = new Filter(f.name, f.source, f.flags, f.replace);
filt.active = f.active; filt.active = f.active;
filt.filterlinks = f.filterlinks; filt.filterlinks = f.filterlinks;
self.updateFilter(filt, false); self.updateFilter(filt, false);
} }
}
self.broadcastChatFilters(); /* Load MOTD */
} if (data.motd) {
if(data.motd) {
self.motd = data.motd; self.motd = data.motd;
self.broadcastMotd();
} }
self.setLock(!(data.openqueue || false));
/* Load chat history */
self.chatbuffer = data.chatbuffer || []; self.chatbuffer = data.chatbuffer || [];
for(var i = 0; i < self.chatbuffer.length; i++) {
self.sendAll("chatMsg", self.chatbuffer[i]); /* Load CSS, JS */
}
self.css = data.css || ""; self.css = data.css || "";
self.js = data.js || ""; self.js = data.js || "";
self.sendAll("channelCSSJS", {css: self.css, js: self.js}); self.ready = true;
self.initialized = true; self.shouldDump = true;
} self.handlePendingJoins();
catch(e) { } catch(e) {
Logger.errlog.log("Channel dump load failed: "); Logger.errlog.log("Channel dump load failed: ");
Logger.errlog.log(e.stack); Logger.errlog.log(e.stack);
} }
}); });
});
} }
Channel.prototype.saveDump = function() { Channel.prototype.saveDump = function() {
if (this.dead) if (this.dead)
return; return;
if(!this.initialized || this.name === "") if (!this.shouldDump || this.name === "")
return; return;
var filts = new Array(this.filters.length); var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) { for (var i = 0; i < this.filters.length; i++) {
filts[i] = this.filters[i].pack(); filts[i] = this.filters[i].pack();
} }
var dump = { var dump = {
@ -425,7 +425,7 @@ Channel.prototype.tryRegister = function (user) {
if (self.dead) if (self.dead)
return; return;
self.registered = true; self.registered = true;
self.initialized = true; self.shouldDump = true;
self.saveDump(); self.saveDump();
self.saveRank(user); self.saveRank(user);
user.socket.emit("registerChannel", { user.socket.emit("registerChannel", {
@ -800,8 +800,23 @@ Channel.prototype.search = function(query, callback) {
/* REGION User interaction */ /* REGION User interaction */
Channel.prototype.handlePendingJoins = function () {
var self = this;
self.pendingJoins.forEach(function (u) {
self.userJoin(u);
});
delete self["pendingJoins"];
};
Channel.prototype.userJoin = function(user) { Channel.prototype.userJoin = function(user) {
var self = this; var self = this;
if (!self.ready) {
if (!("pendingJoins" in self)) {
self.pendingJoins = [];
}
self.pendingJoins.push(user);
return;
}
var parts = user.ip.split("."); var parts = user.ip.split(".");
var slash24 = parts[0] + "." + parts[1] + "." + parts[2]; var slash24 = parts[0] + "." + parts[1] + "." + parts[2];
// GTFO // GTFO
@ -820,6 +835,9 @@ Channel.prototype.userJoin = function(user) {
// Join the socket pool for this channel // Join the socket pool for this channel
user.socket.join(this.name); user.socket.join(this.name);
// Enable AFK timer
user.autoAFK();
// Prevent duplicate login // Prevent duplicate login
if(user.name != "") { if(user.name != "") {
for(var i = 0; i < this.users.length; i++) { for(var i = 0; i < this.users.length; i++) {

View file

@ -156,7 +156,6 @@ User.prototype.initCallbacks = function () {
}); });
} }
self.channel.userJoin(self); self.channel.userJoin(self);
self.autoAFK();
}); });
self.socket.on("login", function (data) { self.socket.on("login", function (data) {