-
Notifications
You must be signed in to change notification settings - Fork 1
/
poorchat.js
executable file
·115 lines (102 loc) · 3.52 KB
/
poorchat.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
const WebSocket = require('ws')
const ReconnectingWebSocket= require('reconnecting-websocket')
const EventEmitter = require('events')
const parse = require('irc-message').parse
class Poorchat extends EventEmitter {
constructor(options) {
super()
this.ws = new ReconnectingWebSocket(options.websocket, ['base64'], {
WebSocket: WebSocket
})
this.login = options.login
this.password = options.password
this.cap = options.cap
this.channel = options.channel
this.debug = options.debug
this.options = options
this.PONGInterval = null
}
messageEncode(data) {
return Buffer.from(`${data}\r\n`).toString('base64')
}
messageDecode(data) {
return Buffer.from(data, 'base64').toString('utf-8')
}
sendMessage(message) {
const encodedMessage = this.messageEncode(message)
this.ws.send(encodedMessage)
}
readMessage(data) {
const decodedMessage = this.messageDecode(data)
return parse(decodedMessage)
}
say(data) {
const encodedMessage = this.messageEncode(`PRIVMSG ${this.channel} :${data}`)
this.ws.send(encodedMessage)
}
pm(channel, data) {
const encodedMessage = this.messageEncode(`PRIVMSG ${channel} :${data}`)
this.ws.send(encodedMessage)
}
connect() {
return new Promise((resolve) => {
console.log('Connecting to IRC...')
this.ws.addEventListener('open', () => {
this.sendMessage(`NICK ${this.login}`)
this.sendMessage(`USER ${this.login} ${this.options.irc} Poorchat ${this.login}`)
for (const cap of this.cap) {
this.sendMessage(cap)
}
this.sendMessage('CAP END')
})
this.ws.addEventListener('message', ({ data }) => {
const message = this.readMessage(data)
if (message.command === '422') {
this.sendMessage(`PRIVMSG Poorchat :LOGIN ${this.login} ${this.password}`)
this.sendMessage(`JOIN ${this.channel}`)
}
if (message.command === 'JOIN' && message.prefix.split('!')[0] === this.login) {
clearInterval(this.PONGInterval)
setInterval(() => {
this.sendMessage(`PONG irc.poorchat.net`)
}, 30000)
console.log('IRC Conntected!')
resolve()
}
this.messageHandler(message)
if (this.debug) {
console.log(message)
}
})
})
}
messageHandler(message) {
switch (message.command) {
case 'PING':
this.sendMessage(`PONG ${message.params[0]}`)
break
case 'PRIVMSG':
this.emit('message', message)
break
case 'JOIN':
this.emit('join', message)
break
case 'PART':
this.emit('part', message)
break
case 'EMBED':
this.emit('message', message)
break
case 'NOTICE':
this.emit('message', message)
break
case 'MODE':
this.emit('mode', message)
default:
if (this.debug) {
console.log(message.raw)
}
}
}
}
module.exports = Poorchat