aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes10
-rw-r--r--META.info2
-rw-r--r--lib/IRC/Client.pm660
-rw-r--r--lib/IRC/Client/Plugin.pm65
-rw-r--r--lib/IRC/Client/Plugin/Debugger.pm68
-rw-r--r--lib/IRC/Client/Plugin/PingPong.pm612
6 files changed, 72 insertions, 25 deletions
diff --git a/Changes b/Changes
index 2ea7f5e..e04841e 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,14 @@
Revision History for 'IRC::Client' Perl 6 Distribution
+1.002001 Unreleased
+ [Redesign plugin system]
+ - each IRC command can now be implemented with a specific method of
+ name 'irc_COMMAND'
+ - Added individual privmsg-me and notice-me methods
+ - Removed `interval` plugin methods.
+ - Added `register` plugin method that is called during client start up
+ [Other]
+ - Improved debug output
+
1.001001 2015-11-19
- First version released on an unsuspecting world
diff --git a/META.info b/META.info
index 39e6093..02c3eab 100644
--- a/META.info
+++ b/META.info
@@ -1,7 +1,7 @@
{
"perl" : "6.*",
"name" : "IRC::Client",
- "version" : "1.001001",
+ "version" : "1.002001",
"description" : "Extendable Internet Relay Chat client",
"depends" : [ "Data::Dump" ],
"test-depends" : [ "Test" ],
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6
index 55d4262..4e95aaf 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<1.002001> {
has Bool:D $.debug = False;
has Str:D $.host = 'localhost';
has Int:D $.port where 0 <= $_ <= 65535 = 6667;
@@ -16,6 +16,7 @@ class IRC::Client:ver<1.001001> {
has @.plugins-essential = [
IRC::Client::Plugin::PingPong.new
];
+ has @!plugs = [|@!plugins-essential, |@!plugins];
method run {
await IO::Socket::Async.connect( $!host, $!port ).then({
@@ -24,16 +25,44 @@ 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);
+ .register: self for @!plugs.grep(*.^can: 'register');
react {
whenever $!sock.Supply -> $str is copy {
- $!debug and $str.say;
+ $!debug and "[server {DateTime.now}] {$str}".put;
my $messages = parse-irc $str;
- for @$messages -> $message {
- .msg(self, $message)
- for (@!plugins-essential, @!plugins).flat.grep(*.msg);
+ MESSAGES: for @$messages -> $message {
+ $message<handled> = False;
+ $message<pipe> = {};
+
+ if ( $message<command> eq 'PRIVMSG'
+ and $message<params>[0] eq $!nick
+ ) {
+ for @!plugs.grep(*.^can: 'privmsg-me') -> $p {
+ my $res = $p.privmsg-me(self, $message);
+ next MESSAGES unless $res === irc-not-handled;
+ }
+ }
+
+ if ( $message<command> eq 'NOTICE'
+ and $message<params>[0] eq $!nick
+ ) {
+ for @!plugs.grep(*.^can: 'notice-me') -> $p {
+ my $res = $p.notice-me(self, $message);
+ next MESSAGES unless $res === irc-not-handled;
+ }
+ }
+
+ my $cmd = 'irc-' ~ $message<command>.lc;
+ for @!plugs.grep(*.^can: $cmd) -> $p {
+ my $res = $p."$cmd"(self, $message);
+ next MESSAGES unless $res === irc-not-handled;
+ }
+
+ for @!plugs.grep(*.^can: 'msg') -> $p {
+ my $res = $p.msg(self, $message);
+ next MESSAGES unless $res === irc-not-handled;
+ }
}
}
}
@@ -44,14 +73,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] ";
+} \ No newline at end of file
diff --git a/lib/IRC/Client/Plugin.pm6 b/lib/IRC/Client/Plugin.pm6
new file mode 100644
index 0000000..deaef50
--- /dev/null
+++ b/lib/IRC/Client/Plugin.pm6
@@ -0,0 +1,5 @@
+constant irc-handled = "irc plugin handled \x1";
+constant irc-not-handled = "irc plugin not-handled \x2";
+unit class IRC::Client::Plugin:ver<1.002002>;
+has $.irc;
+
diff --git a/lib/IRC/Client/Plugin/Debugger.pm6 b/lib/IRC/Client/Plugin/Debugger.pm6
index 5966ca0..4c6775c 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<1.002001> is IRC::Client::Plugin;
-multi method msg () { True }
-multi method msg ($irc, $msg) {
+method msg ($irc, $msg) {
say Dump $msg, :indent(4);
+ return irc-not-handled;
}
diff --git a/lib/IRC/Client/Plugin/PingPong.pm6 b/lib/IRC/Client/Plugin/PingPong.pm6
index 7ba977d..a85dc96 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<1.002001>;
+method irc-ping ($irc, $msg) { $irc.ssay("PONG {$irc.nick} $msg<params>[0]") }