From ac48b1195c4cd68f716e4119181c720cc9ec8b4f Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 16 Oct 2016 22:34:58 +0100 Subject: Handle error and server messages --- commands.js | 8 ++--- handlers.js | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- index.js | 60 ++++++-------------------------- 3 files changed, 121 insertions(+), 59 deletions(-) diff --git a/commands.js b/commands.js index 138c55f..1847b74 100644 --- a/commands.js +++ b/commands.js @@ -5,14 +5,14 @@ module.exports.init = function(myClient) { client = myClient } -function sendMsg(message, channel) { +module.exports.sendMsg = function(message, channel) { client.say(channel, message); var chanTab = $("[data-tab='"+ channel +"'].chat"); - chanTab.append("

<" + myNick + "> " + message +"

"); + chanTab.append("

<" + globals.myNick + "> " + message +"

"); chanTab.scrollTop(chanTab.prop("scrollHeight")); } -function sendPm(message, nick) { +module.exports.sendPm = function(message, nick) { var chanTab = $("[data-tab="+nick+"].tab"); $(".active").removeClass("active"); @@ -31,7 +31,7 @@ function sendPm(message, nick) { var chatTab = $("[data-tab="+nick+"].chat"); console.log(nick + ': ' + message); client.say(nick, message); - chatTab.append("

<" + myNick + "> " + message +"

"); + chatTab.append("

<" + globals.myNick + "> " + message +"

"); chatTab.scrollTop(chanTab.prop("scrollHeight")); } diff --git a/handlers.js b/handlers.js index 24b783c..9142f04 100644 --- a/handlers.js +++ b/handlers.js @@ -18,7 +18,7 @@ module.exports.handleCommands = function(myClient) { }); }); client.on('message', function(event) { - receiveMsg(event.target, event.nick, event.message) + receiveMsg(event.target, event.nick, event.message, event.from_server) }) client.on('userlist', function(event) { @@ -48,17 +48,31 @@ module.exports.handleCommands = function(myClient) { 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+"

") + chatTab.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"); @@ -160,7 +174,7 @@ module.exports.handleCommands = function(myClient) { modeLine = " removes channel half-operator status from " + argument; } else if (mode == "-q") { modeLine = " removes channel owner status from " + argument; - } else if (argument == myNick) { + } else if (argument == globals.myNick) { modeLine = " sets mode " + mode + " on " + argument; } else if (argument != null) { modeLine = " sets mode " + mode + " " + argument + " on " +channel; @@ -190,9 +204,90 @@ module.exports.handleCommands = function(myClient) { } }) + 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 receiveMsg(target, from, message) { + +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; @@ -200,6 +295,11 @@ function receiveMsg(target, from, message) { // target: "_blank" // }); // console.log(message.autoLink()); + + if (from_server) { + serverMsg(message) + return false + } numChans = globals.channels.length console.log(from + ' => '+ target +': ' + message); diff --git a/index.js b/index.js index 48f55a0..1975bc0 100644 --- a/index.js +++ b/index.js @@ -148,53 +148,15 @@ $("#send-message").keypress(function(e) { } }); -function createListeners(client) { - -client.addListener('error', function(message) { - console.log('error: ', message); -}); - - -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_yourhost", "rpl_created", "rpl_myinfo", "rpl_isupport", "rpl_luserclient", "rpl_luserop", "rpl_luserme", "rpl_luserchannels", "rpl_localusers", "rpl_globalusers"]; - if (statusCmds.indexOf(message.command) > -1 || message.command.startsWith("rpl_")) { - statusTab.append("

"+message.command + ": " +message.args+"

") - statusTab.scrollTop(statusTab.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('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")); +// }); +// }); -- cgit v1.2.3