-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwordBot.cs
339 lines (323 loc) · 13.5 KB
/
SwordBot.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace TwitchSwordBot {
/// <summary>
/// Core class for a twitch bot
/// </summary>
class SwordBot {
const string USER_FILE = "User.txt";
const string BANNED_WORDS_FILE = "BannedWords.txt";
internal string Pass { get; set; }
internal string User { get; set; }
const string IP = "irc.chat.twitch.tv";
const int PORT = 6697;
private TaskCompletionSource<int> connected = new TaskCompletionSource<int>();
private StreamWriter tcpWriter;
private StreamReader tcpReader;
public delegate void ChatEventHandler(object sender, ChatMessage e);
public event ChatEventHandler OnMessage = delegate { };
/// <summary>
/// If <c>true</c> writes all received messages to console
/// </summary>
public bool VerboseConsole { get; set; } = false;
private bool isAlive;
private HashSet<string> joinedChannels;
private Dictionary<string, string> bannedWords;
private int privmsgBucket;
private Stopwatch privmsgBucketTimer;
const int BUCKET_MAX = 100;
const int BUCKET_REFILL_RATE = 30000;
public SwordBot() {
privmsgBucket = BUCKET_MAX;
privmsgBucketTimer = new();
joinedChannels = new HashSet<string>();
bannedWords = new Dictionary<string, string>();
GetUserInfo();
GetBannedWords();
Console.WriteLine("User set to: " + User);
//Console.WriteLine(Pass);
}
/// <summary>
/// Starts the bot
/// </summary>
/// <returns></returns>
public async Task Start() {
//Init TCP connection
TcpClient tcpClient = new();
await tcpClient.ConnectAsync(IP, PORT);
SslStream sslStream = new(
tcpClient.GetStream(),
false,
ValidateServerCertificate,
null
);
await sslStream.AuthenticateAsClientAsync(IP);
tcpReader = new(sslStream);
tcpWriter = new(sslStream) {
NewLine = "\r\n",
AutoFlush = true,
};
//Authorise
await tcpWriter.WriteLineAsync($"PASS {Pass}");
await tcpWriter.WriteLineAsync($"NICK {User}");
//Login done
connected.SetResult(0);
await tcpWriter.WriteLineAsync($"CAP REQ :twitch.tv/tags");
await JoinChannel(User);
await SendMessage(User, "MrDestructoid Beep Boop MrDestructoid");
isAlive = true;
while (isAlive) {
await connected.Task;
string line = "";
line = await tcpReader.ReadLineAsync();
if (VerboseConsole) {
Console.WriteLine(line);
}
//Handle Tags
string[] split;
string tags = "";
string remLine = line;
if (string.IsNullOrEmpty(line)) {
await Task.Delay(20);
continue;
}
if (line.StartsWith("@")) {
string[] tagSplit = line.Split(" ");
tags = tagSplit[0];
split = tagSplit.Skip(1).ToArray();
remLine = HelperFunctions.GetStringRemainder(line, tags).TrimStart();
} else {
split = line.Split(" ");
}
//Handle Ping
if (remLine.StartsWith("PING")) {
Console.WriteLine(split[0]);
await tcpWriter.WriteLineAsync($"PONG {split[1]}");
//Handle Message
} else if (split.Length > 2 && split[1] == "PRIVMSG") {
//Parse Message
int exclamPos = split[0].IndexOf("!");
string username = split[0].Substring(1, exclamPos - 1);
int secondColonPos = remLine.IndexOf(':', 1);
string message = remLine.Substring(secondColonPos + 1);
string channel = split[2].TrimStart('#');
//Event Handler
OnMessage(this, new ChatMessage
{
Message = message,
Sender = username,
Channel = channel,
Tags = tags
});
} else if (split.Length > 2 && split[1] == "JOIN") {
string channel = split[2].TrimStart('#');
if (channel != User) {
Console.WriteLine($"Successfully joined channel: {channel}");
await SendMessage(User, $"Successfully joined channel: {channel}");
}
joinedChannels.Add(channel);
} else if (split.Length > 2 && split[1] == "PART") {
string channel = split[2].TrimStart('#');
Console.WriteLine($"Successfully left channel: {channel}");
await SendMessage(User, $"Successfully left channel: {channel}");
if (joinedChannels.Contains(channel)) {
joinedChannels.Remove(channel);
}
}
}
}
private bool GetUserInfo() {
string userPrefix = "user:";
string passPrefix = "pass:";
try {
string[] file = File.ReadAllLines(USER_FILE);
foreach (string line in file) {
if (line.StartsWith(userPrefix)) {
User = HelperFunctions.GetStringRemainder(line, userPrefix).Trim();
} else if (line.StartsWith(passPrefix)) {
Pass = HelperFunctions.GetStringRemainder(line, passPrefix).Trim();
}
}
return true;
} catch(IOException e) {
Console.WriteLine("Failed to read User file (User.txt):\n" + e.Message);
System.Environment.Exit(-1);
}
return false;
}
private void GetBannedWords() {
try {
if (File.Exists(BANNED_WORDS_FILE)) {
string[] lines = File.ReadAllLines(BANNED_WORDS_FILE);
foreach (string line in lines) {
string trim = line.Trim();
if (!bannedWords.ContainsKey(trim) && !string.IsNullOrWhiteSpace(trim)) {
bannedWords.Add(trim.ToLower(),
HelperFunctions.ConvertStringToPlain(trim.ToLower()).ToLower());
}
}
}
} catch (IOException e) {
Console.WriteLine("Failed to read Banned words file (BannedWords.txt):\n" + e.Message);
System.Environment.Exit(-1);
}
}
/// <summary>
/// Adds a new banned word/phrase
/// </summary>
/// <param name="banWord">Word/phrase to ban</param>
public void AddBannedWord(string banWord) {
if (!bannedWords.ContainsKey(banWord)) {
bannedWords.Add(banWord, HelperFunctions.ConvertStringToPlain(banWord));
}
WriteToBannedWords();
}
/// <summary>
/// Removes an existing banned word/phrase
/// </summary>
/// <param name="banWord">Word/phrase to remove</param>
public void RemoveBannedWord(string banWord) {
if (bannedWords.ContainsKey(banWord)) {
bannedWords.Remove(banWord);
}
WriteToBannedWords();
}
private void WriteToBannedWords() {
File.WriteAllLines(BANNED_WORDS_FILE, bannedWords.Keys);
}
/// <summary>
/// Checks a string for banned words/phrases using homoglyph parsing
/// </summary>
/// <param name="str">String to check for banned phrases</param>
/// <returns><c>true</c> if contains any banned phrases, <c>false</c> otherwise</returns>
public bool CheckStringForBannedWords(string str) {
string[] split = str.Split(" ");
for (int i=0; i<split.Length; i++) {
if (i+1 < split.Length) {
if (CheckIfBannedWord(split[i], split.Skip(i+1).ToArray(), bannedWords)) {
return true;
}
} else {
if (CheckIfBannedWord(split[i], new string[0], bannedWords)) {
return true;
}
}
}
return false;
}
private bool CheckIfBannedWord(string str, string[] full, Dictionary<string, string> search) {
string plain = HelperFunctions.ConvertStringToPlain(str);
string flip = HelperFunctions.FlipStringAndStrip(str);
foreach (KeyValuePair<string, string> ban in search) {
if (str.Equals(ban.Key) || plain.Equals(ban.Key) || flip.Equals(ban.Key) ||
str.Equals(ban.Value) || plain.Equals(ban.Value) || flip.Equals(ban.Value)) {
return true;
}
}
if (full.Length < 1) {
return false;
}
Dictionary<string, string> matches = new();
foreach (KeyValuePair<string, string> ban in search) {
if (ban.Key.Contains(str) || ban.Value.Contains(str))
{
matches.TryAdd(HelperFunctions.GetStringRemainder(ban.Key, str),
HelperFunctions.GetStringRemainder(ban.Value, str));
} else if (ban.Key.Contains(plain) || ban.Value.Contains(plain)) {
matches.TryAdd(HelperFunctions.GetStringRemainder(ban.Key, plain),
HelperFunctions.GetStringRemainder(ban.Value, plain));
} else if (ban.Key.Contains(flip) || ban.Value.Contains(flip)) {
matches.TryAdd(HelperFunctions.GetStringRemainder(ban.Key, flip),
HelperFunctions.GetStringRemainder(ban.Value, flip));
}
}
if (matches.Count > 0) {
if (full.Length > 2) {
return CheckIfBannedWord(full[0], full.Skip(1).ToArray(), matches);
} else {
return CheckIfBannedWord(full[0], new string[0], matches);
}
}
return false;
}
private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return sslPolicyErrors == SslPolicyErrors.None;
}
/// <summary>
/// Sends a message to a given channel from bot
/// </summary>
/// <param name="channel">Channel to send message to</param>
/// <param name="message">Message to send</param>
/// <returns></returns>
public async Task SendMessage(string channel, string message) {
await connected.Task;
await CheckRateLimit();
await tcpWriter.WriteLineAsync($"PRIVMSG #{channel} :{message}");
Console.WriteLine($"SENT #{channel} {User}: {message}");
}
/// <summary>
/// Joins a channel to read messages
/// </summary>
/// <param name="channel">Channel to join</param>
/// <returns></returns>
public async Task JoinChannel(string channel) {
await connected.Task;
await tcpWriter.WriteLineAsync($"JOIN #{channel}");
}
/// <summary>
/// Leaves a channel to stop reading messages from there
/// </summary>
/// <param name="channel">Channel to leave</param>
/// <returns></returns>
public async Task LeaveChannel(string channel) {
await connected.Task;
await tcpWriter.WriteLineAsync($"PART #{channel}");
}
/// <summary>
/// Graceful shutdown of the bot
/// </summary>
/// <returns></returns>
public async Task Kill() {
await SendMessage(User, "MrDestructoid 7 boop beep MrDestructoid 7");
isAlive = false;
}
/// <summary>
/// Ensures the rate limit is never exceeded
/// </summary>
/// Assumes worst case refill timer on the bucket
/// <returns></returns>
private async Task CheckRateLimit()
{
if (!privmsgBucketTimer.IsRunning)
{
privmsgBucketTimer.Start();
}
if (privmsgBucket <= 0)
{
int waitTime = BUCKET_REFILL_RATE - ((int)privmsgBucketTimer.ElapsedMilliseconds);
if (waitTime < 0)
{
waitTime = 0;
}
Console.WriteLine("Bucket empty waiting for " + waitTime + "ms");
await Task.Delay(waitTime);
}
if (privmsgBucketTimer.ElapsedMilliseconds > BUCKET_REFILL_RATE)
{
privmsgBucketTimer.Reset();
privmsgBucket = BUCKET_MAX;
Console.WriteLine("Bucket refill");
}
}
}
}