forked from jmacarthur/utah-teabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utah.pl
executable file
·147 lines (138 loc) · 3.45 KB
/
utah.pl
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
#!/usr/bin/perl -w
use Data::Dumper;
use DateTime;
use JSON;
# To use this you need: apt-get install libbot-basicbot-perl libpoe-component-sslify-perl libjson-perl libdatetime-perl
package TeaBot;
use base qw(Bot::BasicBot);
my @teas = ();
my $config = {};
my $helptext = 'Try: "utah: Earl grey, 300, 5 minutes"';
sub saveConfig()
{
$json_text = JSON::encode_json( $config );
open(my $fh, '>', "config.json");
print $fh $json_text;
close($fh);
}
sub said
{
my ($self, $message) = @_;
print "Message from $message->{'who'}: $message->{'body'}\n";
if ($message->{'address'}) {
my $msg = $message->{'body'};
chomp($msg);
if (lc($msg) eq "quit") {
$self->shutdown( "kbai");
}
elsif ($msg =~ /^report to #(\S+)/i) {
$config->{reportChans}->{$1} = 1;
saveConfig();
}
elsif ($msg =~ /^(no|do not) report to #(\S+)/i) {
$config->{reportChans}->{$2} = 0;
saveConfig();
}
elsif ($msg =~ /^cancel/i) {
@teas = ();
return "OK, I have cancelled all tea notifications.";
}
elsif ($msg =~ /,/) {
my @fields = split(/,/, $msg);
my $location = "an unknown location";
my $delay = -1;
my $tea = "Mystery tea";
my $brewer = $message->{'who'};
for my $f(@fields) {
chomp($f);
if($f =~ /(\d+)\s*(m|min|mins)/i) {
$delay = $1;
}
elsif($f =~ /now/i) {
$delay = 0;
}
elsif($f =~ /(300|302|101|jeff|geoff|sean|shaun)/i) {
$location = $1;
}
else {
$tea = $f;
}
}
if($delay == -1) {
return "Please tell me how many minutes you want to wait (e.g. 5m)"
}
print "$tea ready in $delay minutes in $location\n";
my $dt = DateTime->now;
$dt->add(minutes=>$delay);
chomp($tea);
if($tea =~ /^AF$/i || $tea =~ /arctic fire/i) {
$tea = "nasty fruit tea";
}
push @teas, [$dt ,$tea, $location, $brewer];
return "OK";
}
elsif ($msg eq 'brewers' or $msg =~ /^who(( i)|')s (making|brewing)( tea)?\??$/i) {
local $" = ', ';
my @brewers = sort keys %{{map { $_->[3] => 1 } @teas}};
return scalar @brewers > 0 ? "@brewers" : 'No tea is brewing at present.';
}
else {
return "I don't understand. $helptext";
}
}
return undef;
}
sub tick
{
my ($self) = @_;
print "tick\n";
my @newTeas = ();
for my $t (@teas) {
my $res = DateTime->compare($t->[0], DateTime->now());
print "Checking the $t->[1], which is ready at $t->[0]: $res\n";
if($res < 1) {
while (my ($k,$v) = each %{$config->{reportChans}}) {
if($v == 1) {
my $tea = ucfirst($t->[1]);
$self->say(channel => "#$k", body=>"$tea now ready in $t->[2]. Thanks $t->[3]!");
}
}
} else {
push @newTeas, $t;
}
}
@teas = @newTeas;
return 5;
}
# help text for the bot
sub help { $helptext; }
if( -f "config.json") {
print "Loading config.json\n";
my $json_text = "";
open(my $fh, '<', "config.json");
$json_text = join("", <$fh>);
close($fh);
$config = JSON::decode_json($json_text);
}
else {
$config->{reportChans}->{"bottest"} = 1;
}
my @chans = ();
while (my ($k,$v) = each %{$config->{reportChans}}) {
if($v == 1) {
push @chans, "#$k";
}
}
my $bot = TeaBot->new(
server => "irc0.codethink.co.uk",
channels => \@chans,
nick => $config->{nick},
username => $config->{username},
password => $config->{password},
name => "Utah Teabot",
ssl => 1,
port => 6697,
ignore_list => [qw(marvin medibot)],
);
$bot->{IRCNAME} = "utah";
$bot->run();