Commit Diff
Diff:
acd92bcf42b061ba4395363ef5b6ae2c65e87cb3
6e8dadd8eca2128a51e7bfe06365317ace659289
Commit:
6e8dadd8eca2128a51e7bfe06365317ace659289
Tree:
9b3e1e79d7d1c547c0ca84487460049cc107b014
Author:
jrmu <jrmu@lecturify.com>
Committer:
jrmu <jrmu@lecturify.com>
Date:
Thu Sep 24 08:05:57 2020 UTC
Message:
Refactored code
blob - 7cccbed62c96573d71b02d0eba94adc6b8dba21e
blob + e355a40adddacd77f32a22fff7d7bed731f76225
--- botnow.pl
+++ botnow.pl
@@ -21,7 +21,7 @@ foreach my $line (readfile($confpath)) {
} elsif ($line =~ /^([-_a-zA-Z0-9]+)\s*=\s*([[:print:]]+)$/) {
$conf{$1} = $2;
} else {
- die "ERROR: botnow.conf: $line";
+ die "ERROR: botnow.conf format invalid: $line";
}
}
@@ -146,6 +146,7 @@ my $dbh;
my $fh;
if (defined($teamchans)) { @teamchans = split /,/m, $teamchans; }
+# Read from filename and return all lines without trailing newlines
sub readfile {
my ($filename) = @_;
my (@lines, $fh);
@@ -154,6 +155,7 @@ sub readfile {
close $fh;
return @lines;
}
+# Write str to filename
sub writefile {
my ($filename, $str) = @_;
my (@lines, $fh);
@@ -162,32 +164,38 @@ sub writefile {
close $fh;
}
-# Load list of networks
-# To add multiple servers for a single network, simply create a new
-# entry with the same net name; znc ignores addnetwork commands when a network
-# already exists
-my @lines = readfile($netpath);
-foreach my $line (@lines) {
- if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
- next;
- } elsif ($line =~ /^\s*([-a-zA-Z0-9]+)\s*([-_.:a-zA-Z0-9]+)\s*(~|\+)?([0-9]+)\s*$/) {
- my ($name, $server, $port) = ($1, $2, $4);
- my $trustcerts;
- if (!defined($3)) {
- $trustcerts = 0;
- } elsif ($3 eq "~") { # Use SSL but trust all certs
- $port = "+".$port;
- $trustcerts = 1;
- } else { # Use SSL and verify certs
- $port = "+".$port;
- $trustcerts = 0;
+# Return list of networks from filename
+# To add multiple servers for a single network, simply create a new entry with
+# the same net name; znc ignores addnetwork commands when a network already exists
+sub readnetworks {
+ my ($filename) = @_;
+ my @lines = readfile($filename);
+ my @networks;
+ foreach my $line (@lines) {
+ if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
+ next;
+ } elsif ($line =~ /^\s*([-a-zA-Z0-9]+)\s*([-_.:a-zA-Z0-9]+)\s*(~|\+)?([0-9]+)\s*$/) {
+ my ($name, $server, $port) = ($1, $2, $4);
+ my $trustcerts;
+ if (!defined($3)) {
+ $trustcerts = 0;
+ } elsif ($3 eq "~") { # Use SSL but trust all certs
+ $port = "+".$port;
+ $trustcerts = 1;
+ } else { # Use SSL and verify certs
+ $port = "+".$port;
+ $trustcerts = 0;
+ }
+ push(@networks, {"name" => $name, "server" => $server, "port" => $port, "trustcerts" => $trustcerts });
+ } else {
+ die "network format invalid: $line\n";
}
- push(@networks, {"name" => $name, "server" => $server, "port" => $port, "trustcerts" => $trustcerts });
- } else {
- die "network format invalid: $line\n";
}
+ return @networks;
}
+@networks = readnetworks($netpath);
+
# networks must be sorted to avoid multiple connections
@networks = sort @networks;
@@ -195,35 +203,39 @@ foreach my $line (@lines) {
@words = readfile("words");
# Validate ipv6s if it exists, otherwise load addresses from /etc/hostname.if
+my @lines;
-if (!(-s "$ipv6path")) {
- print "No IPv6 addresses in $ipv6path, loading from $hostnameif...\n";
- @lines = readfile($hostnameif);
- my $ipv6s;
+# Given filename, return a list of ipv6 addresses
+sub readipv6s {
+ my ($filename) = @_;
+ my @lines = readfile($filename);
+ my @ipv6s;
foreach my $line (@lines) {
if ($line =~ /^\s*inet6 (alias )?([0-9a-f:]{4,}) [0-9]+\s*$/i) {
- $ipv6s .= "$2\n";
+ push(@ipv6s, $2);
+ } elsif ($line =~ /^\s*([0-9a-f:]{4,})\s*$/i) {
+ push(@ipv6s, $1);
}
}
- writefile($ipv6path, $ipv6s);
+ return @ipv6s;
}
+my @ipv6s;
-@lines = readfile($ipv6path);
-my $ipv6s;
-foreach my $line (@lines) {
- if ($line =~ /^\s*([0-9a-f:]{4,})\s*$/i) {
- $ipv6s .= "$1\n";
- }
+if (!(-s "$ipv6path")) {
+ print "No IPv6 addresses in $ipv6path, loading from $hostnameif...\n";
+ @ipv6s = readipv6s($hostnameif);
+ if (!@ipv6s) { die "No IPv6 addresses in $hostnameif!"; }
+} else {
+ @ipv6s = readipv6s($ipv6path);
}
-writefile($ipv6path, $ipv6s);
# create sockets
my $sel = IO::Select->new( );
-my $lastname = "";
+my $lastnet = "";
foreach my $network (@networks) {
# avoid duplicate connections
- if ($lastname eq $network->{name}) { next; }
- $lastname = $network->{name};
+ if ($lastnet eq $network->{name}) { next; }
+ $lastnet = $network->{name};
my $socket = IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port, Proto=>'tcp', Timeout=>'300') || print "Failed to establish connection\n";
$sel->add($socket);
my $tmp = {"sock" => $socket};
@@ -244,11 +256,9 @@ while(my @ready = $sel->can_read) {
if (!defined($response = <$socket>)) {
next;
}
- if ($response =~ /^PING :ZNC\r\n$/i) {
- print $socket "PONG :ZNC\r\n";
- if ($bot->{name} =~ /$localnet/i) {
- updaterecords();
- }
+ if ($response =~ /^PING :(ZNC)\r\n$/i) {
+ my $reply = $1;
+ parseping($bot, $reply, $localnet);
} elsif ($response =~ /^:irc.znc.in (.*) (.*) :(.*)\r\n$/) {
my ($type, $target, $reply) = ($1, $2, $3);
parseznc($bot, $type, $target, $reply);
@@ -279,6 +289,15 @@ while(my @ready = $sel->can_read) {
}
}
+sub parseping {
+ my ($bot, $reply, $localnet) = @_;
+ my $socket = $bot->{sock};
+ print $socket "PONG :ZNC\r\n";
+ if ($bot->{name} =~ /$localnet/i) {
+ updaterecords();
+ }
+}
+
sub parseznc {
my ($bot, $type, $target, $reply) = @_;
my $socket = $bot->{sock};
@@ -1093,15 +1112,8 @@ sub dns {
sub nextdns {
my ($username) = @_;
my $lines;
- open($fh, '+<', "$ipv6path") or die "Could not read file 'ipv6s' $!";
- chomp(my $ipv6 = <$fh>);
- while(my $line = <$fh>){
- $lines .= $line;
- }
- close $fh;
- open($fh, '>', "$ipv6path") or die "Could not write file 'ipv6s' $!";
- print $fh $lines;
- close $fh;
+ my $ipv6 = shift(@ipv6s);
+ writefile($ipv6path, join("\n", @ipv6s));
if (dns("$username.$hostname", $ipv6)) {
return "$username.$hostname";
}
blob - 38afb89e7c1fa61a670f89a2030b307d64e191e3
blob + 3523f3fabd0f301127b6972f0f2070233b0f8987
--- networks
+++ networks
@@ -40,8 +40,10 @@ bsdunix irc.bsdunix.us +6697
canternet irc.canternet.org +6697
chaat irc.chaat.fr 6667
chat4all irc.chat4all.org ~6697
+chatasia irc.chatasia.net ~6697
chatgentr chat.gen.tr 6667
chathispano irc.chathispano.com +6697
+chathub irc.chathub.org +6697
chating irc.chating.id 6667
chatlatino irc.chatlatinos.cl 9090
chatlounge irc.chatlounge.net +6697
@@ -131,6 +133,7 @@ irc4fun irc.irc4fun.net 6667
irccloud irc.irccloud.com +6697
ircfreakz irc.ircfreakz.net ~6697
ircgate irc.ircgate.it 6667
+irchaven irc.irchaven.org 6667
irchighway irc.irchighway.net ~6697
ircnet open.ircnet.net 6667
ircnet ircnet.hostsailor.com ~6697