Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Can parse messages and properly ingores prefix

(I think)
  • Loading branch information...
commit a8f8b34502d5da068022edbe9b36bee34f6d0383 1 parent c0a9b01
Lucas Dohring authored
Showing with 68 additions and 8 deletions.
  1. +68 −8 IRCd.pl
View
76 IRCd.pl
@@ -19,6 +19,11 @@
my $clients = {};
my $i = 0;
+my $prefix_re = qr/(?:\:[^ \0]*)?/;
+my $command_re = qr/([A-z]+|\d\d\d)/;
+my $middle_re = qr/^ ([^\r\n\0\: ][^\r\n\0 ]*)/;
+my $trailing_re = qr/^ :([^\r\n\0]*)/;
+
while(my @ready = $poll->can_read())
{
foreach my $fh (@ready)
@@ -28,14 +33,20 @@
my $new = $server->accept();
$poll->add($new);
- $clients->{$new} = $i++;
+ $clients->{$new} = { id => $i++};
}
else # Handle a client;
{
- if(defined(my $message = <$fh>))
+ if(my $message = <$fh>)
{
- my $n = $clients->{$fh};
- print "$n: $message";
+ $message =~ s/\R//g;
+ $message =~ $prefix_re;
+ $message = $';
+ my ($command, @params) = parse_message($message);
+ if($command)
+ {
+ handle($fh, $command, @params);
+ }
}
else
{
@@ -45,15 +56,64 @@
}
}
+sub parse_message
+{
+ my $message = shift;
+ if(my ($command) = $message =~ $command_re )
+ {
+
+ my @params;
+ my $rest = $';
+
+ while(my $param=get_middle_or_trailing(\$rest))
+ {
+ push(@params, $param);
+ }
+
+ return ($command, @params);
+ }
+}
+
+sub get_middle_or_trailing
+{
+ my $rest = shift;
+ #print "REST: $$rest\n";
+
+ if($$rest =~ $middle_re)
+ {
+ $$rest = $';
+ #print "MIDDLE: $1\n";
+ return $1;
+ }
+ elsif($$rest =~ $trailing_re)
+ {
+ #print "TRAIL: $1\n";
+ $$rest = "";
+ return $1;
+ }
+ else
+ {
+ return undef;
+ }
+}
+
+sub handle
+{
+ my ($fh, $command, @params) = @_;
+ #my @params = @$params;
+ my $params = join(',', @params);
+
+ print "$command($params);\n";
+}
+
sub end
{
- my $fh = shift;
- $poll->remove($fh);
- $fh->close();
+ my $fd = shift;
+ $poll->remove($fd);
+ $fd->close();
}
sub cleanup;
{
$server->close();
-my $aeu;
}
Please sign in to comment.
Something went wrong with that request. Please try again.