Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the first commands

 - NICK
 - USER
 - DEBUG (__NOT__ an official irc command. it is used to debug the server state and will be removed in version 1.0.0)
  • Loading branch information...
commit f8192f16bac1e3ff4a6908e28bc322dd9b4fadc8 1 parent a8f8b34
Lucas Dohring authored
Showing with 59 additions and 4 deletions.
  1. +59 −4 IRCd.pl
View
63 IRCd.pl
@@ -2,6 +2,8 @@
use strict;
use warnings;
+use Data::Dumper;
+
use IO::Select;
use IO::Socket;
use IO::Socket::INET6;
@@ -11,12 +13,13 @@
$SIG{'TERM'} = 'cleanup';
my $server = IO::Socket::INET6->new( Listen => 5,
- LocalPort => 1234,
+ LocalPort => 6667,
ReuseAddr => 1,
Proto => 'tcp' );
my $poll = IO::Select->new( $server );
-my $clients = {};
+my $clients = {};
+my $nicks = {};
my $i = 0;
my $prefix_re = qr/(?:\:[^ \0]*)?/;
@@ -24,6 +27,53 @@
my $middle_re = qr/^ ([^\r\n\0\: ][^\r\n\0 ]*)/;
my $trailing_re = qr/^ :([^\r\n\0]*)/;
+my $commands =
+ {
+ DEBUG => sub
+ {
+ my $fh = shift;
+ print $fh "Self:", Dumper($clients->{$fh});
+ print $fh "Clients:", Dumper($clients);
+ print $fh "Nicks:", Dumper($nicks);
+ },
+
+ USER => sub
+ {
+ my ($fh, @params) = @_;
+ my $self = $clients->{$fh};
+ unless($self->{connected})
+ {
+ $self->{user} = shift(@params);
+ $self->{ircname} = pop(@params);
+ }
+ },
+
+ NICK => sub
+ {
+ my ($fh, @params) = @_;
+ my $self = $clients->{$fh};
+ my ($nick) = @params;
+
+ unless($nicks->{$nick})
+ {
+ my $old_nick = $self->{nick};
+ if($self->{user})
+ {
+ my $user = $self->{user};
+ my $host = $self->{host};
+ print $fh ":$old_nick!$user\@$host NICK :$nick\r\n";
+ }
+ $nicks->{$nick} = $self;
+ $self->{nick} = $nick;
+ }
+ else
+ {
+ print $fh "error";
+ }
+ return 1;
+ }
+ };
+
while(my @ready = $poll->can_read())
{
foreach my $fh (@ready)
@@ -33,7 +83,7 @@
my $new = $server->accept();
$poll->add($new);
- $clients->{$new} = { id => $i++};
+ $clients->{$new} = { id => $i++, host=> gethostbyaddr($new->peeraddr(), AF_INET6)};
}
else # Handle a client;
{
@@ -45,6 +95,7 @@
my ($command, @params) = parse_message($message);
if($command)
{
+ print $fh "";
handle($fh, $command, @params);
}
}
@@ -102,8 +153,12 @@ sub handle
my ($fh, $command, @params) = @_;
#my @params = @$params;
my $params = join(',', @params);
-
print "$command($params);\n";
+
+ if(my $func = $commands->{uc($command)})
+ {
+ &$func($fh, @params);
+ }
}
sub end
Please sign in to comment.
Something went wrong with that request. Please try again.