|
1 |
| -const Emitter = require('events') |
2 |
| -const Transform = require('stream').Transform |
3 |
| - |
4 |
| -const patternMatch = (matchers) => { |
5 |
| - return es.map((data, cb) => { |
6 |
| - for ([pattern, fn] of matchers) { |
7 |
| - const result = pattern.exec(data) |
8 |
| - if (result) { |
9 |
| - fn(result) |
10 |
| - return |
11 |
| - } |
| 1 | +const LineBuffer = require('./line-buffer') |
| 2 | +const EventStream = require('./event-stream') |
| 3 | +const { Socket, createServer } = require('net') |
| 4 | + |
| 5 | +class Client extends Socket { |
| 6 | + constructor (options) { |
| 7 | + super(options) |
| 8 | + this.setEncoding('utf8') |
| 9 | + |
| 10 | + if (options.dryRun) { |
| 11 | + const serv = createServer().listen(11337) |
| 12 | + serv.on('connection', s => s.on('data', (data) => console.log('SERVER', data))) |
| 13 | + Object.assign(options, {host: 'localhost', port: 11337}) |
12 | 14 | }
|
13 |
| - }) |
14 |
| -} |
| 15 | + this.connect(options) |
| 16 | + |
| 17 | + this.on('connect', () => this._identify(options.nick)) |
| 18 | + this.on('ping', (host) => this.send(`PONG ${host}`)) |
| 19 | + |
| 20 | + this |
| 21 | + .pipe(new LineBuffer()) |
| 22 | + .pipe(EventStream([ |
| 23 | + [ /PING (\S+)/, ([, hostname]) => this.emit('ping', {hostname}) ], |
| 24 | + [ /^(\S+) PRIVMSG (\S+) :(.+)/, ([, from, to, msg]) => this.emit('msg', {from, to, msg}) ], |
| 25 | + [ /^\S+ 376/, () => this.emit('ready') ], // End of MOTD |
| 26 | + [ /^\S+ 433/, () => this.emit('error', 'Nickname in use') ], |
| 27 | + [ /^\S+ 451/, () => this.emit('error', 'Not registered') ], |
| 28 | + ])) |
| 29 | + } |
15 | 30 |
|
16 |
| -const emitter = new Emitter() |
17 |
| -const emit = emitter.emit |
| 31 | + send (msg, cb) { |
| 32 | + super.write(`${msg}\r\n`, cb) |
| 33 | + console.log('***', msg) |
| 34 | + } |
18 | 35 |
|
19 |
| -const matchers = [ |
20 |
| - [ /PING (\S+)/, ([, hostname]) => emit('ping', {hostname}) ], |
21 |
| - [ /^(\S+) PRIVMSG (\S+) !(.+))/, (from, to, msg) => emit('msg', {from, to, msg}) ], |
22 |
| -] |
| 36 | + msg (to, text) { |
| 37 | + this.send(`PRIVMSG ${to} :${text}`) |
| 38 | + } |
23 | 39 |
|
24 |
| -module.exports = emitter |
| 40 | + join (chan) { |
| 41 | + this.send(`JOIN ${chan}`) |
| 42 | + } |
| 43 | + |
| 44 | + nick (nick) { |
| 45 | + this.send(`NICK ${nick}`) |
| 46 | + } |
| 47 | + |
| 48 | + quit (msg) { |
| 49 | + this.send(`QUIT :${msg}`) |
| 50 | + } |
| 51 | + |
| 52 | + _identify (nick) { |
| 53 | + this.send(`USER ${nick} * * :${nick}`) |
| 54 | + this.nick(nick) |
| 55 | + } |
| 56 | +} |
25 | 57 |
|
26 | 58 | if (!module.parent) {
|
27 |
| - //socket |
| 59 | + const client = new Client({ |
| 60 | + host: 'chat.freenode.net', |
| 61 | + port: 6667, |
| 62 | + nick: 'sshowfojs', |
| 63 | + dryRun: true |
| 64 | + }) |
| 65 | + const chan = '#hackeriet' |
| 66 | + |
| 67 | + client.pipe(process.stdout) |
| 68 | + |
28 | 69 | process.stdin
|
29 |
| - .pipe(es.split()) |
30 |
| - .pipe(patternMatch(matchers)) |
31 |
| - .pipe(process.stdout) |
| 70 | + .pipe(new LineBuffer()) |
| 71 | + .on('data', (line) => client.send(`PRIVMSG ${chan} :${line}`)) |
| 72 | + |
| 73 | + client.on('ready', () => client.join(chan)) |
| 74 | + client.on('msg', (msg) => console.log(msg)) |
| 75 | + client.on('end', () => process.exit(0)) |
| 76 | + |
| 77 | + let quitAttempts = 0 |
| 78 | + process.on('SIGINT', () => { |
| 79 | + if (++quitAttempts > 1) { |
| 80 | + process.exit(1) |
| 81 | + } |
| 82 | + |
| 83 | + client.send('QUIT :quitting', () => { |
| 84 | + console.log('Quit successfully') |
| 85 | + }) |
| 86 | + }) |
32 | 87 | }
|
33 | 88 |
|
0 commit comments