var linkify = require("linkifyjs"); var linkifyHtml = require('linkifyjs/html'); var util = require("./util.js") var commands = require("./commands.js") var globals = require("./globals.js") var numChans = globals.channels.length + globals.pms.length; var channels; var client; var myNick = "" module.exports.setChannels = function(myChannels) { channels = myChannels; } module.exports.handleCommands = function(myClient) { client = myClient; client.on('registered', function(event) { channels.forEach(function(channelStr) { commands.joinChannel(channelStr, true); }); }); client.on('message', function(event) { receiveMsg(event.target, event.nick, event.message, event.from_server) }) client.on('userlist', function(event) { channel = event.channel if($(".ui.tab[data-tab='"+channel+"']").length == 0) { commands.joinChannel(channel, false) } event.users.forEach(function (user) { var nick = user.nick var mode = "" user.modes.forEach(function (modeChar) { if (modeChar === "o") { mode = "@" } else if (modeChar === "h") { mode = "%" } else if (modeChar === "v") { mode = "+" } }) if($(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").length == 0) { $(".ui.users[data-tab='"+channel+"']").append("
Topic for "+channel+" is "+topic+"
") var topicTab = $("[data-tab='"+ channel +"'].topic"); topicTab.html(""+topic+"
") }) client.on('topicsetby', function(event) { channel = event.channel nick = event.nick timestamp = event.when //TODO: Handle topicsetby to add the user and time for the topic var chatTab = $("[data-tab='"+ channel +"'].chat"); if($(".ui.tab[data-tab='"+channel+"']").length == 0) { commands.joinChannel(channel, false) } chatTab.append("Set by "+nick+" at "+timestamp+"
") var topicLine = $("[data-tab='"+ channel +"'].topic .topic-line"); topicLine.append(" by " + nick + " at " + timestamp) }) client.on('whois', function(event) { var nick = event.nick var chatTab = $(".active .chat"); chatTab.append(""+event.nick+": "+event.user+"@"+event.host+"
"); chatTab.append("Real Name: "+event.real_name+"
"); chatTab.append("Channels: " + event.channels+"
"); chatTab.append("Server: "+event.server+" "+event.server_info+"
"); if (event.operator.length > 0) { chatTab.append(""+event.operator+"
"); } if (event.registered_nick.length > 0) { chatTab.append(""+event.registered_nick+"
"); } if (event.away.length > 0) { chatTab.append(""+event.away+"
"); } if (event.secure) { chatTab.append("is using a secure connection
"); } chatTab.scrollTop(chatTab.prop("scrollHeight")); }) client.on('channel list', function(event) { var statusTab = $("[data-tab='status'].chat"); statusTab.append("Channel List:
") event.forEach( function(channel) { if (channel.topic.length == 0) { statusTab.append(""+channel.channel+" ("+channel.num_users+")
"); } else { statusTab.append(""+channel.channel+" ("+channel.num_users+") : "+channel.topic+"
"); } statusTab.scrollTop(statusTab.prop("scrollHeight")); }) }) client.on('join', function(event) { var channel = event.channel var nick = event.nick if($(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").length == 0) { $(".ui.users[data-tab='"+channel+"']").append(""+nick+" joined "+channel+"
") }); client.on('part', function(event) { var channel = event.channel var nick = event.nick var message = event.message if($(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").remove(); } var chatTab = $("[data-tab='"+ channel +"'].chat"); chatTab.append(""+nick+" left "+channel+" ("+message+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); }); client.on('quit', function(event) { var nick = event.nick var message = event.message globals.channels.forEach(function(channel) { if($(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").length > 0) { $(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").remove(); var chatTab = $("[data-tab='"+ channel +"'].chat"); chatTab.append(""+nick+" quit ("+message+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); } }); }) client.on('mode', function(event) { var chatTab = $("[data-tab='"+ channel +"'].chat"); var by = event.nick var channel = event.target event.modes.forEach(function (modeObj) { var mode = modeObj.mode var argument = modeObj.param 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 (mode == "-o") { modeLine = " removes channel operator status from " + argument; } else if (mode == "-v") { modeLine = " removes voice from "; } else if (mode == "-h") { modeLine = " removes channel half-operator status from " + argument; } else if (mode == "-q") { modeLine = " removes channel owner status from " + argument; } else if (argument == globals.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='"+ channel +"'].chat"); chatTab.append(""+by+modeLine+"
"); }) chatTab.scrollTop(chatTab.prop("scrollHeight")); }) client.on('kick', function(event) { var kicked = event.kicked var by = event.nick var channel = event.channel var reason = event.message if (globals.myNick == kicked) { commands.removeChannel(channel, false); } else { if($(".ui.users[data-tab='"+channel+"'] [data-nick="+kicked+"]").length > 0) { $(".ui.users[data-tab='"+channel+"'] [data-nick="+kicked+"]").remove(); } var chatTab = $("[data-tab='"+ channel +"'].chat"); chatTab.append(""+kicked+" was kicked from "+channel+" by "+by+" ("+reason+")
") chatTab.scrollTop(chatTab.prop("scrollHeight")); } }) client.on('motd', function(event) { serverMsg(event.motd) }) //Server Info //eg: Your host is irc.lc8n.com, running version Unreal3.2.10.4" //param 0 is your nick client.on('002', function(event) { serverMsg(event.params[1]) }) //Server created //eg Your host is irc.lc8n.com, running version Unreal3.2.10.4" //param 0 is your nick client.on('003', function(event) { serverMsg(event.params[1]) }) //004: Some unknown server strings client.on('004', function(event) { serverMsg(event.params[1] + " " + event.params[2] + " " + event.params[3] + " " + event.params[4]) }) //Users online //eg: There are 7 users and 47 invisible on 4 servers client.on('251', function(event) { serverMsg(event.params[1]) }) //Operators online //eg: "10" "operator(s) online" client.on('252', function(event) { serverMsg(event.params[1] + " " +event.params[2]) }) //Channels formed //eg "49" "channels formed" client.on('254', function(event) { serverMsg(event.params[1] + " " +event.params[2]) }) //'Clients' and 'Servers'? //eg "I have 8 clients and 1 servers" client.on('255', function(event) { serverMsg(event.params[1]) }) //User stats //eg ""Current global users 54, max 63" //param 0 is your nick, 1 is current users, 2 is max users client.on('266', function(event) { serverMsg(event.params[3]) }) client.on('irc error', function(event) { errorMsg(event.error, event.reason, event.channel) }); //No such channel error client.on('403', function(event) { errorMsg("403", event.params[2], event.params[1]) }); } function errorMsg(error, message, param) { var chatTab = $(".active .chat"); chatTab.append("Error: "+error+"!
"); if (param.length > 0) { chatTab.append("Param: "+param+"
"); } if (message.length > 0) { chatTab.append("Message: "+message+"
"); } chatTab.scrollTop(chatTab.prop("scrollHeight")); console.log('error: ', error); } function serverMsg(message) { var statusTab = $("[data-tab=status].chat"); statusTab.append(""+message+"
") statusTab.scrollTop(statusTab.prop("scrollHeight")); } function receiveMsg(target, from, message, from_server) { // message = message.autoLink( { // callback: function(url) { // return /\.(gif|png|jpe?g)$/i.test(url) ? '' : null; // }, // target: "_blank" // }); // console.log(message.autoLink()); if (from_server) { serverMsg(message) return false } numChans = globals.channels.length console.log(from + ' => '+ target +': ' + message); if (target === client.user.nick) { target = from } var chatTab = $("[data-tab='"+ target +"'].chat"); if (chatTab.length == 0) { $(".ui.menu").append(""+target+""); $("#chan-container").append(""); chanTab = $("[data-tab="+target+"].tab"); chanTab.append(""); $('.menu .item').tab({history:false}); global.channels.push(target) numChans++; chatTab = $("[data-tab="+ target +"].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 = util.imagify(imgMatch[0],target); 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) { youtubeParts = youtubeMatch[0].split("/"); youtubeId = youtubeParts[youtubeParts.length-1]; youtubeId = youtubeId.replace("watch?v=",""); chatTab.append(""); } chatTab.scrollTop(chatTab.prop("scrollHeight")); }