3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 13:29:29 +01:00

Fix UTF-8 encoding

This commit is contained in:
Pragmatic Software 2023-03-12 16:06:04 -07:00
parent e908dd1640
commit e736051de5
6 changed files with 23 additions and 15 deletions

View File

@ -77,13 +77,16 @@ sub execute {
$stdin //= ''; $stdin //= '';
$stdin = encode('UTF-8', $stdin);
@cmdline = map { encode('UTF-8', $_) } @cmdline;
print STDERR "execute ($timeout) [$stdin] @cmdline\n"; print STDERR "execute ($timeout) [$stdin] @cmdline\n";
my ($exitval, $stdout, $stderr) = eval { my ($exitval, $stdout, $stderr) = eval {
my ($stdout, $stderr); my ($stdout, $stderr);
run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout); run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout);
my $exitval = $? >> 8; my $exitval = $? >> 8;
return ($exitval, $stdout, $stderr); return ($exitval, decode('UTF-8', $stdout), decode('UTF-8', $stderr));
}; };
if (my $exception = $@) { if (my $exception = $@) {

View File

@ -43,7 +43,7 @@ sub execute($command) {
# to get $? from pipe # to get $? from pipe
local $SIG{CHLD} = 'DEFAULT'; local $SIG{CHLD} = 'DEFAULT';
my $pid = open(my $fh, '-|', split / /, $command); my $pid = open(my $fh, '-|', split / /, encode('UTF-8', $command));
if (not defined $pid) { if (not defined $pid) {
print "Couldn't fork: $!\n"; print "Couldn't fork: $!\n";
@ -55,7 +55,7 @@ sub execute($command) {
local $SIG{ALRM} = sub { kill 9, $pid; die "Timed-out: $output\n"; }; local $SIG{ALRM} = sub { kill 9, $pid; die "Timed-out: $output\n"; };
alarm(COMPILE_TIMEOUT); alarm(COMPILE_TIMEOUT);
while (my $line = <$fh>) { while (my $line = decode('UTF-8', <$fh>)) {
$output .= $line; $output .= $line;
} }
@ -182,7 +182,7 @@ sub handle_client($client, $heartbeat) {
local $SIG{ALRM} = sub { die "Client I/O timed-out\n"; }; local $SIG{ALRM} = sub { die "Client I/O timed-out\n"; };
alarm 5; alarm 5;
while (my $line = <$client>) { while (my $line = decode('UTF-8', <$client>)) {
$line =~ s/[\r\n]+$//; $line =~ s/[\r\n]+$//;
next if $line =~ m/^\s*$/; next if $line =~ m/^\s*$/;
@ -214,7 +214,7 @@ sub handle_client($client, $heartbeat) {
$timed_out = 1; $timed_out = 1;
} }
print $client $result . "\n"; print $client encode('UTF-8', $result . "\n");
last; last;
} }
}; };
@ -238,6 +238,9 @@ sub handle_client($client, $heartbeat) {
} }
sub main() { sub main() {
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
# let OS clean-up child exits # let OS clean-up child exits
$SIG{CHLD} = 'IGNORE'; $SIG{CHLD} = 'IGNORE';

View File

@ -210,7 +210,7 @@ sub execute {
my $stdin = $self->{options}->{'-stdin'}; my $stdin = $self->{options}->{'-stdin'};
if (not length $stdin) { if (not length $stdin) {
$stdin = `fortune -u -s`; $stdin = decode('UTF-8', `fortune -u -s`);
$stdin =~ s/[\n\r\t]/ /msg; $stdin =~ s/[\n\r\t]/ /msg;
$stdin =~ s/:/ - /g; $stdin =~ s/:/ - /g;
$stdin =~ s/\s+/ /g; $stdin =~ s/\s+/ /g;
@ -270,7 +270,7 @@ sub execute {
$compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'}; $compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'};
my $compile_json = encode_json($compile_in); my $compile_json = encode_json($compile_in);
$compile_json .= encode('UTF-8', "\n:end:\n"); $compile_json .= "\n:end:\n";
my $length = length $compile_json; my $length = length $compile_json;
my $sent = 0; my $sent = 0;

1
applets/urban vendored
View File

@ -5,6 +5,7 @@
use warnings; use warnings;
use strict; use strict;
use utf8;
use WebService::UrbanDictionary; use WebService::UrbanDictionary;
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromString);

View File

@ -78,10 +78,6 @@ sub launch_applet {
my ($exitval, $stdout, $stderr) = eval { my ($exitval, $stdout, $stderr) = eval {
my $args = $context->{arguments}; my $args = $context->{arguments};
if (not $context->{args_utf8}) {
$args = encode('UTF-8', $args);
}
my $strip_quotes = 1; my $strip_quotes = 1;
$strip_quotes = 0 if $self->{pbot}->{factoids}->{data}->{storage}->get_data($channel, $trigger, 'keep-quotes'); $strip_quotes = 0 if $self->{pbot}->{factoids}->{data}->{storage}->get_data($channel, $trigger, 'keep-quotes');
@ -92,12 +88,17 @@ sub launch_applet {
my ($stdin, $stdout, $stderr); my ($stdin, $stdout, $stderr);
# encode as UTF-8 if not already encoded (e.g. by encode_json)
if (not $context->{args_utf8}) {
@cmdline = map { encode('UTF-8', $_) } @cmdline;
}
run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout); run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout);
my $exitval = $? >> 8; my $exitval = $? >> 8;
utf8::decode $stdout; $stdout = decode('UTF-8', $stdout);
utf8::decode $stderr; $stderr = decode('UTF-8', $stderr);
return ($exitval, $stdout, $stderr); return ($exitval, $stdout, $stderr);
}; };

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script # These are set by the /misc/update_version script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 4624, BUILD_REVISION => 4626,
BUILD_DATE => "2023-02-24", BUILD_DATE => "2023-03-12",
}; };
sub initialize {} sub initialize {}