aboutsummaryrefslogtreecommitdiff
path: root/lib/IRC/Client.pm6
blob: 3fca5aeed0f3acd431346f490a3c32ae5caf22d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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        @.servers;
has IO::Socket::Async   $!sock;

method run {
    await IO::Socket::Async.connect( $!host, $!port ).then({
        $!sock = .result;
        self!ssay: "PASS $!password" if $!password.defined;
        self!ssay: "NICK $!nick";
        self!ssay: "USER $!username $!username $!host :$!userreal";

        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;

                (my $events, $left-overs) = self!parse: $str;
                for $events.grep: *.defined -> $e {
                    $!debug and debug-print $e;
                    CATCH { warn .backtrace }
                }
            }

            CATCH { warn .backtrace }
        }
        $!sock.close;
    });
}

method !ssay (Str:D $msg) {
    $!debug and debug-print $msg;
    $!sock.print("$msg\n");
    self;
}

method !parse (Str:D $str) {
    return IRC::Client::Grammar.parse(
        $str,
        actions => IRC::Client::Grammar::Actions.new(
            irc    => self,
            server => 'dummy',
        ),
    ).made;
}

sub debug-print ($str, $dir where * eq 'in' | 'out') {
    state $color = try {
        require Terminal::ANSIColor;
        $color = GLOBAL::Terminal::ANSIColor::EXPORT::DEFAULT::<&color>;
    } // sub (Str $s) { '' };

    put ( $dir eq 'in'
        ?? $color('bold blue' ) ~ '▬▬▬▶ '
        !! $color('bold green') ~ '◀▬▬▬ '
    ) ~ $color('bold red') ~ join $color('reset'), $str.split: ' ', 2;
}