3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-07 03:49:04 +02:00

Fix Unicode encoding issues

This commit is contained in:
Pragmatic Software 2019-06-30 15:08:18 -07:00
parent 00eac49ca9
commit 2cdb70c5bf
12 changed files with 50 additions and 44 deletions

View File

@ -96,7 +96,7 @@ sub save {
my $json = JSON->new; my $json = JSON->new;
$json->space_before(0); $json->space_before(0);
my $json_text = $json->pretty->encode($self->{hash}); my $json_text = $json->pretty->utf8->encode($self->{hash});
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n"; open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
print FILE "$json_text\n"; print FILE "$json_text\n";

View File

@ -113,6 +113,8 @@ sub execute_module {
$stuff->{result} = `./$module $stuff->{arguments} 2>> $module-stderr`; $stuff->{result} = `./$module $stuff->{arguments} 2>> $module-stderr`;
chomp $stuff->{result}; chomp $stuff->{result};
utf8::decode($stuff->{result});
my $json = encode_json $stuff; my $json = encode_json $stuff;
print $writer "$json\n"; print $writer "$json\n";
exit 0; exit 0;

View File

@ -387,10 +387,6 @@ sub find_factoid {
sub escape_json { sub escape_json {
my ($self, $text) = @_; my ($self, $text) = @_;
my $thing = {thing => $text}; my $thing = {thing => $text};
# not sure why we need this here, but it seems to stop strange
# text encoding issues in the following encode_json call
use Encode;
$thing->{thing} = decode('utf8', $thing->{thing});
my $json = encode_json $thing; my $json = encode_json $thing;
$json =~ s/^{".*":"//; $json =~ s/^{".*":"//;
$json =~ s/"}$//; $json =~ s/"}$//;
@ -595,11 +591,6 @@ sub expand_action_arguments {
%h = (args => $input); %h = (args => $input);
} }
# not sure why we need this here, but it seems to stop strange
# text encoding issues in the following encode_json call
use Encode;
$h{args} = decode('utf8', $h{args});
my $jsonargs = encode_json \%h; my $jsonargs = encode_json \%h;
$jsonargs =~ s/^{".*":"//; $jsonargs =~ s/^{".*":"//;
$jsonargs =~ s/"}$//; $jsonargs =~ s/"}$//;
@ -712,11 +703,6 @@ sub execute_code_factoid_using_vm {
$h{'persist-key'} = $self->{factoids}->hash->{$stuff->{channel}}->{$stuff->{keyword}}->{'persist-key'}; $h{'persist-key'} = $self->{factoids}->hash->{$stuff->{channel}}->{$stuff->{keyword}}->{'persist-key'};
} }
# not sure why we need this here, but it seems to stop strange
# text encoding issues in the following encode_json call
use Encode;
$h{arguments} = decode('utf8', $h{arguments});
my $json = encode_json \%h; my $json = encode_json \%h;
$stuff->{special} = 'code-factoid'; $stuff->{special} = 'code-factoid';

View File

@ -78,7 +78,6 @@ sub load {
}; };
$self->{hash} = decode_json $contents; $self->{hash} = decode_json $contents;
close FILE; close FILE;
} }
@ -97,7 +96,7 @@ sub save {
my $json = JSON->new; my $json = JSON->new;
$json->space_before(0); $json->space_before(0);
my $json_text = $json->pretty->encode($self->{hash}); my $json_text = $json->pretty->utf8->encode($self->{hash});
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n"; open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
print FILE "$json_text\n"; print FILE "$json_text\n";

View File

@ -21,6 +21,7 @@ use IO::Socket;
use IO::Socket::INET; use IO::Socket::INET;
use Symbol; use Symbol;
use Carp; use Carp;
use Encode;
# all this junk below just to conditionally load a module # all this junk below just to conditionally load a module
# sometimes even perl is braindead... # sometimes even perl is braindead...
@ -41,7 +42,8 @@ use vars (
# The names of the methods to be handled by &AUTOLOAD. # The names of the methods to be handled by &AUTOLOAD.
my %autoloaded = ( 'ircname' => undef, my %autoloaded = (
'ircname' => undef,
'port' => undef, 'port' => undef,
'username' => undef, 'username' => undef,
'socket' => undef, 'socket' => undef,
@ -49,10 +51,11 @@ my %autoloaded = ( 'ircname' => undef,
'parent' => undef, 'parent' => undef,
'hostname' => undef, 'hostname' => undef,
'pacing' => undef, 'pacing' => undef,
'utf8' => undef,
'ssl' => undef, 'ssl' => undef,
'ssl_ca_path' => undef, 'ssl_ca_path' => undef,
'ssl_ca_file' => undef, 'ssl_ca_file' => undef,
); );
# This hash will contain any global default handlers that the user specifies. # This hash will contain any global default handlers that the user specifies.
@ -81,6 +84,7 @@ sub new {
_ssl => 0, # no ssl by default _ssl => 0, # no ssl by default
_ssl_ca_path => undef, _ssl_ca_path => undef,
_ssl_ca_file => undef, _ssl_ca_file => undef,
_utf8 => 0,
_format => { 'default' => "[%f:%t] %m <%d>", }, _format => { 'default' => "[%f:%t] %m <%d>", },
}; };
@ -231,6 +235,7 @@ sub connect {
$self->ircname($arg{'Ircname'}) if exists $arg{'Ircname'}; $self->ircname($arg{'Ircname'}) if exists $arg{'Ircname'};
$self->username($arg{'Username'}) if exists $arg{'Username'}; $self->username($arg{'Username'}) if exists $arg{'Username'};
$self->pacing($arg{'Pacing'}) if exists $arg{'Pacing'}; $self->pacing($arg{'Pacing'}) if exists $arg{'Pacing'};
$self->utf8($arg{'UTF8'}) if exists $arg{'UTF8'};
$self->ssl($arg{'SSL'}) if exists $arg{'SSL'}; $self->ssl($arg{'SSL'}) if exists $arg{'SSL'};
$self->ssl_ca_path($arg{'SSL_ca_path'}) if exists $arg{'SSL_ca_path'}; $self->ssl_ca_path($arg{'SSL_ca_path'}) if exists $arg{'SSL_ca_path'};
$self->ssl_ca_file($arg{'SSL_ca_file'}) if exists $arg{'SSL_ca_file'}; $self->ssl_ca_file($arg{'SSL_ca_file'}) if exists $arg{'SSL_ca_file'};
@ -890,6 +895,9 @@ sub parse {
} }
PARSELOOP: foreach $line (@lines) { PARSELOOP: foreach $line (@lines) {
if ($self->{_utf8}) {
utf8::decode($line);
}
# Clean the lint filter every 2 weeks... # Clean the lint filter every 2 weeks...
$line =~ s/[\012\015]+$//; $line =~ s/[\012\015]+$//;
@ -1439,6 +1447,11 @@ sub sl_real {
return unless defined $self->socket; return unless defined $self->socket;
if ($self->{_utf8}) {
$line = encode('UTF-8', $line);
}
my $rv = eval {
# RFC compliance can be kinda nice... # RFC compliance can be kinda nice...
my $rv = $self->ssl ? my $rv = $self->ssl ?
$self->socket->print("$line\015\012") : $self->socket->print("$line\015\012") :
@ -1448,6 +1461,12 @@ sub sl_real {
return; return;
} }
return $rv; return $rv;
};
if ($@) {
print "Attempt to send bad line: [$line]\n";
}
return $rv;
} }
# Tells any server that you're an oper on to disconnect from the IRC network. # Tells any server that you're an oper on to disconnect from the IRC network.

View File

@ -72,7 +72,7 @@ sub begin {
$self->{pbot}->{logger}->log("Opening message history SQLite database: $self->{filename}\n"); $self->{pbot}->{logger}->log("Opening message history SQLite database: $self->{filename}\n");
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1 }) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1 }) or die $DBI::errstr;
$self->{dbh}->sqlite_enable_load_extension(my $_enabled = 1); $self->{dbh}->sqlite_enable_load_extension(my $_enabled = 1);
$self->{dbh}->prepare("SELECT load_extension('/usr/lib/sqlite3/pcre.so')"); $self->{dbh}->prepare("SELECT load_extension('/usr/lib/sqlite3/pcre.so')");

