/*
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.
*/
Callbacks = {
error: function (reason) {
window.SOCKET_ERROR_REASON = reason;
},
/* fired when socket connection completes */
connect: function() {
socket.emit("initChannelCallbacks");
socket.emit("joinChannel", {
name: CHANNEL.name
});
if (CHANNEL.opts.password) {
socket.emit("channelPassword", CHANNEL.opts.password);
}
if (CLIENT.name && CLIENT.guest) {
socket.emit("login", {
name: CLIENT.name
});
}
$("
").addClass("server-msg-reconnect")
.text("Connected")
.appendTo($("#messagebuffer"));
scrollChat();
},
disconnect: function() {
if(KICKED)
return;
$("")
.addClass("server-msg-disconnect")
.text("Disconnected from server. Attempting reconnection...")
.appendTo($("#messagebuffer"));
scrollChat();
},
errorMsg: function(data) {
if (data.alert) {
alert(data.msg);
} else {
errDialog(data.msg);
}
},
costanza: function (data) {
hidePlayer();
$("#costanza-modal").modal("hide");
var modal = makeModal();
modal.attr("id", "costanza-modal")
.appendTo($("body"));
var body = $("").addClass("modal-body")
.appendTo(modal.find(".modal-content"));
$("").attr("src", "http://i0.kym-cdn.com/entries/icons/original/000/005/498/1300044776986.jpg")
.appendTo(body);
$("").text(data.msg).appendTo(body);
hidePlayer();
modal.modal();
},
announcement: function(data) {
$("#announcements").html("");
makeAlert(data.title, data.text)
.appendTo($("#announcements"));
},
kick: function(data) {
KICKED = true;
$("").addClass("server-msg-disconnect")
.text("Kicked: " + data.reason)
.appendTo($("#messagebuffer"));
scrollChat();
},
noflood: function(data) {
$("")
.addClass("server-msg-disconnect")
.text(data.action + ": " + data.msg)
.appendTo($("#messagebuffer"));
scrollChat();
},
needPassword: function (wrongpw) {
var div = $("");
$("").text("Channel Password")
.appendTo(div);
if (wrongpw) {
$(" ").appendTo(div);
$("").addClass("text-error")
.text("Wrong Password")
.appendTo(div);
}
var pwbox = $("").addClass("form-control")
.attr("type", "password")
.appendTo(div);
var submit = $("").addClass("btn btn-xs btn-default btn-block")
.css("margin-top", "5px")
.text("Submit")
.appendTo(div);
var parent = chatDialog(div);
parent.attr("id", "needpw");
var sendpw = function () {
socket.emit("channelPassword", pwbox.val());
parent.remove();
};
submit.click(sendpw);
pwbox.keydown(function (ev) {
if (ev.keyCode == 13) {
sendpw();
}
});
pwbox.focus();
},
cancelNeedPassword: function () {
$("#needpw").remove();
},
cooldown: function (time) {
time = time + 200;
$("#chatline").css("color", "#ff0000");
if (CHATTHROTTLE && $("#chatline").data("throttle_timer")) {
clearTimeout($("#chatline").data("throttle_timer"));
}
CHATTHROTTLE = true;
$("#chatline").data("throttle_timer", setTimeout(function () {
CHATTHROTTLE = false;
$("#chatline").css("color", "");
}, time));
},
channelNotRegistered: function() {
var div = $("").addClass("alert alert-info")
.appendTo($("").addClass("col-md-12").appendTo($("#announcements")));
$("").addClass("close pull-right")
.appendTo(div)
.click(function () {
div.parent().remove();
})
.html("×");
$("").appendTo(div).text("Unregistered channel");
$("").appendTo(div)
.html("This channel is not registered to a CyTube account. You can still " +
"use it, but some features will not be available. To register a " +
"channel to your account, visit your " +
"channels page.");
},
registerChannel: function(data) {
if ($("#chanregisterbtn").length > 0) {
$("#chanregisterbtn").text("Register it")
.attr("disabled", false);
}
if(data.success) {
$("#chregnotice").remove();
}
else {
makeAlert("Error", data.error, "alert-danger")
.insertAfter($("#chregnotice"));
}
},
unregisterChannel: function(data) {
if(data.success) {
alert("Channel unregistered");
}
else {
alert(data.error);
}
},
setMotd: function(data) {
CHANNEL.motd = data.html;
CHANNEL.motd_text = data.motd;
$("#motd").html(CHANNEL.motd);
$("#cs-motdtext").val(CHANNEL.motd_text);
if (data.motd != "") {
$("#motdwrap").show();
$("#motd").show();
$("#togglemotd").find(".glyphicon-plus")
.removeClass("glyphicon-plus")
.addClass("glyphicon-minus");
} else {
$("#motdwrap").hide();
}
},
chatFilters: function(entries) {
var tbl = $("#cs-chatfilters table");
tbl.data("entries", entries);
formatCSChatFilterList();
},
updateChatFilter: function (f) {
var entries = $("#cs-chatfilters table").data("entries") || [];
var found = false;
for (var i = 0; i < entries.length; i++) {
if (entries[i].name === f.name) {
entries[i] = f;
found = true;
break;
}
}
if (!found) {
entries.push(f);
}
$("#cs-chatfilters table").data("entries", entries);
formatCSChatFilterList();
},
deleteChatFilter: function (f) {
var entries = $("#cs-chatfilters table").data("entries") || [];
var found = false;
for (var i = 0; i < entries.length; i++) {
if (entries[i].name === f.name) {
entries[i] = f;
found = i;
break;
}
}
if (found !== false) {
entries.splice(found, 1);
}
$("#cs-chatfilters table").data("entries", entries);
formatCSChatFilterList();
},
channelOpts: function(opts) {
document.title = opts.pagetitle;
PAGETITLE = opts.pagetitle;
if (!USEROPTS.ignore_channelcss &&
opts.externalcss !== CHANNEL.opts.externalcss) {
$("#chanexternalcss").remove();
if (opts.externalcss.trim() !== "") {
$("#chanexternalcss").remove();
$("")
.attr("rel", "stylesheet")
.attr("href", opts.externalcss)
.attr("id", "chanexternalcss")
.appendTo($("head"));
}
}
if(opts.externaljs.trim() != "" && !USEROPTS.ignore_channeljs &&
opts.externaljs !== CHANNEL.opts.externaljs) {
checkScriptAccess(opts.externaljs, "external", function (pref) {
if (pref === "ALLOW") {
$.getScript(opts.externaljs);
}
});
}
CHANNEL.opts = opts;
if(opts.allow_voteskip)
$("#voteskip").attr("disabled", false);
else
$("#voteskip").attr("disabled", true);
handlePermissionChange();
},
setPermissions: function(perms) {
CHANNEL.perms = perms;
genPermissionsEditor();
handlePermissionChange();
},
channelCSSJS: function(data) {
$("#chancss").remove();
CHANNEL.css = data.css;
$("#csstext").val(data.css);
if(data.css && !USEROPTS.ignore_channelcss) {
$("").attr("type", "text/css")
.attr("id", "chancss")
.text(data.css)
.appendTo($("head"));
}
$("#chanjs").remove();
CHANNEL.js = data.js;
$("#jstext").val(data.js);
if(data.js && !USEROPTS.ignore_channeljs) {
var src = data.js
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/\n/g, " ")
.replace(/\t/g, " ")
.replace(/ /g, " ");
src = encodeURIComponent(src);
var viewsource = "data:text/html, " +
src + "";
checkScriptAccess(viewsource, "embedded", function (pref) {
if (pref === "ALLOW") {
$("").attr("type", "text/javascript")
.attr("id", "chanjs")
.text(data.js)
.appendTo($("body"));
}
});
}
},
banlist: function(entries) {
var tbl = $("#cs-banlist table");
tbl.data("entries", entries);
formatCSBanlist();
},
banlistRemove: function (data) {
var entries = $("#cs-banlist table").data("entries") || [];
var found = false;
for (var i = 0; i < entries.length; i++) {
if (entries[i].id === data.id) {
found = i;
break;
}
}
if (found !== false) {
entries.splice(i, 1);
$("#cs-banlist table").data("entries", entries);
}
formatCSBanlist();
},
recentLogins: function(entries) {
var tbl = $("#loginhistory table");
// I originally added this check because of a race condition
// Now it seems to work without but I don't trust it
if(!tbl.hasClass("table")) {
setTimeout(function() {
Callbacks.recentLogins(entries);
}, 100);
return;
}
if(tbl.children().length > 1) {
$(tbl.children()[1]).remove();
}
for(var i = 0; i < entries.length; i++) {
var tr = document.createElement("tr");
var name = $("
").text(entries[i].name).appendTo(tr);
var aliases = $("
").text(entries[i].aliases.join(", ")).appendTo(tr);
var time = new Date(entries[i].time).toTimeString();
$("