diff options
-rw-r--r-- | examples/bot.pl6 | 4 | ||||
-rw-r--r-- | lib/IRC/Client.pm6 | 31 | ||||
-rw-r--r-- | lib/IRC/Client/Plugin/Debugger.pm6 | 16 | ||||
-rw-r--r-- | lib/IRC/Grammar.pm6 | 21 | ||||
-rw-r--r-- | lib/IRC/Grammar/Actions.pm6 | 30 | ||||
-rw-r--r-- | lib/IRC/Parser.pm6 | 7 | ||||
-rw-r--r-- | test.p6 | 28 |
7 files changed, 74 insertions, 63 deletions
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 { <message> } +token TOP { <message>+ } token SPACE { ' '+ } token message { [':' <prefix> <SPACE> ]? <command> <params> \n } token prefix { @@ -19,21 +18,3 @@ token message { [':' <prefix> <SPACE> ]? <command> <params> \n } token letter { <[a..zA..Z]> } token number { <[0..9]> } token special { <[-\[\]\\`^{}]> } - -# unit class IRC::Grammar::Actions:ver<1.001001>; -# method TOP ($/) { $/.make: $<message>».made } -# method message ($/) { $/.make: -# prefix => $<prefix> .made, -# command => $<command>.made, -# params => $<params .made, -# } -# method prefix ($/) { [<servername> | <nick>] [ '!' <user> ]? [ '@' <host> ]? } -# method command ($/) { <letter>+ | <number>**3 } -# method params ($/) { ' '+ [ ':' <trailing> | <middle> <params> ]? } -# method middle ($/) { <-[: \0\r\n]> <-[ \0\r\n]>+ } -# method trailing ($/) { <-[\0\r\n]> } -# -# method class ($/) { $/.make: ~$/ } -# method rules ($/) { $/.make: ~$/ } -# method pair ($/) { $/.make: $<class>.made => $<rules>.made } -# method TOP ($/) { $/.make: $<pair>».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: $<class>.made => $<rules>.made } - method TOP ($/) { $/.make: $<pair>».made } +unit class IRC::Grammar::Actions:ver<1.001001>; +method TOP ($/) { $/.make: $<message>>>.made } +method message ($/) { + my $pref = $/<prefix>; + my %args = command => ~$/<command>; + if ( $pref<servername>.defined ) { + %args<who><host> = ~$pref<servername>; + } + else { + %args<who><nick user host> = $pref<nick user host>».Str; + } + + my $p = $/<params>; + loop { + if ( $p<middle>.defined ) { + %args<params>.append: ~$p<middle>; + } + if ( $p<trailing>.defined ) { + %args<params>.append: ~$p<trailing>; + last; + } + $p = $p<params>; + } + + $/.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 // []; +} @@ -1,31 +1,11 @@ use v6; grammar IRC::Grammar:ver<1.001001> { - token TOP { <message>+ } - token SPACE { ' '+ } - token message { [':' <prefix> <SPACE> ]? <command> <params> \n } - token prefix { - [ <servername> || <nick> ['!' <user>]? ['@' <host>]? ] - <before <SPACE>> - } - token servername { <host> } - token nick { <letter> [ <letter> | <number> | <special> ]* } - token user { <-[\ \0\r\n]>+? <before [<SPACE> | '@']>} - token host { <-[\s!@]>+ } - token command { <letter>+ | <number>**3 } - token params { <SPACE>* [ ':' <trailing> | <middle> <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: $<message>>>.made } -} - -say IRC::Grammar.parse(":verne.freenode.net 372 Perl6IRC :- running for their sustained support.\r\n"); +say $res[0] =finish |