1 | "use strict";
|
2 |
|
3 | var _ = require("lodash");
|
4 | var Chan = require("./chan");
|
5 |
|
6 | module.exports = Network;
|
7 |
|
8 | let id = 1;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const filteredFromClient = {
|
14 | awayMessage: true,
|
15 | chanCache: true,
|
16 | highlightRegex: true,
|
17 | irc: true,
|
18 | password: true,
|
19 | };
|
20 |
|
21 | function 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 |
|
56 | Network.prototype.destroy = function() {
|
57 | this.channels.forEach((channel) => channel.destroy());
|
58 | };
|
59 |
|
60 | Network.prototype.setNick = function(nick) {
|
61 | this.nick = nick;
|
62 | this.highlightRegex = new RegExp(
|
63 |
|
64 | "(?:^|[^a-z0-9]|\x03[0-9]{1,2})" +
|
65 |
|
66 |
|
67 | _.escapeRegExp(nick) +
|
68 |
|
69 |
|
70 | "(?:[^a-z0-9]|$)",
|
71 |
|
72 |
|
73 | "i"
|
74 | );
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | Network.prototype.getFilteredClone = function(lastActiveChannel, lastMessage) {
|
87 | return Object.keys(this).reduce((newNetwork, prop) => {
|
88 | if (prop === "channels") {
|
89 |
|
90 | newNetwork[prop] = this[prop].map((channel) => channel.getFilteredClone(lastActiveChannel, lastMessage));
|
91 | } else if (!filteredFromClient[prop]) {
|
92 |
|
93 | newNetwork[prop] = this[prop];
|
94 | }
|
95 |
|
96 | return newNetwork;
|
97 | }, {});
|
98 | };
|
99 |
|
100 | Network.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 |
|
133 | Network.prototype.getChannel = function(name) {
|
134 | name = name.toLowerCase();
|
135 |
|
136 | return _.find(this.channels, function(that, i) {
|
137 |
|
138 | return i > 0 && that.name.toLowerCase() === name;
|
139 | });
|
140 | };
|