2014-09-13 21:29:45 +00:00
|
|
|
var _ = require("lodash");
|
|
|
|
var Chan = require("./chan");
|
|
|
|
|
|
|
|
module.exports = Network;
|
|
|
|
|
|
|
|
var id = 0;
|
|
|
|
|
|
|
|
function Network(attr) {
|
|
|
|
_.merge(this, _.extend({
|
2014-10-11 20:44:56 +00:00
|
|
|
name: "",
|
|
|
|
host: "",
|
|
|
|
port: 6667,
|
|
|
|
tls: false,
|
|
|
|
password: "",
|
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,
|
2016-03-08 18:50:48 +00:00
|
|
|
serverOptions: {
|
|
|
|
PREFIX: [],
|
|
|
|
},
|
2014-09-13 21:29:45 +00:00
|
|
|
}, attr));
|
|
|
|
this.name = attr.name || prettify(attr.host);
|
|
|
|
this.channels.unshift(
|
2014-10-11 22:47:24 +00:00
|
|
|
new Chan({
|
|
|
|
name: this.name,
|
|
|
|
type: Chan.Type.LOBBY
|
|
|
|
})
|
2014-09-13 21:29:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
nick.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&") +
|
|
|
|
|
|
|
|
// Do not match characters and numbers
|
|
|
|
"(?:[^a-z0-9]|$)",
|
|
|
|
|
|
|
|
// Case insensitive search
|
|
|
|
"i"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
Network.prototype.toJSON = function() {
|
2016-05-12 11:15:38 +00:00
|
|
|
return _.omit(this, [
|
|
|
|
"irc",
|
|
|
|
"password",
|
|
|
|
"highlightRegex"
|
|
|
|
]);
|
2014-10-11 20:44:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Network.prototype.export = function() {
|
2014-10-11 22:47:24 +00:00
|
|
|
var network = _.pick(this, [
|
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) {
|
|
|
|
return channel.type === Chan.Type.CHANNEL;
|
|
|
|
})
|
|
|
|
.map(function(chan) {
|
|
|
|
return _.pick(chan, [
|
|
|
|
"name"
|
|
|
|
]);
|
|
|
|
});
|
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) {
|
|
|
|
return that.name.toLowerCase() === name;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
function prettify(host) {
|
|
|
|
var name = capitalize(host.split(".")[1]);
|
|
|
|
if (!name) {
|
|
|
|
name = host;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function capitalize(str) {
|
|
|
|
if (typeof str === "string") {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
}
|