Implement away message restoration on reconnections and auto away

This commit is contained in:
Pavel Djundik 2016-12-18 11:24:50 +02:00 committed by Jérémie Astori
parent b66afb6939
commit 4a68b78fd5
5 changed files with 44 additions and 8 deletions

View file

@ -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});

View file

@ -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",

View file

@ -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;
};

View file

@ -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)) {

View file

@ -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,