-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathChannel.lua
479 lines (398 loc) · 15.5 KB
/
Channel.lua
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
--[[-----------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2010-2020 Mark Rogaski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]-----------------------------------------------------------------------
--[[-----------------------------------------------------------------------
Imported Libraries
--]]-----------------------------------------------------------------------
local crc = LibStub:GetLibrary("Hash:CRC:16ccitt-1.0")
local base64 = LibStub:GetLibrary("Encoding:Hash:Base64BCA-1.0")
--[[-----------------------------------------------------------------------
Class Variables
--]]-----------------------------------------------------------------------
GwChannel = {}
GwChannel.__index = GwChannel
--- GwChannel constructor function.
-- @return An initialized GwChannel instance.
function GwChannel:new()
local self = {}
setmetatable(self, GwChannel)
return self:initialize()
end
function GwChannel:initialize()
self.frame_table = {}
self.cversion = 0
self.name = ''
self.password = ''
self.number = 0
self.stale = true
self.fdelay = GwHoldDown:new(GW_CHANNEL_FAILURE_HOLD, GW_CHANNEL_FAILURE_HOLD_MAX)
self.tx_queue = {}
self.tx_hash = {}
self.rx_queue = {}
self.stats = {
txcnt = 0,
rxcnt = 0,
sconn = 0,
fconn = 0,
leave = 0,
disco = 0,
}
return self
end
--[[-----------------------------------------------------------------------
Channel Management Methods
--]]-----------------------------------------------------------------------
--- Configure the channel.
-- @param cversion Configuration version.
-- @param name Name of the channel.
-- @param password Password for the channel. (optional)
function GwChannel:configure(cversion, name, password)
assert(cversion == 1)
assert(name and name ~= '')
self.cversion = cversion
self.name = name
self.password = password and password or ''
self.stale = false
gw.Debug(GW_LOG_INFO, 'configured channel; channel=%s, password=%s, cversion=%d, stale=%s',
gw.Redact(self.name), gw.Redact(self.password), self.cversion, tostring(self.stale));
end
--- Clear the channel configuration.
function GwChannel:clear()
gw.Debug(GW_LOG_INFO, 'clearing channel; number=%d, channel=%s', self.number, gw.Redact(self.name))
self:leave()
self:initialize()
end
--- Mark the channel as stale.
function GwChannel:age()
self.stale = true
gw.Debug(GW_LOG_DEBUG, 'marked stale; number=%d, channel=%s', self.number, gw.Redact(self.name))
end
--- Test if the channel is configured.
-- @return True if configured, false otherwise.
function GwChannel:is_configured()
gw.Debug(GW_LOG_DEBUG, 'number=%d, name=%s, password=%s, cversion=%d, stale=%s',
self.number, gw.Redact(self.name), gw.Redact(self.password), self.cversion, tostring(self.stale))
return self.name and self.name ~= ''
end
--- Check if a connection exists to the custom channel.
-- @return True if connected, otherwise false.
function GwChannel:is_connected()
gw.Debug(GW_LOG_DEBUG, 'checking number=%d, name=%s', self.number, gw.Redact(self.name))
if self:is_configured() then
self.number = GetChannelName(self.name)
gw.Debug(GW_LOG_DEBUG, 'confirmed number=%d, name=%s', self.number, gw.Redact(self.name))
if self.number == 0 then
return false
else
return true
end
else
return false
end
end
--- Check if channel configuration is stale.
-- @return True if stale, false otherwise.
function GwChannel:is_stale()
gw.Debug(GW_LOG_DEBUG, 'number=%d, name=%s, password=%s, cversion=%d, stale=%s',
self.number, gw.Redact(self.name), gw.Redact(self.password), self.cversion, tostring(self.stale))
return self.stale
end
--- Join a bridge channel.
-- @return True if connection success, false otherwise.
function GwChannel:join()
if not self:is_configured() then
-- Only join if we have the channel details
return false
elseif self.fdelay:hold() then
-- Hold down in effect.
return false
elseif self:is_connected() then
-- Already connected
return true
else
gw.Debug(GW_LOG_INFO, 'joining channel; channel=%s, password=%s',
gw.Redact(self.name), gw.Redact(self.password))
JoinTemporaryChannel(self.name, self.password)
local number = GetChannelName(self.name)
if number == 0 then
gw.Error('cannot create communication channel: %s', gw.Redact(self.name))
self.stats.fconn = self.stats.fconn + 1
self.fdelay:continue()
return false
else
self.number = number
self.stats.sconn = self.stats.sconn + 1
self.fdelay:clear()
gw.Debug(GW_LOG_NOTICE, 'joined channel; number=%d, name=%s, password=%s',
self.number, gw.Redact(self.name), gw.Redact(self.password))
gw.Write('Connected to confederation on channel %d.', self.number)
--
-- Hide the channel
--
for i = 1, 10 do
GwChannel.frame_table = { GetChatWindowMessages(i) }
for j, v in ipairs(GwChannel.frame_table) do
if v == self.name then
local frame = format('ChatFrame%d', i)
if _G[frame] then
gw.Debug(GW_LOG_INFO, 'hiding channel: number=%d, name=%s, frame=%s',
self.number, gw.Redact(self.name), frame)
ChatFrame_RemoveChannel(frame, self.name)
end
end
end
end
return true
end
end
end
--- Leave a bridge channel.
-- @return True if a disconnection occurred, false otherwise.
function GwChannel:leave()
if self:is_connected() then
gw.Debug(GW_LOG_INFO, 'leaving channel; number=%d, channel=%s, password=%s',
self.number, gw.Redact(self.name), gw.Redact(self.password))
LeaveChannelByName(self.name)
self.stats.leave = self.stats.leave + 1
self.number = 0
return true
else
return false
end
end
--[[-----------------------------------------------------------------------
Informational Methods
--]]-----------------------------------------------------------------------
--- Dump the channel status.
-- @param label An identifier for the channel.
function GwChannel:dump_status(label)
label = label or 'channel'
gw.Write('%s: connected=%s, number=%d, channel=%s, password=%s, stale=%s (sconn=%d, fconn=%d, leave=%d, disco=%d)',
label, tostring(self:is_connected()), self.number, gw.Redact(self.name),
gw.Redact(self.password), tostring(self:is_stale()),
self.stats.sconn, self.stats.fconn, self.stats.leave, self.stats.disco)
end
--[[-----------------------------------------------------------------------
Transmit Methods
--]]-----------------------------------------------------------------------
--- Sends an encoded message on the shared channel.
-- @param type The message type.
-- Accepted values are:
-- GW_MTYPE_CHAT
-- GW_MTYPE_BROADCAST
-- GW_MTYPE_NOTICE
-- GW_MTYPE_REQUEST
-- GW_MTYPE_ADDON
-- GW_MTYPE_EXTERNAL
-- @param ... Text of the message.
function GwChannel:send(type, ...)
-- Apply adaptation layer encoding
local message = self:al_encode(type, ...)
if not message:match('%S') then
gw.Debug(GW_LOG_WARNING, 'sending a blank message on channel %d', self.number)
end
gw.Debug(GW_LOG_NOTICE, 'channel=%d, type=%d, message=%q', self.number, type, message)
return self:tl_send(type, message)
end
function GwChannel:al_encode(type, ...)
local arg = {...}
if type == GW_MTYPE_BROADCAST then
assert(#arg >= 1)
assert(#arg <= 3)
return strjoin(':', arg[1], arg[2] or '', arg[3] or '')
elseif type == GW_MTYPE_EXTERNAL then
assert(#arg == 2)
return strjoin(':', arg[1], base64.encode(arg[2]))
else
assert(#arg == 1)
return arg[1]
end
end
function GwChannel:tl_send(type, message)
local opcode
if type == GW_MTYPE_CHAT then
opcode = 'C'
elseif type == GW_MTYPE_BROADCAST then
opcode = 'B'
elseif type == GW_MTYPE_NOTICE then
opcode = 'N'
elseif type == GW_MTYPE_REQUEST then
opcode = 'R'
elseif type == GW_MTYPE_ADDON then
opcode = 'M'
elseif type == GW_MTYPE_EXTERNAL then
opcode = 'E'
else
gw.Debug(GW_LOG_ERROR, 'unknown message type: %d', type)
return
end
-- Format the message segment
local segment = strsub(strjoin('#', opcode, gw.config.guild_id, '', message), 1, GW_MAX_MESSAGE_LENGTH)
-- Send the message
self:tl_enqueue(segment)
self:tl_flush()
end
--- Add a segment to the channel transmit queue.
-- @param segment Segment to enqueue.
-- @return Number of segments in queue after the insertion.
function GwChannel:tl_enqueue(segment)
tinsert(self.tx_queue, segment)
gw.Debug(GW_LOG_DEBUG, 'enqueued segment: %q', segment)
return #self.tx_queue
end
--- Remove a segment from the channel transmit queue.
-- @return Segment removed from the queue or nil if queue is empty.
function GwChannel:tl_dequeue()
local segment = tremove(self.tx_queue, 1)
if segment then
gw.Debug(GW_LOG_DEBUG, 'dequeued segment: %q', segment)
end
return segment
end
--- Transmit all messages in the channel transmit queue.
-- @return Number of messages flushed.
function GwChannel:tl_flush()
gw.Debug(GW_LOG_DEBUG, 'servicing transmit queue; channel=%d, %d message(s) queued.', self.number, #self.tx_queue)
if self:is_connected() then
local count = 0
while true do
local segment = self:tl_dequeue()
if segment then
-- Record the segment hash
local hash = crc.Hash(segment)
if self.tx_hash[hash] == nil then
self.tx_hash[hash] = 1
else
self.tx_hash[hash] = self.tx_hash[hash] + 1
end
-- Send the segment
gw.Debug(GW_LOG_INFO, 'channel=%d, segment=%q', self.number, segment)
SendChatMessage(segment, 'CHANNEL', nil, self.number)
self.stats.txcnt = self.stats.txcnt + 1
count = count + 1
else
break
end
end
return count
else
gw.Debug(GW_LOG_WARNING, 'channel=%d, not connected.', self.number)
return 0
end
end
--[[-----------------------------------------------------------------------
Receive Methods
--]]-----------------------------------------------------------------------
--- Handler for data received on a channel.
-- @param f A callback function.
-- @param ... The API event arguments.
-- @return The return value of f applied to the data.
function GwChannel:receive(f, ...)
local sender, guild_id, mtype, message = self:tl_receive(...)
sender = gw.GlobalName(sender)
if message ~= nil then
local content = self:al_decode(mtype, message)
if mtype == GW_MTYPE_EXTERNAL then
-- API traffic is handled regardless of the sender.
if content ~= nil then
local addon, api_message = unpack(content)
if addon ~= nil and api_message ~= nil then
gw.APIDispatcher(addon, sender, guild_id, api_message)
end
end
elseif sender ~= gw.player and guild_id ~= gw.config.guild_id then
-- Process the chat message if it from another co-guild.
gw.Debug(GW_LOG_NOTICE, 'channel=%d, type=%d, sender=%s, guild=%s, message=%q',
self.number, mtype, sender, guild_id, message)
return f(mtype, guild_id, content, {...})
end
end
end
--- Adaptation layer decoding.
-- @param mtype message type
-- @param message message content
-- @return A table of message strings. Returns nil on error.
function GwChannel:al_decode(mtype, message)
local function expand(message, n)
assert(type(n) == 'number' and n > 0)
message = type(message) == 'string' and message or ''
local t = { strsplit(':', message, n) }
for i = 1, n do
t[i] = t[i] ~= nil and t[i] or ''
end
return t
end
gw.Debug(GW_LOG_DEBUG, 'type=%d, message=%q', mtype, message)
if mtype == GW_MTYPE_BROADCAST then
return expand(message, 3)
elseif mtype == GW_MTYPE_EXTERNAL then
local tag, data = unpack(expand(message, 2))
local rv, result = pcall(base64.decode, data)
if (rv) then
return { tag, result }
else
gw.Debug(GW_LOG_DEBUG, 'API error: %s', result)
return
end
else
return { message }
end
end
function GwChannel:tl_receive(...)
local segment, sender = select(1, ...)
sender = gw.GlobalName(sender)
gw.Debug(GW_LOG_INFO, 'channel=%d, sender=%s, segment=%q', self.number, sender, segment)
self.stats.rxcnt = self.stats.rxcnt + 1
-- Check the segment hash
if sender == gw.player then
local hash = crc.Hash(segment)
if self.tx_hash[hash] and self.tx_hash[hash] > 0 then
gw.Debug(GW_LOG_DEBUG, 'channel=%d, tx_hash[0x%04X] == %d', self.number, hash, self.tx_hash[hash])
self.tx_hash[hash] = self.tx_hash[hash] - 1
if self.tx_hash[hash] <= 0 then
self.tx_hash[hash] = nil
end
else
gw.Debug(GW_LOG_WARNING, 'channel=%d, tx_hash[0x%04X] not found', self.number, hash)
gw.Error('Message corruption detected. Please disable add-ons that might modify messages on channel %d.', self.number)
end
end
-- Process the segment
local opcode, guild_id, _, message = strsplit('#', segment, 4)
guild_id = guild_id or '-'
message = message or ''
gw.Debug(GW_LOG_DEBUG, 'opcode=%s, guild_id=%s, message=%q', opcode, guild_id, message)
local type = GW_MTYPE_NONE
if opcode == 'C' then
type = GW_MTYPE_CHAT
elseif opcode == 'B' then
type = GW_MTYPE_BROADCAST
elseif opcode == 'N' then
type = GW_MTYPE_NOTICE
elseif opcode == 'R' then
type = GW_MTYPE_REQUEST
elseif opcode == 'M' then
type = GW_MTYPE_ADDON
elseif opcode == 'E' then
type = GW_MTYPE_EXTERNAL
else
gw.Debug(GW_LOG_WARNING, 'unknown segment opcode: %s', opcode)
end
return sender, guild_id, type, message
end