/
Tellable.p6
executable file
·164 lines (144 loc) · 5.64 KB
/
Tellable.p6
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env perl6
# Copyright © 2019
# Aleks-Daniel Jakimenko-Aleksejev <alex.jakimenko@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use Whateverable;
use Whateverable::Bits;
use Whateverable::FootgunDB;
use Whateverable::Userlist;
use IRC::Client;
use JSON::Fast;
unit class Tellable does Whateverable does Whateverable::Userlist;
my $db-seen = FootgunDB.new: name => ‘tellable-seen’;
my $db-tell = FootgunDB.new: name => ‘tellable-tell’;
method help($msg) {
‘Like this: .tell AlexDaniel your bot is broken’
}
#| normalize nicknames, somewhat
sub normalize-weirdly($_ is copy) {
# We knowingly ignore CASEMAPPING and its bullshit rules.
# Instead we'll do our own crazy stuff in order to DWIM.
# These rules are based on messages that were never delivered.
# XXX not using s/// because there's a sub s (rakudo/rakudo#3111)
$_ .= fc;
s:!g/‘[m]’$//; # matrix users
s:!g/\W+$//; # garbage at the end
s:!g/^\W+//; # garbage at the beginning
s:g/‘-’//; # hyphens
s:g/‘_’//; # underscores
s:g/(.)$0+/$0/; # accidentally doubled characters
s:g/\d// if S:g/\d//.chars > 4; # remove numbers if we still have letters
$_;
}
sub guest-like($nick) { so $nick ~~ /^Guest\d/ }
#| listen for messages
multi method irc-privmsg-channel($msg) {
return $.NEXT if guest-like $msg.nick;
my $normalized = normalize-weirdly $msg.nick;
$db-seen.read-write: {
.{$normalized} = {
text => $msg.text,
channel => $msg.channel,
timestamp => timestampish,
nick => $msg.nick,
}
}
my %mail = $db-tell.read;
if %mail{$normalized} {
for %mail{$normalized}.list {
my $text = sprintf ‘%s %s <%s> %s’, .<timestamp channel from text>;
$msg.irc.send-cmd: 'PRIVMSG', $msg.channel, $text, :server($msg.server)
}
%mail{$normalized}:delete;
$db-tell.write: %mail;
}
$.NEXT
}
#| automatic tell
multi method irc-privmsg-channel($msg where { m:r/^ \s* $<who>=<.&irc-nick> ‘:’+ \s+ (.*) $/ }) {
my $who = $<who>;
return $.NEXT if self.userlist($msg){$who}; # still on the channel
my $normalized = normalize-weirdly $who;
my %seen := $db-seen.read;
return $.NEXT unless %seen{$normalized}:exists; # haven't seen them talk ever
my $last-seen-duration = DateTime.now(:0timezone) - DateTime.new(%seen{$normalized}<timestamp>);
return $.NEXT if $last-seen-duration ≥ 60×60×24 × 28 × 3; # haven't seen for months
$msg.text = ‘tell ’ ~ $msg.text;
self.irc-to-me: $msg;
}
#| .seen
multi method irc-privmsg-channel($msg where .args[1] ~~ /^ ‘.seen’ \s+ (.*) /) {
$msg.text = ~$0;
self.irc-to-me: $msg
}
#| .tell
multi method irc-privmsg-channel($msg where .args[1] ~~ /^ ‘.’[to|tell|ask] \s+ (.*) /) {
$msg.text = ~$0;
self.irc-to-me: $msg
}
sub did-you-mean-seen($who, %seen) {
did-you-mean $who, %seen.sort(*.value<timestamp>).reverse.map(*.key),
:max-distance(2)
}
#| seen
multi method irc-to-me($msg where { m:r/^ \s* [seen \s+]?
$<who>=<.&irc-nick> <[:,]>* \s* $/ }) {
my $who = ~$<who>;
my %seen := $db-seen.read;
my $entry = %seen{normalize-weirdly $who};
without $entry {
return ‘I haven't seen any guests around’ if guest-like $who;
return “I haven't seen $who around”
~ maybe ‘, did you mean %s?’, did-you-mean-seen $who, %seen
}
“I saw $who $entry<timestamp> in $entry<channel>: <$entry<nick>> $entry<text>”
}
#| tell
multi method irc-to-me($msg where { m:r/^ \s* [[to|tell|ask] \s+]?
$<who>=<.&irc-nick> <[:,]>* \s+ .* $/ }) {
my $who = ~$<who>;
my $normalized = normalize-weirdly $who;
return ‘Thanks for the message’ if $who eq $msg.server.current-nick;
return ‘I'll pass that message to your doctor’ if $who eq $msg.nick and not %*ENV<TESTABLE>;
my %seen := $db-seen.read;
without %seen{$normalized} {
return ‘Can't pass messages to guests’ if guest-like $who;
return “I haven't seen $who around”
~ maybe ‘, did you mean %s?’, did-you-mean-seen $who, %seen
}
$db-tell.read-write: {
.{$normalized}.push: {
text => $msg.text,
channel => $msg.channel,
timestamp => timestampish,
from => $msg.nick,
to => $who,
}
}
“I'll pass your message to {%seen{$normalized}<nick>}”
}
my %*BOT-ENV = %();
{
# Renormalize on startup in case the rules were updated
$db-tell.write: $db-tell.read.values».list.flat.classify: {
normalize-weirdly .<to>
};
$db-seen.write: %($db-seen.read.values.map: {
normalize-weirdly(.<nick>) => $_
});
}
Tellable.new.selfrun: ‘tellable6’, [/ [to|tell|ask|seen] 6? <before ‘:’> /,
fuzzy-nick(‘tellable6’, 1)];
# vim: expandtab shiftwidth=4 ft=perl6