Skip to content

Commit

Permalink
8.99: $user->events_for_message($msg) returns events for user handlers.
Browse files Browse the repository at this point in the history
            fixed $user->handle() and $user->handle_unsafe() for use with the new system.
            registration commands now utilize the new parameter parser.
            other cleanup.
  • Loading branch information
cooper committed Jul 13, 2014
1 parent 40dbd62 commit 4512ebc
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 59 deletions.
5 changes: 5 additions & 0 deletions INDEV
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,11 @@ CHANGES:
numerics are no longer sent to servers (not with the server as the target anyway).
servers now do not send ERR_UNKNOWNCOMMAND to other registered servers, and all incoming numerics are ignored.

99. $user->events_for_message($msg) returns events for user handlers.
fixed $user->handle() and $user->handle_unsafe() for use with the new system.
registration commands now utilize the new parameter parser.
other cleanup.




Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.98
8.99
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "8.94",
"version" : "8.99",
"name" : "Base::RegistrationCommands",
"author" : {
"website" : "https://github.com/cooper",
Expand Down
26 changes: 17 additions & 9 deletions modules/Base/RegistrationCommands.module/RegistrationCommands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ sub register_registration_command {
}

# parameter check callback.
my $command = uc delete $opts{name};
my $params = $opts{parameters};
$pool->on("connection.command_$command" => sub {
my ($event, @args) = @_;
my $command = uc delete $opts{name};
my $event_name = "connection.message_$command";
my $params = $opts{parameters};
$pool->on($event_name => sub {
my ($event, $msg) = @_;

# there are enough.
return 1 if @args >= $params;
return 1 if $msg->params >= $params;

# not enough.
my $conn = $event->object;
Expand All @@ -59,17 +60,24 @@ sub register_registration_command {
_caller => $mod->package
) or return if $params;

# wrapper that prevents execution after registration.
# wrapper.
my $code = sub {
my ($conn, $event) = @_;
my ($conn, $event, $msg) = @_;

# prevent execution after registration.
return if $conn->{type} && !$opts{after_reg};
$opts{code}(@_);

# arguments.
my @args = $msg->params;
unshift @args, $msg if $opts{with_msg};
unshift @args, $msg->data if $opts{with_data};

$opts{code}($conn, $event, @args);
$event->cancel('ERR_UNKNOWNCOMMAND');
$event->stop unless $opts{continue_handlers}; # prevent later user/server handlers.
};

# attach the callback.
my $event_name = 'connection.command_'.$command.($opts{with_data} ? '_raw' : '');
my $result = $pool->on($event_name => $code,
name => $opts{cb_name},
with_eo => 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "6.1",
"version" : "6.3",
"name" : "Core::RegistrationCommands",
"author" : {
"website" : "https://github.com/cooper",
Expand Down
36 changes: 17 additions & 19 deletions modules/Core/RegistrationCommands.module/RegistrationCommands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,23 @@ sub init {
);

$mod->register_registration_command(
name => shift @$_, code => shift @$_,
parameters => shift @$_, with_data => shift @$_,
after_reg => shift @$_
name => shift @$_, code => shift @$_,
parameters => shift @$_, after_reg => shift @$_
) or return foreach (
#
# PARAMS = number of parameters
# DATA = true if include $data string
# LATER = true if the command should be handled even after registration
#
# [ NAME => \&sub PARAMS DATA LATER
[ PING => \&rcmd_ping, 1, undef, 1 ],
[ PONG => sub { 1 }, undef, undef, 1 ],
[ CAP => \&rcmd_cap, 1, undef, 1 ],
[ NICK => \&rcmd_nick, 1, undef, ],
[ USER => \&rcmd_user, 4, 1, 1 ],
[ SERVER => \&rcmd_server, 5, undef, ],
[ PASS => \&rcmd_pass, 1, undef, ],
[ QUIT => \&rcmd_quit, undef, 1, ],
[ ERROR => \&rcmd_error, 1, undef, 1 ],
# [ NAME => \&sub PARAMS LATER
[ PING => \&rcmd_ping, 1, 1 ],
[ PONG => sub { 1 }, undef, 1 ],
[ CAP => \&rcmd_cap, 1, 1 ],
[ NICK => \&rcmd_nick, 1, ],
[ USER => \&rcmd_user, 4, 1 ],
[ SERVER => \&rcmd_server, 5, ],
[ PASS => \&rcmd_pass, 1, ],
[ QUIT => \&rcmd_quit, undef, ],
[ ERROR => \&rcmd_error, 1, 1 ],
);

return 1;
Expand Down Expand Up @@ -199,7 +197,7 @@ sub rcmd_nick {
}

sub rcmd_user {
my ($connection, $event, $data, @args) = @_;
my ($connection, $event, @args) = @_;

# already registered.
if ($connection->{type}) {
Expand All @@ -208,8 +206,8 @@ sub rcmd_user {
}

$connection->{ident} = $args[0];
$connection->{real} = col((split /\s+/, $data, 5)[4]);
$connection->fire_event(reg_user => @$connection{ qw(ident real) });
$connection->{real} = $args[3];
$connection->fire_event(reg_user => @args[0,3]);
$connection->reg_continue('id2');
}

Expand Down Expand Up @@ -264,8 +262,8 @@ sub rcmd_pass {

# not used for registered users.
sub rcmd_quit {
my ($connection, $event, $data, $arg) = @_;
my $reason = defined $arg ? col((split /\s+/, $data, 2)[1]) : 'leaving';
my ($connection, $event, $arg) = @_;
my $reason = $arg // 'leaving';
$connection->done("~ $reason");
}

Expand Down
6 changes: 3 additions & 3 deletions modules/Core/UserNumerics.module/UserNumerics.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version" : "8.78",
"version" : "8.99",
"name" : "Core::UserNumerics",
"author" : {
"website" : "https://github.com/cooper",
"name" : "Mitchell Cooper"
},
"description" : "the core set of user numerics",
"package" : "M::Core::UserNumerics",
"depends" : {
"modules" : "Base::UserNumerics"
}
},
"package" : "M::Core::UserNumerics"
}
6 changes: 3 additions & 3 deletions modules/Core/UserNumerics.module/UserNumerics.pm
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ sub rpl_isupport {
$lines[$curr] .= $val eq 'YES' ? "$param " : "$param=$val ";
}

return map { "$_:are supported by this server" } @lines
return map { "$_:are supported by this server" } @lines;
}

# CHANMODES in RPL_ISUPPORT.
Expand All @@ -170,7 +170,7 @@ sub isp_chanmodes {
# status (4)
# key (5)
my %m;
my @a = ('', '', '', '', '');
my @a = ('') x 5;

# find each mode letter.
foreach my $name ($ircd::conf->keys_of_block(['modes', 'channel'])) {
Expand All @@ -188,7 +188,7 @@ sub isp_chanmodes {
# alphabetize each group.
foreach my $type (keys %m) {
my @alphabetized = sort { $a cmp $b } @{ $m{$type} };
$a[$type] = join '', @alphabetized
$a[$type] = join '', @alphabetized;
}

return "$a[3],$a[1],$a[2],$a[0]";
Expand Down
2 changes: 1 addition & 1 deletion modules/ircd.module/connection.module/connection.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "8.98",
"version" : "8.99",
"name" : "ircd::connection",
"no_bless" : 1,
"author" : {
Expand Down
5 changes: 1 addition & 4 deletions modules/ircd.module/connection.module/connection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ sub handle {

# user events.
my $user = $connection->user;
push @events,
[ $user, message => $msg ],
[ $user, "message_${cmd}" => $msg ]
if $user;
push @events, $user->events_for_message($msg) if $user;

# fire with safe option.
my $fire = $connection->prepare(@events)->fire('safe');
Expand Down
6 changes: 3 additions & 3 deletions modules/ircd.module/pool.module/pool.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version" : "8.91",
"version" : "8.99",
"name" : "ircd::pool",
"no_bless" : 1,
"author" : {
"website" : "https://github.com/cooper",
"name" : "Mitchell Cooper"
},
"no_bless" : 1,
"preserve_sym" : 1,
"description" : "manage IRC objects",
"preserve_sym" : 1,
"package" : "pool"
}
6 changes: 3 additions & 3 deletions modules/ircd.module/user.module/user.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version" : "8.95",
"version" : "8.99",
"name" : "ircd::user",
"no_bless" : 1,
"author" : {
"website" : "https://github.com/cooper",
"name" : "Mitchell Cooper"
},
"description" : "represents an IRC user",
"no_bless" : 1,
"preserve_sym" : 1,
"description" : "represents an IRC user",
"package" : "user"
}
36 changes: 25 additions & 11 deletions modules/ircd.module/user.module/user.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use overload
bool => sub { 1 };

use utils qw(v notice col conf irc_time);
use List::Util 'first';
use List::Util 'first';
use Scalar::Util 'blessed';

our ($api, $mod, $pool, $me);

Expand Down Expand Up @@ -270,20 +271,33 @@ sub safe {
}

# handle incoming data.
sub handle { handle_with_opts(@_[0,1]) }
sub handle_unsafe { } # FIXME: !
sub handle { _handle_with_opts(undef, @_[0,1]) }
sub handle_unsafe { _handle_with_opts(1, @_[0,1]) }

# handle data (new method) with options.
sub handle_with_opts {
my ($user, $line, %opts) = @_;
my $msg = message->new(data => $line);
# returns the events for an incoming message.
sub events_for_message {
my ($user, $msg) = @_;
my $cmd = $msg->command;
return (
[ $user, message => $msg ],
[ $user, "message_$cmd" => $msg ]
);
}

# handle data (new method) with options.
sub handle_with_opts { _handle_with_opts(undef, @_) }
sub _handle_with_opts_unsafe { _handle_with_opts(1, @_) }
sub _handle_with_opts {
my ($allow_nonlocal, $user, $line, %opts) = @_;

# nonlocal user on ->handle() or some other safe method.
return if !$allow_nonlocal && !$user->is_local;

my $msg = blessed $line ? $line : message->new(data => $line);

# fire commands with options.
$user->prepare(
[ message => $msg ],
[ "message_${cmd}" => $msg ]
)->fire('safe', data => \%opts);
my @events = $user->events_for_message($msg);
$user->prepare(@events)->fire('safe', data => \%opts);

}

Expand Down

0 comments on commit 4512ebc

Please sign in to comment.