UNPKG

3 kBJavaScriptView Raw
1"use strict";
2
3var _ = require("lodash");
4var Chan = require("./chan");
5
6module.exports = Network;
7
8let id = 1;
9
10/**
11 * @type {Object} List of keys which should not be sent to the client.
12 */
13const filteredFromClient = {
14 awayMessage: true,
15 chanCache: true,
16 highlightRegex: true,
17 irc: true,
18 password: true,
19};
20
21function Network(attr) {
22 _.defaults(this, attr, {
23 name: "",
24 host: "",
25 port: 6667,
26 tls: false,
27 password: "",
28 awayMessage: "",
29 commands: [],
30 username: "",
31 realname: "",
32 channels: [],
33 ip: null,
34 hostname: null,
35 id: id++,
36 irc: null,
37 serverOptions: {
38 PREFIX: [],
39 NETWORK: "",
40 },
41 chanCache: [],
42 });
43
44 if (!this.name) {
45 this.name = this.host;
46 }
47
48 this.channels.unshift(
49 new Chan({
50 name: this.name,
51 type: Chan.Type.LOBBY,
52 })
53 );
54}
55
56Network.prototype.destroy = function() {
57 this.channels.forEach((channel) => channel.destroy());
58};
59
60Network.prototype.setNick = function(nick) {
61 this.nick = nick;
62 this.highlightRegex = new RegExp(
63 // Do not match characters and numbers (unless IRC color)
64 "(?:^|[^a-z0-9]|\x03[0-9]{1,2})" +
65
66 // Escape nickname, as it may contain regex stuff
67 _.escapeRegExp(nick) +
68
69 // Do not match characters and numbers
70 "(?:[^a-z0-9]|$)",
71
72 // Case insensitive search
73 "i"
74 );
75};
76
77/**
78 * Get a clean clone of this network that will be sent to the client.
79 * This function performs manual cloning of network object for
80 * better control of performance and memory usage.
81 *
82 * Both of the parameters that are accepted by this function are passed into channels' getFilteredClone call.
83 *
84 * @see {@link Chan#getFilteredClone}
85 */
86Network.prototype.getFilteredClone = function(lastActiveChannel, lastMessage) {
87 return Object.keys(this).reduce((newNetwork, prop) => {
88 if (prop === "channels") {
89 // Channels objects perform their own cloning
90 newNetwork[prop] = this[prop].map((channel) => channel.getFilteredClone(lastActiveChannel, lastMessage));
91 } else if (!filteredFromClient[prop]) {
92 // Some properties that are not useful for the client are skipped
93 newNetwork[prop] = this[prop];
94 }
95
96 return newNetwork;
97 }, {});
98};
99
100Network.prototype.export = function() {
101 var network = _.pick(this, [
102 "awayMessage",
103 "nick",
104 "name",
105 "host",
106 "port",
107 "tls",
108 "password",
109 "username",
110 "realname",
111 "commands",
112 "ip",
113 "hostname",
114 ]);
115
116 network.channels = this.channels
117 .filter(function(channel) {
118 return channel.type === Chan.Type.CHANNEL || channel.type === Chan.Type.QUERY;
119 })
120 .map(function(chan) {
121 const keys = ["name"];
122 if (chan.type === Chan.Type.CHANNEL) {
123 keys.push("key");
124 } else if (chan.type === Chan.Type.QUERY) {
125 keys.push("type");
126 }
127 return _.pick(chan, keys);
128 });
129
130 return network;
131};
132
133Network.prototype.getChannel = function(name) {
134 name = name.toLowerCase();
135
136 return _.find(this.channels, function(that, i) {
137 // Skip network lobby (it's always unshifted into first position)
138 return i > 0 && that.name.toLowerCase() === name;
139 });
140};