diff options
Diffstat (limited to 'handlers.js')
-rw-r--r-- | handlers.js | 348 |
1 files changed, 348 insertions, 0 deletions
diff --git a/handlers.js b/handlers.js new file mode 100644 index 0000000..9142f04 --- /dev/null +++ b/handlers.js @@ -0,0 +1,348 @@ +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("<div class=item data-nick="+nick+">"+mode+nick+"</div>") + } + }) + }) + + client.on('topic', function(event) { + channel = event.channel + topic = event.topic + //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("<p class='chat-line'>Topic for "+channel+" is "+topic+"</p>") + var topicTab = $("[data-tab='"+ channel +"'].topic"); + topicTab.html("<p class='topic-line'>"+topic+"</p>") + }) + + 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("<p class='chat-line'>Set by "+nick+" at "+timestamp+"</p>") + 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("<p class='chat-line'>"+event.nick+": "+event.user+"@"+event.host+"</p>"); + chatTab.append("<p class='chat-line'> Real Name: "+event.real_name+"</p>"); + chatTab.append("<p class='chat-line'> Channels: " + event.channels+"</p>"); + chatTab.append("<p class='chat-line'> Server: "+event.server+" "+event.server_info+"</p>"); + if (event.operator.length > 0) { + chatTab.append("<p class='chat-line'>"+event.operator+"</p>"); + } + if (event.registered_nick.length > 0) { + chatTab.append("<p class='chat-line'>"+event.registered_nick+"</p>"); + } + if (event.away.length > 0) { + chatTab.append("<p class='chat-line'>"+event.away+"</p>"); + } + if (event.secure) { + chatTab.append("<p class='chat-line'>is using a secure connection</p>"); + } + chatTab.scrollTop(chatTab.prop("scrollHeight")); + + }) + + client.on('channel list', function(event) { + var statusTab = $("[data-tab='status'].chat"); + statusTab.append("<p class='chat-line'>Channel List:</p>") + event.forEach( function(channel) { + if (channel.topic.length == 0) { + statusTab.append("<p class='chat-line'>"+channel.channel+" ("+channel.num_users+")</p>"); + } else { + statusTab.append("<p class='chat-line'>"+channel.channel+" ("+channel.num_users+") : "+channel.topic+"</p>"); + } + 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("<div class=item data-nick="+nick+">"+nick+"</div>") + } + var chatTab = $("[data-tab='"+ channel +"'].chat"); + chatTab.append("<p class='chat-line'>"+nick+" joined "+channel+"</p>") + }); + + 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("<p class='chat-line'>"+nick+" left "+channel+" ("+message+")</p>") + 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("<p class='chat-line'>"+nick+" quit ("+message+")</p>") + 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("<p class='chat-line'>"+by+modeLine+"</p>"); + }) + 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("<p class='chat-line'>"+kicked+" was kicked from "+channel+" by "+by+" ("+reason+")</p>") + 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("<p class='chat-line'>Error: "+error+"!</p>"); + if (param.length > 0) { + chatTab.append("<p class='chat-line'>Param: "+param+"</p>"); + } + if (message.length > 0) { + chatTab.append("<p class='chat-line'>Message: "+message+"</p>"); + } + chatTab.scrollTop(chatTab.prop("scrollHeight")); + console.log('error: ', error); +} +function serverMsg(message) { + var statusTab = $("[data-tab=status].chat"); + statusTab.append("<p class='chat-line'>"+message+"</p>") + 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) ? '<img src="' + 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("<a class='item' data-id='"+numChans+"' data-tab='"+target+"'>"+target+"</a>"); + $("#chan-container").append("<div class='ui tab' data-id='"+numChans+"' data-tab='"+target+"'></div>"); + chanTab = $("[data-tab="+target+"].tab"); + chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='"+target+"'></div>"); + + $('.menu .item').tab({history:false}); + global.channels.push(target) + numChans++; + chatTab = $("[data-tab="+ target +"].chat"); + } + + message = linkifyHtml(message); + + chatTab.append("<p class='chat-line'><" + from + "> " + message +"</p>"); + 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("<p class=chat-line>"+image+"</p>"); + } + if (vidMatch != null && vidMatch.length > 0) { + chatTab.append("<p class=chat-line><video controls><source src="+vidMatch[0]+"></video></p>"); + } + if (audioMatch != null && audioMatch.length > 0) { + chatTab.append("<p class=chat-line><audio controls><source src="+audioMatch[0]+"></audio></p>"); + } + if (youtubeMatch != null && youtubeMatch.length > 0) { + youtubeParts = youtubeMatch[0].split("/"); + youtubeId = youtubeParts[youtubeParts.length-1]; + youtubeId = youtubeId.replace("watch?v=",""); + chatTab.append("<p class=chat-line><iframe width='480' height='270' src='https://www.youtube.com/embed/"+youtubeId+"' frameborder='0' allowfullscreen></iframe></p>"); + } + + chatTab.scrollTop(chatTab.prop("scrollHeight")); +} |