1 | 'use strict';
|
2 |
|
3 | require("core-js/modules/es.symbol.js");
|
4 | require("core-js/modules/es.symbol.description.js");
|
5 | require("core-js/modules/es.symbol.iterator.js");
|
6 | require("core-js/modules/es.array.iterator.js");
|
7 | require("core-js/modules/es.object.define-property.js");
|
8 | require("core-js/modules/es.string.iterator.js");
|
9 | require("core-js/modules/web.dom-collections.iterator.js");
|
10 | require("core-js/modules/es.symbol.to-primitive.js");
|
11 | require("core-js/modules/es.array.filter.js");
|
12 | require("core-js/modules/es.array.find.js");
|
13 | require("core-js/modules/es.array.join.js");
|
14 | require("core-js/modules/es.date.to-primitive.js");
|
15 | require("core-js/modules/es.date.to-string.js");
|
16 | require("core-js/modules/es.function.bind.js");
|
17 | require("core-js/modules/es.function.name.js");
|
18 | require("core-js/modules/es.number.constructor.js");
|
19 | require("core-js/modules/es.object.to-string.js");
|
20 | require("core-js/modules/es.regexp.to-string.js");
|
21 | function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
22 | function _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); }
|
23 | function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
24 | function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
25 | function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
26 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
27 | function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
28 | var _ = {
|
29 | partial: require('lodash/partial'),
|
30 | filter: require('lodash/filter'),
|
31 | find: require('lodash/find'),
|
32 | each: require('lodash/each'),
|
33 | pull: require('lodash/pull'),
|
34 | extend: require('lodash/extend')
|
35 | };
|
36 | var DuplexStream = require('stream').Duplex;
|
37 | module.exports = function () {
|
38 | function IrcChannel(irc_client, channel_name, key) {
|
39 | var _this = this;
|
40 | _classCallCheck(this, IrcChannel);
|
41 | this.irc_client = irc_client;
|
42 | this.name = channel_name;
|
43 |
|
44 |
|
45 |
|
46 | this.say = _.partial(irc_client.say.bind(irc_client), channel_name);
|
47 | this.notice = _.partial(irc_client.notice.bind(irc_client), channel_name);
|
48 |
|
49 | this.part = _.partial(irc_client.part.bind(irc_client), channel_name);
|
50 | this.join = _.partial(irc_client.join.bind(irc_client), channel_name);
|
51 | this.mode = _.partial(irc_client.mode.bind(irc_client), channel_name);
|
52 | this.banlist = _.partial(irc_client.banlist.bind(irc_client), channel_name);
|
53 | this.ban = _.partial(irc_client.ban.bind(irc_client), channel_name);
|
54 | this.unban = _.partial(irc_client.unban.bind(irc_client), channel_name);
|
55 | this.users = [];
|
56 | irc_client.on('userlist', function (event) {
|
57 | if (irc_client.caseCompare(event.channel, _this.name)) {
|
58 | _this.users = event.users;
|
59 | }
|
60 | });
|
61 | irc_client.on('join', function (event) {
|
62 | if (irc_client.caseCompare(event.channel, _this.name)) {
|
63 | _this.users.push(event);
|
64 | }
|
65 | });
|
66 | irc_client.on('part', function (event) {
|
67 | if (irc_client.caseCompare(event.channel, _this.name)) {
|
68 | _this.users = _.filter(_this.users, function (o) {
|
69 | return !irc_client.caseCompare(event.nick, o.nick);
|
70 | });
|
71 | }
|
72 | });
|
73 | irc_client.on('kick', function (event) {
|
74 | if (irc_client.caseCompare(event.channel, _this.name)) {
|
75 | _this.users = _.filter(_this.users, function (o) {
|
76 | return !irc_client.caseCompare(event.kicked, o.nick);
|
77 | });
|
78 | }
|
79 | });
|
80 | irc_client.on('quit', function (event) {
|
81 | _this.users = _.filter(_this.users, function (o) {
|
82 | return !irc_client.caseCompare(event.nick, o.nick);
|
83 | });
|
84 | });
|
85 | irc_client.on('nick', function (event) {
|
86 | _.find(_this.users, function (o) {
|
87 | if (irc_client.caseCompare(event.nick, o.nick)) {
|
88 | o.nick = event.new_nick;
|
89 | return true;
|
90 | }
|
91 | });
|
92 | });
|
93 | irc_client.on('mode', function (event) {
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | if (!irc_client.caseCompare(event.target, _this.name)) {
|
104 | return;
|
105 | }
|
106 |
|
107 |
|
108 | _.each(event.modes, function (mode) {
|
109 |
|
110 |
|
111 | var user_prefix = _.find(irc_client.network.options.PREFIX, {
|
112 | mode: mode.mode[1]
|
113 | });
|
114 | if (!user_prefix) {
|
115 |
|
116 | } else {
|
117 |
|
118 |
|
119 | var user = _.find(_this.users, function (u) {
|
120 | return irc_client.caseCompare(u.nick, mode.param);
|
121 | });
|
122 | if (!user) {
|
123 | return;
|
124 | }
|
125 | if (mode.mode[0] === '+') {
|
126 | user.modes = user.modes || [];
|
127 | user.modes.push(mode.mode[1]);
|
128 | } else {
|
129 | _.pull(user.modes, mode.mode[1]);
|
130 | }
|
131 | }
|
132 | });
|
133 | });
|
134 | this.join(key);
|
135 | }
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | return _createClass(IrcChannel, [{
|
147 | key: "relay",
|
148 | value: function relay(target_chan, opts) {
|
149 | opts = _.extend({
|
150 | one_way: false,
|
151 | replay_nicks: true
|
152 | }, opts);
|
153 | if (typeof target_chan === 'string') {
|
154 | target_chan = this.irc_client.channel(target_chan);
|
155 | }
|
156 | var this_stream = this.stream(opts);
|
157 | var other_stream = target_chan.stream(opts);
|
158 | this_stream.pipe(other_stream);
|
159 | if (!opts.one_way) {
|
160 | other_stream.pipe(this_stream);
|
161 | }
|
162 | }
|
163 | }, {
|
164 | key: "stream",
|
165 | value: function stream(stream_opts) {
|
166 | var _this2 = this;
|
167 | var read_queue = [];
|
168 | var is_reading = false;
|
169 | var stream = new DuplexStream({
|
170 | objectMode: true,
|
171 | write: function write(chunk, encoding, next) {
|
172 |
|
173 | if (_typeof(chunk) === 'object' && typeof chunk.message === 'string') {
|
174 | if (stream_opts.replay_nicks) {
|
175 | chunk = '<' + chunk.nick + '> ' + chunk.message;
|
176 | } else {
|
177 | chunk = chunk.message;
|
178 | }
|
179 | }
|
180 | _this2.say(chunk.toString());
|
181 | next();
|
182 | },
|
183 | read: function read() {
|
184 | is_reading = true;
|
185 | while (read_queue.length > 0) {
|
186 | var message = read_queue.shift();
|
187 | if (stream.push(message) === false) {
|
188 | is_reading = false;
|
189 | break;
|
190 | }
|
191 | }
|
192 | }
|
193 | });
|
194 | this.irc_client.on('privmsg', function (event) {
|
195 | if (_this2.irc_client.caseCompare(event.target, _this2.name)) {
|
196 | read_queue.push(event);
|
197 | if (is_reading) {
|
198 | stream._read();
|
199 | }
|
200 | }
|
201 | });
|
202 | return stream;
|
203 | }
|
204 | }, {
|
205 | key: "updateUsers",
|
206 | value: function updateUsers(cb) {
|
207 | var _this3 = this;
|
208 | var _updateUserList = function updateUserList(event) {
|
209 | if (_this3.irc_client.caseCompare(event.channel, _this3.name)) {
|
210 | _this3.irc_client.removeListener('userlist', _updateUserList);
|
211 | if (typeof cb === 'function') {
|
212 | cb(_this3);
|
213 | }
|
214 | }
|
215 | };
|
216 | this.irc_client.on('userlist', _updateUserList);
|
217 | this.irc_client.raw('NAMES', this.name);
|
218 | }
|
219 | }]);
|
220 | }(); |
\ | No newline at end of file |