From ba793ec6d1d4c66300f40cbced25f9ffd23ec792 Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sun, 6 Dec 2015 15:05:29 -0500 Subject: Debug --- examples/bot.pl6 | 4 +++- lib/IRC/Client.pm6 | 31 ++++++++++++++++++------------- lib/IRC/Client/Plugin/Debugger.pm6 | 16 ++++++++++++++++ lib/IRC/Grammar.pm6 | 21 +-------------------- lib/IRC/Grammar/Actions.pm6 | 30 +++++++++++++++++++++++++----- lib/IRC/Parser.pm6 | 7 +++++++ test.p6 | 28 ++++------------------------ 7 files changed, 74 insertions(+), 63 deletions(-) create mode 100644 lib/IRC/Client/Plugin/Debugger.pm6 create mode 100644 lib/IRC/Parser.pm6 diff --git a/examples/bot.pl6 b/examples/bot.pl6 index 3bd14cc..b410288 100644 --- a/examples/bot.pl6 +++ b/examples/bot.pl6 @@ -1,12 +1,14 @@ use v6; use lib 'lib'; use IRC::Client; +use IRC::Client::Plugin::Debugger; # use IRC::Client::Plugin::HNY; my $irc = IRC::Client.new( - :host('irc.freenode.net'), + :host('10.10.11.12'), :debug, plugins => [ + IRC::Client::Plugin::Debugger.new # IRC::Client::Plugin::HNY.new, ] ).run; diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index 4635301..dcf0fcb 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -1,16 +1,18 @@ use v6; +use IRC::Parser; # parse-irc role IRC::Client::Plugin { ... } class IRC::Client:ver<1.001001> { - has Bool:D $.debug = False; - has Str:D $.host = 'localhost'; - 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 IRC::Client::Plugin @.plugins = []; + has Bool:D $.debug = False; + has Str:D $.host = 'localhost'; + 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 IRC::Client::Plugin @.plugins = []; + has IRC::Client::Plugin @.plugins-essential = []; method run { await IO::Socket::Async.connect( $!host, $!port ).then({ @@ -19,13 +21,16 @@ class IRC::Client:ver<1.001001> { $.ssay("USER $!username $!userhost $!host :$!userreal\n"); $.ssay("JOIN $_\n") for @!channels; - Supply.interval( .interval ).tap({ $OUTER::_.interval(self) }) - for @!plugins.grep(*.interval); + # Supply.interval( .interval ).tap({ $OUTER::_.interval(self) }) + # for @!plugins.grep(*.interval); react { whenever $!sock.Supply -> $str is copy { - "[$str]".perl.say; - .msg(self, $str) for @!plugins.grep(so *.msg); + $!debug and $str.say; + # say parse-irc($str).WHAT; + my $x = parse-irc $str; + @!plugins[0].msg(self, $x); + .msg(self, $x) for @!plugins.grep(*.msg); } } diff --git a/lib/IRC/Client/Plugin/Debugger.pm6 b/lib/IRC/Client/Plugin/Debugger.pm6 new file mode 100644 index 0000000..20a2283 --- /dev/null +++ b/lib/IRC/Client/Plugin/Debugger.pm6 @@ -0,0 +1,16 @@ +use v6; +use IRC::Client; +use IRC::Client::Plugin; +unit class IRC::Client::Plugin::Debugger:ver<1.001001> does IRC::Client::Plugin; + +multi method msg () { True } +multi method msg ($irc, $msg) { + $msg.say; +} + +multi method interval ( ) { 6 } +multi method interval (IRC::Client $irc) { + $irc.privmsg( + $irc.channels[0], "5 seconds passed. Time is now " ~ now + ); +} diff --git a/lib/IRC/Grammar.pm6 b/lib/IRC/Grammar.pm6 index 46d71cc..d26a3dc 100644 --- a/lib/IRC/Grammar.pm6 +++ b/lib/IRC/Grammar.pm6 @@ -1,6 +1,5 @@ -# use Grammar::Debugger; unit grammar IRC::Grammar:ver<1.001001>; -token TOP { } +token TOP { + } token SPACE { ' '+ } token message { [':' ]? \n } token prefix { @@ -19,21 +18,3 @@ token message { [':' ]? \n } token letter { <[a..zA..Z]> } token number { <[0..9]> } token special { <[-\[\]\\`^{}]> } - -# unit class IRC::Grammar::Actions:ver<1.001001>; -# method TOP ($/) { $/.make: $».made } -# method message ($/) { $/.make: -# prefix => $ .made, -# command => $.made, -# params => $ | ] [ '!' ]? [ '@' ]? } -# method command ($/) { + | **3 } -# method params ($/) { ' '+ [ ':' | ]? } -# method middle ($/) { <-[: \0\r\n]> <-[ \0\r\n]>+ } -# method trailing ($/) { <-[\0\r\n]> } -# -# method class ($/) { $/.make: ~$/ } -# method rules ($/) { $/.make: ~$/ } -# method pair ($/) { $/.make: $.made => $.made } -# method TOP ($/) { $/.make: $».made } diff --git a/lib/IRC/Grammar/Actions.pm6 b/lib/IRC/Grammar/Actions.pm6 index 039f11e..7093b99 100644 --- a/lib/IRC/Grammar/Actions.pm6 +++ b/lib/IRC/Grammar/Actions.pm6 @@ -1,6 +1,26 @@ -class IRC::Grammar::Actions { - method class ($/) { $/.make: ~$/ } - method rules ($/) { $/.make: ~$/ } - method pair ($/) { $/.make: $.made => $.made } - method TOP ($/) { $/.make: $».made } +unit class IRC::Grammar::Actions:ver<1.001001>; +method TOP ($/) { $/.make: $>>.made } +method message ($/) { + my $pref = $/; + my %args = command => ~$/; + if ( $pref.defined ) { + %args = ~$pref; + } + else { + %args = $pref».Str; + } + + my $p = $/; + loop { + if ( $p.defined ) { + %args.append: ~$p; + } + if ( $p.defined ) { + %args.append: ~$p; + last; + } + $p = $p; + } + + $/.make: %args; } diff --git a/lib/IRC/Parser.pm6 b/lib/IRC/Parser.pm6 new file mode 100644 index 0000000..aa87fa7 --- /dev/null +++ b/lib/IRC/Parser.pm6 @@ -0,0 +1,7 @@ +use IRC::Grammar; +use IRC::Grammar::Actions; +unit class IRC::Parser:ver<1.001001>; + +sub parse-irc (Str:D $input) is export { + IRC::Grammar.parse($input, actions => IRC::Grammar::Actions).made // []; +} diff --git a/test.p6 b/test.p6 index dfc90f4..aa9e493 100644 --- a/test.p6 +++ b/test.p6 @@ -1,31 +1,11 @@ use v6; grammar IRC::Grammar:ver<1.001001> { - token TOP { + } - token SPACE { ' '+ } - token message { [':' ]? \n } - token prefix { - [ || ['!' ]? ['@' ]? ] - > - } - token servername { } - token nick { [ | | ]* } - token user { <-[\ \0\r\n]>+? | '@']>} - token host { <-[\s!@]>+ } - token command { + | **3 } - token params { * [ ':' | ]? } - token middle { <-[:\ \0\r\n]> <-[\ \0\r\n]>* } - token trailing { <-[\0\r\n]>* } - token letter { <[a..zA..Z]> } - token number { <[0..9]> } - token special { <[-\[\]\\`^{}]> } -} +my $res = IRC::Grammar.parse( +":ZoffixW!~ZoffixW\@unaffiliated/zoffix PRIVMSG #perl6bot :test\r\n" +, :actions(IRC::Grammar::Actions) ).made; -class IRC::Grammar::Actions{ - method TOP ($/) { $/.make: $>>.made } -} - -say IRC::Grammar.parse(":verne.freenode.net 372 Perl6IRC :- running for their sustained support.\r\n"); +say $res[0] =finish -- cgit v1.1