UNPKG

8.71 kBJavaScriptView Raw
1'use strict';
2
3require("core-js/modules/es.symbol.js");
4require("core-js/modules/es.symbol.description.js");
5require("core-js/modules/es.symbol.iterator.js");
6require("core-js/modules/es.array.iterator.js");
7require("core-js/modules/es.object.define-property.js");
8require("core-js/modules/es.string.iterator.js");
9require("core-js/modules/web.dom-collections.iterator.js");
10require("core-js/modules/es.symbol.to-primitive.js");
11require("core-js/modules/es.array.filter.js");
12require("core-js/modules/es.array.find.js");
13require("core-js/modules/es.array.join.js");
14require("core-js/modules/es.date.to-primitive.js");
15require("core-js/modules/es.date.to-string.js");
16require("core-js/modules/es.function.bind.js");
17require("core-js/modules/es.function.name.js");
18require("core-js/modules/es.number.constructor.js");
19require("core-js/modules/es.object.to-string.js");
20require("core-js/modules/es.regexp.to-string.js");
21function _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); }
22function _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); }
23function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
24function _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); } }
25function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
26function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
27function _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); }
28var _ = {
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};
36var DuplexStream = require('stream').Duplex;
37module.exports = /*#__PURE__*/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 // TODO: Proxy channel related events from irc_bot to this instance
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 // this.action = _.partial(irc_client.action.bind(irc_client), channel_name);
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 /* event will be something like:
95 {
96 target: '#prawnsalad',
97 nick: 'ChanServ',
98 modes: [ { mode: '+o', param: 'prawnsalad' } ],
99 time: undefined
100 }
101 */
102
103 if (!irc_client.caseCompare(event.target, _this.name)) {
104 return;
105 }
106
107 // There can be multiple modes set at once, loop through
108 _.each(event.modes, function (mode) {
109 // If this mode has a user prefix then we need to update the user object
110 // eg. +o +h +v
111 var user_prefix = _.find(irc_client.network.options.PREFIX, {
112 mode: mode.mode[1]
113 });
114 if (!user_prefix) {
115 // TODO : manage channel mode changes
116 } else {
117 // It's a user mode
118 // Find the user affected
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 * Relay messages between this channel to another
139 * @param {IrcChannel|String} target_chan Target channel
140 * @param {Object} opts Extra options
141 *
142 * opts may contain the following properties:
143 * one_way (false) Only relay messages to target_chan, not the reverse
144 * replay_nicks (true) Include the sending nick as part of the relayed message
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 // Support piping from one irc buffer to another
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