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: [],
|
|
|
|
connected: false,
|
|
|
|
id: id++,
|
|
|
|
irc: null,
|
|
|
|
}, 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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Network.prototype.toJSON = function() {
|
2016-03-07 21:09:42 +00:00
|
|
|
var json = _.extend(this, {nick: (this.irc && this.irc.user.nick) || ""});
|
2014-10-11 20:44:56 +00:00
|
|
|
return _.omit(json, "irc", "password");
|
|
|
|
};
|
|
|
|
|
|
|
|
Network.prototype.export = function() {
|
2014-10-11 22:47:24 +00:00
|
|
|
var network = _.pick(this, [
|
|
|
|
"name",
|
|
|
|
"host",
|
|
|
|
"port",
|
|
|
|
"tls",
|
|
|
|
"password",
|
|
|
|
"username",
|
2014-11-09 16:01:22 +00:00
|
|
|
"realname",
|
|
|
|
"commands"
|
2014-10-11 22:47:24 +00:00
|
|
|
]);
|
2016-03-07 21:09:42 +00:00
|
|
|
network.nick = (this.irc && this.irc.user.nick) || "";
|
2016-02-14 17:09:51 +00:00
|
|
|
network.join = _.map(
|
|
|
|
_.filter(this.channels, {type: "channel"}),
|
2014-10-11 20:44:56 +00:00
|
|
|
"name"
|
2014-10-11 22:47:24 +00:00
|
|
|
).join(",");
|
2014-10-11 20:44:56 +00:00
|
|
|
return network;
|
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);
|
|
|
|
}
|
|
|
|
}
|