-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw.pp
More file actions
688 lines (528 loc) · 13.4 KB
/
Copy pathhw.pp
File metadata and controls
688 lines (528 loc) · 13.4 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
(*
* InfoBOT, lobby commander (a bot a for Hedgewars lobby chat)
* Copyright (c) 2013-12/2014 Solaris <solargrim@gmail.com>
*)
{$UNDEF DEBUG}
{$MODE OBJFPC}
{
TODO:
- better support for NOTICE
- better support for USER FLAGS (plugin command: onFlagChange)
- sometimes bot use 100% of cpu (?)
}
program Hedgewars_InfoBOT(lobby, commander);
uses
sysutils,
dateutils,
baseunix,
strutils,
cthreads,
cmem,
sockets,
hwTypes in 'hwTypes.pp',
hwObjects in 'hwObjects.pp';
var
hw: hwObjects.THwbot;
procedure Main();cdecl;forward;
function Parse():Boolean;
function StripHTML(S: ansistring): string;
var
TagBegin, TagEnd, TagLength: integer;
begin
TagBegin:=Pos('<', S);
while (TagBegin > 0) do
begin
TagEnd:=Pos('>', S);
TagLength:=TagEnd - TagBegin + 1;
Delete(S, TagBegin, TagLength);
TagBegin:=Pos('<', S);
end;
StripHTML:=S;
end;
var
input: String;
s1, s2, s3, s4, s5, s6, s7,s8: String;
output: AnsiString;
i, k: integer;
begin
// DEFAULT SERVER COMMANDS
input:=hwTypes.line;
if (input = 'BYE') then
begin
readln(sin, s1);
writeln('[!] Register error: ',s1);
quit:=TRUE;
exit;
end;
if (input = 'ERROR') then
begin
readln(sin, s1);
writeln('[!] Error: ',s1);
if (pos('Incorrect',s1) = 0) then
hw.Reconnect();
exit;
end;
if (input = 'NOTICE') then
begin
// hw.Reconnect(); {?}
exit;
end;
if (input = 'QUIT') then
begin
writeln('[#] Closing. ');
exit;
end;
if (input = 'PING') then
begin
writeln(sOut, 'PONG',#10);
exit;
end;
if (input = 'SERVER_MESSAGE') then
begin
readln(sIn, motd);
writeln('[#] MESSAGE OF THE DAY');
writeln(StripHTML(motd));
exit;
end;
if (input = 'LOBBY:LEFT') then
begin
readln(sIn, s1);
readln(sIn, s2);
writeln('[#] ',s1,' quits (',s2,')');
hw.uDelUser(s1);
// plugin:OnQuit (like PartLobby)
if (hw.pc <> 0) then
for k:=1 to (hw.pc) do
begin
try
s8:=hw.plugin[k].onQuit(s1);
if (length(s8) = 0) then
continue;
writeln(sout, 'CHAT'+#10+s8, #10);
except
continue;
end;
end;
if (s1 = HW_NICK) then
begin
hw.Reconnect();
exit;
end;
end;
if (input = 'JOINED') then
begin
readln(sIn, s1);
writeln('[#] ',s1,' joined to room.');
// plugin:OnRoomJoin
if (hw.pc <> 0) then
for k:=1 to (hw.pc) do
begin
try
s8:=hw.plugin[k].onJoinRoom(s1);
if (length(s8) = 0) then
continue;
writeln(sout, 'CHAT'+#10,s8,#10);
except
continue;
end;
end;
exit;
end;
if (input = 'LOBBY:JOINED') then
begin
readln(sIn, s1);
writeln('[#] ',s1,' connected.');
// plugin:OnLobbyJoin
if (hw.pc <> 0) then
for k:=1 to (hw.pc) do
begin
try
s8:=hw.plugin[k].OnJoinLobby(s1);
if (length(s8) = 0) then
continue;
writeln(sout, 'CHAT'+#10,s8, #10);
except
continue;
end;
end;
hw.uAddUser(s1, '+u');
exit;
end;
if (input = 'CLIENT_FLAGS') then
begin
readln(sin, s1);
readln(sin, s2);
writeln('[#] ',s2,': new user flag: ',s1);
//uAddFlag
if (s1 = '-i') then
begin
{plugin:onBackLobby}
end;
end;
if (input = 'ROOM') then
begin
readln(sin, s1);
if (s1 = 'ADD') then
begin
readln(sin, s1);
readln(sin, s2);
readln(sin, s3);
readln(sin, s4);
readln(sin, s5);
readln(sin, s6);
readln(sin, s7);
readln(sin, s8);
readln(sin, s8);
writeln('[#] New room: ',s2);
if (HW_NOTICES = 'true') or (HW_NOTICES = '1') then
writeln(sout, 'CHAT'+#10,'/me -> ',s5,' created new room: ',s2,#10);
end;
if (s1 = 'DEL') then
begin
readln(sin, s1);
writeln('[#] Room deleted: ',s1);
end;
if (s1 = 'UPD') then
begin
readln(sin, s1);
readln(sin, s2);
readln(sin, s3);
readln(sin, s4);
readln(sin, s5);
readln(sin, s6);
readln(sin, s7);
readln(sin, s8);
readln(sin, s8);
writeln('[#] Update room: ',s1{s3});
end;
end;
if (input = 'CHAT') then
begin
readln(sin, s1);
readln(sin, s2);
if (copy(s2, 1, (pos(':', s2)-1)) = HW_NICK) then
begin
s8:=s2;
for i:=1 to pos(':',s2) do
delete(s8, 1, 1);
s8:=trimleft(s8);
s8:=trimright(s8);
s2:=s8;
end;
writeln('[#] [',TimeToStr(Now),'] ',s1,': ',s2);
// Private bot commands
if (copy(s2, 1, 5) = HW_CMDCHAR+'help') then
begin
s3:=copy(s2, 7, length(s2));
s3:=trim(s3);
// Custom help for plugins
if (hw.pc <> 0) and (s3 <> '') then
begin
for k:=1 to (hw.pc) do
if (s3 = trim(hw.plugin[k].name)) then
begin
try
writeln(sout, 'CHAT'+#10+'HELP ('+s3+'): '+hw.plugin[k].ghelp(), #10);
exit;
except
writeln(sout, 'CHAT'+#10+s1+': Help text is not defined.'+#10);
exit;
end;
exit;
end;
writeln(sout, 'CHAT'+#10+s1+': Help is not available.'+#10);
exit;
end;
writeln(sOut, 'CHAT');writeln(sout, 'Available public commands:',#10);
writeln(sout, 'CHAT');writeln(sout, '> '+HW_CMDCHAR+'help [command]',#10);
writeln(sOut, 'CHAT');writeln(sout, '> '+HW_CMDCHAR+'version',#10);
// plugin:usage
if (hw.pc <> 0) then
for k:=1 to (hw.pc) do
begin
try
s8:=hw.plugin[k].cmd;
s8[1]:=HW_CMDCHAR;
writeln(sout, 'CHAT'+#10+'> '+s8+#32+hw.plugin[k].usage+' (Plugin)'+ #10);
except
continue;
end;
end;
end;
if (s2 = HW_CMDCHAR+'version') then
begin
writeln(sout, 'CHAT'+#10+'InfoBOT (hedgewars lobby commander) by solargrim@gmail.com, version: '+VERSION{$IFDEF DEBUG}+'-DEBUG'{$ENDIF},#10);
exit;
end;
if (ExtractWord(1, s2, [#32]) = HW_CMDCHAR+'reload') and (s1 = HW_ADMIN) then
begin
if (hw.LoadConfig()) then
writeln(sout, 'CHAT',#10, s1,': config reloaded.',#10) else
writeln(sout, 'CHAT',#10, s1,': Failed to reload config.',#10);
end;
if (ExtractWord(1, s2, [#32]) = HW_CMDCHAR+'join') and (s1 = HW_ADMIN) then
begin
s3:=ExtractWord(2, s2, [#32]);
if (s3 <> '') then
begin
writeln(sout, 'CREATE_ROOM');
writeln(sout, s3,#10);
writeln(sout, 'JOIN_ROOM');
writeln(sout, s3,#10);
end else
begin
writeln(sout, 'CREATE_ROOM');
writeln(sout, HW_ROOM,#10);
writeln(sout, 'JOIN_ROOM');
writeln(sout, HW_ROOM,#10);
end;
exit;
end;
if (copy(s2, 1, 4) = HW_CMDCHAR+'raw') and (s1 = HW_ADMIN) then
begin
s3:=copy(s2, 6, length(s2));
writeln('[*] Sending RAW DATA to server: ',s3);
s4:=StringReplace(s3, '\n', #10, [rfReplaceAll]);
if (s4[length(s4)] <> #10) then
s4+=#10;
if (s4 <> '') then
writeln(sout, s4);
end;
if (copy(s2, 1, 5) = HW_CMDCHAR+'kick') and (s1 = HW_ADMIN) then
begin
s3:=ExtractWord(2, s2, [#32]);
if (s3 = HW_ADMIN) then
exit;
writeln('[*] Kicking user from room: ',s3);
writeln(sout, 'KICK',#10,s3,#10);
exit;
end;
if (s2 = HW_CMDCHAR+'op') and (s1 = HW_ADMIN) then
begin
writeln('[*] Adding room operator status to bot admin');
writeln(sout, 'CMD');
writeln(sout, 'delegate ',HW_ADMIN,#10);
exit;
end;
if (s2 = HW_CMDCHAR+'part') and (s1 = HW_ADMIN) then
begin
writeln(sout, 'PART',#10);
exit;
end;
if (s2 = HW_CMDCHAR+'die') then
begin
if (s1 = HW_ADMIN) then
begin
quit:=True;
exit;
end;
writeln(sout,'CHAT');
if (random(2) = 1) then
writeln(sout, 'No way ',s1,'!',#10) else
writeln(sout, 'Try again later ',s1,'!',#10);
end;
if (s2 = HW_CMDCHAR+'fork') and (s1 = HW_ADMIN) then
begin
writeln('[*] Clone: ',hwTypes.HW_NICK);
fpExecVe('/home/users/solaris/hwbot/hw', nil, envp);
end;
if (copy(s2, 1,4)= HW_CMDCHAR+'say') and (s1 = HW_ADMIN) then
begin
if (ExtractWord(2, s2, [#32]) = '') then
exit;
writeln(sout, 'CHAT');
writeln(sout, trim(copy(s2, 5, length(s2)))+#10);
end;
if (s2 = HW_CMDCHAR+'users') then
begin
writeln('USER LIST ->');
for i:=0 to high(hw.user) do
if (hw.user[i].nickname <> '') then
write(hw.user[i].nickname + '(',hw.user[i].mode,') ');
writeln;
end;
if (ExtractWord(1, s2, [#32]) = HW_CMDCHAR+'set') then
begin
s7:=ExtractWord(2, s2, [#32]);
s8:=ExtractWord(3, s2, [#32]);
if (s7 = '') and (s8 = '') then
begin
writeln(sout, 'CHAT',#10,s1,': cmdchar = ',HW_CMDCHAR,#10);
writeln(sout, 'CHAT',#10,s1,': notices = ',HW_NOTICES,#10);
exit;
end;
if (s7 <> '') and (s8 = '') then
begin
if (s7 = 'cmdchar') then s5:=HW_CMDCHAR;
if (s7 = 'notices') then s5:=HW_NOTICES;
writeln(sout, 'CHAT',#10,s1+': ',s7,' = ',s5,#10);
exit;
end;
if (s7 = 'cmdchar') then
HW_CMDCHAR:=s8[1];
if (s7 = 'notices') then
HW_NOTICES:=s8;
writeln(sout, 'CHAT',#10,s1+': variable ',s7,' set to ',s8,#10);
end;
if (copy(s2, 1, 7) = HW_CMDCHAR+'plugin') and (s1 = HW_ADMIN) then
begin
if (ExtractWord(2, s2, [#32]) = 'list') then
begin
if (hw.pc = 0) then
begin
writeln(sout, 'CHAT',#10,s1,': No plugins loaded.',#10);
exit;
end;
for k:=1 to (hw.pc) do
begin
try
if (hw.plugin[k].hnd <> 0) then
writeln(sout, 'CHAT'+#10+s1,': (',k,') ',hw.plugin[k].name,' -> ',hw.plugin[k].author,' (',hw.plugin[k].fname,'):',hw.plugin[k].ver,' (',hw.plugin[k].cmd,')'#10);
except
continue;
end;
end;
end;
if (ExtractWord(2, s2, [#32]) = 'reload') then
begin
s8:=ExtractWord(3, s2, [#32]);
writeln('[P:Reloading]: ',s8);
try
i:=StrToInt(ExtractWord(3, s2, [#32]));
except
writeln('FAILED!');
exit;
end;
case hw.plugin[i].Reload of
0: writeln(sout,'CHAT'+#10+s1+': Plugin '+hw.plugin[i].name+' reloaded.',#10);
1: writeln(sout,'CHAT'+#10+s1+': Failed to reload plugin (file not exists).',#10);
2: writeln(sout,'CHAT'+#10+s1+': Failed to reload plugin (not all functions defined).',#10);
3: writeln(sout,'CHAT'+#10+s1+': Failed to reload plugin (wtf?).',#10);
end;
exit;
end;
if (ExtractWord(2, s2, [#32]) = 'load') then
begin
writeln(sout, 'CHAT');
s3:=HW_PLUGINS+ExtractWord(3, s2,[#32]);
if (pos(s3, '.so') = 0) then
s3+='.so';
hw.pc+=1;
with hw do plugin[pc]:=TPlugin.Create;
case (hw.plugin[hw.pc].Load(s3)) of
0: begin
writeln(sout, s1,': Plugin loaded: ',hw.plugin[hw.pc].name+#10);
exit;
end;
1: writeln(sout, s1,': Plugin error: File does not exists.',#10);
2: writeln(sout, s1,': Plugin error: Failed to load library.',#10);
3: writeln(sout, s1,': Plugin error: Is not valid or deprecated.',#10);
end;
hw.pc-=1;
exit;
end;
if (ExtractWord(2, s2, [#32]) = 'unload') then
begin
try
k:=StrToInt(ExtractWord(3, s2, [#32]));
except
exit;
end;
if (hw.plugin[k].Unload()) then
writeln(sout, 'CHAT',#10,s1,': Plugin removed.',#10)
else
writeln(sout, 'CHAT',#10,s1,': Error while removing plugin.',#10);
end;
end;
// run plugins by defined command
if (hw.pc <> 0) then
for k:=1 to (hw.pc) do
begin
s8:=hw.plugin[k].cmd;
s8[1]:=HW_CMDCHAR;
if (copy(s2, 1, pos(#32, s2)-1) = s8) or (s8 = s2) then
begin
try
output:=hw.plugin[k].Parse(s1+':'+s2, hw.user, HW_NICK);
if (length(output) <> 0) then
writeln(sout, 'CHAT'+#10,output, #10);
except
continue;
end;
exit;
end;
end;
end; // .private commands
exit(TRUE);
end;
function test(p: pointer):ptrint;
var
t: TDateTime;
j: LongInt;
begin
t:=Now;
while (fprecv(hwTypes.s, @hwtypes.line, 255, MSG_DONTWAIT) < 0) do
begin
// timeout?
if (Now - t > 10) then
begin
writeln('[!] Connection lost (hwLoop)');
hw.Reconnect();
end;
end;
Parse();
EndThread();
end;
function hwLoop():Boolean;
begin
repeat
readln(sIn, hwTypes.line);
{$IFDEF DEBUG}if (hwTypes.line<>'') then writeln(' -> "',hwTypes.line,'"');{$ENDIF}
{$IFDEF THREAD}
BeginThread(@test);
{$ELSE}
Parse();
{$ENDIF}
until (quit);
exit(true);
end;
procedure RegisterSignal();
var
oa, na: PSigActionRec;
begin
new(oa);
new(na);
na^.sa_Handler:=SigActionHandler(@Main);
fillchar(na^.Sa_Mask,sizeof(na^.sa_mask),#0);
na^.Sa_Flags:=0;
{$IFDEF LINUX}na^.Sa_Restorer:=Nil;{$ENDIF}
if fpSigAction(SigHup,na,oa) <> 0 then
exit;
dispose(oa);
dispose(na);
end;
procedure Main(); cdecl;
begin
writeln('[@] InfoBOT for Hedgewars by Solaris (solargrim@gmail.com) ver. ',VERSION);
hw:=THwbot.Create;
HW_NICK:='INFOBOT';
HW_ROOM:=HW_NICK+'ROOM SERVICE';
HW_CMDCHAR:='.';
HW_NOTICES:='true';
SetLength(hw.plugin, HW_MAXPLUGINS);
SetLength(hw.user, HW_MAXUSERS);
hw.pc:=0;
GetDir(0, HW_PLUGINS);
HW_PLUGINS+='/plugins/';
RegisterSignal();
hw.LoadConfig();
hw.info();
hw.Connect();
hw.Register();
hw.InitPlugins();
hwLoop();
hw.Disconnect();
end;
begin
Hedgewars_InfoBot.Main();
end.