summaryrefslogtreecommitdiff
path: root/boha.raku
blob: 97240ba4dcb140cb9aefd82d85ee16901bf2eb84 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env rakudo 
use v6.d;
use Config::TOML;
use IRC::Client;
 
my $config = from-toml(
    file => (%*ENV<BOHA_CONFIG_FILE> // $?FILE.IO.sibling('boha.toml').Str)
);
 
role TracksOps does IRC::Client::Plugin {
    has %!nick-cache;
    has $!waiting-for-names = False;
 
    method !check-op($e{
        return Promise.broken('not a channel message'unless $e.?channel;
        # if we already know whether that's an op, the cache will 
        # contain a kept promise; if we don't, we have to wait for the 
        # next 353, so we set and return a planned promise 
        return %!nick-cache{$e.channel}{$e.nick} //= Promise.new();
    }
 
    # response to /NAMES 
    method irc-n353($e{
        my ($my-nick,$equal,$channel,$names= $e.args();
 
        for $names.split(/\s+/-> $name-str {
            my $user = $name-str ~~ / ^ $<sigil> = [ '@' | '+' ]? $<nick> = [ .+ ] $ /;
 
            # I'm not sure what `+` means 
            my $is-op = $user<sigil> eq '@';
 
            my $value := %!nick-cache{$channel}{$user<nick>};
            if $value ~~ Promise && $value.status ~~ Planned {
                # someone called check-op before we saw this nick: 
                # keep the promise 
                $value.keep($is-op);
            }
            else {
                # either this nick changed state, or nobody asked 
                # about it before: set a kept promise 
                $value = Promise.kept($is-op);
            }
        }
 
        $!waiting-for-names = False;
 
        return $.NEXT;
    }
 
    method irc-join($e{
        # someone (maybe us) joined the channel: update our ops map 
        # 
        # maybe we should do this every few seconds anyway… 
        Promise.in(5).then: {
            unless ($!waiting-for-names{
                $e.irc.send-cmd('NAMES', $e.channel:server($e.server));
                $!waiting-for-names = True;
            }
        };
        return $.NEXT;
    }
}
 
class Boha1 does TracksOps {
    # irc-addressed for in-channel messages 
    # irc-privmsg-me for direct messages 
 
    method irc-addressed($e{
        return self!check-op($e).then: sub ($promise) {
            if ($promise.status ~~ Broken{
                # not a channel message? 
                return Nil;
            }
            if (so $promise.result{
                return "you're an op";
            }
            else {
                return "you're a normal user";
            }
        };
    }
}
 
.run with IRC::Client.new(
    |($config<server>),
    channels => $config<channels>.map(*.<name>),
    :debug,
    :plugins(
        Boha1.new,
    ),
);