-
Notifications
You must be signed in to change notification settings - Fork 0
/
4x4EvolutionNormalizer.pl
749 lines (634 loc) · 20.1 KB
/
4x4EvolutionNormalizer.pl
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#################################################################
# Author : nightfrog
# Purpose: Show realnames instead of nicks to mimic 4x4 Evolution
# Version: 1 - Complete rewrite
#################################################################
#
#########################################################################################
# Shit that needs accomplished
# 2: Handle misc events that I forgot or overlooked
# 3: Fix any mistakes I have made
# DONE 4: USRIP to generate NICK-PORT
# 5: This is a big one... Connect in the sequence the game connects.
# Usually this requires a source code edit
#########################################################################################
use strict;
use warnings;
use EvoTool;
use IO::Socket;
use Xchat qw( :all );
#Add the network(s) this script should work with
my @netNames = qw( fuzzy personal dc-talk );
# Make a hash for the the nicks and realnames global yet not global
# Eventually make this an object with accessors
#
# Push to the list
# listPush( $net, $nick, $realname )
#
# Delete a user
# listDeleteUser( $net, $nick )
#
# Delete a network
# listDeleteNet( $net )
#
# Get the list
# listGet()
# A reference is return and you must dereference it!
# Example: my %hash = %{ listGet() };
{
my %list;
sub listPush
{
my ( $net, $nick, $realname ) = @_;
if ( $net and $nick and $realname )
{
$list{ $net }->{ $nick } = $realname;
return 1;
}
return;
}
sub listDeleteUser
{
my ( $net, $nick ) = @_;
if ( exists $list{ $net }->{ $nick } )
{
delete $list{ $net }->{ $nick };
return 1;
}
return;
}
sub listDeleteNet
{
my ( $net ) = @_;
if ( exists $list{ $net } )
{
delete $list{ $net };
return 1;
}
return;
}
sub listGet
{
return \%list;
}
}
# When the script is loaded into an already running XChat
# we need to "scan" the networks and WHO each channel on our network(s)
init0();
sub init0
{
# Remember this context for later
my $context = get_info( 'context' );
my $tabType;
my $tabNetwork;
my $tabName;
my $tabContext;
for my $tab ( get_list( 'channels' ) )
{
$tabType = $tab->{ type };
$tabNetwork = $tab->{ network };
$tabName = $tab->{ channel };
$tabContext = $tab->{ context };
if ( $tabType == 2 and grep { lc( $_ ) eq lc( $tabNetwork ) } @netNames )
{
set_context( $tabContext );
whoGet( $tabNetwork, $tabName );
}
}
# Go back to the original window
set_context( $context );
}
# Most of the events are the same and can be looped through
for my $event ( 'Channel Message', 'Channel Msg Hilight' )
{
hook_print( $event, \&eventChannelMessage, { 'data' => $event } );
}
for my $event ( 'Private Message', 'Private Message to Dialog' )
{
hook_print( $event, \&eventPrivateMessage, { 'data' => $event } );
}
for my $event ( 'Notice', 'Notice Send' )
{
hook_print( $event, \&eventNotice, { 'data' => $event } );
}
for my $event ( 'Channel DeOp',
'Channel DeHalfOp',
'Channel DeVoice',
'Channel Half-Operator',
'Channel Operator',
'Channel Voice' )
{
hook_print( $event, \&eventChannelMode, { 'data' => $event } );
}
# Channel messages
sub eventChannelMessage
{
my ( $data, $event ) = @_;
my ( $nick, $what, $mode ) = @$data;
my $network = get_info( 'network' );
my $channel = get_info( 'channel' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
#That dirty little message the client sends when it joins a channel
#emit a join message here since there is a realname to send in it
if ( $what =~ m/^\^STATUS\s(.+)/g )
{
my $realname = ( split( /\^0|\^1/, $1 ) )[0];
emit_print( 'Join', $realname, $channel, user_info( $nick )->{ host } );
return EAT_XCHAT;
}
#If the NICK has not been pushed into the list then
#it needs to be done and hopefully before the emit.
if ( not exists $nnr{ $network }->{ $nick } )
{
whoGet( $network, $nick );
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( $event, $realname, $what, $mode );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
# Private messages
sub eventPrivateMessage
{
my ( $data, $event ) = @_;
my ( $nick, $what ) = @$data;
my $network = get_info( 'network' );
my $channel = get_info( 'channel' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
#If the NICK has not been pushed into the list then
#it needs to be done and hopefully before the emit.
if ( not exists $nnr{ $network }->{ $nick } )
{
whoGet( $network, $nick );
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( $event, $realname, $what );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
# Notices
sub eventNotice
{
my ( $data, $event ) = @_;
my ( $nick, $what ) = @$data;
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
if ( not exists $nnr{ $network }->{ $nick } and $nick !~ /^#/ )
{
whoGet( $network, $nick );
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( $event, $realname, $what );
return EAT_XCHAT;
}
}
}
# Channel modes
sub eventChannelMode
{
my ( $data, $event ) = @_;
my ( $by, $to ) = @$data;
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
#Don't need to who both if only one needs to be who'ed
if ( not exists $nnr{ $network }->{ $by } )
{
whoGet( $network, $by );
}
if ( not exists $nnr{ $network }->{ $to } )
{
whoGet( $network, $to );
}
elsif ( exists $nnr{ $network }->{ $by } and exists $nnr{ $network }->{ $to } )
{
my $realname0 = ( split( /\^0|\^1/, $nnr{ $network }->{ $by } ) )[0];
my $realname1 = ( split( /\^0|\^1/, $nnr{ $network }->{ $to } ) )[0];
emit_print( $event, $realname0, $realname1 );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
#Add or change a user when the /RCHG command is executed
hook_server( 'RCHG', sub
{
my $nick = substr( $_[0][0], 1 );
my $realname = substr( $_[1][2], 1 );
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
if ( not exists $nnr{ $network }->{ $nick } )
{
$nnr{ $network }->{ $nick } = $realname;
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $me = user_info();
#Only need to see when the actual realname changes
#and not the lap count and other shit in the realnames
my $nameNew = ( split( /\^0|\^1/, $realname ) )[0];
my $nameOld = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
#Them
if ( lc( $nameNew ) ne lc( $nameOld ) and $me->{ nick } ne $nick )
{
$nnr{ $network }->{ $nick } = $nameNew;
emit_print( 'Change Nick', $nameOld, $nameNew );
}
#Us
elsif ( lc( $nameNew ) ne lc( $nameOld ) and $me->{ nick } eq $nick )
{
$nnr{ $network }->{ $nick } = $nameNew;
emit_print( 'Your Nick Changing', $nameOld, $nameNew );
}
}
}
return EAT_XCHAT;
}
);
#Delete a network from the hash when a Server context is closed
hook_print( 'Close_Context', sub
{
my $network = context_info->{ network };
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
#Proceed if it is a server context and from out list of networks
if ( context_info->{ type } == '1'
and exists $nnr{ $network }
and grep { lc( $_ ) eq lc( $network ) } @netNames
)
{
listDeleteNet( $network );
}
}
return EAT_NONE;
}
);
#EAT the event since we handle it in the channel messages
hook_print( 'Join', sub
{
my ( $nick, $channel, $host ) = @{ $_[0] };
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames
and $nick =~ /[A-P]{12}|^_/ #NICK must be Evo or Admin
)
{
return EAT_XCHAT;
}
return EAT_NONE;
}
);
hook_print( 'Part', sub
{
my ( $nick, $host, $channel ) = @{ $_[0] };
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
if ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( 'Part', $realname, $host, $channel );
listDeleteUser( $network, $nick );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
);
hook_print( 'Quit', sub
{
my ( $nick, $reason, $host ) = @{ $_[0] };
my $network = get_info( 'network' );
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
if ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( 'Quit', $realname, $reason, $host );
listDeleteUser( $network, $nick );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
);
hook_print( 'You Join', sub
{
my ( $me, $channel, $host ) = @{ $_[0] };
my $network = get_info( 'network' );
# This is the order of the commands the client sends when it joins a channel
# Taken directly from the irc.log file
#
# IN | :aServer.com 366 NICK #EvoR :End of /NAMES list.
# OUT | RCHG :nightfrog^0
# OUT | PRIVMSG #EvoR :^STATUS nightfrog^0
# OUT | TOPIC #EvoR
# OUT | WHO #EvoR
#
# We need to detect the end of /names and then what we need to do.
# Let's do this....
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
my $namesEnd;
$namesEnd = hook_server( '366', sub
{
if ( lc( $_[0][3] ) eq lc( $channel ) )
{
# We will who the channel in a little bit and add our realname
# to the hash then so for now use get_prefs() for our realname
command( 'QUOTE RCHG :' . get_prefs( 'irc_real_name' ) );
command( 'QUOTE PRIVMSG ' . $channel . ' :^STATUS ' . get_prefs('irc_real_name') );
# Just an example if we were follow the order but XChat does this already
# and we don't give a shit when we get the topic.
# command( 'TOPIC ' . $channel );
whoGet( $network, $channel );
}
unhook( $namesEnd );
}
);
}
return EAT_NONE;
}
);
hook_print( 'Your Message', sub
{
my ( $nick, $what, $mode ) = @{ $_[0] };
my $network = get_info( 'network' );
# Do nothing unless there is a network in the network list
return EAT_NONE unless $network;
# Only continue if the channel message is from one of the wanted networks
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
# If the NICK has not been pushed into the list then
# it needs to be done and hopefully before the emit.
if ( not exists $nnr{ $network }->{ $nick } )
{
whoGet( $network, $nick );
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( 'Your Message', $realname, $what, $mode );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
);
hook_print( 'Channel Notice', sub
{
my ( $nick, $channel, $what ) = @{ $_[0] };
my $network = get_info( 'network' );
# Do nothing unless there is a network in the network list
return EAT_NONE unless $network;
# Only continue if the channel message is from one of the wanted networks
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my %nnr = %{ listGet() };
if ( not exists $nnr{ $network }->{ $nick } )
{
whoGet( $network, $nick );
}
elsif ( exists $nnr{ $network }->{ $nick } )
{
my $realname = ( split( /\^0|\^1/, $nnr{ $network }->{ $nick } ) )[0];
emit_print( 'Channel Notice', $realname, $channel, $what );
return EAT_XCHAT;
}
}
return EAT_NONE;
}
);
sub whoGet
{
# Handle channel and nick who's here
# $channel can be either a channel or nick
my ( $network, $channel ) = @_;
if ( $network and $channel ) #Make sure...
{
command( 'WHO ' . $channel ); # Wasn't needed before
my $hooked_who;
$hooked_who = hook_server( '352', sub
{
#$_[0][3] -- CHANNEL
#$_[0][7] -- NICK
#$_[1][10] -- REALNAME
if ( lc( $_[0][3] ) eq lc( $channel )
or lc( $_[0][7] ) eq lc( $channel )
)
{
listPush( $network, $_[0][7], $_[1][10] )
}
return EAT_XCHAT;
}
);
# At the end of the /who unhook each numeric event
my $who_end;
$who_end = hook_server( '315', sub
{
unhook($hooked_who);
unhook($who_end);
return EAT_XCHAT;
}
);
}
}
# This should really use hook_fd;
sub usrip
{
my ( $network ) = @_;
my $ip;
my $host;
my $port;
# Get the host and port from the network list
for my $nets ( get_list( 'networks' ) )
{
if ( lc( $network ) eq lc( $nets->{ network } ) )
{
$host = @{ $nets->{servers} }[0]->{ host };
$port = @{ $nets->{servers} }[0]->{ port };
}
}
# Create the socket
my $socket = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Blocking => 0,
Timeout => 1 # This is needed. Why????
);
while ( my $line = <$socket> )
{
$socket->send( "USRIP\n" );
if ( $line =~ /:.+\s+302\s+:unknown=\+unknown\@(.+)/ )
{
$ip = $1;
$socket->close(); # close the connection
last; # Got what we came for so GTFO of here!
}
}
return $ip;
}
hook_command( 'iptonick', sub
{
my $ip = $_[0][1];
my $regexIP = qr/^(?:(?:25[0-5]|2[0-4][\d]|[0-1]?[\d]{1,2})[.]
(?:25[0-5]|2[0-4][\d]|[0-1]?[\d]{1,2})[.]
(?:25[0-5]|2[0-4][\d]|[0-1]?[\d]{1,2})[.]
(?:25[0-5]|2[0-4][\d]|[0-1]?[\d]{1,2}))$/x;
if ( $ip =~ $regexIP )
{
prnt ipToNick( $ip );
}
else
{
prnt 'Please enter a valid IP address.';
}
return EAT_XCHAT;
},
{
help_text => 'Convert a 32 bit IP to a valid 4x4 EvoR NICK'
}
);
hook_command( 'nicktoip', sub
{
my $nick = $_[0][1];
if ( $nick =~ /[A-P]{12}/ )
{
prnt nickToIPPort( $nick );
}
else
{
prnt 'Please enter a valid 4x4 EvoR nick';
}
return EAT_XCHAT;
},
{
help_text => 'Convert a 4x4 EvoR nick to 32 bit IP address'
}
);
hook_command( 'evogen', sub
{
my $nick = $_[0][1];
my $longIP = longIP( $_[0][2] );
my $password = EvoTool::Evo1Password( $nick, $longIP );
my $pwdVerify = EvoTool::verifyPasswordEvo1( $nick, $longIP, $password );
if ( $pwdVerify )
{
prnt $password;
}
return EAT_XCHAT;
},
{
help_text => 'Convert a 4x4 EvoR password generator'
}
);
sub longIP {
return unpack 'N!', pack 'C4', split /\./, shift;
}
sub nickToIP
{
my $nick = $_[0];
$nick =~ tr/A-P/0-9A-F/;
return sprintf( "%vd", unpack "A4n", pack "H*", $nick );
}
sub nickToIPPort
{
my $nick = $_[0];
$nick =~ tr/A-P/0-9A-F/;
return sprintf( "%vd:d", unpack "A4n", pack "H*", $nick );
}
sub ipToNick
{
if ( $_[0] )
{
my $ip = unpack "H8", pack "C4n", split /\./, $_[0];
$ip =~ tr/0-9a-f/A-P/;
# 65535 - 49152 = 16383
my $port = unpack "H4", pack "S4n", int( rand( 16383 ) ) + 49152;
$port =~ tr/0-9a-f/A-P/;
return $ip . $port;
}
}
register(
'Realnames',
0x3DFB3F,
'Replace NICKS in the Text Events with realnames to mimic 4x4 Evolution'
);
__END__
##########################################################################
##########################################################################
# UPDATE
# So I did some thinking and came up with what you are about to see
# Here is how it works
#
# The real client sends a PRIVMSG beginning with ^STATUS
# when it joins to let the other clients know it joined the channel.
# Since we know it will send this we can use it as a point to emit the join event
#
# 1: The JOIN hook_print hooks the join events for all of XChat
# 2: Check if this happened on a server specified in @netNames (Duh)
# 3: Hook the message and then and use the real name in it to add to the emit
# 4: $1 contains the realname in the ^STATUS message
# 5: Unhook the PRIVMSG event and the JOIN event
#
#
# While typing these comments I came up with a better solution
# $hookJoin being global doesn't sit well with me.
THIS IS WORKING CODE!
my $hookJoin; # Global :-(
$hookJoin =
hook_print( 'Join', sub
{
my ( $nick, $channel, $host ) = @{ $_[0] };
my $network = get_info('network');
if ( grep { lc( $_ ) eq lc( $network ) } @netNames )
{
my $hookPRIVMSG;
$hookPRIVMSG =
hook_server('PRIVMSG', sub
{
# Host $_[0][0];
# Event $_[0][1];
# Channel $_[0][2];
# Message $_[1][3];
if ( lc( $_[0][2] ) eq lc( $channel )
and $_[1][3] =~ m/^:\^STATUS\s(.+)/
and exists $nnr{ $network }->{ $nick }
)
{
my $realname = ( split( /\^0|\^1/, $1 ) )[0];
emit_print( 'Join', $realname, $channel, $host );
}
unhook($hookPRIVMSG);
}
);
unhook($hookJoin);
return EAT_XCHAT;
}
return EAT_NONE;
}
);