Commit Diff


commit - 0744bd0b8629c5e120a268c37b6930b0a10f485b
commit + b0ff9f00cb48f1f107ef15b2abd6ebcfe96b66e0
blob - 18c5a053b4f98afc2461d60a52b541ddf5a36b09
blob + f488f2aab7fb9f9347d94a26ba03eb4cdcaf071c
--- BNC.pm
+++ BNC.pm
@@ -7,6 +7,7 @@ use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
 use Digest::SHA qw(sha256_hex);
+use IRCNOW::IO qw(readarray);
 use lib './';
 require "SQLite.pm";
 require "Hash.pm";
@@ -70,7 +71,7 @@ sub init {
 # the same net name; znc ignores addnetwork commands when a network already exists
 sub readnetworks {
 	my ($filename) = @_;
-	my @lines = main::readarray($filename);
+	my @lines = readarray($filename);
 	my @networks;
 	foreach my $line (@lines) {
 		if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
@@ -539,7 +540,7 @@ sub mlastseen {
 	#print "treeget\n";
 	#print Dumper \treeget($znctree, "User", "Node");
 	#print Dumper \treeget($znctree, "User", "Network", "Node");
-#my @zncconf = main::readarray($zncconfpath);
+#my @zncconf = readarray($zncconfpath);
 #$znctree;
 #foreach my $line (@zncconf) {
 #	if ($line =~ /<User (.*)>/) {
blob - /dev/null
blob + b6261c931f0996160a00ccdfabe32d8647524398 (mode 644)
--- /dev/null
+++ .gitignore
@@ -0,0 +1 @@
+.*~
blob - ad797c7bf421778b4515b7ced6a616e96b7a2b01
blob + 1685f617ad80fac4399878937024c98749fea2f6
--- DNS.pm
+++ DNS.pm
@@ -6,6 +6,8 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+use IRCNOW::IO qw(readarray writefile appendfile);
+
 use File::Copy qw(copy);
 
 my %conf = %main::conf;
@@ -126,7 +128,7 @@ sub mreadip6s {
 # Return list of ipv6 addresses from filename
 sub readip6s {
 	my ($filename) = @_;
-	my @lines = main::readarray($filename);
+	my @lines = readarray($filename);
 	my @ipv6s;
 	foreach my $line (@lines) {
 		if ($line =~ /^\s*inet6\s+(alias\s+)?([0-9a-f:]{4,})\s+[0-9]+\s*$/i) {
@@ -165,7 +167,7 @@ sub delrdns {
 sub setdns {
 	my ($name, $origin, $type, $value) = @_;
 	my $filename = "$zonedir/$origin";
-	my @lines = main::readarray($filename);
+	my @lines = readarray($filename);
 	foreach my $line (@lines) {
 		# increment the zone's serial number
 		if ($line =~ /(\d{8})(\d{2})((\s+\d+){4}\s*\))/) {
@@ -181,7 +183,7 @@ sub setdns {
         	push(@lines, "$name	3600	IN	$type	$value");
 	}
 	# trailing newline necessary
-	main::writefile("$filename.bak", join("\n", @lines)."\n");
+	writefile("$filename.bak", join("\n", @lines)."\n");
 	copy "$filename.bak", $filename;
 	if (system("doas -u _nsd nsd-control reload")) {
 		return 0;
@@ -257,7 +259,7 @@ sub nextdns {
 	while (grep(/$newip6/, @allip6s)) {
 		$newip6 = ip6mask($ip6, $ip6subnet,randip6());
 	}
-	main::appendfile($hostnameif, "inet6 alias $newip6 48\n");
+	appendfile($hostnameif, "inet6 alias $newip6 48\n");
 	`doas ifconfig vio0 inet6 $newip6/48`;
 	if (setdns($subdomain, $hostname, "A", $ip4) && setdns($subdomain, $hostname, "AAAA", $newip6) && setrdns($newip6, $ip6subnet, "$subdomain.$hostname")) {
 		return "$newip6";
blob - b0f8dc9f896801ed80cf43e99367f31ffcf14a67
blob + e9bbdfa1c63860ea5068cfd1e72ee0ea957b3468
--- Hash.pm
+++ Hash.pm
@@ -6,6 +6,10 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+
+use IRCNOW::IO qw(readarray);
+
+
 use Data::Dumper;
 
 my %conf = %main::conf;
@@ -13,7 +17,7 @@ my @words;
 my $wordspath = "words";
 my $passlength = $conf{passlength};
 # dictionary words for passwords
-@words = main::readarray("words");
+@words = readarray("words");
 
 sub init {
 	unveil($wordspath, "r") or die "Unable to unveil $!";
blob - d62b6590fa4a32e12ed973c95b93d8ef33f59340
blob + cd15671b15777423dbfd69225ce598403d9491e7
--- Mail.pm
+++ Mail.pm
@@ -6,6 +6,8 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+use IRCNOW::IO qw(readarray writefile);
+
 use File::Copy qw(copy);
 use MIME::Base64;
 use Digest::SHA qw(sha256_hex);
@@ -72,14 +74,14 @@ sub mmail {
 		return;
 	} elsif (main::isstaff($bot, $nick) && $text =~ /^approve\s+([[:ascii:]]+)/) {
 		my $username = $1;
-		my @passwd = main::readarray($passwdpath);
+		my @passwd = readarray($passwdpath);
 		foreach my $line (@passwd) {
 			$line =~ s/^#(${username}\@${mailhostname}.*)/$1/;
 		}
 		# trailing newline necessary
 		`doas touch $passwdpath.bak`;
 		`doas chmod g+w $passwdpath.bak`;
-		main::writefile("$passwdpath.bak", join("\n", @passwd)."\n");
+		writefile("$passwdpath.bak", join("\n", @passwd)."\n");
 		copy "${passwdpath}.bak", $passwdpath;
 
 		foreach my $chan (@teamchans) {
@@ -118,14 +120,14 @@ sub mmail {
 		sleep(5);
 		mailmail($username, $pass, $email);
 		if ($approval) {
-			my @passwd = main::readarray($passwdpath);
+			my @passwd = readarray($passwdpath);
 			foreach my $line (@passwd) {
 				$line =~ s/^(${username}\@${mailhostname}.*)/#$1/;
 			}
 			# trailing newline necessary
 			`doas touch $passwdpath.bak`;
 			`doas chmod g+w $passwdpath.bak`;
-			main::writefile("$passwdpath.bak", join("\n", @passwd)."\n");
+			writefile("$passwdpath.bak", join("\n", @passwd)."\n");
 			copy "${passwdpath}.bak", $passwdpath;
 
 			main::putserv($bot, "PRIVMSG $nick :Your account has been created but must be manually approved by your admins ($staff) before it can be used.");
@@ -250,9 +252,9 @@ sub createmail {
 
 sub deletemail {
 	my ($username) = @_;
-	my @passwd = main::readarray($passwdpath);
-	my @virtuals = main::readarray($virtualspath);
-	my @senders = main::readarray($senderspath);
+	my @passwd = readarray($passwdpath);
+	my @virtuals = readarray($virtualspath);
+	my @senders = readarray($senderspath);
 	@passwd = grep !/^${username}\@${mailhostname}/, @passwd;
 	@virtuals = grep !/^${username}\@${mailhostname}/, @virtuals;
 	@senders = grep !/^${username}\@${mailhostname}/, @senders;
@@ -262,11 +264,11 @@ sub deletemail {
 	`doas touch $virtualspath.bak`;
 	`doas touch $senderspath.bak`;
 	`doas chmod g+w $passwdpath.bak $virtualspath.bak $senderspath.bak`;
-	main::writefile("$passwdpath.bak", join("\n", @passwd)."\n");
+	writefile("$passwdpath.bak", join("\n", @passwd)."\n");
 	copy "${passwdpath}.bak", $passwdpath;
-	main::writefile("$virtualspath.bak", join("\n", @virtuals)."\n");
+	writefile("$virtualspath.bak", join("\n", @virtuals)."\n");
 	copy "${virtualspath}.bak", $virtualspath;
-	main::writefile("$senderspath.bak", join("\n", @senders)."\n");
+	writefile("$senderspath.bak", join("\n", @senders)."\n");
 	copy "${senderspath}.bak", $senderspath;
 
 	`doas smtpctl update table passwd`;
blob - 463e5263fa5b53ebabf0ad940cbae3e69e534db4
blob + 191d2b61fbb878edf293ee222da31d43cc81504f
--- SQLite.pm
+++ SQLite.pm
@@ -6,6 +6,8 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+use IRCNOW::IO qw(readstr);
+
 use Data::Dumper;
 use DBI;
 use DBD::SQLite;
@@ -171,7 +173,7 @@ sub connectdb {
 		FetchHashKeyName => 'NAME_lc',
 	}) or die "Couldn't connect to database: " . $DBI::errstr;
 	if (!(-s "$dbpath")) {
-		my $sql = main::readstr('table.sql');
+		my $sql = readstr('table.sql');
 		my @sql = split /;/m, $sql;
 		foreach my $s (@sql) {
 			$dbh->do($s);
blob - 5f27c60c151c0dfefff4dbc5ac70e4099004e6b9
blob + a4e6ce428e3a34b87fa5c61695d12630cb2f43fd
--- Sh.pm
+++ Sh.pm
@@ -6,6 +6,8 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+use IRCNOW::IO qw(readarray);
+
 use Data::Dumper;
 
 my $authlog = "/var/log/authlog";
@@ -38,11 +40,3 @@ foreach my $user (@users) {
 		print "$user => Never logged in\n";
 	}
 }
-#warn Dumper \$loglines[1];
-sub readarray {
-	my ($filename) = @_;
-	open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
-	chomp(my @lines = <$fh>);
-	close $fh;
-	return @lines;
-}
blob - 4a204786533077e78963583e6aa94f1f7929590d
blob + e893ad49cdec5dd28e79c5a90a5f7fe46ae7bd8a
--- Shell.pm
+++ Shell.pm
@@ -6,6 +6,7 @@ use strict;
 use warnings;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
+use IRCNOW::IO qw(:FILEIO);
 use MIME::Base64;
 use Data::Dumper;
 use Digest::SHA qw(sha256_hex);
@@ -315,7 +316,7 @@ server "$lusername.$hostname" {
 	root "/htdocs/$username"
 }
 EOF
-	main::appendfile($httpdconfpath, $block);
+	appendfile($httpdconfpath, $block);
 	$block = <<"EOF";
 domain "$lusername.$hostname" {
 	domain key "/etc/ssl/private/$lusername.$hostname.key"
@@ -323,7 +324,7 @@ domain "$lusername.$hostname" {
 	sign with letsencrypt
 }
 EOF
-	main::appendfile($acmeconfpath, $block);
+	appendfile($acmeconfpath, $block);
 	configurepf($username);
 	system "doas rcctl reload httpd";
         system "doas acme-client -F $lusername.$hostname";
@@ -346,7 +347,7 @@ sub deleteshell {
 	system "doas groupdel $username";
 	system "doas userdel $username";
 	system "doas rm -f /etc/ssl/$lusername.$hostname.crt /etc/ssl/$lusername.$hostname.fullchain.pem /etc/ssl/private/$lusername.$hostname.key";
-	my $httpdconf = main::readstr($httpdconfpath);
+	my $httpdconf = readstr($httpdconfpath);
 	my $block = <<"EOF";
 server "$lusername.$hostname" {
 	listen on * port 80
@@ -366,9 +367,9 @@ EOF
 	$block =~ s/\*/\\*/gm;
 	$httpdconf =~ s{$block}{}gm;
 	print $httpdconf;
-	main::writefile($httpdconfpath, $httpdconf);
+	writefile($httpdconfpath, $httpdconf);
 	
-	my $acmeconf = main::readstr($acmeconfpath);
+	my $acmeconf = readstr($acmeconfpath);
 	$block = <<"EOF";
 domain "$lusername.$hostname" {
 	domain key "/etc/ssl/private/$lusername.$hostname.key"
@@ -381,7 +382,7 @@ EOF
 	$block =~ s/\./\\./gm;
 	$block =~ s/\*/\\*/gm;
 	$acmeconf =~ s{$block}{}gm;
-	main::writefile($acmeconfpath, $acmeconf);
+	writefile($acmeconfpath, $acmeconf);
 	return 1;
 }
 
@@ -389,7 +390,7 @@ EOF
 # Return column $i from $filename as an array with file separator $FS
 sub col {
 	my ($filename, $i, $FS) = @_;
-	my @rows = main::readarray($filename);
+	my @rows = readarray($filename);
 	my @results;
 	foreach my $row (@rows) {
 		if ($row =~ /^(.*?)$FS/) {
@@ -401,7 +402,7 @@ sub col {
 
 sub configurepf {
     my $username = shift;
-    my @read = split('\n', main::readstr($pfconfpath) );
+    my @read = split('\n', readstr($pfconfpath) );
     
     my $previousline = "";
     my @pfcontent;
@@ -423,13 +424,13 @@ sub configurepf {
         }
         push(@pfcontent, $currline)
     }
-    main::writefile("$pfconfpath", join("\n",@pfcontent))
+    writefile("$pfconfpath", join("\n",@pfcontent))
 }
 
 sub configurerelayd {  
         my ($username) = @_;                 
 	my $block = "tls { keypair $username.$hostname }";
-	my $relaydconf = main::readstr($relaydconfpath);
+	my $relaydconf = readstr($relaydconfpath);
 	my $newconf;
 	if ($relaydconf =~ /^.*tls\s+{\s+keypair\s+[.0-9a-zA-Z]+\s*}/m) {
 		$newconf = "$`$&\n\t$block$'";
@@ -437,7 +438,7 @@ sub configurerelayd {  
 		$newconf = $relaydconf;
 		main::debug(ERRORS, "ERROR: regex can't match tls { keypair \$username.$hostname }");
 	}
-	main::writefile($relaydconfpath, $newconf);
+	writefile($relaydconfpath, $newconf);
 }
 
 #unveil("./newacct", "rx") or die "Unable to unveil $!";
blob - dc0a0c623d46a907451b5e315dc9aa6a8f1e642e
blob + 2507d2f20186a48c5a30430971fc10f51c0c5e3d
--- botnow
+++ botnow
@@ -7,9 +7,14 @@ use IO::Socket;
 use IO::Select;
 use OpenBSD::Pledge;
 use OpenBSD::Unveil;
-use File::Copy qw(copy);
-use File::Basename;
+use lib "./lib";
+use IRCNOW::IO qw(readarray); # for readarray
 
+#IB Moved to IRCNOW::IO.pm
+#IB
+#IBuse File::Copy qw(copy);
+#IBuse File::Basename;
+
 # Path to configuration file
 my $confpath = "botnow.conf";
 my $backupspath = "/home/botnow/backups/";
@@ -21,41 +26,43 @@ sub date {
 	return $localtime;
 }
 
-# Read from filename and return array of lines without trailing newlines
-sub readarray {
-	my ($filename) = @_;
-	open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
-	chomp(my @lines = <$fh>);
-	close $fh;
-	return @lines;
-}
-
-# Read from filename and return as string
-sub readstr {
-	my ($filename) = @_;
-	open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
-	my $str = do { local $/; <$fh> };
-	close $fh;
-	return $str;
-}
-
-# Write str to filename
-sub writefile {
-	my ($filename, $str) = @_;
-	my $date = date();
-	copy($filename, $backupspath.basename($filename).".".date()) or die "Could not make backup of $filename";
-	open(my $fh, '>', "$filename") or die "Could not write to $filename";
-	print $fh $str;
-	close $fh;
-}
-
-# Append str to filename
-sub appendfile {
-	my ($filename, $str) = @_;
-	open(my $fh, '>>', "$filename") or die "Could not append to $filename";
-	print $fh $str;
-	close $fh;
-}
+#IB Moved to IRCNOW::IO.pm
+#IB
+#IB# Read from filename and return array of lines without trailing newlines
+#IBsub readarray {
+#IB	my ($filename) = @_;
+#IB	open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
+#IB	chomp(my @lines = <$fh>);
+#IB	close $fh;
+#IB	return @lines;
+#IB}
+#IB
+#IB# Read from filename and return as string
+#IBsub readstr {
+#IB	my ($filename) = @_;
+#IB	open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
+#IB	my $str = do { local $/; <$fh> };
+#IB	close $fh;
+#IB	return $str;
+#IB}
+#IB
+#IB# Write str to filename
+#IBsub writefile {
+#IB	my ($filename, $str) = @_;
+#IB	my $date = date();
+#IB	copy($filename, $backupspath.basename($filename).".".date()) or die "Could not make backup of $filename";
+#IB	open(my $fh, '>', "$filename") or die "Could not write to $filename";
+#IB	print $fh $str;
+#IB	close $fh;
+#IB}
+#IB
+#IB# Append str to filename
+#IBsub appendfile {
+#IB	my ($filename, $str) = @_;
+#IB	open(my $fh, '>>', "$filename") or die "Could not append to $filename";
+#IB	print $fh $str;
+#IB	close $fh;
+#IB}
 
 # Returns timestamp in "Day MM DD HH:MM:SS" format
 sub gettime {