var shell = require('electron').shell; var config = require('./config.json') var pjson = require('./package.json'); var handlers = require('./handlers.js'); var commands = require('./commands.js'); var globals = require('./globals.js') var debugmiddleware = require('./debugmiddleware.js'); const progName = pjson.name; const version = pjson.version; var myNick = config.nick; var server = config.server; var port = config.port; var password = config.password; $(document).ready(function() { $("#send-message").focus(); }); $(document).on('click', 'a[href^="http"]', function(event) { event.preventDefault(); shell.openExternal(this.href); }); const irc = require('irc'); const IRC = require('irc-framework'); const client = new IRC.Client() $("#server-input").val(config.server); $("#port-input").val(config.port); $("#nick-input").val(config.nick); $("#password-input").val(config.password); var channels = config.channels var txtChannels = ""; config.channels.forEach(function(channel) { txtChannels += channel + " " }); txtChannels = txtChannels.substring(0, txtChannels.length-1) $("#channel-input").val(txtChannels); $('#connect').on('click', function() { console.log($("#channel-input").val()); server = $("#server-input").val(); port = $("#port-input").val(); channels = $("#channel-input").val().split(" "); myNick = $("#nick-input").val(); password = $("#password-input").val(); ssl = ($("#ssl-input:checked").val() == "on"); console.log(ssl); client.use(debugmiddleware.DebugMiddleware()); console.log(server) client.connect({ host: server, port: port, nick: myNick, username: myNick, gecos: myNick, password: password, tls: ssl, rejectUnauthorized: false, version: "blachat 0.1.3" }); commands.init(client) handlers.setChannels(channels) handlers.handleCommands(client) $("#chan-container").show(); $("#message").show(); $("#connect-container").hide(); }); function imagify(url, channel) { console.log(url); $.ajax({ type: 'HEAD', url: url, complete: function(xhr) { var type = xhr.getResponseHeader('Content-Type') var size = xhr.getResponseHeader('Content-Length'); console.log(type); console.log(size); if (size < 10000000) { $("[data-img='"+url+"']").append("("+humanize(size)+")"); $("[data-img='"+url+"']").append("
"); $("[data-img='"+url+"']").removeClass("loading"); } } }); return ""; } function humanize(size) { var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB']; var ord = Math.floor(Math.log(size) / Math.log(1000)); var s = Math.round((size / Math.pow(1000, ord)) * 100) / 100; return s + ' ' + units[ord]; } var numChans = globals.channels.length $("#send-message").keypress(function(e) { if(e.which == 13) { var message = $("#send-message").val(); var channel = $(".tab.active").attr("data-tab") if (message.indexOf("/join") == 0) { commands.joinChannel(message.substring(6, message.length), true); } else if (message.indexOf("/part") == 0) { if (message.length > 6) { channel = message.substring(6, message.length); } commands.removeChannel(channel, true); } else if (message.indexOf("/whois") == 0) { commands.whois(message.substring(7, message.length)) } else if (message.indexOf("/list") == 0) { commands.list(); } else if (message.indexOf("/msg") == 0) { nick = message.split(" ")[1]; message = message.substring(6+nick.length, message.length); commands.sendPm(message, nick); } else if (message.indexOf("/topic") == 0) { var channel = $(".tab.active").attr("data-tab") topic = message.substring(7, message.length); commands.setTopic(channel, topic); } else if (message.indexOf("/") == 0) { command = message.split(" ")[0].replace("/",""); commandArgs = message.replace(command + " ", ""); client.send("kick", ["#wtest", "wjoe__", "test"]); } else { var tab = $(".tab.active").attr("data-tab"); if (tab.startsWith("#")) { commands.sendMsg(message, channel); } else { commands.sendPm(message, tab); } } $("#send-message").val(''); } }); function createListeners(client) { client.addListener('error', function(message) { console.log('error: ', message); }); client.addListener('motd', function(motd) { var statusTab = $("[data-tab=status].chat"); statusTab.append(""+motd+"
") statusTab.scrollTop(statusTab.prop("scrollHeight")); }); client.addListener('raw', function(message) { var statusTab = $("[data-tab=status].chat"); statusCmds = ["NOTICEAUTH", "rpl_welcome, rpl_yourhoust", "rpl_created", "rpl_myinfo", "rpl_isupport", "rpl_luserclient", "rpl_luserop", "rpl_luserme", "rpl_luserchannels", "rpl_localusers", "rpl_globalusers"]; if (statusCmds.indexOf(message.command) > -1 || message.command.startsWith("rpl_")) { statusTab.append(""+message.command + ": " +message.args+"
") statusTab.scrollTop(statusTab.prop("scrollHeight")); } }); client.addListener('ctcp-version', function(from, to, text, message) { client.ctcp(from, "VERSION", "VERSION "+progName + " v"+ version) }); client.addListener('names', function(channel, nicks) { channel = channel.replace("#","") if($(".ui.tab[data-tab=chan-"+channel+"]").length == 0) { joinChannel(channel, false) } for(nick in nicks) { var mode = nicks[nick]; if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length == 0) { $(".ui.users[data-tab=chan-"+channel+"]").append(""+nick+" joined #"+channel+"
") }); client.addListener('part', function(channel, nick) { channel = channel.replace("#","") if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").remove(); } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+nick+" left #"+channel+"
") chatTab.scrollTop(chatTab.prop("scrollHeight")); }); client.addListener('quit', function(nick, reason, channels, message) { channels.forEach(function(channel) { channel = channel.replace("#","") if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").remove(); } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+nick+" quit ("+reason+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); }); }); client.addListener('kick', function(channel, nick, by, reason, message) { if (myNick == nick) { removeChannel(channel, false); } else { channel = channel.replace("#",""); if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").remove(); } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+nick+"was kicked from #"+channel+" by "+by+" ("+reason+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); } }); client.addListener('kill', function(nick, reason, channels, message) { channels.forEach(function(channel) { channel = channel.replace("#","") if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").remove(); } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+nick+" was killed by the server ("+reason+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); }); }); client.addListener('+mode', function(channel, by, mode, argument, message) { console.log(message) channel = channel.replace("#","") var chatTab = $("[data-tab=chan-"+ channel +"].chat"); var modeLine = mode; if (mode == "o") { modeLine = " gives channel operator status to " + argument; } else if (mode == "v") { modeLine = " gives voice to " + argument; } else if (mode == "h") { modeLine = " gives channel half-operator status to " + argument; } else if (mode == "q") { modeLine = " gives channel owner status to " + argument; } else if (argument == myNick) { modeLine = " sets mode +" + mode + " on " + argument; } else if (argument != null) { modeLine = " sets mode " + mode + " " + argument + " on #" +channel; } else { modeLine = " sets mode +"+mode+" on #" +channel } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+by+modeLine+"
"); chatTab.scrollTop(chatTab.prop("scrollHeight")); }); client.addListener('-mode', function(channel, by, mode, argument, message) { channel = channel.replace("#","") var chatTab = $("[data-tab=chan-"+ channel +"].chat"); var modeLine = mode; if (mode == "o") { modeLine = " removes channel operator status from "; } else if (mode == "v") { modeLine = " removes voice from "; } else if (mode == "h") { modeLine = " removes channel half-operator status from "; } else if (mode == "q") { modeLine = " removes channel owner status from "; } else if (argument == myNick) { modeLine = " sets mode -"+mode+" on " + argument; } else if (argument != null) { modeLine = " sets mode -" + mode + " " + argument + " on #" +channel; } else { modeLine = " sets mode -"+mode+" on #" +channel } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append(""+by+modeLine+"
"); chatTab.scrollTop(chatTab.prop("scrollHeight")); }); client.addListener('channellist', function(channelList) { var statusTab = $("[data-tab=status].chat"); statusTab.append("Channel List:
") for (i in channelList) { if (channelList[i].topic == null) { statusTab.append(""+channelList[i].name+" ("+channelList[i].users+")
"); } else { statusTab.append(""+channelList[i].name+" ("+channelList[i].users+") : "+channelList[i].topic+"
"); } statusTab.scrollTop(statusTab.prop("scrollHeight")); } }); }