aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZoffix Znet <cpan@zoffix.com>2016-01-03 12:37:27 -0500
committerZoffix Znet <cpan@zoffix.com>2016-01-03 12:37:27 -0500
commitf5d28c34ffc14045bc49cd3cb22af108d23886bd (patch)
treefae964d431fc82eb77e0588179735740cc52c4a1 /lib
parent83c4503293f96dac4ec36235605cdde38f9252d6 (diff)
Add .respond method
Diffstat (limited to 'lib')
-rw-r--r--lib/IRC/Client.pm6128
1 files changed, 72 insertions, 56 deletions
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6
index cbdb335..41bb45d 100644
--- a/lib/IRC/Client.pm6
+++ b/lib/IRC/Client.pm6
@@ -20,62 +20,6 @@ has @.plugins-essential = [
];
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("PASS $!password\n") if $!password.defined;
- $.ssay("NICK $!nick\n");
- $.ssay("USER $!username $!username $!host :$!userreal\n");
- $.ssay("JOIN {@!channels[]}\n");
-
- .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected');
-
- # my $left-overs = '';
- react {
- whenever $!sock.Supply :bin -> $buf is copy {
- my $str = try $buf.decode: 'utf8';
- $str or $str = $buf.decode: 'latin-1';
- # $str ~= $left-overs;
- $!debug and "[server {DateTime.now}] {$str}".put;
- my $events = parse-irc $str;
- for @$events -> $e {
- self.handle-event: $e;
- CATCH { warn .backtrace }
- }
- }
-
- CATCH { warn .backtrace }
- }
-
- say "Closing connection";
- $!sock.close;
-
- # CATCH { warn .backtrace }
- });
-}
-
-method ssay (Str:D $msg) {
- $!debug and "{plug-name}$msg".put;
- $!sock.print("$msg\n");
- self;
-}
-
-method privmsg (Str $who, Str $what) {
- my $msg = "PRIVMSG $who :$what\n";
- $!debug and "{plug-name}$msg".put;
- $!sock.print("$msg\n");
- self;
-}
-
-method notice (Str $who, Str $what) {
- my $msg = "NOTICE $who :$what\n";
- $!debug and "{plug-name}$msg".put;
- $!sock.print("$msg\n");
- self;
-}
-
method handle-event ($e) {
$e<pipe> = {};
@@ -128,6 +72,78 @@ method handle-event ($e) {
}
}
+method notice (Str $who, Str $what) {
+ my $msg = "NOTICE $who :$what\n";
+ $!debug and "{plug-name}$msg".put;
+ $!sock.print("$msg\n");
+ self;
+}
+
+method privmsg (Str $who, Str $what) {
+ my $msg = "PRIVMSG $who :$what\n";
+ $!debug and "{plug-name}$msg".put;
+ $!sock.print("$msg\n");
+ self;
+}
+
+method respond (
+ Str:D :$how = 'privmsg',
+ Str:D :$where is required,
+ Str:D :$what is required is copy,
+ Str:D :$who,
+) {
+ $what = "$who, $what" if $who and $where ~~ /^<[#&]>/;
+ my $method = $how.fc eq 'PRIVMSG'.fc ?? 'privmsg'
+ !! $how.fc eq 'NOTICE'.fc ?? 'notice'
+ !! fail 'Unknown :$how specified. Use PRIVMSG or NOTICE';
+
+ self."$method"($where, $what);
+}
+
+method run {
+ .irc-start-up: self for @!plugs.grep(*.^can: 'irc-start-up');
+
+ await IO::Socket::Async.connect( $!host, $!port ).then({
+ $!sock = .result;
+ $.ssay("PASS $!password\n") if $!password.defined;
+ $.ssay("NICK $!nick\n");
+ $.ssay("USER $!username $!username $!host :$!userreal\n");
+ $.ssay("JOIN {@!channels[]}\n");
+
+ .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected');
+
+ # my $left-overs = '';
+ react {
+ whenever $!sock.Supply :bin -> $buf is copy {
+ my $str = try $buf.decode: 'utf8';
+ $str or $str = $buf.decode: 'latin-1';
+ # $str ~= $left-overs;
+ $!debug and "[server {DateTime.now}] {$str}".put;
+ my $events = parse-irc $str;
+ for @$events -> $e {
+ self.handle-event: $e;
+ CATCH { warn .backtrace }
+ }
+ }
+
+ CATCH { warn .backtrace }
+ }
+
+ say "Closing connection";
+ $!sock.close;
+
+ # CATCH { warn .backtrace }
+ });
+}
+
+method ssay (Str:D $msg) {
+ $!debug and "{plug-name}$msg".put;
+ $!sock.print("$msg\n");
+ self;
+}
+
+#### HELPER SUBS
+
sub plug-name {
my $plug = callframe(3).file;
my $cur = $?FILE;