diff options
author | Zoffix Znet <zoffixznet@users.noreply.github.com> | 2015-12-21 09:52:42 -0500 |
---|---|---|
committer | Zoffix Znet <zoffixznet@users.noreply.github.com> | 2015-12-21 09:52:42 -0500 |
commit | 3be97d3be43be4a40737fa21c033763b29361bea (patch) | |
tree | 5ba5df3fcbeb2f640f2dcc9373f2a730a0f80fdc /lib/IRC | |
parent | b8035c1c7840ecb3f6ee2fd46560f41589d846aa (diff) | |
parent | c525790efcc8f1b45c3d236a08da1f8ed122fc9b (diff) |
Merge pull request #2 from zoffixznet/bleed
Bleed v 2.001001
Diffstat (limited to 'lib/IRC')
-rw-r--r-- | lib/IRC/Client.pm6 | 68 | ||||
-rw-r--r-- | lib/IRC/Client/Plugin.pm6 | 3 | ||||
-rw-r--r-- | lib/IRC/Client/Plugin/Debugger.pm6 | 10 | ||||
-rw-r--r-- | lib/IRC/Client/Plugin/PingPong.pm6 | 12 | ||||
-rw-r--r-- | lib/IRC/Grammar.pm6 | 2 | ||||
-rw-r--r-- | lib/IRC/Grammar/Actions.pm6 | 2 | ||||
-rw-r--r-- | lib/IRC/Parser.pm6 | 2 |
7 files changed, 70 insertions, 29 deletions
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index 55d4262..65f80e7 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -1,8 +1,8 @@ use v6; use IRC::Parser; # parse-irc use IRC::Client::Plugin::PingPong; -role IRC::Client::Plugin { ... } -class IRC::Client:ver<1.001001> { +use IRC::Client::Plugin; +class IRC::Client:ver<2.001001> { has Bool:D $.debug = False; has Str:D $.host = 'localhost'; has Int:D $.port where 0 <= $_ <= 65535 = 6667; @@ -16,24 +16,59 @@ class IRC::Client:ver<1.001001> { 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("NICK $!nick\n"); $.ssay("USER $!username $!userhost $!host :$!userreal\n"); $.ssay("JOIN $_\n") for @!channels; - Supply.interval( .interval ).tap({ $OUTER::_.interval(self) }) - for @!plugins.grep(*.interval); + .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected'); react { whenever $!sock.Supply -> $str is copy { - $!debug and $str.say; - my $messages = parse-irc $str; - for @$messages -> $message { - .msg(self, $message) - for (@!plugins-essential, @!plugins).flat.grep(*.msg); + $!debug and "[server {DateTime.now}] {$str}".put; + my $events = parse-irc $str; + EVENTS: for @$events -> $e { + $e<pipe> = {}; + + for @!plugs.grep(*.^can: 'irc-all-events') -> $p { + my $res = $p.irc-all-events(self, $e); + next EVENTS 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; + } + } + + 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; + } + } + + 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; + } + + for @!plugs.grep(*.^can: 'irc-unhandled') -> $p { + my $res = $p.irc-unhandled(self, $e); + next EVENTS unless $res === IRC_NOT_HANDLED; + } } } } @@ -44,14 +79,25 @@ class IRC::Client:ver<1.001001> { } method ssay (Str:D $msg) { + $!debug and "{plug-name}$msg".put; $!sock.print("$msg\n"); self; } method privmsg (Str $who, Str $what) { my $msg = ":$!nick!$!username\@$!userhost PRIVMSG $who :$what\n"; - $!debug and say ".privmsg({$msg.subst("\n", "", :g)})"; - self.ssay: $msg; + $!debug and "{plug-name}$msg".put; + $!sock.print("$msg\n"); self; } } + +sub plug-name { + my $plug = callframe(3).file; + my $cur = $?FILE; + return '[core] ' if $plug eq $cur; + $cur ~~ s/'.pm6'$//; + $plug ~~ s:g/^ $cur '/' | '.pm6'$//; + $plug ~~ s/'/'/::/; + return "[$plug] "; +} diff --git a/lib/IRC/Client/Plugin.pm6 b/lib/IRC/Client/Plugin.pm6 new file mode 100644 index 0000000..83703d7 --- /dev/null +++ b/lib/IRC/Client/Plugin.pm6 @@ -0,0 +1,3 @@ +constant IRC_HANDLED = "irc plugin handled \x1"; +constant IRC_NOT_HANDLED = "irc plugin not-handled \x2"; +unit class IRC::Client::Plugin:ver<2.001001>; diff --git a/lib/IRC/Client/Plugin/Debugger.pm6 b/lib/IRC/Client/Plugin/Debugger.pm6 index 5966ca0..c959cc2 100644 --- a/lib/IRC/Client/Plugin/Debugger.pm6 +++ b/lib/IRC/Client/Plugin/Debugger.pm6 @@ -1,8 +1,8 @@ -use v6; use Data::Dump; -unit class IRC::Client::Plugin::Debugger:ver<1.001001>; +use IRC::Client::Plugin; +unit class IRC::Client::Plugin::Debugger:ver<2.001001> is IRC::Client::Plugin; -multi method msg () { True } -multi method msg ($irc, $msg) { - say Dump $msg, :indent(4); +method irc-all-events ($irc, $e) { + say Dump $e, :indent(4); + return IRC_NOT_HANDLED; } diff --git a/lib/IRC/Client/Plugin/PingPong.pm6 b/lib/IRC/Client/Plugin/PingPong.pm6 index 7ba977d..b499051 100644 --- a/lib/IRC/Client/Plugin/PingPong.pm6 +++ b/lib/IRC/Client/Plugin/PingPong.pm6 @@ -1,10 +1,2 @@ -use v6; -unit class IRC::Client::Plugin::PingPong:ver<1.001001>; - -multi method msg () { True } -multi method msg ($irc, $msg) { - return unless $msg<command> eq 'PING'; - my $res = "PONG {$irc.nick} $msg<params>[0]"; - $irc.debug and say $res; - $irc.ssay($res); -} +unit class IRC::Client::Plugin::PingPong:ver<2.001001>; +method irc-ping ($irc, $e) { $irc.ssay("PONG {$irc.nick} $e<params>[0]") } diff --git a/lib/IRC/Grammar.pm6 b/lib/IRC/Grammar.pm6 index d26a3dc..241c497 100644 --- a/lib/IRC/Grammar.pm6 +++ b/lib/IRC/Grammar.pm6 @@ -1,4 +1,4 @@ -unit grammar IRC::Grammar:ver<1.001001>; +unit grammar IRC::Grammar:ver<2.001001>; token TOP { <message>+ } token SPACE { ' '+ } token message { [':' <prefix> <SPACE> ]? <command> <params> \n } diff --git a/lib/IRC/Grammar/Actions.pm6 b/lib/IRC/Grammar/Actions.pm6 index 74a8f11..469f8f8 100644 --- a/lib/IRC/Grammar/Actions.pm6 +++ b/lib/IRC/Grammar/Actions.pm6 @@ -1,4 +1,4 @@ -unit class IRC::Grammar::Actions:ver<1.001001>; +unit class IRC::Grammar::Actions:ver<2.001001>; method TOP ($/) { $/.make: $<message>>>.made } method message ($/) { my $pref = $/<prefix>; diff --git a/lib/IRC/Parser.pm6 b/lib/IRC/Parser.pm6 index aa87fa7..77de768 100644 --- a/lib/IRC/Parser.pm6 +++ b/lib/IRC/Parser.pm6 @@ -1,6 +1,6 @@ use IRC::Grammar; use IRC::Grammar::Actions; -unit class IRC::Parser:ver<1.001001>; +unit class IRC::Parser:ver<2.001001>; sub parse-irc (Str:D $input) is export { IRC::Grammar.parse($input, actions => IRC::Grammar::Actions).made // []; |