aboutsummaryrefslogtreecommitdiff
path: root/lib/IRC/Client/Grammar/Actions.pm6
blob: 3e190cdcddd1b90009b70d59ec3d5126f87c2272 (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
unit class IRC::Client::Grammar::Actions;

use IRC::Client::Message::Numeric;

has $.irc;
has $.server;

method TOP ($/) { $/.make: ($<message>ยป.made, $<left-overs>) }

method left-overs ($/) {
    $/.made: $/.defined ?? !$/ !! '';
}

method message ($/) {
    my %args;
    my $pref = $/<prefix>;
    for qw/nick user host/ {
        $pref{$_}.defined or next;
        %args<who>{$_} = ~$pref{$_};
    }
    %args<who><host> = ~$pref<servername> if $pref<servername>.defined;

    my $p = $<params>;

    loop {
        if ( $p<middle>.defined ) {
            %args<params>.append: ~$p<middle>;
        }
        if ( $p<trailing>.defined ) {
            %args<params>.append: ~$p<trailing>;
            last;
        }
        $p = $p<params>;
    }

    my %msg-args =
        irc      => $!irc,
        nick     => %args<who><nick>,
        username => %args<who><user>,
        host     => %args<who><host>,
        usermask => "%args<who><nick>!%args<who><user>@%args<who><host>",
        server   => $!server;

    my $msg;
    given ~$<command> {
        when /^ ([0..9]**3) $/ {
            $msg = IRC::Client::Message::Numeric.new:
                :command( $<command> ),
                :args( %args<params> ),
                |%msg-args;
        }
    }

    $/.make: $msg;
}