aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZoffix Znet <cpan@zoffix.com>2016-06-04 19:18:31 -0400
committerZoffix Znet <cpan@zoffix.com>2016-06-04 19:18:31 -0400
commit5a2196b2c8f2ac2eacb3ddaf40b3e75b9c38bb62 (patch)
tree0241ad573b1db26be38dbd6396041643c04113a8 /lib
parent791d7981cff0cd3a85fdd504f410a7c9833272dc (diff)
Stuff
Diffstat (limited to 'lib')
-rw-r--r--lib/IRC/Client.pm657
-rw-r--r--lib/IRC/Client/Grammar/Actions.pm674
-rw-r--r--lib/IRC/Client/Message.pm642
-rw-r--r--lib/IRC/Client/Message/Numeric.pm64
-rw-r--r--lib/IRC/Client/Message/Privmsg.pm64
-rw-r--r--lib/IRC/Client/Message/Privmsg/Channel.pm65
-rw-r--r--lib/IRC/Client/Message/Privmsg/Me.pm62
-rw-r--r--lib/IRC/Client/Message/Unknown.pm64
8 files changed, 158 insertions, 34 deletions
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6
index 3fca5ae..0c836a4 100644
--- a/lib/IRC/Client.pm6
+++ b/lib/IRC/Client.pm6
@@ -25,26 +25,46 @@ method run {
my $left-overs = '';
react {
+ CATCH { warn .backtrace }
+
whenever $!sock.Supply :bin -> $buf is copy {
my $str = try $buf.decode: 'utf8';
$str or $str = $buf.decode: 'latin-1';
$str ~= $left-overs;
(my $events, $left-overs) = self!parse: $str;
+ $str ~~ /$<left>=(\N*)$/;
+ dd $str;
+ say "#### SHOULD Left over: `$<left>`";
+ say "#### LEFT OVERS: `$left-overs`";
for $events.grep: *.defined -> $e {
- $!debug and debug-print $e;
CATCH { warn .backtrace }
+ $!debug and debug-print $e, 'in';
+ self!handle-event: $e;
}
}
-
- CATCH { warn .backtrace }
}
$!sock.close;
});
}
+method send-cmd ($cmd, *@args) {
+ @args[*-1] = ':' ~ @args[*-1];
+ self!ssay: join ' ', $cmd, @args;
+}
+
+method !handle-event ($e) {
+ given $e.command {
+ when '001' { self!ssay: "JOIN @.channels[]"; }
+ when 'PING' { $e.reply }
+ when 'JOIN' {
+ say "Joined channel $e.channel()";
+ }
+ }
+}
+
method !ssay (Str:D $msg) {
- $!debug and debug-print $msg;
+ $!debug and debug-print $msg, 'out';
$!sock.print("$msg\n");
self;
}
@@ -59,14 +79,31 @@ method !parse (Str:D $str) {
).made;
}
-sub debug-print ($str, $dir where * eq 'in' | 'out') {
- state $color = try {
+sub debug-print (Str(Any) $str, $dir where * eq 'in' | 'out') {
+ state $colored = try {
require Terminal::ANSIColor;
- $color = GLOBAL::Terminal::ANSIColor::EXPORT::DEFAULT::<&color>;
+ $colored = GLOBAL::Terminal::ANSIColor::EXPORT::DEFAULT::<&colored>;
} // sub (Str $s) { '' };
+ my @out;
+ if $str ~~ /^ '❚⚠❚'/ {
+ @out = $str.split: ' ', 3;
+ @out[0] = $colored(@out[0], 'bold white on_red');
+ @out[1] = @out[1] ~~ /^ <[0..9]>**3 $/
+ ?? $colored(@out[1], 'bold red')
+ !! $colored(@out[1], 'bold magenta');
+ @out[2] = $colored(@out[2], 'bold cyan');
+ }
+ else {
+ @out = $str.split: ' ', 2;
+ @out[0] = @out[0] ~~ /^ <[0..9]>**3 $/
+ ?? $colored(@out[0], 'bold red')
+ !! $colored(@out[0], 'bold magenta');
+ @out[1] = $colored(@out[1], 'bold cyan');
+ }
+
put ( $dir eq 'in'
- ?? $color('bold blue' ) ~ '▬▬▬▶ '
- !! $color('bold green') ~ '◀▬▬▬ '
- ) ~ $color('bold red') ~ join $color('reset'), $str.split: ' ', 2;
+ ?? $colored('▬▬▶ ', 'bold blue' )
+ !! $colored('◀▬▬ ', 'bold green')
+ ) ~ @out.join: ' ';
}
diff --git a/lib/IRC/Client/Grammar/Actions.pm6 b/lib/IRC/Client/Grammar/Actions.pm6
index 54943bd..6ebe33d 100644
--- a/lib/IRC/Client/Grammar/Actions.pm6
+++ b/lib/IRC/Client/Grammar/Actions.pm6
@@ -1,6 +1,6 @@
unit class IRC::Client::Grammar::Actions;
-use IRC::Client::Message::Numeric;
+use IRC::Client::Message;
has $.irc;
has $.server;
@@ -23,9 +23,8 @@ method message ($match) {
my $p = $match<params>;
loop {
- if ( $p<middle>.defined ) {
- %args<params>.append: ~$p<middle>;
- }
+ %args<params>.append: ~$p<middle> if $p<middle>.defined;
+
if ( $p<trailing>.defined ) {
%args<params>.append: ~$p<trailing>;
last;
@@ -35,22 +34,75 @@ method message ($match) {
}
my %msg-args =
+ command => $match<command>.uc,
+ args => %args<params>,
+ host => %args<who><host>//'',
irc => $!irc,
nick => %args<who><nick>//'',
- username => %args<who><user>//'',
- host => %args<who><host>//'',
- server => $!server;
+ server => $!server,
+ username => %args<who><user>//'';
.<usermask> = .<nick> ~ '!' ~ .<username> ~ '@' ~ .<host> given %msg-args;
my $msg;
- given ~$match<command> {
+ given %msg-args<command> {
when /^ $<command>=(<[0..9]>**3) $/ {
- $msg = IRC::Client::Message::Numeric.new:
- :command( ~$<command> ),
- :args( %args<params> ),
+ $msg = IRC::Client::Message::Numeric.new: |%msg-args;
+ }
+ when 'JOIN' {
+ $msg = IRC::Client::Message::Join.new:
+ :channel( %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 }
+ 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;
+ }
+}
diff --git a/lib/IRC/Client/Message.pm6 b/lib/IRC/Client/Message.pm6
index 69e7c82..4abb718 100644
--- a/lib/IRC/Client/Message.pm6
+++ b/lib/IRC/Client/Message.pm6
@@ -1,9 +1,33 @@
-unit role IRC::Client::Message;
-
-has $.irc is required;
-has Str:D $.nick is required;
-has Str:D $.username is required;
-has Str:D $.host is required;
-has Str:D $.usermask is required;
-has Str:D $.command is required;
-has Str:D $.server is required;
+unit package IRC::Client::Message;
+
+role IRC::Client::Message {
+ has $.irc is required;
+ has Str:D $.nick is required;
+ has Str:D $.username is required;
+ has Str:D $.host is required;
+ has Str:D $.usermask is required;
+ has Str:D $.command is required;
+ has Str:D $.server is required;
+ has @.args is required;
+
+ method Str { "$.command @.args[]" }
+}
+
+constant M = IRC::Client::Message;
+
+role Join does M { has $.channel; }
+role Notice does M { has $.text; }
+role Notice::Channel does Notice { has $.channel; }
+role Notice::Me does Notice { }
+role Mode does M { has @.modes; }
+role Mode::Channel does Mode { has $.channel; }
+role Mode::Me does Mode { }
+role Numeric does M { }
+role Privmsg does M { has $.text; }
+role Privmsg::Channel does Privmsg { has $.channel; }
+role Privmsg::Me does Privmsg { }
+role Unknown does M { method Str { "❚⚠❚ $.command @.args[]" } }
+
+role Ping does M {
+ method reply { $.irc.send-cmd: 'PONG', @.args; }
+}
diff --git a/lib/IRC/Client/Message/Numeric.pm6 b/lib/IRC/Client/Message/Numeric.pm6
index 38e9a26..c059eb4 100644
--- a/lib/IRC/Client/Message/Numeric.pm6
+++ b/lib/IRC/Client/Message/Numeric.pm6
@@ -1,6 +1,2 @@
use IRC::Client::Message;
unit role IRC::Client::Message::Numeric does IRC::Client::Message;
-
-has @.args;
-
-method Str { "$.command @.args[]" }
diff --git a/lib/IRC/Client/Message/Privmsg.pm6 b/lib/IRC/Client/Message/Privmsg.pm6
new file mode 100644
index 0000000..31efdea
--- /dev/null
+++ b/lib/IRC/Client/Message/Privmsg.pm6
@@ -0,0 +1,4 @@
+use IRC::Client::Message;
+unit role IRC::Client::Message::Privmsg does IRC::Client::Message;
+
+has $.what;
diff --git a/lib/IRC/Client/Message/Privmsg/Channel.pm6 b/lib/IRC/Client/Message/Privmsg/Channel.pm6
new file mode 100644
index 0000000..36cf3d6
--- /dev/null
+++ b/lib/IRC/Client/Message/Privmsg/Channel.pm6
@@ -0,0 +1,5 @@
+use IRC::Client::Message::Privmsg;
+unit role IRC::Client::Message::Privmsg::Channel
+ does IRC::Client::Message::Privmsg;
+
+has $.channel;
diff --git a/lib/IRC/Client/Message/Privmsg/Me.pm6 b/lib/IRC/Client/Message/Privmsg/Me.pm6
new file mode 100644
index 0000000..1ff31ba
--- /dev/null
+++ b/lib/IRC/Client/Message/Privmsg/Me.pm6
@@ -0,0 +1,2 @@
+use IRC::Client::Message::Privmsg;
+unit role IRC::Client::Message::Privmsg::Me does IRC::Client::Message::Privmsg;
diff --git a/lib/IRC/Client/Message/Unknown.pm6 b/lib/IRC/Client/Message/Unknown.pm6
new file mode 100644
index 0000000..91baa79
--- /dev/null
+++ b/lib/IRC/Client/Message/Unknown.pm6
@@ -0,0 +1,4 @@
+use IRC::Client::Message;
+unit role IRC::Client::Message::Unknown does IRC::Client::Message;
+
+method Str { "❚⚠❚ $.command @.args[]" }