aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/bot.pl619
-rw-r--r--lib/IRC/Client.pm628
-rw-r--r--lib/IRC/Client/Grammar/Actions.pm633
-rw-r--r--lib/IRC/Client/Message.pm614
-rw-r--r--lib/IRC/Client/Message/Numeric.pm62
5 files changed, 52 insertions, 44 deletions
diff --git a/examples/bot.pl6 b/examples/bot.pl6
index 94eb929..255a0f4 100644
--- a/examples/bot.pl6
+++ b/examples/bot.pl6
@@ -1,16 +1,9 @@
use v6;
use lib 'lib';
-use IRC::Client::Grammar;
-use IRC::Client::Grammar:Actions;
+use IRC::Client;
-say IRC::Client::Grammar.parse(
- 'PRIVMSG #perl6 :hello',
- actions => IRC::Client::Grammar::Actions.new,
-).made;
-
-# use IRC::Client;
-#
-# my $irc = IRC::Client.new(
-# :debug
-# :port<5667>
-# ).run;
+my $irc = IRC::Client.new(
+ :nick('IRCBot' ~ now.Int)
+ :debug
+ :port<5667>
+).run;
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6
index 7d8369a..3fca5ae 100644
--- a/lib/IRC/Client.pm6
+++ b/lib/IRC/Client.pm6
@@ -19,9 +19,9 @@ has IO::Socket::Async $!sock;
method run {
await IO::Socket::Async.connect( $!host, $!port ).then({
$!sock = .result;
- self!ssay: "PASS $!password\n" if $!password.defined;
- self!ssay: "NICK $!nick\n";
- self!ssay: "USER $!username $!username $!host :$!userreal\n";
+ self!ssay: "PASS $!password" if $!password.defined;
+ self!ssay: "NICK $!nick";
+ self!ssay: "USER $!username $!username $!host :$!userreal";
my $left-overs = '';
react {
@@ -31,10 +31,10 @@ method run {
$str ~= $left-overs;
(my $events, $left-overs) = self!parse: $str;
- # for @$events -> $e {
- # say "[event] $e";
- # CATCH { warn .backtrace }
- # }
+ for $events.grep: *.defined -> $e {
+ $!debug and debug-print $e;
+ CATCH { warn .backtrace }
+ }
}
CATCH { warn .backtrace }
@@ -44,7 +44,7 @@ method run {
}
method !ssay (Str:D $msg) {
- $!debug and "$msg".put;
+ $!debug and debug-print $msg;
$!sock.print("$msg\n");
self;
}
@@ -58,3 +58,15 @@ method !parse (Str:D $str) {
),
).made;
}
+
+sub debug-print ($str, $dir where * eq 'in' | 'out') {
+ state $color = try {
+ require Terminal::ANSIColor;
+ $color = GLOBAL::Terminal::ANSIColor::EXPORT::DEFAULT::<&color>;
+ } // sub (Str $s) { '' };
+
+ put ( $dir eq 'in'
+ ?? $color('bold blue' ) ~ '▬▬▬▶ '
+ !! $color('bold green') ~ '◀▬▬▬ '
+ ) ~ $color('bold red') ~ join $color('reset'), $str.split: ' ', 2;
+}
diff --git a/lib/IRC/Client/Grammar/Actions.pm6 b/lib/IRC/Client/Grammar/Actions.pm6
index 3e190cd..54943bd 100644
--- a/lib/IRC/Client/Grammar/Actions.pm6
+++ b/lib/IRC/Client/Grammar/Actions.pm6
@@ -5,23 +5,23 @@ use IRC::Client::Message::Numeric;
has $.irc;
has $.server;
-method TOP ($/) { $/.make: ($<message>».made, $<left-overs>) }
-
-method left-overs ($/) {
- $/.made: $/.defined ?? !$/ !! '';
+method TOP ($/) {
+ $/.make: (
+ $<message>».made,
+ ~( $<left-overs> // '' ),
+ );
}
-method message ($/) {
+method message ($match) {
my %args;
- my $pref = $/<prefix>;
+ my $pref = $match<prefix>;
for qw/nick user host/ {
$pref{$_}.defined or next;
%args<who>{$_} = ~$pref{$_};
}
%args<who><host> = ~$pref<servername> if $pref<servername>.defined;
- my $p = $<params>;
-
+ my $p = $match<params>;
loop {
if ( $p<middle>.defined ) {
%args<params>.append: ~$p<middle>;
@@ -30,26 +30,27 @@ method message ($/) {
%args<params>.append: ~$p<trailing>;
last;
}
+ last unless $p<params>.defined;
$p = $p<params>;
}
my %msg-args =
irc => $!irc,
- nick => %args<who><nick>,
- username => %args<who><user>,
- host => %args<who><host>,
- usermask => "%args<who><nick>!%args<who><user>@%args<who><host>",
+ nick => %args<who><nick>//'',
+ username => %args<who><user>//'',
+ host => %args<who><host>//'',
server => $!server;
+ .<usermask> = .<nick> ~ '!' ~ .<username> ~ '@' ~ .<host> given %msg-args;
my $msg;
- given ~$<command> {
- when /^ ([0..9]**3) $/ {
+ given ~$match<command> {
+ when /^ $<command>=(<[0..9]>**3) $/ {
$msg = IRC::Client::Message::Numeric.new:
- :command( $<command> ),
+ :command( ~$<command> ),
:args( %args<params> ),
|%msg-args;
}
}
- $/.make: $msg;
+ $match.make: $msg;
}
diff --git a/lib/IRC/Client/Message.pm6 b/lib/IRC/Client/Message.pm6
index 6670baa..69e7c82 100644
--- a/lib/IRC/Client/Message.pm6
+++ b/lib/IRC/Client/Message.pm6
@@ -1,9 +1,9 @@
unit role IRC::Client::Message;
-has $.irc;
-has $.nick;
-has $.username;
-has $.host;
-has $.usermask;
-has $.server;
-has $.command;
+has $.irc is required;
+has Str:D $.nick is required;
+has Str:D $.username is required;
+has Str:D $.host is required;
+has Str:D $.usermask is required;
+has Str:D $.command is required;
+has Str:D $.server is required;
diff --git a/lib/IRC/Client/Message/Numeric.pm6 b/lib/IRC/Client/Message/Numeric.pm6
index c57c3c2..38e9a26 100644
--- a/lib/IRC/Client/Message/Numeric.pm6
+++ b/lib/IRC/Client/Message/Numeric.pm6
@@ -2,3 +2,5 @@ use IRC::Client::Message;
unit role IRC::Client::Message::Numeric does IRC::Client::Message;
has @.args;
+
+method Str { "$.command @.args[]" }