-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.pm
740 lines (659 loc) · 18 KB
/
Server.pm
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
#!/usr/bin/perl
#####################################################################
# Server.pm #
# Created: Tue Sep 15 12:56:51 1998 by [email protected] #
# Revised: Wed Jun 5 12:59:27 2002 by [email protected] #
# Copyright 1998 Jay F. Kominek ([email protected]) #
# #
# Consult the file 'LICENSE' for the complete terms under which you #
# may use this file. #
# #
#####################################################################
# server package - juno ircd #
#####################################################################
package Server;
use Utils;
use User;
use Channel;
use strict;
use UNIVERSAL qw(isa);
use Tie::IRCUniqueHash;
my $commands = {'PING' => \&handle_ping,
'PONG' => \&handle_pong,
# Channel membership
'JOIN' => \&handle_join,
'PART' => \&handle_part,
'INVITE' => \&handle_invite,
'KICK' => \&handle_kick,
# Channel status
'TOPIC' => \&handle_topic,
'MODE' => \&handle_mode,
# User presence
'NICK' => \&handle_nick,
'CLIENT' => \&handle_client,
'KILL' => \&handle_kill,
'QUIT' => \&handle_quit,
# User status
'AWAY' => \&handle_away,
# Server presence
'SERVER' => \&handle_server,
'SQUIT' => \&handle_squit,
# Communication
'PRIVMSG'=> \&handle_privmsg,
'NOTICE' => \&handle_notice,
};
###################
# CLASS CONSTRUCTOR
###################
# Pass it its name.
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = { };
my $connection = shift;
$this->{'name'} = $connection->{'servername'};
$this->{'description'} = $connection->{'description'};
$this->{'distance'} = $connection->{'distance'};
$this->{'proto'} = $connection->{'proto'};
$this->{'server'} = $connection->{'server'};
$this->{'version'} = $Utils::VERSION;
$this->{'last_active'} = time();
tie my %usertmp, 'Tie::IRCUniqueHash';
$this->{'users'} = \%usertmp;
tie my %childtmp, 'Tie::IRCUniqueHash';
$this->{'children'} = \%childtmp;
bless($this, $class);
if(defined($connection->{'socket'})) {
$this->{'socket'} = $connection->{'socket'};
$this->{'outbuffer'} = $connection->{'outbuffer'};
$this->setupaslocal($connection->{'initiated'});
}
$this->{'server'}->addchildserver($this);
return $this;
}
sub setupaslocal {
my $this = shift;
my $initiated = shift;
my $server = $this->server;
# lookup and send password
if(!$initiated) {
# when it comes to server linking, passwords mean nothing
# because the guy who wrote this said so, i assume
# *adds to todo list*
$this->senddata("PASS :password\r\n");
###################################################################
# Start dumping the state of the entire network at the new server #
# Dump the server tree
# This server has to be sent in the special fashion. All other
# servers are spewed using the recursive method 'spewchildren'
$this->senddata(join(' ', # delimiter
"SERVER",
$server->name,
1,
0, # timestamp a
time, # timestamp b
"Px1",
":".$server->description)."\r\n");
}
foreach my $childserver ($server->children()) {
$this->spewchildren($childserver);
}
# Dump the users
# For big networks, it would be a very good idea to generate all the
# data that needs to be sent at one time, use Compress::Zlib on that
# data and then spew it binaryily.
# As it is, we will merely use the special burst user command
foreach my $user (values(%{Utils::users()})) {
# I was so excited when i erased BU and BC! :D
$this->senddata(join(' ',
":".$user->server->name,
"CLIENT",
$user->nick,
$server->hops+1,
$user->time_create,
$user->genmodestr,
$user->username,
$user->host,
$user->ip,
$user->nick,
":".$user->ircname)."\r\n");
$this->senddata(join(' ',
":".$user->nick,
"ENCAP",
"*",
"REALHOST",
$user->host)."\r\n");
}
foreach my $channel (values(%{Utils::channels()})) {
my $users = $channel->{'users'};
my($users,$modes,$cargs,$uid,$modestr);
if ($channel->ismode('k')) { $modes .= "k"; $cargs .= " ".$channel->{'key'}; }
if ($channel->ismode('l')) { $modes .= "l"; $cargs .= " ".$channel->{'limit'}; }
if ($channel->ismode('L')) { $modes .= "L"; $cargs .= " ".$channel->{'link'}; }
if ($channel->ismode('i')) { $modes .= "i"; }
if ($channel->ismode('m')) { $modes .= "m"; }
if ($channel->ismode('n')) { $modes .= "n"; }
if ($channel->ismode('s')) { $modes .= "s"; }
if ($channel->ismode('t')) { $modes .= "t"; }
foreach my $user ($channel->users()) {
$uid = $user->{'nick'};
if ($channel->hasvoice($user)) { $uid = "+$uid"; }
if ($channel->ishalfop($user)) { $uid = "\%$uid"; }
if ($channel->isop($user)) { $uid = "\@$uid"; }
if ($channel->isadmin($user)) { $uid = "&$uid"; }
if ($channel->isowner($user)) { $uid = "~$uid"; }
$users .= " ".$uid;
$server = $user->server;
}
$cargs = ltrim($cargs);
if ($cargs eq "" || $cargs eq " ") { $modestr = "+$modes"; } else { $modestr = "+$modes $cargs"; }
$this->senddata(join(' ',
":".$server->{'name'},
"SJOIN",
time,
$channel->{'name'},
$modestr,
":".ltrim($users)."\r\n"
));
my ($bans,$mutes);
foreach my $ban ($channel->bans()) {
$bans .= " ".$ban;
}
$this->senddata(join(' ',
":".$server->{'name'},
"BMASK",
time,
$channel->{'name'},
'b',
":".ltrim($bans)."\r\n"
)) if ($bans ne "");
foreach my $mute ($channel->mutes()) {
$mutes .= " ".$mute;
}
$this->senddata(join(' ',
":".$server->{'name'},
"BMASK",
time,
$channel->{'name'},
'Z',
":".ltrim($mutes)."\r\n"
)) if ($mutes ne "");
$this->senddata(join(' ',
":".$server->{'name'},
"TB",
$channel->{'name'},
$channel->{'topicsettime'},
$channel->{'topicsetter'},
":".$channel->{'topic'}."\r\n"
)) if ($channel->{'topic'} ne "");
}
}
sub spewchildren {
my $this = shift;
my $server = shift;
$this->senddata(join(' ',
":".$this->server->name,
"SERVER",
$server->name,
$server->hops+1,
0, # timestamp a
time, # timestamp b
"Px1",
":".$server->description)."\r\n");
foreach($server->children()) {
$this->spewchildren($_);
}
}
#####################################################################
# PROTOCOL HANDLERS
###################
##############
# Main Handler
sub handle {
my $this = shift;
my $rawline = shift;
my $line;
foreach $line (split(/\x00/,$rawline)) {
$line =~ s/\s+$//;
$this->{'last_active'} = time();
delete($this->{'ping_waiting'});
$line =~ /^\S+ (\S+) .+$/;
my $command = $1;
# Parsing stuff from a server is a bit more complicated than parsing
# for a user.
if(ref($commands->{$command})) {
&{$commands->{$command}}($this,$line);
} else {
if($line =~ /[\w\d\s]/) {
Utils::syslog('notice', "Received unknown command string \"$line\" from ".$this->name);
}
}
}
}
# :remote PING :string
sub handle_ping {
my $this = shift;
my($from,$command,$arg) = split(/\s+/,shift,3);
$arg =~ s/^://;
$this->senddata(":".$this->name." PONG :$arg\r\n");
}
# :remote PONG :string
sub handle_pong {
# my $this = shift;
# Don't waste our time doing anything
}
# :remote BU nick username host modestr ircname
sub handle_burstuser {
my $this = shift;
my($from,$command,$nick,$username,$host,$modestr,$ircname) =
split/\s+/,shift,7;
$from =~ s/^://;
$ircname =~ s/^://;
my $user = Connection->new({nick => $nick,
user => $username,
host => $host,
ircname => $ircname,
server => $this,
connected => time});
my @modes = split//,$modestr;
shift @modes;
foreach my $mode (@modes) {
$user->setmode($mode);
}
Utils::users()->{$user->nick()} = $user;
}
# :remote BC name creation modestr modeargs nicklistwmode
sub handle_burstchannel {
my $this = shift;
my($from,$command,$name,$creation,$modestr,@remainder) = split/\s+/,shift;
$from =~ s/^://;
my $channel = Channel->new($name,$creation);
$modestr =~ s/^\+//;
foreach my $mode (split//,$modestr) {
if(Channel::isvalidchannelmode($mode)) {
$channel->{'modes'};
}
}
}
sub handle_uid {
my $this = shift;
my $line = shift;
$line =~ /^:(\S+)/;
my $from = Utils::lookup($1);
if(!ref($from)) {
Utils::plog("lred","error","network desyncage on client introduction");
return;
}
if($from->isa("Server")) {
my($remote,$command,$nick,$hopcount,$timestamp,$usermodes,$username,$hostname,$ip,$gecos) = split(/\s+/,$line,9);
# The user will add itself to the appropriate server itself
my $user = User->new({ 'nick' => $nick,
'user' => $username,
'host' => $hostname,
'ip' => $ip,
'time_create' => time,
'modes' => $usermodes,
'ircname' => $gecos,
'server' => $from,
'connected' => $timestamp });
Utils::users()->{$user->nick()} = $user;
} elsif($from->isa("User")) {
# User is attempting to change their nick
my($nick,$command,$newnick) = split(/\s+/,$line,3);
} else {my($nick,$command,$newnick) = split(/\s+/,$line,3);
# network weirdness
}
}
sub handle_nick {
my $this = shift;
my $line = shift;
$line =~ /^:(\S+)/;
my $from = Utils::lookup($1);
if ($from->isa("User")) {
my($nick,$command,$newnick) = split(/\s+/,$line,3);
}
else {
my($nick,$command,$newnick) = split(/\s+/,$line,3);
Utils::plog("lred","warning","non-user attempting to change its nick - are you using our formal linking protocol?");
}
}
# :nick KILL target :excuse
sub handle_kill {
my $this = shift;
}
# :remote QUIT nick excuse
sub handle_quit {
my $this = shift;
my($nick,$command,$excuse) = split(/\s+/,shift,3);
# redistribute the quit message to other servers
$nick =~ s/^://;
$excuse =~ s/^://;
my $user = Utils::lookup($nick);
if(ref($user) && $user->isa("User")) {
$user->quit($excuse);
} else {
Utils::syslog('notice', "Attempted to quit user who doesn't exist to us.");
# woo! network desync, getting quits from users we don't know about
}
}
sub handle_mode {
my $this = shift;
my($ref,$command,$mode) = split(/\s+/,shift,3);
# redistribute the quit message to other servers
$ref =~ s/^://;
$mode =~ s/^://;
my @s = split(" ",$mode,3);
my $from = Utils::lookup($ref);
my $channel = Utils::lookup($s[0]);
my @arguments = split(" ",$s[2]);
$channel->mode($ref,$s[1],@arguments);
}
sub handle_privmsg { # added on june 27, 2010 :)
my $this = shift;
my($nick,$command,$msg) = split(/\s+/,shift,3);
$nick =~ s/^://;
my @s = split(" ",$msg,2);
my $user = Utils::lookup($nick);
my $channel = Utils::lookup($s[0]);
my $message = $s[1];
$message = substr($message,1);
my $string = "PRIVMSG";
$channel->privmsgnotice($string,$user,$message);
}
# :nick AWAY :excuse
sub handle_away {
my $this = shift;
my($nick,$command,$excuse) = split/\s+/,shift,3;
$nick =~ s/^://;
$excuse =~ s/^://;
my $user = Utils::lookup($nick);
if(ref($user) && $user->isa("User")) {
if(defined($excuse)) {
$user->{'awaymsg'} = $excuse;
} else {
delete($user->{'awaymsg'});
}
} else {
# network desyncage.
}
}
# :nick JOIN :#channel1,#channel2,...
sub handle_join {
my $this = shift;
my($nick,$command,$channels) = split/\s+/,shift,3;
$nick =~ s/^://;
$channels =~ s/^://;
my $user = Utils::lookup($nick);
if(ref($user) && $user->isa("User")) {
my @channels = split/,/,$channels;
foreach my $channel (@channels) {
$channel = Utils::lookup($channel);
if(ref($channel) && $channel->isa("Channel")) {
$channel->force_join($user,$this);
}
}
} else {
# Network desyncage
}
}
# :nick PART :#channel1,#channel2,...
sub handle_part {
my $this = shift;
my($nick,$command,$channels) = split/\s+/,shift,3;
$nick =~ s/^://;
$channels =~ s/^://;
my $user = Utils::lookup($nick);
if(ref($user) && $user->isa("User")) {
my @channels = split/,/,$channels;
foreach my $channel (@channels) {
$channel = Utils::lookup($channel);
if(ref($channel) && $channel->isa("Channel")) {
$channel->part($user,$this);
}
}
} else {
# Network desyncage
}
}
#######################################################################
sub squit {
my $this = shift;
# we need to descend our tree of children, announcing the signoff of
# every one of them, then announce the server disconnect(s) and dump
# all the data structures. non-trivial.
# for now:
my $user;
foreach $user ($this->users()) {
$user->quit(join(' ',$this->parent->name,$this->name));
}
# Tell our parent we're gone
$this->parent->removechildserver($this);
# Remove us from the Servers hash
delete(Utils::servers()->{$this->name()});
# Close our socket if we have one
if($this->{'socket'}) {
&main::finishclient($this->{'socket'});
}
}
#####################################################################
# SENDING THE SERVER STUFF
##########################
########################
# User state information
# Takes a User as the argument, sends the server all requisite information
# about that user.
sub nick {
my $this = shift;
my $user = shift;
$this->senddata(join(' ',
":".$user->server->name,
"CLIENT",
$user->nick,
$user->server->hops+1,
$user->time_create,
$user->genmodestr,
$user->username,
$user->host,
$user->ip,
":".$user->ircname)."\r\n");
# $this->senddata(join(' ',
# ":".$user->uid,
# "ENCAP",
# "*",
# "REALHOST",
# $user->host)."\r\n");
# I gave up on TS6
}
# Takes a user and their excuse as the arguments and informs
# $this of that user's disconnection
sub uquit {
my $this = shift;
my $user = shift;
my $excuse = shift;
$this->senddata(join(' ',
":".$user->nick,
"QUIT",
$excuse)."\r\n");
}
###############
# Channel state
sub join {
my $this = shift;
my $user = shift;
my $channel = shift;
$this->senddata(join(' ',
":".$user->server->{'name'},
"SJOIN",
time,
$channel->{'name'},
"+nt",
":@".$user->{'nick'}."\r\n"
));
}
sub part {
my $this = shift;
my $user = shift;
my $channel = shift;
$this->senddata(join(' ',
":".$user->nick,
"PART",
$channel->{'name'}),
":Leaving\r\n");
}
sub mode {
my $this = shift;
my $from = shift;
my $target = shift;
my $str = shift;
$this->senddata(join(' ',
":".($from->isa("User")?$from->{'nick'}:$from->{'name'}),
"MODE",
($target->isa("User")?$target->{'nick'}:$target->{'name'}),
$str)."\r\n");
}
# Dispatch a wallops to this server
sub wallops {
my $this = shift;
my($from,$message) = @_;
my $fromstr = "*unknown*";
if($from->isa("User")) {
$fromstr = $from->nick;
} elsif($from->isa("Server")) {
$fromstr = $from->name;
}
$this->senddata(join(' ',
":".$fromstr,
"WALLOPS",
$message)."\r\n");
}
sub ping {
my $this = shift;
if($this->{'socket'}) {
$this->{ping_waiting} = 1;
$this->senddata(":".$this->{'server'}->name." PING :".$this->{'server'}->name."\r\n");
} else {
}
}
############################
# DATA ACCESSING SUBROUTINES
############################
# Get the name of this IRC server
sub name {
my $this = shift;
return $this->{'name'};
}
sub description {
my $this = shift;
return $this->{'description'};
}
# Returns an array of all the users on
# the server
sub users {
my $this = shift;
my @foo = values(%{$this->{users}});
return @foo;
}
# Returns an array of all the children servers
sub children {
my $this = shift;
my @foo = values(%{$this->{children}});
return @foo;
}
sub last_active {
my $this = shift;
return $this->{'last_active'};
}
sub connected {
my $this = shift;
return $this->{'connected'};
}
sub ping_in_air {
my $this = shift;
if($this->{'ping_waiting'}) {
return 1;
} else {
return 0;
}
}
# Returns the parent server of this server.
# (if you keep finding the parent of the parent
# until there isn't one, (this server) and then
# go back one, you'll have the server that a message
# has to be sent through to be routed properly.)
sub parent {
my $this = shift;
return $this->{'server'};
}
sub server {
my $this = shift;
return $this->{'server'};
}
# Returns the number of hops to reach the local
# server from the server represented by this
# server object.
sub hops {
my $this = shift;
return ($this->parent->hops+1);
}
###############################
# DATA MANIPULATING SUBROUTINES
###############################
# Tells the server that this person is now on it
sub adduser {
my $this = shift;
my $user = shift;
$this->{'users'}->{$user->nick()} = $user;
}
sub removeuser {
my $this = shift;
my $user = shift;
# This allows us to remove a user by their nick
# or their User object.
my $nick;
if(ref($user)) {
$nick = $user->nick();
} else {
$nick = $user;
}
Utils::syslog('notice', "server ".$this->name." is being requested to remove user $nick");
delete($this->{'users'}->{$nick});
}
# Adds a server to the list of ones on this one.
sub addchildserver {
my $this = shift;
my $child = shift;
$this->{'children'}->{$child->name()} = $child;
}
# Removes a server from the list of ones on this one.
sub removechildserver {
my $this = shift;
my $child = shift;
my $name;
if(ref($child)) {
$name = $child->name();
} else {
$name = $child;
}
delete($this->{'children'}->{$name});
}
#####################################################################
# RAW, LOW-LEVEL OR MISC SUBROUTINES
####################################
# Add a command to the hash of commandname->subroutine refs
sub addcommand {
my $this = shift;
my $command = shift;
my $subref = shift;
$this->{'commands'}->{$command} = $subref;
}
sub senddata {
my $this = shift;
$this->{'outbuffer'}->{$this->{'socket'}} .= join('',@_);
}
sub ltrim ($) {
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Ri
1;