UNPKG

4.13 kBJavaScriptView Raw
1'use strict';
2
3require("core-js/modules/es.array.find.js");
4require("core-js/modules/es.array.index-of.js");
5require("core-js/modules/es.array.iterator.js");
6require("core-js/modules/es.array.slice.js");
7require("core-js/modules/es.array.some.js");
8require("core-js/modules/es.array.sort.js");
9require("core-js/modules/es.date.now.js");
10require("core-js/modules/es.function.name.js");
11require("core-js/modules/es.map.js");
12require("core-js/modules/es.object.to-string.js");
13require("core-js/modules/es.string.iterator.js");
14require("core-js/modules/web.dom-collections.iterator.js");
15var _ = {
16 find: require('lodash/find')
17};
18module.exports = NetworkInfo;
19function NetworkInfo() {
20 // Name of the network
21 this.name = 'Network';
22
23 // Name of the connected server
24 this.server = '';
25
26 // The reported IRCd type
27 this.ircd = '';
28
29 // Network provided options
30 this.options = {
31 CASEMAPPING: 'rfc1459',
32 PREFIX: [{
33 symbol: '~',
34 mode: 'q'
35 }, {
36 symbol: '&',
37 mode: 'a'
38 }, {
39 symbol: '@',
40 mode: 'o'
41 }, {
42 symbol: '%',
43 mode: 'h'
44 }, {
45 symbol: '+',
46 mode: 'v'
47 }]
48 };
49
50 // Network capabilities
51 this.cap = {
52 negotiating: false,
53 requested: [],
54 enabled: [],
55 available: new Map(),
56 isEnabled: function isEnabled(cap_name) {
57 return this.enabled.indexOf(cap_name) > -1;
58 }
59 };
60 this.time_offsets = [];
61 this.time_offset = 0;
62 this.timeToLocal = function timeToLocal(serverTimeMs) {
63 return serverTimeMs - this.getServerTimeOffset();
64 };
65 this.timeToServer = function timeToServer(localTimeMs) {
66 return localTimeMs + this.getServerTimeOffset();
67 };
68 this.getServerTimeOffset = function getServerTimeOffset() {
69 var sortedOffsets = this.time_offsets.slice(0).sort(function (a, b) {
70 return a - b;
71 });
72 return sortedOffsets[Math.floor(this.time_offsets.length / 2)] || 0;
73 };
74 this.addServerTimeOffset = function addServerTimeOffset(time) {
75 // add our new offset
76 var newOffset = time - Date.now();
77 this.time_offsets.push(newOffset);
78
79 // limit out offsets array to 7 enteries
80 if (this.time_offsets.length > 7) {
81 this.time_offsets = this.time_offsets.slice(this.time_offsets.length - 7);
82 }
83 var currentOffset = this.getServerTimeOffset();
84 if (newOffset - currentOffset > 2000 || newOffset - currentOffset < -2000) {
85 // skew was over 2 seconds, invalidate all but last offset
86 // > 2sec skew is a little large so just use that. Possible
87 // that the time on the IRCd actually changed
88 this.time_offsets = this.time_offsets.slice(-1);
89 }
90 this.time_offset = this.getServerTimeOffset();
91 };
92 this.supports = function supports(support_name) {
93 return this.options[support_name.toUpperCase()];
94 };
95 this.supportsTag = function supportsTag(tag_name) {
96 if (!this.cap.isEnabled('message-tags')) {
97 return false;
98 }
99 if (!this.options.CLIENTTAGDENY || this.options.CLIENTTAGDENY.length === 0) {
100 return true;
101 }
102 var allowAll = this.options.CLIENTTAGDENY[0] !== '*';
103 if (allowAll) {
104 return !this.options.CLIENTTAGDENY.some(function (tag) {
105 return tag === tag_name;
106 });
107 }
108 return this.options.CLIENTTAGDENY.some(function (tag) {
109 return tag === "-".concat(tag_name);
110 });
111 };
112 this.isChannelName = function isChannelName(channel_name) {
113 if (typeof channel_name !== 'string' || channel_name === '') {
114 return false;
115 }
116 var chanPrefixes = this.supports('CHANTYPES') || '&#';
117 return chanPrefixes.indexOf(channel_name[0]) > -1;
118 };
119
120 // Support '@#channel' and '++channel' formats
121 this.extractTargetGroup = function extractTargetGroup(target) {
122 var statusMsg = this.supports('STATUSMSG');
123 if (!statusMsg) {
124 return null;
125 }
126 var target_group = _.find(statusMsg, function (prefix) {
127 if (prefix === target[0]) {
128 target = target.substring(1);
129 return prefix;
130 }
131 });
132 if (!target_group) {
133 return null;
134 }
135 return {
136 target: target,
137 target_group: target_group
138 };
139 };
140}
\No newline at end of file