diff options
author | Zoffix Znet <zoffixznet@users.noreply.github.com> | 2016-07-26 08:50:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-26 08:50:02 -0400 |
commit | e0478c07e2096d85e20764c08c83a3d16c002e94 (patch) | |
tree | 592510005886adaadb49848d289c5c712279ecee /lib/IRC/Client/Grammar | |
parent | e997c1b0b5ad796425abfc9f81b91947357172ce (diff) | |
parent | cc19189ff6b74bea5211d521a59dbff0c71a0749 (diff) |
Merge Rewrite 2.0 version into master
Old version should not be used anymore and 2.0 is ready to go, sans some bugs
Diffstat (limited to 'lib/IRC/Client/Grammar')
-rw-r--r-- | lib/IRC/Client/Grammar/Actions.pm6 | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/IRC/Client/Grammar/Actions.pm6 b/lib/IRC/Client/Grammar/Actions.pm6 new file mode 100644 index 0000000..b1fcc53 --- /dev/null +++ b/lib/IRC/Client/Grammar/Actions.pm6 @@ -0,0 +1,119 @@ +unit class IRC::Client::Grammar::Actions; + +use IRC::Client::Message; + +has $.irc; +has $.server; + +method TOP ($/) { + $/.make: ( + $<message>ยป.made, + ~( $<left-overs> // '' ), + ); +} + +method message ($match) { + my %args; + 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 = $match<params>; + loop { + %args<params>.append: ~$p<middle> if $p<middle>.defined; + + if ( $p<trailing>.defined ) { + %args<params>.append: ~$p<trailing>; + last; + } + last unless $p<params>.defined; + $p = $p<params>; + } + + my %msg-args = + command => $match<command>.uc, + args => %args<params>, + host => %args<who><host>//'', + irc => $!irc, + nick => %args<who><nick>//'', + server => $!server, + usermask => ~($match<prefix>//''), + username => %args<who><user>//''; + + my $msg; + given %msg-args<command> { + when /^ <[0..9]>**3 $/ { + $msg = IRC::Client::Message::Numeric.new: |%msg-args; + } + when 'JOIN' { + $msg = IRC::Client::Message::Join.new: + :channel( %args<params>[0] ), + |%msg-args; + } + when 'PART' { + $msg = IRC::Client::Message::Part.new: + :channel( %args<params>[0] ), + |%msg-args; + } + when 'NICK' { + $msg = IRC::Client::Message::Nick.new: + :new-nick( %args<params>[0] ), + |%msg-args; + } + when 'NOTICE' { $msg = msg-notice %args, %msg-args } + when 'MODE' { $msg = msg-mode %args, %msg-args } + when 'PING' { $msg = IRC::Client::Message::Ping.new: |%msg-args } + when 'PRIVMSG' { $msg = msg-privmsg %args, %msg-args } + when 'QUIT' { $msg = IRC::Client::Message::Quit.new: |%msg-args } + default { $msg = IRC::Client::Message::Unknown.new: |%msg-args } + } + + $match.make: $msg; +} + +sub msg-privmsg (%args, %msg-args) { + %args<params>[0] ~~ /^<[#&]>/ + and return IRC::Client::Message::Privmsg::Channel.new: + :channel( %args<params>[0] ), + :text( %args<params>[1] ), + |%msg-args; + + return IRC::Client::Message::Privmsg::Me.new: + :text( %args<params>[1] ), + |%msg-args; +} + +sub msg-notice (%args, %msg-args) { + %args<params>[0] ~~ /^<[#&]>/ + and return IRC::Client::Message::Notice::Channel.new: + :channel( %args<params>[0] ), + :text( %args<params>[1] ), + |%msg-args; + + return IRC::Client::Message::Notice::Me.new: + :text( %args<params>[1] ), + |%msg-args; +} + +sub msg-mode (%args, %msg-args) { + if %args<params>[0] ~~ /^<[#&]>/ { + my @modes; + for %args<params>[1..*-1].join.comb: /\S/ { + state $sign; + /<[+-]>/ and $sign = $_ and next; + @modes.push: $sign => $_; + }; + return IRC::Client::Message::Mode::Channel.new: + :channel( %args<params>[0] ), + :modes( @modes ), + |%msg-args; + } + else { + return IRC::Client::Message::Mode::Me.new: + :modes( %args<params>[1..*-1].join.comb: /<[a..zA..Z]>/ ), + |%msg-args; + } +} |