More critic! Also use Encode instead of utf8::encode

This commit is contained in:
eleksir
2022-02-19 16:56:15 +03:00
parent 8bbb18bbc2
commit e31998ffcc
12 changed files with 112 additions and 64 deletions

View File

@@ -1,14 +1,13 @@
package Conf;
# Загружает конфиг
# Загружает конфиг файл
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Encode qw (encode decode);
use JSON::XS;
use JSON::XS ();
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -16,17 +15,26 @@ our @EXPORT_OK = qw (LoadConf);
sub LoadConf {
my $c = 'data/config.json';
open my $CH, '<', $c or die "[FATAL] No conf at $c: $OS_ERROR\n";
my $json;
my $CH;
unless (open $CH, '<', $c) {
die "[FATAL] No conf at $c: $OS_ERROR\n";
}
my $len = (stat $c) [7];
my $json;
my $readlen = read $CH, $json, $len;
$json = encode 'UTF-8', $json;
unless ($readlen) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
die "[FATAL] Unable to read $c: $OS_ERROR\n";
}
if ($readlen != $len) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
die "[FATAL] File $c is $len bytes on disk, but we read only $readlen bytes\n";
}
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
my $j = JSON::XS->new->utf8->relaxed;
return $j->decode ($json);

View File

@@ -1,17 +1,18 @@
package Fortune;
# Прокатят любые источники из fortune_mod. Текстовой файл с разделителем "\n%\n" мнжду фразами.
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use Encode qw (decode);
use English qw ( -no_match_vars );
use Carp qw (cluck croak);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use SQLite_File;
use SQLite_File ();
use Conf qw (LoadConf);
@@ -30,7 +31,7 @@ sub Seed () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir;
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR";
}
@@ -41,7 +42,7 @@ sub Seed () {
while (my $fortunefile = readdir $srcdirhandle) {
my $srcfile = sprintf '%s/%s', $srcdir, $fortunefile;
unless (-f $srcfile) {
unless (-e $srcfile) {
next;
}
@@ -77,7 +78,7 @@ sub Fortune () {
my $phrase = $array[irand ($#array + 1)];
# decode?
utf8::decode $phrase;
$phrase = decode 'UTF-8', $phrase;
untie @array;
return $phrase;
}

View File

@@ -1,17 +1,18 @@
package Friday;
# Данные модуля спёрты с https://raw.githubusercontent.com/isida/vi/master/data/friday.txt
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use Encode qw (decode);
use English qw ( -no_match_vars );
use Carp qw (cluck croak);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use SQLite_File;
use SQLite_File ();
use Conf qw (LoadConf);
@@ -34,7 +35,7 @@ sub Seed () {
for (0..$#dow) {
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$_];
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR\n";
}
@@ -87,7 +88,7 @@ sub Friday () {
};
my $phrase = $array[irand ($#array + 1)];
utf8::decode $phrase;
$phrase = decode 'UTF-8', $phrase;
untie @array;
return $phrase;
}

View File

@@ -1,15 +1,14 @@
package Karma;
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use CHI ();
use CHI::Driver::BerkeleyDB ();
use Log::Any qw ($log);
use Mojo::Util qw (trim);
@@ -38,7 +37,7 @@ sub KarmaSet (@) {
my $cache = CHI->new (
driver => 'BerkeleyDB',
root_dir => $cachedir,
namespace => __PACKAGE__ . '_' . $chatid
namespace => __PACKAGE__ . '_' . $chatid,
);
my $score = $cache->get ($phrase);
@@ -90,7 +89,7 @@ sub KarmaGet (@) {
my $cache = CHI->new (
driver => 'BerkeleyDB',
root_dir => $cachedir,
namespace => __PACKAGE__ . '_' . $chatid
namespace => __PACKAGE__ . '_' . $chatid,
);
my $score = $cache->get ($phrase);

View File

@@ -1,7 +1,7 @@
package Phrases;
# Общие модули - синтаксис, кодировки итд
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -12,12 +12,12 @@ use Clone qw (clone);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
# Чтобы "уж точно" использовать hiredis-биндинги, загрузим этот модуль перед Mojo::Redis
use Protocol::Redis::XS;
use Mojo::Redis;
use Mojo::IOLoop;
use Mojo::IOLoop::Signal;
use Protocol::Redis::XS ();
use Mojo::Redis ();
use Mojo::IOLoop ();
use Mojo::IOLoop::Signal ();
use Mojo::Util qw (trim);
use Data::Dumper;
use Data::Dumper qw (Dumper);
use Conf qw (LoadConf);
use Fortune qw (Fortune);
@@ -107,7 +107,7 @@ my $parse_message = sub {
$reply = KarmaSet (
$m->{chatid},
trim (substr $m->{message}, 0, -2),
substr $m->{message}, -2
substr $m->{message}, -2,
);
# Если у нас более одной строки - пусть с этим разбирается craniac, у него есть *мозги*
} else {
@@ -122,7 +122,7 @@ my $parse_message = sub {
$answer->{message} = $reply;
$self->json ($send_to)->notify (
$send_to => $answer
$send_to => $answer,
);
}
@@ -133,7 +133,7 @@ my $__signal_handler = sub {
my ($self, $name) = @_;
$log->info ("[INFO] Caught a signal $name");
if (defined $main::pidfile && -f $main::pidfile) {
if (defined $main::pidfile && -e $main::pidfile) {
unlink $main::pidfile;
}
@@ -146,7 +146,7 @@ sub RunPhrases {
$log->info ("[INFO] Connecting to $c->{server}, $c->{port}");
my $redis = Mojo::Redis->new (
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port}
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port},
);
$log->info ('[INFO] Registering connection-event callback');
@@ -163,11 +163,11 @@ sub RunPhrases {
my ($conn, $error) = @_;
$log->error ("[ERROR] Redis connection error: $error");
return;
}
},
);
return;
}
},
);
my $pubsub = $redis->pubsub;
@@ -178,13 +178,13 @@ sub RunPhrases {
$log->debug ("[DEBUG] Subscribing to $channel");
$sub->{$channel} = $pubsub->json ($channel)->listen (
$channel => sub { $parse_message->(@_); }
$channel => sub { $parse_message->(@_); },
);
}
Mojo::IOLoop::Signal->on (
TERM => $__signal_handler,
INT => $__signal_handler
INT => $__signal_handler,
);
do { Mojo::IOLoop->start } until Mojo::IOLoop->is_running;

View File

@@ -1,18 +1,19 @@
package Proverb;
# Пословицы и поговорки
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use Encode qw (decode);
use English qw ( -no_match_vars );
use Carp qw (cluck croak);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use Mojo::Util qw (trim);
use SQLite_File;
use SQLite_File ();
use Conf qw (LoadConf);
@@ -31,7 +32,7 @@ sub Seed () {
my $backingfile = sprintf '%s/proverb.sqlite', $dir;
if (-f $backingfile) {
if (-e $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR";
}
@@ -41,7 +42,7 @@ sub Seed () {
while (my $proverbfile = readdir $srcdirhandle) {
my $srcfile = sprintf '%s/%s', $srcdir, $proverbfile;
unless (-f $srcfile) {
unless (-e $srcfile) {
next;
}
@@ -79,7 +80,7 @@ sub Proverb () {
my $phrase = $array[irand ($#array + 1)];
# decode?
utf8::decode $phrase;
$phrase = decode 'UTF-8', $phrase;
untie @array;
return $phrase;
}