aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZoffix Znet <cpan@zoffix.com>2016-01-01 20:23:05 -0500
committerZoffix Znet <cpan@zoffix.com>2016-01-01 20:23:05 -0500
commitff7d4cd63a2823cc2687f9f4347ebbb489561e87 (patch)
tree2d245f60edf5b36d947e6af8ecbe11fb92dbb281 /lib
parent7fd44a2042127044b5af9a0a80339ae9de7d8e91 (diff)
Refactor event handling logic to use smaller indent
Diffstat (limited to 'lib')
-rw-r--r--lib/IRC/Client.pm6132
1 files changed, 66 insertions, 66 deletions
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6
index 0b64cab..d003efb 100644
--- a/lib/IRC/Client.pm6
+++ b/lib/IRC/Client.pm6
@@ -2,71 +2,71 @@ use v6;
use IRC::Parser; # parse-irc
use IRC::Client::Plugin::PingPong;
use IRC::Client::Plugin;
-class IRC::Client:ver<2.002001> {
- has Bool:D $.debug = False;
- has Str:D $.host = 'localhost';
- has Str $.password;
- has Int:D $.port where 0 <= $_ <= 65535 = 6667;
- has Str:D $.nick = 'Perl6IRC';
- has Str:D $.username = 'Perl6IRC';
- has Str:D $.userhost = 'localhost';
- has Str:D $.userreal = 'Perl6 IRC Client';
- has Str:D @.channels = ['#perl6bot'];
- has IO::Socket::Async $.sock;
- has @.plugins = [];
- has @.plugins-essential = [
- IRC::Client::Plugin::PingPong.new
- ];
- has @!plugs = [|@!plugins-essential, |@!plugins];
-
- method run {
- .irc-start-up: self for @!plugs.grep(*.^can: 'irc-start-up');
-
- await IO::Socket::Async.connect( $!host, $!port ).then({
- $!sock = .result;
- $.ssay("PASS $!password\n") if $!password.defined;
- $.ssay("NICK $!nick\n");
- $.ssay("USER $!username $!username $!host :$!userreal\n");
- $.ssay("JOIN {@!channels[]} x\n");
-
- .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected');
-
- # my $left-overs = '';
- react {
- whenever $!sock.Supply :bin -> $buf is copy {
- my $str = try $buf.decode: 'utf8';
- $str or $str = $buf.decode: 'latin-1';
- # $str ~= $left-overs;
- $!debug and "[server {DateTime.now}] {$str}".put;
- my $events = parse-irc $str;
- EVENTS: for @$events -> $e {
- self.handle-event: $e;
- CATCH { warn .backtrace }
- }
+unit class IRC::Client:ver<2.002001>;
+
+has Bool:D $.debug = False;
+has Str:D $.host = 'localhost';
+has Str $.password;
+has Int:D $.port where 0 <= $_ <= 65535 = 6667;
+has Str:D $.nick = 'Perl6IRC';
+has Str:D $.username = 'Perl6IRC';
+has Str:D $.userhost = 'localhost';
+has Str:D $.userreal = 'Perl6 IRC Client';
+has Str:D @.channels = ['#perl6bot'];
+has IO::Socket::Async $.sock;
+has @.plugins = [];
+has @.plugins-essential = [
+ IRC::Client::Plugin::PingPong.new
+];
+has @!plugs = [|@!plugins-essential, |@!plugins];
+
+method run {
+ .irc-start-up: self for @!plugs.grep(*.^can: 'irc-start-up');
+
+ await IO::Socket::Async.connect( $!host, $!port ).then({
+ $!sock = .result;
+ $.ssay("PASS $!password\n") if $!password.defined;
+ $.ssay("NICK $!nick\n");
+ $.ssay("USER $!username $!username $!host :$!userreal\n");
+ $.ssay("JOIN {@!channels[]}\n");
+
+ .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected');
+
+ # my $left-overs = '';
+ react {
+ whenever $!sock.Supply :bin -> $buf is copy {
+ my $str = try $buf.decode: 'utf8';
+ $str or $str = $buf.decode: 'latin-1';
+ # $str ~= $left-overs;
+ $!debug and "[server {DateTime.now}] {$str}".put;
+ my $events = parse-irc $str;
+ for @$events -> $e {
+ self.handle-event: $e;
+ CATCH { warn .backtrace }
}
-
- CATCH { warn .backtrace }
}
- say "Closing connection";
- $!sock.close;
+ CATCH { warn .backtrace }
+ }
- # CATCH { warn .backtrace }
- });
- }
+ say "Closing connection";
+ $!sock.close;
- method ssay (Str:D $msg) {
- $!debug and "{plug-name}$msg".put;
- $!sock.print("$msg\n");
- self;
- }
+ # CATCH { warn .backtrace }
+ });
+}
- method privmsg (Str $who, Str $what) {
- my $msg = "PRIVMSG $who :$what\n";
- $!debug and "{plug-name}$msg".put;
- $!sock.print("$msg\n");
- self;
- }
+method ssay (Str:D $msg) {
+ $!debug and "{plug-name}$msg".put;
+ $!sock.print("$msg\n");
+ self;
+}
+
+method privmsg (Str $who, Str $what) {
+ my $msg = "PRIVMSG $who :$what\n";
+ $!debug and "{plug-name}$msg".put;
+ $!sock.print("$msg\n");
+ self;
}
method handle-event ($e) {
@@ -74,44 +74,44 @@ method handle-event ($e) {
for @!plugs.grep(*.^can: 'irc-all-events') -> $p {
my $res = $p.irc-all-events(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
if ( $e<command> eq 'PRIVMSG' and $e<params>[0] eq $!nick ) {
for @!plugs.grep(*.^can: 'irc-privmsg-me') -> $p {
my $res = $p.irc-privmsg-me(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
}
if ( $e<command> eq 'NOTICE' and $e<params>[0] eq $!nick ) {
for @!plugs.grep(*.^can: 'irc-notice-me') -> $p {
my $res = $p.irc-notice-me(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
}
if ( ( $e<command> eq 'PRIVMSG' and $e<params>[0] eq $!nick )
or ( $e<command> eq 'NOTICE' and $e<params>[0] eq $!nick )
or ( $e<command> eq 'PRIVMSG'
- and $e<params>[1] ~~ /:i ^ "$!nick" <[,:]> \s+/
+ and $e<params>[1] ~~ /:i ^ "$.nick" <[,:]> \s+/
)
) {
for @!plugs.grep(*.^can: 'irc-addressed') -> $p {
my $res = $p.irc-notice-me(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
}
my $cmd = 'irc-' ~ $e<command>.lc;
for @!plugs.grep(*.^can: $cmd) -> $p {
my $res = $p."$cmd"(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
for @!plugs.grep(*.^can: 'irc-unhandled') -> $p {
my $res = $p.irc-unhandled(self, $e);
- next EVENTS unless $res === IRC_NOT_HANDLED;
+ return unless $res === IRC_NOT_HANDLED;
}
}