-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatEncoding.txt
369 lines (291 loc) · 11.3 KB
/
ChatEncoding.txt
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
Script("Name") = "Chat Encoding"
Script("Author") = "Pyro"
Script("Major") = 1
Script("Minor") = 0
Script("Description") = "Allows sending and receiving messages in various encoded formats."
Const EVENT_PROCESSOR_SCRIPT = "Chat Event Pre-Processor"
Const ENCODE_COMMAND = "encode"
Const DECODE_COMMAND = "decode"
Const ENCODE_CHAT_COMMAND = "cencode"
Const ENCODE_LIST_COMMAND = "encodings"
Public AvailableEncodings
Private TempUTF8
Sub Event_Load()
If Scripts(EVENT_PROCESSOR_SCRIPT) Is Nothing Then
AddChat Color.Red, "[ENC] Error: The script '" & EVENT_PROCESSOR_SCRIPT & "' could not be found."
Exit Sub
End If
Set AvailableEncodings = CreateObject("Scripting.Dictionary")
With AvailableEncodings
.CompareMode = vbTextCompare
.Add "HEX", New HexEncoding
.Add "ROT13", New Rot13Encoding
.Add "ROT47", New Rot47Encoding
.Add "INVIG", New InvigorationEncoding
.Add "BASE64", New Base64Encoding
End With
Call ObserveScript(EVENT_PROCESSOR_SCRIPT)
Call InitCommands
End Sub
Sub Event_Command(Command)
If Command.Name = ENCODE_LIST_COMMAND Then
list = vbNullString
For Each Encoding In AvailableEncodings.Items()
list = list & Encoding.ShortID & ", "
Next
list = Left(list, Len(list) - 2)
Command.Respond "Available encodings: " & list
Else
Encoding = Command.Argument("encoding")
Message = Command.Argument("text")
If Not AvailableEncodings.Exists(Encoding) Then
Command.Respond StringFormat("Unrecognized encoding: {0}.", Encoding)
Exit Sub
End If
Set oEncoding = AvailableEncodings.Item(Encoding)
Select Case Command.Name
Case ENCODE_COMMAND
Command.Respond StringFormat("{0} encoded: {1}", oEncoding.ShortID, oEncoding.Encode(Message))
Case DECODE_COMMAND
Command.Respond StringFormat("{0} decoded: {1}", oEncoding.ShortID, oEncoding.Decode(Message))
Case ENCODE_CHAT_COMMAND
Call SendInternal(oEncoding, Message)
End Select
End If
End Sub
Sub Event_Close()
Set AvailableEncodings = Nothing
End Sub
Public Sub Event_MessageSent(MessageID, Message, Tag)
If Left(Tag, 8) = "Encoded " Then
Config.UseUTF8 = TempUTF8
Encoding = Split(Tag, " ")(1)
If AvailableEncodings.Exists(Encoding) Then
Set oEncoding = AvailableEncodings.Item(Encoding)
DecodedMessage = oEncoding.Decode(Mid(Message, 2))
If Len(DecodedMessage) > 0 Then
AddChat Color.Red, "[ENC-" & oEncoding.ShortID & "] ", Color.Carats, "<", Color.TalkBotUsername, Channel.Self.Name, Color.Carats, "> ", Color.TalkNormalText, DecodedMessage
End If
End If
End If
End Sub
Public Sub Event_ChatEventReceived(EventID, Flags, Ping, IP, Account, RegAuth, Username, Message)
If EventID <> &H5 Then Exit Sub
If Len(Message) < 2 Then Exit Sub
IsEncoded = False
For Each Encoding In AvailableEncodings.Items()
If Left(Message, 1) = Encoding.MessageIndicator Then
DecodedMessage = Encoding.Decode(Mid(Message, 2))
If Len(DecodedMessage) > 0 Then
VetoThisMessage
AddChat Color.Red, "[ENC-" & Encoding.ShortID & "] ", Color.Carats, "<", Color.TalkUsernameNormal, Username, Color.Carats, "> ", Color.TalkNormalText, DecodedMessage
End If
End If
Next
End Sub
Public Sub Send(ID, Message)
If Not AvailableEncodings.Exists(ID) Then Exit Sub
Set Encoding = AvailableEncodings(ID)
Call SendInternal(Encoding, Message)
End Sub
Private Sub SendInternal(Encoding, Message)
If Encoding Is Nothing Then Exit Sub
TempUTF8 = Config.UseUTF8
Config.UseUTF8 = False
AddQ Encoding.MessageIndicator & Encoding.Encode(Message), -1, "Encoded " & Encoding.ShortID
End Sub
Public Sub TestEncoding(ID, Message)
If Not AvailableEncodings.Exists(ID) Then Exit Sub
Set Encoding = AvailableEncodings(ID)
AddChat Color.Red, "[ENC-" & Encoding.ShortID & "] ", Color.Orange, "Input: " & Message
EncodedMessage = Encoding.Encode(Message)
AddChat Color.Red, "[ENC-" & Encoding.ShortID & "] ", Color.Orange, "Encoded: " & EncodedMessage
Output = Encoding.Decode(EncodedMessage)
AddChat Color.Red, "[ENC-" & Encoding.ShortID & "] ", Color.Orange, "Output: " & Output
IsMatch = CBool(Message = Output)
AddChat Color.Red, "[ENC-" & Encoding.ShortID & "] ", IIf(IsMatch, Color.Green, Color.Yellow), "Success: " & CStr(IsMatch)
End Sub
Function PadLeft(str, c, i)
PadLeft = Right(String(i, c) & str, i)
End Function
Sub InitCommands()
Call InitEncodingCommand(ENCODE_COMMAND, "Encodes the specified text.", 20, "G")
Call InitEncodingCommand(DECODE_COMMAND, "Decodes the specified text.", 20, "G")
Call InitEncodingCommand(ENCODE_CHAT_COMMAND, "Sends a message using the specified encoding.", 60, "T")
If OpenCommand(ENCODE_LIST_COMMAND) Is Nothing Then
Set cmd = CreateCommand(ENCODE_LIST_COMMAND)
With cmd
.Description = "Lists the available chat encodings."
.RequiredRank = 20
.RequiredFlags = "G"
.Save
End With
End If
End Sub
Sub InitEncodingCommand(CommandName, Description, Rank, Flags)
If OpenCommand(CommandName) Is Nothing Then
Set cmd = CreateCommand(CommandName)
With cmd
Set param = .NewParameter("encoding", False, "word")
param.Description = "The encoding to use."
.Parameters.Add param
Set param = .NewParameter("text", False, "string")
param.Description = "The text to be encoded or decoded."
.Parameters.Add param
.Description = Description
.RequiredRank = Rank
.RequiredFlags = Flags
.Save
End With
End If
End Sub
Class HexEncoding
Public ShortID
Public MessageIndicator
Public Sub Class_Initialize()
ShortID = "HEX"
MessageIndicator = Chr(&HA3)
End Sub
Public Function Encode(Text)
Encode = vbNullString
For i = 1 To Len(Text)
Encode = Encode & LCase(PadLeft(CStr(Hex(Asc(Mid(Text, i, 1)))), "0", 2))
Next
End Function
Public Function Decode(Text)
Text = Replace(Text, Space(1), vbNullString)
Decode = vbNullString
For i = 1 To Len(Text) Step 2
Decode = Decode & Chr("&H" & Mid(Text, i, 2))
Next
End Function
End Class
Class Rot13Encoding
Public ShortID
Public MessageIndicator
Public Sub Class_Initialize()
ShortID = "ROT13"
MessageIndicator = Chr(&HA5)
End Sub
Public Function Encode(Text)
Encode = vbNullString
For i = 1 To Len(Text)
val = Asc(Mid(Text, i, 1))
If ((val > 64 And val < 78) Or (val > 96 And val < 110)) Then Encode = Encode & Chr(val + 13)
If ((val > 77 And val < 90) Or (val > 108 And val < 123)) Then Encode = Encode & Chr(val - 13)
Next
End Function
Public Function Decode(Text)
Decode = Encode(Text)
End Function
End Class
Class Rot47Encoding
Public ShortID
Public MessageIndicator
Public Sub Class_Initialize()
ShortID = "ROT47"
MessageIndicator = Chr(&HA6)
End Sub
Public Function Encode(Text)
Encode = vbNullString
For i = 1 To Len(Text)
val = Asc(Mid(Text, i, 1))
If val >= 33 And k <= 126 Then
Encode = Encode & Chr(33 + ((val + 14) Mod 94))
Else
Encode = Encode & Chr(val)
End If
Next
End Function
Public Function Decode(Text)
Decode = Encode(Text)
End Function
End Class
Class InvigorationEncoding
Public ShortID
Public MessageIndicator
Public Sub Class_Initialize()
ShortID = "INVIG"
MessageIndicator = Chr(&H95)
End Sub
Public Function Encode(Text)
Combine = vbNullString
For i = 1 To Len(Text)
Combine = Combine & PadLeft(CStr(Asc(Mid(Text, i, 1))), "0", 3)
Next
Encode = vbNullString
For i = 1 To Len(Combine)
Encode = Encode & Chr(Asc(Mid(Combine, i, 1)) + 101)
Next
End Function
Public Function Decode(Text)
Combine = vbNullString
For i = 1 To Len(Text)
Combine = Combine & Chr(Asc(Mid(Text, i, 1)) - 101)
Next
Decode = vbNullString
For i = 1 To Len(Combine) Step 3
Decode = Decode & Chr(Mid(Combine, i, 3))
Next
End Function
End Class
Class Base64Encoding
Public ShortID
Public MessageIndicator
Private B64
Public Sub Class_Initialize()
ShortID = "BASE64"
MessageIndicator = Chr(&HE6)
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
End Sub
Public Function Encode(Text)
For i = 1 To Len(Text) Step 3
group = &H10000 * Asc(Mid(Text, i, 1)) + &H100 * SafeAsc(Mid(Text, i + 1, 1)) + SafeAsc(Mid(Text, i + 2, 1))
group = Oct(group)
group = String(8 - Len(group), "0") & group
out = Mid(B64, CLng("&o" & Mid(group, 1, 2)) + 1, 1) + _
Mid(B64, CLng("&o" & Mid(group, 3, 2)) + 1, 1) + _
Mid(B64, CLng("&o" & Mid(group, 5, 2)) + 1, 1) + _
Mid(B64, CLng("&o" & Mid(group, 7, 2)) + 1, 1)
Encode = Encode & out
Next
Select Case Len(Text) Mod 3
Case 1: Encode = Left(Encode, Len(Encode) - 2) + "=="
Case 2: Encode = Left(Encode, Len(Encode) - 1) + "="
End Select
End Function
Public Function Decode(Text)
Text = Replace(Text, " ", "")
If length Mod 4 <> 0 Then
AddChat Color.Red, "[ENC-" & ShortID & "] Invalid base64 string: ", Color.Orange, Text
Exit Function
End If
For i = 1 To Len(Text) Step 4
numBytes = 3
group = 0
For cnt = 0 To 3
c = Mid(Text, i + cnt, 1)
If c = "=" Then
numBytes = numBytes - 1
d = 0
Else
d = InStr(1, B64, c, vbBinaryCompare) - 1
End If
If d = -1 Then
AddChat Color.Red, "[ENC-" & ShortID & "] Invalid character in base64 string: ", Color.Orange, c
Exit Function
End If
group = 64 * group + d
Next
group = Hex(group)
group = String(6 - Len(group), "0") & group
out = Chr(CByte("&H" & Mid(group, 1, 2))) + _
Chr(CByte("&H" & Mid(group, 3, 2))) + _
Chr(CByte("&H" & Mid(group, 5, 2)))
Decode = Decode & Left(out, numBytes)
Next
End Function
Private Function SafeAsc(c)
If c = "" Then SafeAsc = 0 Else SafeAsc = Asc(c)
End Function
End Class