-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
441 lines (378 loc) · 8.72 KB
/
index.js
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* Module dependencies.
*/
import { EventEmitter as Emitter } from 'events'
import debugModule from 'debug'
import Parser from 'slate-irc-parser'
import replies from 'irc-replies'
const debug = debugModule('slate-irc')
/**
* Core plugins.
*/
import away from './lib/plugins/away'
import disconnect from './lib/plugins/disconnect'
import errors from './lib/plugins/errors'
import invite from './lib/plugins/invite'
import join from './lib/plugins/join'
import kick from './lib/plugins/kick'
import mode from './lib/plugins/mode'
import motd from './lib/plugins/motd'
import names from './lib/plugins/names'
import nick from './lib/plugins/nick'
import notice from './lib/plugins/notice'
import part from './lib/plugins/part'
import pong from './lib/plugins/pong'
import privmsg from './lib/plugins/privmsg'
import quit from './lib/plugins/quit'
import topic from './lib/plugins/topic'
import welcome from './lib/plugins/welcome'
import whois from './lib/plugins/whois'
/**
* Initialize a new IRC client with the
* given duplex `stream`.
*
* @param {Stream} stream
* @param {Parser} [parser]
* @param {String} [encoding]
* @api public
*/
export default function Client(stream, parser, encoding) {
if (!(this instanceof Client)) return new Client(stream, parser, encoding)
stream.setEncoding(encoding || 'utf8')
this.stream = stream
this.parser = parser || new Parser()
this.parser.on('message', this.onmessage.bind(this))
stream.pipe(this.parser)
this.setMaxListeners(100)
this.use(away())
this.use(disconnect())
this.use(errors())
this.use(invite())
this.use(join())
this.use(kick())
this.use(mode())
this.use(motd())
this.use(names())
this.use(nick())
this.use(notice())
this.use(part())
this.use(pong())
this.use(privmsg())
this.use(quit())
this.use(topic())
this.use(welcome())
this.use(whois())
}
/**
* Inherit from `Emitter.prototype`.
*/
Client.prototype.__proto__ = Emitter.prototype
/**
* Write `str` without checking for '\r' or '\n' and invoke `fn(err)`.
*
* @param {String} str
* @param {Function} [fn]
* @api public
*/
Client.prototype.writeUnsafe = function (str, fn) {
this.stream.write(str + '\r\n', fn)
}
/**
* Write `str` and invoke `fn(err)`.
*
* @param {String} str
* @param {Function} [fn]
* @api public
*/
Client.prototype.write = function (str, fn) {
if (str.indexOf('\n') != -1 || str.indexOf('\r') != -1) {
fn &&
fn(
new Error(
"The parameter to write() must not contain any '\\n' or '\\r'.",
),
)
return
}
this.writeUnsafe(str, fn)
}
/**
* PASS <pass>
*
* @param {String} pass
* @param {Function} [fn]
* @api public
*/
Client.prototype.pass = function (pass, fn) {
this.write('PASS ' + pass, fn)
}
/**
* WEBIRC <password> <username> <hostname> <ip>
* See https://www.irc.wiki/WebIRC
*
* @param {String} password
* @param {String} username
* @param {String} hostname
* @param {String} ip
* @param {Function} [fn]
* @api public
*/
Client.prototype.webirc = function (password, username, hostname, ip, fn) {
var message = [password, username, hostname, ip].join(' ')
this.write('WEBIRC ' + message, fn)
}
/**
* NICK <nick>
*
* @param {String} nick
* @param {Function} [fn]
* @api public
*/
Client.prototype.nick = function (nick, fn) {
this.write('NICK ' + nick, fn)
}
/**
* USER <username> <realname>
*
* @param {String} username
* @param {String} realname
* @param {Function} [fn]
* @api public
*/
Client.prototype.user = function (username, realname, fn) {
this.write('USER ' + username + ' 0 * :' + realname, fn)
}
/**
* Send an invite to `name`, for a `channel`.
*
* @param {String} name
* @param {String} channel
* @param {Function} [fn]
* @api public
*/
Client.prototype.invite = function (name, channel, fn) {
this.write('INVITE ' + name + ' ' + channel, fn)
}
/**
* Send `msg` to `target`, where `target`
* is a channel or user name.
*
* @param {String|Array} target
* @param {String} msg
* @param {Function} [fn]
* @api public
*/
Client.prototype.send = function (target, msg, fn) {
this.write('PRIVMSG ' + toArray(target).join(',') + ' :' + msg, fn)
}
/**
* Send `msg` to `target` as an ACTION, where `target`
* is a channel or user name.
*
* An action is a PRIVMSG with a syntax
* like this:
*
* PRIVMSG <target> :\u0001ACTION <msg>\u0001
*
* @param {String} target
* @param {String} msg
* @param {Function} [fn]
* @api public
*/
Client.prototype.action = function (target, msg, fn) {
this.send(target, '\u0001' + 'ACTION ' + msg + '\u0001', fn)
}
/**
* Send `msg` to `target` as a NOTICE, where `target`
* is a channel or user name.
*
* @param {String} target
* @param {String} msg
* @param {Function} [fn]
* @api public
*/
Client.prototype.notice = function (target, msg, fn) {
this.write('NOTICE ' + target + ' :' + msg, fn)
}
/**
* Send `msg` to `target` as a CTCP notice, where `target`
* is a user name.
*
* @param {String} target
* @param {String} msg
* @param {Function} [fn]
* @api public
*/
Client.prototype.ctcp = function (target, msg, fn) {
this.notice(target, '\x01' + msg + '\x01', fn)
}
/**
* Join channel(s).
*
* @param {String|Array} channels
* @param {String|Array|Function} [keys or fn]
* @param {Function} [fn]
* @api public
*/
Client.prototype.join = function (channels, keys, fn) {
if ('function' == typeof keys) {
fn = keys
keys = ''
}
this.write(
'JOIN ' + toArray(channels).join(',') + ' ' + toArray(keys).join(','),
fn,
)
}
/**
* Leave channel(s) with optional `msg`.
*
* @param {String|Array} channels
* @param {String|Function} [msg or fn]
* @param {Function} [fn]
* @api public
*/
Client.prototype.part = function (channels, msg, fn) {
if ('function' == typeof msg) {
fn = msg
msg = ''
}
var part = 'PART ' + toArray(channels).join(',')
if (msg) {
part += ' :' + msg
}
this.write(part, fn)
}
/**
* Set the user's away message
*
* @param {String} [msg = 'Talk to you later!']
* @param {Function} [fn]
* @api public
*/
Client.prototype.away = function (msg, fn) {
msg = msg || 'Talk to you later!'
this.write('AWAY :' + msg, fn)
}
/**
* Remove user's away message
*
* @param {Function} [fn]
* @api public
*/
Client.prototype.back = function (fn) {
this.write('AWAY', fn)
}
/**
* Get channel topic or set the topic to `topic`.
*
* @param {String} channel
* @param {String|Function} [topic or fn]
* @param {Function} [fn]
* @api public
*/
Client.prototype.topic = function (channel, topic, fn) {
if ('function' == typeof topic) {
fn = topic
topic = ''
}
if (topic) {
topic = ' :' + topic
}
this.write('TOPIC ' + channel + topic, fn)
}
/**
* Kick nick(s) from channel(s) with optional `msg`.
*
* @param {String|Array} channels
* @param {String|Array} nicks
* @param {String|Function} [msg or fn]
* @param {Function} [fn]
* @api public
*/
Client.prototype.kick = function (channels, nicks, msg, fn) {
if ('function' == typeof msg) {
fn = msg
msg = ''
}
var kick =
'KICK ' + toArray(channels).join(',') + ' ' + toArray(nicks).join(',')
if (msg) {
kick += ' :' + msg
}
this.write(kick, fn)
}
/**
* Disconnect from the server with `msg`.
*
* @param {String} [msg]
* @param {Function} [fn]
* @api public
*/
Client.prototype.quit = function (msg, fn) {
msg = msg || 'Bye!'
this.write('QUIT :' + msg, fn)
}
/**
* Used to obtain operator privileges.
* The combination of `name` and `password` are required
* to gain Operator privileges. Upon success, a `'mode'`
* event will be emitted.
*
* @param {String} name
* @param {String} password
* @param {Function} [fn]
* @api public
*/
Client.prototype.oper = function (name, password, fn) {
this.write('OPER ' + name + ' ' + password, fn)
}
/**
* Used to set a user's mode or channel's mode for a user;
*
* @param {String} [nick or channel]
* @param {String} flags
* @param {String} params [nick - if setting channel mode]
* @param {Function} [fn]
* @api public
*/
Client.prototype.mode = function (target, flags, params, fn) {
if ('function' === typeof params) {
fn = params
params = ''
}
if (params) {
this.write('MODE ' + target + ' ' + flags + ' ' + params, fn)
} else {
this.write('MODE ' + target + ' ' + flags, fn)
}
}
/**
* Use the given plugin `fn`.
*
* @param {Function} fn
* @return {Client} self
* @api public
*/
Client.prototype.use = function (fn) {
fn(this)
return this
}
/**
* Handle messages.
*
* Emit "message" (msg).
*
* @api private
*/
Client.prototype.onmessage = function (msg) {
msg.command = replies[msg.command] || msg.command
debug('message %s %s', msg.command, msg.string)
this.emit('data', msg)
}
/**
* Array helper.
*/
function toArray(val) {
return Array.isArray(val) ? val : [val]
}