Merge pull request #775 from thelounge/patches/auto-away

Auto away when no clients are connected
This commit is contained in:
Pavel Djundik 2017-03-31 19:15:55 +03:00 committed by GitHub
commit 332bbad2fd
5 changed files with 44 additions and 8 deletions

View File

@ -65,6 +65,7 @@ function Client(manager, name, config) {
config = {}; config = {};
} }
_.merge(this, { _.merge(this, {
awayMessage: "",
lastActiveChannel: -1, lastActiveChannel: -1,
attachedClients: {}, attachedClients: {},
config: config, config: config,
@ -485,6 +486,16 @@ Client.prototype.clientAttach = function(socketId) {
client.attachedClients[socketId] = client.lastActiveChannel; 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 // Update old networks to store ip and hostmask
client.networks.forEach(network => { client.networks.forEach(network => {
if (!network.ip) { if (!network.ip) {
@ -508,7 +519,19 @@ Client.prototype.clientAttach = function(socketId) {
}; };
Client.prototype.clientDetach = function(socketId) { Client.prototype.clientDetach = function(socketId) {
const client = this;
delete this.attachedClients[socketId]; 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() { Client.prototype.save = _.debounce(function SaveClient() {
@ -518,6 +541,7 @@ Client.prototype.save = _.debounce(function SaveClient() {
const client = this; const client = this;
let json = {}; let json = {};
json.awayMessage = client.awayMessage;
json.networks = this.networks.map(n => n.export()); json.networks = this.networks.map(n => n.export());
client.manager.updateUser(client.name, json); client.manager.updateUser(client.name, json);
}, 1000, {maxWait: 10000}); }, 1000, {maxWait: 10000});

View File

@ -14,6 +14,7 @@ function Network(attr) {
port: 6667, port: 6667,
tls: false, tls: false,
password: "", password: "",
awayMessage: "",
commands: [], commands: [],
username: "", username: "",
realname: "", realname: "",
@ -56,6 +57,7 @@ Network.prototype.setNick = function(nick) {
Network.prototype.toJSON = function() { Network.prototype.toJSON = function() {
return _.omit(this, [ return _.omit(this, [
"awayMessage",
"chanCache", "chanCache",
"highlightRegex", "highlightRegex",
"irc", "irc",
@ -65,6 +67,7 @@ Network.prototype.toJSON = function() {
Network.prototype.export = function() { Network.prototype.export = function() {
var network = _.pick(this, [ var network = _.pick(this, [
"awayMessage",
"nick", "nick",
"name", "name",
"host", "host",

View File

@ -3,17 +3,15 @@
exports.commands = ["away", "back"]; exports.commands = ["away", "back"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (cmd === "away") {
let reason = ""; let reason = "";
if (args.length > 0) { if (cmd === "away") {
reason = args.join(" "); reason = args.length > 0 ? args.join(" ") : " ";
}
network.irc.raw("AWAY", reason); network.irc.raw("AWAY", reason);
} else { // back command
return; network.irc.raw("AWAY");
} }
network.irc.raw("AWAY"); network.awayMessage = reason;
}; };

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Helper = require("../../helper"); var Helper = require("../../helper");
@ -18,6 +19,14 @@ module.exports = function(irc, network) {
}), true); }), 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 delay = 1000;
var commands = network.commands; var commands = network.commands;
if (Array.isArray(commands)) { if (Array.isArray(commands)) {

View File

@ -10,6 +10,7 @@ describe("Network", function() {
describe("#export()", function() { describe("#export()", function() {
it("should produce an valid object", function() { it("should produce an valid object", function() {
var network = new Network({ var network = new Network({
awayMessage: "I am away",
name: "networkName", name: "networkName",
channels: [ channels: [
new Chan({name: "#thelounge"}), new Chan({name: "#thelounge"}),
@ -21,6 +22,7 @@ describe("Network", function() {
network.setNick("chillin`"); network.setNick("chillin`");
expect(network.export()).to.deep.equal({ expect(network.export()).to.deep.equal({
awayMessage: "I am away",
name: "networkName", name: "networkName",
host: "", host: "",
port: 6667, port: 6667,