001
2021-12-17
jrmu
#!/usr/bin/perl
003
2021-12-17
jrmu
use strict;
004
2021-12-17
jrmu
no strict 'refs';
005
2021-12-17
jrmu
use warnings;
006
2021-12-17
jrmu
use IO::Socket;
007
2021-12-17
jrmu
use IO::Select;
008
2021-12-17
jrmu
use OpenBSD::Pledge;
009
2021-12-17
jrmu
use OpenBSD::Unveil;
011
2021-12-17
jrmu
my $confpath = "botnow.conf";
012
2021-12-17
jrmu
our %conf;
013
2021-12-17
jrmu
foreach my $line (readarray($confpath)) {
014
2021-12-17
jrmu
if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
016
2021-12-17
jrmu
} elsif ($line =~ /^([-_a-zA-Z0-9]+)\s*=\s*([[:print:]]+)$/) {
017
2021-12-17
jrmu
$conf{$1} = $2;
018
2021-12-17
jrmu
} else {
019
2021-12-17
jrmu
die "ERROR: botnow.conf format invalid: $line";
023
2021-12-17
jrmu
# Name of local network
024
2021-12-17
jrmu
$conf{localnet} = $conf{localnet} || "ircnow";
026
2021-12-17
jrmu
# Internal IPv4 address and plaintext port
027
2021-12-17
jrmu
$conf{host} = $conf{host} || "127.0.0.1";
028
2021-12-17
jrmu
$conf{port} = $conf{port} || 1337;
030
2021-12-17
jrmu
# Bouncer hostname
031
2021-12-17
jrmu
chomp($conf{hostname} = $conf{hostname} || `hostname`);
033
2021-12-17
jrmu
# External IPv4 address, plaintext and ssl port
034
2021-12-17
jrmu
$conf{ip4} = $conf{ip4} or die "ERROR: botnow.conf: ip4";
035
2021-12-17
jrmu
$conf{ip6} = $conf{ip6} or die "ERROR: botnow.conf: ip6";
036
2021-12-17
jrmu
$conf{ip6subnet} = $conf{ip6subnet} or die "ERROR: botnow.conf: ip6subnet";
037
2021-12-17
jrmu
$conf{plainport} = $conf{plainport} || 1337;
038
2021-12-17
jrmu
$conf{sslport} = $conf{sslport} || 31337;
040
2021-12-17
jrmu
# Nick and password of bot -- Make sure to add to oper block
041
2021-12-17
jrmu
$conf{nick} = $conf{nick} || "botnow";
042
2021-12-17
jrmu
$conf{pass} = $conf{pass} or die "ERROR: botnow.conf: pass";
044
2021-12-17
jrmu
# Comma-separated list of channels for requesting bouncers
045
2021-12-17
jrmu
$conf{chans} = $conf{chans} || "#ircnow";
047
2021-12-17
jrmu
#Join chans on localnet?
048
2021-12-17
jrmu
$conf{localchans} = defined($conf{localchans}) && ($conf{localchans} =~ /^true/i);
050
2021-12-17
jrmu
# Number of words in password
051
2021-12-17
jrmu
$conf{passlength} = $conf{passlength} || 3;
053
2021-12-17
jrmu
# Mail from address
054
2021-12-17
jrmu
if (!defined($conf{mailname})) {
055
2021-12-17
jrmu
if ($conf{mailfrom} =~ /^([^@]+)@/) {
056
2021-12-17
jrmu
$conf{mailname} = $1 or die "ERROR: botnow.conf mailname";
060
2021-12-17
jrmu
# ZNC install directory
061
2021-12-17
jrmu
$conf{zncdir} = $conf{zncdir} || "/home/znc/home/znc";
063
2021-12-17
jrmu
# NSD zone dir
064
2021-12-17
jrmu
$conf{zonedir} = $conf{zonedir} || "/var/nsd/zones/master/";
066
2021-12-17
jrmu
# Network Interface Config File
067
2021-12-17
jrmu
$conf{hostnameif} = $conf{hostnameif} || "/etc/hostname.vio0";
069
2021-12-17
jrmu
# Verbosity: 0 (no errors), 1 (errors), 2 (warnings), 3 (diagnostic)
070
2021-12-17
jrmu
use constant {
071
2021-12-17
jrmu
NONE => 0,
072
2021-12-17
jrmu
ERRORS => 1,
073
2021-12-17
jrmu
WARNINGS => 2,
074
2021-12-17
jrmu
ALL => 3,
076
2021-12-17
jrmu
$conf{verbose} = $conf{verbose} || ERRORS;
078
2021-12-17
jrmu
# Terms of Service; don't edit lines with the word EOF
079
2021-12-17
jrmu
$conf{terms} = $conf{terms} || "IRCNow: Of the User, By the User, For the User. Rules: no porn, no illegal drugs, no gambling, no slander, no warez, no promoting violence, no spam, illegal cracking, or DDoS. Only one account per person. Don't share passwords. Full terms: https://ircnow.org/terms.php";
081
2021-12-17
jrmu
$conf{ipv6path} = "ipv6s"; # ipv6 file path
082
2021-12-17
jrmu
$conf{netpath} = "networks"; # networks file path
083
2021-12-17
jrmu
$conf{expires} = $conf{expires} || 1800; # time before captcha expires
085
2021-12-17
jrmu
if(defined($conf{die})) { die $conf{die}; }
087
2021-12-17
jrmu
my @modules;
088
2021-12-17
jrmu
if (defined($conf{modules})) {
089
2021-12-17
jrmu
@modules = split(/\s+/, $conf{modules});
091
2021-12-17
jrmu
use lib './';
092
2021-12-17
jrmu
foreach my $mod (@modules) {
093
2021-12-17
jrmu
require "$mod.pm";
095
2021-12-17
jrmu
foreach my $mod (@modules) {
096
2021-12-17
jrmu
my $init = "${mod}::init";
097
2021-12-17
jrmu
$init->();
100
2021-12-17
jrmu
our @networks;
101
2021-12-17
jrmu
my @bots;
102
2021-12-17
jrmu
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
103
2021-12-17
jrmu
my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
104
2021-12-17
jrmu
my @chans = split /[,\s]+/m, $conf{chans};
105
2021-12-17
jrmu
my @teamchans;
106
2021-12-17
jrmu
if (defined($conf{teamchans})) { @teamchans = split /[,\s]+/m, $conf{teamchans}; }
107
2021-12-17
jrmu
my $call;
108
2021-12-17
jrmu
my $botnick = $conf{nick};
109
2021-12-17
jrmu
my $host = $conf{host};
110
2021-12-17
jrmu
my $port = $conf{port};
111
2021-12-17
jrmu
my $pass = $conf{pass};
112
2021-12-17
jrmu
my $localnet = $conf{localnet};
113
2021-12-17
jrmu
my $staff = $conf{staff};
114
2021-12-17
jrmu
my @stafflist = split(/ /,$staff);
115
2021-12-17
jrmu
my $verbose = $conf{verbose};
116
2021-12-17
jrmu
my $ipv6path = $conf{ipv6path};
117
2021-12-17
jrmu
my $netpath = $conf{netpath};
118
2021-12-17
jrmu
my $expires = $conf{expires};
119
2021-12-17
jrmu
my $localchans = $conf{localchans};
121
2021-12-17
jrmu
unveil("./", "r") or die "Unable to unveil $!";
122
2021-12-17
jrmu
unveil("$confpath", "r") or die "Unable to unveil $!";
123
2021-12-17
jrmu
unveil("$netpath", "r") or die "Unable to unveil $!";
124
2021-12-17
jrmu
unveil("$ipv6path", "rwc") or die "Unable to unveil $!";
125
2021-12-17
jrmu
unveil() or die "Unable to lock unveil $!";
127
2021-12-17
jrmu
#dns and inet for sockets, proc and exec for figlet
128
2021-12-17
jrmu
#rpath for reading file, wpath for writing file, cpath for creating path
129
2021-12-17
jrmu
#flock, fattr for sqlite
130
2021-12-17
jrmu
pledge( qw(stdio rpath wpath cpath inet dns proc exec flock fattr) ) or die "Unable to pledge: $!";
132
2021-12-17
jrmu
# Read from filename and return array of lines without trailing newlines
133
2021-12-17
jrmu
sub readarray {
134
2021-12-17
jrmu
my ($filename) = @_;
135
2021-12-17
jrmu
open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
136
2021-12-17
jrmu
chomp(my @lines = <$fh>);
137
2021-12-17
jrmu
close $fh;
138
2021-12-17
jrmu
return @lines;
141
2021-12-17
jrmu
# Read from filename and return as string
142
2021-12-17
jrmu
sub readstr {
143
2021-12-17
jrmu
my ($filename) = @_;
144
2021-12-17
jrmu
open my $fh, '<', $filename or die "Could not read file '$filename' $!";
145
2021-12-17
jrmu
my $str = do { local $/; <$fh> };
146
2021-12-17
jrmu
close $fh;
147
2021-12-17
jrmu
return $str;
150
2021-12-17
jrmu
# Write str to filename
151
2021-12-17
jrmu
sub writefile {
152
2021-12-17
jrmu
my ($filename, $str) = @_;
153
2021-12-17
jrmu
open(my $fh, '>', "$filename") or die "Could not write to $filename";
154
2021-12-17
jrmu
print $fh $str;
155
2021-12-17
jrmu
close $fh;
158
2021-12-17
jrmu
# Append str to filename
159
2021-12-17
jrmu
sub appendfile {
160
2021-12-17
jrmu
my ($filename, $str) = @_;
161
2021-12-17
jrmu
open(my $fh, '>>', "$filename") or die "Could not append to $filename";
162
2021-12-17
jrmu
print $fh $str;
163
2021-12-17
jrmu
close $fh;
166
2021-12-17
jrmu
# Return list of networks from filename
167
2021-12-17
jrmu
# To add multiple servers for a single network, simply create a new entry with
168
2021-12-17
jrmu
# the same net name; znc ignores addnetwork commands when a network already exists
169
2021-12-17
jrmu
sub readnetworks {
170
2021-12-17
jrmu
my ($filename) = @_;
171
2021-12-17
jrmu
my @lines = readarray($filename);
172
2021-12-17
jrmu
my @networks;
173
2021-12-17
jrmu
foreach my $line (@lines) {
174
2021-12-17
jrmu
if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
176
2021-12-17
jrmu
} elsif ($line =~ /^\s*([-a-zA-Z0-9]+)\s*([-_.:a-zA-Z0-9]+)\s*(~|\+)?([0-9]+)\s*$/) {
177
2021-12-17
jrmu
my ($name, $server, $port) = ($1, $2, $4);
178
2021-12-17
jrmu
my $trustcerts;
179
2021-12-17
jrmu
if (!defined($3)) {
180
2021-12-17
jrmu
$trustcerts = 0;
181
2021-12-17
jrmu
} elsif ($3 eq "~") { # Use SSL but trust all certs
182
2021-12-17
jrmu
$port = "+".$port;
183
2021-12-17
jrmu
$trustcerts = 1;
184
2021-12-17
jrmu
} else { # Use SSL and verify certs
185
2021-12-17
jrmu
$port = "+".$port;
186
2021-12-17
jrmu
$trustcerts = 0;
188
2021-12-17
jrmu
push(@networks, {"name" => $name, "server" => $server, "port" => $port, "trustcerts" => $trustcerts });
189
2021-12-17
jrmu
} else {
190
2021-12-17
jrmu
die "network format invalid: $line\n";
193
2021-12-17
jrmu
return @networks;
196
2021-12-17
jrmu
@networks = readnetworks($netpath);
198
2021-12-17
jrmu
# networks must be sorted to avoid multiple connections
199
2021-12-17
jrmu
@networks = sort @networks;
201
2021-12-17
jrmu
# create sockets
202
2021-12-17
jrmu
my $sel = IO::Select->new( );
203
2021-12-17
jrmu
my $lastnet = "";
204
2021-12-17
jrmu
foreach my $network (@networks) {
205
2021-12-17
jrmu
# avoid duplicate connections
206
2021-12-17
jrmu
if ($lastnet eq $network->{name}) { next; }
207
2021-12-17
jrmu
$lastnet = $network->{name};
208
2021-12-17
jrmu
my $socket = IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port, Proto=>'tcp', Timeout=>'300') || print "Failed to establish connection\n";
209
2021-12-17
jrmu
$sel->add($socket);
210
2021-12-17
jrmu
my $bot = {("sock" => $socket), %$network};
211
2021-12-17
jrmu
push(@bots, $bot);
212
2021-12-17
jrmu
putserv($bot, "NICK $botnick");
213
2021-12-17
jrmu
putserv($bot, "USER $botnick * * :$botnick");
216
2021-12-17
jrmu
while(my @ready = $sel->can_read) {
217
2021-12-17
jrmu
my ($bot, $response);
218
2021-12-17
jrmu
my ($sender, $val);
219
2021-12-17
jrmu
foreach my $socket (@ready) {
220
2021-12-17
jrmu
foreach my $b (@bots) {
221
2021-12-17
jrmu
if($socket == $b->{sock}) {
222
2021-12-17
jrmu
$bot = $b;
226
2021-12-17
jrmu
if (!defined($response = <$socket>)) {
227
2021-12-17
jrmu
debug(ERRORS, "ERROR ".$bot->{name}." has no response:");
230
2021-12-17
jrmu
if ($response =~ /^PING :(.*)\r\n$/i) {
231
2021-12-17
jrmu
putserv($bot, "PONG :$1");
232
2021-12-17
jrmu
} elsif ($response =~ /^:irc.znc.in (.*) (.*) :(.*)\r\n$/) {
233
2021-12-17
jrmu
my ($type, $target, $text) = ($1, $2, $3);
234
2021-12-17
jrmu
if ($type eq "001" && $target =~ /^$botnick.?$/ && $text eq "Welcome to ZNC") {
235
2021-12-17
jrmu
} elsif ($type eq "NOTICE" && $target =~ /^$botnick.?$/ && $text eq "*** To connect now, you can use /quote PASS <username>:<password>, or /quote PASS <username>/<network>:<password> to connect to a specific network.") {
236
2021-12-17
jrmu
} elsif ($type eq "NOTICE" && $target =~ /^$botnick.?$/ && $text eq "*** You need to send your password. Configure your client to send a server password.") {
237
2021-12-17
jrmu
} elsif ($type eq "464" && $target =~ /^$botnick.?$/ && $text eq "Password required") {
238
2021-12-17
jrmu
putserv($bot, "PASS $botnick/$bot->{name}:$pass");
239
2021-12-17
jrmu
if ($bot->{name} =~ /^$localnet$/i) {
240
2021-12-17
jrmu
putserv($bot, "OPER $botnick $pass");
241
2021-12-17
jrmu
putserv($bot, "PRIVMSG *status :LoadMod --type=user controlpanel");
242
2021-12-17
jrmu
putserv($bot, "PRIVMSG *controlpanel :get Admin $botnick");
243
2021-12-17
jrmu
putserv($bot, "PRIVMSG *controlpanel :get Nick cloneuser");
244
2021-12-17
jrmu
foreach my $chan (@teamchans) {
245
2021-12-17
jrmu
putserv($bot, "JOIN $chan");
248
2021-12-17
jrmu
if ($bot->{name} !~ /^$localnet$/i or $localchans) {
249
2021-12-17
jrmu
foreach my $chan (@chans) {
250
2021-12-17
jrmu
putserv($bot, "JOIN $chan");
253
2021-12-17
jrmu
} elsif ($type eq "464" && $target =~ /^$botnick.?$/ && $text eq "Invalid Password") {
254
2021-12-17
jrmu
die "ERROR: Wrong Username/Password: $bot->{name}";
255
2021-12-17
jrmu
} else {
256
2021-12-17
jrmu
debug(ERRORS, "Unexpected bncnow.pl 257: type: $type, target: $target, text: $text");
258
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) PRIVMSG ([^ ]+) :(.*)\r\n$/i) {
259
2021-12-17
jrmu
my ($hostmask, $nick, $host, $target, $text) = ($1, $2, $3, $4, $5);
260
2021-12-17
jrmu
if ($hostmask eq '*status!znc@znc.in' && $target =~ /^$botnick.?$/) {
261
2021-12-17
jrmu
if ($text =~ /Network ([[:ascii:]]+) doesn't exist./) {
262
2021-12-17
jrmu
debug(ERRORS, "nonexistent: $1");
263
2021-12-17
jrmu
} elsif ($text eq "You are currently disconnected from IRC. Use 'connect' to reconnect.") {
264
2021-12-17
jrmu
debug(ERRORS, "disconnected: $bot->{name}");
265
2021-12-17
jrmu
} elsif ($text =~ /Unable to load module (.*): Module (.*) already loaded./) {
266
2021-12-17
jrmu
debug(ALL, "Module $1 already loaded\n");
267
2021-12-17
jrmu
} elsif ($text =~ /^Disconnected from IRC.*$/) {
268
2021-12-17
jrmu
debug(ERRORS, "$bot->{name}: $text");
269
2021-12-17
jrmu
} elsif ($text =~ /^|/) {
270
2021-12-17
jrmu
debug(ERRORS, "$bot->{name}: $text");
271
2021-12-17
jrmu
} else {
272
2021-12-17
jrmu
debug(ERRORS, "Unexpected bncnow.pl 273: $response");
274
2021-12-17
jrmu
} elsif ($text =~ /^!([[:graph:]]+)\s*(.*)/) {
275
2021-12-17
jrmu
my ($cmd, $text) = ($1, $2);
276
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
277
2021-12-17
jrmu
if ($target =~ /^#/) {
278
2021-12-17
jrmu
foreach my $c (@{$call->{pub}}) {
279
2021-12-17
jrmu
if ($cmd eq $c->{cmd}) {
280
2021-12-17
jrmu
my $proc = $c->{proc};
281
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $target, $text);
284
2021-12-17
jrmu
} else {
285
2021-12-17
jrmu
foreach my $c (@{$call->{msg}}) {
286
2021-12-17
jrmu
if ($cmd eq $c->{cmd}) {
287
2021-12-17
jrmu
my $proc = $c->{proc};
288
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $text);
292
2021-12-17
jrmu
} else {
293
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
294
2021-12-17
jrmu
if ($target =~ /^#/) {
295
2021-12-17
jrmu
foreach my $c (@{$call->{pubm}}) {
296
2021-12-17
jrmu
my $proc = $c->{proc};
297
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $target, $text);
299
2021-12-17
jrmu
} else {
300
2021-12-17
jrmu
foreach my $c (@{$call->{msgm}}) {
301
2021-12-17
jrmu
my $proc = $c->{proc};
302
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $text);
306
2021-12-17
jrmu
debug(ALL, "$hostmask $target $text");
307
2021-12-17
jrmu
} elsif($response =~ /^:([^ ]+) NOTICE ([^ ]+) :(.*)\r\n$/i) {
308
2021-12-17
jrmu
my ($hostmask, $target, $text) = ($1, $2, $3);
309
2021-12-17
jrmu
if ($hostmask =~ /([^!]+)!([^@]+@[^@ ]+)/) {
310
2021-12-17
jrmu
my ($nick, $host) = ($1, $2);
311
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
312
2021-12-17
jrmu
foreach my $c (@{$call->{notc}}) {
313
2021-12-17
jrmu
# if ($text eq $c->{mask}) { # TODO fix later
314
2021-12-17
jrmu
my $proc = $c->{proc};
315
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $text, $target);
318
2021-12-17
jrmu
# TODO use CTCR
319
2021-12-17
jrmu
# CTCP replies
320
2021-12-17
jrmu
if ($hostmask ne '*status!znc@znc.in') {
321
2021-12-17
jrmu
if ($text =~ /^(PING|VERSION|TIME|USERINFO) (.*)$/i) {
322
2021-12-17
jrmu
my ($key, $val) = ($1, $2);
323
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $nick, $expires);
324
2021-12-17
jrmu
SQLite::set("irc", "id", $id, "ctcp".lc($key), $val);
325
2021-12-17
jrmu
SQLite::set("irc", "id", $id, "localtime", time());
329
2021-12-17
jrmu
debug(ALL, "$hostmask NOTICE $target $text");
330
2021-12-17
jrmu
#:portlane.se.quakenet.org NOTICE guava :Highest connection count: 1541 (1540 clients)
331
2021-12-17
jrmu
#:portlane.se.quakenet.org NOTICE guava :on 2 ca 2(4) ft 20(20) tr
332
2021-12-17
jrmu
} elsif($response =~ /^:([^ ]+) MODE ([^ ]+) ([^ ]+)\s*(.*)\r\n$/i) {
333
2021-12-17
jrmu
my ($hostmask, $chan, $change, $targets) = ($1, $2, $3, $4);
334
2021-12-17
jrmu
if ($hostmask =~ /([^!]+)!([^@]+@[^@ ]+)/) {
335
2021-12-17
jrmu
my ($nick, $host) = ($1, $2);
336
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
337
2021-12-17
jrmu
foreach my $c (@{$call->{mode}}) {
338
2021-12-17
jrmu
# TODO filter by mask
339
2021-12-17
jrmu
my $proc = $c->{proc};
340
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $chan, $change, $targets);
343
2021-12-17
jrmu
debug(ALL, "$hostmask MODE $chan $change $targets");
344
2021-12-17
jrmu
#:guava!guava@guava.guava.ircnow.org MODE guava :+Ci
345
2021-12-17
jrmu
#:ChanServ!services@services.irc.ircnow.org MODE #testing +q jrmu
346
2021-12-17
jrmu
#:jrmu!jrmu@jrmu.staff.ircnow.org MODE #testing +o jrmu
347
2021-12-17
jrmu
#Unexpected bncnow.pl 460: :irc.guava.ircnow.org MODE guava :+o
348
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) JOIN :?(.*)\r\n$/i) {
349
2021-12-17
jrmu
my ($hostmask, $nick, $host, $chan) = ($1, $2, $3, $4);
350
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
351
2021-12-17
jrmu
foreach my $c (@{$call->{join}}) {
352
2021-12-17
jrmu
my $proc = $c->{proc};
353
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $chan);
355
2021-12-17
jrmu
debug(ALL, "$hostmask JOIN $chan");
356
2021-12-17
jrmu
#:jrmu!jrmu@jrmu.staff.ircnow.org JOIN :#testing
357
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) PART ([^ ]+) :(.*)\r\n$/i) {
358
2021-12-17
jrmu
my ($hostmask, $nick, $host, $chan, $text) = ($1, $2, $3, $4, $5);
359
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
360
2021-12-17
jrmu
foreach my $c (@{$call->{part}}) {
361
2021-12-17
jrmu
# if ($text eq $c->{mask}) { # TODO fix later
362
2021-12-17
jrmu
my $proc = $c->{proc};
363
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $chan, $text);
366
2021-12-17
jrmu
debug(ALL, "$hostmask PART $chan :$text");
367
2021-12-17
jrmu
#:jrmu!jrmu@jrmu.staff.ircnow.org PART #testing :
368
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) KICK (#[^ ]+) ([^ ]+) :(.*)\r\n$/i) {
369
2021-12-17
jrmu
my ($hostmask, $nick, $host, $chan, $kicked, $text) = ($1, $2, $3, $4, $5, $6);
370
2021-12-17
jrmu
my $hand = $staff; # TODO fix later
371
2021-12-17
jrmu
foreach my $c (@{$call->{kick}}) {
372
2021-12-17
jrmu
# if ($text eq $c->{mask}) { # TODO fix later
373
2021-12-17
jrmu
my $proc = $c->{proc};
374
2021-12-17
jrmu
$proc->($bot, $nick, $host, $hand, $chan, $text);
377
2021-12-17
jrmu
debug(ALL, "$hostmask KICK $chan $kicked :$text");
378
2021-12-17
jrmu
#jrmu!jrmu@jrmu.users.undernet.org KICK #ircnow guava :this is a test
379
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) NICK :?(.*)\r\n$/i) {
380
2021-12-17
jrmu
my ($hostmask, $nick, $host, $text) = ($1, $2, $3, $4);
381
2021-12-17
jrmu
debug(ALL, "$hostmask NICK $text");
382
2021-12-17
jrmu
#:Fly0nDaWaLL|dal!psybnc@do.not.h4ck.me NICK :nec|dal
383
2021-12-17
jrmu
} elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) QUIT :(.*)\r\n$/i) {
384
2021-12-17
jrmu
my ($hostmask, $nick, $host, $text) = ($1, $2, $3, $4);
385
2021-12-17
jrmu
debug(ALL, "$hostmask QUIT :$text");
386
2021-12-17
jrmu
#:Testah!~sid268081@aa38a510 QUIT :Client closed connection
387
2021-12-17
jrmu
} elsif($response =~ /^NOTICE AUTH :(.*)\r\n$/i) {
388
2021-12-17
jrmu
my ($text) = ($1);
389
2021-12-17
jrmu
debug(ALL, "NOTICE AUTH: $text");
390
2021-12-17
jrmu
#NOTICE AUTH :*** Looking up your hostname
391
2021-12-17
jrmu
#NOTICE AUTH: *** Looking up your hostname
392
2021-12-17
jrmu
#NOTICE AUTH: *** Checking Ident
393
2021-12-17
jrmu
#NOTICE AUTH: *** Got ident response
394
2021-12-17
jrmu
#NOTICE AUTH: *** Found your hostname
395
2021-12-17
jrmu
} elsif ($response =~ /^:([[:graph:]]+) (\d\d\d) $botnick.? :?(.*)\r?\n?\r$/i) {
396
2021-12-17
jrmu
my ($server, $code, $text) = ($1, $2, $3);
397
2021-12-17
jrmu
if ($code =~ /^001$/) { # Server Info
398
2021-12-17
jrmu
debug(ERRORS, "connected: $bot->{name}");
399
2021-12-17
jrmu
} elsif ($code =~ /^0\d\d$/) { # Server Info
400
2021-12-17
jrmu
debug(ALL, "$server $code $text");
401
2021-12-17
jrmu
} elsif ($code =~ /^2\d\d$/) { # Server Stats
402
2021-12-17
jrmu
debug(ALL, "$server $code $text");
403
2021-12-17
jrmu
} elsif ($code == 301 && $text =~ /^([-_\|`a-zA-Z0-9]+) :([[:graph:]]+)/) {
404
2021-12-17
jrmu
debug(ALL, "$text");
405
2021-12-17
jrmu
} elsif ($code == 307 && $text =~ /^([-_\|`a-zA-Z0-9]+) (.*)/) {
406
2021-12-17
jrmu
my ($sender, $key) = ($1, "registered");
407
2021-12-17
jrmu
$val = $2 eq ":is a registered nick" ? "True" : "$2";
408
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
409
2021-12-17
jrmu
SQLite::set("irc", "id", $id, "identified", $val);
410
2021-12-17
jrmu
debug(ALL, "$key: $val");
411
2021-12-17
jrmu
} elsif ($code == 311 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([^:]+)\s+([^:]+) \* :([^:]*)/) {
412
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "hostmask", "$1\!$2\@$3");
413
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
414
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
415
2021-12-17
jrmu
debug(ALL, "$key: $val");
416
2021-12-17
jrmu
} elsif ($code == 312 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([^:]+) :([^:]+)/) {
417
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "server", $2);
418
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
419
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
420
2021-12-17
jrmu
debug(ALL, "$key: $val");
421
2021-12-17
jrmu
} elsif ($code == 313 && $text =~ /^([-_\|`a-zA-Z0-9]+) :?(.*)/) {
422
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "oper", ($2 eq "is an IRC operator" ? "True" : "$2"));
423
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
424
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
425
2021-12-17
jrmu
debug(ALL, "$key: $val");
426
2021-12-17
jrmu
} elsif ($code == 315 && $text =~ /^([-_\|`a-zA-Z0-9]+) :End of \/?WHO(IS)? list/) {
427
2021-12-17
jrmu
debug(ALL, "End of WHOIS");
428
2021-12-17
jrmu
} elsif ($code == 317 && $text =~ /^([-_\|`a-zA-Z0-9]+) (\d+) (\d+) :?(.*)/) {
429
2021-12-17
jrmu
($sender, my $idle, my $epochtime) = ($1, $2, $3);
430
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
431
2021-12-17
jrmu
SQLite::set("irc", "id", $id, "idle", $idle);
432
2021-12-17
jrmu
# SQLite::set("irc", "id", $id, "epochtime", time());
433
2021-12-17
jrmu
debug(ALL, "idle: $idle, epochtime: $epochtime");
434
2021-12-17
jrmu
} elsif ($code == 318 && $text =~ /^([-_\|`a-zA-Z0-9]+) :End of \/?WHOIS list/) {
435
2021-12-17
jrmu
debug(ALL, "End of WHOIS");
436
2021-12-17
jrmu
} elsif ($code == 319 && $text =~ /^([-_\|`a-zA-Z0-9]+) :(.*)/) {
437
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "chans", $2);
438
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
439
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
440
2021-12-17
jrmu
debug(ALL, "$key: $val");
441
2021-12-17
jrmu
} elsif ($code == 330 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([-_\|`a-zA-Z0-9]+) :?(.*)/) {
442
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "identified", ($3 eq "is logged in as" ? "True" : $2));
443
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
444
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
445
2021-12-17
jrmu
debug(ALL, "$key: $val");
446
2021-12-17
jrmu
} elsif ($code == 338 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([0-9a-fA-F:.]+) :actually using host/) {
447
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "ip", $2);
448
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
449
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
450
2021-12-17
jrmu
debug(ALL, "$key: $val");
451
2021-12-17
jrmu
#Unexpected: efnet.port80.se 338 jrmu 206.253.167.44 :actually using host
452
2021-12-17
jrmu
} elsif ($code == 378 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is connecting from ([^ ]+)\s*([0-9a-fA-F:.]+)?/) {
453
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "ip", $3);
454
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
455
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
456
2021-12-17
jrmu
debug(ALL, "$key: $val");
457
2021-12-17
jrmu
} elsif ($code == 671 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is using a secure connection/) {
458
2021-12-17
jrmu
my ($sender, $key, $val) = ($1, "ssl", "True");
459
2021-12-17
jrmu
my $id = SQLite::id("irc", "nick", $sender, $expires);
460
2021-12-17
jrmu
SQLite::set("irc", "id", $id, $key, $val);
461
2021-12-17
jrmu
debug(ALL, "$key: $val");
462
2021-12-17
jrmu
} elsif ($code =~ /^332$/) { # Topic
463
2021-12-17
jrmu
# print "$text\r\n";
464
2021-12-17
jrmu
} elsif ($code =~ /^333$/) { #
465
2021-12-17
jrmu
# print "$server $text\r\n";
466
2021-12-17
jrmu
#karatkievich.freenode.net 333 #ircnow jrmu!znc@206.253.167.44 1579277253
467
2021-12-17
jrmu
} elsif ($code =~ /^352$/) { # Hostmask
468
2021-12-17
jrmu
#:datapacket.hk.quakenet.org 352 * znc guava.guava.ircnow.org *.quakenet.org guava H :0 guava
469
2021-12-17
jrmu
# print "$server $code $text\r\n";
470
2021-12-17
jrmu
} elsif ($code =~ /^353$/) { # Names
471
2021-12-17
jrmu
# print "$server $code $text\r\n";
472
2021-12-17
jrmu
} elsif ($code =~ /^366$/) { # End of names
473
2021-12-17
jrmu
# print "$server $code $text\r\n";
474
2021-12-17
jrmu
} elsif ($code =~ /^37\d$/) { # MOTD
475
2021-12-17
jrmu
# print "$server $code $text\r\n";
476
2021-12-17
jrmu
} elsif ($code =~ /^381$/) { # IRC Operator Verified
477
2021-12-17
jrmu
# print "IRC Oper Verified\r\n";
478
2021-12-17
jrmu
} elsif ($code =~ /^401$/) { # IRC Operator Verified
479
2021-12-17
jrmu
# print "IRC Oper Verified\r\n";
480
2021-12-17
jrmu
} elsif ($code =~ /^403$/) { # No such channel
481
2021-12-17
jrmu
# debug(ERRORS, "$text");
482
2021-12-17
jrmu
} elsif ($code =~ /^422$/) { # MOTD missing
483
2021-12-17
jrmu
# print "$server $code $text\r\n";
484
2021-12-17
jrmu
} elsif ($code =~ /^396$/) { # Display hostname
485
2021-12-17
jrmu
# print "$server $code $text\r\n";
486
2021-12-17
jrmu
#Unexpected bncnow.pl 454: irc.guava.ircnow.org 396 guava.guava.ircnow.org :is your displayed hostname now
487
2021-12-17
jrmu
} elsif ($code =~ /^464$/) { # Invalid password for oper
488
2021-12-17
jrmu
foreach my $chan (@teamchans) {
489
2021-12-17
jrmu
putserv($bot, "PRIVMSG $chan :$botnick oper password failed; the bot will be unable to view uncloaked IP addresses");
491
2021-12-17
jrmu
} elsif ($code =~ /^477$/) { # Can't join channel
492
2021-12-17
jrmu
foreach my $chan (@teamchans) {
493
2021-12-17
jrmu
putserv($bot, "PRIVMSG $chan :ERROR: $botnick on $server: $text");
495
2021-12-17
jrmu
} elsif ($code == 716 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is in \+g mode \(server-side ignore.\)/) {
496
2021-12-17
jrmu
debug(ALL, "$text");
497
2021-12-17
jrmu
} else {
498
2021-12-17
jrmu
debug(ERRORS, "Unexpected bncnow.pl 454: $server $code $text");
500
2021-12-17
jrmu
} else {
501
2021-12-17
jrmu
debug(ERRORS, "Unexpected bncnow.pl 460: $response");
506
2021-12-17
jrmu
sub putserv {
507
2021-12-17
jrmu
my( $bot, $text )=@_;
508
2021-12-17
jrmu
my $socket = $bot->{sock};
509
2021-12-17
jrmu
if ($text =~ /^([^:]+):([[:ascii:]]*)$/m) {
510
2021-12-17
jrmu
my ($cmd, $line) = ($1, $2);
511
2021-12-17
jrmu
my @lines = split /\r?\n/m, $line;
512
2021-12-17
jrmu
foreach my $l (@lines) {
513
2021-12-17
jrmu
print $socket "$cmd:$l\r\n";
515
2021-12-17
jrmu
} else {
516
2021-12-17
jrmu
print $socket "$text\r\n";
520
2021-12-17
jrmu
sub putserv {
521
2021-12-17
jrmu
my( $bot, $text )=@_;
522
2021-12-17
jrmu
my $socket = $bot->{sock};
523
2021-12-17
jrmu
if ($text =~ /^([^:]+):([[:ascii:]]*)$/m) {
524
2021-12-17
jrmu
my ($cmd, $line) = ($1, $2);
525
2021-12-17
jrmu
my @lines = split /\r?\n/m, $line;
526
2021-12-17
jrmu
foreach my $l (@lines) {
527
2021-12-17
jrmu
print $socket "$cmd:$l\r\n";
529
2021-12-17
jrmu
} else {
530
2021-12-17
jrmu
print $socket "$text\r\n";
534
2021-12-17
jrmu
sub putservlocalnet {
535
2021-12-17
jrmu
my( $bot, $text )=@_;
536
2021-12-17
jrmu
my $botlocalnet;
537
2021-12-17
jrmu
foreach my $b (@bots) {
538
2021-12-17
jrmu
if($b->{name} =~ /^$localnet$/i) {
539
2021-12-17
jrmu
$botlocalnet = $b;
543
2021-12-17
jrmu
putserv($botlocalnet, $text);
547
2021-12-17
jrmu
sub date {
548
2021-12-17
jrmu
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
549
2021-12-17
jrmu
my $localtime = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
550
2021-12-17
jrmu
return $localtime;
552
2021-12-17
jrmu
sub gettime {
553
2021-12-17
jrmu
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
554
2021-12-17
jrmu
my $localtime = sprintf("%s %s %d %02d:%02d:%02d", $days[$wday], $months[$mon], $mday, $hour, $min, $sec);
555
2021-12-17
jrmu
return $localtime;
558
2021-12-17
jrmu
sub whois {
559
2021-12-17
jrmu
my( $socket, $target )=@_;
560
2021-12-17
jrmu
print $socket "WHOIS $target $target\r\n";
563
2021-12-17
jrmu
sub ctcp {
564
2021-12-17
jrmu
my( $socket, $target )=@_;
565
2021-12-17
jrmu
# print $socket "PRIVMSG $target :".chr(01)."CLIENTINFO".chr(01)."\r\n";
566
2021-12-17
jrmu
# print $socket "PRIVMSG $target :".chr(01)."FINGER".chr(01)."\r\n";
567
2021-12-17
jrmu
# print $socket "PRIVMSG $target :".chr(01)."SOURCE".chr(01)."\r\n";
568
2021-12-17
jrmu
print $socket "PRIVMSG $target :".chr(01)."TIME".chr(01)."\r\n";
569
2021-12-17
jrmu
# print $socket "PRIVMSG $target :".chr(01)."USERINFO".chr(01)."\r\n";
570
2021-12-17
jrmu
print $socket "PRIVMSG $target :".chr(01)."VERSION".chr(01)."\r\n";
571
2021-12-17
jrmu
# print $socket "PRIVMSG $target :".chr(01)."PING".chr(01)."\r\n";
574
2021-12-17
jrmu
sub cbind {
575
2021-12-17
jrmu
my ($type, $flags, $cmd, $proc) = @_;
576
2021-12-17
jrmu
if ($type eq "pub") {
577
2021-12-17
jrmu
push(@{$call->{pub}}, {cmd => $cmd, proc => $proc});
578
2021-12-17
jrmu
} elsif ($type eq "msg") {
579
2021-12-17
jrmu
push(@{$call->{msg}}, {cmd => $cmd, proc => $proc});
580
2021-12-17
jrmu
} elsif ($type eq "notc") {
581
2021-12-17
jrmu
push(@{$call->{notc}}, {mask => $cmd, proc => $proc});
582
2021-12-17
jrmu
} elsif ($type eq "mode") {
583
2021-12-17
jrmu
push(@{$call->{mode}}, {mask => $cmd, proc => $proc});
584
2021-12-17
jrmu
} elsif ($type eq "join") {
585
2021-12-17
jrmu
push(@{$call->{join}}, {mask => $cmd, proc => $proc});
586
2021-12-17
jrmu
} elsif ($type eq "partcall") {
587
2021-12-17
jrmu
push(@{$call->{part}}, {mask => $cmd, proc => $proc});
588
2021-12-17
jrmu
} elsif ($type eq "pubm") {
589
2021-12-17
jrmu
push(@{$call->{pubm}}, {mask => $cmd, proc => $proc});
590
2021-12-17
jrmu
} elsif ($type eq "msgm") {
591
2021-12-17
jrmu
push(@{$call->{msgm}}, {mask => $cmd, proc => $proc});
595
2021-12-17
jrmu
sub debug {
596
2021-12-17
jrmu
my ($level, $msg) = @_;
597
2021-12-17
jrmu
if ($verbose >= $level) { print "$msg\n"; }
600
2021-12-17
jrmu
sub isstaff {
601
2021-12-17
jrmu
my( $bot, $nick ) = @_;
602
2021-12-17
jrmu
if( !( $bot->{name} =~ /^$localnet$/i ) )
604
2021-12-17
jrmu
return 0;
606
2021-12-17
jrmu
my $lnick = lc $nick;
607
2021-12-17
jrmu
foreach( @stafflist ) {
608
2021-12-17
jrmu
if( $lnick eq $_ ) {
609
2021-12-17
jrmu
return 1;
612
2021-12-17
jrmu
return 0;