-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSpammer.cs
394 lines (358 loc) · 15.3 KB
/
Spammer.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
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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using AdvancedBot.client;
using System.Diagnostics;
using System.Drawing;
namespace AdvancedBot
{
public partial class Spammer : Form
{
private static Regex RAND_TOKEN_REGEX = new Regex(@"{rand,([^,]+),(\d+)}", RegexOptions.Compiled);
private static Regex FOREACH_TOKEN_REGEX = new Regex(@"{foreach,([^,]+),(.+)}", RegexOptions.Compiled);
private static Random RNG = new Random();
private Thread msgLoopThread;
private int msgDelay;
bool continueLoop = true;
bool spamMsgs = false;
public Spammer()
{
InitializeComponent();
tbMsg.PreviewKeyDown += (s, e) => e.IsInputKey = e.KeyCode == Keys.Tab;
tbMsg.MouseDown += (s, e) => HideSuggestions();
tbMsg.KeyDown += (s, e) => {
if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
tbMsg.SelectAll();
else
HandleSuggestKeys(e);
};
tbMsg.KeyPress += (s, e) => {
if (e.KeyChar == '\t') { e.Handled = true; return; }
if (e.KeyChar < 0x20) return;
string txt = tbMsg.Text;
if (txt.Length > 0) {
int currentLine = tbMsg.GetFirstCharIndexOfCurrentLine();
int lnEnd = currentLine;
for (int l = txt.Length; lnEnd < l; lnEnd++) {
char c = txt[lnEnd];
if (c == '\r' || c == '\n') break;
}
int lnLen = lnEnd - currentLine;
if (lnLen >= 100) e.Handled = true;
}
};
msgLoopThread = new Thread(() => {
while (continueLoop) {
try {
if (spamMsgs) {
bool delayLn = cbDelayForEachLines.Checked;
if (msgDelay == 0) msgDelay = (int)numericUpDown1.Value;
foreach (string txt in tbMsg.Lines) {
if (!ParseForeachToken(txt)) {
List<MinecraftClient> clients = Program.FrmMain.Clients;
for (int i = 0; i < clients.Count; i++) {
clients[i].SendMessage(ParseRandToken(txt));
}
}
if (delayLn) Thread.Sleep(msgDelay);
}
if (!delayLn) Thread.Sleep(msgDelay);
} else {
Thread.Sleep(20);
}
} catch { }
}
});
msgLoopThread.Start();
FormClosing += (s, e) => {
continueLoop = false;
try {
msgLoopThread.Join(200);
msgLoopThread.Abort();
} catch { }
};
}
private void tbMsg_TextChanged(object sender, EventArgs e)
{
string txt = tbMsg.Text;
int lnLen = txt.Length;
if (txt.Length > 0) {
int currentLine = tbMsg.GetFirstCharIndexOfCurrentLine();
int lnEnd = currentLine;
for (int l = txt.Length; lnEnd < l; lnEnd++) {
char c = txt[lnEnd];
if (c == '\r' || c == '\n') break;
}
lnLen = lnEnd - currentLine;
}
SuggestCompletions(txt);
lblRestChars.Text = (100 - lnLen) + " caracteres restantes";
}
#region auto completion
private ListBox lbSuggestions;
private bool suggestionsForCurrent = true;
private int currentBlockSize = 0;
private void HideSuggestions()
{
suggestionsForCurrent = false;
if (lbSuggestions != null)
lbSuggestions.Visible = false;
}
private void SuggestCompletions(string txt)
{
int cur = tbMsg.SelectionStart;
int tmp;
string block = ExtractTokenBlock(txt, cur, out tmp);
if (block != null) {
string[] p = block.Substring(1).Split(',');
if (!suggestionsForCurrent && p.Length == currentBlockSize) return;
suggestionsForCurrent = true;
currentBlockSize = p.Length;
if (lbSuggestions == null) {
lbSuggestions = new ListBox();
lbSuggestions.ScrollAlwaysVisible = true;
lbSuggestions.Size = new Size(150, 80);
lbSuggestions.IntegralHeight = false;
Controls.Add(lbSuggestions);
}
lbSuggestions.BeginUpdate();
lbSuggestions.Items.Clear();
switch (p.Length) {
case 1:
foreach (string token in "rand,foreach".Split(','))
if (token.StartsWith(p[0]))
lbSuggestions.Items.Add(token);
break;
case 2: {
string first = p[0];
if (first == "rand")
lbSuggestions.Items.AddRange("[a-z],[A-Z],[0-9]".Split(','));
else if (first == "foreach")
lbSuggestions.Items.AddRange("player,playerd,playerdi".Split(','));
}
break;
case 3: {
string first = p[0];
if (first == "rand")
lbSuggestions.Items.AddRange("5,10,20,50,100".Split(','));
else if (first == "foreach")
lbSuggestions.Items.AddRange("%nick%|{rand,@#$!ABCD,100}".Split('|'));
}
break;
}
Point pos = tbMsg.GetPositionFromCharIndex(cur - 1);
lbSuggestions.Location = new Point(tbMsg.Location.X + pos.X + 12, tbMsg.Location.Y + pos.Y + 28);
lbSuggestions.EndUpdate();
lbSuggestions.BringToFront();
if (lbSuggestions.Items.Count == 0)
lbSuggestions.Visible = false;
else {
lbSuggestions.SelectedIndex = 0;
if (!lbSuggestions.Visible) lbSuggestions.Visible = true;
}
} else if (lbSuggestions != null && lbSuggestions.Visible) lbSuggestions.Visible = false;
}
private void ApplySuggestion()
{
if (lbSuggestions.SelectedItem != null) {
string suggest = lbSuggestions.SelectedItem.ToString();
if (suggest[0] == '[' && suggest[suggest.Length - 1] == ']') {
char basis = suggest[1];
int l = (basis | 0x20) == 'a' ? 26 : 10;
char[] c = new char[l];
for (int i = 0; i < l; i++)
c[i] = (char)(basis + i);
suggest = new string(c);
}
int st;
int curOld = tbMsg.SelectionStart;
string txt = tbMsg.Text;
string block = ExtractTokenBlock(txt, curOld, out st);
if (block != null) {
int pos = -1;
int commaCount = 0;
for (int i = curOld - 1; i >= st; i--) {
char ch = txt[i];
if (ch == ',' || ch == '{') {
if (pos == -1) pos = i + 1;
commaCount += ch != '{' ? 1 : 0;
}
}
if (commaCount < 2) {
if (!block.Contains("{rand"))
suggest += ",";
else
pos = st + block.Length;
tbMsg.SelectionStart = st + pos;
tbMsg.SelectionLength = curOld - pos;
}
tbMsg.SelectedText = suggest;
} else tbMsg.AppendText(suggest);
}
}
private void HandleSuggestKeys(KeyEventArgs e)
{
if (lbSuggestions == null || !lbSuggestions.Visible) return;
switch (e.KeyCode) {
case Keys.Down:
lbSuggestions.SelectedIndex = (lbSuggestions.SelectedIndex + 1) % lbSuggestions.Items.Count;
break;
case Keys.Up:
int n = lbSuggestions.SelectedIndex - 1;
if (n < 0) n = lbSuggestions.Items.Count - 1;
lbSuggestions.SelectedIndex = n;
break;
case Keys.Tab: ApplySuggestion(); break;
case Keys.Escape: HideSuggestions(); break;
case Keys.OemPipe: if (lbSuggestions.Visible) lbSuggestions.Visible = false; break;
}
}
private string ExtractTokenBlock(string txt, int pos, out int start)
{
start = -1;
for (int i = pos - 1; i >= 0; i--) {
char ch = txt[i];
if (ch == '{') {
start = i;
break;
} else if (ch == '}') return null;
}
if (start == -1) return null;
for (int i = start, b = 0; i < txt.Length + 1; i++) {
if (i >= pos) return txt.Substring(start);
char ch = txt[i];
if (ch == '{')
b++;
else if (ch == '}' && --b == 0)
return txt.Substring(start, i - start + 1);
}
return null;
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (spamMsgs) {
spamMsgs = false;
btn.Text = "Iniciar";
} else {
spamMsgs = true;
btn.Text = "Parar";
}
}
public static string ParseRandToken(string txt)
{
//foreach(Match m in RAND_TOKEN_REGEX.Matches(txt))
for (Match m; (m = RAND_TOKEN_REGEX.Match(txt)).Success;) {
string chars = m.Groups[1].Value;
int count;
if (int.TryParse(m.Groups[2].Value, out count)) {
if (count > 99) count = 99;
char[] rndStr = new char[count];
for (int i = 0; i < count; i++)
rndStr[i] = chars[RNG.Next(chars.Length)];
if (m.Index != 0) {
string init = txt.Substring(0, m.Index);
string end = txt.Substring(m.Index + m.Length);
txt = init + new string(rndStr) + end;
} else
txt = new string(rndStr) + txt.Substring(m.Length);
}
}
return txt;
}
private bool ParseForeachToken(string txt)
{
Match m = FOREACH_TOKEN_REGEX.Match(txt);
if (m.Success) {
string property = m.Groups[1].Value.ToLower();
string text = m.Groups[2].Value;
switch (property) {
case "player":
foreach (MinecraftClient cli in Program.FrmMain.Clients) {
if (cli.IsBeingTicked()) {
PlayerManager pm = cli.PlayerManager;
foreach (string nick in pm.UUID2Nick.Values)
cli.SendMessage(ParseRandToken(text).Replace("%nick%", nick));
}
}
return true;
case "playerd": return ProcessForeachPlayerDelay(text);
case "playerdi": return ProcessForeachPlayerDelayI(text);
}
}
return false;
}
private bool ProcessForeachPlayerDelay(string text)
{
HashSet<string> nicks = new HashSet<string>();
foreach (MinecraftClient cli in Program.FrmMain.Clients) {
if (cli.IsBeingTicked()) {
PlayerManager pm = cli.PlayerManager;
foreach (string nick in pm.UUID2Nick.Values)
if (nick != cli.Username)
nicks.Add(nick);
}
}
int sepIdx = text.IndexOf('|');
int delay;
if (sepIdx != -1 && int.TryParse(text.Substring(0, sepIdx), out delay)) {
text = text.Substring(sepIdx + 1);
if (text.Length > 0) {
foreach (string nick in nicks) {
if (!spamMsgs) return true;
string ftxt = ParseRandToken(text).Replace("%nick%", nick);
foreach (MinecraftClient cli in Program.FrmMain.Clients)
cli.SendMessage(ftxt);
Thread.Sleep(delay);
}
}
}
return true;
}
private bool ProcessForeachPlayerDelayI(string text)
{
List<string> nicks = new List<string>();
foreach (MinecraftClient cli in Program.FrmMain.Clients) {
if (cli.IsBeingTicked()) {
PlayerManager pm = cli.PlayerManager;
foreach (string nick in pm.UUID2Nick.Values)
if (nick != cli.Username && !nicks.Contains(nick))
nicks.Add(nick);
}
}
int sepIdx = text.IndexOf('|');
int delay;
int count = 2;
if (sepIdx != -1 && nicks.Count > 0) {
string dparams = text.Substring(0, sepIdx);
int sepIdxn = dparams.IndexOf('-');
if (!int.TryParse(sepIdxn == -1 ? dparams : dparams.Substring(0, sepIdxn), out delay)) return false;
if (sepIdxn != -1 ? !int.TryParse(dparams.Substring(sepIdxn + 1), out count) : false) return false;
text = text.Substring(sepIdx + 1);
if (text.Length > 0) {
int nickIdx = 0;
for (int i = 0; i < count; i++) {
foreach (MinecraftClient cli in Program.FrmMain.Clients) {
if (!spamMsgs) return true;
string nick = nicks[nickIdx++ % nicks.Count];
cli.SendMessage(ParseRandToken(text).Replace("%nick%", nick));
}
Thread.Sleep(delay);
}
}
}
return true;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
msgDelay = (int)numericUpDown1.Value;
}
private void Spammer_Load(object sender, EventArgs e)
{
Translation.setup(this);
}
}
}