From 28ae544b2a08e64709b35f2302ed197a31251a90 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 8 Mar 2016 20:50:48 +0200 Subject: [PATCH] Move connection events to a separate file, sync PREFIX with frontend --- client/js/lounge.js | 6 +++ src/client.js | 66 ++++++--------------------- src/models/network.js | 4 +- src/plugins/irc-events/connection.js | 68 ++++++++++++++++++++++++++++ src/plugins/irc-events/message.js | 5 ++ src/plugins/irc-events/welcome.js | 1 - 6 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 src/plugins/irc-events/connection.js diff --git a/client/js/lounge.js b/client/js/lounge.js index c134eba3..9335e557 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -146,6 +146,7 @@ $(function() { }) ); var channels = $.map(data.networks, function(n) { + sidebar.find("#network-" + n.id).data("options", n.serverOptions); return n.channels; }); chat.html( @@ -294,6 +295,7 @@ $(function() { networks: [data.network] }) ); + sidebar.find("#network-" + data.network.id).data("options", data.network.serverOptions); chat.append( render("chat", { channels: data.network.channels @@ -310,6 +312,10 @@ $(function() { sortable(); }); + socket.on("network_changed", function(data) { + sidebar.find("#network-" + data.network).data("options", data.serverOptions); + }); + socket.on("nick", function(data) { var id = data.network; var nick = data.nick; diff --git a/src/client.js b/src/client.js index 5196d078..284c4901 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,6 @@ var _ = require("lodash"); var Chan = require("./models/chan"); var crypto = require("crypto"); -var identd = require("./identd"); var log = require("./log"); var Msg = require("./models/msg"); var Network = require("./models/network"); @@ -12,6 +11,7 @@ module.exports = Client; var id = 0; var events = [ + "connection", "ctcp", "error", "invite", @@ -172,8 +172,8 @@ Client.prototype.connect = function(args) { return; } - var irc = new ircFramework.Client(); - irc.connect({ + network.irc = new ircFramework.Client(); + network.irc.connect({ host: network.host, port: network.port, nick: nick, @@ -186,49 +186,7 @@ Client.prototype.connect = function(args) { auto_reconnect: false, // TODO: Enable auto reconnection }); - network.irc = irc; - - events.forEach(function(plugin) { - var path = "./plugins/irc-events/" + plugin; - require(path).apply(client, [ - irc, - network - ]); - }); - - irc.on("raw socket connected", function() { - identd.hook(irc.socket, network.username); - }); - - irc.on("socket connected", function() { - client.emit("msg", { - chan: network.channels[0].id, - msg: new Msg({ - text: "Connected to the network." - }) - }); - }); - - irc.on("socket close", function() { - client.emit("msg", { - chan: network.channels[0].id, - msg: new Msg({ - type: Msg.Type.ERROR, - text: "Disconnected from the network." - }) - }); - }); - - irc.on("reconnecting", function() { - client.emit("msg", { - chan: network.channels[0].id, - msg: new Msg({ - text: "Reconnecting..." - }) - }); - }); - - irc.on("registered", function() { + network.irc.on("registered", function() { var delay = 1000; var commands = args.commands; if (Array.isArray(commands)) { @@ -247,10 +205,18 @@ Client.prototype.connect = function(args) { if (join) { setTimeout(function() { join = join.replace(/\,/g, " ").split(/\s+/g); - irc.join(join); + network.irc.join(join); }, delay); } }); + + events.forEach(function(plugin) { + var path = "./plugins/irc-events/" + plugin; + require(path).apply(client, [ + network.irc, + network + ]); + }); }; Client.prototype.setPassword = function(hash) { @@ -373,10 +339,8 @@ Client.prototype.quit = function() { } this.networks.forEach(function(network) { var irc = network.irc; - if (network.connected) { - irc.quit(); - } else { - irc.stream.end(); + if (irc.connection) { + irc.connection.end(); } }); }; diff --git a/src/models/network.js b/src/models/network.js index 5e0200dc..cb6d4735 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -16,9 +16,11 @@ function Network(attr) { username: "", realname: "", channels: [], - connected: false, id: id++, irc: null, + serverOptions: { + PREFIX: [], + }, }, attr)); this.name = attr.name || prettify(attr.host); this.channels.unshift( diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js new file mode 100644 index 00000000..44e75714 --- /dev/null +++ b/src/plugins/irc-events/connection.js @@ -0,0 +1,68 @@ +var identd = require("../../identd"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + + client.emit("msg", { + chan: network.channels[0].id, + msg: new Msg({ + text: "Network created, connecting to " + network.host + ":" + network.port + "..." + }) + }); + + irc.on("raw socket connected", function() { + identd.hook(irc.socket, network.username); + }); + + irc.on("socket connected", function() { + client.emit("msg", { + chan: network.channels[0].id, + msg: new Msg({ + text: "Connected to the network." + }) + }); + }); + + irc.on("socket close", function() { + client.emit("msg", { + chan: network.channels[0].id, + msg: new Msg({ + text: "Disconnected from the network." + }) + }); + }); + + irc.on("socket error", function(err) { + console.log(err); + client.emit("msg", { + chan: network.channels[0].id, + msg: new Msg({ + type: Msg.Type.ERROR, + text: "Socket error: " + err + }) + }); + }); + + irc.on("reconnecting", function() { + client.emit("msg", { + chan: network.channels[0].id, + msg: new Msg({ + text: "Reconnecting..." + }) + }); + }); + + irc.on("server options", function(data) { + if (network.serverOptions.PREFIX === data.options.PREFIX) { + return; + } + + network.serverOptions.PREFIX = data.options.PREFIX; + + client.emit("network_changed", { + network: network.id, + serverOptions: network.serverOptions + }); + }); +}; diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index c8677113..467ec2ae 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -42,6 +42,11 @@ module.exports = function(irc, network) { } } + // Server messages go to server window + if (data.from_server) { + chan = network.channels[0]; + } + var self = data.nick === irc.user.nick; // Self messages are never highlighted diff --git a/src/plugins/irc-events/welcome.js b/src/plugins/irc-events/welcome.js index 23c5f54f..ef8de616 100644 --- a/src/plugins/irc-events/welcome.js +++ b/src/plugins/irc-events/welcome.js @@ -3,7 +3,6 @@ var Msg = require("../../models/msg"); module.exports = function(irc, network) { var client = this; irc.on("registered", function(data) { - network.connected = true; network.nick = data.nick; var lobby = network.channels[0]; var msg = new Msg({