var linkify = require("linkifyjs"); var linkifyHtml = require('linkifyjs/html'); var shell = require('electron').shell; var config = require('./config.json') const myNick = config.nick; $(document).ready(function() { $("#send-message").focus(); }); $(document).on('click', 'a[href^="http"]', function(event) { event.preventDefault(); shell.openExternal(this.href); }); const irc = require('irc'); var channels = config.channels const client = new irc.Client(config.server, myNick, { channels: channels, userName: [myNick] }); 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 = 1; $("#send-message").keypress(function(e) { if(e.which == 13) { var message = $("#send-message").val(); var channel = $(".tab.active").attr("data-tab").replace("chan-", "#") if (message.indexOf("/join") == 0) { joinChannel(message.substring(6, message.length)); } else if (message.indexOf("/part") == 0) { if (message.length > 6) { channel = message.substring(6, message.length); } console.log() removeChannel(channel, true); } else if (message.indexOf("/whois") == 0) { console.log("whois "+message.substring(7, message.length)); client.whois(message.substring(7, message.length)); } else if (message.indexOf("/list") == 0) { client.list(); } else { var tab = $(".tab.active").attr("data-tab"); if (tab.startsWith("chan-")) { sendMsg(message, channel.replace("#","")); } else if(tab.startsWith("pm-")){ sendPm(message, tab.substring(3, tab.length)); } } $("#send-message").val(''); } }); function sendMsg(message, channel) { console.log(channel + ': ' + message); client.say(channel, message); var chanTab = $("[data-tab=chan-"+ channel.replace("#","") +"].chat"); chanTab.append("

<" + "wclient" + "> " + message +"

"); chanTab.scrollTop(chanTab.prop("scrollHeight")); } function sendPm(message, nick) { console.log(nick + ': ' + message); client.say(nick, message); var chanTab = $("[data-tab=pm-"+nick+"].chat"); chanTab.append("

<" + "wclient" + "> " + message +"

"); chanTab.scrollTop(chanTab.prop("scrollHeight")); } function joinChannel(channel) { client.join(channel); channel = channel.replace("#",""); chanId = $(".active.chat").attr("data-id") $(".active").removeClass("active"); $(".ui.menu").append("#"+channel+""); $("#chan-container").append("
"); var chanTab = $("[data-tab=chan-"+ channel +"].tab"); chanTab.append("
"); chanTab.append("
"); chanTab.append("
"); $('.menu .item').tab({history:false}); client.addListener('message#'+channel, function (from, message) { // message = message.autoLink( { // callback: function(url) { // return /\.(gif|png|jpe?g)$/i.test(url) ? '' : null; // }, // target: "_blank" // }); // console.log(message.autoLink()); console.log(from + ' => #'+ channel +': ' + message); var chatTab = $("[data-tab=chan-"+ channel +"].chat"); message = linkifyHtml(message); chatTab.append("

<" + from + "> " + message +"

"); var imgMatch = message.match(/[a-z0-9\-]+[\.:]\S+\.(gif|png|jpe?g|bmp)/); var vidMatch = message.match(/[a-z0-9\-]+[\.:]\S+\.(webm|mp4)/); var audioMatch = message.match(/[a-z0-9\-]+[\.:]\S+\.(mp3|wav|ogg)/); var youtubeMatch = message.match(/(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?[a-z0-9A-Z_\-]+/); if (imgMatch != null && imgMatch.length > 0) { var image = imagify(imgMatch[0],channel); chatTab.append("

"+image+"

"); } if (vidMatch != null && vidMatch.length > 0) { chatTab.append("

"); } if (audioMatch != null && audioMatch.length > 0) { chatTab.append("

"); } if (youtubeMatch != null && youtubeMatch.length > 0) { console.log(youtubeMatch) youtubeParts = youtubeMatch[0].split("/"); youtubeId = youtubeParts[youtubeParts.length-1]; youtubeId = youtubeId.replace("watch?v=",""); chatTab.append("

"); } chatTab.scrollTop(chatTab.prop("scrollHeight")); }); numChans++; } function removeChannel(channel, sendPart) { channel = channel.replace("#",""); chanId = $("[data-tab=chan-"+channel+"].chat").attr("data-id"); $("[data-tab=chan-"+channel+"]").remove(); if (sendPart) { client.part("#"+channel); } while ($("[data-id="+(chanId-1)+"]").length == 0 && chanId >=0) { chanId--; } $("[data-id="+(chanId-1)+"]").addClass("active"); } client.addListener('error', function(message) { console.log('error: ', message); }); client.addListener('registered', function() { channels.forEach(function(channel) { joinChannel(channel); }); }); 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) { statusTab.append("

"+message.args+"

") statusTab.scrollTop(statusTab.prop("scrollHeight")); } }); client.addListener('ctcp-version', function(from, to, text, message) { client.ctcp(from, "VERSION", "VERSION blachat v0.1.0") }); client.addListener('names', function(channel, nicks) { for(nick in nicks) { var mode = nicks[nick]; channel = channel.replace("#","") if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length == 0) { $(".ui.users[data-tab=chan-"+channel+"]").append("
"+mode+nick+"
") } } }); - client.addListener('join', function(channel, nick) { channel = channel.replace("#","") if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length == 0) { $(".ui.users[data-tab=chan-"+channel+"]").append("
"+nick+"
") } var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.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('pm', function(nick, text, message) { if($(".ui.tab[data-tab=pm-"+nick+"]").length == 0) { $(".ui.menu").append(""+nick+""); $("#chan-container").append("
"); var chanTab = $("[data-tab=pm-"+ nick +"].tab"); chanTab.append("
"); numChans++; $('.menu .item').tab({history:false}); } var chatTab = $("[data-tab=pm-"+ nick +"].chat"); chatTab.append("

<"+nick+"> "+text+"

") 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('topic', function(channel, topic, nick, message) { channel = channel.replace("#",""); var chatTab = $("[data-tab=chan-"+ channel +"].chat"); chatTab.append("

Topic for #"+channel+" is "+topic+" - set by "+nick+" at "+message.args[3]+"

") var topicTab = $("[data-tab=chan-"+ channel +"].topic"); topicTab.html("

"+topic+"

") }); client.addListener('whois', function(info) { console.log(info) var chatTab = $(".active .chat"); chatTab.append("

Nick:"+info.nick+"

"); chatTab.append("

User:"+info.user+"

"); chatTab.append("

Host:"+info.host+"

"); chatTab.append("

Real Name:"+info.realname+"

"); chatTab.append("

Channels:"); for (channel in info.channels) { chatTab.append(" "+info.channels[channel]) } chatTab.append("

") chatTab.append("

Server:"+info.server+"

"); chatTab.append("

Server Info:"+info.serverinfo+"

"); chatTab.append("

"+info.operator+"

"); 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")); } }); // client.addListener('message#wtest', function (from, message) { // console.log(from + ' => #wtest: ' + message); // var chanTab = $("[data-tab=chan-wtest].chat"); // chanTab.append("

<" + from + "> " + message +"

"); // chanTab.scrollTop(chanTab.prop("scrollHeight")); // }); // // client.addListener('message#jerseyesports', function (from, message) { // console.log(from + ' => #jerseyesports: ' + message); // $("[data-tab=chan-jerseyesports].chat").append("

<" + from + "> " + message +"

"); // }); // client.addListener('message#theblueroom', function (from, message) { // console.log(from + ' => #jerseyesports: ' + message); // $("[data-tab=chan-theblueroom].chat").append("

<" + from + "> " + message +"

"); // });