From 4a68b78fd512f60a0c76275e81b7e8f2a90bac4c Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 18 Dec 2016 11:24:50 +0200 Subject: [PATCH] Implement away message restoration on reconnections and auto away --- src/client.js | 24 ++++++++++++++++++++++++ src/models/network.js | 3 +++ src/plugins/inputs/away.js | 14 ++++++-------- src/plugins/irc-events/connection.js | 9 +++++++++ test/models/network.js | 2 ++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/client.js b/src/client.js index b7e1b3ef..00f3eb35 100644 --- a/src/client.js +++ b/src/client.js @@ -65,6 +65,7 @@ function Client(manager, name, config) { config = {}; } _.merge(this, { + awayMessage: "", lastActiveChannel: -1, attachedClients: {}, config: config, @@ -485,6 +486,16 @@ Client.prototype.clientAttach = function(socketId) { client.attachedClients[socketId] = client.lastActiveChannel; + if (client.awayMessage && _.size(client.attachedClients) === 0) { + client.networks.forEach(function(network) { + // Only remove away on client attachment if + // there is no away message on this network + if (!network.awayMessage) { + network.irc.raw("AWAY"); + } + }); + } + // Update old networks to store ip and hostmask client.networks.forEach(network => { if (!network.ip) { @@ -508,7 +519,19 @@ Client.prototype.clientAttach = function(socketId) { }; Client.prototype.clientDetach = function(socketId) { + const client = this; + delete this.attachedClients[socketId]; + + if (client.awayMessage && _.size(client.attachedClients) === 0) { + client.networks.forEach(function(network) { + // Only set away on client deattachment if + // there is no away message on this network + if (!network.awayMessage) { + network.irc.raw("AWAY", client.awayMessage); + } + }); + } }; Client.prototype.save = _.debounce(function SaveClient() { @@ -518,6 +541,7 @@ Client.prototype.save = _.debounce(function SaveClient() { const client = this; let json = {}; + json.awayMessage = client.awayMessage; json.networks = this.networks.map(n => n.export()); client.manager.updateUser(client.name, json); }, 1000, {maxWait: 10000}); diff --git a/src/models/network.js b/src/models/network.js index 0ddd3530..4484ed96 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -14,6 +14,7 @@ function Network(attr) { port: 6667, tls: false, password: "", + awayMessage: "", commands: [], username: "", realname: "", @@ -56,6 +57,7 @@ Network.prototype.setNick = function(nick) { Network.prototype.toJSON = function() { return _.omit(this, [ + "awayMessage", "chanCache", "highlightRegex", "irc", @@ -65,6 +67,7 @@ Network.prototype.toJSON = function() { Network.prototype.export = function() { var network = _.pick(this, [ + "awayMessage", "nick", "name", "host", diff --git a/src/plugins/inputs/away.js b/src/plugins/inputs/away.js index 201559fe..34c58596 100644 --- a/src/plugins/inputs/away.js +++ b/src/plugins/inputs/away.js @@ -3,17 +3,15 @@ exports.commands = ["away", "back"]; exports.input = function(network, chan, cmd, args) { - if (cmd === "away") { - let reason = " "; + let reason = ""; - if (args.length > 0) { - reason = args.join(" "); - } + if (cmd === "away") { + reason = args.length > 0 ? args.join(" ") : " "; network.irc.raw("AWAY", reason); - - return; + } else { // back command + network.irc.raw("AWAY"); } - network.irc.raw("AWAY"); + network.awayMessage = reason; }; diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index a8ecef54..4e34a154 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -1,5 +1,6 @@ "use strict"; +var _ = require("lodash"); var Msg = require("../../models/msg"); var Chan = require("../../models/chan"); var Helper = require("../../helper"); @@ -18,6 +19,14 @@ module.exports = function(irc, network) { }), true); } + // Always restore away message for this network + if (network.awayMessage) { + irc.raw("AWAY", network.awayMessage); + // Only set generic away message if there are no clients attached + } else if (client.awayMessage && _.size(client.attachedClients) === 0) { + irc.raw("AWAY", client.awayMessage); + } + var delay = 1000; var commands = network.commands; if (Array.isArray(commands)) { diff --git a/test/models/network.js b/test/models/network.js index 438c6251..a5ebb5e4 100644 --- a/test/models/network.js +++ b/test/models/network.js @@ -10,6 +10,7 @@ describe("Network", function() { describe("#export()", function() { it("should produce an valid object", function() { var network = new Network({ + awayMessage: "I am away", name: "networkName", channels: [ new Chan({name: "#thelounge"}), @@ -21,6 +22,7 @@ describe("Network", function() { network.setNick("chillin`"); expect(network.export()).to.deep.equal({ + awayMessage: "I am away", name: "networkName", host: "", port: 6667,