diff options
author | Zoffix Znet <cpan@zoffix.com> | 2016-06-03 07:01:14 -0400 |
---|---|---|
committer | Zoffix Znet <cpan@zoffix.com> | 2016-06-03 07:01:14 -0400 |
commit | 0daa494480f7abe37a6e593c6238811009b7b914 (patch) | |
tree | 5916cb48581e9478c8b92bb458589940f6fa814e /lib/IRC/Client.pm6 | |
parent | 495050a801901460fbd6a9c525216612b0d2f470 (diff) |
Start rewrite
Diffstat (limited to 'lib/IRC/Client.pm6')
-rw-r--r-- | lib/IRC/Client.pm6 | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index e69de29..7c30c4a 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -0,0 +1,56 @@ +unit class IRC::Client; + +use IRC::Client::Grammar; +use IRC::Client::Grammar::Actions; + +has Str:D $.host = 'localhost'; +has Bool $.debug = False; +has Str $.password; +has Int:D $.port where 0 <= $_ <= 65535 = 6667; +has Str:D $.nick = 'Perl6IRC'; +has Str:D $.username = 'Perl6IRC'; +has Str:D $.userhost = 'localhost'; +has Str:D $.userreal = 'Perl6 IRC Client'; +has Str:D @.channels = ['#perl6']; +has @.plugins; +has IO::Socket::Async $!sock; + +method run { + await IO::Socket::Async.connect( $!host, $!port ).then({ + $!sock = .result; + self!ssay: "PASS $!password\n" if $!password.defined; + self!ssay: "NICK $!nick\n"; + self!ssay: "USER $!username $!username $!host :$!userreal\n"; + + 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, $left-overs) = IRC::Client::Grammar.parse( + $str, actions => IRC::Client::Grammar::Actions + ).made; + + for @$events -> $e { + say "[event] $e"; + CATCH { warn .backtrace } + } + } + + CATCH { warn .backtrace } + } + + say "Closing connection"; + $!sock.close; + + # CATCH { warn .backtrace } + }); +} + +method !ssay (Str:D $msg) { + $!debug and "$msg".put; + $!sock.print("$msg\n"); + self; +} |