forked from tera-toolbox/command
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
270 lines (215 loc) · 6.07 KB
/
Copy pathindex.js
File metadata and controls
270 lines (215 loc) · 6.07 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
'use strict'
const PRIVATE_CHANNEL_INDEX = 7,
PRIVATE_CHANNEL_ID = -2 >>> 0,
PRIVATE_CHANNEL_NAME = 'Proxy',
PUBLIC_ENABLE = true,
PUBLIC_MATCH = /^!([^!].*)$/,
LOGIN_MESSAGE = true,
CLI_MODE = true,
readline = require('readline'),
log = require('log')('Command'),
rl = readline.createInterface({ input: process.stdin, output: process.stdout })
class Command {
constructor(mod) {
this.mod = mod
this.loginHook = false
this.hooks = {}
mod.hook('S_LOGIN', 'raw', () => { this.loginHook = true })
mod.hook(mod.patchVersion < 90 ? 'S_LOAD_CLIENT_USER_SETTING' : 'S_REPLY_CLIENT_CHAT_OPTION_SETTING', 'raw', () => {
if(!this.loginHook) return
process.nextTick(() => {
mod.send('S_JOIN_PRIVATE_CHANNEL', 1, {
index: PRIVATE_CHANNEL_INDEX,
id: PRIVATE_CHANNEL_ID,
unk: [],
name: PRIVATE_CHANNEL_NAME
})
if(LOGIN_MESSAGE) this.message(`TERA Proxy enabled. Client version: ${mod.patchVersion} r${mod.protocolVersion}`)
})
this.loginHook = false
})
mod.hook('S_JOIN_PRIVATE_CHANNEL', 2, event => event.index === PRIVATE_CHANNEL_INDEX ? false : undefined)
mod.hook('C_LEAVE_PRIVATE_CHANNEL', 1, event => event.index === PRIVATE_CHANNEL_INDEX ? false : undefined)
if(mod.patchVersion >= 28)
mod.hook('C_REQUEST_PRIVATE_CHANNEL_INFO', 2, event => {
if(event.channelId === PRIVATE_CHANNEL_ID) {
mod.send('S_REQUEST_PRIVATE_CHANNEL_INFO', 2, {
owner: 1,
password: 0,
members: [],
friends: []
})
return false
}
})
let lastError = '',
hookOverride = (name, version, cb) => {
mod.hook(name, version, {order: -10}, cb)
// Let other modules handle possible commands before we silence them
mod.hook(name, version, {order: 10, filter: {silenced: null}}, event => {
if(lastError) {
if(!event.$silenced) this.message(lastError)
lastError = ''
return false
}
})
},
handleCommand = message => {
try {
var args = parseArgs(stripOuterHTML(message))
}
catch(e) {
lastError = 'Syntax error: ' + e.message
return
}
try {
if(!this.exec(args)) {
lastError = `Unknown command "${args[0]}".`
return
}
}
catch(e) {
this.message('Error running callback for command "' + args[0] + '".')
console.error(e)
}
lastError = ''
return false
}
// /! commands
hookOverride('C_OP_COMMAND', 1, event => handleCommand(event.command))
hookOverride('C_CHAT', 1, event => {
// Proxy channel
if(event.channel === 11 + PRIVATE_CHANNEL_INDEX) return handleCommand(event.message)
// Any channel with regex match
if(PUBLIC_ENABLE) {
const match = PUBLIC_MATCH.exec(stripOuterHTML(event.message))
if(match) return handleCommand(match[1])
}
})
// Whispers with regex match
if(PUBLIC_ENABLE)
hookOverride('C_WHISPER', 1, event => {
const match = PUBLIC_MATCH.exec(stripOuterHTML(event.message))
if(match) return handleCommand(match[1])
})
rl.on('line', (cmd) => {
if(CLI_MODE)handleCommand(cmd)
})
}
exec(str) {
const args = Array.isArray(str) ? str : parseArgs(str)
if(args.length === 0) return false
const cb = this.hooks[args[0].toLowerCase()]
if(cb) {
cb.call(...args)
return true
}
return false
}
add(cmd, cb, ctx) {
if(typeof cb === 'function') {
if(ctx !== undefined) cb = cb.bind(ctx)
}
else if(typeof cb === 'object') cb = makeSubCommandHandler(cb, ctx)
else throw new Error('Callback must be a function or object')
if(Array.isArray(cmd)) {
for(let c of cmd) this.add(c, cb)
return
}
if(typeof cmd !== 'string') throw new Error('Command must be a string or array of strings')
if(cmd === '') throw new Error('Command must not be an empty string')
if(this.hooks[cmd = cmd.toLowerCase()]) throw new Error('Command already registered:', cmd)
this.hooks[cmd] = cb
}
remove(cmd) {
if(Array.isArray(cmd)) {
for(let c of cmd) this.remove(c)
return
}
if(typeof cmd !== 'string') throw new Error('Command must be a string or array of strings')
if(cmd === '') throw new Error('Command must not be an empty string')
delete this.hooks[cmd.toLowerCase()]
}
message(msg) {
this.mod.send('S_PRIVATE_CHAT', 1, {
channel: PRIVATE_CHANNEL_ID,
authorID: 0,
authorName: '',
message: msg
})
if(CLI_MODE)log.info(msg)
}
}
function makeSubCommandHandler(_obj, ctx) {
const obj = {}
for(let cmd in _obj) {
const cb = _obj[cmd]
cmd = cmd.toLowerCase()
if(typeof cb === 'function') obj[cmd] = ctx !== undefined ? cb.bind(ctx) : cb
else if(typeof cb === 'object') obj[cmd] = makeSubCommandHandler(cb, ctx)
else throw new Error('Sub-command callback must be a function or object')
}
return function subCommandHandler(cmd) {
let cb = cmd !== undefined ? obj[cmd.toLowerCase()] : obj.$none
if(cb) cb.call(...arguments)
else if(cb = obj.$default) cb.call(cmd, ...arguments)
}
}
function stripOuterHTML(str) {
return str.replace(/^<[^>]+>|<\/[^>]+><[^\/][^>]*>|<\/[^>]+>$/g, '')
}
function parseArgs(str) {
const parseHTML = /.*?<\/.*?>/g,
args = []
let arg = '',
quote = ''
for(let i = 0, c = ''; i < str.length; i++) {
c = str[i]
switch(c) {
case '<':
parseHTML.lastIndex = i + 1
let len = parseHTML.exec(str)
if(!len) throw new Error('HTML parsing failure')
len = len[0].length
arg += str.substr(i, len + 1)
i += len
break
case '\\':
c = str[++i]
if(c === undefined) throw new Error('Unexpected end of line')
arg += c
break
case '\'':
case '"':
if(arg === '' && quote === '') {
quote = c
break
}
if(quote === c) {
quote = ''
break
}
arg += c
break
case ' ':
if(quote === '') {
if(arg !== '') {
args.push(arg)
arg = ''
}
break
}
default:
arg += c
}
}
if(arg !== '') {
if(quote !== '') throw new Error('Expected ' + quote)
args.push(arg)
}
return args
}
module.exports = function Require(mod) {
if(mod.name !== 'command') throw SyntaxError(`Cannot require('command')\nUse "const {command} = mod.require" instead.`)
return new Command(mod)
}