-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIrcAdapter.mjs
374 lines (304 loc) · 11.1 KB
/
IrcAdapter.mjs
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { TextMessage, EnterMessage, LeaveMessage, Response, Adapter } from 'hubot'
class IrcResponse extends Response {
async sendPrivate(...strings) {
return await this.robot.adapter.sendPrivate(this.envelope, ...strings)
}
}
class IrcAdapter extends Adapter {
#bot = null
#client = null
constructor(robot, client) {
super(robot)
this.#client = client
}
async send(envelope, ...strings) {
// Use @notice if SEND_NOTICE_MODE is set
let target
if (this.robot.config.isSendNoticeModeOn) {
return this.notice(envelope, strings)
}
target = this.#getTargetFromEnvelope(envelope)
if (!target) {
return this.robot.logger.error("ERROR: Not sure who to send to. envelope=", envelope)
}
const result = []
console.log('target', target)
for (let str of strings) {
this.robot.logger.debug(`${target} ${str}`)
result.push(this.#client.say(target, str))
}
return result
}
async sendPrivate(envelope, ...strings) {
// Remove the room from the envelope and send as private message to user
if (envelope.room) {
delete envelope.room
}
if (envelope.user != null ? envelope.user.room : undefined) {
delete envelope.user.room
}
return await this.send(envelope, ...strings)
}
async topic(envelope, ...strings) {
const data = strings.join(" / ")
const channel = envelope.room
return this.#client.send('TOPIC', channel, data)
}
async emote(envelope, ...strings) {
// Use @notice if SEND_NOTICE_MODE is set
if (process.env.HUBOT_IRC_SEND_NOTICE_MODE != null) {
return this.notice(envelope, strings)
}
const target = this.#getTargetFromEnvelope(envelope)
if (!target) {
return this.robot.logger.error("ERROR: Not sure who to emote to. envelope=", envelope)
}
return strings.map((str) => this.#client.action(target, str))
}
notice(envelope, ...strings) {
let str
const target = this.#getTargetFromEnvelope(envelope)
if (!target) {
return this.robot.logger.warn("Notice: no target found", envelope)
}
// Flatten out strings from send
let flattened = []
for (str of strings) {
if (typeof str !== 'undefined') {
for (let line of Array.from(str.toString().split(/\r?\n/))) {
if (Array.isArray(line)) {
flattened = flattened.concat(line)
} else {
flattened.push(line)
}
}
}
}
const result = []
for (str of Array.from(flattened)) {
if ((str == null)) {
continue
}
result.push(this.#client.notice(target, str))
}
return result
}
async reply(envelope, ...strings) {
const tasks = strings.map((str) => this.send(envelope.user, `${envelope.user.name}: ${str}`))
return await Promise.all(tasks)
}
join(channel) {
return this.#client.join(channel, async () => {
this.robot.logger.info('joined %s', channel)
const selfUser = this.getUserFromName(this.robot.name)
return await this.receive(new EnterMessage(selfUser))
})
}
part(channel) {
return this.#client.part(channel, async () => {
this.robot.logger.info('left %s', channel)
const selfUser = this.getUserFromName(this.robot.name)
return await this.receive(new LeaveMessage(selfUser))
})
}
getUserFromName(name) {
if ((this.robot.brain != null ? this.robot.brain.userForName : undefined) != null) { return this.robot.brain.userForName(name) }
// Deprecated in 3.0.0
return this.userForName(name)
}
getUserFromId(id) {
// TODO: Add logic to convert object if name matches
if ((this.robot.brain != null ? this.robot.brain.userForId : undefined) != null) { return this.robot.brain.userForId(id) }
// Deprecated in 3.0.0
return this.userForId(id)
}
createUser(channel, from) {
const user = this.getUserFromId(from)
user.name = from
if (channel.match(/^[&#\!]/)) {
user.room = channel
} else {
user.room = null
}
return user
}
kick(channel, client, message) {
return this.#client.emit('raw', {
command: 'KICK',
nick: process.env.HUBOT_IRC_NICK,
args: [ channel, client, message ]
})
}
command(command, ...strings) {
return this.#client.send(command, ...strings)
}
checkCanStart() {
if (!this.robot.config.nick) {
throw new Error("HUBOT_IRC_NICK is not defined try: export HUBOT_IRC_NICK='mybot'")
} else if (!this.robot.config.rooms) {
throw new Error("HUBOT_IRC_ROOMS is not defined try: export HUBOT_IRC_ROOMS='#myroom'")
} else if (!this.robot.config.server) {
throw new Error("HUBOT_IRC_SERVER is not defined: try: export HUBOT_IRC_SERVER='irc.myserver.com'")
}
}
static unfloodProtection(unflood) {
return (unflood === 'true') || !isNaN(parseInt(unflood))
}
static unfloodProtectionDelay(unflood) {
const unfloodProtection = IrcAdapter.unfloodProtection(unflood)
const unfloodValue = parseInt(unflood) || 1000
if (unfloodProtection) {
return unfloodValue
} else {
return 0
}
}
run() {
this.checkCanStart()
if (this.robot.config.nickpass != null) {
let identify_args = ""
if (this.robot.config.nickusername != null) {
identify_args += `${this.robot.config.nickusername} `
}
identify_args += `${this.robot.config.nickpass}`
this.#client.addListener('notice', (from, to, text) => {
if ((from === 'NickServ') && (text.toLowerCase().indexOf('identify') !== -1)) {
return this.#client.say('NickServ', `identify ${identify_args}`)
} else if (this.robot.config.nickpass && (from === 'NickServ') &&
((text.indexOf('Password accepted') !== -1) ||
(text.indexOf('identified') !== -1))) {
return Array.from(this.robot.config.rooms).map((room) =>
this.join(room))
}
})
}
if (this.robot.config.connectCommand != null) {
this.#client.addListener('registered', (message) => {
// The 'registered' event is fired when you are connected to the server
const strings = this.robot.config.connectCommand.split(" ")
return this.command(strings.shift(), ...strings)
})
}
this.#client.addListener('names', (channel, nicks) => {
const result = []
for (let nick in nicks) {
result.push(this.createUser(channel, nick))
}
return result
})
this.#client.addListener('notice', async (from, to, message) => {
if (!from) { return }
if (this.robot.config.ignoreUsers.includes(from)) {
this.robot.logger.info('Ignoring user: %s', from)
// we'll ignore this message if it's from someone we want to ignore
return
}
this.robot.logger.info(`NOTICE from ${from} to ${to}: ${message}`)
const user = this.createUser(to, from)
return await this.receive(new TextMessage(user, message))
})
this.#client.addListener('message', async (from, to, message) => {
if (!from) { return }
if (this.robot.config.nick.toLowerCase() === to.toLowerCase()) {
// this is a private message, let the 'pm' listener handle it
return
}
if (this.robot.config.ignoreUsers.includes(from)) {
this.robot.logger.info('Ignoring user: %s', from)
// we'll ignore this message if it's from someone we want to ignore
return
}
this.robot.logger.debug(`From ${from} to ${to}: ${message}`)
const user = this.createUser(to, from)
if (user.room) {
this.robot.logger.debug(`${to} <${from}> ${message}`)
} else {
if (message.indexOf(to) !== 0) {
message = `${to}: ${message}`
}
this.robot.logger.debug(`msg <${from}> ${message}`)
}
return await this.receive(new TextMessage(user, message))
})
this.#client.addListener('action', async (from, to, message) => {
this.robot.logger.debug(` * From ${from} to ${to}: ${message}`)
if (this.robot.config.ignoreUsers.includes(from)) {
this.robot.logger.info('Ignoring user: %s', from)
// we'll ignore this message if it's from someone we want to ignore
return
}
const user = this.createUser(to, from)
if (user.room) {
this.robot.logger.debug(`${to} * ${from} ${message}`)
} else {
this.robot.logger.debug(`msg <${from}> ${message}`)
}
return await this.receive(new TextMessage(user, message))
})
this.#client.addListener('error', message => this.robot.logger.error('ERROR: %s: %s', message.command, message.args.join(' ')))
this.#client.addListener('pm', async (nick, message) => {
this.robot.logger.info('Got private message from %s: %s', nick, message)
if (this.robot.config.isPrivate) {
return
}
if (this.robot.config.ignoreUsers.includes(nick)) {
this.robot.logger.info('Ignoring user: %s', nick)
// we'll ignore this message if it's from someone we want to ignore
return
}
const nameLength = this.robot.config.nick.length
if (message.slice(0, nameLength).toLowerCase() !== this.robot.config.nick.toLowerCase()) {
message = `${this.robot.config.nick} ${message}`
}
return await this.receive(new TextMessage({reply_to: nick, name: nick}, message))
})
this.#client.addListener('join', async (channel, who) => {
this.robot.logger.info('%s has joined %s', who, channel)
const user = this.createUser(channel, who)
user.room = channel
return await this.receive(new EnterMessage(user))
})
this.#client.addListener('part', async (channel, who, reason) => {
this.robot.logger.info('%s has left %s: %s', who, channel, reason)
const user = this.createUser('', who)
user.room = channel
const msg = new LeaveMessage(user)
msg.text = reason
return await this.receive(msg)
})
this.#client.addListener('quit', async (who, reason, channels) => {
this.robot.logger.info('%s has quit: %s (%s)', who, channels, reason)
const result = []
for (let ch of Array.from(channels)) {
const user = this.createUser('', who)
user.room = ch
const msg = new LeaveMessage(user)
msg.text = reason
result.push(this.receive(msg))
}
return await Promise.all(result)
})
this.#client.addListener('kick', (channel, who, _by, reason) => this.robot.logger.info('%s was kicked from %s by %s: %s', who, channel, _by, reason))
this.#client.addListener('invite', (channel, from) => {
this.robot.logger.info('%s invited you to join %s', from, channel)
if (this.robot.config.ignoreUsers.includes(from)) {
this.robot.logger.info('Ignoring user: %s', from)
// we'll ignore this message if it's from someone we want to ignore
return
}
if (!this.robot.config.isPrivate || !this.robot.config.shouldIgnoreInvite) {
return this.#client.join(channel)
}
})
return this.emit("connected")
}
#getTargetFromEnvelope(envelope) {
let target = envelope?.room ?? envelope.user?.room
return target
}
}
export {
IrcAdapter,
IrcResponse
}
export default IrcAdapter