This repository was archived by the owner on Nov 11, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
301 lines (267 loc) · 9.7 KB
/
Copy pathmain.js
File metadata and controls
301 lines (267 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
const irc = require('irc');
const Promise = require('bluebird'); // jshint ignore:line
const uuid = require('node-uuid');
class IRC extends global.AKP48.pluginTypes.ServerConnector {
constructor(AKP48) {
super(AKP48, 'irc-server');
}
load(persistentObjects) {
this._defaultCommandDelimiters = ['!', '.'];
this.TextDecorator = new (require('./IRCDecorator'))();
var self = this;
var config = this._config;
if(!config || !config.server || !config.nick) {
global.logger.error(`${self.name}|${self._id}: Required server and/or nick options missing from config!`);
this._error = true;
return;
}
if(persistentObjects) {
this._client = persistentObjects.client;
this._client.removeAllListeners('message');
this._client.removeAllListeners('registered');
this._client.removeAllListeners('invite');
this._client.removeAllListeners('kick');
this._client.removeAllListeners('error');
this._client.removeAllListeners('action');
this._client.removeAllListeners('join');
this._client.removeAllListeners('part');
this._client.removeAllListeners('nick');
this._connected = true;
} else {
this._client = new irc.Client(config.server, config.nick, {
autoRejoin: false,
autoConnect: false,
port: config.port || 6667,
userName: config.userName || 'AKP48',
realName: config.realName || 'AKP48',
channels: config.channels || [],
password: config.password || null
});
}
this.joinMsg = config.joinMsg || null;
this._client.on('nick', (oldNick, newNick) => {
global.logger.stupid(`${self.name}|${self._id}: Caught nick change event. "${oldNick}" => "${newNick}"`);
self._AKP48.emit('nick', oldNick, newNick, self);
});
this._client.on('message', function(nick, to, text, message) {
if(to === config.nick) { to = nick; }
self._AKP48.onMessage(self.createContextFromMessage(message, to));
});
this._client.on('action', function(nick, to, text, message) {
if(to === config.nick) { to = nick; }
var context = self.createContextFromMessage(message, to);
context.setCustomData('ircAction', true);
context.setCustomData('isEmote', true);
self._AKP48.onMessage(context);
});
this._client.on('registered', function() {
global.logger.verbose(`${self.name}|${self._id}: Connected to ${self._config.server}.`);
self._AKP48.emit('registeredOnServer', self._id, self);
self.checkNick();
});
this._client.on('join', function(chan, nick) {
if(nick === self._client.nick) { return; }
global.logger.stupid(`${self.name}|${self._id}: Caught join event on ${self._config.server}. ${nick} joined ${chan}.`);
self._AKP48.emit('ircJoin', chan, nick, self);
});
this._client.on('part', function(chan, nick, reason) {
if(nick === self._client.nick) { return; }
global.logger.stupid(`${self.name}|${self._id}: Caught part event on ${self._config.server}. ${nick} left ${chan} (${reason}).`);
self._AKP48.emit('ircPart', chan, nick, reason, self);
});
this._client.on('invite', function(channel, from) {
global.logger.debug(`${self.name}|${self._id}: Invite to channel "${channel}" received from ${from}. Joining channel.`);
self._client.join(channel, function() {
if(!self._config.silentJoin) {
self._client.say(channel, self.joinMsg);
}
var ctx = new self._AKP48.Context({
instance: self,
instanceType: 'irc',
nick: from,
text: self.joinMsg,
to: channel,
user: `GLOBAL`,
commandDelimiters: '',
myNick: self._client.nick,
permissions: []
});
self._AKP48.logMessage(ctx);
self._AKP48.saveConfig(self._config, self._id, true);
});
});
this._client.on('kick', function(channel, nick, by, reason) {
if(nick === self._client.nick) {
global.logger.debug(`${self.name}|${self._id}: Kicked from ${channel} by ${by} for "${reason}". Removing channel from config.`);
var index = self._config.channels.indexOf(channel);
while(index > -1) {
self._config.channels.splice(index, 1);
index = self._config.channels.indexOf(channel);
}
self._AKP48.saveConfig(self._config, self._id, true);
}
});
this._client.on('error', function(message) {
global.logger.error(`${self.name}|${self._id}: Error received from ${message.server}! ${message.command}: ${message.args}`);
});
this._AKP48.on('msg_'+this._id, function(context) {
var message = self.TextDecorator.parse(context.text());
if(!context.getCustomData('noPrefix')) {message = `${context.nick()}: ${message}`;}
try {
if(context.getCustomData('isEmote')) {
self._client.action(context.to(), context.text());
} else {
self._client.say(context.to(), message);
}
self._AKP48.logMessage(context);
} catch (e) {
global.logger.error(`${self.name}|${self._id}: Error sending message to channel '${context.to()}'! ${e.name}: ${e.message}`, e);
}
});
this._AKP48.on('alert', function(context) {
var message = self.TextDecorator.parse(context.text());
for (var i = 0; i < self._config.channels.length; i++) {
var chan = self._config.channels[i];
if(self._config.chanConfig && self._config.chanConfig[chan]) {
if(self._config.chanConfig[chan].alert) {
try {
self._client.say(chan, message);
self._AKP48.logMessage(context.cloneWith({instance: self, myNick: self._client.nick, to: chan}));
} catch (e) {
global.logger.error(`${self.name}|${self._id}: Error sending alert to channel '${chan}'! ${e.name}: ${e.message}`, e);
}
}
}
}
});
}
connect() {
if(this._error) {
global.logger.error(`${this.name}|${this._id}: Cannot connect. Check log for errors.`);
return;
}
if(this._connected) {
global.logger.debug(`${this.name}|${this._id}: Using previous connection.`);
this._connected = false;
} else {
this._client.connect();
}
this._AKP48.emit('serverConnect', this._id, this);
// set joinMsg after 2 seconds, to allow time for connection.
setTimeout(() => {
this.joinMsg = this.joinMsg || `Hello, everyone! I'm ${this._client.nick}! I respond to commands and generally try to be helpful. For more information, say ".help"!`;
}, 2000);
this.checkNick();
}
disconnect(msg) {
if(this._error) {
global.logger.error(`${this.name}|${this._id}: Cannot disconnect. Check log for errors.`);
return;
}
this._client.disconnect(msg || 'Goodbye.');
}
getDefaultConfig() {
return {
'_id': uuid.v4(),
'plugin': 'irc-server',
'config': {
'server': 'irc.esper.net',
'nick': 'MyAKP48Instance',
'channels': ['#AKP'],
'chanConfig': {
'#AKP': {
'commandDelimiters': ['.', '!']
}
}
}
};
}
checkNick() {
if(this._client.nick !== this._config.nick) {
//if nick isn't right, attempt to set it back every 10 seconds until we successfully set it.
this.setNickInterval = this.setNickInterval || setInterval(() => {
this._client.send('NICK', this._config.nick);
if(this._client.nick === this._config.nick) {
clearInterval(this.setNickInterval);
}
}, 10000);
}
}
}
IRC.prototype.createContextFromMessage = function (message, to) {
var perms = this.getPermissions(`${message.user}@${message.host}`, message.nick, to);
var delimit = this.getChannelConfig(to).commandDelimiters || this._config.commandDelimiters || this._defaultCommandDelimiters;
var ctx = new this._AKP48.Context({
instance: this,
instanceType: 'irc',
nick: message.nick,
text: message.args[1],
to: to,
user: `${message.user}@${message.host}`,
commandDelimiters: delimit,
myNick: this._client.nick,
permissions: perms,
rawMessage: message
});
return ctx;
};
IRC.prototype.getChannelConfig = function (channel) {
if(!this._config.chanConfig) {return {};}
return this._config.chanConfig[channel] || {};
};
IRC.prototype.getPermissions = function (prefix, nick, channel) {
var users = {};
var configPerms = {};
var globPerms = {};
var outputPerms = [];
if(nick !== channel) {
try { //surround in try...catch because sometimes weird things happen and the client's users object isn't there.
users = this._client.chans[channel.toLowerCase()].users;
} catch (e) {}
configPerms = this.getChannelConfig(channel).users;
globPerms = this.getChannelConfig('global').users;
} else {
configPerms = this.getChannelConfig('global').users;
}
if(users && users[nick]) {
switch(users[nick]) {
case '~':
outputPerms.push('irc.channel.owner');
break;
case '&':
outputPerms.push('irc.channel.protected');
break;
case '@':
outputPerms.push('irc.channel.op');
break;
case '%':
outputPerms.push('irc.channel.halfop');
break;
case '+':
outputPerms.push('irc.channel.voice');
break;
default:
break;
}
}
if(configPerms) {
if(configPerms[prefix]) {
outputPerms.push.apply(outputPerms, configPerms[prefix]);
}
}
if(globPerms) {
if(globPerms[prefix]) {
outputPerms.push.apply(outputPerms, globPerms[prefix]);
}
}
return outputPerms;
};
IRC.prototype.getPersistentObjects = function () {
let temp = {
client: this._client
};
this._client = null;
return temp;
};
module.exports = IRC;