From 90533882ae77f811f01cc842fce9cab68df877ba Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 17 Aug 2016 15:13:29 +0100 Subject: Fixed receiving messages --- handlers.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 2 deletions(-) (limited to 'handlers.js') diff --git a/handlers.js b/handlers.js index 88259c7..b2437bc 100644 --- a/handlers.js +++ b/handlers.js @@ -1,9 +1,30 @@ -module.exports.handleCommands = function(client) { +var linkify = require("linkifyjs"); +var linkifyHtml = require('linkifyjs/html'); +var util = require("./util.js") +var numChans = 1; +var channels; +var buffers = []; +var client; +module.exports.setChannels = function(myChannels) { + console.log("setting channels: " + myChannels); + channels = myChannels; +} +module.exports.handleCommands = function(myClient) { + client = myClient; client.on('registered', function(event) { console.log(event); + console.log("channels: " + channels); + channels.forEach(function(channelStr) { + console.log(channelStr); + joinChannel(channelStr, true); + }); }); - + client.on('message', function(event) { + console.log("messsagee + " + event.message) + console.log(event); + receiveMsg(event.target, event.nick, event.message) + }) client.on('userlist', function(event) { console.log(event); channel = event.channel.replace("#","") @@ -19,4 +40,86 @@ module.exports.handleCommands = function(client) { } } }); + +} + +function joinChannel(channel, sendJoin) { + console.log("here"); + if (sendJoin) { + var channelObj = client.channel(channel); + buffers.push(channelObj); + channelObj.join(); + channelObj.say("beep"); + } + 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}); + + channelObj.updateUserList(function() { + channel = event.channel.replace("#","") + console.log(channel); + if($(".ui.tab[data-tab=chan-"+channel+"]").length == 0) { + console.log("there"); + } + for(nick in channelObj.users) { + if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length == 0) { + $(".ui.users[data-tab=chan-"+channel+"]").append("
"+nick+"
") + } + } + }) + numChans++; +} + +function receiveMsg(channel, from, message) { + // message = message.autoLink( { + // callback: function(url) { + // return /\.(gif|png|jpe?g)$/i.test(url) ? '' : null; + // }, + // target: "_blank" + // }); + // console.log(message.autoLink()); + channel = channel.replace("#","") + console.log(from + ' => #'+ channel +': ' + message); + console.log($("[data-tab=chan-"+ channel +"].chat")); + var chatTab = $("[data-tab=chan-"+ channel +"].chat"); + console.log(chatTab); + message = linkifyHtml(message); + console.log(from + ' => #'+ channel +': ' + 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],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")); } -- cgit v1.2.3