View File

@ -7,8 +7,6 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
# $Id$
package PBot::PBot; package PBot::PBot;
use strict; use strict;
@ -182,6 +180,7 @@ sub connect {
Ircname => $self->{registry}->get_value('irc', 'ircname'), Ircname => $self->{registry}->get_value('irc', 'ircname'),
Server => $server, Server => $server,
Pacing => 1, Pacing => 1,
UTF8 => 1,
SSL => $self->{registry}->get_value('irc', 'SSL'), SSL => $self->{registry}->get_value('irc', 'SSL'),
SSL_ca_file => $self->{registry}->get_value('irc', 'SSL_ca_file'), SSL_ca_file => $self->{registry}->get_value('irc', 'SSL_ca_file'),
SSL_ca_path => $self->{registry}->get_value('irc', 'SSL_ca_path'), SSL_ca_path => $self->{registry}->get_value('irc', 'SSL_ca_path'),

View File

@ -72,7 +72,7 @@ SQL
sub dbi_begin { sub dbi_begin {
my ($self) = @_; my ($self) = @_;
eval { eval {
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1 }) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1 }) or die $DBI::errstr;
}; };
if ($@) { if ($@) {

View File

@ -57,7 +57,7 @@ sub create_database {
my $self = shift; my $self = shift;
eval { eval {
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1 }) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1 }) or die $DBI::errstr;
$self->{dbh}->do(<<SQL); $self->{dbh}->do(<<SQL);
CREATE TABLE IF NOT EXISTS Counters ( CREATE TABLE IF NOT EXISTS Counters (

View File

@ -39,7 +39,7 @@ sub begin {
$self->{pbot}->{logger}->log("Opening quotegrabs SQLite database: $self->{filename}\n"); $self->{pbot}->{logger}->log("Opening quotegrabs SQLite database: $self->{filename}\n");
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0 }) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, sqlite_unicode => 1 }) or die $DBI::errstr;
eval { eval {
$self->{dbh}->do(<< 'SQL'); $self->{dbh}->do(<< 'SQL');

View File

@ -77,7 +77,7 @@ SQL
sub dbi_begin { sub dbi_begin {
my ($self) = @_; my ($self) = @_;
eval { eval {
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1 }) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1 }) or die $DBI::errstr;
}; };
if ($@) { if ($@) {

View File

@ -121,8 +121,9 @@ sub load_questions {
return "Failed to load $filename"; return "Failed to load $filename";
}; };
local $/; local $/;
<$fh>; my $text = <$fh>;
close $fh; close $fh;
$text;
}; };
$self->{loaded_filename} = $filename; $self->{loaded_filename} = $filename;