UNPKG

3.61 kBJavaScriptView Raw
1'use strict';
2
3require("core-js/modules/es.array.filter.js");
4require("core-js/modules/es.array.index-of.js");
5require("core-js/modules/es.array.map.js");
6require("core-js/modules/es.array.slice.js");
7require("core-js/modules/es.array.splice.js");
8require("core-js/modules/es.object.to-string.js");
9var _ = {
10 map: require('lodash/map')
11};
12var Helper = {
13 parseMask: parseMask,
14 parseWhoFlags: parseWhoFlags,
15 splitOnce: splitOnce
16};
17module.exports = Helper;
18function parseMask(mask) {
19 var nick = '';
20 var user = '';
21 var host = '';
22 var sep1 = mask.indexOf('!');
23 var sep2 = mask.indexOf('@');
24 if (sep1 === -1 && sep2 === -1) {
25 // something
26 if (mask.indexOf('.') > -1) {
27 host = mask;
28 } else {
29 nick = mask;
30 }
31 } else if (sep1 === -1 && sep2 !== -1) {
32 // something@something
33 nick = mask.substring(0, sep2);
34 host = mask.substring(sep2 + 1);
35 } else if (sep1 !== -1 && sep2 === -1) {
36 // something!something
37 nick = mask.substring(0, sep1);
38 user = mask.substring(sep1 + 1);
39 } else {
40 // something!something@something
41 nick = mask.substring(0, sep1);
42 user = mask.substring(sep1 + 1, sep2);
43 host = mask.substring(sep2 + 1);
44 }
45 return {
46 nick: nick,
47 user: user,
48 host: host
49 };
50}
51function parseWhoFlags(flagsParam, networkOptions) {
52 // https://modern.ircdocs.horse/#rplwhoreply-352
53 // unrealircd https://github.com/unrealircd/unrealircd/blob/8536778/doc/conf/help/help.conf#L429
54
55 var unparsedFlags = flagsParam.split('');
56
57 // the flags object to be returned
58 var parsedFlags = {};
59
60 // function to check for flags existence and remove it if existing
61 var hasThenRemove = function hasThenRemove(flag) {
62 var flagIdx = unparsedFlags.indexOf(flag);
63 if (flagIdx > -1) {
64 unparsedFlags.splice(flagIdx, 1);
65 return true;
66 }
67 return false;
68 };
69
70 // away is represented by H = Here, G = Gone
71 parsedFlags.away = !hasThenRemove('H');
72 parsedFlags.away = hasThenRemove('G');
73
74 // add bot mode if its flag is supported by the ircd
75 var bot_mode_token = networkOptions.BOT;
76 if (bot_mode_token) {
77 parsedFlags.bot = hasThenRemove(bot_mode_token);
78 }
79
80 // common extended flags
81 parsedFlags.registered = hasThenRemove('r');
82 parsedFlags.operator = hasThenRemove('*');
83 parsedFlags.secure = hasThenRemove('s');
84
85 // filter PREFIX array against the prefix's in who reply returning matched PREFIX objects
86 var chan_prefixes = networkOptions.PREFIX.filter(function (f) {
87 return hasThenRemove(f.symbol);
88 });
89 // use _.map to return an array of mode strings from matched PREFIX objects
90 parsedFlags.channel_modes = _.map(chan_prefixes, 'mode');
91 return {
92 parsedFlags: parsedFlags,
93 unparsedFlags: unparsedFlags
94 };
95}
96function splitOnce(input, separator) {
97 if (typeof input !== 'string' || typeof separator !== 'string') {
98 throw new TypeError('input and separator must be strings');
99 }
100 var splitPos;
101 if (separator === '') {
102 // special handling required for empty string as separator
103
104 // cannot match '' at start, so start searching after first character
105 splitPos = input.indexOf(separator, 1);
106 if (splitPos === input.length) {
107 // cannot match '' at end, so if that's all we found, act like we found nothing
108 splitPos = -1;
109 }
110 } else {
111 // normal non-zero-length separator
112 splitPos = input.indexOf(separator);
113 }
114
115 // no separator found
116 if (splitPos < 0) {
117 return [input];
118 }
119
120 // the normal case: split around first instance of separator
121 return [input.slice(0, splitPos), input.slice(splitPos + separator.length)];
122}
\No newline at end of file