hardlounge/src/models/network.js

141 lines
2.9 KiB
JavaScript
Raw Normal View History

"use strict";
2014-09-13 21:29:45 +00:00
var _ = require("lodash");
var Chan = require("./chan");
module.exports = Network;
let id = 1;
2014-09-13 21:29:45 +00:00
/**
* @type {Object} List of keys which should not be sent to the client.
*/
const filteredFromClient = {
awayMessage: true,
chanCache: true,
highlightRegex: true,
irc: true,
password: true,
};
2014-09-13 21:29:45 +00:00
function Network(attr) {
_.defaults(this, attr, {
2014-10-11 20:44:56 +00:00
name: "",
host: "",
port: 6667,
tls: false,
password: "",
awayMessage: "",
2014-11-09 16:01:22 +00:00
commands: [],
2014-10-11 20:44:56 +00:00
username: "",
realname: "",
2014-09-13 21:29:45 +00:00
channels: [],
2016-04-03 05:12:49 +00:00
ip: null,
hostname: null,
2014-09-13 21:29:45 +00:00
id: id++,
irc: null,
serverOptions: {
PREFIX: [],
NETWORK: "",
},
2016-05-29 02:07:34 +00:00
chanCache: [],
});
2017-07-06 12:02:32 +00:00
if (!this.name) {
this.name = this.host;
}
2014-09-13 21:29:45 +00:00
this.channels.unshift(
2014-10-11 22:47:24 +00:00
new Chan({
name: this.name,
type: Chan.Type.LOBBY,
2014-10-11 22:47:24 +00:00
})
2014-09-13 21:29:45 +00:00
);
}
Network.prototype.destroy = function() {
this.channels.forEach((channel) => channel.destroy());
};
2016-05-12 11:15:38 +00:00
Network.prototype.setNick = function(nick) {
this.nick = nick;
this.highlightRegex = new RegExp(
// Do not match characters and numbers (unless IRC color)
"(?:^|[^a-z0-9]|\x03[0-9]{1,2})" +
// Escape nickname, as it may contain regex stuff
_.escapeRegExp(nick) +
2016-05-12 11:15:38 +00:00
// Do not match characters and numbers
"(?:[^a-z0-9]|$)",
// Case insensitive search
"i"
);
};
/**
* Get a clean clone of this network that will be sent to the client.
* This function performs manual cloning of network object for
* better control of performance and memory usage.
*
* Both of the parameters that are accepted by this function are passed into channels' getFilteredClone call.
*
* @see {@link Chan#getFilteredClone}
*/
Network.prototype.getFilteredClone = function(lastActiveChannel, lastMessage) {
return Object.keys(this).reduce((newNetwork, prop) => {
if (prop === "channels") {
// Channels objects perform their own cloning
newNetwork[prop] = this[prop].map((channel) => channel.getFilteredClone(lastActiveChannel, lastMessage));
} else if (!filteredFromClient[prop]) {
// Some properties that are not useful for the client are skipped
newNetwork[prop] = this[prop];
}
return newNetwork;
}, {});
2014-10-11 20:44:56 +00:00
};
Network.prototype.export = function() {
2014-10-11 22:47:24 +00:00
var network = _.pick(this, [
"awayMessage",
2016-05-12 11:15:38 +00:00
"nick",
2014-10-11 22:47:24 +00:00
"name",
"host",
"port",
"tls",
"password",
"username",
2014-11-09 16:01:22 +00:00
"realname",
2016-04-03 05:12:49 +00:00
"commands",
"ip",
"hostname",
2014-10-11 22:47:24 +00:00
]);
2016-05-12 11:15:38 +00:00
2016-06-19 17:12:42 +00:00
network.channels = this.channels
.filter(function(channel) {
2018-01-30 16:46:34 +00:00
return channel.type === Chan.Type.CHANNEL || channel.type === Chan.Type.QUERY;
2016-06-19 17:12:42 +00:00
})
.map(function(chan) {
2018-01-30 16:46:34 +00:00
const keys = ["name"];
if (chan.type === Chan.Type.CHANNEL) {
keys.push("key");
} else if (chan.type === Chan.Type.QUERY) {
keys.push("type");
}
return _.pick(chan, keys);
2016-06-19 17:12:42 +00:00
});
2016-05-12 11:15:38 +00:00
2014-10-11 20:44:56 +00:00
return network;
2014-09-13 21:29:45 +00:00
};
2016-03-20 14:28:47 +00:00
Network.prototype.getChannel = function(name) {
name = name.toLowerCase();
return _.find(this.channels, function(that, i) {
// Skip network lobby (it's always unshifted into first position)
return i > 0 && that.name.toLowerCase() === name;
2016-03-20 14:28:47 +00:00
});
};