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) }) 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("
"+mode+nick+"
") } }) }) client.on('topic', function(event) { channel = event.channel topic = event.topic //TODO: Handle topicsetby to add the user and time for the topic nick = "vov" time = "vov" var chatTab = $("[data-tab='"+ channel +"'].chat"); if($(".ui.tab[data-tab='"+channel+"']").length == 0) { commands.joinChannel(channel, false) } chatTab.append("

Topic for "+channel+" is "+topic+" - set by "+nick+" at "+time+"

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

"+topic+"

") }) 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+"
") } var chatTab = $("[data-tab='"+ channel +"'].chat"); chatTab.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")); } }); }) } function receiveMsg(target, from, message) { // message = message.autoLink( { // callback: function(url) { // return /\.(gif|png|jpe?g)$/i.test(url) ? '' : null; // }, // target: "_blank" // }); // console.log(message.autoLink()); 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")); }