###PERL_LIB: use lib /botm/lib/bsta
use botm_common (
'read_data_file', 'write_data_file',
- 'join_path',
'merge_url',
'read_header_env',
'html_entity_encode_dec',
- 'url_query_decode'
+ 'url_query_decode',
+ 'open_encoded'
);
use bsta_lib (
'STATE', 'INTF_STATE',
'ong'
);
-###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
-
###PERL_CGI_PATH: CGI_PATH = /bsta/
###PERL_CGI_2WORDS_PATH: CGI_2WORDS_PATH = /bsta/2words
my $intf_pause;
my $intf_mode;
my $story_i_path;
-my $story_file;
+my $fh;
my $story_lock;
my @story_lines;
my $ong_state;
$cmd_clear_all = $settings{'password'}.' clearall';
$story_lock=0;
-if (open ($story_file, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STORY_PATH()))){
+if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STORY_PATH())) {
$story_lock=1;
- if (flock($story_file,2)) {
+ if (flock($fh,2)) {
$story_lock=2;
}
- %story = read_data_file($story_file);
+ %story = read_data_file($fh);
if ($story{'lastip'} =~ /^.+$/) {
$last_IP=$&;
0 # pause
);
}
- write_data_file($story_file, '', '', \%story);
+ write_data_file($fh, '', '', \%story);
}
if ($words ne '') {
);
}
# save new game
- write_data_file($story_file, '', '', \%new_story);
+ write_data_file($fh, '', '', \%new_story);
}
else {
$message = 'To early to finish this wordgame.';
- write_data_file($story_file, '', '', \%story);
+ write_data_file($fh, '', '', \%story);
}
}
else {
}
}
}
- write_data_file($story_file, '', '', \%story);
+ write_data_file($fh, '', '', \%story);
}
}
}
$intf_mode,
$intf_pause
);
- write_data_file($story_file, '', '', \%story);
+ write_data_file($fh, '', '', \%story);
}
@story_lines = split(/\r?\n/, $story{'content'});
if(@story_lines & 1) {
$turn = !$turn;
}
- close($story_file);
+ close($fh);
}
print "Content-type: text/html\n\n";
}
print ' <input class="intx" type="text" name="words">'."\n";
print ' <input class="inbt" type="submit" value="enter">'."\n";
- if(@story_lines >= (STORY_LENGTH-1)) {
+ if (@story_lines >= (STORY_LENGTH-1)) {
print ' <input class="inbt" type="submit" name="next" value="enter and then start a new one">'."\n";
}
print ' </form>'."\n";
'url_query_decode',
'read_data_file',
'join_path',
- 'merge_url'
+ 'merge_url',
+ 'open_encoded', 'stat_encoded'
);
use bsta_lib (
'STATE',
);
if ($try_onged) {
$file_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
- $r = open($fh, '<' , encode('locale_fs', $file_path));
+ $r = open_encoded($fh, '<' , $file_path);
if ($r) {
close($r);
$file_path = merge_url(
}
unless ($r) {
$file_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $file_name);
- $r = open($fh,'<', encode('locale_fs', $file_path));
+ $r = open_encoded($fh,'<', $file_path);
unless ($r) {
exit fail_attachment($method, $ID);
}
close($fh);
exit fail_500("Can't switch file to binary mode.");
}
- if (my @file_info = stat($file_path)) {
+ if (my @file_info = stat_encoded($file_path)) {
print 'Content-length: '.$file_info[7]."\n";
}
}
-Subproject commit 3231e70f7ba581a5ac018ca4ec2e5f9db100a19a
+Subproject commit 92789f87d558991f3b602fcc553ddc37cfe83b5c
'merge_url',
'read_header_env',
'read_data_file', 'write_data_file',
- 'join_path'
+ 'join_path',
+ 'copy_encoded', 'open_encoded'
);
-use File::Copy;
###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
}
}
else {
- unless (open ($fh, ">:encoding(UTF-8)", encode('locale_fs', $file))) {
+ unless (open_encoded($fh, ">:encoding(UTF-8)", $file)) {
return 0;
}
}
my $r = 1;
my $ong_state = int($state->{'state'});
- unless (open ($fh, ">:encoding(UTF-8)", encode('locale_fs', WWW_INDEX_PATH()))) {
+ unless (open_encoded($fh, ">:encoding(UTF-8)", WWW_INDEX_PATH())) {
return 0;
}
if ($print) {
print $in_path.' -> '.$out_path;
}
- $r = copy($in_path, $out_path);
+ $r = copy_encoded($in_path, $out_path);
if ($print) {
print (($r) ? " OK\n" : " FAIL\n");
}
'url_query_decode', 'url_query_encode',
'merge_url',
'html_entity_encode_dec',
+ 'open_encoded'
);
use bsta_lib (
'STATE', 'CHAT_STATE', 'CHAT_ACTION',
# ongoing chat
if ($page < 0) {
- if (open ($fh, "+<", encode('locale_fs', DATA_CHAT_PATH()))){
+ if (open_encoded($fh, "+<", DATA_CHAT_PATH())) {
if (flock($fh, 2)) {
%chat = read_data_file($fh);
'url_query_decode',
'read_data_file',
'join_path',
- 'merge_url'
+ 'merge_url',
+ 'open_encoded', 'stat_encoded'
);
use bsta_lib (
'STATE', 'INTF_STATE',
if ($try_onged) {
$frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
- $r = open($fh, '<' , encode('locale_fs', $frame_path));
+ $r = open_encoded($fh, '<' , $frame_path);
if ($r) {
close($r);
$frame_path = merge_url(
}
unless ($r) {
$frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
- $r = open($fh, '<' , encode('locale_fs', $frame_path));
+ $r = open_encoded($fh, '<' , $frame_path);
unless ($r) {
exit fail_open_file($method, 'image file', $frame_file);
}
exit fail_500("Can't switch file to binary mode.");
}
-if (my @file_info = stat($frame_path)){
+if (my @file_info = stat_encoded($frame_path)){
print 'Content-length: '.$file_info[7]."\n";
}
print 'Content-type: '.$frame_data{'content-type'}."\n";
# This script renames log files if they are big enough.
# Compresses or removes older log files.
#
-# Copyright (C) 2015, 2016, 2023 Balthasar Szczepański
+# Copyright (C) 2015, 2016, 2023, 2024 Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# TODO: use a real log rotate instead of this tool?
+
+use strict;
+use utf8;
+use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
+###PERL_LIB: use lib /botm/lib/bsta
+use botm_common (
+ 'join_path',
+ 'system_encoded',
+ 'opendir_encoded', 'readdir_decoded',
+ '_x_encoded', 'stat_encoded',
+ 'unlink_encoded', 'rename_encoded'
+);
+
+###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
###PERL_GZIP: GZIP = gzip
###PERL_LOG_PATH: LOG_PATH = /botm/log/bsta/
###PERL_LOGS_UNCOMPRESSED: LOGS_UNCOMPRESSED = 2
###PERL_LOGS_TOTAL: LOGS_TOTAL = 10
+binmode STDIN, ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+decode_argv();
-if ($ARGV[0] ne '') {
- $log_path = $ARGV[0];
-}
-else {
- $log_path = LOG_PATH;
-}
-if ($ARGV[1] =~ /^([0-9]+)$/) {
- $log_size_limit = $1;
-}
-else {
- $log_size_limit = LOG_SIZE_LIMIT;
-}
-if ($ARGV[2] =~ /^([0-9]+)$/) {
- $logs_total = $1;
-}
-else {
- $logs_total = LOGS_TOTAL;
-}
-if ($ARGV[3] =~ /^([0-9]+)$/) {
- $logs_uncompressed = $1;
-}
-else {
- $logs_uncompressed = LOGS_UNCOMPRESSED;
-}
+my $log_path = ($ARGV[0] ne '' ) ? $ARGV[0]: LOG_PATH();
+my $log_size_limit = ($ARGV[1] =~ /^[0-9]+$/) ? int($&) : LOG_SIZE_LIMIT();
+my $logs_total = ($ARGV[2] =~ /^[0-9]+$/) ? int($&) : LOGS_TOTAL();
+my $logs_uncompressed = ($ARGV[3] =~ /^[0-9]+$/) ? int($&) : LOGS_UNCOMPRESSED();
-if ( opendir ($dir, $log_path)) {
- while ($subpath = readdir $dir) {
- if ($subpath !~ /\.log$/) {
+if (opendir_encoded(my $dir, $log_path)) {
+ while (my $file_name = readdir_decoded($dir)) {
+ if ($file_name !~ /\.logo$/) {
next;
}
- $fullpath=$log_path.$subpath;
- unless (-f $fullpath) {
+ my $full_path = join_path(PATH_SEPARATOR(), $log_path, $file_name);
+ unless (_x_encoded('-f', $full_path)) {
next;
}
- unless (@stat = stat($fullpath)) {
+ my @stat;
+ unless (@stat = stat_encoded($full_path)) {
next;
}
if ($stat[7] > $log_size_limit) {
- movelog($fullpath,0,0);
+ move_log($full_path, 0, 0);
}
-
}
closedir($dir);
}
+else {
+ print "fail ";
+ print $log_path;
+ print "\n";
+}
-sub movelog {
+sub move_log {
(my $path, my $number, my $gz) = @_;
- my $nextgz = 0;
- my $thispath;
- my $nextpath;
- my $nextnumber=$number+1;
- my @gzip_arg = (GZIP, '-q', '-9','-f');
+ my $next_gz = 0;
+ my $this_path;
+ my $next_path;
+ my $next_number = $number + 1;
+ my @gzip_arg = (GZIP(), '-q', '-9','-f');
- $thispath = $path.(($number != 0)?'.'.$number.($gz?'.gz':''):'');
+ $this_path = $path.(
+ ($number != 0) ?
+ '.'.$number.($gz ? '.gz' : '') :
+ ''
+ );
if ($number == $logs_total) {
- if (unlink $thispath) {
+ if (unlink_encoded($this_path)) {
return 1;
}
else {
}
}
if ($number == $logs_uncompressed) {
- $nextgz=1;
- $nextpath = $path.'.'.$nextnumber.'.gz';
+ $next_gz = 1;
+ $next_path = $path.'.'.$next_number.'.gz';
}
else {
- $nextpath = $path.'.'.$nextnumber.($gz?'.gz':'');
+ $next_path = $path.'.'.$next_number.($gz ? '.gz' : '');
}
- if (-e $nextpath) {
- unless (movelog($path,$nextnumber,($nextgz or $gz)?1:0)) {
+ # TODO: consider loop instead of recursion?
+ if (_x_encoded('-e', $next_path)) {
+ unless (move_log($path, $next_number, ($next_gz or $gz) ? 1 : 0)) {
return 0;
}
}
- if ($nextgz) {
- push @gzip_arg, $thispath;
- unless (! system (@gzip_arg)) {
+ if ($next_gz) {
+ push @gzip_arg, $this_path;
+ unless (! system_encoded(GZIP(), @gzip_arg)) {
return 0;
}
- $thispath .= '.gz';
+ $this_path .= '.gz';
}
- unless (rename ($thispath, $nextpath)) {
+ unless (rename_encoded($this_path, $next_path)) {
return 0;
}
return 1;
#
# The ONG bot
#
-# Copyright (C) 2016, 2017, 2023 Balthasar Szczepański
+# Copyright (C) 2016, 2017, 2023, 2024 Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
###PERL_LIB: use lib /botm/lib/bsta
use botm_common (
- 'read_data_file', 'write_data_file'
+ 'read_data_file', 'write_data_file',
+ 'open_encoded'
);
use bsta_lib (
'STATE',
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
-unless (open ($fh, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STATE_PATH()))) {
+unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
print "NO STATEFILE\n";
}
else {
'read_header_env',
'read_data_file', 'write_data_file',
'url_query_decode',
- 'join_path'
+ 'join_path',
+ 'open_encoded'
);
use bsta_lib (
'STATE', 'TEXT_MODE', 'INTF_STATE',
$password_ok = ($password eq $settings{'password'});
# state & activation logic
-if (open ($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
+if (open_encoded($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
if (flock($state_file, 2)) {
%state = read_data_file($state_file);