forked from shaniqua/smeggdrop
/
bot.pl
310 lines (252 loc) · 8.22 KB
/
bot.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl
use strict;
use warnings;
use Config::Any;
use Carp::Always;
use Data::Dump qw/ddx/;
use 5.01;
use utf8;
use Data::Dumper;
use AnyEvent::IRC::Client;
use AnyEvent::Socket;
use AnyEvent::IRC::Util qw/prefix_nick prefix_user prefix_host/;
use lib 'lib';
use Shittybot::TCL;
my $config_stem = 'shittybot';
## anyevent stuff
my $cond = AnyEvent->condvar;
## config
my $config = Config::Any->load_stems({
use_ext => 1,
stems => [ $config_stem ],
})->[0] or die "Failed to read config file";
foreach my $config_file (values %{$config}) {
my $networks = $config_file->{networks};
die "Unable to find network configuration" unless $networks;
while (my ($net, $net_conf) = each %$networks) {
make_client($net_conf);
}
}
$cond->wait;
###########
sub make_client {
my ($conf) = @_;
my $client = new AnyEvent::IRC::Client::Pre;
# config
my $botnick = $conf->{nickname};
my $bindip = $conf->{bindip};
my $channels = $conf->{channels};
my $botreal = $conf->{realname};
my $botident = $conf->{username};
my $botserver = $conf->{address};
my $botport = $conf->{port} || 6667;
my $operuser = $conf->{operuser};
my $operpass = $conf->{operpass};
my $nickserv = $conf->{nickserv} || 'NickServ';
my $nickservpw = $conf->{nickpass};
my $state_directory = $conf->{state_directory};
# force array
$channels = [$channels] unless ref $channels;
# closures to be defined
my $init;
my $getNick;
my %states;
if (!$states{$state_directory}) {
my $tcl = Shittybot::TCL->spawn($state_directory, $client);
$states{$state_directory} = $tcl;
ddx($state_directory . " has a tcl set!");
print "Spawned TCL master for state $state_directory\n";
}
## getting it's nick back
$getNick = sub {
my $nick = shift;
# change your nick here
$client->send_srv('PRIVMSG' => $nickserv, "ghost $nick $nickservpw") if defined $nickservpw;
$client->send_srv('NICK', $nick);
};
## callbacks
my $conn = sub {
my ($client, $err) = @_;
return unless $err;
say "Can't connect: $err";
#keep reconecting
$client->{reconnects}{$botserver} = AnyEvent->timer(
after => 1,
interval => 10,
cb => sub {
$init->();
},
);
};
$client->reg_cb(connect => $conn);
$client->reg_cb(
registered => sub {
my $self = shift;
say "Registered on IRC server";
delete $client->{reconnects}{$botserver};
$client->enable_ping(60);
# oper up
if ($operuser && $operpass) {
$client->send_srv(OPER => $operuser, $operpass);
}
# save timer
$client->{_nickTimer} = AnyEvent->timer(
after => 10,
interval => 30, # NICK RECOVERY INTERVAL
cb => sub {
$getNick->($botnick) unless $client->is_my_nick($botnick);
},
);
},
disconnect => sub {
delete $client->{_nickTimer};
say "disconnected: $_[1]! trying to reconnect...";
#keep reconecting
$client->{reconnects}{$botserver} = AnyEvent->timer(
after => 1,
interval => 10,
cb => sub {
$init->();
},
);
},
part => sub {
my ($self, $nick, $channel, $is_myself, $msg) = @_;
return unless $nick eq $client->nick;
say "SAPart from $channel, rejoining";
$client->send_srv('JOIN', $channel);
},
kick => sub {
my ($self, $nick, $channel, $msg, $nick2) = @_;
return unless $nick eq $client->nick;
say "Kicked from $channel; rejoining";
$client->send_srv('JOIN', $channel);
# keep trying to rejoin
$client->{rejoins}{$channel} = AnyEvent->timer(
after => 10,
interval => 60,
cb => sub {
$client->send_srv('JOIN', $channel);
},
);
},
join => sub {
my ($self, $nick, $channel, $is_myself) = @_;
return unless $is_myself;
delete $client->{rejoins}{$channel};
},
nick_change => sub {
my ($self, $old_nick, $new_nick, $is_myself) = @_;
return unless $is_myself;
return if $new_nick eq $botnick;
$getNick->($botnick);
},
);
$client->ctcp_auto_reply ('VERSION', ['VERSION', 'Smeggdrop']);
# default crap
$client->reg_cb
(debug_recv =>
sub {
my ($self, $ircmsg) = @_;
#say dump($ircmsg);
if ($ircmsg->{command} eq '307') { # is a registred nick reply
# do something
}
});
## example of parsing
my $trigger = $conf->{trigger};
my $parse_privmsg = sub {
my ($self, $msg) = @_;
my $chan = $msg->{params}->[0];
my $from = $msg->{prefix};
if ($msg->{params}->[-1] =~ m/^!lol (.*)/) {
$client->send_chan($chan, 'PRIVMSG', $chan, "\001ACTION lol @ $1"); # <--- action here
}
if ($msg->{params}->[-1] =~ m/^!whois$/) {
say $client->send_msg('WHOIS', prefix_nick($from));
}
if ($msg->{params}->[-1] =~ qr/$trigger/) {
my $code = $msg->{params}->[-1];
$code =~ s/$trigger//;
my $nick = prefix_nick($from);
my $mask = prefix_user($from)."@".prefix_host($from);
say "Got trigger: [$trigger] $code";
my $out = $states{$state_directory}->call($nick, $mask, '', $chan, $code);
$out =~ s/\001ACTION /\0777ACTION /g;
$out =~ s/[\000-\001]/ /g;
$out =~ s/\0777ACTION /\001ACTION /g;
my @lines = split( /\n/, $out);
my $limit = $conf->{linelimit} || 20;
# split lines if they are too long
@lines = map { utf8::encode(chunkby($_, 420)) } @lines;
if (@lines > $limit) {
my $n = @lines;
@lines = @lines[0..($limit-1)];
push @lines, "error: output truncated to ".($limit - 1)." of $n lines total"
}
$client->send_chan($chan, 'PRIVMSG', $chan, $_) foreach @lines;
}
};
$client->reg_cb(irc_privmsg => $parse_privmsg);
$init = sub {
$client->send_srv('PRIVMSG' => $nickserv, "identify $nickservpw") if defined $nickservpw;
$client->connect (
$botserver, $botport, { nick => $botnick, user => $botident, real => $botreal },
sub {
my ($fh) = @_;
if ($bindip) {
my $bind = AnyEvent::Socket::pack_sockaddr(undef, parse_address($bindip));
bind $fh, $bind;
}
return 30;
},
);
foreach my $chan (@$channels) {
say "Joining $chan";
$client->send_srv('JOIN', $chan);
# ..You may have wanted to join #bla and the server
# redirects that and sends you that you joined #blubb. You
# may use clear_chan_queue to remove the queue after some
# timeout after joining, so that you don't end up with a
# memory leak.
$client->clear_chan_queue($chan);
#$client->send_chan($chan, 'PRIVMSG', $chan, 'hy');
# $client->send_chan($botchan, 'NOTICE', $botchan, 'notice lol');
}
};
$init->();
}
sub chunkby {
my ($a,$len) = @_;
my @out = ();
while (length($a) > $len) {
push @out,substr($a,0,$len);
$a = substr($a,$len);
}
push @out, $a if ($a);
return @out;
}
# overload the IRC::Client connect method to let us defined a prebinding callback
package AnyEvent::IRC::Client::Pre;
use strict;
use warnings;
use AnyEvent::IRC::Connection;
use parent 'AnyEvent::IRC::Client';
sub connect {
my ($self, $host, $port, $info, $pre) = @_;
if (defined $info) {
$self->{register_cb_guard} = $self->reg_cb (
ext_before_connect => sub {
my ($self, $err) = @_;
unless ($err) {
$self->register(
$info->{nick}, $info->{user}, $info->{real}, $info->{password}
);
}
delete $self->{register_cb_guard};
}
);
}
AnyEvent::IRC::Connection::connect($self, $host, $port, $pre);
}
1;